2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
21 #include "kerncompat.h"
22 #include "radix-tree.h"
25 #include "print-tree.h"
26 #include "transaction.h"
29 #include "free-space-cache.h"
31 #define PENDING_EXTENT_INSERT 0
32 #define PENDING_EXTENT_DELETE 1
33 #define PENDING_BACKREF_UPDATE 2
35 struct pending_extent_op {
40 struct btrfs_disk_key key;
44 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
45 struct btrfs_root *root,
46 u64 root_objectid, u64 generation,
47 u64 flags, struct btrfs_disk_key *key,
48 int level, struct btrfs_key *ins);
49 static int __free_extent(struct btrfs_trans_handle *trans,
50 struct btrfs_root *root,
51 u64 bytenr, u64 num_bytes, u64 parent,
52 u64 root_objectid, u64 owner_objectid,
53 u64 owner_offset, int refs_to_drop);
54 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
55 btrfs_root *extent_root);
56 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
57 btrfs_root *extent_root);
59 static int remove_sb_from_cache(struct btrfs_root *root,
60 struct btrfs_block_group_cache *cache)
66 struct extent_io_tree *free_space_cache;
68 free_space_cache = &root->fs_info->free_space_cache;
69 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
70 bytenr = btrfs_sb_offset(i);
71 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
72 cache->key.objectid, bytenr, 0,
73 &logical, &nr, &stripe_len);
76 clear_extent_dirty(free_space_cache, logical[nr],
77 logical[nr] + stripe_len - 1, GFP_NOFS);
84 static int cache_block_group(struct btrfs_root *root,
85 struct btrfs_block_group_cache *block_group)
87 struct btrfs_path *path;
90 struct extent_buffer *leaf;
91 struct extent_io_tree *free_space_cache;
99 root = root->fs_info->extent_root;
100 free_space_cache = &root->fs_info->free_space_cache;
102 if (block_group->cached)
105 path = btrfs_alloc_path();
110 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
115 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
120 leaf = path->nodes[0];
121 slot = path->slots[0];
122 if (slot >= btrfs_header_nritems(leaf)) {
123 ret = btrfs_next_leaf(root, path);
132 btrfs_item_key_to_cpu(leaf, &key, slot);
133 if (key.objectid < block_group->key.objectid) {
136 if (key.objectid >= block_group->key.objectid +
137 block_group->key.offset) {
141 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
142 key.type == BTRFS_METADATA_ITEM_KEY) {
143 if (key.objectid > last) {
144 hole_size = key.objectid - last;
145 set_extent_dirty(free_space_cache, last,
146 last + hole_size - 1,
149 if (key.type == BTRFS_METADATA_ITEM_KEY)
150 last = key.objectid + root->leafsize;
152 last = key.objectid + key.offset;
158 if (block_group->key.objectid +
159 block_group->key.offset > last) {
160 hole_size = block_group->key.objectid +
161 block_group->key.offset - last;
162 set_extent_dirty(free_space_cache, last,
163 last + hole_size - 1, GFP_NOFS);
165 remove_sb_from_cache(root, block_group);
166 block_group->cached = 1;
168 btrfs_free_path(path);
172 struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
176 struct extent_io_tree *block_group_cache;
177 struct btrfs_block_group_cache *block_group = NULL;
183 bytenr = max_t(u64, bytenr,
184 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
185 block_group_cache = &info->block_group_cache;
186 ret = find_first_extent_bit(block_group_cache,
187 bytenr, &start, &end,
188 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
193 ret = get_state_private(block_group_cache, start, &ptr);
197 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
201 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
205 struct extent_io_tree *block_group_cache;
206 struct btrfs_block_group_cache *block_group = NULL;
212 block_group_cache = &info->block_group_cache;
213 ret = find_first_extent_bit(block_group_cache,
214 bytenr, &start, &end,
215 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
220 ret = get_state_private(block_group_cache, start, &ptr);
224 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
225 if (block_group->key.objectid <= bytenr && bytenr <
226 block_group->key.objectid + block_group->key.offset)
231 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
233 return (cache->flags & bits) == bits;
236 static int noinline find_search_start(struct btrfs_root *root,
237 struct btrfs_block_group_cache **cache_ret,
238 u64 *start_ret, int num, int data)
241 struct btrfs_block_group_cache *cache = *cache_ret;
242 u64 last = *start_ret;
245 u64 search_start = *start_ret;
251 ret = cache_block_group(root, cache);
255 last = max(search_start, cache->key.objectid);
256 if (cache->ro || !block_group_bits(cache, data))
260 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
261 last, &start, &end, EXTENT_DIRTY);
266 start = max(last, start);
268 if (last - start < num) {
271 if (start + num > cache->key.objectid + cache->key.offset) {
279 cache = btrfs_lookup_block_group(root->fs_info, search_start);
281 printk("Unable to find block group for %llu\n",
282 (unsigned long long)search_start);
288 last = cache->key.objectid + cache->key.offset;
290 cache = btrfs_lookup_first_block_group(root->fs_info, last);
303 static u64 div_factor(u64 num, int factor)
312 static int block_group_state_bits(u64 flags)
315 if (flags & BTRFS_BLOCK_GROUP_DATA)
316 bits |= BLOCK_GROUP_DATA;
317 if (flags & BTRFS_BLOCK_GROUP_METADATA)
318 bits |= BLOCK_GROUP_METADATA;
319 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
320 bits |= BLOCK_GROUP_SYSTEM;
324 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
325 struct btrfs_block_group_cache
326 *hint, u64 search_start,
329 struct btrfs_block_group_cache *cache;
330 struct extent_io_tree *block_group_cache;
331 struct btrfs_block_group_cache *found_group = NULL;
332 struct btrfs_fs_info *info = root->fs_info;
345 block_group_cache = &info->block_group_cache;
350 bit = block_group_state_bits(data);
353 struct btrfs_block_group_cache *shint;
354 shint = btrfs_lookup_block_group(info, search_start);
355 if (shint && !shint->ro && block_group_bits(shint, data)) {
356 used = btrfs_block_group_used(&shint->item);
357 if (used + shint->pinned <
358 div_factor(shint->key.offset, factor)) {
363 if (hint && !hint->ro && block_group_bits(hint, data)) {
364 used = btrfs_block_group_used(&hint->item);
365 if (used + hint->pinned <
366 div_factor(hint->key.offset, factor)) {
369 last = hint->key.objectid + hint->key.offset;
373 hint_last = max(hint->key.objectid, search_start);
375 hint_last = search_start;
381 ret = find_first_extent_bit(block_group_cache, last,
386 ret = get_state_private(block_group_cache, start, &ptr);
390 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
391 last = cache->key.objectid + cache->key.offset;
392 used = btrfs_block_group_used(&cache->item);
394 if (!cache->ro && block_group_bits(cache, data)) {
396 free_check = cache->key.offset;
398 free_check = div_factor(cache->key.offset,
401 if (used + cache->pinned < free_check) {
418 * Back reference rules. Back refs have three main goals:
420 * 1) differentiate between all holders of references to an extent so that
421 * when a reference is dropped we can make sure it was a valid reference
422 * before freeing the extent.
424 * 2) Provide enough information to quickly find the holders of an extent
425 * if we notice a given block is corrupted or bad.
427 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
428 * maintenance. This is actually the same as #2, but with a slightly
429 * different use case.
431 * There are two kinds of back refs. The implicit back refs is optimized
432 * for pointers in non-shared tree blocks. For a given pointer in a block,
433 * back refs of this kind provide information about the block's owner tree
434 * and the pointer's key. These information allow us to find the block by
435 * b-tree searching. The full back refs is for pointers in tree blocks not
436 * referenced by their owner trees. The location of tree block is recorded
437 * in the back refs. Actually the full back refs is generic, and can be
438 * used in all cases the implicit back refs is used. The major shortcoming
439 * of the full back refs is its overhead. Every time a tree block gets
440 * COWed, we have to update back refs entry for all pointers in it.
442 * For a newly allocated tree block, we use implicit back refs for
443 * pointers in it. This means most tree related operations only involve
444 * implicit back refs. For a tree block created in old transaction, the
445 * only way to drop a reference to it is COW it. So we can detect the
446 * event that tree block loses its owner tree's reference and do the
447 * back refs conversion.
449 * When a tree block is COW'd through a tree, there are four cases:
451 * The reference count of the block is one and the tree is the block's
452 * owner tree. Nothing to do in this case.
454 * The reference count of the block is one and the tree is not the
455 * block's owner tree. In this case, full back refs is used for pointers
456 * in the block. Remove these full back refs, add implicit back refs for
457 * every pointers in the new block.
459 * The reference count of the block is greater than one and the tree is
460 * the block's owner tree. In this case, implicit back refs is used for
461 * pointers in the block. Add full back refs for every pointers in the
462 * block, increase lower level extents' reference counts. The original
463 * implicit back refs are entailed to the new block.
465 * The reference count of the block is greater than one and the tree is
466 * not the block's owner tree. Add implicit back refs for every pointer in
467 * the new block, increase lower level extents' reference count.
469 * Back Reference Key composing:
471 * The key objectid corresponds to the first byte in the extent,
472 * The key type is used to differentiate between types of back refs.
473 * There are different meanings of the key offset for different types
476 * File extents can be referenced by:
478 * - multiple snapshots, subvolumes, or different generations in one subvol
479 * - different files inside a single subvolume
480 * - different offsets inside a file (bookend extents in file.c)
482 * The extent ref structure for the implicit back refs has fields for:
484 * - Objectid of the subvolume root
485 * - objectid of the file holding the reference
486 * - original offset in the file
487 * - how many bookend extents
489 * The key offset for the implicit back refs is hash of the first
492 * The extent ref structure for the full back refs has field for:
494 * - number of pointers in the tree leaf
496 * The key offset for the implicit back refs is the first byte of
499 * When a file extent is allocated, The implicit back refs is used.
500 * the fields are filled in:
502 * (root_key.objectid, inode objectid, offset in file, 1)
504 * When a file extent is removed file truncation, we find the
505 * corresponding implicit back refs and check the following fields:
507 * (btrfs_header_owner(leaf), inode objectid, offset in file)
509 * Btree extents can be referenced by:
511 * - Different subvolumes
513 * Both the implicit back refs and the full back refs for tree blocks
514 * only consist of key. The key offset for the implicit back refs is
515 * objectid of block's owner tree. The key offset for the full back refs
516 * is the first byte of parent block.
518 * When implicit back refs is used, information about the lowest key and
519 * level of the tree block are required. These information are stored in
520 * tree block info structure.
523 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
524 static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
525 struct btrfs_root *root,
526 struct btrfs_path *path,
527 u64 owner, u32 extra_size)
529 struct btrfs_extent_item *item;
530 struct btrfs_extent_item_v0 *ei0;
531 struct btrfs_extent_ref_v0 *ref0;
532 struct btrfs_tree_block_info *bi;
533 struct extent_buffer *leaf;
534 struct btrfs_key key;
535 struct btrfs_key found_key;
536 u32 new_size = sizeof(*item);
540 leaf = path->nodes[0];
541 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
543 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
544 ei0 = btrfs_item_ptr(leaf, path->slots[0],
545 struct btrfs_extent_item_v0);
546 refs = btrfs_extent_refs_v0(leaf, ei0);
548 if (owner == (u64)-1) {
550 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
551 ret = btrfs_next_leaf(root, path);
555 leaf = path->nodes[0];
557 btrfs_item_key_to_cpu(leaf, &found_key,
559 BUG_ON(key.objectid != found_key.objectid);
560 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
564 ref0 = btrfs_item_ptr(leaf, path->slots[0],
565 struct btrfs_extent_ref_v0);
566 owner = btrfs_ref_objectid_v0(leaf, ref0);
570 btrfs_release_path(root, path);
572 if (owner < BTRFS_FIRST_FREE_OBJECTID)
573 new_size += sizeof(*bi);
575 new_size -= sizeof(*ei0);
576 ret = btrfs_search_slot(trans, root, &key, path, new_size, 1);
581 ret = btrfs_extend_item(trans, root, path, new_size);
584 leaf = path->nodes[0];
585 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
586 btrfs_set_extent_refs(leaf, item, refs);
587 /* FIXME: get real generation */
588 btrfs_set_extent_generation(leaf, item, 0);
589 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
590 btrfs_set_extent_flags(leaf, item,
591 BTRFS_EXTENT_FLAG_TREE_BLOCK |
592 BTRFS_BLOCK_FLAG_FULL_BACKREF);
593 bi = (struct btrfs_tree_block_info *)(item + 1);
594 /* FIXME: get first key of the block */
595 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
596 btrfs_set_tree_block_level(leaf, bi, (int)owner);
598 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
600 btrfs_mark_buffer_dirty(leaf);
605 static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
607 u32 high_crc = ~(u32)0;
608 u32 low_crc = ~(u32)0;
611 lenum = cpu_to_le64(root_objectid);
612 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
613 lenum = cpu_to_le64(owner);
614 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
615 lenum = cpu_to_le64(offset);
616 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
618 return ((u64)high_crc << 31) ^ (u64)low_crc;
621 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
622 struct btrfs_extent_data_ref *ref)
624 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
625 btrfs_extent_data_ref_objectid(leaf, ref),
626 btrfs_extent_data_ref_offset(leaf, ref));
629 static int match_extent_data_ref(struct extent_buffer *leaf,
630 struct btrfs_extent_data_ref *ref,
631 u64 root_objectid, u64 owner, u64 offset)
633 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
634 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
635 btrfs_extent_data_ref_offset(leaf, ref) != offset)
640 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
641 struct btrfs_root *root,
642 struct btrfs_path *path,
643 u64 bytenr, u64 parent,
645 u64 owner, u64 offset)
647 struct btrfs_key key;
648 struct btrfs_extent_data_ref *ref;
649 struct extent_buffer *leaf;
655 key.objectid = bytenr;
657 key.type = BTRFS_SHARED_DATA_REF_KEY;
660 key.type = BTRFS_EXTENT_DATA_REF_KEY;
661 key.offset = hash_extent_data_ref(root_objectid,
666 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
675 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
676 key.type = BTRFS_EXTENT_REF_V0_KEY;
677 btrfs_release_path(root, path);
678 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
689 leaf = path->nodes[0];
690 nritems = btrfs_header_nritems(leaf);
692 if (path->slots[0] >= nritems) {
693 ret = btrfs_next_leaf(root, path);
699 leaf = path->nodes[0];
700 nritems = btrfs_header_nritems(leaf);
704 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
705 if (key.objectid != bytenr ||
706 key.type != BTRFS_EXTENT_DATA_REF_KEY)
709 ref = btrfs_item_ptr(leaf, path->slots[0],
710 struct btrfs_extent_data_ref);
712 if (match_extent_data_ref(leaf, ref, root_objectid,
715 btrfs_release_path(root, path);
727 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
728 struct btrfs_root *root,
729 struct btrfs_path *path,
730 u64 bytenr, u64 parent,
731 u64 root_objectid, u64 owner,
732 u64 offset, int refs_to_add)
734 struct btrfs_key key;
735 struct extent_buffer *leaf;
740 key.objectid = bytenr;
742 key.type = BTRFS_SHARED_DATA_REF_KEY;
744 size = sizeof(struct btrfs_shared_data_ref);
746 key.type = BTRFS_EXTENT_DATA_REF_KEY;
747 key.offset = hash_extent_data_ref(root_objectid,
749 size = sizeof(struct btrfs_extent_data_ref);
752 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
753 if (ret && ret != -EEXIST)
756 leaf = path->nodes[0];
758 struct btrfs_shared_data_ref *ref;
759 ref = btrfs_item_ptr(leaf, path->slots[0],
760 struct btrfs_shared_data_ref);
762 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
764 num_refs = btrfs_shared_data_ref_count(leaf, ref);
765 num_refs += refs_to_add;
766 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
769 struct btrfs_extent_data_ref *ref;
770 while (ret == -EEXIST) {
771 ref = btrfs_item_ptr(leaf, path->slots[0],
772 struct btrfs_extent_data_ref);
773 if (match_extent_data_ref(leaf, ref, root_objectid,
776 btrfs_release_path(root, path);
779 ret = btrfs_insert_empty_item(trans, root, path, &key,
781 if (ret && ret != -EEXIST)
784 leaf = path->nodes[0];
786 ref = btrfs_item_ptr(leaf, path->slots[0],
787 struct btrfs_extent_data_ref);
789 btrfs_set_extent_data_ref_root(leaf, ref,
791 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
792 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
793 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
795 num_refs = btrfs_extent_data_ref_count(leaf, ref);
796 num_refs += refs_to_add;
797 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
800 btrfs_mark_buffer_dirty(leaf);
803 btrfs_release_path(root, path);
807 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
808 struct btrfs_root *root,
809 struct btrfs_path *path,
812 struct btrfs_key key;
813 struct btrfs_extent_data_ref *ref1 = NULL;
814 struct btrfs_shared_data_ref *ref2 = NULL;
815 struct extent_buffer *leaf;
819 leaf = path->nodes[0];
820 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
822 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
823 ref1 = btrfs_item_ptr(leaf, path->slots[0],
824 struct btrfs_extent_data_ref);
825 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
826 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
827 ref2 = btrfs_item_ptr(leaf, path->slots[0],
828 struct btrfs_shared_data_ref);
829 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
830 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
831 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
832 struct btrfs_extent_ref_v0 *ref0;
833 ref0 = btrfs_item_ptr(leaf, path->slots[0],
834 struct btrfs_extent_ref_v0);
835 num_refs = btrfs_ref_count_v0(leaf, ref0);
841 BUG_ON(num_refs < refs_to_drop);
842 num_refs -= refs_to_drop;
845 ret = btrfs_del_item(trans, root, path);
847 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
848 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
849 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
850 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
851 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
853 struct btrfs_extent_ref_v0 *ref0;
854 ref0 = btrfs_item_ptr(leaf, path->slots[0],
855 struct btrfs_extent_ref_v0);
856 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
859 btrfs_mark_buffer_dirty(leaf);
864 static noinline u32 extent_data_ref_count(struct btrfs_root *root,
865 struct btrfs_path *path,
866 struct btrfs_extent_inline_ref *iref)
868 struct btrfs_key key;
869 struct extent_buffer *leaf;
870 struct btrfs_extent_data_ref *ref1;
871 struct btrfs_shared_data_ref *ref2;
874 leaf = path->nodes[0];
875 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
877 if (btrfs_extent_inline_ref_type(leaf, iref) ==
878 BTRFS_EXTENT_DATA_REF_KEY) {
879 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
880 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
882 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
883 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
885 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
886 ref1 = btrfs_item_ptr(leaf, path->slots[0],
887 struct btrfs_extent_data_ref);
888 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
889 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
890 ref2 = btrfs_item_ptr(leaf, path->slots[0],
891 struct btrfs_shared_data_ref);
892 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
893 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
894 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
895 struct btrfs_extent_ref_v0 *ref0;
896 ref0 = btrfs_item_ptr(leaf, path->slots[0],
897 struct btrfs_extent_ref_v0);
898 num_refs = btrfs_ref_count_v0(leaf, ref0);
906 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
907 struct btrfs_root *root,
908 struct btrfs_path *path,
909 u64 bytenr, u64 parent,
912 struct btrfs_key key;
915 key.objectid = bytenr;
917 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
920 key.type = BTRFS_TREE_BLOCK_REF_KEY;
921 key.offset = root_objectid;
924 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
927 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
928 if (ret == -ENOENT && parent) {
929 btrfs_release_path(root, path);
930 key.type = BTRFS_EXTENT_REF_V0_KEY;
931 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
939 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
940 struct btrfs_root *root,
941 struct btrfs_path *path,
942 u64 bytenr, u64 parent,
945 struct btrfs_key key;
948 key.objectid = bytenr;
950 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
953 key.type = BTRFS_TREE_BLOCK_REF_KEY;
954 key.offset = root_objectid;
957 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
959 btrfs_release_path(root, path);
963 static inline int extent_ref_type(u64 parent, u64 owner)
965 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
967 return BTRFS_SHARED_BLOCK_REF_KEY;
969 return BTRFS_TREE_BLOCK_REF_KEY;
972 return BTRFS_SHARED_DATA_REF_KEY;
974 return BTRFS_EXTENT_DATA_REF_KEY;
978 static int find_next_key(struct btrfs_path *path, struct btrfs_key *key)
982 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
983 if (!path->nodes[level])
985 if (path->slots[level] + 1 >=
986 btrfs_header_nritems(path->nodes[level]))
989 btrfs_item_key_to_cpu(path->nodes[level], key,
990 path->slots[level] + 1);
992 btrfs_node_key_to_cpu(path->nodes[level], key,
993 path->slots[level] + 1);
999 static int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1000 struct btrfs_root *root,
1001 struct btrfs_path *path,
1002 struct btrfs_extent_inline_ref **ref_ret,
1003 u64 bytenr, u64 num_bytes,
1004 u64 parent, u64 root_objectid,
1005 u64 owner, u64 offset, int insert)
1007 struct btrfs_key key;
1008 struct extent_buffer *leaf;
1009 struct btrfs_extent_item *ei;
1010 struct btrfs_extent_inline_ref *iref;
1020 int skinny_metadata =
1021 btrfs_fs_incompat(root->fs_info,
1022 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
1024 key.objectid = bytenr;
1025 key.type = BTRFS_EXTENT_ITEM_KEY;
1026 key.offset = num_bytes;
1028 want = extent_ref_type(parent, owner);
1030 extra_size = btrfs_extent_inline_ref_size(want);
1034 if (owner < BTRFS_FIRST_FREE_OBJECTID && skinny_metadata) {
1035 skinny_metadata = 1;
1036 key.type = BTRFS_METADATA_ITEM_KEY;
1038 } else if (skinny_metadata) {
1039 skinny_metadata = 0;
1043 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1050 * We may be a newly converted file system which still has the old fat
1051 * extent entries for metadata, so try and see if we have one of those.
1053 if (ret > 0 && skinny_metadata) {
1054 skinny_metadata = 0;
1055 if (path->slots[0]) {
1057 btrfs_item_key_to_cpu(path->nodes[0], &key,
1059 if (key.objectid == bytenr &&
1060 key.type == BTRFS_EXTENT_ITEM_KEY &&
1061 key.offset == num_bytes)
1065 key.type = BTRFS_EXTENT_ITEM_KEY;
1066 key.offset = num_bytes;
1072 printf("Failed to find [%llu, %u, %llu]\n", key.objectid, key.type, key.offset);
1078 leaf = path->nodes[0];
1079 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1080 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1081 if (item_size < sizeof(*ei)) {
1086 ret = convert_extent_item_v0(trans, root, path, owner,
1092 leaf = path->nodes[0];
1093 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1096 if (item_size < sizeof(*ei)) {
1097 printf("Size is %u, needs to be %u, slot %d\n",
1098 (unsigned)item_size,
1099 (unsigned)sizeof(*ei), path->slots[0]);
1100 btrfs_print_leaf(root, leaf);
1103 BUG_ON(item_size < sizeof(*ei));
1105 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1106 flags = btrfs_extent_flags(leaf, ei);
1108 ptr = (unsigned long)(ei + 1);
1109 end = (unsigned long)ei + item_size;
1111 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
1112 ptr += sizeof(struct btrfs_tree_block_info);
1114 } else if (!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
1115 if (!(flags & BTRFS_EXTENT_FLAG_DATA)) {
1126 iref = (struct btrfs_extent_inline_ref *)ptr;
1127 type = btrfs_extent_inline_ref_type(leaf, iref);
1131 ptr += btrfs_extent_inline_ref_size(type);
1135 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1136 struct btrfs_extent_data_ref *dref;
1137 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1138 if (match_extent_data_ref(leaf, dref, root_objectid,
1143 if (hash_extent_data_ref_item(leaf, dref) <
1144 hash_extent_data_ref(root_objectid, owner, offset))
1148 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1150 if (parent == ref_offset) {
1154 if (ref_offset < parent)
1157 if (root_objectid == ref_offset) {
1161 if (ref_offset < root_objectid)
1165 ptr += btrfs_extent_inline_ref_size(type);
1167 if (err == -ENOENT && insert) {
1168 if (item_size + extra_size >=
1169 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1174 * To add new inline back ref, we have to make sure
1175 * there is no corresponding back ref item.
1176 * For simplicity, we just do not add new inline back
1177 * ref if there is any back ref item.
1179 if (find_next_key(path, &key) == 0 && key.objectid == bytenr &&
1180 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
1185 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1190 static int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1191 struct btrfs_root *root,
1192 struct btrfs_path *path,
1193 struct btrfs_extent_inline_ref *iref,
1194 u64 parent, u64 root_objectid,
1195 u64 owner, u64 offset, int refs_to_add)
1197 struct extent_buffer *leaf;
1198 struct btrfs_extent_item *ei;
1201 unsigned long item_offset;
1207 leaf = path->nodes[0];
1208 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1209 item_offset = (unsigned long)iref - (unsigned long)ei;
1211 type = extent_ref_type(parent, owner);
1212 size = btrfs_extent_inline_ref_size(type);
1214 ret = btrfs_extend_item(trans, root, path, size);
1217 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1218 refs = btrfs_extent_refs(leaf, ei);
1219 refs += refs_to_add;
1220 btrfs_set_extent_refs(leaf, ei, refs);
1222 ptr = (unsigned long)ei + item_offset;
1223 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1224 if (ptr < end - size)
1225 memmove_extent_buffer(leaf, ptr + size, ptr,
1228 iref = (struct btrfs_extent_inline_ref *)ptr;
1229 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1230 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1231 struct btrfs_extent_data_ref *dref;
1232 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1233 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1234 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1235 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1236 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1237 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1238 struct btrfs_shared_data_ref *sref;
1239 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1240 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1241 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1242 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1243 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1245 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1247 btrfs_mark_buffer_dirty(leaf);
1251 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1252 struct btrfs_root *root,
1253 struct btrfs_path *path,
1254 struct btrfs_extent_inline_ref **ref_ret,
1255 u64 bytenr, u64 num_bytes, u64 parent,
1256 u64 root_objectid, u64 owner, u64 offset)
1260 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1261 bytenr, num_bytes, parent,
1262 root_objectid, owner, offset, 0);
1266 btrfs_release_path(root, path);
1269 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1270 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1273 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1274 root_objectid, owner, offset);
1279 static int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1280 struct btrfs_root *root,
1281 struct btrfs_path *path,
1282 struct btrfs_extent_inline_ref *iref,
1285 struct extent_buffer *leaf;
1286 struct btrfs_extent_item *ei;
1287 struct btrfs_extent_data_ref *dref = NULL;
1288 struct btrfs_shared_data_ref *sref = NULL;
1297 leaf = path->nodes[0];
1298 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1299 refs = btrfs_extent_refs(leaf, ei);
1300 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1301 refs += refs_to_mod;
1302 btrfs_set_extent_refs(leaf, ei, refs);
1304 type = btrfs_extent_inline_ref_type(leaf, iref);
1306 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1307 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1308 refs = btrfs_extent_data_ref_count(leaf, dref);
1309 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1310 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1311 refs = btrfs_shared_data_ref_count(leaf, sref);
1314 BUG_ON(refs_to_mod != -1);
1317 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1318 refs += refs_to_mod;
1321 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1322 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1324 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1326 size = btrfs_extent_inline_ref_size(type);
1327 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1328 ptr = (unsigned long)iref;
1329 end = (unsigned long)ei + item_size;
1330 if (ptr + size < end)
1331 memmove_extent_buffer(leaf, ptr, ptr + size,
1334 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1337 btrfs_mark_buffer_dirty(leaf);
1341 static int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1342 struct btrfs_root *root,
1343 struct btrfs_path *path,
1344 u64 bytenr, u64 num_bytes, u64 parent,
1345 u64 root_objectid, u64 owner,
1346 u64 offset, int refs_to_add)
1348 struct btrfs_extent_inline_ref *iref;
1351 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1352 bytenr, num_bytes, parent,
1353 root_objectid, owner, offset, 1);
1355 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1356 ret = update_inline_extent_backref(trans, root, path, iref,
1358 } else if (ret == -ENOENT) {
1359 ret = setup_inline_extent_backref(trans, root, path, iref,
1360 parent, root_objectid,
1361 owner, offset, refs_to_add);
1366 static int insert_extent_backref(struct btrfs_trans_handle *trans,
1367 struct btrfs_root *root,
1368 struct btrfs_path *path,
1369 u64 bytenr, u64 parent, u64 root_objectid,
1370 u64 owner, u64 offset, int refs_to_add)
1374 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
1375 ret = insert_extent_data_ref(trans, root, path, bytenr,
1376 parent, root_objectid,
1377 owner, offset, refs_to_add);
1379 BUG_ON(refs_to_add != 1);
1380 ret = insert_tree_block_ref(trans, root, path, bytenr,
1381 parent, root_objectid);
1386 static int remove_extent_backref(struct btrfs_trans_handle *trans,
1387 struct btrfs_root *root,
1388 struct btrfs_path *path,
1389 struct btrfs_extent_inline_ref *iref,
1390 int refs_to_drop, int is_data)
1394 BUG_ON(!is_data && refs_to_drop != 1);
1396 ret = update_inline_extent_backref(trans, root, path, iref,
1398 } else if (is_data) {
1399 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1401 ret = btrfs_del_item(trans, root, path);
1406 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1407 struct btrfs_root *root,
1408 u64 bytenr, u64 num_bytes, u64 parent,
1409 u64 root_objectid, u64 owner, u64 offset)
1411 struct btrfs_path *path;
1412 struct extent_buffer *leaf;
1413 struct btrfs_extent_item *item;
1418 path = btrfs_alloc_path();
1423 path->leave_spinning = 1;
1425 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1426 path, bytenr, num_bytes, parent,
1427 root_objectid, owner, offset, 1);
1431 if (ret != -EAGAIN) {
1436 leaf = path->nodes[0];
1437 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1438 refs = btrfs_extent_refs(leaf, item);
1439 btrfs_set_extent_refs(leaf, item, refs + 1);
1441 btrfs_mark_buffer_dirty(leaf);
1442 btrfs_release_path(root->fs_info->extent_root, path);
1445 path->leave_spinning = 1;
1447 /* now insert the actual backref */
1448 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1449 path, bytenr, parent, root_objectid,
1454 btrfs_free_path(path);
1455 finish_current_insert(trans, root->fs_info->extent_root);
1456 del_pending_extents(trans, root->fs_info->extent_root);
1461 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1462 struct btrfs_root *root)
1464 finish_current_insert(trans, root->fs_info->extent_root);
1465 del_pending_extents(trans, root->fs_info->extent_root);
1469 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
1470 struct btrfs_root *root, u64 bytenr,
1471 u64 offset, int metadata, u64 *refs, u64 *flags)
1473 struct btrfs_path *path;
1475 struct btrfs_key key;
1476 struct extent_buffer *l;
1477 struct btrfs_extent_item *item;
1483 !btrfs_fs_incompat(root->fs_info,
1484 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA)) {
1485 offset = root->leafsize;
1489 path = btrfs_alloc_path();
1492 key.objectid = bytenr;
1493 key.offset = offset;
1495 key.type = BTRFS_METADATA_ITEM_KEY;
1497 key.type = BTRFS_EXTENT_ITEM_KEY;
1500 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1506 * Deal with the fact that we may have mixed SKINNY and normal refs. If
1507 * we didn't find what we wanted check and see if we have a normal ref
1508 * right next to us, or re-search if we are on the edge of the leaf just
1511 if (ret > 0 && metadata) {
1514 btrfs_item_key_to_cpu(path->nodes[0], &key,
1516 if (key.objectid == bytenr &&
1517 key.type == BTRFS_METADATA_ITEM_KEY)
1522 btrfs_release_path(root, path);
1523 key.type = BTRFS_EXTENT_ITEM_KEY;
1524 key.offset = root->leafsize;
1536 item_size = btrfs_item_size_nr(l, path->slots[0]);
1537 if (item_size >= sizeof(*item)) {
1538 item = btrfs_item_ptr(l, path->slots[0],
1539 struct btrfs_extent_item);
1540 num_refs = btrfs_extent_refs(l, item);
1541 extent_flags = btrfs_extent_flags(l, item);
1543 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1544 struct btrfs_extent_item_v0 *ei0;
1545 BUG_ON(item_size != sizeof(*ei0));
1546 ei0 = btrfs_item_ptr(l, path->slots[0],
1547 struct btrfs_extent_item_v0);
1548 num_refs = btrfs_extent_refs_v0(l, ei0);
1549 /* FIXME: this isn't correct for data */
1550 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
1555 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1559 *flags = extent_flags;
1561 btrfs_free_path(path);
1565 int btrfs_set_block_flags(struct btrfs_trans_handle *trans,
1566 struct btrfs_root *root,
1567 u64 bytenr, int level, u64 flags)
1569 struct btrfs_path *path;
1571 struct btrfs_key key;
1572 struct extent_buffer *l;
1573 struct btrfs_extent_item *item;
1575 int skinny_metadata =
1576 btrfs_fs_incompat(root->fs_info,
1577 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
1579 path = btrfs_alloc_path();
1582 key.objectid = bytenr;
1583 if (skinny_metadata) {
1585 key.type = BTRFS_METADATA_ITEM_KEY;
1587 key.offset = root->leafsize;
1588 key.type = BTRFS_EXTENT_ITEM_KEY;
1592 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1597 if (ret > 0 && skinny_metadata) {
1598 skinny_metadata = 0;
1599 if (path->slots[0]--) {
1601 btrfs_item_key_to_cpu(path->nodes[0], &key,
1603 if (key.objectid == bytenr &&
1604 key.offset == root->leafsize &&
1605 key.type == BTRFS_EXTENT_ITEM_KEY)
1609 btrfs_release_path(root, path);
1610 key.offset = root->leafsize;
1611 key.type = BTRFS_EXTENT_ITEM_KEY;
1617 btrfs_print_leaf(root, path->nodes[0]);
1618 printk("failed to find block number %Lu\n",
1619 (unsigned long long)bytenr);
1623 item_size = btrfs_item_size_nr(l, path->slots[0]);
1624 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1625 if (item_size < sizeof(*item)) {
1626 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1632 item_size = btrfs_item_size_nr(l, path->slots[0]);
1635 BUG_ON(item_size < sizeof(*item));
1636 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1637 flags |= btrfs_extent_flags(l, item);
1638 btrfs_set_extent_flags(l, item, flags);
1640 btrfs_free_path(path);
1641 finish_current_insert(trans, root->fs_info->extent_root);
1642 del_pending_extents(trans, root->fs_info->extent_root);
1646 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
1647 struct btrfs_root *root,
1648 struct extent_buffer *buf,
1649 int record_parent, int inc)
1656 struct btrfs_key key;
1657 struct btrfs_file_extent_item *fi;
1661 int (*process_func)(struct btrfs_trans_handle *trans,
1662 struct btrfs_root *root,
1663 u64, u64, u64, u64, u64, u64);
1665 ref_root = btrfs_header_owner(buf);
1666 nritems = btrfs_header_nritems(buf);
1667 level = btrfs_header_level(buf);
1669 if (!root->ref_cows && level == 0)
1673 process_func = btrfs_inc_extent_ref;
1675 process_func = btrfs_free_extent;
1678 parent = buf->start;
1682 for (i = 0; i < nritems; i++) {
1685 btrfs_item_key_to_cpu(buf, &key, i);
1686 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1688 fi = btrfs_item_ptr(buf, i,
1689 struct btrfs_file_extent_item);
1690 if (btrfs_file_extent_type(buf, fi) ==
1691 BTRFS_FILE_EXTENT_INLINE)
1693 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1697 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
1698 key.offset -= btrfs_file_extent_offset(buf, fi);
1699 ret = process_func(trans, root, bytenr, num_bytes,
1700 parent, ref_root, key.objectid,
1707 bytenr = btrfs_node_blockptr(buf, i);
1708 num_bytes = btrfs_level_size(root, level - 1);
1709 ret = process_func(trans, root, bytenr, num_bytes,
1710 parent, ref_root, level - 1, 0);
1723 int btrfs_inc_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, 1);
1729 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1730 struct extent_buffer *buf, int record_parent)
1732 return __btrfs_mod_ref(trans, root, buf, record_parent, 0);
1735 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1736 struct btrfs_root *root,
1737 struct btrfs_path *path,
1738 struct btrfs_block_group_cache *cache)
1742 struct btrfs_root *extent_root = root->fs_info->extent_root;
1744 struct extent_buffer *leaf;
1746 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1751 leaf = path->nodes[0];
1752 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1753 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1754 btrfs_mark_buffer_dirty(leaf);
1755 btrfs_release_path(extent_root, path);
1757 finish_current_insert(trans, extent_root);
1758 pending_ret = del_pending_extents(trans, extent_root);
1767 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1768 struct btrfs_root *root)
1770 struct extent_io_tree *block_group_cache;
1771 struct btrfs_block_group_cache *cache;
1773 struct btrfs_path *path;
1779 block_group_cache = &root->fs_info->block_group_cache;
1780 path = btrfs_alloc_path();
1785 ret = find_first_extent_bit(block_group_cache, last,
1786 &start, &end, BLOCK_GROUP_DIRTY);
1795 ret = get_state_private(block_group_cache, start, &ptr);
1798 clear_extent_bits(block_group_cache, start, end,
1799 BLOCK_GROUP_DIRTY, GFP_NOFS);
1801 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
1802 ret = write_one_cache_group(trans, root, path, cache);
1804 btrfs_free_path(path);
1808 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
1811 struct list_head *head = &info->space_info;
1812 struct list_head *cur;
1813 struct btrfs_space_info *found;
1814 list_for_each(cur, head) {
1815 found = list_entry(cur, struct btrfs_space_info, list);
1816 if (found->flags & flags)
1823 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1824 u64 total_bytes, u64 bytes_used,
1825 struct btrfs_space_info **space_info)
1827 struct btrfs_space_info *found;
1829 found = __find_space_info(info, flags);
1831 found->total_bytes += total_bytes;
1832 found->bytes_used += bytes_used;
1833 if (found->total_bytes < found->bytes_used) {
1834 fprintf(stderr, "warning, bad space info total_bytes "
1836 (unsigned long long)found->total_bytes,
1837 (unsigned long long)found->bytes_used);
1839 *space_info = found;
1842 found = kmalloc(sizeof(*found), GFP_NOFS);
1846 list_add(&found->list, &info->space_info);
1847 found->flags = flags;
1848 found->total_bytes = total_bytes;
1849 found->bytes_used = bytes_used;
1850 found->bytes_pinned = 0;
1852 *space_info = found;
1857 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1859 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1860 BTRFS_BLOCK_GROUP_RAID1 |
1861 BTRFS_BLOCK_GROUP_RAID10 |
1862 BTRFS_BLOCK_GROUP_RAID5 |
1863 BTRFS_BLOCK_GROUP_RAID6 |
1864 BTRFS_BLOCK_GROUP_DUP);
1866 if (flags & BTRFS_BLOCK_GROUP_DATA)
1867 fs_info->avail_data_alloc_bits |= extra_flags;
1868 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1869 fs_info->avail_metadata_alloc_bits |= extra_flags;
1870 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1871 fs_info->avail_system_alloc_bits |= extra_flags;
1875 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1876 struct btrfs_root *extent_root, u64 alloc_bytes,
1879 struct btrfs_space_info *space_info;
1885 space_info = __find_space_info(extent_root->fs_info, flags);
1887 ret = update_space_info(extent_root->fs_info, flags,
1891 BUG_ON(!space_info);
1893 if (space_info->full)
1896 thresh = div_factor(space_info->total_bytes, 7);
1897 if ((space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
1901 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes,
1903 if (ret == -ENOSPC) {
1904 space_info->full = 1;
1910 ret = btrfs_make_block_group(trans, extent_root, 0, space_info->flags,
1911 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
1916 static int update_block_group(struct btrfs_trans_handle *trans,
1917 struct btrfs_root *root,
1918 u64 bytenr, u64 num_bytes, int alloc,
1921 struct btrfs_block_group_cache *cache;
1922 struct btrfs_fs_info *info = root->fs_info;
1923 u64 total = num_bytes;
1929 /* block accounting for super block */
1930 old_val = btrfs_super_bytes_used(info->super_copy);
1932 old_val += num_bytes;
1934 old_val -= num_bytes;
1935 btrfs_set_super_bytes_used(info->super_copy, old_val);
1937 /* block accounting for root item */
1938 old_val = btrfs_root_used(&root->root_item);
1940 old_val += num_bytes;
1942 old_val -= num_bytes;
1943 btrfs_set_root_used(&root->root_item, old_val);
1946 cache = btrfs_lookup_block_group(info, bytenr);
1950 byte_in_group = bytenr - cache->key.objectid;
1951 WARN_ON(byte_in_group > cache->key.offset);
1952 start = cache->key.objectid;
1953 end = start + cache->key.offset - 1;
1954 set_extent_bits(&info->block_group_cache, start, end,
1955 BLOCK_GROUP_DIRTY, GFP_NOFS);
1957 old_val = btrfs_block_group_used(&cache->item);
1958 num_bytes = min(total, cache->key.offset - byte_in_group);
1961 old_val += num_bytes;
1962 cache->space_info->bytes_used += num_bytes;
1964 old_val -= num_bytes;
1965 cache->space_info->bytes_used -= num_bytes;
1967 set_extent_dirty(&info->free_space_cache,
1968 bytenr, bytenr + num_bytes - 1,
1972 btrfs_set_block_group_used(&cache->item, old_val);
1974 bytenr += num_bytes;
1979 static int update_pinned_extents(struct btrfs_root *root,
1980 u64 bytenr, u64 num, int pin)
1983 struct btrfs_block_group_cache *cache;
1984 struct btrfs_fs_info *fs_info = root->fs_info;
1987 set_extent_dirty(&fs_info->pinned_extents,
1988 bytenr, bytenr + num - 1, GFP_NOFS);
1990 clear_extent_dirty(&fs_info->pinned_extents,
1991 bytenr, bytenr + num - 1, GFP_NOFS);
1994 cache = btrfs_lookup_block_group(fs_info, bytenr);
1996 len = min((u64)root->sectorsize, num);
2000 len = min(num, cache->key.offset -
2001 (bytenr - cache->key.objectid));
2003 cache->pinned += len;
2004 cache->space_info->bytes_pinned += len;
2005 fs_info->total_pinned += len;
2007 cache->pinned -= len;
2008 cache->space_info->bytes_pinned -= len;
2009 fs_info->total_pinned -= len;
2018 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
2023 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
2027 ret = find_first_extent_bit(pinned_extents, last,
2028 &start, &end, EXTENT_DIRTY);
2031 set_extent_dirty(copy, start, end, GFP_NOFS);
2037 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2038 struct btrfs_root *root,
2039 struct extent_io_tree *unpin)
2044 struct extent_io_tree *free_space_cache;
2045 free_space_cache = &root->fs_info->free_space_cache;
2048 ret = find_first_extent_bit(unpin, 0, &start, &end,
2052 update_pinned_extents(root, start, end + 1 - start, 0);
2053 clear_extent_dirty(unpin, start, end, GFP_NOFS);
2054 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
2059 static int extent_root_pending_ops(struct btrfs_fs_info *info)
2065 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
2066 &end, EXTENT_LOCKED);
2068 ret = find_first_extent_bit(&info->pending_del, 0, &start, &end,
2074 static int finish_current_insert(struct btrfs_trans_handle *trans,
2075 struct btrfs_root *extent_root)
2080 struct btrfs_fs_info *info = extent_root->fs_info;
2081 struct btrfs_path *path;
2082 struct pending_extent_op *extent_op;
2083 struct btrfs_key key;
2085 int skinny_metadata =
2086 btrfs_fs_incompat(extent_root->fs_info,
2087 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
2089 path = btrfs_alloc_path();
2092 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
2093 &end, EXTENT_LOCKED);
2097 ret = get_state_private(&info->extent_ins, start, &priv);
2099 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2101 if (extent_op->type == PENDING_EXTENT_INSERT) {
2102 key.objectid = start;
2103 if (skinny_metadata) {
2104 key.offset = extent_op->level;
2105 key.type = BTRFS_METADATA_ITEM_KEY;
2107 key.offset = extent_op->num_bytes;
2108 key.type = BTRFS_EXTENT_ITEM_KEY;
2110 ret = alloc_reserved_tree_block(trans, extent_root,
2111 extent_root->root_key.objectid,
2115 extent_op->level, &key);
2120 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
2124 btrfs_free_path(path);
2128 static int pin_down_bytes(struct btrfs_trans_handle *trans,
2129 struct btrfs_root *root,
2130 u64 bytenr, u64 num_bytes, int is_data)
2133 struct extent_buffer *buf;
2138 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2142 /* we can reuse a block if it hasn't been written
2143 * and it is from this transaction. We can't
2144 * reuse anything from the tree log root because
2145 * it has tiny sub-transactions.
2147 if (btrfs_buffer_uptodate(buf, 0)) {
2148 u64 header_owner = btrfs_header_owner(buf);
2149 u64 header_transid = btrfs_header_generation(buf);
2150 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
2151 header_transid == trans->transid &&
2152 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2153 clean_tree_block(NULL, root, buf);
2154 free_extent_buffer(buf);
2158 free_extent_buffer(buf);
2160 update_pinned_extents(root, bytenr, num_bytes, 1);
2166 void btrfs_pin_extent(struct btrfs_fs_info *fs_info,
2167 u64 bytenr, u64 num_bytes)
2169 update_pinned_extents(fs_info->extent_root, bytenr, num_bytes, 1);
2172 void btrfs_unpin_extent(struct btrfs_fs_info *fs_info,
2173 u64 bytenr, u64 num_bytes)
2175 update_pinned_extents(fs_info->extent_root, bytenr, num_bytes, 0);
2179 * remove an extent from the root, returns 0 on success
2181 static int __free_extent(struct btrfs_trans_handle *trans,
2182 struct btrfs_root *root,
2183 u64 bytenr, u64 num_bytes, u64 parent,
2184 u64 root_objectid, u64 owner_objectid,
2185 u64 owner_offset, int refs_to_drop)
2188 struct btrfs_key key;
2189 struct btrfs_path *path;
2190 struct btrfs_extent_ops *ops = root->fs_info->extent_ops;
2191 struct btrfs_root *extent_root = root->fs_info->extent_root;
2192 struct extent_buffer *leaf;
2193 struct btrfs_extent_item *ei;
2194 struct btrfs_extent_inline_ref *iref;
2197 int extent_slot = 0;
2198 int found_extent = 0;
2202 int skinny_metadata =
2203 btrfs_fs_incompat(extent_root->fs_info,
2204 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
2206 if (root->fs_info->free_extent_hook) {
2207 root->fs_info->free_extent_hook(trans, root, bytenr, num_bytes,
2208 parent, root_objectid, owner_objectid,
2209 owner_offset, refs_to_drop);
2212 path = btrfs_alloc_path();
2217 path->leave_spinning = 1;
2219 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
2221 skinny_metadata = 0;
2222 BUG_ON(!is_data && refs_to_drop != 1);
2224 ret = lookup_extent_backref(trans, extent_root, path, &iref,
2225 bytenr, num_bytes, parent,
2226 root_objectid, owner_objectid,
2229 extent_slot = path->slots[0];
2230 while (extent_slot >= 0) {
2231 btrfs_item_key_to_cpu(path->nodes[0], &key,
2233 if (key.objectid != bytenr)
2235 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
2236 key.offset == num_bytes) {
2240 if (key.type == BTRFS_METADATA_ITEM_KEY &&
2241 key.offset == owner_objectid) {
2245 if (path->slots[0] - extent_slot > 5)
2249 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2250 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
2251 if (found_extent && item_size < sizeof(*ei))
2254 if (!found_extent) {
2256 ret = remove_extent_backref(trans, extent_root, path,
2260 btrfs_release_path(extent_root, path);
2261 path->leave_spinning = 1;
2263 key.objectid = bytenr;
2265 if (skinny_metadata) {
2266 key.type = BTRFS_METADATA_ITEM_KEY;
2267 key.offset = owner_objectid;
2269 key.type = BTRFS_EXTENT_ITEM_KEY;
2270 key.offset = num_bytes;
2273 ret = btrfs_search_slot(trans, extent_root,
2275 if (ret > 0 && skinny_metadata && path->slots[0]) {
2277 btrfs_item_key_to_cpu(path->nodes[0],
2280 if (key.objectid == bytenr &&
2281 key.type == BTRFS_EXTENT_ITEM_KEY &&
2282 key.offset == num_bytes)
2286 if (ret > 0 && skinny_metadata) {
2287 skinny_metadata = 0;
2288 btrfs_release_path(extent_root, path);
2289 key.type = BTRFS_EXTENT_ITEM_KEY;
2290 key.offset = num_bytes;
2291 ret = btrfs_search_slot(trans, extent_root,
2296 printk(KERN_ERR "umm, got %d back from search"
2297 ", was looking for %llu\n", ret,
2298 (unsigned long long)bytenr);
2299 btrfs_print_leaf(extent_root, path->nodes[0]);
2302 extent_slot = path->slots[0];
2305 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
2306 "parent %llu root %llu owner %llu offset %llu\n",
2307 (unsigned long long)bytenr,
2308 (unsigned long long)parent,
2309 (unsigned long long)root_objectid,
2310 (unsigned long long)owner_objectid,
2311 (unsigned long long)owner_offset);
2316 leaf = path->nodes[0];
2317 item_size = btrfs_item_size_nr(leaf, extent_slot);
2318 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2319 if (item_size < sizeof(*ei)) {
2320 BUG_ON(found_extent || extent_slot != path->slots[0]);
2321 ret = convert_extent_item_v0(trans, extent_root, path,
2325 btrfs_release_path(extent_root, path);
2326 path->leave_spinning = 1;
2328 key.objectid = bytenr;
2329 key.type = BTRFS_EXTENT_ITEM_KEY;
2330 key.offset = num_bytes;
2332 ret = btrfs_search_slot(trans, extent_root, &key, path,
2335 printk(KERN_ERR "umm, got %d back from search"
2336 ", was looking for %llu\n", ret,
2337 (unsigned long long)bytenr);
2338 btrfs_print_leaf(extent_root, path->nodes[0]);
2341 extent_slot = path->slots[0];
2342 leaf = path->nodes[0];
2343 item_size = btrfs_item_size_nr(leaf, extent_slot);
2346 BUG_ON(item_size < sizeof(*ei));
2347 ei = btrfs_item_ptr(leaf, extent_slot,
2348 struct btrfs_extent_item);
2349 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
2350 key.type == BTRFS_EXTENT_ITEM_KEY) {
2351 struct btrfs_tree_block_info *bi;
2352 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
2353 bi = (struct btrfs_tree_block_info *)(ei + 1);
2354 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
2357 refs = btrfs_extent_refs(leaf, ei);
2358 BUG_ON(refs < refs_to_drop);
2359 refs -= refs_to_drop;
2363 * In the case of inline back ref, reference count will
2364 * be updated by remove_extent_backref
2367 BUG_ON(!found_extent);
2369 btrfs_set_extent_refs(leaf, ei, refs);
2370 btrfs_mark_buffer_dirty(leaf);
2373 ret = remove_extent_backref(trans, extent_root, path,
2383 BUG_ON(is_data && refs_to_drop !=
2384 extent_data_ref_count(root, path, iref));
2386 BUG_ON(path->slots[0] != extent_slot);
2388 BUG_ON(path->slots[0] != extent_slot + 1);
2389 path->slots[0] = extent_slot;
2394 if (ops && ops->free_extent) {
2395 ret = ops->free_extent(root, bytenr, num_bytes);
2403 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2410 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2413 btrfs_release_path(extent_root, path);
2416 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2420 update_block_group(trans, root, bytenr, num_bytes, 0, mark_free);
2423 btrfs_free_path(path);
2424 finish_current_insert(trans, extent_root);
2429 * find all the blocks marked as pending in the radix tree and remove
2430 * them from the extent map
2432 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
2433 btrfs_root *extent_root)
2440 struct extent_io_tree *pending_del;
2441 struct extent_io_tree *extent_ins;
2442 struct pending_extent_op *extent_op;
2444 extent_ins = &extent_root->fs_info->extent_ins;
2445 pending_del = &extent_root->fs_info->pending_del;
2448 ret = find_first_extent_bit(pending_del, 0, &start, &end,
2453 ret = get_state_private(pending_del, start, &priv);
2455 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2457 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
2460 if (!test_range_bit(extent_ins, start, end,
2461 EXTENT_LOCKED, 0)) {
2462 ret = __free_extent(trans, extent_root,
2463 start, end + 1 - start, 0,
2464 extent_root->root_key.objectid,
2465 extent_op->level, 0, 1);
2469 ret = get_state_private(extent_ins, start, &priv);
2471 extent_op = (struct pending_extent_op *)
2472 (unsigned long)priv;
2474 clear_extent_bits(extent_ins, start, end,
2475 EXTENT_LOCKED, GFP_NOFS);
2477 if (extent_op->type == PENDING_BACKREF_UPDATE)
2489 * remove an extent from the root, returns 0 on success
2492 int btrfs_free_extent(struct btrfs_trans_handle *trans,
2493 struct btrfs_root *root,
2494 u64 bytenr, u64 num_bytes, u64 parent,
2495 u64 root_objectid, u64 owner, u64 offset)
2497 struct btrfs_root *extent_root = root->fs_info->extent_root;
2501 WARN_ON(num_bytes < root->sectorsize);
2502 if (root == extent_root) {
2503 struct pending_extent_op *extent_op;
2505 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2508 extent_op->type = PENDING_EXTENT_DELETE;
2509 extent_op->bytenr = bytenr;
2510 extent_op->num_bytes = num_bytes;
2511 extent_op->level = (int)owner;
2513 set_extent_bits(&root->fs_info->pending_del,
2514 bytenr, bytenr + num_bytes - 1,
2515 EXTENT_LOCKED, GFP_NOFS);
2516 set_state_private(&root->fs_info->pending_del,
2517 bytenr, (unsigned long)extent_op);
2520 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2521 root_objectid, owner, offset, 1);
2522 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
2523 return ret ? ret : pending_ret;
2526 static u64 stripe_align(struct btrfs_root *root, u64 val)
2528 u64 mask = ((u64)root->stripesize - 1);
2529 u64 ret = (val + mask) & ~mask;
2534 * walks the btree of allocated extents and find a hole of a given size.
2535 * The key ins is changed to record the hole:
2536 * ins->objectid == block start
2537 * ins->flags = BTRFS_EXTENT_ITEM_KEY
2538 * ins->offset == number of blocks
2539 * Any available blocks before search_start are skipped.
2541 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2542 struct btrfs_root *orig_root,
2543 u64 num_bytes, u64 empty_size,
2544 u64 search_start, u64 search_end,
2545 u64 hint_byte, struct btrfs_key *ins,
2546 u64 exclude_start, u64 exclude_nr,
2550 u64 orig_search_start = search_start;
2551 struct btrfs_root * root = orig_root->fs_info->extent_root;
2552 struct btrfs_fs_info *info = root->fs_info;
2553 u64 total_needed = num_bytes;
2554 struct btrfs_block_group_cache *block_group;
2558 WARN_ON(num_bytes < root->sectorsize);
2559 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2561 search_start = stripe_align(root, search_start);
2564 block_group = btrfs_lookup_first_block_group(info, hint_byte);
2566 hint_byte = search_start;
2567 block_group = btrfs_find_block_group(root, block_group,
2568 hint_byte, data, 1);
2570 block_group = btrfs_find_block_group(root,
2572 search_start, data, 1);
2575 total_needed += empty_size;
2578 search_start = stripe_align(root, search_start);
2580 block_group = btrfs_lookup_first_block_group(info,
2583 block_group = btrfs_lookup_first_block_group(info,
2586 ret = find_search_start(root, &block_group, &search_start,
2587 total_needed, data);
2591 ins->objectid = search_start;
2592 ins->offset = num_bytes;
2594 if (ins->objectid + num_bytes >
2595 block_group->key.objectid + block_group->key.offset) {
2596 search_start = block_group->key.objectid +
2597 block_group->key.offset;
2601 if (test_range_bit(&info->extent_ins, ins->objectid,
2602 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
2603 search_start = ins->objectid + num_bytes;
2607 if (test_range_bit(&info->pinned_extents, ins->objectid,
2608 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
2609 search_start = ins->objectid + num_bytes;
2613 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
2614 ins->objectid < exclude_start + exclude_nr)) {
2615 search_start = exclude_start + exclude_nr;
2619 if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
2620 block_group = btrfs_lookup_block_group(info, ins->objectid);
2622 trans->block_group = block_group;
2624 ins->offset = num_bytes;
2628 block_group = btrfs_lookup_first_block_group(info, search_start);
2630 search_start = orig_search_start;
2637 total_needed -= empty_size;
2643 block_group = btrfs_find_block_group(root, block_group,
2644 search_start, data, 0);
2651 static int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2652 struct btrfs_root *root,
2653 u64 num_bytes, u64 empty_size,
2654 u64 hint_byte, u64 search_end,
2655 struct btrfs_key *ins, int data)
2658 u64 search_start = 0;
2660 struct btrfs_fs_info *info = root->fs_info;
2662 if (info->extent_ops) {
2663 struct btrfs_extent_ops *ops = info->extent_ops;
2664 ret = ops->alloc_extent(root, num_bytes, hint_byte, ins);
2670 alloc_profile = info->avail_data_alloc_bits &
2671 info->data_alloc_profile;
2672 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2673 } else if ((info->system_allocs > 0 || root == info->chunk_root) &&
2674 info->system_allocs >= 0) {
2675 alloc_profile = info->avail_system_alloc_bits &
2676 info->system_alloc_profile;
2677 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2679 alloc_profile = info->avail_metadata_alloc_bits &
2680 info->metadata_alloc_profile;
2681 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2684 if (root->ref_cows) {
2685 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
2686 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2688 BTRFS_BLOCK_GROUP_METADATA);
2691 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2692 num_bytes + 2 * 1024 * 1024, data);
2696 WARN_ON(num_bytes < root->sectorsize);
2697 ret = find_free_extent(trans, root, num_bytes, empty_size,
2698 search_start, search_end, hint_byte, ins,
2699 trans->alloc_exclude_start,
2700 trans->alloc_exclude_nr, data);
2703 clear_extent_dirty(&root->fs_info->free_space_cache,
2704 ins->objectid, ins->objectid + ins->offset - 1,
2709 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
2710 struct btrfs_root *root,
2711 u64 root_objectid, u64 generation,
2712 u64 flags, struct btrfs_disk_key *key,
2713 int level, struct btrfs_key *ins)
2716 struct btrfs_fs_info *fs_info = root->fs_info;
2717 struct btrfs_extent_item *extent_item;
2718 struct btrfs_tree_block_info *block_info;
2719 struct btrfs_extent_inline_ref *iref;
2720 struct btrfs_path *path;
2721 struct extent_buffer *leaf;
2722 u32 size = sizeof(*extent_item) + sizeof(*iref);
2723 int skinny_metadata =
2724 btrfs_fs_incompat(fs_info,
2725 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
2727 if (!skinny_metadata)
2728 size += sizeof(*block_info);
2730 path = btrfs_alloc_path();
2733 path->leave_spinning = 1;
2734 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
2738 leaf = path->nodes[0];
2739 extent_item = btrfs_item_ptr(leaf, path->slots[0],
2740 struct btrfs_extent_item);
2741 btrfs_set_extent_refs(leaf, extent_item, 1);
2742 btrfs_set_extent_generation(leaf, extent_item, generation);
2743 btrfs_set_extent_flags(leaf, extent_item,
2744 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
2746 if (skinny_metadata) {
2747 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
2749 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
2750 btrfs_set_tree_block_key(leaf, block_info, key);
2751 btrfs_set_tree_block_level(leaf, block_info, level);
2752 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
2755 btrfs_set_extent_inline_ref_type(leaf, iref, BTRFS_TREE_BLOCK_REF_KEY);
2756 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
2758 btrfs_mark_buffer_dirty(leaf);
2759 btrfs_free_path(path);
2761 ret = update_block_group(trans, root, ins->objectid, root->leafsize,
2766 static int alloc_tree_block(struct btrfs_trans_handle *trans,
2767 struct btrfs_root *root, u64 num_bytes,
2768 u64 root_objectid, u64 generation,
2769 u64 flags, struct btrfs_disk_key *key,
2770 int level, u64 empty_size, u64 hint_byte,
2771 u64 search_end, struct btrfs_key *ins)
2774 ret = btrfs_reserve_extent(trans, root, num_bytes, empty_size,
2775 hint_byte, search_end, ins, 0);
2778 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID) {
2779 struct pending_extent_op *extent_op;
2781 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2784 extent_op->type = PENDING_EXTENT_INSERT;
2785 extent_op->bytenr = ins->objectid;
2786 extent_op->num_bytes = ins->offset;
2787 extent_op->level = level;
2788 extent_op->flags = flags;
2789 memcpy(&extent_op->key, key, sizeof(*key));
2791 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2792 ins->objectid + ins->offset - 1,
2793 EXTENT_LOCKED, GFP_NOFS);
2794 set_state_private(&root->fs_info->extent_ins,
2795 ins->objectid, (unsigned long)extent_op);
2797 if (btrfs_fs_incompat(root->fs_info,
2798 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA)) {
2799 ins->offset = level;
2800 ins->type = BTRFS_METADATA_ITEM_KEY;
2802 ret = alloc_reserved_tree_block(trans, root, root_objectid,
2805 finish_current_insert(trans, root->fs_info->extent_root);
2806 del_pending_extents(trans, root->fs_info->extent_root);
2812 * helper function to allocate a block for a given tree
2813 * returns the tree buffer or NULL.
2815 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
2816 struct btrfs_root *root,
2817 u32 blocksize, u64 root_objectid,
2818 struct btrfs_disk_key *key, int level,
2819 u64 hint, u64 empty_size)
2821 struct btrfs_key ins;
2823 struct extent_buffer *buf;
2825 ret = alloc_tree_block(trans, root, blocksize, root_objectid,
2826 trans->transid, 0, key, level,
2827 empty_size, hint, (u64)-1, &ins);
2830 return ERR_PTR(ret);
2833 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
2835 btrfs_free_extent(trans, root, ins.objectid, ins.offset,
2836 0, root->root_key.objectid, level, 0);
2838 return ERR_PTR(-ENOMEM);
2840 btrfs_set_buffer_uptodate(buf);
2841 trans->blocks_used++;
2848 static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
2849 struct btrfs_root *root,
2850 struct extent_buffer *leaf)
2853 u64 leaf_generation;
2854 struct btrfs_key key;
2855 struct btrfs_file_extent_item *fi;
2860 BUG_ON(!btrfs_is_leaf(leaf));
2861 nritems = btrfs_header_nritems(leaf);
2862 leaf_owner = btrfs_header_owner(leaf);
2863 leaf_generation = btrfs_header_generation(leaf);
2865 for (i = 0; i < nritems; i++) {
2868 btrfs_item_key_to_cpu(leaf, &key, i);
2869 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2871 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
2872 if (btrfs_file_extent_type(leaf, fi) ==
2873 BTRFS_FILE_EXTENT_INLINE)
2876 * FIXME make sure to insert a trans record that
2877 * repeats the snapshot del on crash
2879 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2880 if (disk_bytenr == 0)
2882 ret = btrfs_free_extent(trans, root, disk_bytenr,
2883 btrfs_file_extent_disk_num_bytes(leaf, fi),
2884 leaf->start, leaf_owner, leaf_generation,
2891 static void noinline reada_walk_down(struct btrfs_root *root,
2892 struct extent_buffer *node,
2905 nritems = btrfs_header_nritems(node);
2906 level = btrfs_header_level(node);
2910 for (i = slot; i < nritems && skipped < 32; i++) {
2911 bytenr = btrfs_node_blockptr(node, i);
2912 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
2913 (last > bytenr && last - bytenr > 32 * 1024))) {
2917 blocksize = btrfs_level_size(root, level - 1);
2919 ret = btrfs_lookup_extent_ref(NULL, root, bytenr,
2927 mutex_unlock(&root->fs_info->fs_mutex);
2928 ret = readahead_tree_block(root, bytenr, blocksize,
2929 btrfs_node_ptr_generation(node, i));
2930 last = bytenr + blocksize;
2932 mutex_lock(&root->fs_info->fs_mutex);
2939 * helper function for drop_snapshot, this walks down the tree dropping ref
2940 * counts as it goes.
2942 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2943 struct btrfs_root *root,
2944 struct btrfs_path *path, int *level)
2950 struct extent_buffer *next;
2951 struct extent_buffer *cur;
2952 struct extent_buffer *parent;
2957 WARN_ON(*level < 0);
2958 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2959 ret = btrfs_lookup_extent_ref(trans, root,
2960 path->nodes[*level]->start,
2961 path->nodes[*level]->len, &refs);
2967 * walk down to the last node level and free all the leaves
2969 while(*level >= 0) {
2970 WARN_ON(*level < 0);
2971 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2972 cur = path->nodes[*level];
2974 if (btrfs_header_level(cur) != *level)
2977 if (path->slots[*level] >=
2978 btrfs_header_nritems(cur))
2981 ret = drop_leaf_ref(trans, root, cur);
2985 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2986 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2987 blocksize = btrfs_level_size(root, *level - 1);
2988 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
2992 parent = path->nodes[*level];
2993 root_owner = btrfs_header_owner(parent);
2994 root_gen = btrfs_header_generation(parent);
2995 path->slots[*level]++;
2996 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
2997 parent->start, root_owner,
2998 root_gen, *level - 1, 1);
3002 next = btrfs_find_tree_block(root, bytenr, blocksize);
3003 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
3004 free_extent_buffer(next);
3005 reada_walk_down(root, cur, path->slots[*level]);
3006 mutex_unlock(&root->fs_info->fs_mutex);
3007 next = read_tree_block(root, bytenr, blocksize,
3009 mutex_lock(&root->fs_info->fs_mutex);
3011 WARN_ON(*level <= 0);
3012 if (path->nodes[*level-1])
3013 free_extent_buffer(path->nodes[*level-1]);
3014 path->nodes[*level-1] = next;
3015 *level = btrfs_header_level(next);
3016 path->slots[*level] = 0;
3019 WARN_ON(*level < 0);
3020 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3022 if (path->nodes[*level] == root->node) {
3023 root_owner = root->root_key.objectid;
3024 parent = path->nodes[*level];
3026 parent = path->nodes[*level + 1];
3027 root_owner = btrfs_header_owner(parent);
3030 root_gen = btrfs_header_generation(parent);
3031 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
3032 path->nodes[*level]->len, parent->start,
3033 root_owner, root_gen, *level, 1);
3034 free_extent_buffer(path->nodes[*level]);
3035 path->nodes[*level] = NULL;
3042 * helper for dropping snapshots. This walks back up the tree in the path
3043 * to find the first node higher up where we haven't yet gone through
3046 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3047 struct btrfs_root *root,
3048 struct btrfs_path *path, int *level)
3052 struct btrfs_root_item *root_item = &root->root_item;
3057 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
3058 slot = path->slots[i];
3059 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3060 struct extent_buffer *node;
3061 struct btrfs_disk_key disk_key;
3062 node = path->nodes[i];
3065 WARN_ON(*level == 0);
3066 btrfs_node_key(node, &disk_key, path->slots[i]);
3067 memcpy(&root_item->drop_progress,
3068 &disk_key, sizeof(disk_key));
3069 root_item->drop_level = i;
3072 struct extent_buffer *parent;
3073 if (path->nodes[*level] == root->node)
3074 parent = path->nodes[*level];
3076 parent = path->nodes[*level + 1];
3078 root_owner = btrfs_header_owner(parent);
3079 root_gen = btrfs_header_generation(parent);
3080 ret = btrfs_free_extent(trans, root,
3081 path->nodes[*level]->start,
3082 path->nodes[*level]->len,
3083 parent->start, root_owner,
3084 root_gen, *level, 1);
3086 free_extent_buffer(path->nodes[*level]);
3087 path->nodes[*level] = NULL;
3095 * drop the reference count on the tree rooted at 'snap'. This traverses
3096 * the tree freeing any blocks that have a ref count of zero after being
3099 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
3105 struct btrfs_path *path;
3108 struct btrfs_root_item *root_item = &root->root_item;
3110 path = btrfs_alloc_path();
3113 level = btrfs_header_level(root->node);
3115 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3116 path->nodes[level] = root->node;
3117 extent_buffer_get(root->node);
3118 path->slots[level] = 0;
3120 struct btrfs_key key;
3121 struct btrfs_disk_key found_key;
3122 struct extent_buffer *node;
3124 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
3125 level = root_item->drop_level;
3126 path->lowest_level = level;
3127 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3132 node = path->nodes[level];
3133 btrfs_node_key(node, &found_key, path->slots[level]);
3134 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3135 sizeof(found_key)));
3138 wret = walk_down_tree(trans, root, path, &level);
3144 wret = walk_up_tree(trans, root, path, &level);
3154 for (i = 0; i <= orig_level; i++) {
3155 if (path->nodes[i]) {
3156 free_extent_buffer(path->nodes[i]);
3157 path->nodes[i] = NULL;
3161 btrfs_free_path(path);
3167 int btrfs_free_block_groups(struct btrfs_fs_info *info)
3169 struct btrfs_space_info *sinfo;
3170 struct btrfs_block_group_cache *cache;
3177 ret = find_first_extent_bit(&info->block_group_cache, 0,
3178 &start, &end, (unsigned int)-1);
3181 ret = get_state_private(&info->block_group_cache, start, &ptr);
3183 cache = (struct btrfs_block_group_cache *)ptr;
3184 if (cache->free_space_ctl) {
3185 btrfs_remove_free_space_cache(cache);
3186 kfree(cache->free_space_ctl);
3190 clear_extent_bits(&info->block_group_cache, start,
3191 end, (unsigned int)-1, GFP_NOFS);
3194 ret = find_first_extent_bit(&info->free_space_cache, 0,
3195 &start, &end, EXTENT_DIRTY);
3198 clear_extent_dirty(&info->free_space_cache, start,
3202 while (!list_empty(&info->space_info)) {
3203 sinfo = list_entry(info->space_info.next,
3204 struct btrfs_space_info, list);
3205 list_del_init(&sinfo->list);
3211 int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
3212 struct btrfs_key *key)
3215 struct btrfs_key found_key;
3216 struct extent_buffer *leaf;
3219 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
3223 slot = path->slots[0];
3224 leaf = path->nodes[0];
3225 if (slot >= btrfs_header_nritems(leaf)) {
3226 ret = btrfs_next_leaf(root, path);
3233 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3235 if (found_key.objectid >= key->objectid &&
3236 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
3245 int btrfs_read_block_groups(struct btrfs_root *root)
3247 struct btrfs_path *path;
3250 struct btrfs_block_group_cache *cache;
3251 struct btrfs_fs_info *info = root->fs_info;
3252 struct btrfs_space_info *space_info;
3253 struct extent_io_tree *block_group_cache;
3254 struct btrfs_key key;
3255 struct btrfs_key found_key;
3256 struct extent_buffer *leaf;
3258 block_group_cache = &info->block_group_cache;
3260 root = info->extent_root;
3263 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3264 path = btrfs_alloc_path();
3269 ret = find_first_block_group(root, path, &key);
3277 leaf = path->nodes[0];
3278 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3279 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3285 read_extent_buffer(leaf, &cache->item,
3286 btrfs_item_ptr_offset(leaf, path->slots[0]),
3287 sizeof(cache->item));
3288 memcpy(&cache->key, &found_key, sizeof(found_key));
3291 key.objectid = found_key.objectid + found_key.offset;
3292 btrfs_release_path(root, path);
3293 cache->flags = btrfs_block_group_flags(&cache->item);
3295 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
3296 bit = BLOCK_GROUP_DATA;
3297 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
3298 bit = BLOCK_GROUP_SYSTEM;
3299 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
3300 bit = BLOCK_GROUP_METADATA;
3302 set_avail_alloc_bits(info, cache->flags);
3303 if (btrfs_chunk_readonly(root, cache->key.objectid))
3306 ret = update_space_info(info, cache->flags, found_key.offset,
3307 btrfs_block_group_used(&cache->item),
3310 cache->space_info = space_info;
3312 /* use EXTENT_LOCKED to prevent merging */
3313 set_extent_bits(block_group_cache, found_key.objectid,
3314 found_key.objectid + found_key.offset - 1,
3315 bit | EXTENT_LOCKED, GFP_NOFS);
3316 set_state_private(block_group_cache, found_key.objectid,
3317 (unsigned long)cache);
3321 btrfs_free_path(path);
3325 struct btrfs_block_group_cache *
3326 btrfs_add_block_group(struct btrfs_fs_info *fs_info, u64 bytes_used, u64 type,
3327 u64 chunk_objectid, u64 chunk_offset, u64 size)
3331 struct btrfs_block_group_cache *cache;
3332 struct extent_io_tree *block_group_cache;
3334 block_group_cache = &fs_info->block_group_cache;
3336 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3338 cache->key.objectid = chunk_offset;
3339 cache->key.offset = size;
3341 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3342 btrfs_set_block_group_used(&cache->item, bytes_used);
3343 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
3344 cache->flags = type;
3345 btrfs_set_block_group_flags(&cache->item, type);
3347 ret = update_space_info(fs_info, cache->flags, size, bytes_used,
3348 &cache->space_info);
3351 bit = block_group_state_bits(type);
3352 set_extent_bits(block_group_cache, chunk_offset,
3353 chunk_offset + size - 1,
3354 bit | EXTENT_LOCKED, GFP_NOFS);
3356 set_state_private(block_group_cache, chunk_offset,
3357 (unsigned long)cache);
3358 set_avail_alloc_bits(fs_info, type);
3363 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3364 struct btrfs_root *root, u64 bytes_used,
3365 u64 type, u64 chunk_objectid, u64 chunk_offset,
3369 struct btrfs_root *extent_root;
3370 struct btrfs_block_group_cache *cache;
3372 cache = btrfs_add_block_group(root->fs_info, bytes_used, type,
3373 chunk_objectid, chunk_offset, size);
3374 extent_root = root->fs_info->extent_root;
3375 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3376 sizeof(cache->item));
3379 finish_current_insert(trans, extent_root);
3380 ret = del_pending_extents(trans, extent_root);
3385 * This is for converter use only.
3387 * In that case, we don't know where are free blocks located.
3388 * Therefore all block group cache entries must be setup properly
3389 * before doing any block allocation.
3391 int btrfs_make_block_groups(struct btrfs_trans_handle *trans,
3392 struct btrfs_root *root)
3400 u64 total_metadata = 0;
3404 struct btrfs_root *extent_root;
3405 struct btrfs_block_group_cache *cache;
3406 struct extent_io_tree *block_group_cache;
3408 extent_root = root->fs_info->extent_root;
3409 block_group_cache = &root->fs_info->block_group_cache;
3410 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3411 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
3412 group_align = 64 * root->sectorsize;
3415 while (cur_start < total_bytes) {
3416 group_size = total_bytes / 12;
3417 group_size = min_t(u64, group_size, total_bytes - cur_start);
3418 if (cur_start == 0) {
3419 bit = BLOCK_GROUP_SYSTEM;
3420 group_type = BTRFS_BLOCK_GROUP_SYSTEM;
3422 group_size &= ~(group_align - 1);
3423 group_size = max_t(u64, group_size, 8 * 1024 * 1024);
3424 group_size = min_t(u64, group_size, 32 * 1024 * 1024);
3426 group_size &= ~(group_align - 1);
3427 if (total_data >= total_metadata * 2) {
3428 group_type = BTRFS_BLOCK_GROUP_METADATA;
3429 group_size = min_t(u64, group_size,
3430 1ULL * 1024 * 1024 * 1024);
3431 total_metadata += group_size;
3433 group_type = BTRFS_BLOCK_GROUP_DATA;
3434 group_size = min_t(u64, group_size,
3435 5ULL * 1024 * 1024 * 1024);
3436 total_data += group_size;
3438 if ((total_bytes - cur_start) * 4 < group_size * 5)
3439 group_size = total_bytes - cur_start;
3442 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3445 cache->key.objectid = cur_start;
3446 cache->key.offset = group_size;
3447 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3449 btrfs_set_block_group_used(&cache->item, 0);
3450 btrfs_set_block_group_chunk_objectid(&cache->item,
3452 btrfs_set_block_group_flags(&cache->item, group_type);
3454 cache->flags = group_type;
3456 ret = update_space_info(root->fs_info, group_type, group_size,
3457 0, &cache->space_info);
3459 set_avail_alloc_bits(extent_root->fs_info, group_type);
3461 set_extent_bits(block_group_cache, cur_start,
3462 cur_start + group_size - 1,
3463 bit | EXTENT_LOCKED, GFP_NOFS);
3464 set_state_private(block_group_cache, cur_start,
3465 (unsigned long)cache);
3466 cur_start += group_size;
3468 /* then insert all the items */
3470 while(cur_start < total_bytes) {
3471 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
3474 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3475 sizeof(cache->item));
3478 finish_current_insert(trans, extent_root);
3479 ret = del_pending_extents(trans, extent_root);
3482 cur_start = cache->key.objectid + cache->key.offset;
3487 int btrfs_update_block_group(struct btrfs_trans_handle *trans,
3488 struct btrfs_root *root,
3489 u64 bytenr, u64 num_bytes, int alloc,
3492 return update_block_group(trans, root, bytenr, num_bytes,
3496 static int btrfs_count_extents_in_block_group(struct btrfs_root *root,
3497 struct btrfs_path *path, u64 start,
3501 struct btrfs_key key;
3502 struct extent_buffer *leaf;
3508 key.objectid = start;
3509 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
3510 ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
3515 leaf = path->nodes[0];
3516 slot = path->slots[0];
3517 if (slot >= btrfs_header_nritems(leaf)) {
3518 ret = btrfs_next_leaf(root, path);
3523 leaf = path->nodes[0];
3524 slot = path->slots[0];
3526 btrfs_item_key_to_cpu(leaf, &key, slot);
3527 if (key.objectid > start + len)
3529 if (key.type == BTRFS_EXTENT_ITEM_KEY)
3530 bytes_used += key.offset;
3531 if (key.type == BTRFS_METADATA_ITEM_KEY)
3532 bytes_used += root->leafsize;
3535 *total = bytes_used;
3536 btrfs_release_path(root, path);
3540 int btrfs_check_block_accounting(struct btrfs_root *root)
3545 struct btrfs_path path;
3546 struct btrfs_block_group_cache *cache;
3547 struct btrfs_fs_info *fs_info = root->fs_info;
3549 btrfs_init_path(&path);
3552 cache = btrfs_lookup_block_group(fs_info, start);
3556 ret = btrfs_count_extents_in_block_group(root, &path,
3557 cache->key.objectid,
3562 u64 on_disk = btrfs_block_group_used(&cache->item);
3563 if (on_disk != bytes_used) {
3564 fprintf(stderr, "bad block group accounting found %llu "
3565 "expected %llu block group %llu\n",
3566 (unsigned long long)bytes_used,
3567 (unsigned long long)on_disk,
3568 (unsigned long long)cache->key.objectid);
3571 start = cache->key.objectid + cache->key.offset;
3573 cache->space_info->bytes_used = 0;
3579 * Fixup block accounting. The initial block accounting created by
3580 * make_block_groups isn't accuracy in this case.
3582 int btrfs_fix_block_accounting(struct btrfs_trans_handle *trans,
3583 struct btrfs_root *root)
3589 struct btrfs_path path;
3590 struct btrfs_key key;
3591 struct extent_buffer *leaf;
3592 struct btrfs_block_group_cache *cache;
3593 struct btrfs_fs_info *fs_info = root->fs_info;
3595 root = root->fs_info->extent_root;
3597 while(extent_root_pending_ops(fs_info)) {
3598 ret = finish_current_insert(trans, root);
3601 ret = del_pending_extents(trans, root);
3607 cache = btrfs_lookup_first_block_group(fs_info, start);
3610 start = cache->key.objectid + cache->key.offset;
3611 btrfs_set_block_group_used(&cache->item, 0);
3612 cache->space_info->bytes_used = 0;
3613 set_extent_bits(&root->fs_info->block_group_cache,
3614 cache->key.objectid,
3615 cache->key.objectid + cache->key.offset -1,
3616 BLOCK_GROUP_DIRTY, GFP_NOFS);
3619 btrfs_init_path(&path);
3622 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
3623 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
3628 leaf = path.nodes[0];
3629 slot = path.slots[0];
3630 if (slot >= btrfs_header_nritems(leaf)) {
3631 ret = btrfs_next_leaf(root, &path);
3636 leaf = path.nodes[0];
3637 slot = path.slots[0];
3639 btrfs_item_key_to_cpu(leaf, &key, slot);
3640 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
3641 bytes_used += key.offset;
3642 ret = btrfs_update_block_group(trans, root,
3643 key.objectid, key.offset, 1, 0);
3645 } else if (key.type == BTRFS_METADATA_ITEM_KEY) {
3646 bytes_used += root->leafsize;
3647 ret = btrfs_update_block_group(trans, root,
3648 key.objectid, root->leafsize, 1, 0);
3653 btrfs_set_super_bytes_used(root->fs_info->super_copy, bytes_used);
3654 btrfs_release_path(root, &path);