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 /* control flags for do_chunk_alloc's force field
37 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
38 * if we really need one.
40 * CHUNK_ALLOC_FORCE means it must try to allocate one
42 * CHUNK_ALLOC_LIMITED means to only try and allocate one
43 * if we have very few chunks already allocated. This is
44 * used as part of the clustering code to help make sure
45 * we have a good pool of storage to cluster in, without
46 * filling the FS with empty chunks
50 CHUNK_ALLOC_NO_FORCE
= 0,
51 CHUNK_ALLOC_FORCE
= 1,
52 CHUNK_ALLOC_LIMITED
= 2,
56 * Control how reservations are dealt with.
58 * RESERVE_FREE - freeing a reservation.
59 * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for
61 * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update
62 * bytes_may_use as the ENOSPC accounting is done elsewhere
67 RESERVE_ALLOC_NO_ACCOUNT
= 2,
70 static int update_block_group(struct btrfs_trans_handle
*trans
,
71 struct btrfs_root
*root
,
72 u64 bytenr
, u64 num_bytes
, int alloc
);
73 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
74 struct btrfs_root
*root
,
75 u64 bytenr
, u64 num_bytes
, u64 parent
,
76 u64 root_objectid
, u64 owner_objectid
,
77 u64 owner_offset
, int refs_to_drop
,
78 struct btrfs_delayed_extent_op
*extra_op
);
79 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op
*extent_op
,
80 struct extent_buffer
*leaf
,
81 struct btrfs_extent_item
*ei
);
82 static int alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
83 struct btrfs_root
*root
,
84 u64 parent
, u64 root_objectid
,
85 u64 flags
, u64 owner
, u64 offset
,
86 struct btrfs_key
*ins
, int ref_mod
);
87 static int alloc_reserved_tree_block(struct btrfs_trans_handle
*trans
,
88 struct btrfs_root
*root
,
89 u64 parent
, u64 root_objectid
,
90 u64 flags
, struct btrfs_disk_key
*key
,
91 int level
, struct btrfs_key
*ins
);
92 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
93 struct btrfs_root
*extent_root
, u64 alloc_bytes
,
94 u64 flags
, int force
);
95 static int find_next_key(struct btrfs_path
*path
, int level
,
96 struct btrfs_key
*key
);
97 static void dump_space_info(struct btrfs_space_info
*info
, u64 bytes
,
98 int dump_block_groups
);
99 static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache
*cache
,
100 u64 num_bytes
, int reserve
);
103 block_group_cache_done(struct btrfs_block_group_cache
*cache
)
106 return cache
->cached
== BTRFS_CACHE_FINISHED
;
109 static int block_group_bits(struct btrfs_block_group_cache
*cache
, u64 bits
)
111 return (cache
->flags
& bits
) == bits
;
114 static void btrfs_get_block_group(struct btrfs_block_group_cache
*cache
)
116 atomic_inc(&cache
->count
);
119 void btrfs_put_block_group(struct btrfs_block_group_cache
*cache
)
121 if (atomic_dec_and_test(&cache
->count
)) {
122 WARN_ON(cache
->pinned
> 0);
123 WARN_ON(cache
->reserved
> 0);
124 kfree(cache
->free_space_ctl
);
130 * this adds the block group to the fs_info rb tree for the block group
133 static int btrfs_add_block_group_cache(struct btrfs_fs_info
*info
,
134 struct btrfs_block_group_cache
*block_group
)
137 struct rb_node
*parent
= NULL
;
138 struct btrfs_block_group_cache
*cache
;
140 spin_lock(&info
->block_group_cache_lock
);
141 p
= &info
->block_group_cache_tree
.rb_node
;
145 cache
= rb_entry(parent
, struct btrfs_block_group_cache
,
147 if (block_group
->key
.objectid
< cache
->key
.objectid
) {
149 } else if (block_group
->key
.objectid
> cache
->key
.objectid
) {
152 spin_unlock(&info
->block_group_cache_lock
);
157 rb_link_node(&block_group
->cache_node
, parent
, p
);
158 rb_insert_color(&block_group
->cache_node
,
159 &info
->block_group_cache_tree
);
160 spin_unlock(&info
->block_group_cache_lock
);
166 * This will return the block group at or after bytenr if contains is 0, else
167 * it will return the block group that contains the bytenr
169 static struct btrfs_block_group_cache
*
170 block_group_cache_tree_search(struct btrfs_fs_info
*info
, u64 bytenr
,
173 struct btrfs_block_group_cache
*cache
, *ret
= NULL
;
177 spin_lock(&info
->block_group_cache_lock
);
178 n
= info
->block_group_cache_tree
.rb_node
;
181 cache
= rb_entry(n
, struct btrfs_block_group_cache
,
183 end
= cache
->key
.objectid
+ cache
->key
.offset
- 1;
184 start
= cache
->key
.objectid
;
186 if (bytenr
< start
) {
187 if (!contains
&& (!ret
|| start
< ret
->key
.objectid
))
190 } else if (bytenr
> start
) {
191 if (contains
&& bytenr
<= end
) {
202 btrfs_get_block_group(ret
);
203 spin_unlock(&info
->block_group_cache_lock
);
208 static int add_excluded_extent(struct btrfs_root
*root
,
209 u64 start
, u64 num_bytes
)
211 u64 end
= start
+ num_bytes
- 1;
212 set_extent_bits(&root
->fs_info
->freed_extents
[0],
213 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
214 set_extent_bits(&root
->fs_info
->freed_extents
[1],
215 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
219 static void free_excluded_extents(struct btrfs_root
*root
,
220 struct btrfs_block_group_cache
*cache
)
224 start
= cache
->key
.objectid
;
225 end
= start
+ cache
->key
.offset
- 1;
227 clear_extent_bits(&root
->fs_info
->freed_extents
[0],
228 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
229 clear_extent_bits(&root
->fs_info
->freed_extents
[1],
230 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
233 static int exclude_super_stripes(struct btrfs_root
*root
,
234 struct btrfs_block_group_cache
*cache
)
241 if (cache
->key
.objectid
< BTRFS_SUPER_INFO_OFFSET
) {
242 stripe_len
= BTRFS_SUPER_INFO_OFFSET
- cache
->key
.objectid
;
243 cache
->bytes_super
+= stripe_len
;
244 ret
= add_excluded_extent(root
, cache
->key
.objectid
,
249 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
250 bytenr
= btrfs_sb_offset(i
);
251 ret
= btrfs_rmap_block(&root
->fs_info
->mapping_tree
,
252 cache
->key
.objectid
, bytenr
,
253 0, &logical
, &nr
, &stripe_len
);
257 cache
->bytes_super
+= stripe_len
;
258 ret
= add_excluded_extent(root
, logical
[nr
],
268 static struct btrfs_caching_control
*
269 get_caching_control(struct btrfs_block_group_cache
*cache
)
271 struct btrfs_caching_control
*ctl
;
273 spin_lock(&cache
->lock
);
274 if (cache
->cached
!= BTRFS_CACHE_STARTED
) {
275 spin_unlock(&cache
->lock
);
279 /* We're loading it the fast way, so we don't have a caching_ctl. */
280 if (!cache
->caching_ctl
) {
281 spin_unlock(&cache
->lock
);
285 ctl
= cache
->caching_ctl
;
286 atomic_inc(&ctl
->count
);
287 spin_unlock(&cache
->lock
);
291 static void put_caching_control(struct btrfs_caching_control
*ctl
)
293 if (atomic_dec_and_test(&ctl
->count
))
298 * this is only called by cache_block_group, since we could have freed extents
299 * we need to check the pinned_extents for any extents that can't be used yet
300 * since their free space will be released as soon as the transaction commits.
302 static u64
add_new_free_space(struct btrfs_block_group_cache
*block_group
,
303 struct btrfs_fs_info
*info
, u64 start
, u64 end
)
305 u64 extent_start
, extent_end
, size
, total_added
= 0;
308 while (start
< end
) {
309 ret
= find_first_extent_bit(info
->pinned_extents
, start
,
310 &extent_start
, &extent_end
,
311 EXTENT_DIRTY
| EXTENT_UPTODATE
);
315 if (extent_start
<= start
) {
316 start
= extent_end
+ 1;
317 } else if (extent_start
> start
&& extent_start
< end
) {
318 size
= extent_start
- start
;
320 ret
= btrfs_add_free_space(block_group
, start
,
323 start
= extent_end
+ 1;
332 ret
= btrfs_add_free_space(block_group
, start
, size
);
339 static noinline
void caching_thread(struct btrfs_work
*work
)
341 struct btrfs_block_group_cache
*block_group
;
342 struct btrfs_fs_info
*fs_info
;
343 struct btrfs_caching_control
*caching_ctl
;
344 struct btrfs_root
*extent_root
;
345 struct btrfs_path
*path
;
346 struct extent_buffer
*leaf
;
347 struct btrfs_key key
;
353 caching_ctl
= container_of(work
, struct btrfs_caching_control
, work
);
354 block_group
= caching_ctl
->block_group
;
355 fs_info
= block_group
->fs_info
;
356 extent_root
= fs_info
->extent_root
;
358 path
= btrfs_alloc_path();
362 last
= max_t(u64
, block_group
->key
.objectid
, BTRFS_SUPER_INFO_OFFSET
);
365 * We don't want to deadlock with somebody trying to allocate a new
366 * extent for the extent root while also trying to search the extent
367 * root to add free space. So we skip locking and search the commit
368 * root, since its read-only
370 path
->skip_locking
= 1;
371 path
->search_commit_root
= 1;
376 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
378 mutex_lock(&caching_ctl
->mutex
);
379 /* need to make sure the commit_root doesn't disappear */
380 down_read(&fs_info
->extent_commit_sem
);
382 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
386 leaf
= path
->nodes
[0];
387 nritems
= btrfs_header_nritems(leaf
);
390 if (btrfs_fs_closing(fs_info
) > 1) {
395 if (path
->slots
[0] < nritems
) {
396 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
398 ret
= find_next_key(path
, 0, &key
);
402 if (need_resched() ||
403 btrfs_next_leaf(extent_root
, path
)) {
404 caching_ctl
->progress
= last
;
405 btrfs_release_path(path
);
406 up_read(&fs_info
->extent_commit_sem
);
407 mutex_unlock(&caching_ctl
->mutex
);
411 leaf
= path
->nodes
[0];
412 nritems
= btrfs_header_nritems(leaf
);
416 if (key
.objectid
< block_group
->key
.objectid
) {
421 if (key
.objectid
>= block_group
->key
.objectid
+
422 block_group
->key
.offset
)
425 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
426 total_found
+= add_new_free_space(block_group
,
429 last
= key
.objectid
+ key
.offset
;
431 if (total_found
> (1024 * 1024 * 2)) {
433 wake_up(&caching_ctl
->wait
);
440 total_found
+= add_new_free_space(block_group
, fs_info
, last
,
441 block_group
->key
.objectid
+
442 block_group
->key
.offset
);
443 caching_ctl
->progress
= (u64
)-1;
445 spin_lock(&block_group
->lock
);
446 block_group
->caching_ctl
= NULL
;
447 block_group
->cached
= BTRFS_CACHE_FINISHED
;
448 spin_unlock(&block_group
->lock
);
451 btrfs_free_path(path
);
452 up_read(&fs_info
->extent_commit_sem
);
454 free_excluded_extents(extent_root
, block_group
);
456 mutex_unlock(&caching_ctl
->mutex
);
458 wake_up(&caching_ctl
->wait
);
460 put_caching_control(caching_ctl
);
461 btrfs_put_block_group(block_group
);
464 static int cache_block_group(struct btrfs_block_group_cache
*cache
,
465 struct btrfs_trans_handle
*trans
,
466 struct btrfs_root
*root
,
469 struct btrfs_fs_info
*fs_info
= cache
->fs_info
;
470 struct btrfs_caching_control
*caching_ctl
;
474 if (cache
->cached
!= BTRFS_CACHE_NO
)
478 * We can't do the read from on-disk cache during a commit since we need
479 * to have the normal tree locking. Also if we are currently trying to
480 * allocate blocks for the tree root we can't do the fast caching since
481 * we likely hold important locks.
483 if (trans
&& (!trans
->transaction
->in_commit
) &&
484 (root
&& root
!= root
->fs_info
->tree_root
) &&
485 btrfs_test_opt(root
, SPACE_CACHE
)) {
486 spin_lock(&cache
->lock
);
487 if (cache
->cached
!= BTRFS_CACHE_NO
) {
488 spin_unlock(&cache
->lock
);
491 cache
->cached
= BTRFS_CACHE_STARTED
;
492 spin_unlock(&cache
->lock
);
494 ret
= load_free_space_cache(fs_info
, cache
);
496 spin_lock(&cache
->lock
);
498 cache
->cached
= BTRFS_CACHE_FINISHED
;
499 cache
->last_byte_to_unpin
= (u64
)-1;
501 cache
->cached
= BTRFS_CACHE_NO
;
503 spin_unlock(&cache
->lock
);
505 free_excluded_extents(fs_info
->extent_root
, cache
);
513 caching_ctl
= kzalloc(sizeof(*caching_ctl
), GFP_NOFS
);
514 BUG_ON(!caching_ctl
);
516 INIT_LIST_HEAD(&caching_ctl
->list
);
517 mutex_init(&caching_ctl
->mutex
);
518 init_waitqueue_head(&caching_ctl
->wait
);
519 caching_ctl
->block_group
= cache
;
520 caching_ctl
->progress
= cache
->key
.objectid
;
521 /* one for caching kthread, one for caching block group list */
522 atomic_set(&caching_ctl
->count
, 2);
523 caching_ctl
->work
.func
= caching_thread
;
525 spin_lock(&cache
->lock
);
526 if (cache
->cached
!= BTRFS_CACHE_NO
) {
527 spin_unlock(&cache
->lock
);
531 cache
->caching_ctl
= caching_ctl
;
532 cache
->cached
= BTRFS_CACHE_STARTED
;
533 spin_unlock(&cache
->lock
);
535 down_write(&fs_info
->extent_commit_sem
);
536 list_add_tail(&caching_ctl
->list
, &fs_info
->caching_block_groups
);
537 up_write(&fs_info
->extent_commit_sem
);
539 btrfs_get_block_group(cache
);
541 btrfs_queue_worker(&fs_info
->caching_workers
, &caching_ctl
->work
);
547 * return the block group that starts at or after bytenr
549 static struct btrfs_block_group_cache
*
550 btrfs_lookup_first_block_group(struct btrfs_fs_info
*info
, u64 bytenr
)
552 struct btrfs_block_group_cache
*cache
;
554 cache
= block_group_cache_tree_search(info
, bytenr
, 0);
560 * return the block group that contains the given bytenr
562 struct btrfs_block_group_cache
*btrfs_lookup_block_group(
563 struct btrfs_fs_info
*info
,
566 struct btrfs_block_group_cache
*cache
;
568 cache
= block_group_cache_tree_search(info
, bytenr
, 1);
573 static struct btrfs_space_info
*__find_space_info(struct btrfs_fs_info
*info
,
576 struct list_head
*head
= &info
->space_info
;
577 struct btrfs_space_info
*found
;
579 flags
&= BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_SYSTEM
|
580 BTRFS_BLOCK_GROUP_METADATA
;
583 list_for_each_entry_rcu(found
, head
, list
) {
584 if (found
->flags
& flags
) {
594 * after adding space to the filesystem, we need to clear the full flags
595 * on all the space infos.
597 void btrfs_clear_space_info_full(struct btrfs_fs_info
*info
)
599 struct list_head
*head
= &info
->space_info
;
600 struct btrfs_space_info
*found
;
603 list_for_each_entry_rcu(found
, head
, list
)
608 static u64
div_factor(u64 num
, int factor
)
617 static u64
div_factor_fine(u64 num
, int factor
)
626 u64
btrfs_find_block_group(struct btrfs_root
*root
,
627 u64 search_start
, u64 search_hint
, int owner
)
629 struct btrfs_block_group_cache
*cache
;
631 u64 last
= max(search_hint
, search_start
);
638 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
642 spin_lock(&cache
->lock
);
643 last
= cache
->key
.objectid
+ cache
->key
.offset
;
644 used
= btrfs_block_group_used(&cache
->item
);
646 if ((full_search
|| !cache
->ro
) &&
647 block_group_bits(cache
, BTRFS_BLOCK_GROUP_METADATA
)) {
648 if (used
+ cache
->pinned
+ cache
->reserved
<
649 div_factor(cache
->key
.offset
, factor
)) {
650 group_start
= cache
->key
.objectid
;
651 spin_unlock(&cache
->lock
);
652 btrfs_put_block_group(cache
);
656 spin_unlock(&cache
->lock
);
657 btrfs_put_block_group(cache
);
665 if (!full_search
&& factor
< 10) {
675 /* simple helper to search for an existing extent at a given offset */
676 int btrfs_lookup_extent(struct btrfs_root
*root
, u64 start
, u64 len
)
679 struct btrfs_key key
;
680 struct btrfs_path
*path
;
682 path
= btrfs_alloc_path();
686 key
.objectid
= start
;
688 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
689 ret
= btrfs_search_slot(NULL
, root
->fs_info
->extent_root
, &key
, path
,
691 btrfs_free_path(path
);
696 * helper function to lookup reference count and flags of extent.
698 * the head node for delayed ref is used to store the sum of all the
699 * reference count modifications queued up in the rbtree. the head
700 * node may also store the extent flags to set. This way you can check
701 * to see what the reference count and extent flags would be if all of
702 * the delayed refs are not processed.
704 int btrfs_lookup_extent_info(struct btrfs_trans_handle
*trans
,
705 struct btrfs_root
*root
, u64 bytenr
,
706 u64 num_bytes
, u64
*refs
, u64
*flags
)
708 struct btrfs_delayed_ref_head
*head
;
709 struct btrfs_delayed_ref_root
*delayed_refs
;
710 struct btrfs_path
*path
;
711 struct btrfs_extent_item
*ei
;
712 struct extent_buffer
*leaf
;
713 struct btrfs_key key
;
719 path
= btrfs_alloc_path();
723 key
.objectid
= bytenr
;
724 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
725 key
.offset
= num_bytes
;
727 path
->skip_locking
= 1;
728 path
->search_commit_root
= 1;
731 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
,
737 leaf
= path
->nodes
[0];
738 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
739 if (item_size
>= sizeof(*ei
)) {
740 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
741 struct btrfs_extent_item
);
742 num_refs
= btrfs_extent_refs(leaf
, ei
);
743 extent_flags
= btrfs_extent_flags(leaf
, ei
);
745 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
746 struct btrfs_extent_item_v0
*ei0
;
747 BUG_ON(item_size
!= sizeof(*ei0
));
748 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
749 struct btrfs_extent_item_v0
);
750 num_refs
= btrfs_extent_refs_v0(leaf
, ei0
);
751 /* FIXME: this isn't correct for data */
752 extent_flags
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
757 BUG_ON(num_refs
== 0);
767 delayed_refs
= &trans
->transaction
->delayed_refs
;
768 spin_lock(&delayed_refs
->lock
);
769 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
771 if (!mutex_trylock(&head
->mutex
)) {
772 atomic_inc(&head
->node
.refs
);
773 spin_unlock(&delayed_refs
->lock
);
775 btrfs_release_path(path
);
778 * Mutex was contended, block until it's released and try
781 mutex_lock(&head
->mutex
);
782 mutex_unlock(&head
->mutex
);
783 btrfs_put_delayed_ref(&head
->node
);
786 if (head
->extent_op
&& head
->extent_op
->update_flags
)
787 extent_flags
|= head
->extent_op
->flags_to_set
;
789 BUG_ON(num_refs
== 0);
791 num_refs
+= head
->node
.ref_mod
;
792 mutex_unlock(&head
->mutex
);
794 spin_unlock(&delayed_refs
->lock
);
796 WARN_ON(num_refs
== 0);
800 *flags
= extent_flags
;
802 btrfs_free_path(path
);
807 * Back reference rules. Back refs have three main goals:
809 * 1) differentiate between all holders of references to an extent so that
810 * when a reference is dropped we can make sure it was a valid reference
811 * before freeing the extent.
813 * 2) Provide enough information to quickly find the holders of an extent
814 * if we notice a given block is corrupted or bad.
816 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
817 * maintenance. This is actually the same as #2, but with a slightly
818 * different use case.
820 * There are two kinds of back refs. The implicit back refs is optimized
821 * for pointers in non-shared tree blocks. For a given pointer in a block,
822 * back refs of this kind provide information about the block's owner tree
823 * and the pointer's key. These information allow us to find the block by
824 * b-tree searching. The full back refs is for pointers in tree blocks not
825 * referenced by their owner trees. The location of tree block is recorded
826 * in the back refs. Actually the full back refs is generic, and can be
827 * used in all cases the implicit back refs is used. The major shortcoming
828 * of the full back refs is its overhead. Every time a tree block gets
829 * COWed, we have to update back refs entry for all pointers in it.
831 * For a newly allocated tree block, we use implicit back refs for
832 * pointers in it. This means most tree related operations only involve
833 * implicit back refs. For a tree block created in old transaction, the
834 * only way to drop a reference to it is COW it. So we can detect the
835 * event that tree block loses its owner tree's reference and do the
836 * back refs conversion.
838 * When a tree block is COW'd through a tree, there are four cases:
840 * The reference count of the block is one and the tree is the block's
841 * owner tree. Nothing to do in this case.
843 * The reference count of the block is one and the tree is not the
844 * block's owner tree. In this case, full back refs is used for pointers
845 * in the block. Remove these full back refs, add implicit back refs for
846 * every pointers in the new block.
848 * The reference count of the block is greater than one and the tree is
849 * the block's owner tree. In this case, implicit back refs is used for
850 * pointers in the block. Add full back refs for every pointers in the
851 * block, increase lower level extents' reference counts. The original
852 * implicit back refs are entailed to the new block.
854 * The reference count of the block is greater than one and the tree is
855 * not the block's owner tree. Add implicit back refs for every pointer in
856 * the new block, increase lower level extents' reference count.
858 * Back Reference Key composing:
860 * The key objectid corresponds to the first byte in the extent,
861 * The key type is used to differentiate between types of back refs.
862 * There are different meanings of the key offset for different types
865 * File extents can be referenced by:
867 * - multiple snapshots, subvolumes, or different generations in one subvol
868 * - different files inside a single subvolume
869 * - different offsets inside a file (bookend extents in file.c)
871 * The extent ref structure for the implicit back refs has fields for:
873 * - Objectid of the subvolume root
874 * - objectid of the file holding the reference
875 * - original offset in the file
876 * - how many bookend extents
878 * The key offset for the implicit back refs is hash of the first
881 * The extent ref structure for the full back refs has field for:
883 * - number of pointers in the tree leaf
885 * The key offset for the implicit back refs is the first byte of
888 * When a file extent is allocated, The implicit back refs is used.
889 * the fields are filled in:
891 * (root_key.objectid, inode objectid, offset in file, 1)
893 * When a file extent is removed file truncation, we find the
894 * corresponding implicit back refs and check the following fields:
896 * (btrfs_header_owner(leaf), inode objectid, offset in file)
898 * Btree extents can be referenced by:
900 * - Different subvolumes
902 * Both the implicit back refs and the full back refs for tree blocks
903 * only consist of key. The key offset for the implicit back refs is
904 * objectid of block's owner tree. The key offset for the full back refs
905 * is the first byte of parent block.
907 * When implicit back refs is used, information about the lowest key and
908 * level of the tree block are required. These information are stored in
909 * tree block info structure.
912 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
913 static int convert_extent_item_v0(struct btrfs_trans_handle
*trans
,
914 struct btrfs_root
*root
,
915 struct btrfs_path
*path
,
916 u64 owner
, u32 extra_size
)
918 struct btrfs_extent_item
*item
;
919 struct btrfs_extent_item_v0
*ei0
;
920 struct btrfs_extent_ref_v0
*ref0
;
921 struct btrfs_tree_block_info
*bi
;
922 struct extent_buffer
*leaf
;
923 struct btrfs_key key
;
924 struct btrfs_key found_key
;
925 u32 new_size
= sizeof(*item
);
929 leaf
= path
->nodes
[0];
930 BUG_ON(btrfs_item_size_nr(leaf
, path
->slots
[0]) != sizeof(*ei0
));
932 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
933 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
934 struct btrfs_extent_item_v0
);
935 refs
= btrfs_extent_refs_v0(leaf
, ei0
);
937 if (owner
== (u64
)-1) {
939 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
940 ret
= btrfs_next_leaf(root
, path
);
944 leaf
= path
->nodes
[0];
946 btrfs_item_key_to_cpu(leaf
, &found_key
,
948 BUG_ON(key
.objectid
!= found_key
.objectid
);
949 if (found_key
.type
!= BTRFS_EXTENT_REF_V0_KEY
) {
953 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
954 struct btrfs_extent_ref_v0
);
955 owner
= btrfs_ref_objectid_v0(leaf
, ref0
);
959 btrfs_release_path(path
);
961 if (owner
< BTRFS_FIRST_FREE_OBJECTID
)
962 new_size
+= sizeof(*bi
);
964 new_size
-= sizeof(*ei0
);
965 ret
= btrfs_search_slot(trans
, root
, &key
, path
,
966 new_size
+ extra_size
, 1);
971 ret
= btrfs_extend_item(trans
, root
, path
, new_size
);
973 leaf
= path
->nodes
[0];
974 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
975 btrfs_set_extent_refs(leaf
, item
, refs
);
976 /* FIXME: get real generation */
977 btrfs_set_extent_generation(leaf
, item
, 0);
978 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
979 btrfs_set_extent_flags(leaf
, item
,
980 BTRFS_EXTENT_FLAG_TREE_BLOCK
|
981 BTRFS_BLOCK_FLAG_FULL_BACKREF
);
982 bi
= (struct btrfs_tree_block_info
*)(item
+ 1);
983 /* FIXME: get first key of the block */
984 memset_extent_buffer(leaf
, 0, (unsigned long)bi
, sizeof(*bi
));
985 btrfs_set_tree_block_level(leaf
, bi
, (int)owner
);
987 btrfs_set_extent_flags(leaf
, item
, BTRFS_EXTENT_FLAG_DATA
);
989 btrfs_mark_buffer_dirty(leaf
);
994 static u64
hash_extent_data_ref(u64 root_objectid
, u64 owner
, u64 offset
)
996 u32 high_crc
= ~(u32
)0;
997 u32 low_crc
= ~(u32
)0;
1000 lenum
= cpu_to_le64(root_objectid
);
1001 high_crc
= crc32c(high_crc
, &lenum
, sizeof(lenum
));
1002 lenum
= cpu_to_le64(owner
);
1003 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
1004 lenum
= cpu_to_le64(offset
);
1005 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
1007 return ((u64
)high_crc
<< 31) ^ (u64
)low_crc
;
1010 static u64
hash_extent_data_ref_item(struct extent_buffer
*leaf
,
1011 struct btrfs_extent_data_ref
*ref
)
1013 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf
, ref
),
1014 btrfs_extent_data_ref_objectid(leaf
, ref
),
1015 btrfs_extent_data_ref_offset(leaf
, ref
));
1018 static int match_extent_data_ref(struct extent_buffer
*leaf
,
1019 struct btrfs_extent_data_ref
*ref
,
1020 u64 root_objectid
, u64 owner
, u64 offset
)
1022 if (btrfs_extent_data_ref_root(leaf
, ref
) != root_objectid
||
1023 btrfs_extent_data_ref_objectid(leaf
, ref
) != owner
||
1024 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
1029 static noinline
int lookup_extent_data_ref(struct btrfs_trans_handle
*trans
,
1030 struct btrfs_root
*root
,
1031 struct btrfs_path
*path
,
1032 u64 bytenr
, u64 parent
,
1034 u64 owner
, u64 offset
)
1036 struct btrfs_key key
;
1037 struct btrfs_extent_data_ref
*ref
;
1038 struct extent_buffer
*leaf
;
1044 key
.objectid
= bytenr
;
1046 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
1047 key
.offset
= parent
;
1049 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
1050 key
.offset
= hash_extent_data_ref(root_objectid
,
1055 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1064 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1065 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
1066 btrfs_release_path(path
);
1067 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1078 leaf
= path
->nodes
[0];
1079 nritems
= btrfs_header_nritems(leaf
);
1081 if (path
->slots
[0] >= nritems
) {
1082 ret
= btrfs_next_leaf(root
, path
);
1088 leaf
= path
->nodes
[0];
1089 nritems
= btrfs_header_nritems(leaf
);
1093 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1094 if (key
.objectid
!= bytenr
||
1095 key
.type
!= BTRFS_EXTENT_DATA_REF_KEY
)
1098 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1099 struct btrfs_extent_data_ref
);
1101 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
1104 btrfs_release_path(path
);
1116 static noinline
int insert_extent_data_ref(struct btrfs_trans_handle
*trans
,
1117 struct btrfs_root
*root
,
1118 struct btrfs_path
*path
,
1119 u64 bytenr
, u64 parent
,
1120 u64 root_objectid
, u64 owner
,
1121 u64 offset
, int refs_to_add
)
1123 struct btrfs_key key
;
1124 struct extent_buffer
*leaf
;
1129 key
.objectid
= bytenr
;
1131 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
1132 key
.offset
= parent
;
1133 size
= sizeof(struct btrfs_shared_data_ref
);
1135 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
1136 key
.offset
= hash_extent_data_ref(root_objectid
,
1138 size
= sizeof(struct btrfs_extent_data_ref
);
1141 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, size
);
1142 if (ret
&& ret
!= -EEXIST
)
1145 leaf
= path
->nodes
[0];
1147 struct btrfs_shared_data_ref
*ref
;
1148 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1149 struct btrfs_shared_data_ref
);
1151 btrfs_set_shared_data_ref_count(leaf
, ref
, refs_to_add
);
1153 num_refs
= btrfs_shared_data_ref_count(leaf
, ref
);
1154 num_refs
+= refs_to_add
;
1155 btrfs_set_shared_data_ref_count(leaf
, ref
, num_refs
);
1158 struct btrfs_extent_data_ref
*ref
;
1159 while (ret
== -EEXIST
) {
1160 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1161 struct btrfs_extent_data_ref
);
1162 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
1165 btrfs_release_path(path
);
1167 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1169 if (ret
&& ret
!= -EEXIST
)
1172 leaf
= path
->nodes
[0];
1174 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1175 struct btrfs_extent_data_ref
);
1177 btrfs_set_extent_data_ref_root(leaf
, ref
,
1179 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
1180 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
1181 btrfs_set_extent_data_ref_count(leaf
, ref
, refs_to_add
);
1183 num_refs
= btrfs_extent_data_ref_count(leaf
, ref
);
1184 num_refs
+= refs_to_add
;
1185 btrfs_set_extent_data_ref_count(leaf
, ref
, num_refs
);
1188 btrfs_mark_buffer_dirty(leaf
);
1191 btrfs_release_path(path
);
1195 static noinline
int remove_extent_data_ref(struct btrfs_trans_handle
*trans
,
1196 struct btrfs_root
*root
,
1197 struct btrfs_path
*path
,
1200 struct btrfs_key key
;
1201 struct btrfs_extent_data_ref
*ref1
= NULL
;
1202 struct btrfs_shared_data_ref
*ref2
= NULL
;
1203 struct extent_buffer
*leaf
;
1207 leaf
= path
->nodes
[0];
1208 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1210 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1211 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
1212 struct btrfs_extent_data_ref
);
1213 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1214 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
1215 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
1216 struct btrfs_shared_data_ref
);
1217 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1218 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1219 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
1220 struct btrfs_extent_ref_v0
*ref0
;
1221 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1222 struct btrfs_extent_ref_v0
);
1223 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
1229 BUG_ON(num_refs
< refs_to_drop
);
1230 num_refs
-= refs_to_drop
;
1232 if (num_refs
== 0) {
1233 ret
= btrfs_del_item(trans
, root
, path
);
1235 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
)
1236 btrfs_set_extent_data_ref_count(leaf
, ref1
, num_refs
);
1237 else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
)
1238 btrfs_set_shared_data_ref_count(leaf
, ref2
, num_refs
);
1239 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1241 struct btrfs_extent_ref_v0
*ref0
;
1242 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1243 struct btrfs_extent_ref_v0
);
1244 btrfs_set_ref_count_v0(leaf
, ref0
, num_refs
);
1247 btrfs_mark_buffer_dirty(leaf
);
1252 static noinline u32
extent_data_ref_count(struct btrfs_root
*root
,
1253 struct btrfs_path
*path
,
1254 struct btrfs_extent_inline_ref
*iref
)
1256 struct btrfs_key key
;
1257 struct extent_buffer
*leaf
;
1258 struct btrfs_extent_data_ref
*ref1
;
1259 struct btrfs_shared_data_ref
*ref2
;
1262 leaf
= path
->nodes
[0];
1263 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1265 if (btrfs_extent_inline_ref_type(leaf
, iref
) ==
1266 BTRFS_EXTENT_DATA_REF_KEY
) {
1267 ref1
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1268 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1270 ref2
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1271 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1273 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1274 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
1275 struct btrfs_extent_data_ref
);
1276 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1277 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
1278 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
1279 struct btrfs_shared_data_ref
);
1280 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1281 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1282 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
1283 struct btrfs_extent_ref_v0
*ref0
;
1284 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1285 struct btrfs_extent_ref_v0
);
1286 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
1294 static noinline
int lookup_tree_block_ref(struct btrfs_trans_handle
*trans
,
1295 struct btrfs_root
*root
,
1296 struct btrfs_path
*path
,
1297 u64 bytenr
, u64 parent
,
1300 struct btrfs_key key
;
1303 key
.objectid
= bytenr
;
1305 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1306 key
.offset
= parent
;
1308 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
1309 key
.offset
= root_objectid
;
1312 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1315 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1316 if (ret
== -ENOENT
&& parent
) {
1317 btrfs_release_path(path
);
1318 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
1319 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1327 static noinline
int insert_tree_block_ref(struct btrfs_trans_handle
*trans
,
1328 struct btrfs_root
*root
,
1329 struct btrfs_path
*path
,
1330 u64 bytenr
, u64 parent
,
1333 struct btrfs_key key
;
1336 key
.objectid
= bytenr
;
1338 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1339 key
.offset
= parent
;
1341 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
1342 key
.offset
= root_objectid
;
1345 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, 0);
1346 btrfs_release_path(path
);
1350 static inline int extent_ref_type(u64 parent
, u64 owner
)
1353 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1355 type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1357 type
= BTRFS_TREE_BLOCK_REF_KEY
;
1360 type
= BTRFS_SHARED_DATA_REF_KEY
;
1362 type
= BTRFS_EXTENT_DATA_REF_KEY
;
1367 static int find_next_key(struct btrfs_path
*path
, int level
,
1368 struct btrfs_key
*key
)
1371 for (; level
< BTRFS_MAX_LEVEL
; level
++) {
1372 if (!path
->nodes
[level
])
1374 if (path
->slots
[level
] + 1 >=
1375 btrfs_header_nritems(path
->nodes
[level
]))
1378 btrfs_item_key_to_cpu(path
->nodes
[level
], key
,
1379 path
->slots
[level
] + 1);
1381 btrfs_node_key_to_cpu(path
->nodes
[level
], key
,
1382 path
->slots
[level
] + 1);
1389 * look for inline back ref. if back ref is found, *ref_ret is set
1390 * to the address of inline back ref, and 0 is returned.
1392 * if back ref isn't found, *ref_ret is set to the address where it
1393 * should be inserted, and -ENOENT is returned.
1395 * if insert is true and there are too many inline back refs, the path
1396 * points to the extent item, and -EAGAIN is returned.
1398 * NOTE: inline back refs are ordered in the same way that back ref
1399 * items in the tree are ordered.
1401 static noinline_for_stack
1402 int lookup_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1403 struct btrfs_root
*root
,
1404 struct btrfs_path
*path
,
1405 struct btrfs_extent_inline_ref
**ref_ret
,
1406 u64 bytenr
, u64 num_bytes
,
1407 u64 parent
, u64 root_objectid
,
1408 u64 owner
, u64 offset
, int insert
)
1410 struct btrfs_key key
;
1411 struct extent_buffer
*leaf
;
1412 struct btrfs_extent_item
*ei
;
1413 struct btrfs_extent_inline_ref
*iref
;
1424 key
.objectid
= bytenr
;
1425 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1426 key
.offset
= num_bytes
;
1428 want
= extent_ref_type(parent
, owner
);
1430 extra_size
= btrfs_extent_inline_ref_size(want
);
1431 path
->keep_locks
= 1;
1434 ret
= btrfs_search_slot(trans
, root
, &key
, path
, extra_size
, 1);
1441 leaf
= path
->nodes
[0];
1442 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1443 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1444 if (item_size
< sizeof(*ei
)) {
1449 ret
= convert_extent_item_v0(trans
, root
, path
, owner
,
1455 leaf
= path
->nodes
[0];
1456 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1459 BUG_ON(item_size
< sizeof(*ei
));
1461 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1462 flags
= btrfs_extent_flags(leaf
, ei
);
1464 ptr
= (unsigned long)(ei
+ 1);
1465 end
= (unsigned long)ei
+ item_size
;
1467 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
1468 ptr
+= sizeof(struct btrfs_tree_block_info
);
1471 BUG_ON(!(flags
& BTRFS_EXTENT_FLAG_DATA
));
1480 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1481 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1485 ptr
+= btrfs_extent_inline_ref_size(type
);
1489 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1490 struct btrfs_extent_data_ref
*dref
;
1491 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1492 if (match_extent_data_ref(leaf
, dref
, root_objectid
,
1497 if (hash_extent_data_ref_item(leaf
, dref
) <
1498 hash_extent_data_ref(root_objectid
, owner
, offset
))
1502 ref_offset
= btrfs_extent_inline_ref_offset(leaf
, iref
);
1504 if (parent
== ref_offset
) {
1508 if (ref_offset
< parent
)
1511 if (root_objectid
== ref_offset
) {
1515 if (ref_offset
< root_objectid
)
1519 ptr
+= btrfs_extent_inline_ref_size(type
);
1521 if (err
== -ENOENT
&& insert
) {
1522 if (item_size
+ extra_size
>=
1523 BTRFS_MAX_EXTENT_ITEM_SIZE(root
)) {
1528 * To add new inline back ref, we have to make sure
1529 * there is no corresponding back ref item.
1530 * For simplicity, we just do not add new inline back
1531 * ref if there is any kind of item for this block
1533 if (find_next_key(path
, 0, &key
) == 0 &&
1534 key
.objectid
== bytenr
&&
1535 key
.type
< BTRFS_BLOCK_GROUP_ITEM_KEY
) {
1540 *ref_ret
= (struct btrfs_extent_inline_ref
*)ptr
;
1543 path
->keep_locks
= 0;
1544 btrfs_unlock_up_safe(path
, 1);
1550 * helper to add new inline back ref
1552 static noinline_for_stack
1553 int setup_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1554 struct btrfs_root
*root
,
1555 struct btrfs_path
*path
,
1556 struct btrfs_extent_inline_ref
*iref
,
1557 u64 parent
, u64 root_objectid
,
1558 u64 owner
, u64 offset
, int refs_to_add
,
1559 struct btrfs_delayed_extent_op
*extent_op
)
1561 struct extent_buffer
*leaf
;
1562 struct btrfs_extent_item
*ei
;
1565 unsigned long item_offset
;
1571 leaf
= path
->nodes
[0];
1572 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1573 item_offset
= (unsigned long)iref
- (unsigned long)ei
;
1575 type
= extent_ref_type(parent
, owner
);
1576 size
= btrfs_extent_inline_ref_size(type
);
1578 ret
= btrfs_extend_item(trans
, root
, path
, size
);
1580 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1581 refs
= btrfs_extent_refs(leaf
, ei
);
1582 refs
+= refs_to_add
;
1583 btrfs_set_extent_refs(leaf
, ei
, refs
);
1585 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1587 ptr
= (unsigned long)ei
+ item_offset
;
1588 end
= (unsigned long)ei
+ btrfs_item_size_nr(leaf
, path
->slots
[0]);
1589 if (ptr
< end
- size
)
1590 memmove_extent_buffer(leaf
, ptr
+ size
, ptr
,
1593 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1594 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
1595 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1596 struct btrfs_extent_data_ref
*dref
;
1597 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1598 btrfs_set_extent_data_ref_root(leaf
, dref
, root_objectid
);
1599 btrfs_set_extent_data_ref_objectid(leaf
, dref
, owner
);
1600 btrfs_set_extent_data_ref_offset(leaf
, dref
, offset
);
1601 btrfs_set_extent_data_ref_count(leaf
, dref
, refs_to_add
);
1602 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1603 struct btrfs_shared_data_ref
*sref
;
1604 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1605 btrfs_set_shared_data_ref_count(leaf
, sref
, refs_to_add
);
1606 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1607 } else if (type
== BTRFS_SHARED_BLOCK_REF_KEY
) {
1608 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1610 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
1612 btrfs_mark_buffer_dirty(leaf
);
1616 static int lookup_extent_backref(struct btrfs_trans_handle
*trans
,
1617 struct btrfs_root
*root
,
1618 struct btrfs_path
*path
,
1619 struct btrfs_extent_inline_ref
**ref_ret
,
1620 u64 bytenr
, u64 num_bytes
, u64 parent
,
1621 u64 root_objectid
, u64 owner
, u64 offset
)
1625 ret
= lookup_inline_extent_backref(trans
, root
, path
, ref_ret
,
1626 bytenr
, num_bytes
, parent
,
1627 root_objectid
, owner
, offset
, 0);
1631 btrfs_release_path(path
);
1634 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1635 ret
= lookup_tree_block_ref(trans
, root
, path
, bytenr
, parent
,
1638 ret
= lookup_extent_data_ref(trans
, root
, path
, bytenr
, parent
,
1639 root_objectid
, owner
, offset
);
1645 * helper to update/remove inline back ref
1647 static noinline_for_stack
1648 int update_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1649 struct btrfs_root
*root
,
1650 struct btrfs_path
*path
,
1651 struct btrfs_extent_inline_ref
*iref
,
1653 struct btrfs_delayed_extent_op
*extent_op
)
1655 struct extent_buffer
*leaf
;
1656 struct btrfs_extent_item
*ei
;
1657 struct btrfs_extent_data_ref
*dref
= NULL
;
1658 struct btrfs_shared_data_ref
*sref
= NULL
;
1667 leaf
= path
->nodes
[0];
1668 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1669 refs
= btrfs_extent_refs(leaf
, ei
);
1670 WARN_ON(refs_to_mod
< 0 && refs
+ refs_to_mod
<= 0);
1671 refs
+= refs_to_mod
;
1672 btrfs_set_extent_refs(leaf
, ei
, refs
);
1674 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1676 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1678 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1679 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1680 refs
= btrfs_extent_data_ref_count(leaf
, dref
);
1681 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1682 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1683 refs
= btrfs_shared_data_ref_count(leaf
, sref
);
1686 BUG_ON(refs_to_mod
!= -1);
1689 BUG_ON(refs_to_mod
< 0 && refs
< -refs_to_mod
);
1690 refs
+= refs_to_mod
;
1693 if (type
== BTRFS_EXTENT_DATA_REF_KEY
)
1694 btrfs_set_extent_data_ref_count(leaf
, dref
, refs
);
1696 btrfs_set_shared_data_ref_count(leaf
, sref
, refs
);
1698 size
= btrfs_extent_inline_ref_size(type
);
1699 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1700 ptr
= (unsigned long)iref
;
1701 end
= (unsigned long)ei
+ item_size
;
1702 if (ptr
+ size
< end
)
1703 memmove_extent_buffer(leaf
, ptr
, ptr
+ size
,
1706 ret
= btrfs_truncate_item(trans
, root
, path
, item_size
, 1);
1708 btrfs_mark_buffer_dirty(leaf
);
1712 static noinline_for_stack
1713 int insert_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1714 struct btrfs_root
*root
,
1715 struct btrfs_path
*path
,
1716 u64 bytenr
, u64 num_bytes
, u64 parent
,
1717 u64 root_objectid
, u64 owner
,
1718 u64 offset
, int refs_to_add
,
1719 struct btrfs_delayed_extent_op
*extent_op
)
1721 struct btrfs_extent_inline_ref
*iref
;
1724 ret
= lookup_inline_extent_backref(trans
, root
, path
, &iref
,
1725 bytenr
, num_bytes
, parent
,
1726 root_objectid
, owner
, offset
, 1);
1728 BUG_ON(owner
< BTRFS_FIRST_FREE_OBJECTID
);
1729 ret
= update_inline_extent_backref(trans
, root
, path
, iref
,
1730 refs_to_add
, extent_op
);
1731 } else if (ret
== -ENOENT
) {
1732 ret
= setup_inline_extent_backref(trans
, root
, path
, iref
,
1733 parent
, root_objectid
,
1734 owner
, offset
, refs_to_add
,
1740 static int insert_extent_backref(struct btrfs_trans_handle
*trans
,
1741 struct btrfs_root
*root
,
1742 struct btrfs_path
*path
,
1743 u64 bytenr
, u64 parent
, u64 root_objectid
,
1744 u64 owner
, u64 offset
, int refs_to_add
)
1747 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1748 BUG_ON(refs_to_add
!= 1);
1749 ret
= insert_tree_block_ref(trans
, root
, path
, bytenr
,
1750 parent
, root_objectid
);
1752 ret
= insert_extent_data_ref(trans
, root
, path
, bytenr
,
1753 parent
, root_objectid
,
1754 owner
, offset
, refs_to_add
);
1759 static int remove_extent_backref(struct btrfs_trans_handle
*trans
,
1760 struct btrfs_root
*root
,
1761 struct btrfs_path
*path
,
1762 struct btrfs_extent_inline_ref
*iref
,
1763 int refs_to_drop
, int is_data
)
1767 BUG_ON(!is_data
&& refs_to_drop
!= 1);
1769 ret
= update_inline_extent_backref(trans
, root
, path
, iref
,
1770 -refs_to_drop
, NULL
);
1771 } else if (is_data
) {
1772 ret
= remove_extent_data_ref(trans
, root
, path
, refs_to_drop
);
1774 ret
= btrfs_del_item(trans
, root
, path
);
1779 static int btrfs_issue_discard(struct block_device
*bdev
,
1782 return blkdev_issue_discard(bdev
, start
>> 9, len
>> 9, GFP_NOFS
, 0);
1785 static int btrfs_discard_extent(struct btrfs_root
*root
, u64 bytenr
,
1786 u64 num_bytes
, u64
*actual_bytes
)
1789 u64 discarded_bytes
= 0;
1790 struct btrfs_multi_bio
*multi
= NULL
;
1793 /* Tell the block device(s) that the sectors can be discarded */
1794 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, REQ_DISCARD
,
1795 bytenr
, &num_bytes
, &multi
, 0);
1797 struct btrfs_bio_stripe
*stripe
= multi
->stripes
;
1801 for (i
= 0; i
< multi
->num_stripes
; i
++, stripe
++) {
1802 if (!stripe
->dev
->can_discard
)
1805 ret
= btrfs_issue_discard(stripe
->dev
->bdev
,
1809 discarded_bytes
+= stripe
->length
;
1810 else if (ret
!= -EOPNOTSUPP
)
1814 * Just in case we get back EOPNOTSUPP for some reason,
1815 * just ignore the return value so we don't screw up
1816 * people calling discard_extent.
1824 *actual_bytes
= discarded_bytes
;
1830 int btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
1831 struct btrfs_root
*root
,
1832 u64 bytenr
, u64 num_bytes
, u64 parent
,
1833 u64 root_objectid
, u64 owner
, u64 offset
)
1836 BUG_ON(owner
< BTRFS_FIRST_FREE_OBJECTID
&&
1837 root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
1839 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1840 ret
= btrfs_add_delayed_tree_ref(trans
, bytenr
, num_bytes
,
1841 parent
, root_objectid
, (int)owner
,
1842 BTRFS_ADD_DELAYED_REF
, NULL
);
1844 ret
= btrfs_add_delayed_data_ref(trans
, bytenr
, num_bytes
,
1845 parent
, root_objectid
, owner
, offset
,
1846 BTRFS_ADD_DELAYED_REF
, NULL
);
1851 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
1852 struct btrfs_root
*root
,
1853 u64 bytenr
, u64 num_bytes
,
1854 u64 parent
, u64 root_objectid
,
1855 u64 owner
, u64 offset
, int refs_to_add
,
1856 struct btrfs_delayed_extent_op
*extent_op
)
1858 struct btrfs_path
*path
;
1859 struct extent_buffer
*leaf
;
1860 struct btrfs_extent_item
*item
;
1865 path
= btrfs_alloc_path();
1870 path
->leave_spinning
= 1;
1871 /* this will setup the path even if it fails to insert the back ref */
1872 ret
= insert_inline_extent_backref(trans
, root
->fs_info
->extent_root
,
1873 path
, bytenr
, num_bytes
, parent
,
1874 root_objectid
, owner
, offset
,
1875 refs_to_add
, extent_op
);
1879 if (ret
!= -EAGAIN
) {
1884 leaf
= path
->nodes
[0];
1885 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1886 refs
= btrfs_extent_refs(leaf
, item
);
1887 btrfs_set_extent_refs(leaf
, item
, refs
+ refs_to_add
);
1889 __run_delayed_extent_op(extent_op
, leaf
, item
);
1891 btrfs_mark_buffer_dirty(leaf
);
1892 btrfs_release_path(path
);
1895 path
->leave_spinning
= 1;
1897 /* now insert the actual backref */
1898 ret
= insert_extent_backref(trans
, root
->fs_info
->extent_root
,
1899 path
, bytenr
, parent
, root_objectid
,
1900 owner
, offset
, refs_to_add
);
1903 btrfs_free_path(path
);
1907 static int run_delayed_data_ref(struct btrfs_trans_handle
*trans
,
1908 struct btrfs_root
*root
,
1909 struct btrfs_delayed_ref_node
*node
,
1910 struct btrfs_delayed_extent_op
*extent_op
,
1911 int insert_reserved
)
1914 struct btrfs_delayed_data_ref
*ref
;
1915 struct btrfs_key ins
;
1920 ins
.objectid
= node
->bytenr
;
1921 ins
.offset
= node
->num_bytes
;
1922 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1924 ref
= btrfs_delayed_node_to_data_ref(node
);
1925 if (node
->type
== BTRFS_SHARED_DATA_REF_KEY
)
1926 parent
= ref
->parent
;
1928 ref_root
= ref
->root
;
1930 if (node
->action
== BTRFS_ADD_DELAYED_REF
&& insert_reserved
) {
1932 BUG_ON(extent_op
->update_key
);
1933 flags
|= extent_op
->flags_to_set
;
1935 ret
= alloc_reserved_file_extent(trans
, root
,
1936 parent
, ref_root
, flags
,
1937 ref
->objectid
, ref
->offset
,
1938 &ins
, node
->ref_mod
);
1939 } else if (node
->action
== BTRFS_ADD_DELAYED_REF
) {
1940 ret
= __btrfs_inc_extent_ref(trans
, root
, node
->bytenr
,
1941 node
->num_bytes
, parent
,
1942 ref_root
, ref
->objectid
,
1943 ref
->offset
, node
->ref_mod
,
1945 } else if (node
->action
== BTRFS_DROP_DELAYED_REF
) {
1946 ret
= __btrfs_free_extent(trans
, root
, node
->bytenr
,
1947 node
->num_bytes
, parent
,
1948 ref_root
, ref
->objectid
,
1949 ref
->offset
, node
->ref_mod
,
1957 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op
*extent_op
,
1958 struct extent_buffer
*leaf
,
1959 struct btrfs_extent_item
*ei
)
1961 u64 flags
= btrfs_extent_flags(leaf
, ei
);
1962 if (extent_op
->update_flags
) {
1963 flags
|= extent_op
->flags_to_set
;
1964 btrfs_set_extent_flags(leaf
, ei
, flags
);
1967 if (extent_op
->update_key
) {
1968 struct btrfs_tree_block_info
*bi
;
1969 BUG_ON(!(flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
));
1970 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
1971 btrfs_set_tree_block_key(leaf
, bi
, &extent_op
->key
);
1975 static int run_delayed_extent_op(struct btrfs_trans_handle
*trans
,
1976 struct btrfs_root
*root
,
1977 struct btrfs_delayed_ref_node
*node
,
1978 struct btrfs_delayed_extent_op
*extent_op
)
1980 struct btrfs_key key
;
1981 struct btrfs_path
*path
;
1982 struct btrfs_extent_item
*ei
;
1983 struct extent_buffer
*leaf
;
1988 path
= btrfs_alloc_path();
1992 key
.objectid
= node
->bytenr
;
1993 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1994 key
.offset
= node
->num_bytes
;
1997 path
->leave_spinning
= 1;
1998 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
,
2009 leaf
= path
->nodes
[0];
2010 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2011 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2012 if (item_size
< sizeof(*ei
)) {
2013 ret
= convert_extent_item_v0(trans
, root
->fs_info
->extent_root
,
2019 leaf
= path
->nodes
[0];
2020 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2023 BUG_ON(item_size
< sizeof(*ei
));
2024 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
2025 __run_delayed_extent_op(extent_op
, leaf
, ei
);
2027 btrfs_mark_buffer_dirty(leaf
);
2029 btrfs_free_path(path
);
2033 static int run_delayed_tree_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 struct btrfs_delayed_tree_ref
*ref
;
2041 struct btrfs_key ins
;
2045 ins
.objectid
= node
->bytenr
;
2046 ins
.offset
= node
->num_bytes
;
2047 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
2049 ref
= btrfs_delayed_node_to_tree_ref(node
);
2050 if (node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
)
2051 parent
= ref
->parent
;
2053 ref_root
= ref
->root
;
2055 BUG_ON(node
->ref_mod
!= 1);
2056 if (node
->action
== BTRFS_ADD_DELAYED_REF
&& insert_reserved
) {
2057 BUG_ON(!extent_op
|| !extent_op
->update_flags
||
2058 !extent_op
->update_key
);
2059 ret
= alloc_reserved_tree_block(trans
, root
,
2061 extent_op
->flags_to_set
,
2064 } else if (node
->action
== BTRFS_ADD_DELAYED_REF
) {
2065 ret
= __btrfs_inc_extent_ref(trans
, root
, node
->bytenr
,
2066 node
->num_bytes
, parent
, ref_root
,
2067 ref
->level
, 0, 1, extent_op
);
2068 } else if (node
->action
== BTRFS_DROP_DELAYED_REF
) {
2069 ret
= __btrfs_free_extent(trans
, root
, node
->bytenr
,
2070 node
->num_bytes
, parent
, ref_root
,
2071 ref
->level
, 0, 1, extent_op
);
2078 /* helper function to actually process a single delayed ref entry */
2079 static int run_one_delayed_ref(struct btrfs_trans_handle
*trans
,
2080 struct btrfs_root
*root
,
2081 struct btrfs_delayed_ref_node
*node
,
2082 struct btrfs_delayed_extent_op
*extent_op
,
2083 int insert_reserved
)
2086 if (btrfs_delayed_ref_is_head(node
)) {
2087 struct btrfs_delayed_ref_head
*head
;
2089 * we've hit the end of the chain and we were supposed
2090 * to insert this extent into the tree. But, it got
2091 * deleted before we ever needed to insert it, so all
2092 * we have to do is clean up the accounting
2095 head
= btrfs_delayed_node_to_head(node
);
2096 if (insert_reserved
) {
2097 btrfs_pin_extent(root
, node
->bytenr
,
2098 node
->num_bytes
, 1);
2099 if (head
->is_data
) {
2100 ret
= btrfs_del_csums(trans
, root
,
2106 mutex_unlock(&head
->mutex
);
2110 if (node
->type
== BTRFS_TREE_BLOCK_REF_KEY
||
2111 node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
)
2112 ret
= run_delayed_tree_ref(trans
, root
, node
, extent_op
,
2114 else if (node
->type
== BTRFS_EXTENT_DATA_REF_KEY
||
2115 node
->type
== BTRFS_SHARED_DATA_REF_KEY
)
2116 ret
= run_delayed_data_ref(trans
, root
, node
, extent_op
,
2123 static noinline
struct btrfs_delayed_ref_node
*
2124 select_delayed_ref(struct btrfs_delayed_ref_head
*head
)
2126 struct rb_node
*node
;
2127 struct btrfs_delayed_ref_node
*ref
;
2128 int action
= BTRFS_ADD_DELAYED_REF
;
2131 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2132 * this prevents ref count from going down to zero when
2133 * there still are pending delayed ref.
2135 node
= rb_prev(&head
->node
.rb_node
);
2139 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
,
2141 if (ref
->bytenr
!= head
->node
.bytenr
)
2143 if (ref
->action
== action
)
2145 node
= rb_prev(node
);
2147 if (action
== BTRFS_ADD_DELAYED_REF
) {
2148 action
= BTRFS_DROP_DELAYED_REF
;
2154 static noinline
int run_clustered_refs(struct btrfs_trans_handle
*trans
,
2155 struct btrfs_root
*root
,
2156 struct list_head
*cluster
)
2158 struct btrfs_delayed_ref_root
*delayed_refs
;
2159 struct btrfs_delayed_ref_node
*ref
;
2160 struct btrfs_delayed_ref_head
*locked_ref
= NULL
;
2161 struct btrfs_delayed_extent_op
*extent_op
;
2164 int must_insert_reserved
= 0;
2166 delayed_refs
= &trans
->transaction
->delayed_refs
;
2169 /* pick a new head ref from the cluster list */
2170 if (list_empty(cluster
))
2173 locked_ref
= list_entry(cluster
->next
,
2174 struct btrfs_delayed_ref_head
, cluster
);
2176 /* grab the lock that says we are going to process
2177 * all the refs for this head */
2178 ret
= btrfs_delayed_ref_lock(trans
, locked_ref
);
2181 * we may have dropped the spin lock to get the head
2182 * mutex lock, and that might have given someone else
2183 * time to free the head. If that's true, it has been
2184 * removed from our list and we can move on.
2186 if (ret
== -EAGAIN
) {
2194 * record the must insert reserved flag before we
2195 * drop the spin lock.
2197 must_insert_reserved
= locked_ref
->must_insert_reserved
;
2198 locked_ref
->must_insert_reserved
= 0;
2200 extent_op
= locked_ref
->extent_op
;
2201 locked_ref
->extent_op
= NULL
;
2204 * locked_ref is the head node, so we have to go one
2205 * node back for any delayed ref updates
2207 ref
= select_delayed_ref(locked_ref
);
2209 /* All delayed refs have been processed, Go ahead
2210 * and send the head node to run_one_delayed_ref,
2211 * so that any accounting fixes can happen
2213 ref
= &locked_ref
->node
;
2215 if (extent_op
&& must_insert_reserved
) {
2221 spin_unlock(&delayed_refs
->lock
);
2223 ret
= run_delayed_extent_op(trans
, root
,
2229 spin_lock(&delayed_refs
->lock
);
2233 list_del_init(&locked_ref
->cluster
);
2238 rb_erase(&ref
->rb_node
, &delayed_refs
->root
);
2239 delayed_refs
->num_entries
--;
2241 spin_unlock(&delayed_refs
->lock
);
2243 ret
= run_one_delayed_ref(trans
, root
, ref
, extent_op
,
2244 must_insert_reserved
);
2247 btrfs_put_delayed_ref(ref
);
2252 spin_lock(&delayed_refs
->lock
);
2258 * this starts processing the delayed reference count updates and
2259 * extent insertions we have queued up so far. count can be
2260 * 0, which means to process everything in the tree at the start
2261 * of the run (but not newly added entries), or it can be some target
2262 * number you'd like to process.
2264 int btrfs_run_delayed_refs(struct btrfs_trans_handle
*trans
,
2265 struct btrfs_root
*root
, unsigned long count
)
2267 struct rb_node
*node
;
2268 struct btrfs_delayed_ref_root
*delayed_refs
;
2269 struct btrfs_delayed_ref_node
*ref
;
2270 struct list_head cluster
;
2272 int run_all
= count
== (unsigned long)-1;
2275 if (root
== root
->fs_info
->extent_root
)
2276 root
= root
->fs_info
->tree_root
;
2278 delayed_refs
= &trans
->transaction
->delayed_refs
;
2279 INIT_LIST_HEAD(&cluster
);
2281 spin_lock(&delayed_refs
->lock
);
2283 count
= delayed_refs
->num_entries
* 2;
2287 if (!(run_all
|| run_most
) &&
2288 delayed_refs
->num_heads_ready
< 64)
2292 * go find something we can process in the rbtree. We start at
2293 * the beginning of the tree, and then build a cluster
2294 * of refs to process starting at the first one we are able to
2297 ret
= btrfs_find_ref_cluster(trans
, &cluster
,
2298 delayed_refs
->run_delayed_start
);
2302 ret
= run_clustered_refs(trans
, root
, &cluster
);
2305 count
-= min_t(unsigned long, ret
, count
);
2312 node
= rb_first(&delayed_refs
->root
);
2315 count
= (unsigned long)-1;
2318 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
,
2320 if (btrfs_delayed_ref_is_head(ref
)) {
2321 struct btrfs_delayed_ref_head
*head
;
2323 head
= btrfs_delayed_node_to_head(ref
);
2324 atomic_inc(&ref
->refs
);
2326 spin_unlock(&delayed_refs
->lock
);
2328 * Mutex was contended, block until it's
2329 * released and try again
2331 mutex_lock(&head
->mutex
);
2332 mutex_unlock(&head
->mutex
);
2334 btrfs_put_delayed_ref(ref
);
2338 node
= rb_next(node
);
2340 spin_unlock(&delayed_refs
->lock
);
2341 schedule_timeout(1);
2345 spin_unlock(&delayed_refs
->lock
);
2349 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle
*trans
,
2350 struct btrfs_root
*root
,
2351 u64 bytenr
, u64 num_bytes
, u64 flags
,
2354 struct btrfs_delayed_extent_op
*extent_op
;
2357 extent_op
= kmalloc(sizeof(*extent_op
), GFP_NOFS
);
2361 extent_op
->flags_to_set
= flags
;
2362 extent_op
->update_flags
= 1;
2363 extent_op
->update_key
= 0;
2364 extent_op
->is_data
= is_data
? 1 : 0;
2366 ret
= btrfs_add_delayed_extent_op(trans
, bytenr
, num_bytes
, extent_op
);
2372 static noinline
int check_delayed_ref(struct btrfs_trans_handle
*trans
,
2373 struct btrfs_root
*root
,
2374 struct btrfs_path
*path
,
2375 u64 objectid
, u64 offset
, u64 bytenr
)
2377 struct btrfs_delayed_ref_head
*head
;
2378 struct btrfs_delayed_ref_node
*ref
;
2379 struct btrfs_delayed_data_ref
*data_ref
;
2380 struct btrfs_delayed_ref_root
*delayed_refs
;
2381 struct rb_node
*node
;
2385 delayed_refs
= &trans
->transaction
->delayed_refs
;
2386 spin_lock(&delayed_refs
->lock
);
2387 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
2391 if (!mutex_trylock(&head
->mutex
)) {
2392 atomic_inc(&head
->node
.refs
);
2393 spin_unlock(&delayed_refs
->lock
);
2395 btrfs_release_path(path
);
2398 * Mutex was contended, block until it's released and let
2401 mutex_lock(&head
->mutex
);
2402 mutex_unlock(&head
->mutex
);
2403 btrfs_put_delayed_ref(&head
->node
);
2407 node
= rb_prev(&head
->node
.rb_node
);
2411 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
2413 if (ref
->bytenr
!= bytenr
)
2417 if (ref
->type
!= BTRFS_EXTENT_DATA_REF_KEY
)
2420 data_ref
= btrfs_delayed_node_to_data_ref(ref
);
2422 node
= rb_prev(node
);
2424 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
2425 if (ref
->bytenr
== bytenr
)
2429 if (data_ref
->root
!= root
->root_key
.objectid
||
2430 data_ref
->objectid
!= objectid
|| data_ref
->offset
!= offset
)
2435 mutex_unlock(&head
->mutex
);
2437 spin_unlock(&delayed_refs
->lock
);
2441 static noinline
int check_committed_ref(struct btrfs_trans_handle
*trans
,
2442 struct btrfs_root
*root
,
2443 struct btrfs_path
*path
,
2444 u64 objectid
, u64 offset
, u64 bytenr
)
2446 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
2447 struct extent_buffer
*leaf
;
2448 struct btrfs_extent_data_ref
*ref
;
2449 struct btrfs_extent_inline_ref
*iref
;
2450 struct btrfs_extent_item
*ei
;
2451 struct btrfs_key key
;
2455 key
.objectid
= bytenr
;
2456 key
.offset
= (u64
)-1;
2457 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2459 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
2465 if (path
->slots
[0] == 0)
2469 leaf
= path
->nodes
[0];
2470 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2472 if (key
.objectid
!= bytenr
|| key
.type
!= BTRFS_EXTENT_ITEM_KEY
)
2476 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2477 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2478 if (item_size
< sizeof(*ei
)) {
2479 WARN_ON(item_size
!= sizeof(struct btrfs_extent_item_v0
));
2483 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
2485 if (item_size
!= sizeof(*ei
) +
2486 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY
))
2489 if (btrfs_extent_generation(leaf
, ei
) <=
2490 btrfs_root_last_snapshot(&root
->root_item
))
2493 iref
= (struct btrfs_extent_inline_ref
*)(ei
+ 1);
2494 if (btrfs_extent_inline_ref_type(leaf
, iref
) !=
2495 BTRFS_EXTENT_DATA_REF_KEY
)
2498 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
2499 if (btrfs_extent_refs(leaf
, ei
) !=
2500 btrfs_extent_data_ref_count(leaf
, ref
) ||
2501 btrfs_extent_data_ref_root(leaf
, ref
) !=
2502 root
->root_key
.objectid
||
2503 btrfs_extent_data_ref_objectid(leaf
, ref
) != objectid
||
2504 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
2512 int btrfs_cross_ref_exist(struct btrfs_trans_handle
*trans
,
2513 struct btrfs_root
*root
,
2514 u64 objectid
, u64 offset
, u64 bytenr
)
2516 struct btrfs_path
*path
;
2520 path
= btrfs_alloc_path();
2525 ret
= check_committed_ref(trans
, root
, path
, objectid
,
2527 if (ret
&& ret
!= -ENOENT
)
2530 ret2
= check_delayed_ref(trans
, root
, path
, objectid
,
2532 } while (ret2
== -EAGAIN
);
2534 if (ret2
&& ret2
!= -ENOENT
) {
2539 if (ret
!= -ENOENT
|| ret2
!= -ENOENT
)
2542 btrfs_free_path(path
);
2543 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
2548 static int __btrfs_mod_ref(struct btrfs_trans_handle
*trans
,
2549 struct btrfs_root
*root
,
2550 struct extent_buffer
*buf
,
2551 int full_backref
, int inc
)
2558 struct btrfs_key key
;
2559 struct btrfs_file_extent_item
*fi
;
2563 int (*process_func
)(struct btrfs_trans_handle
*, struct btrfs_root
*,
2564 u64
, u64
, u64
, u64
, u64
, u64
);
2566 ref_root
= btrfs_header_owner(buf
);
2567 nritems
= btrfs_header_nritems(buf
);
2568 level
= btrfs_header_level(buf
);
2570 if (!root
->ref_cows
&& level
== 0)
2574 process_func
= btrfs_inc_extent_ref
;
2576 process_func
= btrfs_free_extent
;
2579 parent
= buf
->start
;
2583 for (i
= 0; i
< nritems
; i
++) {
2585 btrfs_item_key_to_cpu(buf
, &key
, i
);
2586 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
2588 fi
= btrfs_item_ptr(buf
, i
,
2589 struct btrfs_file_extent_item
);
2590 if (btrfs_file_extent_type(buf
, fi
) ==
2591 BTRFS_FILE_EXTENT_INLINE
)
2593 bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
2597 num_bytes
= btrfs_file_extent_disk_num_bytes(buf
, fi
);
2598 key
.offset
-= btrfs_file_extent_offset(buf
, fi
);
2599 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
2600 parent
, ref_root
, key
.objectid
,
2605 bytenr
= btrfs_node_blockptr(buf
, i
);
2606 num_bytes
= btrfs_level_size(root
, level
- 1);
2607 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
2608 parent
, ref_root
, level
- 1, 0);
2619 int btrfs_inc_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2620 struct extent_buffer
*buf
, int full_backref
)
2622 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 1);
2625 int btrfs_dec_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2626 struct extent_buffer
*buf
, int full_backref
)
2628 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 0);
2631 static int write_one_cache_group(struct btrfs_trans_handle
*trans
,
2632 struct btrfs_root
*root
,
2633 struct btrfs_path
*path
,
2634 struct btrfs_block_group_cache
*cache
)
2637 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
2639 struct extent_buffer
*leaf
;
2641 ret
= btrfs_search_slot(trans
, extent_root
, &cache
->key
, path
, 0, 1);
2646 leaf
= path
->nodes
[0];
2647 bi
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
2648 write_extent_buffer(leaf
, &cache
->item
, bi
, sizeof(cache
->item
));
2649 btrfs_mark_buffer_dirty(leaf
);
2650 btrfs_release_path(path
);
2658 static struct btrfs_block_group_cache
*
2659 next_block_group(struct btrfs_root
*root
,
2660 struct btrfs_block_group_cache
*cache
)
2662 struct rb_node
*node
;
2663 spin_lock(&root
->fs_info
->block_group_cache_lock
);
2664 node
= rb_next(&cache
->cache_node
);
2665 btrfs_put_block_group(cache
);
2667 cache
= rb_entry(node
, struct btrfs_block_group_cache
,
2669 btrfs_get_block_group(cache
);
2672 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
2676 static int cache_save_setup(struct btrfs_block_group_cache
*block_group
,
2677 struct btrfs_trans_handle
*trans
,
2678 struct btrfs_path
*path
)
2680 struct btrfs_root
*root
= block_group
->fs_info
->tree_root
;
2681 struct inode
*inode
= NULL
;
2683 int dcs
= BTRFS_DC_ERROR
;
2689 * If this block group is smaller than 100 megs don't bother caching the
2692 if (block_group
->key
.offset
< (100 * 1024 * 1024)) {
2693 spin_lock(&block_group
->lock
);
2694 block_group
->disk_cache_state
= BTRFS_DC_WRITTEN
;
2695 spin_unlock(&block_group
->lock
);
2700 inode
= lookup_free_space_inode(root
, block_group
, path
);
2701 if (IS_ERR(inode
) && PTR_ERR(inode
) != -ENOENT
) {
2702 ret
= PTR_ERR(inode
);
2703 btrfs_release_path(path
);
2707 if (IS_ERR(inode
)) {
2711 if (block_group
->ro
)
2714 ret
= create_free_space_inode(root
, trans
, block_group
, path
);
2720 /* We've already setup this transaction, go ahead and exit */
2721 if (block_group
->cache_generation
== trans
->transid
&&
2722 i_size_read(inode
)) {
2723 dcs
= BTRFS_DC_SETUP
;
2728 * We want to set the generation to 0, that way if anything goes wrong
2729 * from here on out we know not to trust this cache when we load up next
2732 BTRFS_I(inode
)->generation
= 0;
2733 ret
= btrfs_update_inode(trans
, root
, inode
);
2736 if (i_size_read(inode
) > 0) {
2737 ret
= btrfs_truncate_free_space_cache(root
, trans
, path
,
2743 spin_lock(&block_group
->lock
);
2744 if (block_group
->cached
!= BTRFS_CACHE_FINISHED
) {
2745 /* We're not cached, don't bother trying to write stuff out */
2746 dcs
= BTRFS_DC_WRITTEN
;
2747 spin_unlock(&block_group
->lock
);
2750 spin_unlock(&block_group
->lock
);
2752 num_pages
= (int)div64_u64(block_group
->key
.offset
, 1024 * 1024 * 1024);
2757 * Just to make absolutely sure we have enough space, we're going to
2758 * preallocate 12 pages worth of space for each block group. In
2759 * practice we ought to use at most 8, but we need extra space so we can
2760 * add our header and have a terminator between the extents and the
2764 num_pages
*= PAGE_CACHE_SIZE
;
2766 ret
= btrfs_check_data_free_space(inode
, num_pages
);
2770 ret
= btrfs_prealloc_file_range_trans(inode
, trans
, 0, 0, num_pages
,
2771 num_pages
, num_pages
,
2774 dcs
= BTRFS_DC_SETUP
;
2775 btrfs_free_reserved_data_space(inode
, num_pages
);
2780 btrfs_release_path(path
);
2782 spin_lock(&block_group
->lock
);
2784 block_group
->cache_generation
= trans
->transid
;
2785 block_group
->disk_cache_state
= dcs
;
2786 spin_unlock(&block_group
->lock
);
2791 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle
*trans
,
2792 struct btrfs_root
*root
)
2794 struct btrfs_block_group_cache
*cache
;
2796 struct btrfs_path
*path
;
2799 path
= btrfs_alloc_path();
2805 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
2807 if (cache
->disk_cache_state
== BTRFS_DC_CLEAR
)
2809 cache
= next_block_group(root
, cache
);
2817 err
= cache_save_setup(cache
, trans
, path
);
2818 last
= cache
->key
.objectid
+ cache
->key
.offset
;
2819 btrfs_put_block_group(cache
);
2824 err
= btrfs_run_delayed_refs(trans
, root
,
2829 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
2831 if (cache
->disk_cache_state
== BTRFS_DC_CLEAR
) {
2832 btrfs_put_block_group(cache
);
2838 cache
= next_block_group(root
, cache
);
2847 if (cache
->disk_cache_state
== BTRFS_DC_SETUP
)
2848 cache
->disk_cache_state
= BTRFS_DC_NEED_WRITE
;
2850 last
= cache
->key
.objectid
+ cache
->key
.offset
;
2852 err
= write_one_cache_group(trans
, root
, path
, cache
);
2854 btrfs_put_block_group(cache
);
2859 * I don't think this is needed since we're just marking our
2860 * preallocated extent as written, but just in case it can't
2864 err
= btrfs_run_delayed_refs(trans
, root
,
2869 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
2872 * Really this shouldn't happen, but it could if we
2873 * couldn't write the entire preallocated extent and
2874 * splitting the extent resulted in a new block.
2877 btrfs_put_block_group(cache
);
2880 if (cache
->disk_cache_state
== BTRFS_DC_NEED_WRITE
)
2882 cache
= next_block_group(root
, cache
);
2891 btrfs_write_out_cache(root
, trans
, cache
, path
);
2894 * If we didn't have an error then the cache state is still
2895 * NEED_WRITE, so we can set it to WRITTEN.
2897 if (cache
->disk_cache_state
== BTRFS_DC_NEED_WRITE
)
2898 cache
->disk_cache_state
= BTRFS_DC_WRITTEN
;
2899 last
= cache
->key
.objectid
+ cache
->key
.offset
;
2900 btrfs_put_block_group(cache
);
2903 btrfs_free_path(path
);
2907 int btrfs_extent_readonly(struct btrfs_root
*root
, u64 bytenr
)
2909 struct btrfs_block_group_cache
*block_group
;
2912 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
2913 if (!block_group
|| block_group
->ro
)
2916 btrfs_put_block_group(block_group
);
2920 static int update_space_info(struct btrfs_fs_info
*info
, u64 flags
,
2921 u64 total_bytes
, u64 bytes_used
,
2922 struct btrfs_space_info
**space_info
)
2924 struct btrfs_space_info
*found
;
2928 if (flags
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
2929 BTRFS_BLOCK_GROUP_RAID10
))
2934 found
= __find_space_info(info
, flags
);
2936 spin_lock(&found
->lock
);
2937 found
->total_bytes
+= total_bytes
;
2938 found
->disk_total
+= total_bytes
* factor
;
2939 found
->bytes_used
+= bytes_used
;
2940 found
->disk_used
+= bytes_used
* factor
;
2942 spin_unlock(&found
->lock
);
2943 *space_info
= found
;
2946 found
= kzalloc(sizeof(*found
), GFP_NOFS
);
2950 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++)
2951 INIT_LIST_HEAD(&found
->block_groups
[i
]);
2952 init_rwsem(&found
->groups_sem
);
2953 spin_lock_init(&found
->lock
);
2954 found
->flags
= flags
& (BTRFS_BLOCK_GROUP_DATA
|
2955 BTRFS_BLOCK_GROUP_SYSTEM
|
2956 BTRFS_BLOCK_GROUP_METADATA
);
2957 found
->total_bytes
= total_bytes
;
2958 found
->disk_total
= total_bytes
* factor
;
2959 found
->bytes_used
= bytes_used
;
2960 found
->disk_used
= bytes_used
* factor
;
2961 found
->bytes_pinned
= 0;
2962 found
->bytes_reserved
= 0;
2963 found
->bytes_readonly
= 0;
2964 found
->bytes_may_use
= 0;
2966 found
->force_alloc
= CHUNK_ALLOC_NO_FORCE
;
2967 found
->chunk_alloc
= 0;
2969 init_waitqueue_head(&found
->wait
);
2970 *space_info
= found
;
2971 list_add_rcu(&found
->list
, &info
->space_info
);
2975 static void set_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
2977 u64 extra_flags
= flags
& (BTRFS_BLOCK_GROUP_RAID0
|
2978 BTRFS_BLOCK_GROUP_RAID1
|
2979 BTRFS_BLOCK_GROUP_RAID10
|
2980 BTRFS_BLOCK_GROUP_DUP
);
2982 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
2983 fs_info
->avail_data_alloc_bits
|= extra_flags
;
2984 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
2985 fs_info
->avail_metadata_alloc_bits
|= extra_flags
;
2986 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
2987 fs_info
->avail_system_alloc_bits
|= extra_flags
;
2991 u64
btrfs_reduce_alloc_profile(struct btrfs_root
*root
, u64 flags
)
2994 * we add in the count of missing devices because we want
2995 * to make sure that any RAID levels on a degraded FS
2996 * continue to be honored.
2998 u64 num_devices
= root
->fs_info
->fs_devices
->rw_devices
+
2999 root
->fs_info
->fs_devices
->missing_devices
;
3001 if (num_devices
== 1)
3002 flags
&= ~(BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID0
);
3003 if (num_devices
< 4)
3004 flags
&= ~BTRFS_BLOCK_GROUP_RAID10
;
3006 if ((flags
& BTRFS_BLOCK_GROUP_DUP
) &&
3007 (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
3008 BTRFS_BLOCK_GROUP_RAID10
))) {
3009 flags
&= ~BTRFS_BLOCK_GROUP_DUP
;
3012 if ((flags
& BTRFS_BLOCK_GROUP_RAID1
) &&
3013 (flags
& BTRFS_BLOCK_GROUP_RAID10
)) {
3014 flags
&= ~BTRFS_BLOCK_GROUP_RAID1
;
3017 if ((flags
& BTRFS_BLOCK_GROUP_RAID0
) &&
3018 ((flags
& BTRFS_BLOCK_GROUP_RAID1
) |
3019 (flags
& BTRFS_BLOCK_GROUP_RAID10
) |
3020 (flags
& BTRFS_BLOCK_GROUP_DUP
)))
3021 flags
&= ~BTRFS_BLOCK_GROUP_RAID0
;
3025 static u64
get_alloc_profile(struct btrfs_root
*root
, u64 flags
)
3027 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3028 flags
|= root
->fs_info
->avail_data_alloc_bits
&
3029 root
->fs_info
->data_alloc_profile
;
3030 else if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
3031 flags
|= root
->fs_info
->avail_system_alloc_bits
&
3032 root
->fs_info
->system_alloc_profile
;
3033 else if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
3034 flags
|= root
->fs_info
->avail_metadata_alloc_bits
&
3035 root
->fs_info
->metadata_alloc_profile
;
3036 return btrfs_reduce_alloc_profile(root
, flags
);
3039 u64
btrfs_get_alloc_profile(struct btrfs_root
*root
, int data
)
3044 flags
= BTRFS_BLOCK_GROUP_DATA
;
3045 else if (root
== root
->fs_info
->chunk_root
)
3046 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
3048 flags
= BTRFS_BLOCK_GROUP_METADATA
;
3050 return get_alloc_profile(root
, flags
);
3053 void btrfs_set_inode_space_info(struct btrfs_root
*root
, struct inode
*inode
)
3055 BTRFS_I(inode
)->space_info
= __find_space_info(root
->fs_info
,
3056 BTRFS_BLOCK_GROUP_DATA
);
3060 * This will check the space that the inode allocates from to make sure we have
3061 * enough space for bytes.
3063 int btrfs_check_data_free_space(struct inode
*inode
, u64 bytes
)
3065 struct btrfs_space_info
*data_sinfo
;
3066 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3068 int ret
= 0, committed
= 0, alloc_chunk
= 1;
3070 /* make sure bytes are sectorsize aligned */
3071 bytes
= (bytes
+ root
->sectorsize
- 1) & ~((u64
)root
->sectorsize
- 1);
3073 if (root
== root
->fs_info
->tree_root
||
3074 BTRFS_I(inode
)->location
.objectid
== BTRFS_FREE_INO_OBJECTID
) {
3079 data_sinfo
= BTRFS_I(inode
)->space_info
;
3084 /* make sure we have enough space to handle the data first */
3085 spin_lock(&data_sinfo
->lock
);
3086 used
= data_sinfo
->bytes_used
+ data_sinfo
->bytes_reserved
+
3087 data_sinfo
->bytes_pinned
+ data_sinfo
->bytes_readonly
+
3088 data_sinfo
->bytes_may_use
;
3090 if (used
+ bytes
> data_sinfo
->total_bytes
) {
3091 struct btrfs_trans_handle
*trans
;
3094 * if we don't have enough free bytes in this space then we need
3095 * to alloc a new chunk.
3097 if (!data_sinfo
->full
&& alloc_chunk
) {
3100 data_sinfo
->force_alloc
= CHUNK_ALLOC_FORCE
;
3101 spin_unlock(&data_sinfo
->lock
);
3103 alloc_target
= btrfs_get_alloc_profile(root
, 1);
3104 trans
= btrfs_join_transaction(root
);
3106 return PTR_ERR(trans
);
3108 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
3109 bytes
+ 2 * 1024 * 1024,
3111 CHUNK_ALLOC_NO_FORCE
);
3112 btrfs_end_transaction(trans
, root
);
3121 btrfs_set_inode_space_info(root
, inode
);
3122 data_sinfo
= BTRFS_I(inode
)->space_info
;
3128 * If we have less pinned bytes than we want to allocate then
3129 * don't bother committing the transaction, it won't help us.
3131 if (data_sinfo
->bytes_pinned
< bytes
)
3133 spin_unlock(&data_sinfo
->lock
);
3135 /* commit the current transaction and try again */
3138 !atomic_read(&root
->fs_info
->open_ioctl_trans
)) {
3140 trans
= btrfs_join_transaction(root
);
3142 return PTR_ERR(trans
);
3143 ret
= btrfs_commit_transaction(trans
, root
);
3151 data_sinfo
->bytes_may_use
+= bytes
;
3152 spin_unlock(&data_sinfo
->lock
);
3158 * Called if we need to clear a data reservation for this inode.
3160 void btrfs_free_reserved_data_space(struct inode
*inode
, u64 bytes
)
3162 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3163 struct btrfs_space_info
*data_sinfo
;
3165 /* make sure bytes are sectorsize aligned */
3166 bytes
= (bytes
+ root
->sectorsize
- 1) & ~((u64
)root
->sectorsize
- 1);
3168 data_sinfo
= BTRFS_I(inode
)->space_info
;
3169 spin_lock(&data_sinfo
->lock
);
3170 data_sinfo
->bytes_may_use
-= bytes
;
3171 spin_unlock(&data_sinfo
->lock
);
3174 static void force_metadata_allocation(struct btrfs_fs_info
*info
)
3176 struct list_head
*head
= &info
->space_info
;
3177 struct btrfs_space_info
*found
;
3180 list_for_each_entry_rcu(found
, head
, list
) {
3181 if (found
->flags
& BTRFS_BLOCK_GROUP_METADATA
)
3182 found
->force_alloc
= CHUNK_ALLOC_FORCE
;
3187 static int should_alloc_chunk(struct btrfs_root
*root
,
3188 struct btrfs_space_info
*sinfo
, u64 alloc_bytes
,
3191 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
3192 u64 num_bytes
= sinfo
->total_bytes
- sinfo
->bytes_readonly
;
3193 u64 num_allocated
= sinfo
->bytes_used
+ sinfo
->bytes_reserved
;
3196 if (force
== CHUNK_ALLOC_FORCE
)
3200 * We need to take into account the global rsv because for all intents
3201 * and purposes it's used space. Don't worry about locking the
3202 * global_rsv, it doesn't change except when the transaction commits.
3204 num_allocated
+= global_rsv
->size
;
3207 * in limited mode, we want to have some free space up to
3208 * about 1% of the FS size.
3210 if (force
== CHUNK_ALLOC_LIMITED
) {
3211 thresh
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
3212 thresh
= max_t(u64
, 64 * 1024 * 1024,
3213 div_factor_fine(thresh
, 1));
3215 if (num_bytes
- num_allocated
< thresh
)
3220 * we have two similar checks here, one based on percentage
3221 * and once based on a hard number of 256MB. The idea
3222 * is that if we have a good amount of free
3223 * room, don't allocate a chunk. A good mount is
3224 * less than 80% utilized of the chunks we have allocated,
3225 * or more than 256MB free
3227 if (num_allocated
+ alloc_bytes
+ 256 * 1024 * 1024 < num_bytes
)
3230 if (num_allocated
+ alloc_bytes
< div_factor(num_bytes
, 8))
3233 thresh
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
3235 /* 256MB or 5% of the FS */
3236 thresh
= max_t(u64
, 256 * 1024 * 1024, div_factor_fine(thresh
, 5));
3238 if (num_bytes
> thresh
&& sinfo
->bytes_used
< div_factor(num_bytes
, 3))
3243 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
3244 struct btrfs_root
*extent_root
, u64 alloc_bytes
,
3245 u64 flags
, int force
)
3247 struct btrfs_space_info
*space_info
;
3248 struct btrfs_fs_info
*fs_info
= extent_root
->fs_info
;
3249 int wait_for_alloc
= 0;
3252 flags
= btrfs_reduce_alloc_profile(extent_root
, flags
);
3254 space_info
= __find_space_info(extent_root
->fs_info
, flags
);
3256 ret
= update_space_info(extent_root
->fs_info
, flags
,
3260 BUG_ON(!space_info
);
3263 spin_lock(&space_info
->lock
);
3264 if (space_info
->force_alloc
)
3265 force
= space_info
->force_alloc
;
3266 if (space_info
->full
) {
3267 spin_unlock(&space_info
->lock
);
3271 if (!should_alloc_chunk(extent_root
, space_info
, alloc_bytes
, force
)) {
3272 spin_unlock(&space_info
->lock
);
3274 } else if (space_info
->chunk_alloc
) {
3277 space_info
->chunk_alloc
= 1;
3280 spin_unlock(&space_info
->lock
);
3282 mutex_lock(&fs_info
->chunk_mutex
);
3285 * The chunk_mutex is held throughout the entirety of a chunk
3286 * allocation, so once we've acquired the chunk_mutex we know that the
3287 * other guy is done and we need to recheck and see if we should
3290 if (wait_for_alloc
) {
3291 mutex_unlock(&fs_info
->chunk_mutex
);
3297 * If we have mixed data/metadata chunks we want to make sure we keep
3298 * allocating mixed chunks instead of individual chunks.
3300 if (btrfs_mixed_space_info(space_info
))
3301 flags
|= (BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
);
3304 * if we're doing a data chunk, go ahead and make sure that
3305 * we keep a reasonable number of metadata chunks allocated in the
3308 if (flags
& BTRFS_BLOCK_GROUP_DATA
&& fs_info
->metadata_ratio
) {
3309 fs_info
->data_chunk_allocations
++;
3310 if (!(fs_info
->data_chunk_allocations
%
3311 fs_info
->metadata_ratio
))
3312 force_metadata_allocation(fs_info
);
3315 ret
= btrfs_alloc_chunk(trans
, extent_root
, flags
);
3316 if (ret
< 0 && ret
!= -ENOSPC
)
3319 spin_lock(&space_info
->lock
);
3321 space_info
->full
= 1;
3325 space_info
->force_alloc
= CHUNK_ALLOC_NO_FORCE
;
3326 space_info
->chunk_alloc
= 0;
3327 spin_unlock(&space_info
->lock
);
3329 mutex_unlock(&extent_root
->fs_info
->chunk_mutex
);
3334 * shrink metadata reservation for delalloc
3336 static int shrink_delalloc(struct btrfs_trans_handle
*trans
,
3337 struct btrfs_root
*root
, u64 to_reclaim
,
3340 struct btrfs_block_rsv
*block_rsv
;
3341 struct btrfs_space_info
*space_info
;
3346 unsigned long nr_pages
= (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT
;
3348 unsigned long progress
;
3350 block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
3351 space_info
= block_rsv
->space_info
;
3354 reserved
= space_info
->bytes_may_use
;
3355 progress
= space_info
->reservation_progress
;
3361 if (root
->fs_info
->delalloc_bytes
== 0) {
3364 btrfs_wait_ordered_extents(root
, 0, 0);
3368 max_reclaim
= min(reserved
, to_reclaim
);
3369 nr_pages
= max_t(unsigned long, nr_pages
,
3370 max_reclaim
>> PAGE_CACHE_SHIFT
);
3371 while (loops
< 1024) {
3372 /* have the flusher threads jump in and do some IO */
3374 nr_pages
= min_t(unsigned long, nr_pages
,
3375 root
->fs_info
->delalloc_bytes
>> PAGE_CACHE_SHIFT
);
3376 writeback_inodes_sb_nr_if_idle(root
->fs_info
->sb
, nr_pages
);
3378 spin_lock(&space_info
->lock
);
3379 if (reserved
> space_info
->bytes_may_use
)
3380 reclaimed
+= reserved
- space_info
->bytes_may_use
;
3381 reserved
= space_info
->bytes_may_use
;
3382 spin_unlock(&space_info
->lock
);
3386 if (reserved
== 0 || reclaimed
>= max_reclaim
)
3389 if (trans
&& trans
->transaction
->blocked
)
3392 if (wait_ordered
&& !trans
) {
3393 btrfs_wait_ordered_extents(root
, 0, 0);
3395 time_left
= schedule_timeout_interruptible(1);
3397 /* We were interrupted, exit */
3402 /* we've kicked the IO a few times, if anything has been freed,
3403 * exit. There is no sense in looping here for a long time
3404 * when we really need to commit the transaction, or there are
3405 * just too many writers without enough free space
3410 if (progress
!= space_info
->reservation_progress
)
3416 return reclaimed
>= to_reclaim
;
3420 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
3421 * @root - the root we're allocating for
3422 * @block_rsv - the block_rsv we're allocating for
3423 * @orig_bytes - the number of bytes we want
3424 * @flush - wether or not we can flush to make our reservation
3426 * This will reserve orgi_bytes number of bytes from the space info associated
3427 * with the block_rsv. If there is not enough space it will make an attempt to
3428 * flush out space to make room. It will do this by flushing delalloc if
3429 * possible or committing the transaction. If flush is 0 then no attempts to
3430 * regain reservations will be made and this will fail if there is not enough
3433 static int reserve_metadata_bytes(struct btrfs_root
*root
,
3434 struct btrfs_block_rsv
*block_rsv
,
3435 u64 orig_bytes
, int flush
)
3437 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
3438 struct btrfs_trans_handle
*trans
;
3440 u64 num_bytes
= orig_bytes
;
3443 bool committed
= false;
3444 bool flushing
= false;
3445 bool wait_ordered
= false;
3447 trans
= (struct btrfs_trans_handle
*)current
->journal_info
;
3450 spin_lock(&space_info
->lock
);
3452 * We only want to wait if somebody other than us is flushing and we are
3453 * actually alloed to flush.
3455 while (flush
&& !flushing
&& space_info
->flush
) {
3456 spin_unlock(&space_info
->lock
);
3458 * If we have a trans handle we can't wait because the flusher
3459 * may have to commit the transaction, which would mean we would
3460 * deadlock since we are waiting for the flusher to finish, but
3461 * hold the current transaction open.
3465 ret
= wait_event_interruptible(space_info
->wait
,
3466 !space_info
->flush
);
3467 /* Must have been interrupted, return */
3471 spin_lock(&space_info
->lock
);
3475 used
= space_info
->bytes_used
+ space_info
->bytes_reserved
+
3476 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
3477 space_info
->bytes_may_use
;
3480 * The idea here is that we've not already over-reserved the block group
3481 * then we can go ahead and save our reservation first and then start
3482 * flushing if we need to. Otherwise if we've already overcommitted
3483 * lets start flushing stuff first and then come back and try to make
3486 if (used
<= space_info
->total_bytes
) {
3487 if (used
+ orig_bytes
<= space_info
->total_bytes
) {
3488 space_info
->bytes_may_use
+= orig_bytes
;
3492 * Ok set num_bytes to orig_bytes since we aren't
3493 * overocmmitted, this way we only try and reclaim what
3496 num_bytes
= orig_bytes
;
3500 * Ok we're over committed, set num_bytes to the overcommitted
3501 * amount plus the amount of bytes that we need for this
3504 wait_ordered
= true;
3505 num_bytes
= used
- space_info
->total_bytes
+
3506 (orig_bytes
* (retries
+ 1));
3510 u64 profile
= btrfs_get_alloc_profile(root
, 0);
3514 * If we have a lot of space that's pinned, don't bother doing
3515 * the overcommit dance yet and just commit the transaction.
3517 avail
= (space_info
->total_bytes
- space_info
->bytes_used
) * 8;
3519 if (space_info
->bytes_pinned
>= avail
&& flush
&& !trans
&&
3521 space_info
->flush
= 1;
3523 spin_unlock(&space_info
->lock
);
3527 spin_lock(&root
->fs_info
->free_chunk_lock
);
3528 avail
= root
->fs_info
->free_chunk_space
;
3531 * If we have dup, raid1 or raid10 then only half of the free
3532 * space is actually useable.
3534 if (profile
& (BTRFS_BLOCK_GROUP_DUP
|
3535 BTRFS_BLOCK_GROUP_RAID1
|
3536 BTRFS_BLOCK_GROUP_RAID10
))
3540 * If we aren't flushing don't let us overcommit too much, say
3541 * 1/8th of the space. If we can flush, let it overcommit up to
3548 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3550 if (used
+ num_bytes
< space_info
->total_bytes
+ avail
) {
3551 space_info
->bytes_may_use
+= orig_bytes
;
3554 wait_ordered
= true;
3559 * Couldn't make our reservation, save our place so while we're trying
3560 * to reclaim space we can actually use it instead of somebody else
3561 * stealing it from us.
3565 space_info
->flush
= 1;
3568 spin_unlock(&space_info
->lock
);
3574 * We do synchronous shrinking since we don't actually unreserve
3575 * metadata until after the IO is completed.
3577 ret
= shrink_delalloc(trans
, root
, num_bytes
, wait_ordered
);
3584 * So if we were overcommitted it's possible that somebody else flushed
3585 * out enough space and we simply didn't have enough space to reclaim,
3586 * so go back around and try again.
3589 wait_ordered
= true;
3603 trans
= btrfs_join_transaction(root
);
3606 ret
= btrfs_commit_transaction(trans
, root
);
3615 spin_lock(&space_info
->lock
);
3616 space_info
->flush
= 0;
3617 wake_up_all(&space_info
->wait
);
3618 spin_unlock(&space_info
->lock
);
3623 static struct btrfs_block_rsv
*get_block_rsv(struct btrfs_trans_handle
*trans
,
3624 struct btrfs_root
*root
)
3626 struct btrfs_block_rsv
*block_rsv
= NULL
;
3628 if (root
->ref_cows
|| root
== root
->fs_info
->csum_root
)
3629 block_rsv
= trans
->block_rsv
;
3632 block_rsv
= root
->block_rsv
;
3635 block_rsv
= &root
->fs_info
->empty_block_rsv
;
3640 static int block_rsv_use_bytes(struct btrfs_block_rsv
*block_rsv
,
3644 spin_lock(&block_rsv
->lock
);
3645 if (block_rsv
->reserved
>= num_bytes
) {
3646 block_rsv
->reserved
-= num_bytes
;
3647 if (block_rsv
->reserved
< block_rsv
->size
)
3648 block_rsv
->full
= 0;
3651 spin_unlock(&block_rsv
->lock
);
3655 static void block_rsv_add_bytes(struct btrfs_block_rsv
*block_rsv
,
3656 u64 num_bytes
, int update_size
)
3658 spin_lock(&block_rsv
->lock
);
3659 block_rsv
->reserved
+= num_bytes
;
3661 block_rsv
->size
+= num_bytes
;
3662 else if (block_rsv
->reserved
>= block_rsv
->size
)
3663 block_rsv
->full
= 1;
3664 spin_unlock(&block_rsv
->lock
);
3667 static void block_rsv_release_bytes(struct btrfs_block_rsv
*block_rsv
,
3668 struct btrfs_block_rsv
*dest
, u64 num_bytes
)
3670 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
3672 spin_lock(&block_rsv
->lock
);
3673 if (num_bytes
== (u64
)-1)
3674 num_bytes
= block_rsv
->size
;
3675 block_rsv
->size
-= num_bytes
;
3676 if (block_rsv
->reserved
>= block_rsv
->size
) {
3677 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
3678 block_rsv
->reserved
= block_rsv
->size
;
3679 block_rsv
->full
= 1;
3683 spin_unlock(&block_rsv
->lock
);
3685 if (num_bytes
> 0) {
3687 spin_lock(&dest
->lock
);
3691 bytes_to_add
= dest
->size
- dest
->reserved
;
3692 bytes_to_add
= min(num_bytes
, bytes_to_add
);
3693 dest
->reserved
+= bytes_to_add
;
3694 if (dest
->reserved
>= dest
->size
)
3696 num_bytes
-= bytes_to_add
;
3698 spin_unlock(&dest
->lock
);
3701 spin_lock(&space_info
->lock
);
3702 space_info
->bytes_may_use
-= num_bytes
;
3703 space_info
->reservation_progress
++;
3704 spin_unlock(&space_info
->lock
);
3709 static int block_rsv_migrate_bytes(struct btrfs_block_rsv
*src
,
3710 struct btrfs_block_rsv
*dst
, u64 num_bytes
)
3714 ret
= block_rsv_use_bytes(src
, num_bytes
);
3718 block_rsv_add_bytes(dst
, num_bytes
, 1);
3722 void btrfs_init_block_rsv(struct btrfs_block_rsv
*rsv
)
3724 memset(rsv
, 0, sizeof(*rsv
));
3725 spin_lock_init(&rsv
->lock
);
3728 struct btrfs_block_rsv
*btrfs_alloc_block_rsv(struct btrfs_root
*root
)
3730 struct btrfs_block_rsv
*block_rsv
;
3731 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3733 block_rsv
= kmalloc(sizeof(*block_rsv
), GFP_NOFS
);
3737 btrfs_init_block_rsv(block_rsv
);
3738 block_rsv
->space_info
= __find_space_info(fs_info
,
3739 BTRFS_BLOCK_GROUP_METADATA
);
3743 void btrfs_free_block_rsv(struct btrfs_root
*root
,
3744 struct btrfs_block_rsv
*rsv
)
3746 btrfs_block_rsv_release(root
, rsv
, (u64
)-1);
3750 int btrfs_block_rsv_add(struct btrfs_root
*root
,
3751 struct btrfs_block_rsv
*block_rsv
,
3759 ret
= reserve_metadata_bytes(root
, block_rsv
, num_bytes
, 1);
3761 block_rsv_add_bytes(block_rsv
, num_bytes
, 1);
3768 int btrfs_block_rsv_check(struct btrfs_root
*root
,
3769 struct btrfs_block_rsv
*block_rsv
, int min_factor
)
3777 spin_lock(&block_rsv
->lock
);
3778 num_bytes
= div_factor(block_rsv
->size
, min_factor
);
3779 if (block_rsv
->reserved
>= num_bytes
)
3781 spin_unlock(&block_rsv
->lock
);
3786 int btrfs_block_rsv_refill(struct btrfs_root
*root
,
3787 struct btrfs_block_rsv
*block_rsv
,
3796 spin_lock(&block_rsv
->lock
);
3797 num_bytes
= min_reserved
;
3798 if (block_rsv
->reserved
>= num_bytes
)
3801 num_bytes
-= block_rsv
->reserved
;
3802 spin_unlock(&block_rsv
->lock
);
3807 ret
= reserve_metadata_bytes(root
, block_rsv
, num_bytes
, 1);
3809 block_rsv_add_bytes(block_rsv
, num_bytes
, 0);
3816 int btrfs_block_rsv_migrate(struct btrfs_block_rsv
*src_rsv
,
3817 struct btrfs_block_rsv
*dst_rsv
,
3820 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
3823 void btrfs_block_rsv_release(struct btrfs_root
*root
,
3824 struct btrfs_block_rsv
*block_rsv
,
3827 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
3828 if (global_rsv
->full
|| global_rsv
== block_rsv
||
3829 block_rsv
->space_info
!= global_rsv
->space_info
)
3831 block_rsv_release_bytes(block_rsv
, global_rsv
, num_bytes
);
3835 * helper to calculate size of global block reservation.
3836 * the desired value is sum of space used by extent tree,
3837 * checksum tree and root tree
3839 static u64
calc_global_metadata_size(struct btrfs_fs_info
*fs_info
)
3841 struct btrfs_space_info
*sinfo
;
3845 int csum_size
= btrfs_super_csum_size(&fs_info
->super_copy
);
3847 sinfo
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_DATA
);
3848 spin_lock(&sinfo
->lock
);
3849 data_used
= sinfo
->bytes_used
;
3850 spin_unlock(&sinfo
->lock
);
3852 sinfo
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
3853 spin_lock(&sinfo
->lock
);
3854 if (sinfo
->flags
& BTRFS_BLOCK_GROUP_DATA
)
3856 meta_used
= sinfo
->bytes_used
;
3857 spin_unlock(&sinfo
->lock
);
3859 num_bytes
= (data_used
>> fs_info
->sb
->s_blocksize_bits
) *
3861 num_bytes
+= div64_u64(data_used
+ meta_used
, 50);
3863 if (num_bytes
* 3 > meta_used
)
3864 num_bytes
= div64_u64(meta_used
, 3);
3866 return ALIGN(num_bytes
, fs_info
->extent_root
->leafsize
<< 10);
3869 static void update_global_block_rsv(struct btrfs_fs_info
*fs_info
)
3871 struct btrfs_block_rsv
*block_rsv
= &fs_info
->global_block_rsv
;
3872 struct btrfs_space_info
*sinfo
= block_rsv
->space_info
;
3875 num_bytes
= calc_global_metadata_size(fs_info
);
3877 spin_lock(&block_rsv
->lock
);
3878 spin_lock(&sinfo
->lock
);
3880 block_rsv
->size
= num_bytes
;
3882 num_bytes
= sinfo
->bytes_used
+ sinfo
->bytes_pinned
+
3883 sinfo
->bytes_reserved
+ sinfo
->bytes_readonly
+
3884 sinfo
->bytes_may_use
;
3886 if (sinfo
->total_bytes
> num_bytes
) {
3887 num_bytes
= sinfo
->total_bytes
- num_bytes
;
3888 block_rsv
->reserved
+= num_bytes
;
3889 sinfo
->bytes_may_use
+= num_bytes
;
3892 if (block_rsv
->reserved
>= block_rsv
->size
) {
3893 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
3894 sinfo
->bytes_may_use
-= num_bytes
;
3895 sinfo
->reservation_progress
++;
3896 block_rsv
->reserved
= block_rsv
->size
;
3897 block_rsv
->full
= 1;
3900 spin_unlock(&sinfo
->lock
);
3901 spin_unlock(&block_rsv
->lock
);
3904 static void init_global_block_rsv(struct btrfs_fs_info
*fs_info
)
3906 struct btrfs_space_info
*space_info
;
3908 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_SYSTEM
);
3909 fs_info
->chunk_block_rsv
.space_info
= space_info
;
3911 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
3912 fs_info
->global_block_rsv
.space_info
= space_info
;
3913 fs_info
->delalloc_block_rsv
.space_info
= space_info
;
3914 fs_info
->trans_block_rsv
.space_info
= space_info
;
3915 fs_info
->empty_block_rsv
.space_info
= space_info
;
3917 fs_info
->extent_root
->block_rsv
= &fs_info
->global_block_rsv
;
3918 fs_info
->csum_root
->block_rsv
= &fs_info
->global_block_rsv
;
3919 fs_info
->dev_root
->block_rsv
= &fs_info
->global_block_rsv
;
3920 fs_info
->tree_root
->block_rsv
= &fs_info
->global_block_rsv
;
3921 fs_info
->chunk_root
->block_rsv
= &fs_info
->chunk_block_rsv
;
3923 update_global_block_rsv(fs_info
);
3926 static void release_global_block_rsv(struct btrfs_fs_info
*fs_info
)
3928 block_rsv_release_bytes(&fs_info
->global_block_rsv
, NULL
, (u64
)-1);
3929 WARN_ON(fs_info
->delalloc_block_rsv
.size
> 0);
3930 WARN_ON(fs_info
->delalloc_block_rsv
.reserved
> 0);
3931 WARN_ON(fs_info
->trans_block_rsv
.size
> 0);
3932 WARN_ON(fs_info
->trans_block_rsv
.reserved
> 0);
3933 WARN_ON(fs_info
->chunk_block_rsv
.size
> 0);
3934 WARN_ON(fs_info
->chunk_block_rsv
.reserved
> 0);
3937 void btrfs_trans_release_metadata(struct btrfs_trans_handle
*trans
,
3938 struct btrfs_root
*root
)
3940 if (!trans
->bytes_reserved
)
3943 btrfs_block_rsv_release(root
, trans
->block_rsv
, trans
->bytes_reserved
);
3944 trans
->bytes_reserved
= 0;
3947 int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle
*trans
,
3948 struct inode
*inode
)
3950 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3951 struct btrfs_block_rsv
*src_rsv
= get_block_rsv(trans
, root
);
3952 struct btrfs_block_rsv
*dst_rsv
= root
->orphan_block_rsv
;
3955 * We need to hold space in order to delete our orphan item once we've
3956 * added it, so this takes the reservation so we can release it later
3957 * when we are truly done with the orphan item.
3959 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 1);
3960 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
3963 void btrfs_orphan_release_metadata(struct inode
*inode
)
3965 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3966 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 1);
3967 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
, num_bytes
);
3970 int btrfs_snap_reserve_metadata(struct btrfs_trans_handle
*trans
,
3971 struct btrfs_pending_snapshot
*pending
)
3973 struct btrfs_root
*root
= pending
->root
;
3974 struct btrfs_block_rsv
*src_rsv
= get_block_rsv(trans
, root
);
3975 struct btrfs_block_rsv
*dst_rsv
= &pending
->block_rsv
;
3977 * two for root back/forward refs, two for directory entries
3978 * and one for root of the snapshot.
3980 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 5);
3981 dst_rsv
->space_info
= src_rsv
->space_info
;
3982 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
3986 * drop_outstanding_extent - drop an outstanding extent
3987 * @inode: the inode we're dropping the extent for
3989 * This is called when we are freeing up an outstanding extent, either called
3990 * after an error or after an extent is written. This will return the number of
3991 * reserved extents that need to be freed. This must be called with
3992 * BTRFS_I(inode)->lock held.
3994 static unsigned drop_outstanding_extent(struct inode
*inode
)
3996 unsigned dropped_extents
= 0;
3998 BUG_ON(!BTRFS_I(inode
)->outstanding_extents
);
3999 BTRFS_I(inode
)->outstanding_extents
--;
4002 * If we have more or the same amount of outsanding extents than we have
4003 * reserved then we need to leave the reserved extents count alone.
4005 if (BTRFS_I(inode
)->outstanding_extents
>=
4006 BTRFS_I(inode
)->reserved_extents
)
4009 dropped_extents
= BTRFS_I(inode
)->reserved_extents
-
4010 BTRFS_I(inode
)->outstanding_extents
;
4011 BTRFS_I(inode
)->reserved_extents
-= dropped_extents
;
4012 return dropped_extents
;
4016 * calc_csum_metadata_size - return the amount of metada space that must be
4017 * reserved/free'd for the given bytes.
4018 * @inode: the inode we're manipulating
4019 * @num_bytes: the number of bytes in question
4020 * @reserve: 1 if we are reserving space, 0 if we are freeing space
4022 * This adjusts the number of csum_bytes in the inode and then returns the
4023 * correct amount of metadata that must either be reserved or freed. We
4024 * calculate how many checksums we can fit into one leaf and then divide the
4025 * number of bytes that will need to be checksumed by this value to figure out
4026 * how many checksums will be required. If we are adding bytes then the number
4027 * may go up and we will return the number of additional bytes that must be
4028 * reserved. If it is going down we will return the number of bytes that must
4031 * This must be called with BTRFS_I(inode)->lock held.
4033 static u64
calc_csum_metadata_size(struct inode
*inode
, u64 num_bytes
,
4036 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4038 int num_csums_per_leaf
;
4042 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
&&
4043 BTRFS_I(inode
)->csum_bytes
== 0)
4046 old_csums
= (int)div64_u64(BTRFS_I(inode
)->csum_bytes
, root
->sectorsize
);
4048 BTRFS_I(inode
)->csum_bytes
+= num_bytes
;
4050 BTRFS_I(inode
)->csum_bytes
-= num_bytes
;
4051 csum_size
= BTRFS_LEAF_DATA_SIZE(root
) - sizeof(struct btrfs_item
);
4052 num_csums_per_leaf
= (int)div64_u64(csum_size
,
4053 sizeof(struct btrfs_csum_item
) +
4054 sizeof(struct btrfs_disk_key
));
4055 num_csums
= (int)div64_u64(BTRFS_I(inode
)->csum_bytes
, root
->sectorsize
);
4056 num_csums
= num_csums
+ num_csums_per_leaf
- 1;
4057 num_csums
= num_csums
/ num_csums_per_leaf
;
4059 old_csums
= old_csums
+ num_csums_per_leaf
- 1;
4060 old_csums
= old_csums
/ num_csums_per_leaf
;
4062 /* No change, no need to reserve more */
4063 if (old_csums
== num_csums
)
4067 return btrfs_calc_trans_metadata_size(root
,
4068 num_csums
- old_csums
);
4070 return btrfs_calc_trans_metadata_size(root
, old_csums
- num_csums
);
4073 int btrfs_delalloc_reserve_metadata(struct inode
*inode
, u64 num_bytes
)
4075 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4076 struct btrfs_block_rsv
*block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
4078 unsigned nr_extents
= 0;
4082 if (btrfs_is_free_space_inode(root
, inode
))
4085 if (flush
&& btrfs_transaction_in_commit(root
->fs_info
))
4086 schedule_timeout(1);
4088 num_bytes
= ALIGN(num_bytes
, root
->sectorsize
);
4090 spin_lock(&BTRFS_I(inode
)->lock
);
4091 BTRFS_I(inode
)->outstanding_extents
++;
4093 if (BTRFS_I(inode
)->outstanding_extents
>
4094 BTRFS_I(inode
)->reserved_extents
) {
4095 nr_extents
= BTRFS_I(inode
)->outstanding_extents
-
4096 BTRFS_I(inode
)->reserved_extents
;
4097 BTRFS_I(inode
)->reserved_extents
+= nr_extents
;
4099 to_reserve
= btrfs_calc_trans_metadata_size(root
, nr_extents
);
4101 to_reserve
+= calc_csum_metadata_size(inode
, num_bytes
, 1);
4102 spin_unlock(&BTRFS_I(inode
)->lock
);
4104 ret
= reserve_metadata_bytes(root
, block_rsv
, to_reserve
, flush
);
4109 spin_lock(&BTRFS_I(inode
)->lock
);
4110 dropped
= drop_outstanding_extent(inode
);
4111 to_free
= calc_csum_metadata_size(inode
, num_bytes
, 0);
4112 spin_unlock(&BTRFS_I(inode
)->lock
);
4113 to_free
+= btrfs_calc_trans_metadata_size(root
, dropped
);
4116 * Somebody could have come in and twiddled with the
4117 * reservation, so if we have to free more than we would have
4118 * reserved from this reservation go ahead and release those
4121 to_free
-= to_reserve
;
4123 btrfs_block_rsv_release(root
, block_rsv
, to_free
);
4127 block_rsv_add_bytes(block_rsv
, to_reserve
, 1);
4133 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
4134 * @inode: the inode to release the reservation for
4135 * @num_bytes: the number of bytes we're releasing
4137 * This will release the metadata reservation for an inode. This can be called
4138 * once we complete IO for a given set of bytes to release their metadata
4141 void btrfs_delalloc_release_metadata(struct inode
*inode
, u64 num_bytes
)
4143 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4147 num_bytes
= ALIGN(num_bytes
, root
->sectorsize
);
4148 spin_lock(&BTRFS_I(inode
)->lock
);
4149 dropped
= drop_outstanding_extent(inode
);
4151 to_free
= calc_csum_metadata_size(inode
, num_bytes
, 0);
4152 spin_unlock(&BTRFS_I(inode
)->lock
);
4154 to_free
+= btrfs_calc_trans_metadata_size(root
, dropped
);
4156 btrfs_block_rsv_release(root
, &root
->fs_info
->delalloc_block_rsv
,
4161 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
4162 * @inode: inode we're writing to
4163 * @num_bytes: the number of bytes we want to allocate
4165 * This will do the following things
4167 * o reserve space in the data space info for num_bytes
4168 * o reserve space in the metadata space info based on number of outstanding
4169 * extents and how much csums will be needed
4170 * o add to the inodes ->delalloc_bytes
4171 * o add it to the fs_info's delalloc inodes list.
4173 * This will return 0 for success and -ENOSPC if there is no space left.
4175 int btrfs_delalloc_reserve_space(struct inode
*inode
, u64 num_bytes
)
4179 ret
= btrfs_check_data_free_space(inode
, num_bytes
);
4183 ret
= btrfs_delalloc_reserve_metadata(inode
, num_bytes
);
4185 btrfs_free_reserved_data_space(inode
, num_bytes
);
4193 * btrfs_delalloc_release_space - release data and metadata space for delalloc
4194 * @inode: inode we're releasing space for
4195 * @num_bytes: the number of bytes we want to free up
4197 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
4198 * called in the case that we don't need the metadata AND data reservations
4199 * anymore. So if there is an error or we insert an inline extent.
4201 * This function will release the metadata space that was not used and will
4202 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
4203 * list if there are no delalloc bytes left.
4205 void btrfs_delalloc_release_space(struct inode
*inode
, u64 num_bytes
)
4207 btrfs_delalloc_release_metadata(inode
, num_bytes
);
4208 btrfs_free_reserved_data_space(inode
, num_bytes
);
4211 static int update_block_group(struct btrfs_trans_handle
*trans
,
4212 struct btrfs_root
*root
,
4213 u64 bytenr
, u64 num_bytes
, int alloc
)
4215 struct btrfs_block_group_cache
*cache
= NULL
;
4216 struct btrfs_fs_info
*info
= root
->fs_info
;
4217 u64 total
= num_bytes
;
4222 /* block accounting for super block */
4223 spin_lock(&info
->delalloc_lock
);
4224 old_val
= btrfs_super_bytes_used(&info
->super_copy
);
4226 old_val
+= num_bytes
;
4228 old_val
-= num_bytes
;
4229 btrfs_set_super_bytes_used(&info
->super_copy
, old_val
);
4230 spin_unlock(&info
->delalloc_lock
);
4233 cache
= btrfs_lookup_block_group(info
, bytenr
);
4236 if (cache
->flags
& (BTRFS_BLOCK_GROUP_DUP
|
4237 BTRFS_BLOCK_GROUP_RAID1
|
4238 BTRFS_BLOCK_GROUP_RAID10
))
4243 * If this block group has free space cache written out, we
4244 * need to make sure to load it if we are removing space. This
4245 * is because we need the unpinning stage to actually add the
4246 * space back to the block group, otherwise we will leak space.
4248 if (!alloc
&& cache
->cached
== BTRFS_CACHE_NO
)
4249 cache_block_group(cache
, trans
, NULL
, 1);
4251 byte_in_group
= bytenr
- cache
->key
.objectid
;
4252 WARN_ON(byte_in_group
> cache
->key
.offset
);
4254 spin_lock(&cache
->space_info
->lock
);
4255 spin_lock(&cache
->lock
);
4257 if (btrfs_test_opt(root
, SPACE_CACHE
) &&
4258 cache
->disk_cache_state
< BTRFS_DC_CLEAR
)
4259 cache
->disk_cache_state
= BTRFS_DC_CLEAR
;
4262 old_val
= btrfs_block_group_used(&cache
->item
);
4263 num_bytes
= min(total
, cache
->key
.offset
- byte_in_group
);
4265 old_val
+= num_bytes
;
4266 btrfs_set_block_group_used(&cache
->item
, old_val
);
4267 cache
->reserved
-= num_bytes
;
4268 cache
->space_info
->bytes_reserved
-= num_bytes
;
4269 cache
->space_info
->bytes_used
+= num_bytes
;
4270 cache
->space_info
->disk_used
+= num_bytes
* factor
;
4271 spin_unlock(&cache
->lock
);
4272 spin_unlock(&cache
->space_info
->lock
);
4274 old_val
-= num_bytes
;
4275 btrfs_set_block_group_used(&cache
->item
, old_val
);
4276 cache
->pinned
+= num_bytes
;
4277 cache
->space_info
->bytes_pinned
+= num_bytes
;
4278 cache
->space_info
->bytes_used
-= num_bytes
;
4279 cache
->space_info
->disk_used
-= num_bytes
* factor
;
4280 spin_unlock(&cache
->lock
);
4281 spin_unlock(&cache
->space_info
->lock
);
4283 set_extent_dirty(info
->pinned_extents
,
4284 bytenr
, bytenr
+ num_bytes
- 1,
4285 GFP_NOFS
| __GFP_NOFAIL
);
4287 btrfs_put_block_group(cache
);
4289 bytenr
+= num_bytes
;
4294 static u64
first_logical_byte(struct btrfs_root
*root
, u64 search_start
)
4296 struct btrfs_block_group_cache
*cache
;
4299 cache
= btrfs_lookup_first_block_group(root
->fs_info
, search_start
);
4303 bytenr
= cache
->key
.objectid
;
4304 btrfs_put_block_group(cache
);
4309 static int pin_down_extent(struct btrfs_root
*root
,
4310 struct btrfs_block_group_cache
*cache
,
4311 u64 bytenr
, u64 num_bytes
, int reserved
)
4313 spin_lock(&cache
->space_info
->lock
);
4314 spin_lock(&cache
->lock
);
4315 cache
->pinned
+= num_bytes
;
4316 cache
->space_info
->bytes_pinned
+= num_bytes
;
4318 cache
->reserved
-= num_bytes
;
4319 cache
->space_info
->bytes_reserved
-= num_bytes
;
4321 spin_unlock(&cache
->lock
);
4322 spin_unlock(&cache
->space_info
->lock
);
4324 set_extent_dirty(root
->fs_info
->pinned_extents
, bytenr
,
4325 bytenr
+ num_bytes
- 1, GFP_NOFS
| __GFP_NOFAIL
);
4330 * this function must be called within transaction
4332 int btrfs_pin_extent(struct btrfs_root
*root
,
4333 u64 bytenr
, u64 num_bytes
, int reserved
)
4335 struct btrfs_block_group_cache
*cache
;
4337 cache
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
4340 pin_down_extent(root
, cache
, bytenr
, num_bytes
, reserved
);
4342 btrfs_put_block_group(cache
);
4347 * btrfs_update_reserved_bytes - update the block_group and space info counters
4348 * @cache: The cache we are manipulating
4349 * @num_bytes: The number of bytes in question
4350 * @reserve: One of the reservation enums
4352 * This is called by the allocator when it reserves space, or by somebody who is
4353 * freeing space that was never actually used on disk. For example if you
4354 * reserve some space for a new leaf in transaction A and before transaction A
4355 * commits you free that leaf, you call this with reserve set to 0 in order to
4356 * clear the reservation.
4358 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
4359 * ENOSPC accounting. For data we handle the reservation through clearing the
4360 * delalloc bits in the io_tree. We have to do this since we could end up
4361 * allocating less disk space for the amount of data we have reserved in the
4362 * case of compression.
4364 * If this is a reservation and the block group has become read only we cannot
4365 * make the reservation and return -EAGAIN, otherwise this function always
4368 static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache
*cache
,
4369 u64 num_bytes
, int reserve
)
4371 struct btrfs_space_info
*space_info
= cache
->space_info
;
4373 spin_lock(&space_info
->lock
);
4374 spin_lock(&cache
->lock
);
4375 if (reserve
!= RESERVE_FREE
) {
4379 cache
->reserved
+= num_bytes
;
4380 space_info
->bytes_reserved
+= num_bytes
;
4381 if (reserve
== RESERVE_ALLOC
) {
4382 BUG_ON(space_info
->bytes_may_use
< num_bytes
);
4383 space_info
->bytes_may_use
-= num_bytes
;
4388 space_info
->bytes_readonly
+= num_bytes
;
4389 cache
->reserved
-= num_bytes
;
4390 space_info
->bytes_reserved
-= num_bytes
;
4391 space_info
->reservation_progress
++;
4393 spin_unlock(&cache
->lock
);
4394 spin_unlock(&space_info
->lock
);
4398 int btrfs_prepare_extent_commit(struct btrfs_trans_handle
*trans
,
4399 struct btrfs_root
*root
)
4401 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4402 struct btrfs_caching_control
*next
;
4403 struct btrfs_caching_control
*caching_ctl
;
4404 struct btrfs_block_group_cache
*cache
;
4406 down_write(&fs_info
->extent_commit_sem
);
4408 list_for_each_entry_safe(caching_ctl
, next
,
4409 &fs_info
->caching_block_groups
, list
) {
4410 cache
= caching_ctl
->block_group
;
4411 if (block_group_cache_done(cache
)) {
4412 cache
->last_byte_to_unpin
= (u64
)-1;
4413 list_del_init(&caching_ctl
->list
);
4414 put_caching_control(caching_ctl
);
4416 cache
->last_byte_to_unpin
= caching_ctl
->progress
;
4420 if (fs_info
->pinned_extents
== &fs_info
->freed_extents
[0])
4421 fs_info
->pinned_extents
= &fs_info
->freed_extents
[1];
4423 fs_info
->pinned_extents
= &fs_info
->freed_extents
[0];
4425 up_write(&fs_info
->extent_commit_sem
);
4427 update_global_block_rsv(fs_info
);
4431 static int unpin_extent_range(struct btrfs_root
*root
, u64 start
, u64 end
)
4433 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4434 struct btrfs_block_group_cache
*cache
= NULL
;
4437 while (start
<= end
) {
4439 start
>= cache
->key
.objectid
+ cache
->key
.offset
) {
4441 btrfs_put_block_group(cache
);
4442 cache
= btrfs_lookup_block_group(fs_info
, start
);
4446 len
= cache
->key
.objectid
+ cache
->key
.offset
- start
;
4447 len
= min(len
, end
+ 1 - start
);
4449 if (start
< cache
->last_byte_to_unpin
) {
4450 len
= min(len
, cache
->last_byte_to_unpin
- start
);
4451 btrfs_add_free_space(cache
, start
, len
);
4456 spin_lock(&cache
->space_info
->lock
);
4457 spin_lock(&cache
->lock
);
4458 cache
->pinned
-= len
;
4459 cache
->space_info
->bytes_pinned
-= len
;
4461 cache
->space_info
->bytes_readonly
+= len
;
4462 spin_unlock(&cache
->lock
);
4463 spin_unlock(&cache
->space_info
->lock
);
4467 btrfs_put_block_group(cache
);
4471 int btrfs_finish_extent_commit(struct btrfs_trans_handle
*trans
,
4472 struct btrfs_root
*root
)
4474 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4475 struct extent_io_tree
*unpin
;
4480 if (fs_info
->pinned_extents
== &fs_info
->freed_extents
[0])
4481 unpin
= &fs_info
->freed_extents
[1];
4483 unpin
= &fs_info
->freed_extents
[0];
4486 ret
= find_first_extent_bit(unpin
, 0, &start
, &end
,
4491 if (btrfs_test_opt(root
, DISCARD
))
4492 ret
= btrfs_discard_extent(root
, start
,
4493 end
+ 1 - start
, NULL
);
4495 clear_extent_dirty(unpin
, start
, end
, GFP_NOFS
);
4496 unpin_extent_range(root
, start
, end
);
4503 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
4504 struct btrfs_root
*root
,
4505 u64 bytenr
, u64 num_bytes
, u64 parent
,
4506 u64 root_objectid
, u64 owner_objectid
,
4507 u64 owner_offset
, int refs_to_drop
,
4508 struct btrfs_delayed_extent_op
*extent_op
)
4510 struct btrfs_key key
;
4511 struct btrfs_path
*path
;
4512 struct btrfs_fs_info
*info
= root
->fs_info
;
4513 struct btrfs_root
*extent_root
= info
->extent_root
;
4514 struct extent_buffer
*leaf
;
4515 struct btrfs_extent_item
*ei
;
4516 struct btrfs_extent_inline_ref
*iref
;
4519 int extent_slot
= 0;
4520 int found_extent
= 0;
4525 path
= btrfs_alloc_path();
4530 path
->leave_spinning
= 1;
4532 is_data
= owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
;
4533 BUG_ON(!is_data
&& refs_to_drop
!= 1);
4535 ret
= lookup_extent_backref(trans
, extent_root
, path
, &iref
,
4536 bytenr
, num_bytes
, parent
,
4537 root_objectid
, owner_objectid
,
4540 extent_slot
= path
->slots
[0];
4541 while (extent_slot
>= 0) {
4542 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
4544 if (key
.objectid
!= bytenr
)
4546 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
4547 key
.offset
== num_bytes
) {
4551 if (path
->slots
[0] - extent_slot
> 5)
4555 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4556 item_size
= btrfs_item_size_nr(path
->nodes
[0], extent_slot
);
4557 if (found_extent
&& item_size
< sizeof(*ei
))
4560 if (!found_extent
) {
4562 ret
= remove_extent_backref(trans
, extent_root
, path
,
4566 btrfs_release_path(path
);
4567 path
->leave_spinning
= 1;
4569 key
.objectid
= bytenr
;
4570 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
4571 key
.offset
= num_bytes
;
4573 ret
= btrfs_search_slot(trans
, extent_root
,
4576 printk(KERN_ERR
"umm, got %d back from search"
4577 ", was looking for %llu\n", ret
,
4578 (unsigned long long)bytenr
);
4580 btrfs_print_leaf(extent_root
,
4584 extent_slot
= path
->slots
[0];
4587 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
4589 printk(KERN_ERR
"btrfs unable to find ref byte nr %llu "
4590 "parent %llu root %llu owner %llu offset %llu\n",
4591 (unsigned long long)bytenr
,
4592 (unsigned long long)parent
,
4593 (unsigned long long)root_objectid
,
4594 (unsigned long long)owner_objectid
,
4595 (unsigned long long)owner_offset
);
4598 leaf
= path
->nodes
[0];
4599 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
4600 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4601 if (item_size
< sizeof(*ei
)) {
4602 BUG_ON(found_extent
|| extent_slot
!= path
->slots
[0]);
4603 ret
= convert_extent_item_v0(trans
, extent_root
, path
,
4607 btrfs_release_path(path
);
4608 path
->leave_spinning
= 1;
4610 key
.objectid
= bytenr
;
4611 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
4612 key
.offset
= num_bytes
;
4614 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
,
4617 printk(KERN_ERR
"umm, got %d back from search"
4618 ", was looking for %llu\n", ret
,
4619 (unsigned long long)bytenr
);
4620 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
4623 extent_slot
= path
->slots
[0];
4624 leaf
= path
->nodes
[0];
4625 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
4628 BUG_ON(item_size
< sizeof(*ei
));
4629 ei
= btrfs_item_ptr(leaf
, extent_slot
,
4630 struct btrfs_extent_item
);
4631 if (owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
) {
4632 struct btrfs_tree_block_info
*bi
;
4633 BUG_ON(item_size
< sizeof(*ei
) + sizeof(*bi
));
4634 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
4635 WARN_ON(owner_objectid
!= btrfs_tree_block_level(leaf
, bi
));
4638 refs
= btrfs_extent_refs(leaf
, ei
);
4639 BUG_ON(refs
< refs_to_drop
);
4640 refs
-= refs_to_drop
;
4644 __run_delayed_extent_op(extent_op
, leaf
, ei
);
4646 * In the case of inline back ref, reference count will
4647 * be updated by remove_extent_backref
4650 BUG_ON(!found_extent
);
4652 btrfs_set_extent_refs(leaf
, ei
, refs
);
4653 btrfs_mark_buffer_dirty(leaf
);
4656 ret
= remove_extent_backref(trans
, extent_root
, path
,
4663 BUG_ON(is_data
&& refs_to_drop
!=
4664 extent_data_ref_count(root
, path
, iref
));
4666 BUG_ON(path
->slots
[0] != extent_slot
);
4668 BUG_ON(path
->slots
[0] != extent_slot
+ 1);
4669 path
->slots
[0] = extent_slot
;
4674 ret
= btrfs_del_items(trans
, extent_root
, path
, path
->slots
[0],
4677 btrfs_release_path(path
);
4680 ret
= btrfs_del_csums(trans
, root
, bytenr
, num_bytes
);
4683 invalidate_mapping_pages(info
->btree_inode
->i_mapping
,
4684 bytenr
>> PAGE_CACHE_SHIFT
,
4685 (bytenr
+ num_bytes
- 1) >> PAGE_CACHE_SHIFT
);
4688 ret
= update_block_group(trans
, root
, bytenr
, num_bytes
, 0);
4691 btrfs_free_path(path
);
4696 * when we free an block, it is possible (and likely) that we free the last
4697 * delayed ref for that extent as well. This searches the delayed ref tree for
4698 * a given extent, and if there are no other delayed refs to be processed, it
4699 * removes it from the tree.
4701 static noinline
int check_ref_cleanup(struct btrfs_trans_handle
*trans
,
4702 struct btrfs_root
*root
, u64 bytenr
)
4704 struct btrfs_delayed_ref_head
*head
;
4705 struct btrfs_delayed_ref_root
*delayed_refs
;
4706 struct btrfs_delayed_ref_node
*ref
;
4707 struct rb_node
*node
;
4710 delayed_refs
= &trans
->transaction
->delayed_refs
;
4711 spin_lock(&delayed_refs
->lock
);
4712 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
4716 node
= rb_prev(&head
->node
.rb_node
);
4720 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
4722 /* there are still entries for this ref, we can't drop it */
4723 if (ref
->bytenr
== bytenr
)
4726 if (head
->extent_op
) {
4727 if (!head
->must_insert_reserved
)
4729 kfree(head
->extent_op
);
4730 head
->extent_op
= NULL
;
4734 * waiting for the lock here would deadlock. If someone else has it
4735 * locked they are already in the process of dropping it anyway
4737 if (!mutex_trylock(&head
->mutex
))
4741 * at this point we have a head with no other entries. Go
4742 * ahead and process it.
4744 head
->node
.in_tree
= 0;
4745 rb_erase(&head
->node
.rb_node
, &delayed_refs
->root
);
4747 delayed_refs
->num_entries
--;
4750 * we don't take a ref on the node because we're removing it from the
4751 * tree, so we just steal the ref the tree was holding.
4753 delayed_refs
->num_heads
--;
4754 if (list_empty(&head
->cluster
))
4755 delayed_refs
->num_heads_ready
--;
4757 list_del_init(&head
->cluster
);
4758 spin_unlock(&delayed_refs
->lock
);
4760 BUG_ON(head
->extent_op
);
4761 if (head
->must_insert_reserved
)
4764 mutex_unlock(&head
->mutex
);
4765 btrfs_put_delayed_ref(&head
->node
);
4768 spin_unlock(&delayed_refs
->lock
);
4772 void btrfs_free_tree_block(struct btrfs_trans_handle
*trans
,
4773 struct btrfs_root
*root
,
4774 struct extent_buffer
*buf
,
4775 u64 parent
, int last_ref
)
4777 struct btrfs_block_group_cache
*cache
= NULL
;
4780 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
4781 ret
= btrfs_add_delayed_tree_ref(trans
, buf
->start
, buf
->len
,
4782 parent
, root
->root_key
.objectid
,
4783 btrfs_header_level(buf
),
4784 BTRFS_DROP_DELAYED_REF
, NULL
);
4791 cache
= btrfs_lookup_block_group(root
->fs_info
, buf
->start
);
4793 if (btrfs_header_generation(buf
) == trans
->transid
) {
4794 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
4795 ret
= check_ref_cleanup(trans
, root
, buf
->start
);
4800 if (btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
)) {
4801 pin_down_extent(root
, cache
, buf
->start
, buf
->len
, 1);
4805 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY
, &buf
->bflags
));
4807 btrfs_add_free_space(cache
, buf
->start
, buf
->len
);
4808 btrfs_update_reserved_bytes(cache
, buf
->len
, RESERVE_FREE
);
4812 * Deleting the buffer, clear the corrupt flag since it doesn't matter
4815 clear_bit(EXTENT_BUFFER_CORRUPT
, &buf
->bflags
);
4816 btrfs_put_block_group(cache
);
4819 int btrfs_free_extent(struct btrfs_trans_handle
*trans
,
4820 struct btrfs_root
*root
,
4821 u64 bytenr
, u64 num_bytes
, u64 parent
,
4822 u64 root_objectid
, u64 owner
, u64 offset
)
4827 * tree log blocks never actually go into the extent allocation
4828 * tree, just update pinning info and exit early.
4830 if (root_objectid
== BTRFS_TREE_LOG_OBJECTID
) {
4831 WARN_ON(owner
>= BTRFS_FIRST_FREE_OBJECTID
);
4832 /* unlocks the pinned mutex */
4833 btrfs_pin_extent(root
, bytenr
, num_bytes
, 1);
4835 } else if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
4836 ret
= btrfs_add_delayed_tree_ref(trans
, bytenr
, num_bytes
,
4837 parent
, root_objectid
, (int)owner
,
4838 BTRFS_DROP_DELAYED_REF
, NULL
);
4841 ret
= btrfs_add_delayed_data_ref(trans
, bytenr
, num_bytes
,
4842 parent
, root_objectid
, owner
,
4843 offset
, BTRFS_DROP_DELAYED_REF
, NULL
);
4849 static u64
stripe_align(struct btrfs_root
*root
, u64 val
)
4851 u64 mask
= ((u64
)root
->stripesize
- 1);
4852 u64 ret
= (val
+ mask
) & ~mask
;
4857 * when we wait for progress in the block group caching, its because
4858 * our allocation attempt failed at least once. So, we must sleep
4859 * and let some progress happen before we try again.
4861 * This function will sleep at least once waiting for new free space to
4862 * show up, and then it will check the block group free space numbers
4863 * for our min num_bytes. Another option is to have it go ahead
4864 * and look in the rbtree for a free extent of a given size, but this
4868 wait_block_group_cache_progress(struct btrfs_block_group_cache
*cache
,
4871 struct btrfs_caching_control
*caching_ctl
;
4874 caching_ctl
= get_caching_control(cache
);
4878 wait_event(caching_ctl
->wait
, block_group_cache_done(cache
) ||
4879 (cache
->free_space_ctl
->free_space
>= num_bytes
));
4881 put_caching_control(caching_ctl
);
4886 wait_block_group_cache_done(struct btrfs_block_group_cache
*cache
)
4888 struct btrfs_caching_control
*caching_ctl
;
4891 caching_ctl
= get_caching_control(cache
);
4895 wait_event(caching_ctl
->wait
, block_group_cache_done(cache
));
4897 put_caching_control(caching_ctl
);
4901 static int get_block_group_index(struct btrfs_block_group_cache
*cache
)
4904 if (cache
->flags
& BTRFS_BLOCK_GROUP_RAID10
)
4906 else if (cache
->flags
& BTRFS_BLOCK_GROUP_RAID1
)
4908 else if (cache
->flags
& BTRFS_BLOCK_GROUP_DUP
)
4910 else if (cache
->flags
& BTRFS_BLOCK_GROUP_RAID0
)
4917 enum btrfs_loop_type
{
4918 LOOP_FIND_IDEAL
= 0,
4919 LOOP_CACHING_NOWAIT
= 1,
4920 LOOP_CACHING_WAIT
= 2,
4921 LOOP_ALLOC_CHUNK
= 3,
4922 LOOP_NO_EMPTY_SIZE
= 4,
4926 * walks the btree of allocated extents and find a hole of a given size.
4927 * The key ins is changed to record the hole:
4928 * ins->objectid == block start
4929 * ins->flags = BTRFS_EXTENT_ITEM_KEY
4930 * ins->offset == number of blocks
4931 * Any available blocks before search_start are skipped.
4933 static noinline
int find_free_extent(struct btrfs_trans_handle
*trans
,
4934 struct btrfs_root
*orig_root
,
4935 u64 num_bytes
, u64 empty_size
,
4936 u64 search_start
, u64 search_end
,
4937 u64 hint_byte
, struct btrfs_key
*ins
,
4941 struct btrfs_root
*root
= orig_root
->fs_info
->extent_root
;
4942 struct btrfs_free_cluster
*last_ptr
= NULL
;
4943 struct btrfs_block_group_cache
*block_group
= NULL
;
4944 int empty_cluster
= 2 * 1024 * 1024;
4945 int allowed_chunk_alloc
= 0;
4946 int done_chunk_alloc
= 0;
4947 struct btrfs_space_info
*space_info
;
4948 int last_ptr_loop
= 0;
4951 int alloc_type
= (data
& BTRFS_BLOCK_GROUP_DATA
) ?
4952 RESERVE_ALLOC_NO_ACCOUNT
: RESERVE_ALLOC
;
4953 bool found_uncached_bg
= false;
4954 bool failed_cluster_refill
= false;
4955 bool failed_alloc
= false;
4956 bool use_cluster
= true;
4957 u64 ideal_cache_percent
= 0;
4958 u64 ideal_cache_offset
= 0;
4960 WARN_ON(num_bytes
< root
->sectorsize
);
4961 btrfs_set_key_type(ins
, BTRFS_EXTENT_ITEM_KEY
);
4965 space_info
= __find_space_info(root
->fs_info
, data
);
4967 printk(KERN_ERR
"No space info for %llu\n", data
);
4972 * If the space info is for both data and metadata it means we have a
4973 * small filesystem and we can't use the clustering stuff.
4975 if (btrfs_mixed_space_info(space_info
))
4976 use_cluster
= false;
4978 if (orig_root
->ref_cows
|| empty_size
)
4979 allowed_chunk_alloc
= 1;
4981 if (data
& BTRFS_BLOCK_GROUP_METADATA
&& use_cluster
) {
4982 last_ptr
= &root
->fs_info
->meta_alloc_cluster
;
4983 if (!btrfs_test_opt(root
, SSD
))
4984 empty_cluster
= 64 * 1024;
4987 if ((data
& BTRFS_BLOCK_GROUP_DATA
) && use_cluster
&&
4988 btrfs_test_opt(root
, SSD
)) {
4989 last_ptr
= &root
->fs_info
->data_alloc_cluster
;
4993 spin_lock(&last_ptr
->lock
);
4994 if (last_ptr
->block_group
)
4995 hint_byte
= last_ptr
->window_start
;
4996 spin_unlock(&last_ptr
->lock
);
4999 search_start
= max(search_start
, first_logical_byte(root
, 0));
5000 search_start
= max(search_start
, hint_byte
);
5005 if (search_start
== hint_byte
) {
5007 block_group
= btrfs_lookup_block_group(root
->fs_info
,
5010 * we don't want to use the block group if it doesn't match our
5011 * allocation bits, or if its not cached.
5013 * However if we are re-searching with an ideal block group
5014 * picked out then we don't care that the block group is cached.
5016 if (block_group
&& block_group_bits(block_group
, data
) &&
5017 (block_group
->cached
!= BTRFS_CACHE_NO
||
5018 search_start
== ideal_cache_offset
)) {
5019 down_read(&space_info
->groups_sem
);
5020 if (list_empty(&block_group
->list
) ||
5023 * someone is removing this block group,
5024 * we can't jump into the have_block_group
5025 * target because our list pointers are not
5028 btrfs_put_block_group(block_group
);
5029 up_read(&space_info
->groups_sem
);
5031 index
= get_block_group_index(block_group
);
5032 goto have_block_group
;
5034 } else if (block_group
) {
5035 btrfs_put_block_group(block_group
);
5039 down_read(&space_info
->groups_sem
);
5040 list_for_each_entry(block_group
, &space_info
->block_groups
[index
],
5045 btrfs_get_block_group(block_group
);
5046 search_start
= block_group
->key
.objectid
;
5049 * this can happen if we end up cycling through all the
5050 * raid types, but we want to make sure we only allocate
5051 * for the proper type.
5053 if (!block_group_bits(block_group
, data
)) {
5054 u64 extra
= BTRFS_BLOCK_GROUP_DUP
|
5055 BTRFS_BLOCK_GROUP_RAID1
|
5056 BTRFS_BLOCK_GROUP_RAID10
;
5059 * if they asked for extra copies and this block group
5060 * doesn't provide them, bail. This does allow us to
5061 * fill raid0 from raid1.
5063 if ((data
& extra
) && !(block_group
->flags
& extra
))
5068 if (unlikely(block_group
->cached
== BTRFS_CACHE_NO
)) {
5071 ret
= cache_block_group(block_group
, trans
,
5073 if (block_group
->cached
== BTRFS_CACHE_FINISHED
)
5074 goto have_block_group
;
5076 free_percent
= btrfs_block_group_used(&block_group
->item
);
5077 free_percent
*= 100;
5078 free_percent
= div64_u64(free_percent
,
5079 block_group
->key
.offset
);
5080 free_percent
= 100 - free_percent
;
5081 if (free_percent
> ideal_cache_percent
&&
5082 likely(!block_group
->ro
)) {
5083 ideal_cache_offset
= block_group
->key
.objectid
;
5084 ideal_cache_percent
= free_percent
;
5088 * The caching workers are limited to 2 threads, so we
5089 * can queue as much work as we care to.
5091 if (loop
> LOOP_FIND_IDEAL
) {
5092 ret
= cache_block_group(block_group
, trans
,
5096 found_uncached_bg
= true;
5099 * If loop is set for cached only, try the next block
5102 if (loop
== LOOP_FIND_IDEAL
)
5106 cached
= block_group_cache_done(block_group
);
5107 if (unlikely(!cached
))
5108 found_uncached_bg
= true;
5110 if (unlikely(block_group
->ro
))
5113 spin_lock(&block_group
->free_space_ctl
->tree_lock
);
5115 block_group
->free_space_ctl
->free_space
<
5116 num_bytes
+ empty_size
) {
5117 spin_unlock(&block_group
->free_space_ctl
->tree_lock
);
5120 spin_unlock(&block_group
->free_space_ctl
->tree_lock
);
5123 * Ok we want to try and use the cluster allocator, so lets look
5124 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
5125 * have tried the cluster allocator plenty of times at this
5126 * point and not have found anything, so we are likely way too
5127 * fragmented for the clustering stuff to find anything, so lets
5128 * just skip it and let the allocator find whatever block it can
5131 if (last_ptr
&& loop
< LOOP_NO_EMPTY_SIZE
) {
5133 * the refill lock keeps out other
5134 * people trying to start a new cluster
5136 spin_lock(&last_ptr
->refill_lock
);
5137 if (last_ptr
->block_group
&&
5138 (last_ptr
->block_group
->ro
||
5139 !block_group_bits(last_ptr
->block_group
, data
))) {
5141 goto refill_cluster
;
5144 offset
= btrfs_alloc_from_cluster(block_group
, last_ptr
,
5145 num_bytes
, search_start
);
5147 /* we have a block, we're done */
5148 spin_unlock(&last_ptr
->refill_lock
);
5152 spin_lock(&last_ptr
->lock
);
5154 * whoops, this cluster doesn't actually point to
5155 * this block group. Get a ref on the block
5156 * group is does point to and try again
5158 if (!last_ptr_loop
&& last_ptr
->block_group
&&
5159 last_ptr
->block_group
!= block_group
&&
5161 get_block_group_index(last_ptr
->block_group
)) {
5163 btrfs_put_block_group(block_group
);
5164 block_group
= last_ptr
->block_group
;
5165 btrfs_get_block_group(block_group
);
5166 spin_unlock(&last_ptr
->lock
);
5167 spin_unlock(&last_ptr
->refill_lock
);
5170 search_start
= block_group
->key
.objectid
;
5172 * we know this block group is properly
5173 * in the list because
5174 * btrfs_remove_block_group, drops the
5175 * cluster before it removes the block
5176 * group from the list
5178 goto have_block_group
;
5180 spin_unlock(&last_ptr
->lock
);
5183 * this cluster didn't work out, free it and
5186 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
5190 /* allocate a cluster in this block group */
5191 ret
= btrfs_find_space_cluster(trans
, root
,
5192 block_group
, last_ptr
,
5194 empty_cluster
+ empty_size
);
5197 * now pull our allocation out of this
5200 offset
= btrfs_alloc_from_cluster(block_group
,
5201 last_ptr
, num_bytes
,
5204 /* we found one, proceed */
5205 spin_unlock(&last_ptr
->refill_lock
);
5208 } else if (!cached
&& loop
> LOOP_CACHING_NOWAIT
5209 && !failed_cluster_refill
) {
5210 spin_unlock(&last_ptr
->refill_lock
);
5212 failed_cluster_refill
= true;
5213 wait_block_group_cache_progress(block_group
,
5214 num_bytes
+ empty_cluster
+ empty_size
);
5215 goto have_block_group
;
5219 * at this point we either didn't find a cluster
5220 * or we weren't able to allocate a block from our
5221 * cluster. Free the cluster we've been trying
5222 * to use, and go to the next block group
5224 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
5225 spin_unlock(&last_ptr
->refill_lock
);
5229 offset
= btrfs_find_space_for_alloc(block_group
, search_start
,
5230 num_bytes
, empty_size
);
5232 * If we didn't find a chunk, and we haven't failed on this
5233 * block group before, and this block group is in the middle of
5234 * caching and we are ok with waiting, then go ahead and wait
5235 * for progress to be made, and set failed_alloc to true.
5237 * If failed_alloc is true then we've already waited on this
5238 * block group once and should move on to the next block group.
5240 if (!offset
&& !failed_alloc
&& !cached
&&
5241 loop
> LOOP_CACHING_NOWAIT
) {
5242 wait_block_group_cache_progress(block_group
,
5243 num_bytes
+ empty_size
);
5244 failed_alloc
= true;
5245 goto have_block_group
;
5246 } else if (!offset
) {
5250 search_start
= stripe_align(root
, offset
);
5251 /* move on to the next group */
5252 if (search_start
+ num_bytes
>= search_end
) {
5253 btrfs_add_free_space(block_group
, offset
, num_bytes
);
5257 /* move on to the next group */
5258 if (search_start
+ num_bytes
>
5259 block_group
->key
.objectid
+ block_group
->key
.offset
) {
5260 btrfs_add_free_space(block_group
, offset
, num_bytes
);
5264 ins
->objectid
= search_start
;
5265 ins
->offset
= num_bytes
;
5267 if (offset
< search_start
)
5268 btrfs_add_free_space(block_group
, offset
,
5269 search_start
- offset
);
5270 BUG_ON(offset
> search_start
);
5272 ret
= btrfs_update_reserved_bytes(block_group
, num_bytes
,
5274 if (ret
== -EAGAIN
) {
5275 btrfs_add_free_space(block_group
, offset
, num_bytes
);
5279 /* we are all good, lets return */
5280 ins
->objectid
= search_start
;
5281 ins
->offset
= num_bytes
;
5283 if (offset
< search_start
)
5284 btrfs_add_free_space(block_group
, offset
,
5285 search_start
- offset
);
5286 BUG_ON(offset
> search_start
);
5287 btrfs_put_block_group(block_group
);
5290 failed_cluster_refill
= false;
5291 failed_alloc
= false;
5292 BUG_ON(index
!= get_block_group_index(block_group
));
5293 btrfs_put_block_group(block_group
);
5295 up_read(&space_info
->groups_sem
);
5297 if (!ins
->objectid
&& ++index
< BTRFS_NR_RAID_TYPES
)
5300 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5301 * for them to make caching progress. Also
5302 * determine the best possible bg to cache
5303 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5304 * caching kthreads as we move along
5305 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5306 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5307 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5310 if (!ins
->objectid
&& loop
< LOOP_NO_EMPTY_SIZE
) {
5312 if (loop
== LOOP_FIND_IDEAL
&& found_uncached_bg
) {
5313 found_uncached_bg
= false;
5315 if (!ideal_cache_percent
)
5319 * 1 of the following 2 things have happened so far
5321 * 1) We found an ideal block group for caching that
5322 * is mostly full and will cache quickly, so we might
5323 * as well wait for it.
5325 * 2) We searched for cached only and we didn't find
5326 * anything, and we didn't start any caching kthreads
5327 * either, so chances are we will loop through and
5328 * start a couple caching kthreads, and then come back
5329 * around and just wait for them. This will be slower
5330 * because we will have 2 caching kthreads reading at
5331 * the same time when we could have just started one
5332 * and waited for it to get far enough to give us an
5333 * allocation, so go ahead and go to the wait caching
5336 loop
= LOOP_CACHING_WAIT
;
5337 search_start
= ideal_cache_offset
;
5338 ideal_cache_percent
= 0;
5340 } else if (loop
== LOOP_FIND_IDEAL
) {
5342 * Didn't find a uncached bg, wait on anything we find
5345 loop
= LOOP_CACHING_WAIT
;
5351 if (loop
== LOOP_ALLOC_CHUNK
) {
5352 if (allowed_chunk_alloc
) {
5353 ret
= do_chunk_alloc(trans
, root
, num_bytes
+
5354 2 * 1024 * 1024, data
,
5355 CHUNK_ALLOC_LIMITED
);
5356 allowed_chunk_alloc
= 0;
5358 done_chunk_alloc
= 1;
5359 } else if (!done_chunk_alloc
&&
5360 space_info
->force_alloc
==
5361 CHUNK_ALLOC_NO_FORCE
) {
5362 space_info
->force_alloc
= CHUNK_ALLOC_LIMITED
;
5366 * We didn't allocate a chunk, go ahead and drop the
5367 * empty size and loop again.
5369 if (!done_chunk_alloc
)
5370 loop
= LOOP_NO_EMPTY_SIZE
;
5373 if (loop
== LOOP_NO_EMPTY_SIZE
) {
5379 } else if (!ins
->objectid
) {
5381 } else if (ins
->objectid
) {
5388 static void dump_space_info(struct btrfs_space_info
*info
, u64 bytes
,
5389 int dump_block_groups
)
5391 struct btrfs_block_group_cache
*cache
;
5394 spin_lock(&info
->lock
);
5395 printk(KERN_INFO
"space_info %llu has %llu free, is %sfull\n",
5396 (unsigned long long)info
->flags
,
5397 (unsigned long long)(info
->total_bytes
- info
->bytes_used
-
5398 info
->bytes_pinned
- info
->bytes_reserved
-
5399 info
->bytes_readonly
),
5400 (info
->full
) ? "" : "not ");
5401 printk(KERN_INFO
"space_info total=%llu, used=%llu, pinned=%llu, "
5402 "reserved=%llu, may_use=%llu, readonly=%llu\n",
5403 (unsigned long long)info
->total_bytes
,
5404 (unsigned long long)info
->bytes_used
,
5405 (unsigned long long)info
->bytes_pinned
,
5406 (unsigned long long)info
->bytes_reserved
,
5407 (unsigned long long)info
->bytes_may_use
,
5408 (unsigned long long)info
->bytes_readonly
);
5409 spin_unlock(&info
->lock
);
5411 if (!dump_block_groups
)
5414 down_read(&info
->groups_sem
);
5416 list_for_each_entry(cache
, &info
->block_groups
[index
], list
) {
5417 spin_lock(&cache
->lock
);
5418 printk(KERN_INFO
"block group %llu has %llu bytes, %llu used "
5419 "%llu pinned %llu reserved\n",
5420 (unsigned long long)cache
->key
.objectid
,
5421 (unsigned long long)cache
->key
.offset
,
5422 (unsigned long long)btrfs_block_group_used(&cache
->item
),
5423 (unsigned long long)cache
->pinned
,
5424 (unsigned long long)cache
->reserved
);
5425 btrfs_dump_free_space(cache
, bytes
);
5426 spin_unlock(&cache
->lock
);
5428 if (++index
< BTRFS_NR_RAID_TYPES
)
5430 up_read(&info
->groups_sem
);
5433 int btrfs_reserve_extent(struct btrfs_trans_handle
*trans
,
5434 struct btrfs_root
*root
,
5435 u64 num_bytes
, u64 min_alloc_size
,
5436 u64 empty_size
, u64 hint_byte
,
5437 u64 search_end
, struct btrfs_key
*ins
,
5441 u64 search_start
= 0;
5443 data
= btrfs_get_alloc_profile(root
, data
);
5446 * the only place that sets empty_size is btrfs_realloc_node, which
5447 * is not called recursively on allocations
5449 if (empty_size
|| root
->ref_cows
)
5450 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
5451 num_bytes
+ 2 * 1024 * 1024, data
,
5452 CHUNK_ALLOC_NO_FORCE
);
5454 WARN_ON(num_bytes
< root
->sectorsize
);
5455 ret
= find_free_extent(trans
, root
, num_bytes
, empty_size
,
5456 search_start
, search_end
, hint_byte
,
5459 if (ret
== -ENOSPC
&& num_bytes
> min_alloc_size
) {
5460 num_bytes
= num_bytes
>> 1;
5461 num_bytes
= num_bytes
& ~(root
->sectorsize
- 1);
5462 num_bytes
= max(num_bytes
, min_alloc_size
);
5463 do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
5464 num_bytes
, data
, CHUNK_ALLOC_FORCE
);
5467 if (ret
== -ENOSPC
&& btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
5468 struct btrfs_space_info
*sinfo
;
5470 sinfo
= __find_space_info(root
->fs_info
, data
);
5471 printk(KERN_ERR
"btrfs allocation failed flags %llu, "
5472 "wanted %llu\n", (unsigned long long)data
,
5473 (unsigned long long)num_bytes
);
5474 dump_space_info(sinfo
, num_bytes
, 1);
5477 trace_btrfs_reserved_extent_alloc(root
, ins
->objectid
, ins
->offset
);
5482 int btrfs_free_reserved_extent(struct btrfs_root
*root
, u64 start
, u64 len
)
5484 struct btrfs_block_group_cache
*cache
;
5487 cache
= btrfs_lookup_block_group(root
->fs_info
, start
);
5489 printk(KERN_ERR
"Unable to find block group for %llu\n",
5490 (unsigned long long)start
);
5494 if (btrfs_test_opt(root
, DISCARD
))
5495 ret
= btrfs_discard_extent(root
, start
, len
, NULL
);
5497 btrfs_add_free_space(cache
, start
, len
);
5498 btrfs_update_reserved_bytes(cache
, len
, RESERVE_FREE
);
5499 btrfs_put_block_group(cache
);
5501 trace_btrfs_reserved_extent_free(root
, start
, len
);
5506 static int alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
5507 struct btrfs_root
*root
,
5508 u64 parent
, u64 root_objectid
,
5509 u64 flags
, u64 owner
, u64 offset
,
5510 struct btrfs_key
*ins
, int ref_mod
)
5513 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5514 struct btrfs_extent_item
*extent_item
;
5515 struct btrfs_extent_inline_ref
*iref
;
5516 struct btrfs_path
*path
;
5517 struct extent_buffer
*leaf
;
5522 type
= BTRFS_SHARED_DATA_REF_KEY
;
5524 type
= BTRFS_EXTENT_DATA_REF_KEY
;
5526 size
= sizeof(*extent_item
) + btrfs_extent_inline_ref_size(type
);
5528 path
= btrfs_alloc_path();
5532 path
->leave_spinning
= 1;
5533 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
5537 leaf
= path
->nodes
[0];
5538 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
5539 struct btrfs_extent_item
);
5540 btrfs_set_extent_refs(leaf
, extent_item
, ref_mod
);
5541 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
5542 btrfs_set_extent_flags(leaf
, extent_item
,
5543 flags
| BTRFS_EXTENT_FLAG_DATA
);
5545 iref
= (struct btrfs_extent_inline_ref
*)(extent_item
+ 1);
5546 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
5548 struct btrfs_shared_data_ref
*ref
;
5549 ref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
5550 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
5551 btrfs_set_shared_data_ref_count(leaf
, ref
, ref_mod
);
5553 struct btrfs_extent_data_ref
*ref
;
5554 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
5555 btrfs_set_extent_data_ref_root(leaf
, ref
, root_objectid
);
5556 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
5557 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
5558 btrfs_set_extent_data_ref_count(leaf
, ref
, ref_mod
);
5561 btrfs_mark_buffer_dirty(path
->nodes
[0]);
5562 btrfs_free_path(path
);
5564 ret
= update_block_group(trans
, root
, ins
->objectid
, ins
->offset
, 1);
5566 printk(KERN_ERR
"btrfs update block group failed for %llu "
5567 "%llu\n", (unsigned long long)ins
->objectid
,
5568 (unsigned long long)ins
->offset
);
5574 static int alloc_reserved_tree_block(struct btrfs_trans_handle
*trans
,
5575 struct btrfs_root
*root
,
5576 u64 parent
, u64 root_objectid
,
5577 u64 flags
, struct btrfs_disk_key
*key
,
5578 int level
, struct btrfs_key
*ins
)
5581 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5582 struct btrfs_extent_item
*extent_item
;
5583 struct btrfs_tree_block_info
*block_info
;
5584 struct btrfs_extent_inline_ref
*iref
;
5585 struct btrfs_path
*path
;
5586 struct extent_buffer
*leaf
;
5587 u32 size
= sizeof(*extent_item
) + sizeof(*block_info
) + sizeof(*iref
);
5589 path
= btrfs_alloc_path();
5593 path
->leave_spinning
= 1;
5594 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
5598 leaf
= path
->nodes
[0];
5599 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
5600 struct btrfs_extent_item
);
5601 btrfs_set_extent_refs(leaf
, extent_item
, 1);
5602 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
5603 btrfs_set_extent_flags(leaf
, extent_item
,
5604 flags
| BTRFS_EXTENT_FLAG_TREE_BLOCK
);
5605 block_info
= (struct btrfs_tree_block_info
*)(extent_item
+ 1);
5607 btrfs_set_tree_block_key(leaf
, block_info
, key
);
5608 btrfs_set_tree_block_level(leaf
, block_info
, level
);
5610 iref
= (struct btrfs_extent_inline_ref
*)(block_info
+ 1);
5612 BUG_ON(!(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
));
5613 btrfs_set_extent_inline_ref_type(leaf
, iref
,
5614 BTRFS_SHARED_BLOCK_REF_KEY
);
5615 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
5617 btrfs_set_extent_inline_ref_type(leaf
, iref
,
5618 BTRFS_TREE_BLOCK_REF_KEY
);
5619 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
5622 btrfs_mark_buffer_dirty(leaf
);
5623 btrfs_free_path(path
);
5625 ret
= update_block_group(trans
, root
, ins
->objectid
, ins
->offset
, 1);
5627 printk(KERN_ERR
"btrfs update block group failed for %llu "
5628 "%llu\n", (unsigned long long)ins
->objectid
,
5629 (unsigned long long)ins
->offset
);
5635 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
5636 struct btrfs_root
*root
,
5637 u64 root_objectid
, u64 owner
,
5638 u64 offset
, struct btrfs_key
*ins
)
5642 BUG_ON(root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
5644 ret
= btrfs_add_delayed_data_ref(trans
, ins
->objectid
, ins
->offset
,
5645 0, root_objectid
, owner
, offset
,
5646 BTRFS_ADD_DELAYED_EXTENT
, NULL
);
5651 * this is used by the tree logging recovery code. It records that
5652 * an extent has been allocated and makes sure to clear the free
5653 * space cache bits as well
5655 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle
*trans
,
5656 struct btrfs_root
*root
,
5657 u64 root_objectid
, u64 owner
, u64 offset
,
5658 struct btrfs_key
*ins
)
5661 struct btrfs_block_group_cache
*block_group
;
5662 struct btrfs_caching_control
*caching_ctl
;
5663 u64 start
= ins
->objectid
;
5664 u64 num_bytes
= ins
->offset
;
5666 block_group
= btrfs_lookup_block_group(root
->fs_info
, ins
->objectid
);
5667 cache_block_group(block_group
, trans
, NULL
, 0);
5668 caching_ctl
= get_caching_control(block_group
);
5671 BUG_ON(!block_group_cache_done(block_group
));
5672 ret
= btrfs_remove_free_space(block_group
, start
, num_bytes
);
5675 mutex_lock(&caching_ctl
->mutex
);
5677 if (start
>= caching_ctl
->progress
) {
5678 ret
= add_excluded_extent(root
, start
, num_bytes
);
5680 } else if (start
+ num_bytes
<= caching_ctl
->progress
) {
5681 ret
= btrfs_remove_free_space(block_group
,
5685 num_bytes
= caching_ctl
->progress
- start
;
5686 ret
= btrfs_remove_free_space(block_group
,
5690 start
= caching_ctl
->progress
;
5691 num_bytes
= ins
->objectid
+ ins
->offset
-
5692 caching_ctl
->progress
;
5693 ret
= add_excluded_extent(root
, start
, num_bytes
);
5697 mutex_unlock(&caching_ctl
->mutex
);
5698 put_caching_control(caching_ctl
);
5701 ret
= btrfs_update_reserved_bytes(block_group
, ins
->offset
,
5702 RESERVE_ALLOC_NO_ACCOUNT
);
5704 btrfs_put_block_group(block_group
);
5705 ret
= alloc_reserved_file_extent(trans
, root
, 0, root_objectid
,
5706 0, owner
, offset
, ins
, 1);
5710 struct extent_buffer
*btrfs_init_new_buffer(struct btrfs_trans_handle
*trans
,
5711 struct btrfs_root
*root
,
5712 u64 bytenr
, u32 blocksize
,
5715 struct extent_buffer
*buf
;
5717 buf
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
5719 return ERR_PTR(-ENOMEM
);
5720 btrfs_set_header_generation(buf
, trans
->transid
);
5721 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, buf
, level
);
5722 btrfs_tree_lock(buf
);
5723 clean_tree_block(trans
, root
, buf
);
5725 btrfs_set_lock_blocking(buf
);
5726 btrfs_set_buffer_uptodate(buf
);
5728 if (root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
) {
5730 * we allow two log transactions at a time, use different
5731 * EXENT bit to differentiate dirty pages.
5733 if (root
->log_transid
% 2 == 0)
5734 set_extent_dirty(&root
->dirty_log_pages
, buf
->start
,
5735 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
5737 set_extent_new(&root
->dirty_log_pages
, buf
->start
,
5738 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
5740 set_extent_dirty(&trans
->transaction
->dirty_pages
, buf
->start
,
5741 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
5743 trans
->blocks_used
++;
5744 /* this returns a buffer locked for blocking */
5748 static struct btrfs_block_rsv
*
5749 use_block_rsv(struct btrfs_trans_handle
*trans
,
5750 struct btrfs_root
*root
, u32 blocksize
)
5752 struct btrfs_block_rsv
*block_rsv
;
5753 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
5756 block_rsv
= get_block_rsv(trans
, root
);
5758 if (block_rsv
->size
== 0) {
5759 ret
= reserve_metadata_bytes(root
, block_rsv
, blocksize
, 0);
5761 * If we couldn't reserve metadata bytes try and use some from
5762 * the global reserve.
5764 if (ret
&& block_rsv
!= global_rsv
) {
5765 ret
= block_rsv_use_bytes(global_rsv
, blocksize
);
5768 return ERR_PTR(ret
);
5770 return ERR_PTR(ret
);
5775 ret
= block_rsv_use_bytes(block_rsv
, blocksize
);
5780 ret
= reserve_metadata_bytes(root
, block_rsv
, blocksize
, 0);
5783 } else if (ret
&& block_rsv
!= global_rsv
) {
5784 ret
= block_rsv_use_bytes(global_rsv
, blocksize
);
5790 return ERR_PTR(-ENOSPC
);
5793 static void unuse_block_rsv(struct btrfs_block_rsv
*block_rsv
, u32 blocksize
)
5795 block_rsv_add_bytes(block_rsv
, blocksize
, 0);
5796 block_rsv_release_bytes(block_rsv
, NULL
, 0);
5800 * finds a free extent and does all the dirty work required for allocation
5801 * returns the key for the extent through ins, and a tree buffer for
5802 * the first block of the extent through buf.
5804 * returns the tree buffer or NULL.
5806 struct extent_buffer
*btrfs_alloc_free_block(struct btrfs_trans_handle
*trans
,
5807 struct btrfs_root
*root
, u32 blocksize
,
5808 u64 parent
, u64 root_objectid
,
5809 struct btrfs_disk_key
*key
, int level
,
5810 u64 hint
, u64 empty_size
)
5812 struct btrfs_key ins
;
5813 struct btrfs_block_rsv
*block_rsv
;
5814 struct extent_buffer
*buf
;
5819 block_rsv
= use_block_rsv(trans
, root
, blocksize
);
5820 if (IS_ERR(block_rsv
))
5821 return ERR_CAST(block_rsv
);
5823 ret
= btrfs_reserve_extent(trans
, root
, blocksize
, blocksize
,
5824 empty_size
, hint
, (u64
)-1, &ins
, 0);
5826 unuse_block_rsv(block_rsv
, blocksize
);
5827 return ERR_PTR(ret
);
5830 buf
= btrfs_init_new_buffer(trans
, root
, ins
.objectid
,
5832 BUG_ON(IS_ERR(buf
));
5834 if (root_objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
5836 parent
= ins
.objectid
;
5837 flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
5841 if (root_objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
5842 struct btrfs_delayed_extent_op
*extent_op
;
5843 extent_op
= kmalloc(sizeof(*extent_op
), GFP_NOFS
);
5846 memcpy(&extent_op
->key
, key
, sizeof(extent_op
->key
));
5848 memset(&extent_op
->key
, 0, sizeof(extent_op
->key
));
5849 extent_op
->flags_to_set
= flags
;
5850 extent_op
->update_key
= 1;
5851 extent_op
->update_flags
= 1;
5852 extent_op
->is_data
= 0;
5854 ret
= btrfs_add_delayed_tree_ref(trans
, ins
.objectid
,
5855 ins
.offset
, parent
, root_objectid
,
5856 level
, BTRFS_ADD_DELAYED_EXTENT
,
5863 struct walk_control
{
5864 u64 refs
[BTRFS_MAX_LEVEL
];
5865 u64 flags
[BTRFS_MAX_LEVEL
];
5866 struct btrfs_key update_progress
;
5876 #define DROP_REFERENCE 1
5877 #define UPDATE_BACKREF 2
5879 static noinline
void reada_walk_down(struct btrfs_trans_handle
*trans
,
5880 struct btrfs_root
*root
,
5881 struct walk_control
*wc
,
5882 struct btrfs_path
*path
)
5890 struct btrfs_key key
;
5891 struct extent_buffer
*eb
;
5896 if (path
->slots
[wc
->level
] < wc
->reada_slot
) {
5897 wc
->reada_count
= wc
->reada_count
* 2 / 3;
5898 wc
->reada_count
= max(wc
->reada_count
, 2);
5900 wc
->reada_count
= wc
->reada_count
* 3 / 2;
5901 wc
->reada_count
= min_t(int, wc
->reada_count
,
5902 BTRFS_NODEPTRS_PER_BLOCK(root
));
5905 eb
= path
->nodes
[wc
->level
];
5906 nritems
= btrfs_header_nritems(eb
);
5907 blocksize
= btrfs_level_size(root
, wc
->level
- 1);
5909 for (slot
= path
->slots
[wc
->level
]; slot
< nritems
; slot
++) {
5910 if (nread
>= wc
->reada_count
)
5914 bytenr
= btrfs_node_blockptr(eb
, slot
);
5915 generation
= btrfs_node_ptr_generation(eb
, slot
);
5917 if (slot
== path
->slots
[wc
->level
])
5920 if (wc
->stage
== UPDATE_BACKREF
&&
5921 generation
<= root
->root_key
.offset
)
5924 /* We don't lock the tree block, it's OK to be racy here */
5925 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
, blocksize
,
5930 if (wc
->stage
== DROP_REFERENCE
) {
5934 if (wc
->level
== 1 &&
5935 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
5937 if (!wc
->update_ref
||
5938 generation
<= root
->root_key
.offset
)
5940 btrfs_node_key_to_cpu(eb
, &key
, slot
);
5941 ret
= btrfs_comp_cpu_keys(&key
,
5942 &wc
->update_progress
);
5946 if (wc
->level
== 1 &&
5947 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
5951 ret
= readahead_tree_block(root
, bytenr
, blocksize
,
5957 wc
->reada_slot
= slot
;
5961 * hepler to process tree block while walking down the tree.
5963 * when wc->stage == UPDATE_BACKREF, this function updates
5964 * back refs for pointers in the block.
5966 * NOTE: return value 1 means we should stop walking down.
5968 static noinline
int walk_down_proc(struct btrfs_trans_handle
*trans
,
5969 struct btrfs_root
*root
,
5970 struct btrfs_path
*path
,
5971 struct walk_control
*wc
, int lookup_info
)
5973 int level
= wc
->level
;
5974 struct extent_buffer
*eb
= path
->nodes
[level
];
5975 u64 flag
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
5978 if (wc
->stage
== UPDATE_BACKREF
&&
5979 btrfs_header_owner(eb
) != root
->root_key
.objectid
)
5983 * when reference count of tree block is 1, it won't increase
5984 * again. once full backref flag is set, we never clear it.
5987 ((wc
->stage
== DROP_REFERENCE
&& wc
->refs
[level
] != 1) ||
5988 (wc
->stage
== UPDATE_BACKREF
&& !(wc
->flags
[level
] & flag
)))) {
5989 BUG_ON(!path
->locks
[level
]);
5990 ret
= btrfs_lookup_extent_info(trans
, root
,
5995 BUG_ON(wc
->refs
[level
] == 0);
5998 if (wc
->stage
== DROP_REFERENCE
) {
5999 if (wc
->refs
[level
] > 1)
6002 if (path
->locks
[level
] && !wc
->keep_locks
) {
6003 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
6004 path
->locks
[level
] = 0;
6009 /* wc->stage == UPDATE_BACKREF */
6010 if (!(wc
->flags
[level
] & flag
)) {
6011 BUG_ON(!path
->locks
[level
]);
6012 ret
= btrfs_inc_ref(trans
, root
, eb
, 1);
6014 ret
= btrfs_dec_ref(trans
, root
, eb
, 0);
6016 ret
= btrfs_set_disk_extent_flags(trans
, root
, eb
->start
,
6019 wc
->flags
[level
] |= flag
;
6023 * the block is shared by multiple trees, so it's not good to
6024 * keep the tree lock
6026 if (path
->locks
[level
] && level
> 0) {
6027 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
6028 path
->locks
[level
] = 0;
6034 * hepler to process tree block pointer.
6036 * when wc->stage == DROP_REFERENCE, this function checks
6037 * reference count of the block pointed to. if the block
6038 * is shared and we need update back refs for the subtree
6039 * rooted at the block, this function changes wc->stage to
6040 * UPDATE_BACKREF. if the block is shared and there is no
6041 * need to update back, this function drops the reference
6044 * NOTE: return value 1 means we should stop walking down.
6046 static noinline
int do_walk_down(struct btrfs_trans_handle
*trans
,
6047 struct btrfs_root
*root
,
6048 struct btrfs_path
*path
,
6049 struct walk_control
*wc
, int *lookup_info
)
6055 struct btrfs_key key
;
6056 struct extent_buffer
*next
;
6057 int level
= wc
->level
;
6061 generation
= btrfs_node_ptr_generation(path
->nodes
[level
],
6062 path
->slots
[level
]);
6064 * if the lower level block was created before the snapshot
6065 * was created, we know there is no need to update back refs
6068 if (wc
->stage
== UPDATE_BACKREF
&&
6069 generation
<= root
->root_key
.offset
) {
6074 bytenr
= btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]);
6075 blocksize
= btrfs_level_size(root
, level
- 1);
6077 next
= btrfs_find_tree_block(root
, bytenr
, blocksize
);
6079 next
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
6084 btrfs_tree_lock(next
);
6085 btrfs_set_lock_blocking(next
);
6087 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
, blocksize
,
6088 &wc
->refs
[level
- 1],
6089 &wc
->flags
[level
- 1]);
6091 BUG_ON(wc
->refs
[level
- 1] == 0);
6094 if (wc
->stage
== DROP_REFERENCE
) {
6095 if (wc
->refs
[level
- 1] > 1) {
6097 (wc
->flags
[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF
))
6100 if (!wc
->update_ref
||
6101 generation
<= root
->root_key
.offset
)
6104 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
6105 path
->slots
[level
]);
6106 ret
= btrfs_comp_cpu_keys(&key
, &wc
->update_progress
);
6110 wc
->stage
= UPDATE_BACKREF
;
6111 wc
->shared_level
= level
- 1;
6115 (wc
->flags
[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF
))
6119 if (!btrfs_buffer_uptodate(next
, generation
)) {
6120 btrfs_tree_unlock(next
);
6121 free_extent_buffer(next
);
6127 if (reada
&& level
== 1)
6128 reada_walk_down(trans
, root
, wc
, path
);
6129 next
= read_tree_block(root
, bytenr
, blocksize
, generation
);
6132 btrfs_tree_lock(next
);
6133 btrfs_set_lock_blocking(next
);
6137 BUG_ON(level
!= btrfs_header_level(next
));
6138 path
->nodes
[level
] = next
;
6139 path
->slots
[level
] = 0;
6140 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
6146 wc
->refs
[level
- 1] = 0;
6147 wc
->flags
[level
- 1] = 0;
6148 if (wc
->stage
== DROP_REFERENCE
) {
6149 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
) {
6150 parent
= path
->nodes
[level
]->start
;
6152 BUG_ON(root
->root_key
.objectid
!=
6153 btrfs_header_owner(path
->nodes
[level
]));
6157 ret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
, parent
,
6158 root
->root_key
.objectid
, level
- 1, 0);
6161 btrfs_tree_unlock(next
);
6162 free_extent_buffer(next
);
6168 * hepler to process tree block while walking up the tree.
6170 * when wc->stage == DROP_REFERENCE, this function drops
6171 * reference count on the block.
6173 * when wc->stage == UPDATE_BACKREF, this function changes
6174 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6175 * to UPDATE_BACKREF previously while processing the block.
6177 * NOTE: return value 1 means we should stop walking up.
6179 static noinline
int walk_up_proc(struct btrfs_trans_handle
*trans
,
6180 struct btrfs_root
*root
,
6181 struct btrfs_path
*path
,
6182 struct walk_control
*wc
)
6185 int level
= wc
->level
;
6186 struct extent_buffer
*eb
= path
->nodes
[level
];
6189 if (wc
->stage
== UPDATE_BACKREF
) {
6190 BUG_ON(wc
->shared_level
< level
);
6191 if (level
< wc
->shared_level
)
6194 ret
= find_next_key(path
, level
+ 1, &wc
->update_progress
);
6198 wc
->stage
= DROP_REFERENCE
;
6199 wc
->shared_level
= -1;
6200 path
->slots
[level
] = 0;
6203 * check reference count again if the block isn't locked.
6204 * we should start walking down the tree again if reference
6207 if (!path
->locks
[level
]) {
6209 btrfs_tree_lock(eb
);
6210 btrfs_set_lock_blocking(eb
);
6211 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
6213 ret
= btrfs_lookup_extent_info(trans
, root
,
6218 BUG_ON(wc
->refs
[level
] == 0);
6219 if (wc
->refs
[level
] == 1) {
6220 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
6226 /* wc->stage == DROP_REFERENCE */
6227 BUG_ON(wc
->refs
[level
] > 1 && !path
->locks
[level
]);
6229 if (wc
->refs
[level
] == 1) {
6231 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
6232 ret
= btrfs_dec_ref(trans
, root
, eb
, 1);
6234 ret
= btrfs_dec_ref(trans
, root
, eb
, 0);
6237 /* make block locked assertion in clean_tree_block happy */
6238 if (!path
->locks
[level
] &&
6239 btrfs_header_generation(eb
) == trans
->transid
) {
6240 btrfs_tree_lock(eb
);
6241 btrfs_set_lock_blocking(eb
);
6242 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
6244 clean_tree_block(trans
, root
, eb
);
6247 if (eb
== root
->node
) {
6248 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
6251 BUG_ON(root
->root_key
.objectid
!=
6252 btrfs_header_owner(eb
));
6254 if (wc
->flags
[level
+ 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
6255 parent
= path
->nodes
[level
+ 1]->start
;
6257 BUG_ON(root
->root_key
.objectid
!=
6258 btrfs_header_owner(path
->nodes
[level
+ 1]));
6261 btrfs_free_tree_block(trans
, root
, eb
, parent
, wc
->refs
[level
] == 1);
6263 wc
->refs
[level
] = 0;
6264 wc
->flags
[level
] = 0;
6268 static noinline
int walk_down_tree(struct btrfs_trans_handle
*trans
,
6269 struct btrfs_root
*root
,
6270 struct btrfs_path
*path
,
6271 struct walk_control
*wc
)
6273 int level
= wc
->level
;
6274 int lookup_info
= 1;
6277 while (level
>= 0) {
6278 ret
= walk_down_proc(trans
, root
, path
, wc
, lookup_info
);
6285 if (path
->slots
[level
] >=
6286 btrfs_header_nritems(path
->nodes
[level
]))
6289 ret
= do_walk_down(trans
, root
, path
, wc
, &lookup_info
);
6291 path
->slots
[level
]++;
6300 static noinline
int walk_up_tree(struct btrfs_trans_handle
*trans
,
6301 struct btrfs_root
*root
,
6302 struct btrfs_path
*path
,
6303 struct walk_control
*wc
, int max_level
)
6305 int level
= wc
->level
;
6308 path
->slots
[level
] = btrfs_header_nritems(path
->nodes
[level
]);
6309 while (level
< max_level
&& path
->nodes
[level
]) {
6311 if (path
->slots
[level
] + 1 <
6312 btrfs_header_nritems(path
->nodes
[level
])) {
6313 path
->slots
[level
]++;
6316 ret
= walk_up_proc(trans
, root
, path
, wc
);
6320 if (path
->locks
[level
]) {
6321 btrfs_tree_unlock_rw(path
->nodes
[level
],
6322 path
->locks
[level
]);
6323 path
->locks
[level
] = 0;
6325 free_extent_buffer(path
->nodes
[level
]);
6326 path
->nodes
[level
] = NULL
;
6334 * drop a subvolume tree.
6336 * this function traverses the tree freeing any blocks that only
6337 * referenced by the tree.
6339 * when a shared tree block is found. this function decreases its
6340 * reference count by one. if update_ref is true, this function
6341 * also make sure backrefs for the shared block and all lower level
6342 * blocks are properly updated.
6344 void btrfs_drop_snapshot(struct btrfs_root
*root
,
6345 struct btrfs_block_rsv
*block_rsv
, int update_ref
)
6347 struct btrfs_path
*path
;
6348 struct btrfs_trans_handle
*trans
;
6349 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
6350 struct btrfs_root_item
*root_item
= &root
->root_item
;
6351 struct walk_control
*wc
;
6352 struct btrfs_key key
;
6357 path
= btrfs_alloc_path();
6363 wc
= kzalloc(sizeof(*wc
), GFP_NOFS
);
6365 btrfs_free_path(path
);
6370 trans
= btrfs_start_transaction(tree_root
, 0);
6371 BUG_ON(IS_ERR(trans
));
6374 trans
->block_rsv
= block_rsv
;
6376 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
6377 level
= btrfs_header_level(root
->node
);
6378 path
->nodes
[level
] = btrfs_lock_root_node(root
);
6379 btrfs_set_lock_blocking(path
->nodes
[level
]);
6380 path
->slots
[level
] = 0;
6381 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
6382 memset(&wc
->update_progress
, 0,
6383 sizeof(wc
->update_progress
));
6385 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
6386 memcpy(&wc
->update_progress
, &key
,
6387 sizeof(wc
->update_progress
));
6389 level
= root_item
->drop_level
;
6391 path
->lowest_level
= level
;
6392 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
6393 path
->lowest_level
= 0;
6401 * unlock our path, this is safe because only this
6402 * function is allowed to delete this snapshot
6404 btrfs_unlock_up_safe(path
, 0);
6406 level
= btrfs_header_level(root
->node
);
6408 btrfs_tree_lock(path
->nodes
[level
]);
6409 btrfs_set_lock_blocking(path
->nodes
[level
]);
6411 ret
= btrfs_lookup_extent_info(trans
, root
,
6412 path
->nodes
[level
]->start
,
6413 path
->nodes
[level
]->len
,
6417 BUG_ON(wc
->refs
[level
] == 0);
6419 if (level
== root_item
->drop_level
)
6422 btrfs_tree_unlock(path
->nodes
[level
]);
6423 WARN_ON(wc
->refs
[level
] != 1);
6429 wc
->shared_level
= -1;
6430 wc
->stage
= DROP_REFERENCE
;
6431 wc
->update_ref
= update_ref
;
6433 wc
->reada_count
= BTRFS_NODEPTRS_PER_BLOCK(root
);
6436 ret
= walk_down_tree(trans
, root
, path
, wc
);
6442 ret
= walk_up_tree(trans
, root
, path
, wc
, BTRFS_MAX_LEVEL
);
6449 BUG_ON(wc
->stage
!= DROP_REFERENCE
);
6453 if (wc
->stage
== DROP_REFERENCE
) {
6455 btrfs_node_key(path
->nodes
[level
],
6456 &root_item
->drop_progress
,
6457 path
->slots
[level
]);
6458 root_item
->drop_level
= level
;
6461 BUG_ON(wc
->level
== 0);
6462 if (btrfs_should_end_transaction(trans
, tree_root
)) {
6463 ret
= btrfs_update_root(trans
, tree_root
,
6468 btrfs_end_transaction_throttle(trans
, tree_root
);
6469 trans
= btrfs_start_transaction(tree_root
, 0);
6470 BUG_ON(IS_ERR(trans
));
6472 trans
->block_rsv
= block_rsv
;
6475 btrfs_release_path(path
);
6478 ret
= btrfs_del_root(trans
, tree_root
, &root
->root_key
);
6481 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
6482 ret
= btrfs_find_last_root(tree_root
, root
->root_key
.objectid
,
6486 /* if we fail to delete the orphan item this time
6487 * around, it'll get picked up the next time.
6489 * The most common failure here is just -ENOENT.
6491 btrfs_del_orphan_item(trans
, tree_root
,
6492 root
->root_key
.objectid
);
6496 if (root
->in_radix
) {
6497 btrfs_free_fs_root(tree_root
->fs_info
, root
);
6499 free_extent_buffer(root
->node
);
6500 free_extent_buffer(root
->commit_root
);
6504 btrfs_end_transaction_throttle(trans
, tree_root
);
6506 btrfs_free_path(path
);
6509 btrfs_std_error(root
->fs_info
, err
);
6514 * drop subtree rooted at tree block 'node'.
6516 * NOTE: this function will unlock and release tree block 'node'
6518 int btrfs_drop_subtree(struct btrfs_trans_handle
*trans
,
6519 struct btrfs_root
*root
,
6520 struct extent_buffer
*node
,
6521 struct extent_buffer
*parent
)
6523 struct btrfs_path
*path
;
6524 struct walk_control
*wc
;
6530 BUG_ON(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
6532 path
= btrfs_alloc_path();
6536 wc
= kzalloc(sizeof(*wc
), GFP_NOFS
);
6538 btrfs_free_path(path
);
6542 btrfs_assert_tree_locked(parent
);
6543 parent_level
= btrfs_header_level(parent
);
6544 extent_buffer_get(parent
);
6545 path
->nodes
[parent_level
] = parent
;
6546 path
->slots
[parent_level
] = btrfs_header_nritems(parent
);
6548 btrfs_assert_tree_locked(node
);
6549 level
= btrfs_header_level(node
);
6550 path
->nodes
[level
] = node
;
6551 path
->slots
[level
] = 0;
6552 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
6554 wc
->refs
[parent_level
] = 1;
6555 wc
->flags
[parent_level
] = BTRFS_BLOCK_FLAG_FULL_BACKREF
;
6557 wc
->shared_level
= -1;
6558 wc
->stage
= DROP_REFERENCE
;
6561 wc
->reada_count
= BTRFS_NODEPTRS_PER_BLOCK(root
);
6564 wret
= walk_down_tree(trans
, root
, path
, wc
);
6570 wret
= walk_up_tree(trans
, root
, path
, wc
, parent_level
);
6578 btrfs_free_path(path
);
6582 static u64
update_block_group_flags(struct btrfs_root
*root
, u64 flags
)
6585 u64 stripped
= BTRFS_BLOCK_GROUP_RAID0
|
6586 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
;
6589 * we add in the count of missing devices because we want
6590 * to make sure that any RAID levels on a degraded FS
6591 * continue to be honored.
6593 num_devices
= root
->fs_info
->fs_devices
->rw_devices
+
6594 root
->fs_info
->fs_devices
->missing_devices
;
6596 if (num_devices
== 1) {
6597 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
6598 stripped
= flags
& ~stripped
;
6600 /* turn raid0 into single device chunks */
6601 if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
6604 /* turn mirroring into duplication */
6605 if (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
6606 BTRFS_BLOCK_GROUP_RAID10
))
6607 return stripped
| BTRFS_BLOCK_GROUP_DUP
;
6610 /* they already had raid on here, just return */
6611 if (flags
& stripped
)
6614 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
6615 stripped
= flags
& ~stripped
;
6617 /* switch duplicated blocks with raid1 */
6618 if (flags
& BTRFS_BLOCK_GROUP_DUP
)
6619 return stripped
| BTRFS_BLOCK_GROUP_RAID1
;
6621 /* turn single device chunks into raid0 */
6622 return stripped
| BTRFS_BLOCK_GROUP_RAID0
;
6627 static int set_block_group_ro(struct btrfs_block_group_cache
*cache
, int force
)
6629 struct btrfs_space_info
*sinfo
= cache
->space_info
;
6631 u64 min_allocable_bytes
;
6636 * We need some metadata space and system metadata space for
6637 * allocating chunks in some corner cases until we force to set
6638 * it to be readonly.
6641 (BTRFS_BLOCK_GROUP_SYSTEM
| BTRFS_BLOCK_GROUP_METADATA
)) &&
6643 min_allocable_bytes
= 1 * 1024 * 1024;
6645 min_allocable_bytes
= 0;
6647 spin_lock(&sinfo
->lock
);
6648 spin_lock(&cache
->lock
);
6655 num_bytes
= cache
->key
.offset
- cache
->reserved
- cache
->pinned
-
6656 cache
->bytes_super
- btrfs_block_group_used(&cache
->item
);
6658 if (sinfo
->bytes_used
+ sinfo
->bytes_reserved
+ sinfo
->bytes_pinned
+
6659 sinfo
->bytes_may_use
+ sinfo
->bytes_readonly
+ num_bytes
+
6660 min_allocable_bytes
<= sinfo
->total_bytes
) {
6661 sinfo
->bytes_readonly
+= num_bytes
;
6666 spin_unlock(&cache
->lock
);
6667 spin_unlock(&sinfo
->lock
);
6671 int btrfs_set_block_group_ro(struct btrfs_root
*root
,
6672 struct btrfs_block_group_cache
*cache
)
6675 struct btrfs_trans_handle
*trans
;
6681 trans
= btrfs_join_transaction(root
);
6682 BUG_ON(IS_ERR(trans
));
6684 alloc_flags
= update_block_group_flags(root
, cache
->flags
);
6685 if (alloc_flags
!= cache
->flags
)
6686 do_chunk_alloc(trans
, root
, 2 * 1024 * 1024, alloc_flags
,
6689 ret
= set_block_group_ro(cache
, 0);
6692 alloc_flags
= get_alloc_profile(root
, cache
->space_info
->flags
);
6693 ret
= do_chunk_alloc(trans
, root
, 2 * 1024 * 1024, alloc_flags
,
6697 ret
= set_block_group_ro(cache
, 0);
6699 btrfs_end_transaction(trans
, root
);
6703 int btrfs_force_chunk_alloc(struct btrfs_trans_handle
*trans
,
6704 struct btrfs_root
*root
, u64 type
)
6706 u64 alloc_flags
= get_alloc_profile(root
, type
);
6707 return do_chunk_alloc(trans
, root
, 2 * 1024 * 1024, alloc_flags
,
6712 * helper to account the unused space of all the readonly block group in the
6713 * list. takes mirrors into account.
6715 static u64
__btrfs_get_ro_block_group_free_space(struct list_head
*groups_list
)
6717 struct btrfs_block_group_cache
*block_group
;
6721 list_for_each_entry(block_group
, groups_list
, list
) {
6722 spin_lock(&block_group
->lock
);
6724 if (!block_group
->ro
) {
6725 spin_unlock(&block_group
->lock
);
6729 if (block_group
->flags
& (BTRFS_BLOCK_GROUP_RAID1
|
6730 BTRFS_BLOCK_GROUP_RAID10
|
6731 BTRFS_BLOCK_GROUP_DUP
))
6736 free_bytes
+= (block_group
->key
.offset
-
6737 btrfs_block_group_used(&block_group
->item
)) *
6740 spin_unlock(&block_group
->lock
);
6747 * helper to account the unused space of all the readonly block group in the
6748 * space_info. takes mirrors into account.
6750 u64
btrfs_account_ro_block_groups_free_space(struct btrfs_space_info
*sinfo
)
6755 spin_lock(&sinfo
->lock
);
6757 for(i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++)
6758 if (!list_empty(&sinfo
->block_groups
[i
]))
6759 free_bytes
+= __btrfs_get_ro_block_group_free_space(
6760 &sinfo
->block_groups
[i
]);
6762 spin_unlock(&sinfo
->lock
);
6767 int btrfs_set_block_group_rw(struct btrfs_root
*root
,
6768 struct btrfs_block_group_cache
*cache
)
6770 struct btrfs_space_info
*sinfo
= cache
->space_info
;
6775 spin_lock(&sinfo
->lock
);
6776 spin_lock(&cache
->lock
);
6777 num_bytes
= cache
->key
.offset
- cache
->reserved
- cache
->pinned
-
6778 cache
->bytes_super
- btrfs_block_group_used(&cache
->item
);
6779 sinfo
->bytes_readonly
-= num_bytes
;
6781 spin_unlock(&cache
->lock
);
6782 spin_unlock(&sinfo
->lock
);
6787 * checks to see if its even possible to relocate this block group.
6789 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
6790 * ok to go ahead and try.
6792 int btrfs_can_relocate(struct btrfs_root
*root
, u64 bytenr
)
6794 struct btrfs_block_group_cache
*block_group
;
6795 struct btrfs_space_info
*space_info
;
6796 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
6797 struct btrfs_device
*device
;
6805 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
6807 /* odd, couldn't find the block group, leave it alone */
6811 min_free
= btrfs_block_group_used(&block_group
->item
);
6813 /* no bytes used, we're good */
6817 space_info
= block_group
->space_info
;
6818 spin_lock(&space_info
->lock
);
6820 full
= space_info
->full
;
6823 * if this is the last block group we have in this space, we can't
6824 * relocate it unless we're able to allocate a new chunk below.
6826 * Otherwise, we need to make sure we have room in the space to handle
6827 * all of the extents from this block group. If we can, we're good
6829 if ((space_info
->total_bytes
!= block_group
->key
.offset
) &&
6830 (space_info
->bytes_used
+ space_info
->bytes_reserved
+
6831 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
6832 min_free
< space_info
->total_bytes
)) {
6833 spin_unlock(&space_info
->lock
);
6836 spin_unlock(&space_info
->lock
);
6839 * ok we don't have enough space, but maybe we have free space on our
6840 * devices to allocate new chunks for relocation, so loop through our
6841 * alloc devices and guess if we have enough space. However, if we
6842 * were marked as full, then we know there aren't enough chunks, and we
6857 index
= get_block_group_index(block_group
);
6862 } else if (index
== 1) {
6864 } else if (index
== 2) {
6867 } else if (index
== 3) {
6868 dev_min
= fs_devices
->rw_devices
;
6869 do_div(min_free
, dev_min
);
6872 mutex_lock(&root
->fs_info
->chunk_mutex
);
6873 list_for_each_entry(device
, &fs_devices
->alloc_list
, dev_alloc_list
) {
6877 * check to make sure we can actually find a chunk with enough
6878 * space to fit our block group in.
6880 if (device
->total_bytes
> device
->bytes_used
+ min_free
) {
6881 ret
= find_free_dev_extent(NULL
, device
, min_free
,
6886 if (dev_nr
>= dev_min
)
6892 mutex_unlock(&root
->fs_info
->chunk_mutex
);
6894 btrfs_put_block_group(block_group
);
6898 static int find_first_block_group(struct btrfs_root
*root
,
6899 struct btrfs_path
*path
, struct btrfs_key
*key
)
6902 struct btrfs_key found_key
;
6903 struct extent_buffer
*leaf
;
6906 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
6911 slot
= path
->slots
[0];
6912 leaf
= path
->nodes
[0];
6913 if (slot
>= btrfs_header_nritems(leaf
)) {
6914 ret
= btrfs_next_leaf(root
, path
);
6921 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
6923 if (found_key
.objectid
>= key
->objectid
&&
6924 found_key
.type
== BTRFS_BLOCK_GROUP_ITEM_KEY
) {
6934 void btrfs_put_block_group_cache(struct btrfs_fs_info
*info
)
6936 struct btrfs_block_group_cache
*block_group
;
6940 struct inode
*inode
;
6942 block_group
= btrfs_lookup_first_block_group(info
, last
);
6943 while (block_group
) {
6944 spin_lock(&block_group
->lock
);
6945 if (block_group
->iref
)
6947 spin_unlock(&block_group
->lock
);
6948 block_group
= next_block_group(info
->tree_root
,
6958 inode
= block_group
->inode
;
6959 block_group
->iref
= 0;
6960 block_group
->inode
= NULL
;
6961 spin_unlock(&block_group
->lock
);
6963 last
= block_group
->key
.objectid
+ block_group
->key
.offset
;
6964 btrfs_put_block_group(block_group
);
6968 int btrfs_free_block_groups(struct btrfs_fs_info
*info
)
6970 struct btrfs_block_group_cache
*block_group
;
6971 struct btrfs_space_info
*space_info
;
6972 struct btrfs_caching_control
*caching_ctl
;
6975 down_write(&info
->extent_commit_sem
);
6976 while (!list_empty(&info
->caching_block_groups
)) {
6977 caching_ctl
= list_entry(info
->caching_block_groups
.next
,
6978 struct btrfs_caching_control
, list
);
6979 list_del(&caching_ctl
->list
);
6980 put_caching_control(caching_ctl
);
6982 up_write(&info
->extent_commit_sem
);
6984 spin_lock(&info
->block_group_cache_lock
);
6985 while ((n
= rb_last(&info
->block_group_cache_tree
)) != NULL
) {
6986 block_group
= rb_entry(n
, struct btrfs_block_group_cache
,
6988 rb_erase(&block_group
->cache_node
,
6989 &info
->block_group_cache_tree
);
6990 spin_unlock(&info
->block_group_cache_lock
);
6992 down_write(&block_group
->space_info
->groups_sem
);
6993 list_del(&block_group
->list
);
6994 up_write(&block_group
->space_info
->groups_sem
);
6996 if (block_group
->cached
== BTRFS_CACHE_STARTED
)
6997 wait_block_group_cache_done(block_group
);
7000 * We haven't cached this block group, which means we could
7001 * possibly have excluded extents on this block group.
7003 if (block_group
->cached
== BTRFS_CACHE_NO
)
7004 free_excluded_extents(info
->extent_root
, block_group
);
7006 btrfs_remove_free_space_cache(block_group
);
7007 btrfs_put_block_group(block_group
);
7009 spin_lock(&info
->block_group_cache_lock
);
7011 spin_unlock(&info
->block_group_cache_lock
);
7013 /* now that all the block groups are freed, go through and
7014 * free all the space_info structs. This is only called during
7015 * the final stages of unmount, and so we know nobody is
7016 * using them. We call synchronize_rcu() once before we start,
7017 * just to be on the safe side.
7021 release_global_block_rsv(info
);
7023 while(!list_empty(&info
->space_info
)) {
7024 space_info
= list_entry(info
->space_info
.next
,
7025 struct btrfs_space_info
,
7027 if (space_info
->bytes_pinned
> 0 ||
7028 space_info
->bytes_reserved
> 0 ||
7029 space_info
->bytes_may_use
> 0) {
7031 dump_space_info(space_info
, 0, 0);
7033 list_del(&space_info
->list
);
7039 static void __link_block_group(struct btrfs_space_info
*space_info
,
7040 struct btrfs_block_group_cache
*cache
)
7042 int index
= get_block_group_index(cache
);
7044 down_write(&space_info
->groups_sem
);
7045 list_add_tail(&cache
->list
, &space_info
->block_groups
[index
]);
7046 up_write(&space_info
->groups_sem
);
7049 int btrfs_read_block_groups(struct btrfs_root
*root
)
7051 struct btrfs_path
*path
;
7053 struct btrfs_block_group_cache
*cache
;
7054 struct btrfs_fs_info
*info
= root
->fs_info
;
7055 struct btrfs_space_info
*space_info
;
7056 struct btrfs_key key
;
7057 struct btrfs_key found_key
;
7058 struct extent_buffer
*leaf
;
7062 root
= info
->extent_root
;
7065 btrfs_set_key_type(&key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
7066 path
= btrfs_alloc_path();
7071 cache_gen
= btrfs_super_cache_generation(&root
->fs_info
->super_copy
);
7072 if (btrfs_test_opt(root
, SPACE_CACHE
) &&
7073 btrfs_super_generation(&root
->fs_info
->super_copy
) != cache_gen
)
7075 if (btrfs_test_opt(root
, CLEAR_CACHE
))
7079 ret
= find_first_block_group(root
, path
, &key
);
7084 leaf
= path
->nodes
[0];
7085 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
7086 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
7091 cache
->free_space_ctl
= kzalloc(sizeof(*cache
->free_space_ctl
),
7093 if (!cache
->free_space_ctl
) {
7099 atomic_set(&cache
->count
, 1);
7100 spin_lock_init(&cache
->lock
);
7101 cache
->fs_info
= info
;
7102 INIT_LIST_HEAD(&cache
->list
);
7103 INIT_LIST_HEAD(&cache
->cluster_list
);
7106 cache
->disk_cache_state
= BTRFS_DC_CLEAR
;
7108 read_extent_buffer(leaf
, &cache
->item
,
7109 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
7110 sizeof(cache
->item
));
7111 memcpy(&cache
->key
, &found_key
, sizeof(found_key
));
7113 key
.objectid
= found_key
.objectid
+ found_key
.offset
;
7114 btrfs_release_path(path
);
7115 cache
->flags
= btrfs_block_group_flags(&cache
->item
);
7116 cache
->sectorsize
= root
->sectorsize
;
7118 btrfs_init_free_space_ctl(cache
);
7121 * We need to exclude the super stripes now so that the space
7122 * info has super bytes accounted for, otherwise we'll think
7123 * we have more space than we actually do.
7125 exclude_super_stripes(root
, cache
);
7128 * check for two cases, either we are full, and therefore
7129 * don't need to bother with the caching work since we won't
7130 * find any space, or we are empty, and we can just add all
7131 * the space in and be done with it. This saves us _alot_ of
7132 * time, particularly in the full case.
7134 if (found_key
.offset
== btrfs_block_group_used(&cache
->item
)) {
7135 cache
->last_byte_to_unpin
= (u64
)-1;
7136 cache
->cached
= BTRFS_CACHE_FINISHED
;
7137 free_excluded_extents(root
, cache
);
7138 } else if (btrfs_block_group_used(&cache
->item
) == 0) {
7139 cache
->last_byte_to_unpin
= (u64
)-1;
7140 cache
->cached
= BTRFS_CACHE_FINISHED
;
7141 add_new_free_space(cache
, root
->fs_info
,
7143 found_key
.objectid
+
7145 free_excluded_extents(root
, cache
);
7148 ret
= update_space_info(info
, cache
->flags
, found_key
.offset
,
7149 btrfs_block_group_used(&cache
->item
),
7152 cache
->space_info
= space_info
;
7153 spin_lock(&cache
->space_info
->lock
);
7154 cache
->space_info
->bytes_readonly
+= cache
->bytes_super
;
7155 spin_unlock(&cache
->space_info
->lock
);
7157 __link_block_group(space_info
, cache
);
7159 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
7162 set_avail_alloc_bits(root
->fs_info
, cache
->flags
);
7163 if (btrfs_chunk_readonly(root
, cache
->key
.objectid
))
7164 set_block_group_ro(cache
, 1);
7167 list_for_each_entry_rcu(space_info
, &root
->fs_info
->space_info
, list
) {
7168 if (!(get_alloc_profile(root
, space_info
->flags
) &
7169 (BTRFS_BLOCK_GROUP_RAID10
|
7170 BTRFS_BLOCK_GROUP_RAID1
|
7171 BTRFS_BLOCK_GROUP_DUP
)))
7174 * avoid allocating from un-mirrored block group if there are
7175 * mirrored block groups.
7177 list_for_each_entry(cache
, &space_info
->block_groups
[3], list
)
7178 set_block_group_ro(cache
, 1);
7179 list_for_each_entry(cache
, &space_info
->block_groups
[4], list
)
7180 set_block_group_ro(cache
, 1);
7183 init_global_block_rsv(info
);
7186 btrfs_free_path(path
);
7190 int btrfs_make_block_group(struct btrfs_trans_handle
*trans
,
7191 struct btrfs_root
*root
, u64 bytes_used
,
7192 u64 type
, u64 chunk_objectid
, u64 chunk_offset
,
7196 struct btrfs_root
*extent_root
;
7197 struct btrfs_block_group_cache
*cache
;
7199 extent_root
= root
->fs_info
->extent_root
;
7201 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
7203 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
7206 cache
->free_space_ctl
= kzalloc(sizeof(*cache
->free_space_ctl
),
7208 if (!cache
->free_space_ctl
) {
7213 cache
->key
.objectid
= chunk_offset
;
7214 cache
->key
.offset
= size
;
7215 cache
->key
.type
= BTRFS_BLOCK_GROUP_ITEM_KEY
;
7216 cache
->sectorsize
= root
->sectorsize
;
7217 cache
->fs_info
= root
->fs_info
;
7219 atomic_set(&cache
->count
, 1);
7220 spin_lock_init(&cache
->lock
);
7221 INIT_LIST_HEAD(&cache
->list
);
7222 INIT_LIST_HEAD(&cache
->cluster_list
);
7224 btrfs_init_free_space_ctl(cache
);
7226 btrfs_set_block_group_used(&cache
->item
, bytes_used
);
7227 btrfs_set_block_group_chunk_objectid(&cache
->item
, chunk_objectid
);
7228 cache
->flags
= type
;
7229 btrfs_set_block_group_flags(&cache
->item
, type
);
7231 cache
->last_byte_to_unpin
= (u64
)-1;
7232 cache
->cached
= BTRFS_CACHE_FINISHED
;
7233 exclude_super_stripes(root
, cache
);
7235 add_new_free_space(cache
, root
->fs_info
, chunk_offset
,
7236 chunk_offset
+ size
);
7238 free_excluded_extents(root
, cache
);
7240 ret
= update_space_info(root
->fs_info
, cache
->flags
, size
, bytes_used
,
7241 &cache
->space_info
);
7244 spin_lock(&cache
->space_info
->lock
);
7245 cache
->space_info
->bytes_readonly
+= cache
->bytes_super
;
7246 spin_unlock(&cache
->space_info
->lock
);
7248 __link_block_group(cache
->space_info
, cache
);
7250 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
7253 ret
= btrfs_insert_item(trans
, extent_root
, &cache
->key
, &cache
->item
,
7254 sizeof(cache
->item
));
7257 set_avail_alloc_bits(extent_root
->fs_info
, type
);
7262 int btrfs_remove_block_group(struct btrfs_trans_handle
*trans
,
7263 struct btrfs_root
*root
, u64 group_start
)
7265 struct btrfs_path
*path
;
7266 struct btrfs_block_group_cache
*block_group
;
7267 struct btrfs_free_cluster
*cluster
;
7268 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
7269 struct btrfs_key key
;
7270 struct inode
*inode
;
7274 root
= root
->fs_info
->extent_root
;
7276 block_group
= btrfs_lookup_block_group(root
->fs_info
, group_start
);
7277 BUG_ON(!block_group
);
7278 BUG_ON(!block_group
->ro
);
7281 * Free the reserved super bytes from this block group before
7284 free_excluded_extents(root
, block_group
);
7286 memcpy(&key
, &block_group
->key
, sizeof(key
));
7287 if (block_group
->flags
& (BTRFS_BLOCK_GROUP_DUP
|
7288 BTRFS_BLOCK_GROUP_RAID1
|
7289 BTRFS_BLOCK_GROUP_RAID10
))
7294 /* make sure this block group isn't part of an allocation cluster */
7295 cluster
= &root
->fs_info
->data_alloc_cluster
;
7296 spin_lock(&cluster
->refill_lock
);
7297 btrfs_return_cluster_to_free_space(block_group
, cluster
);
7298 spin_unlock(&cluster
->refill_lock
);
7301 * make sure this block group isn't part of a metadata
7302 * allocation cluster
7304 cluster
= &root
->fs_info
->meta_alloc_cluster
;
7305 spin_lock(&cluster
->refill_lock
);
7306 btrfs_return_cluster_to_free_space(block_group
, cluster
);
7307 spin_unlock(&cluster
->refill_lock
);
7309 path
= btrfs_alloc_path();
7315 inode
= lookup_free_space_inode(root
, block_group
, path
);
7316 if (!IS_ERR(inode
)) {
7317 ret
= btrfs_orphan_add(trans
, inode
);
7320 /* One for the block groups ref */
7321 spin_lock(&block_group
->lock
);
7322 if (block_group
->iref
) {
7323 block_group
->iref
= 0;
7324 block_group
->inode
= NULL
;
7325 spin_unlock(&block_group
->lock
);
7328 spin_unlock(&block_group
->lock
);
7330 /* One for our lookup ref */
7331 btrfs_add_delayed_iput(inode
);
7334 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
7335 key
.offset
= block_group
->key
.objectid
;
7338 ret
= btrfs_search_slot(trans
, tree_root
, &key
, path
, -1, 1);
7342 btrfs_release_path(path
);
7344 ret
= btrfs_del_item(trans
, tree_root
, path
);
7347 btrfs_release_path(path
);
7350 spin_lock(&root
->fs_info
->block_group_cache_lock
);
7351 rb_erase(&block_group
->cache_node
,
7352 &root
->fs_info
->block_group_cache_tree
);
7353 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
7355 down_write(&block_group
->space_info
->groups_sem
);
7357 * we must use list_del_init so people can check to see if they
7358 * are still on the list after taking the semaphore
7360 list_del_init(&block_group
->list
);
7361 up_write(&block_group
->space_info
->groups_sem
);
7363 if (block_group
->cached
== BTRFS_CACHE_STARTED
)
7364 wait_block_group_cache_done(block_group
);
7366 btrfs_remove_free_space_cache(block_group
);
7368 spin_lock(&block_group
->space_info
->lock
);
7369 block_group
->space_info
->total_bytes
-= block_group
->key
.offset
;
7370 block_group
->space_info
->bytes_readonly
-= block_group
->key
.offset
;
7371 block_group
->space_info
->disk_total
-= block_group
->key
.offset
* factor
;
7372 spin_unlock(&block_group
->space_info
->lock
);
7374 memcpy(&key
, &block_group
->key
, sizeof(key
));
7376 btrfs_clear_space_info_full(root
->fs_info
);
7378 btrfs_put_block_group(block_group
);
7379 btrfs_put_block_group(block_group
);
7381 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
7387 ret
= btrfs_del_item(trans
, root
, path
);
7389 btrfs_free_path(path
);
7393 int btrfs_init_space_info(struct btrfs_fs_info
*fs_info
)
7395 struct btrfs_space_info
*space_info
;
7396 struct btrfs_super_block
*disk_super
;
7402 disk_super
= &fs_info
->super_copy
;
7403 if (!btrfs_super_root(disk_super
))
7406 features
= btrfs_super_incompat_flags(disk_super
);
7407 if (features
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
7410 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
7411 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
7416 flags
= BTRFS_BLOCK_GROUP_METADATA
| BTRFS_BLOCK_GROUP_DATA
;
7417 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
7419 flags
= BTRFS_BLOCK_GROUP_METADATA
;
7420 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
7424 flags
= BTRFS_BLOCK_GROUP_DATA
;
7425 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
7431 int btrfs_error_unpin_extent_range(struct btrfs_root
*root
, u64 start
, u64 end
)
7433 return unpin_extent_range(root
, start
, end
);
7436 int btrfs_error_discard_extent(struct btrfs_root
*root
, u64 bytenr
,
7437 u64 num_bytes
, u64
*actual_bytes
)
7439 return btrfs_discard_extent(root
, bytenr
, num_bytes
, actual_bytes
);
7442 int btrfs_trim_fs(struct btrfs_root
*root
, struct fstrim_range
*range
)
7444 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
7445 struct btrfs_block_group_cache
*cache
= NULL
;
7452 cache
= btrfs_lookup_block_group(fs_info
, range
->start
);
7455 if (cache
->key
.objectid
>= (range
->start
+ range
->len
)) {
7456 btrfs_put_block_group(cache
);
7460 start
= max(range
->start
, cache
->key
.objectid
);
7461 end
= min(range
->start
+ range
->len
,
7462 cache
->key
.objectid
+ cache
->key
.offset
);
7464 if (end
- start
>= range
->minlen
) {
7465 if (!block_group_cache_done(cache
)) {
7466 ret
= cache_block_group(cache
, NULL
, root
, 0);
7468 wait_block_group_cache_done(cache
);
7470 ret
= btrfs_trim_block_group(cache
,
7476 trimmed
+= group_trimmed
;
7478 btrfs_put_block_group(cache
);
7483 cache
= next_block_group(fs_info
->tree_root
, cache
);
7486 range
->len
= trimmed
;