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,
55 static int update_block_group(struct btrfs_trans_handle
*trans
,
56 struct btrfs_root
*root
,
57 u64 bytenr
, u64 num_bytes
, int alloc
);
58 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
59 struct btrfs_root
*root
,
60 u64 bytenr
, u64 num_bytes
, u64 parent
,
61 u64 root_objectid
, u64 owner_objectid
,
62 u64 owner_offset
, int refs_to_drop
,
63 struct btrfs_delayed_extent_op
*extra_op
);
64 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op
*extent_op
,
65 struct extent_buffer
*leaf
,
66 struct btrfs_extent_item
*ei
);
67 static int alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
68 struct btrfs_root
*root
,
69 u64 parent
, u64 root_objectid
,
70 u64 flags
, u64 owner
, u64 offset
,
71 struct btrfs_key
*ins
, int ref_mod
);
72 static int alloc_reserved_tree_block(struct btrfs_trans_handle
*trans
,
73 struct btrfs_root
*root
,
74 u64 parent
, u64 root_objectid
,
75 u64 flags
, struct btrfs_disk_key
*key
,
76 int level
, struct btrfs_key
*ins
);
77 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
78 struct btrfs_root
*extent_root
, u64 alloc_bytes
,
79 u64 flags
, int force
);
80 static int find_next_key(struct btrfs_path
*path
, int level
,
81 struct btrfs_key
*key
);
82 static void dump_space_info(struct btrfs_space_info
*info
, u64 bytes
,
83 int dump_block_groups
);
86 block_group_cache_done(struct btrfs_block_group_cache
*cache
)
89 return cache
->cached
== BTRFS_CACHE_FINISHED
;
92 static int block_group_bits(struct btrfs_block_group_cache
*cache
, u64 bits
)
94 return (cache
->flags
& bits
) == bits
;
97 static void btrfs_get_block_group(struct btrfs_block_group_cache
*cache
)
99 atomic_inc(&cache
->count
);
102 void btrfs_put_block_group(struct btrfs_block_group_cache
*cache
)
104 if (atomic_dec_and_test(&cache
->count
)) {
105 WARN_ON(cache
->pinned
> 0);
106 WARN_ON(cache
->reserved
> 0);
107 WARN_ON(cache
->reserved_pinned
> 0);
108 kfree(cache
->free_space_ctl
);
114 * this adds the block group to the fs_info rb tree for the block group
117 static int btrfs_add_block_group_cache(struct btrfs_fs_info
*info
,
118 struct btrfs_block_group_cache
*block_group
)
121 struct rb_node
*parent
= NULL
;
122 struct btrfs_block_group_cache
*cache
;
124 spin_lock(&info
->block_group_cache_lock
);
125 p
= &info
->block_group_cache_tree
.rb_node
;
129 cache
= rb_entry(parent
, struct btrfs_block_group_cache
,
131 if (block_group
->key
.objectid
< cache
->key
.objectid
) {
133 } else if (block_group
->key
.objectid
> cache
->key
.objectid
) {
136 spin_unlock(&info
->block_group_cache_lock
);
141 rb_link_node(&block_group
->cache_node
, parent
, p
);
142 rb_insert_color(&block_group
->cache_node
,
143 &info
->block_group_cache_tree
);
144 spin_unlock(&info
->block_group_cache_lock
);
150 * This will return the block group at or after bytenr if contains is 0, else
151 * it will return the block group that contains the bytenr
153 static struct btrfs_block_group_cache
*
154 block_group_cache_tree_search(struct btrfs_fs_info
*info
, u64 bytenr
,
157 struct btrfs_block_group_cache
*cache
, *ret
= NULL
;
161 spin_lock(&info
->block_group_cache_lock
);
162 n
= info
->block_group_cache_tree
.rb_node
;
165 cache
= rb_entry(n
, struct btrfs_block_group_cache
,
167 end
= cache
->key
.objectid
+ cache
->key
.offset
- 1;
168 start
= cache
->key
.objectid
;
170 if (bytenr
< start
) {
171 if (!contains
&& (!ret
|| start
< ret
->key
.objectid
))
174 } else if (bytenr
> start
) {
175 if (contains
&& bytenr
<= end
) {
186 btrfs_get_block_group(ret
);
187 spin_unlock(&info
->block_group_cache_lock
);
192 static int add_excluded_extent(struct btrfs_root
*root
,
193 u64 start
, u64 num_bytes
)
195 u64 end
= start
+ num_bytes
- 1;
196 set_extent_bits(&root
->fs_info
->freed_extents
[0],
197 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
198 set_extent_bits(&root
->fs_info
->freed_extents
[1],
199 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
203 static void free_excluded_extents(struct btrfs_root
*root
,
204 struct btrfs_block_group_cache
*cache
)
208 start
= cache
->key
.objectid
;
209 end
= start
+ cache
->key
.offset
- 1;
211 clear_extent_bits(&root
->fs_info
->freed_extents
[0],
212 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
213 clear_extent_bits(&root
->fs_info
->freed_extents
[1],
214 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
217 static int exclude_super_stripes(struct btrfs_root
*root
,
218 struct btrfs_block_group_cache
*cache
)
225 if (cache
->key
.objectid
< BTRFS_SUPER_INFO_OFFSET
) {
226 stripe_len
= BTRFS_SUPER_INFO_OFFSET
- cache
->key
.objectid
;
227 cache
->bytes_super
+= stripe_len
;
228 ret
= add_excluded_extent(root
, cache
->key
.objectid
,
233 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
234 bytenr
= btrfs_sb_offset(i
);
235 ret
= btrfs_rmap_block(&root
->fs_info
->mapping_tree
,
236 cache
->key
.objectid
, bytenr
,
237 0, &logical
, &nr
, &stripe_len
);
241 cache
->bytes_super
+= stripe_len
;
242 ret
= add_excluded_extent(root
, logical
[nr
],
252 static struct btrfs_caching_control
*
253 get_caching_control(struct btrfs_block_group_cache
*cache
)
255 struct btrfs_caching_control
*ctl
;
257 spin_lock(&cache
->lock
);
258 if (cache
->cached
!= BTRFS_CACHE_STARTED
) {
259 spin_unlock(&cache
->lock
);
263 /* We're loading it the fast way, so we don't have a caching_ctl. */
264 if (!cache
->caching_ctl
) {
265 spin_unlock(&cache
->lock
);
269 ctl
= cache
->caching_ctl
;
270 atomic_inc(&ctl
->count
);
271 spin_unlock(&cache
->lock
);
275 static void put_caching_control(struct btrfs_caching_control
*ctl
)
277 if (atomic_dec_and_test(&ctl
->count
))
282 * this is only called by cache_block_group, since we could have freed extents
283 * we need to check the pinned_extents for any extents that can't be used yet
284 * since their free space will be released as soon as the transaction commits.
286 static u64
add_new_free_space(struct btrfs_block_group_cache
*block_group
,
287 struct btrfs_fs_info
*info
, u64 start
, u64 end
)
289 u64 extent_start
, extent_end
, size
, total_added
= 0;
292 while (start
< end
) {
293 ret
= find_first_extent_bit(info
->pinned_extents
, start
,
294 &extent_start
, &extent_end
,
295 EXTENT_DIRTY
| EXTENT_UPTODATE
);
299 if (extent_start
<= start
) {
300 start
= extent_end
+ 1;
301 } else if (extent_start
> start
&& extent_start
< end
) {
302 size
= extent_start
- start
;
304 ret
= btrfs_add_free_space(block_group
, start
,
307 start
= extent_end
+ 1;
316 ret
= btrfs_add_free_space(block_group
, start
, size
);
323 static noinline
void caching_thread(struct btrfs_work
*work
)
325 struct btrfs_block_group_cache
*block_group
;
326 struct btrfs_fs_info
*fs_info
;
327 struct btrfs_caching_control
*caching_ctl
;
328 struct btrfs_root
*extent_root
;
329 struct btrfs_path
*path
;
330 struct extent_buffer
*leaf
;
331 struct btrfs_key key
;
337 caching_ctl
= container_of(work
, struct btrfs_caching_control
, work
);
338 block_group
= caching_ctl
->block_group
;
339 fs_info
= block_group
->fs_info
;
340 extent_root
= fs_info
->extent_root
;
342 path
= btrfs_alloc_path();
346 last
= max_t(u64
, block_group
->key
.objectid
, BTRFS_SUPER_INFO_OFFSET
);
349 * We don't want to deadlock with somebody trying to allocate a new
350 * extent for the extent root while also trying to search the extent
351 * root to add free space. So we skip locking and search the commit
352 * root, since its read-only
354 path
->skip_locking
= 1;
355 path
->search_commit_root
= 1;
360 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
362 mutex_lock(&caching_ctl
->mutex
);
363 /* need to make sure the commit_root doesn't disappear */
364 down_read(&fs_info
->extent_commit_sem
);
366 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
370 leaf
= path
->nodes
[0];
371 nritems
= btrfs_header_nritems(leaf
);
374 if (btrfs_fs_closing(fs_info
) > 1) {
379 if (path
->slots
[0] < nritems
) {
380 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
382 ret
= find_next_key(path
, 0, &key
);
386 if (need_resched() ||
387 btrfs_next_leaf(extent_root
, path
)) {
388 caching_ctl
->progress
= last
;
389 btrfs_release_path(path
);
390 up_read(&fs_info
->extent_commit_sem
);
391 mutex_unlock(&caching_ctl
->mutex
);
395 leaf
= path
->nodes
[0];
396 nritems
= btrfs_header_nritems(leaf
);
400 if (key
.objectid
< block_group
->key
.objectid
) {
405 if (key
.objectid
>= block_group
->key
.objectid
+
406 block_group
->key
.offset
)
409 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
410 total_found
+= add_new_free_space(block_group
,
413 last
= key
.objectid
+ key
.offset
;
415 if (total_found
> (1024 * 1024 * 2)) {
417 wake_up(&caching_ctl
->wait
);
424 total_found
+= add_new_free_space(block_group
, fs_info
, last
,
425 block_group
->key
.objectid
+
426 block_group
->key
.offset
);
427 caching_ctl
->progress
= (u64
)-1;
429 spin_lock(&block_group
->lock
);
430 block_group
->caching_ctl
= NULL
;
431 block_group
->cached
= BTRFS_CACHE_FINISHED
;
432 spin_unlock(&block_group
->lock
);
435 btrfs_free_path(path
);
436 up_read(&fs_info
->extent_commit_sem
);
438 free_excluded_extents(extent_root
, block_group
);
440 mutex_unlock(&caching_ctl
->mutex
);
442 wake_up(&caching_ctl
->wait
);
444 put_caching_control(caching_ctl
);
445 btrfs_put_block_group(block_group
);
448 static int cache_block_group(struct btrfs_block_group_cache
*cache
,
449 struct btrfs_trans_handle
*trans
,
450 struct btrfs_root
*root
,
453 struct btrfs_fs_info
*fs_info
= cache
->fs_info
;
454 struct btrfs_caching_control
*caching_ctl
;
458 if (cache
->cached
!= BTRFS_CACHE_NO
)
462 * We can't do the read from on-disk cache during a commit since we need
463 * to have the normal tree locking. Also if we are currently trying to
464 * allocate blocks for the tree root we can't do the fast caching since
465 * we likely hold important locks.
467 if (trans
&& (!trans
->transaction
->in_commit
) &&
468 (root
&& root
!= root
->fs_info
->tree_root
)) {
469 spin_lock(&cache
->lock
);
470 if (cache
->cached
!= BTRFS_CACHE_NO
) {
471 spin_unlock(&cache
->lock
);
474 cache
->cached
= BTRFS_CACHE_STARTED
;
475 spin_unlock(&cache
->lock
);
477 ret
= load_free_space_cache(fs_info
, cache
);
479 spin_lock(&cache
->lock
);
481 cache
->cached
= BTRFS_CACHE_FINISHED
;
482 cache
->last_byte_to_unpin
= (u64
)-1;
484 cache
->cached
= BTRFS_CACHE_NO
;
486 spin_unlock(&cache
->lock
);
488 free_excluded_extents(fs_info
->extent_root
, cache
);
496 caching_ctl
= kzalloc(sizeof(*caching_ctl
), GFP_NOFS
);
497 BUG_ON(!caching_ctl
);
499 INIT_LIST_HEAD(&caching_ctl
->list
);
500 mutex_init(&caching_ctl
->mutex
);
501 init_waitqueue_head(&caching_ctl
->wait
);
502 caching_ctl
->block_group
= cache
;
503 caching_ctl
->progress
= cache
->key
.objectid
;
504 /* one for caching kthread, one for caching block group list */
505 atomic_set(&caching_ctl
->count
, 2);
506 caching_ctl
->work
.func
= caching_thread
;
508 spin_lock(&cache
->lock
);
509 if (cache
->cached
!= BTRFS_CACHE_NO
) {
510 spin_unlock(&cache
->lock
);
514 cache
->caching_ctl
= caching_ctl
;
515 cache
->cached
= BTRFS_CACHE_STARTED
;
516 spin_unlock(&cache
->lock
);
518 down_write(&fs_info
->extent_commit_sem
);
519 list_add_tail(&caching_ctl
->list
, &fs_info
->caching_block_groups
);
520 up_write(&fs_info
->extent_commit_sem
);
522 btrfs_get_block_group(cache
);
524 btrfs_queue_worker(&fs_info
->caching_workers
, &caching_ctl
->work
);
530 * return the block group that starts at or after bytenr
532 static struct btrfs_block_group_cache
*
533 btrfs_lookup_first_block_group(struct btrfs_fs_info
*info
, u64 bytenr
)
535 struct btrfs_block_group_cache
*cache
;
537 cache
= block_group_cache_tree_search(info
, bytenr
, 0);
543 * return the block group that contains the given bytenr
545 struct btrfs_block_group_cache
*btrfs_lookup_block_group(
546 struct btrfs_fs_info
*info
,
549 struct btrfs_block_group_cache
*cache
;
551 cache
= block_group_cache_tree_search(info
, bytenr
, 1);
556 static struct btrfs_space_info
*__find_space_info(struct btrfs_fs_info
*info
,
559 struct list_head
*head
= &info
->space_info
;
560 struct btrfs_space_info
*found
;
562 flags
&= BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_SYSTEM
|
563 BTRFS_BLOCK_GROUP_METADATA
;
566 list_for_each_entry_rcu(found
, head
, list
) {
567 if (found
->flags
& flags
) {
577 * after adding space to the filesystem, we need to clear the full flags
578 * on all the space infos.
580 void btrfs_clear_space_info_full(struct btrfs_fs_info
*info
)
582 struct list_head
*head
= &info
->space_info
;
583 struct btrfs_space_info
*found
;
586 list_for_each_entry_rcu(found
, head
, list
)
591 static u64
div_factor(u64 num
, int factor
)
600 static u64
div_factor_fine(u64 num
, int factor
)
609 u64
btrfs_find_block_group(struct btrfs_root
*root
,
610 u64 search_start
, u64 search_hint
, int owner
)
612 struct btrfs_block_group_cache
*cache
;
614 u64 last
= max(search_hint
, search_start
);
621 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
625 spin_lock(&cache
->lock
);
626 last
= cache
->key
.objectid
+ cache
->key
.offset
;
627 used
= btrfs_block_group_used(&cache
->item
);
629 if ((full_search
|| !cache
->ro
) &&
630 block_group_bits(cache
, BTRFS_BLOCK_GROUP_METADATA
)) {
631 if (used
+ cache
->pinned
+ cache
->reserved
<
632 div_factor(cache
->key
.offset
, factor
)) {
633 group_start
= cache
->key
.objectid
;
634 spin_unlock(&cache
->lock
);
635 btrfs_put_block_group(cache
);
639 spin_unlock(&cache
->lock
);
640 btrfs_put_block_group(cache
);
648 if (!full_search
&& factor
< 10) {
658 /* simple helper to search for an existing extent at a given offset */
659 int btrfs_lookup_extent(struct btrfs_root
*root
, u64 start
, u64 len
)
662 struct btrfs_key key
;
663 struct btrfs_path
*path
;
665 path
= btrfs_alloc_path();
667 key
.objectid
= start
;
669 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
670 ret
= btrfs_search_slot(NULL
, root
->fs_info
->extent_root
, &key
, path
,
672 btrfs_free_path(path
);
677 * helper function to lookup reference count and flags of extent.
679 * the head node for delayed ref is used to store the sum of all the
680 * reference count modifications queued up in the rbtree. the head
681 * node may also store the extent flags to set. This way you can check
682 * to see what the reference count and extent flags would be if all of
683 * the delayed refs are not processed.
685 int btrfs_lookup_extent_info(struct btrfs_trans_handle
*trans
,
686 struct btrfs_root
*root
, u64 bytenr
,
687 u64 num_bytes
, u64
*refs
, u64
*flags
)
689 struct btrfs_delayed_ref_head
*head
;
690 struct btrfs_delayed_ref_root
*delayed_refs
;
691 struct btrfs_path
*path
;
692 struct btrfs_extent_item
*ei
;
693 struct extent_buffer
*leaf
;
694 struct btrfs_key key
;
700 path
= btrfs_alloc_path();
704 key
.objectid
= bytenr
;
705 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
706 key
.offset
= num_bytes
;
708 path
->skip_locking
= 1;
709 path
->search_commit_root
= 1;
712 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
,
718 leaf
= path
->nodes
[0];
719 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
720 if (item_size
>= sizeof(*ei
)) {
721 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
722 struct btrfs_extent_item
);
723 num_refs
= btrfs_extent_refs(leaf
, ei
);
724 extent_flags
= btrfs_extent_flags(leaf
, ei
);
726 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
727 struct btrfs_extent_item_v0
*ei0
;
728 BUG_ON(item_size
!= sizeof(*ei0
));
729 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
730 struct btrfs_extent_item_v0
);
731 num_refs
= btrfs_extent_refs_v0(leaf
, ei0
);
732 /* FIXME: this isn't correct for data */
733 extent_flags
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
738 BUG_ON(num_refs
== 0);
748 delayed_refs
= &trans
->transaction
->delayed_refs
;
749 spin_lock(&delayed_refs
->lock
);
750 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
752 if (!mutex_trylock(&head
->mutex
)) {
753 atomic_inc(&head
->node
.refs
);
754 spin_unlock(&delayed_refs
->lock
);
756 btrfs_release_path(path
);
759 * Mutex was contended, block until it's released and try
762 mutex_lock(&head
->mutex
);
763 mutex_unlock(&head
->mutex
);
764 btrfs_put_delayed_ref(&head
->node
);
767 if (head
->extent_op
&& head
->extent_op
->update_flags
)
768 extent_flags
|= head
->extent_op
->flags_to_set
;
770 BUG_ON(num_refs
== 0);
772 num_refs
+= head
->node
.ref_mod
;
773 mutex_unlock(&head
->mutex
);
775 spin_unlock(&delayed_refs
->lock
);
777 WARN_ON(num_refs
== 0);
781 *flags
= extent_flags
;
783 btrfs_free_path(path
);
788 * Back reference rules. Back refs have three main goals:
790 * 1) differentiate between all holders of references to an extent so that
791 * when a reference is dropped we can make sure it was a valid reference
792 * before freeing the extent.
794 * 2) Provide enough information to quickly find the holders of an extent
795 * if we notice a given block is corrupted or bad.
797 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
798 * maintenance. This is actually the same as #2, but with a slightly
799 * different use case.
801 * There are two kinds of back refs. The implicit back refs is optimized
802 * for pointers in non-shared tree blocks. For a given pointer in a block,
803 * back refs of this kind provide information about the block's owner tree
804 * and the pointer's key. These information allow us to find the block by
805 * b-tree searching. The full back refs is for pointers in tree blocks not
806 * referenced by their owner trees. The location of tree block is recorded
807 * in the back refs. Actually the full back refs is generic, and can be
808 * used in all cases the implicit back refs is used. The major shortcoming
809 * of the full back refs is its overhead. Every time a tree block gets
810 * COWed, we have to update back refs entry for all pointers in it.
812 * For a newly allocated tree block, we use implicit back refs for
813 * pointers in it. This means most tree related operations only involve
814 * implicit back refs. For a tree block created in old transaction, the
815 * only way to drop a reference to it is COW it. So we can detect the
816 * event that tree block loses its owner tree's reference and do the
817 * back refs conversion.
819 * When a tree block is COW'd through a tree, there are four cases:
821 * The reference count of the block is one and the tree is the block's
822 * owner tree. Nothing to do in this case.
824 * The reference count of the block is one and the tree is not the
825 * block's owner tree. In this case, full back refs is used for pointers
826 * in the block. Remove these full back refs, add implicit back refs for
827 * every pointers in the new block.
829 * The reference count of the block is greater than one and the tree is
830 * the block's owner tree. In this case, implicit back refs is used for
831 * pointers in the block. Add full back refs for every pointers in the
832 * block, increase lower level extents' reference counts. The original
833 * implicit back refs are entailed to the new block.
835 * The reference count of the block is greater than one and the tree is
836 * not the block's owner tree. Add implicit back refs for every pointer in
837 * the new block, increase lower level extents' reference count.
839 * Back Reference Key composing:
841 * The key objectid corresponds to the first byte in the extent,
842 * The key type is used to differentiate between types of back refs.
843 * There are different meanings of the key offset for different types
846 * File extents can be referenced by:
848 * - multiple snapshots, subvolumes, or different generations in one subvol
849 * - different files inside a single subvolume
850 * - different offsets inside a file (bookend extents in file.c)
852 * The extent ref structure for the implicit back refs has fields for:
854 * - Objectid of the subvolume root
855 * - objectid of the file holding the reference
856 * - original offset in the file
857 * - how many bookend extents
859 * The key offset for the implicit back refs is hash of the first
862 * The extent ref structure for the full back refs has field for:
864 * - number of pointers in the tree leaf
866 * The key offset for the implicit back refs is the first byte of
869 * When a file extent is allocated, The implicit back refs is used.
870 * the fields are filled in:
872 * (root_key.objectid, inode objectid, offset in file, 1)
874 * When a file extent is removed file truncation, we find the
875 * corresponding implicit back refs and check the following fields:
877 * (btrfs_header_owner(leaf), inode objectid, offset in file)
879 * Btree extents can be referenced by:
881 * - Different subvolumes
883 * Both the implicit back refs and the full back refs for tree blocks
884 * only consist of key. The key offset for the implicit back refs is
885 * objectid of block's owner tree. The key offset for the full back refs
886 * is the first byte of parent block.
888 * When implicit back refs is used, information about the lowest key and
889 * level of the tree block are required. These information are stored in
890 * tree block info structure.
893 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
894 static int convert_extent_item_v0(struct btrfs_trans_handle
*trans
,
895 struct btrfs_root
*root
,
896 struct btrfs_path
*path
,
897 u64 owner
, u32 extra_size
)
899 struct btrfs_extent_item
*item
;
900 struct btrfs_extent_item_v0
*ei0
;
901 struct btrfs_extent_ref_v0
*ref0
;
902 struct btrfs_tree_block_info
*bi
;
903 struct extent_buffer
*leaf
;
904 struct btrfs_key key
;
905 struct btrfs_key found_key
;
906 u32 new_size
= sizeof(*item
);
910 leaf
= path
->nodes
[0];
911 BUG_ON(btrfs_item_size_nr(leaf
, path
->slots
[0]) != sizeof(*ei0
));
913 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
914 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
915 struct btrfs_extent_item_v0
);
916 refs
= btrfs_extent_refs_v0(leaf
, ei0
);
918 if (owner
== (u64
)-1) {
920 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
921 ret
= btrfs_next_leaf(root
, path
);
925 leaf
= path
->nodes
[0];
927 btrfs_item_key_to_cpu(leaf
, &found_key
,
929 BUG_ON(key
.objectid
!= found_key
.objectid
);
930 if (found_key
.type
!= BTRFS_EXTENT_REF_V0_KEY
) {
934 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
935 struct btrfs_extent_ref_v0
);
936 owner
= btrfs_ref_objectid_v0(leaf
, ref0
);
940 btrfs_release_path(path
);
942 if (owner
< BTRFS_FIRST_FREE_OBJECTID
)
943 new_size
+= sizeof(*bi
);
945 new_size
-= sizeof(*ei0
);
946 ret
= btrfs_search_slot(trans
, root
, &key
, path
,
947 new_size
+ extra_size
, 1);
952 ret
= btrfs_extend_item(trans
, root
, path
, new_size
);
954 leaf
= path
->nodes
[0];
955 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
956 btrfs_set_extent_refs(leaf
, item
, refs
);
957 /* FIXME: get real generation */
958 btrfs_set_extent_generation(leaf
, item
, 0);
959 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
960 btrfs_set_extent_flags(leaf
, item
,
961 BTRFS_EXTENT_FLAG_TREE_BLOCK
|
962 BTRFS_BLOCK_FLAG_FULL_BACKREF
);
963 bi
= (struct btrfs_tree_block_info
*)(item
+ 1);
964 /* FIXME: get first key of the block */
965 memset_extent_buffer(leaf
, 0, (unsigned long)bi
, sizeof(*bi
));
966 btrfs_set_tree_block_level(leaf
, bi
, (int)owner
);
968 btrfs_set_extent_flags(leaf
, item
, BTRFS_EXTENT_FLAG_DATA
);
970 btrfs_mark_buffer_dirty(leaf
);
975 static u64
hash_extent_data_ref(u64 root_objectid
, u64 owner
, u64 offset
)
977 u32 high_crc
= ~(u32
)0;
978 u32 low_crc
= ~(u32
)0;
981 lenum
= cpu_to_le64(root_objectid
);
982 high_crc
= crc32c(high_crc
, &lenum
, sizeof(lenum
));
983 lenum
= cpu_to_le64(owner
);
984 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
985 lenum
= cpu_to_le64(offset
);
986 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
988 return ((u64
)high_crc
<< 31) ^ (u64
)low_crc
;
991 static u64
hash_extent_data_ref_item(struct extent_buffer
*leaf
,
992 struct btrfs_extent_data_ref
*ref
)
994 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf
, ref
),
995 btrfs_extent_data_ref_objectid(leaf
, ref
),
996 btrfs_extent_data_ref_offset(leaf
, ref
));
999 static int match_extent_data_ref(struct extent_buffer
*leaf
,
1000 struct btrfs_extent_data_ref
*ref
,
1001 u64 root_objectid
, u64 owner
, u64 offset
)
1003 if (btrfs_extent_data_ref_root(leaf
, ref
) != root_objectid
||
1004 btrfs_extent_data_ref_objectid(leaf
, ref
) != owner
||
1005 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
1010 static noinline
int lookup_extent_data_ref(struct btrfs_trans_handle
*trans
,
1011 struct btrfs_root
*root
,
1012 struct btrfs_path
*path
,
1013 u64 bytenr
, u64 parent
,
1015 u64 owner
, u64 offset
)
1017 struct btrfs_key key
;
1018 struct btrfs_extent_data_ref
*ref
;
1019 struct extent_buffer
*leaf
;
1025 key
.objectid
= bytenr
;
1027 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
1028 key
.offset
= parent
;
1030 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
1031 key
.offset
= hash_extent_data_ref(root_objectid
,
1036 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1045 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1046 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
1047 btrfs_release_path(path
);
1048 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1059 leaf
= path
->nodes
[0];
1060 nritems
= btrfs_header_nritems(leaf
);
1062 if (path
->slots
[0] >= nritems
) {
1063 ret
= btrfs_next_leaf(root
, path
);
1069 leaf
= path
->nodes
[0];
1070 nritems
= btrfs_header_nritems(leaf
);
1074 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1075 if (key
.objectid
!= bytenr
||
1076 key
.type
!= BTRFS_EXTENT_DATA_REF_KEY
)
1079 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1080 struct btrfs_extent_data_ref
);
1082 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
1085 btrfs_release_path(path
);
1097 static noinline
int insert_extent_data_ref(struct btrfs_trans_handle
*trans
,
1098 struct btrfs_root
*root
,
1099 struct btrfs_path
*path
,
1100 u64 bytenr
, u64 parent
,
1101 u64 root_objectid
, u64 owner
,
1102 u64 offset
, int refs_to_add
)
1104 struct btrfs_key key
;
1105 struct extent_buffer
*leaf
;
1110 key
.objectid
= bytenr
;
1112 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
1113 key
.offset
= parent
;
1114 size
= sizeof(struct btrfs_shared_data_ref
);
1116 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
1117 key
.offset
= hash_extent_data_ref(root_objectid
,
1119 size
= sizeof(struct btrfs_extent_data_ref
);
1122 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, size
);
1123 if (ret
&& ret
!= -EEXIST
)
1126 leaf
= path
->nodes
[0];
1128 struct btrfs_shared_data_ref
*ref
;
1129 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1130 struct btrfs_shared_data_ref
);
1132 btrfs_set_shared_data_ref_count(leaf
, ref
, refs_to_add
);
1134 num_refs
= btrfs_shared_data_ref_count(leaf
, ref
);
1135 num_refs
+= refs_to_add
;
1136 btrfs_set_shared_data_ref_count(leaf
, ref
, num_refs
);
1139 struct btrfs_extent_data_ref
*ref
;
1140 while (ret
== -EEXIST
) {
1141 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1142 struct btrfs_extent_data_ref
);
1143 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
1146 btrfs_release_path(path
);
1148 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1150 if (ret
&& ret
!= -EEXIST
)
1153 leaf
= path
->nodes
[0];
1155 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1156 struct btrfs_extent_data_ref
);
1158 btrfs_set_extent_data_ref_root(leaf
, ref
,
1160 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
1161 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
1162 btrfs_set_extent_data_ref_count(leaf
, ref
, refs_to_add
);
1164 num_refs
= btrfs_extent_data_ref_count(leaf
, ref
);
1165 num_refs
+= refs_to_add
;
1166 btrfs_set_extent_data_ref_count(leaf
, ref
, num_refs
);
1169 btrfs_mark_buffer_dirty(leaf
);
1172 btrfs_release_path(path
);
1176 static noinline
int remove_extent_data_ref(struct btrfs_trans_handle
*trans
,
1177 struct btrfs_root
*root
,
1178 struct btrfs_path
*path
,
1181 struct btrfs_key key
;
1182 struct btrfs_extent_data_ref
*ref1
= NULL
;
1183 struct btrfs_shared_data_ref
*ref2
= NULL
;
1184 struct extent_buffer
*leaf
;
1188 leaf
= path
->nodes
[0];
1189 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1191 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1192 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
1193 struct btrfs_extent_data_ref
);
1194 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1195 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
1196 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
1197 struct btrfs_shared_data_ref
);
1198 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1199 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1200 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
1201 struct btrfs_extent_ref_v0
*ref0
;
1202 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1203 struct btrfs_extent_ref_v0
);
1204 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
1210 BUG_ON(num_refs
< refs_to_drop
);
1211 num_refs
-= refs_to_drop
;
1213 if (num_refs
== 0) {
1214 ret
= btrfs_del_item(trans
, root
, path
);
1216 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
)
1217 btrfs_set_extent_data_ref_count(leaf
, ref1
, num_refs
);
1218 else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
)
1219 btrfs_set_shared_data_ref_count(leaf
, ref2
, num_refs
);
1220 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1222 struct btrfs_extent_ref_v0
*ref0
;
1223 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1224 struct btrfs_extent_ref_v0
);
1225 btrfs_set_ref_count_v0(leaf
, ref0
, num_refs
);
1228 btrfs_mark_buffer_dirty(leaf
);
1233 static noinline u32
extent_data_ref_count(struct btrfs_root
*root
,
1234 struct btrfs_path
*path
,
1235 struct btrfs_extent_inline_ref
*iref
)
1237 struct btrfs_key key
;
1238 struct extent_buffer
*leaf
;
1239 struct btrfs_extent_data_ref
*ref1
;
1240 struct btrfs_shared_data_ref
*ref2
;
1243 leaf
= path
->nodes
[0];
1244 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1246 if (btrfs_extent_inline_ref_type(leaf
, iref
) ==
1247 BTRFS_EXTENT_DATA_REF_KEY
) {
1248 ref1
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1249 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1251 ref2
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1252 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1254 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1255 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
1256 struct btrfs_extent_data_ref
);
1257 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1258 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
1259 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
1260 struct btrfs_shared_data_ref
);
1261 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1262 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1263 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
1264 struct btrfs_extent_ref_v0
*ref0
;
1265 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1266 struct btrfs_extent_ref_v0
);
1267 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
1275 static noinline
int lookup_tree_block_ref(struct btrfs_trans_handle
*trans
,
1276 struct btrfs_root
*root
,
1277 struct btrfs_path
*path
,
1278 u64 bytenr
, u64 parent
,
1281 struct btrfs_key key
;
1284 key
.objectid
= bytenr
;
1286 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1287 key
.offset
= parent
;
1289 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
1290 key
.offset
= root_objectid
;
1293 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1296 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1297 if (ret
== -ENOENT
&& parent
) {
1298 btrfs_release_path(path
);
1299 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
1300 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1308 static noinline
int insert_tree_block_ref(struct btrfs_trans_handle
*trans
,
1309 struct btrfs_root
*root
,
1310 struct btrfs_path
*path
,
1311 u64 bytenr
, u64 parent
,
1314 struct btrfs_key key
;
1317 key
.objectid
= bytenr
;
1319 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1320 key
.offset
= parent
;
1322 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
1323 key
.offset
= root_objectid
;
1326 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, 0);
1327 btrfs_release_path(path
);
1331 static inline int extent_ref_type(u64 parent
, u64 owner
)
1334 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1336 type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1338 type
= BTRFS_TREE_BLOCK_REF_KEY
;
1341 type
= BTRFS_SHARED_DATA_REF_KEY
;
1343 type
= BTRFS_EXTENT_DATA_REF_KEY
;
1348 static int find_next_key(struct btrfs_path
*path
, int level
,
1349 struct btrfs_key
*key
)
1352 for (; level
< BTRFS_MAX_LEVEL
; level
++) {
1353 if (!path
->nodes
[level
])
1355 if (path
->slots
[level
] + 1 >=
1356 btrfs_header_nritems(path
->nodes
[level
]))
1359 btrfs_item_key_to_cpu(path
->nodes
[level
], key
,
1360 path
->slots
[level
] + 1);
1362 btrfs_node_key_to_cpu(path
->nodes
[level
], key
,
1363 path
->slots
[level
] + 1);
1370 * look for inline back ref. if back ref is found, *ref_ret is set
1371 * to the address of inline back ref, and 0 is returned.
1373 * if back ref isn't found, *ref_ret is set to the address where it
1374 * should be inserted, and -ENOENT is returned.
1376 * if insert is true and there are too many inline back refs, the path
1377 * points to the extent item, and -EAGAIN is returned.
1379 * NOTE: inline back refs are ordered in the same way that back ref
1380 * items in the tree are ordered.
1382 static noinline_for_stack
1383 int lookup_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1384 struct btrfs_root
*root
,
1385 struct btrfs_path
*path
,
1386 struct btrfs_extent_inline_ref
**ref_ret
,
1387 u64 bytenr
, u64 num_bytes
,
1388 u64 parent
, u64 root_objectid
,
1389 u64 owner
, u64 offset
, int insert
)
1391 struct btrfs_key key
;
1392 struct extent_buffer
*leaf
;
1393 struct btrfs_extent_item
*ei
;
1394 struct btrfs_extent_inline_ref
*iref
;
1405 key
.objectid
= bytenr
;
1406 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1407 key
.offset
= num_bytes
;
1409 want
= extent_ref_type(parent
, owner
);
1411 extra_size
= btrfs_extent_inline_ref_size(want
);
1412 path
->keep_locks
= 1;
1415 ret
= btrfs_search_slot(trans
, root
, &key
, path
, extra_size
, 1);
1422 leaf
= path
->nodes
[0];
1423 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1424 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1425 if (item_size
< sizeof(*ei
)) {
1430 ret
= convert_extent_item_v0(trans
, root
, path
, owner
,
1436 leaf
= path
->nodes
[0];
1437 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1440 BUG_ON(item_size
< sizeof(*ei
));
1442 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1443 flags
= btrfs_extent_flags(leaf
, ei
);
1445 ptr
= (unsigned long)(ei
+ 1);
1446 end
= (unsigned long)ei
+ item_size
;
1448 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
1449 ptr
+= sizeof(struct btrfs_tree_block_info
);
1452 BUG_ON(!(flags
& BTRFS_EXTENT_FLAG_DATA
));
1461 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1462 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1466 ptr
+= btrfs_extent_inline_ref_size(type
);
1470 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1471 struct btrfs_extent_data_ref
*dref
;
1472 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1473 if (match_extent_data_ref(leaf
, dref
, root_objectid
,
1478 if (hash_extent_data_ref_item(leaf
, dref
) <
1479 hash_extent_data_ref(root_objectid
, owner
, offset
))
1483 ref_offset
= btrfs_extent_inline_ref_offset(leaf
, iref
);
1485 if (parent
== ref_offset
) {
1489 if (ref_offset
< parent
)
1492 if (root_objectid
== ref_offset
) {
1496 if (ref_offset
< root_objectid
)
1500 ptr
+= btrfs_extent_inline_ref_size(type
);
1502 if (err
== -ENOENT
&& insert
) {
1503 if (item_size
+ extra_size
>=
1504 BTRFS_MAX_EXTENT_ITEM_SIZE(root
)) {
1509 * To add new inline back ref, we have to make sure
1510 * there is no corresponding back ref item.
1511 * For simplicity, we just do not add new inline back
1512 * ref if there is any kind of item for this block
1514 if (find_next_key(path
, 0, &key
) == 0 &&
1515 key
.objectid
== bytenr
&&
1516 key
.type
< BTRFS_BLOCK_GROUP_ITEM_KEY
) {
1521 *ref_ret
= (struct btrfs_extent_inline_ref
*)ptr
;
1524 path
->keep_locks
= 0;
1525 btrfs_unlock_up_safe(path
, 1);
1531 * helper to add new inline back ref
1533 static noinline_for_stack
1534 int setup_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1535 struct btrfs_root
*root
,
1536 struct btrfs_path
*path
,
1537 struct btrfs_extent_inline_ref
*iref
,
1538 u64 parent
, u64 root_objectid
,
1539 u64 owner
, u64 offset
, int refs_to_add
,
1540 struct btrfs_delayed_extent_op
*extent_op
)
1542 struct extent_buffer
*leaf
;
1543 struct btrfs_extent_item
*ei
;
1546 unsigned long item_offset
;
1552 leaf
= path
->nodes
[0];
1553 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1554 item_offset
= (unsigned long)iref
- (unsigned long)ei
;
1556 type
= extent_ref_type(parent
, owner
);
1557 size
= btrfs_extent_inline_ref_size(type
);
1559 ret
= btrfs_extend_item(trans
, root
, path
, size
);
1561 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1562 refs
= btrfs_extent_refs(leaf
, ei
);
1563 refs
+= refs_to_add
;
1564 btrfs_set_extent_refs(leaf
, ei
, refs
);
1566 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1568 ptr
= (unsigned long)ei
+ item_offset
;
1569 end
= (unsigned long)ei
+ btrfs_item_size_nr(leaf
, path
->slots
[0]);
1570 if (ptr
< end
- size
)
1571 memmove_extent_buffer(leaf
, ptr
+ size
, ptr
,
1574 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1575 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
1576 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1577 struct btrfs_extent_data_ref
*dref
;
1578 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1579 btrfs_set_extent_data_ref_root(leaf
, dref
, root_objectid
);
1580 btrfs_set_extent_data_ref_objectid(leaf
, dref
, owner
);
1581 btrfs_set_extent_data_ref_offset(leaf
, dref
, offset
);
1582 btrfs_set_extent_data_ref_count(leaf
, dref
, refs_to_add
);
1583 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1584 struct btrfs_shared_data_ref
*sref
;
1585 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1586 btrfs_set_shared_data_ref_count(leaf
, sref
, refs_to_add
);
1587 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1588 } else if (type
== BTRFS_SHARED_BLOCK_REF_KEY
) {
1589 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1591 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
1593 btrfs_mark_buffer_dirty(leaf
);
1597 static int lookup_extent_backref(struct btrfs_trans_handle
*trans
,
1598 struct btrfs_root
*root
,
1599 struct btrfs_path
*path
,
1600 struct btrfs_extent_inline_ref
**ref_ret
,
1601 u64 bytenr
, u64 num_bytes
, u64 parent
,
1602 u64 root_objectid
, u64 owner
, u64 offset
)
1606 ret
= lookup_inline_extent_backref(trans
, root
, path
, ref_ret
,
1607 bytenr
, num_bytes
, parent
,
1608 root_objectid
, owner
, offset
, 0);
1612 btrfs_release_path(path
);
1615 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1616 ret
= lookup_tree_block_ref(trans
, root
, path
, bytenr
, parent
,
1619 ret
= lookup_extent_data_ref(trans
, root
, path
, bytenr
, parent
,
1620 root_objectid
, owner
, offset
);
1626 * helper to update/remove inline back ref
1628 static noinline_for_stack
1629 int update_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1630 struct btrfs_root
*root
,
1631 struct btrfs_path
*path
,
1632 struct btrfs_extent_inline_ref
*iref
,
1634 struct btrfs_delayed_extent_op
*extent_op
)
1636 struct extent_buffer
*leaf
;
1637 struct btrfs_extent_item
*ei
;
1638 struct btrfs_extent_data_ref
*dref
= NULL
;
1639 struct btrfs_shared_data_ref
*sref
= NULL
;
1648 leaf
= path
->nodes
[0];
1649 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1650 refs
= btrfs_extent_refs(leaf
, ei
);
1651 WARN_ON(refs_to_mod
< 0 && refs
+ refs_to_mod
<= 0);
1652 refs
+= refs_to_mod
;
1653 btrfs_set_extent_refs(leaf
, ei
, refs
);
1655 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1657 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1659 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1660 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1661 refs
= btrfs_extent_data_ref_count(leaf
, dref
);
1662 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1663 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1664 refs
= btrfs_shared_data_ref_count(leaf
, sref
);
1667 BUG_ON(refs_to_mod
!= -1);
1670 BUG_ON(refs_to_mod
< 0 && refs
< -refs_to_mod
);
1671 refs
+= refs_to_mod
;
1674 if (type
== BTRFS_EXTENT_DATA_REF_KEY
)
1675 btrfs_set_extent_data_ref_count(leaf
, dref
, refs
);
1677 btrfs_set_shared_data_ref_count(leaf
, sref
, refs
);
1679 size
= btrfs_extent_inline_ref_size(type
);
1680 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1681 ptr
= (unsigned long)iref
;
1682 end
= (unsigned long)ei
+ item_size
;
1683 if (ptr
+ size
< end
)
1684 memmove_extent_buffer(leaf
, ptr
, ptr
+ size
,
1687 ret
= btrfs_truncate_item(trans
, root
, path
, item_size
, 1);
1689 btrfs_mark_buffer_dirty(leaf
);
1693 static noinline_for_stack
1694 int insert_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1695 struct btrfs_root
*root
,
1696 struct btrfs_path
*path
,
1697 u64 bytenr
, u64 num_bytes
, u64 parent
,
1698 u64 root_objectid
, u64 owner
,
1699 u64 offset
, int refs_to_add
,
1700 struct btrfs_delayed_extent_op
*extent_op
)
1702 struct btrfs_extent_inline_ref
*iref
;
1705 ret
= lookup_inline_extent_backref(trans
, root
, path
, &iref
,
1706 bytenr
, num_bytes
, parent
,
1707 root_objectid
, owner
, offset
, 1);
1709 BUG_ON(owner
< BTRFS_FIRST_FREE_OBJECTID
);
1710 ret
= update_inline_extent_backref(trans
, root
, path
, iref
,
1711 refs_to_add
, extent_op
);
1712 } else if (ret
== -ENOENT
) {
1713 ret
= setup_inline_extent_backref(trans
, root
, path
, iref
,
1714 parent
, root_objectid
,
1715 owner
, offset
, refs_to_add
,
1721 static int insert_extent_backref(struct btrfs_trans_handle
*trans
,
1722 struct btrfs_root
*root
,
1723 struct btrfs_path
*path
,
1724 u64 bytenr
, u64 parent
, u64 root_objectid
,
1725 u64 owner
, u64 offset
, int refs_to_add
)
1728 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1729 BUG_ON(refs_to_add
!= 1);
1730 ret
= insert_tree_block_ref(trans
, root
, path
, bytenr
,
1731 parent
, root_objectid
);
1733 ret
= insert_extent_data_ref(trans
, root
, path
, bytenr
,
1734 parent
, root_objectid
,
1735 owner
, offset
, refs_to_add
);
1740 static int remove_extent_backref(struct btrfs_trans_handle
*trans
,
1741 struct btrfs_root
*root
,
1742 struct btrfs_path
*path
,
1743 struct btrfs_extent_inline_ref
*iref
,
1744 int refs_to_drop
, int is_data
)
1748 BUG_ON(!is_data
&& refs_to_drop
!= 1);
1750 ret
= update_inline_extent_backref(trans
, root
, path
, iref
,
1751 -refs_to_drop
, NULL
);
1752 } else if (is_data
) {
1753 ret
= remove_extent_data_ref(trans
, root
, path
, refs_to_drop
);
1755 ret
= btrfs_del_item(trans
, root
, path
);
1760 static int btrfs_issue_discard(struct block_device
*bdev
,
1763 return blkdev_issue_discard(bdev
, start
>> 9, len
>> 9, GFP_NOFS
, 0);
1766 static int btrfs_discard_extent(struct btrfs_root
*root
, u64 bytenr
,
1767 u64 num_bytes
, u64
*actual_bytes
)
1770 u64 discarded_bytes
= 0;
1771 struct btrfs_multi_bio
*multi
= NULL
;
1774 /* Tell the block device(s) that the sectors can be discarded */
1775 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, REQ_DISCARD
,
1776 bytenr
, &num_bytes
, &multi
, 0);
1778 struct btrfs_bio_stripe
*stripe
= multi
->stripes
;
1782 for (i
= 0; i
< multi
->num_stripes
; i
++, stripe
++) {
1783 ret
= btrfs_issue_discard(stripe
->dev
->bdev
,
1787 discarded_bytes
+= stripe
->length
;
1788 else if (ret
!= -EOPNOTSUPP
)
1793 if (discarded_bytes
&& ret
== -EOPNOTSUPP
)
1797 *actual_bytes
= discarded_bytes
;
1803 int btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
1804 struct btrfs_root
*root
,
1805 u64 bytenr
, u64 num_bytes
, u64 parent
,
1806 u64 root_objectid
, u64 owner
, u64 offset
)
1809 BUG_ON(owner
< BTRFS_FIRST_FREE_OBJECTID
&&
1810 root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
1812 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1813 ret
= btrfs_add_delayed_tree_ref(trans
, bytenr
, num_bytes
,
1814 parent
, root_objectid
, (int)owner
,
1815 BTRFS_ADD_DELAYED_REF
, NULL
);
1817 ret
= btrfs_add_delayed_data_ref(trans
, bytenr
, num_bytes
,
1818 parent
, root_objectid
, owner
, offset
,
1819 BTRFS_ADD_DELAYED_REF
, NULL
);
1824 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
1825 struct btrfs_root
*root
,
1826 u64 bytenr
, u64 num_bytes
,
1827 u64 parent
, u64 root_objectid
,
1828 u64 owner
, u64 offset
, int refs_to_add
,
1829 struct btrfs_delayed_extent_op
*extent_op
)
1831 struct btrfs_path
*path
;
1832 struct extent_buffer
*leaf
;
1833 struct btrfs_extent_item
*item
;
1838 path
= btrfs_alloc_path();
1843 path
->leave_spinning
= 1;
1844 /* this will setup the path even if it fails to insert the back ref */
1845 ret
= insert_inline_extent_backref(trans
, root
->fs_info
->extent_root
,
1846 path
, bytenr
, num_bytes
, parent
,
1847 root_objectid
, owner
, offset
,
1848 refs_to_add
, extent_op
);
1852 if (ret
!= -EAGAIN
) {
1857 leaf
= path
->nodes
[0];
1858 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1859 refs
= btrfs_extent_refs(leaf
, item
);
1860 btrfs_set_extent_refs(leaf
, item
, refs
+ refs_to_add
);
1862 __run_delayed_extent_op(extent_op
, leaf
, item
);
1864 btrfs_mark_buffer_dirty(leaf
);
1865 btrfs_release_path(path
);
1868 path
->leave_spinning
= 1;
1870 /* now insert the actual backref */
1871 ret
= insert_extent_backref(trans
, root
->fs_info
->extent_root
,
1872 path
, bytenr
, parent
, root_objectid
,
1873 owner
, offset
, refs_to_add
);
1876 btrfs_free_path(path
);
1880 static int run_delayed_data_ref(struct btrfs_trans_handle
*trans
,
1881 struct btrfs_root
*root
,
1882 struct btrfs_delayed_ref_node
*node
,
1883 struct btrfs_delayed_extent_op
*extent_op
,
1884 int insert_reserved
)
1887 struct btrfs_delayed_data_ref
*ref
;
1888 struct btrfs_key ins
;
1893 ins
.objectid
= node
->bytenr
;
1894 ins
.offset
= node
->num_bytes
;
1895 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1897 ref
= btrfs_delayed_node_to_data_ref(node
);
1898 if (node
->type
== BTRFS_SHARED_DATA_REF_KEY
)
1899 parent
= ref
->parent
;
1901 ref_root
= ref
->root
;
1903 if (node
->action
== BTRFS_ADD_DELAYED_REF
&& insert_reserved
) {
1905 BUG_ON(extent_op
->update_key
);
1906 flags
|= extent_op
->flags_to_set
;
1908 ret
= alloc_reserved_file_extent(trans
, root
,
1909 parent
, ref_root
, flags
,
1910 ref
->objectid
, ref
->offset
,
1911 &ins
, node
->ref_mod
);
1912 } else if (node
->action
== BTRFS_ADD_DELAYED_REF
) {
1913 ret
= __btrfs_inc_extent_ref(trans
, root
, node
->bytenr
,
1914 node
->num_bytes
, parent
,
1915 ref_root
, ref
->objectid
,
1916 ref
->offset
, node
->ref_mod
,
1918 } else if (node
->action
== BTRFS_DROP_DELAYED_REF
) {
1919 ret
= __btrfs_free_extent(trans
, root
, node
->bytenr
,
1920 node
->num_bytes
, parent
,
1921 ref_root
, ref
->objectid
,
1922 ref
->offset
, node
->ref_mod
,
1930 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op
*extent_op
,
1931 struct extent_buffer
*leaf
,
1932 struct btrfs_extent_item
*ei
)
1934 u64 flags
= btrfs_extent_flags(leaf
, ei
);
1935 if (extent_op
->update_flags
) {
1936 flags
|= extent_op
->flags_to_set
;
1937 btrfs_set_extent_flags(leaf
, ei
, flags
);
1940 if (extent_op
->update_key
) {
1941 struct btrfs_tree_block_info
*bi
;
1942 BUG_ON(!(flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
));
1943 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
1944 btrfs_set_tree_block_key(leaf
, bi
, &extent_op
->key
);
1948 static int run_delayed_extent_op(struct btrfs_trans_handle
*trans
,
1949 struct btrfs_root
*root
,
1950 struct btrfs_delayed_ref_node
*node
,
1951 struct btrfs_delayed_extent_op
*extent_op
)
1953 struct btrfs_key key
;
1954 struct btrfs_path
*path
;
1955 struct btrfs_extent_item
*ei
;
1956 struct extent_buffer
*leaf
;
1961 path
= btrfs_alloc_path();
1965 key
.objectid
= node
->bytenr
;
1966 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1967 key
.offset
= node
->num_bytes
;
1970 path
->leave_spinning
= 1;
1971 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
,
1982 leaf
= path
->nodes
[0];
1983 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1984 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1985 if (item_size
< sizeof(*ei
)) {
1986 ret
= convert_extent_item_v0(trans
, root
->fs_info
->extent_root
,
1992 leaf
= path
->nodes
[0];
1993 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1996 BUG_ON(item_size
< sizeof(*ei
));
1997 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1998 __run_delayed_extent_op(extent_op
, leaf
, ei
);
2000 btrfs_mark_buffer_dirty(leaf
);
2002 btrfs_free_path(path
);
2006 static int run_delayed_tree_ref(struct btrfs_trans_handle
*trans
,
2007 struct btrfs_root
*root
,
2008 struct btrfs_delayed_ref_node
*node
,
2009 struct btrfs_delayed_extent_op
*extent_op
,
2010 int insert_reserved
)
2013 struct btrfs_delayed_tree_ref
*ref
;
2014 struct btrfs_key ins
;
2018 ins
.objectid
= node
->bytenr
;
2019 ins
.offset
= node
->num_bytes
;
2020 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
2022 ref
= btrfs_delayed_node_to_tree_ref(node
);
2023 if (node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
)
2024 parent
= ref
->parent
;
2026 ref_root
= ref
->root
;
2028 BUG_ON(node
->ref_mod
!= 1);
2029 if (node
->action
== BTRFS_ADD_DELAYED_REF
&& insert_reserved
) {
2030 BUG_ON(!extent_op
|| !extent_op
->update_flags
||
2031 !extent_op
->update_key
);
2032 ret
= alloc_reserved_tree_block(trans
, root
,
2034 extent_op
->flags_to_set
,
2037 } else if (node
->action
== BTRFS_ADD_DELAYED_REF
) {
2038 ret
= __btrfs_inc_extent_ref(trans
, root
, node
->bytenr
,
2039 node
->num_bytes
, parent
, ref_root
,
2040 ref
->level
, 0, 1, extent_op
);
2041 } else if (node
->action
== BTRFS_DROP_DELAYED_REF
) {
2042 ret
= __btrfs_free_extent(trans
, root
, node
->bytenr
,
2043 node
->num_bytes
, parent
, ref_root
,
2044 ref
->level
, 0, 1, extent_op
);
2051 /* helper function to actually process a single delayed ref entry */
2052 static int run_one_delayed_ref(struct btrfs_trans_handle
*trans
,
2053 struct btrfs_root
*root
,
2054 struct btrfs_delayed_ref_node
*node
,
2055 struct btrfs_delayed_extent_op
*extent_op
,
2056 int insert_reserved
)
2059 if (btrfs_delayed_ref_is_head(node
)) {
2060 struct btrfs_delayed_ref_head
*head
;
2062 * we've hit the end of the chain and we were supposed
2063 * to insert this extent into the tree. But, it got
2064 * deleted before we ever needed to insert it, so all
2065 * we have to do is clean up the accounting
2068 head
= btrfs_delayed_node_to_head(node
);
2069 if (insert_reserved
) {
2070 btrfs_pin_extent(root
, node
->bytenr
,
2071 node
->num_bytes
, 1);
2072 if (head
->is_data
) {
2073 ret
= btrfs_del_csums(trans
, root
,
2079 mutex_unlock(&head
->mutex
);
2083 if (node
->type
== BTRFS_TREE_BLOCK_REF_KEY
||
2084 node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
)
2085 ret
= run_delayed_tree_ref(trans
, root
, node
, extent_op
,
2087 else if (node
->type
== BTRFS_EXTENT_DATA_REF_KEY
||
2088 node
->type
== BTRFS_SHARED_DATA_REF_KEY
)
2089 ret
= run_delayed_data_ref(trans
, root
, node
, extent_op
,
2096 static noinline
struct btrfs_delayed_ref_node
*
2097 select_delayed_ref(struct btrfs_delayed_ref_head
*head
)
2099 struct rb_node
*node
;
2100 struct btrfs_delayed_ref_node
*ref
;
2101 int action
= BTRFS_ADD_DELAYED_REF
;
2104 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2105 * this prevents ref count from going down to zero when
2106 * there still are pending delayed ref.
2108 node
= rb_prev(&head
->node
.rb_node
);
2112 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
,
2114 if (ref
->bytenr
!= head
->node
.bytenr
)
2116 if (ref
->action
== action
)
2118 node
= rb_prev(node
);
2120 if (action
== BTRFS_ADD_DELAYED_REF
) {
2121 action
= BTRFS_DROP_DELAYED_REF
;
2127 static noinline
int run_clustered_refs(struct btrfs_trans_handle
*trans
,
2128 struct btrfs_root
*root
,
2129 struct list_head
*cluster
)
2131 struct btrfs_delayed_ref_root
*delayed_refs
;
2132 struct btrfs_delayed_ref_node
*ref
;
2133 struct btrfs_delayed_ref_head
*locked_ref
= NULL
;
2134 struct btrfs_delayed_extent_op
*extent_op
;
2137 int must_insert_reserved
= 0;
2139 delayed_refs
= &trans
->transaction
->delayed_refs
;
2142 /* pick a new head ref from the cluster list */
2143 if (list_empty(cluster
))
2146 locked_ref
= list_entry(cluster
->next
,
2147 struct btrfs_delayed_ref_head
, cluster
);
2149 /* grab the lock that says we are going to process
2150 * all the refs for this head */
2151 ret
= btrfs_delayed_ref_lock(trans
, locked_ref
);
2154 * we may have dropped the spin lock to get the head
2155 * mutex lock, and that might have given someone else
2156 * time to free the head. If that's true, it has been
2157 * removed from our list and we can move on.
2159 if (ret
== -EAGAIN
) {
2167 * record the must insert reserved flag before we
2168 * drop the spin lock.
2170 must_insert_reserved
= locked_ref
->must_insert_reserved
;
2171 locked_ref
->must_insert_reserved
= 0;
2173 extent_op
= locked_ref
->extent_op
;
2174 locked_ref
->extent_op
= NULL
;
2177 * locked_ref is the head node, so we have to go one
2178 * node back for any delayed ref updates
2180 ref
= select_delayed_ref(locked_ref
);
2182 /* All delayed refs have been processed, Go ahead
2183 * and send the head node to run_one_delayed_ref,
2184 * so that any accounting fixes can happen
2186 ref
= &locked_ref
->node
;
2188 if (extent_op
&& must_insert_reserved
) {
2194 spin_unlock(&delayed_refs
->lock
);
2196 ret
= run_delayed_extent_op(trans
, root
,
2202 spin_lock(&delayed_refs
->lock
);
2206 list_del_init(&locked_ref
->cluster
);
2211 rb_erase(&ref
->rb_node
, &delayed_refs
->root
);
2212 delayed_refs
->num_entries
--;
2214 spin_unlock(&delayed_refs
->lock
);
2216 ret
= run_one_delayed_ref(trans
, root
, ref
, extent_op
,
2217 must_insert_reserved
);
2220 btrfs_put_delayed_ref(ref
);
2225 spin_lock(&delayed_refs
->lock
);
2231 * this starts processing the delayed reference count updates and
2232 * extent insertions we have queued up so far. count can be
2233 * 0, which means to process everything in the tree at the start
2234 * of the run (but not newly added entries), or it can be some target
2235 * number you'd like to process.
2237 int btrfs_run_delayed_refs(struct btrfs_trans_handle
*trans
,
2238 struct btrfs_root
*root
, unsigned long count
)
2240 struct rb_node
*node
;
2241 struct btrfs_delayed_ref_root
*delayed_refs
;
2242 struct btrfs_delayed_ref_node
*ref
;
2243 struct list_head cluster
;
2245 int run_all
= count
== (unsigned long)-1;
2248 if (root
== root
->fs_info
->extent_root
)
2249 root
= root
->fs_info
->tree_root
;
2251 delayed_refs
= &trans
->transaction
->delayed_refs
;
2252 INIT_LIST_HEAD(&cluster
);
2254 spin_lock(&delayed_refs
->lock
);
2256 count
= delayed_refs
->num_entries
* 2;
2260 if (!(run_all
|| run_most
) &&
2261 delayed_refs
->num_heads_ready
< 64)
2265 * go find something we can process in the rbtree. We start at
2266 * the beginning of the tree, and then build a cluster
2267 * of refs to process starting at the first one we are able to
2270 ret
= btrfs_find_ref_cluster(trans
, &cluster
,
2271 delayed_refs
->run_delayed_start
);
2275 ret
= run_clustered_refs(trans
, root
, &cluster
);
2278 count
-= min_t(unsigned long, ret
, count
);
2285 node
= rb_first(&delayed_refs
->root
);
2288 count
= (unsigned long)-1;
2291 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
,
2293 if (btrfs_delayed_ref_is_head(ref
)) {
2294 struct btrfs_delayed_ref_head
*head
;
2296 head
= btrfs_delayed_node_to_head(ref
);
2297 atomic_inc(&ref
->refs
);
2299 spin_unlock(&delayed_refs
->lock
);
2301 * Mutex was contended, block until it's
2302 * released and try again
2304 mutex_lock(&head
->mutex
);
2305 mutex_unlock(&head
->mutex
);
2307 btrfs_put_delayed_ref(ref
);
2311 node
= rb_next(node
);
2313 spin_unlock(&delayed_refs
->lock
);
2314 schedule_timeout(1);
2318 spin_unlock(&delayed_refs
->lock
);
2322 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle
*trans
,
2323 struct btrfs_root
*root
,
2324 u64 bytenr
, u64 num_bytes
, u64 flags
,
2327 struct btrfs_delayed_extent_op
*extent_op
;
2330 extent_op
= kmalloc(sizeof(*extent_op
), GFP_NOFS
);
2334 extent_op
->flags_to_set
= flags
;
2335 extent_op
->update_flags
= 1;
2336 extent_op
->update_key
= 0;
2337 extent_op
->is_data
= is_data
? 1 : 0;
2339 ret
= btrfs_add_delayed_extent_op(trans
, bytenr
, num_bytes
, extent_op
);
2345 static noinline
int check_delayed_ref(struct btrfs_trans_handle
*trans
,
2346 struct btrfs_root
*root
,
2347 struct btrfs_path
*path
,
2348 u64 objectid
, u64 offset
, u64 bytenr
)
2350 struct btrfs_delayed_ref_head
*head
;
2351 struct btrfs_delayed_ref_node
*ref
;
2352 struct btrfs_delayed_data_ref
*data_ref
;
2353 struct btrfs_delayed_ref_root
*delayed_refs
;
2354 struct rb_node
*node
;
2358 delayed_refs
= &trans
->transaction
->delayed_refs
;
2359 spin_lock(&delayed_refs
->lock
);
2360 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
2364 if (!mutex_trylock(&head
->mutex
)) {
2365 atomic_inc(&head
->node
.refs
);
2366 spin_unlock(&delayed_refs
->lock
);
2368 btrfs_release_path(path
);
2371 * Mutex was contended, block until it's released and let
2374 mutex_lock(&head
->mutex
);
2375 mutex_unlock(&head
->mutex
);
2376 btrfs_put_delayed_ref(&head
->node
);
2380 node
= rb_prev(&head
->node
.rb_node
);
2384 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
2386 if (ref
->bytenr
!= bytenr
)
2390 if (ref
->type
!= BTRFS_EXTENT_DATA_REF_KEY
)
2393 data_ref
= btrfs_delayed_node_to_data_ref(ref
);
2395 node
= rb_prev(node
);
2397 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
2398 if (ref
->bytenr
== bytenr
)
2402 if (data_ref
->root
!= root
->root_key
.objectid
||
2403 data_ref
->objectid
!= objectid
|| data_ref
->offset
!= offset
)
2408 mutex_unlock(&head
->mutex
);
2410 spin_unlock(&delayed_refs
->lock
);
2414 static noinline
int check_committed_ref(struct btrfs_trans_handle
*trans
,
2415 struct btrfs_root
*root
,
2416 struct btrfs_path
*path
,
2417 u64 objectid
, u64 offset
, u64 bytenr
)
2419 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
2420 struct extent_buffer
*leaf
;
2421 struct btrfs_extent_data_ref
*ref
;
2422 struct btrfs_extent_inline_ref
*iref
;
2423 struct btrfs_extent_item
*ei
;
2424 struct btrfs_key key
;
2428 key
.objectid
= bytenr
;
2429 key
.offset
= (u64
)-1;
2430 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2432 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
2438 if (path
->slots
[0] == 0)
2442 leaf
= path
->nodes
[0];
2443 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2445 if (key
.objectid
!= bytenr
|| key
.type
!= BTRFS_EXTENT_ITEM_KEY
)
2449 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2450 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2451 if (item_size
< sizeof(*ei
)) {
2452 WARN_ON(item_size
!= sizeof(struct btrfs_extent_item_v0
));
2456 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
2458 if (item_size
!= sizeof(*ei
) +
2459 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY
))
2462 if (btrfs_extent_generation(leaf
, ei
) <=
2463 btrfs_root_last_snapshot(&root
->root_item
))
2466 iref
= (struct btrfs_extent_inline_ref
*)(ei
+ 1);
2467 if (btrfs_extent_inline_ref_type(leaf
, iref
) !=
2468 BTRFS_EXTENT_DATA_REF_KEY
)
2471 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
2472 if (btrfs_extent_refs(leaf
, ei
) !=
2473 btrfs_extent_data_ref_count(leaf
, ref
) ||
2474 btrfs_extent_data_ref_root(leaf
, ref
) !=
2475 root
->root_key
.objectid
||
2476 btrfs_extent_data_ref_objectid(leaf
, ref
) != objectid
||
2477 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
2485 int btrfs_cross_ref_exist(struct btrfs_trans_handle
*trans
,
2486 struct btrfs_root
*root
,
2487 u64 objectid
, u64 offset
, u64 bytenr
)
2489 struct btrfs_path
*path
;
2493 path
= btrfs_alloc_path();
2498 ret
= check_committed_ref(trans
, root
, path
, objectid
,
2500 if (ret
&& ret
!= -ENOENT
)
2503 ret2
= check_delayed_ref(trans
, root
, path
, objectid
,
2505 } while (ret2
== -EAGAIN
);
2507 if (ret2
&& ret2
!= -ENOENT
) {
2512 if (ret
!= -ENOENT
|| ret2
!= -ENOENT
)
2515 btrfs_free_path(path
);
2516 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
2521 static int __btrfs_mod_ref(struct btrfs_trans_handle
*trans
,
2522 struct btrfs_root
*root
,
2523 struct extent_buffer
*buf
,
2524 int full_backref
, int inc
)
2531 struct btrfs_key key
;
2532 struct btrfs_file_extent_item
*fi
;
2536 int (*process_func
)(struct btrfs_trans_handle
*, struct btrfs_root
*,
2537 u64
, u64
, u64
, u64
, u64
, u64
);
2539 ref_root
= btrfs_header_owner(buf
);
2540 nritems
= btrfs_header_nritems(buf
);
2541 level
= btrfs_header_level(buf
);
2543 if (!root
->ref_cows
&& level
== 0)
2547 process_func
= btrfs_inc_extent_ref
;
2549 process_func
= btrfs_free_extent
;
2552 parent
= buf
->start
;
2556 for (i
= 0; i
< nritems
; i
++) {
2558 btrfs_item_key_to_cpu(buf
, &key
, i
);
2559 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
2561 fi
= btrfs_item_ptr(buf
, i
,
2562 struct btrfs_file_extent_item
);
2563 if (btrfs_file_extent_type(buf
, fi
) ==
2564 BTRFS_FILE_EXTENT_INLINE
)
2566 bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
2570 num_bytes
= btrfs_file_extent_disk_num_bytes(buf
, fi
);
2571 key
.offset
-= btrfs_file_extent_offset(buf
, fi
);
2572 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
2573 parent
, ref_root
, key
.objectid
,
2578 bytenr
= btrfs_node_blockptr(buf
, i
);
2579 num_bytes
= btrfs_level_size(root
, level
- 1);
2580 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
2581 parent
, ref_root
, level
- 1, 0);
2592 int btrfs_inc_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2593 struct extent_buffer
*buf
, int full_backref
)
2595 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 1);
2598 int btrfs_dec_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2599 struct extent_buffer
*buf
, int full_backref
)
2601 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 0);
2604 static int write_one_cache_group(struct btrfs_trans_handle
*trans
,
2605 struct btrfs_root
*root
,
2606 struct btrfs_path
*path
,
2607 struct btrfs_block_group_cache
*cache
)
2610 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
2612 struct extent_buffer
*leaf
;
2614 ret
= btrfs_search_slot(trans
, extent_root
, &cache
->key
, path
, 0, 1);
2619 leaf
= path
->nodes
[0];
2620 bi
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
2621 write_extent_buffer(leaf
, &cache
->item
, bi
, sizeof(cache
->item
));
2622 btrfs_mark_buffer_dirty(leaf
);
2623 btrfs_release_path(path
);
2631 static struct btrfs_block_group_cache
*
2632 next_block_group(struct btrfs_root
*root
,
2633 struct btrfs_block_group_cache
*cache
)
2635 struct rb_node
*node
;
2636 spin_lock(&root
->fs_info
->block_group_cache_lock
);
2637 node
= rb_next(&cache
->cache_node
);
2638 btrfs_put_block_group(cache
);
2640 cache
= rb_entry(node
, struct btrfs_block_group_cache
,
2642 btrfs_get_block_group(cache
);
2645 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
2649 static int cache_save_setup(struct btrfs_block_group_cache
*block_group
,
2650 struct btrfs_trans_handle
*trans
,
2651 struct btrfs_path
*path
)
2653 struct btrfs_root
*root
= block_group
->fs_info
->tree_root
;
2654 struct inode
*inode
= NULL
;
2656 int dcs
= BTRFS_DC_ERROR
;
2662 * If this block group is smaller than 100 megs don't bother caching the
2665 if (block_group
->key
.offset
< (100 * 1024 * 1024)) {
2666 spin_lock(&block_group
->lock
);
2667 block_group
->disk_cache_state
= BTRFS_DC_WRITTEN
;
2668 spin_unlock(&block_group
->lock
);
2673 inode
= lookup_free_space_inode(root
, block_group
, path
);
2674 if (IS_ERR(inode
) && PTR_ERR(inode
) != -ENOENT
) {
2675 ret
= PTR_ERR(inode
);
2676 btrfs_release_path(path
);
2680 if (IS_ERR(inode
)) {
2684 if (block_group
->ro
)
2687 ret
= create_free_space_inode(root
, trans
, block_group
, path
);
2694 * We want to set the generation to 0, that way if anything goes wrong
2695 * from here on out we know not to trust this cache when we load up next
2698 BTRFS_I(inode
)->generation
= 0;
2699 ret
= btrfs_update_inode(trans
, root
, inode
);
2702 if (i_size_read(inode
) > 0) {
2703 ret
= btrfs_truncate_free_space_cache(root
, trans
, path
,
2709 spin_lock(&block_group
->lock
);
2710 if (block_group
->cached
!= BTRFS_CACHE_FINISHED
) {
2711 /* We're not cached, don't bother trying to write stuff out */
2712 dcs
= BTRFS_DC_WRITTEN
;
2713 spin_unlock(&block_group
->lock
);
2716 spin_unlock(&block_group
->lock
);
2718 num_pages
= (int)div64_u64(block_group
->key
.offset
, 1024 * 1024 * 1024);
2723 * Just to make absolutely sure we have enough space, we're going to
2724 * preallocate 12 pages worth of space for each block group. In
2725 * practice we ought to use at most 8, but we need extra space so we can
2726 * add our header and have a terminator between the extents and the
2730 num_pages
*= PAGE_CACHE_SIZE
;
2732 ret
= btrfs_check_data_free_space(inode
, num_pages
);
2736 ret
= btrfs_prealloc_file_range_trans(inode
, trans
, 0, 0, num_pages
,
2737 num_pages
, num_pages
,
2740 dcs
= BTRFS_DC_SETUP
;
2741 btrfs_free_reserved_data_space(inode
, num_pages
);
2745 btrfs_release_path(path
);
2747 spin_lock(&block_group
->lock
);
2748 block_group
->disk_cache_state
= dcs
;
2749 spin_unlock(&block_group
->lock
);
2754 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle
*trans
,
2755 struct btrfs_root
*root
)
2757 struct btrfs_block_group_cache
*cache
;
2759 struct btrfs_path
*path
;
2762 path
= btrfs_alloc_path();
2768 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
2770 if (cache
->disk_cache_state
== BTRFS_DC_CLEAR
)
2772 cache
= next_block_group(root
, cache
);
2780 err
= cache_save_setup(cache
, trans
, path
);
2781 last
= cache
->key
.objectid
+ cache
->key
.offset
;
2782 btrfs_put_block_group(cache
);
2787 err
= btrfs_run_delayed_refs(trans
, root
,
2792 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
2794 if (cache
->disk_cache_state
== BTRFS_DC_CLEAR
) {
2795 btrfs_put_block_group(cache
);
2801 cache
= next_block_group(root
, cache
);
2810 if (cache
->disk_cache_state
== BTRFS_DC_SETUP
)
2811 cache
->disk_cache_state
= BTRFS_DC_NEED_WRITE
;
2813 last
= cache
->key
.objectid
+ cache
->key
.offset
;
2815 err
= write_one_cache_group(trans
, root
, path
, cache
);
2817 btrfs_put_block_group(cache
);
2822 * I don't think this is needed since we're just marking our
2823 * preallocated extent as written, but just in case it can't
2827 err
= btrfs_run_delayed_refs(trans
, root
,
2832 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
2835 * Really this shouldn't happen, but it could if we
2836 * couldn't write the entire preallocated extent and
2837 * splitting the extent resulted in a new block.
2840 btrfs_put_block_group(cache
);
2843 if (cache
->disk_cache_state
== BTRFS_DC_NEED_WRITE
)
2845 cache
= next_block_group(root
, cache
);
2854 btrfs_write_out_cache(root
, trans
, cache
, path
);
2857 * If we didn't have an error then the cache state is still
2858 * NEED_WRITE, so we can set it to WRITTEN.
2860 if (cache
->disk_cache_state
== BTRFS_DC_NEED_WRITE
)
2861 cache
->disk_cache_state
= BTRFS_DC_WRITTEN
;
2862 last
= cache
->key
.objectid
+ cache
->key
.offset
;
2863 btrfs_put_block_group(cache
);
2866 btrfs_free_path(path
);
2870 int btrfs_extent_readonly(struct btrfs_root
*root
, u64 bytenr
)
2872 struct btrfs_block_group_cache
*block_group
;
2875 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
2876 if (!block_group
|| block_group
->ro
)
2879 btrfs_put_block_group(block_group
);
2883 static int update_space_info(struct btrfs_fs_info
*info
, u64 flags
,
2884 u64 total_bytes
, u64 bytes_used
,
2885 struct btrfs_space_info
**space_info
)
2887 struct btrfs_space_info
*found
;
2891 if (flags
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
2892 BTRFS_BLOCK_GROUP_RAID10
))
2897 found
= __find_space_info(info
, flags
);
2899 spin_lock(&found
->lock
);
2900 found
->total_bytes
+= total_bytes
;
2901 found
->disk_total
+= total_bytes
* factor
;
2902 found
->bytes_used
+= bytes_used
;
2903 found
->disk_used
+= bytes_used
* factor
;
2905 spin_unlock(&found
->lock
);
2906 *space_info
= found
;
2909 found
= kzalloc(sizeof(*found
), GFP_NOFS
);
2913 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++)
2914 INIT_LIST_HEAD(&found
->block_groups
[i
]);
2915 init_rwsem(&found
->groups_sem
);
2916 spin_lock_init(&found
->lock
);
2917 found
->flags
= flags
& (BTRFS_BLOCK_GROUP_DATA
|
2918 BTRFS_BLOCK_GROUP_SYSTEM
|
2919 BTRFS_BLOCK_GROUP_METADATA
);
2920 found
->total_bytes
= total_bytes
;
2921 found
->disk_total
= total_bytes
* factor
;
2922 found
->bytes_used
= bytes_used
;
2923 found
->disk_used
= bytes_used
* factor
;
2924 found
->bytes_pinned
= 0;
2925 found
->bytes_reserved
= 0;
2926 found
->bytes_readonly
= 0;
2927 found
->bytes_may_use
= 0;
2929 found
->force_alloc
= CHUNK_ALLOC_NO_FORCE
;
2930 found
->chunk_alloc
= 0;
2932 init_waitqueue_head(&found
->wait
);
2933 *space_info
= found
;
2934 list_add_rcu(&found
->list
, &info
->space_info
);
2938 static void set_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
2940 u64 extra_flags
= flags
& (BTRFS_BLOCK_GROUP_RAID0
|
2941 BTRFS_BLOCK_GROUP_RAID1
|
2942 BTRFS_BLOCK_GROUP_RAID10
|
2943 BTRFS_BLOCK_GROUP_DUP
);
2945 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
2946 fs_info
->avail_data_alloc_bits
|= extra_flags
;
2947 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
2948 fs_info
->avail_metadata_alloc_bits
|= extra_flags
;
2949 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
2950 fs_info
->avail_system_alloc_bits
|= extra_flags
;
2954 u64
btrfs_reduce_alloc_profile(struct btrfs_root
*root
, u64 flags
)
2957 * we add in the count of missing devices because we want
2958 * to make sure that any RAID levels on a degraded FS
2959 * continue to be honored.
2961 u64 num_devices
= root
->fs_info
->fs_devices
->rw_devices
+
2962 root
->fs_info
->fs_devices
->missing_devices
;
2964 if (num_devices
== 1)
2965 flags
&= ~(BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID0
);
2966 if (num_devices
< 4)
2967 flags
&= ~BTRFS_BLOCK_GROUP_RAID10
;
2969 if ((flags
& BTRFS_BLOCK_GROUP_DUP
) &&
2970 (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
2971 BTRFS_BLOCK_GROUP_RAID10
))) {
2972 flags
&= ~BTRFS_BLOCK_GROUP_DUP
;
2975 if ((flags
& BTRFS_BLOCK_GROUP_RAID1
) &&
2976 (flags
& BTRFS_BLOCK_GROUP_RAID10
)) {
2977 flags
&= ~BTRFS_BLOCK_GROUP_RAID1
;
2980 if ((flags
& BTRFS_BLOCK_GROUP_RAID0
) &&
2981 ((flags
& BTRFS_BLOCK_GROUP_RAID1
) |
2982 (flags
& BTRFS_BLOCK_GROUP_RAID10
) |
2983 (flags
& BTRFS_BLOCK_GROUP_DUP
)))
2984 flags
&= ~BTRFS_BLOCK_GROUP_RAID0
;
2988 static u64
get_alloc_profile(struct btrfs_root
*root
, u64 flags
)
2990 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
2991 flags
|= root
->fs_info
->avail_data_alloc_bits
&
2992 root
->fs_info
->data_alloc_profile
;
2993 else if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
2994 flags
|= root
->fs_info
->avail_system_alloc_bits
&
2995 root
->fs_info
->system_alloc_profile
;
2996 else if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
2997 flags
|= root
->fs_info
->avail_metadata_alloc_bits
&
2998 root
->fs_info
->metadata_alloc_profile
;
2999 return btrfs_reduce_alloc_profile(root
, flags
);
3002 u64
btrfs_get_alloc_profile(struct btrfs_root
*root
, int data
)
3007 flags
= BTRFS_BLOCK_GROUP_DATA
;
3008 else if (root
== root
->fs_info
->chunk_root
)
3009 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
3011 flags
= BTRFS_BLOCK_GROUP_METADATA
;
3013 return get_alloc_profile(root
, flags
);
3016 void btrfs_set_inode_space_info(struct btrfs_root
*root
, struct inode
*inode
)
3018 BTRFS_I(inode
)->space_info
= __find_space_info(root
->fs_info
,
3019 BTRFS_BLOCK_GROUP_DATA
);
3023 * This will check the space that the inode allocates from to make sure we have
3024 * enough space for bytes.
3026 int btrfs_check_data_free_space(struct inode
*inode
, u64 bytes
)
3028 struct btrfs_space_info
*data_sinfo
;
3029 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3031 int ret
= 0, committed
= 0, alloc_chunk
= 1;
3033 /* make sure bytes are sectorsize aligned */
3034 bytes
= (bytes
+ root
->sectorsize
- 1) & ~((u64
)root
->sectorsize
- 1);
3036 if (root
== root
->fs_info
->tree_root
||
3037 BTRFS_I(inode
)->location
.objectid
== BTRFS_FREE_INO_OBJECTID
) {
3042 data_sinfo
= BTRFS_I(inode
)->space_info
;
3047 /* make sure we have enough space to handle the data first */
3048 spin_lock(&data_sinfo
->lock
);
3049 used
= data_sinfo
->bytes_used
+ data_sinfo
->bytes_reserved
+
3050 data_sinfo
->bytes_pinned
+ data_sinfo
->bytes_readonly
+
3051 data_sinfo
->bytes_may_use
;
3053 if (used
+ bytes
> data_sinfo
->total_bytes
) {
3054 struct btrfs_trans_handle
*trans
;
3057 * if we don't have enough free bytes in this space then we need
3058 * to alloc a new chunk.
3060 if (!data_sinfo
->full
&& alloc_chunk
) {
3063 data_sinfo
->force_alloc
= CHUNK_ALLOC_FORCE
;
3064 spin_unlock(&data_sinfo
->lock
);
3066 alloc_target
= btrfs_get_alloc_profile(root
, 1);
3067 trans
= btrfs_join_transaction(root
);
3069 return PTR_ERR(trans
);
3071 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
3072 bytes
+ 2 * 1024 * 1024,
3074 CHUNK_ALLOC_NO_FORCE
);
3075 btrfs_end_transaction(trans
, root
);
3084 btrfs_set_inode_space_info(root
, inode
);
3085 data_sinfo
= BTRFS_I(inode
)->space_info
;
3091 * If we have less pinned bytes than we want to allocate then
3092 * don't bother committing the transaction, it won't help us.
3094 if (data_sinfo
->bytes_pinned
< bytes
)
3096 spin_unlock(&data_sinfo
->lock
);
3098 /* commit the current transaction and try again */
3101 !atomic_read(&root
->fs_info
->open_ioctl_trans
)) {
3103 trans
= btrfs_join_transaction(root
);
3105 return PTR_ERR(trans
);
3106 ret
= btrfs_commit_transaction(trans
, root
);
3114 data_sinfo
->bytes_may_use
+= bytes
;
3115 BTRFS_I(inode
)->reserved_bytes
+= bytes
;
3116 spin_unlock(&data_sinfo
->lock
);
3122 * called when we are clearing an delalloc extent from the
3123 * inode's io_tree or there was an error for whatever reason
3124 * after calling btrfs_check_data_free_space
3126 void btrfs_free_reserved_data_space(struct inode
*inode
, u64 bytes
)
3128 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3129 struct btrfs_space_info
*data_sinfo
;
3131 /* make sure bytes are sectorsize aligned */
3132 bytes
= (bytes
+ root
->sectorsize
- 1) & ~((u64
)root
->sectorsize
- 1);
3134 data_sinfo
= BTRFS_I(inode
)->space_info
;
3135 spin_lock(&data_sinfo
->lock
);
3136 data_sinfo
->bytes_may_use
-= bytes
;
3137 BTRFS_I(inode
)->reserved_bytes
-= bytes
;
3138 spin_unlock(&data_sinfo
->lock
);
3141 static void force_metadata_allocation(struct btrfs_fs_info
*info
)
3143 struct list_head
*head
= &info
->space_info
;
3144 struct btrfs_space_info
*found
;
3147 list_for_each_entry_rcu(found
, head
, list
) {
3148 if (found
->flags
& BTRFS_BLOCK_GROUP_METADATA
)
3149 found
->force_alloc
= CHUNK_ALLOC_FORCE
;
3154 static int should_alloc_chunk(struct btrfs_root
*root
,
3155 struct btrfs_space_info
*sinfo
, u64 alloc_bytes
,
3158 u64 num_bytes
= sinfo
->total_bytes
- sinfo
->bytes_readonly
;
3159 u64 num_allocated
= sinfo
->bytes_used
+ sinfo
->bytes_reserved
;
3162 if (force
== CHUNK_ALLOC_FORCE
)
3166 * in limited mode, we want to have some free space up to
3167 * about 1% of the FS size.
3169 if (force
== CHUNK_ALLOC_LIMITED
) {
3170 thresh
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
3171 thresh
= max_t(u64
, 64 * 1024 * 1024,
3172 div_factor_fine(thresh
, 1));
3174 if (num_bytes
- num_allocated
< thresh
)
3179 * we have two similar checks here, one based on percentage
3180 * and once based on a hard number of 256MB. The idea
3181 * is that if we have a good amount of free
3182 * room, don't allocate a chunk. A good mount is
3183 * less than 80% utilized of the chunks we have allocated,
3184 * or more than 256MB free
3186 if (num_allocated
+ alloc_bytes
+ 256 * 1024 * 1024 < num_bytes
)
3189 if (num_allocated
+ alloc_bytes
< div_factor(num_bytes
, 8))
3192 thresh
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
3194 /* 256MB or 5% of the FS */
3195 thresh
= max_t(u64
, 256 * 1024 * 1024, div_factor_fine(thresh
, 5));
3197 if (num_bytes
> thresh
&& sinfo
->bytes_used
< div_factor(num_bytes
, 3))
3202 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
3203 struct btrfs_root
*extent_root
, u64 alloc_bytes
,
3204 u64 flags
, int force
)
3206 struct btrfs_space_info
*space_info
;
3207 struct btrfs_fs_info
*fs_info
= extent_root
->fs_info
;
3208 int wait_for_alloc
= 0;
3211 flags
= btrfs_reduce_alloc_profile(extent_root
, flags
);
3213 space_info
= __find_space_info(extent_root
->fs_info
, flags
);
3215 ret
= update_space_info(extent_root
->fs_info
, flags
,
3219 BUG_ON(!space_info
);
3222 spin_lock(&space_info
->lock
);
3223 if (space_info
->force_alloc
)
3224 force
= space_info
->force_alloc
;
3225 if (space_info
->full
) {
3226 spin_unlock(&space_info
->lock
);
3230 if (!should_alloc_chunk(extent_root
, space_info
, alloc_bytes
, force
)) {
3231 spin_unlock(&space_info
->lock
);
3233 } else if (space_info
->chunk_alloc
) {
3236 space_info
->chunk_alloc
= 1;
3239 spin_unlock(&space_info
->lock
);
3241 mutex_lock(&fs_info
->chunk_mutex
);
3244 * The chunk_mutex is held throughout the entirety of a chunk
3245 * allocation, so once we've acquired the chunk_mutex we know that the
3246 * other guy is done and we need to recheck and see if we should
3249 if (wait_for_alloc
) {
3250 mutex_unlock(&fs_info
->chunk_mutex
);
3256 * If we have mixed data/metadata chunks we want to make sure we keep
3257 * allocating mixed chunks instead of individual chunks.
3259 if (btrfs_mixed_space_info(space_info
))
3260 flags
|= (BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
);
3263 * if we're doing a data chunk, go ahead and make sure that
3264 * we keep a reasonable number of metadata chunks allocated in the
3267 if (flags
& BTRFS_BLOCK_GROUP_DATA
&& fs_info
->metadata_ratio
) {
3268 fs_info
->data_chunk_allocations
++;
3269 if (!(fs_info
->data_chunk_allocations
%
3270 fs_info
->metadata_ratio
))
3271 force_metadata_allocation(fs_info
);
3274 ret
= btrfs_alloc_chunk(trans
, extent_root
, flags
);
3275 spin_lock(&space_info
->lock
);
3277 space_info
->full
= 1;
3281 space_info
->force_alloc
= CHUNK_ALLOC_NO_FORCE
;
3282 space_info
->chunk_alloc
= 0;
3283 spin_unlock(&space_info
->lock
);
3284 mutex_unlock(&extent_root
->fs_info
->chunk_mutex
);
3289 * shrink metadata reservation for delalloc
3291 static int shrink_delalloc(struct btrfs_trans_handle
*trans
,
3292 struct btrfs_root
*root
, u64 to_reclaim
, int sync
)
3294 struct btrfs_block_rsv
*block_rsv
;
3295 struct btrfs_space_info
*space_info
;
3300 int nr_pages
= (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT
;
3302 unsigned long progress
;
3304 block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
3305 space_info
= block_rsv
->space_info
;
3308 reserved
= space_info
->bytes_reserved
;
3309 progress
= space_info
->reservation_progress
;
3315 if (root
->fs_info
->delalloc_bytes
== 0) {
3318 btrfs_wait_ordered_extents(root
, 0, 0);
3322 max_reclaim
= min(reserved
, to_reclaim
);
3324 while (loops
< 1024) {
3325 /* have the flusher threads jump in and do some IO */
3327 nr_pages
= min_t(unsigned long, nr_pages
,
3328 root
->fs_info
->delalloc_bytes
>> PAGE_CACHE_SHIFT
);
3329 writeback_inodes_sb_nr_if_idle(root
->fs_info
->sb
, nr_pages
);
3331 spin_lock(&space_info
->lock
);
3332 if (reserved
> space_info
->bytes_reserved
)
3333 reclaimed
+= reserved
- space_info
->bytes_reserved
;
3334 reserved
= space_info
->bytes_reserved
;
3335 spin_unlock(&space_info
->lock
);
3339 if (reserved
== 0 || reclaimed
>= max_reclaim
)
3342 if (trans
&& trans
->transaction
->blocked
)
3345 time_left
= schedule_timeout_interruptible(1);
3347 /* We were interrupted, exit */
3351 /* we've kicked the IO a few times, if anything has been freed,
3352 * exit. There is no sense in looping here for a long time
3353 * when we really need to commit the transaction, or there are
3354 * just too many writers without enough free space
3359 if (progress
!= space_info
->reservation_progress
)
3364 if (reclaimed
>= to_reclaim
&& !trans
)
3365 btrfs_wait_ordered_extents(root
, 0, 0);
3366 return reclaimed
>= to_reclaim
;
3370 * Retries tells us how many times we've called reserve_metadata_bytes. The
3371 * idea is if this is the first call (retries == 0) then we will add to our
3372 * reserved count if we can't make the allocation in order to hold our place
3373 * while we go and try and free up space. That way for retries > 1 we don't try
3374 * and add space, we just check to see if the amount of unused space is >= the
3375 * total space, meaning that our reservation is valid.
3377 * However if we don't intend to retry this reservation, pass -1 as retries so
3378 * that it short circuits this logic.
3380 static int reserve_metadata_bytes(struct btrfs_trans_handle
*trans
,
3381 struct btrfs_root
*root
,
3382 struct btrfs_block_rsv
*block_rsv
,
3383 u64 orig_bytes
, int flush
)
3385 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
3387 u64 num_bytes
= orig_bytes
;
3390 bool committed
= false;
3391 bool flushing
= false;
3395 spin_lock(&space_info
->lock
);
3397 * We only want to wait if somebody other than us is flushing and we are
3398 * actually alloed to flush.
3400 while (flush
&& !flushing
&& space_info
->flush
) {
3401 spin_unlock(&space_info
->lock
);
3403 * If we have a trans handle we can't wait because the flusher
3404 * may have to commit the transaction, which would mean we would
3405 * deadlock since we are waiting for the flusher to finish, but
3406 * hold the current transaction open.
3410 ret
= wait_event_interruptible(space_info
->wait
,
3411 !space_info
->flush
);
3412 /* Must have been interrupted, return */
3416 spin_lock(&space_info
->lock
);
3420 unused
= space_info
->bytes_used
+ space_info
->bytes_reserved
+
3421 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
3422 space_info
->bytes_may_use
;
3425 * The idea here is that we've not already over-reserved the block group
3426 * then we can go ahead and save our reservation first and then start
3427 * flushing if we need to. Otherwise if we've already overcommitted
3428 * lets start flushing stuff first and then come back and try to make
3431 if (unused
<= space_info
->total_bytes
) {
3432 unused
= space_info
->total_bytes
- unused
;
3433 if (unused
>= num_bytes
) {
3434 space_info
->bytes_reserved
+= orig_bytes
;
3438 * Ok set num_bytes to orig_bytes since we aren't
3439 * overocmmitted, this way we only try and reclaim what
3442 num_bytes
= orig_bytes
;
3446 * Ok we're over committed, set num_bytes to the overcommitted
3447 * amount plus the amount of bytes that we need for this
3450 num_bytes
= unused
- space_info
->total_bytes
+
3451 (orig_bytes
* (retries
+ 1));
3455 * Couldn't make our reservation, save our place so while we're trying
3456 * to reclaim space we can actually use it instead of somebody else
3457 * stealing it from us.
3461 space_info
->flush
= 1;
3464 spin_unlock(&space_info
->lock
);
3470 * We do synchronous shrinking since we don't actually unreserve
3471 * metadata until after the IO is completed.
3473 ret
= shrink_delalloc(trans
, root
, num_bytes
, 1);
3478 * So if we were overcommitted it's possible that somebody else flushed
3479 * out enough space and we simply didn't have enough space to reclaim,
3480 * so go back around and try again.
3488 * Not enough space to be reclaimed, don't bother committing the
3491 spin_lock(&space_info
->lock
);
3492 if (space_info
->bytes_pinned
< orig_bytes
)
3494 spin_unlock(&space_info
->lock
);
3499 if (trans
|| committed
)
3503 trans
= btrfs_join_transaction(root
);
3506 ret
= btrfs_commit_transaction(trans
, root
);
3515 spin_lock(&space_info
->lock
);
3516 space_info
->flush
= 0;
3517 wake_up_all(&space_info
->wait
);
3518 spin_unlock(&space_info
->lock
);
3523 static struct btrfs_block_rsv
*get_block_rsv(struct btrfs_trans_handle
*trans
,
3524 struct btrfs_root
*root
)
3526 struct btrfs_block_rsv
*block_rsv
;
3528 block_rsv
= trans
->block_rsv
;
3530 block_rsv
= root
->block_rsv
;
3533 block_rsv
= &root
->fs_info
->empty_block_rsv
;
3538 static int block_rsv_use_bytes(struct btrfs_block_rsv
*block_rsv
,
3542 spin_lock(&block_rsv
->lock
);
3543 if (block_rsv
->reserved
>= num_bytes
) {
3544 block_rsv
->reserved
-= num_bytes
;
3545 if (block_rsv
->reserved
< block_rsv
->size
)
3546 block_rsv
->full
= 0;
3549 spin_unlock(&block_rsv
->lock
);
3553 static void block_rsv_add_bytes(struct btrfs_block_rsv
*block_rsv
,
3554 u64 num_bytes
, int update_size
)
3556 spin_lock(&block_rsv
->lock
);
3557 block_rsv
->reserved
+= num_bytes
;
3559 block_rsv
->size
+= num_bytes
;
3560 else if (block_rsv
->reserved
>= block_rsv
->size
)
3561 block_rsv
->full
= 1;
3562 spin_unlock(&block_rsv
->lock
);
3565 static void block_rsv_release_bytes(struct btrfs_block_rsv
*block_rsv
,
3566 struct btrfs_block_rsv
*dest
, u64 num_bytes
)
3568 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
3570 spin_lock(&block_rsv
->lock
);
3571 if (num_bytes
== (u64
)-1)
3572 num_bytes
= block_rsv
->size
;
3573 block_rsv
->size
-= num_bytes
;
3574 if (block_rsv
->reserved
>= block_rsv
->size
) {
3575 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
3576 block_rsv
->reserved
= block_rsv
->size
;
3577 block_rsv
->full
= 1;
3581 spin_unlock(&block_rsv
->lock
);
3583 if (num_bytes
> 0) {
3585 spin_lock(&dest
->lock
);
3589 bytes_to_add
= dest
->size
- dest
->reserved
;
3590 bytes_to_add
= min(num_bytes
, bytes_to_add
);
3591 dest
->reserved
+= bytes_to_add
;
3592 if (dest
->reserved
>= dest
->size
)
3594 num_bytes
-= bytes_to_add
;
3596 spin_unlock(&dest
->lock
);
3599 spin_lock(&space_info
->lock
);
3600 space_info
->bytes_reserved
-= num_bytes
;
3601 space_info
->reservation_progress
++;
3602 spin_unlock(&space_info
->lock
);
3607 static int block_rsv_migrate_bytes(struct btrfs_block_rsv
*src
,
3608 struct btrfs_block_rsv
*dst
, u64 num_bytes
)
3612 ret
= block_rsv_use_bytes(src
, num_bytes
);
3616 block_rsv_add_bytes(dst
, num_bytes
, 1);
3620 void btrfs_init_block_rsv(struct btrfs_block_rsv
*rsv
)
3622 memset(rsv
, 0, sizeof(*rsv
));
3623 spin_lock_init(&rsv
->lock
);
3624 atomic_set(&rsv
->usage
, 1);
3626 INIT_LIST_HEAD(&rsv
->list
);
3629 struct btrfs_block_rsv
*btrfs_alloc_block_rsv(struct btrfs_root
*root
)
3631 struct btrfs_block_rsv
*block_rsv
;
3632 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3634 block_rsv
= kmalloc(sizeof(*block_rsv
), GFP_NOFS
);
3638 btrfs_init_block_rsv(block_rsv
);
3639 block_rsv
->space_info
= __find_space_info(fs_info
,
3640 BTRFS_BLOCK_GROUP_METADATA
);
3644 void btrfs_free_block_rsv(struct btrfs_root
*root
,
3645 struct btrfs_block_rsv
*rsv
)
3647 if (rsv
&& atomic_dec_and_test(&rsv
->usage
)) {
3648 btrfs_block_rsv_release(root
, rsv
, (u64
)-1);
3655 * make the block_rsv struct be able to capture freed space.
3656 * the captured space will re-add to the the block_rsv struct
3657 * after transaction commit
3659 void btrfs_add_durable_block_rsv(struct btrfs_fs_info
*fs_info
,
3660 struct btrfs_block_rsv
*block_rsv
)
3662 block_rsv
->durable
= 1;
3663 mutex_lock(&fs_info
->durable_block_rsv_mutex
);
3664 list_add_tail(&block_rsv
->list
, &fs_info
->durable_block_rsv_list
);
3665 mutex_unlock(&fs_info
->durable_block_rsv_mutex
);
3668 int btrfs_block_rsv_add(struct btrfs_trans_handle
*trans
,
3669 struct btrfs_root
*root
,
3670 struct btrfs_block_rsv
*block_rsv
,
3678 ret
= reserve_metadata_bytes(trans
, root
, block_rsv
, num_bytes
, 1);
3680 block_rsv_add_bytes(block_rsv
, num_bytes
, 1);
3687 int btrfs_block_rsv_check(struct btrfs_trans_handle
*trans
,
3688 struct btrfs_root
*root
,
3689 struct btrfs_block_rsv
*block_rsv
,
3690 u64 min_reserved
, int min_factor
)
3693 int commit_trans
= 0;
3699 spin_lock(&block_rsv
->lock
);
3701 num_bytes
= div_factor(block_rsv
->size
, min_factor
);
3702 if (min_reserved
> num_bytes
)
3703 num_bytes
= min_reserved
;
3705 if (block_rsv
->reserved
>= num_bytes
) {
3708 num_bytes
-= block_rsv
->reserved
;
3709 if (block_rsv
->durable
&&
3710 block_rsv
->freed
[0] + block_rsv
->freed
[1] >= num_bytes
)
3713 spin_unlock(&block_rsv
->lock
);
3717 if (block_rsv
->refill_used
) {
3718 ret
= reserve_metadata_bytes(trans
, root
, block_rsv
,
3721 block_rsv_add_bytes(block_rsv
, num_bytes
, 0);
3730 trans
= btrfs_join_transaction(root
);
3731 BUG_ON(IS_ERR(trans
));
3732 ret
= btrfs_commit_transaction(trans
, root
);
3739 int btrfs_block_rsv_migrate(struct btrfs_block_rsv
*src_rsv
,
3740 struct btrfs_block_rsv
*dst_rsv
,
3743 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
3746 void btrfs_block_rsv_release(struct btrfs_root
*root
,
3747 struct btrfs_block_rsv
*block_rsv
,
3750 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
3751 if (global_rsv
->full
|| global_rsv
== block_rsv
||
3752 block_rsv
->space_info
!= global_rsv
->space_info
)
3754 block_rsv_release_bytes(block_rsv
, global_rsv
, num_bytes
);
3758 * helper to calculate size of global block reservation.
3759 * the desired value is sum of space used by extent tree,
3760 * checksum tree and root tree
3762 static u64
calc_global_metadata_size(struct btrfs_fs_info
*fs_info
)
3764 struct btrfs_space_info
*sinfo
;
3768 int csum_size
= btrfs_super_csum_size(&fs_info
->super_copy
);
3770 sinfo
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_DATA
);
3771 spin_lock(&sinfo
->lock
);
3772 data_used
= sinfo
->bytes_used
;
3773 spin_unlock(&sinfo
->lock
);
3775 sinfo
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
3776 spin_lock(&sinfo
->lock
);
3777 if (sinfo
->flags
& BTRFS_BLOCK_GROUP_DATA
)
3779 meta_used
= sinfo
->bytes_used
;
3780 spin_unlock(&sinfo
->lock
);
3782 num_bytes
= (data_used
>> fs_info
->sb
->s_blocksize_bits
) *
3784 num_bytes
+= div64_u64(data_used
+ meta_used
, 50);
3786 if (num_bytes
* 3 > meta_used
)
3787 num_bytes
= div64_u64(meta_used
, 3);
3789 return ALIGN(num_bytes
, fs_info
->extent_root
->leafsize
<< 10);
3792 static void update_global_block_rsv(struct btrfs_fs_info
*fs_info
)
3794 struct btrfs_block_rsv
*block_rsv
= &fs_info
->global_block_rsv
;
3795 struct btrfs_space_info
*sinfo
= block_rsv
->space_info
;
3798 num_bytes
= calc_global_metadata_size(fs_info
);
3800 spin_lock(&block_rsv
->lock
);
3801 spin_lock(&sinfo
->lock
);
3803 block_rsv
->size
= num_bytes
;
3805 num_bytes
= sinfo
->bytes_used
+ sinfo
->bytes_pinned
+
3806 sinfo
->bytes_reserved
+ sinfo
->bytes_readonly
+
3807 sinfo
->bytes_may_use
;
3809 if (sinfo
->total_bytes
> num_bytes
) {
3810 num_bytes
= sinfo
->total_bytes
- num_bytes
;
3811 block_rsv
->reserved
+= num_bytes
;
3812 sinfo
->bytes_reserved
+= num_bytes
;
3815 if (block_rsv
->reserved
>= block_rsv
->size
) {
3816 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
3817 sinfo
->bytes_reserved
-= num_bytes
;
3818 sinfo
->reservation_progress
++;
3819 block_rsv
->reserved
= block_rsv
->size
;
3820 block_rsv
->full
= 1;
3823 spin_unlock(&sinfo
->lock
);
3824 spin_unlock(&block_rsv
->lock
);
3827 static void init_global_block_rsv(struct btrfs_fs_info
*fs_info
)
3829 struct btrfs_space_info
*space_info
;
3831 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_SYSTEM
);
3832 fs_info
->chunk_block_rsv
.space_info
= space_info
;
3833 fs_info
->chunk_block_rsv
.priority
= 10;
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
->global_block_rsv
.priority
= 10;
3838 fs_info
->global_block_rsv
.refill_used
= 1;
3839 fs_info
->delalloc_block_rsv
.space_info
= space_info
;
3840 fs_info
->trans_block_rsv
.space_info
= space_info
;
3841 fs_info
->empty_block_rsv
.space_info
= space_info
;
3842 fs_info
->empty_block_rsv
.priority
= 10;
3844 fs_info
->extent_root
->block_rsv
= &fs_info
->global_block_rsv
;
3845 fs_info
->csum_root
->block_rsv
= &fs_info
->global_block_rsv
;
3846 fs_info
->dev_root
->block_rsv
= &fs_info
->global_block_rsv
;
3847 fs_info
->tree_root
->block_rsv
= &fs_info
->global_block_rsv
;
3848 fs_info
->chunk_root
->block_rsv
= &fs_info
->chunk_block_rsv
;
3850 btrfs_add_durable_block_rsv(fs_info
, &fs_info
->global_block_rsv
);
3852 btrfs_add_durable_block_rsv(fs_info
, &fs_info
->delalloc_block_rsv
);
3854 update_global_block_rsv(fs_info
);
3857 static void release_global_block_rsv(struct btrfs_fs_info
*fs_info
)
3859 block_rsv_release_bytes(&fs_info
->global_block_rsv
, NULL
, (u64
)-1);
3860 WARN_ON(fs_info
->delalloc_block_rsv
.size
> 0);
3861 WARN_ON(fs_info
->delalloc_block_rsv
.reserved
> 0);
3862 WARN_ON(fs_info
->trans_block_rsv
.size
> 0);
3863 WARN_ON(fs_info
->trans_block_rsv
.reserved
> 0);
3864 WARN_ON(fs_info
->chunk_block_rsv
.size
> 0);
3865 WARN_ON(fs_info
->chunk_block_rsv
.reserved
> 0);
3868 int btrfs_truncate_reserve_metadata(struct btrfs_trans_handle
*trans
,
3869 struct btrfs_root
*root
,
3870 struct btrfs_block_rsv
*rsv
)
3872 struct btrfs_block_rsv
*trans_rsv
= &root
->fs_info
->trans_block_rsv
;
3877 * Truncate should be freeing data, but give us 2 items just in case it
3878 * needs to use some space. We may want to be smarter about this in the
3881 num_bytes
= btrfs_calc_trans_metadata_size(root
, 2);
3883 /* We already have enough bytes, just return */
3884 if (rsv
->reserved
>= num_bytes
)
3887 num_bytes
-= rsv
->reserved
;
3890 * You should have reserved enough space before hand to do this, so this
3893 ret
= block_rsv_migrate_bytes(trans_rsv
, rsv
, num_bytes
);
3899 void btrfs_trans_release_metadata(struct btrfs_trans_handle
*trans
,
3900 struct btrfs_root
*root
)
3902 if (!trans
->bytes_reserved
)
3905 BUG_ON(trans
->block_rsv
!= &root
->fs_info
->trans_block_rsv
);
3906 btrfs_block_rsv_release(root
, trans
->block_rsv
,
3907 trans
->bytes_reserved
);
3908 trans
->bytes_reserved
= 0;
3911 int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle
*trans
,
3912 struct inode
*inode
)
3914 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3915 struct btrfs_block_rsv
*src_rsv
= get_block_rsv(trans
, root
);
3916 struct btrfs_block_rsv
*dst_rsv
= root
->orphan_block_rsv
;
3919 * We need to hold space in order to delete our orphan item once we've
3920 * added it, so this takes the reservation so we can release it later
3921 * when we are truly done with the orphan item.
3923 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 1);
3924 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
3927 void btrfs_orphan_release_metadata(struct inode
*inode
)
3929 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3930 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 1);
3931 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
, num_bytes
);
3934 int btrfs_snap_reserve_metadata(struct btrfs_trans_handle
*trans
,
3935 struct btrfs_pending_snapshot
*pending
)
3937 struct btrfs_root
*root
= pending
->root
;
3938 struct btrfs_block_rsv
*src_rsv
= get_block_rsv(trans
, root
);
3939 struct btrfs_block_rsv
*dst_rsv
= &pending
->block_rsv
;
3941 * two for root back/forward refs, two for directory entries
3942 * and one for root of the snapshot.
3944 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 5);
3945 dst_rsv
->space_info
= src_rsv
->space_info
;
3946 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
3949 static u64
calc_csum_metadata_size(struct inode
*inode
, u64 num_bytes
)
3951 return num_bytes
>>= 3;
3954 int btrfs_delalloc_reserve_metadata(struct inode
*inode
, u64 num_bytes
)
3956 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3957 struct btrfs_block_rsv
*block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
3960 int reserved_extents
;
3963 if (btrfs_transaction_in_commit(root
->fs_info
))
3964 schedule_timeout(1);
3966 num_bytes
= ALIGN(num_bytes
, root
->sectorsize
);
3968 nr_extents
= atomic_read(&BTRFS_I(inode
)->outstanding_extents
) + 1;
3969 reserved_extents
= atomic_read(&BTRFS_I(inode
)->reserved_extents
);
3971 if (nr_extents
> reserved_extents
) {
3972 nr_extents
-= reserved_extents
;
3973 to_reserve
= btrfs_calc_trans_metadata_size(root
, nr_extents
);
3979 to_reserve
+= calc_csum_metadata_size(inode
, num_bytes
);
3980 ret
= reserve_metadata_bytes(NULL
, root
, block_rsv
, to_reserve
, 1);
3984 atomic_add(nr_extents
, &BTRFS_I(inode
)->reserved_extents
);
3985 atomic_inc(&BTRFS_I(inode
)->outstanding_extents
);
3987 block_rsv_add_bytes(block_rsv
, to_reserve
, 1);
3989 if (block_rsv
->size
> 512 * 1024 * 1024)
3990 shrink_delalloc(NULL
, root
, to_reserve
, 0);
3995 void btrfs_delalloc_release_metadata(struct inode
*inode
, u64 num_bytes
)
3997 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4000 int reserved_extents
;
4002 num_bytes
= ALIGN(num_bytes
, root
->sectorsize
);
4003 atomic_dec(&BTRFS_I(inode
)->outstanding_extents
);
4004 WARN_ON(atomic_read(&BTRFS_I(inode
)->outstanding_extents
) < 0);
4006 reserved_extents
= atomic_read(&BTRFS_I(inode
)->reserved_extents
);
4010 nr_extents
= atomic_read(&BTRFS_I(inode
)->outstanding_extents
);
4011 if (nr_extents
>= reserved_extents
) {
4015 old
= reserved_extents
;
4016 nr_extents
= reserved_extents
- nr_extents
;
4017 new = reserved_extents
- nr_extents
;
4018 old
= atomic_cmpxchg(&BTRFS_I(inode
)->reserved_extents
,
4019 reserved_extents
, new);
4020 if (likely(old
== reserved_extents
))
4022 reserved_extents
= old
;
4025 to_free
= calc_csum_metadata_size(inode
, num_bytes
);
4027 to_free
+= btrfs_calc_trans_metadata_size(root
, nr_extents
);
4029 btrfs_block_rsv_release(root
, &root
->fs_info
->delalloc_block_rsv
,
4033 int btrfs_delalloc_reserve_space(struct inode
*inode
, u64 num_bytes
)
4037 ret
= btrfs_check_data_free_space(inode
, num_bytes
);
4041 ret
= btrfs_delalloc_reserve_metadata(inode
, num_bytes
);
4043 btrfs_free_reserved_data_space(inode
, num_bytes
);
4050 void btrfs_delalloc_release_space(struct inode
*inode
, u64 num_bytes
)
4052 btrfs_delalloc_release_metadata(inode
, num_bytes
);
4053 btrfs_free_reserved_data_space(inode
, num_bytes
);
4056 static int update_block_group(struct btrfs_trans_handle
*trans
,
4057 struct btrfs_root
*root
,
4058 u64 bytenr
, u64 num_bytes
, int alloc
)
4060 struct btrfs_block_group_cache
*cache
= NULL
;
4061 struct btrfs_fs_info
*info
= root
->fs_info
;
4062 u64 total
= num_bytes
;
4067 /* block accounting for super block */
4068 spin_lock(&info
->delalloc_lock
);
4069 old_val
= btrfs_super_bytes_used(&info
->super_copy
);
4071 old_val
+= num_bytes
;
4073 old_val
-= num_bytes
;
4074 btrfs_set_super_bytes_used(&info
->super_copy
, old_val
);
4075 spin_unlock(&info
->delalloc_lock
);
4078 cache
= btrfs_lookup_block_group(info
, bytenr
);
4081 if (cache
->flags
& (BTRFS_BLOCK_GROUP_DUP
|
4082 BTRFS_BLOCK_GROUP_RAID1
|
4083 BTRFS_BLOCK_GROUP_RAID10
))
4088 * If this block group has free space cache written out, we
4089 * need to make sure to load it if we are removing space. This
4090 * is because we need the unpinning stage to actually add the
4091 * space back to the block group, otherwise we will leak space.
4093 if (!alloc
&& cache
->cached
== BTRFS_CACHE_NO
)
4094 cache_block_group(cache
, trans
, NULL
, 1);
4096 byte_in_group
= bytenr
- cache
->key
.objectid
;
4097 WARN_ON(byte_in_group
> cache
->key
.offset
);
4099 spin_lock(&cache
->space_info
->lock
);
4100 spin_lock(&cache
->lock
);
4102 if (btrfs_super_cache_generation(&info
->super_copy
) != 0 &&
4103 cache
->disk_cache_state
< BTRFS_DC_CLEAR
)
4104 cache
->disk_cache_state
= BTRFS_DC_CLEAR
;
4107 old_val
= btrfs_block_group_used(&cache
->item
);
4108 num_bytes
= min(total
, cache
->key
.offset
- byte_in_group
);
4110 old_val
+= num_bytes
;
4111 btrfs_set_block_group_used(&cache
->item
, old_val
);
4112 cache
->reserved
-= num_bytes
;
4113 cache
->space_info
->bytes_reserved
-= num_bytes
;
4114 cache
->space_info
->reservation_progress
++;
4115 cache
->space_info
->bytes_used
+= num_bytes
;
4116 cache
->space_info
->disk_used
+= num_bytes
* factor
;
4117 spin_unlock(&cache
->lock
);
4118 spin_unlock(&cache
->space_info
->lock
);
4120 old_val
-= num_bytes
;
4121 btrfs_set_block_group_used(&cache
->item
, old_val
);
4122 cache
->pinned
+= num_bytes
;
4123 cache
->space_info
->bytes_pinned
+= num_bytes
;
4124 cache
->space_info
->bytes_used
-= num_bytes
;
4125 cache
->space_info
->disk_used
-= num_bytes
* factor
;
4126 spin_unlock(&cache
->lock
);
4127 spin_unlock(&cache
->space_info
->lock
);
4129 set_extent_dirty(info
->pinned_extents
,
4130 bytenr
, bytenr
+ num_bytes
- 1,
4131 GFP_NOFS
| __GFP_NOFAIL
);
4133 btrfs_put_block_group(cache
);
4135 bytenr
+= num_bytes
;
4140 static u64
first_logical_byte(struct btrfs_root
*root
, u64 search_start
)
4142 struct btrfs_block_group_cache
*cache
;
4145 cache
= btrfs_lookup_first_block_group(root
->fs_info
, search_start
);
4149 bytenr
= cache
->key
.objectid
;
4150 btrfs_put_block_group(cache
);
4155 static int pin_down_extent(struct btrfs_root
*root
,
4156 struct btrfs_block_group_cache
*cache
,
4157 u64 bytenr
, u64 num_bytes
, int reserved
)
4159 spin_lock(&cache
->space_info
->lock
);
4160 spin_lock(&cache
->lock
);
4161 cache
->pinned
+= num_bytes
;
4162 cache
->space_info
->bytes_pinned
+= num_bytes
;
4164 cache
->reserved
-= num_bytes
;
4165 cache
->space_info
->bytes_reserved
-= num_bytes
;
4166 cache
->space_info
->reservation_progress
++;
4168 spin_unlock(&cache
->lock
);
4169 spin_unlock(&cache
->space_info
->lock
);
4171 set_extent_dirty(root
->fs_info
->pinned_extents
, bytenr
,
4172 bytenr
+ num_bytes
- 1, GFP_NOFS
| __GFP_NOFAIL
);
4177 * this function must be called within transaction
4179 int btrfs_pin_extent(struct btrfs_root
*root
,
4180 u64 bytenr
, u64 num_bytes
, int reserved
)
4182 struct btrfs_block_group_cache
*cache
;
4184 cache
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
4187 pin_down_extent(root
, cache
, bytenr
, num_bytes
, reserved
);
4189 btrfs_put_block_group(cache
);
4194 * update size of reserved extents. this function may return -EAGAIN
4195 * if 'reserve' is true or 'sinfo' is false.
4197 int btrfs_update_reserved_bytes(struct btrfs_block_group_cache
*cache
,
4198 u64 num_bytes
, int reserve
, int sinfo
)
4202 struct btrfs_space_info
*space_info
= cache
->space_info
;
4203 spin_lock(&space_info
->lock
);
4204 spin_lock(&cache
->lock
);
4209 cache
->reserved
+= num_bytes
;
4210 space_info
->bytes_reserved
+= num_bytes
;
4214 space_info
->bytes_readonly
+= num_bytes
;
4215 cache
->reserved
-= num_bytes
;
4216 space_info
->bytes_reserved
-= num_bytes
;
4217 space_info
->reservation_progress
++;
4219 spin_unlock(&cache
->lock
);
4220 spin_unlock(&space_info
->lock
);
4222 spin_lock(&cache
->lock
);
4227 cache
->reserved
+= num_bytes
;
4229 cache
->reserved
-= num_bytes
;
4231 spin_unlock(&cache
->lock
);
4236 int btrfs_prepare_extent_commit(struct btrfs_trans_handle
*trans
,
4237 struct btrfs_root
*root
)
4239 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4240 struct btrfs_caching_control
*next
;
4241 struct btrfs_caching_control
*caching_ctl
;
4242 struct btrfs_block_group_cache
*cache
;
4244 down_write(&fs_info
->extent_commit_sem
);
4246 list_for_each_entry_safe(caching_ctl
, next
,
4247 &fs_info
->caching_block_groups
, list
) {
4248 cache
= caching_ctl
->block_group
;
4249 if (block_group_cache_done(cache
)) {
4250 cache
->last_byte_to_unpin
= (u64
)-1;
4251 list_del_init(&caching_ctl
->list
);
4252 put_caching_control(caching_ctl
);
4254 cache
->last_byte_to_unpin
= caching_ctl
->progress
;
4258 if (fs_info
->pinned_extents
== &fs_info
->freed_extents
[0])
4259 fs_info
->pinned_extents
= &fs_info
->freed_extents
[1];
4261 fs_info
->pinned_extents
= &fs_info
->freed_extents
[0];
4263 up_write(&fs_info
->extent_commit_sem
);
4265 update_global_block_rsv(fs_info
);
4269 static int unpin_extent_range(struct btrfs_root
*root
, u64 start
, u64 end
)
4271 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4272 struct btrfs_block_group_cache
*cache
= NULL
;
4275 while (start
<= end
) {
4277 start
>= cache
->key
.objectid
+ cache
->key
.offset
) {
4279 btrfs_put_block_group(cache
);
4280 cache
= btrfs_lookup_block_group(fs_info
, start
);
4284 len
= cache
->key
.objectid
+ cache
->key
.offset
- start
;
4285 len
= min(len
, end
+ 1 - start
);
4287 if (start
< cache
->last_byte_to_unpin
) {
4288 len
= min(len
, cache
->last_byte_to_unpin
- start
);
4289 btrfs_add_free_space(cache
, start
, len
);
4294 spin_lock(&cache
->space_info
->lock
);
4295 spin_lock(&cache
->lock
);
4296 cache
->pinned
-= len
;
4297 cache
->space_info
->bytes_pinned
-= len
;
4299 cache
->space_info
->bytes_readonly
+= len
;
4300 } else if (cache
->reserved_pinned
> 0) {
4301 len
= min(len
, cache
->reserved_pinned
);
4302 cache
->reserved_pinned
-= len
;
4303 cache
->space_info
->bytes_reserved
+= len
;
4305 spin_unlock(&cache
->lock
);
4306 spin_unlock(&cache
->space_info
->lock
);
4310 btrfs_put_block_group(cache
);
4314 int btrfs_finish_extent_commit(struct btrfs_trans_handle
*trans
,
4315 struct btrfs_root
*root
)
4317 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4318 struct extent_io_tree
*unpin
;
4319 struct btrfs_block_rsv
*block_rsv
;
4320 struct btrfs_block_rsv
*next_rsv
;
4326 if (fs_info
->pinned_extents
== &fs_info
->freed_extents
[0])
4327 unpin
= &fs_info
->freed_extents
[1];
4329 unpin
= &fs_info
->freed_extents
[0];
4332 ret
= find_first_extent_bit(unpin
, 0, &start
, &end
,
4337 if (btrfs_test_opt(root
, DISCARD
))
4338 ret
= btrfs_discard_extent(root
, start
,
4339 end
+ 1 - start
, NULL
);
4341 clear_extent_dirty(unpin
, start
, end
, GFP_NOFS
);
4342 unpin_extent_range(root
, start
, end
);
4346 mutex_lock(&fs_info
->durable_block_rsv_mutex
);
4347 list_for_each_entry_safe(block_rsv
, next_rsv
,
4348 &fs_info
->durable_block_rsv_list
, list
) {
4350 idx
= trans
->transid
& 0x1;
4351 if (block_rsv
->freed
[idx
] > 0) {
4352 block_rsv_add_bytes(block_rsv
,
4353 block_rsv
->freed
[idx
], 0);
4354 block_rsv
->freed
[idx
] = 0;
4356 if (atomic_read(&block_rsv
->usage
) == 0) {
4357 btrfs_block_rsv_release(root
, block_rsv
, (u64
)-1);
4359 if (block_rsv
->freed
[0] == 0 &&
4360 block_rsv
->freed
[1] == 0) {
4361 list_del_init(&block_rsv
->list
);
4365 btrfs_block_rsv_release(root
, block_rsv
, 0);
4368 mutex_unlock(&fs_info
->durable_block_rsv_mutex
);
4373 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
4374 struct btrfs_root
*root
,
4375 u64 bytenr
, u64 num_bytes
, u64 parent
,
4376 u64 root_objectid
, u64 owner_objectid
,
4377 u64 owner_offset
, int refs_to_drop
,
4378 struct btrfs_delayed_extent_op
*extent_op
)
4380 struct btrfs_key key
;
4381 struct btrfs_path
*path
;
4382 struct btrfs_fs_info
*info
= root
->fs_info
;
4383 struct btrfs_root
*extent_root
= info
->extent_root
;
4384 struct extent_buffer
*leaf
;
4385 struct btrfs_extent_item
*ei
;
4386 struct btrfs_extent_inline_ref
*iref
;
4389 int extent_slot
= 0;
4390 int found_extent
= 0;
4395 path
= btrfs_alloc_path();
4400 path
->leave_spinning
= 1;
4402 is_data
= owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
;
4403 BUG_ON(!is_data
&& refs_to_drop
!= 1);
4405 ret
= lookup_extent_backref(trans
, extent_root
, path
, &iref
,
4406 bytenr
, num_bytes
, parent
,
4407 root_objectid
, owner_objectid
,
4410 extent_slot
= path
->slots
[0];
4411 while (extent_slot
>= 0) {
4412 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
4414 if (key
.objectid
!= bytenr
)
4416 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
4417 key
.offset
== num_bytes
) {
4421 if (path
->slots
[0] - extent_slot
> 5)
4425 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4426 item_size
= btrfs_item_size_nr(path
->nodes
[0], extent_slot
);
4427 if (found_extent
&& item_size
< sizeof(*ei
))
4430 if (!found_extent
) {
4432 ret
= remove_extent_backref(trans
, extent_root
, path
,
4436 btrfs_release_path(path
);
4437 path
->leave_spinning
= 1;
4439 key
.objectid
= bytenr
;
4440 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
4441 key
.offset
= num_bytes
;
4443 ret
= btrfs_search_slot(trans
, extent_root
,
4446 printk(KERN_ERR
"umm, got %d back from search"
4447 ", was looking for %llu\n", ret
,
4448 (unsigned long long)bytenr
);
4449 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
4452 extent_slot
= path
->slots
[0];
4455 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
4457 printk(KERN_ERR
"btrfs unable to find ref byte nr %llu "
4458 "parent %llu root %llu owner %llu offset %llu\n",
4459 (unsigned long long)bytenr
,
4460 (unsigned long long)parent
,
4461 (unsigned long long)root_objectid
,
4462 (unsigned long long)owner_objectid
,
4463 (unsigned long long)owner_offset
);
4466 leaf
= path
->nodes
[0];
4467 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
4468 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4469 if (item_size
< sizeof(*ei
)) {
4470 BUG_ON(found_extent
|| extent_slot
!= path
->slots
[0]);
4471 ret
= convert_extent_item_v0(trans
, extent_root
, path
,
4475 btrfs_release_path(path
);
4476 path
->leave_spinning
= 1;
4478 key
.objectid
= bytenr
;
4479 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
4480 key
.offset
= num_bytes
;
4482 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
,
4485 printk(KERN_ERR
"umm, got %d back from search"
4486 ", was looking for %llu\n", ret
,
4487 (unsigned long long)bytenr
);
4488 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
4491 extent_slot
= path
->slots
[0];
4492 leaf
= path
->nodes
[0];
4493 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
4496 BUG_ON(item_size
< sizeof(*ei
));
4497 ei
= btrfs_item_ptr(leaf
, extent_slot
,
4498 struct btrfs_extent_item
);
4499 if (owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
) {
4500 struct btrfs_tree_block_info
*bi
;
4501 BUG_ON(item_size
< sizeof(*ei
) + sizeof(*bi
));
4502 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
4503 WARN_ON(owner_objectid
!= btrfs_tree_block_level(leaf
, bi
));
4506 refs
= btrfs_extent_refs(leaf
, ei
);
4507 BUG_ON(refs
< refs_to_drop
);
4508 refs
-= refs_to_drop
;
4512 __run_delayed_extent_op(extent_op
, leaf
, ei
);
4514 * In the case of inline back ref, reference count will
4515 * be updated by remove_extent_backref
4518 BUG_ON(!found_extent
);
4520 btrfs_set_extent_refs(leaf
, ei
, refs
);
4521 btrfs_mark_buffer_dirty(leaf
);
4524 ret
= remove_extent_backref(trans
, extent_root
, path
,
4531 BUG_ON(is_data
&& refs_to_drop
!=
4532 extent_data_ref_count(root
, path
, iref
));
4534 BUG_ON(path
->slots
[0] != extent_slot
);
4536 BUG_ON(path
->slots
[0] != extent_slot
+ 1);
4537 path
->slots
[0] = extent_slot
;
4542 ret
= btrfs_del_items(trans
, extent_root
, path
, path
->slots
[0],
4545 btrfs_release_path(path
);
4548 ret
= btrfs_del_csums(trans
, root
, bytenr
, num_bytes
);
4551 invalidate_mapping_pages(info
->btree_inode
->i_mapping
,
4552 bytenr
>> PAGE_CACHE_SHIFT
,
4553 (bytenr
+ num_bytes
- 1) >> PAGE_CACHE_SHIFT
);
4556 ret
= update_block_group(trans
, root
, bytenr
, num_bytes
, 0);
4559 btrfs_free_path(path
);
4564 * when we free an block, it is possible (and likely) that we free the last
4565 * delayed ref for that extent as well. This searches the delayed ref tree for
4566 * a given extent, and if there are no other delayed refs to be processed, it
4567 * removes it from the tree.
4569 static noinline
int check_ref_cleanup(struct btrfs_trans_handle
*trans
,
4570 struct btrfs_root
*root
, u64 bytenr
)
4572 struct btrfs_delayed_ref_head
*head
;
4573 struct btrfs_delayed_ref_root
*delayed_refs
;
4574 struct btrfs_delayed_ref_node
*ref
;
4575 struct rb_node
*node
;
4578 delayed_refs
= &trans
->transaction
->delayed_refs
;
4579 spin_lock(&delayed_refs
->lock
);
4580 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
4584 node
= rb_prev(&head
->node
.rb_node
);
4588 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
4590 /* there are still entries for this ref, we can't drop it */
4591 if (ref
->bytenr
== bytenr
)
4594 if (head
->extent_op
) {
4595 if (!head
->must_insert_reserved
)
4597 kfree(head
->extent_op
);
4598 head
->extent_op
= NULL
;
4602 * waiting for the lock here would deadlock. If someone else has it
4603 * locked they are already in the process of dropping it anyway
4605 if (!mutex_trylock(&head
->mutex
))
4609 * at this point we have a head with no other entries. Go
4610 * ahead and process it.
4612 head
->node
.in_tree
= 0;
4613 rb_erase(&head
->node
.rb_node
, &delayed_refs
->root
);
4615 delayed_refs
->num_entries
--;
4618 * we don't take a ref on the node because we're removing it from the
4619 * tree, so we just steal the ref the tree was holding.
4621 delayed_refs
->num_heads
--;
4622 if (list_empty(&head
->cluster
))
4623 delayed_refs
->num_heads_ready
--;
4625 list_del_init(&head
->cluster
);
4626 spin_unlock(&delayed_refs
->lock
);
4628 BUG_ON(head
->extent_op
);
4629 if (head
->must_insert_reserved
)
4632 mutex_unlock(&head
->mutex
);
4633 btrfs_put_delayed_ref(&head
->node
);
4636 spin_unlock(&delayed_refs
->lock
);
4640 void btrfs_free_tree_block(struct btrfs_trans_handle
*trans
,
4641 struct btrfs_root
*root
,
4642 struct extent_buffer
*buf
,
4643 u64 parent
, int last_ref
)
4645 struct btrfs_block_rsv
*block_rsv
;
4646 struct btrfs_block_group_cache
*cache
= NULL
;
4649 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
4650 ret
= btrfs_add_delayed_tree_ref(trans
, buf
->start
, buf
->len
,
4651 parent
, root
->root_key
.objectid
,
4652 btrfs_header_level(buf
),
4653 BTRFS_DROP_DELAYED_REF
, NULL
);
4660 block_rsv
= get_block_rsv(trans
, root
);
4661 cache
= btrfs_lookup_block_group(root
->fs_info
, buf
->start
);
4662 if (block_rsv
->space_info
!= cache
->space_info
)
4665 if (btrfs_header_generation(buf
) == trans
->transid
) {
4666 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
4667 ret
= check_ref_cleanup(trans
, root
, buf
->start
);
4672 if (btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
)) {
4673 pin_down_extent(root
, cache
, buf
->start
, buf
->len
, 1);
4677 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY
, &buf
->bflags
));
4679 btrfs_add_free_space(cache
, buf
->start
, buf
->len
);
4680 ret
= btrfs_update_reserved_bytes(cache
, buf
->len
, 0, 0);
4681 if (ret
== -EAGAIN
) {
4682 /* block group became read-only */
4683 btrfs_update_reserved_bytes(cache
, buf
->len
, 0, 1);
4688 spin_lock(&block_rsv
->lock
);
4689 if (block_rsv
->reserved
< block_rsv
->size
) {
4690 block_rsv
->reserved
+= buf
->len
;
4693 spin_unlock(&block_rsv
->lock
);
4696 spin_lock(&cache
->space_info
->lock
);
4697 cache
->space_info
->bytes_reserved
-= buf
->len
;
4698 cache
->space_info
->reservation_progress
++;
4699 spin_unlock(&cache
->space_info
->lock
);
4704 if (block_rsv
->durable
&& !cache
->ro
) {
4706 spin_lock(&cache
->lock
);
4708 cache
->reserved_pinned
+= buf
->len
;
4711 spin_unlock(&cache
->lock
);
4714 spin_lock(&block_rsv
->lock
);
4715 block_rsv
->freed
[trans
->transid
& 0x1] += buf
->len
;
4716 spin_unlock(&block_rsv
->lock
);
4721 * Deleting the buffer, clear the corrupt flag since it doesn't matter
4724 clear_bit(EXTENT_BUFFER_CORRUPT
, &buf
->bflags
);
4725 btrfs_put_block_group(cache
);
4728 int btrfs_free_extent(struct btrfs_trans_handle
*trans
,
4729 struct btrfs_root
*root
,
4730 u64 bytenr
, u64 num_bytes
, u64 parent
,
4731 u64 root_objectid
, u64 owner
, u64 offset
)
4736 * tree log blocks never actually go into the extent allocation
4737 * tree, just update pinning info and exit early.
4739 if (root_objectid
== BTRFS_TREE_LOG_OBJECTID
) {
4740 WARN_ON(owner
>= BTRFS_FIRST_FREE_OBJECTID
);
4741 /* unlocks the pinned mutex */
4742 btrfs_pin_extent(root
, bytenr
, num_bytes
, 1);
4744 } else if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
4745 ret
= btrfs_add_delayed_tree_ref(trans
, bytenr
, num_bytes
,
4746 parent
, root_objectid
, (int)owner
,
4747 BTRFS_DROP_DELAYED_REF
, NULL
);
4750 ret
= btrfs_add_delayed_data_ref(trans
, bytenr
, num_bytes
,
4751 parent
, root_objectid
, owner
,
4752 offset
, BTRFS_DROP_DELAYED_REF
, NULL
);
4758 static u64
stripe_align(struct btrfs_root
*root
, u64 val
)
4760 u64 mask
= ((u64
)root
->stripesize
- 1);
4761 u64 ret
= (val
+ mask
) & ~mask
;
4766 * when we wait for progress in the block group caching, its because
4767 * our allocation attempt failed at least once. So, we must sleep
4768 * and let some progress happen before we try again.
4770 * This function will sleep at least once waiting for new free space to
4771 * show up, and then it will check the block group free space numbers
4772 * for our min num_bytes. Another option is to have it go ahead
4773 * and look in the rbtree for a free extent of a given size, but this
4777 wait_block_group_cache_progress(struct btrfs_block_group_cache
*cache
,
4780 struct btrfs_caching_control
*caching_ctl
;
4783 caching_ctl
= get_caching_control(cache
);
4787 wait_event(caching_ctl
->wait
, block_group_cache_done(cache
) ||
4788 (cache
->free_space_ctl
->free_space
>= num_bytes
));
4790 put_caching_control(caching_ctl
);
4795 wait_block_group_cache_done(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
));
4806 put_caching_control(caching_ctl
);
4810 static int get_block_group_index(struct btrfs_block_group_cache
*cache
)
4813 if (cache
->flags
& BTRFS_BLOCK_GROUP_RAID10
)
4815 else if (cache
->flags
& BTRFS_BLOCK_GROUP_RAID1
)
4817 else if (cache
->flags
& BTRFS_BLOCK_GROUP_DUP
)
4819 else if (cache
->flags
& BTRFS_BLOCK_GROUP_RAID0
)
4826 enum btrfs_loop_type
{
4827 LOOP_FIND_IDEAL
= 0,
4828 LOOP_CACHING_NOWAIT
= 1,
4829 LOOP_CACHING_WAIT
= 2,
4830 LOOP_ALLOC_CHUNK
= 3,
4831 LOOP_NO_EMPTY_SIZE
= 4,
4835 * walks the btree of allocated extents and find a hole of a given size.
4836 * The key ins is changed to record the hole:
4837 * ins->objectid == block start
4838 * ins->flags = BTRFS_EXTENT_ITEM_KEY
4839 * ins->offset == number of blocks
4840 * Any available blocks before search_start are skipped.
4842 static noinline
int find_free_extent(struct btrfs_trans_handle
*trans
,
4843 struct btrfs_root
*orig_root
,
4844 u64 num_bytes
, u64 empty_size
,
4845 u64 search_start
, u64 search_end
,
4846 u64 hint_byte
, struct btrfs_key
*ins
,
4850 struct btrfs_root
*root
= orig_root
->fs_info
->extent_root
;
4851 struct btrfs_free_cluster
*last_ptr
= NULL
;
4852 struct btrfs_block_group_cache
*block_group
= NULL
;
4853 int empty_cluster
= 2 * 1024 * 1024;
4854 int allowed_chunk_alloc
= 0;
4855 int done_chunk_alloc
= 0;
4856 struct btrfs_space_info
*space_info
;
4857 int last_ptr_loop
= 0;
4860 bool found_uncached_bg
= false;
4861 bool failed_cluster_refill
= false;
4862 bool failed_alloc
= false;
4863 bool use_cluster
= true;
4864 u64 ideal_cache_percent
= 0;
4865 u64 ideal_cache_offset
= 0;
4867 WARN_ON(num_bytes
< root
->sectorsize
);
4868 btrfs_set_key_type(ins
, BTRFS_EXTENT_ITEM_KEY
);
4872 space_info
= __find_space_info(root
->fs_info
, data
);
4874 printk(KERN_ERR
"No space info for %llu\n", data
);
4879 * If the space info is for both data and metadata it means we have a
4880 * small filesystem and we can't use the clustering stuff.
4882 if (btrfs_mixed_space_info(space_info
))
4883 use_cluster
= false;
4885 if (orig_root
->ref_cows
|| empty_size
)
4886 allowed_chunk_alloc
= 1;
4888 if (data
& BTRFS_BLOCK_GROUP_METADATA
&& use_cluster
) {
4889 last_ptr
= &root
->fs_info
->meta_alloc_cluster
;
4890 if (!btrfs_test_opt(root
, SSD
))
4891 empty_cluster
= 64 * 1024;
4894 if ((data
& BTRFS_BLOCK_GROUP_DATA
) && use_cluster
&&
4895 btrfs_test_opt(root
, SSD
)) {
4896 last_ptr
= &root
->fs_info
->data_alloc_cluster
;
4900 spin_lock(&last_ptr
->lock
);
4901 if (last_ptr
->block_group
)
4902 hint_byte
= last_ptr
->window_start
;
4903 spin_unlock(&last_ptr
->lock
);
4906 search_start
= max(search_start
, first_logical_byte(root
, 0));
4907 search_start
= max(search_start
, hint_byte
);
4912 if (search_start
== hint_byte
) {
4914 block_group
= btrfs_lookup_block_group(root
->fs_info
,
4917 * we don't want to use the block group if it doesn't match our
4918 * allocation bits, or if its not cached.
4920 * However if we are re-searching with an ideal block group
4921 * picked out then we don't care that the block group is cached.
4923 if (block_group
&& block_group_bits(block_group
, data
) &&
4924 (block_group
->cached
!= BTRFS_CACHE_NO
||
4925 search_start
== ideal_cache_offset
)) {
4926 down_read(&space_info
->groups_sem
);
4927 if (list_empty(&block_group
->list
) ||
4930 * someone is removing this block group,
4931 * we can't jump into the have_block_group
4932 * target because our list pointers are not
4935 btrfs_put_block_group(block_group
);
4936 up_read(&space_info
->groups_sem
);
4938 index
= get_block_group_index(block_group
);
4939 goto have_block_group
;
4941 } else if (block_group
) {
4942 btrfs_put_block_group(block_group
);
4946 down_read(&space_info
->groups_sem
);
4947 list_for_each_entry(block_group
, &space_info
->block_groups
[index
],
4952 btrfs_get_block_group(block_group
);
4953 search_start
= block_group
->key
.objectid
;
4956 * this can happen if we end up cycling through all the
4957 * raid types, but we want to make sure we only allocate
4958 * for the proper type.
4960 if (!block_group_bits(block_group
, data
)) {
4961 u64 extra
= BTRFS_BLOCK_GROUP_DUP
|
4962 BTRFS_BLOCK_GROUP_RAID1
|
4963 BTRFS_BLOCK_GROUP_RAID10
;
4966 * if they asked for extra copies and this block group
4967 * doesn't provide them, bail. This does allow us to
4968 * fill raid0 from raid1.
4970 if ((data
& extra
) && !(block_group
->flags
& extra
))
4975 if (unlikely(block_group
->cached
== BTRFS_CACHE_NO
)) {
4978 ret
= cache_block_group(block_group
, trans
,
4980 if (block_group
->cached
== BTRFS_CACHE_FINISHED
)
4981 goto have_block_group
;
4983 free_percent
= btrfs_block_group_used(&block_group
->item
);
4984 free_percent
*= 100;
4985 free_percent
= div64_u64(free_percent
,
4986 block_group
->key
.offset
);
4987 free_percent
= 100 - free_percent
;
4988 if (free_percent
> ideal_cache_percent
&&
4989 likely(!block_group
->ro
)) {
4990 ideal_cache_offset
= block_group
->key
.objectid
;
4991 ideal_cache_percent
= free_percent
;
4995 * The caching workers are limited to 2 threads, so we
4996 * can queue as much work as we care to.
4998 if (loop
> LOOP_FIND_IDEAL
) {
4999 ret
= cache_block_group(block_group
, trans
,
5003 found_uncached_bg
= true;
5006 * If loop is set for cached only, try the next block
5009 if (loop
== LOOP_FIND_IDEAL
)
5013 cached
= block_group_cache_done(block_group
);
5014 if (unlikely(!cached
))
5015 found_uncached_bg
= true;
5017 if (unlikely(block_group
->ro
))
5020 spin_lock(&block_group
->free_space_ctl
->tree_lock
);
5022 block_group
->free_space_ctl
->free_space
<
5023 num_bytes
+ empty_size
) {
5024 spin_unlock(&block_group
->free_space_ctl
->tree_lock
);
5027 spin_unlock(&block_group
->free_space_ctl
->tree_lock
);
5030 * Ok we want to try and use the cluster allocator, so lets look
5031 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
5032 * have tried the cluster allocator plenty of times at this
5033 * point and not have found anything, so we are likely way too
5034 * fragmented for the clustering stuff to find anything, so lets
5035 * just skip it and let the allocator find whatever block it can
5038 if (last_ptr
&& loop
< LOOP_NO_EMPTY_SIZE
) {
5040 * the refill lock keeps out other
5041 * people trying to start a new cluster
5043 spin_lock(&last_ptr
->refill_lock
);
5044 if (last_ptr
->block_group
&&
5045 (last_ptr
->block_group
->ro
||
5046 !block_group_bits(last_ptr
->block_group
, data
))) {
5048 goto refill_cluster
;
5051 offset
= btrfs_alloc_from_cluster(block_group
, last_ptr
,
5052 num_bytes
, search_start
);
5054 /* we have a block, we're done */
5055 spin_unlock(&last_ptr
->refill_lock
);
5059 spin_lock(&last_ptr
->lock
);
5061 * whoops, this cluster doesn't actually point to
5062 * this block group. Get a ref on the block
5063 * group is does point to and try again
5065 if (!last_ptr_loop
&& last_ptr
->block_group
&&
5066 last_ptr
->block_group
!= block_group
) {
5068 btrfs_put_block_group(block_group
);
5069 block_group
= last_ptr
->block_group
;
5070 btrfs_get_block_group(block_group
);
5071 spin_unlock(&last_ptr
->lock
);
5072 spin_unlock(&last_ptr
->refill_lock
);
5075 search_start
= block_group
->key
.objectid
;
5077 * we know this block group is properly
5078 * in the list because
5079 * btrfs_remove_block_group, drops the
5080 * cluster before it removes the block
5081 * group from the list
5083 goto have_block_group
;
5085 spin_unlock(&last_ptr
->lock
);
5088 * this cluster didn't work out, free it and
5091 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
5095 /* allocate a cluster in this block group */
5096 ret
= btrfs_find_space_cluster(trans
, root
,
5097 block_group
, last_ptr
,
5099 empty_cluster
+ empty_size
);
5102 * now pull our allocation out of this
5105 offset
= btrfs_alloc_from_cluster(block_group
,
5106 last_ptr
, num_bytes
,
5109 /* we found one, proceed */
5110 spin_unlock(&last_ptr
->refill_lock
);
5113 } else if (!cached
&& loop
> LOOP_CACHING_NOWAIT
5114 && !failed_cluster_refill
) {
5115 spin_unlock(&last_ptr
->refill_lock
);
5117 failed_cluster_refill
= true;
5118 wait_block_group_cache_progress(block_group
,
5119 num_bytes
+ empty_cluster
+ empty_size
);
5120 goto have_block_group
;
5124 * at this point we either didn't find a cluster
5125 * or we weren't able to allocate a block from our
5126 * cluster. Free the cluster we've been trying
5127 * to use, and go to the next block group
5129 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
5130 spin_unlock(&last_ptr
->refill_lock
);
5134 offset
= btrfs_find_space_for_alloc(block_group
, search_start
,
5135 num_bytes
, empty_size
);
5137 * If we didn't find a chunk, and we haven't failed on this
5138 * block group before, and this block group is in the middle of
5139 * caching and we are ok with waiting, then go ahead and wait
5140 * for progress to be made, and set failed_alloc to true.
5142 * If failed_alloc is true then we've already waited on this
5143 * block group once and should move on to the next block group.
5145 if (!offset
&& !failed_alloc
&& !cached
&&
5146 loop
> LOOP_CACHING_NOWAIT
) {
5147 wait_block_group_cache_progress(block_group
,
5148 num_bytes
+ empty_size
);
5149 failed_alloc
= true;
5150 goto have_block_group
;
5151 } else if (!offset
) {
5155 search_start
= stripe_align(root
, offset
);
5156 /* move on to the next group */
5157 if (search_start
+ num_bytes
>= search_end
) {
5158 btrfs_add_free_space(block_group
, offset
, num_bytes
);
5162 /* move on to the next group */
5163 if (search_start
+ num_bytes
>
5164 block_group
->key
.objectid
+ block_group
->key
.offset
) {
5165 btrfs_add_free_space(block_group
, offset
, num_bytes
);
5169 ins
->objectid
= search_start
;
5170 ins
->offset
= num_bytes
;
5172 if (offset
< search_start
)
5173 btrfs_add_free_space(block_group
, offset
,
5174 search_start
- offset
);
5175 BUG_ON(offset
> search_start
);
5177 ret
= btrfs_update_reserved_bytes(block_group
, num_bytes
, 1,
5178 (data
& BTRFS_BLOCK_GROUP_DATA
));
5179 if (ret
== -EAGAIN
) {
5180 btrfs_add_free_space(block_group
, offset
, num_bytes
);
5184 /* we are all good, lets return */
5185 ins
->objectid
= search_start
;
5186 ins
->offset
= num_bytes
;
5188 if (offset
< search_start
)
5189 btrfs_add_free_space(block_group
, offset
,
5190 search_start
- offset
);
5191 BUG_ON(offset
> search_start
);
5192 btrfs_put_block_group(block_group
);
5195 failed_cluster_refill
= false;
5196 failed_alloc
= false;
5197 BUG_ON(index
!= get_block_group_index(block_group
));
5198 btrfs_put_block_group(block_group
);
5200 up_read(&space_info
->groups_sem
);
5202 if (!ins
->objectid
&& ++index
< BTRFS_NR_RAID_TYPES
)
5205 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5206 * for them to make caching progress. Also
5207 * determine the best possible bg to cache
5208 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5209 * caching kthreads as we move along
5210 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5211 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5212 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5215 if (!ins
->objectid
&& loop
< LOOP_NO_EMPTY_SIZE
) {
5217 if (loop
== LOOP_FIND_IDEAL
&& found_uncached_bg
) {
5218 found_uncached_bg
= false;
5220 if (!ideal_cache_percent
)
5224 * 1 of the following 2 things have happened so far
5226 * 1) We found an ideal block group for caching that
5227 * is mostly full and will cache quickly, so we might
5228 * as well wait for it.
5230 * 2) We searched for cached only and we didn't find
5231 * anything, and we didn't start any caching kthreads
5232 * either, so chances are we will loop through and
5233 * start a couple caching kthreads, and then come back
5234 * around and just wait for them. This will be slower
5235 * because we will have 2 caching kthreads reading at
5236 * the same time when we could have just started one
5237 * and waited for it to get far enough to give us an
5238 * allocation, so go ahead and go to the wait caching
5241 loop
= LOOP_CACHING_WAIT
;
5242 search_start
= ideal_cache_offset
;
5243 ideal_cache_percent
= 0;
5245 } else if (loop
== LOOP_FIND_IDEAL
) {
5247 * Didn't find a uncached bg, wait on anything we find
5250 loop
= LOOP_CACHING_WAIT
;
5256 if (loop
== LOOP_ALLOC_CHUNK
) {
5257 if (allowed_chunk_alloc
) {
5258 ret
= do_chunk_alloc(trans
, root
, num_bytes
+
5259 2 * 1024 * 1024, data
,
5260 CHUNK_ALLOC_LIMITED
);
5261 allowed_chunk_alloc
= 0;
5263 done_chunk_alloc
= 1;
5264 } else if (!done_chunk_alloc
&&
5265 space_info
->force_alloc
==
5266 CHUNK_ALLOC_NO_FORCE
) {
5267 space_info
->force_alloc
= CHUNK_ALLOC_LIMITED
;
5271 * We didn't allocate a chunk, go ahead and drop the
5272 * empty size and loop again.
5274 if (!done_chunk_alloc
)
5275 loop
= LOOP_NO_EMPTY_SIZE
;
5278 if (loop
== LOOP_NO_EMPTY_SIZE
) {
5284 } else if (!ins
->objectid
) {
5286 } else if (ins
->objectid
) {
5293 static void dump_space_info(struct btrfs_space_info
*info
, u64 bytes
,
5294 int dump_block_groups
)
5296 struct btrfs_block_group_cache
*cache
;
5299 spin_lock(&info
->lock
);
5300 printk(KERN_INFO
"space_info has %llu free, is %sfull\n",
5301 (unsigned long long)(info
->total_bytes
- info
->bytes_used
-
5302 info
->bytes_pinned
- info
->bytes_reserved
-
5303 info
->bytes_readonly
),
5304 (info
->full
) ? "" : "not ");
5305 printk(KERN_INFO
"space_info total=%llu, used=%llu, pinned=%llu, "
5306 "reserved=%llu, may_use=%llu, readonly=%llu\n",
5307 (unsigned long long)info
->total_bytes
,
5308 (unsigned long long)info
->bytes_used
,
5309 (unsigned long long)info
->bytes_pinned
,
5310 (unsigned long long)info
->bytes_reserved
,
5311 (unsigned long long)info
->bytes_may_use
,
5312 (unsigned long long)info
->bytes_readonly
);
5313 spin_unlock(&info
->lock
);
5315 if (!dump_block_groups
)
5318 down_read(&info
->groups_sem
);
5320 list_for_each_entry(cache
, &info
->block_groups
[index
], list
) {
5321 spin_lock(&cache
->lock
);
5322 printk(KERN_INFO
"block group %llu has %llu bytes, %llu used "
5323 "%llu pinned %llu reserved\n",
5324 (unsigned long long)cache
->key
.objectid
,
5325 (unsigned long long)cache
->key
.offset
,
5326 (unsigned long long)btrfs_block_group_used(&cache
->item
),
5327 (unsigned long long)cache
->pinned
,
5328 (unsigned long long)cache
->reserved
);
5329 btrfs_dump_free_space(cache
, bytes
);
5330 spin_unlock(&cache
->lock
);
5332 if (++index
< BTRFS_NR_RAID_TYPES
)
5334 up_read(&info
->groups_sem
);
5337 int btrfs_reserve_extent(struct btrfs_trans_handle
*trans
,
5338 struct btrfs_root
*root
,
5339 u64 num_bytes
, u64 min_alloc_size
,
5340 u64 empty_size
, u64 hint_byte
,
5341 u64 search_end
, struct btrfs_key
*ins
,
5345 u64 search_start
= 0;
5347 data
= btrfs_get_alloc_profile(root
, data
);
5350 * the only place that sets empty_size is btrfs_realloc_node, which
5351 * is not called recursively on allocations
5353 if (empty_size
|| root
->ref_cows
)
5354 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
5355 num_bytes
+ 2 * 1024 * 1024, data
,
5356 CHUNK_ALLOC_NO_FORCE
);
5358 WARN_ON(num_bytes
< root
->sectorsize
);
5359 ret
= find_free_extent(trans
, root
, num_bytes
, empty_size
,
5360 search_start
, search_end
, hint_byte
,
5363 if (ret
== -ENOSPC
&& num_bytes
> min_alloc_size
) {
5364 num_bytes
= num_bytes
>> 1;
5365 num_bytes
= num_bytes
& ~(root
->sectorsize
- 1);
5366 num_bytes
= max(num_bytes
, min_alloc_size
);
5367 do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
5368 num_bytes
, data
, CHUNK_ALLOC_FORCE
);
5371 if (ret
== -ENOSPC
&& btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
5372 struct btrfs_space_info
*sinfo
;
5374 sinfo
= __find_space_info(root
->fs_info
, data
);
5375 printk(KERN_ERR
"btrfs allocation failed flags %llu, "
5376 "wanted %llu\n", (unsigned long long)data
,
5377 (unsigned long long)num_bytes
);
5378 dump_space_info(sinfo
, num_bytes
, 1);
5381 trace_btrfs_reserved_extent_alloc(root
, ins
->objectid
, ins
->offset
);
5386 int btrfs_free_reserved_extent(struct btrfs_root
*root
, u64 start
, u64 len
)
5388 struct btrfs_block_group_cache
*cache
;
5391 cache
= btrfs_lookup_block_group(root
->fs_info
, start
);
5393 printk(KERN_ERR
"Unable to find block group for %llu\n",
5394 (unsigned long long)start
);
5398 if (btrfs_test_opt(root
, DISCARD
))
5399 ret
= btrfs_discard_extent(root
, start
, len
, NULL
);
5401 btrfs_add_free_space(cache
, start
, len
);
5402 btrfs_update_reserved_bytes(cache
, len
, 0, 1);
5403 btrfs_put_block_group(cache
);
5405 trace_btrfs_reserved_extent_free(root
, start
, len
);
5410 static int alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
5411 struct btrfs_root
*root
,
5412 u64 parent
, u64 root_objectid
,
5413 u64 flags
, u64 owner
, u64 offset
,
5414 struct btrfs_key
*ins
, int ref_mod
)
5417 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5418 struct btrfs_extent_item
*extent_item
;
5419 struct btrfs_extent_inline_ref
*iref
;
5420 struct btrfs_path
*path
;
5421 struct extent_buffer
*leaf
;
5426 type
= BTRFS_SHARED_DATA_REF_KEY
;
5428 type
= BTRFS_EXTENT_DATA_REF_KEY
;
5430 size
= sizeof(*extent_item
) + btrfs_extent_inline_ref_size(type
);
5432 path
= btrfs_alloc_path();
5436 path
->leave_spinning
= 1;
5437 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
5441 leaf
= path
->nodes
[0];
5442 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
5443 struct btrfs_extent_item
);
5444 btrfs_set_extent_refs(leaf
, extent_item
, ref_mod
);
5445 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
5446 btrfs_set_extent_flags(leaf
, extent_item
,
5447 flags
| BTRFS_EXTENT_FLAG_DATA
);
5449 iref
= (struct btrfs_extent_inline_ref
*)(extent_item
+ 1);
5450 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
5452 struct btrfs_shared_data_ref
*ref
;
5453 ref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
5454 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
5455 btrfs_set_shared_data_ref_count(leaf
, ref
, ref_mod
);
5457 struct btrfs_extent_data_ref
*ref
;
5458 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
5459 btrfs_set_extent_data_ref_root(leaf
, ref
, root_objectid
);
5460 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
5461 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
5462 btrfs_set_extent_data_ref_count(leaf
, ref
, ref_mod
);
5465 btrfs_mark_buffer_dirty(path
->nodes
[0]);
5466 btrfs_free_path(path
);
5468 ret
= update_block_group(trans
, root
, ins
->objectid
, ins
->offset
, 1);
5470 printk(KERN_ERR
"btrfs update block group failed for %llu "
5471 "%llu\n", (unsigned long long)ins
->objectid
,
5472 (unsigned long long)ins
->offset
);
5478 static int alloc_reserved_tree_block(struct btrfs_trans_handle
*trans
,
5479 struct btrfs_root
*root
,
5480 u64 parent
, u64 root_objectid
,
5481 u64 flags
, struct btrfs_disk_key
*key
,
5482 int level
, struct btrfs_key
*ins
)
5485 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5486 struct btrfs_extent_item
*extent_item
;
5487 struct btrfs_tree_block_info
*block_info
;
5488 struct btrfs_extent_inline_ref
*iref
;
5489 struct btrfs_path
*path
;
5490 struct extent_buffer
*leaf
;
5491 u32 size
= sizeof(*extent_item
) + sizeof(*block_info
) + sizeof(*iref
);
5493 path
= btrfs_alloc_path();
5496 path
->leave_spinning
= 1;
5497 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
5501 leaf
= path
->nodes
[0];
5502 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
5503 struct btrfs_extent_item
);
5504 btrfs_set_extent_refs(leaf
, extent_item
, 1);
5505 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
5506 btrfs_set_extent_flags(leaf
, extent_item
,
5507 flags
| BTRFS_EXTENT_FLAG_TREE_BLOCK
);
5508 block_info
= (struct btrfs_tree_block_info
*)(extent_item
+ 1);
5510 btrfs_set_tree_block_key(leaf
, block_info
, key
);
5511 btrfs_set_tree_block_level(leaf
, block_info
, level
);
5513 iref
= (struct btrfs_extent_inline_ref
*)(block_info
+ 1);
5515 BUG_ON(!(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
));
5516 btrfs_set_extent_inline_ref_type(leaf
, iref
,
5517 BTRFS_SHARED_BLOCK_REF_KEY
);
5518 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
5520 btrfs_set_extent_inline_ref_type(leaf
, iref
,
5521 BTRFS_TREE_BLOCK_REF_KEY
);
5522 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
5525 btrfs_mark_buffer_dirty(leaf
);
5526 btrfs_free_path(path
);
5528 ret
= update_block_group(trans
, root
, ins
->objectid
, ins
->offset
, 1);
5530 printk(KERN_ERR
"btrfs update block group failed for %llu "
5531 "%llu\n", (unsigned long long)ins
->objectid
,
5532 (unsigned long long)ins
->offset
);
5538 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
5539 struct btrfs_root
*root
,
5540 u64 root_objectid
, u64 owner
,
5541 u64 offset
, struct btrfs_key
*ins
)
5545 BUG_ON(root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
5547 ret
= btrfs_add_delayed_data_ref(trans
, ins
->objectid
, ins
->offset
,
5548 0, root_objectid
, owner
, offset
,
5549 BTRFS_ADD_DELAYED_EXTENT
, NULL
);
5554 * this is used by the tree logging recovery code. It records that
5555 * an extent has been allocated and makes sure to clear the free
5556 * space cache bits as well
5558 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle
*trans
,
5559 struct btrfs_root
*root
,
5560 u64 root_objectid
, u64 owner
, u64 offset
,
5561 struct btrfs_key
*ins
)
5564 struct btrfs_block_group_cache
*block_group
;
5565 struct btrfs_caching_control
*caching_ctl
;
5566 u64 start
= ins
->objectid
;
5567 u64 num_bytes
= ins
->offset
;
5569 block_group
= btrfs_lookup_block_group(root
->fs_info
, ins
->objectid
);
5570 cache_block_group(block_group
, trans
, NULL
, 0);
5571 caching_ctl
= get_caching_control(block_group
);
5574 BUG_ON(!block_group_cache_done(block_group
));
5575 ret
= btrfs_remove_free_space(block_group
, start
, num_bytes
);
5578 mutex_lock(&caching_ctl
->mutex
);
5580 if (start
>= caching_ctl
->progress
) {
5581 ret
= add_excluded_extent(root
, start
, num_bytes
);
5583 } else if (start
+ num_bytes
<= caching_ctl
->progress
) {
5584 ret
= btrfs_remove_free_space(block_group
,
5588 num_bytes
= caching_ctl
->progress
- start
;
5589 ret
= btrfs_remove_free_space(block_group
,
5593 start
= caching_ctl
->progress
;
5594 num_bytes
= ins
->objectid
+ ins
->offset
-
5595 caching_ctl
->progress
;
5596 ret
= add_excluded_extent(root
, start
, num_bytes
);
5600 mutex_unlock(&caching_ctl
->mutex
);
5601 put_caching_control(caching_ctl
);
5604 ret
= btrfs_update_reserved_bytes(block_group
, ins
->offset
, 1, 1);
5606 btrfs_put_block_group(block_group
);
5607 ret
= alloc_reserved_file_extent(trans
, root
, 0, root_objectid
,
5608 0, owner
, offset
, ins
, 1);
5612 struct extent_buffer
*btrfs_init_new_buffer(struct btrfs_trans_handle
*trans
,
5613 struct btrfs_root
*root
,
5614 u64 bytenr
, u32 blocksize
,
5617 struct extent_buffer
*buf
;
5619 buf
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
5621 return ERR_PTR(-ENOMEM
);
5622 btrfs_set_header_generation(buf
, trans
->transid
);
5623 btrfs_set_buffer_lockdep_class(buf
, level
);
5624 btrfs_tree_lock(buf
);
5625 clean_tree_block(trans
, root
, buf
);
5627 btrfs_set_lock_blocking(buf
);
5628 btrfs_set_buffer_uptodate(buf
);
5630 if (root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
) {
5632 * we allow two log transactions at a time, use different
5633 * EXENT bit to differentiate dirty pages.
5635 if (root
->log_transid
% 2 == 0)
5636 set_extent_dirty(&root
->dirty_log_pages
, buf
->start
,
5637 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
5639 set_extent_new(&root
->dirty_log_pages
, buf
->start
,
5640 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
5642 set_extent_dirty(&trans
->transaction
->dirty_pages
, buf
->start
,
5643 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
5645 trans
->blocks_used
++;
5646 /* this returns a buffer locked for blocking */
5650 static struct btrfs_block_rsv
*
5651 use_block_rsv(struct btrfs_trans_handle
*trans
,
5652 struct btrfs_root
*root
, u32 blocksize
)
5654 struct btrfs_block_rsv
*block_rsv
;
5655 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
5658 block_rsv
= get_block_rsv(trans
, root
);
5660 if (block_rsv
->size
== 0) {
5661 ret
= reserve_metadata_bytes(trans
, root
, block_rsv
,
5664 * If we couldn't reserve metadata bytes try and use some from
5665 * the global reserve.
5667 if (ret
&& block_rsv
!= global_rsv
) {
5668 ret
= block_rsv_use_bytes(global_rsv
, blocksize
);
5671 return ERR_PTR(ret
);
5673 return ERR_PTR(ret
);
5678 ret
= block_rsv_use_bytes(block_rsv
, blocksize
);
5683 ret
= reserve_metadata_bytes(trans
, root
, block_rsv
, blocksize
,
5686 spin_lock(&block_rsv
->lock
);
5687 block_rsv
->size
+= blocksize
;
5688 spin_unlock(&block_rsv
->lock
);
5690 } else if (ret
&& block_rsv
!= global_rsv
) {
5691 ret
= block_rsv_use_bytes(global_rsv
, blocksize
);
5697 return ERR_PTR(-ENOSPC
);
5700 static void unuse_block_rsv(struct btrfs_block_rsv
*block_rsv
, u32 blocksize
)
5702 block_rsv_add_bytes(block_rsv
, blocksize
, 0);
5703 block_rsv_release_bytes(block_rsv
, NULL
, 0);
5707 * finds a free extent and does all the dirty work required for allocation
5708 * returns the key for the extent through ins, and a tree buffer for
5709 * the first block of the extent through buf.
5711 * returns the tree buffer or NULL.
5713 struct extent_buffer
*btrfs_alloc_free_block(struct btrfs_trans_handle
*trans
,
5714 struct btrfs_root
*root
, u32 blocksize
,
5715 u64 parent
, u64 root_objectid
,
5716 struct btrfs_disk_key
*key
, int level
,
5717 u64 hint
, u64 empty_size
)
5719 struct btrfs_key ins
;
5720 struct btrfs_block_rsv
*block_rsv
;
5721 struct extent_buffer
*buf
;
5726 block_rsv
= use_block_rsv(trans
, root
, blocksize
);
5727 if (IS_ERR(block_rsv
))
5728 return ERR_CAST(block_rsv
);
5730 ret
= btrfs_reserve_extent(trans
, root
, blocksize
, blocksize
,
5731 empty_size
, hint
, (u64
)-1, &ins
, 0);
5733 unuse_block_rsv(block_rsv
, blocksize
);
5734 return ERR_PTR(ret
);
5737 buf
= btrfs_init_new_buffer(trans
, root
, ins
.objectid
,
5739 BUG_ON(IS_ERR(buf
));
5741 if (root_objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
5743 parent
= ins
.objectid
;
5744 flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
5748 if (root_objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
5749 struct btrfs_delayed_extent_op
*extent_op
;
5750 extent_op
= kmalloc(sizeof(*extent_op
), GFP_NOFS
);
5753 memcpy(&extent_op
->key
, key
, sizeof(extent_op
->key
));
5755 memset(&extent_op
->key
, 0, sizeof(extent_op
->key
));
5756 extent_op
->flags_to_set
= flags
;
5757 extent_op
->update_key
= 1;
5758 extent_op
->update_flags
= 1;
5759 extent_op
->is_data
= 0;
5761 ret
= btrfs_add_delayed_tree_ref(trans
, ins
.objectid
,
5762 ins
.offset
, parent
, root_objectid
,
5763 level
, BTRFS_ADD_DELAYED_EXTENT
,
5770 struct walk_control
{
5771 u64 refs
[BTRFS_MAX_LEVEL
];
5772 u64 flags
[BTRFS_MAX_LEVEL
];
5773 struct btrfs_key update_progress
;
5783 #define DROP_REFERENCE 1
5784 #define UPDATE_BACKREF 2
5786 static noinline
void reada_walk_down(struct btrfs_trans_handle
*trans
,
5787 struct btrfs_root
*root
,
5788 struct walk_control
*wc
,
5789 struct btrfs_path
*path
)
5797 struct btrfs_key key
;
5798 struct extent_buffer
*eb
;
5803 if (path
->slots
[wc
->level
] < wc
->reada_slot
) {
5804 wc
->reada_count
= wc
->reada_count
* 2 / 3;
5805 wc
->reada_count
= max(wc
->reada_count
, 2);
5807 wc
->reada_count
= wc
->reada_count
* 3 / 2;
5808 wc
->reada_count
= min_t(int, wc
->reada_count
,
5809 BTRFS_NODEPTRS_PER_BLOCK(root
));
5812 eb
= path
->nodes
[wc
->level
];
5813 nritems
= btrfs_header_nritems(eb
);
5814 blocksize
= btrfs_level_size(root
, wc
->level
- 1);
5816 for (slot
= path
->slots
[wc
->level
]; slot
< nritems
; slot
++) {
5817 if (nread
>= wc
->reada_count
)
5821 bytenr
= btrfs_node_blockptr(eb
, slot
);
5822 generation
= btrfs_node_ptr_generation(eb
, slot
);
5824 if (slot
== path
->slots
[wc
->level
])
5827 if (wc
->stage
== UPDATE_BACKREF
&&
5828 generation
<= root
->root_key
.offset
)
5831 /* We don't lock the tree block, it's OK to be racy here */
5832 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
, blocksize
,
5837 if (wc
->stage
== DROP_REFERENCE
) {
5841 if (wc
->level
== 1 &&
5842 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
5844 if (!wc
->update_ref
||
5845 generation
<= root
->root_key
.offset
)
5847 btrfs_node_key_to_cpu(eb
, &key
, slot
);
5848 ret
= btrfs_comp_cpu_keys(&key
,
5849 &wc
->update_progress
);
5853 if (wc
->level
== 1 &&
5854 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
5858 ret
= readahead_tree_block(root
, bytenr
, blocksize
,
5864 wc
->reada_slot
= slot
;
5868 * hepler to process tree block while walking down the tree.
5870 * when wc->stage == UPDATE_BACKREF, this function updates
5871 * back refs for pointers in the block.
5873 * NOTE: return value 1 means we should stop walking down.
5875 static noinline
int walk_down_proc(struct btrfs_trans_handle
*trans
,
5876 struct btrfs_root
*root
,
5877 struct btrfs_path
*path
,
5878 struct walk_control
*wc
, int lookup_info
)
5880 int level
= wc
->level
;
5881 struct extent_buffer
*eb
= path
->nodes
[level
];
5882 u64 flag
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
5885 if (wc
->stage
== UPDATE_BACKREF
&&
5886 btrfs_header_owner(eb
) != root
->root_key
.objectid
)
5890 * when reference count of tree block is 1, it won't increase
5891 * again. once full backref flag is set, we never clear it.
5894 ((wc
->stage
== DROP_REFERENCE
&& wc
->refs
[level
] != 1) ||
5895 (wc
->stage
== UPDATE_BACKREF
&& !(wc
->flags
[level
] & flag
)))) {
5896 BUG_ON(!path
->locks
[level
]);
5897 ret
= btrfs_lookup_extent_info(trans
, root
,
5902 BUG_ON(wc
->refs
[level
] == 0);
5905 if (wc
->stage
== DROP_REFERENCE
) {
5906 if (wc
->refs
[level
] > 1)
5909 if (path
->locks
[level
] && !wc
->keep_locks
) {
5910 btrfs_tree_unlock(eb
);
5911 path
->locks
[level
] = 0;
5916 /* wc->stage == UPDATE_BACKREF */
5917 if (!(wc
->flags
[level
] & flag
)) {
5918 BUG_ON(!path
->locks
[level
]);
5919 ret
= btrfs_inc_ref(trans
, root
, eb
, 1);
5921 ret
= btrfs_dec_ref(trans
, root
, eb
, 0);
5923 ret
= btrfs_set_disk_extent_flags(trans
, root
, eb
->start
,
5926 wc
->flags
[level
] |= flag
;
5930 * the block is shared by multiple trees, so it's not good to
5931 * keep the tree lock
5933 if (path
->locks
[level
] && level
> 0) {
5934 btrfs_tree_unlock(eb
);
5935 path
->locks
[level
] = 0;
5941 * hepler to process tree block pointer.
5943 * when wc->stage == DROP_REFERENCE, this function checks
5944 * reference count of the block pointed to. if the block
5945 * is shared and we need update back refs for the subtree
5946 * rooted at the block, this function changes wc->stage to
5947 * UPDATE_BACKREF. if the block is shared and there is no
5948 * need to update back, this function drops the reference
5951 * NOTE: return value 1 means we should stop walking down.
5953 static noinline
int do_walk_down(struct btrfs_trans_handle
*trans
,
5954 struct btrfs_root
*root
,
5955 struct btrfs_path
*path
,
5956 struct walk_control
*wc
, int *lookup_info
)
5962 struct btrfs_key key
;
5963 struct extent_buffer
*next
;
5964 int level
= wc
->level
;
5968 generation
= btrfs_node_ptr_generation(path
->nodes
[level
],
5969 path
->slots
[level
]);
5971 * if the lower level block was created before the snapshot
5972 * was created, we know there is no need to update back refs
5975 if (wc
->stage
== UPDATE_BACKREF
&&
5976 generation
<= root
->root_key
.offset
) {
5981 bytenr
= btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]);
5982 blocksize
= btrfs_level_size(root
, level
- 1);
5984 next
= btrfs_find_tree_block(root
, bytenr
, blocksize
);
5986 next
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
5991 btrfs_tree_lock(next
);
5992 btrfs_set_lock_blocking(next
);
5994 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
, blocksize
,
5995 &wc
->refs
[level
- 1],
5996 &wc
->flags
[level
- 1]);
5998 BUG_ON(wc
->refs
[level
- 1] == 0);
6001 if (wc
->stage
== DROP_REFERENCE
) {
6002 if (wc
->refs
[level
- 1] > 1) {
6004 (wc
->flags
[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF
))
6007 if (!wc
->update_ref
||
6008 generation
<= root
->root_key
.offset
)
6011 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
6012 path
->slots
[level
]);
6013 ret
= btrfs_comp_cpu_keys(&key
, &wc
->update_progress
);
6017 wc
->stage
= UPDATE_BACKREF
;
6018 wc
->shared_level
= level
- 1;
6022 (wc
->flags
[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF
))
6026 if (!btrfs_buffer_uptodate(next
, generation
)) {
6027 btrfs_tree_unlock(next
);
6028 free_extent_buffer(next
);
6034 if (reada
&& level
== 1)
6035 reada_walk_down(trans
, root
, wc
, path
);
6036 next
= read_tree_block(root
, bytenr
, blocksize
, generation
);
6039 btrfs_tree_lock(next
);
6040 btrfs_set_lock_blocking(next
);
6044 BUG_ON(level
!= btrfs_header_level(next
));
6045 path
->nodes
[level
] = next
;
6046 path
->slots
[level
] = 0;
6047 path
->locks
[level
] = 1;
6053 wc
->refs
[level
- 1] = 0;
6054 wc
->flags
[level
- 1] = 0;
6055 if (wc
->stage
== DROP_REFERENCE
) {
6056 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
) {
6057 parent
= path
->nodes
[level
]->start
;
6059 BUG_ON(root
->root_key
.objectid
!=
6060 btrfs_header_owner(path
->nodes
[level
]));
6064 ret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
, parent
,
6065 root
->root_key
.objectid
, level
- 1, 0);
6068 btrfs_tree_unlock(next
);
6069 free_extent_buffer(next
);
6075 * hepler to process tree block while walking up the tree.
6077 * when wc->stage == DROP_REFERENCE, this function drops
6078 * reference count on the block.
6080 * when wc->stage == UPDATE_BACKREF, this function changes
6081 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6082 * to UPDATE_BACKREF previously while processing the block.
6084 * NOTE: return value 1 means we should stop walking up.
6086 static noinline
int walk_up_proc(struct btrfs_trans_handle
*trans
,
6087 struct btrfs_root
*root
,
6088 struct btrfs_path
*path
,
6089 struct walk_control
*wc
)
6092 int level
= wc
->level
;
6093 struct extent_buffer
*eb
= path
->nodes
[level
];
6096 if (wc
->stage
== UPDATE_BACKREF
) {
6097 BUG_ON(wc
->shared_level
< level
);
6098 if (level
< wc
->shared_level
)
6101 ret
= find_next_key(path
, level
+ 1, &wc
->update_progress
);
6105 wc
->stage
= DROP_REFERENCE
;
6106 wc
->shared_level
= -1;
6107 path
->slots
[level
] = 0;
6110 * check reference count again if the block isn't locked.
6111 * we should start walking down the tree again if reference
6114 if (!path
->locks
[level
]) {
6116 btrfs_tree_lock(eb
);
6117 btrfs_set_lock_blocking(eb
);
6118 path
->locks
[level
] = 1;
6120 ret
= btrfs_lookup_extent_info(trans
, root
,
6125 BUG_ON(wc
->refs
[level
] == 0);
6126 if (wc
->refs
[level
] == 1) {
6127 btrfs_tree_unlock(eb
);
6128 path
->locks
[level
] = 0;
6134 /* wc->stage == DROP_REFERENCE */
6135 BUG_ON(wc
->refs
[level
] > 1 && !path
->locks
[level
]);
6137 if (wc
->refs
[level
] == 1) {
6139 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
6140 ret
= btrfs_dec_ref(trans
, root
, eb
, 1);
6142 ret
= btrfs_dec_ref(trans
, root
, eb
, 0);
6145 /* make block locked assertion in clean_tree_block happy */
6146 if (!path
->locks
[level
] &&
6147 btrfs_header_generation(eb
) == trans
->transid
) {
6148 btrfs_tree_lock(eb
);
6149 btrfs_set_lock_blocking(eb
);
6150 path
->locks
[level
] = 1;
6152 clean_tree_block(trans
, root
, eb
);
6155 if (eb
== root
->node
) {
6156 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
6159 BUG_ON(root
->root_key
.objectid
!=
6160 btrfs_header_owner(eb
));
6162 if (wc
->flags
[level
+ 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
6163 parent
= path
->nodes
[level
+ 1]->start
;
6165 BUG_ON(root
->root_key
.objectid
!=
6166 btrfs_header_owner(path
->nodes
[level
+ 1]));
6169 btrfs_free_tree_block(trans
, root
, eb
, parent
, wc
->refs
[level
] == 1);
6171 wc
->refs
[level
] = 0;
6172 wc
->flags
[level
] = 0;
6176 static noinline
int walk_down_tree(struct btrfs_trans_handle
*trans
,
6177 struct btrfs_root
*root
,
6178 struct btrfs_path
*path
,
6179 struct walk_control
*wc
)
6181 int level
= wc
->level
;
6182 int lookup_info
= 1;
6185 while (level
>= 0) {
6186 ret
= walk_down_proc(trans
, root
, path
, wc
, lookup_info
);
6193 if (path
->slots
[level
] >=
6194 btrfs_header_nritems(path
->nodes
[level
]))
6197 ret
= do_walk_down(trans
, root
, path
, wc
, &lookup_info
);
6199 path
->slots
[level
]++;
6208 static noinline
int walk_up_tree(struct btrfs_trans_handle
*trans
,
6209 struct btrfs_root
*root
,
6210 struct btrfs_path
*path
,
6211 struct walk_control
*wc
, int max_level
)
6213 int level
= wc
->level
;
6216 path
->slots
[level
] = btrfs_header_nritems(path
->nodes
[level
]);
6217 while (level
< max_level
&& path
->nodes
[level
]) {
6219 if (path
->slots
[level
] + 1 <
6220 btrfs_header_nritems(path
->nodes
[level
])) {
6221 path
->slots
[level
]++;
6224 ret
= walk_up_proc(trans
, root
, path
, wc
);
6228 if (path
->locks
[level
]) {
6229 btrfs_tree_unlock(path
->nodes
[level
]);
6230 path
->locks
[level
] = 0;
6232 free_extent_buffer(path
->nodes
[level
]);
6233 path
->nodes
[level
] = NULL
;
6241 * drop a subvolume tree.
6243 * this function traverses the tree freeing any blocks that only
6244 * referenced by the tree.
6246 * when a shared tree block is found. this function decreases its
6247 * reference count by one. if update_ref is true, this function
6248 * also make sure backrefs for the shared block and all lower level
6249 * blocks are properly updated.
6251 int btrfs_drop_snapshot(struct btrfs_root
*root
,
6252 struct btrfs_block_rsv
*block_rsv
, int update_ref
)
6254 struct btrfs_path
*path
;
6255 struct btrfs_trans_handle
*trans
;
6256 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
6257 struct btrfs_root_item
*root_item
= &root
->root_item
;
6258 struct walk_control
*wc
;
6259 struct btrfs_key key
;
6264 path
= btrfs_alloc_path();
6267 wc
= kzalloc(sizeof(*wc
), GFP_NOFS
);
6270 trans
= btrfs_start_transaction(tree_root
, 0);
6271 BUG_ON(IS_ERR(trans
));
6274 trans
->block_rsv
= block_rsv
;
6276 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
6277 level
= btrfs_header_level(root
->node
);
6278 path
->nodes
[level
] = btrfs_lock_root_node(root
);
6279 btrfs_set_lock_blocking(path
->nodes
[level
]);
6280 path
->slots
[level
] = 0;
6281 path
->locks
[level
] = 1;
6282 memset(&wc
->update_progress
, 0,
6283 sizeof(wc
->update_progress
));
6285 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
6286 memcpy(&wc
->update_progress
, &key
,
6287 sizeof(wc
->update_progress
));
6289 level
= root_item
->drop_level
;
6291 path
->lowest_level
= level
;
6292 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
6293 path
->lowest_level
= 0;
6301 * unlock our path, this is safe because only this
6302 * function is allowed to delete this snapshot
6304 btrfs_unlock_up_safe(path
, 0);
6306 level
= btrfs_header_level(root
->node
);
6308 btrfs_tree_lock(path
->nodes
[level
]);
6309 btrfs_set_lock_blocking(path
->nodes
[level
]);
6311 ret
= btrfs_lookup_extent_info(trans
, root
,
6312 path
->nodes
[level
]->start
,
6313 path
->nodes
[level
]->len
,
6317 BUG_ON(wc
->refs
[level
] == 0);
6319 if (level
== root_item
->drop_level
)
6322 btrfs_tree_unlock(path
->nodes
[level
]);
6323 WARN_ON(wc
->refs
[level
] != 1);
6329 wc
->shared_level
= -1;
6330 wc
->stage
= DROP_REFERENCE
;
6331 wc
->update_ref
= update_ref
;
6333 wc
->reada_count
= BTRFS_NODEPTRS_PER_BLOCK(root
);
6336 ret
= walk_down_tree(trans
, root
, path
, wc
);
6342 ret
= walk_up_tree(trans
, root
, path
, wc
, BTRFS_MAX_LEVEL
);
6349 BUG_ON(wc
->stage
!= DROP_REFERENCE
);
6353 if (wc
->stage
== DROP_REFERENCE
) {
6355 btrfs_node_key(path
->nodes
[level
],
6356 &root_item
->drop_progress
,
6357 path
->slots
[level
]);
6358 root_item
->drop_level
= level
;
6361 BUG_ON(wc
->level
== 0);
6362 if (btrfs_should_end_transaction(trans
, tree_root
)) {
6363 ret
= btrfs_update_root(trans
, tree_root
,
6368 btrfs_end_transaction_throttle(trans
, tree_root
);
6369 trans
= btrfs_start_transaction(tree_root
, 0);
6370 BUG_ON(IS_ERR(trans
));
6372 trans
->block_rsv
= block_rsv
;
6375 btrfs_release_path(path
);
6378 ret
= btrfs_del_root(trans
, tree_root
, &root
->root_key
);
6381 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
6382 ret
= btrfs_find_last_root(tree_root
, root
->root_key
.objectid
,
6386 /* if we fail to delete the orphan item this time
6387 * around, it'll get picked up the next time.
6389 * The most common failure here is just -ENOENT.
6391 btrfs_del_orphan_item(trans
, tree_root
,
6392 root
->root_key
.objectid
);
6396 if (root
->in_radix
) {
6397 btrfs_free_fs_root(tree_root
->fs_info
, root
);
6399 free_extent_buffer(root
->node
);
6400 free_extent_buffer(root
->commit_root
);
6404 btrfs_end_transaction_throttle(trans
, tree_root
);
6406 btrfs_free_path(path
);
6411 * drop subtree rooted at tree block 'node'.
6413 * NOTE: this function will unlock and release tree block 'node'
6415 int btrfs_drop_subtree(struct btrfs_trans_handle
*trans
,
6416 struct btrfs_root
*root
,
6417 struct extent_buffer
*node
,
6418 struct extent_buffer
*parent
)
6420 struct btrfs_path
*path
;
6421 struct walk_control
*wc
;
6427 BUG_ON(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
6429 path
= btrfs_alloc_path();
6433 wc
= kzalloc(sizeof(*wc
), GFP_NOFS
);
6435 btrfs_free_path(path
);
6439 btrfs_assert_tree_locked(parent
);
6440 parent_level
= btrfs_header_level(parent
);
6441 extent_buffer_get(parent
);
6442 path
->nodes
[parent_level
] = parent
;
6443 path
->slots
[parent_level
] = btrfs_header_nritems(parent
);
6445 btrfs_assert_tree_locked(node
);
6446 level
= btrfs_header_level(node
);
6447 path
->nodes
[level
] = node
;
6448 path
->slots
[level
] = 0;
6449 path
->locks
[level
] = 1;
6451 wc
->refs
[parent_level
] = 1;
6452 wc
->flags
[parent_level
] = BTRFS_BLOCK_FLAG_FULL_BACKREF
;
6454 wc
->shared_level
= -1;
6455 wc
->stage
= DROP_REFERENCE
;
6458 wc
->reada_count
= BTRFS_NODEPTRS_PER_BLOCK(root
);
6461 wret
= walk_down_tree(trans
, root
, path
, wc
);
6467 wret
= walk_up_tree(trans
, root
, path
, wc
, parent_level
);
6475 btrfs_free_path(path
);
6479 static u64
update_block_group_flags(struct btrfs_root
*root
, u64 flags
)
6482 u64 stripped
= BTRFS_BLOCK_GROUP_RAID0
|
6483 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
;
6486 * we add in the count of missing devices because we want
6487 * to make sure that any RAID levels on a degraded FS
6488 * continue to be honored.
6490 num_devices
= root
->fs_info
->fs_devices
->rw_devices
+
6491 root
->fs_info
->fs_devices
->missing_devices
;
6493 if (num_devices
== 1) {
6494 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
6495 stripped
= flags
& ~stripped
;
6497 /* turn raid0 into single device chunks */
6498 if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
6501 /* turn mirroring into duplication */
6502 if (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
6503 BTRFS_BLOCK_GROUP_RAID10
))
6504 return stripped
| BTRFS_BLOCK_GROUP_DUP
;
6507 /* they already had raid on here, just return */
6508 if (flags
& stripped
)
6511 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
6512 stripped
= flags
& ~stripped
;
6514 /* switch duplicated blocks with raid1 */
6515 if (flags
& BTRFS_BLOCK_GROUP_DUP
)
6516 return stripped
| BTRFS_BLOCK_GROUP_RAID1
;
6518 /* turn single device chunks into raid0 */
6519 return stripped
| BTRFS_BLOCK_GROUP_RAID0
;
6524 static int set_block_group_ro(struct btrfs_block_group_cache
*cache
)
6526 struct btrfs_space_info
*sinfo
= cache
->space_info
;
6533 spin_lock(&sinfo
->lock
);
6534 spin_lock(&cache
->lock
);
6535 num_bytes
= cache
->key
.offset
- cache
->reserved
- cache
->pinned
-
6536 cache
->bytes_super
- btrfs_block_group_used(&cache
->item
);
6538 if (sinfo
->bytes_used
+ sinfo
->bytes_reserved
+ sinfo
->bytes_pinned
+
6539 sinfo
->bytes_may_use
+ sinfo
->bytes_readonly
+
6540 cache
->reserved_pinned
+ num_bytes
<= sinfo
->total_bytes
) {
6541 sinfo
->bytes_readonly
+= num_bytes
;
6542 sinfo
->bytes_reserved
+= cache
->reserved_pinned
;
6543 cache
->reserved_pinned
= 0;
6548 spin_unlock(&cache
->lock
);
6549 spin_unlock(&sinfo
->lock
);
6553 int btrfs_set_block_group_ro(struct btrfs_root
*root
,
6554 struct btrfs_block_group_cache
*cache
)
6557 struct btrfs_trans_handle
*trans
;
6563 trans
= btrfs_join_transaction(root
);
6564 BUG_ON(IS_ERR(trans
));
6566 alloc_flags
= update_block_group_flags(root
, cache
->flags
);
6567 if (alloc_flags
!= cache
->flags
)
6568 do_chunk_alloc(trans
, root
, 2 * 1024 * 1024, alloc_flags
,
6571 ret
= set_block_group_ro(cache
);
6574 alloc_flags
= get_alloc_profile(root
, cache
->space_info
->flags
);
6575 ret
= do_chunk_alloc(trans
, root
, 2 * 1024 * 1024, alloc_flags
,
6579 ret
= set_block_group_ro(cache
);
6581 btrfs_end_transaction(trans
, root
);
6585 int btrfs_force_chunk_alloc(struct btrfs_trans_handle
*trans
,
6586 struct btrfs_root
*root
, u64 type
)
6588 u64 alloc_flags
= get_alloc_profile(root
, type
);
6589 return do_chunk_alloc(trans
, root
, 2 * 1024 * 1024, alloc_flags
,
6594 * helper to account the unused space of all the readonly block group in the
6595 * list. takes mirrors into account.
6597 static u64
__btrfs_get_ro_block_group_free_space(struct list_head
*groups_list
)
6599 struct btrfs_block_group_cache
*block_group
;
6603 list_for_each_entry(block_group
, groups_list
, list
) {
6604 spin_lock(&block_group
->lock
);
6606 if (!block_group
->ro
) {
6607 spin_unlock(&block_group
->lock
);
6611 if (block_group
->flags
& (BTRFS_BLOCK_GROUP_RAID1
|
6612 BTRFS_BLOCK_GROUP_RAID10
|
6613 BTRFS_BLOCK_GROUP_DUP
))
6618 free_bytes
+= (block_group
->key
.offset
-
6619 btrfs_block_group_used(&block_group
->item
)) *
6622 spin_unlock(&block_group
->lock
);
6629 * helper to account the unused space of all the readonly block group in the
6630 * space_info. takes mirrors into account.
6632 u64
btrfs_account_ro_block_groups_free_space(struct btrfs_space_info
*sinfo
)
6637 spin_lock(&sinfo
->lock
);
6639 for(i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++)
6640 if (!list_empty(&sinfo
->block_groups
[i
]))
6641 free_bytes
+= __btrfs_get_ro_block_group_free_space(
6642 &sinfo
->block_groups
[i
]);
6644 spin_unlock(&sinfo
->lock
);
6649 int btrfs_set_block_group_rw(struct btrfs_root
*root
,
6650 struct btrfs_block_group_cache
*cache
)
6652 struct btrfs_space_info
*sinfo
= cache
->space_info
;
6657 spin_lock(&sinfo
->lock
);
6658 spin_lock(&cache
->lock
);
6659 num_bytes
= cache
->key
.offset
- cache
->reserved
- cache
->pinned
-
6660 cache
->bytes_super
- btrfs_block_group_used(&cache
->item
);
6661 sinfo
->bytes_readonly
-= num_bytes
;
6663 spin_unlock(&cache
->lock
);
6664 spin_unlock(&sinfo
->lock
);
6669 * checks to see if its even possible to relocate this block group.
6671 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
6672 * ok to go ahead and try.
6674 int btrfs_can_relocate(struct btrfs_root
*root
, u64 bytenr
)
6676 struct btrfs_block_group_cache
*block_group
;
6677 struct btrfs_space_info
*space_info
;
6678 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
6679 struct btrfs_device
*device
;
6683 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
6685 /* odd, couldn't find the block group, leave it alone */
6689 /* no bytes used, we're good */
6690 if (!btrfs_block_group_used(&block_group
->item
))
6693 space_info
= block_group
->space_info
;
6694 spin_lock(&space_info
->lock
);
6696 full
= space_info
->full
;
6699 * if this is the last block group we have in this space, we can't
6700 * relocate it unless we're able to allocate a new chunk below.
6702 * Otherwise, we need to make sure we have room in the space to handle
6703 * all of the extents from this block group. If we can, we're good
6705 if ((space_info
->total_bytes
!= block_group
->key
.offset
) &&
6706 (space_info
->bytes_used
+ space_info
->bytes_reserved
+
6707 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
6708 btrfs_block_group_used(&block_group
->item
) <
6709 space_info
->total_bytes
)) {
6710 spin_unlock(&space_info
->lock
);
6713 spin_unlock(&space_info
->lock
);
6716 * ok we don't have enough space, but maybe we have free space on our
6717 * devices to allocate new chunks for relocation, so loop through our
6718 * alloc devices and guess if we have enough space. However, if we
6719 * were marked as full, then we know there aren't enough chunks, and we
6726 mutex_lock(&root
->fs_info
->chunk_mutex
);
6727 list_for_each_entry(device
, &fs_devices
->alloc_list
, dev_alloc_list
) {
6728 u64 min_free
= btrfs_block_group_used(&block_group
->item
);
6732 * check to make sure we can actually find a chunk with enough
6733 * space to fit our block group in.
6735 if (device
->total_bytes
> device
->bytes_used
+ min_free
) {
6736 ret
= find_free_dev_extent(NULL
, device
, min_free
,
6743 mutex_unlock(&root
->fs_info
->chunk_mutex
);
6745 btrfs_put_block_group(block_group
);
6749 static int find_first_block_group(struct btrfs_root
*root
,
6750 struct btrfs_path
*path
, struct btrfs_key
*key
)
6753 struct btrfs_key found_key
;
6754 struct extent_buffer
*leaf
;
6757 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
6762 slot
= path
->slots
[0];
6763 leaf
= path
->nodes
[0];
6764 if (slot
>= btrfs_header_nritems(leaf
)) {
6765 ret
= btrfs_next_leaf(root
, path
);
6772 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
6774 if (found_key
.objectid
>= key
->objectid
&&
6775 found_key
.type
== BTRFS_BLOCK_GROUP_ITEM_KEY
) {
6785 void btrfs_put_block_group_cache(struct btrfs_fs_info
*info
)
6787 struct btrfs_block_group_cache
*block_group
;
6791 struct inode
*inode
;
6793 block_group
= btrfs_lookup_first_block_group(info
, last
);
6794 while (block_group
) {
6795 spin_lock(&block_group
->lock
);
6796 if (block_group
->iref
)
6798 spin_unlock(&block_group
->lock
);
6799 block_group
= next_block_group(info
->tree_root
,
6809 inode
= block_group
->inode
;
6810 block_group
->iref
= 0;
6811 block_group
->inode
= NULL
;
6812 spin_unlock(&block_group
->lock
);
6814 last
= block_group
->key
.objectid
+ block_group
->key
.offset
;
6815 btrfs_put_block_group(block_group
);
6819 int btrfs_free_block_groups(struct btrfs_fs_info
*info
)
6821 struct btrfs_block_group_cache
*block_group
;
6822 struct btrfs_space_info
*space_info
;
6823 struct btrfs_caching_control
*caching_ctl
;
6826 down_write(&info
->extent_commit_sem
);
6827 while (!list_empty(&info
->caching_block_groups
)) {
6828 caching_ctl
= list_entry(info
->caching_block_groups
.next
,
6829 struct btrfs_caching_control
, list
);
6830 list_del(&caching_ctl
->list
);
6831 put_caching_control(caching_ctl
);
6833 up_write(&info
->extent_commit_sem
);
6835 spin_lock(&info
->block_group_cache_lock
);
6836 while ((n
= rb_last(&info
->block_group_cache_tree
)) != NULL
) {
6837 block_group
= rb_entry(n
, struct btrfs_block_group_cache
,
6839 rb_erase(&block_group
->cache_node
,
6840 &info
->block_group_cache_tree
);
6841 spin_unlock(&info
->block_group_cache_lock
);
6843 down_write(&block_group
->space_info
->groups_sem
);
6844 list_del(&block_group
->list
);
6845 up_write(&block_group
->space_info
->groups_sem
);
6847 if (block_group
->cached
== BTRFS_CACHE_STARTED
)
6848 wait_block_group_cache_done(block_group
);
6851 * We haven't cached this block group, which means we could
6852 * possibly have excluded extents on this block group.
6854 if (block_group
->cached
== BTRFS_CACHE_NO
)
6855 free_excluded_extents(info
->extent_root
, block_group
);
6857 btrfs_remove_free_space_cache(block_group
);
6858 btrfs_put_block_group(block_group
);
6860 spin_lock(&info
->block_group_cache_lock
);
6862 spin_unlock(&info
->block_group_cache_lock
);
6864 /* now that all the block groups are freed, go through and
6865 * free all the space_info structs. This is only called during
6866 * the final stages of unmount, and so we know nobody is
6867 * using them. We call synchronize_rcu() once before we start,
6868 * just to be on the safe side.
6872 release_global_block_rsv(info
);
6874 while(!list_empty(&info
->space_info
)) {
6875 space_info
= list_entry(info
->space_info
.next
,
6876 struct btrfs_space_info
,
6878 if (space_info
->bytes_pinned
> 0 ||
6879 space_info
->bytes_reserved
> 0) {
6881 dump_space_info(space_info
, 0, 0);
6883 list_del(&space_info
->list
);
6889 static void __link_block_group(struct btrfs_space_info
*space_info
,
6890 struct btrfs_block_group_cache
*cache
)
6892 int index
= get_block_group_index(cache
);
6894 down_write(&space_info
->groups_sem
);
6895 list_add_tail(&cache
->list
, &space_info
->block_groups
[index
]);
6896 up_write(&space_info
->groups_sem
);
6899 int btrfs_read_block_groups(struct btrfs_root
*root
)
6901 struct btrfs_path
*path
;
6903 struct btrfs_block_group_cache
*cache
;
6904 struct btrfs_fs_info
*info
= root
->fs_info
;
6905 struct btrfs_space_info
*space_info
;
6906 struct btrfs_key key
;
6907 struct btrfs_key found_key
;
6908 struct extent_buffer
*leaf
;
6912 root
= info
->extent_root
;
6915 btrfs_set_key_type(&key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
6916 path
= btrfs_alloc_path();
6921 cache_gen
= btrfs_super_cache_generation(&root
->fs_info
->super_copy
);
6922 if (cache_gen
!= 0 &&
6923 btrfs_super_generation(&root
->fs_info
->super_copy
) != cache_gen
)
6925 if (btrfs_test_opt(root
, CLEAR_CACHE
))
6927 if (!btrfs_test_opt(root
, SPACE_CACHE
) && cache_gen
)
6928 printk(KERN_INFO
"btrfs: disk space caching is enabled\n");
6931 ret
= find_first_block_group(root
, path
, &key
);
6936 leaf
= path
->nodes
[0];
6937 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6938 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
6943 cache
->free_space_ctl
= kzalloc(sizeof(*cache
->free_space_ctl
),
6945 if (!cache
->free_space_ctl
) {
6951 atomic_set(&cache
->count
, 1);
6952 spin_lock_init(&cache
->lock
);
6953 cache
->fs_info
= info
;
6954 INIT_LIST_HEAD(&cache
->list
);
6955 INIT_LIST_HEAD(&cache
->cluster_list
);
6958 cache
->disk_cache_state
= BTRFS_DC_CLEAR
;
6960 read_extent_buffer(leaf
, &cache
->item
,
6961 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
6962 sizeof(cache
->item
));
6963 memcpy(&cache
->key
, &found_key
, sizeof(found_key
));
6965 key
.objectid
= found_key
.objectid
+ found_key
.offset
;
6966 btrfs_release_path(path
);
6967 cache
->flags
= btrfs_block_group_flags(&cache
->item
);
6968 cache
->sectorsize
= root
->sectorsize
;
6970 btrfs_init_free_space_ctl(cache
);
6973 * We need to exclude the super stripes now so that the space
6974 * info has super bytes accounted for, otherwise we'll think
6975 * we have more space than we actually do.
6977 exclude_super_stripes(root
, cache
);
6980 * check for two cases, either we are full, and therefore
6981 * don't need to bother with the caching work since we won't
6982 * find any space, or we are empty, and we can just add all
6983 * the space in and be done with it. This saves us _alot_ of
6984 * time, particularly in the full case.
6986 if (found_key
.offset
== btrfs_block_group_used(&cache
->item
)) {
6987 cache
->last_byte_to_unpin
= (u64
)-1;
6988 cache
->cached
= BTRFS_CACHE_FINISHED
;
6989 free_excluded_extents(root
, cache
);
6990 } else if (btrfs_block_group_used(&cache
->item
) == 0) {
6991 cache
->last_byte_to_unpin
= (u64
)-1;
6992 cache
->cached
= BTRFS_CACHE_FINISHED
;
6993 add_new_free_space(cache
, root
->fs_info
,
6995 found_key
.objectid
+
6997 free_excluded_extents(root
, cache
);
7000 ret
= update_space_info(info
, cache
->flags
, found_key
.offset
,
7001 btrfs_block_group_used(&cache
->item
),
7004 cache
->space_info
= space_info
;
7005 spin_lock(&cache
->space_info
->lock
);
7006 cache
->space_info
->bytes_readonly
+= cache
->bytes_super
;
7007 spin_unlock(&cache
->space_info
->lock
);
7009 __link_block_group(space_info
, cache
);
7011 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
7014 set_avail_alloc_bits(root
->fs_info
, cache
->flags
);
7015 if (btrfs_chunk_readonly(root
, cache
->key
.objectid
))
7016 set_block_group_ro(cache
);
7019 list_for_each_entry_rcu(space_info
, &root
->fs_info
->space_info
, list
) {
7020 if (!(get_alloc_profile(root
, space_info
->flags
) &
7021 (BTRFS_BLOCK_GROUP_RAID10
|
7022 BTRFS_BLOCK_GROUP_RAID1
|
7023 BTRFS_BLOCK_GROUP_DUP
)))
7026 * avoid allocating from un-mirrored block group if there are
7027 * mirrored block groups.
7029 list_for_each_entry(cache
, &space_info
->block_groups
[3], list
)
7030 set_block_group_ro(cache
);
7031 list_for_each_entry(cache
, &space_info
->block_groups
[4], list
)
7032 set_block_group_ro(cache
);
7035 init_global_block_rsv(info
);
7038 btrfs_free_path(path
);
7042 int btrfs_make_block_group(struct btrfs_trans_handle
*trans
,
7043 struct btrfs_root
*root
, u64 bytes_used
,
7044 u64 type
, u64 chunk_objectid
, u64 chunk_offset
,
7048 struct btrfs_root
*extent_root
;
7049 struct btrfs_block_group_cache
*cache
;
7051 extent_root
= root
->fs_info
->extent_root
;
7053 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
7055 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
7058 cache
->free_space_ctl
= kzalloc(sizeof(*cache
->free_space_ctl
),
7060 if (!cache
->free_space_ctl
) {
7065 cache
->key
.objectid
= chunk_offset
;
7066 cache
->key
.offset
= size
;
7067 cache
->key
.type
= BTRFS_BLOCK_GROUP_ITEM_KEY
;
7068 cache
->sectorsize
= root
->sectorsize
;
7069 cache
->fs_info
= root
->fs_info
;
7071 atomic_set(&cache
->count
, 1);
7072 spin_lock_init(&cache
->lock
);
7073 INIT_LIST_HEAD(&cache
->list
);
7074 INIT_LIST_HEAD(&cache
->cluster_list
);
7076 btrfs_init_free_space_ctl(cache
);
7078 btrfs_set_block_group_used(&cache
->item
, bytes_used
);
7079 btrfs_set_block_group_chunk_objectid(&cache
->item
, chunk_objectid
);
7080 cache
->flags
= type
;
7081 btrfs_set_block_group_flags(&cache
->item
, type
);
7083 cache
->last_byte_to_unpin
= (u64
)-1;
7084 cache
->cached
= BTRFS_CACHE_FINISHED
;
7085 exclude_super_stripes(root
, cache
);
7087 add_new_free_space(cache
, root
->fs_info
, chunk_offset
,
7088 chunk_offset
+ size
);
7090 free_excluded_extents(root
, cache
);
7092 ret
= update_space_info(root
->fs_info
, cache
->flags
, size
, bytes_used
,
7093 &cache
->space_info
);
7096 spin_lock(&cache
->space_info
->lock
);
7097 cache
->space_info
->bytes_readonly
+= cache
->bytes_super
;
7098 spin_unlock(&cache
->space_info
->lock
);
7100 __link_block_group(cache
->space_info
, cache
);
7102 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
7105 ret
= btrfs_insert_item(trans
, extent_root
, &cache
->key
, &cache
->item
,
7106 sizeof(cache
->item
));
7109 set_avail_alloc_bits(extent_root
->fs_info
, type
);
7114 int btrfs_remove_block_group(struct btrfs_trans_handle
*trans
,
7115 struct btrfs_root
*root
, u64 group_start
)
7117 struct btrfs_path
*path
;
7118 struct btrfs_block_group_cache
*block_group
;
7119 struct btrfs_free_cluster
*cluster
;
7120 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
7121 struct btrfs_key key
;
7122 struct inode
*inode
;
7126 root
= root
->fs_info
->extent_root
;
7128 block_group
= btrfs_lookup_block_group(root
->fs_info
, group_start
);
7129 BUG_ON(!block_group
);
7130 BUG_ON(!block_group
->ro
);
7133 * Free the reserved super bytes from this block group before
7136 free_excluded_extents(root
, block_group
);
7138 memcpy(&key
, &block_group
->key
, sizeof(key
));
7139 if (block_group
->flags
& (BTRFS_BLOCK_GROUP_DUP
|
7140 BTRFS_BLOCK_GROUP_RAID1
|
7141 BTRFS_BLOCK_GROUP_RAID10
))
7146 /* make sure this block group isn't part of an allocation cluster */
7147 cluster
= &root
->fs_info
->data_alloc_cluster
;
7148 spin_lock(&cluster
->refill_lock
);
7149 btrfs_return_cluster_to_free_space(block_group
, cluster
);
7150 spin_unlock(&cluster
->refill_lock
);
7153 * make sure this block group isn't part of a metadata
7154 * allocation cluster
7156 cluster
= &root
->fs_info
->meta_alloc_cluster
;
7157 spin_lock(&cluster
->refill_lock
);
7158 btrfs_return_cluster_to_free_space(block_group
, cluster
);
7159 spin_unlock(&cluster
->refill_lock
);
7161 path
= btrfs_alloc_path();
7164 inode
= lookup_free_space_inode(root
, block_group
, path
);
7165 if (!IS_ERR(inode
)) {
7166 btrfs_orphan_add(trans
, inode
);
7168 /* One for the block groups ref */
7169 spin_lock(&block_group
->lock
);
7170 if (block_group
->iref
) {
7171 block_group
->iref
= 0;
7172 block_group
->inode
= NULL
;
7173 spin_unlock(&block_group
->lock
);
7176 spin_unlock(&block_group
->lock
);
7178 /* One for our lookup ref */
7182 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
7183 key
.offset
= block_group
->key
.objectid
;
7186 ret
= btrfs_search_slot(trans
, tree_root
, &key
, path
, -1, 1);
7190 btrfs_release_path(path
);
7192 ret
= btrfs_del_item(trans
, tree_root
, path
);
7195 btrfs_release_path(path
);
7198 spin_lock(&root
->fs_info
->block_group_cache_lock
);
7199 rb_erase(&block_group
->cache_node
,
7200 &root
->fs_info
->block_group_cache_tree
);
7201 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
7203 down_write(&block_group
->space_info
->groups_sem
);
7205 * we must use list_del_init so people can check to see if they
7206 * are still on the list after taking the semaphore
7208 list_del_init(&block_group
->list
);
7209 up_write(&block_group
->space_info
->groups_sem
);
7211 if (block_group
->cached
== BTRFS_CACHE_STARTED
)
7212 wait_block_group_cache_done(block_group
);
7214 btrfs_remove_free_space_cache(block_group
);
7216 spin_lock(&block_group
->space_info
->lock
);
7217 block_group
->space_info
->total_bytes
-= block_group
->key
.offset
;
7218 block_group
->space_info
->bytes_readonly
-= block_group
->key
.offset
;
7219 block_group
->space_info
->disk_total
-= block_group
->key
.offset
* factor
;
7220 spin_unlock(&block_group
->space_info
->lock
);
7222 memcpy(&key
, &block_group
->key
, sizeof(key
));
7224 btrfs_clear_space_info_full(root
->fs_info
);
7226 btrfs_put_block_group(block_group
);
7227 btrfs_put_block_group(block_group
);
7229 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
7235 ret
= btrfs_del_item(trans
, root
, path
);
7237 btrfs_free_path(path
);
7241 int btrfs_init_space_info(struct btrfs_fs_info
*fs_info
)
7243 struct btrfs_space_info
*space_info
;
7244 struct btrfs_super_block
*disk_super
;
7250 disk_super
= &fs_info
->super_copy
;
7251 if (!btrfs_super_root(disk_super
))
7254 features
= btrfs_super_incompat_flags(disk_super
);
7255 if (features
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
7258 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
7259 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
7264 flags
= BTRFS_BLOCK_GROUP_METADATA
| BTRFS_BLOCK_GROUP_DATA
;
7265 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
7267 flags
= BTRFS_BLOCK_GROUP_METADATA
;
7268 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
7272 flags
= BTRFS_BLOCK_GROUP_DATA
;
7273 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
7279 int btrfs_error_unpin_extent_range(struct btrfs_root
*root
, u64 start
, u64 end
)
7281 return unpin_extent_range(root
, start
, end
);
7284 int btrfs_error_discard_extent(struct btrfs_root
*root
, u64 bytenr
,
7285 u64 num_bytes
, u64
*actual_bytes
)
7287 return btrfs_discard_extent(root
, bytenr
, num_bytes
, actual_bytes
);
7290 int btrfs_trim_fs(struct btrfs_root
*root
, struct fstrim_range
*range
)
7292 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
7293 struct btrfs_block_group_cache
*cache
= NULL
;
7300 cache
= btrfs_lookup_block_group(fs_info
, range
->start
);
7303 if (cache
->key
.objectid
>= (range
->start
+ range
->len
)) {
7304 btrfs_put_block_group(cache
);
7308 start
= max(range
->start
, cache
->key
.objectid
);
7309 end
= min(range
->start
+ range
->len
,
7310 cache
->key
.objectid
+ cache
->key
.offset
);
7312 if (end
- start
>= range
->minlen
) {
7313 if (!block_group_cache_done(cache
)) {
7314 ret
= cache_block_group(cache
, NULL
, root
, 0);
7316 wait_block_group_cache_done(cache
);
7318 ret
= btrfs_trim_block_group(cache
,
7324 trimmed
+= group_trimmed
;
7326 btrfs_put_block_group(cache
);
7331 cache
= next_block_group(fs_info
->tree_root
, cache
);
7334 range
->len
= trimmed
;