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 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);
113 * this adds the block group to the fs_info rb tree for the block group
116 static int btrfs_add_block_group_cache(struct btrfs_fs_info
*info
,
117 struct btrfs_block_group_cache
*block_group
)
120 struct rb_node
*parent
= NULL
;
121 struct btrfs_block_group_cache
*cache
;
123 spin_lock(&info
->block_group_cache_lock
);
124 p
= &info
->block_group_cache_tree
.rb_node
;
128 cache
= rb_entry(parent
, struct btrfs_block_group_cache
,
130 if (block_group
->key
.objectid
< cache
->key
.objectid
) {
132 } else if (block_group
->key
.objectid
> cache
->key
.objectid
) {
135 spin_unlock(&info
->block_group_cache_lock
);
140 rb_link_node(&block_group
->cache_node
, parent
, p
);
141 rb_insert_color(&block_group
->cache_node
,
142 &info
->block_group_cache_tree
);
143 spin_unlock(&info
->block_group_cache_lock
);
149 * This will return the block group at or after bytenr if contains is 0, else
150 * it will return the block group that contains the bytenr
152 static struct btrfs_block_group_cache
*
153 block_group_cache_tree_search(struct btrfs_fs_info
*info
, u64 bytenr
,
156 struct btrfs_block_group_cache
*cache
, *ret
= NULL
;
160 spin_lock(&info
->block_group_cache_lock
);
161 n
= info
->block_group_cache_tree
.rb_node
;
164 cache
= rb_entry(n
, struct btrfs_block_group_cache
,
166 end
= cache
->key
.objectid
+ cache
->key
.offset
- 1;
167 start
= cache
->key
.objectid
;
169 if (bytenr
< start
) {
170 if (!contains
&& (!ret
|| start
< ret
->key
.objectid
))
173 } else if (bytenr
> start
) {
174 if (contains
&& bytenr
<= end
) {
185 btrfs_get_block_group(ret
);
186 spin_unlock(&info
->block_group_cache_lock
);
191 static int add_excluded_extent(struct btrfs_root
*root
,
192 u64 start
, u64 num_bytes
)
194 u64 end
= start
+ num_bytes
- 1;
195 set_extent_bits(&root
->fs_info
->freed_extents
[0],
196 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
197 set_extent_bits(&root
->fs_info
->freed_extents
[1],
198 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
202 static void free_excluded_extents(struct btrfs_root
*root
,
203 struct btrfs_block_group_cache
*cache
)
207 start
= cache
->key
.objectid
;
208 end
= start
+ cache
->key
.offset
- 1;
210 clear_extent_bits(&root
->fs_info
->freed_extents
[0],
211 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
212 clear_extent_bits(&root
->fs_info
->freed_extents
[1],
213 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
216 static int exclude_super_stripes(struct btrfs_root
*root
,
217 struct btrfs_block_group_cache
*cache
)
224 if (cache
->key
.objectid
< BTRFS_SUPER_INFO_OFFSET
) {
225 stripe_len
= BTRFS_SUPER_INFO_OFFSET
- cache
->key
.objectid
;
226 cache
->bytes_super
+= stripe_len
;
227 ret
= add_excluded_extent(root
, cache
->key
.objectid
,
232 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
233 bytenr
= btrfs_sb_offset(i
);
234 ret
= btrfs_rmap_block(&root
->fs_info
->mapping_tree
,
235 cache
->key
.objectid
, bytenr
,
236 0, &logical
, &nr
, &stripe_len
);
240 cache
->bytes_super
+= stripe_len
;
241 ret
= add_excluded_extent(root
, logical
[nr
],
251 static struct btrfs_caching_control
*
252 get_caching_control(struct btrfs_block_group_cache
*cache
)
254 struct btrfs_caching_control
*ctl
;
256 spin_lock(&cache
->lock
);
257 if (cache
->cached
!= BTRFS_CACHE_STARTED
) {
258 spin_unlock(&cache
->lock
);
262 /* We're loading it the fast way, so we don't have a caching_ctl. */
263 if (!cache
->caching_ctl
) {
264 spin_unlock(&cache
->lock
);
268 ctl
= cache
->caching_ctl
;
269 atomic_inc(&ctl
->count
);
270 spin_unlock(&cache
->lock
);
274 static void put_caching_control(struct btrfs_caching_control
*ctl
)
276 if (atomic_dec_and_test(&ctl
->count
))
281 * this is only called by cache_block_group, since we could have freed extents
282 * we need to check the pinned_extents for any extents that can't be used yet
283 * since their free space will be released as soon as the transaction commits.
285 static u64
add_new_free_space(struct btrfs_block_group_cache
*block_group
,
286 struct btrfs_fs_info
*info
, u64 start
, u64 end
)
288 u64 extent_start
, extent_end
, size
, total_added
= 0;
291 while (start
< end
) {
292 ret
= find_first_extent_bit(info
->pinned_extents
, start
,
293 &extent_start
, &extent_end
,
294 EXTENT_DIRTY
| EXTENT_UPTODATE
);
298 if (extent_start
<= start
) {
299 start
= extent_end
+ 1;
300 } else if (extent_start
> start
&& extent_start
< end
) {
301 size
= extent_start
- start
;
303 ret
= btrfs_add_free_space(block_group
, start
,
306 start
= extent_end
+ 1;
315 ret
= btrfs_add_free_space(block_group
, start
, size
);
322 static int caching_kthread(void *data
)
324 struct btrfs_block_group_cache
*block_group
= data
;
325 struct btrfs_fs_info
*fs_info
= block_group
->fs_info
;
326 struct btrfs_caching_control
*caching_ctl
= block_group
->caching_ctl
;
327 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
328 struct btrfs_path
*path
;
329 struct extent_buffer
*leaf
;
330 struct btrfs_key key
;
336 path
= btrfs_alloc_path();
340 last
= max_t(u64
, block_group
->key
.objectid
, BTRFS_SUPER_INFO_OFFSET
);
343 * We don't want to deadlock with somebody trying to allocate a new
344 * extent for the extent root while also trying to search the extent
345 * root to add free space. So we skip locking and search the commit
346 * root, since its read-only
348 path
->skip_locking
= 1;
349 path
->search_commit_root
= 1;
354 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
356 mutex_lock(&caching_ctl
->mutex
);
357 /* need to make sure the commit_root doesn't disappear */
358 down_read(&fs_info
->extent_commit_sem
);
360 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
364 leaf
= path
->nodes
[0];
365 nritems
= btrfs_header_nritems(leaf
);
369 if (fs_info
->closing
> 1) {
374 if (path
->slots
[0] < nritems
) {
375 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
377 ret
= find_next_key(path
, 0, &key
);
381 caching_ctl
->progress
= last
;
382 btrfs_release_path(extent_root
, path
);
383 up_read(&fs_info
->extent_commit_sem
);
384 mutex_unlock(&caching_ctl
->mutex
);
385 if (btrfs_transaction_in_commit(fs_info
))
392 if (key
.objectid
< block_group
->key
.objectid
) {
397 if (key
.objectid
>= block_group
->key
.objectid
+
398 block_group
->key
.offset
)
401 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
402 total_found
+= add_new_free_space(block_group
,
405 last
= key
.objectid
+ key
.offset
;
407 if (total_found
> (1024 * 1024 * 2)) {
409 wake_up(&caching_ctl
->wait
);
416 total_found
+= add_new_free_space(block_group
, fs_info
, last
,
417 block_group
->key
.objectid
+
418 block_group
->key
.offset
);
419 caching_ctl
->progress
= (u64
)-1;
421 spin_lock(&block_group
->lock
);
422 block_group
->caching_ctl
= NULL
;
423 block_group
->cached
= BTRFS_CACHE_FINISHED
;
424 spin_unlock(&block_group
->lock
);
427 btrfs_free_path(path
);
428 up_read(&fs_info
->extent_commit_sem
);
430 free_excluded_extents(extent_root
, block_group
);
432 mutex_unlock(&caching_ctl
->mutex
);
433 wake_up(&caching_ctl
->wait
);
435 put_caching_control(caching_ctl
);
436 atomic_dec(&block_group
->space_info
->caching_threads
);
437 btrfs_put_block_group(block_group
);
442 static int cache_block_group(struct btrfs_block_group_cache
*cache
,
443 struct btrfs_trans_handle
*trans
,
444 struct btrfs_root
*root
,
447 struct btrfs_fs_info
*fs_info
= cache
->fs_info
;
448 struct btrfs_caching_control
*caching_ctl
;
449 struct task_struct
*tsk
;
453 if (cache
->cached
!= BTRFS_CACHE_NO
)
457 * We can't do the read from on-disk cache during a commit since we need
458 * to have the normal tree locking. Also if we are currently trying to
459 * allocate blocks for the tree root we can't do the fast caching since
460 * we likely hold important locks.
462 if (trans
&& (!trans
->transaction
->in_commit
) &&
463 (root
&& root
!= root
->fs_info
->tree_root
)) {
464 spin_lock(&cache
->lock
);
465 if (cache
->cached
!= BTRFS_CACHE_NO
) {
466 spin_unlock(&cache
->lock
);
469 cache
->cached
= BTRFS_CACHE_STARTED
;
470 spin_unlock(&cache
->lock
);
472 ret
= load_free_space_cache(fs_info
, cache
);
474 spin_lock(&cache
->lock
);
476 cache
->cached
= BTRFS_CACHE_FINISHED
;
477 cache
->last_byte_to_unpin
= (u64
)-1;
479 cache
->cached
= BTRFS_CACHE_NO
;
481 spin_unlock(&cache
->lock
);
483 free_excluded_extents(fs_info
->extent_root
, cache
);
491 caching_ctl
= kzalloc(sizeof(*caching_ctl
), GFP_NOFS
);
492 BUG_ON(!caching_ctl
);
494 INIT_LIST_HEAD(&caching_ctl
->list
);
495 mutex_init(&caching_ctl
->mutex
);
496 init_waitqueue_head(&caching_ctl
->wait
);
497 caching_ctl
->block_group
= cache
;
498 caching_ctl
->progress
= cache
->key
.objectid
;
499 /* one for caching kthread, one for caching block group list */
500 atomic_set(&caching_ctl
->count
, 2);
502 spin_lock(&cache
->lock
);
503 if (cache
->cached
!= BTRFS_CACHE_NO
) {
504 spin_unlock(&cache
->lock
);
508 cache
->caching_ctl
= caching_ctl
;
509 cache
->cached
= BTRFS_CACHE_STARTED
;
510 spin_unlock(&cache
->lock
);
512 down_write(&fs_info
->extent_commit_sem
);
513 list_add_tail(&caching_ctl
->list
, &fs_info
->caching_block_groups
);
514 up_write(&fs_info
->extent_commit_sem
);
516 atomic_inc(&cache
->space_info
->caching_threads
);
517 btrfs_get_block_group(cache
);
519 tsk
= kthread_run(caching_kthread
, cache
, "btrfs-cache-%llu\n",
520 cache
->key
.objectid
);
523 printk(KERN_ERR
"error running thread %d\n", ret
);
531 * return the block group that starts at or after bytenr
533 static struct btrfs_block_group_cache
*
534 btrfs_lookup_first_block_group(struct btrfs_fs_info
*info
, u64 bytenr
)
536 struct btrfs_block_group_cache
*cache
;
538 cache
= block_group_cache_tree_search(info
, bytenr
, 0);
544 * return the block group that contains the given bytenr
546 struct btrfs_block_group_cache
*btrfs_lookup_block_group(
547 struct btrfs_fs_info
*info
,
550 struct btrfs_block_group_cache
*cache
;
552 cache
= block_group_cache_tree_search(info
, bytenr
, 1);
557 static struct btrfs_space_info
*__find_space_info(struct btrfs_fs_info
*info
,
560 struct list_head
*head
= &info
->space_info
;
561 struct btrfs_space_info
*found
;
563 flags
&= BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_SYSTEM
|
564 BTRFS_BLOCK_GROUP_METADATA
;
567 list_for_each_entry_rcu(found
, head
, list
) {
568 if (found
->flags
& flags
) {
578 * after adding space to the filesystem, we need to clear the full flags
579 * on all the space infos.
581 void btrfs_clear_space_info_full(struct btrfs_fs_info
*info
)
583 struct list_head
*head
= &info
->space_info
;
584 struct btrfs_space_info
*found
;
587 list_for_each_entry_rcu(found
, head
, list
)
592 static u64
div_factor(u64 num
, int factor
)
601 static u64
div_factor_fine(u64 num
, int factor
)
610 u64
btrfs_find_block_group(struct btrfs_root
*root
,
611 u64 search_start
, u64 search_hint
, int owner
)
613 struct btrfs_block_group_cache
*cache
;
615 u64 last
= max(search_hint
, search_start
);
622 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
626 spin_lock(&cache
->lock
);
627 last
= cache
->key
.objectid
+ cache
->key
.offset
;
628 used
= btrfs_block_group_used(&cache
->item
);
630 if ((full_search
|| !cache
->ro
) &&
631 block_group_bits(cache
, BTRFS_BLOCK_GROUP_METADATA
)) {
632 if (used
+ cache
->pinned
+ cache
->reserved
<
633 div_factor(cache
->key
.offset
, factor
)) {
634 group_start
= cache
->key
.objectid
;
635 spin_unlock(&cache
->lock
);
636 btrfs_put_block_group(cache
);
640 spin_unlock(&cache
->lock
);
641 btrfs_put_block_group(cache
);
649 if (!full_search
&& factor
< 10) {
659 /* simple helper to search for an existing extent at a given offset */
660 int btrfs_lookup_extent(struct btrfs_root
*root
, u64 start
, u64 len
)
663 struct btrfs_key key
;
664 struct btrfs_path
*path
;
666 path
= btrfs_alloc_path();
668 key
.objectid
= start
;
670 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
671 ret
= btrfs_search_slot(NULL
, root
->fs_info
->extent_root
, &key
, path
,
673 btrfs_free_path(path
);
678 * helper function to lookup reference count and flags of extent.
680 * the head node for delayed ref is used to store the sum of all the
681 * reference count modifications queued up in the rbtree. the head
682 * node may also store the extent flags to set. This way you can check
683 * to see what the reference count and extent flags would be if all of
684 * the delayed refs are not processed.
686 int btrfs_lookup_extent_info(struct btrfs_trans_handle
*trans
,
687 struct btrfs_root
*root
, u64 bytenr
,
688 u64 num_bytes
, u64
*refs
, u64
*flags
)
690 struct btrfs_delayed_ref_head
*head
;
691 struct btrfs_delayed_ref_root
*delayed_refs
;
692 struct btrfs_path
*path
;
693 struct btrfs_extent_item
*ei
;
694 struct extent_buffer
*leaf
;
695 struct btrfs_key key
;
701 path
= btrfs_alloc_path();
705 key
.objectid
= bytenr
;
706 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
707 key
.offset
= num_bytes
;
709 path
->skip_locking
= 1;
710 path
->search_commit_root
= 1;
713 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
,
719 leaf
= path
->nodes
[0];
720 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
721 if (item_size
>= sizeof(*ei
)) {
722 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
723 struct btrfs_extent_item
);
724 num_refs
= btrfs_extent_refs(leaf
, ei
);
725 extent_flags
= btrfs_extent_flags(leaf
, ei
);
727 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
728 struct btrfs_extent_item_v0
*ei0
;
729 BUG_ON(item_size
!= sizeof(*ei0
));
730 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
731 struct btrfs_extent_item_v0
);
732 num_refs
= btrfs_extent_refs_v0(leaf
, ei0
);
733 /* FIXME: this isn't correct for data */
734 extent_flags
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
739 BUG_ON(num_refs
== 0);
749 delayed_refs
= &trans
->transaction
->delayed_refs
;
750 spin_lock(&delayed_refs
->lock
);
751 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
753 if (!mutex_trylock(&head
->mutex
)) {
754 atomic_inc(&head
->node
.refs
);
755 spin_unlock(&delayed_refs
->lock
);
757 btrfs_release_path(root
->fs_info
->extent_root
, path
);
759 mutex_lock(&head
->mutex
);
760 mutex_unlock(&head
->mutex
);
761 btrfs_put_delayed_ref(&head
->node
);
764 if (head
->extent_op
&& head
->extent_op
->update_flags
)
765 extent_flags
|= head
->extent_op
->flags_to_set
;
767 BUG_ON(num_refs
== 0);
769 num_refs
+= head
->node
.ref_mod
;
770 mutex_unlock(&head
->mutex
);
772 spin_unlock(&delayed_refs
->lock
);
774 WARN_ON(num_refs
== 0);
778 *flags
= extent_flags
;
780 btrfs_free_path(path
);
785 * Back reference rules. Back refs have three main goals:
787 * 1) differentiate between all holders of references to an extent so that
788 * when a reference is dropped we can make sure it was a valid reference
789 * before freeing the extent.
791 * 2) Provide enough information to quickly find the holders of an extent
792 * if we notice a given block is corrupted or bad.
794 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
795 * maintenance. This is actually the same as #2, but with a slightly
796 * different use case.
798 * There are two kinds of back refs. The implicit back refs is optimized
799 * for pointers in non-shared tree blocks. For a given pointer in a block,
800 * back refs of this kind provide information about the block's owner tree
801 * and the pointer's key. These information allow us to find the block by
802 * b-tree searching. The full back refs is for pointers in tree blocks not
803 * referenced by their owner trees. The location of tree block is recorded
804 * in the back refs. Actually the full back refs is generic, and can be
805 * used in all cases the implicit back refs is used. The major shortcoming
806 * of the full back refs is its overhead. Every time a tree block gets
807 * COWed, we have to update back refs entry for all pointers in it.
809 * For a newly allocated tree block, we use implicit back refs for
810 * pointers in it. This means most tree related operations only involve
811 * implicit back refs. For a tree block created in old transaction, the
812 * only way to drop a reference to it is COW it. So we can detect the
813 * event that tree block loses its owner tree's reference and do the
814 * back refs conversion.
816 * When a tree block is COW'd through a tree, there are four cases:
818 * The reference count of the block is one and the tree is the block's
819 * owner tree. Nothing to do in this case.
821 * The reference count of the block is one and the tree is not the
822 * block's owner tree. In this case, full back refs is used for pointers
823 * in the block. Remove these full back refs, add implicit back refs for
824 * every pointers in the new block.
826 * The reference count of the block is greater than one and the tree is
827 * the block's owner tree. In this case, implicit back refs is used for
828 * pointers in the block. Add full back refs for every pointers in the
829 * block, increase lower level extents' reference counts. The original
830 * implicit back refs are entailed to the new block.
832 * The reference count of the block is greater than one and the tree is
833 * not the block's owner tree. Add implicit back refs for every pointer in
834 * the new block, increase lower level extents' reference count.
836 * Back Reference Key composing:
838 * The key objectid corresponds to the first byte in the extent,
839 * The key type is used to differentiate between types of back refs.
840 * There are different meanings of the key offset for different types
843 * File extents can be referenced by:
845 * - multiple snapshots, subvolumes, or different generations in one subvol
846 * - different files inside a single subvolume
847 * - different offsets inside a file (bookend extents in file.c)
849 * The extent ref structure for the implicit back refs has fields for:
851 * - Objectid of the subvolume root
852 * - objectid of the file holding the reference
853 * - original offset in the file
854 * - how many bookend extents
856 * The key offset for the implicit back refs is hash of the first
859 * The extent ref structure for the full back refs has field for:
861 * - number of pointers in the tree leaf
863 * The key offset for the implicit back refs is the first byte of
866 * When a file extent is allocated, The implicit back refs is used.
867 * the fields are filled in:
869 * (root_key.objectid, inode objectid, offset in file, 1)
871 * When a file extent is removed file truncation, we find the
872 * corresponding implicit back refs and check the following fields:
874 * (btrfs_header_owner(leaf), inode objectid, offset in file)
876 * Btree extents can be referenced by:
878 * - Different subvolumes
880 * Both the implicit back refs and the full back refs for tree blocks
881 * only consist of key. The key offset for the implicit back refs is
882 * objectid of block's owner tree. The key offset for the full back refs
883 * is the first byte of parent block.
885 * When implicit back refs is used, information about the lowest key and
886 * level of the tree block are required. These information are stored in
887 * tree block info structure.
890 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
891 static int convert_extent_item_v0(struct btrfs_trans_handle
*trans
,
892 struct btrfs_root
*root
,
893 struct btrfs_path
*path
,
894 u64 owner
, u32 extra_size
)
896 struct btrfs_extent_item
*item
;
897 struct btrfs_extent_item_v0
*ei0
;
898 struct btrfs_extent_ref_v0
*ref0
;
899 struct btrfs_tree_block_info
*bi
;
900 struct extent_buffer
*leaf
;
901 struct btrfs_key key
;
902 struct btrfs_key found_key
;
903 u32 new_size
= sizeof(*item
);
907 leaf
= path
->nodes
[0];
908 BUG_ON(btrfs_item_size_nr(leaf
, path
->slots
[0]) != sizeof(*ei0
));
910 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
911 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
912 struct btrfs_extent_item_v0
);
913 refs
= btrfs_extent_refs_v0(leaf
, ei0
);
915 if (owner
== (u64
)-1) {
917 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
918 ret
= btrfs_next_leaf(root
, path
);
922 leaf
= path
->nodes
[0];
924 btrfs_item_key_to_cpu(leaf
, &found_key
,
926 BUG_ON(key
.objectid
!= found_key
.objectid
);
927 if (found_key
.type
!= BTRFS_EXTENT_REF_V0_KEY
) {
931 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
932 struct btrfs_extent_ref_v0
);
933 owner
= btrfs_ref_objectid_v0(leaf
, ref0
);
937 btrfs_release_path(root
, path
);
939 if (owner
< BTRFS_FIRST_FREE_OBJECTID
)
940 new_size
+= sizeof(*bi
);
942 new_size
-= sizeof(*ei0
);
943 ret
= btrfs_search_slot(trans
, root
, &key
, path
,
944 new_size
+ extra_size
, 1);
949 ret
= btrfs_extend_item(trans
, root
, path
, new_size
);
952 leaf
= path
->nodes
[0];
953 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
954 btrfs_set_extent_refs(leaf
, item
, refs
);
955 /* FIXME: get real generation */
956 btrfs_set_extent_generation(leaf
, item
, 0);
957 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
958 btrfs_set_extent_flags(leaf
, item
,
959 BTRFS_EXTENT_FLAG_TREE_BLOCK
|
960 BTRFS_BLOCK_FLAG_FULL_BACKREF
);
961 bi
= (struct btrfs_tree_block_info
*)(item
+ 1);
962 /* FIXME: get first key of the block */
963 memset_extent_buffer(leaf
, 0, (unsigned long)bi
, sizeof(*bi
));
964 btrfs_set_tree_block_level(leaf
, bi
, (int)owner
);
966 btrfs_set_extent_flags(leaf
, item
, BTRFS_EXTENT_FLAG_DATA
);
968 btrfs_mark_buffer_dirty(leaf
);
973 static u64
hash_extent_data_ref(u64 root_objectid
, u64 owner
, u64 offset
)
975 u32 high_crc
= ~(u32
)0;
976 u32 low_crc
= ~(u32
)0;
979 lenum
= cpu_to_le64(root_objectid
);
980 high_crc
= crc32c(high_crc
, &lenum
, sizeof(lenum
));
981 lenum
= cpu_to_le64(owner
);
982 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
983 lenum
= cpu_to_le64(offset
);
984 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
986 return ((u64
)high_crc
<< 31) ^ (u64
)low_crc
;
989 static u64
hash_extent_data_ref_item(struct extent_buffer
*leaf
,
990 struct btrfs_extent_data_ref
*ref
)
992 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf
, ref
),
993 btrfs_extent_data_ref_objectid(leaf
, ref
),
994 btrfs_extent_data_ref_offset(leaf
, ref
));
997 static int match_extent_data_ref(struct extent_buffer
*leaf
,
998 struct btrfs_extent_data_ref
*ref
,
999 u64 root_objectid
, u64 owner
, u64 offset
)
1001 if (btrfs_extent_data_ref_root(leaf
, ref
) != root_objectid
||
1002 btrfs_extent_data_ref_objectid(leaf
, ref
) != owner
||
1003 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
1008 static noinline
int lookup_extent_data_ref(struct btrfs_trans_handle
*trans
,
1009 struct btrfs_root
*root
,
1010 struct btrfs_path
*path
,
1011 u64 bytenr
, u64 parent
,
1013 u64 owner
, u64 offset
)
1015 struct btrfs_key key
;
1016 struct btrfs_extent_data_ref
*ref
;
1017 struct extent_buffer
*leaf
;
1023 key
.objectid
= bytenr
;
1025 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
1026 key
.offset
= parent
;
1028 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
1029 key
.offset
= hash_extent_data_ref(root_objectid
,
1034 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1043 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1044 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
1045 btrfs_release_path(root
, path
);
1046 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1057 leaf
= path
->nodes
[0];
1058 nritems
= btrfs_header_nritems(leaf
);
1060 if (path
->slots
[0] >= nritems
) {
1061 ret
= btrfs_next_leaf(root
, path
);
1067 leaf
= path
->nodes
[0];
1068 nritems
= btrfs_header_nritems(leaf
);
1072 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1073 if (key
.objectid
!= bytenr
||
1074 key
.type
!= BTRFS_EXTENT_DATA_REF_KEY
)
1077 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1078 struct btrfs_extent_data_ref
);
1080 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
1083 btrfs_release_path(root
, path
);
1095 static noinline
int insert_extent_data_ref(struct btrfs_trans_handle
*trans
,
1096 struct btrfs_root
*root
,
1097 struct btrfs_path
*path
,
1098 u64 bytenr
, u64 parent
,
1099 u64 root_objectid
, u64 owner
,
1100 u64 offset
, int refs_to_add
)
1102 struct btrfs_key key
;
1103 struct extent_buffer
*leaf
;
1108 key
.objectid
= bytenr
;
1110 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
1111 key
.offset
= parent
;
1112 size
= sizeof(struct btrfs_shared_data_ref
);
1114 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
1115 key
.offset
= hash_extent_data_ref(root_objectid
,
1117 size
= sizeof(struct btrfs_extent_data_ref
);
1120 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, size
);
1121 if (ret
&& ret
!= -EEXIST
)
1124 leaf
= path
->nodes
[0];
1126 struct btrfs_shared_data_ref
*ref
;
1127 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1128 struct btrfs_shared_data_ref
);
1130 btrfs_set_shared_data_ref_count(leaf
, ref
, refs_to_add
);
1132 num_refs
= btrfs_shared_data_ref_count(leaf
, ref
);
1133 num_refs
+= refs_to_add
;
1134 btrfs_set_shared_data_ref_count(leaf
, ref
, num_refs
);
1137 struct btrfs_extent_data_ref
*ref
;
1138 while (ret
== -EEXIST
) {
1139 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1140 struct btrfs_extent_data_ref
);
1141 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
1144 btrfs_release_path(root
, path
);
1146 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1148 if (ret
&& ret
!= -EEXIST
)
1151 leaf
= path
->nodes
[0];
1153 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1154 struct btrfs_extent_data_ref
);
1156 btrfs_set_extent_data_ref_root(leaf
, ref
,
1158 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
1159 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
1160 btrfs_set_extent_data_ref_count(leaf
, ref
, refs_to_add
);
1162 num_refs
= btrfs_extent_data_ref_count(leaf
, ref
);
1163 num_refs
+= refs_to_add
;
1164 btrfs_set_extent_data_ref_count(leaf
, ref
, num_refs
);
1167 btrfs_mark_buffer_dirty(leaf
);
1170 btrfs_release_path(root
, path
);
1174 static noinline
int remove_extent_data_ref(struct btrfs_trans_handle
*trans
,
1175 struct btrfs_root
*root
,
1176 struct btrfs_path
*path
,
1179 struct btrfs_key key
;
1180 struct btrfs_extent_data_ref
*ref1
= NULL
;
1181 struct btrfs_shared_data_ref
*ref2
= NULL
;
1182 struct extent_buffer
*leaf
;
1186 leaf
= path
->nodes
[0];
1187 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1189 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1190 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
1191 struct btrfs_extent_data_ref
);
1192 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1193 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
1194 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
1195 struct btrfs_shared_data_ref
);
1196 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1197 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1198 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
1199 struct btrfs_extent_ref_v0
*ref0
;
1200 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1201 struct btrfs_extent_ref_v0
);
1202 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
1208 BUG_ON(num_refs
< refs_to_drop
);
1209 num_refs
-= refs_to_drop
;
1211 if (num_refs
== 0) {
1212 ret
= btrfs_del_item(trans
, root
, path
);
1214 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
)
1215 btrfs_set_extent_data_ref_count(leaf
, ref1
, num_refs
);
1216 else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
)
1217 btrfs_set_shared_data_ref_count(leaf
, ref2
, num_refs
);
1218 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1220 struct btrfs_extent_ref_v0
*ref0
;
1221 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1222 struct btrfs_extent_ref_v0
);
1223 btrfs_set_ref_count_v0(leaf
, ref0
, num_refs
);
1226 btrfs_mark_buffer_dirty(leaf
);
1231 static noinline u32
extent_data_ref_count(struct btrfs_root
*root
,
1232 struct btrfs_path
*path
,
1233 struct btrfs_extent_inline_ref
*iref
)
1235 struct btrfs_key key
;
1236 struct extent_buffer
*leaf
;
1237 struct btrfs_extent_data_ref
*ref1
;
1238 struct btrfs_shared_data_ref
*ref2
;
1241 leaf
= path
->nodes
[0];
1242 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1244 if (btrfs_extent_inline_ref_type(leaf
, iref
) ==
1245 BTRFS_EXTENT_DATA_REF_KEY
) {
1246 ref1
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1247 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1249 ref2
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1250 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1252 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1253 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
1254 struct btrfs_extent_data_ref
);
1255 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1256 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
1257 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
1258 struct btrfs_shared_data_ref
);
1259 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1260 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1261 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
1262 struct btrfs_extent_ref_v0
*ref0
;
1263 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1264 struct btrfs_extent_ref_v0
);
1265 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
1273 static noinline
int lookup_tree_block_ref(struct btrfs_trans_handle
*trans
,
1274 struct btrfs_root
*root
,
1275 struct btrfs_path
*path
,
1276 u64 bytenr
, u64 parent
,
1279 struct btrfs_key key
;
1282 key
.objectid
= bytenr
;
1284 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1285 key
.offset
= parent
;
1287 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
1288 key
.offset
= root_objectid
;
1291 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1294 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1295 if (ret
== -ENOENT
&& parent
) {
1296 btrfs_release_path(root
, path
);
1297 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
1298 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1306 static noinline
int insert_tree_block_ref(struct btrfs_trans_handle
*trans
,
1307 struct btrfs_root
*root
,
1308 struct btrfs_path
*path
,
1309 u64 bytenr
, u64 parent
,
1312 struct btrfs_key key
;
1315 key
.objectid
= bytenr
;
1317 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1318 key
.offset
= parent
;
1320 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
1321 key
.offset
= root_objectid
;
1324 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, 0);
1325 btrfs_release_path(root
, path
);
1329 static inline int extent_ref_type(u64 parent
, u64 owner
)
1332 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1334 type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1336 type
= BTRFS_TREE_BLOCK_REF_KEY
;
1339 type
= BTRFS_SHARED_DATA_REF_KEY
;
1341 type
= BTRFS_EXTENT_DATA_REF_KEY
;
1346 static int find_next_key(struct btrfs_path
*path
, int level
,
1347 struct btrfs_key
*key
)
1350 for (; level
< BTRFS_MAX_LEVEL
; level
++) {
1351 if (!path
->nodes
[level
])
1353 if (path
->slots
[level
] + 1 >=
1354 btrfs_header_nritems(path
->nodes
[level
]))
1357 btrfs_item_key_to_cpu(path
->nodes
[level
], key
,
1358 path
->slots
[level
] + 1);
1360 btrfs_node_key_to_cpu(path
->nodes
[level
], key
,
1361 path
->slots
[level
] + 1);
1368 * look for inline back ref. if back ref is found, *ref_ret is set
1369 * to the address of inline back ref, and 0 is returned.
1371 * if back ref isn't found, *ref_ret is set to the address where it
1372 * should be inserted, and -ENOENT is returned.
1374 * if insert is true and there are too many inline back refs, the path
1375 * points to the extent item, and -EAGAIN is returned.
1377 * NOTE: inline back refs are ordered in the same way that back ref
1378 * items in the tree are ordered.
1380 static noinline_for_stack
1381 int lookup_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1382 struct btrfs_root
*root
,
1383 struct btrfs_path
*path
,
1384 struct btrfs_extent_inline_ref
**ref_ret
,
1385 u64 bytenr
, u64 num_bytes
,
1386 u64 parent
, u64 root_objectid
,
1387 u64 owner
, u64 offset
, int insert
)
1389 struct btrfs_key key
;
1390 struct extent_buffer
*leaf
;
1391 struct btrfs_extent_item
*ei
;
1392 struct btrfs_extent_inline_ref
*iref
;
1403 key
.objectid
= bytenr
;
1404 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1405 key
.offset
= num_bytes
;
1407 want
= extent_ref_type(parent
, owner
);
1409 extra_size
= btrfs_extent_inline_ref_size(want
);
1410 path
->keep_locks
= 1;
1413 ret
= btrfs_search_slot(trans
, root
, &key
, path
, extra_size
, 1);
1420 leaf
= path
->nodes
[0];
1421 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1422 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1423 if (item_size
< sizeof(*ei
)) {
1428 ret
= convert_extent_item_v0(trans
, root
, path
, owner
,
1434 leaf
= path
->nodes
[0];
1435 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1438 BUG_ON(item_size
< sizeof(*ei
));
1440 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1441 flags
= btrfs_extent_flags(leaf
, ei
);
1443 ptr
= (unsigned long)(ei
+ 1);
1444 end
= (unsigned long)ei
+ item_size
;
1446 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
1447 ptr
+= sizeof(struct btrfs_tree_block_info
);
1450 BUG_ON(!(flags
& BTRFS_EXTENT_FLAG_DATA
));
1459 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1460 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1464 ptr
+= btrfs_extent_inline_ref_size(type
);
1468 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1469 struct btrfs_extent_data_ref
*dref
;
1470 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1471 if (match_extent_data_ref(leaf
, dref
, root_objectid
,
1476 if (hash_extent_data_ref_item(leaf
, dref
) <
1477 hash_extent_data_ref(root_objectid
, owner
, offset
))
1481 ref_offset
= btrfs_extent_inline_ref_offset(leaf
, iref
);
1483 if (parent
== ref_offset
) {
1487 if (ref_offset
< parent
)
1490 if (root_objectid
== ref_offset
) {
1494 if (ref_offset
< root_objectid
)
1498 ptr
+= btrfs_extent_inline_ref_size(type
);
1500 if (err
== -ENOENT
&& insert
) {
1501 if (item_size
+ extra_size
>=
1502 BTRFS_MAX_EXTENT_ITEM_SIZE(root
)) {
1507 * To add new inline back ref, we have to make sure
1508 * there is no corresponding back ref item.
1509 * For simplicity, we just do not add new inline back
1510 * ref if there is any kind of item for this block
1512 if (find_next_key(path
, 0, &key
) == 0 &&
1513 key
.objectid
== bytenr
&&
1514 key
.type
< BTRFS_BLOCK_GROUP_ITEM_KEY
) {
1519 *ref_ret
= (struct btrfs_extent_inline_ref
*)ptr
;
1522 path
->keep_locks
= 0;
1523 btrfs_unlock_up_safe(path
, 1);
1529 * helper to add new inline back ref
1531 static noinline_for_stack
1532 int setup_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1533 struct btrfs_root
*root
,
1534 struct btrfs_path
*path
,
1535 struct btrfs_extent_inline_ref
*iref
,
1536 u64 parent
, u64 root_objectid
,
1537 u64 owner
, u64 offset
, int refs_to_add
,
1538 struct btrfs_delayed_extent_op
*extent_op
)
1540 struct extent_buffer
*leaf
;
1541 struct btrfs_extent_item
*ei
;
1544 unsigned long item_offset
;
1550 leaf
= path
->nodes
[0];
1551 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1552 item_offset
= (unsigned long)iref
- (unsigned long)ei
;
1554 type
= extent_ref_type(parent
, owner
);
1555 size
= btrfs_extent_inline_ref_size(type
);
1557 ret
= btrfs_extend_item(trans
, root
, path
, size
);
1560 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1561 refs
= btrfs_extent_refs(leaf
, ei
);
1562 refs
+= refs_to_add
;
1563 btrfs_set_extent_refs(leaf
, ei
, refs
);
1565 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1567 ptr
= (unsigned long)ei
+ item_offset
;
1568 end
= (unsigned long)ei
+ btrfs_item_size_nr(leaf
, path
->slots
[0]);
1569 if (ptr
< end
- size
)
1570 memmove_extent_buffer(leaf
, ptr
+ size
, ptr
,
1573 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1574 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
1575 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1576 struct btrfs_extent_data_ref
*dref
;
1577 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1578 btrfs_set_extent_data_ref_root(leaf
, dref
, root_objectid
);
1579 btrfs_set_extent_data_ref_objectid(leaf
, dref
, owner
);
1580 btrfs_set_extent_data_ref_offset(leaf
, dref
, offset
);
1581 btrfs_set_extent_data_ref_count(leaf
, dref
, refs_to_add
);
1582 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1583 struct btrfs_shared_data_ref
*sref
;
1584 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1585 btrfs_set_shared_data_ref_count(leaf
, sref
, refs_to_add
);
1586 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1587 } else if (type
== BTRFS_SHARED_BLOCK_REF_KEY
) {
1588 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1590 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
1592 btrfs_mark_buffer_dirty(leaf
);
1596 static int lookup_extent_backref(struct btrfs_trans_handle
*trans
,
1597 struct btrfs_root
*root
,
1598 struct btrfs_path
*path
,
1599 struct btrfs_extent_inline_ref
**ref_ret
,
1600 u64 bytenr
, u64 num_bytes
, u64 parent
,
1601 u64 root_objectid
, u64 owner
, u64 offset
)
1605 ret
= lookup_inline_extent_backref(trans
, root
, path
, ref_ret
,
1606 bytenr
, num_bytes
, parent
,
1607 root_objectid
, owner
, offset
, 0);
1611 btrfs_release_path(root
, path
);
1614 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1615 ret
= lookup_tree_block_ref(trans
, root
, path
, bytenr
, parent
,
1618 ret
= lookup_extent_data_ref(trans
, root
, path
, bytenr
, parent
,
1619 root_objectid
, owner
, offset
);
1625 * helper to update/remove inline back ref
1627 static noinline_for_stack
1628 int update_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1629 struct btrfs_root
*root
,
1630 struct btrfs_path
*path
,
1631 struct btrfs_extent_inline_ref
*iref
,
1633 struct btrfs_delayed_extent_op
*extent_op
)
1635 struct extent_buffer
*leaf
;
1636 struct btrfs_extent_item
*ei
;
1637 struct btrfs_extent_data_ref
*dref
= NULL
;
1638 struct btrfs_shared_data_ref
*sref
= NULL
;
1647 leaf
= path
->nodes
[0];
1648 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1649 refs
= btrfs_extent_refs(leaf
, ei
);
1650 WARN_ON(refs_to_mod
< 0 && refs
+ refs_to_mod
<= 0);
1651 refs
+= refs_to_mod
;
1652 btrfs_set_extent_refs(leaf
, ei
, refs
);
1654 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1656 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1658 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1659 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1660 refs
= btrfs_extent_data_ref_count(leaf
, dref
);
1661 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1662 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1663 refs
= btrfs_shared_data_ref_count(leaf
, sref
);
1666 BUG_ON(refs_to_mod
!= -1);
1669 BUG_ON(refs_to_mod
< 0 && refs
< -refs_to_mod
);
1670 refs
+= refs_to_mod
;
1673 if (type
== BTRFS_EXTENT_DATA_REF_KEY
)
1674 btrfs_set_extent_data_ref_count(leaf
, dref
, refs
);
1676 btrfs_set_shared_data_ref_count(leaf
, sref
, refs
);
1678 size
= btrfs_extent_inline_ref_size(type
);
1679 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1680 ptr
= (unsigned long)iref
;
1681 end
= (unsigned long)ei
+ item_size
;
1682 if (ptr
+ size
< end
)
1683 memmove_extent_buffer(leaf
, ptr
, ptr
+ size
,
1686 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(root
->fs_info
->extent_root
, 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
);
2300 mutex_lock(&head
->mutex
);
2301 mutex_unlock(&head
->mutex
);
2303 btrfs_put_delayed_ref(ref
);
2307 node
= rb_next(node
);
2309 spin_unlock(&delayed_refs
->lock
);
2310 schedule_timeout(1);
2314 spin_unlock(&delayed_refs
->lock
);
2318 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle
*trans
,
2319 struct btrfs_root
*root
,
2320 u64 bytenr
, u64 num_bytes
, u64 flags
,
2323 struct btrfs_delayed_extent_op
*extent_op
;
2326 extent_op
= kmalloc(sizeof(*extent_op
), GFP_NOFS
);
2330 extent_op
->flags_to_set
= flags
;
2331 extent_op
->update_flags
= 1;
2332 extent_op
->update_key
= 0;
2333 extent_op
->is_data
= is_data
? 1 : 0;
2335 ret
= btrfs_add_delayed_extent_op(trans
, bytenr
, num_bytes
, extent_op
);
2341 static noinline
int check_delayed_ref(struct btrfs_trans_handle
*trans
,
2342 struct btrfs_root
*root
,
2343 struct btrfs_path
*path
,
2344 u64 objectid
, u64 offset
, u64 bytenr
)
2346 struct btrfs_delayed_ref_head
*head
;
2347 struct btrfs_delayed_ref_node
*ref
;
2348 struct btrfs_delayed_data_ref
*data_ref
;
2349 struct btrfs_delayed_ref_root
*delayed_refs
;
2350 struct rb_node
*node
;
2354 delayed_refs
= &trans
->transaction
->delayed_refs
;
2355 spin_lock(&delayed_refs
->lock
);
2356 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
2360 if (!mutex_trylock(&head
->mutex
)) {
2361 atomic_inc(&head
->node
.refs
);
2362 spin_unlock(&delayed_refs
->lock
);
2364 btrfs_release_path(root
->fs_info
->extent_root
, path
);
2366 mutex_lock(&head
->mutex
);
2367 mutex_unlock(&head
->mutex
);
2368 btrfs_put_delayed_ref(&head
->node
);
2372 node
= rb_prev(&head
->node
.rb_node
);
2376 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
2378 if (ref
->bytenr
!= bytenr
)
2382 if (ref
->type
!= BTRFS_EXTENT_DATA_REF_KEY
)
2385 data_ref
= btrfs_delayed_node_to_data_ref(ref
);
2387 node
= rb_prev(node
);
2389 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
2390 if (ref
->bytenr
== bytenr
)
2394 if (data_ref
->root
!= root
->root_key
.objectid
||
2395 data_ref
->objectid
!= objectid
|| data_ref
->offset
!= offset
)
2400 mutex_unlock(&head
->mutex
);
2402 spin_unlock(&delayed_refs
->lock
);
2406 static noinline
int check_committed_ref(struct btrfs_trans_handle
*trans
,
2407 struct btrfs_root
*root
,
2408 struct btrfs_path
*path
,
2409 u64 objectid
, u64 offset
, u64 bytenr
)
2411 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
2412 struct extent_buffer
*leaf
;
2413 struct btrfs_extent_data_ref
*ref
;
2414 struct btrfs_extent_inline_ref
*iref
;
2415 struct btrfs_extent_item
*ei
;
2416 struct btrfs_key key
;
2420 key
.objectid
= bytenr
;
2421 key
.offset
= (u64
)-1;
2422 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2424 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
2430 if (path
->slots
[0] == 0)
2434 leaf
= path
->nodes
[0];
2435 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2437 if (key
.objectid
!= bytenr
|| key
.type
!= BTRFS_EXTENT_ITEM_KEY
)
2441 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2442 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2443 if (item_size
< sizeof(*ei
)) {
2444 WARN_ON(item_size
!= sizeof(struct btrfs_extent_item_v0
));
2448 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
2450 if (item_size
!= sizeof(*ei
) +
2451 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY
))
2454 if (btrfs_extent_generation(leaf
, ei
) <=
2455 btrfs_root_last_snapshot(&root
->root_item
))
2458 iref
= (struct btrfs_extent_inline_ref
*)(ei
+ 1);
2459 if (btrfs_extent_inline_ref_type(leaf
, iref
) !=
2460 BTRFS_EXTENT_DATA_REF_KEY
)
2463 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
2464 if (btrfs_extent_refs(leaf
, ei
) !=
2465 btrfs_extent_data_ref_count(leaf
, ref
) ||
2466 btrfs_extent_data_ref_root(leaf
, ref
) !=
2467 root
->root_key
.objectid
||
2468 btrfs_extent_data_ref_objectid(leaf
, ref
) != objectid
||
2469 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
2477 int btrfs_cross_ref_exist(struct btrfs_trans_handle
*trans
,
2478 struct btrfs_root
*root
,
2479 u64 objectid
, u64 offset
, u64 bytenr
)
2481 struct btrfs_path
*path
;
2485 path
= btrfs_alloc_path();
2490 ret
= check_committed_ref(trans
, root
, path
, objectid
,
2492 if (ret
&& ret
!= -ENOENT
)
2495 ret2
= check_delayed_ref(trans
, root
, path
, objectid
,
2497 } while (ret2
== -EAGAIN
);
2499 if (ret2
&& ret2
!= -ENOENT
) {
2504 if (ret
!= -ENOENT
|| ret2
!= -ENOENT
)
2507 btrfs_free_path(path
);
2508 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
2514 int btrfs_cache_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2515 struct extent_buffer
*buf
, u32 nr_extents
)
2517 struct btrfs_key key
;
2518 struct btrfs_file_extent_item
*fi
;
2526 if (!root
->ref_cows
)
2529 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
2531 root_gen
= root
->root_key
.offset
;
2534 root_gen
= trans
->transid
- 1;
2537 level
= btrfs_header_level(buf
);
2538 nritems
= btrfs_header_nritems(buf
);
2541 struct btrfs_leaf_ref
*ref
;
2542 struct btrfs_extent_info
*info
;
2544 ref
= btrfs_alloc_leaf_ref(root
, nr_extents
);
2550 ref
->root_gen
= root_gen
;
2551 ref
->bytenr
= buf
->start
;
2552 ref
->owner
= btrfs_header_owner(buf
);
2553 ref
->generation
= btrfs_header_generation(buf
);
2554 ref
->nritems
= nr_extents
;
2555 info
= ref
->extents
;
2557 for (i
= 0; nr_extents
> 0 && i
< nritems
; i
++) {
2559 btrfs_item_key_to_cpu(buf
, &key
, i
);
2560 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
2562 fi
= btrfs_item_ptr(buf
, i
,
2563 struct btrfs_file_extent_item
);
2564 if (btrfs_file_extent_type(buf
, fi
) ==
2565 BTRFS_FILE_EXTENT_INLINE
)
2567 disk_bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
2568 if (disk_bytenr
== 0)
2571 info
->bytenr
= disk_bytenr
;
2573 btrfs_file_extent_disk_num_bytes(buf
, fi
);
2574 info
->objectid
= key
.objectid
;
2575 info
->offset
= key
.offset
;
2579 ret
= btrfs_add_leaf_ref(root
, ref
, shared
);
2580 if (ret
== -EEXIST
&& shared
) {
2581 struct btrfs_leaf_ref
*old
;
2582 old
= btrfs_lookup_leaf_ref(root
, ref
->bytenr
);
2584 btrfs_remove_leaf_ref(root
, old
);
2585 btrfs_free_leaf_ref(root
, old
);
2586 ret
= btrfs_add_leaf_ref(root
, ref
, shared
);
2589 btrfs_free_leaf_ref(root
, ref
);
2595 /* when a block goes through cow, we update the reference counts of
2596 * everything that block points to. The internal pointers of the block
2597 * can be in just about any order, and it is likely to have clusters of
2598 * things that are close together and clusters of things that are not.
2600 * To help reduce the seeks that come with updating all of these reference
2601 * counts, sort them by byte number before actual updates are done.
2603 * struct refsort is used to match byte number to slot in the btree block.
2604 * we sort based on the byte number and then use the slot to actually
2607 * struct refsort is smaller than strcut btrfs_item and smaller than
2608 * struct btrfs_key_ptr. Since we're currently limited to the page size
2609 * for a btree block, there's no way for a kmalloc of refsorts for a
2610 * single node to be bigger than a page.
2618 * for passing into sort()
2620 static int refsort_cmp(const void *a_void
, const void *b_void
)
2622 const struct refsort
*a
= a_void
;
2623 const struct refsort
*b
= b_void
;
2625 if (a
->bytenr
< b
->bytenr
)
2627 if (a
->bytenr
> b
->bytenr
)
2633 static int __btrfs_mod_ref(struct btrfs_trans_handle
*trans
,
2634 struct btrfs_root
*root
,
2635 struct extent_buffer
*buf
,
2636 int full_backref
, int inc
)
2643 struct btrfs_key key
;
2644 struct btrfs_file_extent_item
*fi
;
2648 int (*process_func
)(struct btrfs_trans_handle
*, struct btrfs_root
*,
2649 u64
, u64
, u64
, u64
, u64
, u64
);
2651 ref_root
= btrfs_header_owner(buf
);
2652 nritems
= btrfs_header_nritems(buf
);
2653 level
= btrfs_header_level(buf
);
2655 if (!root
->ref_cows
&& level
== 0)
2659 process_func
= btrfs_inc_extent_ref
;
2661 process_func
= btrfs_free_extent
;
2664 parent
= buf
->start
;
2668 for (i
= 0; i
< nritems
; i
++) {
2670 btrfs_item_key_to_cpu(buf
, &key
, i
);
2671 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
2673 fi
= btrfs_item_ptr(buf
, i
,
2674 struct btrfs_file_extent_item
);
2675 if (btrfs_file_extent_type(buf
, fi
) ==
2676 BTRFS_FILE_EXTENT_INLINE
)
2678 bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
2682 num_bytes
= btrfs_file_extent_disk_num_bytes(buf
, fi
);
2683 key
.offset
-= btrfs_file_extent_offset(buf
, fi
);
2684 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
2685 parent
, ref_root
, key
.objectid
,
2690 bytenr
= btrfs_node_blockptr(buf
, i
);
2691 num_bytes
= btrfs_level_size(root
, level
- 1);
2692 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
2693 parent
, ref_root
, level
- 1, 0);
2704 int btrfs_inc_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2705 struct extent_buffer
*buf
, int full_backref
)
2707 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 1);
2710 int btrfs_dec_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2711 struct extent_buffer
*buf
, int full_backref
)
2713 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 0);
2716 static int write_one_cache_group(struct btrfs_trans_handle
*trans
,
2717 struct btrfs_root
*root
,
2718 struct btrfs_path
*path
,
2719 struct btrfs_block_group_cache
*cache
)
2722 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
2724 struct extent_buffer
*leaf
;
2726 ret
= btrfs_search_slot(trans
, extent_root
, &cache
->key
, path
, 0, 1);
2731 leaf
= path
->nodes
[0];
2732 bi
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
2733 write_extent_buffer(leaf
, &cache
->item
, bi
, sizeof(cache
->item
));
2734 btrfs_mark_buffer_dirty(leaf
);
2735 btrfs_release_path(extent_root
, path
);
2743 static struct btrfs_block_group_cache
*
2744 next_block_group(struct btrfs_root
*root
,
2745 struct btrfs_block_group_cache
*cache
)
2747 struct rb_node
*node
;
2748 spin_lock(&root
->fs_info
->block_group_cache_lock
);
2749 node
= rb_next(&cache
->cache_node
);
2750 btrfs_put_block_group(cache
);
2752 cache
= rb_entry(node
, struct btrfs_block_group_cache
,
2754 btrfs_get_block_group(cache
);
2757 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
2761 static int cache_save_setup(struct btrfs_block_group_cache
*block_group
,
2762 struct btrfs_trans_handle
*trans
,
2763 struct btrfs_path
*path
)
2765 struct btrfs_root
*root
= block_group
->fs_info
->tree_root
;
2766 struct inode
*inode
= NULL
;
2768 int dcs
= BTRFS_DC_ERROR
;
2774 * If this block group is smaller than 100 megs don't bother caching the
2777 if (block_group
->key
.offset
< (100 * 1024 * 1024)) {
2778 spin_lock(&block_group
->lock
);
2779 block_group
->disk_cache_state
= BTRFS_DC_WRITTEN
;
2780 spin_unlock(&block_group
->lock
);
2785 inode
= lookup_free_space_inode(root
, block_group
, path
);
2786 if (IS_ERR(inode
) && PTR_ERR(inode
) != -ENOENT
) {
2787 ret
= PTR_ERR(inode
);
2788 btrfs_release_path(root
, path
);
2792 if (IS_ERR(inode
)) {
2796 if (block_group
->ro
)
2799 ret
= create_free_space_inode(root
, trans
, block_group
, path
);
2806 * We want to set the generation to 0, that way if anything goes wrong
2807 * from here on out we know not to trust this cache when we load up next
2810 BTRFS_I(inode
)->generation
= 0;
2811 ret
= btrfs_update_inode(trans
, root
, inode
);
2814 if (i_size_read(inode
) > 0) {
2815 ret
= btrfs_truncate_free_space_cache(root
, trans
, path
,
2821 spin_lock(&block_group
->lock
);
2822 if (block_group
->cached
!= BTRFS_CACHE_FINISHED
) {
2823 /* We're not cached, don't bother trying to write stuff out */
2824 dcs
= BTRFS_DC_WRITTEN
;
2825 spin_unlock(&block_group
->lock
);
2828 spin_unlock(&block_group
->lock
);
2830 num_pages
= (int)div64_u64(block_group
->key
.offset
, 1024 * 1024 * 1024);
2835 * Just to make absolutely sure we have enough space, we're going to
2836 * preallocate 12 pages worth of space for each block group. In
2837 * practice we ought to use at most 8, but we need extra space so we can
2838 * add our header and have a terminator between the extents and the
2842 num_pages
*= PAGE_CACHE_SIZE
;
2844 ret
= btrfs_check_data_free_space(inode
, num_pages
);
2848 ret
= btrfs_prealloc_file_range_trans(inode
, trans
, 0, 0, num_pages
,
2849 num_pages
, num_pages
,
2852 dcs
= BTRFS_DC_SETUP
;
2853 btrfs_free_reserved_data_space(inode
, num_pages
);
2857 btrfs_release_path(root
, path
);
2859 spin_lock(&block_group
->lock
);
2860 block_group
->disk_cache_state
= dcs
;
2861 spin_unlock(&block_group
->lock
);
2866 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle
*trans
,
2867 struct btrfs_root
*root
)
2869 struct btrfs_block_group_cache
*cache
;
2871 struct btrfs_path
*path
;
2874 path
= btrfs_alloc_path();
2880 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
2882 if (cache
->disk_cache_state
== BTRFS_DC_CLEAR
)
2884 cache
= next_block_group(root
, cache
);
2892 err
= cache_save_setup(cache
, trans
, path
);
2893 last
= cache
->key
.objectid
+ cache
->key
.offset
;
2894 btrfs_put_block_group(cache
);
2899 err
= btrfs_run_delayed_refs(trans
, root
,
2904 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
2906 if (cache
->disk_cache_state
== BTRFS_DC_CLEAR
) {
2907 btrfs_put_block_group(cache
);
2913 cache
= next_block_group(root
, cache
);
2922 if (cache
->disk_cache_state
== BTRFS_DC_SETUP
)
2923 cache
->disk_cache_state
= BTRFS_DC_NEED_WRITE
;
2925 last
= cache
->key
.objectid
+ cache
->key
.offset
;
2927 err
= write_one_cache_group(trans
, root
, path
, cache
);
2929 btrfs_put_block_group(cache
);
2934 * I don't think this is needed since we're just marking our
2935 * preallocated extent as written, but just in case it can't
2939 err
= btrfs_run_delayed_refs(trans
, root
,
2944 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
2947 * Really this shouldn't happen, but it could if we
2948 * couldn't write the entire preallocated extent and
2949 * splitting the extent resulted in a new block.
2952 btrfs_put_block_group(cache
);
2955 if (cache
->disk_cache_state
== BTRFS_DC_NEED_WRITE
)
2957 cache
= next_block_group(root
, cache
);
2966 btrfs_write_out_cache(root
, trans
, cache
, path
);
2969 * If we didn't have an error then the cache state is still
2970 * NEED_WRITE, so we can set it to WRITTEN.
2972 if (cache
->disk_cache_state
== BTRFS_DC_NEED_WRITE
)
2973 cache
->disk_cache_state
= BTRFS_DC_WRITTEN
;
2974 last
= cache
->key
.objectid
+ cache
->key
.offset
;
2975 btrfs_put_block_group(cache
);
2978 btrfs_free_path(path
);
2982 int btrfs_extent_readonly(struct btrfs_root
*root
, u64 bytenr
)
2984 struct btrfs_block_group_cache
*block_group
;
2987 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
2988 if (!block_group
|| block_group
->ro
)
2991 btrfs_put_block_group(block_group
);
2995 static int update_space_info(struct btrfs_fs_info
*info
, u64 flags
,
2996 u64 total_bytes
, u64 bytes_used
,
2997 struct btrfs_space_info
**space_info
)
2999 struct btrfs_space_info
*found
;
3003 if (flags
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
3004 BTRFS_BLOCK_GROUP_RAID10
))
3009 found
= __find_space_info(info
, flags
);
3011 spin_lock(&found
->lock
);
3012 found
->total_bytes
+= total_bytes
;
3013 found
->disk_total
+= total_bytes
* factor
;
3014 found
->bytes_used
+= bytes_used
;
3015 found
->disk_used
+= bytes_used
* factor
;
3017 spin_unlock(&found
->lock
);
3018 *space_info
= found
;
3021 found
= kzalloc(sizeof(*found
), GFP_NOFS
);
3025 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++)
3026 INIT_LIST_HEAD(&found
->block_groups
[i
]);
3027 init_rwsem(&found
->groups_sem
);
3028 spin_lock_init(&found
->lock
);
3029 found
->flags
= flags
& (BTRFS_BLOCK_GROUP_DATA
|
3030 BTRFS_BLOCK_GROUP_SYSTEM
|
3031 BTRFS_BLOCK_GROUP_METADATA
);
3032 found
->total_bytes
= total_bytes
;
3033 found
->disk_total
= total_bytes
* factor
;
3034 found
->bytes_used
= bytes_used
;
3035 found
->disk_used
= bytes_used
* factor
;
3036 found
->bytes_pinned
= 0;
3037 found
->bytes_reserved
= 0;
3038 found
->bytes_readonly
= 0;
3039 found
->bytes_may_use
= 0;
3041 found
->force_alloc
= CHUNK_ALLOC_NO_FORCE
;
3042 found
->chunk_alloc
= 0;
3043 *space_info
= found
;
3044 list_add_rcu(&found
->list
, &info
->space_info
);
3045 atomic_set(&found
->caching_threads
, 0);
3049 static void set_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
3051 u64 extra_flags
= flags
& (BTRFS_BLOCK_GROUP_RAID0
|
3052 BTRFS_BLOCK_GROUP_RAID1
|
3053 BTRFS_BLOCK_GROUP_RAID10
|
3054 BTRFS_BLOCK_GROUP_DUP
);
3056 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3057 fs_info
->avail_data_alloc_bits
|= extra_flags
;
3058 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
3059 fs_info
->avail_metadata_alloc_bits
|= extra_flags
;
3060 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
3061 fs_info
->avail_system_alloc_bits
|= extra_flags
;
3065 u64
btrfs_reduce_alloc_profile(struct btrfs_root
*root
, u64 flags
)
3068 * we add in the count of missing devices because we want
3069 * to make sure that any RAID levels on a degraded FS
3070 * continue to be honored.
3072 u64 num_devices
= root
->fs_info
->fs_devices
->rw_devices
+
3073 root
->fs_info
->fs_devices
->missing_devices
;
3075 if (num_devices
== 1)
3076 flags
&= ~(BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID0
);
3077 if (num_devices
< 4)
3078 flags
&= ~BTRFS_BLOCK_GROUP_RAID10
;
3080 if ((flags
& BTRFS_BLOCK_GROUP_DUP
) &&
3081 (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
3082 BTRFS_BLOCK_GROUP_RAID10
))) {
3083 flags
&= ~BTRFS_BLOCK_GROUP_DUP
;
3086 if ((flags
& BTRFS_BLOCK_GROUP_RAID1
) &&
3087 (flags
& BTRFS_BLOCK_GROUP_RAID10
)) {
3088 flags
&= ~BTRFS_BLOCK_GROUP_RAID1
;
3091 if ((flags
& BTRFS_BLOCK_GROUP_RAID0
) &&
3092 ((flags
& BTRFS_BLOCK_GROUP_RAID1
) |
3093 (flags
& BTRFS_BLOCK_GROUP_RAID10
) |
3094 (flags
& BTRFS_BLOCK_GROUP_DUP
)))
3095 flags
&= ~BTRFS_BLOCK_GROUP_RAID0
;
3099 static u64
get_alloc_profile(struct btrfs_root
*root
, u64 flags
)
3101 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3102 flags
|= root
->fs_info
->avail_data_alloc_bits
&
3103 root
->fs_info
->data_alloc_profile
;
3104 else if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
3105 flags
|= root
->fs_info
->avail_system_alloc_bits
&
3106 root
->fs_info
->system_alloc_profile
;
3107 else if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
3108 flags
|= root
->fs_info
->avail_metadata_alloc_bits
&
3109 root
->fs_info
->metadata_alloc_profile
;
3110 return btrfs_reduce_alloc_profile(root
, flags
);
3113 u64
btrfs_get_alloc_profile(struct btrfs_root
*root
, int data
)
3118 flags
= BTRFS_BLOCK_GROUP_DATA
;
3119 else if (root
== root
->fs_info
->chunk_root
)
3120 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
3122 flags
= BTRFS_BLOCK_GROUP_METADATA
;
3124 return get_alloc_profile(root
, flags
);
3127 void btrfs_set_inode_space_info(struct btrfs_root
*root
, struct inode
*inode
)
3129 BTRFS_I(inode
)->space_info
= __find_space_info(root
->fs_info
,
3130 BTRFS_BLOCK_GROUP_DATA
);
3134 * This will check the space that the inode allocates from to make sure we have
3135 * enough space for bytes.
3137 int btrfs_check_data_free_space(struct inode
*inode
, u64 bytes
)
3139 struct btrfs_space_info
*data_sinfo
;
3140 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3142 int ret
= 0, committed
= 0, alloc_chunk
= 1;
3144 /* make sure bytes are sectorsize aligned */
3145 bytes
= (bytes
+ root
->sectorsize
- 1) & ~((u64
)root
->sectorsize
- 1);
3147 if (root
== root
->fs_info
->tree_root
) {
3152 data_sinfo
= BTRFS_I(inode
)->space_info
;
3157 /* make sure we have enough space to handle the data first */
3158 spin_lock(&data_sinfo
->lock
);
3159 used
= data_sinfo
->bytes_used
+ data_sinfo
->bytes_reserved
+
3160 data_sinfo
->bytes_pinned
+ data_sinfo
->bytes_readonly
+
3161 data_sinfo
->bytes_may_use
;
3163 if (used
+ bytes
> data_sinfo
->total_bytes
) {
3164 struct btrfs_trans_handle
*trans
;
3167 * if we don't have enough free bytes in this space then we need
3168 * to alloc a new chunk.
3170 if (!data_sinfo
->full
&& alloc_chunk
) {
3173 data_sinfo
->force_alloc
= CHUNK_ALLOC_FORCE
;
3174 spin_unlock(&data_sinfo
->lock
);
3176 alloc_target
= btrfs_get_alloc_profile(root
, 1);
3177 trans
= btrfs_join_transaction(root
);
3179 return PTR_ERR(trans
);
3181 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
3182 bytes
+ 2 * 1024 * 1024,
3184 CHUNK_ALLOC_NO_FORCE
);
3185 btrfs_end_transaction(trans
, root
);
3194 btrfs_set_inode_space_info(root
, inode
);
3195 data_sinfo
= BTRFS_I(inode
)->space_info
;
3199 spin_unlock(&data_sinfo
->lock
);
3201 /* commit the current transaction and try again */
3204 !atomic_read(&root
->fs_info
->open_ioctl_trans
)) {
3206 trans
= btrfs_join_transaction(root
);
3208 return PTR_ERR(trans
);
3209 ret
= btrfs_commit_transaction(trans
, root
);
3215 #if 0 /* I hope we never need this code again, just in case */
3216 printk(KERN_ERR
"no space left, need %llu, %llu bytes_used, "
3217 "%llu bytes_reserved, " "%llu bytes_pinned, "
3218 "%llu bytes_readonly, %llu may use %llu total\n",
3219 (unsigned long long)bytes
,
3220 (unsigned long long)data_sinfo
->bytes_used
,
3221 (unsigned long long)data_sinfo
->bytes_reserved
,
3222 (unsigned long long)data_sinfo
->bytes_pinned
,
3223 (unsigned long long)data_sinfo
->bytes_readonly
,
3224 (unsigned long long)data_sinfo
->bytes_may_use
,
3225 (unsigned long long)data_sinfo
->total_bytes
);
3229 data_sinfo
->bytes_may_use
+= bytes
;
3230 BTRFS_I(inode
)->reserved_bytes
+= bytes
;
3231 spin_unlock(&data_sinfo
->lock
);
3237 * called when we are clearing an delalloc extent from the
3238 * inode's io_tree or there was an error for whatever reason
3239 * after calling btrfs_check_data_free_space
3241 void btrfs_free_reserved_data_space(struct inode
*inode
, u64 bytes
)
3243 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3244 struct btrfs_space_info
*data_sinfo
;
3246 /* make sure bytes are sectorsize aligned */
3247 bytes
= (bytes
+ root
->sectorsize
- 1) & ~((u64
)root
->sectorsize
- 1);
3249 data_sinfo
= BTRFS_I(inode
)->space_info
;
3250 spin_lock(&data_sinfo
->lock
);
3251 data_sinfo
->bytes_may_use
-= bytes
;
3252 BTRFS_I(inode
)->reserved_bytes
-= bytes
;
3253 spin_unlock(&data_sinfo
->lock
);
3256 static void force_metadata_allocation(struct btrfs_fs_info
*info
)
3258 struct list_head
*head
= &info
->space_info
;
3259 struct btrfs_space_info
*found
;
3262 list_for_each_entry_rcu(found
, head
, list
) {
3263 if (found
->flags
& BTRFS_BLOCK_GROUP_METADATA
)
3264 found
->force_alloc
= CHUNK_ALLOC_FORCE
;
3269 static int should_alloc_chunk(struct btrfs_root
*root
,
3270 struct btrfs_space_info
*sinfo
, u64 alloc_bytes
,
3273 u64 num_bytes
= sinfo
->total_bytes
- sinfo
->bytes_readonly
;
3274 u64 num_allocated
= sinfo
->bytes_used
+ sinfo
->bytes_reserved
;
3277 if (force
== CHUNK_ALLOC_FORCE
)
3281 * in limited mode, we want to have some free space up to
3282 * about 1% of the FS size.
3284 if (force
== CHUNK_ALLOC_LIMITED
) {
3285 thresh
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
3286 thresh
= max_t(u64
, 64 * 1024 * 1024,
3287 div_factor_fine(thresh
, 1));
3289 if (num_bytes
- num_allocated
< thresh
)
3294 * we have two similar checks here, one based on percentage
3295 * and once based on a hard number of 256MB. The idea
3296 * is that if we have a good amount of free
3297 * room, don't allocate a chunk. A good mount is
3298 * less than 80% utilized of the chunks we have allocated,
3299 * or more than 256MB free
3301 if (num_allocated
+ alloc_bytes
+ 256 * 1024 * 1024 < num_bytes
)
3304 if (num_allocated
+ alloc_bytes
< div_factor(num_bytes
, 8))
3307 thresh
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
3309 /* 256MB or 5% of the FS */
3310 thresh
= max_t(u64
, 256 * 1024 * 1024, div_factor_fine(thresh
, 5));
3312 if (num_bytes
> thresh
&& sinfo
->bytes_used
< div_factor(num_bytes
, 3))
3317 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
3318 struct btrfs_root
*extent_root
, u64 alloc_bytes
,
3319 u64 flags
, int force
)
3321 struct btrfs_space_info
*space_info
;
3322 struct btrfs_fs_info
*fs_info
= extent_root
->fs_info
;
3323 int wait_for_alloc
= 0;
3326 flags
= btrfs_reduce_alloc_profile(extent_root
, flags
);
3328 space_info
= __find_space_info(extent_root
->fs_info
, flags
);
3330 ret
= update_space_info(extent_root
->fs_info
, flags
,
3334 BUG_ON(!space_info
);
3337 spin_lock(&space_info
->lock
);
3338 if (space_info
->force_alloc
)
3339 force
= space_info
->force_alloc
;
3340 if (space_info
->full
) {
3341 spin_unlock(&space_info
->lock
);
3345 if (!should_alloc_chunk(extent_root
, space_info
, alloc_bytes
, force
)) {
3346 spin_unlock(&space_info
->lock
);
3348 } else if (space_info
->chunk_alloc
) {
3351 space_info
->chunk_alloc
= 1;
3354 spin_unlock(&space_info
->lock
);
3356 mutex_lock(&fs_info
->chunk_mutex
);
3359 * The chunk_mutex is held throughout the entirety of a chunk
3360 * allocation, so once we've acquired the chunk_mutex we know that the
3361 * other guy is done and we need to recheck and see if we should
3364 if (wait_for_alloc
) {
3365 mutex_unlock(&fs_info
->chunk_mutex
);
3371 * If we have mixed data/metadata chunks we want to make sure we keep
3372 * allocating mixed chunks instead of individual chunks.
3374 if (btrfs_mixed_space_info(space_info
))
3375 flags
|= (BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
);
3378 * if we're doing a data chunk, go ahead and make sure that
3379 * we keep a reasonable number of metadata chunks allocated in the
3382 if (flags
& BTRFS_BLOCK_GROUP_DATA
&& fs_info
->metadata_ratio
) {
3383 fs_info
->data_chunk_allocations
++;
3384 if (!(fs_info
->data_chunk_allocations
%
3385 fs_info
->metadata_ratio
))
3386 force_metadata_allocation(fs_info
);
3389 ret
= btrfs_alloc_chunk(trans
, extent_root
, flags
);
3390 spin_lock(&space_info
->lock
);
3392 space_info
->full
= 1;
3396 space_info
->force_alloc
= CHUNK_ALLOC_NO_FORCE
;
3397 space_info
->chunk_alloc
= 0;
3398 spin_unlock(&space_info
->lock
);
3399 mutex_unlock(&extent_root
->fs_info
->chunk_mutex
);
3404 * shrink metadata reservation for delalloc
3406 static int shrink_delalloc(struct btrfs_trans_handle
*trans
,
3407 struct btrfs_root
*root
, u64 to_reclaim
, int sync
)
3409 struct btrfs_block_rsv
*block_rsv
;
3410 struct btrfs_space_info
*space_info
;
3415 int nr_pages
= (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT
;
3417 unsigned long progress
;
3419 block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
3420 space_info
= block_rsv
->space_info
;
3423 reserved
= space_info
->bytes_reserved
;
3424 progress
= space_info
->reservation_progress
;
3429 max_reclaim
= min(reserved
, to_reclaim
);
3431 while (loops
< 1024) {
3432 /* have the flusher threads jump in and do some IO */
3434 nr_pages
= min_t(unsigned long, nr_pages
,
3435 root
->fs_info
->delalloc_bytes
>> PAGE_CACHE_SHIFT
);
3436 writeback_inodes_sb_nr_if_idle(root
->fs_info
->sb
, nr_pages
);
3438 spin_lock(&space_info
->lock
);
3439 if (reserved
> space_info
->bytes_reserved
)
3440 reclaimed
+= reserved
- space_info
->bytes_reserved
;
3441 reserved
= space_info
->bytes_reserved
;
3442 spin_unlock(&space_info
->lock
);
3446 if (reserved
== 0 || reclaimed
>= max_reclaim
)
3449 if (trans
&& trans
->transaction
->blocked
)
3452 time_left
= schedule_timeout_interruptible(1);
3454 /* We were interrupted, exit */
3458 /* we've kicked the IO a few times, if anything has been freed,
3459 * exit. There is no sense in looping here for a long time
3460 * when we really need to commit the transaction, or there are
3461 * just too many writers without enough free space
3466 if (progress
!= space_info
->reservation_progress
)
3471 return reclaimed
>= to_reclaim
;
3475 * Retries tells us how many times we've called reserve_metadata_bytes. The
3476 * idea is if this is the first call (retries == 0) then we will add to our
3477 * reserved count if we can't make the allocation in order to hold our place
3478 * while we go and try and free up space. That way for retries > 1 we don't try
3479 * and add space, we just check to see if the amount of unused space is >= the
3480 * total space, meaning that our reservation is valid.
3482 * However if we don't intend to retry this reservation, pass -1 as retries so
3483 * that it short circuits this logic.
3485 static int reserve_metadata_bytes(struct btrfs_trans_handle
*trans
,
3486 struct btrfs_root
*root
,
3487 struct btrfs_block_rsv
*block_rsv
,
3488 u64 orig_bytes
, int flush
)
3490 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
3492 u64 num_bytes
= orig_bytes
;
3495 bool reserved
= false;
3496 bool committed
= false;
3503 spin_lock(&space_info
->lock
);
3504 unused
= space_info
->bytes_used
+ space_info
->bytes_reserved
+
3505 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
3506 space_info
->bytes_may_use
;
3509 * The idea here is that we've not already over-reserved the block group
3510 * then we can go ahead and save our reservation first and then start
3511 * flushing if we need to. Otherwise if we've already overcommitted
3512 * lets start flushing stuff first and then come back and try to make
3515 if (unused
<= space_info
->total_bytes
) {
3516 unused
= space_info
->total_bytes
- unused
;
3517 if (unused
>= num_bytes
) {
3519 space_info
->bytes_reserved
+= orig_bytes
;
3523 * Ok set num_bytes to orig_bytes since we aren't
3524 * overocmmitted, this way we only try and reclaim what
3527 num_bytes
= orig_bytes
;
3531 * Ok we're over committed, set num_bytes to the overcommitted
3532 * amount plus the amount of bytes that we need for this
3535 num_bytes
= unused
- space_info
->total_bytes
+
3536 (orig_bytes
* (retries
+ 1));
3540 * Couldn't make our reservation, save our place so while we're trying
3541 * to reclaim space we can actually use it instead of somebody else
3542 * stealing it from us.
3544 if (ret
&& !reserved
) {
3545 space_info
->bytes_reserved
+= orig_bytes
;
3549 spin_unlock(&space_info
->lock
);
3558 * We do synchronous shrinking since we don't actually unreserve
3559 * metadata until after the IO is completed.
3561 ret
= shrink_delalloc(trans
, root
, num_bytes
, 1);
3568 * So if we were overcommitted it's possible that somebody else flushed
3569 * out enough space and we simply didn't have enough space to reclaim,
3570 * so go back around and try again.
3577 spin_lock(&space_info
->lock
);
3579 * Not enough space to be reclaimed, don't bother committing the
3582 if (space_info
->bytes_pinned
< orig_bytes
)
3584 spin_unlock(&space_info
->lock
);
3589 if (trans
|| committed
)
3593 trans
= btrfs_join_transaction(root
);
3596 ret
= btrfs_commit_transaction(trans
, root
);
3605 spin_lock(&space_info
->lock
);
3606 space_info
->bytes_reserved
-= orig_bytes
;
3607 spin_unlock(&space_info
->lock
);
3613 static struct btrfs_block_rsv
*get_block_rsv(struct btrfs_trans_handle
*trans
,
3614 struct btrfs_root
*root
)
3616 struct btrfs_block_rsv
*block_rsv
;
3618 block_rsv
= trans
->block_rsv
;
3620 block_rsv
= root
->block_rsv
;
3623 block_rsv
= &root
->fs_info
->empty_block_rsv
;
3628 static int block_rsv_use_bytes(struct btrfs_block_rsv
*block_rsv
,
3632 spin_lock(&block_rsv
->lock
);
3633 if (block_rsv
->reserved
>= num_bytes
) {
3634 block_rsv
->reserved
-= num_bytes
;
3635 if (block_rsv
->reserved
< block_rsv
->size
)
3636 block_rsv
->full
= 0;
3639 spin_unlock(&block_rsv
->lock
);
3643 static void block_rsv_add_bytes(struct btrfs_block_rsv
*block_rsv
,
3644 u64 num_bytes
, int update_size
)
3646 spin_lock(&block_rsv
->lock
);
3647 block_rsv
->reserved
+= num_bytes
;
3649 block_rsv
->size
+= num_bytes
;
3650 else if (block_rsv
->reserved
>= block_rsv
->size
)
3651 block_rsv
->full
= 1;
3652 spin_unlock(&block_rsv
->lock
);
3655 void block_rsv_release_bytes(struct btrfs_block_rsv
*block_rsv
,
3656 struct btrfs_block_rsv
*dest
, u64 num_bytes
)
3658 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
3660 spin_lock(&block_rsv
->lock
);
3661 if (num_bytes
== (u64
)-1)
3662 num_bytes
= block_rsv
->size
;
3663 block_rsv
->size
-= num_bytes
;
3664 if (block_rsv
->reserved
>= block_rsv
->size
) {
3665 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
3666 block_rsv
->reserved
= block_rsv
->size
;
3667 block_rsv
->full
= 1;
3671 spin_unlock(&block_rsv
->lock
);
3673 if (num_bytes
> 0) {
3675 spin_lock(&dest
->lock
);
3679 bytes_to_add
= dest
->size
- dest
->reserved
;
3680 bytes_to_add
= min(num_bytes
, bytes_to_add
);
3681 dest
->reserved
+= bytes_to_add
;
3682 if (dest
->reserved
>= dest
->size
)
3684 num_bytes
-= bytes_to_add
;
3686 spin_unlock(&dest
->lock
);
3689 spin_lock(&space_info
->lock
);
3690 space_info
->bytes_reserved
-= num_bytes
;
3691 space_info
->reservation_progress
++;
3692 spin_unlock(&space_info
->lock
);
3697 static int block_rsv_migrate_bytes(struct btrfs_block_rsv
*src
,
3698 struct btrfs_block_rsv
*dst
, u64 num_bytes
)
3702 ret
= block_rsv_use_bytes(src
, num_bytes
);
3706 block_rsv_add_bytes(dst
, num_bytes
, 1);
3710 void btrfs_init_block_rsv(struct btrfs_block_rsv
*rsv
)
3712 memset(rsv
, 0, sizeof(*rsv
));
3713 spin_lock_init(&rsv
->lock
);
3714 atomic_set(&rsv
->usage
, 1);
3716 INIT_LIST_HEAD(&rsv
->list
);
3719 struct btrfs_block_rsv
*btrfs_alloc_block_rsv(struct btrfs_root
*root
)
3721 struct btrfs_block_rsv
*block_rsv
;
3722 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3724 block_rsv
= kmalloc(sizeof(*block_rsv
), GFP_NOFS
);
3728 btrfs_init_block_rsv(block_rsv
);
3729 block_rsv
->space_info
= __find_space_info(fs_info
,
3730 BTRFS_BLOCK_GROUP_METADATA
);
3734 void btrfs_free_block_rsv(struct btrfs_root
*root
,
3735 struct btrfs_block_rsv
*rsv
)
3737 if (rsv
&& atomic_dec_and_test(&rsv
->usage
)) {
3738 btrfs_block_rsv_release(root
, rsv
, (u64
)-1);
3745 * make the block_rsv struct be able to capture freed space.
3746 * the captured space will re-add to the the block_rsv struct
3747 * after transaction commit
3749 void btrfs_add_durable_block_rsv(struct btrfs_fs_info
*fs_info
,
3750 struct btrfs_block_rsv
*block_rsv
)
3752 block_rsv
->durable
= 1;
3753 mutex_lock(&fs_info
->durable_block_rsv_mutex
);
3754 list_add_tail(&block_rsv
->list
, &fs_info
->durable_block_rsv_list
);
3755 mutex_unlock(&fs_info
->durable_block_rsv_mutex
);
3758 int btrfs_block_rsv_add(struct btrfs_trans_handle
*trans
,
3759 struct btrfs_root
*root
,
3760 struct btrfs_block_rsv
*block_rsv
,
3768 ret
= reserve_metadata_bytes(trans
, root
, block_rsv
, num_bytes
, 1);
3770 block_rsv_add_bytes(block_rsv
, num_bytes
, 1);
3777 int btrfs_block_rsv_check(struct btrfs_trans_handle
*trans
,
3778 struct btrfs_root
*root
,
3779 struct btrfs_block_rsv
*block_rsv
,
3780 u64 min_reserved
, int min_factor
)
3783 int commit_trans
= 0;
3789 spin_lock(&block_rsv
->lock
);
3791 num_bytes
= div_factor(block_rsv
->size
, min_factor
);
3792 if (min_reserved
> num_bytes
)
3793 num_bytes
= min_reserved
;
3795 if (block_rsv
->reserved
>= num_bytes
) {
3798 num_bytes
-= block_rsv
->reserved
;
3799 if (block_rsv
->durable
&&
3800 block_rsv
->freed
[0] + block_rsv
->freed
[1] >= num_bytes
)
3803 spin_unlock(&block_rsv
->lock
);
3807 if (block_rsv
->refill_used
) {
3808 ret
= reserve_metadata_bytes(trans
, root
, block_rsv
,
3811 block_rsv_add_bytes(block_rsv
, num_bytes
, 0);
3820 trans
= btrfs_join_transaction(root
);
3821 BUG_ON(IS_ERR(trans
));
3822 ret
= btrfs_commit_transaction(trans
, root
);
3829 int btrfs_block_rsv_migrate(struct btrfs_block_rsv
*src_rsv
,
3830 struct btrfs_block_rsv
*dst_rsv
,
3833 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
3836 void btrfs_block_rsv_release(struct btrfs_root
*root
,
3837 struct btrfs_block_rsv
*block_rsv
,
3840 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
3841 if (global_rsv
->full
|| global_rsv
== block_rsv
||
3842 block_rsv
->space_info
!= global_rsv
->space_info
)
3844 block_rsv_release_bytes(block_rsv
, global_rsv
, num_bytes
);
3848 * helper to calculate size of global block reservation.
3849 * the desired value is sum of space used by extent tree,
3850 * checksum tree and root tree
3852 static u64
calc_global_metadata_size(struct btrfs_fs_info
*fs_info
)
3854 struct btrfs_space_info
*sinfo
;
3858 int csum_size
= btrfs_super_csum_size(&fs_info
->super_copy
);
3861 * per tree used space accounting can be inaccuracy, so we
3864 spin_lock(&fs_info
->extent_root
->accounting_lock
);
3865 num_bytes
= btrfs_root_used(&fs_info
->extent_root
->root_item
);
3866 spin_unlock(&fs_info
->extent_root
->accounting_lock
);
3868 spin_lock(&fs_info
->csum_root
->accounting_lock
);
3869 num_bytes
+= btrfs_root_used(&fs_info
->csum_root
->root_item
);
3870 spin_unlock(&fs_info
->csum_root
->accounting_lock
);
3872 spin_lock(&fs_info
->tree_root
->accounting_lock
);
3873 num_bytes
+= btrfs_root_used(&fs_info
->tree_root
->root_item
);
3874 spin_unlock(&fs_info
->tree_root
->accounting_lock
);
3876 sinfo
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_DATA
);
3877 spin_lock(&sinfo
->lock
);
3878 data_used
= sinfo
->bytes_used
;
3879 spin_unlock(&sinfo
->lock
);
3881 sinfo
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
3882 spin_lock(&sinfo
->lock
);
3883 if (sinfo
->flags
& BTRFS_BLOCK_GROUP_DATA
)
3885 meta_used
= sinfo
->bytes_used
;
3886 spin_unlock(&sinfo
->lock
);
3888 num_bytes
= (data_used
>> fs_info
->sb
->s_blocksize_bits
) *
3890 num_bytes
+= div64_u64(data_used
+ meta_used
, 50);
3892 if (num_bytes
* 3 > meta_used
)
3893 num_bytes
= div64_u64(meta_used
, 3);
3895 return ALIGN(num_bytes
, fs_info
->extent_root
->leafsize
<< 10);
3898 static void update_global_block_rsv(struct btrfs_fs_info
*fs_info
)
3900 struct btrfs_block_rsv
*block_rsv
= &fs_info
->global_block_rsv
;
3901 struct btrfs_space_info
*sinfo
= block_rsv
->space_info
;
3904 num_bytes
= calc_global_metadata_size(fs_info
);
3906 spin_lock(&block_rsv
->lock
);
3907 spin_lock(&sinfo
->lock
);
3909 block_rsv
->size
= num_bytes
;
3911 num_bytes
= sinfo
->bytes_used
+ sinfo
->bytes_pinned
+
3912 sinfo
->bytes_reserved
+ sinfo
->bytes_readonly
+
3913 sinfo
->bytes_may_use
;
3915 if (sinfo
->total_bytes
> num_bytes
) {
3916 num_bytes
= sinfo
->total_bytes
- num_bytes
;
3917 block_rsv
->reserved
+= num_bytes
;
3918 sinfo
->bytes_reserved
+= num_bytes
;
3921 if (block_rsv
->reserved
>= block_rsv
->size
) {
3922 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
3923 sinfo
->bytes_reserved
-= num_bytes
;
3924 sinfo
->reservation_progress
++;
3925 block_rsv
->reserved
= block_rsv
->size
;
3926 block_rsv
->full
= 1;
3929 printk(KERN_INFO
"global block rsv size %llu reserved %llu\n",
3930 block_rsv
->size
, block_rsv
->reserved
);
3932 spin_unlock(&sinfo
->lock
);
3933 spin_unlock(&block_rsv
->lock
);
3936 static void init_global_block_rsv(struct btrfs_fs_info
*fs_info
)
3938 struct btrfs_space_info
*space_info
;
3940 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_SYSTEM
);
3941 fs_info
->chunk_block_rsv
.space_info
= space_info
;
3942 fs_info
->chunk_block_rsv
.priority
= 10;
3944 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
3945 fs_info
->global_block_rsv
.space_info
= space_info
;
3946 fs_info
->global_block_rsv
.priority
= 10;
3947 fs_info
->global_block_rsv
.refill_used
= 1;
3948 fs_info
->delalloc_block_rsv
.space_info
= space_info
;
3949 fs_info
->trans_block_rsv
.space_info
= space_info
;
3950 fs_info
->empty_block_rsv
.space_info
= space_info
;
3951 fs_info
->empty_block_rsv
.priority
= 10;
3953 fs_info
->extent_root
->block_rsv
= &fs_info
->global_block_rsv
;
3954 fs_info
->csum_root
->block_rsv
= &fs_info
->global_block_rsv
;
3955 fs_info
->dev_root
->block_rsv
= &fs_info
->global_block_rsv
;
3956 fs_info
->tree_root
->block_rsv
= &fs_info
->global_block_rsv
;
3957 fs_info
->chunk_root
->block_rsv
= &fs_info
->chunk_block_rsv
;
3959 btrfs_add_durable_block_rsv(fs_info
, &fs_info
->global_block_rsv
);
3961 btrfs_add_durable_block_rsv(fs_info
, &fs_info
->delalloc_block_rsv
);
3963 update_global_block_rsv(fs_info
);
3966 static void release_global_block_rsv(struct btrfs_fs_info
*fs_info
)
3968 block_rsv_release_bytes(&fs_info
->global_block_rsv
, NULL
, (u64
)-1);
3969 WARN_ON(fs_info
->delalloc_block_rsv
.size
> 0);
3970 WARN_ON(fs_info
->delalloc_block_rsv
.reserved
> 0);
3971 WARN_ON(fs_info
->trans_block_rsv
.size
> 0);
3972 WARN_ON(fs_info
->trans_block_rsv
.reserved
> 0);
3973 WARN_ON(fs_info
->chunk_block_rsv
.size
> 0);
3974 WARN_ON(fs_info
->chunk_block_rsv
.reserved
> 0);
3977 static u64
calc_trans_metadata_size(struct btrfs_root
*root
, int num_items
)
3979 return (root
->leafsize
+ root
->nodesize
* (BTRFS_MAX_LEVEL
- 1)) *
3983 int btrfs_truncate_reserve_metadata(struct btrfs_trans_handle
*trans
,
3984 struct btrfs_root
*root
,
3985 struct btrfs_block_rsv
*rsv
)
3987 struct btrfs_block_rsv
*trans_rsv
= &root
->fs_info
->trans_block_rsv
;
3992 * Truncate should be freeing data, but give us 2 items just in case it
3993 * needs to use some space. We may want to be smarter about this in the
3996 num_bytes
= calc_trans_metadata_size(root
, 2);
3998 /* We already have enough bytes, just return */
3999 if (rsv
->reserved
>= num_bytes
)
4002 num_bytes
-= rsv
->reserved
;
4005 * You should have reserved enough space before hand to do this, so this
4008 ret
= block_rsv_migrate_bytes(trans_rsv
, rsv
, num_bytes
);
4014 int btrfs_trans_reserve_metadata(struct btrfs_trans_handle
*trans
,
4015 struct btrfs_root
*root
,
4021 if (num_items
== 0 || root
->fs_info
->chunk_root
== root
)
4024 num_bytes
= calc_trans_metadata_size(root
, num_items
);
4025 ret
= btrfs_block_rsv_add(trans
, root
, &root
->fs_info
->trans_block_rsv
,
4028 trans
->bytes_reserved
+= num_bytes
;
4029 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4034 void btrfs_trans_release_metadata(struct btrfs_trans_handle
*trans
,
4035 struct btrfs_root
*root
)
4037 if (!trans
->bytes_reserved
)
4040 BUG_ON(trans
->block_rsv
!= &root
->fs_info
->trans_block_rsv
);
4041 btrfs_block_rsv_release(root
, trans
->block_rsv
,
4042 trans
->bytes_reserved
);
4043 trans
->bytes_reserved
= 0;
4046 int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle
*trans
,
4047 struct inode
*inode
)
4049 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4050 struct btrfs_block_rsv
*src_rsv
= get_block_rsv(trans
, root
);
4051 struct btrfs_block_rsv
*dst_rsv
= root
->orphan_block_rsv
;
4054 * We need to hold space in order to delete our orphan item once we've
4055 * added it, so this takes the reservation so we can release it later
4056 * when we are truly done with the orphan item.
4058 u64 num_bytes
= calc_trans_metadata_size(root
, 1);
4059 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
4062 void btrfs_orphan_release_metadata(struct inode
*inode
)
4064 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4065 u64 num_bytes
= calc_trans_metadata_size(root
, 1);
4066 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
, num_bytes
);
4069 int btrfs_snap_reserve_metadata(struct btrfs_trans_handle
*trans
,
4070 struct btrfs_pending_snapshot
*pending
)
4072 struct btrfs_root
*root
= pending
->root
;
4073 struct btrfs_block_rsv
*src_rsv
= get_block_rsv(trans
, root
);
4074 struct btrfs_block_rsv
*dst_rsv
= &pending
->block_rsv
;
4076 * two for root back/forward refs, two for directory entries
4077 * and one for root of the snapshot.
4079 u64 num_bytes
= calc_trans_metadata_size(root
, 5);
4080 dst_rsv
->space_info
= src_rsv
->space_info
;
4081 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
4084 static u64
calc_csum_metadata_size(struct inode
*inode
, u64 num_bytes
)
4086 return num_bytes
>>= 3;
4089 int btrfs_delalloc_reserve_metadata(struct inode
*inode
, u64 num_bytes
)
4091 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4092 struct btrfs_block_rsv
*block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
4095 int reserved_extents
;
4098 if (btrfs_transaction_in_commit(root
->fs_info
))
4099 schedule_timeout(1);
4101 num_bytes
= ALIGN(num_bytes
, root
->sectorsize
);
4103 nr_extents
= atomic_read(&BTRFS_I(inode
)->outstanding_extents
) + 1;
4104 reserved_extents
= atomic_read(&BTRFS_I(inode
)->reserved_extents
);
4106 if (nr_extents
> reserved_extents
) {
4107 nr_extents
-= reserved_extents
;
4108 to_reserve
= calc_trans_metadata_size(root
, nr_extents
);
4114 to_reserve
+= calc_csum_metadata_size(inode
, num_bytes
);
4115 ret
= reserve_metadata_bytes(NULL
, root
, block_rsv
, to_reserve
, 1);
4119 atomic_add(nr_extents
, &BTRFS_I(inode
)->reserved_extents
);
4120 atomic_inc(&BTRFS_I(inode
)->outstanding_extents
);
4122 block_rsv_add_bytes(block_rsv
, to_reserve
, 1);
4124 if (block_rsv
->size
> 512 * 1024 * 1024)
4125 shrink_delalloc(NULL
, root
, to_reserve
, 0);
4130 void btrfs_delalloc_release_metadata(struct inode
*inode
, u64 num_bytes
)
4132 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4135 int reserved_extents
;
4137 num_bytes
= ALIGN(num_bytes
, root
->sectorsize
);
4138 atomic_dec(&BTRFS_I(inode
)->outstanding_extents
);
4139 WARN_ON(atomic_read(&BTRFS_I(inode
)->outstanding_extents
) < 0);
4141 reserved_extents
= atomic_read(&BTRFS_I(inode
)->reserved_extents
);
4145 nr_extents
= atomic_read(&BTRFS_I(inode
)->outstanding_extents
);
4146 if (nr_extents
>= reserved_extents
) {
4150 old
= reserved_extents
;
4151 nr_extents
= reserved_extents
- nr_extents
;
4152 new = reserved_extents
- nr_extents
;
4153 old
= atomic_cmpxchg(&BTRFS_I(inode
)->reserved_extents
,
4154 reserved_extents
, new);
4155 if (likely(old
== reserved_extents
))
4157 reserved_extents
= old
;
4160 to_free
= calc_csum_metadata_size(inode
, num_bytes
);
4162 to_free
+= calc_trans_metadata_size(root
, nr_extents
);
4164 btrfs_block_rsv_release(root
, &root
->fs_info
->delalloc_block_rsv
,
4168 int btrfs_delalloc_reserve_space(struct inode
*inode
, u64 num_bytes
)
4172 ret
= btrfs_check_data_free_space(inode
, num_bytes
);
4176 ret
= btrfs_delalloc_reserve_metadata(inode
, num_bytes
);
4178 btrfs_free_reserved_data_space(inode
, num_bytes
);
4185 void btrfs_delalloc_release_space(struct inode
*inode
, u64 num_bytes
)
4187 btrfs_delalloc_release_metadata(inode
, num_bytes
);
4188 btrfs_free_reserved_data_space(inode
, num_bytes
);
4191 static int update_block_group(struct btrfs_trans_handle
*trans
,
4192 struct btrfs_root
*root
,
4193 u64 bytenr
, u64 num_bytes
, int alloc
)
4195 struct btrfs_block_group_cache
*cache
= NULL
;
4196 struct btrfs_fs_info
*info
= root
->fs_info
;
4197 u64 total
= num_bytes
;
4202 /* block accounting for super block */
4203 spin_lock(&info
->delalloc_lock
);
4204 old_val
= btrfs_super_bytes_used(&info
->super_copy
);
4206 old_val
+= num_bytes
;
4208 old_val
-= num_bytes
;
4209 btrfs_set_super_bytes_used(&info
->super_copy
, old_val
);
4210 spin_unlock(&info
->delalloc_lock
);
4213 cache
= btrfs_lookup_block_group(info
, bytenr
);
4216 if (cache
->flags
& (BTRFS_BLOCK_GROUP_DUP
|
4217 BTRFS_BLOCK_GROUP_RAID1
|
4218 BTRFS_BLOCK_GROUP_RAID10
))
4223 * If this block group has free space cache written out, we
4224 * need to make sure to load it if we are removing space. This
4225 * is because we need the unpinning stage to actually add the
4226 * space back to the block group, otherwise we will leak space.
4228 if (!alloc
&& cache
->cached
== BTRFS_CACHE_NO
)
4229 cache_block_group(cache
, trans
, NULL
, 1);
4231 byte_in_group
= bytenr
- cache
->key
.objectid
;
4232 WARN_ON(byte_in_group
> cache
->key
.offset
);
4234 spin_lock(&cache
->space_info
->lock
);
4235 spin_lock(&cache
->lock
);
4237 if (btrfs_super_cache_generation(&info
->super_copy
) != 0 &&
4238 cache
->disk_cache_state
< BTRFS_DC_CLEAR
)
4239 cache
->disk_cache_state
= BTRFS_DC_CLEAR
;
4242 old_val
= btrfs_block_group_used(&cache
->item
);
4243 num_bytes
= min(total
, cache
->key
.offset
- byte_in_group
);
4245 old_val
+= num_bytes
;
4246 btrfs_set_block_group_used(&cache
->item
, old_val
);
4247 cache
->reserved
-= num_bytes
;
4248 cache
->space_info
->bytes_reserved
-= num_bytes
;
4249 cache
->space_info
->reservation_progress
++;
4250 cache
->space_info
->bytes_used
+= num_bytes
;
4251 cache
->space_info
->disk_used
+= num_bytes
* factor
;
4252 spin_unlock(&cache
->lock
);
4253 spin_unlock(&cache
->space_info
->lock
);
4255 old_val
-= num_bytes
;
4256 btrfs_set_block_group_used(&cache
->item
, old_val
);
4257 cache
->pinned
+= num_bytes
;
4258 cache
->space_info
->bytes_pinned
+= num_bytes
;
4259 cache
->space_info
->bytes_used
-= num_bytes
;
4260 cache
->space_info
->disk_used
-= num_bytes
* factor
;
4261 spin_unlock(&cache
->lock
);
4262 spin_unlock(&cache
->space_info
->lock
);
4264 set_extent_dirty(info
->pinned_extents
,
4265 bytenr
, bytenr
+ num_bytes
- 1,
4266 GFP_NOFS
| __GFP_NOFAIL
);
4268 btrfs_put_block_group(cache
);
4270 bytenr
+= num_bytes
;
4275 static u64
first_logical_byte(struct btrfs_root
*root
, u64 search_start
)
4277 struct btrfs_block_group_cache
*cache
;
4280 cache
= btrfs_lookup_first_block_group(root
->fs_info
, search_start
);
4284 bytenr
= cache
->key
.objectid
;
4285 btrfs_put_block_group(cache
);
4290 static int pin_down_extent(struct btrfs_root
*root
,
4291 struct btrfs_block_group_cache
*cache
,
4292 u64 bytenr
, u64 num_bytes
, int reserved
)
4294 spin_lock(&cache
->space_info
->lock
);
4295 spin_lock(&cache
->lock
);
4296 cache
->pinned
+= num_bytes
;
4297 cache
->space_info
->bytes_pinned
+= num_bytes
;
4299 cache
->reserved
-= num_bytes
;
4300 cache
->space_info
->bytes_reserved
-= num_bytes
;
4301 cache
->space_info
->reservation_progress
++;
4303 spin_unlock(&cache
->lock
);
4304 spin_unlock(&cache
->space_info
->lock
);
4306 set_extent_dirty(root
->fs_info
->pinned_extents
, bytenr
,
4307 bytenr
+ num_bytes
- 1, GFP_NOFS
| __GFP_NOFAIL
);
4312 * this function must be called within transaction
4314 int btrfs_pin_extent(struct btrfs_root
*root
,
4315 u64 bytenr
, u64 num_bytes
, int reserved
)
4317 struct btrfs_block_group_cache
*cache
;
4319 cache
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
4322 pin_down_extent(root
, cache
, bytenr
, num_bytes
, reserved
);
4324 btrfs_put_block_group(cache
);
4329 * update size of reserved extents. this function may return -EAGAIN
4330 * if 'reserve' is true or 'sinfo' is false.
4332 int btrfs_update_reserved_bytes(struct btrfs_block_group_cache
*cache
,
4333 u64 num_bytes
, int reserve
, int sinfo
)
4337 struct btrfs_space_info
*space_info
= cache
->space_info
;
4338 spin_lock(&space_info
->lock
);
4339 spin_lock(&cache
->lock
);
4344 cache
->reserved
+= num_bytes
;
4345 space_info
->bytes_reserved
+= num_bytes
;
4349 space_info
->bytes_readonly
+= num_bytes
;
4350 cache
->reserved
-= num_bytes
;
4351 space_info
->bytes_reserved
-= num_bytes
;
4352 space_info
->reservation_progress
++;
4354 spin_unlock(&cache
->lock
);
4355 spin_unlock(&space_info
->lock
);
4357 spin_lock(&cache
->lock
);
4362 cache
->reserved
+= num_bytes
;
4364 cache
->reserved
-= num_bytes
;
4366 spin_unlock(&cache
->lock
);
4371 int btrfs_prepare_extent_commit(struct btrfs_trans_handle
*trans
,
4372 struct btrfs_root
*root
)
4374 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4375 struct btrfs_caching_control
*next
;
4376 struct btrfs_caching_control
*caching_ctl
;
4377 struct btrfs_block_group_cache
*cache
;
4379 down_write(&fs_info
->extent_commit_sem
);
4381 list_for_each_entry_safe(caching_ctl
, next
,
4382 &fs_info
->caching_block_groups
, list
) {
4383 cache
= caching_ctl
->block_group
;
4384 if (block_group_cache_done(cache
)) {
4385 cache
->last_byte_to_unpin
= (u64
)-1;
4386 list_del_init(&caching_ctl
->list
);
4387 put_caching_control(caching_ctl
);
4389 cache
->last_byte_to_unpin
= caching_ctl
->progress
;
4393 if (fs_info
->pinned_extents
== &fs_info
->freed_extents
[0])
4394 fs_info
->pinned_extents
= &fs_info
->freed_extents
[1];
4396 fs_info
->pinned_extents
= &fs_info
->freed_extents
[0];
4398 up_write(&fs_info
->extent_commit_sem
);
4400 update_global_block_rsv(fs_info
);
4404 static int unpin_extent_range(struct btrfs_root
*root
, u64 start
, u64 end
)
4406 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4407 struct btrfs_block_group_cache
*cache
= NULL
;
4410 while (start
<= end
) {
4412 start
>= cache
->key
.objectid
+ cache
->key
.offset
) {
4414 btrfs_put_block_group(cache
);
4415 cache
= btrfs_lookup_block_group(fs_info
, start
);
4419 len
= cache
->key
.objectid
+ cache
->key
.offset
- start
;
4420 len
= min(len
, end
+ 1 - start
);
4422 if (start
< cache
->last_byte_to_unpin
) {
4423 len
= min(len
, cache
->last_byte_to_unpin
- start
);
4424 btrfs_add_free_space(cache
, start
, len
);
4429 spin_lock(&cache
->space_info
->lock
);
4430 spin_lock(&cache
->lock
);
4431 cache
->pinned
-= len
;
4432 cache
->space_info
->bytes_pinned
-= len
;
4434 cache
->space_info
->bytes_readonly
+= len
;
4435 } else if (cache
->reserved_pinned
> 0) {
4436 len
= min(len
, cache
->reserved_pinned
);
4437 cache
->reserved_pinned
-= len
;
4438 cache
->space_info
->bytes_reserved
+= len
;
4440 spin_unlock(&cache
->lock
);
4441 spin_unlock(&cache
->space_info
->lock
);
4445 btrfs_put_block_group(cache
);
4449 int btrfs_finish_extent_commit(struct btrfs_trans_handle
*trans
,
4450 struct btrfs_root
*root
)
4452 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4453 struct extent_io_tree
*unpin
;
4454 struct btrfs_block_rsv
*block_rsv
;
4455 struct btrfs_block_rsv
*next_rsv
;
4461 if (fs_info
->pinned_extents
== &fs_info
->freed_extents
[0])
4462 unpin
= &fs_info
->freed_extents
[1];
4464 unpin
= &fs_info
->freed_extents
[0];
4467 ret
= find_first_extent_bit(unpin
, 0, &start
, &end
,
4472 if (btrfs_test_opt(root
, DISCARD
))
4473 ret
= btrfs_discard_extent(root
, start
,
4474 end
+ 1 - start
, NULL
);
4476 clear_extent_dirty(unpin
, start
, end
, GFP_NOFS
);
4477 unpin_extent_range(root
, start
, end
);
4481 mutex_lock(&fs_info
->durable_block_rsv_mutex
);
4482 list_for_each_entry_safe(block_rsv
, next_rsv
,
4483 &fs_info
->durable_block_rsv_list
, list
) {
4485 idx
= trans
->transid
& 0x1;
4486 if (block_rsv
->freed
[idx
] > 0) {
4487 block_rsv_add_bytes(block_rsv
,
4488 block_rsv
->freed
[idx
], 0);
4489 block_rsv
->freed
[idx
] = 0;
4491 if (atomic_read(&block_rsv
->usage
) == 0) {
4492 btrfs_block_rsv_release(root
, block_rsv
, (u64
)-1);
4494 if (block_rsv
->freed
[0] == 0 &&
4495 block_rsv
->freed
[1] == 0) {
4496 list_del_init(&block_rsv
->list
);
4500 btrfs_block_rsv_release(root
, block_rsv
, 0);
4503 mutex_unlock(&fs_info
->durable_block_rsv_mutex
);
4508 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
4509 struct btrfs_root
*root
,
4510 u64 bytenr
, u64 num_bytes
, u64 parent
,
4511 u64 root_objectid
, u64 owner_objectid
,
4512 u64 owner_offset
, int refs_to_drop
,
4513 struct btrfs_delayed_extent_op
*extent_op
)
4515 struct btrfs_key key
;
4516 struct btrfs_path
*path
;
4517 struct btrfs_fs_info
*info
= root
->fs_info
;
4518 struct btrfs_root
*extent_root
= info
->extent_root
;
4519 struct extent_buffer
*leaf
;
4520 struct btrfs_extent_item
*ei
;
4521 struct btrfs_extent_inline_ref
*iref
;
4524 int extent_slot
= 0;
4525 int found_extent
= 0;
4530 path
= btrfs_alloc_path();
4535 path
->leave_spinning
= 1;
4537 is_data
= owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
;
4538 BUG_ON(!is_data
&& refs_to_drop
!= 1);
4540 ret
= lookup_extent_backref(trans
, extent_root
, path
, &iref
,
4541 bytenr
, num_bytes
, parent
,
4542 root_objectid
, owner_objectid
,
4545 extent_slot
= path
->slots
[0];
4546 while (extent_slot
>= 0) {
4547 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
4549 if (key
.objectid
!= bytenr
)
4551 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
4552 key
.offset
== num_bytes
) {
4556 if (path
->slots
[0] - extent_slot
> 5)
4560 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4561 item_size
= btrfs_item_size_nr(path
->nodes
[0], extent_slot
);
4562 if (found_extent
&& item_size
< sizeof(*ei
))
4565 if (!found_extent
) {
4567 ret
= remove_extent_backref(trans
, extent_root
, path
,
4571 btrfs_release_path(extent_root
, path
);
4572 path
->leave_spinning
= 1;
4574 key
.objectid
= bytenr
;
4575 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
4576 key
.offset
= num_bytes
;
4578 ret
= btrfs_search_slot(trans
, extent_root
,
4581 printk(KERN_ERR
"umm, got %d back from search"
4582 ", was looking for %llu\n", ret
,
4583 (unsigned long long)bytenr
);
4584 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
4587 extent_slot
= path
->slots
[0];
4590 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
4592 printk(KERN_ERR
"btrfs unable to find ref byte nr %llu "
4593 "parent %llu root %llu owner %llu offset %llu\n",
4594 (unsigned long long)bytenr
,
4595 (unsigned long long)parent
,
4596 (unsigned long long)root_objectid
,
4597 (unsigned long long)owner_objectid
,
4598 (unsigned long long)owner_offset
);
4601 leaf
= path
->nodes
[0];
4602 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
4603 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4604 if (item_size
< sizeof(*ei
)) {
4605 BUG_ON(found_extent
|| extent_slot
!= path
->slots
[0]);
4606 ret
= convert_extent_item_v0(trans
, extent_root
, path
,
4610 btrfs_release_path(extent_root
, path
);
4611 path
->leave_spinning
= 1;
4613 key
.objectid
= bytenr
;
4614 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
4615 key
.offset
= num_bytes
;
4617 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
,
4620 printk(KERN_ERR
"umm, got %d back from search"
4621 ", was looking for %llu\n", ret
,
4622 (unsigned long long)bytenr
);
4623 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
4626 extent_slot
= path
->slots
[0];
4627 leaf
= path
->nodes
[0];
4628 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
4631 BUG_ON(item_size
< sizeof(*ei
));
4632 ei
= btrfs_item_ptr(leaf
, extent_slot
,
4633 struct btrfs_extent_item
);
4634 if (owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
) {
4635 struct btrfs_tree_block_info
*bi
;
4636 BUG_ON(item_size
< sizeof(*ei
) + sizeof(*bi
));
4637 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
4638 WARN_ON(owner_objectid
!= btrfs_tree_block_level(leaf
, bi
));
4641 refs
= btrfs_extent_refs(leaf
, ei
);
4642 BUG_ON(refs
< refs_to_drop
);
4643 refs
-= refs_to_drop
;
4647 __run_delayed_extent_op(extent_op
, leaf
, ei
);
4649 * In the case of inline back ref, reference count will
4650 * be updated by remove_extent_backref
4653 BUG_ON(!found_extent
);
4655 btrfs_set_extent_refs(leaf
, ei
, refs
);
4656 btrfs_mark_buffer_dirty(leaf
);
4659 ret
= remove_extent_backref(trans
, extent_root
, path
,
4666 BUG_ON(is_data
&& refs_to_drop
!=
4667 extent_data_ref_count(root
, path
, iref
));
4669 BUG_ON(path
->slots
[0] != extent_slot
);
4671 BUG_ON(path
->slots
[0] != extent_slot
+ 1);
4672 path
->slots
[0] = extent_slot
;
4677 ret
= btrfs_del_items(trans
, extent_root
, path
, path
->slots
[0],
4680 btrfs_release_path(extent_root
, path
);
4683 ret
= btrfs_del_csums(trans
, root
, bytenr
, num_bytes
);
4686 invalidate_mapping_pages(info
->btree_inode
->i_mapping
,
4687 bytenr
>> PAGE_CACHE_SHIFT
,
4688 (bytenr
+ num_bytes
- 1) >> PAGE_CACHE_SHIFT
);
4691 ret
= update_block_group(trans
, root
, bytenr
, num_bytes
, 0);
4694 btrfs_free_path(path
);
4699 * when we free an block, it is possible (and likely) that we free the last
4700 * delayed ref for that extent as well. This searches the delayed ref tree for
4701 * a given extent, and if there are no other delayed refs to be processed, it
4702 * removes it from the tree.
4704 static noinline
int check_ref_cleanup(struct btrfs_trans_handle
*trans
,
4705 struct btrfs_root
*root
, u64 bytenr
)
4707 struct btrfs_delayed_ref_head
*head
;
4708 struct btrfs_delayed_ref_root
*delayed_refs
;
4709 struct btrfs_delayed_ref_node
*ref
;
4710 struct rb_node
*node
;
4713 delayed_refs
= &trans
->transaction
->delayed_refs
;
4714 spin_lock(&delayed_refs
->lock
);
4715 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
4719 node
= rb_prev(&head
->node
.rb_node
);
4723 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
4725 /* there are still entries for this ref, we can't drop it */
4726 if (ref
->bytenr
== bytenr
)
4729 if (head
->extent_op
) {
4730 if (!head
->must_insert_reserved
)
4732 kfree(head
->extent_op
);
4733 head
->extent_op
= NULL
;
4737 * waiting for the lock here would deadlock. If someone else has it
4738 * locked they are already in the process of dropping it anyway
4740 if (!mutex_trylock(&head
->mutex
))
4744 * at this point we have a head with no other entries. Go
4745 * ahead and process it.
4747 head
->node
.in_tree
= 0;
4748 rb_erase(&head
->node
.rb_node
, &delayed_refs
->root
);
4750 delayed_refs
->num_entries
--;
4753 * we don't take a ref on the node because we're removing it from the
4754 * tree, so we just steal the ref the tree was holding.
4756 delayed_refs
->num_heads
--;
4757 if (list_empty(&head
->cluster
))
4758 delayed_refs
->num_heads_ready
--;
4760 list_del_init(&head
->cluster
);
4761 spin_unlock(&delayed_refs
->lock
);
4763 BUG_ON(head
->extent_op
);
4764 if (head
->must_insert_reserved
)
4767 mutex_unlock(&head
->mutex
);
4768 btrfs_put_delayed_ref(&head
->node
);
4771 spin_unlock(&delayed_refs
->lock
);
4775 void btrfs_free_tree_block(struct btrfs_trans_handle
*trans
,
4776 struct btrfs_root
*root
,
4777 struct extent_buffer
*buf
,
4778 u64 parent
, int last_ref
)
4780 struct btrfs_block_rsv
*block_rsv
;
4781 struct btrfs_block_group_cache
*cache
= NULL
;
4784 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
4785 ret
= btrfs_add_delayed_tree_ref(trans
, buf
->start
, buf
->len
,
4786 parent
, root
->root_key
.objectid
,
4787 btrfs_header_level(buf
),
4788 BTRFS_DROP_DELAYED_REF
, NULL
);
4795 block_rsv
= get_block_rsv(trans
, root
);
4796 cache
= btrfs_lookup_block_group(root
->fs_info
, buf
->start
);
4797 if (block_rsv
->space_info
!= cache
->space_info
)
4800 if (btrfs_header_generation(buf
) == trans
->transid
) {
4801 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
4802 ret
= check_ref_cleanup(trans
, root
, buf
->start
);
4807 if (btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
)) {
4808 pin_down_extent(root
, cache
, buf
->start
, buf
->len
, 1);
4812 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY
, &buf
->bflags
));
4814 btrfs_add_free_space(cache
, buf
->start
, buf
->len
);
4815 ret
= btrfs_update_reserved_bytes(cache
, buf
->len
, 0, 0);
4816 if (ret
== -EAGAIN
) {
4817 /* block group became read-only */
4818 btrfs_update_reserved_bytes(cache
, buf
->len
, 0, 1);
4823 spin_lock(&block_rsv
->lock
);
4824 if (block_rsv
->reserved
< block_rsv
->size
) {
4825 block_rsv
->reserved
+= buf
->len
;
4828 spin_unlock(&block_rsv
->lock
);
4831 spin_lock(&cache
->space_info
->lock
);
4832 cache
->space_info
->bytes_reserved
-= buf
->len
;
4833 cache
->space_info
->reservation_progress
++;
4834 spin_unlock(&cache
->space_info
->lock
);
4839 if (block_rsv
->durable
&& !cache
->ro
) {
4841 spin_lock(&cache
->lock
);
4843 cache
->reserved_pinned
+= buf
->len
;
4846 spin_unlock(&cache
->lock
);
4849 spin_lock(&block_rsv
->lock
);
4850 block_rsv
->freed
[trans
->transid
& 0x1] += buf
->len
;
4851 spin_unlock(&block_rsv
->lock
);
4856 * Deleting the buffer, clear the corrupt flag since it doesn't matter
4859 clear_bit(EXTENT_BUFFER_CORRUPT
, &buf
->bflags
);
4860 btrfs_put_block_group(cache
);
4863 int btrfs_free_extent(struct btrfs_trans_handle
*trans
,
4864 struct btrfs_root
*root
,
4865 u64 bytenr
, u64 num_bytes
, u64 parent
,
4866 u64 root_objectid
, u64 owner
, u64 offset
)
4871 * tree log blocks never actually go into the extent allocation
4872 * tree, just update pinning info and exit early.
4874 if (root_objectid
== BTRFS_TREE_LOG_OBJECTID
) {
4875 WARN_ON(owner
>= BTRFS_FIRST_FREE_OBJECTID
);
4876 /* unlocks the pinned mutex */
4877 btrfs_pin_extent(root
, bytenr
, num_bytes
, 1);
4879 } else if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
4880 ret
= btrfs_add_delayed_tree_ref(trans
, bytenr
, num_bytes
,
4881 parent
, root_objectid
, (int)owner
,
4882 BTRFS_DROP_DELAYED_REF
, NULL
);
4885 ret
= btrfs_add_delayed_data_ref(trans
, bytenr
, num_bytes
,
4886 parent
, root_objectid
, owner
,
4887 offset
, BTRFS_DROP_DELAYED_REF
, NULL
);
4893 static u64
stripe_align(struct btrfs_root
*root
, u64 val
)
4895 u64 mask
= ((u64
)root
->stripesize
- 1);
4896 u64 ret
= (val
+ mask
) & ~mask
;
4901 * when we wait for progress in the block group caching, its because
4902 * our allocation attempt failed at least once. So, we must sleep
4903 * and let some progress happen before we try again.
4905 * This function will sleep at least once waiting for new free space to
4906 * show up, and then it will check the block group free space numbers
4907 * for our min num_bytes. Another option is to have it go ahead
4908 * and look in the rbtree for a free extent of a given size, but this
4912 wait_block_group_cache_progress(struct btrfs_block_group_cache
*cache
,
4915 struct btrfs_caching_control
*caching_ctl
;
4918 caching_ctl
= get_caching_control(cache
);
4922 wait_event(caching_ctl
->wait
, block_group_cache_done(cache
) ||
4923 (cache
->free_space
>= num_bytes
));
4925 put_caching_control(caching_ctl
);
4930 wait_block_group_cache_done(struct btrfs_block_group_cache
*cache
)
4932 struct btrfs_caching_control
*caching_ctl
;
4935 caching_ctl
= get_caching_control(cache
);
4939 wait_event(caching_ctl
->wait
, block_group_cache_done(cache
));
4941 put_caching_control(caching_ctl
);
4945 static int get_block_group_index(struct btrfs_block_group_cache
*cache
)
4948 if (cache
->flags
& BTRFS_BLOCK_GROUP_RAID10
)
4950 else if (cache
->flags
& BTRFS_BLOCK_GROUP_RAID1
)
4952 else if (cache
->flags
& BTRFS_BLOCK_GROUP_DUP
)
4954 else if (cache
->flags
& BTRFS_BLOCK_GROUP_RAID0
)
4961 enum btrfs_loop_type
{
4962 LOOP_FIND_IDEAL
= 0,
4963 LOOP_CACHING_NOWAIT
= 1,
4964 LOOP_CACHING_WAIT
= 2,
4965 LOOP_ALLOC_CHUNK
= 3,
4966 LOOP_NO_EMPTY_SIZE
= 4,
4970 * walks the btree of allocated extents and find a hole of a given size.
4971 * The key ins is changed to record the hole:
4972 * ins->objectid == block start
4973 * ins->flags = BTRFS_EXTENT_ITEM_KEY
4974 * ins->offset == number of blocks
4975 * Any available blocks before search_start are skipped.
4977 static noinline
int find_free_extent(struct btrfs_trans_handle
*trans
,
4978 struct btrfs_root
*orig_root
,
4979 u64 num_bytes
, u64 empty_size
,
4980 u64 search_start
, u64 search_end
,
4981 u64 hint_byte
, struct btrfs_key
*ins
,
4985 struct btrfs_root
*root
= orig_root
->fs_info
->extent_root
;
4986 struct btrfs_free_cluster
*last_ptr
= NULL
;
4987 struct btrfs_block_group_cache
*block_group
= NULL
;
4988 int empty_cluster
= 2 * 1024 * 1024;
4989 int allowed_chunk_alloc
= 0;
4990 int done_chunk_alloc
= 0;
4991 struct btrfs_space_info
*space_info
;
4992 int last_ptr_loop
= 0;
4995 bool found_uncached_bg
= false;
4996 bool failed_cluster_refill
= false;
4997 bool failed_alloc
= false;
4998 bool use_cluster
= true;
4999 u64 ideal_cache_percent
= 0;
5000 u64 ideal_cache_offset
= 0;
5002 WARN_ON(num_bytes
< root
->sectorsize
);
5003 btrfs_set_key_type(ins
, BTRFS_EXTENT_ITEM_KEY
);
5007 space_info
= __find_space_info(root
->fs_info
, data
);
5009 printk(KERN_ERR
"No space info for %d\n", data
);
5014 * If the space info is for both data and metadata it means we have a
5015 * small filesystem and we can't use the clustering stuff.
5017 if (btrfs_mixed_space_info(space_info
))
5018 use_cluster
= false;
5020 if (orig_root
->ref_cows
|| empty_size
)
5021 allowed_chunk_alloc
= 1;
5023 if (data
& BTRFS_BLOCK_GROUP_METADATA
&& use_cluster
) {
5024 last_ptr
= &root
->fs_info
->meta_alloc_cluster
;
5025 if (!btrfs_test_opt(root
, SSD
))
5026 empty_cluster
= 64 * 1024;
5029 if ((data
& BTRFS_BLOCK_GROUP_DATA
) && use_cluster
&&
5030 btrfs_test_opt(root
, SSD
)) {
5031 last_ptr
= &root
->fs_info
->data_alloc_cluster
;
5035 spin_lock(&last_ptr
->lock
);
5036 if (last_ptr
->block_group
)
5037 hint_byte
= last_ptr
->window_start
;
5038 spin_unlock(&last_ptr
->lock
);
5041 search_start
= max(search_start
, first_logical_byte(root
, 0));
5042 search_start
= max(search_start
, hint_byte
);
5047 if (search_start
== hint_byte
) {
5049 block_group
= btrfs_lookup_block_group(root
->fs_info
,
5052 * we don't want to use the block group if it doesn't match our
5053 * allocation bits, or if its not cached.
5055 * However if we are re-searching with an ideal block group
5056 * picked out then we don't care that the block group is cached.
5058 if (block_group
&& block_group_bits(block_group
, data
) &&
5059 (block_group
->cached
!= BTRFS_CACHE_NO
||
5060 search_start
== ideal_cache_offset
)) {
5061 down_read(&space_info
->groups_sem
);
5062 if (list_empty(&block_group
->list
) ||
5065 * someone is removing this block group,
5066 * we can't jump into the have_block_group
5067 * target because our list pointers are not
5070 btrfs_put_block_group(block_group
);
5071 up_read(&space_info
->groups_sem
);
5073 index
= get_block_group_index(block_group
);
5074 goto have_block_group
;
5076 } else if (block_group
) {
5077 btrfs_put_block_group(block_group
);
5081 down_read(&space_info
->groups_sem
);
5082 list_for_each_entry(block_group
, &space_info
->block_groups
[index
],
5087 btrfs_get_block_group(block_group
);
5088 search_start
= block_group
->key
.objectid
;
5091 * this can happen if we end up cycling through all the
5092 * raid types, but we want to make sure we only allocate
5093 * for the proper type.
5095 if (!block_group_bits(block_group
, data
)) {
5096 u64 extra
= BTRFS_BLOCK_GROUP_DUP
|
5097 BTRFS_BLOCK_GROUP_RAID1
|
5098 BTRFS_BLOCK_GROUP_RAID10
;
5101 * if they asked for extra copies and this block group
5102 * doesn't provide them, bail. This does allow us to
5103 * fill raid0 from raid1.
5105 if ((data
& extra
) && !(block_group
->flags
& extra
))
5110 if (unlikely(block_group
->cached
== BTRFS_CACHE_NO
)) {
5113 ret
= cache_block_group(block_group
, trans
,
5115 if (block_group
->cached
== BTRFS_CACHE_FINISHED
)
5116 goto have_block_group
;
5118 free_percent
= btrfs_block_group_used(&block_group
->item
);
5119 free_percent
*= 100;
5120 free_percent
= div64_u64(free_percent
,
5121 block_group
->key
.offset
);
5122 free_percent
= 100 - free_percent
;
5123 if (free_percent
> ideal_cache_percent
&&
5124 likely(!block_group
->ro
)) {
5125 ideal_cache_offset
= block_group
->key
.objectid
;
5126 ideal_cache_percent
= free_percent
;
5130 * We only want to start kthread caching if we are at
5131 * the point where we will wait for caching to make
5132 * progress, or if our ideal search is over and we've
5133 * found somebody to start caching.
5135 if (loop
> LOOP_CACHING_NOWAIT
||
5136 (loop
> LOOP_FIND_IDEAL
&&
5137 atomic_read(&space_info
->caching_threads
) < 2)) {
5138 ret
= cache_block_group(block_group
, trans
,
5142 found_uncached_bg
= true;
5145 * If loop is set for cached only, try the next block
5148 if (loop
== LOOP_FIND_IDEAL
)
5152 cached
= block_group_cache_done(block_group
);
5153 if (unlikely(!cached
))
5154 found_uncached_bg
= true;
5156 if (unlikely(block_group
->ro
))
5160 * Ok we want to try and use the cluster allocator, so lets look
5161 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
5162 * have tried the cluster allocator plenty of times at this
5163 * point and not have found anything, so we are likely way too
5164 * fragmented for the clustering stuff to find anything, so lets
5165 * just skip it and let the allocator find whatever block it can
5168 if (last_ptr
&& loop
< LOOP_NO_EMPTY_SIZE
) {
5170 * the refill lock keeps out other
5171 * people trying to start a new cluster
5173 spin_lock(&last_ptr
->refill_lock
);
5174 if (last_ptr
->block_group
&&
5175 (last_ptr
->block_group
->ro
||
5176 !block_group_bits(last_ptr
->block_group
, data
))) {
5178 goto refill_cluster
;
5181 offset
= btrfs_alloc_from_cluster(block_group
, last_ptr
,
5182 num_bytes
, search_start
);
5184 /* we have a block, we're done */
5185 spin_unlock(&last_ptr
->refill_lock
);
5189 spin_lock(&last_ptr
->lock
);
5191 * whoops, this cluster doesn't actually point to
5192 * this block group. Get a ref on the block
5193 * group is does point to and try again
5195 if (!last_ptr_loop
&& last_ptr
->block_group
&&
5196 last_ptr
->block_group
!= block_group
) {
5198 btrfs_put_block_group(block_group
);
5199 block_group
= last_ptr
->block_group
;
5200 btrfs_get_block_group(block_group
);
5201 spin_unlock(&last_ptr
->lock
);
5202 spin_unlock(&last_ptr
->refill_lock
);
5205 search_start
= block_group
->key
.objectid
;
5207 * we know this block group is properly
5208 * in the list because
5209 * btrfs_remove_block_group, drops the
5210 * cluster before it removes the block
5211 * group from the list
5213 goto have_block_group
;
5215 spin_unlock(&last_ptr
->lock
);
5218 * this cluster didn't work out, free it and
5221 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
5225 /* allocate a cluster in this block group */
5226 ret
= btrfs_find_space_cluster(trans
, root
,
5227 block_group
, last_ptr
,
5229 empty_cluster
+ empty_size
);
5232 * now pull our allocation out of this
5235 offset
= btrfs_alloc_from_cluster(block_group
,
5236 last_ptr
, num_bytes
,
5239 /* we found one, proceed */
5240 spin_unlock(&last_ptr
->refill_lock
);
5243 } else if (!cached
&& loop
> LOOP_CACHING_NOWAIT
5244 && !failed_cluster_refill
) {
5245 spin_unlock(&last_ptr
->refill_lock
);
5247 failed_cluster_refill
= true;
5248 wait_block_group_cache_progress(block_group
,
5249 num_bytes
+ empty_cluster
+ empty_size
);
5250 goto have_block_group
;
5254 * at this point we either didn't find a cluster
5255 * or we weren't able to allocate a block from our
5256 * cluster. Free the cluster we've been trying
5257 * to use, and go to the next block group
5259 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
5260 spin_unlock(&last_ptr
->refill_lock
);
5264 offset
= btrfs_find_space_for_alloc(block_group
, search_start
,
5265 num_bytes
, empty_size
);
5267 * If we didn't find a chunk, and we haven't failed on this
5268 * block group before, and this block group is in the middle of
5269 * caching and we are ok with waiting, then go ahead and wait
5270 * for progress to be made, and set failed_alloc to true.
5272 * If failed_alloc is true then we've already waited on this
5273 * block group once and should move on to the next block group.
5275 if (!offset
&& !failed_alloc
&& !cached
&&
5276 loop
> LOOP_CACHING_NOWAIT
) {
5277 wait_block_group_cache_progress(block_group
,
5278 num_bytes
+ empty_size
);
5279 failed_alloc
= true;
5280 goto have_block_group
;
5281 } else if (!offset
) {
5285 search_start
= stripe_align(root
, offset
);
5286 /* move on to the next group */
5287 if (search_start
+ num_bytes
>= search_end
) {
5288 btrfs_add_free_space(block_group
, offset
, num_bytes
);
5292 /* move on to the next group */
5293 if (search_start
+ num_bytes
>
5294 block_group
->key
.objectid
+ block_group
->key
.offset
) {
5295 btrfs_add_free_space(block_group
, offset
, num_bytes
);
5299 ins
->objectid
= search_start
;
5300 ins
->offset
= num_bytes
;
5302 if (offset
< search_start
)
5303 btrfs_add_free_space(block_group
, offset
,
5304 search_start
- offset
);
5305 BUG_ON(offset
> search_start
);
5307 ret
= btrfs_update_reserved_bytes(block_group
, num_bytes
, 1,
5308 (data
& BTRFS_BLOCK_GROUP_DATA
));
5309 if (ret
== -EAGAIN
) {
5310 btrfs_add_free_space(block_group
, offset
, num_bytes
);
5314 /* we are all good, lets return */
5315 ins
->objectid
= search_start
;
5316 ins
->offset
= num_bytes
;
5318 if (offset
< search_start
)
5319 btrfs_add_free_space(block_group
, offset
,
5320 search_start
- offset
);
5321 BUG_ON(offset
> search_start
);
5324 failed_cluster_refill
= false;
5325 failed_alloc
= false;
5326 BUG_ON(index
!= get_block_group_index(block_group
));
5327 btrfs_put_block_group(block_group
);
5329 up_read(&space_info
->groups_sem
);
5331 if (!ins
->objectid
&& ++index
< BTRFS_NR_RAID_TYPES
)
5334 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5335 * for them to make caching progress. Also
5336 * determine the best possible bg to cache
5337 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5338 * caching kthreads as we move along
5339 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5340 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5341 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5344 if (!ins
->objectid
&& loop
< LOOP_NO_EMPTY_SIZE
&&
5345 (found_uncached_bg
|| empty_size
|| empty_cluster
||
5346 allowed_chunk_alloc
)) {
5348 if (loop
== LOOP_FIND_IDEAL
&& found_uncached_bg
) {
5349 found_uncached_bg
= false;
5351 if (!ideal_cache_percent
&&
5352 atomic_read(&space_info
->caching_threads
))
5356 * 1 of the following 2 things have happened so far
5358 * 1) We found an ideal block group for caching that
5359 * is mostly full and will cache quickly, so we might
5360 * as well wait for it.
5362 * 2) We searched for cached only and we didn't find
5363 * anything, and we didn't start any caching kthreads
5364 * either, so chances are we will loop through and
5365 * start a couple caching kthreads, and then come back
5366 * around and just wait for them. This will be slower
5367 * because we will have 2 caching kthreads reading at
5368 * the same time when we could have just started one
5369 * and waited for it to get far enough to give us an
5370 * allocation, so go ahead and go to the wait caching
5373 loop
= LOOP_CACHING_WAIT
;
5374 search_start
= ideal_cache_offset
;
5375 ideal_cache_percent
= 0;
5377 } else if (loop
== LOOP_FIND_IDEAL
) {
5379 * Didn't find a uncached bg, wait on anything we find
5382 loop
= LOOP_CACHING_WAIT
;
5386 if (loop
< LOOP_CACHING_WAIT
) {
5391 if (loop
== LOOP_ALLOC_CHUNK
) {
5396 if (allowed_chunk_alloc
) {
5397 ret
= do_chunk_alloc(trans
, root
, num_bytes
+
5398 2 * 1024 * 1024, data
,
5399 CHUNK_ALLOC_LIMITED
);
5400 allowed_chunk_alloc
= 0;
5401 done_chunk_alloc
= 1;
5402 } else if (!done_chunk_alloc
&&
5403 space_info
->force_alloc
== CHUNK_ALLOC_NO_FORCE
) {
5404 space_info
->force_alloc
= CHUNK_ALLOC_LIMITED
;
5407 if (loop
< LOOP_NO_EMPTY_SIZE
) {
5412 } else if (!ins
->objectid
) {
5416 /* we found what we needed */
5417 if (ins
->objectid
) {
5418 if (!(data
& BTRFS_BLOCK_GROUP_DATA
))
5419 trans
->block_group
= block_group
->key
.objectid
;
5421 btrfs_put_block_group(block_group
);
5428 static void dump_space_info(struct btrfs_space_info
*info
, u64 bytes
,
5429 int dump_block_groups
)
5431 struct btrfs_block_group_cache
*cache
;
5434 spin_lock(&info
->lock
);
5435 printk(KERN_INFO
"space_info has %llu free, is %sfull\n",
5436 (unsigned long long)(info
->total_bytes
- info
->bytes_used
-
5437 info
->bytes_pinned
- info
->bytes_reserved
-
5438 info
->bytes_readonly
),
5439 (info
->full
) ? "" : "not ");
5440 printk(KERN_INFO
"space_info total=%llu, used=%llu, pinned=%llu, "
5441 "reserved=%llu, may_use=%llu, readonly=%llu\n",
5442 (unsigned long long)info
->total_bytes
,
5443 (unsigned long long)info
->bytes_used
,
5444 (unsigned long long)info
->bytes_pinned
,
5445 (unsigned long long)info
->bytes_reserved
,
5446 (unsigned long long)info
->bytes_may_use
,
5447 (unsigned long long)info
->bytes_readonly
);
5448 spin_unlock(&info
->lock
);
5450 if (!dump_block_groups
)
5453 down_read(&info
->groups_sem
);
5455 list_for_each_entry(cache
, &info
->block_groups
[index
], list
) {
5456 spin_lock(&cache
->lock
);
5457 printk(KERN_INFO
"block group %llu has %llu bytes, %llu used "
5458 "%llu pinned %llu reserved\n",
5459 (unsigned long long)cache
->key
.objectid
,
5460 (unsigned long long)cache
->key
.offset
,
5461 (unsigned long long)btrfs_block_group_used(&cache
->item
),
5462 (unsigned long long)cache
->pinned
,
5463 (unsigned long long)cache
->reserved
);
5464 btrfs_dump_free_space(cache
, bytes
);
5465 spin_unlock(&cache
->lock
);
5467 if (++index
< BTRFS_NR_RAID_TYPES
)
5469 up_read(&info
->groups_sem
);
5472 int btrfs_reserve_extent(struct btrfs_trans_handle
*trans
,
5473 struct btrfs_root
*root
,
5474 u64 num_bytes
, u64 min_alloc_size
,
5475 u64 empty_size
, u64 hint_byte
,
5476 u64 search_end
, struct btrfs_key
*ins
,
5480 u64 search_start
= 0;
5482 data
= btrfs_get_alloc_profile(root
, data
);
5485 * the only place that sets empty_size is btrfs_realloc_node, which
5486 * is not called recursively on allocations
5488 if (empty_size
|| root
->ref_cows
)
5489 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
5490 num_bytes
+ 2 * 1024 * 1024, data
,
5491 CHUNK_ALLOC_NO_FORCE
);
5493 WARN_ON(num_bytes
< root
->sectorsize
);
5494 ret
= find_free_extent(trans
, root
, num_bytes
, empty_size
,
5495 search_start
, search_end
, hint_byte
,
5498 if (ret
== -ENOSPC
&& num_bytes
> min_alloc_size
) {
5499 num_bytes
= num_bytes
>> 1;
5500 num_bytes
= num_bytes
& ~(root
->sectorsize
- 1);
5501 num_bytes
= max(num_bytes
, min_alloc_size
);
5502 do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
5503 num_bytes
, data
, CHUNK_ALLOC_FORCE
);
5506 if (ret
== -ENOSPC
&& btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
5507 struct btrfs_space_info
*sinfo
;
5509 sinfo
= __find_space_info(root
->fs_info
, data
);
5510 printk(KERN_ERR
"btrfs allocation failed flags %llu, "
5511 "wanted %llu\n", (unsigned long long)data
,
5512 (unsigned long long)num_bytes
);
5513 dump_space_info(sinfo
, num_bytes
, 1);
5516 trace_btrfs_reserved_extent_alloc(root
, ins
->objectid
, ins
->offset
);
5521 int btrfs_free_reserved_extent(struct btrfs_root
*root
, u64 start
, u64 len
)
5523 struct btrfs_block_group_cache
*cache
;
5526 cache
= btrfs_lookup_block_group(root
->fs_info
, start
);
5528 printk(KERN_ERR
"Unable to find block group for %llu\n",
5529 (unsigned long long)start
);
5533 if (btrfs_test_opt(root
, DISCARD
))
5534 ret
= btrfs_discard_extent(root
, start
, len
, NULL
);
5536 btrfs_add_free_space(cache
, start
, len
);
5537 btrfs_update_reserved_bytes(cache
, len
, 0, 1);
5538 btrfs_put_block_group(cache
);
5540 trace_btrfs_reserved_extent_free(root
, start
, len
);
5545 static int alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
5546 struct btrfs_root
*root
,
5547 u64 parent
, u64 root_objectid
,
5548 u64 flags
, u64 owner
, u64 offset
,
5549 struct btrfs_key
*ins
, int ref_mod
)
5552 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5553 struct btrfs_extent_item
*extent_item
;
5554 struct btrfs_extent_inline_ref
*iref
;
5555 struct btrfs_path
*path
;
5556 struct extent_buffer
*leaf
;
5561 type
= BTRFS_SHARED_DATA_REF_KEY
;
5563 type
= BTRFS_EXTENT_DATA_REF_KEY
;
5565 size
= sizeof(*extent_item
) + btrfs_extent_inline_ref_size(type
);
5567 path
= btrfs_alloc_path();
5571 path
->leave_spinning
= 1;
5572 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
5576 leaf
= path
->nodes
[0];
5577 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
5578 struct btrfs_extent_item
);
5579 btrfs_set_extent_refs(leaf
, extent_item
, ref_mod
);
5580 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
5581 btrfs_set_extent_flags(leaf
, extent_item
,
5582 flags
| BTRFS_EXTENT_FLAG_DATA
);
5584 iref
= (struct btrfs_extent_inline_ref
*)(extent_item
+ 1);
5585 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
5587 struct btrfs_shared_data_ref
*ref
;
5588 ref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
5589 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
5590 btrfs_set_shared_data_ref_count(leaf
, ref
, ref_mod
);
5592 struct btrfs_extent_data_ref
*ref
;
5593 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
5594 btrfs_set_extent_data_ref_root(leaf
, ref
, root_objectid
);
5595 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
5596 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
5597 btrfs_set_extent_data_ref_count(leaf
, ref
, ref_mod
);
5600 btrfs_mark_buffer_dirty(path
->nodes
[0]);
5601 btrfs_free_path(path
);
5603 ret
= update_block_group(trans
, root
, ins
->objectid
, ins
->offset
, 1);
5605 printk(KERN_ERR
"btrfs update block group failed for %llu "
5606 "%llu\n", (unsigned long long)ins
->objectid
,
5607 (unsigned long long)ins
->offset
);
5613 static int alloc_reserved_tree_block(struct btrfs_trans_handle
*trans
,
5614 struct btrfs_root
*root
,
5615 u64 parent
, u64 root_objectid
,
5616 u64 flags
, struct btrfs_disk_key
*key
,
5617 int level
, struct btrfs_key
*ins
)
5620 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5621 struct btrfs_extent_item
*extent_item
;
5622 struct btrfs_tree_block_info
*block_info
;
5623 struct btrfs_extent_inline_ref
*iref
;
5624 struct btrfs_path
*path
;
5625 struct extent_buffer
*leaf
;
5626 u32 size
= sizeof(*extent_item
) + sizeof(*block_info
) + sizeof(*iref
);
5628 path
= btrfs_alloc_path();
5631 path
->leave_spinning
= 1;
5632 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
5636 leaf
= path
->nodes
[0];
5637 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
5638 struct btrfs_extent_item
);
5639 btrfs_set_extent_refs(leaf
, extent_item
, 1);
5640 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
5641 btrfs_set_extent_flags(leaf
, extent_item
,
5642 flags
| BTRFS_EXTENT_FLAG_TREE_BLOCK
);
5643 block_info
= (struct btrfs_tree_block_info
*)(extent_item
+ 1);
5645 btrfs_set_tree_block_key(leaf
, block_info
, key
);
5646 btrfs_set_tree_block_level(leaf
, block_info
, level
);
5648 iref
= (struct btrfs_extent_inline_ref
*)(block_info
+ 1);
5650 BUG_ON(!(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
));
5651 btrfs_set_extent_inline_ref_type(leaf
, iref
,
5652 BTRFS_SHARED_BLOCK_REF_KEY
);
5653 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
5655 btrfs_set_extent_inline_ref_type(leaf
, iref
,
5656 BTRFS_TREE_BLOCK_REF_KEY
);
5657 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
5660 btrfs_mark_buffer_dirty(leaf
);
5661 btrfs_free_path(path
);
5663 ret
= update_block_group(trans
, root
, ins
->objectid
, ins
->offset
, 1);
5665 printk(KERN_ERR
"btrfs update block group failed for %llu "
5666 "%llu\n", (unsigned long long)ins
->objectid
,
5667 (unsigned long long)ins
->offset
);
5673 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
5674 struct btrfs_root
*root
,
5675 u64 root_objectid
, u64 owner
,
5676 u64 offset
, struct btrfs_key
*ins
)
5680 BUG_ON(root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
5682 ret
= btrfs_add_delayed_data_ref(trans
, ins
->objectid
, ins
->offset
,
5683 0, root_objectid
, owner
, offset
,
5684 BTRFS_ADD_DELAYED_EXTENT
, NULL
);
5689 * this is used by the tree logging recovery code. It records that
5690 * an extent has been allocated and makes sure to clear the free
5691 * space cache bits as well
5693 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle
*trans
,
5694 struct btrfs_root
*root
,
5695 u64 root_objectid
, u64 owner
, u64 offset
,
5696 struct btrfs_key
*ins
)
5699 struct btrfs_block_group_cache
*block_group
;
5700 struct btrfs_caching_control
*caching_ctl
;
5701 u64 start
= ins
->objectid
;
5702 u64 num_bytes
= ins
->offset
;
5704 block_group
= btrfs_lookup_block_group(root
->fs_info
, ins
->objectid
);
5705 cache_block_group(block_group
, trans
, NULL
, 0);
5706 caching_ctl
= get_caching_control(block_group
);
5709 BUG_ON(!block_group_cache_done(block_group
));
5710 ret
= btrfs_remove_free_space(block_group
, start
, num_bytes
);
5713 mutex_lock(&caching_ctl
->mutex
);
5715 if (start
>= caching_ctl
->progress
) {
5716 ret
= add_excluded_extent(root
, start
, num_bytes
);
5718 } else if (start
+ num_bytes
<= caching_ctl
->progress
) {
5719 ret
= btrfs_remove_free_space(block_group
,
5723 num_bytes
= caching_ctl
->progress
- start
;
5724 ret
= btrfs_remove_free_space(block_group
,
5728 start
= caching_ctl
->progress
;
5729 num_bytes
= ins
->objectid
+ ins
->offset
-
5730 caching_ctl
->progress
;
5731 ret
= add_excluded_extent(root
, start
, num_bytes
);
5735 mutex_unlock(&caching_ctl
->mutex
);
5736 put_caching_control(caching_ctl
);
5739 ret
= btrfs_update_reserved_bytes(block_group
, ins
->offset
, 1, 1);
5741 btrfs_put_block_group(block_group
);
5742 ret
= alloc_reserved_file_extent(trans
, root
, 0, root_objectid
,
5743 0, owner
, offset
, ins
, 1);
5747 struct extent_buffer
*btrfs_init_new_buffer(struct btrfs_trans_handle
*trans
,
5748 struct btrfs_root
*root
,
5749 u64 bytenr
, u32 blocksize
,
5752 struct extent_buffer
*buf
;
5754 buf
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
5756 return ERR_PTR(-ENOMEM
);
5757 btrfs_set_header_generation(buf
, trans
->transid
);
5758 btrfs_set_buffer_lockdep_class(buf
, level
);
5759 btrfs_tree_lock(buf
);
5760 clean_tree_block(trans
, root
, buf
);
5762 btrfs_set_lock_blocking(buf
);
5763 btrfs_set_buffer_uptodate(buf
);
5765 if (root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
) {
5767 * we allow two log transactions at a time, use different
5768 * EXENT bit to differentiate dirty pages.
5770 if (root
->log_transid
% 2 == 0)
5771 set_extent_dirty(&root
->dirty_log_pages
, buf
->start
,
5772 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
5774 set_extent_new(&root
->dirty_log_pages
, buf
->start
,
5775 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
5777 set_extent_dirty(&trans
->transaction
->dirty_pages
, buf
->start
,
5778 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
5780 trans
->blocks_used
++;
5781 /* this returns a buffer locked for blocking */
5785 static struct btrfs_block_rsv
*
5786 use_block_rsv(struct btrfs_trans_handle
*trans
,
5787 struct btrfs_root
*root
, u32 blocksize
)
5789 struct btrfs_block_rsv
*block_rsv
;
5790 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
5793 block_rsv
= get_block_rsv(trans
, root
);
5795 if (block_rsv
->size
== 0) {
5796 ret
= reserve_metadata_bytes(trans
, root
, block_rsv
,
5799 * If we couldn't reserve metadata bytes try and use some from
5800 * the global reserve.
5802 if (ret
&& block_rsv
!= global_rsv
) {
5803 ret
= block_rsv_use_bytes(global_rsv
, blocksize
);
5806 return ERR_PTR(ret
);
5808 return ERR_PTR(ret
);
5813 ret
= block_rsv_use_bytes(block_rsv
, blocksize
);
5818 ret
= reserve_metadata_bytes(trans
, root
, block_rsv
, blocksize
,
5821 spin_lock(&block_rsv
->lock
);
5822 block_rsv
->size
+= blocksize
;
5823 spin_unlock(&block_rsv
->lock
);
5825 } else if (ret
&& block_rsv
!= global_rsv
) {
5826 ret
= block_rsv_use_bytes(global_rsv
, blocksize
);
5832 return ERR_PTR(-ENOSPC
);
5835 static void unuse_block_rsv(struct btrfs_block_rsv
*block_rsv
, u32 blocksize
)
5837 block_rsv_add_bytes(block_rsv
, blocksize
, 0);
5838 block_rsv_release_bytes(block_rsv
, NULL
, 0);
5842 * finds a free extent and does all the dirty work required for allocation
5843 * returns the key for the extent through ins, and a tree buffer for
5844 * the first block of the extent through buf.
5846 * returns the tree buffer or NULL.
5848 struct extent_buffer
*btrfs_alloc_free_block(struct btrfs_trans_handle
*trans
,
5849 struct btrfs_root
*root
, u32 blocksize
,
5850 u64 parent
, u64 root_objectid
,
5851 struct btrfs_disk_key
*key
, int level
,
5852 u64 hint
, u64 empty_size
)
5854 struct btrfs_key ins
;
5855 struct btrfs_block_rsv
*block_rsv
;
5856 struct extent_buffer
*buf
;
5861 block_rsv
= use_block_rsv(trans
, root
, blocksize
);
5862 if (IS_ERR(block_rsv
))
5863 return ERR_CAST(block_rsv
);
5865 ret
= btrfs_reserve_extent(trans
, root
, blocksize
, blocksize
,
5866 empty_size
, hint
, (u64
)-1, &ins
, 0);
5868 unuse_block_rsv(block_rsv
, blocksize
);
5869 return ERR_PTR(ret
);
5872 buf
= btrfs_init_new_buffer(trans
, root
, ins
.objectid
,
5874 BUG_ON(IS_ERR(buf
));
5876 if (root_objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
5878 parent
= ins
.objectid
;
5879 flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
5883 if (root_objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
5884 struct btrfs_delayed_extent_op
*extent_op
;
5885 extent_op
= kmalloc(sizeof(*extent_op
), GFP_NOFS
);
5888 memcpy(&extent_op
->key
, key
, sizeof(extent_op
->key
));
5890 memset(&extent_op
->key
, 0, sizeof(extent_op
->key
));
5891 extent_op
->flags_to_set
= flags
;
5892 extent_op
->update_key
= 1;
5893 extent_op
->update_flags
= 1;
5894 extent_op
->is_data
= 0;
5896 ret
= btrfs_add_delayed_tree_ref(trans
, ins
.objectid
,
5897 ins
.offset
, parent
, root_objectid
,
5898 level
, BTRFS_ADD_DELAYED_EXTENT
,
5905 struct walk_control
{
5906 u64 refs
[BTRFS_MAX_LEVEL
];
5907 u64 flags
[BTRFS_MAX_LEVEL
];
5908 struct btrfs_key update_progress
;
5918 #define DROP_REFERENCE 1
5919 #define UPDATE_BACKREF 2
5921 static noinline
void reada_walk_down(struct btrfs_trans_handle
*trans
,
5922 struct btrfs_root
*root
,
5923 struct walk_control
*wc
,
5924 struct btrfs_path
*path
)
5932 struct btrfs_key key
;
5933 struct extent_buffer
*eb
;
5938 if (path
->slots
[wc
->level
] < wc
->reada_slot
) {
5939 wc
->reada_count
= wc
->reada_count
* 2 / 3;
5940 wc
->reada_count
= max(wc
->reada_count
, 2);
5942 wc
->reada_count
= wc
->reada_count
* 3 / 2;
5943 wc
->reada_count
= min_t(int, wc
->reada_count
,
5944 BTRFS_NODEPTRS_PER_BLOCK(root
));
5947 eb
= path
->nodes
[wc
->level
];
5948 nritems
= btrfs_header_nritems(eb
);
5949 blocksize
= btrfs_level_size(root
, wc
->level
- 1);
5951 for (slot
= path
->slots
[wc
->level
]; slot
< nritems
; slot
++) {
5952 if (nread
>= wc
->reada_count
)
5956 bytenr
= btrfs_node_blockptr(eb
, slot
);
5957 generation
= btrfs_node_ptr_generation(eb
, slot
);
5959 if (slot
== path
->slots
[wc
->level
])
5962 if (wc
->stage
== UPDATE_BACKREF
&&
5963 generation
<= root
->root_key
.offset
)
5966 /* We don't lock the tree block, it's OK to be racy here */
5967 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
, blocksize
,
5972 if (wc
->stage
== DROP_REFERENCE
) {
5976 if (wc
->level
== 1 &&
5977 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
5979 if (!wc
->update_ref
||
5980 generation
<= root
->root_key
.offset
)
5982 btrfs_node_key_to_cpu(eb
, &key
, slot
);
5983 ret
= btrfs_comp_cpu_keys(&key
,
5984 &wc
->update_progress
);
5988 if (wc
->level
== 1 &&
5989 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
5993 ret
= readahead_tree_block(root
, bytenr
, blocksize
,
5999 wc
->reada_slot
= slot
;
6003 * hepler to process tree block while walking down the tree.
6005 * when wc->stage == UPDATE_BACKREF, this function updates
6006 * back refs for pointers in the block.
6008 * NOTE: return value 1 means we should stop walking down.
6010 static noinline
int walk_down_proc(struct btrfs_trans_handle
*trans
,
6011 struct btrfs_root
*root
,
6012 struct btrfs_path
*path
,
6013 struct walk_control
*wc
, int lookup_info
)
6015 int level
= wc
->level
;
6016 struct extent_buffer
*eb
= path
->nodes
[level
];
6017 u64 flag
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
6020 if (wc
->stage
== UPDATE_BACKREF
&&
6021 btrfs_header_owner(eb
) != root
->root_key
.objectid
)
6025 * when reference count of tree block is 1, it won't increase
6026 * again. once full backref flag is set, we never clear it.
6029 ((wc
->stage
== DROP_REFERENCE
&& wc
->refs
[level
] != 1) ||
6030 (wc
->stage
== UPDATE_BACKREF
&& !(wc
->flags
[level
] & flag
)))) {
6031 BUG_ON(!path
->locks
[level
]);
6032 ret
= btrfs_lookup_extent_info(trans
, root
,
6037 BUG_ON(wc
->refs
[level
] == 0);
6040 if (wc
->stage
== DROP_REFERENCE
) {
6041 if (wc
->refs
[level
] > 1)
6044 if (path
->locks
[level
] && !wc
->keep_locks
) {
6045 btrfs_tree_unlock(eb
);
6046 path
->locks
[level
] = 0;
6051 /* wc->stage == UPDATE_BACKREF */
6052 if (!(wc
->flags
[level
] & flag
)) {
6053 BUG_ON(!path
->locks
[level
]);
6054 ret
= btrfs_inc_ref(trans
, root
, eb
, 1);
6056 ret
= btrfs_dec_ref(trans
, root
, eb
, 0);
6058 ret
= btrfs_set_disk_extent_flags(trans
, root
, eb
->start
,
6061 wc
->flags
[level
] |= flag
;
6065 * the block is shared by multiple trees, so it's not good to
6066 * keep the tree lock
6068 if (path
->locks
[level
] && level
> 0) {
6069 btrfs_tree_unlock(eb
);
6070 path
->locks
[level
] = 0;
6076 * hepler to process tree block pointer.
6078 * when wc->stage == DROP_REFERENCE, this function checks
6079 * reference count of the block pointed to. if the block
6080 * is shared and we need update back refs for the subtree
6081 * rooted at the block, this function changes wc->stage to
6082 * UPDATE_BACKREF. if the block is shared and there is no
6083 * need to update back, this function drops the reference
6086 * NOTE: return value 1 means we should stop walking down.
6088 static noinline
int do_walk_down(struct btrfs_trans_handle
*trans
,
6089 struct btrfs_root
*root
,
6090 struct btrfs_path
*path
,
6091 struct walk_control
*wc
, int *lookup_info
)
6097 struct btrfs_key key
;
6098 struct extent_buffer
*next
;
6099 int level
= wc
->level
;
6103 generation
= btrfs_node_ptr_generation(path
->nodes
[level
],
6104 path
->slots
[level
]);
6106 * if the lower level block was created before the snapshot
6107 * was created, we know there is no need to update back refs
6110 if (wc
->stage
== UPDATE_BACKREF
&&
6111 generation
<= root
->root_key
.offset
) {
6116 bytenr
= btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]);
6117 blocksize
= btrfs_level_size(root
, level
- 1);
6119 next
= btrfs_find_tree_block(root
, bytenr
, blocksize
);
6121 next
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
6126 btrfs_tree_lock(next
);
6127 btrfs_set_lock_blocking(next
);
6129 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
, blocksize
,
6130 &wc
->refs
[level
- 1],
6131 &wc
->flags
[level
- 1]);
6133 BUG_ON(wc
->refs
[level
- 1] == 0);
6136 if (wc
->stage
== DROP_REFERENCE
) {
6137 if (wc
->refs
[level
- 1] > 1) {
6139 (wc
->flags
[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF
))
6142 if (!wc
->update_ref
||
6143 generation
<= root
->root_key
.offset
)
6146 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
6147 path
->slots
[level
]);
6148 ret
= btrfs_comp_cpu_keys(&key
, &wc
->update_progress
);
6152 wc
->stage
= UPDATE_BACKREF
;
6153 wc
->shared_level
= level
- 1;
6157 (wc
->flags
[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF
))
6161 if (!btrfs_buffer_uptodate(next
, generation
)) {
6162 btrfs_tree_unlock(next
);
6163 free_extent_buffer(next
);
6169 if (reada
&& level
== 1)
6170 reada_walk_down(trans
, root
, wc
, path
);
6171 next
= read_tree_block(root
, bytenr
, blocksize
, generation
);
6174 btrfs_tree_lock(next
);
6175 btrfs_set_lock_blocking(next
);
6179 BUG_ON(level
!= btrfs_header_level(next
));
6180 path
->nodes
[level
] = next
;
6181 path
->slots
[level
] = 0;
6182 path
->locks
[level
] = 1;
6188 wc
->refs
[level
- 1] = 0;
6189 wc
->flags
[level
- 1] = 0;
6190 if (wc
->stage
== DROP_REFERENCE
) {
6191 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
) {
6192 parent
= path
->nodes
[level
]->start
;
6194 BUG_ON(root
->root_key
.objectid
!=
6195 btrfs_header_owner(path
->nodes
[level
]));
6199 ret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
, parent
,
6200 root
->root_key
.objectid
, level
- 1, 0);
6203 btrfs_tree_unlock(next
);
6204 free_extent_buffer(next
);
6210 * hepler to process tree block while walking up the tree.
6212 * when wc->stage == DROP_REFERENCE, this function drops
6213 * reference count on the block.
6215 * when wc->stage == UPDATE_BACKREF, this function changes
6216 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6217 * to UPDATE_BACKREF previously while processing the block.
6219 * NOTE: return value 1 means we should stop walking up.
6221 static noinline
int walk_up_proc(struct btrfs_trans_handle
*trans
,
6222 struct btrfs_root
*root
,
6223 struct btrfs_path
*path
,
6224 struct walk_control
*wc
)
6227 int level
= wc
->level
;
6228 struct extent_buffer
*eb
= path
->nodes
[level
];
6231 if (wc
->stage
== UPDATE_BACKREF
) {
6232 BUG_ON(wc
->shared_level
< level
);
6233 if (level
< wc
->shared_level
)
6236 ret
= find_next_key(path
, level
+ 1, &wc
->update_progress
);
6240 wc
->stage
= DROP_REFERENCE
;
6241 wc
->shared_level
= -1;
6242 path
->slots
[level
] = 0;
6245 * check reference count again if the block isn't locked.
6246 * we should start walking down the tree again if reference
6249 if (!path
->locks
[level
]) {
6251 btrfs_tree_lock(eb
);
6252 btrfs_set_lock_blocking(eb
);
6253 path
->locks
[level
] = 1;
6255 ret
= btrfs_lookup_extent_info(trans
, root
,
6260 BUG_ON(wc
->refs
[level
] == 0);
6261 if (wc
->refs
[level
] == 1) {
6262 btrfs_tree_unlock(eb
);
6263 path
->locks
[level
] = 0;
6269 /* wc->stage == DROP_REFERENCE */
6270 BUG_ON(wc
->refs
[level
] > 1 && !path
->locks
[level
]);
6272 if (wc
->refs
[level
] == 1) {
6274 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
6275 ret
= btrfs_dec_ref(trans
, root
, eb
, 1);
6277 ret
= btrfs_dec_ref(trans
, root
, eb
, 0);
6280 /* make block locked assertion in clean_tree_block happy */
6281 if (!path
->locks
[level
] &&
6282 btrfs_header_generation(eb
) == trans
->transid
) {
6283 btrfs_tree_lock(eb
);
6284 btrfs_set_lock_blocking(eb
);
6285 path
->locks
[level
] = 1;
6287 clean_tree_block(trans
, root
, eb
);
6290 if (eb
== root
->node
) {
6291 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
6294 BUG_ON(root
->root_key
.objectid
!=
6295 btrfs_header_owner(eb
));
6297 if (wc
->flags
[level
+ 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
6298 parent
= path
->nodes
[level
+ 1]->start
;
6300 BUG_ON(root
->root_key
.objectid
!=
6301 btrfs_header_owner(path
->nodes
[level
+ 1]));
6304 btrfs_free_tree_block(trans
, root
, eb
, parent
, wc
->refs
[level
] == 1);
6306 wc
->refs
[level
] = 0;
6307 wc
->flags
[level
] = 0;
6311 static noinline
int walk_down_tree(struct btrfs_trans_handle
*trans
,
6312 struct btrfs_root
*root
,
6313 struct btrfs_path
*path
,
6314 struct walk_control
*wc
)
6316 int level
= wc
->level
;
6317 int lookup_info
= 1;
6320 while (level
>= 0) {
6321 ret
= walk_down_proc(trans
, root
, path
, wc
, lookup_info
);
6328 if (path
->slots
[level
] >=
6329 btrfs_header_nritems(path
->nodes
[level
]))
6332 ret
= do_walk_down(trans
, root
, path
, wc
, &lookup_info
);
6334 path
->slots
[level
]++;
6343 static noinline
int walk_up_tree(struct btrfs_trans_handle
*trans
,
6344 struct btrfs_root
*root
,
6345 struct btrfs_path
*path
,
6346 struct walk_control
*wc
, int max_level
)
6348 int level
= wc
->level
;
6351 path
->slots
[level
] = btrfs_header_nritems(path
->nodes
[level
]);
6352 while (level
< max_level
&& path
->nodes
[level
]) {
6354 if (path
->slots
[level
] + 1 <
6355 btrfs_header_nritems(path
->nodes
[level
])) {
6356 path
->slots
[level
]++;
6359 ret
= walk_up_proc(trans
, root
, path
, wc
);
6363 if (path
->locks
[level
]) {
6364 btrfs_tree_unlock(path
->nodes
[level
]);
6365 path
->locks
[level
] = 0;
6367 free_extent_buffer(path
->nodes
[level
]);
6368 path
->nodes
[level
] = NULL
;
6376 * drop a subvolume tree.
6378 * this function traverses the tree freeing any blocks that only
6379 * referenced by the tree.
6381 * when a shared tree block is found. this function decreases its
6382 * reference count by one. if update_ref is true, this function
6383 * also make sure backrefs for the shared block and all lower level
6384 * blocks are properly updated.
6386 int btrfs_drop_snapshot(struct btrfs_root
*root
,
6387 struct btrfs_block_rsv
*block_rsv
, int update_ref
)
6389 struct btrfs_path
*path
;
6390 struct btrfs_trans_handle
*trans
;
6391 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
6392 struct btrfs_root_item
*root_item
= &root
->root_item
;
6393 struct walk_control
*wc
;
6394 struct btrfs_key key
;
6399 path
= btrfs_alloc_path();
6402 wc
= kzalloc(sizeof(*wc
), GFP_NOFS
);
6405 trans
= btrfs_start_transaction(tree_root
, 0);
6406 BUG_ON(IS_ERR(trans
));
6409 trans
->block_rsv
= block_rsv
;
6411 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
6412 level
= btrfs_header_level(root
->node
);
6413 path
->nodes
[level
] = btrfs_lock_root_node(root
);
6414 btrfs_set_lock_blocking(path
->nodes
[level
]);
6415 path
->slots
[level
] = 0;
6416 path
->locks
[level
] = 1;
6417 memset(&wc
->update_progress
, 0,
6418 sizeof(wc
->update_progress
));
6420 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
6421 memcpy(&wc
->update_progress
, &key
,
6422 sizeof(wc
->update_progress
));
6424 level
= root_item
->drop_level
;
6426 path
->lowest_level
= level
;
6427 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
6428 path
->lowest_level
= 0;
6436 * unlock our path, this is safe because only this
6437 * function is allowed to delete this snapshot
6439 btrfs_unlock_up_safe(path
, 0);
6441 level
= btrfs_header_level(root
->node
);
6443 btrfs_tree_lock(path
->nodes
[level
]);
6444 btrfs_set_lock_blocking(path
->nodes
[level
]);
6446 ret
= btrfs_lookup_extent_info(trans
, root
,
6447 path
->nodes
[level
]->start
,
6448 path
->nodes
[level
]->len
,
6452 BUG_ON(wc
->refs
[level
] == 0);
6454 if (level
== root_item
->drop_level
)
6457 btrfs_tree_unlock(path
->nodes
[level
]);
6458 WARN_ON(wc
->refs
[level
] != 1);
6464 wc
->shared_level
= -1;
6465 wc
->stage
= DROP_REFERENCE
;
6466 wc
->update_ref
= update_ref
;
6468 wc
->reada_count
= BTRFS_NODEPTRS_PER_BLOCK(root
);
6471 ret
= walk_down_tree(trans
, root
, path
, wc
);
6477 ret
= walk_up_tree(trans
, root
, path
, wc
, BTRFS_MAX_LEVEL
);
6484 BUG_ON(wc
->stage
!= DROP_REFERENCE
);
6488 if (wc
->stage
== DROP_REFERENCE
) {
6490 btrfs_node_key(path
->nodes
[level
],
6491 &root_item
->drop_progress
,
6492 path
->slots
[level
]);
6493 root_item
->drop_level
= level
;
6496 BUG_ON(wc
->level
== 0);
6497 if (btrfs_should_end_transaction(trans
, tree_root
)) {
6498 ret
= btrfs_update_root(trans
, tree_root
,
6503 btrfs_end_transaction_throttle(trans
, tree_root
);
6504 trans
= btrfs_start_transaction(tree_root
, 0);
6505 BUG_ON(IS_ERR(trans
));
6507 trans
->block_rsv
= block_rsv
;
6510 btrfs_release_path(root
, path
);
6513 ret
= btrfs_del_root(trans
, tree_root
, &root
->root_key
);
6516 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
6517 ret
= btrfs_find_last_root(tree_root
, root
->root_key
.objectid
,
6521 /* if we fail to delete the orphan item this time
6522 * around, it'll get picked up the next time.
6524 * The most common failure here is just -ENOENT.
6526 btrfs_del_orphan_item(trans
, tree_root
,
6527 root
->root_key
.objectid
);
6531 if (root
->in_radix
) {
6532 btrfs_free_fs_root(tree_root
->fs_info
, root
);
6534 free_extent_buffer(root
->node
);
6535 free_extent_buffer(root
->commit_root
);
6539 btrfs_end_transaction_throttle(trans
, tree_root
);
6541 btrfs_free_path(path
);
6546 * drop subtree rooted at tree block 'node'.
6548 * NOTE: this function will unlock and release tree block 'node'
6550 int btrfs_drop_subtree(struct btrfs_trans_handle
*trans
,
6551 struct btrfs_root
*root
,
6552 struct extent_buffer
*node
,
6553 struct extent_buffer
*parent
)
6555 struct btrfs_path
*path
;
6556 struct walk_control
*wc
;
6562 BUG_ON(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
6564 path
= btrfs_alloc_path();
6568 wc
= kzalloc(sizeof(*wc
), GFP_NOFS
);
6570 btrfs_free_path(path
);
6574 btrfs_assert_tree_locked(parent
);
6575 parent_level
= btrfs_header_level(parent
);
6576 extent_buffer_get(parent
);
6577 path
->nodes
[parent_level
] = parent
;
6578 path
->slots
[parent_level
] = btrfs_header_nritems(parent
);
6580 btrfs_assert_tree_locked(node
);
6581 level
= btrfs_header_level(node
);
6582 path
->nodes
[level
] = node
;
6583 path
->slots
[level
] = 0;
6584 path
->locks
[level
] = 1;
6586 wc
->refs
[parent_level
] = 1;
6587 wc
->flags
[parent_level
] = BTRFS_BLOCK_FLAG_FULL_BACKREF
;
6589 wc
->shared_level
= -1;
6590 wc
->stage
= DROP_REFERENCE
;
6593 wc
->reada_count
= BTRFS_NODEPTRS_PER_BLOCK(root
);
6596 wret
= walk_down_tree(trans
, root
, path
, wc
);
6602 wret
= walk_up_tree(trans
, root
, path
, wc
, parent_level
);
6610 btrfs_free_path(path
);
6615 static unsigned long calc_ra(unsigned long start
, unsigned long last
,
6618 return min(last
, start
+ nr
- 1);
6621 static noinline
int relocate_inode_pages(struct inode
*inode
, u64 start
,
6626 unsigned long first_index
;
6627 unsigned long last_index
;
6630 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6631 struct file_ra_state
*ra
;
6632 struct btrfs_ordered_extent
*ordered
;
6633 unsigned int total_read
= 0;
6634 unsigned int total_dirty
= 0;
6637 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
6641 mutex_lock(&inode
->i_mutex
);
6642 first_index
= start
>> PAGE_CACHE_SHIFT
;
6643 last_index
= (start
+ len
- 1) >> PAGE_CACHE_SHIFT
;
6645 /* make sure the dirty trick played by the caller work */
6646 ret
= invalidate_inode_pages2_range(inode
->i_mapping
,
6647 first_index
, last_index
);
6651 file_ra_state_init(ra
, inode
->i_mapping
);
6653 for (i
= first_index
; i
<= last_index
; i
++) {
6654 if (total_read
% ra
->ra_pages
== 0) {
6655 btrfs_force_ra(inode
->i_mapping
, ra
, NULL
, i
,
6656 calc_ra(i
, last_index
, ra
->ra_pages
));
6660 if (((u64
)i
<< PAGE_CACHE_SHIFT
) > i_size_read(inode
))
6662 page
= grab_cache_page(inode
->i_mapping
, i
);
6667 if (!PageUptodate(page
)) {
6668 btrfs_readpage(NULL
, page
);
6670 if (!PageUptodate(page
)) {
6672 page_cache_release(page
);
6677 wait_on_page_writeback(page
);
6679 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
6680 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
6681 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
6683 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
6685 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
6687 page_cache_release(page
);
6688 btrfs_start_ordered_extent(inode
, ordered
, 1);
6689 btrfs_put_ordered_extent(ordered
);
6692 set_page_extent_mapped(page
);
6694 if (i
== first_index
)
6695 set_extent_bits(io_tree
, page_start
, page_end
,
6696 EXTENT_BOUNDARY
, GFP_NOFS
);
6697 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
6699 set_page_dirty(page
);
6702 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
6704 page_cache_release(page
);
6709 mutex_unlock(&inode
->i_mutex
);
6710 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, total_dirty
);
6714 static noinline
int relocate_data_extent(struct inode
*reloc_inode
,
6715 struct btrfs_key
*extent_key
,
6718 struct btrfs_root
*root
= BTRFS_I(reloc_inode
)->root
;
6719 struct extent_map_tree
*em_tree
= &BTRFS_I(reloc_inode
)->extent_tree
;
6720 struct extent_map
*em
;
6721 u64 start
= extent_key
->objectid
- offset
;
6722 u64 end
= start
+ extent_key
->offset
- 1;
6724 em
= alloc_extent_map(GFP_NOFS
);
6728 em
->len
= extent_key
->offset
;
6729 em
->block_len
= extent_key
->offset
;
6730 em
->block_start
= extent_key
->objectid
;
6731 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6732 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
6734 /* setup extent map to cheat btrfs_readpage */
6735 lock_extent(&BTRFS_I(reloc_inode
)->io_tree
, start
, end
, GFP_NOFS
);
6738 write_lock(&em_tree
->lock
);
6739 ret
= add_extent_mapping(em_tree
, em
);
6740 write_unlock(&em_tree
->lock
);
6741 if (ret
!= -EEXIST
) {
6742 free_extent_map(em
);
6745 btrfs_drop_extent_cache(reloc_inode
, start
, end
, 0);
6747 unlock_extent(&BTRFS_I(reloc_inode
)->io_tree
, start
, end
, GFP_NOFS
);
6749 return relocate_inode_pages(reloc_inode
, start
, extent_key
->offset
);
6752 struct btrfs_ref_path
{
6754 u64 nodes
[BTRFS_MAX_LEVEL
];
6756 u64 root_generation
;
6763 struct btrfs_key node_keys
[BTRFS_MAX_LEVEL
];
6764 u64 new_nodes
[BTRFS_MAX_LEVEL
];
6767 struct disk_extent
{
6778 static int is_cowonly_root(u64 root_objectid
)
6780 if (root_objectid
== BTRFS_ROOT_TREE_OBJECTID
||
6781 root_objectid
== BTRFS_EXTENT_TREE_OBJECTID
||
6782 root_objectid
== BTRFS_CHUNK_TREE_OBJECTID
||
6783 root_objectid
== BTRFS_DEV_TREE_OBJECTID
||
6784 root_objectid
== BTRFS_TREE_LOG_OBJECTID
||
6785 root_objectid
== BTRFS_CSUM_TREE_OBJECTID
)
6790 static noinline
int __next_ref_path(struct btrfs_trans_handle
*trans
,
6791 struct btrfs_root
*extent_root
,
6792 struct btrfs_ref_path
*ref_path
,
6795 struct extent_buffer
*leaf
;
6796 struct btrfs_path
*path
;
6797 struct btrfs_extent_ref
*ref
;
6798 struct btrfs_key key
;
6799 struct btrfs_key found_key
;
6805 path
= btrfs_alloc_path();
6810 ref_path
->lowest_level
= -1;
6811 ref_path
->current_level
= -1;
6812 ref_path
->shared_level
= -1;
6816 level
= ref_path
->current_level
- 1;
6817 while (level
>= -1) {
6819 if (level
< ref_path
->lowest_level
)
6823 bytenr
= ref_path
->nodes
[level
];
6825 bytenr
= ref_path
->extent_start
;
6826 BUG_ON(bytenr
== 0);
6828 parent
= ref_path
->nodes
[level
+ 1];
6829 ref_path
->nodes
[level
+ 1] = 0;
6830 ref_path
->current_level
= level
;
6831 BUG_ON(parent
== 0);
6833 key
.objectid
= bytenr
;
6834 key
.offset
= parent
+ 1;
6835 key
.type
= BTRFS_EXTENT_REF_KEY
;
6837 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
, 0, 0);
6842 leaf
= path
->nodes
[0];
6843 nritems
= btrfs_header_nritems(leaf
);
6844 if (path
->slots
[0] >= nritems
) {
6845 ret
= btrfs_next_leaf(extent_root
, path
);
6850 leaf
= path
->nodes
[0];
6853 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6854 if (found_key
.objectid
== bytenr
&&
6855 found_key
.type
== BTRFS_EXTENT_REF_KEY
) {
6856 if (level
< ref_path
->shared_level
)
6857 ref_path
->shared_level
= level
;
6862 btrfs_release_path(extent_root
, path
);
6865 /* reached lowest level */
6869 level
= ref_path
->current_level
;
6870 while (level
< BTRFS_MAX_LEVEL
- 1) {
6874 bytenr
= ref_path
->nodes
[level
];
6876 bytenr
= ref_path
->extent_start
;
6878 BUG_ON(bytenr
== 0);
6880 key
.objectid
= bytenr
;
6882 key
.type
= BTRFS_EXTENT_REF_KEY
;
6884 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
, 0, 0);
6888 leaf
= path
->nodes
[0];
6889 nritems
= btrfs_header_nritems(leaf
);
6890 if (path
->slots
[0] >= nritems
) {
6891 ret
= btrfs_next_leaf(extent_root
, path
);
6895 /* the extent was freed by someone */
6896 if (ref_path
->lowest_level
== level
)
6898 btrfs_release_path(extent_root
, path
);
6901 leaf
= path
->nodes
[0];
6904 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6905 if (found_key
.objectid
!= bytenr
||
6906 found_key
.type
!= BTRFS_EXTENT_REF_KEY
) {
6907 /* the extent was freed by someone */
6908 if (ref_path
->lowest_level
== level
) {
6912 btrfs_release_path(extent_root
, path
);
6916 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
6917 struct btrfs_extent_ref
);
6918 ref_objectid
= btrfs_ref_objectid(leaf
, ref
);
6919 if (ref_objectid
< BTRFS_FIRST_FREE_OBJECTID
) {
6921 level
= (int)ref_objectid
;
6922 BUG_ON(level
>= BTRFS_MAX_LEVEL
);
6923 ref_path
->lowest_level
= level
;
6924 ref_path
->current_level
= level
;
6925 ref_path
->nodes
[level
] = bytenr
;
6927 WARN_ON(ref_objectid
!= level
);
6930 WARN_ON(level
!= -1);
6934 if (ref_path
->lowest_level
== level
) {
6935 ref_path
->owner_objectid
= ref_objectid
;
6936 ref_path
->num_refs
= btrfs_ref_num_refs(leaf
, ref
);
6940 * the block is tree root or the block isn't in reference
6943 if (found_key
.objectid
== found_key
.offset
||
6944 is_cowonly_root(btrfs_ref_root(leaf
, ref
))) {
6945 ref_path
->root_objectid
= btrfs_ref_root(leaf
, ref
);
6946 ref_path
->root_generation
=
6947 btrfs_ref_generation(leaf
, ref
);
6949 /* special reference from the tree log */
6950 ref_path
->nodes
[0] = found_key
.offset
;
6951 ref_path
->current_level
= 0;
6958 BUG_ON(ref_path
->nodes
[level
] != 0);
6959 ref_path
->nodes
[level
] = found_key
.offset
;
6960 ref_path
->current_level
= level
;
6963 * the reference was created in the running transaction,
6964 * no need to continue walking up.
6966 if (btrfs_ref_generation(leaf
, ref
) == trans
->transid
) {
6967 ref_path
->root_objectid
= btrfs_ref_root(leaf
, ref
);
6968 ref_path
->root_generation
=
6969 btrfs_ref_generation(leaf
, ref
);
6974 btrfs_release_path(extent_root
, path
);
6977 /* reached max tree level, but no tree root found. */
6980 btrfs_free_path(path
);
6984 static int btrfs_first_ref_path(struct btrfs_trans_handle
*trans
,
6985 struct btrfs_root
*extent_root
,
6986 struct btrfs_ref_path
*ref_path
,
6989 memset(ref_path
, 0, sizeof(*ref_path
));
6990 ref_path
->extent_start
= extent_start
;
6992 return __next_ref_path(trans
, extent_root
, ref_path
, 1);
6995 static int btrfs_next_ref_path(struct btrfs_trans_handle
*trans
,
6996 struct btrfs_root
*extent_root
,
6997 struct btrfs_ref_path
*ref_path
)
6999 return __next_ref_path(trans
, extent_root
, ref_path
, 0);
7002 static noinline
int get_new_locations(struct inode
*reloc_inode
,
7003 struct btrfs_key
*extent_key
,
7004 u64 offset
, int no_fragment
,
7005 struct disk_extent
**extents
,
7008 struct btrfs_root
*root
= BTRFS_I(reloc_inode
)->root
;
7009 struct btrfs_path
*path
;
7010 struct btrfs_file_extent_item
*fi
;
7011 struct extent_buffer
*leaf
;
7012 struct disk_extent
*exts
= *extents
;
7013 struct btrfs_key found_key
;
7018 int max
= *nr_extents
;
7021 WARN_ON(!no_fragment
&& *extents
);
7024 exts
= kmalloc(sizeof(*exts
) * max
, GFP_NOFS
);
7029 path
= btrfs_alloc_path();
7031 if (exts
!= *extents
)
7036 cur_pos
= extent_key
->objectid
- offset
;
7037 last_byte
= extent_key
->objectid
+ extent_key
->offset
;
7038 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, reloc_inode
->i_ino
,
7048 leaf
= path
->nodes
[0];
7049 nritems
= btrfs_header_nritems(leaf
);
7050 if (path
->slots
[0] >= nritems
) {
7051 ret
= btrfs_next_leaf(root
, path
);
7056 leaf
= path
->nodes
[0];
7059 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
7060 if (found_key
.offset
!= cur_pos
||
7061 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
||
7062 found_key
.objectid
!= reloc_inode
->i_ino
)
7065 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
7066 struct btrfs_file_extent_item
);
7067 if (btrfs_file_extent_type(leaf
, fi
) !=
7068 BTRFS_FILE_EXTENT_REG
||
7069 btrfs_file_extent_disk_bytenr(leaf
, fi
) == 0)
7073 struct disk_extent
*old
= exts
;
7075 exts
= kzalloc(sizeof(*exts
) * max
, GFP_NOFS
);
7080 memcpy(exts
, old
, sizeof(*exts
) * nr
);
7081 if (old
!= *extents
)
7085 exts
[nr
].disk_bytenr
=
7086 btrfs_file_extent_disk_bytenr(leaf
, fi
);
7087 exts
[nr
].disk_num_bytes
=
7088 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
7089 exts
[nr
].offset
= btrfs_file_extent_offset(leaf
, fi
);
7090 exts
[nr
].num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
7091 exts
[nr
].ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
7092 exts
[nr
].compression
= btrfs_file_extent_compression(leaf
, fi
);
7093 exts
[nr
].encryption
= btrfs_file_extent_encryption(leaf
, fi
);
7094 exts
[nr
].other_encoding
= btrfs_file_extent_other_encoding(leaf
,
7096 BUG_ON(exts
[nr
].offset
> 0);
7097 BUG_ON(exts
[nr
].compression
|| exts
[nr
].encryption
);
7098 BUG_ON(exts
[nr
].num_bytes
!= exts
[nr
].disk_num_bytes
);
7100 cur_pos
+= exts
[nr
].num_bytes
;
7103 if (cur_pos
+ offset
>= last_byte
)
7113 BUG_ON(cur_pos
+ offset
> last_byte
);
7114 if (cur_pos
+ offset
< last_byte
) {
7120 btrfs_free_path(path
);
7122 if (exts
!= *extents
)
7131 static noinline
int replace_one_extent(struct btrfs_trans_handle
*trans
,
7132 struct btrfs_root
*root
,
7133 struct btrfs_path
*path
,
7134 struct btrfs_key
*extent_key
,
7135 struct btrfs_key
*leaf_key
,
7136 struct btrfs_ref_path
*ref_path
,
7137 struct disk_extent
*new_extents
,
7140 struct extent_buffer
*leaf
;
7141 struct btrfs_file_extent_item
*fi
;
7142 struct inode
*inode
= NULL
;
7143 struct btrfs_key key
;
7148 u64 search_end
= (u64
)-1;
7151 int extent_locked
= 0;
7155 memcpy(&key
, leaf_key
, sizeof(key
));
7156 if (ref_path
->owner_objectid
!= BTRFS_MULTIPLE_OBJECTIDS
) {
7157 if (key
.objectid
< ref_path
->owner_objectid
||
7158 (key
.objectid
== ref_path
->owner_objectid
&&
7159 key
.type
< BTRFS_EXTENT_DATA_KEY
)) {
7160 key
.objectid
= ref_path
->owner_objectid
;
7161 key
.type
= BTRFS_EXTENT_DATA_KEY
;
7167 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
7171 leaf
= path
->nodes
[0];
7172 nritems
= btrfs_header_nritems(leaf
);
7174 if (extent_locked
&& ret
> 0) {
7176 * the file extent item was modified by someone
7177 * before the extent got locked.
7179 unlock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
7180 lock_end
, GFP_NOFS
);
7184 if (path
->slots
[0] >= nritems
) {
7185 if (++nr_scaned
> 2)
7188 BUG_ON(extent_locked
);
7189 ret
= btrfs_next_leaf(root
, path
);
7194 leaf
= path
->nodes
[0];
7195 nritems
= btrfs_header_nritems(leaf
);
7198 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
7200 if (ref_path
->owner_objectid
!= BTRFS_MULTIPLE_OBJECTIDS
) {
7201 if ((key
.objectid
> ref_path
->owner_objectid
) ||
7202 (key
.objectid
== ref_path
->owner_objectid
&&
7203 key
.type
> BTRFS_EXTENT_DATA_KEY
) ||
7204 key
.offset
>= search_end
)
7208 if (inode
&& key
.objectid
!= inode
->i_ino
) {
7209 BUG_ON(extent_locked
);
7210 btrfs_release_path(root
, path
);
7211 mutex_unlock(&inode
->i_mutex
);
7217 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
7222 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
7223 struct btrfs_file_extent_item
);
7224 extent_type
= btrfs_file_extent_type(leaf
, fi
);
7225 if ((extent_type
!= BTRFS_FILE_EXTENT_REG
&&
7226 extent_type
!= BTRFS_FILE_EXTENT_PREALLOC
) ||
7227 (btrfs_file_extent_disk_bytenr(leaf
, fi
) !=
7228 extent_key
->objectid
)) {
7234 num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
7235 ext_offset
= btrfs_file_extent_offset(leaf
, fi
);
7237 if (search_end
== (u64
)-1) {
7238 search_end
= key
.offset
- ext_offset
+
7239 btrfs_file_extent_ram_bytes(leaf
, fi
);
7242 if (!extent_locked
) {
7243 lock_start
= key
.offset
;
7244 lock_end
= lock_start
+ num_bytes
- 1;
7246 if (lock_start
> key
.offset
||
7247 lock_end
+ 1 < key
.offset
+ num_bytes
) {
7248 unlock_extent(&BTRFS_I(inode
)->io_tree
,
7249 lock_start
, lock_end
, GFP_NOFS
);
7255 btrfs_release_path(root
, path
);
7257 inode
= btrfs_iget_locked(root
->fs_info
->sb
,
7258 key
.objectid
, root
);
7259 if (inode
->i_state
& I_NEW
) {
7260 BTRFS_I(inode
)->root
= root
;
7261 BTRFS_I(inode
)->location
.objectid
=
7263 BTRFS_I(inode
)->location
.type
=
7264 BTRFS_INODE_ITEM_KEY
;
7265 BTRFS_I(inode
)->location
.offset
= 0;
7266 btrfs_read_locked_inode(inode
);
7267 unlock_new_inode(inode
);
7270 * some code call btrfs_commit_transaction while
7271 * holding the i_mutex, so we can't use mutex_lock
7274 if (is_bad_inode(inode
) ||
7275 !mutex_trylock(&inode
->i_mutex
)) {
7278 key
.offset
= (u64
)-1;
7283 if (!extent_locked
) {
7284 struct btrfs_ordered_extent
*ordered
;
7286 btrfs_release_path(root
, path
);
7288 lock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
7289 lock_end
, GFP_NOFS
);
7290 ordered
= btrfs_lookup_first_ordered_extent(inode
,
7293 ordered
->file_offset
<= lock_end
&&
7294 ordered
->file_offset
+ ordered
->len
> lock_start
) {
7295 unlock_extent(&BTRFS_I(inode
)->io_tree
,
7296 lock_start
, lock_end
, GFP_NOFS
);
7297 btrfs_start_ordered_extent(inode
, ordered
, 1);
7298 btrfs_put_ordered_extent(ordered
);
7299 key
.offset
+= num_bytes
;
7303 btrfs_put_ordered_extent(ordered
);
7309 if (nr_extents
== 1) {
7310 /* update extent pointer in place */
7311 btrfs_set_file_extent_disk_bytenr(leaf
, fi
,
7312 new_extents
[0].disk_bytenr
);
7313 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
,
7314 new_extents
[0].disk_num_bytes
);
7315 btrfs_mark_buffer_dirty(leaf
);
7317 btrfs_drop_extent_cache(inode
, key
.offset
,
7318 key
.offset
+ num_bytes
- 1, 0);
7320 ret
= btrfs_inc_extent_ref(trans
, root
,
7321 new_extents
[0].disk_bytenr
,
7322 new_extents
[0].disk_num_bytes
,
7324 root
->root_key
.objectid
,
7329 ret
= btrfs_free_extent(trans
, root
,
7330 extent_key
->objectid
,
7333 btrfs_header_owner(leaf
),
7334 btrfs_header_generation(leaf
),
7338 btrfs_release_path(root
, path
);
7339 key
.offset
+= num_bytes
;
7347 * drop old extent pointer at first, then insert the
7348 * new pointers one bye one
7350 btrfs_release_path(root
, path
);
7351 ret
= btrfs_drop_extents(trans
, root
, inode
, key
.offset
,
7352 key
.offset
+ num_bytes
,
7353 key
.offset
, &alloc_hint
);
7356 for (i
= 0; i
< nr_extents
; i
++) {
7357 if (ext_offset
>= new_extents
[i
].num_bytes
) {
7358 ext_offset
-= new_extents
[i
].num_bytes
;
7361 extent_len
= min(new_extents
[i
].num_bytes
-
7362 ext_offset
, num_bytes
);
7364 ret
= btrfs_insert_empty_item(trans
, root
,
7369 leaf
= path
->nodes
[0];
7370 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
7371 struct btrfs_file_extent_item
);
7372 btrfs_set_file_extent_generation(leaf
, fi
,
7374 btrfs_set_file_extent_type(leaf
, fi
,
7375 BTRFS_FILE_EXTENT_REG
);
7376 btrfs_set_file_extent_disk_bytenr(leaf
, fi
,
7377 new_extents
[i
].disk_bytenr
);
7378 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
,
7379 new_extents
[i
].disk_num_bytes
);
7380 btrfs_set_file_extent_ram_bytes(leaf
, fi
,
7381 new_extents
[i
].ram_bytes
);
7383 btrfs_set_file_extent_compression(leaf
, fi
,
7384 new_extents
[i
].compression
);
7385 btrfs_set_file_extent_encryption(leaf
, fi
,
7386 new_extents
[i
].encryption
);
7387 btrfs_set_file_extent_other_encoding(leaf
, fi
,
7388 new_extents
[i
].other_encoding
);
7390 btrfs_set_file_extent_num_bytes(leaf
, fi
,
7392 ext_offset
+= new_extents
[i
].offset
;
7393 btrfs_set_file_extent_offset(leaf
, fi
,
7395 btrfs_mark_buffer_dirty(leaf
);
7397 btrfs_drop_extent_cache(inode
, key
.offset
,
7398 key
.offset
+ extent_len
- 1, 0);
7400 ret
= btrfs_inc_extent_ref(trans
, root
,
7401 new_extents
[i
].disk_bytenr
,
7402 new_extents
[i
].disk_num_bytes
,
7404 root
->root_key
.objectid
,
7405 trans
->transid
, key
.objectid
);
7407 btrfs_release_path(root
, path
);
7409 inode_add_bytes(inode
, extent_len
);
7412 num_bytes
-= extent_len
;
7413 key
.offset
+= extent_len
;
7418 BUG_ON(i
>= nr_extents
);
7422 if (extent_locked
) {
7423 unlock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
7424 lock_end
, GFP_NOFS
);
7428 if (ref_path
->owner_objectid
!= BTRFS_MULTIPLE_OBJECTIDS
&&
7429 key
.offset
>= search_end
)
7436 btrfs_release_path(root
, path
);
7438 mutex_unlock(&inode
->i_mutex
);
7439 if (extent_locked
) {
7440 unlock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
7441 lock_end
, GFP_NOFS
);
7448 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle
*trans
,
7449 struct btrfs_root
*root
,
7450 struct extent_buffer
*buf
, u64 orig_start
)
7455 BUG_ON(btrfs_header_generation(buf
) != trans
->transid
);
7456 BUG_ON(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
7458 level
= btrfs_header_level(buf
);
7460 struct btrfs_leaf_ref
*ref
;
7461 struct btrfs_leaf_ref
*orig_ref
;
7463 orig_ref
= btrfs_lookup_leaf_ref(root
, orig_start
);
7467 ref
= btrfs_alloc_leaf_ref(root
, orig_ref
->nritems
);
7469 btrfs_free_leaf_ref(root
, orig_ref
);
7473 ref
->nritems
= orig_ref
->nritems
;
7474 memcpy(ref
->extents
, orig_ref
->extents
,
7475 sizeof(ref
->extents
[0]) * ref
->nritems
);
7477 btrfs_free_leaf_ref(root
, orig_ref
);
7479 ref
->root_gen
= trans
->transid
;
7480 ref
->bytenr
= buf
->start
;
7481 ref
->owner
= btrfs_header_owner(buf
);
7482 ref
->generation
= btrfs_header_generation(buf
);
7484 ret
= btrfs_add_leaf_ref(root
, ref
, 0);
7486 btrfs_free_leaf_ref(root
, ref
);
7491 static noinline
int invalidate_extent_cache(struct btrfs_root
*root
,
7492 struct extent_buffer
*leaf
,
7493 struct btrfs_block_group_cache
*group
,
7494 struct btrfs_root
*target_root
)
7496 struct btrfs_key key
;
7497 struct inode
*inode
= NULL
;
7498 struct btrfs_file_extent_item
*fi
;
7499 struct extent_state
*cached_state
= NULL
;
7501 u64 skip_objectid
= 0;
7505 nritems
= btrfs_header_nritems(leaf
);
7506 for (i
= 0; i
< nritems
; i
++) {
7507 btrfs_item_key_to_cpu(leaf
, &key
, i
);
7508 if (key
.objectid
== skip_objectid
||
7509 key
.type
!= BTRFS_EXTENT_DATA_KEY
)
7511 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
7512 if (btrfs_file_extent_type(leaf
, fi
) ==
7513 BTRFS_FILE_EXTENT_INLINE
)
7515 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) == 0)
7517 if (!inode
|| inode
->i_ino
!= key
.objectid
) {
7519 inode
= btrfs_ilookup(target_root
->fs_info
->sb
,
7520 key
.objectid
, target_root
, 1);
7523 skip_objectid
= key
.objectid
;
7526 num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
7528 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, key
.offset
,
7529 key
.offset
+ num_bytes
- 1, 0, &cached_state
,
7531 btrfs_drop_extent_cache(inode
, key
.offset
,
7532 key
.offset
+ num_bytes
- 1, 1);
7533 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, key
.offset
,
7534 key
.offset
+ num_bytes
- 1, &cached_state
,
7542 static noinline
int replace_extents_in_leaf(struct btrfs_trans_handle
*trans
,
7543 struct btrfs_root
*root
,
7544 struct extent_buffer
*leaf
,
7545 struct btrfs_block_group_cache
*group
,
7546 struct inode
*reloc_inode
)
7548 struct btrfs_key key
;
7549 struct btrfs_key extent_key
;
7550 struct btrfs_file_extent_item
*fi
;
7551 struct btrfs_leaf_ref
*ref
;
7552 struct disk_extent
*new_extent
;
7561 new_extent
= kmalloc(sizeof(*new_extent
), GFP_NOFS
);
7565 ref
= btrfs_lookup_leaf_ref(root
, leaf
->start
);
7569 nritems
= btrfs_header_nritems(leaf
);
7570 for (i
= 0; i
< nritems
; i
++) {
7571 btrfs_item_key_to_cpu(leaf
, &key
, i
);
7572 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
7574 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
7575 if (btrfs_file_extent_type(leaf
, fi
) ==
7576 BTRFS_FILE_EXTENT_INLINE
)
7578 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
7579 num_bytes
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
7584 if (bytenr
>= group
->key
.objectid
+ group
->key
.offset
||
7585 bytenr
+ num_bytes
<= group
->key
.objectid
)
7588 extent_key
.objectid
= bytenr
;
7589 extent_key
.offset
= num_bytes
;
7590 extent_key
.type
= BTRFS_EXTENT_ITEM_KEY
;
7592 ret
= get_new_locations(reloc_inode
, &extent_key
,
7593 group
->key
.objectid
, 1,
7594 &new_extent
, &nr_extent
);
7599 BUG_ON(ref
->extents
[ext_index
].bytenr
!= bytenr
);
7600 BUG_ON(ref
->extents
[ext_index
].num_bytes
!= num_bytes
);
7601 ref
->extents
[ext_index
].bytenr
= new_extent
->disk_bytenr
;
7602 ref
->extents
[ext_index
].num_bytes
= new_extent
->disk_num_bytes
;
7604 btrfs_set_file_extent_disk_bytenr(leaf
, fi
,
7605 new_extent
->disk_bytenr
);
7606 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
,
7607 new_extent
->disk_num_bytes
);
7608 btrfs_mark_buffer_dirty(leaf
);
7610 ret
= btrfs_inc_extent_ref(trans
, root
,
7611 new_extent
->disk_bytenr
,
7612 new_extent
->disk_num_bytes
,
7614 root
->root_key
.objectid
,
7615 trans
->transid
, key
.objectid
);
7618 ret
= btrfs_free_extent(trans
, root
,
7619 bytenr
, num_bytes
, leaf
->start
,
7620 btrfs_header_owner(leaf
),
7621 btrfs_header_generation(leaf
),
7627 BUG_ON(ext_index
+ 1 != ref
->nritems
);
7628 btrfs_free_leaf_ref(root
, ref
);
7632 int btrfs_free_reloc_root(struct btrfs_trans_handle
*trans
,
7633 struct btrfs_root
*root
)
7635 struct btrfs_root
*reloc_root
;
7638 if (root
->reloc_root
) {
7639 reloc_root
= root
->reloc_root
;
7640 root
->reloc_root
= NULL
;
7641 list_add(&reloc_root
->dead_list
,
7642 &root
->fs_info
->dead_reloc_roots
);
7644 btrfs_set_root_bytenr(&reloc_root
->root_item
,
7645 reloc_root
->node
->start
);
7646 btrfs_set_root_level(&root
->root_item
,
7647 btrfs_header_level(reloc_root
->node
));
7648 memset(&reloc_root
->root_item
.drop_progress
, 0,
7649 sizeof(struct btrfs_disk_key
));
7650 reloc_root
->root_item
.drop_level
= 0;
7652 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
7653 &reloc_root
->root_key
,
7654 &reloc_root
->root_item
);
7660 int btrfs_drop_dead_reloc_roots(struct btrfs_root
*root
)
7662 struct btrfs_trans_handle
*trans
;
7663 struct btrfs_root
*reloc_root
;
7664 struct btrfs_root
*prev_root
= NULL
;
7665 struct list_head dead_roots
;
7669 INIT_LIST_HEAD(&dead_roots
);
7670 list_splice_init(&root
->fs_info
->dead_reloc_roots
, &dead_roots
);
7672 while (!list_empty(&dead_roots
)) {
7673 reloc_root
= list_entry(dead_roots
.prev
,
7674 struct btrfs_root
, dead_list
);
7675 list_del_init(&reloc_root
->dead_list
);
7677 BUG_ON(reloc_root
->commit_root
!= NULL
);
7679 trans
= btrfs_join_transaction(root
);
7680 BUG_ON(IS_ERR(trans
));
7682 mutex_lock(&root
->fs_info
->drop_mutex
);
7683 ret
= btrfs_drop_snapshot(trans
, reloc_root
);
7686 mutex_unlock(&root
->fs_info
->drop_mutex
);
7688 nr
= trans
->blocks_used
;
7689 ret
= btrfs_end_transaction(trans
, root
);
7691 btrfs_btree_balance_dirty(root
, nr
);
7694 free_extent_buffer(reloc_root
->node
);
7696 ret
= btrfs_del_root(trans
, root
->fs_info
->tree_root
,
7697 &reloc_root
->root_key
);
7699 mutex_unlock(&root
->fs_info
->drop_mutex
);
7701 nr
= trans
->blocks_used
;
7702 ret
= btrfs_end_transaction(trans
, root
);
7704 btrfs_btree_balance_dirty(root
, nr
);
7707 prev_root
= reloc_root
;
7710 btrfs_remove_leaf_refs(prev_root
, (u64
)-1, 0);
7716 int btrfs_add_dead_reloc_root(struct btrfs_root
*root
)
7718 list_add(&root
->dead_list
, &root
->fs_info
->dead_reloc_roots
);
7722 int btrfs_cleanup_reloc_trees(struct btrfs_root
*root
)
7724 struct btrfs_root
*reloc_root
;
7725 struct btrfs_trans_handle
*trans
;
7726 struct btrfs_key location
;
7730 mutex_lock(&root
->fs_info
->tree_reloc_mutex
);
7731 ret
= btrfs_find_dead_roots(root
, BTRFS_TREE_RELOC_OBJECTID
, NULL
);
7733 found
= !list_empty(&root
->fs_info
->dead_reloc_roots
);
7734 mutex_unlock(&root
->fs_info
->tree_reloc_mutex
);
7737 trans
= btrfs_start_transaction(root
, 1);
7738 BUG_ON(IS_ERR(trans
));
7739 ret
= btrfs_commit_transaction(trans
, root
);
7743 location
.objectid
= BTRFS_DATA_RELOC_TREE_OBJECTID
;
7744 location
.offset
= (u64
)-1;
7745 location
.type
= BTRFS_ROOT_ITEM_KEY
;
7747 reloc_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
7748 BUG_ON(!reloc_root
);
7749 ret
= btrfs_orphan_cleanup(reloc_root
);
7754 static noinline
int init_reloc_tree(struct btrfs_trans_handle
*trans
,
7755 struct btrfs_root
*root
)
7757 struct btrfs_root
*reloc_root
;
7758 struct extent_buffer
*eb
;
7759 struct btrfs_root_item
*root_item
;
7760 struct btrfs_key root_key
;
7763 BUG_ON(!root
->ref_cows
);
7764 if (root
->reloc_root
)
7767 root_item
= kmalloc(sizeof(*root_item
), GFP_NOFS
);
7771 ret
= btrfs_copy_root(trans
, root
, root
->commit_root
,
7772 &eb
, BTRFS_TREE_RELOC_OBJECTID
);
7775 root_key
.objectid
= BTRFS_TREE_RELOC_OBJECTID
;
7776 root_key
.offset
= root
->root_key
.objectid
;
7777 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
7779 memcpy(root_item
, &root
->root_item
, sizeof(root_item
));
7780 btrfs_set_root_refs(root_item
, 0);
7781 btrfs_set_root_bytenr(root_item
, eb
->start
);
7782 btrfs_set_root_level(root_item
, btrfs_header_level(eb
));
7783 btrfs_set_root_generation(root_item
, trans
->transid
);
7785 btrfs_tree_unlock(eb
);
7786 free_extent_buffer(eb
);
7788 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
,
7789 &root_key
, root_item
);
7793 reloc_root
= btrfs_read_fs_root_no_radix(root
->fs_info
->tree_root
,
7795 BUG_ON(IS_ERR(reloc_root
));
7796 reloc_root
->last_trans
= trans
->transid
;
7797 reloc_root
->commit_root
= NULL
;
7798 reloc_root
->ref_tree
= &root
->fs_info
->reloc_ref_tree
;
7800 root
->reloc_root
= reloc_root
;
7805 * Core function of space balance.
7807 * The idea is using reloc trees to relocate tree blocks in reference
7808 * counted roots. There is one reloc tree for each subvol, and all
7809 * reloc trees share same root key objectid. Reloc trees are snapshots
7810 * of the latest committed roots of subvols (root->commit_root).
7812 * To relocate a tree block referenced by a subvol, there are two steps.
7813 * COW the block through subvol's reloc tree, then update block pointer
7814 * in the subvol to point to the new block. Since all reloc trees share
7815 * same root key objectid, doing special handing for tree blocks owned
7816 * by them is easy. Once a tree block has been COWed in one reloc tree,
7817 * we can use the resulting new block directly when the same block is
7818 * required to COW again through other reloc trees. By this way, relocated
7819 * tree blocks are shared between reloc trees, so they are also shared
7822 static noinline
int relocate_one_path(struct btrfs_trans_handle
*trans
,
7823 struct btrfs_root
*root
,
7824 struct btrfs_path
*path
,
7825 struct btrfs_key
*first_key
,
7826 struct btrfs_ref_path
*ref_path
,
7827 struct btrfs_block_group_cache
*group
,
7828 struct inode
*reloc_inode
)
7830 struct btrfs_root
*reloc_root
;
7831 struct extent_buffer
*eb
= NULL
;
7832 struct btrfs_key
*keys
;
7836 int lowest_level
= 0;
7839 if (ref_path
->owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
)
7840 lowest_level
= ref_path
->owner_objectid
;
7842 if (!root
->ref_cows
) {
7843 path
->lowest_level
= lowest_level
;
7844 ret
= btrfs_search_slot(trans
, root
, first_key
, path
, 0, 1);
7846 path
->lowest_level
= 0;
7847 btrfs_release_path(root
, path
);
7851 mutex_lock(&root
->fs_info
->tree_reloc_mutex
);
7852 ret
= init_reloc_tree(trans
, root
);
7854 reloc_root
= root
->reloc_root
;
7856 shared_level
= ref_path
->shared_level
;
7857 ref_path
->shared_level
= BTRFS_MAX_LEVEL
- 1;
7859 keys
= ref_path
->node_keys
;
7860 nodes
= ref_path
->new_nodes
;
7861 memset(&keys
[shared_level
+ 1], 0,
7862 sizeof(*keys
) * (BTRFS_MAX_LEVEL
- shared_level
- 1));
7863 memset(&nodes
[shared_level
+ 1], 0,
7864 sizeof(*nodes
) * (BTRFS_MAX_LEVEL
- shared_level
- 1));
7866 if (nodes
[lowest_level
] == 0) {
7867 path
->lowest_level
= lowest_level
;
7868 ret
= btrfs_search_slot(trans
, reloc_root
, first_key
, path
,
7871 for (level
= lowest_level
; level
< BTRFS_MAX_LEVEL
; level
++) {
7872 eb
= path
->nodes
[level
];
7873 if (!eb
|| eb
== reloc_root
->node
)
7875 nodes
[level
] = eb
->start
;
7877 btrfs_item_key_to_cpu(eb
, &keys
[level
], 0);
7879 btrfs_node_key_to_cpu(eb
, &keys
[level
], 0);
7882 ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
7883 eb
= path
->nodes
[0];
7884 ret
= replace_extents_in_leaf(trans
, reloc_root
, eb
,
7885 group
, reloc_inode
);
7888 btrfs_release_path(reloc_root
, path
);
7890 ret
= btrfs_merge_path(trans
, reloc_root
, keys
, nodes
,
7896 * replace tree blocks in the fs tree with tree blocks in
7899 ret
= btrfs_merge_path(trans
, root
, keys
, nodes
, lowest_level
);
7902 if (ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
7903 ret
= btrfs_search_slot(trans
, reloc_root
, first_key
, path
,
7906 extent_buffer_get(path
->nodes
[0]);
7907 eb
= path
->nodes
[0];
7908 btrfs_release_path(reloc_root
, path
);
7909 ret
= invalidate_extent_cache(reloc_root
, eb
, group
, root
);
7911 free_extent_buffer(eb
);
7914 mutex_unlock(&root
->fs_info
->tree_reloc_mutex
);
7915 path
->lowest_level
= 0;
7919 static noinline
int relocate_tree_block(struct btrfs_trans_handle
*trans
,
7920 struct btrfs_root
*root
,
7921 struct btrfs_path
*path
,
7922 struct btrfs_key
*first_key
,
7923 struct btrfs_ref_path
*ref_path
)
7927 ret
= relocate_one_path(trans
, root
, path
, first_key
,
7928 ref_path
, NULL
, NULL
);
7934 static noinline
int del_extent_zero(struct btrfs_trans_handle
*trans
,
7935 struct btrfs_root
*extent_root
,
7936 struct btrfs_path
*path
,
7937 struct btrfs_key
*extent_key
)
7941 ret
= btrfs_search_slot(trans
, extent_root
, extent_key
, path
, -1, 1);
7944 ret
= btrfs_del_item(trans
, extent_root
, path
);
7946 btrfs_release_path(extent_root
, path
);
7950 static noinline
struct btrfs_root
*read_ref_root(struct btrfs_fs_info
*fs_info
,
7951 struct btrfs_ref_path
*ref_path
)
7953 struct btrfs_key root_key
;
7955 root_key
.objectid
= ref_path
->root_objectid
;
7956 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
7957 if (is_cowonly_root(ref_path
->root_objectid
))
7958 root_key
.offset
= 0;
7960 root_key
.offset
= (u64
)-1;
7962 return btrfs_read_fs_root_no_name(fs_info
, &root_key
);
7965 static noinline
int relocate_one_extent(struct btrfs_root
*extent_root
,
7966 struct btrfs_path
*path
,
7967 struct btrfs_key
*extent_key
,
7968 struct btrfs_block_group_cache
*group
,
7969 struct inode
*reloc_inode
, int pass
)
7971 struct btrfs_trans_handle
*trans
;
7972 struct btrfs_root
*found_root
;
7973 struct btrfs_ref_path
*ref_path
= NULL
;
7974 struct disk_extent
*new_extents
= NULL
;
7979 struct btrfs_key first_key
;
7983 trans
= btrfs_start_transaction(extent_root
, 1);
7984 BUG_ON(IS_ERR(trans
));
7986 if (extent_key
->objectid
== 0) {
7987 ret
= del_extent_zero(trans
, extent_root
, path
, extent_key
);
7991 ref_path
= kmalloc(sizeof(*ref_path
), GFP_NOFS
);
7997 for (loops
= 0; ; loops
++) {
7999 ret
= btrfs_first_ref_path(trans
, extent_root
, ref_path
,
8000 extent_key
->objectid
);
8002 ret
= btrfs_next_ref_path(trans
, extent_root
, ref_path
);
8009 if (ref_path
->root_objectid
== BTRFS_TREE_LOG_OBJECTID
||
8010 ref_path
->root_objectid
== BTRFS_TREE_RELOC_OBJECTID
)
8013 found_root
= read_ref_root(extent_root
->fs_info
, ref_path
);
8014 BUG_ON(!found_root
);
8016 * for reference counted tree, only process reference paths
8017 * rooted at the latest committed root.
8019 if (found_root
->ref_cows
&&
8020 ref_path
->root_generation
!= found_root
->root_key
.offset
)
8023 if (ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
8026 * copy data extents to new locations
8028 u64 group_start
= group
->key
.objectid
;
8029 ret
= relocate_data_extent(reloc_inode
,
8038 level
= ref_path
->owner_objectid
;
8041 if (prev_block
!= ref_path
->nodes
[level
]) {
8042 struct extent_buffer
*eb
;
8043 u64 block_start
= ref_path
->nodes
[level
];
8044 u64 block_size
= btrfs_level_size(found_root
, level
);
8046 eb
= read_tree_block(found_root
, block_start
,
8052 btrfs_tree_lock(eb
);
8053 BUG_ON(level
!= btrfs_header_level(eb
));
8056 btrfs_item_key_to_cpu(eb
, &first_key
, 0);
8058 btrfs_node_key_to_cpu(eb
, &first_key
, 0);
8060 btrfs_tree_unlock(eb
);
8061 free_extent_buffer(eb
);
8062 prev_block
= block_start
;
8065 mutex_lock(&extent_root
->fs_info
->trans_mutex
);
8066 btrfs_record_root_in_trans(found_root
);
8067 mutex_unlock(&extent_root
->fs_info
->trans_mutex
);
8068 if (ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
8070 * try to update data extent references while
8071 * keeping metadata shared between snapshots.
8074 ret
= relocate_one_path(trans
, found_root
,
8075 path
, &first_key
, ref_path
,
8076 group
, reloc_inode
);
8082 * use fallback method to process the remaining
8086 u64 group_start
= group
->key
.objectid
;
8087 new_extents
= kmalloc(sizeof(*new_extents
),
8094 ret
= get_new_locations(reloc_inode
,
8102 ret
= replace_one_extent(trans
, found_root
,
8104 &first_key
, ref_path
,
8105 new_extents
, nr_extents
);
8107 ret
= relocate_tree_block(trans
, found_root
, path
,
8108 &first_key
, ref_path
);
8115 btrfs_end_transaction(trans
, extent_root
);
8122 static u64
update_block_group_flags(struct btrfs_root
*root
, u64 flags
)
8125 u64 stripped
= BTRFS_BLOCK_GROUP_RAID0
|
8126 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
;
8129 * we add in the count of missing devices because we want
8130 * to make sure that any RAID levels on a degraded FS
8131 * continue to be honored.
8133 num_devices
= root
->fs_info
->fs_devices
->rw_devices
+
8134 root
->fs_info
->fs_devices
->missing_devices
;
8136 if (num_devices
== 1) {
8137 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
8138 stripped
= flags
& ~stripped
;
8140 /* turn raid0 into single device chunks */
8141 if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
8144 /* turn mirroring into duplication */
8145 if (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
8146 BTRFS_BLOCK_GROUP_RAID10
))
8147 return stripped
| BTRFS_BLOCK_GROUP_DUP
;
8150 /* they already had raid on here, just return */
8151 if (flags
& stripped
)
8154 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
8155 stripped
= flags
& ~stripped
;
8157 /* switch duplicated blocks with raid1 */
8158 if (flags
& BTRFS_BLOCK_GROUP_DUP
)
8159 return stripped
| BTRFS_BLOCK_GROUP_RAID1
;
8161 /* turn single device chunks into raid0 */
8162 return stripped
| BTRFS_BLOCK_GROUP_RAID0
;
8167 static int set_block_group_ro(struct btrfs_block_group_cache
*cache
)
8169 struct btrfs_space_info
*sinfo
= cache
->space_info
;
8176 spin_lock(&sinfo
->lock
);
8177 spin_lock(&cache
->lock
);
8178 num_bytes
= cache
->key
.offset
- cache
->reserved
- cache
->pinned
-
8179 cache
->bytes_super
- btrfs_block_group_used(&cache
->item
);
8181 if (sinfo
->bytes_used
+ sinfo
->bytes_reserved
+ sinfo
->bytes_pinned
+
8182 sinfo
->bytes_may_use
+ sinfo
->bytes_readonly
+
8183 cache
->reserved_pinned
+ num_bytes
<= sinfo
->total_bytes
) {
8184 sinfo
->bytes_readonly
+= num_bytes
;
8185 sinfo
->bytes_reserved
+= cache
->reserved_pinned
;
8186 cache
->reserved_pinned
= 0;
8191 spin_unlock(&cache
->lock
);
8192 spin_unlock(&sinfo
->lock
);
8196 int btrfs_set_block_group_ro(struct btrfs_root
*root
,
8197 struct btrfs_block_group_cache
*cache
)
8200 struct btrfs_trans_handle
*trans
;
8206 trans
= btrfs_join_transaction(root
);
8207 BUG_ON(IS_ERR(trans
));
8209 alloc_flags
= update_block_group_flags(root
, cache
->flags
);
8210 if (alloc_flags
!= cache
->flags
)
8211 do_chunk_alloc(trans
, root
, 2 * 1024 * 1024, alloc_flags
,
8214 ret
= set_block_group_ro(cache
);
8217 alloc_flags
= get_alloc_profile(root
, cache
->space_info
->flags
);
8218 ret
= do_chunk_alloc(trans
, root
, 2 * 1024 * 1024, alloc_flags
,
8222 ret
= set_block_group_ro(cache
);
8224 btrfs_end_transaction(trans
, root
);
8228 int btrfs_force_chunk_alloc(struct btrfs_trans_handle
*trans
,
8229 struct btrfs_root
*root
, u64 type
)
8231 u64 alloc_flags
= get_alloc_profile(root
, type
);
8232 return do_chunk_alloc(trans
, root
, 2 * 1024 * 1024, alloc_flags
,
8237 * helper to account the unused space of all the readonly block group in the
8238 * list. takes mirrors into account.
8240 static u64
__btrfs_get_ro_block_group_free_space(struct list_head
*groups_list
)
8242 struct btrfs_block_group_cache
*block_group
;
8246 list_for_each_entry(block_group
, groups_list
, list
) {
8247 spin_lock(&block_group
->lock
);
8249 if (!block_group
->ro
) {
8250 spin_unlock(&block_group
->lock
);
8254 if (block_group
->flags
& (BTRFS_BLOCK_GROUP_RAID1
|
8255 BTRFS_BLOCK_GROUP_RAID10
|
8256 BTRFS_BLOCK_GROUP_DUP
))
8261 free_bytes
+= (block_group
->key
.offset
-
8262 btrfs_block_group_used(&block_group
->item
)) *
8265 spin_unlock(&block_group
->lock
);
8272 * helper to account the unused space of all the readonly block group in the
8273 * space_info. takes mirrors into account.
8275 u64
btrfs_account_ro_block_groups_free_space(struct btrfs_space_info
*sinfo
)
8280 spin_lock(&sinfo
->lock
);
8282 for(i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++)
8283 if (!list_empty(&sinfo
->block_groups
[i
]))
8284 free_bytes
+= __btrfs_get_ro_block_group_free_space(
8285 &sinfo
->block_groups
[i
]);
8287 spin_unlock(&sinfo
->lock
);
8292 int btrfs_set_block_group_rw(struct btrfs_root
*root
,
8293 struct btrfs_block_group_cache
*cache
)
8295 struct btrfs_space_info
*sinfo
= cache
->space_info
;
8300 spin_lock(&sinfo
->lock
);
8301 spin_lock(&cache
->lock
);
8302 num_bytes
= cache
->key
.offset
- cache
->reserved
- cache
->pinned
-
8303 cache
->bytes_super
- btrfs_block_group_used(&cache
->item
);
8304 sinfo
->bytes_readonly
-= num_bytes
;
8306 spin_unlock(&cache
->lock
);
8307 spin_unlock(&sinfo
->lock
);
8312 * checks to see if its even possible to relocate this block group.
8314 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
8315 * ok to go ahead and try.
8317 int btrfs_can_relocate(struct btrfs_root
*root
, u64 bytenr
)
8319 struct btrfs_block_group_cache
*block_group
;
8320 struct btrfs_space_info
*space_info
;
8321 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
8322 struct btrfs_device
*device
;
8326 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
8328 /* odd, couldn't find the block group, leave it alone */
8332 /* no bytes used, we're good */
8333 if (!btrfs_block_group_used(&block_group
->item
))
8336 space_info
= block_group
->space_info
;
8337 spin_lock(&space_info
->lock
);
8339 full
= space_info
->full
;
8342 * if this is the last block group we have in this space, we can't
8343 * relocate it unless we're able to allocate a new chunk below.
8345 * Otherwise, we need to make sure we have room in the space to handle
8346 * all of the extents from this block group. If we can, we're good
8348 if ((space_info
->total_bytes
!= block_group
->key
.offset
) &&
8349 (space_info
->bytes_used
+ space_info
->bytes_reserved
+
8350 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
8351 btrfs_block_group_used(&block_group
->item
) <
8352 space_info
->total_bytes
)) {
8353 spin_unlock(&space_info
->lock
);
8356 spin_unlock(&space_info
->lock
);
8359 * ok we don't have enough space, but maybe we have free space on our
8360 * devices to allocate new chunks for relocation, so loop through our
8361 * alloc devices and guess if we have enough space. However, if we
8362 * were marked as full, then we know there aren't enough chunks, and we
8369 mutex_lock(&root
->fs_info
->chunk_mutex
);
8370 list_for_each_entry(device
, &fs_devices
->alloc_list
, dev_alloc_list
) {
8371 u64 min_free
= btrfs_block_group_used(&block_group
->item
);
8375 * check to make sure we can actually find a chunk with enough
8376 * space to fit our block group in.
8378 if (device
->total_bytes
> device
->bytes_used
+ min_free
) {
8379 ret
= find_free_dev_extent(NULL
, device
, min_free
,
8386 mutex_unlock(&root
->fs_info
->chunk_mutex
);
8388 btrfs_put_block_group(block_group
);
8392 static int find_first_block_group(struct btrfs_root
*root
,
8393 struct btrfs_path
*path
, struct btrfs_key
*key
)
8396 struct btrfs_key found_key
;
8397 struct extent_buffer
*leaf
;
8400 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
8405 slot
= path
->slots
[0];
8406 leaf
= path
->nodes
[0];
8407 if (slot
>= btrfs_header_nritems(leaf
)) {
8408 ret
= btrfs_next_leaf(root
, path
);
8415 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
8417 if (found_key
.objectid
>= key
->objectid
&&
8418 found_key
.type
== BTRFS_BLOCK_GROUP_ITEM_KEY
) {
8428 void btrfs_put_block_group_cache(struct btrfs_fs_info
*info
)
8430 struct btrfs_block_group_cache
*block_group
;
8434 struct inode
*inode
;
8436 block_group
= btrfs_lookup_first_block_group(info
, last
);
8437 while (block_group
) {
8438 spin_lock(&block_group
->lock
);
8439 if (block_group
->iref
)
8441 spin_unlock(&block_group
->lock
);
8442 block_group
= next_block_group(info
->tree_root
,
8452 inode
= block_group
->inode
;
8453 block_group
->iref
= 0;
8454 block_group
->inode
= NULL
;
8455 spin_unlock(&block_group
->lock
);
8457 last
= block_group
->key
.objectid
+ block_group
->key
.offset
;
8458 btrfs_put_block_group(block_group
);
8462 int btrfs_free_block_groups(struct btrfs_fs_info
*info
)
8464 struct btrfs_block_group_cache
*block_group
;
8465 struct btrfs_space_info
*space_info
;
8466 struct btrfs_caching_control
*caching_ctl
;
8469 down_write(&info
->extent_commit_sem
);
8470 while (!list_empty(&info
->caching_block_groups
)) {
8471 caching_ctl
= list_entry(info
->caching_block_groups
.next
,
8472 struct btrfs_caching_control
, list
);
8473 list_del(&caching_ctl
->list
);
8474 put_caching_control(caching_ctl
);
8476 up_write(&info
->extent_commit_sem
);
8478 spin_lock(&info
->block_group_cache_lock
);
8479 while ((n
= rb_last(&info
->block_group_cache_tree
)) != NULL
) {
8480 block_group
= rb_entry(n
, struct btrfs_block_group_cache
,
8482 rb_erase(&block_group
->cache_node
,
8483 &info
->block_group_cache_tree
);
8484 spin_unlock(&info
->block_group_cache_lock
);
8486 down_write(&block_group
->space_info
->groups_sem
);
8487 list_del(&block_group
->list
);
8488 up_write(&block_group
->space_info
->groups_sem
);
8490 if (block_group
->cached
== BTRFS_CACHE_STARTED
)
8491 wait_block_group_cache_done(block_group
);
8494 * We haven't cached this block group, which means we could
8495 * possibly have excluded extents on this block group.
8497 if (block_group
->cached
== BTRFS_CACHE_NO
)
8498 free_excluded_extents(info
->extent_root
, block_group
);
8500 btrfs_remove_free_space_cache(block_group
);
8501 btrfs_put_block_group(block_group
);
8503 spin_lock(&info
->block_group_cache_lock
);
8505 spin_unlock(&info
->block_group_cache_lock
);
8507 /* now that all the block groups are freed, go through and
8508 * free all the space_info structs. This is only called during
8509 * the final stages of unmount, and so we know nobody is
8510 * using them. We call synchronize_rcu() once before we start,
8511 * just to be on the safe side.
8515 release_global_block_rsv(info
);
8517 while(!list_empty(&info
->space_info
)) {
8518 space_info
= list_entry(info
->space_info
.next
,
8519 struct btrfs_space_info
,
8521 if (space_info
->bytes_pinned
> 0 ||
8522 space_info
->bytes_reserved
> 0) {
8524 dump_space_info(space_info
, 0, 0);
8526 list_del(&space_info
->list
);
8532 static void __link_block_group(struct btrfs_space_info
*space_info
,
8533 struct btrfs_block_group_cache
*cache
)
8535 int index
= get_block_group_index(cache
);
8537 down_write(&space_info
->groups_sem
);
8538 list_add_tail(&cache
->list
, &space_info
->block_groups
[index
]);
8539 up_write(&space_info
->groups_sem
);
8542 int btrfs_read_block_groups(struct btrfs_root
*root
)
8544 struct btrfs_path
*path
;
8546 struct btrfs_block_group_cache
*cache
;
8547 struct btrfs_fs_info
*info
= root
->fs_info
;
8548 struct btrfs_space_info
*space_info
;
8549 struct btrfs_key key
;
8550 struct btrfs_key found_key
;
8551 struct extent_buffer
*leaf
;
8555 root
= info
->extent_root
;
8558 btrfs_set_key_type(&key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
8559 path
= btrfs_alloc_path();
8563 cache_gen
= btrfs_super_cache_generation(&root
->fs_info
->super_copy
);
8564 if (cache_gen
!= 0 &&
8565 btrfs_super_generation(&root
->fs_info
->super_copy
) != cache_gen
)
8567 if (btrfs_test_opt(root
, CLEAR_CACHE
))
8569 if (!btrfs_test_opt(root
, SPACE_CACHE
) && cache_gen
)
8570 printk(KERN_INFO
"btrfs: disk space caching is enabled\n");
8573 ret
= find_first_block_group(root
, path
, &key
);
8578 leaf
= path
->nodes
[0];
8579 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
8580 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
8586 atomic_set(&cache
->count
, 1);
8587 spin_lock_init(&cache
->lock
);
8588 spin_lock_init(&cache
->tree_lock
);
8589 cache
->fs_info
= info
;
8590 INIT_LIST_HEAD(&cache
->list
);
8591 INIT_LIST_HEAD(&cache
->cluster_list
);
8594 cache
->disk_cache_state
= BTRFS_DC_CLEAR
;
8597 * we only want to have 32k of ram per block group for keeping
8598 * track of free space, and if we pass 1/2 of that we want to
8599 * start converting things over to using bitmaps
8601 cache
->extents_thresh
= ((1024 * 32) / 2) /
8602 sizeof(struct btrfs_free_space
);
8604 read_extent_buffer(leaf
, &cache
->item
,
8605 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
8606 sizeof(cache
->item
));
8607 memcpy(&cache
->key
, &found_key
, sizeof(found_key
));
8609 key
.objectid
= found_key
.objectid
+ found_key
.offset
;
8610 btrfs_release_path(root
, path
);
8611 cache
->flags
= btrfs_block_group_flags(&cache
->item
);
8612 cache
->sectorsize
= root
->sectorsize
;
8615 * We need to exclude the super stripes now so that the space
8616 * info has super bytes accounted for, otherwise we'll think
8617 * we have more space than we actually do.
8619 exclude_super_stripes(root
, cache
);
8622 * check for two cases, either we are full, and therefore
8623 * don't need to bother with the caching work since we won't
8624 * find any space, or we are empty, and we can just add all
8625 * the space in and be done with it. This saves us _alot_ of
8626 * time, particularly in the full case.
8628 if (found_key
.offset
== btrfs_block_group_used(&cache
->item
)) {
8629 cache
->last_byte_to_unpin
= (u64
)-1;
8630 cache
->cached
= BTRFS_CACHE_FINISHED
;
8631 free_excluded_extents(root
, cache
);
8632 } else if (btrfs_block_group_used(&cache
->item
) == 0) {
8633 cache
->last_byte_to_unpin
= (u64
)-1;
8634 cache
->cached
= BTRFS_CACHE_FINISHED
;
8635 add_new_free_space(cache
, root
->fs_info
,
8637 found_key
.objectid
+
8639 free_excluded_extents(root
, cache
);
8642 ret
= update_space_info(info
, cache
->flags
, found_key
.offset
,
8643 btrfs_block_group_used(&cache
->item
),
8646 cache
->space_info
= space_info
;
8647 spin_lock(&cache
->space_info
->lock
);
8648 cache
->space_info
->bytes_readonly
+= cache
->bytes_super
;
8649 spin_unlock(&cache
->space_info
->lock
);
8651 __link_block_group(space_info
, cache
);
8653 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
8656 set_avail_alloc_bits(root
->fs_info
, cache
->flags
);
8657 if (btrfs_chunk_readonly(root
, cache
->key
.objectid
))
8658 set_block_group_ro(cache
);
8661 list_for_each_entry_rcu(space_info
, &root
->fs_info
->space_info
, list
) {
8662 if (!(get_alloc_profile(root
, space_info
->flags
) &
8663 (BTRFS_BLOCK_GROUP_RAID10
|
8664 BTRFS_BLOCK_GROUP_RAID1
|
8665 BTRFS_BLOCK_GROUP_DUP
)))
8668 * avoid allocating from un-mirrored block group if there are
8669 * mirrored block groups.
8671 list_for_each_entry(cache
, &space_info
->block_groups
[3], list
)
8672 set_block_group_ro(cache
);
8673 list_for_each_entry(cache
, &space_info
->block_groups
[4], list
)
8674 set_block_group_ro(cache
);
8677 init_global_block_rsv(info
);
8680 btrfs_free_path(path
);
8684 int btrfs_make_block_group(struct btrfs_trans_handle
*trans
,
8685 struct btrfs_root
*root
, u64 bytes_used
,
8686 u64 type
, u64 chunk_objectid
, u64 chunk_offset
,
8690 struct btrfs_root
*extent_root
;
8691 struct btrfs_block_group_cache
*cache
;
8693 extent_root
= root
->fs_info
->extent_root
;
8695 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
8697 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
8701 cache
->key
.objectid
= chunk_offset
;
8702 cache
->key
.offset
= size
;
8703 cache
->key
.type
= BTRFS_BLOCK_GROUP_ITEM_KEY
;
8704 cache
->sectorsize
= root
->sectorsize
;
8705 cache
->fs_info
= root
->fs_info
;
8708 * we only want to have 32k of ram per block group for keeping track
8709 * of free space, and if we pass 1/2 of that we want to start
8710 * converting things over to using bitmaps
8712 cache
->extents_thresh
= ((1024 * 32) / 2) /
8713 sizeof(struct btrfs_free_space
);
8714 atomic_set(&cache
->count
, 1);
8715 spin_lock_init(&cache
->lock
);
8716 spin_lock_init(&cache
->tree_lock
);
8717 INIT_LIST_HEAD(&cache
->list
);
8718 INIT_LIST_HEAD(&cache
->cluster_list
);
8720 btrfs_set_block_group_used(&cache
->item
, bytes_used
);
8721 btrfs_set_block_group_chunk_objectid(&cache
->item
, chunk_objectid
);
8722 cache
->flags
= type
;
8723 btrfs_set_block_group_flags(&cache
->item
, type
);
8725 cache
->last_byte_to_unpin
= (u64
)-1;
8726 cache
->cached
= BTRFS_CACHE_FINISHED
;
8727 exclude_super_stripes(root
, cache
);
8729 add_new_free_space(cache
, root
->fs_info
, chunk_offset
,
8730 chunk_offset
+ size
);
8732 free_excluded_extents(root
, cache
);
8734 ret
= update_space_info(root
->fs_info
, cache
->flags
, size
, bytes_used
,
8735 &cache
->space_info
);
8738 spin_lock(&cache
->space_info
->lock
);
8739 cache
->space_info
->bytes_readonly
+= cache
->bytes_super
;
8740 spin_unlock(&cache
->space_info
->lock
);
8742 __link_block_group(cache
->space_info
, cache
);
8744 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
8747 ret
= btrfs_insert_item(trans
, extent_root
, &cache
->key
, &cache
->item
,
8748 sizeof(cache
->item
));
8751 set_avail_alloc_bits(extent_root
->fs_info
, type
);
8756 int btrfs_remove_block_group(struct btrfs_trans_handle
*trans
,
8757 struct btrfs_root
*root
, u64 group_start
)
8759 struct btrfs_path
*path
;
8760 struct btrfs_block_group_cache
*block_group
;
8761 struct btrfs_free_cluster
*cluster
;
8762 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
8763 struct btrfs_key key
;
8764 struct inode
*inode
;
8768 root
= root
->fs_info
->extent_root
;
8770 block_group
= btrfs_lookup_block_group(root
->fs_info
, group_start
);
8771 BUG_ON(!block_group
);
8772 BUG_ON(!block_group
->ro
);
8775 * Free the reserved super bytes from this block group before
8778 free_excluded_extents(root
, block_group
);
8780 memcpy(&key
, &block_group
->key
, sizeof(key
));
8781 if (block_group
->flags
& (BTRFS_BLOCK_GROUP_DUP
|
8782 BTRFS_BLOCK_GROUP_RAID1
|
8783 BTRFS_BLOCK_GROUP_RAID10
))
8788 /* make sure this block group isn't part of an allocation cluster */
8789 cluster
= &root
->fs_info
->data_alloc_cluster
;
8790 spin_lock(&cluster
->refill_lock
);
8791 btrfs_return_cluster_to_free_space(block_group
, cluster
);
8792 spin_unlock(&cluster
->refill_lock
);
8795 * make sure this block group isn't part of a metadata
8796 * allocation cluster
8798 cluster
= &root
->fs_info
->meta_alloc_cluster
;
8799 spin_lock(&cluster
->refill_lock
);
8800 btrfs_return_cluster_to_free_space(block_group
, cluster
);
8801 spin_unlock(&cluster
->refill_lock
);
8803 path
= btrfs_alloc_path();
8806 inode
= lookup_free_space_inode(root
, block_group
, path
);
8807 if (!IS_ERR(inode
)) {
8808 btrfs_orphan_add(trans
, inode
);
8810 /* One for the block groups ref */
8811 spin_lock(&block_group
->lock
);
8812 if (block_group
->iref
) {
8813 block_group
->iref
= 0;
8814 block_group
->inode
= NULL
;
8815 spin_unlock(&block_group
->lock
);
8818 spin_unlock(&block_group
->lock
);
8820 /* One for our lookup ref */
8824 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
8825 key
.offset
= block_group
->key
.objectid
;
8828 ret
= btrfs_search_slot(trans
, tree_root
, &key
, path
, -1, 1);
8832 btrfs_release_path(tree_root
, path
);
8834 ret
= btrfs_del_item(trans
, tree_root
, path
);
8837 btrfs_release_path(tree_root
, path
);
8840 spin_lock(&root
->fs_info
->block_group_cache_lock
);
8841 rb_erase(&block_group
->cache_node
,
8842 &root
->fs_info
->block_group_cache_tree
);
8843 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
8845 down_write(&block_group
->space_info
->groups_sem
);
8847 * we must use list_del_init so people can check to see if they
8848 * are still on the list after taking the semaphore
8850 list_del_init(&block_group
->list
);
8851 up_write(&block_group
->space_info
->groups_sem
);
8853 if (block_group
->cached
== BTRFS_CACHE_STARTED
)
8854 wait_block_group_cache_done(block_group
);
8856 btrfs_remove_free_space_cache(block_group
);
8858 spin_lock(&block_group
->space_info
->lock
);
8859 block_group
->space_info
->total_bytes
-= block_group
->key
.offset
;
8860 block_group
->space_info
->bytes_readonly
-= block_group
->key
.offset
;
8861 block_group
->space_info
->disk_total
-= block_group
->key
.offset
* factor
;
8862 spin_unlock(&block_group
->space_info
->lock
);
8864 memcpy(&key
, &block_group
->key
, sizeof(key
));
8866 btrfs_clear_space_info_full(root
->fs_info
);
8868 btrfs_put_block_group(block_group
);
8869 btrfs_put_block_group(block_group
);
8871 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
8877 ret
= btrfs_del_item(trans
, root
, path
);
8879 btrfs_free_path(path
);
8883 int btrfs_init_space_info(struct btrfs_fs_info
*fs_info
)
8885 struct btrfs_space_info
*space_info
;
8886 struct btrfs_super_block
*disk_super
;
8892 disk_super
= &fs_info
->super_copy
;
8893 if (!btrfs_super_root(disk_super
))
8896 features
= btrfs_super_incompat_flags(disk_super
);
8897 if (features
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
8900 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
8901 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
8906 flags
= BTRFS_BLOCK_GROUP_METADATA
| BTRFS_BLOCK_GROUP_DATA
;
8907 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
8909 flags
= BTRFS_BLOCK_GROUP_METADATA
;
8910 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
8914 flags
= BTRFS_BLOCK_GROUP_DATA
;
8915 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
8921 int btrfs_error_unpin_extent_range(struct btrfs_root
*root
, u64 start
, u64 end
)
8923 return unpin_extent_range(root
, start
, end
);
8926 int btrfs_error_discard_extent(struct btrfs_root
*root
, u64 bytenr
,
8927 u64 num_bytes
, u64
*actual_bytes
)
8929 return btrfs_discard_extent(root
, bytenr
, num_bytes
, actual_bytes
);
8932 int btrfs_trim_fs(struct btrfs_root
*root
, struct fstrim_range
*range
)
8934 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
8935 struct btrfs_block_group_cache
*cache
= NULL
;
8942 cache
= btrfs_lookup_block_group(fs_info
, range
->start
);
8945 if (cache
->key
.objectid
>= (range
->start
+ range
->len
)) {
8946 btrfs_put_block_group(cache
);
8950 start
= max(range
->start
, cache
->key
.objectid
);
8951 end
= min(range
->start
+ range
->len
,
8952 cache
->key
.objectid
+ cache
->key
.offset
);
8954 if (end
- start
>= range
->minlen
) {
8955 if (!block_group_cache_done(cache
)) {
8956 ret
= cache_block_group(cache
, NULL
, root
, 0);
8958 wait_block_group_cache_done(cache
);
8960 ret
= btrfs_trim_block_group(cache
,
8966 trimmed
+= group_trimmed
;
8968 btrfs_put_block_group(cache
);
8973 cache
= next_block_group(fs_info
->tree_root
, cache
);
8976 range
->len
= trimmed
;