2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
22 #include "kerncompat.h"
23 #include "radix-tree.h"
26 #include "print-tree.h"
27 #include "transaction.h"
30 #include "free-space-cache.h"
33 #define PENDING_EXTENT_INSERT 0
34 #define PENDING_EXTENT_DELETE 1
35 #define PENDING_BACKREF_UPDATE 2
37 struct pending_extent_op {
42 struct btrfs_disk_key key;
46 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
47 struct btrfs_root *root,
48 u64 root_objectid, u64 generation,
49 u64 flags, struct btrfs_disk_key *key,
50 int level, struct btrfs_key *ins);
51 static int __free_extent(struct btrfs_trans_handle *trans,
52 struct btrfs_root *root,
53 u64 bytenr, u64 num_bytes, u64 parent,
54 u64 root_objectid, u64 owner_objectid,
55 u64 owner_offset, int refs_to_drop);
56 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
57 btrfs_root *extent_root);
58 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
59 btrfs_root *extent_root);
60 static struct btrfs_block_group_cache *
61 btrfs_find_block_group(struct btrfs_root *root, struct btrfs_block_group_cache
62 *hint, u64 search_start, int data, int owner);
64 static int remove_sb_from_cache(struct btrfs_root *root,
65 struct btrfs_block_group_cache *cache)
71 struct extent_io_tree *free_space_cache;
73 free_space_cache = &root->fs_info->free_space_cache;
74 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
75 bytenr = btrfs_sb_offset(i);
76 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
77 cache->key.objectid, bytenr, 0,
78 &logical, &nr, &stripe_len);
81 clear_extent_dirty(free_space_cache, logical[nr],
82 logical[nr] + stripe_len - 1, GFP_NOFS);
89 static int cache_block_group(struct btrfs_root *root,
90 struct btrfs_block_group_cache *block_group)
92 struct btrfs_path *path;
95 struct extent_buffer *leaf;
96 struct extent_io_tree *free_space_cache;
104 root = root->fs_info->extent_root;
105 free_space_cache = &root->fs_info->free_space_cache;
107 if (block_group->cached)
110 path = btrfs_alloc_path();
115 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
120 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
125 leaf = path->nodes[0];
126 slot = path->slots[0];
127 if (slot >= btrfs_header_nritems(leaf)) {
128 ret = btrfs_next_leaf(root, path);
137 btrfs_item_key_to_cpu(leaf, &key, slot);
138 if (key.objectid < block_group->key.objectid) {
141 if (key.objectid >= block_group->key.objectid +
142 block_group->key.offset) {
146 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
147 key.type == BTRFS_METADATA_ITEM_KEY) {
148 if (key.objectid > last) {
149 hole_size = key.objectid - last;
150 set_extent_dirty(free_space_cache, last,
151 last + hole_size - 1,
154 if (key.type == BTRFS_METADATA_ITEM_KEY)
155 last = key.objectid + root->leafsize;
157 last = key.objectid + key.offset;
163 if (block_group->key.objectid +
164 block_group->key.offset > last) {
165 hole_size = block_group->key.objectid +
166 block_group->key.offset - last;
167 set_extent_dirty(free_space_cache, last,
168 last + hole_size - 1, GFP_NOFS);
170 remove_sb_from_cache(root, block_group);
171 block_group->cached = 1;
173 btrfs_free_path(path);
177 struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
181 struct extent_io_tree *block_group_cache;
182 struct btrfs_block_group_cache *block_group = NULL;
188 bytenr = max_t(u64, bytenr,
189 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
190 block_group_cache = &info->block_group_cache;
191 ret = find_first_extent_bit(block_group_cache,
192 bytenr, &start, &end,
193 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
198 ret = get_state_private(block_group_cache, start, &ptr);
202 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
206 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
210 struct extent_io_tree *block_group_cache;
211 struct btrfs_block_group_cache *block_group = NULL;
217 block_group_cache = &info->block_group_cache;
218 ret = find_first_extent_bit(block_group_cache,
219 bytenr, &start, &end,
220 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
225 ret = get_state_private(block_group_cache, start, &ptr);
229 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
230 if (block_group->key.objectid <= bytenr && bytenr <
231 block_group->key.objectid + block_group->key.offset)
236 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
238 return (cache->flags & bits) == bits;
241 static int noinline find_search_start(struct btrfs_root *root,
242 struct btrfs_block_group_cache **cache_ret,
243 u64 *start_ret, int num, int data)
246 struct btrfs_block_group_cache *cache = *cache_ret;
247 u64 last = *start_ret;
250 u64 search_start = *start_ret;
256 ret = cache_block_group(root, cache);
260 last = max(search_start, cache->key.objectid);
261 if (cache->ro || !block_group_bits(cache, data))
265 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
266 last, &start, &end, EXTENT_DIRTY);
271 start = max(last, start);
273 if (last - start < num) {
276 if (start + num > cache->key.objectid + cache->key.offset) {
284 cache = btrfs_lookup_block_group(root->fs_info, search_start);
286 printk("Unable to find block group for %llu\n",
287 (unsigned long long)search_start);
293 last = cache->key.objectid + cache->key.offset;
295 cache = btrfs_lookup_first_block_group(root->fs_info, last);
308 static int block_group_state_bits(u64 flags)
311 if (flags & BTRFS_BLOCK_GROUP_DATA)
312 bits |= BLOCK_GROUP_DATA;
313 if (flags & BTRFS_BLOCK_GROUP_METADATA)
314 bits |= BLOCK_GROUP_METADATA;
315 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
316 bits |= BLOCK_GROUP_SYSTEM;
320 static struct btrfs_block_group_cache *
321 btrfs_find_block_group(struct btrfs_root *root, struct btrfs_block_group_cache
322 *hint, u64 search_start, int data, int owner)
324 struct btrfs_block_group_cache *cache;
325 struct extent_io_tree *block_group_cache;
326 struct btrfs_block_group_cache *found_group = NULL;
327 struct btrfs_fs_info *info = root->fs_info;
340 block_group_cache = &info->block_group_cache;
345 bit = block_group_state_bits(data);
348 struct btrfs_block_group_cache *shint;
349 shint = btrfs_lookup_block_group(info, search_start);
350 if (shint && !shint->ro && block_group_bits(shint, data)) {
351 used = btrfs_block_group_used(&shint->item);
352 if (used + shint->pinned <
353 div_factor(shint->key.offset, factor)) {
358 if (hint && !hint->ro && block_group_bits(hint, data)) {
359 used = btrfs_block_group_used(&hint->item);
360 if (used + hint->pinned <
361 div_factor(hint->key.offset, factor)) {
364 last = hint->key.objectid + hint->key.offset;
368 hint_last = max(hint->key.objectid, search_start);
370 hint_last = search_start;
376 ret = find_first_extent_bit(block_group_cache, last,
381 ret = get_state_private(block_group_cache, start, &ptr);
385 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
386 last = cache->key.objectid + cache->key.offset;
387 used = btrfs_block_group_used(&cache->item);
389 if (!cache->ro && block_group_bits(cache, data)) {
391 free_check = cache->key.offset;
393 free_check = div_factor(cache->key.offset,
396 if (used + cache->pinned < free_check) {
413 * Back reference rules. Back refs have three main goals:
415 * 1) differentiate between all holders of references to an extent so that
416 * when a reference is dropped we can make sure it was a valid reference
417 * before freeing the extent.
419 * 2) Provide enough information to quickly find the holders of an extent
420 * if we notice a given block is corrupted or bad.
422 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
423 * maintenance. This is actually the same as #2, but with a slightly
424 * different use case.
426 * There are two kinds of back refs. The implicit back refs is optimized
427 * for pointers in non-shared tree blocks. For a given pointer in a block,
428 * back refs of this kind provide information about the block's owner tree
429 * and the pointer's key. These information allow us to find the block by
430 * b-tree searching. The full back refs is for pointers in tree blocks not
431 * referenced by their owner trees. The location of tree block is recorded
432 * in the back refs. Actually the full back refs is generic, and can be
433 * used in all cases the implicit back refs is used. The major shortcoming
434 * of the full back refs is its overhead. Every time a tree block gets
435 * COWed, we have to update back refs entry for all pointers in it.
437 * For a newly allocated tree block, we use implicit back refs for
438 * pointers in it. This means most tree related operations only involve
439 * implicit back refs. For a tree block created in old transaction, the
440 * only way to drop a reference to it is COW it. So we can detect the
441 * event that tree block loses its owner tree's reference and do the
442 * back refs conversion.
444 * When a tree block is COW'd through a tree, there are four cases:
446 * The reference count of the block is one and the tree is the block's
447 * owner tree. Nothing to do in this case.
449 * The reference count of the block is one and the tree is not the
450 * block's owner tree. In this case, full back refs is used for pointers
451 * in the block. Remove these full back refs, add implicit back refs for
452 * every pointers in the new block.
454 * The reference count of the block is greater than one and the tree is
455 * the block's owner tree. In this case, implicit back refs is used for
456 * pointers in the block. Add full back refs for every pointers in the
457 * block, increase lower level extents' reference counts. The original
458 * implicit back refs are entailed to the new block.
460 * The reference count of the block is greater than one and the tree is
461 * not the block's owner tree. Add implicit back refs for every pointer in
462 * the new block, increase lower level extents' reference count.
464 * Back Reference Key composing:
466 * The key objectid corresponds to the first byte in the extent,
467 * The key type is used to differentiate between types of back refs.
468 * There are different meanings of the key offset for different types
471 * File extents can be referenced by:
473 * - multiple snapshots, subvolumes, or different generations in one subvol
474 * - different files inside a single subvolume
475 * - different offsets inside a file (bookend extents in file.c)
477 * The extent ref structure for the implicit back refs has fields for:
479 * - Objectid of the subvolume root
480 * - objectid of the file holding the reference
481 * - original offset in the file
482 * - how many bookend extents
484 * The key offset for the implicit back refs is hash of the first
487 * The extent ref structure for the full back refs has field for:
489 * - number of pointers in the tree leaf
491 * The key offset for the implicit back refs is the first byte of
494 * When a file extent is allocated, The implicit back refs is used.
495 * the fields are filled in:
497 * (root_key.objectid, inode objectid, offset in file, 1)
499 * When a file extent is removed file truncation, we find the
500 * corresponding implicit back refs and check the following fields:
502 * (btrfs_header_owner(leaf), inode objectid, offset in file)
504 * Btree extents can be referenced by:
506 * - Different subvolumes
508 * Both the implicit back refs and the full back refs for tree blocks
509 * only consist of key. The key offset for the implicit back refs is
510 * objectid of block's owner tree. The key offset for the full back refs
511 * is the first byte of parent block.
513 * When implicit back refs is used, information about the lowest key and
514 * level of the tree block are required. These information are stored in
515 * tree block info structure.
518 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
519 static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
520 struct btrfs_root *root,
521 struct btrfs_path *path,
522 u64 owner, u32 extra_size)
524 struct btrfs_extent_item *item;
525 struct btrfs_extent_item_v0 *ei0;
526 struct btrfs_extent_ref_v0 *ref0;
527 struct btrfs_tree_block_info *bi;
528 struct extent_buffer *leaf;
529 struct btrfs_key key;
530 struct btrfs_key found_key;
531 u32 new_size = sizeof(*item);
535 leaf = path->nodes[0];
536 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
538 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
539 ei0 = btrfs_item_ptr(leaf, path->slots[0],
540 struct btrfs_extent_item_v0);
541 refs = btrfs_extent_refs_v0(leaf, ei0);
543 if (owner == (u64)-1) {
545 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
546 ret = btrfs_next_leaf(root, path);
550 leaf = path->nodes[0];
552 btrfs_item_key_to_cpu(leaf, &found_key,
554 BUG_ON(key.objectid != found_key.objectid);
555 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
559 ref0 = btrfs_item_ptr(leaf, path->slots[0],
560 struct btrfs_extent_ref_v0);
561 owner = btrfs_ref_objectid_v0(leaf, ref0);
565 btrfs_release_path(path);
567 if (owner < BTRFS_FIRST_FREE_OBJECTID)
568 new_size += sizeof(*bi);
570 new_size -= sizeof(*ei0);
571 ret = btrfs_search_slot(trans, root, &key, path, new_size, 1);
576 ret = btrfs_extend_item(trans, root, path, new_size);
579 leaf = path->nodes[0];
580 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
581 btrfs_set_extent_refs(leaf, item, refs);
582 /* FIXME: get real generation */
583 btrfs_set_extent_generation(leaf, item, 0);
584 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
585 btrfs_set_extent_flags(leaf, item,
586 BTRFS_EXTENT_FLAG_TREE_BLOCK |
587 BTRFS_BLOCK_FLAG_FULL_BACKREF);
588 bi = (struct btrfs_tree_block_info *)(item + 1);
589 /* FIXME: get first key of the block */
590 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
591 btrfs_set_tree_block_level(leaf, bi, (int)owner);
593 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
595 btrfs_mark_buffer_dirty(leaf);
600 static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
602 u32 high_crc = ~(u32)0;
603 u32 low_crc = ~(u32)0;
606 lenum = cpu_to_le64(root_objectid);
607 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
608 lenum = cpu_to_le64(owner);
609 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
610 lenum = cpu_to_le64(offset);
611 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
613 return ((u64)high_crc << 31) ^ (u64)low_crc;
616 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
617 struct btrfs_extent_data_ref *ref)
619 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
620 btrfs_extent_data_ref_objectid(leaf, ref),
621 btrfs_extent_data_ref_offset(leaf, ref));
624 static int match_extent_data_ref(struct extent_buffer *leaf,
625 struct btrfs_extent_data_ref *ref,
626 u64 root_objectid, u64 owner, u64 offset)
628 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
629 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
630 btrfs_extent_data_ref_offset(leaf, ref) != offset)
635 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
636 struct btrfs_root *root,
637 struct btrfs_path *path,
638 u64 bytenr, u64 parent,
640 u64 owner, u64 offset)
642 struct btrfs_key key;
643 struct btrfs_extent_data_ref *ref;
644 struct extent_buffer *leaf;
650 key.objectid = bytenr;
652 key.type = BTRFS_SHARED_DATA_REF_KEY;
655 key.type = BTRFS_EXTENT_DATA_REF_KEY;
656 key.offset = hash_extent_data_ref(root_objectid,
661 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
670 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
671 key.type = BTRFS_EXTENT_REF_V0_KEY;
672 btrfs_release_path(path);
673 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
684 leaf = path->nodes[0];
685 nritems = btrfs_header_nritems(leaf);
687 if (path->slots[0] >= nritems) {
688 ret = btrfs_next_leaf(root, path);
694 leaf = path->nodes[0];
695 nritems = btrfs_header_nritems(leaf);
699 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
700 if (key.objectid != bytenr ||
701 key.type != BTRFS_EXTENT_DATA_REF_KEY)
704 ref = btrfs_item_ptr(leaf, path->slots[0],
705 struct btrfs_extent_data_ref);
707 if (match_extent_data_ref(leaf, ref, root_objectid,
710 btrfs_release_path(path);
722 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
723 struct btrfs_root *root,
724 struct btrfs_path *path,
725 u64 bytenr, u64 parent,
726 u64 root_objectid, u64 owner,
727 u64 offset, int refs_to_add)
729 struct btrfs_key key;
730 struct extent_buffer *leaf;
735 key.objectid = bytenr;
737 key.type = BTRFS_SHARED_DATA_REF_KEY;
739 size = sizeof(struct btrfs_shared_data_ref);
741 key.type = BTRFS_EXTENT_DATA_REF_KEY;
742 key.offset = hash_extent_data_ref(root_objectid,
744 size = sizeof(struct btrfs_extent_data_ref);
747 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
748 if (ret && ret != -EEXIST)
751 leaf = path->nodes[0];
753 struct btrfs_shared_data_ref *ref;
754 ref = btrfs_item_ptr(leaf, path->slots[0],
755 struct btrfs_shared_data_ref);
757 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
759 num_refs = btrfs_shared_data_ref_count(leaf, ref);
760 num_refs += refs_to_add;
761 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
764 struct btrfs_extent_data_ref *ref;
765 while (ret == -EEXIST) {
766 ref = btrfs_item_ptr(leaf, path->slots[0],
767 struct btrfs_extent_data_ref);
768 if (match_extent_data_ref(leaf, ref, root_objectid,
771 btrfs_release_path(path);
774 ret = btrfs_insert_empty_item(trans, root, path, &key,
776 if (ret && ret != -EEXIST)
779 leaf = path->nodes[0];
781 ref = btrfs_item_ptr(leaf, path->slots[0],
782 struct btrfs_extent_data_ref);
784 btrfs_set_extent_data_ref_root(leaf, ref,
786 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
787 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
788 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
790 num_refs = btrfs_extent_data_ref_count(leaf, ref);
791 num_refs += refs_to_add;
792 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
795 btrfs_mark_buffer_dirty(leaf);
798 btrfs_release_path(path);
802 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
803 struct btrfs_root *root,
804 struct btrfs_path *path,
807 struct btrfs_key key;
808 struct btrfs_extent_data_ref *ref1 = NULL;
809 struct btrfs_shared_data_ref *ref2 = NULL;
810 struct extent_buffer *leaf;
814 leaf = path->nodes[0];
815 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
817 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
818 ref1 = btrfs_item_ptr(leaf, path->slots[0],
819 struct btrfs_extent_data_ref);
820 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
821 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
822 ref2 = btrfs_item_ptr(leaf, path->slots[0],
823 struct btrfs_shared_data_ref);
824 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
825 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
826 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
827 struct btrfs_extent_ref_v0 *ref0;
828 ref0 = btrfs_item_ptr(leaf, path->slots[0],
829 struct btrfs_extent_ref_v0);
830 num_refs = btrfs_ref_count_v0(leaf, ref0);
836 BUG_ON(num_refs < refs_to_drop);
837 num_refs -= refs_to_drop;
840 ret = btrfs_del_item(trans, root, path);
842 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
843 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
844 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
845 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
846 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
848 struct btrfs_extent_ref_v0 *ref0;
849 ref0 = btrfs_item_ptr(leaf, path->slots[0],
850 struct btrfs_extent_ref_v0);
851 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
854 btrfs_mark_buffer_dirty(leaf);
859 static noinline u32 extent_data_ref_count(struct btrfs_root *root,
860 struct btrfs_path *path,
861 struct btrfs_extent_inline_ref *iref)
863 struct btrfs_key key;
864 struct extent_buffer *leaf;
865 struct btrfs_extent_data_ref *ref1;
866 struct btrfs_shared_data_ref *ref2;
869 leaf = path->nodes[0];
870 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
872 if (btrfs_extent_inline_ref_type(leaf, iref) ==
873 BTRFS_EXTENT_DATA_REF_KEY) {
874 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
875 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
877 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
878 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
880 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
881 ref1 = btrfs_item_ptr(leaf, path->slots[0],
882 struct btrfs_extent_data_ref);
883 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
884 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
885 ref2 = btrfs_item_ptr(leaf, path->slots[0],
886 struct btrfs_shared_data_ref);
887 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
888 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
889 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
890 struct btrfs_extent_ref_v0 *ref0;
891 ref0 = btrfs_item_ptr(leaf, path->slots[0],
892 struct btrfs_extent_ref_v0);
893 num_refs = btrfs_ref_count_v0(leaf, ref0);
901 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
902 struct btrfs_root *root,
903 struct btrfs_path *path,
904 u64 bytenr, u64 parent,
907 struct btrfs_key key;
910 key.objectid = bytenr;
912 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
915 key.type = BTRFS_TREE_BLOCK_REF_KEY;
916 key.offset = root_objectid;
919 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
922 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
923 if (ret == -ENOENT && parent) {
924 btrfs_release_path(path);
925 key.type = BTRFS_EXTENT_REF_V0_KEY;
926 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
934 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
935 struct btrfs_root *root,
936 struct btrfs_path *path,
937 u64 bytenr, u64 parent,
940 struct btrfs_key key;
943 key.objectid = bytenr;
945 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
948 key.type = BTRFS_TREE_BLOCK_REF_KEY;
949 key.offset = root_objectid;
952 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
954 btrfs_release_path(path);
958 static inline int extent_ref_type(u64 parent, u64 owner)
961 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
963 type = BTRFS_SHARED_BLOCK_REF_KEY;
965 type = BTRFS_TREE_BLOCK_REF_KEY;
968 type = BTRFS_SHARED_DATA_REF_KEY;
970 type = BTRFS_EXTENT_DATA_REF_KEY;
975 static int find_next_key(struct btrfs_path *path, struct btrfs_key *key)
979 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
980 if (!path->nodes[level])
982 if (path->slots[level] + 1 >=
983 btrfs_header_nritems(path->nodes[level]))
986 btrfs_item_key_to_cpu(path->nodes[level], key,
987 path->slots[level] + 1);
989 btrfs_node_key_to_cpu(path->nodes[level], key,
990 path->slots[level] + 1);
996 static int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
997 struct btrfs_root *root,
998 struct btrfs_path *path,
999 struct btrfs_extent_inline_ref **ref_ret,
1000 u64 bytenr, u64 num_bytes,
1001 u64 parent, u64 root_objectid,
1002 u64 owner, u64 offset, int insert)
1004 struct btrfs_key key;
1005 struct extent_buffer *leaf;
1006 struct btrfs_extent_item *ei;
1007 struct btrfs_extent_inline_ref *iref;
1017 int skinny_metadata =
1018 btrfs_fs_incompat(root->fs_info,
1019 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
1021 key.objectid = bytenr;
1022 key.type = BTRFS_EXTENT_ITEM_KEY;
1023 key.offset = num_bytes;
1025 want = extent_ref_type(parent, owner);
1027 extra_size = btrfs_extent_inline_ref_size(want);
1031 if (owner < BTRFS_FIRST_FREE_OBJECTID && skinny_metadata) {
1032 skinny_metadata = 1;
1033 key.type = BTRFS_METADATA_ITEM_KEY;
1035 } else if (skinny_metadata) {
1036 skinny_metadata = 0;
1040 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1047 * We may be a newly converted file system which still has the old fat
1048 * extent entries for metadata, so try and see if we have one of those.
1050 if (ret > 0 && skinny_metadata) {
1051 skinny_metadata = 0;
1052 if (path->slots[0]) {
1054 btrfs_item_key_to_cpu(path->nodes[0], &key,
1056 if (key.objectid == bytenr &&
1057 key.type == BTRFS_EXTENT_ITEM_KEY &&
1058 key.offset == num_bytes)
1062 key.type = BTRFS_EXTENT_ITEM_KEY;
1063 key.offset = num_bytes;
1064 btrfs_release_path(path);
1070 printf("Failed to find [%llu, %u, %llu]\n", key.objectid, key.type, key.offset);
1076 leaf = path->nodes[0];
1077 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1078 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1079 if (item_size < sizeof(*ei)) {
1084 ret = convert_extent_item_v0(trans, root, path, owner,
1090 leaf = path->nodes[0];
1091 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1094 if (item_size < sizeof(*ei)) {
1095 printf("Size is %u, needs to be %u, slot %d\n",
1096 (unsigned)item_size,
1097 (unsigned)sizeof(*ei), path->slots[0]);
1098 btrfs_print_leaf(root, leaf);
1101 BUG_ON(item_size < sizeof(*ei));
1103 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1104 flags = btrfs_extent_flags(leaf, ei);
1106 ptr = (unsigned long)(ei + 1);
1107 end = (unsigned long)ei + item_size;
1109 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
1110 ptr += sizeof(struct btrfs_tree_block_info);
1112 } else if (!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
1113 if (!(flags & BTRFS_EXTENT_FLAG_DATA)) {
1124 iref = (struct btrfs_extent_inline_ref *)ptr;
1125 type = btrfs_extent_inline_ref_type(leaf, iref);
1129 ptr += btrfs_extent_inline_ref_size(type);
1133 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1134 struct btrfs_extent_data_ref *dref;
1135 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1136 if (match_extent_data_ref(leaf, dref, root_objectid,
1141 if (hash_extent_data_ref_item(leaf, dref) <
1142 hash_extent_data_ref(root_objectid, owner, offset))
1146 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1148 if (parent == ref_offset) {
1152 if (ref_offset < parent)
1155 if (root_objectid == ref_offset) {
1159 if (ref_offset < root_objectid)
1163 ptr += btrfs_extent_inline_ref_size(type);
1165 if (err == -ENOENT && insert) {
1166 if (item_size + extra_size >=
1167 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1172 * To add new inline back ref, we have to make sure
1173 * there is no corresponding back ref item.
1174 * For simplicity, we just do not add new inline back
1175 * ref if there is any back ref item.
1177 if (find_next_key(path, &key) == 0 && key.objectid == bytenr &&
1178 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
1183 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1188 static int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1189 struct btrfs_root *root,
1190 struct btrfs_path *path,
1191 struct btrfs_extent_inline_ref *iref,
1192 u64 parent, u64 root_objectid,
1193 u64 owner, u64 offset, int refs_to_add)
1195 struct extent_buffer *leaf;
1196 struct btrfs_extent_item *ei;
1199 unsigned long item_offset;
1205 leaf = path->nodes[0];
1206 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1207 item_offset = (unsigned long)iref - (unsigned long)ei;
1209 type = extent_ref_type(parent, owner);
1210 size = btrfs_extent_inline_ref_size(type);
1212 ret = btrfs_extend_item(trans, root, path, size);
1215 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1216 refs = btrfs_extent_refs(leaf, ei);
1217 refs += refs_to_add;
1218 btrfs_set_extent_refs(leaf, ei, refs);
1220 ptr = (unsigned long)ei + item_offset;
1221 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1222 if (ptr < end - size)
1223 memmove_extent_buffer(leaf, ptr + size, ptr,
1226 iref = (struct btrfs_extent_inline_ref *)ptr;
1227 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1228 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1229 struct btrfs_extent_data_ref *dref;
1230 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1231 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1232 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1233 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1234 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1235 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1236 struct btrfs_shared_data_ref *sref;
1237 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1238 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1239 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1240 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1241 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1243 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1245 btrfs_mark_buffer_dirty(leaf);
1249 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1250 struct btrfs_root *root,
1251 struct btrfs_path *path,
1252 struct btrfs_extent_inline_ref **ref_ret,
1253 u64 bytenr, u64 num_bytes, u64 parent,
1254 u64 root_objectid, u64 owner, u64 offset)
1258 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1259 bytenr, num_bytes, parent,
1260 root_objectid, owner, offset, 0);
1264 btrfs_release_path(path);
1267 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1268 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1271 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1272 root_objectid, owner, offset);
1277 static int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1278 struct btrfs_root *root,
1279 struct btrfs_path *path,
1280 struct btrfs_extent_inline_ref *iref,
1283 struct extent_buffer *leaf;
1284 struct btrfs_extent_item *ei;
1285 struct btrfs_extent_data_ref *dref = NULL;
1286 struct btrfs_shared_data_ref *sref = NULL;
1295 leaf = path->nodes[0];
1296 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1297 refs = btrfs_extent_refs(leaf, ei);
1298 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1299 refs += refs_to_mod;
1300 btrfs_set_extent_refs(leaf, ei, refs);
1302 type = btrfs_extent_inline_ref_type(leaf, iref);
1304 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1305 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1306 refs = btrfs_extent_data_ref_count(leaf, dref);
1307 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1308 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1309 refs = btrfs_shared_data_ref_count(leaf, sref);
1312 BUG_ON(refs_to_mod != -1);
1315 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1316 refs += refs_to_mod;
1319 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1320 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1322 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1324 size = btrfs_extent_inline_ref_size(type);
1325 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1326 ptr = (unsigned long)iref;
1327 end = (unsigned long)ei + item_size;
1328 if (ptr + size < end)
1329 memmove_extent_buffer(leaf, ptr, ptr + size,
1332 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1335 btrfs_mark_buffer_dirty(leaf);
1339 static int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1340 struct btrfs_root *root,
1341 struct btrfs_path *path,
1342 u64 bytenr, u64 num_bytes, u64 parent,
1343 u64 root_objectid, u64 owner,
1344 u64 offset, int refs_to_add)
1346 struct btrfs_extent_inline_ref *iref;
1349 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1350 bytenr, num_bytes, parent,
1351 root_objectid, owner, offset, 1);
1353 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1354 ret = update_inline_extent_backref(trans, root, path, iref,
1356 } else if (ret == -ENOENT) {
1357 ret = setup_inline_extent_backref(trans, root, path, iref,
1358 parent, root_objectid,
1359 owner, offset, refs_to_add);
1364 static int insert_extent_backref(struct btrfs_trans_handle *trans,
1365 struct btrfs_root *root,
1366 struct btrfs_path *path,
1367 u64 bytenr, u64 parent, u64 root_objectid,
1368 u64 owner, u64 offset, int refs_to_add)
1372 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
1373 ret = insert_extent_data_ref(trans, root, path, bytenr,
1374 parent, root_objectid,
1375 owner, offset, refs_to_add);
1377 BUG_ON(refs_to_add != 1);
1378 ret = insert_tree_block_ref(trans, root, path, bytenr,
1379 parent, root_objectid);
1384 static int remove_extent_backref(struct btrfs_trans_handle *trans,
1385 struct btrfs_root *root,
1386 struct btrfs_path *path,
1387 struct btrfs_extent_inline_ref *iref,
1388 int refs_to_drop, int is_data)
1392 BUG_ON(!is_data && refs_to_drop != 1);
1394 ret = update_inline_extent_backref(trans, root, path, iref,
1396 } else if (is_data) {
1397 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1399 ret = btrfs_del_item(trans, root, path);
1404 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1405 struct btrfs_root *root,
1406 u64 bytenr, u64 num_bytes, u64 parent,
1407 u64 root_objectid, u64 owner, u64 offset)
1409 struct btrfs_path *path;
1410 struct extent_buffer *leaf;
1411 struct btrfs_extent_item *item;
1416 path = btrfs_alloc_path();
1421 path->leave_spinning = 1;
1423 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1424 path, bytenr, num_bytes, parent,
1425 root_objectid, owner, offset, 1);
1429 if (ret != -EAGAIN) {
1434 leaf = path->nodes[0];
1435 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1436 refs = btrfs_extent_refs(leaf, item);
1437 btrfs_set_extent_refs(leaf, item, refs + 1);
1439 btrfs_mark_buffer_dirty(leaf);
1440 btrfs_release_path(path);
1443 path->leave_spinning = 1;
1445 /* now insert the actual backref */
1446 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1447 path, bytenr, parent, root_objectid,
1452 btrfs_free_path(path);
1453 finish_current_insert(trans, root->fs_info->extent_root);
1454 del_pending_extents(trans, root->fs_info->extent_root);
1459 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1460 struct btrfs_root *root)
1462 finish_current_insert(trans, root->fs_info->extent_root);
1463 del_pending_extents(trans, root->fs_info->extent_root);
1467 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
1468 struct btrfs_root *root, u64 bytenr,
1469 u64 offset, int metadata, u64 *refs, u64 *flags)
1471 struct btrfs_path *path;
1473 struct btrfs_key key;
1474 struct extent_buffer *l;
1475 struct btrfs_extent_item *item;
1481 !btrfs_fs_incompat(root->fs_info,
1482 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA)) {
1483 offset = root->leafsize;
1487 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) {
1512 if (path->slots[0]) {
1514 btrfs_item_key_to_cpu(path->nodes[0], &key,
1516 if (key.objectid == bytenr &&
1517 key.type == BTRFS_EXTENT_ITEM_KEY &&
1518 key.offset == root->leafsize)
1523 btrfs_release_path(path);
1524 key.type = BTRFS_EXTENT_ITEM_KEY;
1525 key.offset = root->leafsize;
1537 item_size = btrfs_item_size_nr(l, path->slots[0]);
1538 if (item_size >= sizeof(*item)) {
1539 item = btrfs_item_ptr(l, path->slots[0],
1540 struct btrfs_extent_item);
1541 num_refs = btrfs_extent_refs(l, item);
1542 extent_flags = btrfs_extent_flags(l, item);
1544 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1545 struct btrfs_extent_item_v0 *ei0;
1546 BUG_ON(item_size != sizeof(*ei0));
1547 ei0 = btrfs_item_ptr(l, path->slots[0],
1548 struct btrfs_extent_item_v0);
1549 num_refs = btrfs_extent_refs_v0(l, ei0);
1550 /* FIXME: this isn't correct for data */
1551 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
1556 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1560 *flags = extent_flags;
1562 btrfs_free_path(path);
1566 int btrfs_set_block_flags(struct btrfs_trans_handle *trans,
1567 struct btrfs_root *root,
1568 u64 bytenr, int level, u64 flags)
1570 struct btrfs_path *path;
1572 struct btrfs_key key;
1573 struct extent_buffer *l;
1574 struct btrfs_extent_item *item;
1576 int skinny_metadata =
1577 btrfs_fs_incompat(root->fs_info,
1578 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
1580 path = btrfs_alloc_path();
1585 key.objectid = bytenr;
1586 if (skinny_metadata) {
1588 key.type = BTRFS_METADATA_ITEM_KEY;
1590 key.offset = root->leafsize;
1591 key.type = BTRFS_EXTENT_ITEM_KEY;
1595 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1600 if (ret > 0 && skinny_metadata) {
1601 skinny_metadata = 0;
1602 if (path->slots[0]) {
1604 btrfs_item_key_to_cpu(path->nodes[0], &key,
1606 if (key.objectid == bytenr &&
1607 key.offset == root->leafsize &&
1608 key.type == BTRFS_EXTENT_ITEM_KEY)
1612 btrfs_release_path(path);
1613 key.offset = root->leafsize;
1614 key.type = BTRFS_EXTENT_ITEM_KEY;
1620 btrfs_print_leaf(root, path->nodes[0]);
1621 printk("failed to find block number %Lu\n",
1622 (unsigned long long)bytenr);
1626 item_size = btrfs_item_size_nr(l, path->slots[0]);
1627 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1628 if (item_size < sizeof(*item)) {
1629 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1635 item_size = btrfs_item_size_nr(l, path->slots[0]);
1638 BUG_ON(item_size < sizeof(*item));
1639 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1640 flags |= btrfs_extent_flags(l, item);
1641 btrfs_set_extent_flags(l, item, flags);
1643 btrfs_free_path(path);
1644 finish_current_insert(trans, root->fs_info->extent_root);
1645 del_pending_extents(trans, root->fs_info->extent_root);
1649 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
1650 struct btrfs_root *root,
1651 struct extent_buffer *buf,
1652 int record_parent, int inc)
1659 struct btrfs_key key;
1660 struct btrfs_file_extent_item *fi;
1664 int (*process_func)(struct btrfs_trans_handle *trans,
1665 struct btrfs_root *root,
1666 u64, u64, u64, u64, u64, u64);
1668 ref_root = btrfs_header_owner(buf);
1669 nritems = btrfs_header_nritems(buf);
1670 level = btrfs_header_level(buf);
1672 if (!root->ref_cows && level == 0)
1676 process_func = btrfs_inc_extent_ref;
1678 process_func = btrfs_free_extent;
1681 parent = buf->start;
1685 for (i = 0; i < nritems; i++) {
1688 btrfs_item_key_to_cpu(buf, &key, i);
1689 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1691 fi = btrfs_item_ptr(buf, i,
1692 struct btrfs_file_extent_item);
1693 if (btrfs_file_extent_type(buf, fi) ==
1694 BTRFS_FILE_EXTENT_INLINE)
1696 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1700 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
1701 key.offset -= btrfs_file_extent_offset(buf, fi);
1702 ret = process_func(trans, root, bytenr, num_bytes,
1703 parent, ref_root, key.objectid,
1710 bytenr = btrfs_node_blockptr(buf, i);
1711 num_bytes = btrfs_level_size(root, level - 1);
1712 ret = process_func(trans, root, bytenr, num_bytes,
1713 parent, ref_root, level - 1, 0);
1726 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1727 struct extent_buffer *buf, int record_parent)
1729 return __btrfs_mod_ref(trans, root, buf, record_parent, 1);
1732 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1733 struct extent_buffer *buf, int record_parent)
1735 return __btrfs_mod_ref(trans, root, buf, record_parent, 0);
1738 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1739 struct btrfs_root *root,
1740 struct btrfs_path *path,
1741 struct btrfs_block_group_cache *cache)
1745 struct btrfs_root *extent_root = root->fs_info->extent_root;
1747 struct extent_buffer *leaf;
1749 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1754 leaf = path->nodes[0];
1755 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1756 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1757 btrfs_mark_buffer_dirty(leaf);
1758 btrfs_release_path(path);
1760 finish_current_insert(trans, extent_root);
1761 pending_ret = del_pending_extents(trans, extent_root);
1770 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1771 struct btrfs_root *root)
1773 struct extent_io_tree *block_group_cache;
1774 struct btrfs_block_group_cache *cache;
1776 struct btrfs_path *path;
1782 block_group_cache = &root->fs_info->block_group_cache;
1783 path = btrfs_alloc_path();
1788 ret = find_first_extent_bit(block_group_cache, last,
1789 &start, &end, BLOCK_GROUP_DIRTY);
1798 ret = get_state_private(block_group_cache, start, &ptr);
1801 clear_extent_bits(block_group_cache, start, end,
1802 BLOCK_GROUP_DIRTY, GFP_NOFS);
1804 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
1805 ret = write_one_cache_group(trans, root, path, cache);
1807 btrfs_free_path(path);
1811 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
1814 struct list_head *head = &info->space_info;
1815 struct list_head *cur;
1816 struct btrfs_space_info *found;
1817 list_for_each(cur, head) {
1818 found = list_entry(cur, struct btrfs_space_info, list);
1819 if (found->flags & flags)
1826 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1827 u64 total_bytes, u64 bytes_used,
1828 struct btrfs_space_info **space_info)
1830 struct btrfs_space_info *found;
1832 found = __find_space_info(info, flags);
1834 found->total_bytes += total_bytes;
1835 found->bytes_used += bytes_used;
1836 if (found->total_bytes < found->bytes_used) {
1837 fprintf(stderr, "warning, bad space info total_bytes "
1839 (unsigned long long)found->total_bytes,
1840 (unsigned long long)found->bytes_used);
1842 *space_info = found;
1845 found = kmalloc(sizeof(*found), GFP_NOFS);
1849 list_add(&found->list, &info->space_info);
1850 found->flags = flags;
1851 found->total_bytes = total_bytes;
1852 found->bytes_used = bytes_used;
1853 found->bytes_pinned = 0;
1855 *space_info = found;
1860 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1862 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1863 BTRFS_BLOCK_GROUP_RAID1 |
1864 BTRFS_BLOCK_GROUP_RAID10 |
1865 BTRFS_BLOCK_GROUP_RAID5 |
1866 BTRFS_BLOCK_GROUP_RAID6 |
1867 BTRFS_BLOCK_GROUP_DUP);
1869 if (flags & BTRFS_BLOCK_GROUP_DATA)
1870 fs_info->avail_data_alloc_bits |= extra_flags;
1871 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1872 fs_info->avail_metadata_alloc_bits |= extra_flags;
1873 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1874 fs_info->avail_system_alloc_bits |= extra_flags;
1878 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1879 struct btrfs_root *extent_root, u64 alloc_bytes,
1882 struct btrfs_space_info *space_info;
1888 space_info = __find_space_info(extent_root->fs_info, flags);
1890 ret = update_space_info(extent_root->fs_info, flags,
1894 BUG_ON(!space_info);
1896 if (space_info->full)
1899 thresh = div_factor(space_info->total_bytes, 7);
1900 if ((space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
1904 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes,
1906 if (ret == -ENOSPC) {
1907 space_info->full = 1;
1913 ret = btrfs_make_block_group(trans, extent_root, 0, space_info->flags,
1914 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
1919 static int update_block_group(struct btrfs_trans_handle *trans,
1920 struct btrfs_root *root,
1921 u64 bytenr, u64 num_bytes, int alloc,
1924 struct btrfs_block_group_cache *cache;
1925 struct btrfs_fs_info *info = root->fs_info;
1926 u64 total = num_bytes;
1932 /* block accounting for super block */
1933 old_val = btrfs_super_bytes_used(info->super_copy);
1935 old_val += num_bytes;
1937 old_val -= num_bytes;
1938 btrfs_set_super_bytes_used(info->super_copy, old_val);
1940 /* block accounting for root item */
1941 old_val = btrfs_root_used(&root->root_item);
1943 old_val += num_bytes;
1945 old_val -= num_bytes;
1946 btrfs_set_root_used(&root->root_item, old_val);
1949 cache = btrfs_lookup_block_group(info, bytenr);
1953 byte_in_group = bytenr - cache->key.objectid;
1954 WARN_ON(byte_in_group > cache->key.offset);
1955 start = cache->key.objectid;
1956 end = start + cache->key.offset - 1;
1957 set_extent_bits(&info->block_group_cache, start, end,
1958 BLOCK_GROUP_DIRTY, GFP_NOFS);
1960 old_val = btrfs_block_group_used(&cache->item);
1961 num_bytes = min(total, cache->key.offset - byte_in_group);
1964 old_val += num_bytes;
1965 cache->space_info->bytes_used += num_bytes;
1967 old_val -= num_bytes;
1968 cache->space_info->bytes_used -= num_bytes;
1970 set_extent_dirty(&info->free_space_cache,
1971 bytenr, bytenr + num_bytes - 1,
1975 btrfs_set_block_group_used(&cache->item, old_val);
1977 bytenr += num_bytes;
1982 static int update_pinned_extents(struct btrfs_root *root,
1983 u64 bytenr, u64 num, int pin)
1986 struct btrfs_block_group_cache *cache;
1987 struct btrfs_fs_info *fs_info = root->fs_info;
1990 set_extent_dirty(&fs_info->pinned_extents,
1991 bytenr, bytenr + num - 1, GFP_NOFS);
1993 clear_extent_dirty(&fs_info->pinned_extents,
1994 bytenr, bytenr + num - 1, GFP_NOFS);
1997 cache = btrfs_lookup_block_group(fs_info, bytenr);
1999 len = min((u64)root->sectorsize, num);
2003 len = min(num, cache->key.offset -
2004 (bytenr - cache->key.objectid));
2006 cache->pinned += len;
2007 cache->space_info->bytes_pinned += len;
2008 fs_info->total_pinned += len;
2010 cache->pinned -= len;
2011 cache->space_info->bytes_pinned -= len;
2012 fs_info->total_pinned -= len;
2021 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2022 struct btrfs_root *root,
2023 struct extent_io_tree *unpin)
2028 struct extent_io_tree *free_space_cache;
2029 free_space_cache = &root->fs_info->free_space_cache;
2032 ret = find_first_extent_bit(unpin, 0, &start, &end,
2036 update_pinned_extents(root, start, end + 1 - start, 0);
2037 clear_extent_dirty(unpin, start, end, GFP_NOFS);
2038 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
2043 static int extent_root_pending_ops(struct btrfs_fs_info *info)
2049 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
2050 &end, EXTENT_LOCKED);
2052 ret = find_first_extent_bit(&info->pending_del, 0, &start, &end,
2058 static int finish_current_insert(struct btrfs_trans_handle *trans,
2059 struct btrfs_root *extent_root)
2064 struct btrfs_fs_info *info = extent_root->fs_info;
2065 struct pending_extent_op *extent_op;
2066 struct btrfs_key key;
2068 int skinny_metadata =
2069 btrfs_fs_incompat(extent_root->fs_info,
2070 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
2073 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
2074 &end, EXTENT_LOCKED);
2078 ret = get_state_private(&info->extent_ins, start, &priv);
2080 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2082 if (extent_op->type == PENDING_EXTENT_INSERT) {
2083 key.objectid = start;
2084 if (skinny_metadata) {
2085 key.offset = extent_op->level;
2086 key.type = BTRFS_METADATA_ITEM_KEY;
2088 key.offset = extent_op->num_bytes;
2089 key.type = BTRFS_EXTENT_ITEM_KEY;
2091 ret = alloc_reserved_tree_block(trans, extent_root,
2092 extent_root->root_key.objectid,
2096 extent_op->level, &key);
2102 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
2109 static int pin_down_bytes(struct btrfs_trans_handle *trans,
2110 struct btrfs_root *root,
2111 u64 bytenr, u64 num_bytes, int is_data)
2114 struct extent_buffer *buf;
2119 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2123 /* we can reuse a block if it hasn't been written
2124 * and it is from this transaction. We can't
2125 * reuse anything from the tree log root because
2126 * it has tiny sub-transactions.
2128 if (btrfs_buffer_uptodate(buf, 0)) {
2129 u64 header_owner = btrfs_header_owner(buf);
2130 u64 header_transid = btrfs_header_generation(buf);
2131 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
2132 header_transid == trans->transid &&
2133 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2134 clean_tree_block(NULL, root, buf);
2135 free_extent_buffer(buf);
2139 free_extent_buffer(buf);
2141 update_pinned_extents(root, bytenr, num_bytes, 1);
2147 void btrfs_pin_extent(struct btrfs_fs_info *fs_info,
2148 u64 bytenr, u64 num_bytes)
2150 update_pinned_extents(fs_info->extent_root, bytenr, num_bytes, 1);
2153 void btrfs_unpin_extent(struct btrfs_fs_info *fs_info,
2154 u64 bytenr, u64 num_bytes)
2156 update_pinned_extents(fs_info->extent_root, bytenr, num_bytes, 0);
2160 * remove an extent from the root, returns 0 on success
2162 static int __free_extent(struct btrfs_trans_handle *trans,
2163 struct btrfs_root *root,
2164 u64 bytenr, u64 num_bytes, u64 parent,
2165 u64 root_objectid, u64 owner_objectid,
2166 u64 owner_offset, int refs_to_drop)
2169 struct btrfs_key key;
2170 struct btrfs_path *path;
2171 struct btrfs_extent_ops *ops = root->fs_info->extent_ops;
2172 struct btrfs_root *extent_root = root->fs_info->extent_root;
2173 struct extent_buffer *leaf;
2174 struct btrfs_extent_item *ei;
2175 struct btrfs_extent_inline_ref *iref;
2178 int extent_slot = 0;
2179 int found_extent = 0;
2183 int skinny_metadata =
2184 btrfs_fs_incompat(extent_root->fs_info,
2185 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
2187 if (root->fs_info->free_extent_hook) {
2188 root->fs_info->free_extent_hook(trans, root, bytenr, num_bytes,
2189 parent, root_objectid, owner_objectid,
2190 owner_offset, refs_to_drop);
2193 path = btrfs_alloc_path();
2198 path->leave_spinning = 1;
2200 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
2202 skinny_metadata = 0;
2203 BUG_ON(!is_data && refs_to_drop != 1);
2205 ret = lookup_extent_backref(trans, extent_root, path, &iref,
2206 bytenr, num_bytes, parent,
2207 root_objectid, owner_objectid,
2210 extent_slot = path->slots[0];
2211 while (extent_slot >= 0) {
2212 btrfs_item_key_to_cpu(path->nodes[0], &key,
2214 if (key.objectid != bytenr)
2216 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
2217 key.offset == num_bytes) {
2221 if (key.type == BTRFS_METADATA_ITEM_KEY &&
2222 key.offset == owner_objectid) {
2226 if (path->slots[0] - extent_slot > 5)
2230 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2231 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
2232 if (found_extent && item_size < sizeof(*ei))
2235 if (!found_extent) {
2237 ret = remove_extent_backref(trans, extent_root, path,
2241 btrfs_release_path(path);
2242 path->leave_spinning = 1;
2244 key.objectid = bytenr;
2246 if (skinny_metadata) {
2247 key.type = BTRFS_METADATA_ITEM_KEY;
2248 key.offset = owner_objectid;
2250 key.type = BTRFS_EXTENT_ITEM_KEY;
2251 key.offset = num_bytes;
2254 ret = btrfs_search_slot(trans, extent_root,
2256 if (ret > 0 && skinny_metadata && path->slots[0]) {
2258 btrfs_item_key_to_cpu(path->nodes[0],
2261 if (key.objectid == bytenr &&
2262 key.type == BTRFS_EXTENT_ITEM_KEY &&
2263 key.offset == num_bytes)
2267 if (ret > 0 && skinny_metadata) {
2268 skinny_metadata = 0;
2269 btrfs_release_path(path);
2270 key.type = BTRFS_EXTENT_ITEM_KEY;
2271 key.offset = num_bytes;
2272 ret = btrfs_search_slot(trans, extent_root,
2277 printk(KERN_ERR "umm, got %d back from search"
2278 ", was looking for %llu\n", ret,
2279 (unsigned long long)bytenr);
2280 btrfs_print_leaf(extent_root, path->nodes[0]);
2283 extent_slot = path->slots[0];
2286 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
2287 "parent %llu root %llu owner %llu offset %llu\n",
2288 (unsigned long long)bytenr,
2289 (unsigned long long)parent,
2290 (unsigned long long)root_objectid,
2291 (unsigned long long)owner_objectid,
2292 (unsigned long long)owner_offset);
2297 leaf = path->nodes[0];
2298 item_size = btrfs_item_size_nr(leaf, extent_slot);
2299 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2300 if (item_size < sizeof(*ei)) {
2301 BUG_ON(found_extent || extent_slot != path->slots[0]);
2302 ret = convert_extent_item_v0(trans, extent_root, path,
2306 btrfs_release_path(path);
2307 path->leave_spinning = 1;
2309 key.objectid = bytenr;
2310 key.type = BTRFS_EXTENT_ITEM_KEY;
2311 key.offset = num_bytes;
2313 ret = btrfs_search_slot(trans, extent_root, &key, path,
2316 printk(KERN_ERR "umm, got %d back from search"
2317 ", was looking for %llu\n", ret,
2318 (unsigned long long)bytenr);
2319 btrfs_print_leaf(extent_root, path->nodes[0]);
2322 extent_slot = path->slots[0];
2323 leaf = path->nodes[0];
2324 item_size = btrfs_item_size_nr(leaf, extent_slot);
2327 BUG_ON(item_size < sizeof(*ei));
2328 ei = btrfs_item_ptr(leaf, extent_slot,
2329 struct btrfs_extent_item);
2330 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
2331 key.type == BTRFS_EXTENT_ITEM_KEY) {
2332 struct btrfs_tree_block_info *bi;
2333 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
2334 bi = (struct btrfs_tree_block_info *)(ei + 1);
2335 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
2338 refs = btrfs_extent_refs(leaf, ei);
2339 BUG_ON(refs < refs_to_drop);
2340 refs -= refs_to_drop;
2344 * In the case of inline back ref, reference count will
2345 * be updated by remove_extent_backref
2348 BUG_ON(!found_extent);
2350 btrfs_set_extent_refs(leaf, ei, refs);
2351 btrfs_mark_buffer_dirty(leaf);
2354 ret = remove_extent_backref(trans, extent_root, path,
2364 BUG_ON(is_data && refs_to_drop !=
2365 extent_data_ref_count(root, path, iref));
2367 BUG_ON(path->slots[0] != extent_slot);
2369 BUG_ON(path->slots[0] != extent_slot + 1);
2370 path->slots[0] = extent_slot;
2375 if (ops && ops->free_extent) {
2376 ret = ops->free_extent(root, bytenr, num_bytes);
2384 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2391 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2394 btrfs_release_path(path);
2397 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2401 update_block_group(trans, root, bytenr, num_bytes, 0, mark_free);
2404 btrfs_free_path(path);
2405 finish_current_insert(trans, extent_root);
2410 * find all the blocks marked as pending in the radix tree and remove
2411 * them from the extent map
2413 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
2414 btrfs_root *extent_root)
2421 struct extent_io_tree *pending_del;
2422 struct extent_io_tree *extent_ins;
2423 struct pending_extent_op *extent_op;
2425 extent_ins = &extent_root->fs_info->extent_ins;
2426 pending_del = &extent_root->fs_info->pending_del;
2429 ret = find_first_extent_bit(pending_del, 0, &start, &end,
2434 ret = get_state_private(pending_del, start, &priv);
2436 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2438 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
2441 if (!test_range_bit(extent_ins, start, end,
2442 EXTENT_LOCKED, 0)) {
2443 ret = __free_extent(trans, extent_root,
2444 start, end + 1 - start, 0,
2445 extent_root->root_key.objectid,
2446 extent_op->level, 0, 1);
2450 ret = get_state_private(extent_ins, start, &priv);
2452 extent_op = (struct pending_extent_op *)
2453 (unsigned long)priv;
2455 clear_extent_bits(extent_ins, start, end,
2456 EXTENT_LOCKED, GFP_NOFS);
2458 if (extent_op->type == PENDING_BACKREF_UPDATE)
2470 * remove an extent from the root, returns 0 on success
2473 int btrfs_free_extent(struct btrfs_trans_handle *trans,
2474 struct btrfs_root *root,
2475 u64 bytenr, u64 num_bytes, u64 parent,
2476 u64 root_objectid, u64 owner, u64 offset)
2478 struct btrfs_root *extent_root = root->fs_info->extent_root;
2482 WARN_ON(num_bytes < root->sectorsize);
2483 if (root == extent_root) {
2484 struct pending_extent_op *extent_op;
2486 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2489 extent_op->type = PENDING_EXTENT_DELETE;
2490 extent_op->bytenr = bytenr;
2491 extent_op->num_bytes = num_bytes;
2492 extent_op->level = (int)owner;
2494 set_extent_bits(&root->fs_info->pending_del,
2495 bytenr, bytenr + num_bytes - 1,
2496 EXTENT_LOCKED, GFP_NOFS);
2497 set_state_private(&root->fs_info->pending_del,
2498 bytenr, (unsigned long)extent_op);
2501 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2502 root_objectid, owner, offset, 1);
2503 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
2504 return ret ? ret : pending_ret;
2507 static u64 stripe_align(struct btrfs_root *root, u64 val)
2509 u64 mask = ((u64)root->stripesize - 1);
2510 u64 ret = (val + mask) & ~mask;
2515 * walks the btree of allocated extents and find a hole of a given size.
2516 * The key ins is changed to record the hole:
2517 * ins->objectid == block start
2518 * ins->flags = BTRFS_EXTENT_ITEM_KEY
2519 * ins->offset == number of blocks
2520 * Any available blocks before search_start are skipped.
2522 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2523 struct btrfs_root *orig_root,
2524 u64 num_bytes, u64 empty_size,
2525 u64 search_start, u64 search_end,
2526 u64 hint_byte, struct btrfs_key *ins,
2527 u64 exclude_start, u64 exclude_nr,
2531 u64 orig_search_start = search_start;
2532 struct btrfs_root * root = orig_root->fs_info->extent_root;
2533 struct btrfs_fs_info *info = root->fs_info;
2534 u64 total_needed = num_bytes;
2535 struct btrfs_block_group_cache *block_group;
2539 WARN_ON(num_bytes < root->sectorsize);
2540 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2542 search_start = stripe_align(root, search_start);
2545 block_group = btrfs_lookup_first_block_group(info, hint_byte);
2547 hint_byte = search_start;
2548 block_group = btrfs_find_block_group(root, block_group,
2549 hint_byte, data, 1);
2551 block_group = btrfs_find_block_group(root,
2553 search_start, data, 1);
2556 total_needed += empty_size;
2559 search_start = stripe_align(root, search_start);
2561 block_group = btrfs_lookup_first_block_group(info,
2564 block_group = btrfs_lookup_first_block_group(info,
2567 ret = find_search_start(root, &block_group, &search_start,
2568 total_needed, data);
2572 ins->objectid = search_start;
2573 ins->offset = num_bytes;
2575 if (ins->objectid + num_bytes >
2576 block_group->key.objectid + block_group->key.offset) {
2577 search_start = block_group->key.objectid +
2578 block_group->key.offset;
2582 if (test_range_bit(&info->extent_ins, ins->objectid,
2583 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
2584 search_start = ins->objectid + num_bytes;
2588 if (test_range_bit(&info->pinned_extents, ins->objectid,
2589 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
2590 search_start = ins->objectid + num_bytes;
2594 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
2595 ins->objectid < exclude_start + exclude_nr)) {
2596 search_start = exclude_start + exclude_nr;
2600 if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
2601 block_group = btrfs_lookup_block_group(info, ins->objectid);
2603 trans->block_group = block_group;
2605 ins->offset = num_bytes;
2609 block_group = btrfs_lookup_first_block_group(info, search_start);
2611 search_start = orig_search_start;
2618 total_needed -= empty_size;
2624 block_group = btrfs_find_block_group(root, block_group,
2625 search_start, data, 0);
2632 static int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2633 struct btrfs_root *root,
2634 u64 num_bytes, u64 empty_size,
2635 u64 hint_byte, u64 search_end,
2636 struct btrfs_key *ins, int data)
2639 u64 search_start = 0;
2641 struct btrfs_fs_info *info = root->fs_info;
2643 if (info->extent_ops) {
2644 struct btrfs_extent_ops *ops = info->extent_ops;
2645 ret = ops->alloc_extent(root, num_bytes, hint_byte, ins);
2651 alloc_profile = info->avail_data_alloc_bits &
2652 info->data_alloc_profile;
2653 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2654 } else if ((info->system_allocs > 0 || root == info->chunk_root) &&
2655 info->system_allocs >= 0) {
2656 alloc_profile = info->avail_system_alloc_bits &
2657 info->system_alloc_profile;
2658 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2660 alloc_profile = info->avail_metadata_alloc_bits &
2661 info->metadata_alloc_profile;
2662 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2665 if (root->ref_cows) {
2666 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
2667 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2669 BTRFS_BLOCK_GROUP_METADATA);
2672 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2673 num_bytes + 2 * 1024 * 1024, data);
2677 WARN_ON(num_bytes < root->sectorsize);
2678 ret = find_free_extent(trans, root, num_bytes, empty_size,
2679 search_start, search_end, hint_byte, ins,
2680 trans->alloc_exclude_start,
2681 trans->alloc_exclude_nr, data);
2684 clear_extent_dirty(&root->fs_info->free_space_cache,
2685 ins->objectid, ins->objectid + ins->offset - 1,
2690 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
2691 struct btrfs_root *root,
2692 u64 root_objectid, u64 generation,
2693 u64 flags, struct btrfs_disk_key *key,
2694 int level, struct btrfs_key *ins)
2697 struct btrfs_fs_info *fs_info = root->fs_info;
2698 struct btrfs_extent_item *extent_item;
2699 struct btrfs_tree_block_info *block_info;
2700 struct btrfs_extent_inline_ref *iref;
2701 struct btrfs_path *path;
2702 struct extent_buffer *leaf;
2703 u32 size = sizeof(*extent_item) + sizeof(*iref);
2704 int skinny_metadata =
2705 btrfs_fs_incompat(fs_info,
2706 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
2708 if (!skinny_metadata)
2709 size += sizeof(*block_info);
2711 path = btrfs_alloc_path();
2714 path->leave_spinning = 1;
2715 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
2719 leaf = path->nodes[0];
2720 extent_item = btrfs_item_ptr(leaf, path->slots[0],
2721 struct btrfs_extent_item);
2722 btrfs_set_extent_refs(leaf, extent_item, 1);
2723 btrfs_set_extent_generation(leaf, extent_item, generation);
2724 btrfs_set_extent_flags(leaf, extent_item,
2725 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
2727 if (skinny_metadata) {
2728 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
2730 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
2731 btrfs_set_tree_block_key(leaf, block_info, key);
2732 btrfs_set_tree_block_level(leaf, block_info, level);
2733 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
2736 btrfs_set_extent_inline_ref_type(leaf, iref, BTRFS_TREE_BLOCK_REF_KEY);
2737 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
2739 btrfs_mark_buffer_dirty(leaf);
2740 btrfs_free_path(path);
2742 ret = update_block_group(trans, root, ins->objectid, root->leafsize,
2747 static int alloc_tree_block(struct btrfs_trans_handle *trans,
2748 struct btrfs_root *root, u64 num_bytes,
2749 u64 root_objectid, u64 generation,
2750 u64 flags, struct btrfs_disk_key *key,
2751 int level, u64 empty_size, u64 hint_byte,
2752 u64 search_end, struct btrfs_key *ins)
2755 ret = btrfs_reserve_extent(trans, root, num_bytes, empty_size,
2756 hint_byte, search_end, ins, 0);
2759 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID) {
2760 struct pending_extent_op *extent_op;
2762 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2765 extent_op->type = PENDING_EXTENT_INSERT;
2766 extent_op->bytenr = ins->objectid;
2767 extent_op->num_bytes = ins->offset;
2768 extent_op->level = level;
2769 extent_op->flags = flags;
2770 memcpy(&extent_op->key, key, sizeof(*key));
2772 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2773 ins->objectid + ins->offset - 1,
2774 EXTENT_LOCKED, GFP_NOFS);
2775 set_state_private(&root->fs_info->extent_ins,
2776 ins->objectid, (unsigned long)extent_op);
2778 if (btrfs_fs_incompat(root->fs_info,
2779 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA)) {
2780 ins->offset = level;
2781 ins->type = BTRFS_METADATA_ITEM_KEY;
2783 ret = alloc_reserved_tree_block(trans, root, root_objectid,
2786 finish_current_insert(trans, root->fs_info->extent_root);
2787 del_pending_extents(trans, root->fs_info->extent_root);
2793 * helper function to allocate a block for a given tree
2794 * returns the tree buffer or NULL.
2796 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
2797 struct btrfs_root *root,
2798 u32 blocksize, u64 root_objectid,
2799 struct btrfs_disk_key *key, int level,
2800 u64 hint, u64 empty_size)
2802 struct btrfs_key ins;
2804 struct extent_buffer *buf;
2806 ret = alloc_tree_block(trans, root, blocksize, root_objectid,
2807 trans->transid, 0, key, level,
2808 empty_size, hint, (u64)-1, &ins);
2811 return ERR_PTR(ret);
2814 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
2816 btrfs_free_extent(trans, root, ins.objectid, ins.offset,
2817 0, root->root_key.objectid, level, 0);
2819 return ERR_PTR(-ENOMEM);
2821 btrfs_set_buffer_uptodate(buf);
2822 trans->blocks_used++;
2829 static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
2830 struct btrfs_root *root,
2831 struct extent_buffer *leaf)
2834 u64 leaf_generation;
2835 struct btrfs_key key;
2836 struct btrfs_file_extent_item *fi;
2841 BUG_ON(!btrfs_is_leaf(leaf));
2842 nritems = btrfs_header_nritems(leaf);
2843 leaf_owner = btrfs_header_owner(leaf);
2844 leaf_generation = btrfs_header_generation(leaf);
2846 for (i = 0; i < nritems; i++) {
2849 btrfs_item_key_to_cpu(leaf, &key, i);
2850 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2852 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
2853 if (btrfs_file_extent_type(leaf, fi) ==
2854 BTRFS_FILE_EXTENT_INLINE)
2857 * FIXME make sure to insert a trans record that
2858 * repeats the snapshot del on crash
2860 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2861 if (disk_bytenr == 0)
2863 ret = btrfs_free_extent(trans, root, disk_bytenr,
2864 btrfs_file_extent_disk_num_bytes(leaf, fi),
2865 leaf->start, leaf_owner, leaf_generation,
2872 static void noinline reada_walk_down(struct btrfs_root *root,
2873 struct extent_buffer *node,
2886 nritems = btrfs_header_nritems(node);
2887 level = btrfs_header_level(node);
2891 for (i = slot; i < nritems && skipped < 32; i++) {
2892 bytenr = btrfs_node_blockptr(node, i);
2893 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
2894 (last > bytenr && last - bytenr > 32 * 1024))) {
2898 blocksize = btrfs_level_size(root, level - 1);
2900 ret = btrfs_lookup_extent_ref(NULL, root, bytenr,
2908 mutex_unlock(&root->fs_info->fs_mutex);
2909 ret = readahead_tree_block(root, bytenr, blocksize,
2910 btrfs_node_ptr_generation(node, i));
2911 last = bytenr + blocksize;
2913 mutex_lock(&root->fs_info->fs_mutex);
2920 * helper function for drop_snapshot, this walks down the tree dropping ref
2921 * counts as it goes.
2923 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2924 struct btrfs_root *root,
2925 struct btrfs_path *path, int *level)
2931 struct extent_buffer *next;
2932 struct extent_buffer *cur;
2933 struct extent_buffer *parent;
2938 WARN_ON(*level < 0);
2939 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2940 ret = btrfs_lookup_extent_ref(trans, root,
2941 path->nodes[*level]->start,
2942 path->nodes[*level]->len, &refs);
2948 * walk down to the last node level and free all the leaves
2950 while(*level >= 0) {
2951 WARN_ON(*level < 0);
2952 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2953 cur = path->nodes[*level];
2955 if (btrfs_header_level(cur) != *level)
2958 if (path->slots[*level] >=
2959 btrfs_header_nritems(cur))
2962 ret = drop_leaf_ref(trans, root, cur);
2966 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2967 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2968 blocksize = btrfs_level_size(root, *level - 1);
2969 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
2973 parent = path->nodes[*level];
2974 root_owner = btrfs_header_owner(parent);
2975 root_gen = btrfs_header_generation(parent);
2976 path->slots[*level]++;
2977 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
2978 parent->start, root_owner,
2979 root_gen, *level - 1, 1);
2983 next = btrfs_find_tree_block(root, bytenr, blocksize);
2984 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
2985 free_extent_buffer(next);
2986 reada_walk_down(root, cur, path->slots[*level]);
2987 mutex_unlock(&root->fs_info->fs_mutex);
2988 next = read_tree_block(root, bytenr, blocksize,
2990 mutex_lock(&root->fs_info->fs_mutex);
2992 WARN_ON(*level <= 0);
2993 if (path->nodes[*level-1])
2994 free_extent_buffer(path->nodes[*level-1]);
2995 path->nodes[*level-1] = next;
2996 *level = btrfs_header_level(next);
2997 path->slots[*level] = 0;
3000 WARN_ON(*level < 0);
3001 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3003 if (path->nodes[*level] == root->node) {
3004 root_owner = root->root_key.objectid;
3005 parent = path->nodes[*level];
3007 parent = path->nodes[*level + 1];
3008 root_owner = btrfs_header_owner(parent);
3011 root_gen = btrfs_header_generation(parent);
3012 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
3013 path->nodes[*level]->len, parent->start,
3014 root_owner, root_gen, *level, 1);
3015 free_extent_buffer(path->nodes[*level]);
3016 path->nodes[*level] = NULL;
3023 * helper for dropping snapshots. This walks back up the tree in the path
3024 * to find the first node higher up where we haven't yet gone through
3027 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3028 struct btrfs_root *root,
3029 struct btrfs_path *path, int *level)
3033 struct btrfs_root_item *root_item = &root->root_item;
3038 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
3039 slot = path->slots[i];
3040 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3041 struct extent_buffer *node;
3042 struct btrfs_disk_key disk_key;
3043 node = path->nodes[i];
3046 WARN_ON(*level == 0);
3047 btrfs_node_key(node, &disk_key, path->slots[i]);
3048 memcpy(&root_item->drop_progress,
3049 &disk_key, sizeof(disk_key));
3050 root_item->drop_level = i;
3053 struct extent_buffer *parent;
3054 if (path->nodes[*level] == root->node)
3055 parent = path->nodes[*level];
3057 parent = path->nodes[*level + 1];
3059 root_owner = btrfs_header_owner(parent);
3060 root_gen = btrfs_header_generation(parent);
3061 ret = btrfs_free_extent(trans, root,
3062 path->nodes[*level]->start,
3063 path->nodes[*level]->len,
3064 parent->start, root_owner,
3065 root_gen, *level, 1);
3067 free_extent_buffer(path->nodes[*level]);
3068 path->nodes[*level] = NULL;
3077 int btrfs_free_block_groups(struct btrfs_fs_info *info)
3079 struct btrfs_space_info *sinfo;
3080 struct btrfs_block_group_cache *cache;
3087 ret = find_first_extent_bit(&info->block_group_cache, 0,
3088 &start, &end, (unsigned int)-1);
3091 ret = get_state_private(&info->block_group_cache, start, &ptr);
3093 cache = (struct btrfs_block_group_cache *)
3095 if (cache->free_space_ctl) {
3096 btrfs_remove_free_space_cache(cache);
3097 kfree(cache->free_space_ctl);
3101 clear_extent_bits(&info->block_group_cache, start,
3102 end, (unsigned int)-1, GFP_NOFS);
3105 ret = find_first_extent_bit(&info->free_space_cache, 0,
3106 &start, &end, EXTENT_DIRTY);
3109 clear_extent_dirty(&info->free_space_cache, start,
3113 while (!list_empty(&info->space_info)) {
3114 sinfo = list_entry(info->space_info.next,
3115 struct btrfs_space_info, list);
3116 list_del_init(&sinfo->list);
3122 static int find_first_block_group(struct btrfs_root *root,
3123 struct btrfs_path *path, struct btrfs_key *key)
3126 struct btrfs_key found_key;
3127 struct extent_buffer *leaf;
3130 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
3134 slot = path->slots[0];
3135 leaf = path->nodes[0];
3136 if (slot >= btrfs_header_nritems(leaf)) {
3137 ret = btrfs_next_leaf(root, path);
3144 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3146 if (found_key.objectid >= key->objectid &&
3147 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
3156 int btrfs_read_block_groups(struct btrfs_root *root)
3158 struct btrfs_path *path;
3161 struct btrfs_block_group_cache *cache;
3162 struct btrfs_fs_info *info = root->fs_info;
3163 struct btrfs_space_info *space_info;
3164 struct extent_io_tree *block_group_cache;
3165 struct btrfs_key key;
3166 struct btrfs_key found_key;
3167 struct extent_buffer *leaf;
3169 block_group_cache = &info->block_group_cache;
3171 root = info->extent_root;
3174 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3175 path = btrfs_alloc_path();
3180 ret = find_first_block_group(root, path, &key);
3188 leaf = path->nodes[0];
3189 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3190 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3196 read_extent_buffer(leaf, &cache->item,
3197 btrfs_item_ptr_offset(leaf, path->slots[0]),
3198 sizeof(cache->item));
3199 memcpy(&cache->key, &found_key, sizeof(found_key));
3202 key.objectid = found_key.objectid + found_key.offset;
3203 btrfs_release_path(path);
3204 cache->flags = btrfs_block_group_flags(&cache->item);
3206 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
3207 bit = BLOCK_GROUP_DATA;
3208 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
3209 bit = BLOCK_GROUP_SYSTEM;
3210 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
3211 bit = BLOCK_GROUP_METADATA;
3213 set_avail_alloc_bits(info, cache->flags);
3214 if (btrfs_chunk_readonly(root, cache->key.objectid))
3217 ret = update_space_info(info, cache->flags, found_key.offset,
3218 btrfs_block_group_used(&cache->item),
3221 cache->space_info = space_info;
3223 /* use EXTENT_LOCKED to prevent merging */
3224 set_extent_bits(block_group_cache, found_key.objectid,
3225 found_key.objectid + found_key.offset - 1,
3226 bit | EXTENT_LOCKED, GFP_NOFS);
3227 set_state_private(block_group_cache, found_key.objectid,
3228 (unsigned long)cache);
3232 btrfs_free_path(path);
3236 struct btrfs_block_group_cache *
3237 btrfs_add_block_group(struct btrfs_fs_info *fs_info, u64 bytes_used, u64 type,
3238 u64 chunk_objectid, u64 chunk_offset, u64 size)
3242 struct btrfs_block_group_cache *cache;
3243 struct extent_io_tree *block_group_cache;
3245 block_group_cache = &fs_info->block_group_cache;
3247 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3249 cache->key.objectid = chunk_offset;
3250 cache->key.offset = size;
3252 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3253 btrfs_set_block_group_used(&cache->item, bytes_used);
3254 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
3255 cache->flags = type;
3256 btrfs_set_block_group_flags(&cache->item, type);
3258 ret = update_space_info(fs_info, cache->flags, size, bytes_used,
3259 &cache->space_info);
3262 bit = block_group_state_bits(type);
3263 ret = set_extent_bits(block_group_cache, chunk_offset,
3264 chunk_offset + size - 1,
3265 bit | EXTENT_LOCKED, GFP_NOFS);
3268 ret = set_state_private(block_group_cache, chunk_offset,
3269 (unsigned long)cache);
3271 set_avail_alloc_bits(fs_info, type);
3276 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3277 struct btrfs_root *root, u64 bytes_used,
3278 u64 type, u64 chunk_objectid, u64 chunk_offset,
3282 struct btrfs_root *extent_root;
3283 struct btrfs_block_group_cache *cache;
3285 cache = btrfs_add_block_group(root->fs_info, bytes_used, type,
3286 chunk_objectid, chunk_offset, size);
3287 extent_root = root->fs_info->extent_root;
3288 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3289 sizeof(cache->item));
3292 ret = finish_current_insert(trans, extent_root);
3294 ret = del_pending_extents(trans, extent_root);
3301 * This is for converter use only.
3303 * In that case, we don't know where are free blocks located.
3304 * Therefore all block group cache entries must be setup properly
3305 * before doing any block allocation.
3307 int btrfs_make_block_groups(struct btrfs_trans_handle *trans,
3308 struct btrfs_root *root)
3316 u64 total_metadata = 0;
3320 struct btrfs_root *extent_root;
3321 struct btrfs_block_group_cache *cache;
3322 struct extent_io_tree *block_group_cache;
3324 extent_root = root->fs_info->extent_root;
3325 block_group_cache = &root->fs_info->block_group_cache;
3326 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3327 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
3328 group_align = 64 * root->sectorsize;
3331 while (cur_start < total_bytes) {
3332 group_size = total_bytes / 12;
3333 group_size = min_t(u64, group_size, total_bytes - cur_start);
3334 if (cur_start == 0) {
3335 bit = BLOCK_GROUP_SYSTEM;
3336 group_type = BTRFS_BLOCK_GROUP_SYSTEM;
3338 group_size &= ~(group_align - 1);
3339 group_size = max_t(u64, group_size, 8 * 1024 * 1024);
3340 group_size = min_t(u64, group_size, 32 * 1024 * 1024);
3342 group_size &= ~(group_align - 1);
3343 if (total_data >= total_metadata * 2) {
3344 group_type = BTRFS_BLOCK_GROUP_METADATA;
3345 group_size = min_t(u64, group_size,
3346 1ULL * 1024 * 1024 * 1024);
3347 total_metadata += group_size;
3349 group_type = BTRFS_BLOCK_GROUP_DATA;
3350 group_size = min_t(u64, group_size,
3351 5ULL * 1024 * 1024 * 1024);
3352 total_data += group_size;
3354 if ((total_bytes - cur_start) * 4 < group_size * 5)
3355 group_size = total_bytes - cur_start;
3358 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3361 cache->key.objectid = cur_start;
3362 cache->key.offset = group_size;
3363 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3365 btrfs_set_block_group_used(&cache->item, 0);
3366 btrfs_set_block_group_chunk_objectid(&cache->item,
3368 btrfs_set_block_group_flags(&cache->item, group_type);
3370 cache->flags = group_type;
3372 ret = update_space_info(root->fs_info, group_type, group_size,
3373 0, &cache->space_info);
3375 set_avail_alloc_bits(extent_root->fs_info, group_type);
3377 set_extent_bits(block_group_cache, cur_start,
3378 cur_start + group_size - 1,
3379 bit | EXTENT_LOCKED, GFP_NOFS);
3380 set_state_private(block_group_cache, cur_start,
3381 (unsigned long)cache);
3382 cur_start += group_size;
3384 /* then insert all the items */
3386 while(cur_start < total_bytes) {
3387 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
3390 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3391 sizeof(cache->item));
3394 finish_current_insert(trans, extent_root);
3395 ret = del_pending_extents(trans, extent_root);
3398 cur_start = cache->key.objectid + cache->key.offset;
3403 int btrfs_update_block_group(struct btrfs_trans_handle *trans,
3404 struct btrfs_root *root,
3405 u64 bytenr, u64 num_bytes, int alloc,
3408 return update_block_group(trans, root, bytenr, num_bytes,
3413 * Fixup block accounting. The initial block accounting created by
3414 * make_block_groups isn't accuracy in this case.
3416 int btrfs_fix_block_accounting(struct btrfs_trans_handle *trans,
3417 struct btrfs_root *root)
3423 struct btrfs_path path;
3424 struct btrfs_key key;
3425 struct extent_buffer *leaf;
3426 struct btrfs_block_group_cache *cache;
3427 struct btrfs_fs_info *fs_info = root->fs_info;
3429 root = root->fs_info->extent_root;
3431 while(extent_root_pending_ops(fs_info)) {
3432 ret = finish_current_insert(trans, root);
3435 ret = del_pending_extents(trans, root);
3441 cache = btrfs_lookup_first_block_group(fs_info, start);
3444 start = cache->key.objectid + cache->key.offset;
3445 btrfs_set_block_group_used(&cache->item, 0);
3446 cache->space_info->bytes_used = 0;
3447 set_extent_bits(&root->fs_info->block_group_cache,
3448 cache->key.objectid,
3449 cache->key.objectid + cache->key.offset -1,
3450 BLOCK_GROUP_DIRTY, GFP_NOFS);
3453 btrfs_init_path(&path);
3456 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
3457 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
3462 leaf = path.nodes[0];
3463 slot = path.slots[0];
3464 if (slot >= btrfs_header_nritems(leaf)) {
3465 ret = btrfs_next_leaf(root, &path);
3470 leaf = path.nodes[0];
3471 slot = path.slots[0];
3473 btrfs_item_key_to_cpu(leaf, &key, slot);
3474 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
3475 bytes_used += key.offset;
3476 ret = btrfs_update_block_group(trans, root,
3477 key.objectid, key.offset, 1, 0);
3479 } else if (key.type == BTRFS_METADATA_ITEM_KEY) {
3480 bytes_used += root->leafsize;
3481 ret = btrfs_update_block_group(trans, root,
3482 key.objectid, root->leafsize, 1, 0);
3487 btrfs_set_super_bytes_used(root->fs_info->super_copy, bytes_used);
3488 btrfs_release_path(&path);