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"
32 #define PENDING_EXTENT_INSERT 0
33 #define PENDING_EXTENT_DELETE 1
34 #define PENDING_BACKREF_UPDATE 2
36 struct pending_extent_op {
41 struct btrfs_disk_key key;
45 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
46 struct btrfs_root *root,
47 u64 root_objectid, u64 generation,
48 u64 flags, struct btrfs_disk_key *key,
49 int level, struct btrfs_key *ins);
50 static int __free_extent(struct btrfs_trans_handle *trans,
51 struct btrfs_root *root,
52 u64 bytenr, u64 num_bytes, u64 parent,
53 u64 root_objectid, u64 owner_objectid,
54 u64 owner_offset, int refs_to_drop);
55 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
56 btrfs_root *extent_root);
57 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
58 btrfs_root *extent_root);
60 static int remove_sb_from_cache(struct btrfs_root *root,
61 struct btrfs_block_group_cache *cache)
67 struct extent_io_tree *free_space_cache;
69 free_space_cache = &root->fs_info->free_space_cache;
70 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
71 bytenr = btrfs_sb_offset(i);
72 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
73 cache->key.objectid, bytenr, 0,
74 &logical, &nr, &stripe_len);
77 clear_extent_dirty(free_space_cache, logical[nr],
78 logical[nr] + stripe_len - 1, GFP_NOFS);
85 static int cache_block_group(struct btrfs_root *root,
86 struct btrfs_block_group_cache *block_group)
88 struct btrfs_path *path;
91 struct extent_buffer *leaf;
92 struct extent_io_tree *free_space_cache;
100 root = root->fs_info->extent_root;
101 free_space_cache = &root->fs_info->free_space_cache;
103 if (block_group->cached)
106 path = btrfs_alloc_path();
111 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
116 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
121 leaf = path->nodes[0];
122 slot = path->slots[0];
123 if (slot >= btrfs_header_nritems(leaf)) {
124 ret = btrfs_next_leaf(root, path);
133 btrfs_item_key_to_cpu(leaf, &key, slot);
134 if (key.objectid < block_group->key.objectid) {
137 if (key.objectid >= block_group->key.objectid +
138 block_group->key.offset) {
142 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
143 key.type == BTRFS_METADATA_ITEM_KEY) {
144 if (key.objectid > last) {
145 hole_size = key.objectid - last;
146 set_extent_dirty(free_space_cache, last,
147 last + hole_size - 1,
150 if (key.type == BTRFS_METADATA_ITEM_KEY)
151 last = key.objectid + root->leafsize;
153 last = key.objectid + key.offset;
159 if (block_group->key.objectid +
160 block_group->key.offset > last) {
161 hole_size = block_group->key.objectid +
162 block_group->key.offset - last;
163 set_extent_dirty(free_space_cache, last,
164 last + hole_size - 1, GFP_NOFS);
166 remove_sb_from_cache(root, block_group);
167 block_group->cached = 1;
169 btrfs_free_path(path);
173 struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
177 struct extent_io_tree *block_group_cache;
178 struct btrfs_block_group_cache *block_group = NULL;
184 bytenr = max_t(u64, bytenr,
185 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
186 block_group_cache = &info->block_group_cache;
187 ret = find_first_extent_bit(block_group_cache,
188 bytenr, &start, &end,
189 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
194 ret = get_state_private(block_group_cache, start, &ptr);
198 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
202 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
206 struct extent_io_tree *block_group_cache;
207 struct btrfs_block_group_cache *block_group = NULL;
213 block_group_cache = &info->block_group_cache;
214 ret = find_first_extent_bit(block_group_cache,
215 bytenr, &start, &end,
216 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
221 ret = get_state_private(block_group_cache, start, &ptr);
225 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
226 if (block_group->key.objectid <= bytenr && bytenr <
227 block_group->key.objectid + block_group->key.offset)
232 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
234 return (cache->flags & bits) == bits;
237 static int noinline find_search_start(struct btrfs_root *root,
238 struct btrfs_block_group_cache **cache_ret,
239 u64 *start_ret, int num, int data)
242 struct btrfs_block_group_cache *cache = *cache_ret;
243 u64 last = *start_ret;
246 u64 search_start = *start_ret;
252 ret = cache_block_group(root, cache);
256 last = max(search_start, cache->key.objectid);
257 if (cache->ro || !block_group_bits(cache, data))
261 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
262 last, &start, &end, EXTENT_DIRTY);
267 start = max(last, start);
269 if (last - start < num) {
272 if (start + num > cache->key.objectid + cache->key.offset) {
280 cache = btrfs_lookup_block_group(root->fs_info, search_start);
282 printk("Unable to find block group for %llu\n",
283 (unsigned long long)search_start);
289 last = cache->key.objectid + cache->key.offset;
291 cache = btrfs_lookup_first_block_group(root->fs_info, last);
304 static int block_group_state_bits(u64 flags)
307 if (flags & BTRFS_BLOCK_GROUP_DATA)
308 bits |= BLOCK_GROUP_DATA;
309 if (flags & BTRFS_BLOCK_GROUP_METADATA)
310 bits |= BLOCK_GROUP_METADATA;
311 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
312 bits |= BLOCK_GROUP_SYSTEM;
316 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
317 struct btrfs_block_group_cache
318 *hint, u64 search_start,
321 struct btrfs_block_group_cache *cache;
322 struct extent_io_tree *block_group_cache;
323 struct btrfs_block_group_cache *found_group = NULL;
324 struct btrfs_fs_info *info = root->fs_info;
337 block_group_cache = &info->block_group_cache;
342 bit = block_group_state_bits(data);
345 struct btrfs_block_group_cache *shint;
346 shint = btrfs_lookup_block_group(info, search_start);
347 if (shint && !shint->ro && block_group_bits(shint, data)) {
348 used = btrfs_block_group_used(&shint->item);
349 if (used + shint->pinned <
350 div_factor(shint->key.offset, factor)) {
355 if (hint && !hint->ro && block_group_bits(hint, data)) {
356 used = btrfs_block_group_used(&hint->item);
357 if (used + hint->pinned <
358 div_factor(hint->key.offset, factor)) {
361 last = hint->key.objectid + hint->key.offset;
365 hint_last = max(hint->key.objectid, search_start);
367 hint_last = search_start;
373 ret = find_first_extent_bit(block_group_cache, last,
378 ret = get_state_private(block_group_cache, start, &ptr);
382 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
383 last = cache->key.objectid + cache->key.offset;
384 used = btrfs_block_group_used(&cache->item);
386 if (!cache->ro && block_group_bits(cache, data)) {
388 free_check = cache->key.offset;
390 free_check = div_factor(cache->key.offset,
393 if (used + cache->pinned < free_check) {
410 * Back reference rules. Back refs have three main goals:
412 * 1) differentiate between all holders of references to an extent so that
413 * when a reference is dropped we can make sure it was a valid reference
414 * before freeing the extent.
416 * 2) Provide enough information to quickly find the holders of an extent
417 * if we notice a given block is corrupted or bad.
419 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
420 * maintenance. This is actually the same as #2, but with a slightly
421 * different use case.
423 * There are two kinds of back refs. The implicit back refs is optimized
424 * for pointers in non-shared tree blocks. For a given pointer in a block,
425 * back refs of this kind provide information about the block's owner tree
426 * and the pointer's key. These information allow us to find the block by
427 * b-tree searching. The full back refs is for pointers in tree blocks not
428 * referenced by their owner trees. The location of tree block is recorded
429 * in the back refs. Actually the full back refs is generic, and can be
430 * used in all cases the implicit back refs is used. The major shortcoming
431 * of the full back refs is its overhead. Every time a tree block gets
432 * COWed, we have to update back refs entry for all pointers in it.
434 * For a newly allocated tree block, we use implicit back refs for
435 * pointers in it. This means most tree related operations only involve
436 * implicit back refs. For a tree block created in old transaction, the
437 * only way to drop a reference to it is COW it. So we can detect the
438 * event that tree block loses its owner tree's reference and do the
439 * back refs conversion.
441 * When a tree block is COW'd through a tree, there are four cases:
443 * The reference count of the block is one and the tree is the block's
444 * owner tree. Nothing to do in this case.
446 * The reference count of the block is one and the tree is not the
447 * block's owner tree. In this case, full back refs is used for pointers
448 * in the block. Remove these full back refs, add implicit back refs for
449 * every pointers in the new block.
451 * The reference count of the block is greater than one and the tree is
452 * the block's owner tree. In this case, implicit back refs is used for
453 * pointers in the block. Add full back refs for every pointers in the
454 * block, increase lower level extents' reference counts. The original
455 * implicit back refs are entailed to the new block.
457 * The reference count of the block is greater than one and the tree is
458 * not the block's owner tree. Add implicit back refs for every pointer in
459 * the new block, increase lower level extents' reference count.
461 * Back Reference Key composing:
463 * The key objectid corresponds to the first byte in the extent,
464 * The key type is used to differentiate between types of back refs.
465 * There are different meanings of the key offset for different types
468 * File extents can be referenced by:
470 * - multiple snapshots, subvolumes, or different generations in one subvol
471 * - different files inside a single subvolume
472 * - different offsets inside a file (bookend extents in file.c)
474 * The extent ref structure for the implicit back refs has fields for:
476 * - Objectid of the subvolume root
477 * - objectid of the file holding the reference
478 * - original offset in the file
479 * - how many bookend extents
481 * The key offset for the implicit back refs is hash of the first
484 * The extent ref structure for the full back refs has field for:
486 * - number of pointers in the tree leaf
488 * The key offset for the implicit back refs is the first byte of
491 * When a file extent is allocated, The implicit back refs is used.
492 * the fields are filled in:
494 * (root_key.objectid, inode objectid, offset in file, 1)
496 * When a file extent is removed file truncation, we find the
497 * corresponding implicit back refs and check the following fields:
499 * (btrfs_header_owner(leaf), inode objectid, offset in file)
501 * Btree extents can be referenced by:
503 * - Different subvolumes
505 * Both the implicit back refs and the full back refs for tree blocks
506 * only consist of key. The key offset for the implicit back refs is
507 * objectid of block's owner tree. The key offset for the full back refs
508 * is the first byte of parent block.
510 * When implicit back refs is used, information about the lowest key and
511 * level of the tree block are required. These information are stored in
512 * tree block info structure.
515 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
516 static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
517 struct btrfs_root *root,
518 struct btrfs_path *path,
519 u64 owner, u32 extra_size)
521 struct btrfs_extent_item *item;
522 struct btrfs_extent_item_v0 *ei0;
523 struct btrfs_extent_ref_v0 *ref0;
524 struct btrfs_tree_block_info *bi;
525 struct extent_buffer *leaf;
526 struct btrfs_key key;
527 struct btrfs_key found_key;
528 u32 new_size = sizeof(*item);
532 leaf = path->nodes[0];
533 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
535 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
536 ei0 = btrfs_item_ptr(leaf, path->slots[0],
537 struct btrfs_extent_item_v0);
538 refs = btrfs_extent_refs_v0(leaf, ei0);
540 if (owner == (u64)-1) {
542 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
543 ret = btrfs_next_leaf(root, path);
547 leaf = path->nodes[0];
549 btrfs_item_key_to_cpu(leaf, &found_key,
551 BUG_ON(key.objectid != found_key.objectid);
552 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
556 ref0 = btrfs_item_ptr(leaf, path->slots[0],
557 struct btrfs_extent_ref_v0);
558 owner = btrfs_ref_objectid_v0(leaf, ref0);
562 btrfs_release_path(root, path);
564 if (owner < BTRFS_FIRST_FREE_OBJECTID)
565 new_size += sizeof(*bi);
567 new_size -= sizeof(*ei0);
568 ret = btrfs_search_slot(trans, root, &key, path, new_size, 1);
573 ret = btrfs_extend_item(trans, root, path, new_size);
576 leaf = path->nodes[0];
577 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
578 btrfs_set_extent_refs(leaf, item, refs);
579 /* FIXME: get real generation */
580 btrfs_set_extent_generation(leaf, item, 0);
581 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
582 btrfs_set_extent_flags(leaf, item,
583 BTRFS_EXTENT_FLAG_TREE_BLOCK |
584 BTRFS_BLOCK_FLAG_FULL_BACKREF);
585 bi = (struct btrfs_tree_block_info *)(item + 1);
586 /* FIXME: get first key of the block */
587 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
588 btrfs_set_tree_block_level(leaf, bi, (int)owner);
590 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
592 btrfs_mark_buffer_dirty(leaf);
597 static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
599 u32 high_crc = ~(u32)0;
600 u32 low_crc = ~(u32)0;
603 lenum = cpu_to_le64(root_objectid);
604 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
605 lenum = cpu_to_le64(owner);
606 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
607 lenum = cpu_to_le64(offset);
608 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
610 return ((u64)high_crc << 31) ^ (u64)low_crc;
613 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
614 struct btrfs_extent_data_ref *ref)
616 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
617 btrfs_extent_data_ref_objectid(leaf, ref),
618 btrfs_extent_data_ref_offset(leaf, ref));
621 static int match_extent_data_ref(struct extent_buffer *leaf,
622 struct btrfs_extent_data_ref *ref,
623 u64 root_objectid, u64 owner, u64 offset)
625 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
626 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
627 btrfs_extent_data_ref_offset(leaf, ref) != offset)
632 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
633 struct btrfs_root *root,
634 struct btrfs_path *path,
635 u64 bytenr, u64 parent,
637 u64 owner, u64 offset)
639 struct btrfs_key key;
640 struct btrfs_extent_data_ref *ref;
641 struct extent_buffer *leaf;
647 key.objectid = bytenr;
649 key.type = BTRFS_SHARED_DATA_REF_KEY;
652 key.type = BTRFS_EXTENT_DATA_REF_KEY;
653 key.offset = hash_extent_data_ref(root_objectid,
658 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
667 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
668 key.type = BTRFS_EXTENT_REF_V0_KEY;
669 btrfs_release_path(root, path);
670 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
681 leaf = path->nodes[0];
682 nritems = btrfs_header_nritems(leaf);
684 if (path->slots[0] >= nritems) {
685 ret = btrfs_next_leaf(root, path);
691 leaf = path->nodes[0];
692 nritems = btrfs_header_nritems(leaf);
696 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
697 if (key.objectid != bytenr ||
698 key.type != BTRFS_EXTENT_DATA_REF_KEY)
701 ref = btrfs_item_ptr(leaf, path->slots[0],
702 struct btrfs_extent_data_ref);
704 if (match_extent_data_ref(leaf, ref, root_objectid,
707 btrfs_release_path(root, path);
719 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
720 struct btrfs_root *root,
721 struct btrfs_path *path,
722 u64 bytenr, u64 parent,
723 u64 root_objectid, u64 owner,
724 u64 offset, int refs_to_add)
726 struct btrfs_key key;
727 struct extent_buffer *leaf;
732 key.objectid = bytenr;
734 key.type = BTRFS_SHARED_DATA_REF_KEY;
736 size = sizeof(struct btrfs_shared_data_ref);
738 key.type = BTRFS_EXTENT_DATA_REF_KEY;
739 key.offset = hash_extent_data_ref(root_objectid,
741 size = sizeof(struct btrfs_extent_data_ref);
744 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
745 if (ret && ret != -EEXIST)
748 leaf = path->nodes[0];
750 struct btrfs_shared_data_ref *ref;
751 ref = btrfs_item_ptr(leaf, path->slots[0],
752 struct btrfs_shared_data_ref);
754 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
756 num_refs = btrfs_shared_data_ref_count(leaf, ref);
757 num_refs += refs_to_add;
758 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
761 struct btrfs_extent_data_ref *ref;
762 while (ret == -EEXIST) {
763 ref = btrfs_item_ptr(leaf, path->slots[0],
764 struct btrfs_extent_data_ref);
765 if (match_extent_data_ref(leaf, ref, root_objectid,
768 btrfs_release_path(root, path);
771 ret = btrfs_insert_empty_item(trans, root, path, &key,
773 if (ret && ret != -EEXIST)
776 leaf = path->nodes[0];
778 ref = btrfs_item_ptr(leaf, path->slots[0],
779 struct btrfs_extent_data_ref);
781 btrfs_set_extent_data_ref_root(leaf, ref,
783 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
784 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
785 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
787 num_refs = btrfs_extent_data_ref_count(leaf, ref);
788 num_refs += refs_to_add;
789 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
792 btrfs_mark_buffer_dirty(leaf);
795 btrfs_release_path(root, path);
799 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
800 struct btrfs_root *root,
801 struct btrfs_path *path,
804 struct btrfs_key key;
805 struct btrfs_extent_data_ref *ref1 = NULL;
806 struct btrfs_shared_data_ref *ref2 = NULL;
807 struct extent_buffer *leaf;
811 leaf = path->nodes[0];
812 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
814 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
815 ref1 = btrfs_item_ptr(leaf, path->slots[0],
816 struct btrfs_extent_data_ref);
817 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
818 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
819 ref2 = btrfs_item_ptr(leaf, path->slots[0],
820 struct btrfs_shared_data_ref);
821 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
822 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
823 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
824 struct btrfs_extent_ref_v0 *ref0;
825 ref0 = btrfs_item_ptr(leaf, path->slots[0],
826 struct btrfs_extent_ref_v0);
827 num_refs = btrfs_ref_count_v0(leaf, ref0);
833 BUG_ON(num_refs < refs_to_drop);
834 num_refs -= refs_to_drop;
837 ret = btrfs_del_item(trans, root, path);
839 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
840 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
841 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
842 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
843 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
845 struct btrfs_extent_ref_v0 *ref0;
846 ref0 = btrfs_item_ptr(leaf, path->slots[0],
847 struct btrfs_extent_ref_v0);
848 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
851 btrfs_mark_buffer_dirty(leaf);
856 static noinline u32 extent_data_ref_count(struct btrfs_root *root,
857 struct btrfs_path *path,
858 struct btrfs_extent_inline_ref *iref)
860 struct btrfs_key key;
861 struct extent_buffer *leaf;
862 struct btrfs_extent_data_ref *ref1;
863 struct btrfs_shared_data_ref *ref2;
866 leaf = path->nodes[0];
867 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
869 if (btrfs_extent_inline_ref_type(leaf, iref) ==
870 BTRFS_EXTENT_DATA_REF_KEY) {
871 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
872 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
874 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
875 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
877 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
878 ref1 = btrfs_item_ptr(leaf, path->slots[0],
879 struct btrfs_extent_data_ref);
880 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
881 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
882 ref2 = btrfs_item_ptr(leaf, path->slots[0],
883 struct btrfs_shared_data_ref);
884 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
885 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
886 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
887 struct btrfs_extent_ref_v0 *ref0;
888 ref0 = btrfs_item_ptr(leaf, path->slots[0],
889 struct btrfs_extent_ref_v0);
890 num_refs = btrfs_ref_count_v0(leaf, ref0);
898 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
899 struct btrfs_root *root,
900 struct btrfs_path *path,
901 u64 bytenr, u64 parent,
904 struct btrfs_key key;
907 key.objectid = bytenr;
909 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
912 key.type = BTRFS_TREE_BLOCK_REF_KEY;
913 key.offset = root_objectid;
916 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
919 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
920 if (ret == -ENOENT && parent) {
921 btrfs_release_path(root, path);
922 key.type = BTRFS_EXTENT_REF_V0_KEY;
923 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
931 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
932 struct btrfs_root *root,
933 struct btrfs_path *path,
934 u64 bytenr, u64 parent,
937 struct btrfs_key key;
940 key.objectid = bytenr;
942 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
945 key.type = BTRFS_TREE_BLOCK_REF_KEY;
946 key.offset = root_objectid;
949 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
951 btrfs_release_path(root, path);
955 static inline int extent_ref_type(u64 parent, u64 owner)
958 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
960 type = BTRFS_SHARED_BLOCK_REF_KEY;
962 type = BTRFS_TREE_BLOCK_REF_KEY;
965 type = BTRFS_SHARED_DATA_REF_KEY;
967 type = BTRFS_EXTENT_DATA_REF_KEY;
972 static int find_next_key(struct btrfs_path *path, struct btrfs_key *key)
976 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
977 if (!path->nodes[level])
979 if (path->slots[level] + 1 >=
980 btrfs_header_nritems(path->nodes[level]))
983 btrfs_item_key_to_cpu(path->nodes[level], key,
984 path->slots[level] + 1);
986 btrfs_node_key_to_cpu(path->nodes[level], key,
987 path->slots[level] + 1);
993 static int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
994 struct btrfs_root *root,
995 struct btrfs_path *path,
996 struct btrfs_extent_inline_ref **ref_ret,
997 u64 bytenr, u64 num_bytes,
998 u64 parent, u64 root_objectid,
999 u64 owner, u64 offset, int insert)
1001 struct btrfs_key key;
1002 struct extent_buffer *leaf;
1003 struct btrfs_extent_item *ei;
1004 struct btrfs_extent_inline_ref *iref;
1014 int skinny_metadata =
1015 btrfs_fs_incompat(root->fs_info,
1016 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
1018 key.objectid = bytenr;
1019 key.type = BTRFS_EXTENT_ITEM_KEY;
1020 key.offset = num_bytes;
1022 want = extent_ref_type(parent, owner);
1024 extra_size = btrfs_extent_inline_ref_size(want);
1028 if (owner < BTRFS_FIRST_FREE_OBJECTID && skinny_metadata) {
1029 skinny_metadata = 1;
1030 key.type = BTRFS_METADATA_ITEM_KEY;
1032 } else if (skinny_metadata) {
1033 skinny_metadata = 0;
1037 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1044 * We may be a newly converted file system which still has the old fat
1045 * extent entries for metadata, so try and see if we have one of those.
1047 if (ret > 0 && skinny_metadata) {
1048 skinny_metadata = 0;
1049 if (path->slots[0]) {
1051 btrfs_item_key_to_cpu(path->nodes[0], &key,
1053 if (key.objectid == bytenr &&
1054 key.type == BTRFS_EXTENT_ITEM_KEY &&
1055 key.offset == num_bytes)
1059 key.type = BTRFS_EXTENT_ITEM_KEY;
1060 key.offset = num_bytes;
1066 printf("Failed to find [%llu, %u, %llu]\n", key.objectid, key.type, key.offset);
1072 leaf = path->nodes[0];
1073 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1074 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1075 if (item_size < sizeof(*ei)) {
1080 ret = convert_extent_item_v0(trans, root, path, owner,
1086 leaf = path->nodes[0];
1087 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1090 if (item_size < sizeof(*ei)) {
1091 printf("Size is %u, needs to be %u, slot %d\n",
1092 (unsigned)item_size,
1093 (unsigned)sizeof(*ei), path->slots[0]);
1094 btrfs_print_leaf(root, leaf);
1097 BUG_ON(item_size < sizeof(*ei));
1099 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1100 flags = btrfs_extent_flags(leaf, ei);
1102 ptr = (unsigned long)(ei + 1);
1103 end = (unsigned long)ei + item_size;
1105 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
1106 ptr += sizeof(struct btrfs_tree_block_info);
1108 } else if (!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
1109 if (!(flags & BTRFS_EXTENT_FLAG_DATA)) {
1120 iref = (struct btrfs_extent_inline_ref *)ptr;
1121 type = btrfs_extent_inline_ref_type(leaf, iref);
1125 ptr += btrfs_extent_inline_ref_size(type);
1129 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1130 struct btrfs_extent_data_ref *dref;
1131 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1132 if (match_extent_data_ref(leaf, dref, root_objectid,
1137 if (hash_extent_data_ref_item(leaf, dref) <
1138 hash_extent_data_ref(root_objectid, owner, offset))
1142 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1144 if (parent == ref_offset) {
1148 if (ref_offset < parent)
1151 if (root_objectid == ref_offset) {
1155 if (ref_offset < root_objectid)
1159 ptr += btrfs_extent_inline_ref_size(type);
1161 if (err == -ENOENT && insert) {
1162 if (item_size + extra_size >=
1163 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1168 * To add new inline back ref, we have to make sure
1169 * there is no corresponding back ref item.
1170 * For simplicity, we just do not add new inline back
1171 * ref if there is any back ref item.
1173 if (find_next_key(path, &key) == 0 && key.objectid == bytenr &&
1174 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
1179 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1184 static int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1185 struct btrfs_root *root,
1186 struct btrfs_path *path,
1187 struct btrfs_extent_inline_ref *iref,
1188 u64 parent, u64 root_objectid,
1189 u64 owner, u64 offset, int refs_to_add)
1191 struct extent_buffer *leaf;
1192 struct btrfs_extent_item *ei;
1195 unsigned long item_offset;
1201 leaf = path->nodes[0];
1202 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1203 item_offset = (unsigned long)iref - (unsigned long)ei;
1205 type = extent_ref_type(parent, owner);
1206 size = btrfs_extent_inline_ref_size(type);
1208 ret = btrfs_extend_item(trans, root, path, size);
1211 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1212 refs = btrfs_extent_refs(leaf, ei);
1213 refs += refs_to_add;
1214 btrfs_set_extent_refs(leaf, ei, refs);
1216 ptr = (unsigned long)ei + item_offset;
1217 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1218 if (ptr < end - size)
1219 memmove_extent_buffer(leaf, ptr + size, ptr,
1222 iref = (struct btrfs_extent_inline_ref *)ptr;
1223 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1224 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1225 struct btrfs_extent_data_ref *dref;
1226 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1227 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1228 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1229 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1230 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1231 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1232 struct btrfs_shared_data_ref *sref;
1233 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1234 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1235 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1236 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1237 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1239 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1241 btrfs_mark_buffer_dirty(leaf);
1245 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1246 struct btrfs_root *root,
1247 struct btrfs_path *path,
1248 struct btrfs_extent_inline_ref **ref_ret,
1249 u64 bytenr, u64 num_bytes, u64 parent,
1250 u64 root_objectid, u64 owner, u64 offset)
1254 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1255 bytenr, num_bytes, parent,
1256 root_objectid, owner, offset, 0);
1260 btrfs_release_path(root, path);
1263 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1264 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1267 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1268 root_objectid, owner, offset);
1273 static int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1274 struct btrfs_root *root,
1275 struct btrfs_path *path,
1276 struct btrfs_extent_inline_ref *iref,
1279 struct extent_buffer *leaf;
1280 struct btrfs_extent_item *ei;
1281 struct btrfs_extent_data_ref *dref = NULL;
1282 struct btrfs_shared_data_ref *sref = NULL;
1291 leaf = path->nodes[0];
1292 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1293 refs = btrfs_extent_refs(leaf, ei);
1294 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1295 refs += refs_to_mod;
1296 btrfs_set_extent_refs(leaf, ei, refs);
1298 type = btrfs_extent_inline_ref_type(leaf, iref);
1300 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1301 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1302 refs = btrfs_extent_data_ref_count(leaf, dref);
1303 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1304 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1305 refs = btrfs_shared_data_ref_count(leaf, sref);
1308 BUG_ON(refs_to_mod != -1);
1311 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1312 refs += refs_to_mod;
1315 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1316 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1318 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1320 size = btrfs_extent_inline_ref_size(type);
1321 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1322 ptr = (unsigned long)iref;
1323 end = (unsigned long)ei + item_size;
1324 if (ptr + size < end)
1325 memmove_extent_buffer(leaf, ptr, ptr + size,
1328 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1331 btrfs_mark_buffer_dirty(leaf);
1335 static int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1336 struct btrfs_root *root,
1337 struct btrfs_path *path,
1338 u64 bytenr, u64 num_bytes, u64 parent,
1339 u64 root_objectid, u64 owner,
1340 u64 offset, int refs_to_add)
1342 struct btrfs_extent_inline_ref *iref;
1345 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1346 bytenr, num_bytes, parent,
1347 root_objectid, owner, offset, 1);
1349 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1350 ret = update_inline_extent_backref(trans, root, path, iref,
1352 } else if (ret == -ENOENT) {
1353 ret = setup_inline_extent_backref(trans, root, path, iref,
1354 parent, root_objectid,
1355 owner, offset, refs_to_add);
1360 static int insert_extent_backref(struct btrfs_trans_handle *trans,
1361 struct btrfs_root *root,
1362 struct btrfs_path *path,
1363 u64 bytenr, u64 parent, u64 root_objectid,
1364 u64 owner, u64 offset, int refs_to_add)
1368 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
1369 ret = insert_extent_data_ref(trans, root, path, bytenr,
1370 parent, root_objectid,
1371 owner, offset, refs_to_add);
1373 BUG_ON(refs_to_add != 1);
1374 ret = insert_tree_block_ref(trans, root, path, bytenr,
1375 parent, root_objectid);
1380 static int remove_extent_backref(struct btrfs_trans_handle *trans,
1381 struct btrfs_root *root,
1382 struct btrfs_path *path,
1383 struct btrfs_extent_inline_ref *iref,
1384 int refs_to_drop, int is_data)
1388 BUG_ON(!is_data && refs_to_drop != 1);
1390 ret = update_inline_extent_backref(trans, root, path, iref,
1392 } else if (is_data) {
1393 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1395 ret = btrfs_del_item(trans, root, path);
1400 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1401 struct btrfs_root *root,
1402 u64 bytenr, u64 num_bytes, u64 parent,
1403 u64 root_objectid, u64 owner, u64 offset)
1405 struct btrfs_path *path;
1406 struct extent_buffer *leaf;
1407 struct btrfs_extent_item *item;
1412 path = btrfs_alloc_path();
1417 path->leave_spinning = 1;
1419 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1420 path, bytenr, num_bytes, parent,
1421 root_objectid, owner, offset, 1);
1425 if (ret != -EAGAIN) {
1430 leaf = path->nodes[0];
1431 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1432 refs = btrfs_extent_refs(leaf, item);
1433 btrfs_set_extent_refs(leaf, item, refs + 1);
1435 btrfs_mark_buffer_dirty(leaf);
1436 btrfs_release_path(root->fs_info->extent_root, path);
1439 path->leave_spinning = 1;
1441 /* now insert the actual backref */
1442 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1443 path, bytenr, parent, root_objectid,
1448 btrfs_free_path(path);
1449 finish_current_insert(trans, root->fs_info->extent_root);
1450 del_pending_extents(trans, root->fs_info->extent_root);
1455 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1456 struct btrfs_root *root)
1458 finish_current_insert(trans, root->fs_info->extent_root);
1459 del_pending_extents(trans, root->fs_info->extent_root);
1463 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
1464 struct btrfs_root *root, u64 bytenr,
1465 u64 offset, int metadata, u64 *refs, u64 *flags)
1467 struct btrfs_path *path;
1469 struct btrfs_key key;
1470 struct extent_buffer *l;
1471 struct btrfs_extent_item *item;
1477 !btrfs_fs_incompat(root->fs_info,
1478 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA)) {
1479 offset = root->leafsize;
1483 path = btrfs_alloc_path();
1486 key.objectid = bytenr;
1487 key.offset = offset;
1489 key.type = BTRFS_METADATA_ITEM_KEY;
1491 key.type = BTRFS_EXTENT_ITEM_KEY;
1494 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1500 * Deal with the fact that we may have mixed SKINNY and normal refs. If
1501 * we didn't find what we wanted check and see if we have a normal ref
1502 * right next to us, or re-search if we are on the edge of the leaf just
1505 if (ret > 0 && metadata) {
1508 btrfs_item_key_to_cpu(path->nodes[0], &key,
1510 if (key.objectid == bytenr &&
1511 key.type == BTRFS_METADATA_ITEM_KEY)
1516 btrfs_release_path(root, path);
1517 key.type = BTRFS_EXTENT_ITEM_KEY;
1518 key.offset = root->leafsize;
1530 item_size = btrfs_item_size_nr(l, path->slots[0]);
1531 if (item_size >= sizeof(*item)) {
1532 item = btrfs_item_ptr(l, path->slots[0],
1533 struct btrfs_extent_item);
1534 num_refs = btrfs_extent_refs(l, item);
1535 extent_flags = btrfs_extent_flags(l, item);
1537 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1538 struct btrfs_extent_item_v0 *ei0;
1539 BUG_ON(item_size != sizeof(*ei0));
1540 ei0 = btrfs_item_ptr(l, path->slots[0],
1541 struct btrfs_extent_item_v0);
1542 num_refs = btrfs_extent_refs_v0(l, ei0);
1543 /* FIXME: this isn't correct for data */
1544 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
1549 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1553 *flags = extent_flags;
1555 btrfs_free_path(path);
1559 int btrfs_set_block_flags(struct btrfs_trans_handle *trans,
1560 struct btrfs_root *root,
1561 u64 bytenr, int level, u64 flags)
1563 struct btrfs_path *path;
1565 struct btrfs_key key;
1566 struct extent_buffer *l;
1567 struct btrfs_extent_item *item;
1569 int skinny_metadata =
1570 btrfs_fs_incompat(root->fs_info,
1571 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
1573 path = btrfs_alloc_path();
1576 key.objectid = bytenr;
1577 if (skinny_metadata) {
1579 key.type = BTRFS_METADATA_ITEM_KEY;
1581 key.offset = root->leafsize;
1582 key.type = BTRFS_EXTENT_ITEM_KEY;
1586 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1591 if (ret > 0 && skinny_metadata) {
1592 skinny_metadata = 0;
1593 if (path->slots[0]--) {
1595 btrfs_item_key_to_cpu(path->nodes[0], &key,
1597 if (key.objectid == bytenr &&
1598 key.offset == root->leafsize &&
1599 key.type == BTRFS_EXTENT_ITEM_KEY)
1603 btrfs_release_path(root, path);
1604 key.offset = root->leafsize;
1605 key.type = BTRFS_EXTENT_ITEM_KEY;
1611 btrfs_print_leaf(root, path->nodes[0]);
1612 printk("failed to find block number %Lu\n",
1613 (unsigned long long)bytenr);
1617 item_size = btrfs_item_size_nr(l, path->slots[0]);
1618 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1619 if (item_size < sizeof(*item)) {
1620 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1626 item_size = btrfs_item_size_nr(l, path->slots[0]);
1629 BUG_ON(item_size < sizeof(*item));
1630 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1631 flags |= btrfs_extent_flags(l, item);
1632 btrfs_set_extent_flags(l, item, flags);
1634 btrfs_free_path(path);
1635 finish_current_insert(trans, root->fs_info->extent_root);
1636 del_pending_extents(trans, root->fs_info->extent_root);
1640 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
1641 struct btrfs_root *root,
1642 struct extent_buffer *buf,
1643 int record_parent, int inc)
1650 struct btrfs_key key;
1651 struct btrfs_file_extent_item *fi;
1655 int (*process_func)(struct btrfs_trans_handle *trans,
1656 struct btrfs_root *root,
1657 u64, u64, u64, u64, u64, u64);
1659 ref_root = btrfs_header_owner(buf);
1660 nritems = btrfs_header_nritems(buf);
1661 level = btrfs_header_level(buf);
1663 if (!root->ref_cows && level == 0)
1667 process_func = btrfs_inc_extent_ref;
1669 process_func = btrfs_free_extent;
1672 parent = buf->start;
1676 for (i = 0; i < nritems; i++) {
1679 btrfs_item_key_to_cpu(buf, &key, i);
1680 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1682 fi = btrfs_item_ptr(buf, i,
1683 struct btrfs_file_extent_item);
1684 if (btrfs_file_extent_type(buf, fi) ==
1685 BTRFS_FILE_EXTENT_INLINE)
1687 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1691 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
1692 key.offset -= btrfs_file_extent_offset(buf, fi);
1693 ret = process_func(trans, root, bytenr, num_bytes,
1694 parent, ref_root, key.objectid,
1701 bytenr = btrfs_node_blockptr(buf, i);
1702 num_bytes = btrfs_level_size(root, level - 1);
1703 ret = process_func(trans, root, bytenr, num_bytes,
1704 parent, ref_root, level - 1, 0);
1717 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1718 struct extent_buffer *buf, int record_parent)
1720 return __btrfs_mod_ref(trans, root, buf, record_parent, 1);
1723 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1724 struct extent_buffer *buf, int record_parent)
1726 return __btrfs_mod_ref(trans, root, buf, record_parent, 0);
1729 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1730 struct btrfs_root *root,
1731 struct btrfs_path *path,
1732 struct btrfs_block_group_cache *cache)
1736 struct btrfs_root *extent_root = root->fs_info->extent_root;
1738 struct extent_buffer *leaf;
1740 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1745 leaf = path->nodes[0];
1746 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1747 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1748 btrfs_mark_buffer_dirty(leaf);
1749 btrfs_release_path(extent_root, path);
1751 finish_current_insert(trans, extent_root);
1752 pending_ret = del_pending_extents(trans, extent_root);
1761 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1762 struct btrfs_root *root)
1764 struct extent_io_tree *block_group_cache;
1765 struct btrfs_block_group_cache *cache;
1767 struct btrfs_path *path;
1773 block_group_cache = &root->fs_info->block_group_cache;
1774 path = btrfs_alloc_path();
1779 ret = find_first_extent_bit(block_group_cache, last,
1780 &start, &end, BLOCK_GROUP_DIRTY);
1789 ret = get_state_private(block_group_cache, start, &ptr);
1792 clear_extent_bits(block_group_cache, start, end,
1793 BLOCK_GROUP_DIRTY, GFP_NOFS);
1795 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
1796 ret = write_one_cache_group(trans, root, path, cache);
1798 btrfs_free_path(path);
1802 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
1805 struct list_head *head = &info->space_info;
1806 struct list_head *cur;
1807 struct btrfs_space_info *found;
1808 list_for_each(cur, head) {
1809 found = list_entry(cur, struct btrfs_space_info, list);
1810 if (found->flags & flags)
1817 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1818 u64 total_bytes, u64 bytes_used,
1819 struct btrfs_space_info **space_info)
1821 struct btrfs_space_info *found;
1823 found = __find_space_info(info, flags);
1825 found->total_bytes += total_bytes;
1826 found->bytes_used += bytes_used;
1827 if (found->total_bytes < found->bytes_used) {
1828 fprintf(stderr, "warning, bad space info total_bytes "
1830 (unsigned long long)found->total_bytes,
1831 (unsigned long long)found->bytes_used);
1833 *space_info = found;
1836 found = kmalloc(sizeof(*found), GFP_NOFS);
1840 list_add(&found->list, &info->space_info);
1841 found->flags = flags;
1842 found->total_bytes = total_bytes;
1843 found->bytes_used = bytes_used;
1844 found->bytes_pinned = 0;
1846 *space_info = found;
1851 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1853 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1854 BTRFS_BLOCK_GROUP_RAID1 |
1855 BTRFS_BLOCK_GROUP_RAID10 |
1856 BTRFS_BLOCK_GROUP_RAID5 |
1857 BTRFS_BLOCK_GROUP_RAID6 |
1858 BTRFS_BLOCK_GROUP_DUP);
1860 if (flags & BTRFS_BLOCK_GROUP_DATA)
1861 fs_info->avail_data_alloc_bits |= extra_flags;
1862 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1863 fs_info->avail_metadata_alloc_bits |= extra_flags;
1864 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1865 fs_info->avail_system_alloc_bits |= extra_flags;
1869 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1870 struct btrfs_root *extent_root, u64 alloc_bytes,
1873 struct btrfs_space_info *space_info;
1879 space_info = __find_space_info(extent_root->fs_info, flags);
1881 ret = update_space_info(extent_root->fs_info, flags,
1885 BUG_ON(!space_info);
1887 if (space_info->full)
1890 thresh = div_factor(space_info->total_bytes, 7);
1891 if ((space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
1895 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes,
1897 if (ret == -ENOSPC) {
1898 space_info->full = 1;
1904 ret = btrfs_make_block_group(trans, extent_root, 0, space_info->flags,
1905 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
1910 static int update_block_group(struct btrfs_trans_handle *trans,
1911 struct btrfs_root *root,
1912 u64 bytenr, u64 num_bytes, int alloc,
1915 struct btrfs_block_group_cache *cache;
1916 struct btrfs_fs_info *info = root->fs_info;
1917 u64 total = num_bytes;
1923 /* block accounting for super block */
1924 old_val = btrfs_super_bytes_used(info->super_copy);
1926 old_val += num_bytes;
1928 old_val -= num_bytes;
1929 btrfs_set_super_bytes_used(info->super_copy, old_val);
1931 /* block accounting for root item */
1932 old_val = btrfs_root_used(&root->root_item);
1934 old_val += num_bytes;
1936 old_val -= num_bytes;
1937 btrfs_set_root_used(&root->root_item, old_val);
1940 cache = btrfs_lookup_block_group(info, bytenr);
1944 byte_in_group = bytenr - cache->key.objectid;
1945 WARN_ON(byte_in_group > cache->key.offset);
1946 start = cache->key.objectid;
1947 end = start + cache->key.offset - 1;
1948 set_extent_bits(&info->block_group_cache, start, end,
1949 BLOCK_GROUP_DIRTY, GFP_NOFS);
1951 old_val = btrfs_block_group_used(&cache->item);
1952 num_bytes = min(total, cache->key.offset - byte_in_group);
1955 old_val += num_bytes;
1956 cache->space_info->bytes_used += num_bytes;
1958 old_val -= num_bytes;
1959 cache->space_info->bytes_used -= num_bytes;
1961 set_extent_dirty(&info->free_space_cache,
1962 bytenr, bytenr + num_bytes - 1,
1966 btrfs_set_block_group_used(&cache->item, old_val);
1968 bytenr += num_bytes;
1973 static int update_pinned_extents(struct btrfs_root *root,
1974 u64 bytenr, u64 num, int pin)
1977 struct btrfs_block_group_cache *cache;
1978 struct btrfs_fs_info *fs_info = root->fs_info;
1981 set_extent_dirty(&fs_info->pinned_extents,
1982 bytenr, bytenr + num - 1, GFP_NOFS);
1984 clear_extent_dirty(&fs_info->pinned_extents,
1985 bytenr, bytenr + num - 1, GFP_NOFS);
1988 cache = btrfs_lookup_block_group(fs_info, bytenr);
1990 len = min((u64)root->sectorsize, num);
1994 len = min(num, cache->key.offset -
1995 (bytenr - cache->key.objectid));
1997 cache->pinned += len;
1998 cache->space_info->bytes_pinned += len;
1999 fs_info->total_pinned += len;
2001 cache->pinned -= len;
2002 cache->space_info->bytes_pinned -= len;
2003 fs_info->total_pinned -= len;
2012 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
2017 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
2021 ret = find_first_extent_bit(pinned_extents, last,
2022 &start, &end, EXTENT_DIRTY);
2025 set_extent_dirty(copy, start, end, GFP_NOFS);
2031 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2032 struct btrfs_root *root,
2033 struct extent_io_tree *unpin)
2038 struct extent_io_tree *free_space_cache;
2039 free_space_cache = &root->fs_info->free_space_cache;
2042 ret = find_first_extent_bit(unpin, 0, &start, &end,
2046 update_pinned_extents(root, start, end + 1 - start, 0);
2047 clear_extent_dirty(unpin, start, end, GFP_NOFS);
2048 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
2053 static int extent_root_pending_ops(struct btrfs_fs_info *info)
2059 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
2060 &end, EXTENT_LOCKED);
2062 ret = find_first_extent_bit(&info->pending_del, 0, &start, &end,
2068 static int finish_current_insert(struct btrfs_trans_handle *trans,
2069 struct btrfs_root *extent_root)
2074 struct btrfs_fs_info *info = extent_root->fs_info;
2075 struct btrfs_path *path;
2076 struct pending_extent_op *extent_op;
2077 struct btrfs_key key;
2079 int skinny_metadata =
2080 btrfs_fs_incompat(extent_root->fs_info,
2081 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
2083 path = btrfs_alloc_path();
2086 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
2087 &end, EXTENT_LOCKED);
2091 ret = get_state_private(&info->extent_ins, start, &priv);
2093 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2095 if (extent_op->type == PENDING_EXTENT_INSERT) {
2096 key.objectid = start;
2097 if (skinny_metadata) {
2098 key.offset = extent_op->level;
2099 key.type = BTRFS_METADATA_ITEM_KEY;
2101 key.offset = extent_op->num_bytes;
2102 key.type = BTRFS_EXTENT_ITEM_KEY;
2104 ret = alloc_reserved_tree_block(trans, extent_root,
2105 extent_root->root_key.objectid,
2109 extent_op->level, &key);
2114 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
2118 btrfs_free_path(path);
2122 static int pin_down_bytes(struct btrfs_trans_handle *trans,
2123 struct btrfs_root *root,
2124 u64 bytenr, u64 num_bytes, int is_data)
2127 struct extent_buffer *buf;
2132 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2136 /* we can reuse a block if it hasn't been written
2137 * and it is from this transaction. We can't
2138 * reuse anything from the tree log root because
2139 * it has tiny sub-transactions.
2141 if (btrfs_buffer_uptodate(buf, 0)) {
2142 u64 header_owner = btrfs_header_owner(buf);
2143 u64 header_transid = btrfs_header_generation(buf);
2144 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
2145 header_transid == trans->transid &&
2146 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2147 clean_tree_block(NULL, root, buf);
2148 free_extent_buffer(buf);
2152 free_extent_buffer(buf);
2154 update_pinned_extents(root, bytenr, num_bytes, 1);
2160 void btrfs_pin_extent(struct btrfs_fs_info *fs_info,
2161 u64 bytenr, u64 num_bytes)
2163 update_pinned_extents(fs_info->extent_root, bytenr, num_bytes, 1);
2166 void btrfs_unpin_extent(struct btrfs_fs_info *fs_info,
2167 u64 bytenr, u64 num_bytes)
2169 update_pinned_extents(fs_info->extent_root, bytenr, num_bytes, 0);
2173 * remove an extent from the root, returns 0 on success
2175 static int __free_extent(struct btrfs_trans_handle *trans,
2176 struct btrfs_root *root,
2177 u64 bytenr, u64 num_bytes, u64 parent,
2178 u64 root_objectid, u64 owner_objectid,
2179 u64 owner_offset, int refs_to_drop)
2182 struct btrfs_key key;
2183 struct btrfs_path *path;
2184 struct btrfs_extent_ops *ops = root->fs_info->extent_ops;
2185 struct btrfs_root *extent_root = root->fs_info->extent_root;
2186 struct extent_buffer *leaf;
2187 struct btrfs_extent_item *ei;
2188 struct btrfs_extent_inline_ref *iref;
2191 int extent_slot = 0;
2192 int found_extent = 0;
2196 int skinny_metadata =
2197 btrfs_fs_incompat(extent_root->fs_info,
2198 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
2200 if (root->fs_info->free_extent_hook) {
2201 root->fs_info->free_extent_hook(trans, root, bytenr, num_bytes,
2202 parent, root_objectid, owner_objectid,
2203 owner_offset, refs_to_drop);
2206 path = btrfs_alloc_path();
2211 path->leave_spinning = 1;
2213 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
2215 skinny_metadata = 0;
2216 BUG_ON(!is_data && refs_to_drop != 1);
2218 ret = lookup_extent_backref(trans, extent_root, path, &iref,
2219 bytenr, num_bytes, parent,
2220 root_objectid, owner_objectid,
2223 extent_slot = path->slots[0];
2224 while (extent_slot >= 0) {
2225 btrfs_item_key_to_cpu(path->nodes[0], &key,
2227 if (key.objectid != bytenr)
2229 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
2230 key.offset == num_bytes) {
2234 if (key.type == BTRFS_METADATA_ITEM_KEY &&
2235 key.offset == owner_objectid) {
2239 if (path->slots[0] - extent_slot > 5)
2243 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2244 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
2245 if (found_extent && item_size < sizeof(*ei))
2248 if (!found_extent) {
2250 ret = remove_extent_backref(trans, extent_root, path,
2254 btrfs_release_path(extent_root, path);
2255 path->leave_spinning = 1;
2257 key.objectid = bytenr;
2259 if (skinny_metadata) {
2260 key.type = BTRFS_METADATA_ITEM_KEY;
2261 key.offset = owner_objectid;
2263 key.type = BTRFS_EXTENT_ITEM_KEY;
2264 key.offset = num_bytes;
2267 ret = btrfs_search_slot(trans, extent_root,
2269 if (ret > 0 && skinny_metadata && path->slots[0]) {
2271 btrfs_item_key_to_cpu(path->nodes[0],
2274 if (key.objectid == bytenr &&
2275 key.type == BTRFS_EXTENT_ITEM_KEY &&
2276 key.offset == num_bytes)
2280 if (ret > 0 && skinny_metadata) {
2281 skinny_metadata = 0;
2282 btrfs_release_path(extent_root, path);
2283 key.type = BTRFS_EXTENT_ITEM_KEY;
2284 key.offset = num_bytes;
2285 ret = btrfs_search_slot(trans, extent_root,
2290 printk(KERN_ERR "umm, got %d back from search"
2291 ", was looking for %llu\n", ret,
2292 (unsigned long long)bytenr);
2293 btrfs_print_leaf(extent_root, path->nodes[0]);
2296 extent_slot = path->slots[0];
2299 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
2300 "parent %llu root %llu owner %llu offset %llu\n",
2301 (unsigned long long)bytenr,
2302 (unsigned long long)parent,
2303 (unsigned long long)root_objectid,
2304 (unsigned long long)owner_objectid,
2305 (unsigned long long)owner_offset);
2310 leaf = path->nodes[0];
2311 item_size = btrfs_item_size_nr(leaf, extent_slot);
2312 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2313 if (item_size < sizeof(*ei)) {
2314 BUG_ON(found_extent || extent_slot != path->slots[0]);
2315 ret = convert_extent_item_v0(trans, extent_root, path,
2319 btrfs_release_path(extent_root, path);
2320 path->leave_spinning = 1;
2322 key.objectid = bytenr;
2323 key.type = BTRFS_EXTENT_ITEM_KEY;
2324 key.offset = num_bytes;
2326 ret = btrfs_search_slot(trans, extent_root, &key, path,
2329 printk(KERN_ERR "umm, got %d back from search"
2330 ", was looking for %llu\n", ret,
2331 (unsigned long long)bytenr);
2332 btrfs_print_leaf(extent_root, path->nodes[0]);
2335 extent_slot = path->slots[0];
2336 leaf = path->nodes[0];
2337 item_size = btrfs_item_size_nr(leaf, extent_slot);
2340 BUG_ON(item_size < sizeof(*ei));
2341 ei = btrfs_item_ptr(leaf, extent_slot,
2342 struct btrfs_extent_item);
2343 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
2344 key.type == BTRFS_EXTENT_ITEM_KEY) {
2345 struct btrfs_tree_block_info *bi;
2346 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
2347 bi = (struct btrfs_tree_block_info *)(ei + 1);
2348 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
2351 refs = btrfs_extent_refs(leaf, ei);
2352 BUG_ON(refs < refs_to_drop);
2353 refs -= refs_to_drop;
2357 * In the case of inline back ref, reference count will
2358 * be updated by remove_extent_backref
2361 BUG_ON(!found_extent);
2363 btrfs_set_extent_refs(leaf, ei, refs);
2364 btrfs_mark_buffer_dirty(leaf);
2367 ret = remove_extent_backref(trans, extent_root, path,
2377 BUG_ON(is_data && refs_to_drop !=
2378 extent_data_ref_count(root, path, iref));
2380 BUG_ON(path->slots[0] != extent_slot);
2382 BUG_ON(path->slots[0] != extent_slot + 1);
2383 path->slots[0] = extent_slot;
2388 if (ops && ops->free_extent) {
2389 ret = ops->free_extent(root, bytenr, num_bytes);
2397 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2404 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2407 btrfs_release_path(extent_root, path);
2410 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2414 update_block_group(trans, root, bytenr, num_bytes, 0, mark_free);
2417 btrfs_free_path(path);
2418 finish_current_insert(trans, extent_root);
2423 * find all the blocks marked as pending in the radix tree and remove
2424 * them from the extent map
2426 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
2427 btrfs_root *extent_root)
2434 struct extent_io_tree *pending_del;
2435 struct extent_io_tree *extent_ins;
2436 struct pending_extent_op *extent_op;
2438 extent_ins = &extent_root->fs_info->extent_ins;
2439 pending_del = &extent_root->fs_info->pending_del;
2442 ret = find_first_extent_bit(pending_del, 0, &start, &end,
2447 ret = get_state_private(pending_del, start, &priv);
2449 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2451 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
2454 if (!test_range_bit(extent_ins, start, end,
2455 EXTENT_LOCKED, 0)) {
2456 ret = __free_extent(trans, extent_root,
2457 start, end + 1 - start, 0,
2458 extent_root->root_key.objectid,
2459 extent_op->level, 0, 1);
2463 ret = get_state_private(extent_ins, start, &priv);
2465 extent_op = (struct pending_extent_op *)
2466 (unsigned long)priv;
2468 clear_extent_bits(extent_ins, start, end,
2469 EXTENT_LOCKED, GFP_NOFS);
2471 if (extent_op->type == PENDING_BACKREF_UPDATE)
2483 * remove an extent from the root, returns 0 on success
2486 int btrfs_free_extent(struct btrfs_trans_handle *trans,
2487 struct btrfs_root *root,
2488 u64 bytenr, u64 num_bytes, u64 parent,
2489 u64 root_objectid, u64 owner, u64 offset)
2491 struct btrfs_root *extent_root = root->fs_info->extent_root;
2495 WARN_ON(num_bytes < root->sectorsize);
2496 if (root == extent_root) {
2497 struct pending_extent_op *extent_op;
2499 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2502 extent_op->type = PENDING_EXTENT_DELETE;
2503 extent_op->bytenr = bytenr;
2504 extent_op->num_bytes = num_bytes;
2505 extent_op->level = (int)owner;
2507 set_extent_bits(&root->fs_info->pending_del,
2508 bytenr, bytenr + num_bytes - 1,
2509 EXTENT_LOCKED, GFP_NOFS);
2510 set_state_private(&root->fs_info->pending_del,
2511 bytenr, (unsigned long)extent_op);
2514 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2515 root_objectid, owner, offset, 1);
2516 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
2517 return ret ? ret : pending_ret;
2520 static u64 stripe_align(struct btrfs_root *root, u64 val)
2522 u64 mask = ((u64)root->stripesize - 1);
2523 u64 ret = (val + mask) & ~mask;
2528 * walks the btree of allocated extents and find a hole of a given size.
2529 * The key ins is changed to record the hole:
2530 * ins->objectid == block start
2531 * ins->flags = BTRFS_EXTENT_ITEM_KEY
2532 * ins->offset == number of blocks
2533 * Any available blocks before search_start are skipped.
2535 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2536 struct btrfs_root *orig_root,
2537 u64 num_bytes, u64 empty_size,
2538 u64 search_start, u64 search_end,
2539 u64 hint_byte, struct btrfs_key *ins,
2540 u64 exclude_start, u64 exclude_nr,
2544 u64 orig_search_start = search_start;
2545 struct btrfs_root * root = orig_root->fs_info->extent_root;
2546 struct btrfs_fs_info *info = root->fs_info;
2547 u64 total_needed = num_bytes;
2548 struct btrfs_block_group_cache *block_group;
2552 WARN_ON(num_bytes < root->sectorsize);
2553 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2555 search_start = stripe_align(root, search_start);
2558 block_group = btrfs_lookup_first_block_group(info, hint_byte);
2560 hint_byte = search_start;
2561 block_group = btrfs_find_block_group(root, block_group,
2562 hint_byte, data, 1);
2564 block_group = btrfs_find_block_group(root,
2566 search_start, data, 1);
2569 total_needed += empty_size;
2572 search_start = stripe_align(root, search_start);
2574 block_group = btrfs_lookup_first_block_group(info,
2577 block_group = btrfs_lookup_first_block_group(info,
2580 ret = find_search_start(root, &block_group, &search_start,
2581 total_needed, data);
2585 ins->objectid = search_start;
2586 ins->offset = num_bytes;
2588 if (ins->objectid + num_bytes >
2589 block_group->key.objectid + block_group->key.offset) {
2590 search_start = block_group->key.objectid +
2591 block_group->key.offset;
2595 if (test_range_bit(&info->extent_ins, ins->objectid,
2596 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
2597 search_start = ins->objectid + num_bytes;
2601 if (test_range_bit(&info->pinned_extents, ins->objectid,
2602 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
2603 search_start = ins->objectid + num_bytes;
2607 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
2608 ins->objectid < exclude_start + exclude_nr)) {
2609 search_start = exclude_start + exclude_nr;
2613 if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
2614 block_group = btrfs_lookup_block_group(info, ins->objectid);
2616 trans->block_group = block_group;
2618 ins->offset = num_bytes;
2622 block_group = btrfs_lookup_first_block_group(info, search_start);
2624 search_start = orig_search_start;
2631 total_needed -= empty_size;
2637 block_group = btrfs_find_block_group(root, block_group,
2638 search_start, data, 0);
2645 static int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2646 struct btrfs_root *root,
2647 u64 num_bytes, u64 empty_size,
2648 u64 hint_byte, u64 search_end,
2649 struct btrfs_key *ins, int data)
2652 u64 search_start = 0;
2654 struct btrfs_fs_info *info = root->fs_info;
2656 if (info->extent_ops) {
2657 struct btrfs_extent_ops *ops = info->extent_ops;
2658 ret = ops->alloc_extent(root, num_bytes, hint_byte, ins);
2664 alloc_profile = info->avail_data_alloc_bits &
2665 info->data_alloc_profile;
2666 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2667 } else if ((info->system_allocs > 0 || root == info->chunk_root) &&
2668 info->system_allocs >= 0) {
2669 alloc_profile = info->avail_system_alloc_bits &
2670 info->system_alloc_profile;
2671 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2673 alloc_profile = info->avail_metadata_alloc_bits &
2674 info->metadata_alloc_profile;
2675 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2678 if (root->ref_cows) {
2679 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
2680 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2682 BTRFS_BLOCK_GROUP_METADATA);
2685 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2686 num_bytes + 2 * 1024 * 1024, data);
2690 WARN_ON(num_bytes < root->sectorsize);
2691 ret = find_free_extent(trans, root, num_bytes, empty_size,
2692 search_start, search_end, hint_byte, ins,
2693 trans->alloc_exclude_start,
2694 trans->alloc_exclude_nr, data);
2697 clear_extent_dirty(&root->fs_info->free_space_cache,
2698 ins->objectid, ins->objectid + ins->offset - 1,
2703 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
2704 struct btrfs_root *root,
2705 u64 root_objectid, u64 generation,
2706 u64 flags, struct btrfs_disk_key *key,
2707 int level, struct btrfs_key *ins)
2710 struct btrfs_fs_info *fs_info = root->fs_info;
2711 struct btrfs_extent_item *extent_item;
2712 struct btrfs_tree_block_info *block_info;
2713 struct btrfs_extent_inline_ref *iref;
2714 struct btrfs_path *path;
2715 struct extent_buffer *leaf;
2716 u32 size = sizeof(*extent_item) + sizeof(*iref);
2717 int skinny_metadata =
2718 btrfs_fs_incompat(fs_info,
2719 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
2721 if (!skinny_metadata)
2722 size += sizeof(*block_info);
2724 path = btrfs_alloc_path();
2727 path->leave_spinning = 1;
2728 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
2732 leaf = path->nodes[0];
2733 extent_item = btrfs_item_ptr(leaf, path->slots[0],
2734 struct btrfs_extent_item);
2735 btrfs_set_extent_refs(leaf, extent_item, 1);
2736 btrfs_set_extent_generation(leaf, extent_item, generation);
2737 btrfs_set_extent_flags(leaf, extent_item,
2738 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
2740 if (skinny_metadata) {
2741 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
2743 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
2744 btrfs_set_tree_block_key(leaf, block_info, key);
2745 btrfs_set_tree_block_level(leaf, block_info, level);
2746 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
2749 btrfs_set_extent_inline_ref_type(leaf, iref, BTRFS_TREE_BLOCK_REF_KEY);
2750 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
2752 btrfs_mark_buffer_dirty(leaf);
2753 btrfs_free_path(path);
2755 ret = update_block_group(trans, root, ins->objectid, root->leafsize,
2760 static int alloc_tree_block(struct btrfs_trans_handle *trans,
2761 struct btrfs_root *root, u64 num_bytes,
2762 u64 root_objectid, u64 generation,
2763 u64 flags, struct btrfs_disk_key *key,
2764 int level, u64 empty_size, u64 hint_byte,
2765 u64 search_end, struct btrfs_key *ins)
2768 ret = btrfs_reserve_extent(trans, root, num_bytes, empty_size,
2769 hint_byte, search_end, ins, 0);
2772 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID) {
2773 struct pending_extent_op *extent_op;
2775 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2778 extent_op->type = PENDING_EXTENT_INSERT;
2779 extent_op->bytenr = ins->objectid;
2780 extent_op->num_bytes = ins->offset;
2781 extent_op->level = level;
2782 extent_op->flags = flags;
2783 memcpy(&extent_op->key, key, sizeof(*key));
2785 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2786 ins->objectid + ins->offset - 1,
2787 EXTENT_LOCKED, GFP_NOFS);
2788 set_state_private(&root->fs_info->extent_ins,
2789 ins->objectid, (unsigned long)extent_op);
2791 if (btrfs_fs_incompat(root->fs_info,
2792 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA)) {
2793 ins->offset = level;
2794 ins->type = BTRFS_METADATA_ITEM_KEY;
2796 ret = alloc_reserved_tree_block(trans, root, root_objectid,
2799 finish_current_insert(trans, root->fs_info->extent_root);
2800 del_pending_extents(trans, root->fs_info->extent_root);
2806 * helper function to allocate a block for a given tree
2807 * returns the tree buffer or NULL.
2809 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
2810 struct btrfs_root *root,
2811 u32 blocksize, u64 root_objectid,
2812 struct btrfs_disk_key *key, int level,
2813 u64 hint, u64 empty_size)
2815 struct btrfs_key ins;
2817 struct extent_buffer *buf;
2819 ret = alloc_tree_block(trans, root, blocksize, root_objectid,
2820 trans->transid, 0, key, level,
2821 empty_size, hint, (u64)-1, &ins);
2824 return ERR_PTR(ret);
2827 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
2829 btrfs_free_extent(trans, root, ins.objectid, ins.offset,
2830 0, root->root_key.objectid, level, 0);
2832 return ERR_PTR(-ENOMEM);
2834 btrfs_set_buffer_uptodate(buf);
2835 trans->blocks_used++;
2842 static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
2843 struct btrfs_root *root,
2844 struct extent_buffer *leaf)
2847 u64 leaf_generation;
2848 struct btrfs_key key;
2849 struct btrfs_file_extent_item *fi;
2854 BUG_ON(!btrfs_is_leaf(leaf));
2855 nritems = btrfs_header_nritems(leaf);
2856 leaf_owner = btrfs_header_owner(leaf);
2857 leaf_generation = btrfs_header_generation(leaf);
2859 for (i = 0; i < nritems; i++) {
2862 btrfs_item_key_to_cpu(leaf, &key, i);
2863 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2865 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
2866 if (btrfs_file_extent_type(leaf, fi) ==
2867 BTRFS_FILE_EXTENT_INLINE)
2870 * FIXME make sure to insert a trans record that
2871 * repeats the snapshot del on crash
2873 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2874 if (disk_bytenr == 0)
2876 ret = btrfs_free_extent(trans, root, disk_bytenr,
2877 btrfs_file_extent_disk_num_bytes(leaf, fi),
2878 leaf->start, leaf_owner, leaf_generation,
2885 static void noinline reada_walk_down(struct btrfs_root *root,
2886 struct extent_buffer *node,
2899 nritems = btrfs_header_nritems(node);
2900 level = btrfs_header_level(node);
2904 for (i = slot; i < nritems && skipped < 32; i++) {
2905 bytenr = btrfs_node_blockptr(node, i);
2906 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
2907 (last > bytenr && last - bytenr > 32 * 1024))) {
2911 blocksize = btrfs_level_size(root, level - 1);
2913 ret = btrfs_lookup_extent_ref(NULL, root, bytenr,
2921 mutex_unlock(&root->fs_info->fs_mutex);
2922 ret = readahead_tree_block(root, bytenr, blocksize,
2923 btrfs_node_ptr_generation(node, i));
2924 last = bytenr + blocksize;
2926 mutex_lock(&root->fs_info->fs_mutex);
2933 * helper function for drop_snapshot, this walks down the tree dropping ref
2934 * counts as it goes.
2936 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2937 struct btrfs_root *root,
2938 struct btrfs_path *path, int *level)
2944 struct extent_buffer *next;
2945 struct extent_buffer *cur;
2946 struct extent_buffer *parent;
2951 WARN_ON(*level < 0);
2952 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2953 ret = btrfs_lookup_extent_ref(trans, root,
2954 path->nodes[*level]->start,
2955 path->nodes[*level]->len, &refs);
2961 * walk down to the last node level and free all the leaves
2963 while(*level >= 0) {
2964 WARN_ON(*level < 0);
2965 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2966 cur = path->nodes[*level];
2968 if (btrfs_header_level(cur) != *level)
2971 if (path->slots[*level] >=
2972 btrfs_header_nritems(cur))
2975 ret = drop_leaf_ref(trans, root, cur);
2979 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2980 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2981 blocksize = btrfs_level_size(root, *level - 1);
2982 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
2986 parent = path->nodes[*level];
2987 root_owner = btrfs_header_owner(parent);
2988 root_gen = btrfs_header_generation(parent);
2989 path->slots[*level]++;
2990 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
2991 parent->start, root_owner,
2992 root_gen, *level - 1, 1);
2996 next = btrfs_find_tree_block(root, bytenr, blocksize);
2997 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
2998 free_extent_buffer(next);
2999 reada_walk_down(root, cur, path->slots[*level]);
3000 mutex_unlock(&root->fs_info->fs_mutex);
3001 next = read_tree_block(root, bytenr, blocksize,
3003 mutex_lock(&root->fs_info->fs_mutex);
3005 WARN_ON(*level <= 0);
3006 if (path->nodes[*level-1])
3007 free_extent_buffer(path->nodes[*level-1]);
3008 path->nodes[*level-1] = next;
3009 *level = btrfs_header_level(next);
3010 path->slots[*level] = 0;
3013 WARN_ON(*level < 0);
3014 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3016 if (path->nodes[*level] == root->node) {
3017 root_owner = root->root_key.objectid;
3018 parent = path->nodes[*level];
3020 parent = path->nodes[*level + 1];
3021 root_owner = btrfs_header_owner(parent);
3024 root_gen = btrfs_header_generation(parent);
3025 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
3026 path->nodes[*level]->len, parent->start,
3027 root_owner, root_gen, *level, 1);
3028 free_extent_buffer(path->nodes[*level]);
3029 path->nodes[*level] = NULL;
3036 * helper for dropping snapshots. This walks back up the tree in the path
3037 * to find the first node higher up where we haven't yet gone through
3040 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3041 struct btrfs_root *root,
3042 struct btrfs_path *path, int *level)
3046 struct btrfs_root_item *root_item = &root->root_item;
3051 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
3052 slot = path->slots[i];
3053 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3054 struct extent_buffer *node;
3055 struct btrfs_disk_key disk_key;
3056 node = path->nodes[i];
3059 WARN_ON(*level == 0);
3060 btrfs_node_key(node, &disk_key, path->slots[i]);
3061 memcpy(&root_item->drop_progress,
3062 &disk_key, sizeof(disk_key));
3063 root_item->drop_level = i;
3066 struct extent_buffer *parent;
3067 if (path->nodes[*level] == root->node)
3068 parent = path->nodes[*level];
3070 parent = path->nodes[*level + 1];
3072 root_owner = btrfs_header_owner(parent);
3073 root_gen = btrfs_header_generation(parent);
3074 ret = btrfs_free_extent(trans, root,
3075 path->nodes[*level]->start,
3076 path->nodes[*level]->len,
3077 parent->start, root_owner,
3078 root_gen, *level, 1);
3080 free_extent_buffer(path->nodes[*level]);
3081 path->nodes[*level] = NULL;
3090 int btrfs_free_block_groups(struct btrfs_fs_info *info)
3092 struct btrfs_space_info *sinfo;
3093 struct btrfs_block_group_cache *cache;
3100 ret = find_first_extent_bit(&info->block_group_cache, 0,
3101 &start, &end, (unsigned int)-1);
3104 ret = get_state_private(&info->block_group_cache, start, &ptr);
3106 cache = (struct btrfs_block_group_cache *)ptr;
3107 if (cache->free_space_ctl) {
3108 btrfs_remove_free_space_cache(cache);
3109 kfree(cache->free_space_ctl);
3113 clear_extent_bits(&info->block_group_cache, start,
3114 end, (unsigned int)-1, GFP_NOFS);
3117 ret = find_first_extent_bit(&info->free_space_cache, 0,
3118 &start, &end, EXTENT_DIRTY);
3121 clear_extent_dirty(&info->free_space_cache, start,
3125 while (!list_empty(&info->space_info)) {
3126 sinfo = list_entry(info->space_info.next,
3127 struct btrfs_space_info, list);
3128 list_del_init(&sinfo->list);
3134 int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
3135 struct btrfs_key *key)
3138 struct btrfs_key found_key;
3139 struct extent_buffer *leaf;
3142 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
3146 slot = path->slots[0];
3147 leaf = path->nodes[0];
3148 if (slot >= btrfs_header_nritems(leaf)) {
3149 ret = btrfs_next_leaf(root, path);
3156 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3158 if (found_key.objectid >= key->objectid &&
3159 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
3168 int btrfs_read_block_groups(struct btrfs_root *root)
3170 struct btrfs_path *path;
3173 struct btrfs_block_group_cache *cache;
3174 struct btrfs_fs_info *info = root->fs_info;
3175 struct btrfs_space_info *space_info;
3176 struct extent_io_tree *block_group_cache;
3177 struct btrfs_key key;
3178 struct btrfs_key found_key;
3179 struct extent_buffer *leaf;
3181 block_group_cache = &info->block_group_cache;
3183 root = info->extent_root;
3186 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3187 path = btrfs_alloc_path();
3192 ret = find_first_block_group(root, path, &key);
3200 leaf = path->nodes[0];
3201 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3202 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3208 read_extent_buffer(leaf, &cache->item,
3209 btrfs_item_ptr_offset(leaf, path->slots[0]),
3210 sizeof(cache->item));
3211 memcpy(&cache->key, &found_key, sizeof(found_key));
3214 key.objectid = found_key.objectid + found_key.offset;
3215 btrfs_release_path(root, path);
3216 cache->flags = btrfs_block_group_flags(&cache->item);
3218 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
3219 bit = BLOCK_GROUP_DATA;
3220 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
3221 bit = BLOCK_GROUP_SYSTEM;
3222 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
3223 bit = BLOCK_GROUP_METADATA;
3225 set_avail_alloc_bits(info, cache->flags);
3226 if (btrfs_chunk_readonly(root, cache->key.objectid))
3229 ret = update_space_info(info, cache->flags, found_key.offset,
3230 btrfs_block_group_used(&cache->item),
3233 cache->space_info = space_info;
3235 /* use EXTENT_LOCKED to prevent merging */
3236 set_extent_bits(block_group_cache, found_key.objectid,
3237 found_key.objectid + found_key.offset - 1,
3238 bit | EXTENT_LOCKED, GFP_NOFS);
3239 set_state_private(block_group_cache, found_key.objectid,
3240 (unsigned long)cache);
3244 btrfs_free_path(path);
3248 struct btrfs_block_group_cache *
3249 btrfs_add_block_group(struct btrfs_fs_info *fs_info, u64 bytes_used, u64 type,
3250 u64 chunk_objectid, u64 chunk_offset, u64 size)
3254 struct btrfs_block_group_cache *cache;
3255 struct extent_io_tree *block_group_cache;
3257 block_group_cache = &fs_info->block_group_cache;
3259 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3261 cache->key.objectid = chunk_offset;
3262 cache->key.offset = size;
3264 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3265 btrfs_set_block_group_used(&cache->item, bytes_used);
3266 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
3267 cache->flags = type;
3268 btrfs_set_block_group_flags(&cache->item, type);
3270 ret = update_space_info(fs_info, cache->flags, size, bytes_used,
3271 &cache->space_info);
3274 bit = block_group_state_bits(type);
3275 set_extent_bits(block_group_cache, chunk_offset,
3276 chunk_offset + size - 1,
3277 bit | EXTENT_LOCKED, GFP_NOFS);
3279 set_state_private(block_group_cache, chunk_offset,
3280 (unsigned long)cache);
3281 set_avail_alloc_bits(fs_info, type);
3286 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3287 struct btrfs_root *root, u64 bytes_used,
3288 u64 type, u64 chunk_objectid, u64 chunk_offset,
3292 struct btrfs_root *extent_root;
3293 struct btrfs_block_group_cache *cache;
3295 cache = btrfs_add_block_group(root->fs_info, bytes_used, type,
3296 chunk_objectid, chunk_offset, size);
3297 extent_root = root->fs_info->extent_root;
3298 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3299 sizeof(cache->item));
3302 finish_current_insert(trans, extent_root);
3303 ret = del_pending_extents(trans, extent_root);
3308 * This is for converter use only.
3310 * In that case, we don't know where are free blocks located.
3311 * Therefore all block group cache entries must be setup properly
3312 * before doing any block allocation.
3314 int btrfs_make_block_groups(struct btrfs_trans_handle *trans,
3315 struct btrfs_root *root)
3323 u64 total_metadata = 0;
3327 struct btrfs_root *extent_root;
3328 struct btrfs_block_group_cache *cache;
3329 struct extent_io_tree *block_group_cache;
3331 extent_root = root->fs_info->extent_root;
3332 block_group_cache = &root->fs_info->block_group_cache;
3333 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3334 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
3335 group_align = 64 * root->sectorsize;
3338 while (cur_start < total_bytes) {
3339 group_size = total_bytes / 12;
3340 group_size = min_t(u64, group_size, total_bytes - cur_start);
3341 if (cur_start == 0) {
3342 bit = BLOCK_GROUP_SYSTEM;
3343 group_type = BTRFS_BLOCK_GROUP_SYSTEM;
3345 group_size &= ~(group_align - 1);
3346 group_size = max_t(u64, group_size, 8 * 1024 * 1024);
3347 group_size = min_t(u64, group_size, 32 * 1024 * 1024);
3349 group_size &= ~(group_align - 1);
3350 if (total_data >= total_metadata * 2) {
3351 group_type = BTRFS_BLOCK_GROUP_METADATA;
3352 group_size = min_t(u64, group_size,
3353 1ULL * 1024 * 1024 * 1024);
3354 total_metadata += group_size;
3356 group_type = BTRFS_BLOCK_GROUP_DATA;
3357 group_size = min_t(u64, group_size,
3358 5ULL * 1024 * 1024 * 1024);
3359 total_data += group_size;
3361 if ((total_bytes - cur_start) * 4 < group_size * 5)
3362 group_size = total_bytes - cur_start;
3365 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3368 cache->key.objectid = cur_start;
3369 cache->key.offset = group_size;
3370 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3372 btrfs_set_block_group_used(&cache->item, 0);
3373 btrfs_set_block_group_chunk_objectid(&cache->item,
3375 btrfs_set_block_group_flags(&cache->item, group_type);
3377 cache->flags = group_type;
3379 ret = update_space_info(root->fs_info, group_type, group_size,
3380 0, &cache->space_info);
3382 set_avail_alloc_bits(extent_root->fs_info, group_type);
3384 set_extent_bits(block_group_cache, cur_start,
3385 cur_start + group_size - 1,
3386 bit | EXTENT_LOCKED, GFP_NOFS);
3387 set_state_private(block_group_cache, cur_start,
3388 (unsigned long)cache);
3389 cur_start += group_size;
3391 /* then insert all the items */
3393 while(cur_start < total_bytes) {
3394 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
3397 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3398 sizeof(cache->item));
3401 finish_current_insert(trans, extent_root);
3402 ret = del_pending_extents(trans, extent_root);
3405 cur_start = cache->key.objectid + cache->key.offset;
3410 int btrfs_update_block_group(struct btrfs_trans_handle *trans,
3411 struct btrfs_root *root,
3412 u64 bytenr, u64 num_bytes, int alloc,
3415 return update_block_group(trans, root, bytenr, num_bytes,
3419 static int btrfs_count_extents_in_block_group(struct btrfs_root *root,
3420 struct btrfs_path *path, u64 start,
3424 struct btrfs_key key;
3425 struct extent_buffer *leaf;
3431 key.objectid = start;
3432 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
3433 ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
3438 leaf = path->nodes[0];
3439 slot = path->slots[0];
3440 if (slot >= btrfs_header_nritems(leaf)) {
3441 ret = btrfs_next_leaf(root, path);
3446 leaf = path->nodes[0];
3447 slot = path->slots[0];
3449 btrfs_item_key_to_cpu(leaf, &key, slot);
3450 if (key.objectid > start + len)
3452 if (key.type == BTRFS_EXTENT_ITEM_KEY)
3453 bytes_used += key.offset;
3454 if (key.type == BTRFS_METADATA_ITEM_KEY)
3455 bytes_used += root->leafsize;
3458 *total = bytes_used;
3459 btrfs_release_path(root, path);
3463 int btrfs_check_block_accounting(struct btrfs_root *root)
3468 struct btrfs_path path;
3469 struct btrfs_block_group_cache *cache;
3470 struct btrfs_fs_info *fs_info = root->fs_info;
3472 btrfs_init_path(&path);
3475 cache = btrfs_lookup_block_group(fs_info, start);
3479 ret = btrfs_count_extents_in_block_group(root, &path,
3480 cache->key.objectid,
3485 u64 on_disk = btrfs_block_group_used(&cache->item);
3486 if (on_disk != bytes_used) {
3487 fprintf(stderr, "bad block group accounting found %llu "
3488 "expected %llu block group %llu\n",
3489 (unsigned long long)bytes_used,
3490 (unsigned long long)on_disk,
3491 (unsigned long long)cache->key.objectid);
3494 start = cache->key.objectid + cache->key.offset;
3496 cache->space_info->bytes_used = 0;
3502 * Fixup block accounting. The initial block accounting created by
3503 * make_block_groups isn't accuracy in this case.
3505 int btrfs_fix_block_accounting(struct btrfs_trans_handle *trans,
3506 struct btrfs_root *root)
3512 struct btrfs_path path;
3513 struct btrfs_key key;
3514 struct extent_buffer *leaf;
3515 struct btrfs_block_group_cache *cache;
3516 struct btrfs_fs_info *fs_info = root->fs_info;
3518 root = root->fs_info->extent_root;
3520 while(extent_root_pending_ops(fs_info)) {
3521 ret = finish_current_insert(trans, root);
3524 ret = del_pending_extents(trans, root);
3530 cache = btrfs_lookup_first_block_group(fs_info, start);
3533 start = cache->key.objectid + cache->key.offset;
3534 btrfs_set_block_group_used(&cache->item, 0);
3535 cache->space_info->bytes_used = 0;
3536 set_extent_bits(&root->fs_info->block_group_cache,
3537 cache->key.objectid,
3538 cache->key.objectid + cache->key.offset -1,
3539 BLOCK_GROUP_DIRTY, GFP_NOFS);
3542 btrfs_init_path(&path);
3545 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
3546 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
3551 leaf = path.nodes[0];
3552 slot = path.slots[0];
3553 if (slot >= btrfs_header_nritems(leaf)) {
3554 ret = btrfs_next_leaf(root, &path);
3559 leaf = path.nodes[0];
3560 slot = path.slots[0];
3562 btrfs_item_key_to_cpu(leaf, &key, slot);
3563 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
3564 bytes_used += key.offset;
3565 ret = btrfs_update_block_group(trans, root,
3566 key.objectid, key.offset, 1, 0);
3568 } else if (key.type == BTRFS_METADATA_ITEM_KEY) {
3569 bytes_used += root->leafsize;
3570 ret = btrfs_update_block_group(trans, root,
3571 key.objectid, root->leafsize, 1, 0);
3576 btrfs_set_super_bytes_used(root->fs_info->super_copy, bytes_used);
3577 btrfs_release_path(root, &path);