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.
18 #include <linux/sched.h>
19 #include <linux/pagemap.h>
20 #include <linux/writeback.h>
21 #include <linux/blkdev.h>
22 #include <linux/sort.h>
23 #include <linux/rcupdate.h>
24 #include <linux/kthread.h>
25 #include <linux/slab.h>
30 #include "print-tree.h"
31 #include "transaction.h"
34 #include "free-space-cache.h"
36 static int update_block_group(struct btrfs_trans_handle
*trans
,
37 struct btrfs_root
*root
,
38 u64 bytenr
, u64 num_bytes
, int alloc
);
39 static int update_reserved_bytes(struct btrfs_block_group_cache
*cache
,
40 u64 num_bytes
, int reserve
, int sinfo
);
41 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
42 struct btrfs_root
*root
,
43 u64 bytenr
, u64 num_bytes
, u64 parent
,
44 u64 root_objectid
, u64 owner_objectid
,
45 u64 owner_offset
, int refs_to_drop
,
46 struct btrfs_delayed_extent_op
*extra_op
);
47 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op
*extent_op
,
48 struct extent_buffer
*leaf
,
49 struct btrfs_extent_item
*ei
);
50 static int alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
51 struct btrfs_root
*root
,
52 u64 parent
, u64 root_objectid
,
53 u64 flags
, u64 owner
, u64 offset
,
54 struct btrfs_key
*ins
, int ref_mod
);
55 static int alloc_reserved_tree_block(struct btrfs_trans_handle
*trans
,
56 struct btrfs_root
*root
,
57 u64 parent
, u64 root_objectid
,
58 u64 flags
, struct btrfs_disk_key
*key
,
59 int level
, struct btrfs_key
*ins
);
60 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
61 struct btrfs_root
*extent_root
, u64 alloc_bytes
,
62 u64 flags
, int force
);
63 static int find_next_key(struct btrfs_path
*path
, int level
,
64 struct btrfs_key
*key
);
65 static void dump_space_info(struct btrfs_space_info
*info
, u64 bytes
,
66 int dump_block_groups
);
69 block_group_cache_done(struct btrfs_block_group_cache
*cache
)
72 return cache
->cached
== BTRFS_CACHE_FINISHED
;
75 static int block_group_bits(struct btrfs_block_group_cache
*cache
, u64 bits
)
77 return (cache
->flags
& bits
) == bits
;
80 void btrfs_get_block_group(struct btrfs_block_group_cache
*cache
)
82 atomic_inc(&cache
->count
);
85 void btrfs_put_block_group(struct btrfs_block_group_cache
*cache
)
87 if (atomic_dec_and_test(&cache
->count
)) {
88 WARN_ON(cache
->pinned
> 0);
89 WARN_ON(cache
->reserved
> 0);
90 WARN_ON(cache
->reserved_pinned
> 0);
96 * this adds the block group to the fs_info rb tree for the block group
99 static int btrfs_add_block_group_cache(struct btrfs_fs_info
*info
,
100 struct btrfs_block_group_cache
*block_group
)
103 struct rb_node
*parent
= NULL
;
104 struct btrfs_block_group_cache
*cache
;
106 spin_lock(&info
->block_group_cache_lock
);
107 p
= &info
->block_group_cache_tree
.rb_node
;
111 cache
= rb_entry(parent
, struct btrfs_block_group_cache
,
113 if (block_group
->key
.objectid
< cache
->key
.objectid
) {
115 } else if (block_group
->key
.objectid
> cache
->key
.objectid
) {
118 spin_unlock(&info
->block_group_cache_lock
);
123 rb_link_node(&block_group
->cache_node
, parent
, p
);
124 rb_insert_color(&block_group
->cache_node
,
125 &info
->block_group_cache_tree
);
126 spin_unlock(&info
->block_group_cache_lock
);
132 * This will return the block group at or after bytenr if contains is 0, else
133 * it will return the block group that contains the bytenr
135 static struct btrfs_block_group_cache
*
136 block_group_cache_tree_search(struct btrfs_fs_info
*info
, u64 bytenr
,
139 struct btrfs_block_group_cache
*cache
, *ret
= NULL
;
143 spin_lock(&info
->block_group_cache_lock
);
144 n
= info
->block_group_cache_tree
.rb_node
;
147 cache
= rb_entry(n
, struct btrfs_block_group_cache
,
149 end
= cache
->key
.objectid
+ cache
->key
.offset
- 1;
150 start
= cache
->key
.objectid
;
152 if (bytenr
< start
) {
153 if (!contains
&& (!ret
|| start
< ret
->key
.objectid
))
156 } else if (bytenr
> start
) {
157 if (contains
&& bytenr
<= end
) {
168 btrfs_get_block_group(ret
);
169 spin_unlock(&info
->block_group_cache_lock
);
174 static int add_excluded_extent(struct btrfs_root
*root
,
175 u64 start
, u64 num_bytes
)
177 u64 end
= start
+ num_bytes
- 1;
178 set_extent_bits(&root
->fs_info
->freed_extents
[0],
179 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
180 set_extent_bits(&root
->fs_info
->freed_extents
[1],
181 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
185 static void free_excluded_extents(struct btrfs_root
*root
,
186 struct btrfs_block_group_cache
*cache
)
190 start
= cache
->key
.objectid
;
191 end
= start
+ cache
->key
.offset
- 1;
193 clear_extent_bits(&root
->fs_info
->freed_extents
[0],
194 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
195 clear_extent_bits(&root
->fs_info
->freed_extents
[1],
196 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
199 static int exclude_super_stripes(struct btrfs_root
*root
,
200 struct btrfs_block_group_cache
*cache
)
207 if (cache
->key
.objectid
< BTRFS_SUPER_INFO_OFFSET
) {
208 stripe_len
= BTRFS_SUPER_INFO_OFFSET
- cache
->key
.objectid
;
209 cache
->bytes_super
+= stripe_len
;
210 ret
= add_excluded_extent(root
, cache
->key
.objectid
,
215 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
216 bytenr
= btrfs_sb_offset(i
);
217 ret
= btrfs_rmap_block(&root
->fs_info
->mapping_tree
,
218 cache
->key
.objectid
, bytenr
,
219 0, &logical
, &nr
, &stripe_len
);
223 cache
->bytes_super
+= stripe_len
;
224 ret
= add_excluded_extent(root
, logical
[nr
],
234 static struct btrfs_caching_control
*
235 get_caching_control(struct btrfs_block_group_cache
*cache
)
237 struct btrfs_caching_control
*ctl
;
239 spin_lock(&cache
->lock
);
240 if (cache
->cached
!= BTRFS_CACHE_STARTED
) {
241 spin_unlock(&cache
->lock
);
245 /* We're loading it the fast way, so we don't have a caching_ctl. */
246 if (!cache
->caching_ctl
) {
247 spin_unlock(&cache
->lock
);
251 ctl
= cache
->caching_ctl
;
252 atomic_inc(&ctl
->count
);
253 spin_unlock(&cache
->lock
);
257 static void put_caching_control(struct btrfs_caching_control
*ctl
)
259 if (atomic_dec_and_test(&ctl
->count
))
264 * this is only called by cache_block_group, since we could have freed extents
265 * we need to check the pinned_extents for any extents that can't be used yet
266 * since their free space will be released as soon as the transaction commits.
268 static u64
add_new_free_space(struct btrfs_block_group_cache
*block_group
,
269 struct btrfs_fs_info
*info
, u64 start
, u64 end
)
271 u64 extent_start
, extent_end
, size
, total_added
= 0;
274 while (start
< end
) {
275 ret
= find_first_extent_bit(info
->pinned_extents
, start
,
276 &extent_start
, &extent_end
,
277 EXTENT_DIRTY
| EXTENT_UPTODATE
);
281 if (extent_start
<= start
) {
282 start
= extent_end
+ 1;
283 } else if (extent_start
> start
&& extent_start
< end
) {
284 size
= extent_start
- start
;
286 ret
= btrfs_add_free_space(block_group
, start
,
289 start
= extent_end
+ 1;
298 ret
= btrfs_add_free_space(block_group
, start
, size
);
305 static int caching_kthread(void *data
)
307 struct btrfs_block_group_cache
*block_group
= data
;
308 struct btrfs_fs_info
*fs_info
= block_group
->fs_info
;
309 struct btrfs_caching_control
*caching_ctl
= block_group
->caching_ctl
;
310 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
311 struct btrfs_path
*path
;
312 struct extent_buffer
*leaf
;
313 struct btrfs_key key
;
319 path
= btrfs_alloc_path();
323 exclude_super_stripes(extent_root
, block_group
);
324 spin_lock(&block_group
->space_info
->lock
);
325 block_group
->space_info
->bytes_readonly
+= block_group
->bytes_super
;
326 spin_unlock(&block_group
->space_info
->lock
);
328 last
= max_t(u64
, block_group
->key
.objectid
, BTRFS_SUPER_INFO_OFFSET
);
331 * We don't want to deadlock with somebody trying to allocate a new
332 * extent for the extent root while also trying to search the extent
333 * root to add free space. So we skip locking and search the commit
334 * root, since its read-only
336 path
->skip_locking
= 1;
337 path
->search_commit_root
= 1;
342 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
344 mutex_lock(&caching_ctl
->mutex
);
345 /* need to make sure the commit_root doesn't disappear */
346 down_read(&fs_info
->extent_commit_sem
);
348 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
352 leaf
= path
->nodes
[0];
353 nritems
= btrfs_header_nritems(leaf
);
357 if (fs_info
->closing
> 1) {
362 if (path
->slots
[0] < nritems
) {
363 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
365 ret
= find_next_key(path
, 0, &key
);
369 caching_ctl
->progress
= last
;
370 btrfs_release_path(extent_root
, path
);
371 up_read(&fs_info
->extent_commit_sem
);
372 mutex_unlock(&caching_ctl
->mutex
);
373 if (btrfs_transaction_in_commit(fs_info
))
380 if (key
.objectid
< block_group
->key
.objectid
) {
385 if (key
.objectid
>= block_group
->key
.objectid
+
386 block_group
->key
.offset
)
389 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
390 total_found
+= add_new_free_space(block_group
,
393 last
= key
.objectid
+ key
.offset
;
395 if (total_found
> (1024 * 1024 * 2)) {
397 wake_up(&caching_ctl
->wait
);
404 total_found
+= add_new_free_space(block_group
, fs_info
, last
,
405 block_group
->key
.objectid
+
406 block_group
->key
.offset
);
407 caching_ctl
->progress
= (u64
)-1;
409 spin_lock(&block_group
->lock
);
410 block_group
->caching_ctl
= NULL
;
411 block_group
->cached
= BTRFS_CACHE_FINISHED
;
412 spin_unlock(&block_group
->lock
);
415 btrfs_free_path(path
);
416 up_read(&fs_info
->extent_commit_sem
);
418 free_excluded_extents(extent_root
, block_group
);
420 mutex_unlock(&caching_ctl
->mutex
);
421 wake_up(&caching_ctl
->wait
);
423 put_caching_control(caching_ctl
);
424 atomic_dec(&block_group
->space_info
->caching_threads
);
425 btrfs_put_block_group(block_group
);
430 static int cache_block_group(struct btrfs_block_group_cache
*cache
,
431 struct btrfs_trans_handle
*trans
,
432 struct btrfs_root
*root
,
435 struct btrfs_fs_info
*fs_info
= cache
->fs_info
;
436 struct btrfs_caching_control
*caching_ctl
;
437 struct task_struct
*tsk
;
441 if (cache
->cached
!= BTRFS_CACHE_NO
)
445 * We can't do the read from on-disk cache during a commit since we need
446 * to have the normal tree locking. Also if we are currently trying to
447 * allocate blocks for the tree root we can't do the fast caching since
448 * we likely hold important locks.
450 if (!trans
->transaction
->in_commit
&&
451 (root
&& root
!= root
->fs_info
->tree_root
)) {
452 spin_lock(&cache
->lock
);
453 if (cache
->cached
!= BTRFS_CACHE_NO
) {
454 spin_unlock(&cache
->lock
);
457 cache
->cached
= BTRFS_CACHE_STARTED
;
458 spin_unlock(&cache
->lock
);
460 ret
= load_free_space_cache(fs_info
, cache
);
462 spin_lock(&cache
->lock
);
464 cache
->cached
= BTRFS_CACHE_FINISHED
;
465 cache
->last_byte_to_unpin
= (u64
)-1;
467 cache
->cached
= BTRFS_CACHE_NO
;
469 spin_unlock(&cache
->lock
);
477 caching_ctl
= kzalloc(sizeof(*caching_ctl
), GFP_KERNEL
);
478 BUG_ON(!caching_ctl
);
480 INIT_LIST_HEAD(&caching_ctl
->list
);
481 mutex_init(&caching_ctl
->mutex
);
482 init_waitqueue_head(&caching_ctl
->wait
);
483 caching_ctl
->block_group
= cache
;
484 caching_ctl
->progress
= cache
->key
.objectid
;
485 /* one for caching kthread, one for caching block group list */
486 atomic_set(&caching_ctl
->count
, 2);
488 spin_lock(&cache
->lock
);
489 if (cache
->cached
!= BTRFS_CACHE_NO
) {
490 spin_unlock(&cache
->lock
);
494 cache
->caching_ctl
= caching_ctl
;
495 cache
->cached
= BTRFS_CACHE_STARTED
;
496 spin_unlock(&cache
->lock
);
498 down_write(&fs_info
->extent_commit_sem
);
499 list_add_tail(&caching_ctl
->list
, &fs_info
->caching_block_groups
);
500 up_write(&fs_info
->extent_commit_sem
);
502 atomic_inc(&cache
->space_info
->caching_threads
);
503 btrfs_get_block_group(cache
);
505 tsk
= kthread_run(caching_kthread
, cache
, "btrfs-cache-%llu\n",
506 cache
->key
.objectid
);
509 printk(KERN_ERR
"error running thread %d\n", ret
);
517 * return the block group that starts at or after bytenr
519 static struct btrfs_block_group_cache
*
520 btrfs_lookup_first_block_group(struct btrfs_fs_info
*info
, u64 bytenr
)
522 struct btrfs_block_group_cache
*cache
;
524 cache
= block_group_cache_tree_search(info
, bytenr
, 0);
530 * return the block group that contains the given bytenr
532 struct btrfs_block_group_cache
*btrfs_lookup_block_group(
533 struct btrfs_fs_info
*info
,
536 struct btrfs_block_group_cache
*cache
;
538 cache
= block_group_cache_tree_search(info
, bytenr
, 1);
543 static struct btrfs_space_info
*__find_space_info(struct btrfs_fs_info
*info
,
546 struct list_head
*head
= &info
->space_info
;
547 struct btrfs_space_info
*found
;
549 flags
&= BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_SYSTEM
|
550 BTRFS_BLOCK_GROUP_METADATA
;
553 list_for_each_entry_rcu(found
, head
, list
) {
554 if (found
->flags
& flags
) {
564 * after adding space to the filesystem, we need to clear the full flags
565 * on all the space infos.
567 void btrfs_clear_space_info_full(struct btrfs_fs_info
*info
)
569 struct list_head
*head
= &info
->space_info
;
570 struct btrfs_space_info
*found
;
573 list_for_each_entry_rcu(found
, head
, list
)
578 static u64
div_factor(u64 num
, int factor
)
587 static u64
div_factor_fine(u64 num
, int factor
)
596 u64
btrfs_find_block_group(struct btrfs_root
*root
,
597 u64 search_start
, u64 search_hint
, int owner
)
599 struct btrfs_block_group_cache
*cache
;
601 u64 last
= max(search_hint
, search_start
);
608 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
612 spin_lock(&cache
->lock
);
613 last
= cache
->key
.objectid
+ cache
->key
.offset
;
614 used
= btrfs_block_group_used(&cache
->item
);
616 if ((full_search
|| !cache
->ro
) &&
617 block_group_bits(cache
, BTRFS_BLOCK_GROUP_METADATA
)) {
618 if (used
+ cache
->pinned
+ cache
->reserved
<
619 div_factor(cache
->key
.offset
, factor
)) {
620 group_start
= cache
->key
.objectid
;
621 spin_unlock(&cache
->lock
);
622 btrfs_put_block_group(cache
);
626 spin_unlock(&cache
->lock
);
627 btrfs_put_block_group(cache
);
635 if (!full_search
&& factor
< 10) {
645 /* simple helper to search for an existing extent at a given offset */
646 int btrfs_lookup_extent(struct btrfs_root
*root
, u64 start
, u64 len
)
649 struct btrfs_key key
;
650 struct btrfs_path
*path
;
652 path
= btrfs_alloc_path();
654 key
.objectid
= start
;
656 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
657 ret
= btrfs_search_slot(NULL
, root
->fs_info
->extent_root
, &key
, path
,
659 btrfs_free_path(path
);
664 * helper function to lookup reference count and flags of extent.
666 * the head node for delayed ref is used to store the sum of all the
667 * reference count modifications queued up in the rbtree. the head
668 * node may also store the extent flags to set. This way you can check
669 * to see what the reference count and extent flags would be if all of
670 * the delayed refs are not processed.
672 int btrfs_lookup_extent_info(struct btrfs_trans_handle
*trans
,
673 struct btrfs_root
*root
, u64 bytenr
,
674 u64 num_bytes
, u64
*refs
, u64
*flags
)
676 struct btrfs_delayed_ref_head
*head
;
677 struct btrfs_delayed_ref_root
*delayed_refs
;
678 struct btrfs_path
*path
;
679 struct btrfs_extent_item
*ei
;
680 struct extent_buffer
*leaf
;
681 struct btrfs_key key
;
687 path
= btrfs_alloc_path();
691 key
.objectid
= bytenr
;
692 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
693 key
.offset
= num_bytes
;
695 path
->skip_locking
= 1;
696 path
->search_commit_root
= 1;
699 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
,
705 leaf
= path
->nodes
[0];
706 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
707 if (item_size
>= sizeof(*ei
)) {
708 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
709 struct btrfs_extent_item
);
710 num_refs
= btrfs_extent_refs(leaf
, ei
);
711 extent_flags
= btrfs_extent_flags(leaf
, ei
);
713 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
714 struct btrfs_extent_item_v0
*ei0
;
715 BUG_ON(item_size
!= sizeof(*ei0
));
716 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
717 struct btrfs_extent_item_v0
);
718 num_refs
= btrfs_extent_refs_v0(leaf
, ei0
);
719 /* FIXME: this isn't correct for data */
720 extent_flags
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
725 BUG_ON(num_refs
== 0);
735 delayed_refs
= &trans
->transaction
->delayed_refs
;
736 spin_lock(&delayed_refs
->lock
);
737 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
739 if (!mutex_trylock(&head
->mutex
)) {
740 atomic_inc(&head
->node
.refs
);
741 spin_unlock(&delayed_refs
->lock
);
743 btrfs_release_path(root
->fs_info
->extent_root
, path
);
745 mutex_lock(&head
->mutex
);
746 mutex_unlock(&head
->mutex
);
747 btrfs_put_delayed_ref(&head
->node
);
750 if (head
->extent_op
&& head
->extent_op
->update_flags
)
751 extent_flags
|= head
->extent_op
->flags_to_set
;
753 BUG_ON(num_refs
== 0);
755 num_refs
+= head
->node
.ref_mod
;
756 mutex_unlock(&head
->mutex
);
758 spin_unlock(&delayed_refs
->lock
);
760 WARN_ON(num_refs
== 0);
764 *flags
= extent_flags
;
766 btrfs_free_path(path
);
771 * Back reference rules. Back refs have three main goals:
773 * 1) differentiate between all holders of references to an extent so that
774 * when a reference is dropped we can make sure it was a valid reference
775 * before freeing the extent.
777 * 2) Provide enough information to quickly find the holders of an extent
778 * if we notice a given block is corrupted or bad.
780 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
781 * maintenance. This is actually the same as #2, but with a slightly
782 * different use case.
784 * There are two kinds of back refs. The implicit back refs is optimized
785 * for pointers in non-shared tree blocks. For a given pointer in a block,
786 * back refs of this kind provide information about the block's owner tree
787 * and the pointer's key. These information allow us to find the block by
788 * b-tree searching. The full back refs is for pointers in tree blocks not
789 * referenced by their owner trees. The location of tree block is recorded
790 * in the back refs. Actually the full back refs is generic, and can be
791 * used in all cases the implicit back refs is used. The major shortcoming
792 * of the full back refs is its overhead. Every time a tree block gets
793 * COWed, we have to update back refs entry for all pointers in it.
795 * For a newly allocated tree block, we use implicit back refs for
796 * pointers in it. This means most tree related operations only involve
797 * implicit back refs. For a tree block created in old transaction, the
798 * only way to drop a reference to it is COW it. So we can detect the
799 * event that tree block loses its owner tree's reference and do the
800 * back refs conversion.
802 * When a tree block is COW'd through a tree, there are four cases:
804 * The reference count of the block is one and the tree is the block's
805 * owner tree. Nothing to do in this case.
807 * The reference count of the block is one and the tree is not the
808 * block's owner tree. In this case, full back refs is used for pointers
809 * in the block. Remove these full back refs, add implicit back refs for
810 * every pointers in the new block.
812 * The reference count of the block is greater than one and the tree is
813 * the block's owner tree. In this case, implicit back refs is used for
814 * pointers in the block. Add full back refs for every pointers in the
815 * block, increase lower level extents' reference counts. The original
816 * implicit back refs are entailed to the new block.
818 * The reference count of the block is greater than one and the tree is
819 * not the block's owner tree. Add implicit back refs for every pointer in
820 * the new block, increase lower level extents' reference count.
822 * Back Reference Key composing:
824 * The key objectid corresponds to the first byte in the extent,
825 * The key type is used to differentiate between types of back refs.
826 * There are different meanings of the key offset for different types
829 * File extents can be referenced by:
831 * - multiple snapshots, subvolumes, or different generations in one subvol
832 * - different files inside a single subvolume
833 * - different offsets inside a file (bookend extents in file.c)
835 * The extent ref structure for the implicit back refs has fields for:
837 * - Objectid of the subvolume root
838 * - objectid of the file holding the reference
839 * - original offset in the file
840 * - how many bookend extents
842 * The key offset for the implicit back refs is hash of the first
845 * The extent ref structure for the full back refs has field for:
847 * - number of pointers in the tree leaf
849 * The key offset for the implicit back refs is the first byte of
852 * When a file extent is allocated, The implicit back refs is used.
853 * the fields are filled in:
855 * (root_key.objectid, inode objectid, offset in file, 1)
857 * When a file extent is removed file truncation, we find the
858 * corresponding implicit back refs and check the following fields:
860 * (btrfs_header_owner(leaf), inode objectid, offset in file)
862 * Btree extents can be referenced by:
864 * - Different subvolumes
866 * Both the implicit back refs and the full back refs for tree blocks
867 * only consist of key. The key offset for the implicit back refs is
868 * objectid of block's owner tree. The key offset for the full back refs
869 * is the first byte of parent block.
871 * When implicit back refs is used, information about the lowest key and
872 * level of the tree block are required. These information are stored in
873 * tree block info structure.
876 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
877 static int convert_extent_item_v0(struct btrfs_trans_handle
*trans
,
878 struct btrfs_root
*root
,
879 struct btrfs_path
*path
,
880 u64 owner
, u32 extra_size
)
882 struct btrfs_extent_item
*item
;
883 struct btrfs_extent_item_v0
*ei0
;
884 struct btrfs_extent_ref_v0
*ref0
;
885 struct btrfs_tree_block_info
*bi
;
886 struct extent_buffer
*leaf
;
887 struct btrfs_key key
;
888 struct btrfs_key found_key
;
889 u32 new_size
= sizeof(*item
);
893 leaf
= path
->nodes
[0];
894 BUG_ON(btrfs_item_size_nr(leaf
, path
->slots
[0]) != sizeof(*ei0
));
896 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
897 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
898 struct btrfs_extent_item_v0
);
899 refs
= btrfs_extent_refs_v0(leaf
, ei0
);
901 if (owner
== (u64
)-1) {
903 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
904 ret
= btrfs_next_leaf(root
, path
);
908 leaf
= path
->nodes
[0];
910 btrfs_item_key_to_cpu(leaf
, &found_key
,
912 BUG_ON(key
.objectid
!= found_key
.objectid
);
913 if (found_key
.type
!= BTRFS_EXTENT_REF_V0_KEY
) {
917 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
918 struct btrfs_extent_ref_v0
);
919 owner
= btrfs_ref_objectid_v0(leaf
, ref0
);
923 btrfs_release_path(root
, path
);
925 if (owner
< BTRFS_FIRST_FREE_OBJECTID
)
926 new_size
+= sizeof(*bi
);
928 new_size
-= sizeof(*ei0
);
929 ret
= btrfs_search_slot(trans
, root
, &key
, path
,
930 new_size
+ extra_size
, 1);
935 ret
= btrfs_extend_item(trans
, root
, path
, new_size
);
938 leaf
= path
->nodes
[0];
939 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
940 btrfs_set_extent_refs(leaf
, item
, refs
);
941 /* FIXME: get real generation */
942 btrfs_set_extent_generation(leaf
, item
, 0);
943 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
944 btrfs_set_extent_flags(leaf
, item
,
945 BTRFS_EXTENT_FLAG_TREE_BLOCK
|
946 BTRFS_BLOCK_FLAG_FULL_BACKREF
);
947 bi
= (struct btrfs_tree_block_info
*)(item
+ 1);
948 /* FIXME: get first key of the block */
949 memset_extent_buffer(leaf
, 0, (unsigned long)bi
, sizeof(*bi
));
950 btrfs_set_tree_block_level(leaf
, bi
, (int)owner
);
952 btrfs_set_extent_flags(leaf
, item
, BTRFS_EXTENT_FLAG_DATA
);
954 btrfs_mark_buffer_dirty(leaf
);
959 static u64
hash_extent_data_ref(u64 root_objectid
, u64 owner
, u64 offset
)
961 u32 high_crc
= ~(u32
)0;
962 u32 low_crc
= ~(u32
)0;
965 lenum
= cpu_to_le64(root_objectid
);
966 high_crc
= crc32c(high_crc
, &lenum
, sizeof(lenum
));
967 lenum
= cpu_to_le64(owner
);
968 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
969 lenum
= cpu_to_le64(offset
);
970 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
972 return ((u64
)high_crc
<< 31) ^ (u64
)low_crc
;
975 static u64
hash_extent_data_ref_item(struct extent_buffer
*leaf
,
976 struct btrfs_extent_data_ref
*ref
)
978 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf
, ref
),
979 btrfs_extent_data_ref_objectid(leaf
, ref
),
980 btrfs_extent_data_ref_offset(leaf
, ref
));
983 static int match_extent_data_ref(struct extent_buffer
*leaf
,
984 struct btrfs_extent_data_ref
*ref
,
985 u64 root_objectid
, u64 owner
, u64 offset
)
987 if (btrfs_extent_data_ref_root(leaf
, ref
) != root_objectid
||
988 btrfs_extent_data_ref_objectid(leaf
, ref
) != owner
||
989 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
994 static noinline
int lookup_extent_data_ref(struct btrfs_trans_handle
*trans
,
995 struct btrfs_root
*root
,
996 struct btrfs_path
*path
,
997 u64 bytenr
, u64 parent
,
999 u64 owner
, u64 offset
)
1001 struct btrfs_key key
;
1002 struct btrfs_extent_data_ref
*ref
;
1003 struct extent_buffer
*leaf
;
1009 key
.objectid
= bytenr
;
1011 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
1012 key
.offset
= parent
;
1014 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
1015 key
.offset
= hash_extent_data_ref(root_objectid
,
1020 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1029 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1030 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
1031 btrfs_release_path(root
, path
);
1032 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1043 leaf
= path
->nodes
[0];
1044 nritems
= btrfs_header_nritems(leaf
);
1046 if (path
->slots
[0] >= nritems
) {
1047 ret
= btrfs_next_leaf(root
, path
);
1053 leaf
= path
->nodes
[0];
1054 nritems
= btrfs_header_nritems(leaf
);
1058 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1059 if (key
.objectid
!= bytenr
||
1060 key
.type
!= BTRFS_EXTENT_DATA_REF_KEY
)
1063 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1064 struct btrfs_extent_data_ref
);
1066 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
1069 btrfs_release_path(root
, path
);
1081 static noinline
int insert_extent_data_ref(struct btrfs_trans_handle
*trans
,
1082 struct btrfs_root
*root
,
1083 struct btrfs_path
*path
,
1084 u64 bytenr
, u64 parent
,
1085 u64 root_objectid
, u64 owner
,
1086 u64 offset
, int refs_to_add
)
1088 struct btrfs_key key
;
1089 struct extent_buffer
*leaf
;
1094 key
.objectid
= bytenr
;
1096 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
1097 key
.offset
= parent
;
1098 size
= sizeof(struct btrfs_shared_data_ref
);
1100 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
1101 key
.offset
= hash_extent_data_ref(root_objectid
,
1103 size
= sizeof(struct btrfs_extent_data_ref
);
1106 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, size
);
1107 if (ret
&& ret
!= -EEXIST
)
1110 leaf
= path
->nodes
[0];
1112 struct btrfs_shared_data_ref
*ref
;
1113 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1114 struct btrfs_shared_data_ref
);
1116 btrfs_set_shared_data_ref_count(leaf
, ref
, refs_to_add
);
1118 num_refs
= btrfs_shared_data_ref_count(leaf
, ref
);
1119 num_refs
+= refs_to_add
;
1120 btrfs_set_shared_data_ref_count(leaf
, ref
, num_refs
);
1123 struct btrfs_extent_data_ref
*ref
;
1124 while (ret
== -EEXIST
) {
1125 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1126 struct btrfs_extent_data_ref
);
1127 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
1130 btrfs_release_path(root
, path
);
1132 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1134 if (ret
&& ret
!= -EEXIST
)
1137 leaf
= path
->nodes
[0];
1139 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1140 struct btrfs_extent_data_ref
);
1142 btrfs_set_extent_data_ref_root(leaf
, ref
,
1144 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
1145 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
1146 btrfs_set_extent_data_ref_count(leaf
, ref
, refs_to_add
);
1148 num_refs
= btrfs_extent_data_ref_count(leaf
, ref
);
1149 num_refs
+= refs_to_add
;
1150 btrfs_set_extent_data_ref_count(leaf
, ref
, num_refs
);
1153 btrfs_mark_buffer_dirty(leaf
);
1156 btrfs_release_path(root
, path
);
1160 static noinline
int remove_extent_data_ref(struct btrfs_trans_handle
*trans
,
1161 struct btrfs_root
*root
,
1162 struct btrfs_path
*path
,
1165 struct btrfs_key key
;
1166 struct btrfs_extent_data_ref
*ref1
= NULL
;
1167 struct btrfs_shared_data_ref
*ref2
= NULL
;
1168 struct extent_buffer
*leaf
;
1172 leaf
= path
->nodes
[0];
1173 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1175 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1176 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
1177 struct btrfs_extent_data_ref
);
1178 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1179 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
1180 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
1181 struct btrfs_shared_data_ref
);
1182 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1183 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1184 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
1185 struct btrfs_extent_ref_v0
*ref0
;
1186 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1187 struct btrfs_extent_ref_v0
);
1188 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
1194 BUG_ON(num_refs
< refs_to_drop
);
1195 num_refs
-= refs_to_drop
;
1197 if (num_refs
== 0) {
1198 ret
= btrfs_del_item(trans
, root
, path
);
1200 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
)
1201 btrfs_set_extent_data_ref_count(leaf
, ref1
, num_refs
);
1202 else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
)
1203 btrfs_set_shared_data_ref_count(leaf
, ref2
, num_refs
);
1204 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1206 struct btrfs_extent_ref_v0
*ref0
;
1207 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1208 struct btrfs_extent_ref_v0
);
1209 btrfs_set_ref_count_v0(leaf
, ref0
, num_refs
);
1212 btrfs_mark_buffer_dirty(leaf
);
1217 static noinline u32
extent_data_ref_count(struct btrfs_root
*root
,
1218 struct btrfs_path
*path
,
1219 struct btrfs_extent_inline_ref
*iref
)
1221 struct btrfs_key key
;
1222 struct extent_buffer
*leaf
;
1223 struct btrfs_extent_data_ref
*ref1
;
1224 struct btrfs_shared_data_ref
*ref2
;
1227 leaf
= path
->nodes
[0];
1228 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1230 if (btrfs_extent_inline_ref_type(leaf
, iref
) ==
1231 BTRFS_EXTENT_DATA_REF_KEY
) {
1232 ref1
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1233 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1235 ref2
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1236 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1238 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1239 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
1240 struct btrfs_extent_data_ref
);
1241 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1242 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
1243 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
1244 struct btrfs_shared_data_ref
);
1245 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1246 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1247 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
1248 struct btrfs_extent_ref_v0
*ref0
;
1249 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1250 struct btrfs_extent_ref_v0
);
1251 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
1259 static noinline
int lookup_tree_block_ref(struct btrfs_trans_handle
*trans
,
1260 struct btrfs_root
*root
,
1261 struct btrfs_path
*path
,
1262 u64 bytenr
, u64 parent
,
1265 struct btrfs_key key
;
1268 key
.objectid
= bytenr
;
1270 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1271 key
.offset
= parent
;
1273 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
1274 key
.offset
= root_objectid
;
1277 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1280 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1281 if (ret
== -ENOENT
&& parent
) {
1282 btrfs_release_path(root
, path
);
1283 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
1284 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1292 static noinline
int insert_tree_block_ref(struct btrfs_trans_handle
*trans
,
1293 struct btrfs_root
*root
,
1294 struct btrfs_path
*path
,
1295 u64 bytenr
, u64 parent
,
1298 struct btrfs_key key
;
1301 key
.objectid
= bytenr
;
1303 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1304 key
.offset
= parent
;
1306 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
1307 key
.offset
= root_objectid
;
1310 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, 0);
1311 btrfs_release_path(root
, path
);
1315 static inline int extent_ref_type(u64 parent
, u64 owner
)
1318 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1320 type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1322 type
= BTRFS_TREE_BLOCK_REF_KEY
;
1325 type
= BTRFS_SHARED_DATA_REF_KEY
;
1327 type
= BTRFS_EXTENT_DATA_REF_KEY
;
1332 static int find_next_key(struct btrfs_path
*path
, int level
,
1333 struct btrfs_key
*key
)
1336 for (; level
< BTRFS_MAX_LEVEL
; level
++) {
1337 if (!path
->nodes
[level
])
1339 if (path
->slots
[level
] + 1 >=
1340 btrfs_header_nritems(path
->nodes
[level
]))
1343 btrfs_item_key_to_cpu(path
->nodes
[level
], key
,
1344 path
->slots
[level
] + 1);
1346 btrfs_node_key_to_cpu(path
->nodes
[level
], key
,
1347 path
->slots
[level
] + 1);
1354 * look for inline back ref. if back ref is found, *ref_ret is set
1355 * to the address of inline back ref, and 0 is returned.
1357 * if back ref isn't found, *ref_ret is set to the address where it
1358 * should be inserted, and -ENOENT is returned.
1360 * if insert is true and there are too many inline back refs, the path
1361 * points to the extent item, and -EAGAIN is returned.
1363 * NOTE: inline back refs are ordered in the same way that back ref
1364 * items in the tree are ordered.
1366 static noinline_for_stack
1367 int lookup_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1368 struct btrfs_root
*root
,
1369 struct btrfs_path
*path
,
1370 struct btrfs_extent_inline_ref
**ref_ret
,
1371 u64 bytenr
, u64 num_bytes
,
1372 u64 parent
, u64 root_objectid
,
1373 u64 owner
, u64 offset
, int insert
)
1375 struct btrfs_key key
;
1376 struct extent_buffer
*leaf
;
1377 struct btrfs_extent_item
*ei
;
1378 struct btrfs_extent_inline_ref
*iref
;
1389 key
.objectid
= bytenr
;
1390 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1391 key
.offset
= num_bytes
;
1393 want
= extent_ref_type(parent
, owner
);
1395 extra_size
= btrfs_extent_inline_ref_size(want
);
1396 path
->keep_locks
= 1;
1399 ret
= btrfs_search_slot(trans
, root
, &key
, path
, extra_size
, 1);
1406 leaf
= path
->nodes
[0];
1407 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1408 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1409 if (item_size
< sizeof(*ei
)) {
1414 ret
= convert_extent_item_v0(trans
, root
, path
, owner
,
1420 leaf
= path
->nodes
[0];
1421 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1424 BUG_ON(item_size
< sizeof(*ei
));
1426 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1427 flags
= btrfs_extent_flags(leaf
, ei
);
1429 ptr
= (unsigned long)(ei
+ 1);
1430 end
= (unsigned long)ei
+ item_size
;
1432 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
1433 ptr
+= sizeof(struct btrfs_tree_block_info
);
1436 BUG_ON(!(flags
& BTRFS_EXTENT_FLAG_DATA
));
1445 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1446 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1450 ptr
+= btrfs_extent_inline_ref_size(type
);
1454 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1455 struct btrfs_extent_data_ref
*dref
;
1456 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1457 if (match_extent_data_ref(leaf
, dref
, root_objectid
,
1462 if (hash_extent_data_ref_item(leaf
, dref
) <
1463 hash_extent_data_ref(root_objectid
, owner
, offset
))
1467 ref_offset
= btrfs_extent_inline_ref_offset(leaf
, iref
);
1469 if (parent
== ref_offset
) {
1473 if (ref_offset
< parent
)
1476 if (root_objectid
== ref_offset
) {
1480 if (ref_offset
< root_objectid
)
1484 ptr
+= btrfs_extent_inline_ref_size(type
);
1486 if (err
== -ENOENT
&& insert
) {
1487 if (item_size
+ extra_size
>=
1488 BTRFS_MAX_EXTENT_ITEM_SIZE(root
)) {
1493 * To add new inline back ref, we have to make sure
1494 * there is no corresponding back ref item.
1495 * For simplicity, we just do not add new inline back
1496 * ref if there is any kind of item for this block
1498 if (find_next_key(path
, 0, &key
) == 0 &&
1499 key
.objectid
== bytenr
&&
1500 key
.type
< BTRFS_BLOCK_GROUP_ITEM_KEY
) {
1505 *ref_ret
= (struct btrfs_extent_inline_ref
*)ptr
;
1508 path
->keep_locks
= 0;
1509 btrfs_unlock_up_safe(path
, 1);
1515 * helper to add new inline back ref
1517 static noinline_for_stack
1518 int setup_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1519 struct btrfs_root
*root
,
1520 struct btrfs_path
*path
,
1521 struct btrfs_extent_inline_ref
*iref
,
1522 u64 parent
, u64 root_objectid
,
1523 u64 owner
, u64 offset
, int refs_to_add
,
1524 struct btrfs_delayed_extent_op
*extent_op
)
1526 struct extent_buffer
*leaf
;
1527 struct btrfs_extent_item
*ei
;
1530 unsigned long item_offset
;
1536 leaf
= path
->nodes
[0];
1537 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1538 item_offset
= (unsigned long)iref
- (unsigned long)ei
;
1540 type
= extent_ref_type(parent
, owner
);
1541 size
= btrfs_extent_inline_ref_size(type
);
1543 ret
= btrfs_extend_item(trans
, root
, path
, size
);
1546 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1547 refs
= btrfs_extent_refs(leaf
, ei
);
1548 refs
+= refs_to_add
;
1549 btrfs_set_extent_refs(leaf
, ei
, refs
);
1551 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1553 ptr
= (unsigned long)ei
+ item_offset
;
1554 end
= (unsigned long)ei
+ btrfs_item_size_nr(leaf
, path
->slots
[0]);
1555 if (ptr
< end
- size
)
1556 memmove_extent_buffer(leaf
, ptr
+ size
, ptr
,
1559 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1560 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
1561 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1562 struct btrfs_extent_data_ref
*dref
;
1563 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1564 btrfs_set_extent_data_ref_root(leaf
, dref
, root_objectid
);
1565 btrfs_set_extent_data_ref_objectid(leaf
, dref
, owner
);
1566 btrfs_set_extent_data_ref_offset(leaf
, dref
, offset
);
1567 btrfs_set_extent_data_ref_count(leaf
, dref
, refs_to_add
);
1568 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1569 struct btrfs_shared_data_ref
*sref
;
1570 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1571 btrfs_set_shared_data_ref_count(leaf
, sref
, refs_to_add
);
1572 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1573 } else if (type
== BTRFS_SHARED_BLOCK_REF_KEY
) {
1574 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1576 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
1578 btrfs_mark_buffer_dirty(leaf
);
1582 static int lookup_extent_backref(struct btrfs_trans_handle
*trans
,
1583 struct btrfs_root
*root
,
1584 struct btrfs_path
*path
,
1585 struct btrfs_extent_inline_ref
**ref_ret
,
1586 u64 bytenr
, u64 num_bytes
, u64 parent
,
1587 u64 root_objectid
, u64 owner
, u64 offset
)
1591 ret
= lookup_inline_extent_backref(trans
, root
, path
, ref_ret
,
1592 bytenr
, num_bytes
, parent
,
1593 root_objectid
, owner
, offset
, 0);
1597 btrfs_release_path(root
, path
);
1600 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1601 ret
= lookup_tree_block_ref(trans
, root
, path
, bytenr
, parent
,
1604 ret
= lookup_extent_data_ref(trans
, root
, path
, bytenr
, parent
,
1605 root_objectid
, owner
, offset
);
1611 * helper to update/remove inline back ref
1613 static noinline_for_stack
1614 int update_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1615 struct btrfs_root
*root
,
1616 struct btrfs_path
*path
,
1617 struct btrfs_extent_inline_ref
*iref
,
1619 struct btrfs_delayed_extent_op
*extent_op
)
1621 struct extent_buffer
*leaf
;
1622 struct btrfs_extent_item
*ei
;
1623 struct btrfs_extent_data_ref
*dref
= NULL
;
1624 struct btrfs_shared_data_ref
*sref
= NULL
;
1633 leaf
= path
->nodes
[0];
1634 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1635 refs
= btrfs_extent_refs(leaf
, ei
);
1636 WARN_ON(refs_to_mod
< 0 && refs
+ refs_to_mod
<= 0);
1637 refs
+= refs_to_mod
;
1638 btrfs_set_extent_refs(leaf
, ei
, refs
);
1640 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1642 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1644 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1645 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1646 refs
= btrfs_extent_data_ref_count(leaf
, dref
);
1647 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1648 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1649 refs
= btrfs_shared_data_ref_count(leaf
, sref
);
1652 BUG_ON(refs_to_mod
!= -1);
1655 BUG_ON(refs_to_mod
< 0 && refs
< -refs_to_mod
);
1656 refs
+= refs_to_mod
;
1659 if (type
== BTRFS_EXTENT_DATA_REF_KEY
)
1660 btrfs_set_extent_data_ref_count(leaf
, dref
, refs
);
1662 btrfs_set_shared_data_ref_count(leaf
, sref
, refs
);
1664 size
= btrfs_extent_inline_ref_size(type
);
1665 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1666 ptr
= (unsigned long)iref
;
1667 end
= (unsigned long)ei
+ item_size
;
1668 if (ptr
+ size
< end
)
1669 memmove_extent_buffer(leaf
, ptr
, ptr
+ size
,
1672 ret
= btrfs_truncate_item(trans
, root
, path
, item_size
, 1);
1675 btrfs_mark_buffer_dirty(leaf
);
1679 static noinline_for_stack
1680 int insert_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1681 struct btrfs_root
*root
,
1682 struct btrfs_path
*path
,
1683 u64 bytenr
, u64 num_bytes
, u64 parent
,
1684 u64 root_objectid
, u64 owner
,
1685 u64 offset
, int refs_to_add
,
1686 struct btrfs_delayed_extent_op
*extent_op
)
1688 struct btrfs_extent_inline_ref
*iref
;
1691 ret
= lookup_inline_extent_backref(trans
, root
, path
, &iref
,
1692 bytenr
, num_bytes
, parent
,
1693 root_objectid
, owner
, offset
, 1);
1695 BUG_ON(owner
< BTRFS_FIRST_FREE_OBJECTID
);
1696 ret
= update_inline_extent_backref(trans
, root
, path
, iref
,
1697 refs_to_add
, extent_op
);
1698 } else if (ret
== -ENOENT
) {
1699 ret
= setup_inline_extent_backref(trans
, root
, path
, iref
,
1700 parent
, root_objectid
,
1701 owner
, offset
, refs_to_add
,
1707 static int insert_extent_backref(struct btrfs_trans_handle
*trans
,
1708 struct btrfs_root
*root
,
1709 struct btrfs_path
*path
,
1710 u64 bytenr
, u64 parent
, u64 root_objectid
,
1711 u64 owner
, u64 offset
, int refs_to_add
)
1714 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1715 BUG_ON(refs_to_add
!= 1);
1716 ret
= insert_tree_block_ref(trans
, root
, path
, bytenr
,
1717 parent
, root_objectid
);
1719 ret
= insert_extent_data_ref(trans
, root
, path
, bytenr
,
1720 parent
, root_objectid
,
1721 owner
, offset
, refs_to_add
);
1726 static int remove_extent_backref(struct btrfs_trans_handle
*trans
,
1727 struct btrfs_root
*root
,
1728 struct btrfs_path
*path
,
1729 struct btrfs_extent_inline_ref
*iref
,
1730 int refs_to_drop
, int is_data
)
1734 BUG_ON(!is_data
&& refs_to_drop
!= 1);
1736 ret
= update_inline_extent_backref(trans
, root
, path
, iref
,
1737 -refs_to_drop
, NULL
);
1738 } else if (is_data
) {
1739 ret
= remove_extent_data_ref(trans
, root
, path
, refs_to_drop
);
1741 ret
= btrfs_del_item(trans
, root
, path
);
1746 static void btrfs_issue_discard(struct block_device
*bdev
,
1749 blkdev_issue_discard(bdev
, start
>> 9, len
>> 9, GFP_KERNEL
,
1750 BLKDEV_IFL_WAIT
| BLKDEV_IFL_BARRIER
);
1753 static int btrfs_discard_extent(struct btrfs_root
*root
, u64 bytenr
,
1757 u64 map_length
= num_bytes
;
1758 struct btrfs_multi_bio
*multi
= NULL
;
1760 if (!btrfs_test_opt(root
, DISCARD
))
1763 /* Tell the block device(s) that the sectors can be discarded */
1764 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
1765 bytenr
, &map_length
, &multi
, 0);
1767 struct btrfs_bio_stripe
*stripe
= multi
->stripes
;
1770 if (map_length
> num_bytes
)
1771 map_length
= num_bytes
;
1773 for (i
= 0; i
< multi
->num_stripes
; i
++, stripe
++) {
1774 btrfs_issue_discard(stripe
->dev
->bdev
,
1784 int btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
1785 struct btrfs_root
*root
,
1786 u64 bytenr
, u64 num_bytes
, u64 parent
,
1787 u64 root_objectid
, u64 owner
, u64 offset
)
1790 BUG_ON(owner
< BTRFS_FIRST_FREE_OBJECTID
&&
1791 root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
1793 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1794 ret
= btrfs_add_delayed_tree_ref(trans
, bytenr
, num_bytes
,
1795 parent
, root_objectid
, (int)owner
,
1796 BTRFS_ADD_DELAYED_REF
, NULL
);
1798 ret
= btrfs_add_delayed_data_ref(trans
, bytenr
, num_bytes
,
1799 parent
, root_objectid
, owner
, offset
,
1800 BTRFS_ADD_DELAYED_REF
, NULL
);
1805 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
1806 struct btrfs_root
*root
,
1807 u64 bytenr
, u64 num_bytes
,
1808 u64 parent
, u64 root_objectid
,
1809 u64 owner
, u64 offset
, int refs_to_add
,
1810 struct btrfs_delayed_extent_op
*extent_op
)
1812 struct btrfs_path
*path
;
1813 struct extent_buffer
*leaf
;
1814 struct btrfs_extent_item
*item
;
1819 path
= btrfs_alloc_path();
1824 path
->leave_spinning
= 1;
1825 /* this will setup the path even if it fails to insert the back ref */
1826 ret
= insert_inline_extent_backref(trans
, root
->fs_info
->extent_root
,
1827 path
, bytenr
, num_bytes
, parent
,
1828 root_objectid
, owner
, offset
,
1829 refs_to_add
, extent_op
);
1833 if (ret
!= -EAGAIN
) {
1838 leaf
= path
->nodes
[0];
1839 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1840 refs
= btrfs_extent_refs(leaf
, item
);
1841 btrfs_set_extent_refs(leaf
, item
, refs
+ refs_to_add
);
1843 __run_delayed_extent_op(extent_op
, leaf
, item
);
1845 btrfs_mark_buffer_dirty(leaf
);
1846 btrfs_release_path(root
->fs_info
->extent_root
, path
);
1849 path
->leave_spinning
= 1;
1851 /* now insert the actual backref */
1852 ret
= insert_extent_backref(trans
, root
->fs_info
->extent_root
,
1853 path
, bytenr
, parent
, root_objectid
,
1854 owner
, offset
, refs_to_add
);
1857 btrfs_free_path(path
);
1861 static int run_delayed_data_ref(struct btrfs_trans_handle
*trans
,
1862 struct btrfs_root
*root
,
1863 struct btrfs_delayed_ref_node
*node
,
1864 struct btrfs_delayed_extent_op
*extent_op
,
1865 int insert_reserved
)
1868 struct btrfs_delayed_data_ref
*ref
;
1869 struct btrfs_key ins
;
1874 ins
.objectid
= node
->bytenr
;
1875 ins
.offset
= node
->num_bytes
;
1876 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1878 ref
= btrfs_delayed_node_to_data_ref(node
);
1879 if (node
->type
== BTRFS_SHARED_DATA_REF_KEY
)
1880 parent
= ref
->parent
;
1882 ref_root
= ref
->root
;
1884 if (node
->action
== BTRFS_ADD_DELAYED_REF
&& insert_reserved
) {
1886 BUG_ON(extent_op
->update_key
);
1887 flags
|= extent_op
->flags_to_set
;
1889 ret
= alloc_reserved_file_extent(trans
, root
,
1890 parent
, ref_root
, flags
,
1891 ref
->objectid
, ref
->offset
,
1892 &ins
, node
->ref_mod
);
1893 } else if (node
->action
== BTRFS_ADD_DELAYED_REF
) {
1894 ret
= __btrfs_inc_extent_ref(trans
, root
, node
->bytenr
,
1895 node
->num_bytes
, parent
,
1896 ref_root
, ref
->objectid
,
1897 ref
->offset
, node
->ref_mod
,
1899 } else if (node
->action
== BTRFS_DROP_DELAYED_REF
) {
1900 ret
= __btrfs_free_extent(trans
, root
, node
->bytenr
,
1901 node
->num_bytes
, parent
,
1902 ref_root
, ref
->objectid
,
1903 ref
->offset
, node
->ref_mod
,
1911 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op
*extent_op
,
1912 struct extent_buffer
*leaf
,
1913 struct btrfs_extent_item
*ei
)
1915 u64 flags
= btrfs_extent_flags(leaf
, ei
);
1916 if (extent_op
->update_flags
) {
1917 flags
|= extent_op
->flags_to_set
;
1918 btrfs_set_extent_flags(leaf
, ei
, flags
);
1921 if (extent_op
->update_key
) {
1922 struct btrfs_tree_block_info
*bi
;
1923 BUG_ON(!(flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
));
1924 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
1925 btrfs_set_tree_block_key(leaf
, bi
, &extent_op
->key
);
1929 static int run_delayed_extent_op(struct btrfs_trans_handle
*trans
,
1930 struct btrfs_root
*root
,
1931 struct btrfs_delayed_ref_node
*node
,
1932 struct btrfs_delayed_extent_op
*extent_op
)
1934 struct btrfs_key key
;
1935 struct btrfs_path
*path
;
1936 struct btrfs_extent_item
*ei
;
1937 struct extent_buffer
*leaf
;
1942 path
= btrfs_alloc_path();
1946 key
.objectid
= node
->bytenr
;
1947 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1948 key
.offset
= node
->num_bytes
;
1951 path
->leave_spinning
= 1;
1952 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
,
1963 leaf
= path
->nodes
[0];
1964 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1965 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1966 if (item_size
< sizeof(*ei
)) {
1967 ret
= convert_extent_item_v0(trans
, root
->fs_info
->extent_root
,
1973 leaf
= path
->nodes
[0];
1974 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1977 BUG_ON(item_size
< sizeof(*ei
));
1978 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1979 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1981 btrfs_mark_buffer_dirty(leaf
);
1983 btrfs_free_path(path
);
1987 static int run_delayed_tree_ref(struct btrfs_trans_handle
*trans
,
1988 struct btrfs_root
*root
,
1989 struct btrfs_delayed_ref_node
*node
,
1990 struct btrfs_delayed_extent_op
*extent_op
,
1991 int insert_reserved
)
1994 struct btrfs_delayed_tree_ref
*ref
;
1995 struct btrfs_key ins
;
1999 ins
.objectid
= node
->bytenr
;
2000 ins
.offset
= node
->num_bytes
;
2001 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
2003 ref
= btrfs_delayed_node_to_tree_ref(node
);
2004 if (node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
)
2005 parent
= ref
->parent
;
2007 ref_root
= ref
->root
;
2009 BUG_ON(node
->ref_mod
!= 1);
2010 if (node
->action
== BTRFS_ADD_DELAYED_REF
&& insert_reserved
) {
2011 BUG_ON(!extent_op
|| !extent_op
->update_flags
||
2012 !extent_op
->update_key
);
2013 ret
= alloc_reserved_tree_block(trans
, root
,
2015 extent_op
->flags_to_set
,
2018 } else if (node
->action
== BTRFS_ADD_DELAYED_REF
) {
2019 ret
= __btrfs_inc_extent_ref(trans
, root
, node
->bytenr
,
2020 node
->num_bytes
, parent
, ref_root
,
2021 ref
->level
, 0, 1, extent_op
);
2022 } else if (node
->action
== BTRFS_DROP_DELAYED_REF
) {
2023 ret
= __btrfs_free_extent(trans
, root
, node
->bytenr
,
2024 node
->num_bytes
, parent
, ref_root
,
2025 ref
->level
, 0, 1, extent_op
);
2032 /* helper function to actually process a single delayed ref entry */
2033 static int run_one_delayed_ref(struct btrfs_trans_handle
*trans
,
2034 struct btrfs_root
*root
,
2035 struct btrfs_delayed_ref_node
*node
,
2036 struct btrfs_delayed_extent_op
*extent_op
,
2037 int insert_reserved
)
2040 if (btrfs_delayed_ref_is_head(node
)) {
2041 struct btrfs_delayed_ref_head
*head
;
2043 * we've hit the end of the chain and we were supposed
2044 * to insert this extent into the tree. But, it got
2045 * deleted before we ever needed to insert it, so all
2046 * we have to do is clean up the accounting
2049 head
= btrfs_delayed_node_to_head(node
);
2050 if (insert_reserved
) {
2051 btrfs_pin_extent(root
, node
->bytenr
,
2052 node
->num_bytes
, 1);
2053 if (head
->is_data
) {
2054 ret
= btrfs_del_csums(trans
, root
,
2060 mutex_unlock(&head
->mutex
);
2064 if (node
->type
== BTRFS_TREE_BLOCK_REF_KEY
||
2065 node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
)
2066 ret
= run_delayed_tree_ref(trans
, root
, node
, extent_op
,
2068 else if (node
->type
== BTRFS_EXTENT_DATA_REF_KEY
||
2069 node
->type
== BTRFS_SHARED_DATA_REF_KEY
)
2070 ret
= run_delayed_data_ref(trans
, root
, node
, extent_op
,
2077 static noinline
struct btrfs_delayed_ref_node
*
2078 select_delayed_ref(struct btrfs_delayed_ref_head
*head
)
2080 struct rb_node
*node
;
2081 struct btrfs_delayed_ref_node
*ref
;
2082 int action
= BTRFS_ADD_DELAYED_REF
;
2085 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2086 * this prevents ref count from going down to zero when
2087 * there still are pending delayed ref.
2089 node
= rb_prev(&head
->node
.rb_node
);
2093 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
,
2095 if (ref
->bytenr
!= head
->node
.bytenr
)
2097 if (ref
->action
== action
)
2099 node
= rb_prev(node
);
2101 if (action
== BTRFS_ADD_DELAYED_REF
) {
2102 action
= BTRFS_DROP_DELAYED_REF
;
2108 static noinline
int run_clustered_refs(struct btrfs_trans_handle
*trans
,
2109 struct btrfs_root
*root
,
2110 struct list_head
*cluster
)
2112 struct btrfs_delayed_ref_root
*delayed_refs
;
2113 struct btrfs_delayed_ref_node
*ref
;
2114 struct btrfs_delayed_ref_head
*locked_ref
= NULL
;
2115 struct btrfs_delayed_extent_op
*extent_op
;
2118 int must_insert_reserved
= 0;
2120 delayed_refs
= &trans
->transaction
->delayed_refs
;
2123 /* pick a new head ref from the cluster list */
2124 if (list_empty(cluster
))
2127 locked_ref
= list_entry(cluster
->next
,
2128 struct btrfs_delayed_ref_head
, cluster
);
2130 /* grab the lock that says we are going to process
2131 * all the refs for this head */
2132 ret
= btrfs_delayed_ref_lock(trans
, locked_ref
);
2135 * we may have dropped the spin lock to get the head
2136 * mutex lock, and that might have given someone else
2137 * time to free the head. If that's true, it has been
2138 * removed from our list and we can move on.
2140 if (ret
== -EAGAIN
) {
2148 * record the must insert reserved flag before we
2149 * drop the spin lock.
2151 must_insert_reserved
= locked_ref
->must_insert_reserved
;
2152 locked_ref
->must_insert_reserved
= 0;
2154 extent_op
= locked_ref
->extent_op
;
2155 locked_ref
->extent_op
= NULL
;
2158 * locked_ref is the head node, so we have to go one
2159 * node back for any delayed ref updates
2161 ref
= select_delayed_ref(locked_ref
);
2163 /* All delayed refs have been processed, Go ahead
2164 * and send the head node to run_one_delayed_ref,
2165 * so that any accounting fixes can happen
2167 ref
= &locked_ref
->node
;
2169 if (extent_op
&& must_insert_reserved
) {
2175 spin_unlock(&delayed_refs
->lock
);
2177 ret
= run_delayed_extent_op(trans
, root
,
2183 spin_lock(&delayed_refs
->lock
);
2187 list_del_init(&locked_ref
->cluster
);
2192 rb_erase(&ref
->rb_node
, &delayed_refs
->root
);
2193 delayed_refs
->num_entries
--;
2195 spin_unlock(&delayed_refs
->lock
);
2197 ret
= run_one_delayed_ref(trans
, root
, ref
, extent_op
,
2198 must_insert_reserved
);
2201 btrfs_put_delayed_ref(ref
);
2206 spin_lock(&delayed_refs
->lock
);
2212 * this starts processing the delayed reference count updates and
2213 * extent insertions we have queued up so far. count can be
2214 * 0, which means to process everything in the tree at the start
2215 * of the run (but not newly added entries), or it can be some target
2216 * number you'd like to process.
2218 int btrfs_run_delayed_refs(struct btrfs_trans_handle
*trans
,
2219 struct btrfs_root
*root
, unsigned long count
)
2221 struct rb_node
*node
;
2222 struct btrfs_delayed_ref_root
*delayed_refs
;
2223 struct btrfs_delayed_ref_node
*ref
;
2224 struct list_head cluster
;
2226 int run_all
= count
== (unsigned long)-1;
2229 if (root
== root
->fs_info
->extent_root
)
2230 root
= root
->fs_info
->tree_root
;
2232 delayed_refs
= &trans
->transaction
->delayed_refs
;
2233 INIT_LIST_HEAD(&cluster
);
2235 spin_lock(&delayed_refs
->lock
);
2237 count
= delayed_refs
->num_entries
* 2;
2241 if (!(run_all
|| run_most
) &&
2242 delayed_refs
->num_heads_ready
< 64)
2246 * go find something we can process in the rbtree. We start at
2247 * the beginning of the tree, and then build a cluster
2248 * of refs to process starting at the first one we are able to
2251 ret
= btrfs_find_ref_cluster(trans
, &cluster
,
2252 delayed_refs
->run_delayed_start
);
2256 ret
= run_clustered_refs(trans
, root
, &cluster
);
2259 count
-= min_t(unsigned long, ret
, count
);
2266 node
= rb_first(&delayed_refs
->root
);
2269 count
= (unsigned long)-1;
2272 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
,
2274 if (btrfs_delayed_ref_is_head(ref
)) {
2275 struct btrfs_delayed_ref_head
*head
;
2277 head
= btrfs_delayed_node_to_head(ref
);
2278 atomic_inc(&ref
->refs
);
2280 spin_unlock(&delayed_refs
->lock
);
2281 mutex_lock(&head
->mutex
);
2282 mutex_unlock(&head
->mutex
);
2284 btrfs_put_delayed_ref(ref
);
2288 node
= rb_next(node
);
2290 spin_unlock(&delayed_refs
->lock
);
2291 schedule_timeout(1);
2295 spin_unlock(&delayed_refs
->lock
);
2299 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle
*trans
,
2300 struct btrfs_root
*root
,
2301 u64 bytenr
, u64 num_bytes
, u64 flags
,
2304 struct btrfs_delayed_extent_op
*extent_op
;
2307 extent_op
= kmalloc(sizeof(*extent_op
), GFP_NOFS
);
2311 extent_op
->flags_to_set
= flags
;
2312 extent_op
->update_flags
= 1;
2313 extent_op
->update_key
= 0;
2314 extent_op
->is_data
= is_data
? 1 : 0;
2316 ret
= btrfs_add_delayed_extent_op(trans
, bytenr
, num_bytes
, extent_op
);
2322 static noinline
int check_delayed_ref(struct btrfs_trans_handle
*trans
,
2323 struct btrfs_root
*root
,
2324 struct btrfs_path
*path
,
2325 u64 objectid
, u64 offset
, u64 bytenr
)
2327 struct btrfs_delayed_ref_head
*head
;
2328 struct btrfs_delayed_ref_node
*ref
;
2329 struct btrfs_delayed_data_ref
*data_ref
;
2330 struct btrfs_delayed_ref_root
*delayed_refs
;
2331 struct rb_node
*node
;
2335 delayed_refs
= &trans
->transaction
->delayed_refs
;
2336 spin_lock(&delayed_refs
->lock
);
2337 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
2341 if (!mutex_trylock(&head
->mutex
)) {
2342 atomic_inc(&head
->node
.refs
);
2343 spin_unlock(&delayed_refs
->lock
);
2345 btrfs_release_path(root
->fs_info
->extent_root
, path
);
2347 mutex_lock(&head
->mutex
);
2348 mutex_unlock(&head
->mutex
);
2349 btrfs_put_delayed_ref(&head
->node
);
2353 node
= rb_prev(&head
->node
.rb_node
);
2357 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
2359 if (ref
->bytenr
!= bytenr
)
2363 if (ref
->type
!= BTRFS_EXTENT_DATA_REF_KEY
)
2366 data_ref
= btrfs_delayed_node_to_data_ref(ref
);
2368 node
= rb_prev(node
);
2370 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
2371 if (ref
->bytenr
== bytenr
)
2375 if (data_ref
->root
!= root
->root_key
.objectid
||
2376 data_ref
->objectid
!= objectid
|| data_ref
->offset
!= offset
)
2381 mutex_unlock(&head
->mutex
);
2383 spin_unlock(&delayed_refs
->lock
);
2387 static noinline
int check_committed_ref(struct btrfs_trans_handle
*trans
,
2388 struct btrfs_root
*root
,
2389 struct btrfs_path
*path
,
2390 u64 objectid
, u64 offset
, u64 bytenr
)
2392 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
2393 struct extent_buffer
*leaf
;
2394 struct btrfs_extent_data_ref
*ref
;
2395 struct btrfs_extent_inline_ref
*iref
;
2396 struct btrfs_extent_item
*ei
;
2397 struct btrfs_key key
;
2401 key
.objectid
= bytenr
;
2402 key
.offset
= (u64
)-1;
2403 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2405 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
2411 if (path
->slots
[0] == 0)
2415 leaf
= path
->nodes
[0];
2416 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2418 if (key
.objectid
!= bytenr
|| key
.type
!= BTRFS_EXTENT_ITEM_KEY
)
2422 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2423 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2424 if (item_size
< sizeof(*ei
)) {
2425 WARN_ON(item_size
!= sizeof(struct btrfs_extent_item_v0
));
2429 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
2431 if (item_size
!= sizeof(*ei
) +
2432 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY
))
2435 if (btrfs_extent_generation(leaf
, ei
) <=
2436 btrfs_root_last_snapshot(&root
->root_item
))
2439 iref
= (struct btrfs_extent_inline_ref
*)(ei
+ 1);
2440 if (btrfs_extent_inline_ref_type(leaf
, iref
) !=
2441 BTRFS_EXTENT_DATA_REF_KEY
)
2444 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
2445 if (btrfs_extent_refs(leaf
, ei
) !=
2446 btrfs_extent_data_ref_count(leaf
, ref
) ||
2447 btrfs_extent_data_ref_root(leaf
, ref
) !=
2448 root
->root_key
.objectid
||
2449 btrfs_extent_data_ref_objectid(leaf
, ref
) != objectid
||
2450 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
2458 int btrfs_cross_ref_exist(struct btrfs_trans_handle
*trans
,
2459 struct btrfs_root
*root
,
2460 u64 objectid
, u64 offset
, u64 bytenr
)
2462 struct btrfs_path
*path
;
2466 path
= btrfs_alloc_path();
2471 ret
= check_committed_ref(trans
, root
, path
, objectid
,
2473 if (ret
&& ret
!= -ENOENT
)
2476 ret2
= check_delayed_ref(trans
, root
, path
, objectid
,
2478 } while (ret2
== -EAGAIN
);
2480 if (ret2
&& ret2
!= -ENOENT
) {
2485 if (ret
!= -ENOENT
|| ret2
!= -ENOENT
)
2488 btrfs_free_path(path
);
2489 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
2495 int btrfs_cache_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2496 struct extent_buffer
*buf
, u32 nr_extents
)
2498 struct btrfs_key key
;
2499 struct btrfs_file_extent_item
*fi
;
2507 if (!root
->ref_cows
)
2510 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
2512 root_gen
= root
->root_key
.offset
;
2515 root_gen
= trans
->transid
- 1;
2518 level
= btrfs_header_level(buf
);
2519 nritems
= btrfs_header_nritems(buf
);
2522 struct btrfs_leaf_ref
*ref
;
2523 struct btrfs_extent_info
*info
;
2525 ref
= btrfs_alloc_leaf_ref(root
, nr_extents
);
2531 ref
->root_gen
= root_gen
;
2532 ref
->bytenr
= buf
->start
;
2533 ref
->owner
= btrfs_header_owner(buf
);
2534 ref
->generation
= btrfs_header_generation(buf
);
2535 ref
->nritems
= nr_extents
;
2536 info
= ref
->extents
;
2538 for (i
= 0; nr_extents
> 0 && i
< nritems
; i
++) {
2540 btrfs_item_key_to_cpu(buf
, &key
, i
);
2541 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
2543 fi
= btrfs_item_ptr(buf
, i
,
2544 struct btrfs_file_extent_item
);
2545 if (btrfs_file_extent_type(buf
, fi
) ==
2546 BTRFS_FILE_EXTENT_INLINE
)
2548 disk_bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
2549 if (disk_bytenr
== 0)
2552 info
->bytenr
= disk_bytenr
;
2554 btrfs_file_extent_disk_num_bytes(buf
, fi
);
2555 info
->objectid
= key
.objectid
;
2556 info
->offset
= key
.offset
;
2560 ret
= btrfs_add_leaf_ref(root
, ref
, shared
);
2561 if (ret
== -EEXIST
&& shared
) {
2562 struct btrfs_leaf_ref
*old
;
2563 old
= btrfs_lookup_leaf_ref(root
, ref
->bytenr
);
2565 btrfs_remove_leaf_ref(root
, old
);
2566 btrfs_free_leaf_ref(root
, old
);
2567 ret
= btrfs_add_leaf_ref(root
, ref
, shared
);
2570 btrfs_free_leaf_ref(root
, ref
);
2576 /* when a block goes through cow, we update the reference counts of
2577 * everything that block points to. The internal pointers of the block
2578 * can be in just about any order, and it is likely to have clusters of
2579 * things that are close together and clusters of things that are not.
2581 * To help reduce the seeks that come with updating all of these reference
2582 * counts, sort them by byte number before actual updates are done.
2584 * struct refsort is used to match byte number to slot in the btree block.
2585 * we sort based on the byte number and then use the slot to actually
2588 * struct refsort is smaller than strcut btrfs_item and smaller than
2589 * struct btrfs_key_ptr. Since we're currently limited to the page size
2590 * for a btree block, there's no way for a kmalloc of refsorts for a
2591 * single node to be bigger than a page.
2599 * for passing into sort()
2601 static int refsort_cmp(const void *a_void
, const void *b_void
)
2603 const struct refsort
*a
= a_void
;
2604 const struct refsort
*b
= b_void
;
2606 if (a
->bytenr
< b
->bytenr
)
2608 if (a
->bytenr
> b
->bytenr
)
2614 static int __btrfs_mod_ref(struct btrfs_trans_handle
*trans
,
2615 struct btrfs_root
*root
,
2616 struct extent_buffer
*buf
,
2617 int full_backref
, int inc
)
2624 struct btrfs_key key
;
2625 struct btrfs_file_extent_item
*fi
;
2629 int (*process_func
)(struct btrfs_trans_handle
*, struct btrfs_root
*,
2630 u64
, u64
, u64
, u64
, u64
, u64
);
2632 ref_root
= btrfs_header_owner(buf
);
2633 nritems
= btrfs_header_nritems(buf
);
2634 level
= btrfs_header_level(buf
);
2636 if (!root
->ref_cows
&& level
== 0)
2640 process_func
= btrfs_inc_extent_ref
;
2642 process_func
= btrfs_free_extent
;
2645 parent
= buf
->start
;
2649 for (i
= 0; i
< nritems
; i
++) {
2651 btrfs_item_key_to_cpu(buf
, &key
, i
);
2652 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
2654 fi
= btrfs_item_ptr(buf
, i
,
2655 struct btrfs_file_extent_item
);
2656 if (btrfs_file_extent_type(buf
, fi
) ==
2657 BTRFS_FILE_EXTENT_INLINE
)
2659 bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
2663 num_bytes
= btrfs_file_extent_disk_num_bytes(buf
, fi
);
2664 key
.offset
-= btrfs_file_extent_offset(buf
, fi
);
2665 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
2666 parent
, ref_root
, key
.objectid
,
2671 bytenr
= btrfs_node_blockptr(buf
, i
);
2672 num_bytes
= btrfs_level_size(root
, level
- 1);
2673 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
2674 parent
, ref_root
, level
- 1, 0);
2685 int btrfs_inc_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2686 struct extent_buffer
*buf
, int full_backref
)
2688 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 1);
2691 int btrfs_dec_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2692 struct extent_buffer
*buf
, int full_backref
)
2694 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 0);
2697 static int write_one_cache_group(struct btrfs_trans_handle
*trans
,
2698 struct btrfs_root
*root
,
2699 struct btrfs_path
*path
,
2700 struct btrfs_block_group_cache
*cache
)
2703 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
2705 struct extent_buffer
*leaf
;
2707 ret
= btrfs_search_slot(trans
, extent_root
, &cache
->key
, path
, 0, 1);
2712 leaf
= path
->nodes
[0];
2713 bi
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
2714 write_extent_buffer(leaf
, &cache
->item
, bi
, sizeof(cache
->item
));
2715 btrfs_mark_buffer_dirty(leaf
);
2716 btrfs_release_path(extent_root
, path
);
2724 static struct btrfs_block_group_cache
*
2725 next_block_group(struct btrfs_root
*root
,
2726 struct btrfs_block_group_cache
*cache
)
2728 struct rb_node
*node
;
2729 spin_lock(&root
->fs_info
->block_group_cache_lock
);
2730 node
= rb_next(&cache
->cache_node
);
2731 btrfs_put_block_group(cache
);
2733 cache
= rb_entry(node
, struct btrfs_block_group_cache
,
2735 btrfs_get_block_group(cache
);
2738 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
2742 static int cache_save_setup(struct btrfs_block_group_cache
*block_group
,
2743 struct btrfs_trans_handle
*trans
,
2744 struct btrfs_path
*path
)
2746 struct btrfs_root
*root
= block_group
->fs_info
->tree_root
;
2747 struct inode
*inode
= NULL
;
2749 int dcs
= BTRFS_DC_ERROR
;
2755 * If this block group is smaller than 100 megs don't bother caching the
2758 if (block_group
->key
.offset
< (100 * 1024 * 1024)) {
2759 spin_lock(&block_group
->lock
);
2760 block_group
->disk_cache_state
= BTRFS_DC_WRITTEN
;
2761 spin_unlock(&block_group
->lock
);
2766 inode
= lookup_free_space_inode(root
, block_group
, path
);
2767 if (IS_ERR(inode
) && PTR_ERR(inode
) != -ENOENT
) {
2768 ret
= PTR_ERR(inode
);
2769 btrfs_release_path(root
, path
);
2773 if (IS_ERR(inode
)) {
2777 if (block_group
->ro
)
2780 ret
= create_free_space_inode(root
, trans
, block_group
, path
);
2787 * We want to set the generation to 0, that way if anything goes wrong
2788 * from here on out we know not to trust this cache when we load up next
2791 BTRFS_I(inode
)->generation
= 0;
2792 ret
= btrfs_update_inode(trans
, root
, inode
);
2795 if (i_size_read(inode
) > 0) {
2796 ret
= btrfs_truncate_free_space_cache(root
, trans
, path
,
2802 spin_lock(&block_group
->lock
);
2803 if (block_group
->cached
!= BTRFS_CACHE_FINISHED
) {
2804 /* We're not cached, don't bother trying to write stuff out */
2805 dcs
= BTRFS_DC_WRITTEN
;
2806 spin_unlock(&block_group
->lock
);
2809 spin_unlock(&block_group
->lock
);
2811 num_pages
= (int)div64_u64(block_group
->key
.offset
, 1024 * 1024 * 1024);
2816 * Just to make absolutely sure we have enough space, we're going to
2817 * preallocate 12 pages worth of space for each block group. In
2818 * practice we ought to use at most 8, but we need extra space so we can
2819 * add our header and have a terminator between the extents and the
2823 num_pages
*= PAGE_CACHE_SIZE
;
2825 ret
= btrfs_check_data_free_space(inode
, num_pages
);
2829 ret
= btrfs_prealloc_file_range_trans(inode
, trans
, 0, 0, num_pages
,
2830 num_pages
, num_pages
,
2833 dcs
= BTRFS_DC_SETUP
;
2834 btrfs_free_reserved_data_space(inode
, num_pages
);
2838 btrfs_release_path(root
, path
);
2840 spin_lock(&block_group
->lock
);
2841 block_group
->disk_cache_state
= dcs
;
2842 spin_unlock(&block_group
->lock
);
2847 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle
*trans
,
2848 struct btrfs_root
*root
)
2850 struct btrfs_block_group_cache
*cache
;
2852 struct btrfs_path
*path
;
2855 path
= btrfs_alloc_path();
2861 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
2863 if (cache
->disk_cache_state
== BTRFS_DC_CLEAR
)
2865 cache
= next_block_group(root
, cache
);
2873 err
= cache_save_setup(cache
, trans
, path
);
2874 last
= cache
->key
.objectid
+ cache
->key
.offset
;
2875 btrfs_put_block_group(cache
);
2880 err
= btrfs_run_delayed_refs(trans
, root
,
2885 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
2887 if (cache
->disk_cache_state
== BTRFS_DC_CLEAR
) {
2888 btrfs_put_block_group(cache
);
2894 cache
= next_block_group(root
, cache
);
2903 if (cache
->disk_cache_state
== BTRFS_DC_SETUP
)
2904 cache
->disk_cache_state
= BTRFS_DC_NEED_WRITE
;
2906 last
= cache
->key
.objectid
+ cache
->key
.offset
;
2908 err
= write_one_cache_group(trans
, root
, path
, cache
);
2910 btrfs_put_block_group(cache
);
2915 * I don't think this is needed since we're just marking our
2916 * preallocated extent as written, but just in case it can't
2920 err
= btrfs_run_delayed_refs(trans
, root
,
2925 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
2928 * Really this shouldn't happen, but it could if we
2929 * couldn't write the entire preallocated extent and
2930 * splitting the extent resulted in a new block.
2933 btrfs_put_block_group(cache
);
2936 if (cache
->disk_cache_state
== BTRFS_DC_NEED_WRITE
)
2938 cache
= next_block_group(root
, cache
);
2947 btrfs_write_out_cache(root
, trans
, cache
, path
);
2950 * If we didn't have an error then the cache state is still
2951 * NEED_WRITE, so we can set it to WRITTEN.
2953 if (cache
->disk_cache_state
== BTRFS_DC_NEED_WRITE
)
2954 cache
->disk_cache_state
= BTRFS_DC_WRITTEN
;
2955 last
= cache
->key
.objectid
+ cache
->key
.offset
;
2956 btrfs_put_block_group(cache
);
2959 btrfs_free_path(path
);
2963 int btrfs_extent_readonly(struct btrfs_root
*root
, u64 bytenr
)
2965 struct btrfs_block_group_cache
*block_group
;
2968 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
2969 if (!block_group
|| block_group
->ro
)
2972 btrfs_put_block_group(block_group
);
2976 static int update_space_info(struct btrfs_fs_info
*info
, u64 flags
,
2977 u64 total_bytes
, u64 bytes_used
,
2978 struct btrfs_space_info
**space_info
)
2980 struct btrfs_space_info
*found
;
2984 if (flags
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
2985 BTRFS_BLOCK_GROUP_RAID10
))
2990 found
= __find_space_info(info
, flags
);
2992 spin_lock(&found
->lock
);
2993 found
->total_bytes
+= total_bytes
;
2994 found
->disk_total
+= total_bytes
* factor
;
2995 found
->bytes_used
+= bytes_used
;
2996 found
->disk_used
+= bytes_used
* factor
;
2998 spin_unlock(&found
->lock
);
2999 *space_info
= found
;
3002 found
= kzalloc(sizeof(*found
), GFP_NOFS
);
3006 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++)
3007 INIT_LIST_HEAD(&found
->block_groups
[i
]);
3008 init_rwsem(&found
->groups_sem
);
3009 spin_lock_init(&found
->lock
);
3010 found
->flags
= flags
& (BTRFS_BLOCK_GROUP_DATA
|
3011 BTRFS_BLOCK_GROUP_SYSTEM
|
3012 BTRFS_BLOCK_GROUP_METADATA
);
3013 found
->total_bytes
= total_bytes
;
3014 found
->disk_total
= total_bytes
* factor
;
3015 found
->bytes_used
= bytes_used
;
3016 found
->disk_used
= bytes_used
* factor
;
3017 found
->bytes_pinned
= 0;
3018 found
->bytes_reserved
= 0;
3019 found
->bytes_readonly
= 0;
3020 found
->bytes_may_use
= 0;
3022 found
->force_alloc
= 0;
3023 *space_info
= found
;
3024 list_add_rcu(&found
->list
, &info
->space_info
);
3025 atomic_set(&found
->caching_threads
, 0);
3029 static void set_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
3031 u64 extra_flags
= flags
& (BTRFS_BLOCK_GROUP_RAID0
|
3032 BTRFS_BLOCK_GROUP_RAID1
|
3033 BTRFS_BLOCK_GROUP_RAID10
|
3034 BTRFS_BLOCK_GROUP_DUP
);
3036 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3037 fs_info
->avail_data_alloc_bits
|= extra_flags
;
3038 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
3039 fs_info
->avail_metadata_alloc_bits
|= extra_flags
;
3040 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
3041 fs_info
->avail_system_alloc_bits
|= extra_flags
;
3045 u64
btrfs_reduce_alloc_profile(struct btrfs_root
*root
, u64 flags
)
3048 * we add in the count of missing devices because we want
3049 * to make sure that any RAID levels on a degraded FS
3050 * continue to be honored.
3052 u64 num_devices
= root
->fs_info
->fs_devices
->rw_devices
+
3053 root
->fs_info
->fs_devices
->missing_devices
;
3055 if (num_devices
== 1)
3056 flags
&= ~(BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID0
);
3057 if (num_devices
< 4)
3058 flags
&= ~BTRFS_BLOCK_GROUP_RAID10
;
3060 if ((flags
& BTRFS_BLOCK_GROUP_DUP
) &&
3061 (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
3062 BTRFS_BLOCK_GROUP_RAID10
))) {
3063 flags
&= ~BTRFS_BLOCK_GROUP_DUP
;
3066 if ((flags
& BTRFS_BLOCK_GROUP_RAID1
) &&
3067 (flags
& BTRFS_BLOCK_GROUP_RAID10
)) {
3068 flags
&= ~BTRFS_BLOCK_GROUP_RAID1
;
3071 if ((flags
& BTRFS_BLOCK_GROUP_RAID0
) &&
3072 ((flags
& BTRFS_BLOCK_GROUP_RAID1
) |
3073 (flags
& BTRFS_BLOCK_GROUP_RAID10
) |
3074 (flags
& BTRFS_BLOCK_GROUP_DUP
)))
3075 flags
&= ~BTRFS_BLOCK_GROUP_RAID0
;
3079 static u64
get_alloc_profile(struct btrfs_root
*root
, u64 flags
)
3081 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3082 flags
|= root
->fs_info
->avail_data_alloc_bits
&
3083 root
->fs_info
->data_alloc_profile
;
3084 else if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
3085 flags
|= root
->fs_info
->avail_system_alloc_bits
&
3086 root
->fs_info
->system_alloc_profile
;
3087 else if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
3088 flags
|= root
->fs_info
->avail_metadata_alloc_bits
&
3089 root
->fs_info
->metadata_alloc_profile
;
3090 return btrfs_reduce_alloc_profile(root
, flags
);
3093 u64
btrfs_get_alloc_profile(struct btrfs_root
*root
, int data
)
3098 flags
= BTRFS_BLOCK_GROUP_DATA
;
3099 else if (root
== root
->fs_info
->chunk_root
)
3100 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
3102 flags
= BTRFS_BLOCK_GROUP_METADATA
;
3104 return get_alloc_profile(root
, flags
);
3107 void btrfs_set_inode_space_info(struct btrfs_root
*root
, struct inode
*inode
)
3109 BTRFS_I(inode
)->space_info
= __find_space_info(root
->fs_info
,
3110 BTRFS_BLOCK_GROUP_DATA
);
3114 * This will check the space that the inode allocates from to make sure we have
3115 * enough space for bytes.
3117 int btrfs_check_data_free_space(struct inode
*inode
, u64 bytes
)
3119 struct btrfs_space_info
*data_sinfo
;
3120 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3122 int ret
= 0, committed
= 0, alloc_chunk
= 1;
3124 /* make sure bytes are sectorsize aligned */
3125 bytes
= (bytes
+ root
->sectorsize
- 1) & ~((u64
)root
->sectorsize
- 1);
3127 if (root
== root
->fs_info
->tree_root
) {
3132 data_sinfo
= BTRFS_I(inode
)->space_info
;
3137 /* make sure we have enough space to handle the data first */
3138 spin_lock(&data_sinfo
->lock
);
3139 used
= data_sinfo
->bytes_used
+ data_sinfo
->bytes_reserved
+
3140 data_sinfo
->bytes_pinned
+ data_sinfo
->bytes_readonly
+
3141 data_sinfo
->bytes_may_use
;
3143 if (used
+ bytes
> data_sinfo
->total_bytes
) {
3144 struct btrfs_trans_handle
*trans
;
3147 * if we don't have enough free bytes in this space then we need
3148 * to alloc a new chunk.
3150 if (!data_sinfo
->full
&& alloc_chunk
) {
3153 data_sinfo
->force_alloc
= 1;
3154 spin_unlock(&data_sinfo
->lock
);
3156 alloc_target
= btrfs_get_alloc_profile(root
, 1);
3157 trans
= btrfs_join_transaction(root
, 1);
3159 return PTR_ERR(trans
);
3161 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
3162 bytes
+ 2 * 1024 * 1024,
3164 btrfs_end_transaction(trans
, root
);
3173 btrfs_set_inode_space_info(root
, inode
);
3174 data_sinfo
= BTRFS_I(inode
)->space_info
;
3178 spin_unlock(&data_sinfo
->lock
);
3180 /* commit the current transaction and try again */
3182 if (!committed
&& !root
->fs_info
->open_ioctl_trans
) {
3184 trans
= btrfs_join_transaction(root
, 1);
3186 return PTR_ERR(trans
);
3187 ret
= btrfs_commit_transaction(trans
, root
);
3193 #if 0 /* I hope we never need this code again, just in case */
3194 printk(KERN_ERR
"no space left, need %llu, %llu bytes_used, "
3195 "%llu bytes_reserved, " "%llu bytes_pinned, "
3196 "%llu bytes_readonly, %llu may use %llu total\n",
3197 (unsigned long long)bytes
,
3198 (unsigned long long)data_sinfo
->bytes_used
,
3199 (unsigned long long)data_sinfo
->bytes_reserved
,
3200 (unsigned long long)data_sinfo
->bytes_pinned
,
3201 (unsigned long long)data_sinfo
->bytes_readonly
,
3202 (unsigned long long)data_sinfo
->bytes_may_use
,
3203 (unsigned long long)data_sinfo
->total_bytes
);
3207 data_sinfo
->bytes_may_use
+= bytes
;
3208 BTRFS_I(inode
)->reserved_bytes
+= bytes
;
3209 spin_unlock(&data_sinfo
->lock
);
3215 * called when we are clearing an delalloc extent from the
3216 * inode's io_tree or there was an error for whatever reason
3217 * after calling btrfs_check_data_free_space
3219 void btrfs_free_reserved_data_space(struct inode
*inode
, u64 bytes
)
3221 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3222 struct btrfs_space_info
*data_sinfo
;
3224 /* make sure bytes are sectorsize aligned */
3225 bytes
= (bytes
+ root
->sectorsize
- 1) & ~((u64
)root
->sectorsize
- 1);
3227 data_sinfo
= BTRFS_I(inode
)->space_info
;
3228 spin_lock(&data_sinfo
->lock
);
3229 data_sinfo
->bytes_may_use
-= bytes
;
3230 BTRFS_I(inode
)->reserved_bytes
-= bytes
;
3231 spin_unlock(&data_sinfo
->lock
);
3234 static void force_metadata_allocation(struct btrfs_fs_info
*info
)
3236 struct list_head
*head
= &info
->space_info
;
3237 struct btrfs_space_info
*found
;
3240 list_for_each_entry_rcu(found
, head
, list
) {
3241 if (found
->flags
& BTRFS_BLOCK_GROUP_METADATA
)
3242 found
->force_alloc
= 1;
3247 static int should_alloc_chunk(struct btrfs_root
*root
,
3248 struct btrfs_space_info
*sinfo
, u64 alloc_bytes
)
3250 u64 num_bytes
= sinfo
->total_bytes
- sinfo
->bytes_readonly
;
3253 if (sinfo
->bytes_used
+ sinfo
->bytes_reserved
+
3254 alloc_bytes
+ 256 * 1024 * 1024 < num_bytes
)
3257 if (sinfo
->bytes_used
+ sinfo
->bytes_reserved
+
3258 alloc_bytes
< div_factor(num_bytes
, 8))
3261 thresh
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
3262 thresh
= max_t(u64
, 256 * 1024 * 1024, div_factor_fine(thresh
, 5));
3264 if (num_bytes
> thresh
&& sinfo
->bytes_used
< div_factor(num_bytes
, 3))
3270 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
3271 struct btrfs_root
*extent_root
, u64 alloc_bytes
,
3272 u64 flags
, int force
)
3274 struct btrfs_space_info
*space_info
;
3275 struct btrfs_fs_info
*fs_info
= extent_root
->fs_info
;
3278 mutex_lock(&fs_info
->chunk_mutex
);
3280 flags
= btrfs_reduce_alloc_profile(extent_root
, flags
);
3282 space_info
= __find_space_info(extent_root
->fs_info
, flags
);
3284 ret
= update_space_info(extent_root
->fs_info
, flags
,
3288 BUG_ON(!space_info
);
3290 spin_lock(&space_info
->lock
);
3291 if (space_info
->force_alloc
)
3293 if (space_info
->full
) {
3294 spin_unlock(&space_info
->lock
);
3298 if (!force
&& !should_alloc_chunk(extent_root
, space_info
,
3300 spin_unlock(&space_info
->lock
);
3303 spin_unlock(&space_info
->lock
);
3306 * If we have mixed data/metadata chunks we want to make sure we keep
3307 * allocating mixed chunks instead of individual chunks.
3309 if (btrfs_mixed_space_info(space_info
))
3310 flags
|= (BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
);
3313 * if we're doing a data chunk, go ahead and make sure that
3314 * we keep a reasonable number of metadata chunks allocated in the
3317 if (flags
& BTRFS_BLOCK_GROUP_DATA
&& fs_info
->metadata_ratio
) {
3318 fs_info
->data_chunk_allocations
++;
3319 if (!(fs_info
->data_chunk_allocations
%
3320 fs_info
->metadata_ratio
))
3321 force_metadata_allocation(fs_info
);
3324 ret
= btrfs_alloc_chunk(trans
, extent_root
, flags
);
3325 spin_lock(&space_info
->lock
);
3327 space_info
->full
= 1;
3330 space_info
->force_alloc
= 0;
3331 spin_unlock(&space_info
->lock
);
3333 mutex_unlock(&extent_root
->fs_info
->chunk_mutex
);
3338 * shrink metadata reservation for delalloc
3340 static int shrink_delalloc(struct btrfs_trans_handle
*trans
,
3341 struct btrfs_root
*root
, u64 to_reclaim
, int sync
)
3343 struct btrfs_block_rsv
*block_rsv
;
3344 struct btrfs_space_info
*space_info
;
3350 int nr_pages
= (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT
;
3353 block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
3354 space_info
= block_rsv
->space_info
;
3357 reserved
= space_info
->bytes_reserved
;
3362 max_reclaim
= min(reserved
, to_reclaim
);
3364 while (loops
< 1024) {
3365 /* have the flusher threads jump in and do some IO */
3367 nr_pages
= min_t(unsigned long, nr_pages
,
3368 root
->fs_info
->delalloc_bytes
>> PAGE_CACHE_SHIFT
);
3369 writeback_inodes_sb_nr_if_idle(root
->fs_info
->sb
, nr_pages
);
3371 spin_lock(&space_info
->lock
);
3372 if (reserved
> space_info
->bytes_reserved
) {
3374 reclaimed
+= reserved
- space_info
->bytes_reserved
;
3378 reserved
= space_info
->bytes_reserved
;
3379 spin_unlock(&space_info
->lock
);
3381 if (reserved
== 0 || reclaimed
>= max_reclaim
)
3384 if (trans
&& trans
->transaction
->blocked
)
3387 __set_current_state(TASK_INTERRUPTIBLE
);
3388 time_left
= schedule_timeout(pause
);
3390 /* We were interrupted, exit */
3395 if (pause
> HZ
/ 10)
3399 return reclaimed
>= to_reclaim
;
3403 * Retries tells us how many times we've called reserve_metadata_bytes. The
3404 * idea is if this is the first call (retries == 0) then we will add to our
3405 * reserved count if we can't make the allocation in order to hold our place
3406 * while we go and try and free up space. That way for retries > 1 we don't try
3407 * and add space, we just check to see if the amount of unused space is >= the
3408 * total space, meaning that our reservation is valid.
3410 * However if we don't intend to retry this reservation, pass -1 as retries so
3411 * that it short circuits this logic.
3413 static int reserve_metadata_bytes(struct btrfs_trans_handle
*trans
,
3414 struct btrfs_root
*root
,
3415 struct btrfs_block_rsv
*block_rsv
,
3416 u64 orig_bytes
, int flush
)
3418 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
3420 u64 num_bytes
= orig_bytes
;
3423 bool reserved
= false;
3424 bool committed
= false;
3431 spin_lock(&space_info
->lock
);
3432 unused
= space_info
->bytes_used
+ space_info
->bytes_reserved
+
3433 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
3434 space_info
->bytes_may_use
;
3437 * The idea here is that we've not already over-reserved the block group
3438 * then we can go ahead and save our reservation first and then start
3439 * flushing if we need to. Otherwise if we've already overcommitted
3440 * lets start flushing stuff first and then come back and try to make
3443 if (unused
<= space_info
->total_bytes
) {
3444 unused
= space_info
->total_bytes
- unused
;
3445 if (unused
>= num_bytes
) {
3447 space_info
->bytes_reserved
+= orig_bytes
;
3451 * Ok set num_bytes to orig_bytes since we aren't
3452 * overocmmitted, this way we only try and reclaim what
3455 num_bytes
= orig_bytes
;
3459 * Ok we're over committed, set num_bytes to the overcommitted
3460 * amount plus the amount of bytes that we need for this
3463 num_bytes
= unused
- space_info
->total_bytes
+
3464 (orig_bytes
* (retries
+ 1));
3468 * Couldn't make our reservation, save our place so while we're trying
3469 * to reclaim space we can actually use it instead of somebody else
3470 * stealing it from us.
3472 if (ret
&& !reserved
) {
3473 space_info
->bytes_reserved
+= orig_bytes
;
3477 spin_unlock(&space_info
->lock
);
3486 * We do synchronous shrinking since we don't actually unreserve
3487 * metadata until after the IO is completed.
3489 ret
= shrink_delalloc(trans
, root
, num_bytes
, 1);
3496 * So if we were overcommitted it's possible that somebody else flushed
3497 * out enough space and we simply didn't have enough space to reclaim,
3498 * so go back around and try again.
3505 spin_lock(&space_info
->lock
);
3507 * Not enough space to be reclaimed, don't bother committing the
3510 if (space_info
->bytes_pinned
< orig_bytes
)
3512 spin_unlock(&space_info
->lock
);
3517 if (trans
|| committed
)
3521 trans
= btrfs_join_transaction(root
, 1);
3524 ret
= btrfs_commit_transaction(trans
, root
);
3533 spin_lock(&space_info
->lock
);
3534 space_info
->bytes_reserved
-= orig_bytes
;
3535 spin_unlock(&space_info
->lock
);
3541 static struct btrfs_block_rsv
*get_block_rsv(struct btrfs_trans_handle
*trans
,
3542 struct btrfs_root
*root
)
3544 struct btrfs_block_rsv
*block_rsv
;
3546 block_rsv
= trans
->block_rsv
;
3548 block_rsv
= root
->block_rsv
;
3551 block_rsv
= &root
->fs_info
->empty_block_rsv
;
3556 static int block_rsv_use_bytes(struct btrfs_block_rsv
*block_rsv
,
3560 spin_lock(&block_rsv
->lock
);
3561 if (block_rsv
->reserved
>= num_bytes
) {
3562 block_rsv
->reserved
-= num_bytes
;
3563 if (block_rsv
->reserved
< block_rsv
->size
)
3564 block_rsv
->full
= 0;
3567 spin_unlock(&block_rsv
->lock
);
3571 static void block_rsv_add_bytes(struct btrfs_block_rsv
*block_rsv
,
3572 u64 num_bytes
, int update_size
)
3574 spin_lock(&block_rsv
->lock
);
3575 block_rsv
->reserved
+= num_bytes
;
3577 block_rsv
->size
+= num_bytes
;
3578 else if (block_rsv
->reserved
>= block_rsv
->size
)
3579 block_rsv
->full
= 1;
3580 spin_unlock(&block_rsv
->lock
);
3583 void block_rsv_release_bytes(struct btrfs_block_rsv
*block_rsv
,
3584 struct btrfs_block_rsv
*dest
, u64 num_bytes
)
3586 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
3588 spin_lock(&block_rsv
->lock
);
3589 if (num_bytes
== (u64
)-1)
3590 num_bytes
= block_rsv
->size
;
3591 block_rsv
->size
-= num_bytes
;
3592 if (block_rsv
->reserved
>= block_rsv
->size
) {
3593 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
3594 block_rsv
->reserved
= block_rsv
->size
;
3595 block_rsv
->full
= 1;
3599 spin_unlock(&block_rsv
->lock
);
3601 if (num_bytes
> 0) {
3603 spin_lock(&dest
->lock
);
3607 bytes_to_add
= dest
->size
- dest
->reserved
;
3608 bytes_to_add
= min(num_bytes
, bytes_to_add
);
3609 dest
->reserved
+= bytes_to_add
;
3610 if (dest
->reserved
>= dest
->size
)
3612 num_bytes
-= bytes_to_add
;
3614 spin_unlock(&dest
->lock
);
3617 spin_lock(&space_info
->lock
);
3618 space_info
->bytes_reserved
-= num_bytes
;
3619 spin_unlock(&space_info
->lock
);
3624 static int block_rsv_migrate_bytes(struct btrfs_block_rsv
*src
,
3625 struct btrfs_block_rsv
*dst
, u64 num_bytes
)
3629 ret
= block_rsv_use_bytes(src
, num_bytes
);
3633 block_rsv_add_bytes(dst
, num_bytes
, 1);
3637 void btrfs_init_block_rsv(struct btrfs_block_rsv
*rsv
)
3639 memset(rsv
, 0, sizeof(*rsv
));
3640 spin_lock_init(&rsv
->lock
);
3641 atomic_set(&rsv
->usage
, 1);
3643 INIT_LIST_HEAD(&rsv
->list
);
3646 struct btrfs_block_rsv
*btrfs_alloc_block_rsv(struct btrfs_root
*root
)
3648 struct btrfs_block_rsv
*block_rsv
;
3649 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3651 block_rsv
= kmalloc(sizeof(*block_rsv
), GFP_NOFS
);
3655 btrfs_init_block_rsv(block_rsv
);
3656 block_rsv
->space_info
= __find_space_info(fs_info
,
3657 BTRFS_BLOCK_GROUP_METADATA
);
3661 void btrfs_free_block_rsv(struct btrfs_root
*root
,
3662 struct btrfs_block_rsv
*rsv
)
3664 if (rsv
&& atomic_dec_and_test(&rsv
->usage
)) {
3665 btrfs_block_rsv_release(root
, rsv
, (u64
)-1);
3672 * make the block_rsv struct be able to capture freed space.
3673 * the captured space will re-add to the the block_rsv struct
3674 * after transaction commit
3676 void btrfs_add_durable_block_rsv(struct btrfs_fs_info
*fs_info
,
3677 struct btrfs_block_rsv
*block_rsv
)
3679 block_rsv
->durable
= 1;
3680 mutex_lock(&fs_info
->durable_block_rsv_mutex
);
3681 list_add_tail(&block_rsv
->list
, &fs_info
->durable_block_rsv_list
);
3682 mutex_unlock(&fs_info
->durable_block_rsv_mutex
);
3685 int btrfs_block_rsv_add(struct btrfs_trans_handle
*trans
,
3686 struct btrfs_root
*root
,
3687 struct btrfs_block_rsv
*block_rsv
,
3695 ret
= reserve_metadata_bytes(trans
, root
, block_rsv
, num_bytes
, 1);
3697 block_rsv_add_bytes(block_rsv
, num_bytes
, 1);
3704 int btrfs_block_rsv_check(struct btrfs_trans_handle
*trans
,
3705 struct btrfs_root
*root
,
3706 struct btrfs_block_rsv
*block_rsv
,
3707 u64 min_reserved
, int min_factor
)
3710 int commit_trans
= 0;
3716 spin_lock(&block_rsv
->lock
);
3718 num_bytes
= div_factor(block_rsv
->size
, min_factor
);
3719 if (min_reserved
> num_bytes
)
3720 num_bytes
= min_reserved
;
3722 if (block_rsv
->reserved
>= num_bytes
) {
3725 num_bytes
-= block_rsv
->reserved
;
3726 if (block_rsv
->durable
&&
3727 block_rsv
->freed
[0] + block_rsv
->freed
[1] >= num_bytes
)
3730 spin_unlock(&block_rsv
->lock
);
3734 if (block_rsv
->refill_used
) {
3735 ret
= reserve_metadata_bytes(trans
, root
, block_rsv
,
3738 block_rsv_add_bytes(block_rsv
, num_bytes
, 0);
3747 trans
= btrfs_join_transaction(root
, 1);
3748 BUG_ON(IS_ERR(trans
));
3749 ret
= btrfs_commit_transaction(trans
, root
);
3756 int btrfs_block_rsv_migrate(struct btrfs_block_rsv
*src_rsv
,
3757 struct btrfs_block_rsv
*dst_rsv
,
3760 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
3763 void btrfs_block_rsv_release(struct btrfs_root
*root
,
3764 struct btrfs_block_rsv
*block_rsv
,
3767 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
3768 if (global_rsv
->full
|| global_rsv
== block_rsv
||
3769 block_rsv
->space_info
!= global_rsv
->space_info
)
3771 block_rsv_release_bytes(block_rsv
, global_rsv
, num_bytes
);
3775 * helper to calculate size of global block reservation.
3776 * the desired value is sum of space used by extent tree,
3777 * checksum tree and root tree
3779 static u64
calc_global_metadata_size(struct btrfs_fs_info
*fs_info
)
3781 struct btrfs_space_info
*sinfo
;
3785 int csum_size
= btrfs_super_csum_size(&fs_info
->super_copy
);
3788 * per tree used space accounting can be inaccuracy, so we
3791 spin_lock(&fs_info
->extent_root
->accounting_lock
);
3792 num_bytes
= btrfs_root_used(&fs_info
->extent_root
->root_item
);
3793 spin_unlock(&fs_info
->extent_root
->accounting_lock
);
3795 spin_lock(&fs_info
->csum_root
->accounting_lock
);
3796 num_bytes
+= btrfs_root_used(&fs_info
->csum_root
->root_item
);
3797 spin_unlock(&fs_info
->csum_root
->accounting_lock
);
3799 spin_lock(&fs_info
->tree_root
->accounting_lock
);
3800 num_bytes
+= btrfs_root_used(&fs_info
->tree_root
->root_item
);
3801 spin_unlock(&fs_info
->tree_root
->accounting_lock
);
3803 sinfo
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_DATA
);
3804 spin_lock(&sinfo
->lock
);
3805 data_used
= sinfo
->bytes_used
;
3806 spin_unlock(&sinfo
->lock
);
3808 sinfo
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
3809 spin_lock(&sinfo
->lock
);
3810 if (sinfo
->flags
& BTRFS_BLOCK_GROUP_DATA
)
3812 meta_used
= sinfo
->bytes_used
;
3813 spin_unlock(&sinfo
->lock
);
3815 num_bytes
= (data_used
>> fs_info
->sb
->s_blocksize_bits
) *
3817 num_bytes
+= div64_u64(data_used
+ meta_used
, 50);
3819 if (num_bytes
* 3 > meta_used
)
3820 num_bytes
= div64_u64(meta_used
, 3);
3822 return ALIGN(num_bytes
, fs_info
->extent_root
->leafsize
<< 10);
3825 static void update_global_block_rsv(struct btrfs_fs_info
*fs_info
)
3827 struct btrfs_block_rsv
*block_rsv
= &fs_info
->global_block_rsv
;
3828 struct btrfs_space_info
*sinfo
= block_rsv
->space_info
;
3831 num_bytes
= calc_global_metadata_size(fs_info
);
3833 spin_lock(&block_rsv
->lock
);
3834 spin_lock(&sinfo
->lock
);
3836 block_rsv
->size
= num_bytes
;
3838 num_bytes
= sinfo
->bytes_used
+ sinfo
->bytes_pinned
+
3839 sinfo
->bytes_reserved
+ sinfo
->bytes_readonly
+
3840 sinfo
->bytes_may_use
;
3842 if (sinfo
->total_bytes
> num_bytes
) {
3843 num_bytes
= sinfo
->total_bytes
- num_bytes
;
3844 block_rsv
->reserved
+= num_bytes
;
3845 sinfo
->bytes_reserved
+= num_bytes
;
3848 if (block_rsv
->reserved
>= block_rsv
->size
) {
3849 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
3850 sinfo
->bytes_reserved
-= num_bytes
;
3851 block_rsv
->reserved
= block_rsv
->size
;
3852 block_rsv
->full
= 1;
3855 printk(KERN_INFO
"global block rsv size %llu reserved %llu\n",
3856 block_rsv
->size
, block_rsv
->reserved
);
3858 spin_unlock(&sinfo
->lock
);
3859 spin_unlock(&block_rsv
->lock
);
3862 static void init_global_block_rsv(struct btrfs_fs_info
*fs_info
)
3864 struct btrfs_space_info
*space_info
;
3866 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_SYSTEM
);
3867 fs_info
->chunk_block_rsv
.space_info
= space_info
;
3868 fs_info
->chunk_block_rsv
.priority
= 10;
3870 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
3871 fs_info
->global_block_rsv
.space_info
= space_info
;
3872 fs_info
->global_block_rsv
.priority
= 10;
3873 fs_info
->global_block_rsv
.refill_used
= 1;
3874 fs_info
->delalloc_block_rsv
.space_info
= space_info
;
3875 fs_info
->trans_block_rsv
.space_info
= space_info
;
3876 fs_info
->empty_block_rsv
.space_info
= space_info
;
3877 fs_info
->empty_block_rsv
.priority
= 10;
3879 fs_info
->extent_root
->block_rsv
= &fs_info
->global_block_rsv
;
3880 fs_info
->csum_root
->block_rsv
= &fs_info
->global_block_rsv
;
3881 fs_info
->dev_root
->block_rsv
= &fs_info
->global_block_rsv
;
3882 fs_info
->tree_root
->block_rsv
= &fs_info
->global_block_rsv
;
3883 fs_info
->chunk_root
->block_rsv
= &fs_info
->chunk_block_rsv
;
3885 btrfs_add_durable_block_rsv(fs_info
, &fs_info
->global_block_rsv
);
3887 btrfs_add_durable_block_rsv(fs_info
, &fs_info
->delalloc_block_rsv
);
3889 update_global_block_rsv(fs_info
);
3892 static void release_global_block_rsv(struct btrfs_fs_info
*fs_info
)
3894 block_rsv_release_bytes(&fs_info
->global_block_rsv
, NULL
, (u64
)-1);
3895 WARN_ON(fs_info
->delalloc_block_rsv
.size
> 0);
3896 WARN_ON(fs_info
->delalloc_block_rsv
.reserved
> 0);
3897 WARN_ON(fs_info
->trans_block_rsv
.size
> 0);
3898 WARN_ON(fs_info
->trans_block_rsv
.reserved
> 0);
3899 WARN_ON(fs_info
->chunk_block_rsv
.size
> 0);
3900 WARN_ON(fs_info
->chunk_block_rsv
.reserved
> 0);
3903 static u64
calc_trans_metadata_size(struct btrfs_root
*root
, int num_items
)
3905 return (root
->leafsize
+ root
->nodesize
* (BTRFS_MAX_LEVEL
- 1)) *
3909 int btrfs_trans_reserve_metadata(struct btrfs_trans_handle
*trans
,
3910 struct btrfs_root
*root
,
3916 if (num_items
== 0 || root
->fs_info
->chunk_root
== root
)
3919 num_bytes
= calc_trans_metadata_size(root
, num_items
);
3920 ret
= btrfs_block_rsv_add(trans
, root
, &root
->fs_info
->trans_block_rsv
,
3923 trans
->bytes_reserved
+= num_bytes
;
3924 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
3929 void btrfs_trans_release_metadata(struct btrfs_trans_handle
*trans
,
3930 struct btrfs_root
*root
)
3932 if (!trans
->bytes_reserved
)
3935 BUG_ON(trans
->block_rsv
!= &root
->fs_info
->trans_block_rsv
);
3936 btrfs_block_rsv_release(root
, trans
->block_rsv
,
3937 trans
->bytes_reserved
);
3938 trans
->bytes_reserved
= 0;
3941 int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle
*trans
,
3942 struct inode
*inode
)
3944 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3945 struct btrfs_block_rsv
*src_rsv
= get_block_rsv(trans
, root
);
3946 struct btrfs_block_rsv
*dst_rsv
= root
->orphan_block_rsv
;
3949 * one for deleting orphan item, one for updating inode and
3950 * two for calling btrfs_truncate_inode_items.
3952 * btrfs_truncate_inode_items is a delete operation, it frees
3953 * more space than it uses in most cases. So two units of
3954 * metadata space should be enough for calling it many times.
3955 * If all of the metadata space is used, we can commit
3956 * transaction and use space it freed.
3958 u64 num_bytes
= calc_trans_metadata_size(root
, 4);
3959 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
3962 void btrfs_orphan_release_metadata(struct inode
*inode
)
3964 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3965 u64 num_bytes
= calc_trans_metadata_size(root
, 4);
3966 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
, num_bytes
);
3969 int btrfs_snap_reserve_metadata(struct btrfs_trans_handle
*trans
,
3970 struct btrfs_pending_snapshot
*pending
)
3972 struct btrfs_root
*root
= pending
->root
;
3973 struct btrfs_block_rsv
*src_rsv
= get_block_rsv(trans
, root
);
3974 struct btrfs_block_rsv
*dst_rsv
= &pending
->block_rsv
;
3976 * two for root back/forward refs, two for directory entries
3977 * and one for root of the snapshot.
3979 u64 num_bytes
= calc_trans_metadata_size(root
, 5);
3980 dst_rsv
->space_info
= src_rsv
->space_info
;
3981 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
3984 static u64
calc_csum_metadata_size(struct inode
*inode
, u64 num_bytes
)
3986 return num_bytes
>>= 3;
3989 int btrfs_delalloc_reserve_metadata(struct inode
*inode
, u64 num_bytes
)
3991 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3992 struct btrfs_block_rsv
*block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
3997 if (btrfs_transaction_in_commit(root
->fs_info
))
3998 schedule_timeout(1);
4000 num_bytes
= ALIGN(num_bytes
, root
->sectorsize
);
4002 spin_lock(&BTRFS_I(inode
)->accounting_lock
);
4003 nr_extents
= atomic_read(&BTRFS_I(inode
)->outstanding_extents
) + 1;
4004 if (nr_extents
> BTRFS_I(inode
)->reserved_extents
) {
4005 nr_extents
-= BTRFS_I(inode
)->reserved_extents
;
4006 to_reserve
= calc_trans_metadata_size(root
, nr_extents
);
4011 spin_unlock(&BTRFS_I(inode
)->accounting_lock
);
4013 to_reserve
+= calc_csum_metadata_size(inode
, num_bytes
);
4014 ret
= reserve_metadata_bytes(NULL
, root
, block_rsv
, to_reserve
, 1);
4018 spin_lock(&BTRFS_I(inode
)->accounting_lock
);
4019 BTRFS_I(inode
)->reserved_extents
+= nr_extents
;
4020 atomic_inc(&BTRFS_I(inode
)->outstanding_extents
);
4021 spin_unlock(&BTRFS_I(inode
)->accounting_lock
);
4023 block_rsv_add_bytes(block_rsv
, to_reserve
, 1);
4025 if (block_rsv
->size
> 512 * 1024 * 1024)
4026 shrink_delalloc(NULL
, root
, to_reserve
, 0);
4031 void btrfs_delalloc_release_metadata(struct inode
*inode
, u64 num_bytes
)
4033 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4037 num_bytes
= ALIGN(num_bytes
, root
->sectorsize
);
4038 atomic_dec(&BTRFS_I(inode
)->outstanding_extents
);
4040 spin_lock(&BTRFS_I(inode
)->accounting_lock
);
4041 nr_extents
= atomic_read(&BTRFS_I(inode
)->outstanding_extents
);
4042 if (nr_extents
< BTRFS_I(inode
)->reserved_extents
) {
4043 nr_extents
= BTRFS_I(inode
)->reserved_extents
- nr_extents
;
4044 BTRFS_I(inode
)->reserved_extents
-= nr_extents
;
4048 spin_unlock(&BTRFS_I(inode
)->accounting_lock
);
4050 to_free
= calc_csum_metadata_size(inode
, num_bytes
);
4052 to_free
+= calc_trans_metadata_size(root
, nr_extents
);
4054 btrfs_block_rsv_release(root
, &root
->fs_info
->delalloc_block_rsv
,
4058 int btrfs_delalloc_reserve_space(struct inode
*inode
, u64 num_bytes
)
4062 ret
= btrfs_check_data_free_space(inode
, num_bytes
);
4066 ret
= btrfs_delalloc_reserve_metadata(inode
, num_bytes
);
4068 btrfs_free_reserved_data_space(inode
, num_bytes
);
4075 void btrfs_delalloc_release_space(struct inode
*inode
, u64 num_bytes
)
4077 btrfs_delalloc_release_metadata(inode
, num_bytes
);
4078 btrfs_free_reserved_data_space(inode
, num_bytes
);
4081 static int update_block_group(struct btrfs_trans_handle
*trans
,
4082 struct btrfs_root
*root
,
4083 u64 bytenr
, u64 num_bytes
, int alloc
)
4085 struct btrfs_block_group_cache
*cache
= NULL
;
4086 struct btrfs_fs_info
*info
= root
->fs_info
;
4087 u64 total
= num_bytes
;
4092 /* block accounting for super block */
4093 spin_lock(&info
->delalloc_lock
);
4094 old_val
= btrfs_super_bytes_used(&info
->super_copy
);
4096 old_val
+= num_bytes
;
4098 old_val
-= num_bytes
;
4099 btrfs_set_super_bytes_used(&info
->super_copy
, old_val
);
4100 spin_unlock(&info
->delalloc_lock
);
4103 cache
= btrfs_lookup_block_group(info
, bytenr
);
4106 if (cache
->flags
& (BTRFS_BLOCK_GROUP_DUP
|
4107 BTRFS_BLOCK_GROUP_RAID1
|
4108 BTRFS_BLOCK_GROUP_RAID10
))
4113 * If this block group has free space cache written out, we
4114 * need to make sure to load it if we are removing space. This
4115 * is because we need the unpinning stage to actually add the
4116 * space back to the block group, otherwise we will leak space.
4118 if (!alloc
&& cache
->cached
== BTRFS_CACHE_NO
)
4119 cache_block_group(cache
, trans
, NULL
, 1);
4121 byte_in_group
= bytenr
- cache
->key
.objectid
;
4122 WARN_ON(byte_in_group
> cache
->key
.offset
);
4124 spin_lock(&cache
->space_info
->lock
);
4125 spin_lock(&cache
->lock
);
4127 if (btrfs_super_cache_generation(&info
->super_copy
) != 0 &&
4128 cache
->disk_cache_state
< BTRFS_DC_CLEAR
)
4129 cache
->disk_cache_state
= BTRFS_DC_CLEAR
;
4132 old_val
= btrfs_block_group_used(&cache
->item
);
4133 num_bytes
= min(total
, cache
->key
.offset
- byte_in_group
);
4135 old_val
+= num_bytes
;
4136 btrfs_set_block_group_used(&cache
->item
, old_val
);
4137 cache
->reserved
-= num_bytes
;
4138 cache
->space_info
->bytes_reserved
-= num_bytes
;
4139 cache
->space_info
->bytes_used
+= num_bytes
;
4140 cache
->space_info
->disk_used
+= num_bytes
* factor
;
4141 spin_unlock(&cache
->lock
);
4142 spin_unlock(&cache
->space_info
->lock
);
4144 old_val
-= num_bytes
;
4145 btrfs_set_block_group_used(&cache
->item
, old_val
);
4146 cache
->pinned
+= num_bytes
;
4147 cache
->space_info
->bytes_pinned
+= num_bytes
;
4148 cache
->space_info
->bytes_used
-= num_bytes
;
4149 cache
->space_info
->disk_used
-= num_bytes
* factor
;
4150 spin_unlock(&cache
->lock
);
4151 spin_unlock(&cache
->space_info
->lock
);
4153 set_extent_dirty(info
->pinned_extents
,
4154 bytenr
, bytenr
+ num_bytes
- 1,
4155 GFP_NOFS
| __GFP_NOFAIL
);
4157 btrfs_put_block_group(cache
);
4159 bytenr
+= num_bytes
;
4164 static u64
first_logical_byte(struct btrfs_root
*root
, u64 search_start
)
4166 struct btrfs_block_group_cache
*cache
;
4169 cache
= btrfs_lookup_first_block_group(root
->fs_info
, search_start
);
4173 bytenr
= cache
->key
.objectid
;
4174 btrfs_put_block_group(cache
);
4179 static int pin_down_extent(struct btrfs_root
*root
,
4180 struct btrfs_block_group_cache
*cache
,
4181 u64 bytenr
, u64 num_bytes
, int reserved
)
4183 spin_lock(&cache
->space_info
->lock
);
4184 spin_lock(&cache
->lock
);
4185 cache
->pinned
+= num_bytes
;
4186 cache
->space_info
->bytes_pinned
+= num_bytes
;
4188 cache
->reserved
-= num_bytes
;
4189 cache
->space_info
->bytes_reserved
-= num_bytes
;
4191 spin_unlock(&cache
->lock
);
4192 spin_unlock(&cache
->space_info
->lock
);
4194 set_extent_dirty(root
->fs_info
->pinned_extents
, bytenr
,
4195 bytenr
+ num_bytes
- 1, GFP_NOFS
| __GFP_NOFAIL
);
4200 * this function must be called within transaction
4202 int btrfs_pin_extent(struct btrfs_root
*root
,
4203 u64 bytenr
, u64 num_bytes
, int reserved
)
4205 struct btrfs_block_group_cache
*cache
;
4207 cache
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
4210 pin_down_extent(root
, cache
, bytenr
, num_bytes
, reserved
);
4212 btrfs_put_block_group(cache
);
4217 * update size of reserved extents. this function may return -EAGAIN
4218 * if 'reserve' is true or 'sinfo' is false.
4220 static int update_reserved_bytes(struct btrfs_block_group_cache
*cache
,
4221 u64 num_bytes
, int reserve
, int sinfo
)
4225 struct btrfs_space_info
*space_info
= cache
->space_info
;
4226 spin_lock(&space_info
->lock
);
4227 spin_lock(&cache
->lock
);
4232 cache
->reserved
+= num_bytes
;
4233 space_info
->bytes_reserved
+= num_bytes
;
4237 space_info
->bytes_readonly
+= num_bytes
;
4238 cache
->reserved
-= num_bytes
;
4239 space_info
->bytes_reserved
-= num_bytes
;
4241 spin_unlock(&cache
->lock
);
4242 spin_unlock(&space_info
->lock
);
4244 spin_lock(&cache
->lock
);
4249 cache
->reserved
+= num_bytes
;
4251 cache
->reserved
-= num_bytes
;
4253 spin_unlock(&cache
->lock
);
4258 int btrfs_prepare_extent_commit(struct btrfs_trans_handle
*trans
,
4259 struct btrfs_root
*root
)
4261 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4262 struct btrfs_caching_control
*next
;
4263 struct btrfs_caching_control
*caching_ctl
;
4264 struct btrfs_block_group_cache
*cache
;
4266 down_write(&fs_info
->extent_commit_sem
);
4268 list_for_each_entry_safe(caching_ctl
, next
,
4269 &fs_info
->caching_block_groups
, list
) {
4270 cache
= caching_ctl
->block_group
;
4271 if (block_group_cache_done(cache
)) {
4272 cache
->last_byte_to_unpin
= (u64
)-1;
4273 list_del_init(&caching_ctl
->list
);
4274 put_caching_control(caching_ctl
);
4276 cache
->last_byte_to_unpin
= caching_ctl
->progress
;
4280 if (fs_info
->pinned_extents
== &fs_info
->freed_extents
[0])
4281 fs_info
->pinned_extents
= &fs_info
->freed_extents
[1];
4283 fs_info
->pinned_extents
= &fs_info
->freed_extents
[0];
4285 up_write(&fs_info
->extent_commit_sem
);
4287 update_global_block_rsv(fs_info
);
4291 static int unpin_extent_range(struct btrfs_root
*root
, u64 start
, u64 end
)
4293 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4294 struct btrfs_block_group_cache
*cache
= NULL
;
4297 while (start
<= end
) {
4299 start
>= cache
->key
.objectid
+ cache
->key
.offset
) {
4301 btrfs_put_block_group(cache
);
4302 cache
= btrfs_lookup_block_group(fs_info
, start
);
4306 len
= cache
->key
.objectid
+ cache
->key
.offset
- start
;
4307 len
= min(len
, end
+ 1 - start
);
4309 if (start
< cache
->last_byte_to_unpin
) {
4310 len
= min(len
, cache
->last_byte_to_unpin
- start
);
4311 btrfs_add_free_space(cache
, start
, len
);
4316 spin_lock(&cache
->space_info
->lock
);
4317 spin_lock(&cache
->lock
);
4318 cache
->pinned
-= len
;
4319 cache
->space_info
->bytes_pinned
-= len
;
4321 cache
->space_info
->bytes_readonly
+= len
;
4322 } else if (cache
->reserved_pinned
> 0) {
4323 len
= min(len
, cache
->reserved_pinned
);
4324 cache
->reserved_pinned
-= len
;
4325 cache
->space_info
->bytes_reserved
+= len
;
4327 spin_unlock(&cache
->lock
);
4328 spin_unlock(&cache
->space_info
->lock
);
4332 btrfs_put_block_group(cache
);
4336 int btrfs_finish_extent_commit(struct btrfs_trans_handle
*trans
,
4337 struct btrfs_root
*root
)
4339 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4340 struct extent_io_tree
*unpin
;
4341 struct btrfs_block_rsv
*block_rsv
;
4342 struct btrfs_block_rsv
*next_rsv
;
4348 if (fs_info
->pinned_extents
== &fs_info
->freed_extents
[0])
4349 unpin
= &fs_info
->freed_extents
[1];
4351 unpin
= &fs_info
->freed_extents
[0];
4354 ret
= find_first_extent_bit(unpin
, 0, &start
, &end
,
4359 ret
= btrfs_discard_extent(root
, start
, end
+ 1 - start
);
4361 clear_extent_dirty(unpin
, start
, end
, GFP_NOFS
);
4362 unpin_extent_range(root
, start
, end
);
4366 mutex_lock(&fs_info
->durable_block_rsv_mutex
);
4367 list_for_each_entry_safe(block_rsv
, next_rsv
,
4368 &fs_info
->durable_block_rsv_list
, list
) {
4370 idx
= trans
->transid
& 0x1;
4371 if (block_rsv
->freed
[idx
] > 0) {
4372 block_rsv_add_bytes(block_rsv
,
4373 block_rsv
->freed
[idx
], 0);
4374 block_rsv
->freed
[idx
] = 0;
4376 if (atomic_read(&block_rsv
->usage
) == 0) {
4377 btrfs_block_rsv_release(root
, block_rsv
, (u64
)-1);
4379 if (block_rsv
->freed
[0] == 0 &&
4380 block_rsv
->freed
[1] == 0) {
4381 list_del_init(&block_rsv
->list
);
4385 btrfs_block_rsv_release(root
, block_rsv
, 0);
4388 mutex_unlock(&fs_info
->durable_block_rsv_mutex
);
4393 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
4394 struct btrfs_root
*root
,
4395 u64 bytenr
, u64 num_bytes
, u64 parent
,
4396 u64 root_objectid
, u64 owner_objectid
,
4397 u64 owner_offset
, int refs_to_drop
,
4398 struct btrfs_delayed_extent_op
*extent_op
)
4400 struct btrfs_key key
;
4401 struct btrfs_path
*path
;
4402 struct btrfs_fs_info
*info
= root
->fs_info
;
4403 struct btrfs_root
*extent_root
= info
->extent_root
;
4404 struct extent_buffer
*leaf
;
4405 struct btrfs_extent_item
*ei
;
4406 struct btrfs_extent_inline_ref
*iref
;
4409 int extent_slot
= 0;
4410 int found_extent
= 0;
4415 path
= btrfs_alloc_path();
4420 path
->leave_spinning
= 1;
4422 is_data
= owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
;
4423 BUG_ON(!is_data
&& refs_to_drop
!= 1);
4425 ret
= lookup_extent_backref(trans
, extent_root
, path
, &iref
,
4426 bytenr
, num_bytes
, parent
,
4427 root_objectid
, owner_objectid
,
4430 extent_slot
= path
->slots
[0];
4431 while (extent_slot
>= 0) {
4432 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
4434 if (key
.objectid
!= bytenr
)
4436 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
4437 key
.offset
== num_bytes
) {
4441 if (path
->slots
[0] - extent_slot
> 5)
4445 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4446 item_size
= btrfs_item_size_nr(path
->nodes
[0], extent_slot
);
4447 if (found_extent
&& item_size
< sizeof(*ei
))
4450 if (!found_extent
) {
4452 ret
= remove_extent_backref(trans
, extent_root
, path
,
4456 btrfs_release_path(extent_root
, path
);
4457 path
->leave_spinning
= 1;
4459 key
.objectid
= bytenr
;
4460 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
4461 key
.offset
= num_bytes
;
4463 ret
= btrfs_search_slot(trans
, extent_root
,
4466 printk(KERN_ERR
"umm, got %d back from search"
4467 ", was looking for %llu\n", ret
,
4468 (unsigned long long)bytenr
);
4469 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
4472 extent_slot
= path
->slots
[0];
4475 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
4477 printk(KERN_ERR
"btrfs unable to find ref byte nr %llu "
4478 "parent %llu root %llu owner %llu offset %llu\n",
4479 (unsigned long long)bytenr
,
4480 (unsigned long long)parent
,
4481 (unsigned long long)root_objectid
,
4482 (unsigned long long)owner_objectid
,
4483 (unsigned long long)owner_offset
);
4486 leaf
= path
->nodes
[0];
4487 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
4488 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4489 if (item_size
< sizeof(*ei
)) {
4490 BUG_ON(found_extent
|| extent_slot
!= path
->slots
[0]);
4491 ret
= convert_extent_item_v0(trans
, extent_root
, path
,
4495 btrfs_release_path(extent_root
, path
);
4496 path
->leave_spinning
= 1;
4498 key
.objectid
= bytenr
;
4499 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
4500 key
.offset
= num_bytes
;
4502 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
,
4505 printk(KERN_ERR
"umm, got %d back from search"
4506 ", was looking for %llu\n", ret
,
4507 (unsigned long long)bytenr
);
4508 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
4511 extent_slot
= path
->slots
[0];
4512 leaf
= path
->nodes
[0];
4513 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
4516 BUG_ON(item_size
< sizeof(*ei
));
4517 ei
= btrfs_item_ptr(leaf
, extent_slot
,
4518 struct btrfs_extent_item
);
4519 if (owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
) {
4520 struct btrfs_tree_block_info
*bi
;
4521 BUG_ON(item_size
< sizeof(*ei
) + sizeof(*bi
));
4522 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
4523 WARN_ON(owner_objectid
!= btrfs_tree_block_level(leaf
, bi
));
4526 refs
= btrfs_extent_refs(leaf
, ei
);
4527 BUG_ON(refs
< refs_to_drop
);
4528 refs
-= refs_to_drop
;
4532 __run_delayed_extent_op(extent_op
, leaf
, ei
);
4534 * In the case of inline back ref, reference count will
4535 * be updated by remove_extent_backref
4538 BUG_ON(!found_extent
);
4540 btrfs_set_extent_refs(leaf
, ei
, refs
);
4541 btrfs_mark_buffer_dirty(leaf
);
4544 ret
= remove_extent_backref(trans
, extent_root
, path
,
4551 BUG_ON(is_data
&& refs_to_drop
!=
4552 extent_data_ref_count(root
, path
, iref
));
4554 BUG_ON(path
->slots
[0] != extent_slot
);
4556 BUG_ON(path
->slots
[0] != extent_slot
+ 1);
4557 path
->slots
[0] = extent_slot
;
4562 ret
= btrfs_del_items(trans
, extent_root
, path
, path
->slots
[0],
4565 btrfs_release_path(extent_root
, path
);
4568 ret
= btrfs_del_csums(trans
, root
, bytenr
, num_bytes
);
4571 invalidate_mapping_pages(info
->btree_inode
->i_mapping
,
4572 bytenr
>> PAGE_CACHE_SHIFT
,
4573 (bytenr
+ num_bytes
- 1) >> PAGE_CACHE_SHIFT
);
4576 ret
= update_block_group(trans
, root
, bytenr
, num_bytes
, 0);
4579 btrfs_free_path(path
);
4584 * when we free an block, it is possible (and likely) that we free the last
4585 * delayed ref for that extent as well. This searches the delayed ref tree for
4586 * a given extent, and if there are no other delayed refs to be processed, it
4587 * removes it from the tree.
4589 static noinline
int check_ref_cleanup(struct btrfs_trans_handle
*trans
,
4590 struct btrfs_root
*root
, u64 bytenr
)
4592 struct btrfs_delayed_ref_head
*head
;
4593 struct btrfs_delayed_ref_root
*delayed_refs
;
4594 struct btrfs_delayed_ref_node
*ref
;
4595 struct rb_node
*node
;
4598 delayed_refs
= &trans
->transaction
->delayed_refs
;
4599 spin_lock(&delayed_refs
->lock
);
4600 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
4604 node
= rb_prev(&head
->node
.rb_node
);
4608 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
4610 /* there are still entries for this ref, we can't drop it */
4611 if (ref
->bytenr
== bytenr
)
4614 if (head
->extent_op
) {
4615 if (!head
->must_insert_reserved
)
4617 kfree(head
->extent_op
);
4618 head
->extent_op
= NULL
;
4622 * waiting for the lock here would deadlock. If someone else has it
4623 * locked they are already in the process of dropping it anyway
4625 if (!mutex_trylock(&head
->mutex
))
4629 * at this point we have a head with no other entries. Go
4630 * ahead and process it.
4632 head
->node
.in_tree
= 0;
4633 rb_erase(&head
->node
.rb_node
, &delayed_refs
->root
);
4635 delayed_refs
->num_entries
--;
4638 * we don't take a ref on the node because we're removing it from the
4639 * tree, so we just steal the ref the tree was holding.
4641 delayed_refs
->num_heads
--;
4642 if (list_empty(&head
->cluster
))
4643 delayed_refs
->num_heads_ready
--;
4645 list_del_init(&head
->cluster
);
4646 spin_unlock(&delayed_refs
->lock
);
4648 BUG_ON(head
->extent_op
);
4649 if (head
->must_insert_reserved
)
4652 mutex_unlock(&head
->mutex
);
4653 btrfs_put_delayed_ref(&head
->node
);
4656 spin_unlock(&delayed_refs
->lock
);
4660 void btrfs_free_tree_block(struct btrfs_trans_handle
*trans
,
4661 struct btrfs_root
*root
,
4662 struct extent_buffer
*buf
,
4663 u64 parent
, int last_ref
)
4665 struct btrfs_block_rsv
*block_rsv
;
4666 struct btrfs_block_group_cache
*cache
= NULL
;
4669 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
4670 ret
= btrfs_add_delayed_tree_ref(trans
, buf
->start
, buf
->len
,
4671 parent
, root
->root_key
.objectid
,
4672 btrfs_header_level(buf
),
4673 BTRFS_DROP_DELAYED_REF
, NULL
);
4680 block_rsv
= get_block_rsv(trans
, root
);
4681 cache
= btrfs_lookup_block_group(root
->fs_info
, buf
->start
);
4682 if (block_rsv
->space_info
!= cache
->space_info
)
4685 if (btrfs_header_generation(buf
) == trans
->transid
) {
4686 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
4687 ret
= check_ref_cleanup(trans
, root
, buf
->start
);
4692 if (btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
)) {
4693 pin_down_extent(root
, cache
, buf
->start
, buf
->len
, 1);
4697 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY
, &buf
->bflags
));
4699 btrfs_add_free_space(cache
, buf
->start
, buf
->len
);
4700 ret
= update_reserved_bytes(cache
, buf
->len
, 0, 0);
4701 if (ret
== -EAGAIN
) {
4702 /* block group became read-only */
4703 update_reserved_bytes(cache
, buf
->len
, 0, 1);
4708 spin_lock(&block_rsv
->lock
);
4709 if (block_rsv
->reserved
< block_rsv
->size
) {
4710 block_rsv
->reserved
+= buf
->len
;
4713 spin_unlock(&block_rsv
->lock
);
4716 spin_lock(&cache
->space_info
->lock
);
4717 cache
->space_info
->bytes_reserved
-= buf
->len
;
4718 spin_unlock(&cache
->space_info
->lock
);
4723 if (block_rsv
->durable
&& !cache
->ro
) {
4725 spin_lock(&cache
->lock
);
4727 cache
->reserved_pinned
+= buf
->len
;
4730 spin_unlock(&cache
->lock
);
4733 spin_lock(&block_rsv
->lock
);
4734 block_rsv
->freed
[trans
->transid
& 0x1] += buf
->len
;
4735 spin_unlock(&block_rsv
->lock
);
4739 btrfs_put_block_group(cache
);
4742 int btrfs_free_extent(struct btrfs_trans_handle
*trans
,
4743 struct btrfs_root
*root
,
4744 u64 bytenr
, u64 num_bytes
, u64 parent
,
4745 u64 root_objectid
, u64 owner
, u64 offset
)
4750 * tree log blocks never actually go into the extent allocation
4751 * tree, just update pinning info and exit early.
4753 if (root_objectid
== BTRFS_TREE_LOG_OBJECTID
) {
4754 WARN_ON(owner
>= BTRFS_FIRST_FREE_OBJECTID
);
4755 /* unlocks the pinned mutex */
4756 btrfs_pin_extent(root
, bytenr
, num_bytes
, 1);
4758 } else if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
4759 ret
= btrfs_add_delayed_tree_ref(trans
, bytenr
, num_bytes
,
4760 parent
, root_objectid
, (int)owner
,
4761 BTRFS_DROP_DELAYED_REF
, NULL
);
4764 ret
= btrfs_add_delayed_data_ref(trans
, bytenr
, num_bytes
,
4765 parent
, root_objectid
, owner
,
4766 offset
, BTRFS_DROP_DELAYED_REF
, NULL
);
4772 static u64
stripe_align(struct btrfs_root
*root
, u64 val
)
4774 u64 mask
= ((u64
)root
->stripesize
- 1);
4775 u64 ret
= (val
+ mask
) & ~mask
;
4780 * when we wait for progress in the block group caching, its because
4781 * our allocation attempt failed at least once. So, we must sleep
4782 * and let some progress happen before we try again.
4784 * This function will sleep at least once waiting for new free space to
4785 * show up, and then it will check the block group free space numbers
4786 * for our min num_bytes. Another option is to have it go ahead
4787 * and look in the rbtree for a free extent of a given size, but this
4791 wait_block_group_cache_progress(struct btrfs_block_group_cache
*cache
,
4794 struct btrfs_caching_control
*caching_ctl
;
4797 caching_ctl
= get_caching_control(cache
);
4801 wait_event(caching_ctl
->wait
, block_group_cache_done(cache
) ||
4802 (cache
->free_space
>= num_bytes
));
4804 put_caching_control(caching_ctl
);
4809 wait_block_group_cache_done(struct btrfs_block_group_cache
*cache
)
4811 struct btrfs_caching_control
*caching_ctl
;
4814 caching_ctl
= get_caching_control(cache
);
4818 wait_event(caching_ctl
->wait
, block_group_cache_done(cache
));
4820 put_caching_control(caching_ctl
);
4824 static int get_block_group_index(struct btrfs_block_group_cache
*cache
)
4827 if (cache
->flags
& BTRFS_BLOCK_GROUP_RAID10
)
4829 else if (cache
->flags
& BTRFS_BLOCK_GROUP_RAID1
)
4831 else if (cache
->flags
& BTRFS_BLOCK_GROUP_DUP
)
4833 else if (cache
->flags
& BTRFS_BLOCK_GROUP_RAID0
)
4840 enum btrfs_loop_type
{
4841 LOOP_FIND_IDEAL
= 0,
4842 LOOP_CACHING_NOWAIT
= 1,
4843 LOOP_CACHING_WAIT
= 2,
4844 LOOP_ALLOC_CHUNK
= 3,
4845 LOOP_NO_EMPTY_SIZE
= 4,
4849 * walks the btree of allocated extents and find a hole of a given size.
4850 * The key ins is changed to record the hole:
4851 * ins->objectid == block start
4852 * ins->flags = BTRFS_EXTENT_ITEM_KEY
4853 * ins->offset == number of blocks
4854 * Any available blocks before search_start are skipped.
4856 static noinline
int find_free_extent(struct btrfs_trans_handle
*trans
,
4857 struct btrfs_root
*orig_root
,
4858 u64 num_bytes
, u64 empty_size
,
4859 u64 search_start
, u64 search_end
,
4860 u64 hint_byte
, struct btrfs_key
*ins
,
4864 struct btrfs_root
*root
= orig_root
->fs_info
->extent_root
;
4865 struct btrfs_free_cluster
*last_ptr
= NULL
;
4866 struct btrfs_block_group_cache
*block_group
= NULL
;
4867 int empty_cluster
= 2 * 1024 * 1024;
4868 int allowed_chunk_alloc
= 0;
4869 int done_chunk_alloc
= 0;
4870 struct btrfs_space_info
*space_info
;
4871 int last_ptr_loop
= 0;
4874 bool found_uncached_bg
= false;
4875 bool failed_cluster_refill
= false;
4876 bool failed_alloc
= false;
4877 bool use_cluster
= true;
4878 u64 ideal_cache_percent
= 0;
4879 u64 ideal_cache_offset
= 0;
4881 WARN_ON(num_bytes
< root
->sectorsize
);
4882 btrfs_set_key_type(ins
, BTRFS_EXTENT_ITEM_KEY
);
4886 space_info
= __find_space_info(root
->fs_info
, data
);
4888 printk(KERN_ERR
"No space info for %d\n", data
);
4893 * If the space info is for both data and metadata it means we have a
4894 * small filesystem and we can't use the clustering stuff.
4896 if (btrfs_mixed_space_info(space_info
))
4897 use_cluster
= false;
4899 if (orig_root
->ref_cows
|| empty_size
)
4900 allowed_chunk_alloc
= 1;
4902 if (data
& BTRFS_BLOCK_GROUP_METADATA
&& use_cluster
) {
4903 last_ptr
= &root
->fs_info
->meta_alloc_cluster
;
4904 if (!btrfs_test_opt(root
, SSD
))
4905 empty_cluster
= 64 * 1024;
4908 if ((data
& BTRFS_BLOCK_GROUP_DATA
) && use_cluster
&&
4909 btrfs_test_opt(root
, SSD
)) {
4910 last_ptr
= &root
->fs_info
->data_alloc_cluster
;
4914 spin_lock(&last_ptr
->lock
);
4915 if (last_ptr
->block_group
)
4916 hint_byte
= last_ptr
->window_start
;
4917 spin_unlock(&last_ptr
->lock
);
4920 search_start
= max(search_start
, first_logical_byte(root
, 0));
4921 search_start
= max(search_start
, hint_byte
);
4926 if (search_start
== hint_byte
) {
4928 block_group
= btrfs_lookup_block_group(root
->fs_info
,
4931 * we don't want to use the block group if it doesn't match our
4932 * allocation bits, or if its not cached.
4934 * However if we are re-searching with an ideal block group
4935 * picked out then we don't care that the block group is cached.
4937 if (block_group
&& block_group_bits(block_group
, data
) &&
4938 (block_group
->cached
!= BTRFS_CACHE_NO
||
4939 search_start
== ideal_cache_offset
)) {
4940 down_read(&space_info
->groups_sem
);
4941 if (list_empty(&block_group
->list
) ||
4944 * someone is removing this block group,
4945 * we can't jump into the have_block_group
4946 * target because our list pointers are not
4949 btrfs_put_block_group(block_group
);
4950 up_read(&space_info
->groups_sem
);
4952 index
= get_block_group_index(block_group
);
4953 goto have_block_group
;
4955 } else if (block_group
) {
4956 btrfs_put_block_group(block_group
);
4960 down_read(&space_info
->groups_sem
);
4961 list_for_each_entry(block_group
, &space_info
->block_groups
[index
],
4966 btrfs_get_block_group(block_group
);
4967 search_start
= block_group
->key
.objectid
;
4970 * this can happen if we end up cycling through all the
4971 * raid types, but we want to make sure we only allocate
4972 * for the proper type.
4974 if (!block_group_bits(block_group
, data
)) {
4975 u64 extra
= BTRFS_BLOCK_GROUP_DUP
|
4976 BTRFS_BLOCK_GROUP_RAID1
|
4977 BTRFS_BLOCK_GROUP_RAID10
;
4980 * if they asked for extra copies and this block group
4981 * doesn't provide them, bail. This does allow us to
4982 * fill raid0 from raid1.
4984 if ((data
& extra
) && !(block_group
->flags
& extra
))
4989 if (unlikely(block_group
->cached
== BTRFS_CACHE_NO
)) {
4992 ret
= cache_block_group(block_group
, trans
,
4994 if (block_group
->cached
== BTRFS_CACHE_FINISHED
)
4995 goto have_block_group
;
4997 free_percent
= btrfs_block_group_used(&block_group
->item
);
4998 free_percent
*= 100;
4999 free_percent
= div64_u64(free_percent
,
5000 block_group
->key
.offset
);
5001 free_percent
= 100 - free_percent
;
5002 if (free_percent
> ideal_cache_percent
&&
5003 likely(!block_group
->ro
)) {
5004 ideal_cache_offset
= block_group
->key
.objectid
;
5005 ideal_cache_percent
= free_percent
;
5009 * We only want to start kthread caching if we are at
5010 * the point where we will wait for caching to make
5011 * progress, or if our ideal search is over and we've
5012 * found somebody to start caching.
5014 if (loop
> LOOP_CACHING_NOWAIT
||
5015 (loop
> LOOP_FIND_IDEAL
&&
5016 atomic_read(&space_info
->caching_threads
) < 2)) {
5017 ret
= cache_block_group(block_group
, trans
,
5021 found_uncached_bg
= true;
5024 * If loop is set for cached only, try the next block
5027 if (loop
== LOOP_FIND_IDEAL
)
5031 cached
= block_group_cache_done(block_group
);
5032 if (unlikely(!cached
))
5033 found_uncached_bg
= true;
5035 if (unlikely(block_group
->ro
))
5039 * Ok we want to try and use the cluster allocator, so lets look
5040 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
5041 * have tried the cluster allocator plenty of times at this
5042 * point and not have found anything, so we are likely way too
5043 * fragmented for the clustering stuff to find anything, so lets
5044 * just skip it and let the allocator find whatever block it can
5047 if (last_ptr
&& loop
< LOOP_NO_EMPTY_SIZE
) {
5049 * the refill lock keeps out other
5050 * people trying to start a new cluster
5052 spin_lock(&last_ptr
->refill_lock
);
5053 if (last_ptr
->block_group
&&
5054 (last_ptr
->block_group
->ro
||
5055 !block_group_bits(last_ptr
->block_group
, data
))) {
5057 goto refill_cluster
;
5060 offset
= btrfs_alloc_from_cluster(block_group
, last_ptr
,
5061 num_bytes
, search_start
);
5063 /* we have a block, we're done */
5064 spin_unlock(&last_ptr
->refill_lock
);
5068 spin_lock(&last_ptr
->lock
);
5070 * whoops, this cluster doesn't actually point to
5071 * this block group. Get a ref on the block
5072 * group is does point to and try again
5074 if (!last_ptr_loop
&& last_ptr
->block_group
&&
5075 last_ptr
->block_group
!= block_group
) {
5077 btrfs_put_block_group(block_group
);
5078 block_group
= last_ptr
->block_group
;
5079 btrfs_get_block_group(block_group
);
5080 spin_unlock(&last_ptr
->lock
);
5081 spin_unlock(&last_ptr
->refill_lock
);
5084 search_start
= block_group
->key
.objectid
;
5086 * we know this block group is properly
5087 * in the list because
5088 * btrfs_remove_block_group, drops the
5089 * cluster before it removes the block
5090 * group from the list
5092 goto have_block_group
;
5094 spin_unlock(&last_ptr
->lock
);
5097 * this cluster didn't work out, free it and
5100 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
5104 /* allocate a cluster in this block group */
5105 ret
= btrfs_find_space_cluster(trans
, root
,
5106 block_group
, last_ptr
,
5108 empty_cluster
+ empty_size
);
5111 * now pull our allocation out of this
5114 offset
= btrfs_alloc_from_cluster(block_group
,
5115 last_ptr
, num_bytes
,
5118 /* we found one, proceed */
5119 spin_unlock(&last_ptr
->refill_lock
);
5122 } else if (!cached
&& loop
> LOOP_CACHING_NOWAIT
5123 && !failed_cluster_refill
) {
5124 spin_unlock(&last_ptr
->refill_lock
);
5126 failed_cluster_refill
= true;
5127 wait_block_group_cache_progress(block_group
,
5128 num_bytes
+ empty_cluster
+ empty_size
);
5129 goto have_block_group
;
5133 * at this point we either didn't find a cluster
5134 * or we weren't able to allocate a block from our
5135 * cluster. Free the cluster we've been trying
5136 * to use, and go to the next block group
5138 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
5139 spin_unlock(&last_ptr
->refill_lock
);
5143 offset
= btrfs_find_space_for_alloc(block_group
, search_start
,
5144 num_bytes
, empty_size
);
5146 * If we didn't find a chunk, and we haven't failed on this
5147 * block group before, and this block group is in the middle of
5148 * caching and we are ok with waiting, then go ahead and wait
5149 * for progress to be made, and set failed_alloc to true.
5151 * If failed_alloc is true then we've already waited on this
5152 * block group once and should move on to the next block group.
5154 if (!offset
&& !failed_alloc
&& !cached
&&
5155 loop
> LOOP_CACHING_NOWAIT
) {
5156 wait_block_group_cache_progress(block_group
,
5157 num_bytes
+ empty_size
);
5158 failed_alloc
= true;
5159 goto have_block_group
;
5160 } else if (!offset
) {
5164 search_start
= stripe_align(root
, offset
);
5165 /* move on to the next group */
5166 if (search_start
+ num_bytes
>= search_end
) {
5167 btrfs_add_free_space(block_group
, offset
, num_bytes
);
5171 /* move on to the next group */
5172 if (search_start
+ num_bytes
>
5173 block_group
->key
.objectid
+ block_group
->key
.offset
) {
5174 btrfs_add_free_space(block_group
, offset
, num_bytes
);
5178 ins
->objectid
= search_start
;
5179 ins
->offset
= num_bytes
;
5181 if (offset
< search_start
)
5182 btrfs_add_free_space(block_group
, offset
,
5183 search_start
- offset
);
5184 BUG_ON(offset
> search_start
);
5186 ret
= update_reserved_bytes(block_group
, num_bytes
, 1,
5187 (data
& BTRFS_BLOCK_GROUP_DATA
));
5188 if (ret
== -EAGAIN
) {
5189 btrfs_add_free_space(block_group
, offset
, num_bytes
);
5193 /* we are all good, lets return */
5194 ins
->objectid
= search_start
;
5195 ins
->offset
= num_bytes
;
5197 if (offset
< search_start
)
5198 btrfs_add_free_space(block_group
, offset
,
5199 search_start
- offset
);
5200 BUG_ON(offset
> search_start
);
5203 failed_cluster_refill
= false;
5204 failed_alloc
= false;
5205 BUG_ON(index
!= get_block_group_index(block_group
));
5206 btrfs_put_block_group(block_group
);
5208 up_read(&space_info
->groups_sem
);
5210 if (!ins
->objectid
&& ++index
< BTRFS_NR_RAID_TYPES
)
5213 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5214 * for them to make caching progress. Also
5215 * determine the best possible bg to cache
5216 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5217 * caching kthreads as we move along
5218 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5219 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5220 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5223 if (!ins
->objectid
&& loop
< LOOP_NO_EMPTY_SIZE
&&
5224 (found_uncached_bg
|| empty_size
|| empty_cluster
||
5225 allowed_chunk_alloc
)) {
5227 if (loop
== LOOP_FIND_IDEAL
&& found_uncached_bg
) {
5228 found_uncached_bg
= false;
5230 if (!ideal_cache_percent
&&
5231 atomic_read(&space_info
->caching_threads
))
5235 * 1 of the following 2 things have happened so far
5237 * 1) We found an ideal block group for caching that
5238 * is mostly full and will cache quickly, so we might
5239 * as well wait for it.
5241 * 2) We searched for cached only and we didn't find
5242 * anything, and we didn't start any caching kthreads
5243 * either, so chances are we will loop through and
5244 * start a couple caching kthreads, and then come back
5245 * around and just wait for them. This will be slower
5246 * because we will have 2 caching kthreads reading at
5247 * the same time when we could have just started one
5248 * and waited for it to get far enough to give us an
5249 * allocation, so go ahead and go to the wait caching
5252 loop
= LOOP_CACHING_WAIT
;
5253 search_start
= ideal_cache_offset
;
5254 ideal_cache_percent
= 0;
5256 } else if (loop
== LOOP_FIND_IDEAL
) {
5258 * Didn't find a uncached bg, wait on anything we find
5261 loop
= LOOP_CACHING_WAIT
;
5265 if (loop
< LOOP_CACHING_WAIT
) {
5270 if (loop
== LOOP_ALLOC_CHUNK
) {
5275 if (allowed_chunk_alloc
) {
5276 ret
= do_chunk_alloc(trans
, root
, num_bytes
+
5277 2 * 1024 * 1024, data
, 1);
5278 allowed_chunk_alloc
= 0;
5279 done_chunk_alloc
= 1;
5280 } else if (!done_chunk_alloc
) {
5281 space_info
->force_alloc
= 1;
5284 if (loop
< LOOP_NO_EMPTY_SIZE
) {
5289 } else if (!ins
->objectid
) {
5293 /* we found what we needed */
5294 if (ins
->objectid
) {
5295 if (!(data
& BTRFS_BLOCK_GROUP_DATA
))
5296 trans
->block_group
= block_group
->key
.objectid
;
5298 btrfs_put_block_group(block_group
);
5305 static void dump_space_info(struct btrfs_space_info
*info
, u64 bytes
,
5306 int dump_block_groups
)
5308 struct btrfs_block_group_cache
*cache
;
5311 spin_lock(&info
->lock
);
5312 printk(KERN_INFO
"space_info has %llu free, is %sfull\n",
5313 (unsigned long long)(info
->total_bytes
- info
->bytes_used
-
5314 info
->bytes_pinned
- info
->bytes_reserved
-
5315 info
->bytes_readonly
),
5316 (info
->full
) ? "" : "not ");
5317 printk(KERN_INFO
"space_info total=%llu, used=%llu, pinned=%llu, "
5318 "reserved=%llu, may_use=%llu, readonly=%llu\n",
5319 (unsigned long long)info
->total_bytes
,
5320 (unsigned long long)info
->bytes_used
,
5321 (unsigned long long)info
->bytes_pinned
,
5322 (unsigned long long)info
->bytes_reserved
,
5323 (unsigned long long)info
->bytes_may_use
,
5324 (unsigned long long)info
->bytes_readonly
);
5325 spin_unlock(&info
->lock
);
5327 if (!dump_block_groups
)
5330 down_read(&info
->groups_sem
);
5332 list_for_each_entry(cache
, &info
->block_groups
[index
], list
) {
5333 spin_lock(&cache
->lock
);
5334 printk(KERN_INFO
"block group %llu has %llu bytes, %llu used "
5335 "%llu pinned %llu reserved\n",
5336 (unsigned long long)cache
->key
.objectid
,
5337 (unsigned long long)cache
->key
.offset
,
5338 (unsigned long long)btrfs_block_group_used(&cache
->item
),
5339 (unsigned long long)cache
->pinned
,
5340 (unsigned long long)cache
->reserved
);
5341 btrfs_dump_free_space(cache
, bytes
);
5342 spin_unlock(&cache
->lock
);
5344 if (++index
< BTRFS_NR_RAID_TYPES
)
5346 up_read(&info
->groups_sem
);
5349 int btrfs_reserve_extent(struct btrfs_trans_handle
*trans
,
5350 struct btrfs_root
*root
,
5351 u64 num_bytes
, u64 min_alloc_size
,
5352 u64 empty_size
, u64 hint_byte
,
5353 u64 search_end
, struct btrfs_key
*ins
,
5357 u64 search_start
= 0;
5359 data
= btrfs_get_alloc_profile(root
, data
);
5362 * the only place that sets empty_size is btrfs_realloc_node, which
5363 * is not called recursively on allocations
5365 if (empty_size
|| root
->ref_cows
)
5366 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
5367 num_bytes
+ 2 * 1024 * 1024, data
, 0);
5369 WARN_ON(num_bytes
< root
->sectorsize
);
5370 ret
= find_free_extent(trans
, root
, num_bytes
, empty_size
,
5371 search_start
, search_end
, hint_byte
,
5374 if (ret
== -ENOSPC
&& num_bytes
> min_alloc_size
) {
5375 num_bytes
= num_bytes
>> 1;
5376 num_bytes
= num_bytes
& ~(root
->sectorsize
- 1);
5377 num_bytes
= max(num_bytes
, min_alloc_size
);
5378 do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
5379 num_bytes
, data
, 1);
5382 if (ret
== -ENOSPC
) {
5383 struct btrfs_space_info
*sinfo
;
5385 sinfo
= __find_space_info(root
->fs_info
, data
);
5386 printk(KERN_ERR
"btrfs allocation failed flags %llu, "
5387 "wanted %llu\n", (unsigned long long)data
,
5388 (unsigned long long)num_bytes
);
5389 dump_space_info(sinfo
, num_bytes
, 1);
5395 int btrfs_free_reserved_extent(struct btrfs_root
*root
, u64 start
, u64 len
)
5397 struct btrfs_block_group_cache
*cache
;
5400 cache
= btrfs_lookup_block_group(root
->fs_info
, start
);
5402 printk(KERN_ERR
"Unable to find block group for %llu\n",
5403 (unsigned long long)start
);
5407 ret
= btrfs_discard_extent(root
, start
, len
);
5409 btrfs_add_free_space(cache
, start
, len
);
5410 update_reserved_bytes(cache
, len
, 0, 1);
5411 btrfs_put_block_group(cache
);
5416 static int alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
5417 struct btrfs_root
*root
,
5418 u64 parent
, u64 root_objectid
,
5419 u64 flags
, u64 owner
, u64 offset
,
5420 struct btrfs_key
*ins
, int ref_mod
)
5423 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5424 struct btrfs_extent_item
*extent_item
;
5425 struct btrfs_extent_inline_ref
*iref
;
5426 struct btrfs_path
*path
;
5427 struct extent_buffer
*leaf
;
5432 type
= BTRFS_SHARED_DATA_REF_KEY
;
5434 type
= BTRFS_EXTENT_DATA_REF_KEY
;
5436 size
= sizeof(*extent_item
) + btrfs_extent_inline_ref_size(type
);
5438 path
= btrfs_alloc_path();
5441 path
->leave_spinning
= 1;
5442 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
5446 leaf
= path
->nodes
[0];
5447 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
5448 struct btrfs_extent_item
);
5449 btrfs_set_extent_refs(leaf
, extent_item
, ref_mod
);
5450 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
5451 btrfs_set_extent_flags(leaf
, extent_item
,
5452 flags
| BTRFS_EXTENT_FLAG_DATA
);
5454 iref
= (struct btrfs_extent_inline_ref
*)(extent_item
+ 1);
5455 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
5457 struct btrfs_shared_data_ref
*ref
;
5458 ref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
5459 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
5460 btrfs_set_shared_data_ref_count(leaf
, ref
, ref_mod
);
5462 struct btrfs_extent_data_ref
*ref
;
5463 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
5464 btrfs_set_extent_data_ref_root(leaf
, ref
, root_objectid
);
5465 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
5466 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
5467 btrfs_set_extent_data_ref_count(leaf
, ref
, ref_mod
);
5470 btrfs_mark_buffer_dirty(path
->nodes
[0]);
5471 btrfs_free_path(path
);
5473 ret
= update_block_group(trans
, root
, ins
->objectid
, ins
->offset
, 1);
5475 printk(KERN_ERR
"btrfs update block group failed for %llu "
5476 "%llu\n", (unsigned long long)ins
->objectid
,
5477 (unsigned long long)ins
->offset
);
5483 static int alloc_reserved_tree_block(struct btrfs_trans_handle
*trans
,
5484 struct btrfs_root
*root
,
5485 u64 parent
, u64 root_objectid
,
5486 u64 flags
, struct btrfs_disk_key
*key
,
5487 int level
, struct btrfs_key
*ins
)
5490 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5491 struct btrfs_extent_item
*extent_item
;
5492 struct btrfs_tree_block_info
*block_info
;
5493 struct btrfs_extent_inline_ref
*iref
;
5494 struct btrfs_path
*path
;
5495 struct extent_buffer
*leaf
;
5496 u32 size
= sizeof(*extent_item
) + sizeof(*block_info
) + sizeof(*iref
);
5498 path
= btrfs_alloc_path();
5501 path
->leave_spinning
= 1;
5502 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
5506 leaf
= path
->nodes
[0];
5507 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
5508 struct btrfs_extent_item
);
5509 btrfs_set_extent_refs(leaf
, extent_item
, 1);
5510 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
5511 btrfs_set_extent_flags(leaf
, extent_item
,
5512 flags
| BTRFS_EXTENT_FLAG_TREE_BLOCK
);
5513 block_info
= (struct btrfs_tree_block_info
*)(extent_item
+ 1);
5515 btrfs_set_tree_block_key(leaf
, block_info
, key
);
5516 btrfs_set_tree_block_level(leaf
, block_info
, level
);
5518 iref
= (struct btrfs_extent_inline_ref
*)(block_info
+ 1);
5520 BUG_ON(!(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
));
5521 btrfs_set_extent_inline_ref_type(leaf
, iref
,
5522 BTRFS_SHARED_BLOCK_REF_KEY
);
5523 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
5525 btrfs_set_extent_inline_ref_type(leaf
, iref
,
5526 BTRFS_TREE_BLOCK_REF_KEY
);
5527 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
5530 btrfs_mark_buffer_dirty(leaf
);
5531 btrfs_free_path(path
);
5533 ret
= update_block_group(trans
, root
, ins
->objectid
, ins
->offset
, 1);
5535 printk(KERN_ERR
"btrfs update block group failed for %llu "
5536 "%llu\n", (unsigned long long)ins
->objectid
,
5537 (unsigned long long)ins
->offset
);
5543 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
5544 struct btrfs_root
*root
,
5545 u64 root_objectid
, u64 owner
,
5546 u64 offset
, struct btrfs_key
*ins
)
5550 BUG_ON(root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
5552 ret
= btrfs_add_delayed_data_ref(trans
, ins
->objectid
, ins
->offset
,
5553 0, root_objectid
, owner
, offset
,
5554 BTRFS_ADD_DELAYED_EXTENT
, NULL
);
5559 * this is used by the tree logging recovery code. It records that
5560 * an extent has been allocated and makes sure to clear the free
5561 * space cache bits as well
5563 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle
*trans
,
5564 struct btrfs_root
*root
,
5565 u64 root_objectid
, u64 owner
, u64 offset
,
5566 struct btrfs_key
*ins
)
5569 struct btrfs_block_group_cache
*block_group
;
5570 struct btrfs_caching_control
*caching_ctl
;
5571 u64 start
= ins
->objectid
;
5572 u64 num_bytes
= ins
->offset
;
5574 block_group
= btrfs_lookup_block_group(root
->fs_info
, ins
->objectid
);
5575 cache_block_group(block_group
, trans
, NULL
, 0);
5576 caching_ctl
= get_caching_control(block_group
);
5579 BUG_ON(!block_group_cache_done(block_group
));
5580 ret
= btrfs_remove_free_space(block_group
, start
, num_bytes
);
5583 mutex_lock(&caching_ctl
->mutex
);
5585 if (start
>= caching_ctl
->progress
) {
5586 ret
= add_excluded_extent(root
, start
, num_bytes
);
5588 } else if (start
+ num_bytes
<= caching_ctl
->progress
) {
5589 ret
= btrfs_remove_free_space(block_group
,
5593 num_bytes
= caching_ctl
->progress
- start
;
5594 ret
= btrfs_remove_free_space(block_group
,
5598 start
= caching_ctl
->progress
;
5599 num_bytes
= ins
->objectid
+ ins
->offset
-
5600 caching_ctl
->progress
;
5601 ret
= add_excluded_extent(root
, start
, num_bytes
);
5605 mutex_unlock(&caching_ctl
->mutex
);
5606 put_caching_control(caching_ctl
);
5609 ret
= update_reserved_bytes(block_group
, ins
->offset
, 1, 1);
5611 btrfs_put_block_group(block_group
);
5612 ret
= alloc_reserved_file_extent(trans
, root
, 0, root_objectid
,
5613 0, owner
, offset
, ins
, 1);
5617 struct extent_buffer
*btrfs_init_new_buffer(struct btrfs_trans_handle
*trans
,
5618 struct btrfs_root
*root
,
5619 u64 bytenr
, u32 blocksize
,
5622 struct extent_buffer
*buf
;
5624 buf
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
5626 return ERR_PTR(-ENOMEM
);
5627 btrfs_set_header_generation(buf
, trans
->transid
);
5628 btrfs_set_buffer_lockdep_class(buf
, level
);
5629 btrfs_tree_lock(buf
);
5630 clean_tree_block(trans
, root
, buf
);
5632 btrfs_set_lock_blocking(buf
);
5633 btrfs_set_buffer_uptodate(buf
);
5635 if (root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
) {
5637 * we allow two log transactions at a time, use different
5638 * EXENT bit to differentiate dirty pages.
5640 if (root
->log_transid
% 2 == 0)
5641 set_extent_dirty(&root
->dirty_log_pages
, buf
->start
,
5642 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
5644 set_extent_new(&root
->dirty_log_pages
, buf
->start
,
5645 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
5647 set_extent_dirty(&trans
->transaction
->dirty_pages
, buf
->start
,
5648 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
5650 trans
->blocks_used
++;
5651 /* this returns a buffer locked for blocking */
5655 static struct btrfs_block_rsv
*
5656 use_block_rsv(struct btrfs_trans_handle
*trans
,
5657 struct btrfs_root
*root
, u32 blocksize
)
5659 struct btrfs_block_rsv
*block_rsv
;
5660 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
5663 block_rsv
= get_block_rsv(trans
, root
);
5665 if (block_rsv
->size
== 0) {
5666 ret
= reserve_metadata_bytes(trans
, root
, block_rsv
,
5669 * If we couldn't reserve metadata bytes try and use some from
5670 * the global reserve.
5672 if (ret
&& block_rsv
!= global_rsv
) {
5673 ret
= block_rsv_use_bytes(global_rsv
, blocksize
);
5676 return ERR_PTR(ret
);
5678 return ERR_PTR(ret
);
5683 ret
= block_rsv_use_bytes(block_rsv
, blocksize
);
5688 ret
= reserve_metadata_bytes(trans
, root
, block_rsv
, blocksize
,
5691 spin_lock(&block_rsv
->lock
);
5692 block_rsv
->size
+= blocksize
;
5693 spin_unlock(&block_rsv
->lock
);
5695 } else if (ret
&& block_rsv
!= global_rsv
) {
5696 ret
= block_rsv_use_bytes(global_rsv
, blocksize
);
5702 return ERR_PTR(-ENOSPC
);
5705 static void unuse_block_rsv(struct btrfs_block_rsv
*block_rsv
, u32 blocksize
)
5707 block_rsv_add_bytes(block_rsv
, blocksize
, 0);
5708 block_rsv_release_bytes(block_rsv
, NULL
, 0);
5712 * finds a free extent and does all the dirty work required for allocation
5713 * returns the key for the extent through ins, and a tree buffer for
5714 * the first block of the extent through buf.
5716 * returns the tree buffer or NULL.
5718 struct extent_buffer
*btrfs_alloc_free_block(struct btrfs_trans_handle
*trans
,
5719 struct btrfs_root
*root
, u32 blocksize
,
5720 u64 parent
, u64 root_objectid
,
5721 struct btrfs_disk_key
*key
, int level
,
5722 u64 hint
, u64 empty_size
)
5724 struct btrfs_key ins
;
5725 struct btrfs_block_rsv
*block_rsv
;
5726 struct extent_buffer
*buf
;
5731 block_rsv
= use_block_rsv(trans
, root
, blocksize
);
5732 if (IS_ERR(block_rsv
))
5733 return ERR_CAST(block_rsv
);
5735 ret
= btrfs_reserve_extent(trans
, root
, blocksize
, blocksize
,
5736 empty_size
, hint
, (u64
)-1, &ins
, 0);
5738 unuse_block_rsv(block_rsv
, blocksize
);
5739 return ERR_PTR(ret
);
5742 buf
= btrfs_init_new_buffer(trans
, root
, ins
.objectid
,
5744 BUG_ON(IS_ERR(buf
));
5746 if (root_objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
5748 parent
= ins
.objectid
;
5749 flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
5753 if (root_objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
5754 struct btrfs_delayed_extent_op
*extent_op
;
5755 extent_op
= kmalloc(sizeof(*extent_op
), GFP_NOFS
);
5758 memcpy(&extent_op
->key
, key
, sizeof(extent_op
->key
));
5760 memset(&extent_op
->key
, 0, sizeof(extent_op
->key
));
5761 extent_op
->flags_to_set
= flags
;
5762 extent_op
->update_key
= 1;
5763 extent_op
->update_flags
= 1;
5764 extent_op
->is_data
= 0;
5766 ret
= btrfs_add_delayed_tree_ref(trans
, ins
.objectid
,
5767 ins
.offset
, parent
, root_objectid
,
5768 level
, BTRFS_ADD_DELAYED_EXTENT
,
5775 struct walk_control
{
5776 u64 refs
[BTRFS_MAX_LEVEL
];
5777 u64 flags
[BTRFS_MAX_LEVEL
];
5778 struct btrfs_key update_progress
;
5788 #define DROP_REFERENCE 1
5789 #define UPDATE_BACKREF 2
5791 static noinline
void reada_walk_down(struct btrfs_trans_handle
*trans
,
5792 struct btrfs_root
*root
,
5793 struct walk_control
*wc
,
5794 struct btrfs_path
*path
)
5802 struct btrfs_key key
;
5803 struct extent_buffer
*eb
;
5808 if (path
->slots
[wc
->level
] < wc
->reada_slot
) {
5809 wc
->reada_count
= wc
->reada_count
* 2 / 3;
5810 wc
->reada_count
= max(wc
->reada_count
, 2);
5812 wc
->reada_count
= wc
->reada_count
* 3 / 2;
5813 wc
->reada_count
= min_t(int, wc
->reada_count
,
5814 BTRFS_NODEPTRS_PER_BLOCK(root
));
5817 eb
= path
->nodes
[wc
->level
];
5818 nritems
= btrfs_header_nritems(eb
);
5819 blocksize
= btrfs_level_size(root
, wc
->level
- 1);
5821 for (slot
= path
->slots
[wc
->level
]; slot
< nritems
; slot
++) {
5822 if (nread
>= wc
->reada_count
)
5826 bytenr
= btrfs_node_blockptr(eb
, slot
);
5827 generation
= btrfs_node_ptr_generation(eb
, slot
);
5829 if (slot
== path
->slots
[wc
->level
])
5832 if (wc
->stage
== UPDATE_BACKREF
&&
5833 generation
<= root
->root_key
.offset
)
5836 /* We don't lock the tree block, it's OK to be racy here */
5837 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
, blocksize
,
5842 if (wc
->stage
== DROP_REFERENCE
) {
5846 if (wc
->level
== 1 &&
5847 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
5849 if (!wc
->update_ref
||
5850 generation
<= root
->root_key
.offset
)
5852 btrfs_node_key_to_cpu(eb
, &key
, slot
);
5853 ret
= btrfs_comp_cpu_keys(&key
,
5854 &wc
->update_progress
);
5858 if (wc
->level
== 1 &&
5859 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
5863 ret
= readahead_tree_block(root
, bytenr
, blocksize
,
5869 wc
->reada_slot
= slot
;
5873 * hepler to process tree block while walking down the tree.
5875 * when wc->stage == UPDATE_BACKREF, this function updates
5876 * back refs for pointers in the block.
5878 * NOTE: return value 1 means we should stop walking down.
5880 static noinline
int walk_down_proc(struct btrfs_trans_handle
*trans
,
5881 struct btrfs_root
*root
,
5882 struct btrfs_path
*path
,
5883 struct walk_control
*wc
, int lookup_info
)
5885 int level
= wc
->level
;
5886 struct extent_buffer
*eb
= path
->nodes
[level
];
5887 u64 flag
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
5890 if (wc
->stage
== UPDATE_BACKREF
&&
5891 btrfs_header_owner(eb
) != root
->root_key
.objectid
)
5895 * when reference count of tree block is 1, it won't increase
5896 * again. once full backref flag is set, we never clear it.
5899 ((wc
->stage
== DROP_REFERENCE
&& wc
->refs
[level
] != 1) ||
5900 (wc
->stage
== UPDATE_BACKREF
&& !(wc
->flags
[level
] & flag
)))) {
5901 BUG_ON(!path
->locks
[level
]);
5902 ret
= btrfs_lookup_extent_info(trans
, root
,
5907 BUG_ON(wc
->refs
[level
] == 0);
5910 if (wc
->stage
== DROP_REFERENCE
) {
5911 if (wc
->refs
[level
] > 1)
5914 if (path
->locks
[level
] && !wc
->keep_locks
) {
5915 btrfs_tree_unlock(eb
);
5916 path
->locks
[level
] = 0;
5921 /* wc->stage == UPDATE_BACKREF */
5922 if (!(wc
->flags
[level
] & flag
)) {
5923 BUG_ON(!path
->locks
[level
]);
5924 ret
= btrfs_inc_ref(trans
, root
, eb
, 1);
5926 ret
= btrfs_dec_ref(trans
, root
, eb
, 0);
5928 ret
= btrfs_set_disk_extent_flags(trans
, root
, eb
->start
,
5931 wc
->flags
[level
] |= flag
;
5935 * the block is shared by multiple trees, so it's not good to
5936 * keep the tree lock
5938 if (path
->locks
[level
] && level
> 0) {
5939 btrfs_tree_unlock(eb
);
5940 path
->locks
[level
] = 0;
5946 * hepler to process tree block pointer.
5948 * when wc->stage == DROP_REFERENCE, this function checks
5949 * reference count of the block pointed to. if the block
5950 * is shared and we need update back refs for the subtree
5951 * rooted at the block, this function changes wc->stage to
5952 * UPDATE_BACKREF. if the block is shared and there is no
5953 * need to update back, this function drops the reference
5956 * NOTE: return value 1 means we should stop walking down.
5958 static noinline
int do_walk_down(struct btrfs_trans_handle
*trans
,
5959 struct btrfs_root
*root
,
5960 struct btrfs_path
*path
,
5961 struct walk_control
*wc
, int *lookup_info
)
5967 struct btrfs_key key
;
5968 struct extent_buffer
*next
;
5969 int level
= wc
->level
;
5973 generation
= btrfs_node_ptr_generation(path
->nodes
[level
],
5974 path
->slots
[level
]);
5976 * if the lower level block was created before the snapshot
5977 * was created, we know there is no need to update back refs
5980 if (wc
->stage
== UPDATE_BACKREF
&&
5981 generation
<= root
->root_key
.offset
) {
5986 bytenr
= btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]);
5987 blocksize
= btrfs_level_size(root
, level
- 1);
5989 next
= btrfs_find_tree_block(root
, bytenr
, blocksize
);
5991 next
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
5996 btrfs_tree_lock(next
);
5997 btrfs_set_lock_blocking(next
);
5999 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
, blocksize
,
6000 &wc
->refs
[level
- 1],
6001 &wc
->flags
[level
- 1]);
6003 BUG_ON(wc
->refs
[level
- 1] == 0);
6006 if (wc
->stage
== DROP_REFERENCE
) {
6007 if (wc
->refs
[level
- 1] > 1) {
6009 (wc
->flags
[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF
))
6012 if (!wc
->update_ref
||
6013 generation
<= root
->root_key
.offset
)
6016 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
6017 path
->slots
[level
]);
6018 ret
= btrfs_comp_cpu_keys(&key
, &wc
->update_progress
);
6022 wc
->stage
= UPDATE_BACKREF
;
6023 wc
->shared_level
= level
- 1;
6027 (wc
->flags
[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF
))
6031 if (!btrfs_buffer_uptodate(next
, generation
)) {
6032 btrfs_tree_unlock(next
);
6033 free_extent_buffer(next
);
6039 if (reada
&& level
== 1)
6040 reada_walk_down(trans
, root
, wc
, path
);
6041 next
= read_tree_block(root
, bytenr
, blocksize
, generation
);
6042 btrfs_tree_lock(next
);
6043 btrfs_set_lock_blocking(next
);
6047 BUG_ON(level
!= btrfs_header_level(next
));
6048 path
->nodes
[level
] = next
;
6049 path
->slots
[level
] = 0;
6050 path
->locks
[level
] = 1;
6056 wc
->refs
[level
- 1] = 0;
6057 wc
->flags
[level
- 1] = 0;
6058 if (wc
->stage
== DROP_REFERENCE
) {
6059 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
) {
6060 parent
= path
->nodes
[level
]->start
;
6062 BUG_ON(root
->root_key
.objectid
!=
6063 btrfs_header_owner(path
->nodes
[level
]));
6067 ret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
, parent
,
6068 root
->root_key
.objectid
, level
- 1, 0);
6071 btrfs_tree_unlock(next
);
6072 free_extent_buffer(next
);
6078 * hepler to process tree block while walking up the tree.
6080 * when wc->stage == DROP_REFERENCE, this function drops
6081 * reference count on the block.
6083 * when wc->stage == UPDATE_BACKREF, this function changes
6084 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6085 * to UPDATE_BACKREF previously while processing the block.
6087 * NOTE: return value 1 means we should stop walking up.
6089 static noinline
int walk_up_proc(struct btrfs_trans_handle
*trans
,
6090 struct btrfs_root
*root
,
6091 struct btrfs_path
*path
,
6092 struct walk_control
*wc
)
6095 int level
= wc
->level
;
6096 struct extent_buffer
*eb
= path
->nodes
[level
];
6099 if (wc
->stage
== UPDATE_BACKREF
) {
6100 BUG_ON(wc
->shared_level
< level
);
6101 if (level
< wc
->shared_level
)
6104 ret
= find_next_key(path
, level
+ 1, &wc
->update_progress
);
6108 wc
->stage
= DROP_REFERENCE
;
6109 wc
->shared_level
= -1;
6110 path
->slots
[level
] = 0;
6113 * check reference count again if the block isn't locked.
6114 * we should start walking down the tree again if reference
6117 if (!path
->locks
[level
]) {
6119 btrfs_tree_lock(eb
);
6120 btrfs_set_lock_blocking(eb
);
6121 path
->locks
[level
] = 1;
6123 ret
= btrfs_lookup_extent_info(trans
, root
,
6128 BUG_ON(wc
->refs
[level
] == 0);
6129 if (wc
->refs
[level
] == 1) {
6130 btrfs_tree_unlock(eb
);
6131 path
->locks
[level
] = 0;
6137 /* wc->stage == DROP_REFERENCE */
6138 BUG_ON(wc
->refs
[level
] > 1 && !path
->locks
[level
]);
6140 if (wc
->refs
[level
] == 1) {
6142 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
6143 ret
= btrfs_dec_ref(trans
, root
, eb
, 1);
6145 ret
= btrfs_dec_ref(trans
, root
, eb
, 0);
6148 /* make block locked assertion in clean_tree_block happy */
6149 if (!path
->locks
[level
] &&
6150 btrfs_header_generation(eb
) == trans
->transid
) {
6151 btrfs_tree_lock(eb
);
6152 btrfs_set_lock_blocking(eb
);
6153 path
->locks
[level
] = 1;
6155 clean_tree_block(trans
, root
, eb
);
6158 if (eb
== root
->node
) {
6159 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
6162 BUG_ON(root
->root_key
.objectid
!=
6163 btrfs_header_owner(eb
));
6165 if (wc
->flags
[level
+ 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
6166 parent
= path
->nodes
[level
+ 1]->start
;
6168 BUG_ON(root
->root_key
.objectid
!=
6169 btrfs_header_owner(path
->nodes
[level
+ 1]));
6172 btrfs_free_tree_block(trans
, root
, eb
, parent
, wc
->refs
[level
] == 1);
6174 wc
->refs
[level
] = 0;
6175 wc
->flags
[level
] = 0;
6179 static noinline
int walk_down_tree(struct btrfs_trans_handle
*trans
,
6180 struct btrfs_root
*root
,
6181 struct btrfs_path
*path
,
6182 struct walk_control
*wc
)
6184 int level
= wc
->level
;
6185 int lookup_info
= 1;
6188 while (level
>= 0) {
6189 ret
= walk_down_proc(trans
, root
, path
, wc
, lookup_info
);
6196 if (path
->slots
[level
] >=
6197 btrfs_header_nritems(path
->nodes
[level
]))
6200 ret
= do_walk_down(trans
, root
, path
, wc
, &lookup_info
);
6202 path
->slots
[level
]++;
6211 static noinline
int walk_up_tree(struct btrfs_trans_handle
*trans
,
6212 struct btrfs_root
*root
,
6213 struct btrfs_path
*path
,
6214 struct walk_control
*wc
, int max_level
)
6216 int level
= wc
->level
;
6219 path
->slots
[level
] = btrfs_header_nritems(path
->nodes
[level
]);
6220 while (level
< max_level
&& path
->nodes
[level
]) {
6222 if (path
->slots
[level
] + 1 <
6223 btrfs_header_nritems(path
->nodes
[level
])) {
6224 path
->slots
[level
]++;
6227 ret
= walk_up_proc(trans
, root
, path
, wc
);
6231 if (path
->locks
[level
]) {
6232 btrfs_tree_unlock(path
->nodes
[level
]);
6233 path
->locks
[level
] = 0;
6235 free_extent_buffer(path
->nodes
[level
]);
6236 path
->nodes
[level
] = NULL
;
6244 * drop a subvolume tree.
6246 * this function traverses the tree freeing any blocks that only
6247 * referenced by the tree.
6249 * when a shared tree block is found. this function decreases its
6250 * reference count by one. if update_ref is true, this function
6251 * also make sure backrefs for the shared block and all lower level
6252 * blocks are properly updated.
6254 int btrfs_drop_snapshot(struct btrfs_root
*root
,
6255 struct btrfs_block_rsv
*block_rsv
, int update_ref
)
6257 struct btrfs_path
*path
;
6258 struct btrfs_trans_handle
*trans
;
6259 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
6260 struct btrfs_root_item
*root_item
= &root
->root_item
;
6261 struct walk_control
*wc
;
6262 struct btrfs_key key
;
6267 path
= btrfs_alloc_path();
6270 wc
= kzalloc(sizeof(*wc
), GFP_NOFS
);
6273 trans
= btrfs_start_transaction(tree_root
, 0);
6275 trans
->block_rsv
= block_rsv
;
6277 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
6278 level
= btrfs_header_level(root
->node
);
6279 path
->nodes
[level
] = btrfs_lock_root_node(root
);
6280 btrfs_set_lock_blocking(path
->nodes
[level
]);
6281 path
->slots
[level
] = 0;
6282 path
->locks
[level
] = 1;
6283 memset(&wc
->update_progress
, 0,
6284 sizeof(wc
->update_progress
));
6286 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
6287 memcpy(&wc
->update_progress
, &key
,
6288 sizeof(wc
->update_progress
));
6290 level
= root_item
->drop_level
;
6292 path
->lowest_level
= level
;
6293 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
6294 path
->lowest_level
= 0;
6302 * unlock our path, this is safe because only this
6303 * function is allowed to delete this snapshot
6305 btrfs_unlock_up_safe(path
, 0);
6307 level
= btrfs_header_level(root
->node
);
6309 btrfs_tree_lock(path
->nodes
[level
]);
6310 btrfs_set_lock_blocking(path
->nodes
[level
]);
6312 ret
= btrfs_lookup_extent_info(trans
, root
,
6313 path
->nodes
[level
]->start
,
6314 path
->nodes
[level
]->len
,
6318 BUG_ON(wc
->refs
[level
] == 0);
6320 if (level
== root_item
->drop_level
)
6323 btrfs_tree_unlock(path
->nodes
[level
]);
6324 WARN_ON(wc
->refs
[level
] != 1);
6330 wc
->shared_level
= -1;
6331 wc
->stage
= DROP_REFERENCE
;
6332 wc
->update_ref
= update_ref
;
6334 wc
->reada_count
= BTRFS_NODEPTRS_PER_BLOCK(root
);
6337 ret
= walk_down_tree(trans
, root
, path
, wc
);
6343 ret
= walk_up_tree(trans
, root
, path
, wc
, BTRFS_MAX_LEVEL
);
6350 BUG_ON(wc
->stage
!= DROP_REFERENCE
);
6354 if (wc
->stage
== DROP_REFERENCE
) {
6356 btrfs_node_key(path
->nodes
[level
],
6357 &root_item
->drop_progress
,
6358 path
->slots
[level
]);
6359 root_item
->drop_level
= level
;
6362 BUG_ON(wc
->level
== 0);
6363 if (btrfs_should_end_transaction(trans
, tree_root
)) {
6364 ret
= btrfs_update_root(trans
, tree_root
,
6369 btrfs_end_transaction_throttle(trans
, tree_root
);
6370 trans
= btrfs_start_transaction(tree_root
, 0);
6372 trans
->block_rsv
= block_rsv
;
6375 btrfs_release_path(root
, path
);
6378 ret
= btrfs_del_root(trans
, tree_root
, &root
->root_key
);
6381 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
6382 ret
= btrfs_find_last_root(tree_root
, root
->root_key
.objectid
,
6386 /* if we fail to delete the orphan item this time
6387 * around, it'll get picked up the next time.
6389 * The most common failure here is just -ENOENT.
6391 btrfs_del_orphan_item(trans
, tree_root
,
6392 root
->root_key
.objectid
);
6396 if (root
->in_radix
) {
6397 btrfs_free_fs_root(tree_root
->fs_info
, root
);
6399 free_extent_buffer(root
->node
);
6400 free_extent_buffer(root
->commit_root
);
6404 btrfs_end_transaction_throttle(trans
, tree_root
);
6406 btrfs_free_path(path
);
6411 * drop subtree rooted at tree block 'node'.
6413 * NOTE: this function will unlock and release tree block 'node'
6415 int btrfs_drop_subtree(struct btrfs_trans_handle
*trans
,
6416 struct btrfs_root
*root
,
6417 struct extent_buffer
*node
,
6418 struct extent_buffer
*parent
)
6420 struct btrfs_path
*path
;
6421 struct walk_control
*wc
;
6427 BUG_ON(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
6429 path
= btrfs_alloc_path();
6432 wc
= kzalloc(sizeof(*wc
), GFP_NOFS
);
6435 btrfs_assert_tree_locked(parent
);
6436 parent_level
= btrfs_header_level(parent
);
6437 extent_buffer_get(parent
);
6438 path
->nodes
[parent_level
] = parent
;
6439 path
->slots
[parent_level
] = btrfs_header_nritems(parent
);
6441 btrfs_assert_tree_locked(node
);
6442 level
= btrfs_header_level(node
);
6443 path
->nodes
[level
] = node
;
6444 path
->slots
[level
] = 0;
6445 path
->locks
[level
] = 1;
6447 wc
->refs
[parent_level
] = 1;
6448 wc
->flags
[parent_level
] = BTRFS_BLOCK_FLAG_FULL_BACKREF
;
6450 wc
->shared_level
= -1;
6451 wc
->stage
= DROP_REFERENCE
;
6454 wc
->reada_count
= BTRFS_NODEPTRS_PER_BLOCK(root
);
6457 wret
= walk_down_tree(trans
, root
, path
, wc
);
6463 wret
= walk_up_tree(trans
, root
, path
, wc
, parent_level
);
6471 btrfs_free_path(path
);
6476 static unsigned long calc_ra(unsigned long start
, unsigned long last
,
6479 return min(last
, start
+ nr
- 1);
6482 static noinline
int relocate_inode_pages(struct inode
*inode
, u64 start
,
6487 unsigned long first_index
;
6488 unsigned long last_index
;
6491 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6492 struct file_ra_state
*ra
;
6493 struct btrfs_ordered_extent
*ordered
;
6494 unsigned int total_read
= 0;
6495 unsigned int total_dirty
= 0;
6498 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
6500 mutex_lock(&inode
->i_mutex
);
6501 first_index
= start
>> PAGE_CACHE_SHIFT
;
6502 last_index
= (start
+ len
- 1) >> PAGE_CACHE_SHIFT
;
6504 /* make sure the dirty trick played by the caller work */
6505 ret
= invalidate_inode_pages2_range(inode
->i_mapping
,
6506 first_index
, last_index
);
6510 file_ra_state_init(ra
, inode
->i_mapping
);
6512 for (i
= first_index
; i
<= last_index
; i
++) {
6513 if (total_read
% ra
->ra_pages
== 0) {
6514 btrfs_force_ra(inode
->i_mapping
, ra
, NULL
, i
,
6515 calc_ra(i
, last_index
, ra
->ra_pages
));
6519 if (((u64
)i
<< PAGE_CACHE_SHIFT
) > i_size_read(inode
))
6521 page
= grab_cache_page(inode
->i_mapping
, i
);
6526 if (!PageUptodate(page
)) {
6527 btrfs_readpage(NULL
, page
);
6529 if (!PageUptodate(page
)) {
6531 page_cache_release(page
);
6536 wait_on_page_writeback(page
);
6538 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
6539 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
6540 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
6542 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
6544 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
6546 page_cache_release(page
);
6547 btrfs_start_ordered_extent(inode
, ordered
, 1);
6548 btrfs_put_ordered_extent(ordered
);
6551 set_page_extent_mapped(page
);
6553 if (i
== first_index
)
6554 set_extent_bits(io_tree
, page_start
, page_end
,
6555 EXTENT_BOUNDARY
, GFP_NOFS
);
6556 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
6558 set_page_dirty(page
);
6561 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
6563 page_cache_release(page
);
6568 mutex_unlock(&inode
->i_mutex
);
6569 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, total_dirty
);
6573 static noinline
int relocate_data_extent(struct inode
*reloc_inode
,
6574 struct btrfs_key
*extent_key
,
6577 struct btrfs_root
*root
= BTRFS_I(reloc_inode
)->root
;
6578 struct extent_map_tree
*em_tree
= &BTRFS_I(reloc_inode
)->extent_tree
;
6579 struct extent_map
*em
;
6580 u64 start
= extent_key
->objectid
- offset
;
6581 u64 end
= start
+ extent_key
->offset
- 1;
6583 em
= alloc_extent_map(GFP_NOFS
);
6584 BUG_ON(!em
|| IS_ERR(em
));
6587 em
->len
= extent_key
->offset
;
6588 em
->block_len
= extent_key
->offset
;
6589 em
->block_start
= extent_key
->objectid
;
6590 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6591 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
6593 /* setup extent map to cheat btrfs_readpage */
6594 lock_extent(&BTRFS_I(reloc_inode
)->io_tree
, start
, end
, GFP_NOFS
);
6597 write_lock(&em_tree
->lock
);
6598 ret
= add_extent_mapping(em_tree
, em
);
6599 write_unlock(&em_tree
->lock
);
6600 if (ret
!= -EEXIST
) {
6601 free_extent_map(em
);
6604 btrfs_drop_extent_cache(reloc_inode
, start
, end
, 0);
6606 unlock_extent(&BTRFS_I(reloc_inode
)->io_tree
, start
, end
, GFP_NOFS
);
6608 return relocate_inode_pages(reloc_inode
, start
, extent_key
->offset
);
6611 struct btrfs_ref_path
{
6613 u64 nodes
[BTRFS_MAX_LEVEL
];
6615 u64 root_generation
;
6622 struct btrfs_key node_keys
[BTRFS_MAX_LEVEL
];
6623 u64 new_nodes
[BTRFS_MAX_LEVEL
];
6626 struct disk_extent
{
6637 static int is_cowonly_root(u64 root_objectid
)
6639 if (root_objectid
== BTRFS_ROOT_TREE_OBJECTID
||
6640 root_objectid
== BTRFS_EXTENT_TREE_OBJECTID
||
6641 root_objectid
== BTRFS_CHUNK_TREE_OBJECTID
||
6642 root_objectid
== BTRFS_DEV_TREE_OBJECTID
||
6643 root_objectid
== BTRFS_TREE_LOG_OBJECTID
||
6644 root_objectid
== BTRFS_CSUM_TREE_OBJECTID
)
6649 static noinline
int __next_ref_path(struct btrfs_trans_handle
*trans
,
6650 struct btrfs_root
*extent_root
,
6651 struct btrfs_ref_path
*ref_path
,
6654 struct extent_buffer
*leaf
;
6655 struct btrfs_path
*path
;
6656 struct btrfs_extent_ref
*ref
;
6657 struct btrfs_key key
;
6658 struct btrfs_key found_key
;
6664 path
= btrfs_alloc_path();
6669 ref_path
->lowest_level
= -1;
6670 ref_path
->current_level
= -1;
6671 ref_path
->shared_level
= -1;
6675 level
= ref_path
->current_level
- 1;
6676 while (level
>= -1) {
6678 if (level
< ref_path
->lowest_level
)
6682 bytenr
= ref_path
->nodes
[level
];
6684 bytenr
= ref_path
->extent_start
;
6685 BUG_ON(bytenr
== 0);
6687 parent
= ref_path
->nodes
[level
+ 1];
6688 ref_path
->nodes
[level
+ 1] = 0;
6689 ref_path
->current_level
= level
;
6690 BUG_ON(parent
== 0);
6692 key
.objectid
= bytenr
;
6693 key
.offset
= parent
+ 1;
6694 key
.type
= BTRFS_EXTENT_REF_KEY
;
6696 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
, 0, 0);
6701 leaf
= path
->nodes
[0];
6702 nritems
= btrfs_header_nritems(leaf
);
6703 if (path
->slots
[0] >= nritems
) {
6704 ret
= btrfs_next_leaf(extent_root
, path
);
6709 leaf
= path
->nodes
[0];
6712 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6713 if (found_key
.objectid
== bytenr
&&
6714 found_key
.type
== BTRFS_EXTENT_REF_KEY
) {
6715 if (level
< ref_path
->shared_level
)
6716 ref_path
->shared_level
= level
;
6721 btrfs_release_path(extent_root
, path
);
6724 /* reached lowest level */
6728 level
= ref_path
->current_level
;
6729 while (level
< BTRFS_MAX_LEVEL
- 1) {
6733 bytenr
= ref_path
->nodes
[level
];
6735 bytenr
= ref_path
->extent_start
;
6737 BUG_ON(bytenr
== 0);
6739 key
.objectid
= bytenr
;
6741 key
.type
= BTRFS_EXTENT_REF_KEY
;
6743 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
, 0, 0);
6747 leaf
= path
->nodes
[0];
6748 nritems
= btrfs_header_nritems(leaf
);
6749 if (path
->slots
[0] >= nritems
) {
6750 ret
= btrfs_next_leaf(extent_root
, path
);
6754 /* the extent was freed by someone */
6755 if (ref_path
->lowest_level
== level
)
6757 btrfs_release_path(extent_root
, path
);
6760 leaf
= path
->nodes
[0];
6763 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6764 if (found_key
.objectid
!= bytenr
||
6765 found_key
.type
!= BTRFS_EXTENT_REF_KEY
) {
6766 /* the extent was freed by someone */
6767 if (ref_path
->lowest_level
== level
) {
6771 btrfs_release_path(extent_root
, path
);
6775 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
6776 struct btrfs_extent_ref
);
6777 ref_objectid
= btrfs_ref_objectid(leaf
, ref
);
6778 if (ref_objectid
< BTRFS_FIRST_FREE_OBJECTID
) {
6780 level
= (int)ref_objectid
;
6781 BUG_ON(level
>= BTRFS_MAX_LEVEL
);
6782 ref_path
->lowest_level
= level
;
6783 ref_path
->current_level
= level
;
6784 ref_path
->nodes
[level
] = bytenr
;
6786 WARN_ON(ref_objectid
!= level
);
6789 WARN_ON(level
!= -1);
6793 if (ref_path
->lowest_level
== level
) {
6794 ref_path
->owner_objectid
= ref_objectid
;
6795 ref_path
->num_refs
= btrfs_ref_num_refs(leaf
, ref
);
6799 * the block is tree root or the block isn't in reference
6802 if (found_key
.objectid
== found_key
.offset
||
6803 is_cowonly_root(btrfs_ref_root(leaf
, ref
))) {
6804 ref_path
->root_objectid
= btrfs_ref_root(leaf
, ref
);
6805 ref_path
->root_generation
=
6806 btrfs_ref_generation(leaf
, ref
);
6808 /* special reference from the tree log */
6809 ref_path
->nodes
[0] = found_key
.offset
;
6810 ref_path
->current_level
= 0;
6817 BUG_ON(ref_path
->nodes
[level
] != 0);
6818 ref_path
->nodes
[level
] = found_key
.offset
;
6819 ref_path
->current_level
= level
;
6822 * the reference was created in the running transaction,
6823 * no need to continue walking up.
6825 if (btrfs_ref_generation(leaf
, ref
) == trans
->transid
) {
6826 ref_path
->root_objectid
= btrfs_ref_root(leaf
, ref
);
6827 ref_path
->root_generation
=
6828 btrfs_ref_generation(leaf
, ref
);
6833 btrfs_release_path(extent_root
, path
);
6836 /* reached max tree level, but no tree root found. */
6839 btrfs_free_path(path
);
6843 static int btrfs_first_ref_path(struct btrfs_trans_handle
*trans
,
6844 struct btrfs_root
*extent_root
,
6845 struct btrfs_ref_path
*ref_path
,
6848 memset(ref_path
, 0, sizeof(*ref_path
));
6849 ref_path
->extent_start
= extent_start
;
6851 return __next_ref_path(trans
, extent_root
, ref_path
, 1);
6854 static int btrfs_next_ref_path(struct btrfs_trans_handle
*trans
,
6855 struct btrfs_root
*extent_root
,
6856 struct btrfs_ref_path
*ref_path
)
6858 return __next_ref_path(trans
, extent_root
, ref_path
, 0);
6861 static noinline
int get_new_locations(struct inode
*reloc_inode
,
6862 struct btrfs_key
*extent_key
,
6863 u64 offset
, int no_fragment
,
6864 struct disk_extent
**extents
,
6867 struct btrfs_root
*root
= BTRFS_I(reloc_inode
)->root
;
6868 struct btrfs_path
*path
;
6869 struct btrfs_file_extent_item
*fi
;
6870 struct extent_buffer
*leaf
;
6871 struct disk_extent
*exts
= *extents
;
6872 struct btrfs_key found_key
;
6877 int max
= *nr_extents
;
6880 WARN_ON(!no_fragment
&& *extents
);
6883 exts
= kmalloc(sizeof(*exts
) * max
, GFP_NOFS
);
6888 path
= btrfs_alloc_path();
6891 cur_pos
= extent_key
->objectid
- offset
;
6892 last_byte
= extent_key
->objectid
+ extent_key
->offset
;
6893 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, reloc_inode
->i_ino
,
6903 leaf
= path
->nodes
[0];
6904 nritems
= btrfs_header_nritems(leaf
);
6905 if (path
->slots
[0] >= nritems
) {
6906 ret
= btrfs_next_leaf(root
, path
);
6911 leaf
= path
->nodes
[0];
6914 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6915 if (found_key
.offset
!= cur_pos
||
6916 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
||
6917 found_key
.objectid
!= reloc_inode
->i_ino
)
6920 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
6921 struct btrfs_file_extent_item
);
6922 if (btrfs_file_extent_type(leaf
, fi
) !=
6923 BTRFS_FILE_EXTENT_REG
||
6924 btrfs_file_extent_disk_bytenr(leaf
, fi
) == 0)
6928 struct disk_extent
*old
= exts
;
6930 exts
= kzalloc(sizeof(*exts
) * max
, GFP_NOFS
);
6931 memcpy(exts
, old
, sizeof(*exts
) * nr
);
6932 if (old
!= *extents
)
6936 exts
[nr
].disk_bytenr
=
6937 btrfs_file_extent_disk_bytenr(leaf
, fi
);
6938 exts
[nr
].disk_num_bytes
=
6939 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
6940 exts
[nr
].offset
= btrfs_file_extent_offset(leaf
, fi
);
6941 exts
[nr
].num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
6942 exts
[nr
].ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
6943 exts
[nr
].compression
= btrfs_file_extent_compression(leaf
, fi
);
6944 exts
[nr
].encryption
= btrfs_file_extent_encryption(leaf
, fi
);
6945 exts
[nr
].other_encoding
= btrfs_file_extent_other_encoding(leaf
,
6947 BUG_ON(exts
[nr
].offset
> 0);
6948 BUG_ON(exts
[nr
].compression
|| exts
[nr
].encryption
);
6949 BUG_ON(exts
[nr
].num_bytes
!= exts
[nr
].disk_num_bytes
);
6951 cur_pos
+= exts
[nr
].num_bytes
;
6954 if (cur_pos
+ offset
>= last_byte
)
6964 BUG_ON(cur_pos
+ offset
> last_byte
);
6965 if (cur_pos
+ offset
< last_byte
) {
6971 btrfs_free_path(path
);
6973 if (exts
!= *extents
)
6982 static noinline
int replace_one_extent(struct btrfs_trans_handle
*trans
,
6983 struct btrfs_root
*root
,
6984 struct btrfs_path
*path
,
6985 struct btrfs_key
*extent_key
,
6986 struct btrfs_key
*leaf_key
,
6987 struct btrfs_ref_path
*ref_path
,
6988 struct disk_extent
*new_extents
,
6991 struct extent_buffer
*leaf
;
6992 struct btrfs_file_extent_item
*fi
;
6993 struct inode
*inode
= NULL
;
6994 struct btrfs_key key
;
6999 u64 search_end
= (u64
)-1;
7002 int extent_locked
= 0;
7006 memcpy(&key
, leaf_key
, sizeof(key
));
7007 if (ref_path
->owner_objectid
!= BTRFS_MULTIPLE_OBJECTIDS
) {
7008 if (key
.objectid
< ref_path
->owner_objectid
||
7009 (key
.objectid
== ref_path
->owner_objectid
&&
7010 key
.type
< BTRFS_EXTENT_DATA_KEY
)) {
7011 key
.objectid
= ref_path
->owner_objectid
;
7012 key
.type
= BTRFS_EXTENT_DATA_KEY
;
7018 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
7022 leaf
= path
->nodes
[0];
7023 nritems
= btrfs_header_nritems(leaf
);
7025 if (extent_locked
&& ret
> 0) {
7027 * the file extent item was modified by someone
7028 * before the extent got locked.
7030 unlock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
7031 lock_end
, GFP_NOFS
);
7035 if (path
->slots
[0] >= nritems
) {
7036 if (++nr_scaned
> 2)
7039 BUG_ON(extent_locked
);
7040 ret
= btrfs_next_leaf(root
, path
);
7045 leaf
= path
->nodes
[0];
7046 nritems
= btrfs_header_nritems(leaf
);
7049 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
7051 if (ref_path
->owner_objectid
!= BTRFS_MULTIPLE_OBJECTIDS
) {
7052 if ((key
.objectid
> ref_path
->owner_objectid
) ||
7053 (key
.objectid
== ref_path
->owner_objectid
&&
7054 key
.type
> BTRFS_EXTENT_DATA_KEY
) ||
7055 key
.offset
>= search_end
)
7059 if (inode
&& key
.objectid
!= inode
->i_ino
) {
7060 BUG_ON(extent_locked
);
7061 btrfs_release_path(root
, path
);
7062 mutex_unlock(&inode
->i_mutex
);
7068 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
7073 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
7074 struct btrfs_file_extent_item
);
7075 extent_type
= btrfs_file_extent_type(leaf
, fi
);
7076 if ((extent_type
!= BTRFS_FILE_EXTENT_REG
&&
7077 extent_type
!= BTRFS_FILE_EXTENT_PREALLOC
) ||
7078 (btrfs_file_extent_disk_bytenr(leaf
, fi
) !=
7079 extent_key
->objectid
)) {
7085 num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
7086 ext_offset
= btrfs_file_extent_offset(leaf
, fi
);
7088 if (search_end
== (u64
)-1) {
7089 search_end
= key
.offset
- ext_offset
+
7090 btrfs_file_extent_ram_bytes(leaf
, fi
);
7093 if (!extent_locked
) {
7094 lock_start
= key
.offset
;
7095 lock_end
= lock_start
+ num_bytes
- 1;
7097 if (lock_start
> key
.offset
||
7098 lock_end
+ 1 < key
.offset
+ num_bytes
) {
7099 unlock_extent(&BTRFS_I(inode
)->io_tree
,
7100 lock_start
, lock_end
, GFP_NOFS
);
7106 btrfs_release_path(root
, path
);
7108 inode
= btrfs_iget_locked(root
->fs_info
->sb
,
7109 key
.objectid
, root
);
7110 if (inode
->i_state
& I_NEW
) {
7111 BTRFS_I(inode
)->root
= root
;
7112 BTRFS_I(inode
)->location
.objectid
=
7114 BTRFS_I(inode
)->location
.type
=
7115 BTRFS_INODE_ITEM_KEY
;
7116 BTRFS_I(inode
)->location
.offset
= 0;
7117 btrfs_read_locked_inode(inode
);
7118 unlock_new_inode(inode
);
7121 * some code call btrfs_commit_transaction while
7122 * holding the i_mutex, so we can't use mutex_lock
7125 if (is_bad_inode(inode
) ||
7126 !mutex_trylock(&inode
->i_mutex
)) {
7129 key
.offset
= (u64
)-1;
7134 if (!extent_locked
) {
7135 struct btrfs_ordered_extent
*ordered
;
7137 btrfs_release_path(root
, path
);
7139 lock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
7140 lock_end
, GFP_NOFS
);
7141 ordered
= btrfs_lookup_first_ordered_extent(inode
,
7144 ordered
->file_offset
<= lock_end
&&
7145 ordered
->file_offset
+ ordered
->len
> lock_start
) {
7146 unlock_extent(&BTRFS_I(inode
)->io_tree
,
7147 lock_start
, lock_end
, GFP_NOFS
);
7148 btrfs_start_ordered_extent(inode
, ordered
, 1);
7149 btrfs_put_ordered_extent(ordered
);
7150 key
.offset
+= num_bytes
;
7154 btrfs_put_ordered_extent(ordered
);
7160 if (nr_extents
== 1) {
7161 /* update extent pointer in place */
7162 btrfs_set_file_extent_disk_bytenr(leaf
, fi
,
7163 new_extents
[0].disk_bytenr
);
7164 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
,
7165 new_extents
[0].disk_num_bytes
);
7166 btrfs_mark_buffer_dirty(leaf
);
7168 btrfs_drop_extent_cache(inode
, key
.offset
,
7169 key
.offset
+ num_bytes
- 1, 0);
7171 ret
= btrfs_inc_extent_ref(trans
, root
,
7172 new_extents
[0].disk_bytenr
,
7173 new_extents
[0].disk_num_bytes
,
7175 root
->root_key
.objectid
,
7180 ret
= btrfs_free_extent(trans
, root
,
7181 extent_key
->objectid
,
7184 btrfs_header_owner(leaf
),
7185 btrfs_header_generation(leaf
),
7189 btrfs_release_path(root
, path
);
7190 key
.offset
+= num_bytes
;
7198 * drop old extent pointer at first, then insert the
7199 * new pointers one bye one
7201 btrfs_release_path(root
, path
);
7202 ret
= btrfs_drop_extents(trans
, root
, inode
, key
.offset
,
7203 key
.offset
+ num_bytes
,
7204 key
.offset
, &alloc_hint
);
7207 for (i
= 0; i
< nr_extents
; i
++) {
7208 if (ext_offset
>= new_extents
[i
].num_bytes
) {
7209 ext_offset
-= new_extents
[i
].num_bytes
;
7212 extent_len
= min(new_extents
[i
].num_bytes
-
7213 ext_offset
, num_bytes
);
7215 ret
= btrfs_insert_empty_item(trans
, root
,
7220 leaf
= path
->nodes
[0];
7221 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
7222 struct btrfs_file_extent_item
);
7223 btrfs_set_file_extent_generation(leaf
, fi
,
7225 btrfs_set_file_extent_type(leaf
, fi
,
7226 BTRFS_FILE_EXTENT_REG
);
7227 btrfs_set_file_extent_disk_bytenr(leaf
, fi
,
7228 new_extents
[i
].disk_bytenr
);
7229 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
,
7230 new_extents
[i
].disk_num_bytes
);
7231 btrfs_set_file_extent_ram_bytes(leaf
, fi
,
7232 new_extents
[i
].ram_bytes
);
7234 btrfs_set_file_extent_compression(leaf
, fi
,
7235 new_extents
[i
].compression
);
7236 btrfs_set_file_extent_encryption(leaf
, fi
,
7237 new_extents
[i
].encryption
);
7238 btrfs_set_file_extent_other_encoding(leaf
, fi
,
7239 new_extents
[i
].other_encoding
);
7241 btrfs_set_file_extent_num_bytes(leaf
, fi
,
7243 ext_offset
+= new_extents
[i
].offset
;
7244 btrfs_set_file_extent_offset(leaf
, fi
,
7246 btrfs_mark_buffer_dirty(leaf
);
7248 btrfs_drop_extent_cache(inode
, key
.offset
,
7249 key
.offset
+ extent_len
- 1, 0);
7251 ret
= btrfs_inc_extent_ref(trans
, root
,
7252 new_extents
[i
].disk_bytenr
,
7253 new_extents
[i
].disk_num_bytes
,
7255 root
->root_key
.objectid
,
7256 trans
->transid
, key
.objectid
);
7258 btrfs_release_path(root
, path
);
7260 inode_add_bytes(inode
, extent_len
);
7263 num_bytes
-= extent_len
;
7264 key
.offset
+= extent_len
;
7269 BUG_ON(i
>= nr_extents
);
7273 if (extent_locked
) {
7274 unlock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
7275 lock_end
, GFP_NOFS
);
7279 if (ref_path
->owner_objectid
!= BTRFS_MULTIPLE_OBJECTIDS
&&
7280 key
.offset
>= search_end
)
7287 btrfs_release_path(root
, path
);
7289 mutex_unlock(&inode
->i_mutex
);
7290 if (extent_locked
) {
7291 unlock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
7292 lock_end
, GFP_NOFS
);
7299 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle
*trans
,
7300 struct btrfs_root
*root
,
7301 struct extent_buffer
*buf
, u64 orig_start
)
7306 BUG_ON(btrfs_header_generation(buf
) != trans
->transid
);
7307 BUG_ON(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
7309 level
= btrfs_header_level(buf
);
7311 struct btrfs_leaf_ref
*ref
;
7312 struct btrfs_leaf_ref
*orig_ref
;
7314 orig_ref
= btrfs_lookup_leaf_ref(root
, orig_start
);
7318 ref
= btrfs_alloc_leaf_ref(root
, orig_ref
->nritems
);
7320 btrfs_free_leaf_ref(root
, orig_ref
);
7324 ref
->nritems
= orig_ref
->nritems
;
7325 memcpy(ref
->extents
, orig_ref
->extents
,
7326 sizeof(ref
->extents
[0]) * ref
->nritems
);
7328 btrfs_free_leaf_ref(root
, orig_ref
);
7330 ref
->root_gen
= trans
->transid
;
7331 ref
->bytenr
= buf
->start
;
7332 ref
->owner
= btrfs_header_owner(buf
);
7333 ref
->generation
= btrfs_header_generation(buf
);
7335 ret
= btrfs_add_leaf_ref(root
, ref
, 0);
7337 btrfs_free_leaf_ref(root
, ref
);
7342 static noinline
int invalidate_extent_cache(struct btrfs_root
*root
,
7343 struct extent_buffer
*leaf
,
7344 struct btrfs_block_group_cache
*group
,
7345 struct btrfs_root
*target_root
)
7347 struct btrfs_key key
;
7348 struct inode
*inode
= NULL
;
7349 struct btrfs_file_extent_item
*fi
;
7350 struct extent_state
*cached_state
= NULL
;
7352 u64 skip_objectid
= 0;
7356 nritems
= btrfs_header_nritems(leaf
);
7357 for (i
= 0; i
< nritems
; i
++) {
7358 btrfs_item_key_to_cpu(leaf
, &key
, i
);
7359 if (key
.objectid
== skip_objectid
||
7360 key
.type
!= BTRFS_EXTENT_DATA_KEY
)
7362 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
7363 if (btrfs_file_extent_type(leaf
, fi
) ==
7364 BTRFS_FILE_EXTENT_INLINE
)
7366 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) == 0)
7368 if (!inode
|| inode
->i_ino
!= key
.objectid
) {
7370 inode
= btrfs_ilookup(target_root
->fs_info
->sb
,
7371 key
.objectid
, target_root
, 1);
7374 skip_objectid
= key
.objectid
;
7377 num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
7379 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, key
.offset
,
7380 key
.offset
+ num_bytes
- 1, 0, &cached_state
,
7382 btrfs_drop_extent_cache(inode
, key
.offset
,
7383 key
.offset
+ num_bytes
- 1, 1);
7384 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, key
.offset
,
7385 key
.offset
+ num_bytes
- 1, &cached_state
,
7393 static noinline
int replace_extents_in_leaf(struct btrfs_trans_handle
*trans
,
7394 struct btrfs_root
*root
,
7395 struct extent_buffer
*leaf
,
7396 struct btrfs_block_group_cache
*group
,
7397 struct inode
*reloc_inode
)
7399 struct btrfs_key key
;
7400 struct btrfs_key extent_key
;
7401 struct btrfs_file_extent_item
*fi
;
7402 struct btrfs_leaf_ref
*ref
;
7403 struct disk_extent
*new_extent
;
7412 new_extent
= kmalloc(sizeof(*new_extent
), GFP_NOFS
);
7413 BUG_ON(!new_extent
);
7415 ref
= btrfs_lookup_leaf_ref(root
, leaf
->start
);
7419 nritems
= btrfs_header_nritems(leaf
);
7420 for (i
= 0; i
< nritems
; i
++) {
7421 btrfs_item_key_to_cpu(leaf
, &key
, i
);
7422 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
7424 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
7425 if (btrfs_file_extent_type(leaf
, fi
) ==
7426 BTRFS_FILE_EXTENT_INLINE
)
7428 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
7429 num_bytes
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
7434 if (bytenr
>= group
->key
.objectid
+ group
->key
.offset
||
7435 bytenr
+ num_bytes
<= group
->key
.objectid
)
7438 extent_key
.objectid
= bytenr
;
7439 extent_key
.offset
= num_bytes
;
7440 extent_key
.type
= BTRFS_EXTENT_ITEM_KEY
;
7442 ret
= get_new_locations(reloc_inode
, &extent_key
,
7443 group
->key
.objectid
, 1,
7444 &new_extent
, &nr_extent
);
7449 BUG_ON(ref
->extents
[ext_index
].bytenr
!= bytenr
);
7450 BUG_ON(ref
->extents
[ext_index
].num_bytes
!= num_bytes
);
7451 ref
->extents
[ext_index
].bytenr
= new_extent
->disk_bytenr
;
7452 ref
->extents
[ext_index
].num_bytes
= new_extent
->disk_num_bytes
;
7454 btrfs_set_file_extent_disk_bytenr(leaf
, fi
,
7455 new_extent
->disk_bytenr
);
7456 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
,
7457 new_extent
->disk_num_bytes
);
7458 btrfs_mark_buffer_dirty(leaf
);
7460 ret
= btrfs_inc_extent_ref(trans
, root
,
7461 new_extent
->disk_bytenr
,
7462 new_extent
->disk_num_bytes
,
7464 root
->root_key
.objectid
,
7465 trans
->transid
, key
.objectid
);
7468 ret
= btrfs_free_extent(trans
, root
,
7469 bytenr
, num_bytes
, leaf
->start
,
7470 btrfs_header_owner(leaf
),
7471 btrfs_header_generation(leaf
),
7477 BUG_ON(ext_index
+ 1 != ref
->nritems
);
7478 btrfs_free_leaf_ref(root
, ref
);
7482 int btrfs_free_reloc_root(struct btrfs_trans_handle
*trans
,
7483 struct btrfs_root
*root
)
7485 struct btrfs_root
*reloc_root
;
7488 if (root
->reloc_root
) {
7489 reloc_root
= root
->reloc_root
;
7490 root
->reloc_root
= NULL
;
7491 list_add(&reloc_root
->dead_list
,
7492 &root
->fs_info
->dead_reloc_roots
);
7494 btrfs_set_root_bytenr(&reloc_root
->root_item
,
7495 reloc_root
->node
->start
);
7496 btrfs_set_root_level(&root
->root_item
,
7497 btrfs_header_level(reloc_root
->node
));
7498 memset(&reloc_root
->root_item
.drop_progress
, 0,
7499 sizeof(struct btrfs_disk_key
));
7500 reloc_root
->root_item
.drop_level
= 0;
7502 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
7503 &reloc_root
->root_key
,
7504 &reloc_root
->root_item
);
7510 int btrfs_drop_dead_reloc_roots(struct btrfs_root
*root
)
7512 struct btrfs_trans_handle
*trans
;
7513 struct btrfs_root
*reloc_root
;
7514 struct btrfs_root
*prev_root
= NULL
;
7515 struct list_head dead_roots
;
7519 INIT_LIST_HEAD(&dead_roots
);
7520 list_splice_init(&root
->fs_info
->dead_reloc_roots
, &dead_roots
);
7522 while (!list_empty(&dead_roots
)) {
7523 reloc_root
= list_entry(dead_roots
.prev
,
7524 struct btrfs_root
, dead_list
);
7525 list_del_init(&reloc_root
->dead_list
);
7527 BUG_ON(reloc_root
->commit_root
!= NULL
);
7529 trans
= btrfs_join_transaction(root
, 1);
7530 BUG_ON(IS_ERR(trans
));
7532 mutex_lock(&root
->fs_info
->drop_mutex
);
7533 ret
= btrfs_drop_snapshot(trans
, reloc_root
);
7536 mutex_unlock(&root
->fs_info
->drop_mutex
);
7538 nr
= trans
->blocks_used
;
7539 ret
= btrfs_end_transaction(trans
, root
);
7541 btrfs_btree_balance_dirty(root
, nr
);
7544 free_extent_buffer(reloc_root
->node
);
7546 ret
= btrfs_del_root(trans
, root
->fs_info
->tree_root
,
7547 &reloc_root
->root_key
);
7549 mutex_unlock(&root
->fs_info
->drop_mutex
);
7551 nr
= trans
->blocks_used
;
7552 ret
= btrfs_end_transaction(trans
, root
);
7554 btrfs_btree_balance_dirty(root
, nr
);
7557 prev_root
= reloc_root
;
7560 btrfs_remove_leaf_refs(prev_root
, (u64
)-1, 0);
7566 int btrfs_add_dead_reloc_root(struct btrfs_root
*root
)
7568 list_add(&root
->dead_list
, &root
->fs_info
->dead_reloc_roots
);
7572 int btrfs_cleanup_reloc_trees(struct btrfs_root
*root
)
7574 struct btrfs_root
*reloc_root
;
7575 struct btrfs_trans_handle
*trans
;
7576 struct btrfs_key location
;
7580 mutex_lock(&root
->fs_info
->tree_reloc_mutex
);
7581 ret
= btrfs_find_dead_roots(root
, BTRFS_TREE_RELOC_OBJECTID
, NULL
);
7583 found
= !list_empty(&root
->fs_info
->dead_reloc_roots
);
7584 mutex_unlock(&root
->fs_info
->tree_reloc_mutex
);
7587 trans
= btrfs_start_transaction(root
, 1);
7589 ret
= btrfs_commit_transaction(trans
, root
);
7593 location
.objectid
= BTRFS_DATA_RELOC_TREE_OBJECTID
;
7594 location
.offset
= (u64
)-1;
7595 location
.type
= BTRFS_ROOT_ITEM_KEY
;
7597 reloc_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
7598 BUG_ON(!reloc_root
);
7599 btrfs_orphan_cleanup(reloc_root
);
7603 static noinline
int init_reloc_tree(struct btrfs_trans_handle
*trans
,
7604 struct btrfs_root
*root
)
7606 struct btrfs_root
*reloc_root
;
7607 struct extent_buffer
*eb
;
7608 struct btrfs_root_item
*root_item
;
7609 struct btrfs_key root_key
;
7612 BUG_ON(!root
->ref_cows
);
7613 if (root
->reloc_root
)
7616 root_item
= kmalloc(sizeof(*root_item
), GFP_NOFS
);
7619 ret
= btrfs_copy_root(trans
, root
, root
->commit_root
,
7620 &eb
, BTRFS_TREE_RELOC_OBJECTID
);
7623 root_key
.objectid
= BTRFS_TREE_RELOC_OBJECTID
;
7624 root_key
.offset
= root
->root_key
.objectid
;
7625 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
7627 memcpy(root_item
, &root
->root_item
, sizeof(root_item
));
7628 btrfs_set_root_refs(root_item
, 0);
7629 btrfs_set_root_bytenr(root_item
, eb
->start
);
7630 btrfs_set_root_level(root_item
, btrfs_header_level(eb
));
7631 btrfs_set_root_generation(root_item
, trans
->transid
);
7633 btrfs_tree_unlock(eb
);
7634 free_extent_buffer(eb
);
7636 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
,
7637 &root_key
, root_item
);
7641 reloc_root
= btrfs_read_fs_root_no_radix(root
->fs_info
->tree_root
,
7643 BUG_ON(!reloc_root
);
7644 reloc_root
->last_trans
= trans
->transid
;
7645 reloc_root
->commit_root
= NULL
;
7646 reloc_root
->ref_tree
= &root
->fs_info
->reloc_ref_tree
;
7648 root
->reloc_root
= reloc_root
;
7653 * Core function of space balance.
7655 * The idea is using reloc trees to relocate tree blocks in reference
7656 * counted roots. There is one reloc tree for each subvol, and all
7657 * reloc trees share same root key objectid. Reloc trees are snapshots
7658 * of the latest committed roots of subvols (root->commit_root).
7660 * To relocate a tree block referenced by a subvol, there are two steps.
7661 * COW the block through subvol's reloc tree, then update block pointer
7662 * in the subvol to point to the new block. Since all reloc trees share
7663 * same root key objectid, doing special handing for tree blocks owned
7664 * by them is easy. Once a tree block has been COWed in one reloc tree,
7665 * we can use the resulting new block directly when the same block is
7666 * required to COW again through other reloc trees. By this way, relocated
7667 * tree blocks are shared between reloc trees, so they are also shared
7670 static noinline
int relocate_one_path(struct btrfs_trans_handle
*trans
,
7671 struct btrfs_root
*root
,
7672 struct btrfs_path
*path
,
7673 struct btrfs_key
*first_key
,
7674 struct btrfs_ref_path
*ref_path
,
7675 struct btrfs_block_group_cache
*group
,
7676 struct inode
*reloc_inode
)
7678 struct btrfs_root
*reloc_root
;
7679 struct extent_buffer
*eb
= NULL
;
7680 struct btrfs_key
*keys
;
7684 int lowest_level
= 0;
7687 if (ref_path
->owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
)
7688 lowest_level
= ref_path
->owner_objectid
;
7690 if (!root
->ref_cows
) {
7691 path
->lowest_level
= lowest_level
;
7692 ret
= btrfs_search_slot(trans
, root
, first_key
, path
, 0, 1);
7694 path
->lowest_level
= 0;
7695 btrfs_release_path(root
, path
);
7699 mutex_lock(&root
->fs_info
->tree_reloc_mutex
);
7700 ret
= init_reloc_tree(trans
, root
);
7702 reloc_root
= root
->reloc_root
;
7704 shared_level
= ref_path
->shared_level
;
7705 ref_path
->shared_level
= BTRFS_MAX_LEVEL
- 1;
7707 keys
= ref_path
->node_keys
;
7708 nodes
= ref_path
->new_nodes
;
7709 memset(&keys
[shared_level
+ 1], 0,
7710 sizeof(*keys
) * (BTRFS_MAX_LEVEL
- shared_level
- 1));
7711 memset(&nodes
[shared_level
+ 1], 0,
7712 sizeof(*nodes
) * (BTRFS_MAX_LEVEL
- shared_level
- 1));
7714 if (nodes
[lowest_level
] == 0) {
7715 path
->lowest_level
= lowest_level
;
7716 ret
= btrfs_search_slot(trans
, reloc_root
, first_key
, path
,
7719 for (level
= lowest_level
; level
< BTRFS_MAX_LEVEL
; level
++) {
7720 eb
= path
->nodes
[level
];
7721 if (!eb
|| eb
== reloc_root
->node
)
7723 nodes
[level
] = eb
->start
;
7725 btrfs_item_key_to_cpu(eb
, &keys
[level
], 0);
7727 btrfs_node_key_to_cpu(eb
, &keys
[level
], 0);
7730 ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
7731 eb
= path
->nodes
[0];
7732 ret
= replace_extents_in_leaf(trans
, reloc_root
, eb
,
7733 group
, reloc_inode
);
7736 btrfs_release_path(reloc_root
, path
);
7738 ret
= btrfs_merge_path(trans
, reloc_root
, keys
, nodes
,
7744 * replace tree blocks in the fs tree with tree blocks in
7747 ret
= btrfs_merge_path(trans
, root
, keys
, nodes
, lowest_level
);
7750 if (ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
7751 ret
= btrfs_search_slot(trans
, reloc_root
, first_key
, path
,
7754 extent_buffer_get(path
->nodes
[0]);
7755 eb
= path
->nodes
[0];
7756 btrfs_release_path(reloc_root
, path
);
7757 ret
= invalidate_extent_cache(reloc_root
, eb
, group
, root
);
7759 free_extent_buffer(eb
);
7762 mutex_unlock(&root
->fs_info
->tree_reloc_mutex
);
7763 path
->lowest_level
= 0;
7767 static noinline
int relocate_tree_block(struct btrfs_trans_handle
*trans
,
7768 struct btrfs_root
*root
,
7769 struct btrfs_path
*path
,
7770 struct btrfs_key
*first_key
,
7771 struct btrfs_ref_path
*ref_path
)
7775 ret
= relocate_one_path(trans
, root
, path
, first_key
,
7776 ref_path
, NULL
, NULL
);
7782 static noinline
int del_extent_zero(struct btrfs_trans_handle
*trans
,
7783 struct btrfs_root
*extent_root
,
7784 struct btrfs_path
*path
,
7785 struct btrfs_key
*extent_key
)
7789 ret
= btrfs_search_slot(trans
, extent_root
, extent_key
, path
, -1, 1);
7792 ret
= btrfs_del_item(trans
, extent_root
, path
);
7794 btrfs_release_path(extent_root
, path
);
7798 static noinline
struct btrfs_root
*read_ref_root(struct btrfs_fs_info
*fs_info
,
7799 struct btrfs_ref_path
*ref_path
)
7801 struct btrfs_key root_key
;
7803 root_key
.objectid
= ref_path
->root_objectid
;
7804 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
7805 if (is_cowonly_root(ref_path
->root_objectid
))
7806 root_key
.offset
= 0;
7808 root_key
.offset
= (u64
)-1;
7810 return btrfs_read_fs_root_no_name(fs_info
, &root_key
);
7813 static noinline
int relocate_one_extent(struct btrfs_root
*extent_root
,
7814 struct btrfs_path
*path
,
7815 struct btrfs_key
*extent_key
,
7816 struct btrfs_block_group_cache
*group
,
7817 struct inode
*reloc_inode
, int pass
)
7819 struct btrfs_trans_handle
*trans
;
7820 struct btrfs_root
*found_root
;
7821 struct btrfs_ref_path
*ref_path
= NULL
;
7822 struct disk_extent
*new_extents
= NULL
;
7827 struct btrfs_key first_key
;
7831 trans
= btrfs_start_transaction(extent_root
, 1);
7834 if (extent_key
->objectid
== 0) {
7835 ret
= del_extent_zero(trans
, extent_root
, path
, extent_key
);
7839 ref_path
= kmalloc(sizeof(*ref_path
), GFP_NOFS
);
7845 for (loops
= 0; ; loops
++) {
7847 ret
= btrfs_first_ref_path(trans
, extent_root
, ref_path
,
7848 extent_key
->objectid
);
7850 ret
= btrfs_next_ref_path(trans
, extent_root
, ref_path
);
7857 if (ref_path
->root_objectid
== BTRFS_TREE_LOG_OBJECTID
||
7858 ref_path
->root_objectid
== BTRFS_TREE_RELOC_OBJECTID
)
7861 found_root
= read_ref_root(extent_root
->fs_info
, ref_path
);
7862 BUG_ON(!found_root
);
7864 * for reference counted tree, only process reference paths
7865 * rooted at the latest committed root.
7867 if (found_root
->ref_cows
&&
7868 ref_path
->root_generation
!= found_root
->root_key
.offset
)
7871 if (ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
7874 * copy data extents to new locations
7876 u64 group_start
= group
->key
.objectid
;
7877 ret
= relocate_data_extent(reloc_inode
,
7886 level
= ref_path
->owner_objectid
;
7889 if (prev_block
!= ref_path
->nodes
[level
]) {
7890 struct extent_buffer
*eb
;
7891 u64 block_start
= ref_path
->nodes
[level
];
7892 u64 block_size
= btrfs_level_size(found_root
, level
);
7894 eb
= read_tree_block(found_root
, block_start
,
7896 btrfs_tree_lock(eb
);
7897 BUG_ON(level
!= btrfs_header_level(eb
));
7900 btrfs_item_key_to_cpu(eb
, &first_key
, 0);
7902 btrfs_node_key_to_cpu(eb
, &first_key
, 0);
7904 btrfs_tree_unlock(eb
);
7905 free_extent_buffer(eb
);
7906 prev_block
= block_start
;
7909 mutex_lock(&extent_root
->fs_info
->trans_mutex
);
7910 btrfs_record_root_in_trans(found_root
);
7911 mutex_unlock(&extent_root
->fs_info
->trans_mutex
);
7912 if (ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
7914 * try to update data extent references while
7915 * keeping metadata shared between snapshots.
7918 ret
= relocate_one_path(trans
, found_root
,
7919 path
, &first_key
, ref_path
,
7920 group
, reloc_inode
);
7926 * use fallback method to process the remaining
7930 u64 group_start
= group
->key
.objectid
;
7931 new_extents
= kmalloc(sizeof(*new_extents
),
7934 ret
= get_new_locations(reloc_inode
,
7942 ret
= replace_one_extent(trans
, found_root
,
7944 &first_key
, ref_path
,
7945 new_extents
, nr_extents
);
7947 ret
= relocate_tree_block(trans
, found_root
, path
,
7948 &first_key
, ref_path
);
7955 btrfs_end_transaction(trans
, extent_root
);
7962 static u64
update_block_group_flags(struct btrfs_root
*root
, u64 flags
)
7965 u64 stripped
= BTRFS_BLOCK_GROUP_RAID0
|
7966 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
;
7969 * we add in the count of missing devices because we want
7970 * to make sure that any RAID levels on a degraded FS
7971 * continue to be honored.
7973 num_devices
= root
->fs_info
->fs_devices
->rw_devices
+
7974 root
->fs_info
->fs_devices
->missing_devices
;
7976 if (num_devices
== 1) {
7977 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
7978 stripped
= flags
& ~stripped
;
7980 /* turn raid0 into single device chunks */
7981 if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
7984 /* turn mirroring into duplication */
7985 if (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
7986 BTRFS_BLOCK_GROUP_RAID10
))
7987 return stripped
| BTRFS_BLOCK_GROUP_DUP
;
7990 /* they already had raid on here, just return */
7991 if (flags
& stripped
)
7994 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
7995 stripped
= flags
& ~stripped
;
7997 /* switch duplicated blocks with raid1 */
7998 if (flags
& BTRFS_BLOCK_GROUP_DUP
)
7999 return stripped
| BTRFS_BLOCK_GROUP_RAID1
;
8001 /* turn single device chunks into raid0 */
8002 return stripped
| BTRFS_BLOCK_GROUP_RAID0
;
8007 static int set_block_group_ro(struct btrfs_block_group_cache
*cache
)
8009 struct btrfs_space_info
*sinfo
= cache
->space_info
;
8016 spin_lock(&sinfo
->lock
);
8017 spin_lock(&cache
->lock
);
8018 num_bytes
= cache
->key
.offset
- cache
->reserved
- cache
->pinned
-
8019 cache
->bytes_super
- btrfs_block_group_used(&cache
->item
);
8021 if (sinfo
->bytes_used
+ sinfo
->bytes_reserved
+ sinfo
->bytes_pinned
+
8022 sinfo
->bytes_may_use
+ sinfo
->bytes_readonly
+
8023 cache
->reserved_pinned
+ num_bytes
<= sinfo
->total_bytes
) {
8024 sinfo
->bytes_readonly
+= num_bytes
;
8025 sinfo
->bytes_reserved
+= cache
->reserved_pinned
;
8026 cache
->reserved_pinned
= 0;
8031 spin_unlock(&cache
->lock
);
8032 spin_unlock(&sinfo
->lock
);
8036 int btrfs_set_block_group_ro(struct btrfs_root
*root
,
8037 struct btrfs_block_group_cache
*cache
)
8040 struct btrfs_trans_handle
*trans
;
8046 trans
= btrfs_join_transaction(root
, 1);
8047 BUG_ON(IS_ERR(trans
));
8049 alloc_flags
= update_block_group_flags(root
, cache
->flags
);
8050 if (alloc_flags
!= cache
->flags
)
8051 do_chunk_alloc(trans
, root
, 2 * 1024 * 1024, alloc_flags
, 1);
8053 ret
= set_block_group_ro(cache
);
8056 alloc_flags
= get_alloc_profile(root
, cache
->space_info
->flags
);
8057 ret
= do_chunk_alloc(trans
, root
, 2 * 1024 * 1024, alloc_flags
, 1);
8060 ret
= set_block_group_ro(cache
);
8062 btrfs_end_transaction(trans
, root
);
8067 * helper to account the unused space of all the readonly block group in the
8068 * list. takes mirrors into account.
8070 static u64
__btrfs_get_ro_block_group_free_space(struct list_head
*groups_list
)
8072 struct btrfs_block_group_cache
*block_group
;
8076 list_for_each_entry(block_group
, groups_list
, list
) {
8077 spin_lock(&block_group
->lock
);
8079 if (!block_group
->ro
) {
8080 spin_unlock(&block_group
->lock
);
8084 if (block_group
->flags
& (BTRFS_BLOCK_GROUP_RAID1
|
8085 BTRFS_BLOCK_GROUP_RAID10
|
8086 BTRFS_BLOCK_GROUP_DUP
))
8091 free_bytes
+= (block_group
->key
.offset
-
8092 btrfs_block_group_used(&block_group
->item
)) *
8095 spin_unlock(&block_group
->lock
);
8102 * helper to account the unused space of all the readonly block group in the
8103 * space_info. takes mirrors into account.
8105 u64
btrfs_account_ro_block_groups_free_space(struct btrfs_space_info
*sinfo
)
8110 spin_lock(&sinfo
->lock
);
8112 for(i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++)
8113 if (!list_empty(&sinfo
->block_groups
[i
]))
8114 free_bytes
+= __btrfs_get_ro_block_group_free_space(
8115 &sinfo
->block_groups
[i
]);
8117 spin_unlock(&sinfo
->lock
);
8122 int btrfs_set_block_group_rw(struct btrfs_root
*root
,
8123 struct btrfs_block_group_cache
*cache
)
8125 struct btrfs_space_info
*sinfo
= cache
->space_info
;
8130 spin_lock(&sinfo
->lock
);
8131 spin_lock(&cache
->lock
);
8132 num_bytes
= cache
->key
.offset
- cache
->reserved
- cache
->pinned
-
8133 cache
->bytes_super
- btrfs_block_group_used(&cache
->item
);
8134 sinfo
->bytes_readonly
-= num_bytes
;
8136 spin_unlock(&cache
->lock
);
8137 spin_unlock(&sinfo
->lock
);
8142 * checks to see if its even possible to relocate this block group.
8144 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
8145 * ok to go ahead and try.
8147 int btrfs_can_relocate(struct btrfs_root
*root
, u64 bytenr
)
8149 struct btrfs_block_group_cache
*block_group
;
8150 struct btrfs_space_info
*space_info
;
8151 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
8152 struct btrfs_device
*device
;
8156 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
8158 /* odd, couldn't find the block group, leave it alone */
8162 /* no bytes used, we're good */
8163 if (!btrfs_block_group_used(&block_group
->item
))
8166 space_info
= block_group
->space_info
;
8167 spin_lock(&space_info
->lock
);
8169 full
= space_info
->full
;
8172 * if this is the last block group we have in this space, we can't
8173 * relocate it unless we're able to allocate a new chunk below.
8175 * Otherwise, we need to make sure we have room in the space to handle
8176 * all of the extents from this block group. If we can, we're good
8178 if ((space_info
->total_bytes
!= block_group
->key
.offset
) &&
8179 (space_info
->bytes_used
+ space_info
->bytes_reserved
+
8180 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
8181 btrfs_block_group_used(&block_group
->item
) <
8182 space_info
->total_bytes
)) {
8183 spin_unlock(&space_info
->lock
);
8186 spin_unlock(&space_info
->lock
);
8189 * ok we don't have enough space, but maybe we have free space on our
8190 * devices to allocate new chunks for relocation, so loop through our
8191 * alloc devices and guess if we have enough space. However, if we
8192 * were marked as full, then we know there aren't enough chunks, and we
8199 mutex_lock(&root
->fs_info
->chunk_mutex
);
8200 list_for_each_entry(device
, &fs_devices
->alloc_list
, dev_alloc_list
) {
8201 u64 min_free
= btrfs_block_group_used(&block_group
->item
);
8205 * check to make sure we can actually find a chunk with enough
8206 * space to fit our block group in.
8208 if (device
->total_bytes
> device
->bytes_used
+ min_free
) {
8209 ret
= find_free_dev_extent(NULL
, device
, min_free
,
8216 mutex_unlock(&root
->fs_info
->chunk_mutex
);
8218 btrfs_put_block_group(block_group
);
8222 static int find_first_block_group(struct btrfs_root
*root
,
8223 struct btrfs_path
*path
, struct btrfs_key
*key
)
8226 struct btrfs_key found_key
;
8227 struct extent_buffer
*leaf
;
8230 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
8235 slot
= path
->slots
[0];
8236 leaf
= path
->nodes
[0];
8237 if (slot
>= btrfs_header_nritems(leaf
)) {
8238 ret
= btrfs_next_leaf(root
, path
);
8245 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
8247 if (found_key
.objectid
>= key
->objectid
&&
8248 found_key
.type
== BTRFS_BLOCK_GROUP_ITEM_KEY
) {
8258 void btrfs_put_block_group_cache(struct btrfs_fs_info
*info
)
8260 struct btrfs_block_group_cache
*block_group
;
8264 struct inode
*inode
;
8266 block_group
= btrfs_lookup_first_block_group(info
, last
);
8267 while (block_group
) {
8268 spin_lock(&block_group
->lock
);
8269 if (block_group
->iref
)
8271 spin_unlock(&block_group
->lock
);
8272 block_group
= next_block_group(info
->tree_root
,
8282 inode
= block_group
->inode
;
8283 block_group
->iref
= 0;
8284 block_group
->inode
= NULL
;
8285 spin_unlock(&block_group
->lock
);
8287 last
= block_group
->key
.objectid
+ block_group
->key
.offset
;
8288 btrfs_put_block_group(block_group
);
8292 int btrfs_free_block_groups(struct btrfs_fs_info
*info
)
8294 struct btrfs_block_group_cache
*block_group
;
8295 struct btrfs_space_info
*space_info
;
8296 struct btrfs_caching_control
*caching_ctl
;
8299 down_write(&info
->extent_commit_sem
);
8300 while (!list_empty(&info
->caching_block_groups
)) {
8301 caching_ctl
= list_entry(info
->caching_block_groups
.next
,
8302 struct btrfs_caching_control
, list
);
8303 list_del(&caching_ctl
->list
);
8304 put_caching_control(caching_ctl
);
8306 up_write(&info
->extent_commit_sem
);
8308 spin_lock(&info
->block_group_cache_lock
);
8309 while ((n
= rb_last(&info
->block_group_cache_tree
)) != NULL
) {
8310 block_group
= rb_entry(n
, struct btrfs_block_group_cache
,
8312 rb_erase(&block_group
->cache_node
,
8313 &info
->block_group_cache_tree
);
8314 spin_unlock(&info
->block_group_cache_lock
);
8316 down_write(&block_group
->space_info
->groups_sem
);
8317 list_del(&block_group
->list
);
8318 up_write(&block_group
->space_info
->groups_sem
);
8320 if (block_group
->cached
== BTRFS_CACHE_STARTED
)
8321 wait_block_group_cache_done(block_group
);
8323 btrfs_remove_free_space_cache(block_group
);
8324 btrfs_put_block_group(block_group
);
8326 spin_lock(&info
->block_group_cache_lock
);
8328 spin_unlock(&info
->block_group_cache_lock
);
8330 /* now that all the block groups are freed, go through and
8331 * free all the space_info structs. This is only called during
8332 * the final stages of unmount, and so we know nobody is
8333 * using them. We call synchronize_rcu() once before we start,
8334 * just to be on the safe side.
8338 release_global_block_rsv(info
);
8340 while(!list_empty(&info
->space_info
)) {
8341 space_info
= list_entry(info
->space_info
.next
,
8342 struct btrfs_space_info
,
8344 if (space_info
->bytes_pinned
> 0 ||
8345 space_info
->bytes_reserved
> 0) {
8347 dump_space_info(space_info
, 0, 0);
8349 list_del(&space_info
->list
);
8355 static void __link_block_group(struct btrfs_space_info
*space_info
,
8356 struct btrfs_block_group_cache
*cache
)
8358 int index
= get_block_group_index(cache
);
8360 down_write(&space_info
->groups_sem
);
8361 list_add_tail(&cache
->list
, &space_info
->block_groups
[index
]);
8362 up_write(&space_info
->groups_sem
);
8365 int btrfs_read_block_groups(struct btrfs_root
*root
)
8367 struct btrfs_path
*path
;
8369 struct btrfs_block_group_cache
*cache
;
8370 struct btrfs_fs_info
*info
= root
->fs_info
;
8371 struct btrfs_space_info
*space_info
;
8372 struct btrfs_key key
;
8373 struct btrfs_key found_key
;
8374 struct extent_buffer
*leaf
;
8378 root
= info
->extent_root
;
8381 btrfs_set_key_type(&key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
8382 path
= btrfs_alloc_path();
8386 cache_gen
= btrfs_super_cache_generation(&root
->fs_info
->super_copy
);
8387 if (cache_gen
!= 0 &&
8388 btrfs_super_generation(&root
->fs_info
->super_copy
) != cache_gen
)
8390 if (btrfs_test_opt(root
, CLEAR_CACHE
))
8392 if (!btrfs_test_opt(root
, SPACE_CACHE
) && cache_gen
)
8393 printk(KERN_INFO
"btrfs: disk space caching is enabled\n");
8396 ret
= find_first_block_group(root
, path
, &key
);
8401 leaf
= path
->nodes
[0];
8402 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
8403 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
8409 atomic_set(&cache
->count
, 1);
8410 spin_lock_init(&cache
->lock
);
8411 spin_lock_init(&cache
->tree_lock
);
8412 cache
->fs_info
= info
;
8413 INIT_LIST_HEAD(&cache
->list
);
8414 INIT_LIST_HEAD(&cache
->cluster_list
);
8417 cache
->disk_cache_state
= BTRFS_DC_CLEAR
;
8420 * we only want to have 32k of ram per block group for keeping
8421 * track of free space, and if we pass 1/2 of that we want to
8422 * start converting things over to using bitmaps
8424 cache
->extents_thresh
= ((1024 * 32) / 2) /
8425 sizeof(struct btrfs_free_space
);
8427 read_extent_buffer(leaf
, &cache
->item
,
8428 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
8429 sizeof(cache
->item
));
8430 memcpy(&cache
->key
, &found_key
, sizeof(found_key
));
8432 key
.objectid
= found_key
.objectid
+ found_key
.offset
;
8433 btrfs_release_path(root
, path
);
8434 cache
->flags
= btrfs_block_group_flags(&cache
->item
);
8435 cache
->sectorsize
= root
->sectorsize
;
8438 * check for two cases, either we are full, and therefore
8439 * don't need to bother with the caching work since we won't
8440 * find any space, or we are empty, and we can just add all
8441 * the space in and be done with it. This saves us _alot_ of
8442 * time, particularly in the full case.
8444 if (found_key
.offset
== btrfs_block_group_used(&cache
->item
)) {
8445 exclude_super_stripes(root
, cache
);
8446 cache
->last_byte_to_unpin
= (u64
)-1;
8447 cache
->cached
= BTRFS_CACHE_FINISHED
;
8448 free_excluded_extents(root
, cache
);
8449 } else if (btrfs_block_group_used(&cache
->item
) == 0) {
8450 exclude_super_stripes(root
, cache
);
8451 cache
->last_byte_to_unpin
= (u64
)-1;
8452 cache
->cached
= BTRFS_CACHE_FINISHED
;
8453 add_new_free_space(cache
, root
->fs_info
,
8455 found_key
.objectid
+
8457 free_excluded_extents(root
, cache
);
8460 ret
= update_space_info(info
, cache
->flags
, found_key
.offset
,
8461 btrfs_block_group_used(&cache
->item
),
8464 cache
->space_info
= space_info
;
8465 spin_lock(&cache
->space_info
->lock
);
8466 cache
->space_info
->bytes_readonly
+= cache
->bytes_super
;
8467 spin_unlock(&cache
->space_info
->lock
);
8469 __link_block_group(space_info
, cache
);
8471 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
8474 set_avail_alloc_bits(root
->fs_info
, cache
->flags
);
8475 if (btrfs_chunk_readonly(root
, cache
->key
.objectid
))
8476 set_block_group_ro(cache
);
8479 list_for_each_entry_rcu(space_info
, &root
->fs_info
->space_info
, list
) {
8480 if (!(get_alloc_profile(root
, space_info
->flags
) &
8481 (BTRFS_BLOCK_GROUP_RAID10
|
8482 BTRFS_BLOCK_GROUP_RAID1
|
8483 BTRFS_BLOCK_GROUP_DUP
)))
8486 * avoid allocating from un-mirrored block group if there are
8487 * mirrored block groups.
8489 list_for_each_entry(cache
, &space_info
->block_groups
[3], list
)
8490 set_block_group_ro(cache
);
8491 list_for_each_entry(cache
, &space_info
->block_groups
[4], list
)
8492 set_block_group_ro(cache
);
8495 init_global_block_rsv(info
);
8498 btrfs_free_path(path
);
8502 int btrfs_make_block_group(struct btrfs_trans_handle
*trans
,
8503 struct btrfs_root
*root
, u64 bytes_used
,
8504 u64 type
, u64 chunk_objectid
, u64 chunk_offset
,
8508 struct btrfs_root
*extent_root
;
8509 struct btrfs_block_group_cache
*cache
;
8511 extent_root
= root
->fs_info
->extent_root
;
8513 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
8515 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
8519 cache
->key
.objectid
= chunk_offset
;
8520 cache
->key
.offset
= size
;
8521 cache
->key
.type
= BTRFS_BLOCK_GROUP_ITEM_KEY
;
8522 cache
->sectorsize
= root
->sectorsize
;
8523 cache
->fs_info
= root
->fs_info
;
8526 * we only want to have 32k of ram per block group for keeping track
8527 * of free space, and if we pass 1/2 of that we want to start
8528 * converting things over to using bitmaps
8530 cache
->extents_thresh
= ((1024 * 32) / 2) /
8531 sizeof(struct btrfs_free_space
);
8532 atomic_set(&cache
->count
, 1);
8533 spin_lock_init(&cache
->lock
);
8534 spin_lock_init(&cache
->tree_lock
);
8535 INIT_LIST_HEAD(&cache
->list
);
8536 INIT_LIST_HEAD(&cache
->cluster_list
);
8538 btrfs_set_block_group_used(&cache
->item
, bytes_used
);
8539 btrfs_set_block_group_chunk_objectid(&cache
->item
, chunk_objectid
);
8540 cache
->flags
= type
;
8541 btrfs_set_block_group_flags(&cache
->item
, type
);
8543 cache
->last_byte_to_unpin
= (u64
)-1;
8544 cache
->cached
= BTRFS_CACHE_FINISHED
;
8545 exclude_super_stripes(root
, cache
);
8547 add_new_free_space(cache
, root
->fs_info
, chunk_offset
,
8548 chunk_offset
+ size
);
8550 free_excluded_extents(root
, cache
);
8552 ret
= update_space_info(root
->fs_info
, cache
->flags
, size
, bytes_used
,
8553 &cache
->space_info
);
8556 spin_lock(&cache
->space_info
->lock
);
8557 cache
->space_info
->bytes_readonly
+= cache
->bytes_super
;
8558 spin_unlock(&cache
->space_info
->lock
);
8560 __link_block_group(cache
->space_info
, cache
);
8562 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
8565 ret
= btrfs_insert_item(trans
, extent_root
, &cache
->key
, &cache
->item
,
8566 sizeof(cache
->item
));
8569 set_avail_alloc_bits(extent_root
->fs_info
, type
);
8574 int btrfs_remove_block_group(struct btrfs_trans_handle
*trans
,
8575 struct btrfs_root
*root
, u64 group_start
)
8577 struct btrfs_path
*path
;
8578 struct btrfs_block_group_cache
*block_group
;
8579 struct btrfs_free_cluster
*cluster
;
8580 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
8581 struct btrfs_key key
;
8582 struct inode
*inode
;
8586 root
= root
->fs_info
->extent_root
;
8588 block_group
= btrfs_lookup_block_group(root
->fs_info
, group_start
);
8589 BUG_ON(!block_group
);
8590 BUG_ON(!block_group
->ro
);
8592 memcpy(&key
, &block_group
->key
, sizeof(key
));
8593 if (block_group
->flags
& (BTRFS_BLOCK_GROUP_DUP
|
8594 BTRFS_BLOCK_GROUP_RAID1
|
8595 BTRFS_BLOCK_GROUP_RAID10
))
8600 /* make sure this block group isn't part of an allocation cluster */
8601 cluster
= &root
->fs_info
->data_alloc_cluster
;
8602 spin_lock(&cluster
->refill_lock
);
8603 btrfs_return_cluster_to_free_space(block_group
, cluster
);
8604 spin_unlock(&cluster
->refill_lock
);
8607 * make sure this block group isn't part of a metadata
8608 * allocation cluster
8610 cluster
= &root
->fs_info
->meta_alloc_cluster
;
8611 spin_lock(&cluster
->refill_lock
);
8612 btrfs_return_cluster_to_free_space(block_group
, cluster
);
8613 spin_unlock(&cluster
->refill_lock
);
8615 path
= btrfs_alloc_path();
8618 inode
= lookup_free_space_inode(root
, block_group
, path
);
8619 if (!IS_ERR(inode
)) {
8620 btrfs_orphan_add(trans
, inode
);
8622 /* One for the block groups ref */
8623 spin_lock(&block_group
->lock
);
8624 if (block_group
->iref
) {
8625 block_group
->iref
= 0;
8626 block_group
->inode
= NULL
;
8627 spin_unlock(&block_group
->lock
);
8630 spin_unlock(&block_group
->lock
);
8632 /* One for our lookup ref */
8636 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
8637 key
.offset
= block_group
->key
.objectid
;
8640 ret
= btrfs_search_slot(trans
, tree_root
, &key
, path
, -1, 1);
8644 btrfs_release_path(tree_root
, path
);
8646 ret
= btrfs_del_item(trans
, tree_root
, path
);
8649 btrfs_release_path(tree_root
, path
);
8652 spin_lock(&root
->fs_info
->block_group_cache_lock
);
8653 rb_erase(&block_group
->cache_node
,
8654 &root
->fs_info
->block_group_cache_tree
);
8655 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
8657 down_write(&block_group
->space_info
->groups_sem
);
8659 * we must use list_del_init so people can check to see if they
8660 * are still on the list after taking the semaphore
8662 list_del_init(&block_group
->list
);
8663 up_write(&block_group
->space_info
->groups_sem
);
8665 if (block_group
->cached
== BTRFS_CACHE_STARTED
)
8666 wait_block_group_cache_done(block_group
);
8668 btrfs_remove_free_space_cache(block_group
);
8670 spin_lock(&block_group
->space_info
->lock
);
8671 block_group
->space_info
->total_bytes
-= block_group
->key
.offset
;
8672 block_group
->space_info
->bytes_readonly
-= block_group
->key
.offset
;
8673 block_group
->space_info
->disk_total
-= block_group
->key
.offset
* factor
;
8674 spin_unlock(&block_group
->space_info
->lock
);
8676 memcpy(&key
, &block_group
->key
, sizeof(key
));
8678 btrfs_clear_space_info_full(root
->fs_info
);
8680 btrfs_put_block_group(block_group
);
8681 btrfs_put_block_group(block_group
);
8683 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
8689 ret
= btrfs_del_item(trans
, root
, path
);
8691 btrfs_free_path(path
);
8695 int btrfs_error_unpin_extent_range(struct btrfs_root
*root
, u64 start
, u64 end
)
8697 return unpin_extent_range(root
, start
, end
);
8700 int btrfs_error_discard_extent(struct btrfs_root
*root
, u64 bytenr
,
8703 return btrfs_discard_extent(root
, bytenr
, num_bytes
);