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 spin_lock(&cache
->lock
);
486 if (cache
->cached
!= BTRFS_CACHE_NO
) {
487 spin_unlock(&cache
->lock
);
490 cache
->cached
= BTRFS_CACHE_STARTED
;
491 spin_unlock(&cache
->lock
);
493 ret
= load_free_space_cache(fs_info
, cache
);
495 spin_lock(&cache
->lock
);
497 cache
->cached
= BTRFS_CACHE_FINISHED
;
498 cache
->last_byte_to_unpin
= (u64
)-1;
500 cache
->cached
= BTRFS_CACHE_NO
;
502 spin_unlock(&cache
->lock
);
504 free_excluded_extents(fs_info
->extent_root
, cache
);
512 caching_ctl
= kzalloc(sizeof(*caching_ctl
), GFP_NOFS
);
513 BUG_ON(!caching_ctl
);
515 INIT_LIST_HEAD(&caching_ctl
->list
);
516 mutex_init(&caching_ctl
->mutex
);
517 init_waitqueue_head(&caching_ctl
->wait
);
518 caching_ctl
->block_group
= cache
;
519 caching_ctl
->progress
= cache
->key
.objectid
;
520 /* one for caching kthread, one for caching block group list */
521 atomic_set(&caching_ctl
->count
, 2);
522 caching_ctl
->work
.func
= caching_thread
;
524 spin_lock(&cache
->lock
);
525 if (cache
->cached
!= BTRFS_CACHE_NO
) {
526 spin_unlock(&cache
->lock
);
530 cache
->caching_ctl
= caching_ctl
;
531 cache
->cached
= BTRFS_CACHE_STARTED
;
532 spin_unlock(&cache
->lock
);
534 down_write(&fs_info
->extent_commit_sem
);
535 list_add_tail(&caching_ctl
->list
, &fs_info
->caching_block_groups
);
536 up_write(&fs_info
->extent_commit_sem
);
538 btrfs_get_block_group(cache
);
540 btrfs_queue_worker(&fs_info
->caching_workers
, &caching_ctl
->work
);
546 * return the block group that starts at or after bytenr
548 static struct btrfs_block_group_cache
*
549 btrfs_lookup_first_block_group(struct btrfs_fs_info
*info
, u64 bytenr
)
551 struct btrfs_block_group_cache
*cache
;
553 cache
= block_group_cache_tree_search(info
, bytenr
, 0);
559 * return the block group that contains the given bytenr
561 struct btrfs_block_group_cache
*btrfs_lookup_block_group(
562 struct btrfs_fs_info
*info
,
565 struct btrfs_block_group_cache
*cache
;
567 cache
= block_group_cache_tree_search(info
, bytenr
, 1);
572 static struct btrfs_space_info
*__find_space_info(struct btrfs_fs_info
*info
,
575 struct list_head
*head
= &info
->space_info
;
576 struct btrfs_space_info
*found
;
578 flags
&= BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_SYSTEM
|
579 BTRFS_BLOCK_GROUP_METADATA
;
582 list_for_each_entry_rcu(found
, head
, list
) {
583 if (found
->flags
& flags
) {
593 * after adding space to the filesystem, we need to clear the full flags
594 * on all the space infos.
596 void btrfs_clear_space_info_full(struct btrfs_fs_info
*info
)
598 struct list_head
*head
= &info
->space_info
;
599 struct btrfs_space_info
*found
;
602 list_for_each_entry_rcu(found
, head
, list
)
607 static u64
div_factor(u64 num
, int factor
)
616 static u64
div_factor_fine(u64 num
, int factor
)
625 u64
btrfs_find_block_group(struct btrfs_root
*root
,
626 u64 search_start
, u64 search_hint
, int owner
)
628 struct btrfs_block_group_cache
*cache
;
630 u64 last
= max(search_hint
, search_start
);
637 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
641 spin_lock(&cache
->lock
);
642 last
= cache
->key
.objectid
+ cache
->key
.offset
;
643 used
= btrfs_block_group_used(&cache
->item
);
645 if ((full_search
|| !cache
->ro
) &&
646 block_group_bits(cache
, BTRFS_BLOCK_GROUP_METADATA
)) {
647 if (used
+ cache
->pinned
+ cache
->reserved
<
648 div_factor(cache
->key
.offset
, factor
)) {
649 group_start
= cache
->key
.objectid
;
650 spin_unlock(&cache
->lock
);
651 btrfs_put_block_group(cache
);
655 spin_unlock(&cache
->lock
);
656 btrfs_put_block_group(cache
);
664 if (!full_search
&& factor
< 10) {
674 /* simple helper to search for an existing extent at a given offset */
675 int btrfs_lookup_extent(struct btrfs_root
*root
, u64 start
, u64 len
)
678 struct btrfs_key key
;
679 struct btrfs_path
*path
;
681 path
= btrfs_alloc_path();
685 key
.objectid
= start
;
687 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
688 ret
= btrfs_search_slot(NULL
, root
->fs_info
->extent_root
, &key
, path
,
690 btrfs_free_path(path
);
695 * helper function to lookup reference count and flags of extent.
697 * the head node for delayed ref is used to store the sum of all the
698 * reference count modifications queued up in the rbtree. the head
699 * node may also store the extent flags to set. This way you can check
700 * to see what the reference count and extent flags would be if all of
701 * the delayed refs are not processed.
703 int btrfs_lookup_extent_info(struct btrfs_trans_handle
*trans
,
704 struct btrfs_root
*root
, u64 bytenr
,
705 u64 num_bytes
, u64
*refs
, u64
*flags
)
707 struct btrfs_delayed_ref_head
*head
;
708 struct btrfs_delayed_ref_root
*delayed_refs
;
709 struct btrfs_path
*path
;
710 struct btrfs_extent_item
*ei
;
711 struct extent_buffer
*leaf
;
712 struct btrfs_key key
;
718 path
= btrfs_alloc_path();
722 key
.objectid
= bytenr
;
723 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
724 key
.offset
= num_bytes
;
726 path
->skip_locking
= 1;
727 path
->search_commit_root
= 1;
730 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
,
736 leaf
= path
->nodes
[0];
737 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
738 if (item_size
>= sizeof(*ei
)) {
739 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
740 struct btrfs_extent_item
);
741 num_refs
= btrfs_extent_refs(leaf
, ei
);
742 extent_flags
= btrfs_extent_flags(leaf
, ei
);
744 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
745 struct btrfs_extent_item_v0
*ei0
;
746 BUG_ON(item_size
!= sizeof(*ei0
));
747 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
748 struct btrfs_extent_item_v0
);
749 num_refs
= btrfs_extent_refs_v0(leaf
, ei0
);
750 /* FIXME: this isn't correct for data */
751 extent_flags
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
756 BUG_ON(num_refs
== 0);
766 delayed_refs
= &trans
->transaction
->delayed_refs
;
767 spin_lock(&delayed_refs
->lock
);
768 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
770 if (!mutex_trylock(&head
->mutex
)) {
771 atomic_inc(&head
->node
.refs
);
772 spin_unlock(&delayed_refs
->lock
);
774 btrfs_release_path(path
);
777 * Mutex was contended, block until it's released and try
780 mutex_lock(&head
->mutex
);
781 mutex_unlock(&head
->mutex
);
782 btrfs_put_delayed_ref(&head
->node
);
785 if (head
->extent_op
&& head
->extent_op
->update_flags
)
786 extent_flags
|= head
->extent_op
->flags_to_set
;
788 BUG_ON(num_refs
== 0);
790 num_refs
+= head
->node
.ref_mod
;
791 mutex_unlock(&head
->mutex
);
793 spin_unlock(&delayed_refs
->lock
);
795 WARN_ON(num_refs
== 0);
799 *flags
= extent_flags
;
801 btrfs_free_path(path
);
806 * Back reference rules. Back refs have three main goals:
808 * 1) differentiate between all holders of references to an extent so that
809 * when a reference is dropped we can make sure it was a valid reference
810 * before freeing the extent.
812 * 2) Provide enough information to quickly find the holders of an extent
813 * if we notice a given block is corrupted or bad.
815 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
816 * maintenance. This is actually the same as #2, but with a slightly
817 * different use case.
819 * There are two kinds of back refs. The implicit back refs is optimized
820 * for pointers in non-shared tree blocks. For a given pointer in a block,
821 * back refs of this kind provide information about the block's owner tree
822 * and the pointer's key. These information allow us to find the block by
823 * b-tree searching. The full back refs is for pointers in tree blocks not
824 * referenced by their owner trees. The location of tree block is recorded
825 * in the back refs. Actually the full back refs is generic, and can be
826 * used in all cases the implicit back refs is used. The major shortcoming
827 * of the full back refs is its overhead. Every time a tree block gets
828 * COWed, we have to update back refs entry for all pointers in it.
830 * For a newly allocated tree block, we use implicit back refs for
831 * pointers in it. This means most tree related operations only involve
832 * implicit back refs. For a tree block created in old transaction, the
833 * only way to drop a reference to it is COW it. So we can detect the
834 * event that tree block loses its owner tree's reference and do the
835 * back refs conversion.
837 * When a tree block is COW'd through a tree, there are four cases:
839 * The reference count of the block is one and the tree is the block's
840 * owner tree. Nothing to do in this case.
842 * The reference count of the block is one and the tree is not the
843 * block's owner tree. In this case, full back refs is used for pointers
844 * in the block. Remove these full back refs, add implicit back refs for
845 * every pointers in the new block.
847 * The reference count of the block is greater than one and the tree is
848 * the block's owner tree. In this case, implicit back refs is used for
849 * pointers in the block. Add full back refs for every pointers in the
850 * block, increase lower level extents' reference counts. The original
851 * implicit back refs are entailed to the new block.
853 * The reference count of the block is greater than one and the tree is
854 * not the block's owner tree. Add implicit back refs for every pointer in
855 * the new block, increase lower level extents' reference count.
857 * Back Reference Key composing:
859 * The key objectid corresponds to the first byte in the extent,
860 * The key type is used to differentiate between types of back refs.
861 * There are different meanings of the key offset for different types
864 * File extents can be referenced by:
866 * - multiple snapshots, subvolumes, or different generations in one subvol
867 * - different files inside a single subvolume
868 * - different offsets inside a file (bookend extents in file.c)
870 * The extent ref structure for the implicit back refs has fields for:
872 * - Objectid of the subvolume root
873 * - objectid of the file holding the reference
874 * - original offset in the file
875 * - how many bookend extents
877 * The key offset for the implicit back refs is hash of the first
880 * The extent ref structure for the full back refs has field for:
882 * - number of pointers in the tree leaf
884 * The key offset for the implicit back refs is the first byte of
887 * When a file extent is allocated, The implicit back refs is used.
888 * the fields are filled in:
890 * (root_key.objectid, inode objectid, offset in file, 1)
892 * When a file extent is removed file truncation, we find the
893 * corresponding implicit back refs and check the following fields:
895 * (btrfs_header_owner(leaf), inode objectid, offset in file)
897 * Btree extents can be referenced by:
899 * - Different subvolumes
901 * Both the implicit back refs and the full back refs for tree blocks
902 * only consist of key. The key offset for the implicit back refs is
903 * objectid of block's owner tree. The key offset for the full back refs
904 * is the first byte of parent block.
906 * When implicit back refs is used, information about the lowest key and
907 * level of the tree block are required. These information are stored in
908 * tree block info structure.
911 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
912 static int convert_extent_item_v0(struct btrfs_trans_handle
*trans
,
913 struct btrfs_root
*root
,
914 struct btrfs_path
*path
,
915 u64 owner
, u32 extra_size
)
917 struct btrfs_extent_item
*item
;
918 struct btrfs_extent_item_v0
*ei0
;
919 struct btrfs_extent_ref_v0
*ref0
;
920 struct btrfs_tree_block_info
*bi
;
921 struct extent_buffer
*leaf
;
922 struct btrfs_key key
;
923 struct btrfs_key found_key
;
924 u32 new_size
= sizeof(*item
);
928 leaf
= path
->nodes
[0];
929 BUG_ON(btrfs_item_size_nr(leaf
, path
->slots
[0]) != sizeof(*ei0
));
931 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
932 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
933 struct btrfs_extent_item_v0
);
934 refs
= btrfs_extent_refs_v0(leaf
, ei0
);
936 if (owner
== (u64
)-1) {
938 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
939 ret
= btrfs_next_leaf(root
, path
);
943 leaf
= path
->nodes
[0];
945 btrfs_item_key_to_cpu(leaf
, &found_key
,
947 BUG_ON(key
.objectid
!= found_key
.objectid
);
948 if (found_key
.type
!= BTRFS_EXTENT_REF_V0_KEY
) {
952 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
953 struct btrfs_extent_ref_v0
);
954 owner
= btrfs_ref_objectid_v0(leaf
, ref0
);
958 btrfs_release_path(path
);
960 if (owner
< BTRFS_FIRST_FREE_OBJECTID
)
961 new_size
+= sizeof(*bi
);
963 new_size
-= sizeof(*ei0
);
964 ret
= btrfs_search_slot(trans
, root
, &key
, path
,
965 new_size
+ extra_size
, 1);
970 ret
= btrfs_extend_item(trans
, root
, path
, new_size
);
972 leaf
= path
->nodes
[0];
973 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
974 btrfs_set_extent_refs(leaf
, item
, refs
);
975 /* FIXME: get real generation */
976 btrfs_set_extent_generation(leaf
, item
, 0);
977 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
978 btrfs_set_extent_flags(leaf
, item
,
979 BTRFS_EXTENT_FLAG_TREE_BLOCK
|
980 BTRFS_BLOCK_FLAG_FULL_BACKREF
);
981 bi
= (struct btrfs_tree_block_info
*)(item
+ 1);
982 /* FIXME: get first key of the block */
983 memset_extent_buffer(leaf
, 0, (unsigned long)bi
, sizeof(*bi
));
984 btrfs_set_tree_block_level(leaf
, bi
, (int)owner
);
986 btrfs_set_extent_flags(leaf
, item
, BTRFS_EXTENT_FLAG_DATA
);
988 btrfs_mark_buffer_dirty(leaf
);
993 static u64
hash_extent_data_ref(u64 root_objectid
, u64 owner
, u64 offset
)
995 u32 high_crc
= ~(u32
)0;
996 u32 low_crc
= ~(u32
)0;
999 lenum
= cpu_to_le64(root_objectid
);
1000 high_crc
= crc32c(high_crc
, &lenum
, sizeof(lenum
));
1001 lenum
= cpu_to_le64(owner
);
1002 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
1003 lenum
= cpu_to_le64(offset
);
1004 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
1006 return ((u64
)high_crc
<< 31) ^ (u64
)low_crc
;
1009 static u64
hash_extent_data_ref_item(struct extent_buffer
*leaf
,
1010 struct btrfs_extent_data_ref
*ref
)
1012 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf
, ref
),
1013 btrfs_extent_data_ref_objectid(leaf
, ref
),
1014 btrfs_extent_data_ref_offset(leaf
, ref
));
1017 static int match_extent_data_ref(struct extent_buffer
*leaf
,
1018 struct btrfs_extent_data_ref
*ref
,
1019 u64 root_objectid
, u64 owner
, u64 offset
)
1021 if (btrfs_extent_data_ref_root(leaf
, ref
) != root_objectid
||
1022 btrfs_extent_data_ref_objectid(leaf
, ref
) != owner
||
1023 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
1028 static noinline
int lookup_extent_data_ref(struct btrfs_trans_handle
*trans
,
1029 struct btrfs_root
*root
,
1030 struct btrfs_path
*path
,
1031 u64 bytenr
, u64 parent
,
1033 u64 owner
, u64 offset
)
1035 struct btrfs_key key
;
1036 struct btrfs_extent_data_ref
*ref
;
1037 struct extent_buffer
*leaf
;
1043 key
.objectid
= bytenr
;
1045 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
1046 key
.offset
= parent
;
1048 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
1049 key
.offset
= hash_extent_data_ref(root_objectid
,
1054 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1063 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1064 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
1065 btrfs_release_path(path
);
1066 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1077 leaf
= path
->nodes
[0];
1078 nritems
= btrfs_header_nritems(leaf
);
1080 if (path
->slots
[0] >= nritems
) {
1081 ret
= btrfs_next_leaf(root
, path
);
1087 leaf
= path
->nodes
[0];
1088 nritems
= btrfs_header_nritems(leaf
);
1092 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1093 if (key
.objectid
!= bytenr
||
1094 key
.type
!= BTRFS_EXTENT_DATA_REF_KEY
)
1097 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1098 struct btrfs_extent_data_ref
);
1100 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
1103 btrfs_release_path(path
);
1115 static noinline
int insert_extent_data_ref(struct btrfs_trans_handle
*trans
,
1116 struct btrfs_root
*root
,
1117 struct btrfs_path
*path
,
1118 u64 bytenr
, u64 parent
,
1119 u64 root_objectid
, u64 owner
,
1120 u64 offset
, int refs_to_add
)
1122 struct btrfs_key key
;
1123 struct extent_buffer
*leaf
;
1128 key
.objectid
= bytenr
;
1130 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
1131 key
.offset
= parent
;
1132 size
= sizeof(struct btrfs_shared_data_ref
);
1134 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
1135 key
.offset
= hash_extent_data_ref(root_objectid
,
1137 size
= sizeof(struct btrfs_extent_data_ref
);
1140 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, size
);
1141 if (ret
&& ret
!= -EEXIST
)
1144 leaf
= path
->nodes
[0];
1146 struct btrfs_shared_data_ref
*ref
;
1147 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1148 struct btrfs_shared_data_ref
);
1150 btrfs_set_shared_data_ref_count(leaf
, ref
, refs_to_add
);
1152 num_refs
= btrfs_shared_data_ref_count(leaf
, ref
);
1153 num_refs
+= refs_to_add
;
1154 btrfs_set_shared_data_ref_count(leaf
, ref
, num_refs
);
1157 struct btrfs_extent_data_ref
*ref
;
1158 while (ret
== -EEXIST
) {
1159 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1160 struct btrfs_extent_data_ref
);
1161 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
1164 btrfs_release_path(path
);
1166 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1168 if (ret
&& ret
!= -EEXIST
)
1171 leaf
= path
->nodes
[0];
1173 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1174 struct btrfs_extent_data_ref
);
1176 btrfs_set_extent_data_ref_root(leaf
, ref
,
1178 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
1179 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
1180 btrfs_set_extent_data_ref_count(leaf
, ref
, refs_to_add
);
1182 num_refs
= btrfs_extent_data_ref_count(leaf
, ref
);
1183 num_refs
+= refs_to_add
;
1184 btrfs_set_extent_data_ref_count(leaf
, ref
, num_refs
);
1187 btrfs_mark_buffer_dirty(leaf
);
1190 btrfs_release_path(path
);
1194 static noinline
int remove_extent_data_ref(struct btrfs_trans_handle
*trans
,
1195 struct btrfs_root
*root
,
1196 struct btrfs_path
*path
,
1199 struct btrfs_key key
;
1200 struct btrfs_extent_data_ref
*ref1
= NULL
;
1201 struct btrfs_shared_data_ref
*ref2
= NULL
;
1202 struct extent_buffer
*leaf
;
1206 leaf
= path
->nodes
[0];
1207 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1209 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1210 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
1211 struct btrfs_extent_data_ref
);
1212 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1213 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
1214 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
1215 struct btrfs_shared_data_ref
);
1216 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1217 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1218 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
1219 struct btrfs_extent_ref_v0
*ref0
;
1220 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1221 struct btrfs_extent_ref_v0
);
1222 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
1228 BUG_ON(num_refs
< refs_to_drop
);
1229 num_refs
-= refs_to_drop
;
1231 if (num_refs
== 0) {
1232 ret
= btrfs_del_item(trans
, root
, path
);
1234 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
)
1235 btrfs_set_extent_data_ref_count(leaf
, ref1
, num_refs
);
1236 else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
)
1237 btrfs_set_shared_data_ref_count(leaf
, ref2
, num_refs
);
1238 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1240 struct btrfs_extent_ref_v0
*ref0
;
1241 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1242 struct btrfs_extent_ref_v0
);
1243 btrfs_set_ref_count_v0(leaf
, ref0
, num_refs
);
1246 btrfs_mark_buffer_dirty(leaf
);
1251 static noinline u32
extent_data_ref_count(struct btrfs_root
*root
,
1252 struct btrfs_path
*path
,
1253 struct btrfs_extent_inline_ref
*iref
)
1255 struct btrfs_key key
;
1256 struct extent_buffer
*leaf
;
1257 struct btrfs_extent_data_ref
*ref1
;
1258 struct btrfs_shared_data_ref
*ref2
;
1261 leaf
= path
->nodes
[0];
1262 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1264 if (btrfs_extent_inline_ref_type(leaf
, iref
) ==
1265 BTRFS_EXTENT_DATA_REF_KEY
) {
1266 ref1
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1267 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1269 ref2
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1270 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1272 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1273 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
1274 struct btrfs_extent_data_ref
);
1275 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1276 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
1277 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
1278 struct btrfs_shared_data_ref
);
1279 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1280 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1281 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
1282 struct btrfs_extent_ref_v0
*ref0
;
1283 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1284 struct btrfs_extent_ref_v0
);
1285 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
1293 static noinline
int lookup_tree_block_ref(struct btrfs_trans_handle
*trans
,
1294 struct btrfs_root
*root
,
1295 struct btrfs_path
*path
,
1296 u64 bytenr
, u64 parent
,
1299 struct btrfs_key key
;
1302 key
.objectid
= bytenr
;
1304 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1305 key
.offset
= parent
;
1307 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
1308 key
.offset
= root_objectid
;
1311 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1314 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1315 if (ret
== -ENOENT
&& parent
) {
1316 btrfs_release_path(path
);
1317 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
1318 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1326 static noinline
int insert_tree_block_ref(struct btrfs_trans_handle
*trans
,
1327 struct btrfs_root
*root
,
1328 struct btrfs_path
*path
,
1329 u64 bytenr
, u64 parent
,
1332 struct btrfs_key key
;
1335 key
.objectid
= bytenr
;
1337 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1338 key
.offset
= parent
;
1340 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
1341 key
.offset
= root_objectid
;
1344 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, 0);
1345 btrfs_release_path(path
);
1349 static inline int extent_ref_type(u64 parent
, u64 owner
)
1352 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1354 type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1356 type
= BTRFS_TREE_BLOCK_REF_KEY
;
1359 type
= BTRFS_SHARED_DATA_REF_KEY
;
1361 type
= BTRFS_EXTENT_DATA_REF_KEY
;
1366 static int find_next_key(struct btrfs_path
*path
, int level
,
1367 struct btrfs_key
*key
)
1370 for (; level
< BTRFS_MAX_LEVEL
; level
++) {
1371 if (!path
->nodes
[level
])
1373 if (path
->slots
[level
] + 1 >=
1374 btrfs_header_nritems(path
->nodes
[level
]))
1377 btrfs_item_key_to_cpu(path
->nodes
[level
], key
,
1378 path
->slots
[level
] + 1);
1380 btrfs_node_key_to_cpu(path
->nodes
[level
], key
,
1381 path
->slots
[level
] + 1);
1388 * look for inline back ref. if back ref is found, *ref_ret is set
1389 * to the address of inline back ref, and 0 is returned.
1391 * if back ref isn't found, *ref_ret is set to the address where it
1392 * should be inserted, and -ENOENT is returned.
1394 * if insert is true and there are too many inline back refs, the path
1395 * points to the extent item, and -EAGAIN is returned.
1397 * NOTE: inline back refs are ordered in the same way that back ref
1398 * items in the tree are ordered.
1400 static noinline_for_stack
1401 int lookup_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1402 struct btrfs_root
*root
,
1403 struct btrfs_path
*path
,
1404 struct btrfs_extent_inline_ref
**ref_ret
,
1405 u64 bytenr
, u64 num_bytes
,
1406 u64 parent
, u64 root_objectid
,
1407 u64 owner
, u64 offset
, int insert
)
1409 struct btrfs_key key
;
1410 struct extent_buffer
*leaf
;
1411 struct btrfs_extent_item
*ei
;
1412 struct btrfs_extent_inline_ref
*iref
;
1423 key
.objectid
= bytenr
;
1424 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1425 key
.offset
= num_bytes
;
1427 want
= extent_ref_type(parent
, owner
);
1429 extra_size
= btrfs_extent_inline_ref_size(want
);
1430 path
->keep_locks
= 1;
1433 ret
= btrfs_search_slot(trans
, root
, &key
, path
, extra_size
, 1);
1440 leaf
= path
->nodes
[0];
1441 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1442 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1443 if (item_size
< sizeof(*ei
)) {
1448 ret
= convert_extent_item_v0(trans
, root
, path
, owner
,
1454 leaf
= path
->nodes
[0];
1455 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1458 BUG_ON(item_size
< sizeof(*ei
));
1460 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1461 flags
= btrfs_extent_flags(leaf
, ei
);
1463 ptr
= (unsigned long)(ei
+ 1);
1464 end
= (unsigned long)ei
+ item_size
;
1466 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
1467 ptr
+= sizeof(struct btrfs_tree_block_info
);
1470 BUG_ON(!(flags
& BTRFS_EXTENT_FLAG_DATA
));
1479 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1480 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1484 ptr
+= btrfs_extent_inline_ref_size(type
);
1488 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1489 struct btrfs_extent_data_ref
*dref
;
1490 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1491 if (match_extent_data_ref(leaf
, dref
, root_objectid
,
1496 if (hash_extent_data_ref_item(leaf
, dref
) <
1497 hash_extent_data_ref(root_objectid
, owner
, offset
))
1501 ref_offset
= btrfs_extent_inline_ref_offset(leaf
, iref
);
1503 if (parent
== ref_offset
) {
1507 if (ref_offset
< parent
)
1510 if (root_objectid
== ref_offset
) {
1514 if (ref_offset
< root_objectid
)
1518 ptr
+= btrfs_extent_inline_ref_size(type
);
1520 if (err
== -ENOENT
&& insert
) {
1521 if (item_size
+ extra_size
>=
1522 BTRFS_MAX_EXTENT_ITEM_SIZE(root
)) {
1527 * To add new inline back ref, we have to make sure
1528 * there is no corresponding back ref item.
1529 * For simplicity, we just do not add new inline back
1530 * ref if there is any kind of item for this block
1532 if (find_next_key(path
, 0, &key
) == 0 &&
1533 key
.objectid
== bytenr
&&
1534 key
.type
< BTRFS_BLOCK_GROUP_ITEM_KEY
) {
1539 *ref_ret
= (struct btrfs_extent_inline_ref
*)ptr
;
1542 path
->keep_locks
= 0;
1543 btrfs_unlock_up_safe(path
, 1);
1549 * helper to add new inline back ref
1551 static noinline_for_stack
1552 int setup_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1553 struct btrfs_root
*root
,
1554 struct btrfs_path
*path
,
1555 struct btrfs_extent_inline_ref
*iref
,
1556 u64 parent
, u64 root_objectid
,
1557 u64 owner
, u64 offset
, int refs_to_add
,
1558 struct btrfs_delayed_extent_op
*extent_op
)
1560 struct extent_buffer
*leaf
;
1561 struct btrfs_extent_item
*ei
;
1564 unsigned long item_offset
;
1570 leaf
= path
->nodes
[0];
1571 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1572 item_offset
= (unsigned long)iref
- (unsigned long)ei
;
1574 type
= extent_ref_type(parent
, owner
);
1575 size
= btrfs_extent_inline_ref_size(type
);
1577 ret
= btrfs_extend_item(trans
, root
, path
, size
);
1579 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1580 refs
= btrfs_extent_refs(leaf
, ei
);
1581 refs
+= refs_to_add
;
1582 btrfs_set_extent_refs(leaf
, ei
, refs
);
1584 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1586 ptr
= (unsigned long)ei
+ item_offset
;
1587 end
= (unsigned long)ei
+ btrfs_item_size_nr(leaf
, path
->slots
[0]);
1588 if (ptr
< end
- size
)
1589 memmove_extent_buffer(leaf
, ptr
+ size
, ptr
,
1592 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1593 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
1594 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1595 struct btrfs_extent_data_ref
*dref
;
1596 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1597 btrfs_set_extent_data_ref_root(leaf
, dref
, root_objectid
);
1598 btrfs_set_extent_data_ref_objectid(leaf
, dref
, owner
);
1599 btrfs_set_extent_data_ref_offset(leaf
, dref
, offset
);
1600 btrfs_set_extent_data_ref_count(leaf
, dref
, refs_to_add
);
1601 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1602 struct btrfs_shared_data_ref
*sref
;
1603 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1604 btrfs_set_shared_data_ref_count(leaf
, sref
, refs_to_add
);
1605 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1606 } else if (type
== BTRFS_SHARED_BLOCK_REF_KEY
) {
1607 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1609 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
1611 btrfs_mark_buffer_dirty(leaf
);
1615 static int lookup_extent_backref(struct btrfs_trans_handle
*trans
,
1616 struct btrfs_root
*root
,
1617 struct btrfs_path
*path
,
1618 struct btrfs_extent_inline_ref
**ref_ret
,
1619 u64 bytenr
, u64 num_bytes
, u64 parent
,
1620 u64 root_objectid
, u64 owner
, u64 offset
)
1624 ret
= lookup_inline_extent_backref(trans
, root
, path
, ref_ret
,
1625 bytenr
, num_bytes
, parent
,
1626 root_objectid
, owner
, offset
, 0);
1630 btrfs_release_path(path
);
1633 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1634 ret
= lookup_tree_block_ref(trans
, root
, path
, bytenr
, parent
,
1637 ret
= lookup_extent_data_ref(trans
, root
, path
, bytenr
, parent
,
1638 root_objectid
, owner
, offset
);
1644 * helper to update/remove inline back ref
1646 static noinline_for_stack
1647 int update_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1648 struct btrfs_root
*root
,
1649 struct btrfs_path
*path
,
1650 struct btrfs_extent_inline_ref
*iref
,
1652 struct btrfs_delayed_extent_op
*extent_op
)
1654 struct extent_buffer
*leaf
;
1655 struct btrfs_extent_item
*ei
;
1656 struct btrfs_extent_data_ref
*dref
= NULL
;
1657 struct btrfs_shared_data_ref
*sref
= NULL
;
1666 leaf
= path
->nodes
[0];
1667 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1668 refs
= btrfs_extent_refs(leaf
, ei
);
1669 WARN_ON(refs_to_mod
< 0 && refs
+ refs_to_mod
<= 0);
1670 refs
+= refs_to_mod
;
1671 btrfs_set_extent_refs(leaf
, ei
, refs
);
1673 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1675 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1677 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1678 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1679 refs
= btrfs_extent_data_ref_count(leaf
, dref
);
1680 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1681 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1682 refs
= btrfs_shared_data_ref_count(leaf
, sref
);
1685 BUG_ON(refs_to_mod
!= -1);
1688 BUG_ON(refs_to_mod
< 0 && refs
< -refs_to_mod
);
1689 refs
+= refs_to_mod
;
1692 if (type
== BTRFS_EXTENT_DATA_REF_KEY
)
1693 btrfs_set_extent_data_ref_count(leaf
, dref
, refs
);
1695 btrfs_set_shared_data_ref_count(leaf
, sref
, refs
);
1697 size
= btrfs_extent_inline_ref_size(type
);
1698 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1699 ptr
= (unsigned long)iref
;
1700 end
= (unsigned long)ei
+ item_size
;
1701 if (ptr
+ size
< end
)
1702 memmove_extent_buffer(leaf
, ptr
, ptr
+ size
,
1705 ret
= btrfs_truncate_item(trans
, root
, path
, item_size
, 1);
1707 btrfs_mark_buffer_dirty(leaf
);
1711 static noinline_for_stack
1712 int insert_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1713 struct btrfs_root
*root
,
1714 struct btrfs_path
*path
,
1715 u64 bytenr
, u64 num_bytes
, u64 parent
,
1716 u64 root_objectid
, u64 owner
,
1717 u64 offset
, int refs_to_add
,
1718 struct btrfs_delayed_extent_op
*extent_op
)
1720 struct btrfs_extent_inline_ref
*iref
;
1723 ret
= lookup_inline_extent_backref(trans
, root
, path
, &iref
,
1724 bytenr
, num_bytes
, parent
,
1725 root_objectid
, owner
, offset
, 1);
1727 BUG_ON(owner
< BTRFS_FIRST_FREE_OBJECTID
);
1728 ret
= update_inline_extent_backref(trans
, root
, path
, iref
,
1729 refs_to_add
, extent_op
);
1730 } else if (ret
== -ENOENT
) {
1731 ret
= setup_inline_extent_backref(trans
, root
, path
, iref
,
1732 parent
, root_objectid
,
1733 owner
, offset
, refs_to_add
,
1739 static int insert_extent_backref(struct btrfs_trans_handle
*trans
,
1740 struct btrfs_root
*root
,
1741 struct btrfs_path
*path
,
1742 u64 bytenr
, u64 parent
, u64 root_objectid
,
1743 u64 owner
, u64 offset
, int refs_to_add
)
1746 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1747 BUG_ON(refs_to_add
!= 1);
1748 ret
= insert_tree_block_ref(trans
, root
, path
, bytenr
,
1749 parent
, root_objectid
);
1751 ret
= insert_extent_data_ref(trans
, root
, path
, bytenr
,
1752 parent
, root_objectid
,
1753 owner
, offset
, refs_to_add
);
1758 static int remove_extent_backref(struct btrfs_trans_handle
*trans
,
1759 struct btrfs_root
*root
,
1760 struct btrfs_path
*path
,
1761 struct btrfs_extent_inline_ref
*iref
,
1762 int refs_to_drop
, int is_data
)
1766 BUG_ON(!is_data
&& refs_to_drop
!= 1);
1768 ret
= update_inline_extent_backref(trans
, root
, path
, iref
,
1769 -refs_to_drop
, NULL
);
1770 } else if (is_data
) {
1771 ret
= remove_extent_data_ref(trans
, root
, path
, refs_to_drop
);
1773 ret
= btrfs_del_item(trans
, root
, path
);
1778 static int btrfs_issue_discard(struct block_device
*bdev
,
1781 return blkdev_issue_discard(bdev
, start
>> 9, len
>> 9, GFP_NOFS
, 0);
1784 static int btrfs_discard_extent(struct btrfs_root
*root
, u64 bytenr
,
1785 u64 num_bytes
, u64
*actual_bytes
)
1788 u64 discarded_bytes
= 0;
1789 struct btrfs_multi_bio
*multi
= NULL
;
1792 /* Tell the block device(s) that the sectors can be discarded */
1793 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, REQ_DISCARD
,
1794 bytenr
, &num_bytes
, &multi
, 0);
1796 struct btrfs_bio_stripe
*stripe
= multi
->stripes
;
1800 for (i
= 0; i
< multi
->num_stripes
; i
++, stripe
++) {
1801 if (!stripe
->dev
->can_discard
)
1804 ret
= btrfs_issue_discard(stripe
->dev
->bdev
,
1808 discarded_bytes
+= stripe
->length
;
1809 else if (ret
!= -EOPNOTSUPP
)
1813 * Just in case we get back EOPNOTSUPP for some reason,
1814 * just ignore the return value so we don't screw up
1815 * people calling discard_extent.
1823 *actual_bytes
= discarded_bytes
;
1829 int btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
1830 struct btrfs_root
*root
,
1831 u64 bytenr
, u64 num_bytes
, u64 parent
,
1832 u64 root_objectid
, u64 owner
, u64 offset
)
1835 BUG_ON(owner
< BTRFS_FIRST_FREE_OBJECTID
&&
1836 root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
1838 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1839 ret
= btrfs_add_delayed_tree_ref(trans
, bytenr
, num_bytes
,
1840 parent
, root_objectid
, (int)owner
,
1841 BTRFS_ADD_DELAYED_REF
, NULL
);
1843 ret
= btrfs_add_delayed_data_ref(trans
, bytenr
, num_bytes
,
1844 parent
, root_objectid
, owner
, offset
,
1845 BTRFS_ADD_DELAYED_REF
, NULL
);
1850 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
1851 struct btrfs_root
*root
,
1852 u64 bytenr
, u64 num_bytes
,
1853 u64 parent
, u64 root_objectid
,
1854 u64 owner
, u64 offset
, int refs_to_add
,
1855 struct btrfs_delayed_extent_op
*extent_op
)
1857 struct btrfs_path
*path
;
1858 struct extent_buffer
*leaf
;
1859 struct btrfs_extent_item
*item
;
1864 path
= btrfs_alloc_path();
1869 path
->leave_spinning
= 1;
1870 /* this will setup the path even if it fails to insert the back ref */
1871 ret
= insert_inline_extent_backref(trans
, root
->fs_info
->extent_root
,
1872 path
, bytenr
, num_bytes
, parent
,
1873 root_objectid
, owner
, offset
,
1874 refs_to_add
, extent_op
);
1878 if (ret
!= -EAGAIN
) {
1883 leaf
= path
->nodes
[0];
1884 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1885 refs
= btrfs_extent_refs(leaf
, item
);
1886 btrfs_set_extent_refs(leaf
, item
, refs
+ refs_to_add
);
1888 __run_delayed_extent_op(extent_op
, leaf
, item
);
1890 btrfs_mark_buffer_dirty(leaf
);
1891 btrfs_release_path(path
);
1894 path
->leave_spinning
= 1;
1896 /* now insert the actual backref */
1897 ret
= insert_extent_backref(trans
, root
->fs_info
->extent_root
,
1898 path
, bytenr
, parent
, root_objectid
,
1899 owner
, offset
, refs_to_add
);
1902 btrfs_free_path(path
);
1906 static int run_delayed_data_ref(struct btrfs_trans_handle
*trans
,
1907 struct btrfs_root
*root
,
1908 struct btrfs_delayed_ref_node
*node
,
1909 struct btrfs_delayed_extent_op
*extent_op
,
1910 int insert_reserved
)
1913 struct btrfs_delayed_data_ref
*ref
;
1914 struct btrfs_key ins
;
1919 ins
.objectid
= node
->bytenr
;
1920 ins
.offset
= node
->num_bytes
;
1921 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1923 ref
= btrfs_delayed_node_to_data_ref(node
);
1924 if (node
->type
== BTRFS_SHARED_DATA_REF_KEY
)
1925 parent
= ref
->parent
;
1927 ref_root
= ref
->root
;
1929 if (node
->action
== BTRFS_ADD_DELAYED_REF
&& insert_reserved
) {
1931 BUG_ON(extent_op
->update_key
);
1932 flags
|= extent_op
->flags_to_set
;
1934 ret
= alloc_reserved_file_extent(trans
, root
,
1935 parent
, ref_root
, flags
,
1936 ref
->objectid
, ref
->offset
,
1937 &ins
, node
->ref_mod
);
1938 } else if (node
->action
== BTRFS_ADD_DELAYED_REF
) {
1939 ret
= __btrfs_inc_extent_ref(trans
, root
, node
->bytenr
,
1940 node
->num_bytes
, parent
,
1941 ref_root
, ref
->objectid
,
1942 ref
->offset
, node
->ref_mod
,
1944 } else if (node
->action
== BTRFS_DROP_DELAYED_REF
) {
1945 ret
= __btrfs_free_extent(trans
, root
, node
->bytenr
,
1946 node
->num_bytes
, parent
,
1947 ref_root
, ref
->objectid
,
1948 ref
->offset
, node
->ref_mod
,
1956 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op
*extent_op
,
1957 struct extent_buffer
*leaf
,
1958 struct btrfs_extent_item
*ei
)
1960 u64 flags
= btrfs_extent_flags(leaf
, ei
);
1961 if (extent_op
->update_flags
) {
1962 flags
|= extent_op
->flags_to_set
;
1963 btrfs_set_extent_flags(leaf
, ei
, flags
);
1966 if (extent_op
->update_key
) {
1967 struct btrfs_tree_block_info
*bi
;
1968 BUG_ON(!(flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
));
1969 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
1970 btrfs_set_tree_block_key(leaf
, bi
, &extent_op
->key
);
1974 static int run_delayed_extent_op(struct btrfs_trans_handle
*trans
,
1975 struct btrfs_root
*root
,
1976 struct btrfs_delayed_ref_node
*node
,
1977 struct btrfs_delayed_extent_op
*extent_op
)
1979 struct btrfs_key key
;
1980 struct btrfs_path
*path
;
1981 struct btrfs_extent_item
*ei
;
1982 struct extent_buffer
*leaf
;
1987 path
= btrfs_alloc_path();
1991 key
.objectid
= node
->bytenr
;
1992 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1993 key
.offset
= node
->num_bytes
;
1996 path
->leave_spinning
= 1;
1997 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
,
2008 leaf
= path
->nodes
[0];
2009 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2010 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2011 if (item_size
< sizeof(*ei
)) {
2012 ret
= convert_extent_item_v0(trans
, root
->fs_info
->extent_root
,
2018 leaf
= path
->nodes
[0];
2019 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2022 BUG_ON(item_size
< sizeof(*ei
));
2023 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
2024 __run_delayed_extent_op(extent_op
, leaf
, ei
);
2026 btrfs_mark_buffer_dirty(leaf
);
2028 btrfs_free_path(path
);
2032 static int run_delayed_tree_ref(struct btrfs_trans_handle
*trans
,
2033 struct btrfs_root
*root
,
2034 struct btrfs_delayed_ref_node
*node
,
2035 struct btrfs_delayed_extent_op
*extent_op
,
2036 int insert_reserved
)
2039 struct btrfs_delayed_tree_ref
*ref
;
2040 struct btrfs_key ins
;
2044 ins
.objectid
= node
->bytenr
;
2045 ins
.offset
= node
->num_bytes
;
2046 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
2048 ref
= btrfs_delayed_node_to_tree_ref(node
);
2049 if (node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
)
2050 parent
= ref
->parent
;
2052 ref_root
= ref
->root
;
2054 BUG_ON(node
->ref_mod
!= 1);
2055 if (node
->action
== BTRFS_ADD_DELAYED_REF
&& insert_reserved
) {
2056 BUG_ON(!extent_op
|| !extent_op
->update_flags
||
2057 !extent_op
->update_key
);
2058 ret
= alloc_reserved_tree_block(trans
, root
,
2060 extent_op
->flags_to_set
,
2063 } else if (node
->action
== BTRFS_ADD_DELAYED_REF
) {
2064 ret
= __btrfs_inc_extent_ref(trans
, root
, node
->bytenr
,
2065 node
->num_bytes
, parent
, ref_root
,
2066 ref
->level
, 0, 1, extent_op
);
2067 } else if (node
->action
== BTRFS_DROP_DELAYED_REF
) {
2068 ret
= __btrfs_free_extent(trans
, root
, node
->bytenr
,
2069 node
->num_bytes
, parent
, ref_root
,
2070 ref
->level
, 0, 1, extent_op
);
2077 /* helper function to actually process a single delayed ref entry */
2078 static int run_one_delayed_ref(struct btrfs_trans_handle
*trans
,
2079 struct btrfs_root
*root
,
2080 struct btrfs_delayed_ref_node
*node
,
2081 struct btrfs_delayed_extent_op
*extent_op
,
2082 int insert_reserved
)
2085 if (btrfs_delayed_ref_is_head(node
)) {
2086 struct btrfs_delayed_ref_head
*head
;
2088 * we've hit the end of the chain and we were supposed
2089 * to insert this extent into the tree. But, it got
2090 * deleted before we ever needed to insert it, so all
2091 * we have to do is clean up the accounting
2094 head
= btrfs_delayed_node_to_head(node
);
2095 if (insert_reserved
) {
2096 btrfs_pin_extent(root
, node
->bytenr
,
2097 node
->num_bytes
, 1);
2098 if (head
->is_data
) {
2099 ret
= btrfs_del_csums(trans
, root
,
2105 mutex_unlock(&head
->mutex
);
2109 if (node
->type
== BTRFS_TREE_BLOCK_REF_KEY
||
2110 node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
)
2111 ret
= run_delayed_tree_ref(trans
, root
, node
, extent_op
,
2113 else if (node
->type
== BTRFS_EXTENT_DATA_REF_KEY
||
2114 node
->type
== BTRFS_SHARED_DATA_REF_KEY
)
2115 ret
= run_delayed_data_ref(trans
, root
, node
, extent_op
,
2122 static noinline
struct btrfs_delayed_ref_node
*
2123 select_delayed_ref(struct btrfs_delayed_ref_head
*head
)
2125 struct rb_node
*node
;
2126 struct btrfs_delayed_ref_node
*ref
;
2127 int action
= BTRFS_ADD_DELAYED_REF
;
2130 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2131 * this prevents ref count from going down to zero when
2132 * there still are pending delayed ref.
2134 node
= rb_prev(&head
->node
.rb_node
);
2138 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
,
2140 if (ref
->bytenr
!= head
->node
.bytenr
)
2142 if (ref
->action
== action
)
2144 node
= rb_prev(node
);
2146 if (action
== BTRFS_ADD_DELAYED_REF
) {
2147 action
= BTRFS_DROP_DELAYED_REF
;
2153 static noinline
int run_clustered_refs(struct btrfs_trans_handle
*trans
,
2154 struct btrfs_root
*root
,
2155 struct list_head
*cluster
)
2157 struct btrfs_delayed_ref_root
*delayed_refs
;
2158 struct btrfs_delayed_ref_node
*ref
;
2159 struct btrfs_delayed_ref_head
*locked_ref
= NULL
;
2160 struct btrfs_delayed_extent_op
*extent_op
;
2163 int must_insert_reserved
= 0;
2165 delayed_refs
= &trans
->transaction
->delayed_refs
;
2168 /* pick a new head ref from the cluster list */
2169 if (list_empty(cluster
))
2172 locked_ref
= list_entry(cluster
->next
,
2173 struct btrfs_delayed_ref_head
, cluster
);
2175 /* grab the lock that says we are going to process
2176 * all the refs for this head */
2177 ret
= btrfs_delayed_ref_lock(trans
, locked_ref
);
2180 * we may have dropped the spin lock to get the head
2181 * mutex lock, and that might have given someone else
2182 * time to free the head. If that's true, it has been
2183 * removed from our list and we can move on.
2185 if (ret
== -EAGAIN
) {
2193 * record the must insert reserved flag before we
2194 * drop the spin lock.
2196 must_insert_reserved
= locked_ref
->must_insert_reserved
;
2197 locked_ref
->must_insert_reserved
= 0;
2199 extent_op
= locked_ref
->extent_op
;
2200 locked_ref
->extent_op
= NULL
;
2203 * locked_ref is the head node, so we have to go one
2204 * node back for any delayed ref updates
2206 ref
= select_delayed_ref(locked_ref
);
2208 /* All delayed refs have been processed, Go ahead
2209 * and send the head node to run_one_delayed_ref,
2210 * so that any accounting fixes can happen
2212 ref
= &locked_ref
->node
;
2214 if (extent_op
&& must_insert_reserved
) {
2220 spin_unlock(&delayed_refs
->lock
);
2222 ret
= run_delayed_extent_op(trans
, root
,
2228 spin_lock(&delayed_refs
->lock
);
2232 list_del_init(&locked_ref
->cluster
);
2237 rb_erase(&ref
->rb_node
, &delayed_refs
->root
);
2238 delayed_refs
->num_entries
--;
2240 spin_unlock(&delayed_refs
->lock
);
2242 ret
= run_one_delayed_ref(trans
, root
, ref
, extent_op
,
2243 must_insert_reserved
);
2246 btrfs_put_delayed_ref(ref
);
2251 spin_lock(&delayed_refs
->lock
);
2257 * this starts processing the delayed reference count updates and
2258 * extent insertions we have queued up so far. count can be
2259 * 0, which means to process everything in the tree at the start
2260 * of the run (but not newly added entries), or it can be some target
2261 * number you'd like to process.
2263 int btrfs_run_delayed_refs(struct btrfs_trans_handle
*trans
,
2264 struct btrfs_root
*root
, unsigned long count
)
2266 struct rb_node
*node
;
2267 struct btrfs_delayed_ref_root
*delayed_refs
;
2268 struct btrfs_delayed_ref_node
*ref
;
2269 struct list_head cluster
;
2271 int run_all
= count
== (unsigned long)-1;
2274 if (root
== root
->fs_info
->extent_root
)
2275 root
= root
->fs_info
->tree_root
;
2277 delayed_refs
= &trans
->transaction
->delayed_refs
;
2278 INIT_LIST_HEAD(&cluster
);
2280 spin_lock(&delayed_refs
->lock
);
2282 count
= delayed_refs
->num_entries
* 2;
2286 if (!(run_all
|| run_most
) &&
2287 delayed_refs
->num_heads_ready
< 64)
2291 * go find something we can process in the rbtree. We start at
2292 * the beginning of the tree, and then build a cluster
2293 * of refs to process starting at the first one we are able to
2296 ret
= btrfs_find_ref_cluster(trans
, &cluster
,
2297 delayed_refs
->run_delayed_start
);
2301 ret
= run_clustered_refs(trans
, root
, &cluster
);
2304 count
-= min_t(unsigned long, ret
, count
);
2311 node
= rb_first(&delayed_refs
->root
);
2314 count
= (unsigned long)-1;
2317 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
,
2319 if (btrfs_delayed_ref_is_head(ref
)) {
2320 struct btrfs_delayed_ref_head
*head
;
2322 head
= btrfs_delayed_node_to_head(ref
);
2323 atomic_inc(&ref
->refs
);
2325 spin_unlock(&delayed_refs
->lock
);
2327 * Mutex was contended, block until it's
2328 * released and try again
2330 mutex_lock(&head
->mutex
);
2331 mutex_unlock(&head
->mutex
);
2333 btrfs_put_delayed_ref(ref
);
2337 node
= rb_next(node
);
2339 spin_unlock(&delayed_refs
->lock
);
2340 schedule_timeout(1);
2344 spin_unlock(&delayed_refs
->lock
);
2348 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle
*trans
,
2349 struct btrfs_root
*root
,
2350 u64 bytenr
, u64 num_bytes
, u64 flags
,
2353 struct btrfs_delayed_extent_op
*extent_op
;
2356 extent_op
= kmalloc(sizeof(*extent_op
), GFP_NOFS
);
2360 extent_op
->flags_to_set
= flags
;
2361 extent_op
->update_flags
= 1;
2362 extent_op
->update_key
= 0;
2363 extent_op
->is_data
= is_data
? 1 : 0;
2365 ret
= btrfs_add_delayed_extent_op(trans
, bytenr
, num_bytes
, extent_op
);
2371 static noinline
int check_delayed_ref(struct btrfs_trans_handle
*trans
,
2372 struct btrfs_root
*root
,
2373 struct btrfs_path
*path
,
2374 u64 objectid
, u64 offset
, u64 bytenr
)
2376 struct btrfs_delayed_ref_head
*head
;
2377 struct btrfs_delayed_ref_node
*ref
;
2378 struct btrfs_delayed_data_ref
*data_ref
;
2379 struct btrfs_delayed_ref_root
*delayed_refs
;
2380 struct rb_node
*node
;
2384 delayed_refs
= &trans
->transaction
->delayed_refs
;
2385 spin_lock(&delayed_refs
->lock
);
2386 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
2390 if (!mutex_trylock(&head
->mutex
)) {
2391 atomic_inc(&head
->node
.refs
);
2392 spin_unlock(&delayed_refs
->lock
);
2394 btrfs_release_path(path
);
2397 * Mutex was contended, block until it's released and let
2400 mutex_lock(&head
->mutex
);
2401 mutex_unlock(&head
->mutex
);
2402 btrfs_put_delayed_ref(&head
->node
);
2406 node
= rb_prev(&head
->node
.rb_node
);
2410 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
2412 if (ref
->bytenr
!= bytenr
)
2416 if (ref
->type
!= BTRFS_EXTENT_DATA_REF_KEY
)
2419 data_ref
= btrfs_delayed_node_to_data_ref(ref
);
2421 node
= rb_prev(node
);
2423 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
2424 if (ref
->bytenr
== bytenr
)
2428 if (data_ref
->root
!= root
->root_key
.objectid
||
2429 data_ref
->objectid
!= objectid
|| data_ref
->offset
!= offset
)
2434 mutex_unlock(&head
->mutex
);
2436 spin_unlock(&delayed_refs
->lock
);
2440 static noinline
int check_committed_ref(struct btrfs_trans_handle
*trans
,
2441 struct btrfs_root
*root
,
2442 struct btrfs_path
*path
,
2443 u64 objectid
, u64 offset
, u64 bytenr
)
2445 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
2446 struct extent_buffer
*leaf
;
2447 struct btrfs_extent_data_ref
*ref
;
2448 struct btrfs_extent_inline_ref
*iref
;
2449 struct btrfs_extent_item
*ei
;
2450 struct btrfs_key key
;
2454 key
.objectid
= bytenr
;
2455 key
.offset
= (u64
)-1;
2456 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2458 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
2464 if (path
->slots
[0] == 0)
2468 leaf
= path
->nodes
[0];
2469 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2471 if (key
.objectid
!= bytenr
|| key
.type
!= BTRFS_EXTENT_ITEM_KEY
)
2475 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2476 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2477 if (item_size
< sizeof(*ei
)) {
2478 WARN_ON(item_size
!= sizeof(struct btrfs_extent_item_v0
));
2482 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
2484 if (item_size
!= sizeof(*ei
) +
2485 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY
))
2488 if (btrfs_extent_generation(leaf
, ei
) <=
2489 btrfs_root_last_snapshot(&root
->root_item
))
2492 iref
= (struct btrfs_extent_inline_ref
*)(ei
+ 1);
2493 if (btrfs_extent_inline_ref_type(leaf
, iref
) !=
2494 BTRFS_EXTENT_DATA_REF_KEY
)
2497 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
2498 if (btrfs_extent_refs(leaf
, ei
) !=
2499 btrfs_extent_data_ref_count(leaf
, ref
) ||
2500 btrfs_extent_data_ref_root(leaf
, ref
) !=
2501 root
->root_key
.objectid
||
2502 btrfs_extent_data_ref_objectid(leaf
, ref
) != objectid
||
2503 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
2511 int btrfs_cross_ref_exist(struct btrfs_trans_handle
*trans
,
2512 struct btrfs_root
*root
,
2513 u64 objectid
, u64 offset
, u64 bytenr
)
2515 struct btrfs_path
*path
;
2519 path
= btrfs_alloc_path();
2524 ret
= check_committed_ref(trans
, root
, path
, objectid
,
2526 if (ret
&& ret
!= -ENOENT
)
2529 ret2
= check_delayed_ref(trans
, root
, path
, objectid
,
2531 } while (ret2
== -EAGAIN
);
2533 if (ret2
&& ret2
!= -ENOENT
) {
2538 if (ret
!= -ENOENT
|| ret2
!= -ENOENT
)
2541 btrfs_free_path(path
);
2542 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
2547 static int __btrfs_mod_ref(struct btrfs_trans_handle
*trans
,
2548 struct btrfs_root
*root
,
2549 struct extent_buffer
*buf
,
2550 int full_backref
, int inc
)
2557 struct btrfs_key key
;
2558 struct btrfs_file_extent_item
*fi
;
2562 int (*process_func
)(struct btrfs_trans_handle
*, struct btrfs_root
*,
2563 u64
, u64
, u64
, u64
, u64
, u64
);
2565 ref_root
= btrfs_header_owner(buf
);
2566 nritems
= btrfs_header_nritems(buf
);
2567 level
= btrfs_header_level(buf
);
2569 if (!root
->ref_cows
&& level
== 0)
2573 process_func
= btrfs_inc_extent_ref
;
2575 process_func
= btrfs_free_extent
;
2578 parent
= buf
->start
;
2582 for (i
= 0; i
< nritems
; i
++) {
2584 btrfs_item_key_to_cpu(buf
, &key
, i
);
2585 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
2587 fi
= btrfs_item_ptr(buf
, i
,
2588 struct btrfs_file_extent_item
);
2589 if (btrfs_file_extent_type(buf
, fi
) ==
2590 BTRFS_FILE_EXTENT_INLINE
)
2592 bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
2596 num_bytes
= btrfs_file_extent_disk_num_bytes(buf
, fi
);
2597 key
.offset
-= btrfs_file_extent_offset(buf
, fi
);
2598 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
2599 parent
, ref_root
, key
.objectid
,
2604 bytenr
= btrfs_node_blockptr(buf
, i
);
2605 num_bytes
= btrfs_level_size(root
, level
- 1);
2606 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
2607 parent
, ref_root
, level
- 1, 0);
2618 int btrfs_inc_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2619 struct extent_buffer
*buf
, int full_backref
)
2621 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 1);
2624 int btrfs_dec_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2625 struct extent_buffer
*buf
, int full_backref
)
2627 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 0);
2630 static int write_one_cache_group(struct btrfs_trans_handle
*trans
,
2631 struct btrfs_root
*root
,
2632 struct btrfs_path
*path
,
2633 struct btrfs_block_group_cache
*cache
)
2636 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
2638 struct extent_buffer
*leaf
;
2640 ret
= btrfs_search_slot(trans
, extent_root
, &cache
->key
, path
, 0, 1);
2645 leaf
= path
->nodes
[0];
2646 bi
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
2647 write_extent_buffer(leaf
, &cache
->item
, bi
, sizeof(cache
->item
));
2648 btrfs_mark_buffer_dirty(leaf
);
2649 btrfs_release_path(path
);
2657 static struct btrfs_block_group_cache
*
2658 next_block_group(struct btrfs_root
*root
,
2659 struct btrfs_block_group_cache
*cache
)
2661 struct rb_node
*node
;
2662 spin_lock(&root
->fs_info
->block_group_cache_lock
);
2663 node
= rb_next(&cache
->cache_node
);
2664 btrfs_put_block_group(cache
);
2666 cache
= rb_entry(node
, struct btrfs_block_group_cache
,
2668 btrfs_get_block_group(cache
);
2671 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
2675 static int cache_save_setup(struct btrfs_block_group_cache
*block_group
,
2676 struct btrfs_trans_handle
*trans
,
2677 struct btrfs_path
*path
)
2679 struct btrfs_root
*root
= block_group
->fs_info
->tree_root
;
2680 struct inode
*inode
= NULL
;
2682 int dcs
= BTRFS_DC_ERROR
;
2688 * If this block group is smaller than 100 megs don't bother caching the
2691 if (block_group
->key
.offset
< (100 * 1024 * 1024)) {
2692 spin_lock(&block_group
->lock
);
2693 block_group
->disk_cache_state
= BTRFS_DC_WRITTEN
;
2694 spin_unlock(&block_group
->lock
);
2699 inode
= lookup_free_space_inode(root
, block_group
, path
);
2700 if (IS_ERR(inode
) && PTR_ERR(inode
) != -ENOENT
) {
2701 ret
= PTR_ERR(inode
);
2702 btrfs_release_path(path
);
2706 if (IS_ERR(inode
)) {
2710 if (block_group
->ro
)
2713 ret
= create_free_space_inode(root
, trans
, block_group
, path
);
2720 * We want to set the generation to 0, that way if anything goes wrong
2721 * from here on out we know not to trust this cache when we load up next
2724 BTRFS_I(inode
)->generation
= 0;
2725 ret
= btrfs_update_inode(trans
, root
, inode
);
2728 if (i_size_read(inode
) > 0) {
2729 ret
= btrfs_truncate_free_space_cache(root
, trans
, path
,
2735 spin_lock(&block_group
->lock
);
2736 if (block_group
->cached
!= BTRFS_CACHE_FINISHED
) {
2737 /* We're not cached, don't bother trying to write stuff out */
2738 dcs
= BTRFS_DC_WRITTEN
;
2739 spin_unlock(&block_group
->lock
);
2742 spin_unlock(&block_group
->lock
);
2744 num_pages
= (int)div64_u64(block_group
->key
.offset
, 1024 * 1024 * 1024);
2749 * Just to make absolutely sure we have enough space, we're going to
2750 * preallocate 12 pages worth of space for each block group. In
2751 * practice we ought to use at most 8, but we need extra space so we can
2752 * add our header and have a terminator between the extents and the
2756 num_pages
*= PAGE_CACHE_SIZE
;
2758 ret
= btrfs_check_data_free_space(inode
, num_pages
);
2762 ret
= btrfs_prealloc_file_range_trans(inode
, trans
, 0, 0, num_pages
,
2763 num_pages
, num_pages
,
2766 dcs
= BTRFS_DC_SETUP
;
2767 btrfs_free_reserved_data_space(inode
, num_pages
);
2771 btrfs_release_path(path
);
2773 spin_lock(&block_group
->lock
);
2774 block_group
->disk_cache_state
= dcs
;
2775 spin_unlock(&block_group
->lock
);
2780 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle
*trans
,
2781 struct btrfs_root
*root
)
2783 struct btrfs_block_group_cache
*cache
;
2785 struct btrfs_path
*path
;
2788 path
= btrfs_alloc_path();
2794 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
2796 if (cache
->disk_cache_state
== BTRFS_DC_CLEAR
)
2798 cache
= next_block_group(root
, cache
);
2806 err
= cache_save_setup(cache
, trans
, path
);
2807 last
= cache
->key
.objectid
+ cache
->key
.offset
;
2808 btrfs_put_block_group(cache
);
2813 err
= btrfs_run_delayed_refs(trans
, root
,
2818 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
2820 if (cache
->disk_cache_state
== BTRFS_DC_CLEAR
) {
2821 btrfs_put_block_group(cache
);
2827 cache
= next_block_group(root
, cache
);
2836 if (cache
->disk_cache_state
== BTRFS_DC_SETUP
)
2837 cache
->disk_cache_state
= BTRFS_DC_NEED_WRITE
;
2839 last
= cache
->key
.objectid
+ cache
->key
.offset
;
2841 err
= write_one_cache_group(trans
, root
, path
, cache
);
2843 btrfs_put_block_group(cache
);
2848 * I don't think this is needed since we're just marking our
2849 * preallocated extent as written, but just in case it can't
2853 err
= btrfs_run_delayed_refs(trans
, root
,
2858 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
2861 * Really this shouldn't happen, but it could if we
2862 * couldn't write the entire preallocated extent and
2863 * splitting the extent resulted in a new block.
2866 btrfs_put_block_group(cache
);
2869 if (cache
->disk_cache_state
== BTRFS_DC_NEED_WRITE
)
2871 cache
= next_block_group(root
, cache
);
2880 btrfs_write_out_cache(root
, trans
, cache
, path
);
2883 * If we didn't have an error then the cache state is still
2884 * NEED_WRITE, so we can set it to WRITTEN.
2886 if (cache
->disk_cache_state
== BTRFS_DC_NEED_WRITE
)
2887 cache
->disk_cache_state
= BTRFS_DC_WRITTEN
;
2888 last
= cache
->key
.objectid
+ cache
->key
.offset
;
2889 btrfs_put_block_group(cache
);
2892 btrfs_free_path(path
);
2896 int btrfs_extent_readonly(struct btrfs_root
*root
, u64 bytenr
)
2898 struct btrfs_block_group_cache
*block_group
;
2901 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
2902 if (!block_group
|| block_group
->ro
)
2905 btrfs_put_block_group(block_group
);
2909 static int update_space_info(struct btrfs_fs_info
*info
, u64 flags
,
2910 u64 total_bytes
, u64 bytes_used
,
2911 struct btrfs_space_info
**space_info
)
2913 struct btrfs_space_info
*found
;
2917 if (flags
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
2918 BTRFS_BLOCK_GROUP_RAID10
))
2923 found
= __find_space_info(info
, flags
);
2925 spin_lock(&found
->lock
);
2926 found
->total_bytes
+= total_bytes
;
2927 found
->disk_total
+= total_bytes
* factor
;
2928 found
->bytes_used
+= bytes_used
;
2929 found
->disk_used
+= bytes_used
* factor
;
2931 spin_unlock(&found
->lock
);
2932 *space_info
= found
;
2935 found
= kzalloc(sizeof(*found
), GFP_NOFS
);
2939 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++)
2940 INIT_LIST_HEAD(&found
->block_groups
[i
]);
2941 init_rwsem(&found
->groups_sem
);
2942 spin_lock_init(&found
->lock
);
2943 found
->flags
= flags
& (BTRFS_BLOCK_GROUP_DATA
|
2944 BTRFS_BLOCK_GROUP_SYSTEM
|
2945 BTRFS_BLOCK_GROUP_METADATA
);
2946 found
->total_bytes
= total_bytes
;
2947 found
->disk_total
= total_bytes
* factor
;
2948 found
->bytes_used
= bytes_used
;
2949 found
->disk_used
= bytes_used
* factor
;
2950 found
->bytes_pinned
= 0;
2951 found
->bytes_reserved
= 0;
2952 found
->bytes_readonly
= 0;
2953 found
->bytes_may_use
= 0;
2955 found
->force_alloc
= CHUNK_ALLOC_NO_FORCE
;
2956 found
->chunk_alloc
= 0;
2958 init_waitqueue_head(&found
->wait
);
2959 *space_info
= found
;
2960 list_add_rcu(&found
->list
, &info
->space_info
);
2964 static void set_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
2966 u64 extra_flags
= flags
& (BTRFS_BLOCK_GROUP_RAID0
|
2967 BTRFS_BLOCK_GROUP_RAID1
|
2968 BTRFS_BLOCK_GROUP_RAID10
|
2969 BTRFS_BLOCK_GROUP_DUP
);
2971 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
2972 fs_info
->avail_data_alloc_bits
|= extra_flags
;
2973 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
2974 fs_info
->avail_metadata_alloc_bits
|= extra_flags
;
2975 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
2976 fs_info
->avail_system_alloc_bits
|= extra_flags
;
2980 u64
btrfs_reduce_alloc_profile(struct btrfs_root
*root
, u64 flags
)
2983 * we add in the count of missing devices because we want
2984 * to make sure that any RAID levels on a degraded FS
2985 * continue to be honored.
2987 u64 num_devices
= root
->fs_info
->fs_devices
->rw_devices
+
2988 root
->fs_info
->fs_devices
->missing_devices
;
2990 if (num_devices
== 1)
2991 flags
&= ~(BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID0
);
2992 if (num_devices
< 4)
2993 flags
&= ~BTRFS_BLOCK_GROUP_RAID10
;
2995 if ((flags
& BTRFS_BLOCK_GROUP_DUP
) &&
2996 (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
2997 BTRFS_BLOCK_GROUP_RAID10
))) {
2998 flags
&= ~BTRFS_BLOCK_GROUP_DUP
;
3001 if ((flags
& BTRFS_BLOCK_GROUP_RAID1
) &&
3002 (flags
& BTRFS_BLOCK_GROUP_RAID10
)) {
3003 flags
&= ~BTRFS_BLOCK_GROUP_RAID1
;
3006 if ((flags
& BTRFS_BLOCK_GROUP_RAID0
) &&
3007 ((flags
& BTRFS_BLOCK_GROUP_RAID1
) |
3008 (flags
& BTRFS_BLOCK_GROUP_RAID10
) |
3009 (flags
& BTRFS_BLOCK_GROUP_DUP
)))
3010 flags
&= ~BTRFS_BLOCK_GROUP_RAID0
;
3014 static u64
get_alloc_profile(struct btrfs_root
*root
, u64 flags
)
3016 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3017 flags
|= root
->fs_info
->avail_data_alloc_bits
&
3018 root
->fs_info
->data_alloc_profile
;
3019 else if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
3020 flags
|= root
->fs_info
->avail_system_alloc_bits
&
3021 root
->fs_info
->system_alloc_profile
;
3022 else if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
3023 flags
|= root
->fs_info
->avail_metadata_alloc_bits
&
3024 root
->fs_info
->metadata_alloc_profile
;
3025 return btrfs_reduce_alloc_profile(root
, flags
);
3028 u64
btrfs_get_alloc_profile(struct btrfs_root
*root
, int data
)
3033 flags
= BTRFS_BLOCK_GROUP_DATA
;
3034 else if (root
== root
->fs_info
->chunk_root
)
3035 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
3037 flags
= BTRFS_BLOCK_GROUP_METADATA
;
3039 return get_alloc_profile(root
, flags
);
3042 void btrfs_set_inode_space_info(struct btrfs_root
*root
, struct inode
*inode
)
3044 BTRFS_I(inode
)->space_info
= __find_space_info(root
->fs_info
,
3045 BTRFS_BLOCK_GROUP_DATA
);
3049 * This will check the space that the inode allocates from to make sure we have
3050 * enough space for bytes.
3052 int btrfs_check_data_free_space(struct inode
*inode
, u64 bytes
)
3054 struct btrfs_space_info
*data_sinfo
;
3055 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3057 int ret
= 0, committed
= 0, alloc_chunk
= 1;
3059 /* make sure bytes are sectorsize aligned */
3060 bytes
= (bytes
+ root
->sectorsize
- 1) & ~((u64
)root
->sectorsize
- 1);
3062 if (root
== root
->fs_info
->tree_root
||
3063 BTRFS_I(inode
)->location
.objectid
== BTRFS_FREE_INO_OBJECTID
) {
3068 data_sinfo
= BTRFS_I(inode
)->space_info
;
3073 /* make sure we have enough space to handle the data first */
3074 spin_lock(&data_sinfo
->lock
);
3075 used
= data_sinfo
->bytes_used
+ data_sinfo
->bytes_reserved
+
3076 data_sinfo
->bytes_pinned
+ data_sinfo
->bytes_readonly
+
3077 data_sinfo
->bytes_may_use
;
3079 if (used
+ bytes
> data_sinfo
->total_bytes
) {
3080 struct btrfs_trans_handle
*trans
;
3083 * if we don't have enough free bytes in this space then we need
3084 * to alloc a new chunk.
3086 if (!data_sinfo
->full
&& alloc_chunk
) {
3089 data_sinfo
->force_alloc
= CHUNK_ALLOC_FORCE
;
3090 spin_unlock(&data_sinfo
->lock
);
3092 alloc_target
= btrfs_get_alloc_profile(root
, 1);
3093 trans
= btrfs_join_transaction(root
);
3095 return PTR_ERR(trans
);
3097 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
3098 bytes
+ 2 * 1024 * 1024,
3100 CHUNK_ALLOC_NO_FORCE
);
3101 btrfs_end_transaction(trans
, root
);
3110 btrfs_set_inode_space_info(root
, inode
);
3111 data_sinfo
= BTRFS_I(inode
)->space_info
;
3117 * If we have less pinned bytes than we want to allocate then
3118 * don't bother committing the transaction, it won't help us.
3120 if (data_sinfo
->bytes_pinned
< bytes
)
3122 spin_unlock(&data_sinfo
->lock
);
3124 /* commit the current transaction and try again */
3127 !atomic_read(&root
->fs_info
->open_ioctl_trans
)) {
3129 trans
= btrfs_join_transaction(root
);
3131 return PTR_ERR(trans
);
3132 ret
= btrfs_commit_transaction(trans
, root
);
3140 data_sinfo
->bytes_may_use
+= bytes
;
3141 spin_unlock(&data_sinfo
->lock
);
3147 * Called if we need to clear a data reservation for this inode.
3149 void btrfs_free_reserved_data_space(struct inode
*inode
, u64 bytes
)
3151 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3152 struct btrfs_space_info
*data_sinfo
;
3154 /* make sure bytes are sectorsize aligned */
3155 bytes
= (bytes
+ root
->sectorsize
- 1) & ~((u64
)root
->sectorsize
- 1);
3157 data_sinfo
= BTRFS_I(inode
)->space_info
;
3158 spin_lock(&data_sinfo
->lock
);
3159 data_sinfo
->bytes_may_use
-= bytes
;
3160 spin_unlock(&data_sinfo
->lock
);
3163 static void force_metadata_allocation(struct btrfs_fs_info
*info
)
3165 struct list_head
*head
= &info
->space_info
;
3166 struct btrfs_space_info
*found
;
3169 list_for_each_entry_rcu(found
, head
, list
) {
3170 if (found
->flags
& BTRFS_BLOCK_GROUP_METADATA
)
3171 found
->force_alloc
= CHUNK_ALLOC_FORCE
;
3176 static int should_alloc_chunk(struct btrfs_root
*root
,
3177 struct btrfs_space_info
*sinfo
, u64 alloc_bytes
,
3180 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
3181 u64 num_bytes
= sinfo
->total_bytes
- sinfo
->bytes_readonly
;
3182 u64 num_allocated
= sinfo
->bytes_used
+ sinfo
->bytes_reserved
;
3185 if (force
== CHUNK_ALLOC_FORCE
)
3189 * We need to take into account the global rsv because for all intents
3190 * and purposes it's used space. Don't worry about locking the
3191 * global_rsv, it doesn't change except when the transaction commits.
3193 num_allocated
+= global_rsv
->size
;
3196 * in limited mode, we want to have some free space up to
3197 * about 1% of the FS size.
3199 if (force
== CHUNK_ALLOC_LIMITED
) {
3200 thresh
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
3201 thresh
= max_t(u64
, 64 * 1024 * 1024,
3202 div_factor_fine(thresh
, 1));
3204 if (num_bytes
- num_allocated
< thresh
)
3209 * we have two similar checks here, one based on percentage
3210 * and once based on a hard number of 256MB. The idea
3211 * is that if we have a good amount of free
3212 * room, don't allocate a chunk. A good mount is
3213 * less than 80% utilized of the chunks we have allocated,
3214 * or more than 256MB free
3216 if (num_allocated
+ alloc_bytes
+ 256 * 1024 * 1024 < num_bytes
)
3219 if (num_allocated
+ alloc_bytes
< div_factor(num_bytes
, 8))
3222 thresh
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
3224 /* 256MB or 5% of the FS */
3225 thresh
= max_t(u64
, 256 * 1024 * 1024, div_factor_fine(thresh
, 5));
3227 if (num_bytes
> thresh
&& sinfo
->bytes_used
< div_factor(num_bytes
, 3))
3232 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
3233 struct btrfs_root
*extent_root
, u64 alloc_bytes
,
3234 u64 flags
, int force
)
3236 struct btrfs_space_info
*space_info
;
3237 struct btrfs_fs_info
*fs_info
= extent_root
->fs_info
;
3238 int wait_for_alloc
= 0;
3241 flags
= btrfs_reduce_alloc_profile(extent_root
, flags
);
3243 space_info
= __find_space_info(extent_root
->fs_info
, flags
);
3245 ret
= update_space_info(extent_root
->fs_info
, flags
,
3249 BUG_ON(!space_info
);
3252 spin_lock(&space_info
->lock
);
3253 if (space_info
->force_alloc
)
3254 force
= space_info
->force_alloc
;
3255 if (space_info
->full
) {
3256 spin_unlock(&space_info
->lock
);
3260 if (!should_alloc_chunk(extent_root
, space_info
, alloc_bytes
, force
)) {
3261 spin_unlock(&space_info
->lock
);
3263 } else if (space_info
->chunk_alloc
) {
3266 space_info
->chunk_alloc
= 1;
3269 spin_unlock(&space_info
->lock
);
3271 mutex_lock(&fs_info
->chunk_mutex
);
3274 * The chunk_mutex is held throughout the entirety of a chunk
3275 * allocation, so once we've acquired the chunk_mutex we know that the
3276 * other guy is done and we need to recheck and see if we should
3279 if (wait_for_alloc
) {
3280 mutex_unlock(&fs_info
->chunk_mutex
);
3286 * If we have mixed data/metadata chunks we want to make sure we keep
3287 * allocating mixed chunks instead of individual chunks.
3289 if (btrfs_mixed_space_info(space_info
))
3290 flags
|= (BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
);
3293 * if we're doing a data chunk, go ahead and make sure that
3294 * we keep a reasonable number of metadata chunks allocated in the
3297 if (flags
& BTRFS_BLOCK_GROUP_DATA
&& fs_info
->metadata_ratio
) {
3298 fs_info
->data_chunk_allocations
++;
3299 if (!(fs_info
->data_chunk_allocations
%
3300 fs_info
->metadata_ratio
))
3301 force_metadata_allocation(fs_info
);
3304 ret
= btrfs_alloc_chunk(trans
, extent_root
, flags
);
3305 if (ret
< 0 && ret
!= -ENOSPC
)
3308 spin_lock(&space_info
->lock
);
3310 space_info
->full
= 1;
3314 space_info
->force_alloc
= CHUNK_ALLOC_NO_FORCE
;
3315 space_info
->chunk_alloc
= 0;
3316 spin_unlock(&space_info
->lock
);
3318 mutex_unlock(&extent_root
->fs_info
->chunk_mutex
);
3323 * shrink metadata reservation for delalloc
3325 static int shrink_delalloc(struct btrfs_trans_handle
*trans
,
3326 struct btrfs_root
*root
, u64 to_reclaim
, int sync
)
3328 struct btrfs_block_rsv
*block_rsv
;
3329 struct btrfs_space_info
*space_info
;
3334 int nr_pages
= (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT
;
3336 unsigned long progress
;
3338 block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
3339 space_info
= block_rsv
->space_info
;
3342 reserved
= space_info
->bytes_may_use
;
3343 progress
= space_info
->reservation_progress
;
3349 if (root
->fs_info
->delalloc_bytes
== 0) {
3352 btrfs_wait_ordered_extents(root
, 0, 0);
3356 max_reclaim
= min(reserved
, to_reclaim
);
3358 while (loops
< 1024) {
3359 /* have the flusher threads jump in and do some IO */
3361 nr_pages
= min_t(unsigned long, nr_pages
,
3362 root
->fs_info
->delalloc_bytes
>> PAGE_CACHE_SHIFT
);
3363 writeback_inodes_sb_nr_if_idle(root
->fs_info
->sb
, nr_pages
);
3365 spin_lock(&space_info
->lock
);
3366 if (reserved
> space_info
->bytes_may_use
)
3367 reclaimed
+= reserved
- space_info
->bytes_may_use
;
3368 reserved
= space_info
->bytes_may_use
;
3369 spin_unlock(&space_info
->lock
);
3373 if (reserved
== 0 || reclaimed
>= max_reclaim
)
3376 if (trans
&& trans
->transaction
->blocked
)
3379 time_left
= schedule_timeout_interruptible(1);
3381 /* We were interrupted, exit */
3385 /* we've kicked the IO a few times, if anything has been freed,
3386 * exit. There is no sense in looping here for a long time
3387 * when we really need to commit the transaction, or there are
3388 * just too many writers without enough free space
3393 if (progress
!= space_info
->reservation_progress
)
3398 if (reclaimed
>= to_reclaim
&& !trans
)
3399 btrfs_wait_ordered_extents(root
, 0, 0);
3400 return reclaimed
>= to_reclaim
;
3404 * Retries tells us how many times we've called reserve_metadata_bytes. The
3405 * idea is if this is the first call (retries == 0) then we will add to our
3406 * reserved count if we can't make the allocation in order to hold our place
3407 * while we go and try and free up space. That way for retries > 1 we don't try
3408 * and add space, we just check to see if the amount of unused space is >= the
3409 * total space, meaning that our reservation is valid.
3411 * However if we don't intend to retry this reservation, pass -1 as retries so
3412 * that it short circuits this logic.
3414 static int reserve_metadata_bytes(struct btrfs_trans_handle
*trans
,
3415 struct btrfs_root
*root
,
3416 struct btrfs_block_rsv
*block_rsv
,
3417 u64 orig_bytes
, int flush
)
3419 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
3421 u64 num_bytes
= orig_bytes
;
3424 bool committed
= false;
3425 bool flushing
= false;
3428 spin_lock(&space_info
->lock
);
3430 * We only want to wait if somebody other than us is flushing and we are
3431 * actually alloed to flush.
3433 while (flush
&& !flushing
&& space_info
->flush
) {
3434 spin_unlock(&space_info
->lock
);
3436 * If we have a trans handle we can't wait because the flusher
3437 * may have to commit the transaction, which would mean we would
3438 * deadlock since we are waiting for the flusher to finish, but
3439 * hold the current transaction open.
3443 ret
= wait_event_interruptible(space_info
->wait
,
3444 !space_info
->flush
);
3445 /* Must have been interrupted, return */
3449 spin_lock(&space_info
->lock
);
3453 unused
= space_info
->bytes_used
+ space_info
->bytes_reserved
+
3454 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
3455 space_info
->bytes_may_use
;
3458 * The idea here is that we've not already over-reserved the block group
3459 * then we can go ahead and save our reservation first and then start
3460 * flushing if we need to. Otherwise if we've already overcommitted
3461 * lets start flushing stuff first and then come back and try to make
3464 if (unused
<= space_info
->total_bytes
) {
3465 unused
= space_info
->total_bytes
- unused
;
3466 if (unused
>= num_bytes
) {
3467 space_info
->bytes_may_use
+= orig_bytes
;
3471 * Ok set num_bytes to orig_bytes since we aren't
3472 * overocmmitted, this way we only try and reclaim what
3475 num_bytes
= orig_bytes
;
3479 * Ok we're over committed, set num_bytes to the overcommitted
3480 * amount plus the amount of bytes that we need for this
3483 num_bytes
= unused
- space_info
->total_bytes
+
3484 (orig_bytes
* (retries
+ 1));
3488 * Couldn't make our reservation, save our place so while we're trying
3489 * to reclaim space we can actually use it instead of somebody else
3490 * stealing it from us.
3494 space_info
->flush
= 1;
3497 spin_unlock(&space_info
->lock
);
3503 * We do synchronous shrinking since we don't actually unreserve
3504 * metadata until after the IO is completed.
3506 ret
= shrink_delalloc(trans
, root
, num_bytes
, 1);
3513 * So if we were overcommitted it's possible that somebody else flushed
3514 * out enough space and we simply didn't have enough space to reclaim,
3515 * so go back around and try again.
3523 * Not enough space to be reclaimed, don't bother committing the
3526 spin_lock(&space_info
->lock
);
3527 if (space_info
->bytes_pinned
< orig_bytes
)
3529 spin_unlock(&space_info
->lock
);
3541 trans
= btrfs_join_transaction(root
);
3544 ret
= btrfs_commit_transaction(trans
, root
);
3553 spin_lock(&space_info
->lock
);
3554 space_info
->flush
= 0;
3555 wake_up_all(&space_info
->wait
);
3556 spin_unlock(&space_info
->lock
);
3561 static struct btrfs_block_rsv
*get_block_rsv(struct btrfs_trans_handle
*trans
,
3562 struct btrfs_root
*root
)
3564 struct btrfs_block_rsv
*block_rsv
;
3566 block_rsv
= trans
->block_rsv
;
3568 block_rsv
= root
->block_rsv
;
3571 block_rsv
= &root
->fs_info
->empty_block_rsv
;
3576 static int block_rsv_use_bytes(struct btrfs_block_rsv
*block_rsv
,
3580 spin_lock(&block_rsv
->lock
);
3581 if (block_rsv
->reserved
>= num_bytes
) {
3582 block_rsv
->reserved
-= num_bytes
;
3583 if (block_rsv
->reserved
< block_rsv
->size
)
3584 block_rsv
->full
= 0;
3587 spin_unlock(&block_rsv
->lock
);
3591 static void block_rsv_add_bytes(struct btrfs_block_rsv
*block_rsv
,
3592 u64 num_bytes
, int update_size
)
3594 spin_lock(&block_rsv
->lock
);
3595 block_rsv
->reserved
+= num_bytes
;
3597 block_rsv
->size
+= num_bytes
;
3598 else if (block_rsv
->reserved
>= block_rsv
->size
)
3599 block_rsv
->full
= 1;
3600 spin_unlock(&block_rsv
->lock
);
3603 static void block_rsv_release_bytes(struct btrfs_block_rsv
*block_rsv
,
3604 struct btrfs_block_rsv
*dest
, u64 num_bytes
)
3606 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
3608 spin_lock(&block_rsv
->lock
);
3609 if (num_bytes
== (u64
)-1)
3610 num_bytes
= block_rsv
->size
;
3611 block_rsv
->size
-= num_bytes
;
3612 if (block_rsv
->reserved
>= block_rsv
->size
) {
3613 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
3614 block_rsv
->reserved
= block_rsv
->size
;
3615 block_rsv
->full
= 1;
3619 spin_unlock(&block_rsv
->lock
);
3621 if (num_bytes
> 0) {
3623 spin_lock(&dest
->lock
);
3627 bytes_to_add
= dest
->size
- dest
->reserved
;
3628 bytes_to_add
= min(num_bytes
, bytes_to_add
);
3629 dest
->reserved
+= bytes_to_add
;
3630 if (dest
->reserved
>= dest
->size
)
3632 num_bytes
-= bytes_to_add
;
3634 spin_unlock(&dest
->lock
);
3637 spin_lock(&space_info
->lock
);
3638 space_info
->bytes_may_use
-= num_bytes
;
3639 space_info
->reservation_progress
++;
3640 spin_unlock(&space_info
->lock
);
3645 static int block_rsv_migrate_bytes(struct btrfs_block_rsv
*src
,
3646 struct btrfs_block_rsv
*dst
, u64 num_bytes
)
3650 ret
= block_rsv_use_bytes(src
, num_bytes
);
3654 block_rsv_add_bytes(dst
, num_bytes
, 1);
3658 void btrfs_init_block_rsv(struct btrfs_block_rsv
*rsv
)
3660 memset(rsv
, 0, sizeof(*rsv
));
3661 spin_lock_init(&rsv
->lock
);
3664 struct btrfs_block_rsv
*btrfs_alloc_block_rsv(struct btrfs_root
*root
)
3666 struct btrfs_block_rsv
*block_rsv
;
3667 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3669 block_rsv
= kmalloc(sizeof(*block_rsv
), GFP_NOFS
);
3673 btrfs_init_block_rsv(block_rsv
);
3674 block_rsv
->space_info
= __find_space_info(fs_info
,
3675 BTRFS_BLOCK_GROUP_METADATA
);
3679 void btrfs_free_block_rsv(struct btrfs_root
*root
,
3680 struct btrfs_block_rsv
*rsv
)
3682 btrfs_block_rsv_release(root
, rsv
, (u64
)-1);
3686 int btrfs_block_rsv_add(struct btrfs_trans_handle
*trans
,
3687 struct btrfs_root
*root
,
3688 struct btrfs_block_rsv
*block_rsv
,
3696 ret
= reserve_metadata_bytes(trans
, root
, block_rsv
, num_bytes
, 1);
3698 block_rsv_add_bytes(block_rsv
, num_bytes
, 1);
3705 int btrfs_block_rsv_check(struct btrfs_trans_handle
*trans
,
3706 struct btrfs_root
*root
,
3707 struct btrfs_block_rsv
*block_rsv
,
3708 u64 min_reserved
, int min_factor
, int flush
)
3716 spin_lock(&block_rsv
->lock
);
3718 num_bytes
= div_factor(block_rsv
->size
, min_factor
);
3719 if (min_reserved
> num_bytes
)
3720 num_bytes
= min_reserved
;
3722 if (block_rsv
->reserved
>= num_bytes
)
3725 num_bytes
-= block_rsv
->reserved
;
3726 spin_unlock(&block_rsv
->lock
);
3731 ret
= reserve_metadata_bytes(trans
, root
, block_rsv
, num_bytes
, flush
);
3733 block_rsv_add_bytes(block_rsv
, num_bytes
, 0);
3740 int btrfs_block_rsv_migrate(struct btrfs_block_rsv
*src_rsv
,
3741 struct btrfs_block_rsv
*dst_rsv
,
3744 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
3747 void btrfs_block_rsv_release(struct btrfs_root
*root
,
3748 struct btrfs_block_rsv
*block_rsv
,
3751 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
3752 if (global_rsv
->full
|| global_rsv
== block_rsv
||
3753 block_rsv
->space_info
!= global_rsv
->space_info
)
3755 block_rsv_release_bytes(block_rsv
, global_rsv
, num_bytes
);
3759 * helper to calculate size of global block reservation.
3760 * the desired value is sum of space used by extent tree,
3761 * checksum tree and root tree
3763 static u64
calc_global_metadata_size(struct btrfs_fs_info
*fs_info
)
3765 struct btrfs_space_info
*sinfo
;
3769 int csum_size
= btrfs_super_csum_size(&fs_info
->super_copy
);
3771 sinfo
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_DATA
);
3772 spin_lock(&sinfo
->lock
);
3773 data_used
= sinfo
->bytes_used
;
3774 spin_unlock(&sinfo
->lock
);
3776 sinfo
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
3777 spin_lock(&sinfo
->lock
);
3778 if (sinfo
->flags
& BTRFS_BLOCK_GROUP_DATA
)
3780 meta_used
= sinfo
->bytes_used
;
3781 spin_unlock(&sinfo
->lock
);
3783 num_bytes
= (data_used
>> fs_info
->sb
->s_blocksize_bits
) *
3785 num_bytes
+= div64_u64(data_used
+ meta_used
, 50);
3787 if (num_bytes
* 3 > meta_used
)
3788 num_bytes
= div64_u64(meta_used
, 3);
3790 return ALIGN(num_bytes
, fs_info
->extent_root
->leafsize
<< 10);
3793 static void update_global_block_rsv(struct btrfs_fs_info
*fs_info
)
3795 struct btrfs_block_rsv
*block_rsv
= &fs_info
->global_block_rsv
;
3796 struct btrfs_space_info
*sinfo
= block_rsv
->space_info
;
3799 num_bytes
= calc_global_metadata_size(fs_info
);
3801 spin_lock(&block_rsv
->lock
);
3802 spin_lock(&sinfo
->lock
);
3804 block_rsv
->size
= num_bytes
;
3806 num_bytes
= sinfo
->bytes_used
+ sinfo
->bytes_pinned
+
3807 sinfo
->bytes_reserved
+ sinfo
->bytes_readonly
+
3808 sinfo
->bytes_may_use
;
3810 if (sinfo
->total_bytes
> num_bytes
) {
3811 num_bytes
= sinfo
->total_bytes
- num_bytes
;
3812 block_rsv
->reserved
+= num_bytes
;
3813 sinfo
->bytes_may_use
+= num_bytes
;
3816 if (block_rsv
->reserved
>= block_rsv
->size
) {
3817 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
3818 sinfo
->bytes_may_use
-= num_bytes
;
3819 sinfo
->reservation_progress
++;
3820 block_rsv
->reserved
= block_rsv
->size
;
3821 block_rsv
->full
= 1;
3824 spin_unlock(&sinfo
->lock
);
3825 spin_unlock(&block_rsv
->lock
);
3828 static void init_global_block_rsv(struct btrfs_fs_info
*fs_info
)
3830 struct btrfs_space_info
*space_info
;
3832 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_SYSTEM
);
3833 fs_info
->chunk_block_rsv
.space_info
= space_info
;
3835 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
3836 fs_info
->global_block_rsv
.space_info
= space_info
;
3837 fs_info
->delalloc_block_rsv
.space_info
= space_info
;
3838 fs_info
->trans_block_rsv
.space_info
= space_info
;
3839 fs_info
->empty_block_rsv
.space_info
= space_info
;
3841 fs_info
->extent_root
->block_rsv
= &fs_info
->global_block_rsv
;
3842 fs_info
->csum_root
->block_rsv
= &fs_info
->global_block_rsv
;
3843 fs_info
->dev_root
->block_rsv
= &fs_info
->global_block_rsv
;
3844 fs_info
->tree_root
->block_rsv
= &fs_info
->global_block_rsv
;
3845 fs_info
->chunk_root
->block_rsv
= &fs_info
->chunk_block_rsv
;
3847 update_global_block_rsv(fs_info
);
3850 static void release_global_block_rsv(struct btrfs_fs_info
*fs_info
)
3852 block_rsv_release_bytes(&fs_info
->global_block_rsv
, NULL
, (u64
)-1);
3853 WARN_ON(fs_info
->delalloc_block_rsv
.size
> 0);
3854 WARN_ON(fs_info
->delalloc_block_rsv
.reserved
> 0);
3855 WARN_ON(fs_info
->trans_block_rsv
.size
> 0);
3856 WARN_ON(fs_info
->trans_block_rsv
.reserved
> 0);
3857 WARN_ON(fs_info
->chunk_block_rsv
.size
> 0);
3858 WARN_ON(fs_info
->chunk_block_rsv
.reserved
> 0);
3861 void btrfs_trans_release_metadata(struct btrfs_trans_handle
*trans
,
3862 struct btrfs_root
*root
)
3864 if (!trans
->bytes_reserved
)
3867 BUG_ON(trans
->block_rsv
!= &root
->fs_info
->trans_block_rsv
);
3868 btrfs_block_rsv_release(root
, trans
->block_rsv
,
3869 trans
->bytes_reserved
);
3870 trans
->bytes_reserved
= 0;
3873 int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle
*trans
,
3874 struct inode
*inode
)
3876 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3877 struct btrfs_block_rsv
*src_rsv
= get_block_rsv(trans
, root
);
3878 struct btrfs_block_rsv
*dst_rsv
= root
->orphan_block_rsv
;
3881 * We need to hold space in order to delete our orphan item once we've
3882 * added it, so this takes the reservation so we can release it later
3883 * when we are truly done with the orphan item.
3885 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 1);
3886 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
3889 void btrfs_orphan_release_metadata(struct inode
*inode
)
3891 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3892 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 1);
3893 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
, num_bytes
);
3896 int btrfs_snap_reserve_metadata(struct btrfs_trans_handle
*trans
,
3897 struct btrfs_pending_snapshot
*pending
)
3899 struct btrfs_root
*root
= pending
->root
;
3900 struct btrfs_block_rsv
*src_rsv
= get_block_rsv(trans
, root
);
3901 struct btrfs_block_rsv
*dst_rsv
= &pending
->block_rsv
;
3903 * two for root back/forward refs, two for directory entries
3904 * and one for root of the snapshot.
3906 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 5);
3907 dst_rsv
->space_info
= src_rsv
->space_info
;
3908 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
3912 * drop_outstanding_extent - drop an outstanding extent
3913 * @inode: the inode we're dropping the extent for
3915 * This is called when we are freeing up an outstanding extent, either called
3916 * after an error or after an extent is written. This will return the number of
3917 * reserved extents that need to be freed. This must be called with
3918 * BTRFS_I(inode)->lock held.
3920 static unsigned drop_outstanding_extent(struct inode
*inode
)
3922 unsigned dropped_extents
= 0;
3924 BUG_ON(!BTRFS_I(inode
)->outstanding_extents
);
3925 BTRFS_I(inode
)->outstanding_extents
--;
3928 * If we have more or the same amount of outsanding extents than we have
3929 * reserved then we need to leave the reserved extents count alone.
3931 if (BTRFS_I(inode
)->outstanding_extents
>=
3932 BTRFS_I(inode
)->reserved_extents
)
3935 dropped_extents
= BTRFS_I(inode
)->reserved_extents
-
3936 BTRFS_I(inode
)->outstanding_extents
;
3937 BTRFS_I(inode
)->reserved_extents
-= dropped_extents
;
3938 return dropped_extents
;
3942 * calc_csum_metadata_size - return the amount of metada space that must be
3943 * reserved/free'd for the given bytes.
3944 * @inode: the inode we're manipulating
3945 * @num_bytes: the number of bytes in question
3946 * @reserve: 1 if we are reserving space, 0 if we are freeing space
3948 * This adjusts the number of csum_bytes in the inode and then returns the
3949 * correct amount of metadata that must either be reserved or freed. We
3950 * calculate how many checksums we can fit into one leaf and then divide the
3951 * number of bytes that will need to be checksumed by this value to figure out
3952 * how many checksums will be required. If we are adding bytes then the number
3953 * may go up and we will return the number of additional bytes that must be
3954 * reserved. If it is going down we will return the number of bytes that must
3957 * This must be called with BTRFS_I(inode)->lock held.
3959 static u64
calc_csum_metadata_size(struct inode
*inode
, u64 num_bytes
,
3962 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3964 int num_csums_per_leaf
;
3968 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
&&
3969 BTRFS_I(inode
)->csum_bytes
== 0)
3972 old_csums
= (int)div64_u64(BTRFS_I(inode
)->csum_bytes
, root
->sectorsize
);
3974 BTRFS_I(inode
)->csum_bytes
+= num_bytes
;
3976 BTRFS_I(inode
)->csum_bytes
-= num_bytes
;
3977 csum_size
= BTRFS_LEAF_DATA_SIZE(root
) - sizeof(struct btrfs_item
);
3978 num_csums_per_leaf
= (int)div64_u64(csum_size
,
3979 sizeof(struct btrfs_csum_item
) +
3980 sizeof(struct btrfs_disk_key
));
3981 num_csums
= (int)div64_u64(BTRFS_I(inode
)->csum_bytes
, root
->sectorsize
);
3982 num_csums
= num_csums
+ num_csums_per_leaf
- 1;
3983 num_csums
= num_csums
/ num_csums_per_leaf
;
3985 old_csums
= old_csums
+ num_csums_per_leaf
- 1;
3986 old_csums
= old_csums
/ num_csums_per_leaf
;
3988 /* No change, no need to reserve more */
3989 if (old_csums
== num_csums
)
3993 return btrfs_calc_trans_metadata_size(root
,
3994 num_csums
- old_csums
);
3996 return btrfs_calc_trans_metadata_size(root
, old_csums
- num_csums
);
3999 int btrfs_delalloc_reserve_metadata(struct inode
*inode
, u64 num_bytes
)
4001 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4002 struct btrfs_block_rsv
*block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
4004 unsigned nr_extents
= 0;
4007 if (btrfs_transaction_in_commit(root
->fs_info
))
4008 schedule_timeout(1);
4010 num_bytes
= ALIGN(num_bytes
, root
->sectorsize
);
4012 spin_lock(&BTRFS_I(inode
)->lock
);
4013 BTRFS_I(inode
)->outstanding_extents
++;
4015 if (BTRFS_I(inode
)->outstanding_extents
>
4016 BTRFS_I(inode
)->reserved_extents
) {
4017 nr_extents
= BTRFS_I(inode
)->outstanding_extents
-
4018 BTRFS_I(inode
)->reserved_extents
;
4019 BTRFS_I(inode
)->reserved_extents
+= nr_extents
;
4021 to_reserve
= btrfs_calc_trans_metadata_size(root
, nr_extents
);
4023 to_reserve
+= calc_csum_metadata_size(inode
, num_bytes
, 1);
4024 spin_unlock(&BTRFS_I(inode
)->lock
);
4026 ret
= reserve_metadata_bytes(NULL
, root
, block_rsv
, to_reserve
, 1);
4031 spin_lock(&BTRFS_I(inode
)->lock
);
4032 dropped
= drop_outstanding_extent(inode
);
4033 to_free
= calc_csum_metadata_size(inode
, num_bytes
, 0);
4034 spin_unlock(&BTRFS_I(inode
)->lock
);
4035 to_free
+= btrfs_calc_trans_metadata_size(root
, dropped
);
4038 * Somebody could have come in and twiddled with the
4039 * reservation, so if we have to free more than we would have
4040 * reserved from this reservation go ahead and release those
4043 to_free
-= to_reserve
;
4045 btrfs_block_rsv_release(root
, block_rsv
, to_free
);
4049 block_rsv_add_bytes(block_rsv
, to_reserve
, 1);
4055 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
4056 * @inode: the inode to release the reservation for
4057 * @num_bytes: the number of bytes we're releasing
4059 * This will release the metadata reservation for an inode. This can be called
4060 * once we complete IO for a given set of bytes to release their metadata
4063 void btrfs_delalloc_release_metadata(struct inode
*inode
, u64 num_bytes
)
4065 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4069 num_bytes
= ALIGN(num_bytes
, root
->sectorsize
);
4070 spin_lock(&BTRFS_I(inode
)->lock
);
4071 dropped
= drop_outstanding_extent(inode
);
4073 to_free
= calc_csum_metadata_size(inode
, num_bytes
, 0);
4074 spin_unlock(&BTRFS_I(inode
)->lock
);
4076 to_free
+= btrfs_calc_trans_metadata_size(root
, dropped
);
4078 btrfs_block_rsv_release(root
, &root
->fs_info
->delalloc_block_rsv
,
4083 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
4084 * @inode: inode we're writing to
4085 * @num_bytes: the number of bytes we want to allocate
4087 * This will do the following things
4089 * o reserve space in the data space info for num_bytes
4090 * o reserve space in the metadata space info based on number of outstanding
4091 * extents and how much csums will be needed
4092 * o add to the inodes ->delalloc_bytes
4093 * o add it to the fs_info's delalloc inodes list.
4095 * This will return 0 for success and -ENOSPC if there is no space left.
4097 int btrfs_delalloc_reserve_space(struct inode
*inode
, u64 num_bytes
)
4101 ret
= btrfs_check_data_free_space(inode
, num_bytes
);
4105 ret
= btrfs_delalloc_reserve_metadata(inode
, num_bytes
);
4107 btrfs_free_reserved_data_space(inode
, num_bytes
);
4115 * btrfs_delalloc_release_space - release data and metadata space for delalloc
4116 * @inode: inode we're releasing space for
4117 * @num_bytes: the number of bytes we want to free up
4119 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
4120 * called in the case that we don't need the metadata AND data reservations
4121 * anymore. So if there is an error or we insert an inline extent.
4123 * This function will release the metadata space that was not used and will
4124 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
4125 * list if there are no delalloc bytes left.
4127 void btrfs_delalloc_release_space(struct inode
*inode
, u64 num_bytes
)
4129 btrfs_delalloc_release_metadata(inode
, num_bytes
);
4130 btrfs_free_reserved_data_space(inode
, num_bytes
);
4133 static int update_block_group(struct btrfs_trans_handle
*trans
,
4134 struct btrfs_root
*root
,
4135 u64 bytenr
, u64 num_bytes
, int alloc
)
4137 struct btrfs_block_group_cache
*cache
= NULL
;
4138 struct btrfs_fs_info
*info
= root
->fs_info
;
4139 u64 total
= num_bytes
;
4144 /* block accounting for super block */
4145 spin_lock(&info
->delalloc_lock
);
4146 old_val
= btrfs_super_bytes_used(&info
->super_copy
);
4148 old_val
+= num_bytes
;
4150 old_val
-= num_bytes
;
4151 btrfs_set_super_bytes_used(&info
->super_copy
, old_val
);
4152 spin_unlock(&info
->delalloc_lock
);
4155 cache
= btrfs_lookup_block_group(info
, bytenr
);
4158 if (cache
->flags
& (BTRFS_BLOCK_GROUP_DUP
|
4159 BTRFS_BLOCK_GROUP_RAID1
|
4160 BTRFS_BLOCK_GROUP_RAID10
))
4165 * If this block group has free space cache written out, we
4166 * need to make sure to load it if we are removing space. This
4167 * is because we need the unpinning stage to actually add the
4168 * space back to the block group, otherwise we will leak space.
4170 if (!alloc
&& cache
->cached
== BTRFS_CACHE_NO
)
4171 cache_block_group(cache
, trans
, NULL
, 1);
4173 byte_in_group
= bytenr
- cache
->key
.objectid
;
4174 WARN_ON(byte_in_group
> cache
->key
.offset
);
4176 spin_lock(&cache
->space_info
->lock
);
4177 spin_lock(&cache
->lock
);
4179 if (btrfs_super_cache_generation(&info
->super_copy
) != 0 &&
4180 cache
->disk_cache_state
< BTRFS_DC_CLEAR
)
4181 cache
->disk_cache_state
= BTRFS_DC_CLEAR
;
4184 old_val
= btrfs_block_group_used(&cache
->item
);
4185 num_bytes
= min(total
, cache
->key
.offset
- byte_in_group
);
4187 old_val
+= num_bytes
;
4188 btrfs_set_block_group_used(&cache
->item
, old_val
);
4189 cache
->reserved
-= num_bytes
;
4190 cache
->space_info
->bytes_reserved
-= num_bytes
;
4191 cache
->space_info
->bytes_used
+= num_bytes
;
4192 cache
->space_info
->disk_used
+= num_bytes
* factor
;
4193 spin_unlock(&cache
->lock
);
4194 spin_unlock(&cache
->space_info
->lock
);
4196 old_val
-= num_bytes
;
4197 btrfs_set_block_group_used(&cache
->item
, old_val
);
4198 cache
->pinned
+= num_bytes
;
4199 cache
->space_info
->bytes_pinned
+= num_bytes
;
4200 cache
->space_info
->bytes_used
-= num_bytes
;
4201 cache
->space_info
->disk_used
-= num_bytes
* factor
;
4202 spin_unlock(&cache
->lock
);
4203 spin_unlock(&cache
->space_info
->lock
);
4205 set_extent_dirty(info
->pinned_extents
,
4206 bytenr
, bytenr
+ num_bytes
- 1,
4207 GFP_NOFS
| __GFP_NOFAIL
);
4209 btrfs_put_block_group(cache
);
4211 bytenr
+= num_bytes
;
4216 static u64
first_logical_byte(struct btrfs_root
*root
, u64 search_start
)
4218 struct btrfs_block_group_cache
*cache
;
4221 cache
= btrfs_lookup_first_block_group(root
->fs_info
, search_start
);
4225 bytenr
= cache
->key
.objectid
;
4226 btrfs_put_block_group(cache
);
4231 static int pin_down_extent(struct btrfs_root
*root
,
4232 struct btrfs_block_group_cache
*cache
,
4233 u64 bytenr
, u64 num_bytes
, int reserved
)
4235 spin_lock(&cache
->space_info
->lock
);
4236 spin_lock(&cache
->lock
);
4237 cache
->pinned
+= num_bytes
;
4238 cache
->space_info
->bytes_pinned
+= num_bytes
;
4240 cache
->reserved
-= num_bytes
;
4241 cache
->space_info
->bytes_reserved
-= num_bytes
;
4243 spin_unlock(&cache
->lock
);
4244 spin_unlock(&cache
->space_info
->lock
);
4246 set_extent_dirty(root
->fs_info
->pinned_extents
, bytenr
,
4247 bytenr
+ num_bytes
- 1, GFP_NOFS
| __GFP_NOFAIL
);
4252 * this function must be called within transaction
4254 int btrfs_pin_extent(struct btrfs_root
*root
,
4255 u64 bytenr
, u64 num_bytes
, int reserved
)
4257 struct btrfs_block_group_cache
*cache
;
4259 cache
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
4262 pin_down_extent(root
, cache
, bytenr
, num_bytes
, reserved
);
4264 btrfs_put_block_group(cache
);
4269 * btrfs_update_reserved_bytes - update the block_group and space info counters
4270 * @cache: The cache we are manipulating
4271 * @num_bytes: The number of bytes in question
4272 * @reserve: One of the reservation enums
4274 * This is called by the allocator when it reserves space, or by somebody who is
4275 * freeing space that was never actually used on disk. For example if you
4276 * reserve some space for a new leaf in transaction A and before transaction A
4277 * commits you free that leaf, you call this with reserve set to 0 in order to
4278 * clear the reservation.
4280 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
4281 * ENOSPC accounting. For data we handle the reservation through clearing the
4282 * delalloc bits in the io_tree. We have to do this since we could end up
4283 * allocating less disk space for the amount of data we have reserved in the
4284 * case of compression.
4286 * If this is a reservation and the block group has become read only we cannot
4287 * make the reservation and return -EAGAIN, otherwise this function always
4290 static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache
*cache
,
4291 u64 num_bytes
, int reserve
)
4293 struct btrfs_space_info
*space_info
= cache
->space_info
;
4295 spin_lock(&space_info
->lock
);
4296 spin_lock(&cache
->lock
);
4297 if (reserve
!= RESERVE_FREE
) {
4301 cache
->reserved
+= num_bytes
;
4302 space_info
->bytes_reserved
+= num_bytes
;
4303 if (reserve
== RESERVE_ALLOC
) {
4304 BUG_ON(space_info
->bytes_may_use
< num_bytes
);
4305 space_info
->bytes_may_use
-= num_bytes
;
4310 space_info
->bytes_readonly
+= num_bytes
;
4311 cache
->reserved
-= num_bytes
;
4312 space_info
->bytes_reserved
-= num_bytes
;
4313 space_info
->reservation_progress
++;
4315 spin_unlock(&cache
->lock
);
4316 spin_unlock(&space_info
->lock
);
4320 int btrfs_prepare_extent_commit(struct btrfs_trans_handle
*trans
,
4321 struct btrfs_root
*root
)
4323 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4324 struct btrfs_caching_control
*next
;
4325 struct btrfs_caching_control
*caching_ctl
;
4326 struct btrfs_block_group_cache
*cache
;
4328 down_write(&fs_info
->extent_commit_sem
);
4330 list_for_each_entry_safe(caching_ctl
, next
,
4331 &fs_info
->caching_block_groups
, list
) {
4332 cache
= caching_ctl
->block_group
;
4333 if (block_group_cache_done(cache
)) {
4334 cache
->last_byte_to_unpin
= (u64
)-1;
4335 list_del_init(&caching_ctl
->list
);
4336 put_caching_control(caching_ctl
);
4338 cache
->last_byte_to_unpin
= caching_ctl
->progress
;
4342 if (fs_info
->pinned_extents
== &fs_info
->freed_extents
[0])
4343 fs_info
->pinned_extents
= &fs_info
->freed_extents
[1];
4345 fs_info
->pinned_extents
= &fs_info
->freed_extents
[0];
4347 up_write(&fs_info
->extent_commit_sem
);
4349 update_global_block_rsv(fs_info
);
4353 static int unpin_extent_range(struct btrfs_root
*root
, u64 start
, u64 end
)
4355 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4356 struct btrfs_block_group_cache
*cache
= NULL
;
4359 while (start
<= end
) {
4361 start
>= cache
->key
.objectid
+ cache
->key
.offset
) {
4363 btrfs_put_block_group(cache
);
4364 cache
= btrfs_lookup_block_group(fs_info
, start
);
4368 len
= cache
->key
.objectid
+ cache
->key
.offset
- start
;
4369 len
= min(len
, end
+ 1 - start
);
4371 if (start
< cache
->last_byte_to_unpin
) {
4372 len
= min(len
, cache
->last_byte_to_unpin
- start
);
4373 btrfs_add_free_space(cache
, start
, len
);
4378 spin_lock(&cache
->space_info
->lock
);
4379 spin_lock(&cache
->lock
);
4380 cache
->pinned
-= len
;
4381 cache
->space_info
->bytes_pinned
-= len
;
4383 cache
->space_info
->bytes_readonly
+= len
;
4384 spin_unlock(&cache
->lock
);
4385 spin_unlock(&cache
->space_info
->lock
);
4389 btrfs_put_block_group(cache
);
4393 int btrfs_finish_extent_commit(struct btrfs_trans_handle
*trans
,
4394 struct btrfs_root
*root
)
4396 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4397 struct extent_io_tree
*unpin
;
4402 if (fs_info
->pinned_extents
== &fs_info
->freed_extents
[0])
4403 unpin
= &fs_info
->freed_extents
[1];
4405 unpin
= &fs_info
->freed_extents
[0];
4408 ret
= find_first_extent_bit(unpin
, 0, &start
, &end
,
4413 if (btrfs_test_opt(root
, DISCARD
))
4414 ret
= btrfs_discard_extent(root
, start
,
4415 end
+ 1 - start
, NULL
);
4417 clear_extent_dirty(unpin
, start
, end
, GFP_NOFS
);
4418 unpin_extent_range(root
, start
, end
);
4425 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
4426 struct btrfs_root
*root
,
4427 u64 bytenr
, u64 num_bytes
, u64 parent
,
4428 u64 root_objectid
, u64 owner_objectid
,
4429 u64 owner_offset
, int refs_to_drop
,
4430 struct btrfs_delayed_extent_op
*extent_op
)
4432 struct btrfs_key key
;
4433 struct btrfs_path
*path
;
4434 struct btrfs_fs_info
*info
= root
->fs_info
;
4435 struct btrfs_root
*extent_root
= info
->extent_root
;
4436 struct extent_buffer
*leaf
;
4437 struct btrfs_extent_item
*ei
;
4438 struct btrfs_extent_inline_ref
*iref
;
4441 int extent_slot
= 0;
4442 int found_extent
= 0;
4447 path
= btrfs_alloc_path();
4452 path
->leave_spinning
= 1;
4454 is_data
= owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
;
4455 BUG_ON(!is_data
&& refs_to_drop
!= 1);
4457 ret
= lookup_extent_backref(trans
, extent_root
, path
, &iref
,
4458 bytenr
, num_bytes
, parent
,
4459 root_objectid
, owner_objectid
,
4462 extent_slot
= path
->slots
[0];
4463 while (extent_slot
>= 0) {
4464 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
4466 if (key
.objectid
!= bytenr
)
4468 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
4469 key
.offset
== num_bytes
) {
4473 if (path
->slots
[0] - extent_slot
> 5)
4477 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4478 item_size
= btrfs_item_size_nr(path
->nodes
[0], extent_slot
);
4479 if (found_extent
&& item_size
< sizeof(*ei
))
4482 if (!found_extent
) {
4484 ret
= remove_extent_backref(trans
, extent_root
, path
,
4488 btrfs_release_path(path
);
4489 path
->leave_spinning
= 1;
4491 key
.objectid
= bytenr
;
4492 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
4493 key
.offset
= num_bytes
;
4495 ret
= btrfs_search_slot(trans
, extent_root
,
4498 printk(KERN_ERR
"umm, got %d back from search"
4499 ", was looking for %llu\n", ret
,
4500 (unsigned long long)bytenr
);
4502 btrfs_print_leaf(extent_root
,
4506 extent_slot
= path
->slots
[0];
4509 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
4511 printk(KERN_ERR
"btrfs unable to find ref byte nr %llu "
4512 "parent %llu root %llu owner %llu offset %llu\n",
4513 (unsigned long long)bytenr
,
4514 (unsigned long long)parent
,
4515 (unsigned long long)root_objectid
,
4516 (unsigned long long)owner_objectid
,
4517 (unsigned long long)owner_offset
);
4520 leaf
= path
->nodes
[0];
4521 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
4522 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4523 if (item_size
< sizeof(*ei
)) {
4524 BUG_ON(found_extent
|| extent_slot
!= path
->slots
[0]);
4525 ret
= convert_extent_item_v0(trans
, extent_root
, path
,
4529 btrfs_release_path(path
);
4530 path
->leave_spinning
= 1;
4532 key
.objectid
= bytenr
;
4533 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
4534 key
.offset
= num_bytes
;
4536 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
,
4539 printk(KERN_ERR
"umm, got %d back from search"
4540 ", was looking for %llu\n", ret
,
4541 (unsigned long long)bytenr
);
4542 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
4545 extent_slot
= path
->slots
[0];
4546 leaf
= path
->nodes
[0];
4547 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
4550 BUG_ON(item_size
< sizeof(*ei
));
4551 ei
= btrfs_item_ptr(leaf
, extent_slot
,
4552 struct btrfs_extent_item
);
4553 if (owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
) {
4554 struct btrfs_tree_block_info
*bi
;
4555 BUG_ON(item_size
< sizeof(*ei
) + sizeof(*bi
));
4556 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
4557 WARN_ON(owner_objectid
!= btrfs_tree_block_level(leaf
, bi
));
4560 refs
= btrfs_extent_refs(leaf
, ei
);
4561 BUG_ON(refs
< refs_to_drop
);
4562 refs
-= refs_to_drop
;
4566 __run_delayed_extent_op(extent_op
, leaf
, ei
);
4568 * In the case of inline back ref, reference count will
4569 * be updated by remove_extent_backref
4572 BUG_ON(!found_extent
);
4574 btrfs_set_extent_refs(leaf
, ei
, refs
);
4575 btrfs_mark_buffer_dirty(leaf
);
4578 ret
= remove_extent_backref(trans
, extent_root
, path
,
4585 BUG_ON(is_data
&& refs_to_drop
!=
4586 extent_data_ref_count(root
, path
, iref
));
4588 BUG_ON(path
->slots
[0] != extent_slot
);
4590 BUG_ON(path
->slots
[0] != extent_slot
+ 1);
4591 path
->slots
[0] = extent_slot
;
4596 ret
= btrfs_del_items(trans
, extent_root
, path
, path
->slots
[0],
4599 btrfs_release_path(path
);
4602 ret
= btrfs_del_csums(trans
, root
, bytenr
, num_bytes
);
4605 invalidate_mapping_pages(info
->btree_inode
->i_mapping
,
4606 bytenr
>> PAGE_CACHE_SHIFT
,
4607 (bytenr
+ num_bytes
- 1) >> PAGE_CACHE_SHIFT
);
4610 ret
= update_block_group(trans
, root
, bytenr
, num_bytes
, 0);
4613 btrfs_free_path(path
);
4618 * when we free an block, it is possible (and likely) that we free the last
4619 * delayed ref for that extent as well. This searches the delayed ref tree for
4620 * a given extent, and if there are no other delayed refs to be processed, it
4621 * removes it from the tree.
4623 static noinline
int check_ref_cleanup(struct btrfs_trans_handle
*trans
,
4624 struct btrfs_root
*root
, u64 bytenr
)
4626 struct btrfs_delayed_ref_head
*head
;
4627 struct btrfs_delayed_ref_root
*delayed_refs
;
4628 struct btrfs_delayed_ref_node
*ref
;
4629 struct rb_node
*node
;
4632 delayed_refs
= &trans
->transaction
->delayed_refs
;
4633 spin_lock(&delayed_refs
->lock
);
4634 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
4638 node
= rb_prev(&head
->node
.rb_node
);
4642 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
4644 /* there are still entries for this ref, we can't drop it */
4645 if (ref
->bytenr
== bytenr
)
4648 if (head
->extent_op
) {
4649 if (!head
->must_insert_reserved
)
4651 kfree(head
->extent_op
);
4652 head
->extent_op
= NULL
;
4656 * waiting for the lock here would deadlock. If someone else has it
4657 * locked they are already in the process of dropping it anyway
4659 if (!mutex_trylock(&head
->mutex
))
4663 * at this point we have a head with no other entries. Go
4664 * ahead and process it.
4666 head
->node
.in_tree
= 0;
4667 rb_erase(&head
->node
.rb_node
, &delayed_refs
->root
);
4669 delayed_refs
->num_entries
--;
4672 * we don't take a ref on the node because we're removing it from the
4673 * tree, so we just steal the ref the tree was holding.
4675 delayed_refs
->num_heads
--;
4676 if (list_empty(&head
->cluster
))
4677 delayed_refs
->num_heads_ready
--;
4679 list_del_init(&head
->cluster
);
4680 spin_unlock(&delayed_refs
->lock
);
4682 BUG_ON(head
->extent_op
);
4683 if (head
->must_insert_reserved
)
4686 mutex_unlock(&head
->mutex
);
4687 btrfs_put_delayed_ref(&head
->node
);
4690 spin_unlock(&delayed_refs
->lock
);
4694 void btrfs_free_tree_block(struct btrfs_trans_handle
*trans
,
4695 struct btrfs_root
*root
,
4696 struct extent_buffer
*buf
,
4697 u64 parent
, int last_ref
)
4699 struct btrfs_block_rsv
*block_rsv
;
4700 struct btrfs_block_group_cache
*cache
= NULL
;
4703 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
4704 ret
= btrfs_add_delayed_tree_ref(trans
, buf
->start
, buf
->len
,
4705 parent
, root
->root_key
.objectid
,
4706 btrfs_header_level(buf
),
4707 BTRFS_DROP_DELAYED_REF
, NULL
);
4714 block_rsv
= get_block_rsv(trans
, root
);
4715 cache
= btrfs_lookup_block_group(root
->fs_info
, buf
->start
);
4716 if (block_rsv
->space_info
!= cache
->space_info
)
4719 if (btrfs_header_generation(buf
) == trans
->transid
) {
4720 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
4721 ret
= check_ref_cleanup(trans
, root
, buf
->start
);
4726 if (btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
)) {
4727 pin_down_extent(root
, cache
, buf
->start
, buf
->len
, 1);
4731 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY
, &buf
->bflags
));
4733 btrfs_add_free_space(cache
, buf
->start
, buf
->len
);
4734 btrfs_update_reserved_bytes(cache
, buf
->len
, RESERVE_FREE
);
4738 * Deleting the buffer, clear the corrupt flag since it doesn't matter
4741 clear_bit(EXTENT_BUFFER_CORRUPT
, &buf
->bflags
);
4742 btrfs_put_block_group(cache
);
4745 int btrfs_free_extent(struct btrfs_trans_handle
*trans
,
4746 struct btrfs_root
*root
,
4747 u64 bytenr
, u64 num_bytes
, u64 parent
,
4748 u64 root_objectid
, u64 owner
, u64 offset
)
4753 * tree log blocks never actually go into the extent allocation
4754 * tree, just update pinning info and exit early.
4756 if (root_objectid
== BTRFS_TREE_LOG_OBJECTID
) {
4757 WARN_ON(owner
>= BTRFS_FIRST_FREE_OBJECTID
);
4758 /* unlocks the pinned mutex */
4759 btrfs_pin_extent(root
, bytenr
, num_bytes
, 1);
4761 } else if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
4762 ret
= btrfs_add_delayed_tree_ref(trans
, bytenr
, num_bytes
,
4763 parent
, root_objectid
, (int)owner
,
4764 BTRFS_DROP_DELAYED_REF
, NULL
);
4767 ret
= btrfs_add_delayed_data_ref(trans
, bytenr
, num_bytes
,
4768 parent
, root_objectid
, owner
,
4769 offset
, BTRFS_DROP_DELAYED_REF
, NULL
);
4775 static u64
stripe_align(struct btrfs_root
*root
, u64 val
)
4777 u64 mask
= ((u64
)root
->stripesize
- 1);
4778 u64 ret
= (val
+ mask
) & ~mask
;
4783 * when we wait for progress in the block group caching, its because
4784 * our allocation attempt failed at least once. So, we must sleep
4785 * and let some progress happen before we try again.
4787 * This function will sleep at least once waiting for new free space to
4788 * show up, and then it will check the block group free space numbers
4789 * for our min num_bytes. Another option is to have it go ahead
4790 * and look in the rbtree for a free extent of a given size, but this
4794 wait_block_group_cache_progress(struct btrfs_block_group_cache
*cache
,
4797 struct btrfs_caching_control
*caching_ctl
;
4800 caching_ctl
= get_caching_control(cache
);
4804 wait_event(caching_ctl
->wait
, block_group_cache_done(cache
) ||
4805 (cache
->free_space_ctl
->free_space
>= num_bytes
));
4807 put_caching_control(caching_ctl
);
4812 wait_block_group_cache_done(struct btrfs_block_group_cache
*cache
)
4814 struct btrfs_caching_control
*caching_ctl
;
4817 caching_ctl
= get_caching_control(cache
);
4821 wait_event(caching_ctl
->wait
, block_group_cache_done(cache
));
4823 put_caching_control(caching_ctl
);
4827 static int get_block_group_index(struct btrfs_block_group_cache
*cache
)
4830 if (cache
->flags
& BTRFS_BLOCK_GROUP_RAID10
)
4832 else if (cache
->flags
& BTRFS_BLOCK_GROUP_RAID1
)
4834 else if (cache
->flags
& BTRFS_BLOCK_GROUP_DUP
)
4836 else if (cache
->flags
& BTRFS_BLOCK_GROUP_RAID0
)
4843 enum btrfs_loop_type
{
4844 LOOP_FIND_IDEAL
= 0,
4845 LOOP_CACHING_NOWAIT
= 1,
4846 LOOP_CACHING_WAIT
= 2,
4847 LOOP_ALLOC_CHUNK
= 3,
4848 LOOP_NO_EMPTY_SIZE
= 4,
4852 * walks the btree of allocated extents and find a hole of a given size.
4853 * The key ins is changed to record the hole:
4854 * ins->objectid == block start
4855 * ins->flags = BTRFS_EXTENT_ITEM_KEY
4856 * ins->offset == number of blocks
4857 * Any available blocks before search_start are skipped.
4859 static noinline
int find_free_extent(struct btrfs_trans_handle
*trans
,
4860 struct btrfs_root
*orig_root
,
4861 u64 num_bytes
, u64 empty_size
,
4862 u64 search_start
, u64 search_end
,
4863 u64 hint_byte
, struct btrfs_key
*ins
,
4867 struct btrfs_root
*root
= orig_root
->fs_info
->extent_root
;
4868 struct btrfs_free_cluster
*last_ptr
= NULL
;
4869 struct btrfs_block_group_cache
*block_group
= NULL
;
4870 int empty_cluster
= 2 * 1024 * 1024;
4871 int allowed_chunk_alloc
= 0;
4872 int done_chunk_alloc
= 0;
4873 struct btrfs_space_info
*space_info
;
4874 int last_ptr_loop
= 0;
4877 int alloc_type
= (data
& BTRFS_BLOCK_GROUP_DATA
) ?
4878 RESERVE_ALLOC_NO_ACCOUNT
: RESERVE_ALLOC
;
4879 bool found_uncached_bg
= false;
4880 bool failed_cluster_refill
= false;
4881 bool failed_alloc
= false;
4882 bool use_cluster
= true;
4883 u64 ideal_cache_percent
= 0;
4884 u64 ideal_cache_offset
= 0;
4886 WARN_ON(num_bytes
< root
->sectorsize
);
4887 btrfs_set_key_type(ins
, BTRFS_EXTENT_ITEM_KEY
);
4891 space_info
= __find_space_info(root
->fs_info
, data
);
4893 printk(KERN_ERR
"No space info for %llu\n", data
);
4898 * If the space info is for both data and metadata it means we have a
4899 * small filesystem and we can't use the clustering stuff.
4901 if (btrfs_mixed_space_info(space_info
))
4902 use_cluster
= false;
4904 if (orig_root
->ref_cows
|| empty_size
)
4905 allowed_chunk_alloc
= 1;
4907 if (data
& BTRFS_BLOCK_GROUP_METADATA
&& use_cluster
) {
4908 last_ptr
= &root
->fs_info
->meta_alloc_cluster
;
4909 if (!btrfs_test_opt(root
, SSD
))
4910 empty_cluster
= 64 * 1024;
4913 if ((data
& BTRFS_BLOCK_GROUP_DATA
) && use_cluster
&&
4914 btrfs_test_opt(root
, SSD
)) {
4915 last_ptr
= &root
->fs_info
->data_alloc_cluster
;
4919 spin_lock(&last_ptr
->lock
);
4920 if (last_ptr
->block_group
)
4921 hint_byte
= last_ptr
->window_start
;
4922 spin_unlock(&last_ptr
->lock
);
4925 search_start
= max(search_start
, first_logical_byte(root
, 0));
4926 search_start
= max(search_start
, hint_byte
);
4931 if (search_start
== hint_byte
) {
4933 block_group
= btrfs_lookup_block_group(root
->fs_info
,
4936 * we don't want to use the block group if it doesn't match our
4937 * allocation bits, or if its not cached.
4939 * However if we are re-searching with an ideal block group
4940 * picked out then we don't care that the block group is cached.
4942 if (block_group
&& block_group_bits(block_group
, data
) &&
4943 (block_group
->cached
!= BTRFS_CACHE_NO
||
4944 search_start
== ideal_cache_offset
)) {
4945 down_read(&space_info
->groups_sem
);
4946 if (list_empty(&block_group
->list
) ||
4949 * someone is removing this block group,
4950 * we can't jump into the have_block_group
4951 * target because our list pointers are not
4954 btrfs_put_block_group(block_group
);
4955 up_read(&space_info
->groups_sem
);
4957 index
= get_block_group_index(block_group
);
4958 goto have_block_group
;
4960 } else if (block_group
) {
4961 btrfs_put_block_group(block_group
);
4965 down_read(&space_info
->groups_sem
);
4966 list_for_each_entry(block_group
, &space_info
->block_groups
[index
],
4971 btrfs_get_block_group(block_group
);
4972 search_start
= block_group
->key
.objectid
;
4975 * this can happen if we end up cycling through all the
4976 * raid types, but we want to make sure we only allocate
4977 * for the proper type.
4979 if (!block_group_bits(block_group
, data
)) {
4980 u64 extra
= BTRFS_BLOCK_GROUP_DUP
|
4981 BTRFS_BLOCK_GROUP_RAID1
|
4982 BTRFS_BLOCK_GROUP_RAID10
;
4985 * if they asked for extra copies and this block group
4986 * doesn't provide them, bail. This does allow us to
4987 * fill raid0 from raid1.
4989 if ((data
& extra
) && !(block_group
->flags
& extra
))
4994 if (unlikely(block_group
->cached
== BTRFS_CACHE_NO
)) {
4997 ret
= cache_block_group(block_group
, trans
,
4999 if (block_group
->cached
== BTRFS_CACHE_FINISHED
)
5000 goto have_block_group
;
5002 free_percent
= btrfs_block_group_used(&block_group
->item
);
5003 free_percent
*= 100;
5004 free_percent
= div64_u64(free_percent
,
5005 block_group
->key
.offset
);
5006 free_percent
= 100 - free_percent
;
5007 if (free_percent
> ideal_cache_percent
&&
5008 likely(!block_group
->ro
)) {
5009 ideal_cache_offset
= block_group
->key
.objectid
;
5010 ideal_cache_percent
= free_percent
;
5014 * The caching workers are limited to 2 threads, so we
5015 * can queue as much work as we care to.
5017 if (loop
> LOOP_FIND_IDEAL
) {
5018 ret
= cache_block_group(block_group
, trans
,
5022 found_uncached_bg
= true;
5025 * If loop is set for cached only, try the next block
5028 if (loop
== LOOP_FIND_IDEAL
)
5032 cached
= block_group_cache_done(block_group
);
5033 if (unlikely(!cached
))
5034 found_uncached_bg
= true;
5036 if (unlikely(block_group
->ro
))
5039 spin_lock(&block_group
->free_space_ctl
->tree_lock
);
5041 block_group
->free_space_ctl
->free_space
<
5042 num_bytes
+ empty_size
) {
5043 spin_unlock(&block_group
->free_space_ctl
->tree_lock
);
5046 spin_unlock(&block_group
->free_space_ctl
->tree_lock
);
5049 * Ok we want to try and use the cluster allocator, so lets look
5050 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
5051 * have tried the cluster allocator plenty of times at this
5052 * point and not have found anything, so we are likely way too
5053 * fragmented for the clustering stuff to find anything, so lets
5054 * just skip it and let the allocator find whatever block it can
5057 if (last_ptr
&& loop
< LOOP_NO_EMPTY_SIZE
) {
5059 * the refill lock keeps out other
5060 * people trying to start a new cluster
5062 spin_lock(&last_ptr
->refill_lock
);
5063 if (last_ptr
->block_group
&&
5064 (last_ptr
->block_group
->ro
||
5065 !block_group_bits(last_ptr
->block_group
, data
))) {
5067 goto refill_cluster
;
5070 offset
= btrfs_alloc_from_cluster(block_group
, last_ptr
,
5071 num_bytes
, search_start
);
5073 /* we have a block, we're done */
5074 spin_unlock(&last_ptr
->refill_lock
);
5078 spin_lock(&last_ptr
->lock
);
5080 * whoops, this cluster doesn't actually point to
5081 * this block group. Get a ref on the block
5082 * group is does point to and try again
5084 if (!last_ptr_loop
&& last_ptr
->block_group
&&
5085 last_ptr
->block_group
!= block_group
&&
5087 get_block_group_index(last_ptr
->block_group
)) {
5089 btrfs_put_block_group(block_group
);
5090 block_group
= last_ptr
->block_group
;
5091 btrfs_get_block_group(block_group
);
5092 spin_unlock(&last_ptr
->lock
);
5093 spin_unlock(&last_ptr
->refill_lock
);
5096 search_start
= block_group
->key
.objectid
;
5098 * we know this block group is properly
5099 * in the list because
5100 * btrfs_remove_block_group, drops the
5101 * cluster before it removes the block
5102 * group from the list
5104 goto have_block_group
;
5106 spin_unlock(&last_ptr
->lock
);
5109 * this cluster didn't work out, free it and
5112 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
5116 /* allocate a cluster in this block group */
5117 ret
= btrfs_find_space_cluster(trans
, root
,
5118 block_group
, last_ptr
,
5120 empty_cluster
+ empty_size
);
5123 * now pull our allocation out of this
5126 offset
= btrfs_alloc_from_cluster(block_group
,
5127 last_ptr
, num_bytes
,
5130 /* we found one, proceed */
5131 spin_unlock(&last_ptr
->refill_lock
);
5134 } else if (!cached
&& loop
> LOOP_CACHING_NOWAIT
5135 && !failed_cluster_refill
) {
5136 spin_unlock(&last_ptr
->refill_lock
);
5138 failed_cluster_refill
= true;
5139 wait_block_group_cache_progress(block_group
,
5140 num_bytes
+ empty_cluster
+ empty_size
);
5141 goto have_block_group
;
5145 * at this point we either didn't find a cluster
5146 * or we weren't able to allocate a block from our
5147 * cluster. Free the cluster we've been trying
5148 * to use, and go to the next block group
5150 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
5151 spin_unlock(&last_ptr
->refill_lock
);
5155 offset
= btrfs_find_space_for_alloc(block_group
, search_start
,
5156 num_bytes
, empty_size
);
5158 * If we didn't find a chunk, and we haven't failed on this
5159 * block group before, and this block group is in the middle of
5160 * caching and we are ok with waiting, then go ahead and wait
5161 * for progress to be made, and set failed_alloc to true.
5163 * If failed_alloc is true then we've already waited on this
5164 * block group once and should move on to the next block group.
5166 if (!offset
&& !failed_alloc
&& !cached
&&
5167 loop
> LOOP_CACHING_NOWAIT
) {
5168 wait_block_group_cache_progress(block_group
,
5169 num_bytes
+ empty_size
);
5170 failed_alloc
= true;
5171 goto have_block_group
;
5172 } else if (!offset
) {
5176 search_start
= stripe_align(root
, offset
);
5177 /* move on to the next group */
5178 if (search_start
+ num_bytes
>= search_end
) {
5179 btrfs_add_free_space(block_group
, offset
, num_bytes
);
5183 /* move on to the next group */
5184 if (search_start
+ num_bytes
>
5185 block_group
->key
.objectid
+ block_group
->key
.offset
) {
5186 btrfs_add_free_space(block_group
, offset
, num_bytes
);
5190 ins
->objectid
= search_start
;
5191 ins
->offset
= num_bytes
;
5193 if (offset
< search_start
)
5194 btrfs_add_free_space(block_group
, offset
,
5195 search_start
- offset
);
5196 BUG_ON(offset
> search_start
);
5198 ret
= btrfs_update_reserved_bytes(block_group
, num_bytes
,
5200 if (ret
== -EAGAIN
) {
5201 btrfs_add_free_space(block_group
, offset
, num_bytes
);
5205 /* we are all good, lets return */
5206 ins
->objectid
= search_start
;
5207 ins
->offset
= num_bytes
;
5209 if (offset
< search_start
)
5210 btrfs_add_free_space(block_group
, offset
,
5211 search_start
- offset
);
5212 BUG_ON(offset
> search_start
);
5213 btrfs_put_block_group(block_group
);
5216 failed_cluster_refill
= false;
5217 failed_alloc
= false;
5218 BUG_ON(index
!= get_block_group_index(block_group
));
5219 btrfs_put_block_group(block_group
);
5221 up_read(&space_info
->groups_sem
);
5223 if (!ins
->objectid
&& ++index
< BTRFS_NR_RAID_TYPES
)
5226 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5227 * for them to make caching progress. Also
5228 * determine the best possible bg to cache
5229 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5230 * caching kthreads as we move along
5231 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5232 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5233 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5236 if (!ins
->objectid
&& loop
< LOOP_NO_EMPTY_SIZE
) {
5238 if (loop
== LOOP_FIND_IDEAL
&& found_uncached_bg
) {
5239 found_uncached_bg
= false;
5241 if (!ideal_cache_percent
)
5245 * 1 of the following 2 things have happened so far
5247 * 1) We found an ideal block group for caching that
5248 * is mostly full and will cache quickly, so we might
5249 * as well wait for it.
5251 * 2) We searched for cached only and we didn't find
5252 * anything, and we didn't start any caching kthreads
5253 * either, so chances are we will loop through and
5254 * start a couple caching kthreads, and then come back
5255 * around and just wait for them. This will be slower
5256 * because we will have 2 caching kthreads reading at
5257 * the same time when we could have just started one
5258 * and waited for it to get far enough to give us an
5259 * allocation, so go ahead and go to the wait caching
5262 loop
= LOOP_CACHING_WAIT
;
5263 search_start
= ideal_cache_offset
;
5264 ideal_cache_percent
= 0;
5266 } else if (loop
== LOOP_FIND_IDEAL
) {
5268 * Didn't find a uncached bg, wait on anything we find
5271 loop
= LOOP_CACHING_WAIT
;
5277 if (loop
== LOOP_ALLOC_CHUNK
) {
5278 if (allowed_chunk_alloc
) {
5279 ret
= do_chunk_alloc(trans
, root
, num_bytes
+
5280 2 * 1024 * 1024, data
,
5281 CHUNK_ALLOC_LIMITED
);
5282 allowed_chunk_alloc
= 0;
5284 done_chunk_alloc
= 1;
5285 } else if (!done_chunk_alloc
&&
5286 space_info
->force_alloc
==
5287 CHUNK_ALLOC_NO_FORCE
) {
5288 space_info
->force_alloc
= CHUNK_ALLOC_LIMITED
;
5292 * We didn't allocate a chunk, go ahead and drop the
5293 * empty size and loop again.
5295 if (!done_chunk_alloc
)
5296 loop
= LOOP_NO_EMPTY_SIZE
;
5299 if (loop
== LOOP_NO_EMPTY_SIZE
) {
5305 } else if (!ins
->objectid
) {
5307 } else if (ins
->objectid
) {
5314 static void dump_space_info(struct btrfs_space_info
*info
, u64 bytes
,
5315 int dump_block_groups
)
5317 struct btrfs_block_group_cache
*cache
;
5320 spin_lock(&info
->lock
);
5321 printk(KERN_INFO
"space_info %llu has %llu free, is %sfull\n",
5322 (unsigned long long)info
->flags
,
5323 (unsigned long long)(info
->total_bytes
- info
->bytes_used
-
5324 info
->bytes_pinned
- info
->bytes_reserved
-
5325 info
->bytes_readonly
),
5326 (info
->full
) ? "" : "not ");
5327 printk(KERN_INFO
"space_info total=%llu, used=%llu, pinned=%llu, "
5328 "reserved=%llu, may_use=%llu, readonly=%llu\n",
5329 (unsigned long long)info
->total_bytes
,
5330 (unsigned long long)info
->bytes_used
,
5331 (unsigned long long)info
->bytes_pinned
,
5332 (unsigned long long)info
->bytes_reserved
,
5333 (unsigned long long)info
->bytes_may_use
,
5334 (unsigned long long)info
->bytes_readonly
);
5335 spin_unlock(&info
->lock
);
5337 if (!dump_block_groups
)
5340 down_read(&info
->groups_sem
);
5342 list_for_each_entry(cache
, &info
->block_groups
[index
], list
) {
5343 spin_lock(&cache
->lock
);
5344 printk(KERN_INFO
"block group %llu has %llu bytes, %llu used "
5345 "%llu pinned %llu reserved\n",
5346 (unsigned long long)cache
->key
.objectid
,
5347 (unsigned long long)cache
->key
.offset
,
5348 (unsigned long long)btrfs_block_group_used(&cache
->item
),
5349 (unsigned long long)cache
->pinned
,
5350 (unsigned long long)cache
->reserved
);
5351 btrfs_dump_free_space(cache
, bytes
);
5352 spin_unlock(&cache
->lock
);
5354 if (++index
< BTRFS_NR_RAID_TYPES
)
5356 up_read(&info
->groups_sem
);
5359 int btrfs_reserve_extent(struct btrfs_trans_handle
*trans
,
5360 struct btrfs_root
*root
,
5361 u64 num_bytes
, u64 min_alloc_size
,
5362 u64 empty_size
, u64 hint_byte
,
5363 u64 search_end
, struct btrfs_key
*ins
,
5367 u64 search_start
= 0;
5369 data
= btrfs_get_alloc_profile(root
, data
);
5372 * the only place that sets empty_size is btrfs_realloc_node, which
5373 * is not called recursively on allocations
5375 if (empty_size
|| root
->ref_cows
)
5376 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
5377 num_bytes
+ 2 * 1024 * 1024, data
,
5378 CHUNK_ALLOC_NO_FORCE
);
5380 WARN_ON(num_bytes
< root
->sectorsize
);
5381 ret
= find_free_extent(trans
, root
, num_bytes
, empty_size
,
5382 search_start
, search_end
, hint_byte
,
5385 if (ret
== -ENOSPC
&& num_bytes
> min_alloc_size
) {
5386 num_bytes
= num_bytes
>> 1;
5387 num_bytes
= num_bytes
& ~(root
->sectorsize
- 1);
5388 num_bytes
= max(num_bytes
, min_alloc_size
);
5389 do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
5390 num_bytes
, data
, CHUNK_ALLOC_FORCE
);
5393 if (ret
== -ENOSPC
&& btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
5394 struct btrfs_space_info
*sinfo
;
5396 sinfo
= __find_space_info(root
->fs_info
, data
);
5397 printk(KERN_ERR
"btrfs allocation failed flags %llu, "
5398 "wanted %llu\n", (unsigned long long)data
,
5399 (unsigned long long)num_bytes
);
5400 dump_space_info(sinfo
, num_bytes
, 1);
5403 trace_btrfs_reserved_extent_alloc(root
, ins
->objectid
, ins
->offset
);
5408 int btrfs_free_reserved_extent(struct btrfs_root
*root
, u64 start
, u64 len
)
5410 struct btrfs_block_group_cache
*cache
;
5413 cache
= btrfs_lookup_block_group(root
->fs_info
, start
);
5415 printk(KERN_ERR
"Unable to find block group for %llu\n",
5416 (unsigned long long)start
);
5420 if (btrfs_test_opt(root
, DISCARD
))
5421 ret
= btrfs_discard_extent(root
, start
, len
, NULL
);
5423 btrfs_add_free_space(cache
, start
, len
);
5424 btrfs_update_reserved_bytes(cache
, len
, RESERVE_FREE
);
5425 btrfs_put_block_group(cache
);
5427 trace_btrfs_reserved_extent_free(root
, start
, len
);
5432 static int alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
5433 struct btrfs_root
*root
,
5434 u64 parent
, u64 root_objectid
,
5435 u64 flags
, u64 owner
, u64 offset
,
5436 struct btrfs_key
*ins
, int ref_mod
)
5439 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5440 struct btrfs_extent_item
*extent_item
;
5441 struct btrfs_extent_inline_ref
*iref
;
5442 struct btrfs_path
*path
;
5443 struct extent_buffer
*leaf
;
5448 type
= BTRFS_SHARED_DATA_REF_KEY
;
5450 type
= BTRFS_EXTENT_DATA_REF_KEY
;
5452 size
= sizeof(*extent_item
) + btrfs_extent_inline_ref_size(type
);
5454 path
= btrfs_alloc_path();
5458 path
->leave_spinning
= 1;
5459 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
5463 leaf
= path
->nodes
[0];
5464 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
5465 struct btrfs_extent_item
);
5466 btrfs_set_extent_refs(leaf
, extent_item
, ref_mod
);
5467 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
5468 btrfs_set_extent_flags(leaf
, extent_item
,
5469 flags
| BTRFS_EXTENT_FLAG_DATA
);
5471 iref
= (struct btrfs_extent_inline_ref
*)(extent_item
+ 1);
5472 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
5474 struct btrfs_shared_data_ref
*ref
;
5475 ref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
5476 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
5477 btrfs_set_shared_data_ref_count(leaf
, ref
, ref_mod
);
5479 struct btrfs_extent_data_ref
*ref
;
5480 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
5481 btrfs_set_extent_data_ref_root(leaf
, ref
, root_objectid
);
5482 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
5483 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
5484 btrfs_set_extent_data_ref_count(leaf
, ref
, ref_mod
);
5487 btrfs_mark_buffer_dirty(path
->nodes
[0]);
5488 btrfs_free_path(path
);
5490 ret
= update_block_group(trans
, root
, ins
->objectid
, ins
->offset
, 1);
5492 printk(KERN_ERR
"btrfs update block group failed for %llu "
5493 "%llu\n", (unsigned long long)ins
->objectid
,
5494 (unsigned long long)ins
->offset
);
5500 static int alloc_reserved_tree_block(struct btrfs_trans_handle
*trans
,
5501 struct btrfs_root
*root
,
5502 u64 parent
, u64 root_objectid
,
5503 u64 flags
, struct btrfs_disk_key
*key
,
5504 int level
, struct btrfs_key
*ins
)
5507 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5508 struct btrfs_extent_item
*extent_item
;
5509 struct btrfs_tree_block_info
*block_info
;
5510 struct btrfs_extent_inline_ref
*iref
;
5511 struct btrfs_path
*path
;
5512 struct extent_buffer
*leaf
;
5513 u32 size
= sizeof(*extent_item
) + sizeof(*block_info
) + sizeof(*iref
);
5515 path
= btrfs_alloc_path();
5519 path
->leave_spinning
= 1;
5520 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
5524 leaf
= path
->nodes
[0];
5525 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
5526 struct btrfs_extent_item
);
5527 btrfs_set_extent_refs(leaf
, extent_item
, 1);
5528 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
5529 btrfs_set_extent_flags(leaf
, extent_item
,
5530 flags
| BTRFS_EXTENT_FLAG_TREE_BLOCK
);
5531 block_info
= (struct btrfs_tree_block_info
*)(extent_item
+ 1);
5533 btrfs_set_tree_block_key(leaf
, block_info
, key
);
5534 btrfs_set_tree_block_level(leaf
, block_info
, level
);
5536 iref
= (struct btrfs_extent_inline_ref
*)(block_info
+ 1);
5538 BUG_ON(!(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
));
5539 btrfs_set_extent_inline_ref_type(leaf
, iref
,
5540 BTRFS_SHARED_BLOCK_REF_KEY
);
5541 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
5543 btrfs_set_extent_inline_ref_type(leaf
, iref
,
5544 BTRFS_TREE_BLOCK_REF_KEY
);
5545 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
5548 btrfs_mark_buffer_dirty(leaf
);
5549 btrfs_free_path(path
);
5551 ret
= update_block_group(trans
, root
, ins
->objectid
, ins
->offset
, 1);
5553 printk(KERN_ERR
"btrfs update block group failed for %llu "
5554 "%llu\n", (unsigned long long)ins
->objectid
,
5555 (unsigned long long)ins
->offset
);
5561 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
5562 struct btrfs_root
*root
,
5563 u64 root_objectid
, u64 owner
,
5564 u64 offset
, struct btrfs_key
*ins
)
5568 BUG_ON(root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
5570 ret
= btrfs_add_delayed_data_ref(trans
, ins
->objectid
, ins
->offset
,
5571 0, root_objectid
, owner
, offset
,
5572 BTRFS_ADD_DELAYED_EXTENT
, NULL
);
5577 * this is used by the tree logging recovery code. It records that
5578 * an extent has been allocated and makes sure to clear the free
5579 * space cache bits as well
5581 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle
*trans
,
5582 struct btrfs_root
*root
,
5583 u64 root_objectid
, u64 owner
, u64 offset
,
5584 struct btrfs_key
*ins
)
5587 struct btrfs_block_group_cache
*block_group
;
5588 struct btrfs_caching_control
*caching_ctl
;
5589 u64 start
= ins
->objectid
;
5590 u64 num_bytes
= ins
->offset
;
5592 block_group
= btrfs_lookup_block_group(root
->fs_info
, ins
->objectid
);
5593 cache_block_group(block_group
, trans
, NULL
, 0);
5594 caching_ctl
= get_caching_control(block_group
);
5597 BUG_ON(!block_group_cache_done(block_group
));
5598 ret
= btrfs_remove_free_space(block_group
, start
, num_bytes
);
5601 mutex_lock(&caching_ctl
->mutex
);
5603 if (start
>= caching_ctl
->progress
) {
5604 ret
= add_excluded_extent(root
, start
, num_bytes
);
5606 } else if (start
+ num_bytes
<= caching_ctl
->progress
) {
5607 ret
= btrfs_remove_free_space(block_group
,
5611 num_bytes
= caching_ctl
->progress
- start
;
5612 ret
= btrfs_remove_free_space(block_group
,
5616 start
= caching_ctl
->progress
;
5617 num_bytes
= ins
->objectid
+ ins
->offset
-
5618 caching_ctl
->progress
;
5619 ret
= add_excluded_extent(root
, start
, num_bytes
);
5623 mutex_unlock(&caching_ctl
->mutex
);
5624 put_caching_control(caching_ctl
);
5627 ret
= btrfs_update_reserved_bytes(block_group
, ins
->offset
,
5628 RESERVE_ALLOC_NO_ACCOUNT
);
5630 btrfs_put_block_group(block_group
);
5631 ret
= alloc_reserved_file_extent(trans
, root
, 0, root_objectid
,
5632 0, owner
, offset
, ins
, 1);
5636 struct extent_buffer
*btrfs_init_new_buffer(struct btrfs_trans_handle
*trans
,
5637 struct btrfs_root
*root
,
5638 u64 bytenr
, u32 blocksize
,
5641 struct extent_buffer
*buf
;
5643 buf
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
5645 return ERR_PTR(-ENOMEM
);
5646 btrfs_set_header_generation(buf
, trans
->transid
);
5647 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, buf
, level
);
5648 btrfs_tree_lock(buf
);
5649 clean_tree_block(trans
, root
, buf
);
5651 btrfs_set_lock_blocking(buf
);
5652 btrfs_set_buffer_uptodate(buf
);
5654 if (root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
) {
5656 * we allow two log transactions at a time, use different
5657 * EXENT bit to differentiate dirty pages.
5659 if (root
->log_transid
% 2 == 0)
5660 set_extent_dirty(&root
->dirty_log_pages
, buf
->start
,
5661 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
5663 set_extent_new(&root
->dirty_log_pages
, buf
->start
,
5664 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
5666 set_extent_dirty(&trans
->transaction
->dirty_pages
, buf
->start
,
5667 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
5669 trans
->blocks_used
++;
5670 /* this returns a buffer locked for blocking */
5674 static struct btrfs_block_rsv
*
5675 use_block_rsv(struct btrfs_trans_handle
*trans
,
5676 struct btrfs_root
*root
, u32 blocksize
)
5678 struct btrfs_block_rsv
*block_rsv
;
5679 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
5682 block_rsv
= get_block_rsv(trans
, root
);
5684 if (block_rsv
->size
== 0) {
5685 ret
= reserve_metadata_bytes(trans
, root
, block_rsv
,
5688 * If we couldn't reserve metadata bytes try and use some from
5689 * the global reserve.
5691 if (ret
&& block_rsv
!= global_rsv
) {
5692 ret
= block_rsv_use_bytes(global_rsv
, blocksize
);
5695 return ERR_PTR(ret
);
5697 return ERR_PTR(ret
);
5702 ret
= block_rsv_use_bytes(block_rsv
, blocksize
);
5707 ret
= reserve_metadata_bytes(trans
, root
, block_rsv
, blocksize
,
5710 spin_lock(&block_rsv
->lock
);
5711 block_rsv
->size
+= blocksize
;
5712 spin_unlock(&block_rsv
->lock
);
5714 } else if (ret
&& block_rsv
!= global_rsv
) {
5715 ret
= block_rsv_use_bytes(global_rsv
, blocksize
);
5721 return ERR_PTR(-ENOSPC
);
5724 static void unuse_block_rsv(struct btrfs_block_rsv
*block_rsv
, u32 blocksize
)
5726 block_rsv_add_bytes(block_rsv
, blocksize
, 0);
5727 block_rsv_release_bytes(block_rsv
, NULL
, 0);
5731 * finds a free extent and does all the dirty work required for allocation
5732 * returns the key for the extent through ins, and a tree buffer for
5733 * the first block of the extent through buf.
5735 * returns the tree buffer or NULL.
5737 struct extent_buffer
*btrfs_alloc_free_block(struct btrfs_trans_handle
*trans
,
5738 struct btrfs_root
*root
, u32 blocksize
,
5739 u64 parent
, u64 root_objectid
,
5740 struct btrfs_disk_key
*key
, int level
,
5741 u64 hint
, u64 empty_size
)
5743 struct btrfs_key ins
;
5744 struct btrfs_block_rsv
*block_rsv
;
5745 struct extent_buffer
*buf
;
5750 block_rsv
= use_block_rsv(trans
, root
, blocksize
);
5751 if (IS_ERR(block_rsv
))
5752 return ERR_CAST(block_rsv
);
5754 ret
= btrfs_reserve_extent(trans
, root
, blocksize
, blocksize
,
5755 empty_size
, hint
, (u64
)-1, &ins
, 0);
5757 unuse_block_rsv(block_rsv
, blocksize
);
5758 return ERR_PTR(ret
);
5761 buf
= btrfs_init_new_buffer(trans
, root
, ins
.objectid
,
5763 BUG_ON(IS_ERR(buf
));
5765 if (root_objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
5767 parent
= ins
.objectid
;
5768 flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
5772 if (root_objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
5773 struct btrfs_delayed_extent_op
*extent_op
;
5774 extent_op
= kmalloc(sizeof(*extent_op
), GFP_NOFS
);
5777 memcpy(&extent_op
->key
, key
, sizeof(extent_op
->key
));
5779 memset(&extent_op
->key
, 0, sizeof(extent_op
->key
));
5780 extent_op
->flags_to_set
= flags
;
5781 extent_op
->update_key
= 1;
5782 extent_op
->update_flags
= 1;
5783 extent_op
->is_data
= 0;
5785 ret
= btrfs_add_delayed_tree_ref(trans
, ins
.objectid
,
5786 ins
.offset
, parent
, root_objectid
,
5787 level
, BTRFS_ADD_DELAYED_EXTENT
,
5794 struct walk_control
{
5795 u64 refs
[BTRFS_MAX_LEVEL
];
5796 u64 flags
[BTRFS_MAX_LEVEL
];
5797 struct btrfs_key update_progress
;
5807 #define DROP_REFERENCE 1
5808 #define UPDATE_BACKREF 2
5810 static noinline
void reada_walk_down(struct btrfs_trans_handle
*trans
,
5811 struct btrfs_root
*root
,
5812 struct walk_control
*wc
,
5813 struct btrfs_path
*path
)
5821 struct btrfs_key key
;
5822 struct extent_buffer
*eb
;
5827 if (path
->slots
[wc
->level
] < wc
->reada_slot
) {
5828 wc
->reada_count
= wc
->reada_count
* 2 / 3;
5829 wc
->reada_count
= max(wc
->reada_count
, 2);
5831 wc
->reada_count
= wc
->reada_count
* 3 / 2;
5832 wc
->reada_count
= min_t(int, wc
->reada_count
,
5833 BTRFS_NODEPTRS_PER_BLOCK(root
));
5836 eb
= path
->nodes
[wc
->level
];
5837 nritems
= btrfs_header_nritems(eb
);
5838 blocksize
= btrfs_level_size(root
, wc
->level
- 1);
5840 for (slot
= path
->slots
[wc
->level
]; slot
< nritems
; slot
++) {
5841 if (nread
>= wc
->reada_count
)
5845 bytenr
= btrfs_node_blockptr(eb
, slot
);
5846 generation
= btrfs_node_ptr_generation(eb
, slot
);
5848 if (slot
== path
->slots
[wc
->level
])
5851 if (wc
->stage
== UPDATE_BACKREF
&&
5852 generation
<= root
->root_key
.offset
)
5855 /* We don't lock the tree block, it's OK to be racy here */
5856 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
, blocksize
,
5861 if (wc
->stage
== DROP_REFERENCE
) {
5865 if (wc
->level
== 1 &&
5866 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
5868 if (!wc
->update_ref
||
5869 generation
<= root
->root_key
.offset
)
5871 btrfs_node_key_to_cpu(eb
, &key
, slot
);
5872 ret
= btrfs_comp_cpu_keys(&key
,
5873 &wc
->update_progress
);
5877 if (wc
->level
== 1 &&
5878 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
5882 ret
= readahead_tree_block(root
, bytenr
, blocksize
,
5888 wc
->reada_slot
= slot
;
5892 * hepler to process tree block while walking down the tree.
5894 * when wc->stage == UPDATE_BACKREF, this function updates
5895 * back refs for pointers in the block.
5897 * NOTE: return value 1 means we should stop walking down.
5899 static noinline
int walk_down_proc(struct btrfs_trans_handle
*trans
,
5900 struct btrfs_root
*root
,
5901 struct btrfs_path
*path
,
5902 struct walk_control
*wc
, int lookup_info
)
5904 int level
= wc
->level
;
5905 struct extent_buffer
*eb
= path
->nodes
[level
];
5906 u64 flag
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
5909 if (wc
->stage
== UPDATE_BACKREF
&&
5910 btrfs_header_owner(eb
) != root
->root_key
.objectid
)
5914 * when reference count of tree block is 1, it won't increase
5915 * again. once full backref flag is set, we never clear it.
5918 ((wc
->stage
== DROP_REFERENCE
&& wc
->refs
[level
] != 1) ||
5919 (wc
->stage
== UPDATE_BACKREF
&& !(wc
->flags
[level
] & flag
)))) {
5920 BUG_ON(!path
->locks
[level
]);
5921 ret
= btrfs_lookup_extent_info(trans
, root
,
5926 BUG_ON(wc
->refs
[level
] == 0);
5929 if (wc
->stage
== DROP_REFERENCE
) {
5930 if (wc
->refs
[level
] > 1)
5933 if (path
->locks
[level
] && !wc
->keep_locks
) {
5934 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
5935 path
->locks
[level
] = 0;
5940 /* wc->stage == UPDATE_BACKREF */
5941 if (!(wc
->flags
[level
] & flag
)) {
5942 BUG_ON(!path
->locks
[level
]);
5943 ret
= btrfs_inc_ref(trans
, root
, eb
, 1);
5945 ret
= btrfs_dec_ref(trans
, root
, eb
, 0);
5947 ret
= btrfs_set_disk_extent_flags(trans
, root
, eb
->start
,
5950 wc
->flags
[level
] |= flag
;
5954 * the block is shared by multiple trees, so it's not good to
5955 * keep the tree lock
5957 if (path
->locks
[level
] && level
> 0) {
5958 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
5959 path
->locks
[level
] = 0;
5965 * hepler to process tree block pointer.
5967 * when wc->stage == DROP_REFERENCE, this function checks
5968 * reference count of the block pointed to. if the block
5969 * is shared and we need update back refs for the subtree
5970 * rooted at the block, this function changes wc->stage to
5971 * UPDATE_BACKREF. if the block is shared and there is no
5972 * need to update back, this function drops the reference
5975 * NOTE: return value 1 means we should stop walking down.
5977 static noinline
int do_walk_down(struct btrfs_trans_handle
*trans
,
5978 struct btrfs_root
*root
,
5979 struct btrfs_path
*path
,
5980 struct walk_control
*wc
, int *lookup_info
)
5986 struct btrfs_key key
;
5987 struct extent_buffer
*next
;
5988 int level
= wc
->level
;
5992 generation
= btrfs_node_ptr_generation(path
->nodes
[level
],
5993 path
->slots
[level
]);
5995 * if the lower level block was created before the snapshot
5996 * was created, we know there is no need to update back refs
5999 if (wc
->stage
== UPDATE_BACKREF
&&
6000 generation
<= root
->root_key
.offset
) {
6005 bytenr
= btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]);
6006 blocksize
= btrfs_level_size(root
, level
- 1);
6008 next
= btrfs_find_tree_block(root
, bytenr
, blocksize
);
6010 next
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
6015 btrfs_tree_lock(next
);
6016 btrfs_set_lock_blocking(next
);
6018 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
, blocksize
,
6019 &wc
->refs
[level
- 1],
6020 &wc
->flags
[level
- 1]);
6022 BUG_ON(wc
->refs
[level
- 1] == 0);
6025 if (wc
->stage
== DROP_REFERENCE
) {
6026 if (wc
->refs
[level
- 1] > 1) {
6028 (wc
->flags
[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF
))
6031 if (!wc
->update_ref
||
6032 generation
<= root
->root_key
.offset
)
6035 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
6036 path
->slots
[level
]);
6037 ret
= btrfs_comp_cpu_keys(&key
, &wc
->update_progress
);
6041 wc
->stage
= UPDATE_BACKREF
;
6042 wc
->shared_level
= level
- 1;
6046 (wc
->flags
[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF
))
6050 if (!btrfs_buffer_uptodate(next
, generation
)) {
6051 btrfs_tree_unlock(next
);
6052 free_extent_buffer(next
);
6058 if (reada
&& level
== 1)
6059 reada_walk_down(trans
, root
, wc
, path
);
6060 next
= read_tree_block(root
, bytenr
, blocksize
, generation
);
6063 btrfs_tree_lock(next
);
6064 btrfs_set_lock_blocking(next
);
6068 BUG_ON(level
!= btrfs_header_level(next
));
6069 path
->nodes
[level
] = next
;
6070 path
->slots
[level
] = 0;
6071 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
6077 wc
->refs
[level
- 1] = 0;
6078 wc
->flags
[level
- 1] = 0;
6079 if (wc
->stage
== DROP_REFERENCE
) {
6080 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
) {
6081 parent
= path
->nodes
[level
]->start
;
6083 BUG_ON(root
->root_key
.objectid
!=
6084 btrfs_header_owner(path
->nodes
[level
]));
6088 ret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
, parent
,
6089 root
->root_key
.objectid
, level
- 1, 0);
6092 btrfs_tree_unlock(next
);
6093 free_extent_buffer(next
);
6099 * hepler to process tree block while walking up the tree.
6101 * when wc->stage == DROP_REFERENCE, this function drops
6102 * reference count on the block.
6104 * when wc->stage == UPDATE_BACKREF, this function changes
6105 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6106 * to UPDATE_BACKREF previously while processing the block.
6108 * NOTE: return value 1 means we should stop walking up.
6110 static noinline
int walk_up_proc(struct btrfs_trans_handle
*trans
,
6111 struct btrfs_root
*root
,
6112 struct btrfs_path
*path
,
6113 struct walk_control
*wc
)
6116 int level
= wc
->level
;
6117 struct extent_buffer
*eb
= path
->nodes
[level
];
6120 if (wc
->stage
== UPDATE_BACKREF
) {
6121 BUG_ON(wc
->shared_level
< level
);
6122 if (level
< wc
->shared_level
)
6125 ret
= find_next_key(path
, level
+ 1, &wc
->update_progress
);
6129 wc
->stage
= DROP_REFERENCE
;
6130 wc
->shared_level
= -1;
6131 path
->slots
[level
] = 0;
6134 * check reference count again if the block isn't locked.
6135 * we should start walking down the tree again if reference
6138 if (!path
->locks
[level
]) {
6140 btrfs_tree_lock(eb
);
6141 btrfs_set_lock_blocking(eb
);
6142 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
6144 ret
= btrfs_lookup_extent_info(trans
, root
,
6149 BUG_ON(wc
->refs
[level
] == 0);
6150 if (wc
->refs
[level
] == 1) {
6151 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
6157 /* wc->stage == DROP_REFERENCE */
6158 BUG_ON(wc
->refs
[level
] > 1 && !path
->locks
[level
]);
6160 if (wc
->refs
[level
] == 1) {
6162 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
6163 ret
= btrfs_dec_ref(trans
, root
, eb
, 1);
6165 ret
= btrfs_dec_ref(trans
, root
, eb
, 0);
6168 /* make block locked assertion in clean_tree_block happy */
6169 if (!path
->locks
[level
] &&
6170 btrfs_header_generation(eb
) == trans
->transid
) {
6171 btrfs_tree_lock(eb
);
6172 btrfs_set_lock_blocking(eb
);
6173 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
6175 clean_tree_block(trans
, root
, eb
);
6178 if (eb
== root
->node
) {
6179 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
6182 BUG_ON(root
->root_key
.objectid
!=
6183 btrfs_header_owner(eb
));
6185 if (wc
->flags
[level
+ 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
6186 parent
= path
->nodes
[level
+ 1]->start
;
6188 BUG_ON(root
->root_key
.objectid
!=
6189 btrfs_header_owner(path
->nodes
[level
+ 1]));
6192 btrfs_free_tree_block(trans
, root
, eb
, parent
, wc
->refs
[level
] == 1);
6194 wc
->refs
[level
] = 0;
6195 wc
->flags
[level
] = 0;
6199 static noinline
int walk_down_tree(struct btrfs_trans_handle
*trans
,
6200 struct btrfs_root
*root
,
6201 struct btrfs_path
*path
,
6202 struct walk_control
*wc
)
6204 int level
= wc
->level
;
6205 int lookup_info
= 1;
6208 while (level
>= 0) {
6209 ret
= walk_down_proc(trans
, root
, path
, wc
, lookup_info
);
6216 if (path
->slots
[level
] >=
6217 btrfs_header_nritems(path
->nodes
[level
]))
6220 ret
= do_walk_down(trans
, root
, path
, wc
, &lookup_info
);
6222 path
->slots
[level
]++;
6231 static noinline
int walk_up_tree(struct btrfs_trans_handle
*trans
,
6232 struct btrfs_root
*root
,
6233 struct btrfs_path
*path
,
6234 struct walk_control
*wc
, int max_level
)
6236 int level
= wc
->level
;
6239 path
->slots
[level
] = btrfs_header_nritems(path
->nodes
[level
]);
6240 while (level
< max_level
&& path
->nodes
[level
]) {
6242 if (path
->slots
[level
] + 1 <
6243 btrfs_header_nritems(path
->nodes
[level
])) {
6244 path
->slots
[level
]++;
6247 ret
= walk_up_proc(trans
, root
, path
, wc
);
6251 if (path
->locks
[level
]) {
6252 btrfs_tree_unlock_rw(path
->nodes
[level
],
6253 path
->locks
[level
]);
6254 path
->locks
[level
] = 0;
6256 free_extent_buffer(path
->nodes
[level
]);
6257 path
->nodes
[level
] = NULL
;
6265 * drop a subvolume tree.
6267 * this function traverses the tree freeing any blocks that only
6268 * referenced by the tree.
6270 * when a shared tree block is found. this function decreases its
6271 * reference count by one. if update_ref is true, this function
6272 * also make sure backrefs for the shared block and all lower level
6273 * blocks are properly updated.
6275 void btrfs_drop_snapshot(struct btrfs_root
*root
,
6276 struct btrfs_block_rsv
*block_rsv
, int update_ref
)
6278 struct btrfs_path
*path
;
6279 struct btrfs_trans_handle
*trans
;
6280 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
6281 struct btrfs_root_item
*root_item
= &root
->root_item
;
6282 struct walk_control
*wc
;
6283 struct btrfs_key key
;
6288 path
= btrfs_alloc_path();
6294 wc
= kzalloc(sizeof(*wc
), GFP_NOFS
);
6296 btrfs_free_path(path
);
6301 trans
= btrfs_start_transaction(tree_root
, 0);
6302 BUG_ON(IS_ERR(trans
));
6305 trans
->block_rsv
= block_rsv
;
6307 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
6308 level
= btrfs_header_level(root
->node
);
6309 path
->nodes
[level
] = btrfs_lock_root_node(root
);
6310 btrfs_set_lock_blocking(path
->nodes
[level
]);
6311 path
->slots
[level
] = 0;
6312 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
6313 memset(&wc
->update_progress
, 0,
6314 sizeof(wc
->update_progress
));
6316 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
6317 memcpy(&wc
->update_progress
, &key
,
6318 sizeof(wc
->update_progress
));
6320 level
= root_item
->drop_level
;
6322 path
->lowest_level
= level
;
6323 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
6324 path
->lowest_level
= 0;
6332 * unlock our path, this is safe because only this
6333 * function is allowed to delete this snapshot
6335 btrfs_unlock_up_safe(path
, 0);
6337 level
= btrfs_header_level(root
->node
);
6339 btrfs_tree_lock(path
->nodes
[level
]);
6340 btrfs_set_lock_blocking(path
->nodes
[level
]);
6342 ret
= btrfs_lookup_extent_info(trans
, root
,
6343 path
->nodes
[level
]->start
,
6344 path
->nodes
[level
]->len
,
6348 BUG_ON(wc
->refs
[level
] == 0);
6350 if (level
== root_item
->drop_level
)
6353 btrfs_tree_unlock(path
->nodes
[level
]);
6354 WARN_ON(wc
->refs
[level
] != 1);
6360 wc
->shared_level
= -1;
6361 wc
->stage
= DROP_REFERENCE
;
6362 wc
->update_ref
= update_ref
;
6364 wc
->reada_count
= BTRFS_NODEPTRS_PER_BLOCK(root
);
6367 ret
= walk_down_tree(trans
, root
, path
, wc
);
6373 ret
= walk_up_tree(trans
, root
, path
, wc
, BTRFS_MAX_LEVEL
);
6380 BUG_ON(wc
->stage
!= DROP_REFERENCE
);
6384 if (wc
->stage
== DROP_REFERENCE
) {
6386 btrfs_node_key(path
->nodes
[level
],
6387 &root_item
->drop_progress
,
6388 path
->slots
[level
]);
6389 root_item
->drop_level
= level
;
6392 BUG_ON(wc
->level
== 0);
6393 if (btrfs_should_end_transaction(trans
, tree_root
)) {
6394 ret
= btrfs_update_root(trans
, tree_root
,
6399 btrfs_end_transaction_throttle(trans
, tree_root
);
6400 trans
= btrfs_start_transaction(tree_root
, 0);
6401 BUG_ON(IS_ERR(trans
));
6403 trans
->block_rsv
= block_rsv
;
6406 btrfs_release_path(path
);
6409 ret
= btrfs_del_root(trans
, tree_root
, &root
->root_key
);
6412 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
6413 ret
= btrfs_find_last_root(tree_root
, root
->root_key
.objectid
,
6417 /* if we fail to delete the orphan item this time
6418 * around, it'll get picked up the next time.
6420 * The most common failure here is just -ENOENT.
6422 btrfs_del_orphan_item(trans
, tree_root
,
6423 root
->root_key
.objectid
);
6427 if (root
->in_radix
) {
6428 btrfs_free_fs_root(tree_root
->fs_info
, root
);
6430 free_extent_buffer(root
->node
);
6431 free_extent_buffer(root
->commit_root
);
6435 btrfs_end_transaction_throttle(trans
, tree_root
);
6437 btrfs_free_path(path
);
6440 btrfs_std_error(root
->fs_info
, err
);
6445 * drop subtree rooted at tree block 'node'.
6447 * NOTE: this function will unlock and release tree block 'node'
6449 int btrfs_drop_subtree(struct btrfs_trans_handle
*trans
,
6450 struct btrfs_root
*root
,
6451 struct extent_buffer
*node
,
6452 struct extent_buffer
*parent
)
6454 struct btrfs_path
*path
;
6455 struct walk_control
*wc
;
6461 BUG_ON(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
6463 path
= btrfs_alloc_path();
6467 wc
= kzalloc(sizeof(*wc
), GFP_NOFS
);
6469 btrfs_free_path(path
);
6473 btrfs_assert_tree_locked(parent
);
6474 parent_level
= btrfs_header_level(parent
);
6475 extent_buffer_get(parent
);
6476 path
->nodes
[parent_level
] = parent
;
6477 path
->slots
[parent_level
] = btrfs_header_nritems(parent
);
6479 btrfs_assert_tree_locked(node
);
6480 level
= btrfs_header_level(node
);
6481 path
->nodes
[level
] = node
;
6482 path
->slots
[level
] = 0;
6483 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
6485 wc
->refs
[parent_level
] = 1;
6486 wc
->flags
[parent_level
] = BTRFS_BLOCK_FLAG_FULL_BACKREF
;
6488 wc
->shared_level
= -1;
6489 wc
->stage
= DROP_REFERENCE
;
6492 wc
->reada_count
= BTRFS_NODEPTRS_PER_BLOCK(root
);
6495 wret
= walk_down_tree(trans
, root
, path
, wc
);
6501 wret
= walk_up_tree(trans
, root
, path
, wc
, parent_level
);
6509 btrfs_free_path(path
);
6513 static u64
update_block_group_flags(struct btrfs_root
*root
, u64 flags
)
6516 u64 stripped
= BTRFS_BLOCK_GROUP_RAID0
|
6517 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
;
6520 * we add in the count of missing devices because we want
6521 * to make sure that any RAID levels on a degraded FS
6522 * continue to be honored.
6524 num_devices
= root
->fs_info
->fs_devices
->rw_devices
+
6525 root
->fs_info
->fs_devices
->missing_devices
;
6527 if (num_devices
== 1) {
6528 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
6529 stripped
= flags
& ~stripped
;
6531 /* turn raid0 into single device chunks */
6532 if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
6535 /* turn mirroring into duplication */
6536 if (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
6537 BTRFS_BLOCK_GROUP_RAID10
))
6538 return stripped
| BTRFS_BLOCK_GROUP_DUP
;
6541 /* they already had raid on here, just return */
6542 if (flags
& stripped
)
6545 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
6546 stripped
= flags
& ~stripped
;
6548 /* switch duplicated blocks with raid1 */
6549 if (flags
& BTRFS_BLOCK_GROUP_DUP
)
6550 return stripped
| BTRFS_BLOCK_GROUP_RAID1
;
6552 /* turn single device chunks into raid0 */
6553 return stripped
| BTRFS_BLOCK_GROUP_RAID0
;
6558 static int set_block_group_ro(struct btrfs_block_group_cache
*cache
, int force
)
6560 struct btrfs_space_info
*sinfo
= cache
->space_info
;
6562 u64 min_allocable_bytes
;
6567 * We need some metadata space and system metadata space for
6568 * allocating chunks in some corner cases until we force to set
6569 * it to be readonly.
6572 (BTRFS_BLOCK_GROUP_SYSTEM
| BTRFS_BLOCK_GROUP_METADATA
)) &&
6574 min_allocable_bytes
= 1 * 1024 * 1024;
6576 min_allocable_bytes
= 0;
6578 spin_lock(&sinfo
->lock
);
6579 spin_lock(&cache
->lock
);
6586 num_bytes
= cache
->key
.offset
- cache
->reserved
- cache
->pinned
-
6587 cache
->bytes_super
- btrfs_block_group_used(&cache
->item
);
6589 if (sinfo
->bytes_used
+ sinfo
->bytes_reserved
+ sinfo
->bytes_pinned
+
6590 sinfo
->bytes_may_use
+ sinfo
->bytes_readonly
+ num_bytes
+
6591 min_allocable_bytes
<= sinfo
->total_bytes
) {
6592 sinfo
->bytes_readonly
+= num_bytes
;
6597 spin_unlock(&cache
->lock
);
6598 spin_unlock(&sinfo
->lock
);
6602 int btrfs_set_block_group_ro(struct btrfs_root
*root
,
6603 struct btrfs_block_group_cache
*cache
)
6606 struct btrfs_trans_handle
*trans
;
6612 trans
= btrfs_join_transaction(root
);
6613 BUG_ON(IS_ERR(trans
));
6615 alloc_flags
= update_block_group_flags(root
, cache
->flags
);
6616 if (alloc_flags
!= cache
->flags
)
6617 do_chunk_alloc(trans
, root
, 2 * 1024 * 1024, alloc_flags
,
6620 ret
= set_block_group_ro(cache
, 0);
6623 alloc_flags
= get_alloc_profile(root
, cache
->space_info
->flags
);
6624 ret
= do_chunk_alloc(trans
, root
, 2 * 1024 * 1024, alloc_flags
,
6628 ret
= set_block_group_ro(cache
, 0);
6630 btrfs_end_transaction(trans
, root
);
6634 int btrfs_force_chunk_alloc(struct btrfs_trans_handle
*trans
,
6635 struct btrfs_root
*root
, u64 type
)
6637 u64 alloc_flags
= get_alloc_profile(root
, type
);
6638 return do_chunk_alloc(trans
, root
, 2 * 1024 * 1024, alloc_flags
,
6643 * helper to account the unused space of all the readonly block group in the
6644 * list. takes mirrors into account.
6646 static u64
__btrfs_get_ro_block_group_free_space(struct list_head
*groups_list
)
6648 struct btrfs_block_group_cache
*block_group
;
6652 list_for_each_entry(block_group
, groups_list
, list
) {
6653 spin_lock(&block_group
->lock
);
6655 if (!block_group
->ro
) {
6656 spin_unlock(&block_group
->lock
);
6660 if (block_group
->flags
& (BTRFS_BLOCK_GROUP_RAID1
|
6661 BTRFS_BLOCK_GROUP_RAID10
|
6662 BTRFS_BLOCK_GROUP_DUP
))
6667 free_bytes
+= (block_group
->key
.offset
-
6668 btrfs_block_group_used(&block_group
->item
)) *
6671 spin_unlock(&block_group
->lock
);
6678 * helper to account the unused space of all the readonly block group in the
6679 * space_info. takes mirrors into account.
6681 u64
btrfs_account_ro_block_groups_free_space(struct btrfs_space_info
*sinfo
)
6686 spin_lock(&sinfo
->lock
);
6688 for(i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++)
6689 if (!list_empty(&sinfo
->block_groups
[i
]))
6690 free_bytes
+= __btrfs_get_ro_block_group_free_space(
6691 &sinfo
->block_groups
[i
]);
6693 spin_unlock(&sinfo
->lock
);
6698 int btrfs_set_block_group_rw(struct btrfs_root
*root
,
6699 struct btrfs_block_group_cache
*cache
)
6701 struct btrfs_space_info
*sinfo
= cache
->space_info
;
6706 spin_lock(&sinfo
->lock
);
6707 spin_lock(&cache
->lock
);
6708 num_bytes
= cache
->key
.offset
- cache
->reserved
- cache
->pinned
-
6709 cache
->bytes_super
- btrfs_block_group_used(&cache
->item
);
6710 sinfo
->bytes_readonly
-= num_bytes
;
6712 spin_unlock(&cache
->lock
);
6713 spin_unlock(&sinfo
->lock
);
6718 * checks to see if its even possible to relocate this block group.
6720 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
6721 * ok to go ahead and try.
6723 int btrfs_can_relocate(struct btrfs_root
*root
, u64 bytenr
)
6725 struct btrfs_block_group_cache
*block_group
;
6726 struct btrfs_space_info
*space_info
;
6727 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
6728 struct btrfs_device
*device
;
6736 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
6738 /* odd, couldn't find the block group, leave it alone */
6742 min_free
= btrfs_block_group_used(&block_group
->item
);
6744 /* no bytes used, we're good */
6748 space_info
= block_group
->space_info
;
6749 spin_lock(&space_info
->lock
);
6751 full
= space_info
->full
;
6754 * if this is the last block group we have in this space, we can't
6755 * relocate it unless we're able to allocate a new chunk below.
6757 * Otherwise, we need to make sure we have room in the space to handle
6758 * all of the extents from this block group. If we can, we're good
6760 if ((space_info
->total_bytes
!= block_group
->key
.offset
) &&
6761 (space_info
->bytes_used
+ space_info
->bytes_reserved
+
6762 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
6763 min_free
< space_info
->total_bytes
)) {
6764 spin_unlock(&space_info
->lock
);
6767 spin_unlock(&space_info
->lock
);
6770 * ok we don't have enough space, but maybe we have free space on our
6771 * devices to allocate new chunks for relocation, so loop through our
6772 * alloc devices and guess if we have enough space. However, if we
6773 * were marked as full, then we know there aren't enough chunks, and we
6788 index
= get_block_group_index(block_group
);
6793 } else if (index
== 1) {
6795 } else if (index
== 2) {
6798 } else if (index
== 3) {
6799 dev_min
= fs_devices
->rw_devices
;
6800 do_div(min_free
, dev_min
);
6803 mutex_lock(&root
->fs_info
->chunk_mutex
);
6804 list_for_each_entry(device
, &fs_devices
->alloc_list
, dev_alloc_list
) {
6808 * check to make sure we can actually find a chunk with enough
6809 * space to fit our block group in.
6811 if (device
->total_bytes
> device
->bytes_used
+ min_free
) {
6812 ret
= find_free_dev_extent(NULL
, device
, min_free
,
6817 if (dev_nr
>= dev_min
)
6823 mutex_unlock(&root
->fs_info
->chunk_mutex
);
6825 btrfs_put_block_group(block_group
);
6829 static int find_first_block_group(struct btrfs_root
*root
,
6830 struct btrfs_path
*path
, struct btrfs_key
*key
)
6833 struct btrfs_key found_key
;
6834 struct extent_buffer
*leaf
;
6837 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
6842 slot
= path
->slots
[0];
6843 leaf
= path
->nodes
[0];
6844 if (slot
>= btrfs_header_nritems(leaf
)) {
6845 ret
= btrfs_next_leaf(root
, path
);
6852 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
6854 if (found_key
.objectid
>= key
->objectid
&&
6855 found_key
.type
== BTRFS_BLOCK_GROUP_ITEM_KEY
) {
6865 void btrfs_put_block_group_cache(struct btrfs_fs_info
*info
)
6867 struct btrfs_block_group_cache
*block_group
;
6871 struct inode
*inode
;
6873 block_group
= btrfs_lookup_first_block_group(info
, last
);
6874 while (block_group
) {
6875 spin_lock(&block_group
->lock
);
6876 if (block_group
->iref
)
6878 spin_unlock(&block_group
->lock
);
6879 block_group
= next_block_group(info
->tree_root
,
6889 inode
= block_group
->inode
;
6890 block_group
->iref
= 0;
6891 block_group
->inode
= NULL
;
6892 spin_unlock(&block_group
->lock
);
6894 last
= block_group
->key
.objectid
+ block_group
->key
.offset
;
6895 btrfs_put_block_group(block_group
);
6899 int btrfs_free_block_groups(struct btrfs_fs_info
*info
)
6901 struct btrfs_block_group_cache
*block_group
;
6902 struct btrfs_space_info
*space_info
;
6903 struct btrfs_caching_control
*caching_ctl
;
6906 down_write(&info
->extent_commit_sem
);
6907 while (!list_empty(&info
->caching_block_groups
)) {
6908 caching_ctl
= list_entry(info
->caching_block_groups
.next
,
6909 struct btrfs_caching_control
, list
);
6910 list_del(&caching_ctl
->list
);
6911 put_caching_control(caching_ctl
);
6913 up_write(&info
->extent_commit_sem
);
6915 spin_lock(&info
->block_group_cache_lock
);
6916 while ((n
= rb_last(&info
->block_group_cache_tree
)) != NULL
) {
6917 block_group
= rb_entry(n
, struct btrfs_block_group_cache
,
6919 rb_erase(&block_group
->cache_node
,
6920 &info
->block_group_cache_tree
);
6921 spin_unlock(&info
->block_group_cache_lock
);
6923 down_write(&block_group
->space_info
->groups_sem
);
6924 list_del(&block_group
->list
);
6925 up_write(&block_group
->space_info
->groups_sem
);
6927 if (block_group
->cached
== BTRFS_CACHE_STARTED
)
6928 wait_block_group_cache_done(block_group
);
6931 * We haven't cached this block group, which means we could
6932 * possibly have excluded extents on this block group.
6934 if (block_group
->cached
== BTRFS_CACHE_NO
)
6935 free_excluded_extents(info
->extent_root
, block_group
);
6937 btrfs_remove_free_space_cache(block_group
);
6938 btrfs_put_block_group(block_group
);
6940 spin_lock(&info
->block_group_cache_lock
);
6942 spin_unlock(&info
->block_group_cache_lock
);
6944 /* now that all the block groups are freed, go through and
6945 * free all the space_info structs. This is only called during
6946 * the final stages of unmount, and so we know nobody is
6947 * using them. We call synchronize_rcu() once before we start,
6948 * just to be on the safe side.
6952 release_global_block_rsv(info
);
6954 while(!list_empty(&info
->space_info
)) {
6955 space_info
= list_entry(info
->space_info
.next
,
6956 struct btrfs_space_info
,
6958 if (space_info
->bytes_pinned
> 0 ||
6959 space_info
->bytes_reserved
> 0 ||
6960 space_info
->bytes_may_use
> 0) {
6962 dump_space_info(space_info
, 0, 0);
6964 list_del(&space_info
->list
);
6970 static void __link_block_group(struct btrfs_space_info
*space_info
,
6971 struct btrfs_block_group_cache
*cache
)
6973 int index
= get_block_group_index(cache
);
6975 down_write(&space_info
->groups_sem
);
6976 list_add_tail(&cache
->list
, &space_info
->block_groups
[index
]);
6977 up_write(&space_info
->groups_sem
);
6980 int btrfs_read_block_groups(struct btrfs_root
*root
)
6982 struct btrfs_path
*path
;
6984 struct btrfs_block_group_cache
*cache
;
6985 struct btrfs_fs_info
*info
= root
->fs_info
;
6986 struct btrfs_space_info
*space_info
;
6987 struct btrfs_key key
;
6988 struct btrfs_key found_key
;
6989 struct extent_buffer
*leaf
;
6993 root
= info
->extent_root
;
6996 btrfs_set_key_type(&key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
6997 path
= btrfs_alloc_path();
7002 cache_gen
= btrfs_super_cache_generation(&root
->fs_info
->super_copy
);
7003 if (cache_gen
!= 0 &&
7004 btrfs_super_generation(&root
->fs_info
->super_copy
) != cache_gen
)
7006 if (btrfs_test_opt(root
, CLEAR_CACHE
))
7008 if (!btrfs_test_opt(root
, SPACE_CACHE
) && cache_gen
)
7009 printk(KERN_INFO
"btrfs: disk space caching is enabled\n");
7012 ret
= find_first_block_group(root
, path
, &key
);
7017 leaf
= path
->nodes
[0];
7018 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
7019 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
7024 cache
->free_space_ctl
= kzalloc(sizeof(*cache
->free_space_ctl
),
7026 if (!cache
->free_space_ctl
) {
7032 atomic_set(&cache
->count
, 1);
7033 spin_lock_init(&cache
->lock
);
7034 cache
->fs_info
= info
;
7035 INIT_LIST_HEAD(&cache
->list
);
7036 INIT_LIST_HEAD(&cache
->cluster_list
);
7039 cache
->disk_cache_state
= BTRFS_DC_CLEAR
;
7041 read_extent_buffer(leaf
, &cache
->item
,
7042 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
7043 sizeof(cache
->item
));
7044 memcpy(&cache
->key
, &found_key
, sizeof(found_key
));
7046 key
.objectid
= found_key
.objectid
+ found_key
.offset
;
7047 btrfs_release_path(path
);
7048 cache
->flags
= btrfs_block_group_flags(&cache
->item
);
7049 cache
->sectorsize
= root
->sectorsize
;
7051 btrfs_init_free_space_ctl(cache
);
7054 * We need to exclude the super stripes now so that the space
7055 * info has super bytes accounted for, otherwise we'll think
7056 * we have more space than we actually do.
7058 exclude_super_stripes(root
, cache
);
7061 * check for two cases, either we are full, and therefore
7062 * don't need to bother with the caching work since we won't
7063 * find any space, or we are empty, and we can just add all
7064 * the space in and be done with it. This saves us _alot_ of
7065 * time, particularly in the full case.
7067 if (found_key
.offset
== btrfs_block_group_used(&cache
->item
)) {
7068 cache
->last_byte_to_unpin
= (u64
)-1;
7069 cache
->cached
= BTRFS_CACHE_FINISHED
;
7070 free_excluded_extents(root
, cache
);
7071 } else if (btrfs_block_group_used(&cache
->item
) == 0) {
7072 cache
->last_byte_to_unpin
= (u64
)-1;
7073 cache
->cached
= BTRFS_CACHE_FINISHED
;
7074 add_new_free_space(cache
, root
->fs_info
,
7076 found_key
.objectid
+
7078 free_excluded_extents(root
, cache
);
7081 ret
= update_space_info(info
, cache
->flags
, found_key
.offset
,
7082 btrfs_block_group_used(&cache
->item
),
7085 cache
->space_info
= space_info
;
7086 spin_lock(&cache
->space_info
->lock
);
7087 cache
->space_info
->bytes_readonly
+= cache
->bytes_super
;
7088 spin_unlock(&cache
->space_info
->lock
);
7090 __link_block_group(space_info
, cache
);
7092 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
7095 set_avail_alloc_bits(root
->fs_info
, cache
->flags
);
7096 if (btrfs_chunk_readonly(root
, cache
->key
.objectid
))
7097 set_block_group_ro(cache
, 1);
7100 list_for_each_entry_rcu(space_info
, &root
->fs_info
->space_info
, list
) {
7101 if (!(get_alloc_profile(root
, space_info
->flags
) &
7102 (BTRFS_BLOCK_GROUP_RAID10
|
7103 BTRFS_BLOCK_GROUP_RAID1
|
7104 BTRFS_BLOCK_GROUP_DUP
)))
7107 * avoid allocating from un-mirrored block group if there are
7108 * mirrored block groups.
7110 list_for_each_entry(cache
, &space_info
->block_groups
[3], list
)
7111 set_block_group_ro(cache
, 1);
7112 list_for_each_entry(cache
, &space_info
->block_groups
[4], list
)
7113 set_block_group_ro(cache
, 1);
7116 init_global_block_rsv(info
);
7119 btrfs_free_path(path
);
7123 int btrfs_make_block_group(struct btrfs_trans_handle
*trans
,
7124 struct btrfs_root
*root
, u64 bytes_used
,
7125 u64 type
, u64 chunk_objectid
, u64 chunk_offset
,
7129 struct btrfs_root
*extent_root
;
7130 struct btrfs_block_group_cache
*cache
;
7132 extent_root
= root
->fs_info
->extent_root
;
7134 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
7136 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
7139 cache
->free_space_ctl
= kzalloc(sizeof(*cache
->free_space_ctl
),
7141 if (!cache
->free_space_ctl
) {
7146 cache
->key
.objectid
= chunk_offset
;
7147 cache
->key
.offset
= size
;
7148 cache
->key
.type
= BTRFS_BLOCK_GROUP_ITEM_KEY
;
7149 cache
->sectorsize
= root
->sectorsize
;
7150 cache
->fs_info
= root
->fs_info
;
7152 atomic_set(&cache
->count
, 1);
7153 spin_lock_init(&cache
->lock
);
7154 INIT_LIST_HEAD(&cache
->list
);
7155 INIT_LIST_HEAD(&cache
->cluster_list
);
7157 btrfs_init_free_space_ctl(cache
);
7159 btrfs_set_block_group_used(&cache
->item
, bytes_used
);
7160 btrfs_set_block_group_chunk_objectid(&cache
->item
, chunk_objectid
);
7161 cache
->flags
= type
;
7162 btrfs_set_block_group_flags(&cache
->item
, type
);
7164 cache
->last_byte_to_unpin
= (u64
)-1;
7165 cache
->cached
= BTRFS_CACHE_FINISHED
;
7166 exclude_super_stripes(root
, cache
);
7168 add_new_free_space(cache
, root
->fs_info
, chunk_offset
,
7169 chunk_offset
+ size
);
7171 free_excluded_extents(root
, cache
);
7173 ret
= update_space_info(root
->fs_info
, cache
->flags
, size
, bytes_used
,
7174 &cache
->space_info
);
7177 spin_lock(&cache
->space_info
->lock
);
7178 cache
->space_info
->bytes_readonly
+= cache
->bytes_super
;
7179 spin_unlock(&cache
->space_info
->lock
);
7181 __link_block_group(cache
->space_info
, cache
);
7183 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
7186 ret
= btrfs_insert_item(trans
, extent_root
, &cache
->key
, &cache
->item
,
7187 sizeof(cache
->item
));
7190 set_avail_alloc_bits(extent_root
->fs_info
, type
);
7195 int btrfs_remove_block_group(struct btrfs_trans_handle
*trans
,
7196 struct btrfs_root
*root
, u64 group_start
)
7198 struct btrfs_path
*path
;
7199 struct btrfs_block_group_cache
*block_group
;
7200 struct btrfs_free_cluster
*cluster
;
7201 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
7202 struct btrfs_key key
;
7203 struct inode
*inode
;
7207 root
= root
->fs_info
->extent_root
;
7209 block_group
= btrfs_lookup_block_group(root
->fs_info
, group_start
);
7210 BUG_ON(!block_group
);
7211 BUG_ON(!block_group
->ro
);
7214 * Free the reserved super bytes from this block group before
7217 free_excluded_extents(root
, block_group
);
7219 memcpy(&key
, &block_group
->key
, sizeof(key
));
7220 if (block_group
->flags
& (BTRFS_BLOCK_GROUP_DUP
|
7221 BTRFS_BLOCK_GROUP_RAID1
|
7222 BTRFS_BLOCK_GROUP_RAID10
))
7227 /* make sure this block group isn't part of an allocation cluster */
7228 cluster
= &root
->fs_info
->data_alloc_cluster
;
7229 spin_lock(&cluster
->refill_lock
);
7230 btrfs_return_cluster_to_free_space(block_group
, cluster
);
7231 spin_unlock(&cluster
->refill_lock
);
7234 * make sure this block group isn't part of a metadata
7235 * allocation cluster
7237 cluster
= &root
->fs_info
->meta_alloc_cluster
;
7238 spin_lock(&cluster
->refill_lock
);
7239 btrfs_return_cluster_to_free_space(block_group
, cluster
);
7240 spin_unlock(&cluster
->refill_lock
);
7242 path
= btrfs_alloc_path();
7248 inode
= lookup_free_space_inode(root
, block_group
, path
);
7249 if (!IS_ERR(inode
)) {
7250 ret
= btrfs_orphan_add(trans
, inode
);
7253 /* One for the block groups ref */
7254 spin_lock(&block_group
->lock
);
7255 if (block_group
->iref
) {
7256 block_group
->iref
= 0;
7257 block_group
->inode
= NULL
;
7258 spin_unlock(&block_group
->lock
);
7261 spin_unlock(&block_group
->lock
);
7263 /* One for our lookup ref */
7267 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
7268 key
.offset
= block_group
->key
.objectid
;
7271 ret
= btrfs_search_slot(trans
, tree_root
, &key
, path
, -1, 1);
7275 btrfs_release_path(path
);
7277 ret
= btrfs_del_item(trans
, tree_root
, path
);
7280 btrfs_release_path(path
);
7283 spin_lock(&root
->fs_info
->block_group_cache_lock
);
7284 rb_erase(&block_group
->cache_node
,
7285 &root
->fs_info
->block_group_cache_tree
);
7286 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
7288 down_write(&block_group
->space_info
->groups_sem
);
7290 * we must use list_del_init so people can check to see if they
7291 * are still on the list after taking the semaphore
7293 list_del_init(&block_group
->list
);
7294 up_write(&block_group
->space_info
->groups_sem
);
7296 if (block_group
->cached
== BTRFS_CACHE_STARTED
)
7297 wait_block_group_cache_done(block_group
);
7299 btrfs_remove_free_space_cache(block_group
);
7301 spin_lock(&block_group
->space_info
->lock
);
7302 block_group
->space_info
->total_bytes
-= block_group
->key
.offset
;
7303 block_group
->space_info
->bytes_readonly
-= block_group
->key
.offset
;
7304 block_group
->space_info
->disk_total
-= block_group
->key
.offset
* factor
;
7305 spin_unlock(&block_group
->space_info
->lock
);
7307 memcpy(&key
, &block_group
->key
, sizeof(key
));
7309 btrfs_clear_space_info_full(root
->fs_info
);
7311 btrfs_put_block_group(block_group
);
7312 btrfs_put_block_group(block_group
);
7314 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
7320 ret
= btrfs_del_item(trans
, root
, path
);
7322 btrfs_free_path(path
);
7326 int btrfs_init_space_info(struct btrfs_fs_info
*fs_info
)
7328 struct btrfs_space_info
*space_info
;
7329 struct btrfs_super_block
*disk_super
;
7335 disk_super
= &fs_info
->super_copy
;
7336 if (!btrfs_super_root(disk_super
))
7339 features
= btrfs_super_incompat_flags(disk_super
);
7340 if (features
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
7343 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
7344 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
7349 flags
= BTRFS_BLOCK_GROUP_METADATA
| BTRFS_BLOCK_GROUP_DATA
;
7350 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
7352 flags
= BTRFS_BLOCK_GROUP_METADATA
;
7353 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
7357 flags
= BTRFS_BLOCK_GROUP_DATA
;
7358 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
7364 int btrfs_error_unpin_extent_range(struct btrfs_root
*root
, u64 start
, u64 end
)
7366 return unpin_extent_range(root
, start
, end
);
7369 int btrfs_error_discard_extent(struct btrfs_root
*root
, u64 bytenr
,
7370 u64 num_bytes
, u64
*actual_bytes
)
7372 return btrfs_discard_extent(root
, bytenr
, num_bytes
, actual_bytes
);
7375 int btrfs_trim_fs(struct btrfs_root
*root
, struct fstrim_range
*range
)
7377 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
7378 struct btrfs_block_group_cache
*cache
= NULL
;
7385 cache
= btrfs_lookup_block_group(fs_info
, range
->start
);
7388 if (cache
->key
.objectid
>= (range
->start
+ range
->len
)) {
7389 btrfs_put_block_group(cache
);
7393 start
= max(range
->start
, cache
->key
.objectid
);
7394 end
= min(range
->start
+ range
->len
,
7395 cache
->key
.objectid
+ cache
->key
.offset
);
7397 if (end
- start
>= range
->minlen
) {
7398 if (!block_group_cache_done(cache
)) {
7399 ret
= cache_block_group(cache
, NULL
, root
, 0);
7401 wait_block_group_cache_done(cache
);
7403 ret
= btrfs_trim_block_group(cache
,
7409 trimmed
+= group_trimmed
;
7411 btrfs_put_block_group(cache
);
7416 cache
= next_block_group(fs_info
->tree_root
, cache
);
7419 range
->len
= trimmed
;