2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
18 #include <linux/sched.h>
19 #include <linux/pagemap.h>
20 #include <linux/writeback.h>
21 #include <linux/blkdev.h>
22 #include <linux/sort.h>
23 #include <linux/rcupdate.h>
24 #include <linux/kthread.h>
25 #include <linux/slab.h>
30 #include "print-tree.h"
31 #include "transaction.h"
34 #include "free-space-cache.h"
36 static int update_block_group(struct btrfs_trans_handle
*trans
,
37 struct btrfs_root
*root
,
38 u64 bytenr
, u64 num_bytes
, int alloc
);
39 static int update_reserved_bytes(struct btrfs_block_group_cache
*cache
,
40 u64 num_bytes
, int reserve
, int sinfo
);
41 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
42 struct btrfs_root
*root
,
43 u64 bytenr
, u64 num_bytes
, u64 parent
,
44 u64 root_objectid
, u64 owner_objectid
,
45 u64 owner_offset
, int refs_to_drop
,
46 struct btrfs_delayed_extent_op
*extra_op
);
47 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op
*extent_op
,
48 struct extent_buffer
*leaf
,
49 struct btrfs_extent_item
*ei
);
50 static int alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
51 struct btrfs_root
*root
,
52 u64 parent
, u64 root_objectid
,
53 u64 flags
, u64 owner
, u64 offset
,
54 struct btrfs_key
*ins
, int ref_mod
);
55 static int alloc_reserved_tree_block(struct btrfs_trans_handle
*trans
,
56 struct btrfs_root
*root
,
57 u64 parent
, u64 root_objectid
,
58 u64 flags
, struct btrfs_disk_key
*key
,
59 int level
, struct btrfs_key
*ins
);
60 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
61 struct btrfs_root
*extent_root
, u64 alloc_bytes
,
62 u64 flags
, int force
);
63 static int find_next_key(struct btrfs_path
*path
, int level
,
64 struct btrfs_key
*key
);
65 static void dump_space_info(struct btrfs_space_info
*info
, u64 bytes
,
66 int dump_block_groups
);
69 block_group_cache_done(struct btrfs_block_group_cache
*cache
)
72 return cache
->cached
== BTRFS_CACHE_FINISHED
;
75 static int block_group_bits(struct btrfs_block_group_cache
*cache
, u64 bits
)
77 return (cache
->flags
& bits
) == bits
;
80 void btrfs_get_block_group(struct btrfs_block_group_cache
*cache
)
82 atomic_inc(&cache
->count
);
85 void btrfs_put_block_group(struct btrfs_block_group_cache
*cache
)
87 if (atomic_dec_and_test(&cache
->count
)) {
88 WARN_ON(cache
->pinned
> 0);
89 WARN_ON(cache
->reserved
> 0);
90 WARN_ON(cache
->reserved_pinned
> 0);
96 * this adds the block group to the fs_info rb tree for the block group
99 static int btrfs_add_block_group_cache(struct btrfs_fs_info
*info
,
100 struct btrfs_block_group_cache
*block_group
)
103 struct rb_node
*parent
= NULL
;
104 struct btrfs_block_group_cache
*cache
;
106 spin_lock(&info
->block_group_cache_lock
);
107 p
= &info
->block_group_cache_tree
.rb_node
;
111 cache
= rb_entry(parent
, struct btrfs_block_group_cache
,
113 if (block_group
->key
.objectid
< cache
->key
.objectid
) {
115 } else if (block_group
->key
.objectid
> cache
->key
.objectid
) {
118 spin_unlock(&info
->block_group_cache_lock
);
123 rb_link_node(&block_group
->cache_node
, parent
, p
);
124 rb_insert_color(&block_group
->cache_node
,
125 &info
->block_group_cache_tree
);
126 spin_unlock(&info
->block_group_cache_lock
);
132 * This will return the block group at or after bytenr if contains is 0, else
133 * it will return the block group that contains the bytenr
135 static struct btrfs_block_group_cache
*
136 block_group_cache_tree_search(struct btrfs_fs_info
*info
, u64 bytenr
,
139 struct btrfs_block_group_cache
*cache
, *ret
= NULL
;
143 spin_lock(&info
->block_group_cache_lock
);
144 n
= info
->block_group_cache_tree
.rb_node
;
147 cache
= rb_entry(n
, struct btrfs_block_group_cache
,
149 end
= cache
->key
.objectid
+ cache
->key
.offset
- 1;
150 start
= cache
->key
.objectid
;
152 if (bytenr
< start
) {
153 if (!contains
&& (!ret
|| start
< ret
->key
.objectid
))
156 } else if (bytenr
> start
) {
157 if (contains
&& bytenr
<= end
) {
168 btrfs_get_block_group(ret
);
169 spin_unlock(&info
->block_group_cache_lock
);
174 static int add_excluded_extent(struct btrfs_root
*root
,
175 u64 start
, u64 num_bytes
)
177 u64 end
= start
+ num_bytes
- 1;
178 set_extent_bits(&root
->fs_info
->freed_extents
[0],
179 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
180 set_extent_bits(&root
->fs_info
->freed_extents
[1],
181 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
185 static void free_excluded_extents(struct btrfs_root
*root
,
186 struct btrfs_block_group_cache
*cache
)
190 start
= cache
->key
.objectid
;
191 end
= start
+ cache
->key
.offset
- 1;
193 clear_extent_bits(&root
->fs_info
->freed_extents
[0],
194 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
195 clear_extent_bits(&root
->fs_info
->freed_extents
[1],
196 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
199 static int exclude_super_stripes(struct btrfs_root
*root
,
200 struct btrfs_block_group_cache
*cache
)
207 if (cache
->key
.objectid
< BTRFS_SUPER_INFO_OFFSET
) {
208 stripe_len
= BTRFS_SUPER_INFO_OFFSET
- cache
->key
.objectid
;
209 cache
->bytes_super
+= stripe_len
;
210 ret
= add_excluded_extent(root
, cache
->key
.objectid
,
215 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
216 bytenr
= btrfs_sb_offset(i
);
217 ret
= btrfs_rmap_block(&root
->fs_info
->mapping_tree
,
218 cache
->key
.objectid
, bytenr
,
219 0, &logical
, &nr
, &stripe_len
);
223 cache
->bytes_super
+= stripe_len
;
224 ret
= add_excluded_extent(root
, logical
[nr
],
234 static struct btrfs_caching_control
*
235 get_caching_control(struct btrfs_block_group_cache
*cache
)
237 struct btrfs_caching_control
*ctl
;
239 spin_lock(&cache
->lock
);
240 if (cache
->cached
!= BTRFS_CACHE_STARTED
) {
241 spin_unlock(&cache
->lock
);
245 ctl
= cache
->caching_ctl
;
246 atomic_inc(&ctl
->count
);
247 spin_unlock(&cache
->lock
);
251 static void put_caching_control(struct btrfs_caching_control
*ctl
)
253 if (atomic_dec_and_test(&ctl
->count
))
258 * this is only called by cache_block_group, since we could have freed extents
259 * we need to check the pinned_extents for any extents that can't be used yet
260 * since their free space will be released as soon as the transaction commits.
262 static u64
add_new_free_space(struct btrfs_block_group_cache
*block_group
,
263 struct btrfs_fs_info
*info
, u64 start
, u64 end
)
265 u64 extent_start
, extent_end
, size
, total_added
= 0;
268 while (start
< end
) {
269 ret
= find_first_extent_bit(info
->pinned_extents
, start
,
270 &extent_start
, &extent_end
,
271 EXTENT_DIRTY
| EXTENT_UPTODATE
);
275 if (extent_start
<= start
) {
276 start
= extent_end
+ 1;
277 } else if (extent_start
> start
&& extent_start
< end
) {
278 size
= extent_start
- start
;
280 ret
= btrfs_add_free_space(block_group
, start
,
283 start
= extent_end
+ 1;
292 ret
= btrfs_add_free_space(block_group
, start
, size
);
299 static int caching_kthread(void *data
)
301 struct btrfs_block_group_cache
*block_group
= data
;
302 struct btrfs_fs_info
*fs_info
= block_group
->fs_info
;
303 struct btrfs_caching_control
*caching_ctl
= block_group
->caching_ctl
;
304 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
305 struct btrfs_path
*path
;
306 struct extent_buffer
*leaf
;
307 struct btrfs_key key
;
313 path
= btrfs_alloc_path();
317 exclude_super_stripes(extent_root
, block_group
);
318 spin_lock(&block_group
->space_info
->lock
);
319 block_group
->space_info
->bytes_readonly
+= block_group
->bytes_super
;
320 spin_unlock(&block_group
->space_info
->lock
);
322 last
= max_t(u64
, block_group
->key
.objectid
, BTRFS_SUPER_INFO_OFFSET
);
325 * We don't want to deadlock with somebody trying to allocate a new
326 * extent for the extent root while also trying to search the extent
327 * root to add free space. So we skip locking and search the commit
328 * root, since its read-only
330 path
->skip_locking
= 1;
331 path
->search_commit_root
= 1;
336 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
338 mutex_lock(&caching_ctl
->mutex
);
339 /* need to make sure the commit_root doesn't disappear */
340 down_read(&fs_info
->extent_commit_sem
);
342 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
346 leaf
= path
->nodes
[0];
347 nritems
= btrfs_header_nritems(leaf
);
351 if (fs_info
->closing
> 1) {
356 if (path
->slots
[0] < nritems
) {
357 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
359 ret
= find_next_key(path
, 0, &key
);
363 caching_ctl
->progress
= last
;
364 btrfs_release_path(extent_root
, path
);
365 up_read(&fs_info
->extent_commit_sem
);
366 mutex_unlock(&caching_ctl
->mutex
);
367 if (btrfs_transaction_in_commit(fs_info
))
374 if (key
.objectid
< block_group
->key
.objectid
) {
379 if (key
.objectid
>= block_group
->key
.objectid
+
380 block_group
->key
.offset
)
383 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
384 total_found
+= add_new_free_space(block_group
,
387 last
= key
.objectid
+ key
.offset
;
389 if (total_found
> (1024 * 1024 * 2)) {
391 wake_up(&caching_ctl
->wait
);
398 total_found
+= add_new_free_space(block_group
, fs_info
, last
,
399 block_group
->key
.objectid
+
400 block_group
->key
.offset
);
401 caching_ctl
->progress
= (u64
)-1;
403 spin_lock(&block_group
->lock
);
404 block_group
->caching_ctl
= NULL
;
405 block_group
->cached
= BTRFS_CACHE_FINISHED
;
406 spin_unlock(&block_group
->lock
);
409 btrfs_free_path(path
);
410 up_read(&fs_info
->extent_commit_sem
);
412 free_excluded_extents(extent_root
, block_group
);
414 mutex_unlock(&caching_ctl
->mutex
);
415 wake_up(&caching_ctl
->wait
);
417 put_caching_control(caching_ctl
);
418 atomic_dec(&block_group
->space_info
->caching_threads
);
419 btrfs_put_block_group(block_group
);
424 static int cache_block_group(struct btrfs_block_group_cache
*cache
)
426 struct btrfs_fs_info
*fs_info
= cache
->fs_info
;
427 struct btrfs_caching_control
*caching_ctl
;
428 struct task_struct
*tsk
;
432 if (cache
->cached
!= BTRFS_CACHE_NO
)
435 caching_ctl
= kzalloc(sizeof(*caching_ctl
), GFP_KERNEL
);
436 BUG_ON(!caching_ctl
);
438 INIT_LIST_HEAD(&caching_ctl
->list
);
439 mutex_init(&caching_ctl
->mutex
);
440 init_waitqueue_head(&caching_ctl
->wait
);
441 caching_ctl
->block_group
= cache
;
442 caching_ctl
->progress
= cache
->key
.objectid
;
443 /* one for caching kthread, one for caching block group list */
444 atomic_set(&caching_ctl
->count
, 2);
446 spin_lock(&cache
->lock
);
447 if (cache
->cached
!= BTRFS_CACHE_NO
) {
448 spin_unlock(&cache
->lock
);
452 cache
->caching_ctl
= caching_ctl
;
453 cache
->cached
= BTRFS_CACHE_STARTED
;
454 spin_unlock(&cache
->lock
);
456 down_write(&fs_info
->extent_commit_sem
);
457 list_add_tail(&caching_ctl
->list
, &fs_info
->caching_block_groups
);
458 up_write(&fs_info
->extent_commit_sem
);
460 atomic_inc(&cache
->space_info
->caching_threads
);
461 btrfs_get_block_group(cache
);
463 tsk
= kthread_run(caching_kthread
, cache
, "btrfs-cache-%llu\n",
464 cache
->key
.objectid
);
467 printk(KERN_ERR
"error running thread %d\n", ret
);
475 * return the block group that starts at or after bytenr
477 static struct btrfs_block_group_cache
*
478 btrfs_lookup_first_block_group(struct btrfs_fs_info
*info
, u64 bytenr
)
480 struct btrfs_block_group_cache
*cache
;
482 cache
= block_group_cache_tree_search(info
, bytenr
, 0);
488 * return the block group that contains the given bytenr
490 struct btrfs_block_group_cache
*btrfs_lookup_block_group(
491 struct btrfs_fs_info
*info
,
494 struct btrfs_block_group_cache
*cache
;
496 cache
= block_group_cache_tree_search(info
, bytenr
, 1);
501 static struct btrfs_space_info
*__find_space_info(struct btrfs_fs_info
*info
,
504 struct list_head
*head
= &info
->space_info
;
505 struct btrfs_space_info
*found
;
507 flags
&= BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_SYSTEM
|
508 BTRFS_BLOCK_GROUP_METADATA
;
511 list_for_each_entry_rcu(found
, head
, list
) {
512 if (found
->flags
== flags
) {
522 * after adding space to the filesystem, we need to clear the full flags
523 * on all the space infos.
525 void btrfs_clear_space_info_full(struct btrfs_fs_info
*info
)
527 struct list_head
*head
= &info
->space_info
;
528 struct btrfs_space_info
*found
;
531 list_for_each_entry_rcu(found
, head
, list
)
536 static u64
div_factor(u64 num
, int factor
)
545 u64
btrfs_find_block_group(struct btrfs_root
*root
,
546 u64 search_start
, u64 search_hint
, int owner
)
548 struct btrfs_block_group_cache
*cache
;
550 u64 last
= max(search_hint
, search_start
);
557 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
561 spin_lock(&cache
->lock
);
562 last
= cache
->key
.objectid
+ cache
->key
.offset
;
563 used
= btrfs_block_group_used(&cache
->item
);
565 if ((full_search
|| !cache
->ro
) &&
566 block_group_bits(cache
, BTRFS_BLOCK_GROUP_METADATA
)) {
567 if (used
+ cache
->pinned
+ cache
->reserved
<
568 div_factor(cache
->key
.offset
, factor
)) {
569 group_start
= cache
->key
.objectid
;
570 spin_unlock(&cache
->lock
);
571 btrfs_put_block_group(cache
);
575 spin_unlock(&cache
->lock
);
576 btrfs_put_block_group(cache
);
584 if (!full_search
&& factor
< 10) {
594 /* simple helper to search for an existing extent at a given offset */
595 int btrfs_lookup_extent(struct btrfs_root
*root
, u64 start
, u64 len
)
598 struct btrfs_key key
;
599 struct btrfs_path
*path
;
601 path
= btrfs_alloc_path();
603 key
.objectid
= start
;
605 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
606 ret
= btrfs_search_slot(NULL
, root
->fs_info
->extent_root
, &key
, path
,
608 btrfs_free_path(path
);
613 * helper function to lookup reference count and flags of extent.
615 * the head node for delayed ref is used to store the sum of all the
616 * reference count modifications queued up in the rbtree. the head
617 * node may also store the extent flags to set. This way you can check
618 * to see what the reference count and extent flags would be if all of
619 * the delayed refs are not processed.
621 int btrfs_lookup_extent_info(struct btrfs_trans_handle
*trans
,
622 struct btrfs_root
*root
, u64 bytenr
,
623 u64 num_bytes
, u64
*refs
, u64
*flags
)
625 struct btrfs_delayed_ref_head
*head
;
626 struct btrfs_delayed_ref_root
*delayed_refs
;
627 struct btrfs_path
*path
;
628 struct btrfs_extent_item
*ei
;
629 struct extent_buffer
*leaf
;
630 struct btrfs_key key
;
636 path
= btrfs_alloc_path();
640 key
.objectid
= bytenr
;
641 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
642 key
.offset
= num_bytes
;
644 path
->skip_locking
= 1;
645 path
->search_commit_root
= 1;
648 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
,
654 leaf
= path
->nodes
[0];
655 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
656 if (item_size
>= sizeof(*ei
)) {
657 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
658 struct btrfs_extent_item
);
659 num_refs
= btrfs_extent_refs(leaf
, ei
);
660 extent_flags
= btrfs_extent_flags(leaf
, ei
);
662 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
663 struct btrfs_extent_item_v0
*ei0
;
664 BUG_ON(item_size
!= sizeof(*ei0
));
665 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
666 struct btrfs_extent_item_v0
);
667 num_refs
= btrfs_extent_refs_v0(leaf
, ei0
);
668 /* FIXME: this isn't correct for data */
669 extent_flags
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
674 BUG_ON(num_refs
== 0);
684 delayed_refs
= &trans
->transaction
->delayed_refs
;
685 spin_lock(&delayed_refs
->lock
);
686 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
688 if (!mutex_trylock(&head
->mutex
)) {
689 atomic_inc(&head
->node
.refs
);
690 spin_unlock(&delayed_refs
->lock
);
692 btrfs_release_path(root
->fs_info
->extent_root
, path
);
694 mutex_lock(&head
->mutex
);
695 mutex_unlock(&head
->mutex
);
696 btrfs_put_delayed_ref(&head
->node
);
699 if (head
->extent_op
&& head
->extent_op
->update_flags
)
700 extent_flags
|= head
->extent_op
->flags_to_set
;
702 BUG_ON(num_refs
== 0);
704 num_refs
+= head
->node
.ref_mod
;
705 mutex_unlock(&head
->mutex
);
707 spin_unlock(&delayed_refs
->lock
);
709 WARN_ON(num_refs
== 0);
713 *flags
= extent_flags
;
715 btrfs_free_path(path
);
720 * Back reference rules. Back refs have three main goals:
722 * 1) differentiate between all holders of references to an extent so that
723 * when a reference is dropped we can make sure it was a valid reference
724 * before freeing the extent.
726 * 2) Provide enough information to quickly find the holders of an extent
727 * if we notice a given block is corrupted or bad.
729 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
730 * maintenance. This is actually the same as #2, but with a slightly
731 * different use case.
733 * There are two kinds of back refs. The implicit back refs is optimized
734 * for pointers in non-shared tree blocks. For a given pointer in a block,
735 * back refs of this kind provide information about the block's owner tree
736 * and the pointer's key. These information allow us to find the block by
737 * b-tree searching. The full back refs is for pointers in tree blocks not
738 * referenced by their owner trees. The location of tree block is recorded
739 * in the back refs. Actually the full back refs is generic, and can be
740 * used in all cases the implicit back refs is used. The major shortcoming
741 * of the full back refs is its overhead. Every time a tree block gets
742 * COWed, we have to update back refs entry for all pointers in it.
744 * For a newly allocated tree block, we use implicit back refs for
745 * pointers in it. This means most tree related operations only involve
746 * implicit back refs. For a tree block created in old transaction, the
747 * only way to drop a reference to it is COW it. So we can detect the
748 * event that tree block loses its owner tree's reference and do the
749 * back refs conversion.
751 * When a tree block is COW'd through a tree, there are four cases:
753 * The reference count of the block is one and the tree is the block's
754 * owner tree. Nothing to do in this case.
756 * The reference count of the block is one and the tree is not the
757 * block's owner tree. In this case, full back refs is used for pointers
758 * in the block. Remove these full back refs, add implicit back refs for
759 * every pointers in the new block.
761 * The reference count of the block is greater than one and the tree is
762 * the block's owner tree. In this case, implicit back refs is used for
763 * pointers in the block. Add full back refs for every pointers in the
764 * block, increase lower level extents' reference counts. The original
765 * implicit back refs are entailed to the new block.
767 * The reference count of the block is greater than one and the tree is
768 * not the block's owner tree. Add implicit back refs for every pointer in
769 * the new block, increase lower level extents' reference count.
771 * Back Reference Key composing:
773 * The key objectid corresponds to the first byte in the extent,
774 * The key type is used to differentiate between types of back refs.
775 * There are different meanings of the key offset for different types
778 * File extents can be referenced by:
780 * - multiple snapshots, subvolumes, or different generations in one subvol
781 * - different files inside a single subvolume
782 * - different offsets inside a file (bookend extents in file.c)
784 * The extent ref structure for the implicit back refs has fields for:
786 * - Objectid of the subvolume root
787 * - objectid of the file holding the reference
788 * - original offset in the file
789 * - how many bookend extents
791 * The key offset for the implicit back refs is hash of the first
794 * The extent ref structure for the full back refs has field for:
796 * - number of pointers in the tree leaf
798 * The key offset for the implicit back refs is the first byte of
801 * When a file extent is allocated, The implicit back refs is used.
802 * the fields are filled in:
804 * (root_key.objectid, inode objectid, offset in file, 1)
806 * When a file extent is removed file truncation, we find the
807 * corresponding implicit back refs and check the following fields:
809 * (btrfs_header_owner(leaf), inode objectid, offset in file)
811 * Btree extents can be referenced by:
813 * - Different subvolumes
815 * Both the implicit back refs and the full back refs for tree blocks
816 * only consist of key. The key offset for the implicit back refs is
817 * objectid of block's owner tree. The key offset for the full back refs
818 * is the first byte of parent block.
820 * When implicit back refs is used, information about the lowest key and
821 * level of the tree block are required. These information are stored in
822 * tree block info structure.
825 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
826 static int convert_extent_item_v0(struct btrfs_trans_handle
*trans
,
827 struct btrfs_root
*root
,
828 struct btrfs_path
*path
,
829 u64 owner
, u32 extra_size
)
831 struct btrfs_extent_item
*item
;
832 struct btrfs_extent_item_v0
*ei0
;
833 struct btrfs_extent_ref_v0
*ref0
;
834 struct btrfs_tree_block_info
*bi
;
835 struct extent_buffer
*leaf
;
836 struct btrfs_key key
;
837 struct btrfs_key found_key
;
838 u32 new_size
= sizeof(*item
);
842 leaf
= path
->nodes
[0];
843 BUG_ON(btrfs_item_size_nr(leaf
, path
->slots
[0]) != sizeof(*ei0
));
845 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
846 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
847 struct btrfs_extent_item_v0
);
848 refs
= btrfs_extent_refs_v0(leaf
, ei0
);
850 if (owner
== (u64
)-1) {
852 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
853 ret
= btrfs_next_leaf(root
, path
);
857 leaf
= path
->nodes
[0];
859 btrfs_item_key_to_cpu(leaf
, &found_key
,
861 BUG_ON(key
.objectid
!= found_key
.objectid
);
862 if (found_key
.type
!= BTRFS_EXTENT_REF_V0_KEY
) {
866 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
867 struct btrfs_extent_ref_v0
);
868 owner
= btrfs_ref_objectid_v0(leaf
, ref0
);
872 btrfs_release_path(root
, path
);
874 if (owner
< BTRFS_FIRST_FREE_OBJECTID
)
875 new_size
+= sizeof(*bi
);
877 new_size
-= sizeof(*ei0
);
878 ret
= btrfs_search_slot(trans
, root
, &key
, path
,
879 new_size
+ extra_size
, 1);
884 ret
= btrfs_extend_item(trans
, root
, path
, new_size
);
887 leaf
= path
->nodes
[0];
888 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
889 btrfs_set_extent_refs(leaf
, item
, refs
);
890 /* FIXME: get real generation */
891 btrfs_set_extent_generation(leaf
, item
, 0);
892 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
893 btrfs_set_extent_flags(leaf
, item
,
894 BTRFS_EXTENT_FLAG_TREE_BLOCK
|
895 BTRFS_BLOCK_FLAG_FULL_BACKREF
);
896 bi
= (struct btrfs_tree_block_info
*)(item
+ 1);
897 /* FIXME: get first key of the block */
898 memset_extent_buffer(leaf
, 0, (unsigned long)bi
, sizeof(*bi
));
899 btrfs_set_tree_block_level(leaf
, bi
, (int)owner
);
901 btrfs_set_extent_flags(leaf
, item
, BTRFS_EXTENT_FLAG_DATA
);
903 btrfs_mark_buffer_dirty(leaf
);
908 static u64
hash_extent_data_ref(u64 root_objectid
, u64 owner
, u64 offset
)
910 u32 high_crc
= ~(u32
)0;
911 u32 low_crc
= ~(u32
)0;
914 lenum
= cpu_to_le64(root_objectid
);
915 high_crc
= crc32c(high_crc
, &lenum
, sizeof(lenum
));
916 lenum
= cpu_to_le64(owner
);
917 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
918 lenum
= cpu_to_le64(offset
);
919 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
921 return ((u64
)high_crc
<< 31) ^ (u64
)low_crc
;
924 static u64
hash_extent_data_ref_item(struct extent_buffer
*leaf
,
925 struct btrfs_extent_data_ref
*ref
)
927 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf
, ref
),
928 btrfs_extent_data_ref_objectid(leaf
, ref
),
929 btrfs_extent_data_ref_offset(leaf
, ref
));
932 static int match_extent_data_ref(struct extent_buffer
*leaf
,
933 struct btrfs_extent_data_ref
*ref
,
934 u64 root_objectid
, u64 owner
, u64 offset
)
936 if (btrfs_extent_data_ref_root(leaf
, ref
) != root_objectid
||
937 btrfs_extent_data_ref_objectid(leaf
, ref
) != owner
||
938 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
943 static noinline
int lookup_extent_data_ref(struct btrfs_trans_handle
*trans
,
944 struct btrfs_root
*root
,
945 struct btrfs_path
*path
,
946 u64 bytenr
, u64 parent
,
948 u64 owner
, u64 offset
)
950 struct btrfs_key key
;
951 struct btrfs_extent_data_ref
*ref
;
952 struct extent_buffer
*leaf
;
958 key
.objectid
= bytenr
;
960 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
963 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
964 key
.offset
= hash_extent_data_ref(root_objectid
,
969 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
978 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
979 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
980 btrfs_release_path(root
, path
);
981 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
992 leaf
= path
->nodes
[0];
993 nritems
= btrfs_header_nritems(leaf
);
995 if (path
->slots
[0] >= nritems
) {
996 ret
= btrfs_next_leaf(root
, path
);
1002 leaf
= path
->nodes
[0];
1003 nritems
= btrfs_header_nritems(leaf
);
1007 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1008 if (key
.objectid
!= bytenr
||
1009 key
.type
!= BTRFS_EXTENT_DATA_REF_KEY
)
1012 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1013 struct btrfs_extent_data_ref
);
1015 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
1018 btrfs_release_path(root
, path
);
1030 static noinline
int insert_extent_data_ref(struct btrfs_trans_handle
*trans
,
1031 struct btrfs_root
*root
,
1032 struct btrfs_path
*path
,
1033 u64 bytenr
, u64 parent
,
1034 u64 root_objectid
, u64 owner
,
1035 u64 offset
, int refs_to_add
)
1037 struct btrfs_key key
;
1038 struct extent_buffer
*leaf
;
1043 key
.objectid
= bytenr
;
1045 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
1046 key
.offset
= parent
;
1047 size
= sizeof(struct btrfs_shared_data_ref
);
1049 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
1050 key
.offset
= hash_extent_data_ref(root_objectid
,
1052 size
= sizeof(struct btrfs_extent_data_ref
);
1055 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, size
);
1056 if (ret
&& ret
!= -EEXIST
)
1059 leaf
= path
->nodes
[0];
1061 struct btrfs_shared_data_ref
*ref
;
1062 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1063 struct btrfs_shared_data_ref
);
1065 btrfs_set_shared_data_ref_count(leaf
, ref
, refs_to_add
);
1067 num_refs
= btrfs_shared_data_ref_count(leaf
, ref
);
1068 num_refs
+= refs_to_add
;
1069 btrfs_set_shared_data_ref_count(leaf
, ref
, num_refs
);
1072 struct btrfs_extent_data_ref
*ref
;
1073 while (ret
== -EEXIST
) {
1074 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1075 struct btrfs_extent_data_ref
);
1076 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
1079 btrfs_release_path(root
, path
);
1081 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1083 if (ret
&& ret
!= -EEXIST
)
1086 leaf
= path
->nodes
[0];
1088 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1089 struct btrfs_extent_data_ref
);
1091 btrfs_set_extent_data_ref_root(leaf
, ref
,
1093 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
1094 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
1095 btrfs_set_extent_data_ref_count(leaf
, ref
, refs_to_add
);
1097 num_refs
= btrfs_extent_data_ref_count(leaf
, ref
);
1098 num_refs
+= refs_to_add
;
1099 btrfs_set_extent_data_ref_count(leaf
, ref
, num_refs
);
1102 btrfs_mark_buffer_dirty(leaf
);
1105 btrfs_release_path(root
, path
);
1109 static noinline
int remove_extent_data_ref(struct btrfs_trans_handle
*trans
,
1110 struct btrfs_root
*root
,
1111 struct btrfs_path
*path
,
1114 struct btrfs_key key
;
1115 struct btrfs_extent_data_ref
*ref1
= NULL
;
1116 struct btrfs_shared_data_ref
*ref2
= NULL
;
1117 struct extent_buffer
*leaf
;
1121 leaf
= path
->nodes
[0];
1122 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1124 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1125 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
1126 struct btrfs_extent_data_ref
);
1127 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1128 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
1129 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
1130 struct btrfs_shared_data_ref
);
1131 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1132 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1133 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
1134 struct btrfs_extent_ref_v0
*ref0
;
1135 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1136 struct btrfs_extent_ref_v0
);
1137 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
1143 BUG_ON(num_refs
< refs_to_drop
);
1144 num_refs
-= refs_to_drop
;
1146 if (num_refs
== 0) {
1147 ret
= btrfs_del_item(trans
, root
, path
);
1149 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
)
1150 btrfs_set_extent_data_ref_count(leaf
, ref1
, num_refs
);
1151 else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
)
1152 btrfs_set_shared_data_ref_count(leaf
, ref2
, num_refs
);
1153 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1155 struct btrfs_extent_ref_v0
*ref0
;
1156 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1157 struct btrfs_extent_ref_v0
);
1158 btrfs_set_ref_count_v0(leaf
, ref0
, num_refs
);
1161 btrfs_mark_buffer_dirty(leaf
);
1166 static noinline u32
extent_data_ref_count(struct btrfs_root
*root
,
1167 struct btrfs_path
*path
,
1168 struct btrfs_extent_inline_ref
*iref
)
1170 struct btrfs_key key
;
1171 struct extent_buffer
*leaf
;
1172 struct btrfs_extent_data_ref
*ref1
;
1173 struct btrfs_shared_data_ref
*ref2
;
1176 leaf
= path
->nodes
[0];
1177 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1179 if (btrfs_extent_inline_ref_type(leaf
, iref
) ==
1180 BTRFS_EXTENT_DATA_REF_KEY
) {
1181 ref1
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1182 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1184 ref2
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1185 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1187 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1188 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
1189 struct btrfs_extent_data_ref
);
1190 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1191 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
1192 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
1193 struct btrfs_shared_data_ref
);
1194 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1195 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1196 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
1197 struct btrfs_extent_ref_v0
*ref0
;
1198 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1199 struct btrfs_extent_ref_v0
);
1200 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
1208 static noinline
int lookup_tree_block_ref(struct btrfs_trans_handle
*trans
,
1209 struct btrfs_root
*root
,
1210 struct btrfs_path
*path
,
1211 u64 bytenr
, u64 parent
,
1214 struct btrfs_key key
;
1217 key
.objectid
= bytenr
;
1219 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1220 key
.offset
= parent
;
1222 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
1223 key
.offset
= root_objectid
;
1226 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1229 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1230 if (ret
== -ENOENT
&& parent
) {
1231 btrfs_release_path(root
, path
);
1232 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
1233 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1241 static noinline
int insert_tree_block_ref(struct btrfs_trans_handle
*trans
,
1242 struct btrfs_root
*root
,
1243 struct btrfs_path
*path
,
1244 u64 bytenr
, u64 parent
,
1247 struct btrfs_key key
;
1250 key
.objectid
= bytenr
;
1252 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1253 key
.offset
= parent
;
1255 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
1256 key
.offset
= root_objectid
;
1259 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, 0);
1260 btrfs_release_path(root
, path
);
1264 static inline int extent_ref_type(u64 parent
, u64 owner
)
1267 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1269 type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1271 type
= BTRFS_TREE_BLOCK_REF_KEY
;
1274 type
= BTRFS_SHARED_DATA_REF_KEY
;
1276 type
= BTRFS_EXTENT_DATA_REF_KEY
;
1281 static int find_next_key(struct btrfs_path
*path
, int level
,
1282 struct btrfs_key
*key
)
1285 for (; level
< BTRFS_MAX_LEVEL
; level
++) {
1286 if (!path
->nodes
[level
])
1288 if (path
->slots
[level
] + 1 >=
1289 btrfs_header_nritems(path
->nodes
[level
]))
1292 btrfs_item_key_to_cpu(path
->nodes
[level
], key
,
1293 path
->slots
[level
] + 1);
1295 btrfs_node_key_to_cpu(path
->nodes
[level
], key
,
1296 path
->slots
[level
] + 1);
1303 * look for inline back ref. if back ref is found, *ref_ret is set
1304 * to the address of inline back ref, and 0 is returned.
1306 * if back ref isn't found, *ref_ret is set to the address where it
1307 * should be inserted, and -ENOENT is returned.
1309 * if insert is true and there are too many inline back refs, the path
1310 * points to the extent item, and -EAGAIN is returned.
1312 * NOTE: inline back refs are ordered in the same way that back ref
1313 * items in the tree are ordered.
1315 static noinline_for_stack
1316 int lookup_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1317 struct btrfs_root
*root
,
1318 struct btrfs_path
*path
,
1319 struct btrfs_extent_inline_ref
**ref_ret
,
1320 u64 bytenr
, u64 num_bytes
,
1321 u64 parent
, u64 root_objectid
,
1322 u64 owner
, u64 offset
, int insert
)
1324 struct btrfs_key key
;
1325 struct extent_buffer
*leaf
;
1326 struct btrfs_extent_item
*ei
;
1327 struct btrfs_extent_inline_ref
*iref
;
1338 key
.objectid
= bytenr
;
1339 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1340 key
.offset
= num_bytes
;
1342 want
= extent_ref_type(parent
, owner
);
1344 extra_size
= btrfs_extent_inline_ref_size(want
);
1345 path
->keep_locks
= 1;
1348 ret
= btrfs_search_slot(trans
, root
, &key
, path
, extra_size
, 1);
1355 leaf
= path
->nodes
[0];
1356 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1357 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1358 if (item_size
< sizeof(*ei
)) {
1363 ret
= convert_extent_item_v0(trans
, root
, path
, owner
,
1369 leaf
= path
->nodes
[0];
1370 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1373 BUG_ON(item_size
< sizeof(*ei
));
1375 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1376 flags
= btrfs_extent_flags(leaf
, ei
);
1378 ptr
= (unsigned long)(ei
+ 1);
1379 end
= (unsigned long)ei
+ item_size
;
1381 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
1382 ptr
+= sizeof(struct btrfs_tree_block_info
);
1385 BUG_ON(!(flags
& BTRFS_EXTENT_FLAG_DATA
));
1394 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1395 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1399 ptr
+= btrfs_extent_inline_ref_size(type
);
1403 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1404 struct btrfs_extent_data_ref
*dref
;
1405 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1406 if (match_extent_data_ref(leaf
, dref
, root_objectid
,
1411 if (hash_extent_data_ref_item(leaf
, dref
) <
1412 hash_extent_data_ref(root_objectid
, owner
, offset
))
1416 ref_offset
= btrfs_extent_inline_ref_offset(leaf
, iref
);
1418 if (parent
== ref_offset
) {
1422 if (ref_offset
< parent
)
1425 if (root_objectid
== ref_offset
) {
1429 if (ref_offset
< root_objectid
)
1433 ptr
+= btrfs_extent_inline_ref_size(type
);
1435 if (err
== -ENOENT
&& insert
) {
1436 if (item_size
+ extra_size
>=
1437 BTRFS_MAX_EXTENT_ITEM_SIZE(root
)) {
1442 * To add new inline back ref, we have to make sure
1443 * there is no corresponding back ref item.
1444 * For simplicity, we just do not add new inline back
1445 * ref if there is any kind of item for this block
1447 if (find_next_key(path
, 0, &key
) == 0 &&
1448 key
.objectid
== bytenr
&&
1449 key
.type
< BTRFS_BLOCK_GROUP_ITEM_KEY
) {
1454 *ref_ret
= (struct btrfs_extent_inline_ref
*)ptr
;
1457 path
->keep_locks
= 0;
1458 btrfs_unlock_up_safe(path
, 1);
1464 * helper to add new inline back ref
1466 static noinline_for_stack
1467 int setup_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1468 struct btrfs_root
*root
,
1469 struct btrfs_path
*path
,
1470 struct btrfs_extent_inline_ref
*iref
,
1471 u64 parent
, u64 root_objectid
,
1472 u64 owner
, u64 offset
, int refs_to_add
,
1473 struct btrfs_delayed_extent_op
*extent_op
)
1475 struct extent_buffer
*leaf
;
1476 struct btrfs_extent_item
*ei
;
1479 unsigned long item_offset
;
1485 leaf
= path
->nodes
[0];
1486 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1487 item_offset
= (unsigned long)iref
- (unsigned long)ei
;
1489 type
= extent_ref_type(parent
, owner
);
1490 size
= btrfs_extent_inline_ref_size(type
);
1492 ret
= btrfs_extend_item(trans
, root
, path
, size
);
1495 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1496 refs
= btrfs_extent_refs(leaf
, ei
);
1497 refs
+= refs_to_add
;
1498 btrfs_set_extent_refs(leaf
, ei
, refs
);
1500 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1502 ptr
= (unsigned long)ei
+ item_offset
;
1503 end
= (unsigned long)ei
+ btrfs_item_size_nr(leaf
, path
->slots
[0]);
1504 if (ptr
< end
- size
)
1505 memmove_extent_buffer(leaf
, ptr
+ size
, ptr
,
1508 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1509 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
1510 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1511 struct btrfs_extent_data_ref
*dref
;
1512 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1513 btrfs_set_extent_data_ref_root(leaf
, dref
, root_objectid
);
1514 btrfs_set_extent_data_ref_objectid(leaf
, dref
, owner
);
1515 btrfs_set_extent_data_ref_offset(leaf
, dref
, offset
);
1516 btrfs_set_extent_data_ref_count(leaf
, dref
, refs_to_add
);
1517 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1518 struct btrfs_shared_data_ref
*sref
;
1519 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1520 btrfs_set_shared_data_ref_count(leaf
, sref
, refs_to_add
);
1521 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1522 } else if (type
== BTRFS_SHARED_BLOCK_REF_KEY
) {
1523 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1525 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
1527 btrfs_mark_buffer_dirty(leaf
);
1531 static int lookup_extent_backref(struct btrfs_trans_handle
*trans
,
1532 struct btrfs_root
*root
,
1533 struct btrfs_path
*path
,
1534 struct btrfs_extent_inline_ref
**ref_ret
,
1535 u64 bytenr
, u64 num_bytes
, u64 parent
,
1536 u64 root_objectid
, u64 owner
, u64 offset
)
1540 ret
= lookup_inline_extent_backref(trans
, root
, path
, ref_ret
,
1541 bytenr
, num_bytes
, parent
,
1542 root_objectid
, owner
, offset
, 0);
1546 btrfs_release_path(root
, path
);
1549 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1550 ret
= lookup_tree_block_ref(trans
, root
, path
, bytenr
, parent
,
1553 ret
= lookup_extent_data_ref(trans
, root
, path
, bytenr
, parent
,
1554 root_objectid
, owner
, offset
);
1560 * helper to update/remove inline back ref
1562 static noinline_for_stack
1563 int update_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1564 struct btrfs_root
*root
,
1565 struct btrfs_path
*path
,
1566 struct btrfs_extent_inline_ref
*iref
,
1568 struct btrfs_delayed_extent_op
*extent_op
)
1570 struct extent_buffer
*leaf
;
1571 struct btrfs_extent_item
*ei
;
1572 struct btrfs_extent_data_ref
*dref
= NULL
;
1573 struct btrfs_shared_data_ref
*sref
= NULL
;
1582 leaf
= path
->nodes
[0];
1583 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1584 refs
= btrfs_extent_refs(leaf
, ei
);
1585 WARN_ON(refs_to_mod
< 0 && refs
+ refs_to_mod
<= 0);
1586 refs
+= refs_to_mod
;
1587 btrfs_set_extent_refs(leaf
, ei
, refs
);
1589 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1591 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1593 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1594 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1595 refs
= btrfs_extent_data_ref_count(leaf
, dref
);
1596 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1597 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1598 refs
= btrfs_shared_data_ref_count(leaf
, sref
);
1601 BUG_ON(refs_to_mod
!= -1);
1604 BUG_ON(refs_to_mod
< 0 && refs
< -refs_to_mod
);
1605 refs
+= refs_to_mod
;
1608 if (type
== BTRFS_EXTENT_DATA_REF_KEY
)
1609 btrfs_set_extent_data_ref_count(leaf
, dref
, refs
);
1611 btrfs_set_shared_data_ref_count(leaf
, sref
, refs
);
1613 size
= btrfs_extent_inline_ref_size(type
);
1614 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1615 ptr
= (unsigned long)iref
;
1616 end
= (unsigned long)ei
+ item_size
;
1617 if (ptr
+ size
< end
)
1618 memmove_extent_buffer(leaf
, ptr
, ptr
+ size
,
1621 ret
= btrfs_truncate_item(trans
, root
, path
, item_size
, 1);
1624 btrfs_mark_buffer_dirty(leaf
);
1628 static noinline_for_stack
1629 int insert_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1630 struct btrfs_root
*root
,
1631 struct btrfs_path
*path
,
1632 u64 bytenr
, u64 num_bytes
, u64 parent
,
1633 u64 root_objectid
, u64 owner
,
1634 u64 offset
, int refs_to_add
,
1635 struct btrfs_delayed_extent_op
*extent_op
)
1637 struct btrfs_extent_inline_ref
*iref
;
1640 ret
= lookup_inline_extent_backref(trans
, root
, path
, &iref
,
1641 bytenr
, num_bytes
, parent
,
1642 root_objectid
, owner
, offset
, 1);
1644 BUG_ON(owner
< BTRFS_FIRST_FREE_OBJECTID
);
1645 ret
= update_inline_extent_backref(trans
, root
, path
, iref
,
1646 refs_to_add
, extent_op
);
1647 } else if (ret
== -ENOENT
) {
1648 ret
= setup_inline_extent_backref(trans
, root
, path
, iref
,
1649 parent
, root_objectid
,
1650 owner
, offset
, refs_to_add
,
1656 static int insert_extent_backref(struct btrfs_trans_handle
*trans
,
1657 struct btrfs_root
*root
,
1658 struct btrfs_path
*path
,
1659 u64 bytenr
, u64 parent
, u64 root_objectid
,
1660 u64 owner
, u64 offset
, int refs_to_add
)
1663 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1664 BUG_ON(refs_to_add
!= 1);
1665 ret
= insert_tree_block_ref(trans
, root
, path
, bytenr
,
1666 parent
, root_objectid
);
1668 ret
= insert_extent_data_ref(trans
, root
, path
, bytenr
,
1669 parent
, root_objectid
,
1670 owner
, offset
, refs_to_add
);
1675 static int remove_extent_backref(struct btrfs_trans_handle
*trans
,
1676 struct btrfs_root
*root
,
1677 struct btrfs_path
*path
,
1678 struct btrfs_extent_inline_ref
*iref
,
1679 int refs_to_drop
, int is_data
)
1683 BUG_ON(!is_data
&& refs_to_drop
!= 1);
1685 ret
= update_inline_extent_backref(trans
, root
, path
, iref
,
1686 -refs_to_drop
, NULL
);
1687 } else if (is_data
) {
1688 ret
= remove_extent_data_ref(trans
, root
, path
, refs_to_drop
);
1690 ret
= btrfs_del_item(trans
, root
, path
);
1695 static void btrfs_issue_discard(struct block_device
*bdev
,
1698 blkdev_issue_discard(bdev
, start
>> 9, len
>> 9, GFP_KERNEL
,
1699 BLKDEV_IFL_WAIT
| BLKDEV_IFL_BARRIER
);
1702 static int btrfs_discard_extent(struct btrfs_root
*root
, u64 bytenr
,
1706 u64 map_length
= num_bytes
;
1707 struct btrfs_multi_bio
*multi
= NULL
;
1709 if (!btrfs_test_opt(root
, DISCARD
))
1712 /* Tell the block device(s) that the sectors can be discarded */
1713 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
1714 bytenr
, &map_length
, &multi
, 0);
1716 struct btrfs_bio_stripe
*stripe
= multi
->stripes
;
1719 if (map_length
> num_bytes
)
1720 map_length
= num_bytes
;
1722 for (i
= 0; i
< multi
->num_stripes
; i
++, stripe
++) {
1723 btrfs_issue_discard(stripe
->dev
->bdev
,
1733 int btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
1734 struct btrfs_root
*root
,
1735 u64 bytenr
, u64 num_bytes
, u64 parent
,
1736 u64 root_objectid
, u64 owner
, u64 offset
)
1739 BUG_ON(owner
< BTRFS_FIRST_FREE_OBJECTID
&&
1740 root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
1742 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1743 ret
= btrfs_add_delayed_tree_ref(trans
, bytenr
, num_bytes
,
1744 parent
, root_objectid
, (int)owner
,
1745 BTRFS_ADD_DELAYED_REF
, NULL
);
1747 ret
= btrfs_add_delayed_data_ref(trans
, bytenr
, num_bytes
,
1748 parent
, root_objectid
, owner
, offset
,
1749 BTRFS_ADD_DELAYED_REF
, NULL
);
1754 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
1755 struct btrfs_root
*root
,
1756 u64 bytenr
, u64 num_bytes
,
1757 u64 parent
, u64 root_objectid
,
1758 u64 owner
, u64 offset
, int refs_to_add
,
1759 struct btrfs_delayed_extent_op
*extent_op
)
1761 struct btrfs_path
*path
;
1762 struct extent_buffer
*leaf
;
1763 struct btrfs_extent_item
*item
;
1768 path
= btrfs_alloc_path();
1773 path
->leave_spinning
= 1;
1774 /* this will setup the path even if it fails to insert the back ref */
1775 ret
= insert_inline_extent_backref(trans
, root
->fs_info
->extent_root
,
1776 path
, bytenr
, num_bytes
, parent
,
1777 root_objectid
, owner
, offset
,
1778 refs_to_add
, extent_op
);
1782 if (ret
!= -EAGAIN
) {
1787 leaf
= path
->nodes
[0];
1788 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1789 refs
= btrfs_extent_refs(leaf
, item
);
1790 btrfs_set_extent_refs(leaf
, item
, refs
+ refs_to_add
);
1792 __run_delayed_extent_op(extent_op
, leaf
, item
);
1794 btrfs_mark_buffer_dirty(leaf
);
1795 btrfs_release_path(root
->fs_info
->extent_root
, path
);
1798 path
->leave_spinning
= 1;
1800 /* now insert the actual backref */
1801 ret
= insert_extent_backref(trans
, root
->fs_info
->extent_root
,
1802 path
, bytenr
, parent
, root_objectid
,
1803 owner
, offset
, refs_to_add
);
1806 btrfs_free_path(path
);
1810 static int run_delayed_data_ref(struct btrfs_trans_handle
*trans
,
1811 struct btrfs_root
*root
,
1812 struct btrfs_delayed_ref_node
*node
,
1813 struct btrfs_delayed_extent_op
*extent_op
,
1814 int insert_reserved
)
1817 struct btrfs_delayed_data_ref
*ref
;
1818 struct btrfs_key ins
;
1823 ins
.objectid
= node
->bytenr
;
1824 ins
.offset
= node
->num_bytes
;
1825 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1827 ref
= btrfs_delayed_node_to_data_ref(node
);
1828 if (node
->type
== BTRFS_SHARED_DATA_REF_KEY
)
1829 parent
= ref
->parent
;
1831 ref_root
= ref
->root
;
1833 if (node
->action
== BTRFS_ADD_DELAYED_REF
&& insert_reserved
) {
1835 BUG_ON(extent_op
->update_key
);
1836 flags
|= extent_op
->flags_to_set
;
1838 ret
= alloc_reserved_file_extent(trans
, root
,
1839 parent
, ref_root
, flags
,
1840 ref
->objectid
, ref
->offset
,
1841 &ins
, node
->ref_mod
);
1842 } else if (node
->action
== BTRFS_ADD_DELAYED_REF
) {
1843 ret
= __btrfs_inc_extent_ref(trans
, root
, node
->bytenr
,
1844 node
->num_bytes
, parent
,
1845 ref_root
, ref
->objectid
,
1846 ref
->offset
, node
->ref_mod
,
1848 } else if (node
->action
== BTRFS_DROP_DELAYED_REF
) {
1849 ret
= __btrfs_free_extent(trans
, root
, node
->bytenr
,
1850 node
->num_bytes
, parent
,
1851 ref_root
, ref
->objectid
,
1852 ref
->offset
, node
->ref_mod
,
1860 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op
*extent_op
,
1861 struct extent_buffer
*leaf
,
1862 struct btrfs_extent_item
*ei
)
1864 u64 flags
= btrfs_extent_flags(leaf
, ei
);
1865 if (extent_op
->update_flags
) {
1866 flags
|= extent_op
->flags_to_set
;
1867 btrfs_set_extent_flags(leaf
, ei
, flags
);
1870 if (extent_op
->update_key
) {
1871 struct btrfs_tree_block_info
*bi
;
1872 BUG_ON(!(flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
));
1873 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
1874 btrfs_set_tree_block_key(leaf
, bi
, &extent_op
->key
);
1878 static int run_delayed_extent_op(struct btrfs_trans_handle
*trans
,
1879 struct btrfs_root
*root
,
1880 struct btrfs_delayed_ref_node
*node
,
1881 struct btrfs_delayed_extent_op
*extent_op
)
1883 struct btrfs_key key
;
1884 struct btrfs_path
*path
;
1885 struct btrfs_extent_item
*ei
;
1886 struct extent_buffer
*leaf
;
1891 path
= btrfs_alloc_path();
1895 key
.objectid
= node
->bytenr
;
1896 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1897 key
.offset
= node
->num_bytes
;
1900 path
->leave_spinning
= 1;
1901 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
,
1912 leaf
= path
->nodes
[0];
1913 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1914 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1915 if (item_size
< sizeof(*ei
)) {
1916 ret
= convert_extent_item_v0(trans
, root
->fs_info
->extent_root
,
1922 leaf
= path
->nodes
[0];
1923 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1926 BUG_ON(item_size
< sizeof(*ei
));
1927 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1928 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1930 btrfs_mark_buffer_dirty(leaf
);
1932 btrfs_free_path(path
);
1936 static int run_delayed_tree_ref(struct btrfs_trans_handle
*trans
,
1937 struct btrfs_root
*root
,
1938 struct btrfs_delayed_ref_node
*node
,
1939 struct btrfs_delayed_extent_op
*extent_op
,
1940 int insert_reserved
)
1943 struct btrfs_delayed_tree_ref
*ref
;
1944 struct btrfs_key ins
;
1948 ins
.objectid
= node
->bytenr
;
1949 ins
.offset
= node
->num_bytes
;
1950 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1952 ref
= btrfs_delayed_node_to_tree_ref(node
);
1953 if (node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
)
1954 parent
= ref
->parent
;
1956 ref_root
= ref
->root
;
1958 BUG_ON(node
->ref_mod
!= 1);
1959 if (node
->action
== BTRFS_ADD_DELAYED_REF
&& insert_reserved
) {
1960 BUG_ON(!extent_op
|| !extent_op
->update_flags
||
1961 !extent_op
->update_key
);
1962 ret
= alloc_reserved_tree_block(trans
, root
,
1964 extent_op
->flags_to_set
,
1967 } else if (node
->action
== BTRFS_ADD_DELAYED_REF
) {
1968 ret
= __btrfs_inc_extent_ref(trans
, root
, node
->bytenr
,
1969 node
->num_bytes
, parent
, ref_root
,
1970 ref
->level
, 0, 1, extent_op
);
1971 } else if (node
->action
== BTRFS_DROP_DELAYED_REF
) {
1972 ret
= __btrfs_free_extent(trans
, root
, node
->bytenr
,
1973 node
->num_bytes
, parent
, ref_root
,
1974 ref
->level
, 0, 1, extent_op
);
1981 /* helper function to actually process a single delayed ref entry */
1982 static int run_one_delayed_ref(struct btrfs_trans_handle
*trans
,
1983 struct btrfs_root
*root
,
1984 struct btrfs_delayed_ref_node
*node
,
1985 struct btrfs_delayed_extent_op
*extent_op
,
1986 int insert_reserved
)
1989 if (btrfs_delayed_ref_is_head(node
)) {
1990 struct btrfs_delayed_ref_head
*head
;
1992 * we've hit the end of the chain and we were supposed
1993 * to insert this extent into the tree. But, it got
1994 * deleted before we ever needed to insert it, so all
1995 * we have to do is clean up the accounting
1998 head
= btrfs_delayed_node_to_head(node
);
1999 if (insert_reserved
) {
2000 btrfs_pin_extent(root
, node
->bytenr
,
2001 node
->num_bytes
, 1);
2002 if (head
->is_data
) {
2003 ret
= btrfs_del_csums(trans
, root
,
2009 mutex_unlock(&head
->mutex
);
2013 if (node
->type
== BTRFS_TREE_BLOCK_REF_KEY
||
2014 node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
)
2015 ret
= run_delayed_tree_ref(trans
, root
, node
, extent_op
,
2017 else if (node
->type
== BTRFS_EXTENT_DATA_REF_KEY
||
2018 node
->type
== BTRFS_SHARED_DATA_REF_KEY
)
2019 ret
= run_delayed_data_ref(trans
, root
, node
, extent_op
,
2026 static noinline
struct btrfs_delayed_ref_node
*
2027 select_delayed_ref(struct btrfs_delayed_ref_head
*head
)
2029 struct rb_node
*node
;
2030 struct btrfs_delayed_ref_node
*ref
;
2031 int action
= BTRFS_ADD_DELAYED_REF
;
2034 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2035 * this prevents ref count from going down to zero when
2036 * there still are pending delayed ref.
2038 node
= rb_prev(&head
->node
.rb_node
);
2042 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
,
2044 if (ref
->bytenr
!= head
->node
.bytenr
)
2046 if (ref
->action
== action
)
2048 node
= rb_prev(node
);
2050 if (action
== BTRFS_ADD_DELAYED_REF
) {
2051 action
= BTRFS_DROP_DELAYED_REF
;
2057 static noinline
int run_clustered_refs(struct btrfs_trans_handle
*trans
,
2058 struct btrfs_root
*root
,
2059 struct list_head
*cluster
)
2061 struct btrfs_delayed_ref_root
*delayed_refs
;
2062 struct btrfs_delayed_ref_node
*ref
;
2063 struct btrfs_delayed_ref_head
*locked_ref
= NULL
;
2064 struct btrfs_delayed_extent_op
*extent_op
;
2067 int must_insert_reserved
= 0;
2069 delayed_refs
= &trans
->transaction
->delayed_refs
;
2072 /* pick a new head ref from the cluster list */
2073 if (list_empty(cluster
))
2076 locked_ref
= list_entry(cluster
->next
,
2077 struct btrfs_delayed_ref_head
, cluster
);
2079 /* grab the lock that says we are going to process
2080 * all the refs for this head */
2081 ret
= btrfs_delayed_ref_lock(trans
, locked_ref
);
2084 * we may have dropped the spin lock to get the head
2085 * mutex lock, and that might have given someone else
2086 * time to free the head. If that's true, it has been
2087 * removed from our list and we can move on.
2089 if (ret
== -EAGAIN
) {
2097 * record the must insert reserved flag before we
2098 * drop the spin lock.
2100 must_insert_reserved
= locked_ref
->must_insert_reserved
;
2101 locked_ref
->must_insert_reserved
= 0;
2103 extent_op
= locked_ref
->extent_op
;
2104 locked_ref
->extent_op
= NULL
;
2107 * locked_ref is the head node, so we have to go one
2108 * node back for any delayed ref updates
2110 ref
= select_delayed_ref(locked_ref
);
2112 /* All delayed refs have been processed, Go ahead
2113 * and send the head node to run_one_delayed_ref,
2114 * so that any accounting fixes can happen
2116 ref
= &locked_ref
->node
;
2118 if (extent_op
&& must_insert_reserved
) {
2124 spin_unlock(&delayed_refs
->lock
);
2126 ret
= run_delayed_extent_op(trans
, root
,
2132 spin_lock(&delayed_refs
->lock
);
2136 list_del_init(&locked_ref
->cluster
);
2141 rb_erase(&ref
->rb_node
, &delayed_refs
->root
);
2142 delayed_refs
->num_entries
--;
2144 spin_unlock(&delayed_refs
->lock
);
2146 ret
= run_one_delayed_ref(trans
, root
, ref
, extent_op
,
2147 must_insert_reserved
);
2150 btrfs_put_delayed_ref(ref
);
2155 spin_lock(&delayed_refs
->lock
);
2161 * this starts processing the delayed reference count updates and
2162 * extent insertions we have queued up so far. count can be
2163 * 0, which means to process everything in the tree at the start
2164 * of the run (but not newly added entries), or it can be some target
2165 * number you'd like to process.
2167 int btrfs_run_delayed_refs(struct btrfs_trans_handle
*trans
,
2168 struct btrfs_root
*root
, unsigned long count
)
2170 struct rb_node
*node
;
2171 struct btrfs_delayed_ref_root
*delayed_refs
;
2172 struct btrfs_delayed_ref_node
*ref
;
2173 struct list_head cluster
;
2175 int run_all
= count
== (unsigned long)-1;
2178 if (root
== root
->fs_info
->extent_root
)
2179 root
= root
->fs_info
->tree_root
;
2181 delayed_refs
= &trans
->transaction
->delayed_refs
;
2182 INIT_LIST_HEAD(&cluster
);
2184 spin_lock(&delayed_refs
->lock
);
2186 count
= delayed_refs
->num_entries
* 2;
2190 if (!(run_all
|| run_most
) &&
2191 delayed_refs
->num_heads_ready
< 64)
2195 * go find something we can process in the rbtree. We start at
2196 * the beginning of the tree, and then build a cluster
2197 * of refs to process starting at the first one we are able to
2200 ret
= btrfs_find_ref_cluster(trans
, &cluster
,
2201 delayed_refs
->run_delayed_start
);
2205 ret
= run_clustered_refs(trans
, root
, &cluster
);
2208 count
-= min_t(unsigned long, ret
, count
);
2215 node
= rb_first(&delayed_refs
->root
);
2218 count
= (unsigned long)-1;
2221 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
,
2223 if (btrfs_delayed_ref_is_head(ref
)) {
2224 struct btrfs_delayed_ref_head
*head
;
2226 head
= btrfs_delayed_node_to_head(ref
);
2227 atomic_inc(&ref
->refs
);
2229 spin_unlock(&delayed_refs
->lock
);
2230 mutex_lock(&head
->mutex
);
2231 mutex_unlock(&head
->mutex
);
2233 btrfs_put_delayed_ref(ref
);
2237 node
= rb_next(node
);
2239 spin_unlock(&delayed_refs
->lock
);
2240 schedule_timeout(1);
2244 spin_unlock(&delayed_refs
->lock
);
2248 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle
*trans
,
2249 struct btrfs_root
*root
,
2250 u64 bytenr
, u64 num_bytes
, u64 flags
,
2253 struct btrfs_delayed_extent_op
*extent_op
;
2256 extent_op
= kmalloc(sizeof(*extent_op
), GFP_NOFS
);
2260 extent_op
->flags_to_set
= flags
;
2261 extent_op
->update_flags
= 1;
2262 extent_op
->update_key
= 0;
2263 extent_op
->is_data
= is_data
? 1 : 0;
2265 ret
= btrfs_add_delayed_extent_op(trans
, bytenr
, num_bytes
, extent_op
);
2271 static noinline
int check_delayed_ref(struct btrfs_trans_handle
*trans
,
2272 struct btrfs_root
*root
,
2273 struct btrfs_path
*path
,
2274 u64 objectid
, u64 offset
, u64 bytenr
)
2276 struct btrfs_delayed_ref_head
*head
;
2277 struct btrfs_delayed_ref_node
*ref
;
2278 struct btrfs_delayed_data_ref
*data_ref
;
2279 struct btrfs_delayed_ref_root
*delayed_refs
;
2280 struct rb_node
*node
;
2284 delayed_refs
= &trans
->transaction
->delayed_refs
;
2285 spin_lock(&delayed_refs
->lock
);
2286 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
2290 if (!mutex_trylock(&head
->mutex
)) {
2291 atomic_inc(&head
->node
.refs
);
2292 spin_unlock(&delayed_refs
->lock
);
2294 btrfs_release_path(root
->fs_info
->extent_root
, path
);
2296 mutex_lock(&head
->mutex
);
2297 mutex_unlock(&head
->mutex
);
2298 btrfs_put_delayed_ref(&head
->node
);
2302 node
= rb_prev(&head
->node
.rb_node
);
2306 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
2308 if (ref
->bytenr
!= bytenr
)
2312 if (ref
->type
!= BTRFS_EXTENT_DATA_REF_KEY
)
2315 data_ref
= btrfs_delayed_node_to_data_ref(ref
);
2317 node
= rb_prev(node
);
2319 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
2320 if (ref
->bytenr
== bytenr
)
2324 if (data_ref
->root
!= root
->root_key
.objectid
||
2325 data_ref
->objectid
!= objectid
|| data_ref
->offset
!= offset
)
2330 mutex_unlock(&head
->mutex
);
2332 spin_unlock(&delayed_refs
->lock
);
2336 static noinline
int check_committed_ref(struct btrfs_trans_handle
*trans
,
2337 struct btrfs_root
*root
,
2338 struct btrfs_path
*path
,
2339 u64 objectid
, u64 offset
, u64 bytenr
)
2341 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
2342 struct extent_buffer
*leaf
;
2343 struct btrfs_extent_data_ref
*ref
;
2344 struct btrfs_extent_inline_ref
*iref
;
2345 struct btrfs_extent_item
*ei
;
2346 struct btrfs_key key
;
2350 key
.objectid
= bytenr
;
2351 key
.offset
= (u64
)-1;
2352 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2354 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
2360 if (path
->slots
[0] == 0)
2364 leaf
= path
->nodes
[0];
2365 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2367 if (key
.objectid
!= bytenr
|| key
.type
!= BTRFS_EXTENT_ITEM_KEY
)
2371 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2372 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2373 if (item_size
< sizeof(*ei
)) {
2374 WARN_ON(item_size
!= sizeof(struct btrfs_extent_item_v0
));
2378 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
2380 if (item_size
!= sizeof(*ei
) +
2381 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY
))
2384 if (btrfs_extent_generation(leaf
, ei
) <=
2385 btrfs_root_last_snapshot(&root
->root_item
))
2388 iref
= (struct btrfs_extent_inline_ref
*)(ei
+ 1);
2389 if (btrfs_extent_inline_ref_type(leaf
, iref
) !=
2390 BTRFS_EXTENT_DATA_REF_KEY
)
2393 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
2394 if (btrfs_extent_refs(leaf
, ei
) !=
2395 btrfs_extent_data_ref_count(leaf
, ref
) ||
2396 btrfs_extent_data_ref_root(leaf
, ref
) !=
2397 root
->root_key
.objectid
||
2398 btrfs_extent_data_ref_objectid(leaf
, ref
) != objectid
||
2399 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
2407 int btrfs_cross_ref_exist(struct btrfs_trans_handle
*trans
,
2408 struct btrfs_root
*root
,
2409 u64 objectid
, u64 offset
, u64 bytenr
)
2411 struct btrfs_path
*path
;
2415 path
= btrfs_alloc_path();
2420 ret
= check_committed_ref(trans
, root
, path
, objectid
,
2422 if (ret
&& ret
!= -ENOENT
)
2425 ret2
= check_delayed_ref(trans
, root
, path
, objectid
,
2427 } while (ret2
== -EAGAIN
);
2429 if (ret2
&& ret2
!= -ENOENT
) {
2434 if (ret
!= -ENOENT
|| ret2
!= -ENOENT
)
2437 btrfs_free_path(path
);
2438 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
2444 int btrfs_cache_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2445 struct extent_buffer
*buf
, u32 nr_extents
)
2447 struct btrfs_key key
;
2448 struct btrfs_file_extent_item
*fi
;
2456 if (!root
->ref_cows
)
2459 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
2461 root_gen
= root
->root_key
.offset
;
2464 root_gen
= trans
->transid
- 1;
2467 level
= btrfs_header_level(buf
);
2468 nritems
= btrfs_header_nritems(buf
);
2471 struct btrfs_leaf_ref
*ref
;
2472 struct btrfs_extent_info
*info
;
2474 ref
= btrfs_alloc_leaf_ref(root
, nr_extents
);
2480 ref
->root_gen
= root_gen
;
2481 ref
->bytenr
= buf
->start
;
2482 ref
->owner
= btrfs_header_owner(buf
);
2483 ref
->generation
= btrfs_header_generation(buf
);
2484 ref
->nritems
= nr_extents
;
2485 info
= ref
->extents
;
2487 for (i
= 0; nr_extents
> 0 && i
< nritems
; i
++) {
2489 btrfs_item_key_to_cpu(buf
, &key
, i
);
2490 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
2492 fi
= btrfs_item_ptr(buf
, i
,
2493 struct btrfs_file_extent_item
);
2494 if (btrfs_file_extent_type(buf
, fi
) ==
2495 BTRFS_FILE_EXTENT_INLINE
)
2497 disk_bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
2498 if (disk_bytenr
== 0)
2501 info
->bytenr
= disk_bytenr
;
2503 btrfs_file_extent_disk_num_bytes(buf
, fi
);
2504 info
->objectid
= key
.objectid
;
2505 info
->offset
= key
.offset
;
2509 ret
= btrfs_add_leaf_ref(root
, ref
, shared
);
2510 if (ret
== -EEXIST
&& shared
) {
2511 struct btrfs_leaf_ref
*old
;
2512 old
= btrfs_lookup_leaf_ref(root
, ref
->bytenr
);
2514 btrfs_remove_leaf_ref(root
, old
);
2515 btrfs_free_leaf_ref(root
, old
);
2516 ret
= btrfs_add_leaf_ref(root
, ref
, shared
);
2519 btrfs_free_leaf_ref(root
, ref
);
2525 /* when a block goes through cow, we update the reference counts of
2526 * everything that block points to. The internal pointers of the block
2527 * can be in just about any order, and it is likely to have clusters of
2528 * things that are close together and clusters of things that are not.
2530 * To help reduce the seeks that come with updating all of these reference
2531 * counts, sort them by byte number before actual updates are done.
2533 * struct refsort is used to match byte number to slot in the btree block.
2534 * we sort based on the byte number and then use the slot to actually
2537 * struct refsort is smaller than strcut btrfs_item and smaller than
2538 * struct btrfs_key_ptr. Since we're currently limited to the page size
2539 * for a btree block, there's no way for a kmalloc of refsorts for a
2540 * single node to be bigger than a page.
2548 * for passing into sort()
2550 static int refsort_cmp(const void *a_void
, const void *b_void
)
2552 const struct refsort
*a
= a_void
;
2553 const struct refsort
*b
= b_void
;
2555 if (a
->bytenr
< b
->bytenr
)
2557 if (a
->bytenr
> b
->bytenr
)
2563 static int __btrfs_mod_ref(struct btrfs_trans_handle
*trans
,
2564 struct btrfs_root
*root
,
2565 struct extent_buffer
*buf
,
2566 int full_backref
, int inc
)
2573 struct btrfs_key key
;
2574 struct btrfs_file_extent_item
*fi
;
2578 int (*process_func
)(struct btrfs_trans_handle
*, struct btrfs_root
*,
2579 u64
, u64
, u64
, u64
, u64
, u64
);
2581 ref_root
= btrfs_header_owner(buf
);
2582 nritems
= btrfs_header_nritems(buf
);
2583 level
= btrfs_header_level(buf
);
2585 if (!root
->ref_cows
&& level
== 0)
2589 process_func
= btrfs_inc_extent_ref
;
2591 process_func
= btrfs_free_extent
;
2594 parent
= buf
->start
;
2598 for (i
= 0; i
< nritems
; i
++) {
2600 btrfs_item_key_to_cpu(buf
, &key
, i
);
2601 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
2603 fi
= btrfs_item_ptr(buf
, i
,
2604 struct btrfs_file_extent_item
);
2605 if (btrfs_file_extent_type(buf
, fi
) ==
2606 BTRFS_FILE_EXTENT_INLINE
)
2608 bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
2612 num_bytes
= btrfs_file_extent_disk_num_bytes(buf
, fi
);
2613 key
.offset
-= btrfs_file_extent_offset(buf
, fi
);
2614 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
2615 parent
, ref_root
, key
.objectid
,
2620 bytenr
= btrfs_node_blockptr(buf
, i
);
2621 num_bytes
= btrfs_level_size(root
, level
- 1);
2622 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
2623 parent
, ref_root
, level
- 1, 0);
2634 int btrfs_inc_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2635 struct extent_buffer
*buf
, int full_backref
)
2637 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 1);
2640 int btrfs_dec_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2641 struct extent_buffer
*buf
, int full_backref
)
2643 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 0);
2646 static int write_one_cache_group(struct btrfs_trans_handle
*trans
,
2647 struct btrfs_root
*root
,
2648 struct btrfs_path
*path
,
2649 struct btrfs_block_group_cache
*cache
)
2652 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
2654 struct extent_buffer
*leaf
;
2656 ret
= btrfs_search_slot(trans
, extent_root
, &cache
->key
, path
, 0, 1);
2661 leaf
= path
->nodes
[0];
2662 bi
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
2663 write_extent_buffer(leaf
, &cache
->item
, bi
, sizeof(cache
->item
));
2664 btrfs_mark_buffer_dirty(leaf
);
2665 btrfs_release_path(extent_root
, path
);
2673 static struct btrfs_block_group_cache
*
2674 next_block_group(struct btrfs_root
*root
,
2675 struct btrfs_block_group_cache
*cache
)
2677 struct rb_node
*node
;
2678 spin_lock(&root
->fs_info
->block_group_cache_lock
);
2679 node
= rb_next(&cache
->cache_node
);
2680 btrfs_put_block_group(cache
);
2682 cache
= rb_entry(node
, struct btrfs_block_group_cache
,
2684 btrfs_get_block_group(cache
);
2687 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
2691 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle
*trans
,
2692 struct btrfs_root
*root
)
2694 struct btrfs_block_group_cache
*cache
;
2696 struct btrfs_path
*path
;
2699 path
= btrfs_alloc_path();
2705 err
= btrfs_run_delayed_refs(trans
, root
,
2710 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
2714 cache
= next_block_group(root
, cache
);
2724 last
= cache
->key
.objectid
+ cache
->key
.offset
;
2726 err
= write_one_cache_group(trans
, root
, path
, cache
);
2728 btrfs_put_block_group(cache
);
2731 btrfs_free_path(path
);
2735 int btrfs_extent_readonly(struct btrfs_root
*root
, u64 bytenr
)
2737 struct btrfs_block_group_cache
*block_group
;
2740 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
2741 if (!block_group
|| block_group
->ro
)
2744 btrfs_put_block_group(block_group
);
2748 static int update_space_info(struct btrfs_fs_info
*info
, u64 flags
,
2749 u64 total_bytes
, u64 bytes_used
,
2750 struct btrfs_space_info
**space_info
)
2752 struct btrfs_space_info
*found
;
2756 if (flags
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
2757 BTRFS_BLOCK_GROUP_RAID10
))
2762 found
= __find_space_info(info
, flags
);
2764 spin_lock(&found
->lock
);
2765 found
->total_bytes
+= total_bytes
;
2766 found
->bytes_used
+= bytes_used
;
2767 found
->disk_used
+= bytes_used
* factor
;
2769 spin_unlock(&found
->lock
);
2770 *space_info
= found
;
2773 found
= kzalloc(sizeof(*found
), GFP_NOFS
);
2777 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++)
2778 INIT_LIST_HEAD(&found
->block_groups
[i
]);
2779 init_rwsem(&found
->groups_sem
);
2780 spin_lock_init(&found
->lock
);
2781 found
->flags
= flags
& (BTRFS_BLOCK_GROUP_DATA
|
2782 BTRFS_BLOCK_GROUP_SYSTEM
|
2783 BTRFS_BLOCK_GROUP_METADATA
);
2784 found
->total_bytes
= total_bytes
;
2785 found
->bytes_used
= bytes_used
;
2786 found
->disk_used
= bytes_used
* factor
;
2787 found
->bytes_pinned
= 0;
2788 found
->bytes_reserved
= 0;
2789 found
->bytes_readonly
= 0;
2790 found
->bytes_may_use
= 0;
2792 found
->force_alloc
= 0;
2793 *space_info
= found
;
2794 list_add_rcu(&found
->list
, &info
->space_info
);
2795 atomic_set(&found
->caching_threads
, 0);
2799 static void set_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
2801 u64 extra_flags
= flags
& (BTRFS_BLOCK_GROUP_RAID0
|
2802 BTRFS_BLOCK_GROUP_RAID1
|
2803 BTRFS_BLOCK_GROUP_RAID10
|
2804 BTRFS_BLOCK_GROUP_DUP
);
2806 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
2807 fs_info
->avail_data_alloc_bits
|= extra_flags
;
2808 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
2809 fs_info
->avail_metadata_alloc_bits
|= extra_flags
;
2810 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
2811 fs_info
->avail_system_alloc_bits
|= extra_flags
;
2815 u64
btrfs_reduce_alloc_profile(struct btrfs_root
*root
, u64 flags
)
2817 u64 num_devices
= root
->fs_info
->fs_devices
->rw_devices
;
2819 if (num_devices
== 1)
2820 flags
&= ~(BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID0
);
2821 if (num_devices
< 4)
2822 flags
&= ~BTRFS_BLOCK_GROUP_RAID10
;
2824 if ((flags
& BTRFS_BLOCK_GROUP_DUP
) &&
2825 (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
2826 BTRFS_BLOCK_GROUP_RAID10
))) {
2827 flags
&= ~BTRFS_BLOCK_GROUP_DUP
;
2830 if ((flags
& BTRFS_BLOCK_GROUP_RAID1
) &&
2831 (flags
& BTRFS_BLOCK_GROUP_RAID10
)) {
2832 flags
&= ~BTRFS_BLOCK_GROUP_RAID1
;
2835 if ((flags
& BTRFS_BLOCK_GROUP_RAID0
) &&
2836 ((flags
& BTRFS_BLOCK_GROUP_RAID1
) |
2837 (flags
& BTRFS_BLOCK_GROUP_RAID10
) |
2838 (flags
& BTRFS_BLOCK_GROUP_DUP
)))
2839 flags
&= ~BTRFS_BLOCK_GROUP_RAID0
;
2843 static u64
get_alloc_profile(struct btrfs_root
*root
, u64 flags
)
2845 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
2846 flags
|= root
->fs_info
->avail_data_alloc_bits
&
2847 root
->fs_info
->data_alloc_profile
;
2848 else if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
2849 flags
|= root
->fs_info
->avail_system_alloc_bits
&
2850 root
->fs_info
->system_alloc_profile
;
2851 else if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
2852 flags
|= root
->fs_info
->avail_metadata_alloc_bits
&
2853 root
->fs_info
->metadata_alloc_profile
;
2854 return btrfs_reduce_alloc_profile(root
, flags
);
2857 static u64
btrfs_get_alloc_profile(struct btrfs_root
*root
, int data
)
2862 flags
= BTRFS_BLOCK_GROUP_DATA
;
2863 else if (root
== root
->fs_info
->chunk_root
)
2864 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
2866 flags
= BTRFS_BLOCK_GROUP_METADATA
;
2868 return get_alloc_profile(root
, flags
);
2871 void btrfs_set_inode_space_info(struct btrfs_root
*root
, struct inode
*inode
)
2873 BTRFS_I(inode
)->space_info
= __find_space_info(root
->fs_info
,
2874 BTRFS_BLOCK_GROUP_DATA
);
2878 * This will check the space that the inode allocates from to make sure we have
2879 * enough space for bytes.
2881 int btrfs_check_data_free_space(struct inode
*inode
, u64 bytes
)
2883 struct btrfs_space_info
*data_sinfo
;
2884 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2886 int ret
= 0, committed
= 0;
2888 /* make sure bytes are sectorsize aligned */
2889 bytes
= (bytes
+ root
->sectorsize
- 1) & ~((u64
)root
->sectorsize
- 1);
2891 data_sinfo
= BTRFS_I(inode
)->space_info
;
2896 /* make sure we have enough space to handle the data first */
2897 spin_lock(&data_sinfo
->lock
);
2898 used
= data_sinfo
->bytes_used
+ data_sinfo
->bytes_reserved
+
2899 data_sinfo
->bytes_pinned
+ data_sinfo
->bytes_readonly
+
2900 data_sinfo
->bytes_may_use
;
2902 if (used
+ bytes
> data_sinfo
->total_bytes
) {
2903 struct btrfs_trans_handle
*trans
;
2906 * if we don't have enough free bytes in this space then we need
2907 * to alloc a new chunk.
2909 if (!data_sinfo
->full
) {
2912 data_sinfo
->force_alloc
= 1;
2913 spin_unlock(&data_sinfo
->lock
);
2915 alloc_target
= btrfs_get_alloc_profile(root
, 1);
2916 trans
= btrfs_join_transaction(root
, 1);
2918 return PTR_ERR(trans
);
2920 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
2921 bytes
+ 2 * 1024 * 1024,
2923 btrfs_end_transaction(trans
, root
);
2928 btrfs_set_inode_space_info(root
, inode
);
2929 data_sinfo
= BTRFS_I(inode
)->space_info
;
2933 spin_unlock(&data_sinfo
->lock
);
2935 /* commit the current transaction and try again */
2936 if (!committed
&& !root
->fs_info
->open_ioctl_trans
) {
2938 trans
= btrfs_join_transaction(root
, 1);
2940 return PTR_ERR(trans
);
2941 ret
= btrfs_commit_transaction(trans
, root
);
2947 #if 0 /* I hope we never need this code again, just in case */
2948 printk(KERN_ERR
"no space left, need %llu, %llu bytes_used, "
2949 "%llu bytes_reserved, " "%llu bytes_pinned, "
2950 "%llu bytes_readonly, %llu may use %llu total\n",
2951 (unsigned long long)bytes
,
2952 (unsigned long long)data_sinfo
->bytes_used
,
2953 (unsigned long long)data_sinfo
->bytes_reserved
,
2954 (unsigned long long)data_sinfo
->bytes_pinned
,
2955 (unsigned long long)data_sinfo
->bytes_readonly
,
2956 (unsigned long long)data_sinfo
->bytes_may_use
,
2957 (unsigned long long)data_sinfo
->total_bytes
);
2961 data_sinfo
->bytes_may_use
+= bytes
;
2962 BTRFS_I(inode
)->reserved_bytes
+= bytes
;
2963 spin_unlock(&data_sinfo
->lock
);
2969 * called when we are clearing an delalloc extent from the
2970 * inode's io_tree or there was an error for whatever reason
2971 * after calling btrfs_check_data_free_space
2973 void btrfs_free_reserved_data_space(struct inode
*inode
, u64 bytes
)
2975 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2976 struct btrfs_space_info
*data_sinfo
;
2978 /* make sure bytes are sectorsize aligned */
2979 bytes
= (bytes
+ root
->sectorsize
- 1) & ~((u64
)root
->sectorsize
- 1);
2981 data_sinfo
= BTRFS_I(inode
)->space_info
;
2982 spin_lock(&data_sinfo
->lock
);
2983 data_sinfo
->bytes_may_use
-= bytes
;
2984 BTRFS_I(inode
)->reserved_bytes
-= bytes
;
2985 spin_unlock(&data_sinfo
->lock
);
2988 static void force_metadata_allocation(struct btrfs_fs_info
*info
)
2990 struct list_head
*head
= &info
->space_info
;
2991 struct btrfs_space_info
*found
;
2994 list_for_each_entry_rcu(found
, head
, list
) {
2995 if (found
->flags
& BTRFS_BLOCK_GROUP_METADATA
)
2996 found
->force_alloc
= 1;
3001 static int should_alloc_chunk(struct btrfs_space_info
*sinfo
,
3004 u64 num_bytes
= sinfo
->total_bytes
- sinfo
->bytes_readonly
;
3006 if (sinfo
->bytes_used
+ sinfo
->bytes_reserved
+
3007 alloc_bytes
+ 256 * 1024 * 1024 < num_bytes
)
3010 if (sinfo
->bytes_used
+ sinfo
->bytes_reserved
+
3011 alloc_bytes
< div_factor(num_bytes
, 8))
3017 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
3018 struct btrfs_root
*extent_root
, u64 alloc_bytes
,
3019 u64 flags
, int force
)
3021 struct btrfs_space_info
*space_info
;
3022 struct btrfs_fs_info
*fs_info
= extent_root
->fs_info
;
3025 mutex_lock(&fs_info
->chunk_mutex
);
3027 flags
= btrfs_reduce_alloc_profile(extent_root
, flags
);
3029 space_info
= __find_space_info(extent_root
->fs_info
, flags
);
3031 ret
= update_space_info(extent_root
->fs_info
, flags
,
3035 BUG_ON(!space_info
);
3037 spin_lock(&space_info
->lock
);
3038 if (space_info
->force_alloc
)
3040 if (space_info
->full
) {
3041 spin_unlock(&space_info
->lock
);
3045 if (!force
&& !should_alloc_chunk(space_info
, alloc_bytes
)) {
3046 spin_unlock(&space_info
->lock
);
3049 spin_unlock(&space_info
->lock
);
3052 * if we're doing a data chunk, go ahead and make sure that
3053 * we keep a reasonable number of metadata chunks allocated in the
3056 if (flags
& BTRFS_BLOCK_GROUP_DATA
&& fs_info
->metadata_ratio
) {
3057 fs_info
->data_chunk_allocations
++;
3058 if (!(fs_info
->data_chunk_allocations
%
3059 fs_info
->metadata_ratio
))
3060 force_metadata_allocation(fs_info
);
3063 ret
= btrfs_alloc_chunk(trans
, extent_root
, flags
);
3064 spin_lock(&space_info
->lock
);
3066 space_info
->full
= 1;
3069 space_info
->force_alloc
= 0;
3070 spin_unlock(&space_info
->lock
);
3072 mutex_unlock(&extent_root
->fs_info
->chunk_mutex
);
3076 static int maybe_allocate_chunk(struct btrfs_trans_handle
*trans
,
3077 struct btrfs_root
*root
,
3078 struct btrfs_space_info
*sinfo
, u64 num_bytes
)
3086 spin_lock(&sinfo
->lock
);
3087 ret
= should_alloc_chunk(sinfo
, num_bytes
+ 2 * 1024 * 1024);
3088 spin_unlock(&sinfo
->lock
);
3093 trans
= btrfs_join_transaction(root
, 1);
3094 BUG_ON(IS_ERR(trans
));
3098 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
3099 num_bytes
+ 2 * 1024 * 1024,
3100 get_alloc_profile(root
, sinfo
->flags
), 0);
3103 btrfs_end_transaction(trans
, root
);
3105 return ret
== 1 ? 1 : 0;
3109 * shrink metadata reservation for delalloc
3111 static int shrink_delalloc(struct btrfs_trans_handle
*trans
,
3112 struct btrfs_root
*root
, u64 to_reclaim
)
3114 struct btrfs_block_rsv
*block_rsv
;
3121 block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
3122 spin_lock(&block_rsv
->lock
);
3123 reserved
= block_rsv
->reserved
;
3124 spin_unlock(&block_rsv
->lock
);
3129 max_reclaim
= min(reserved
, to_reclaim
);
3132 ret
= btrfs_start_one_delalloc_inode(root
, trans
? 1 : 0);
3134 __set_current_state(TASK_INTERRUPTIBLE
);
3135 schedule_timeout(pause
);
3137 if (pause
> HZ
/ 10)
3143 spin_lock(&block_rsv
->lock
);
3144 if (reserved
> block_rsv
->reserved
)
3145 reclaimed
= reserved
- block_rsv
->reserved
;
3146 reserved
= block_rsv
->reserved
;
3147 spin_unlock(&block_rsv
->lock
);
3149 if (reserved
== 0 || reclaimed
>= max_reclaim
)
3152 if (trans
&& trans
->transaction
->blocked
)
3155 return reclaimed
>= to_reclaim
;
3158 static int should_retry_reserve(struct btrfs_trans_handle
*trans
,
3159 struct btrfs_root
*root
,
3160 struct btrfs_block_rsv
*block_rsv
,
3161 u64 num_bytes
, int *retries
)
3163 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
3169 ret
= maybe_allocate_chunk(trans
, root
, space_info
, num_bytes
);
3173 if (trans
&& trans
->transaction
->in_commit
)
3176 ret
= shrink_delalloc(trans
, root
, num_bytes
);
3180 spin_lock(&space_info
->lock
);
3181 if (space_info
->bytes_pinned
< num_bytes
)
3183 spin_unlock(&space_info
->lock
);
3192 trans
= btrfs_join_transaction(root
, 1);
3193 BUG_ON(IS_ERR(trans
));
3194 ret
= btrfs_commit_transaction(trans
, root
);
3200 static int reserve_metadata_bytes(struct btrfs_block_rsv
*block_rsv
,
3203 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
3207 spin_lock(&space_info
->lock
);
3208 unused
= space_info
->bytes_used
+ space_info
->bytes_reserved
+
3209 space_info
->bytes_pinned
+ space_info
->bytes_readonly
;
3211 if (unused
< space_info
->total_bytes
)
3212 unused
= space_info
->total_bytes
- unused
;
3216 if (unused
>= num_bytes
) {
3217 if (block_rsv
->priority
>= 10) {
3218 space_info
->bytes_reserved
+= num_bytes
;
3221 if ((unused
+ block_rsv
->reserved
) *
3222 block_rsv
->priority
>=
3223 (num_bytes
+ block_rsv
->reserved
) * 10) {
3224 space_info
->bytes_reserved
+= num_bytes
;
3229 spin_unlock(&space_info
->lock
);
3234 static struct btrfs_block_rsv
*get_block_rsv(struct btrfs_trans_handle
*trans
,
3235 struct btrfs_root
*root
)
3237 struct btrfs_block_rsv
*block_rsv
;
3239 block_rsv
= trans
->block_rsv
;
3241 block_rsv
= root
->block_rsv
;
3244 block_rsv
= &root
->fs_info
->empty_block_rsv
;
3249 static int block_rsv_use_bytes(struct btrfs_block_rsv
*block_rsv
,
3253 spin_lock(&block_rsv
->lock
);
3254 if (block_rsv
->reserved
>= num_bytes
) {
3255 block_rsv
->reserved
-= num_bytes
;
3256 if (block_rsv
->reserved
< block_rsv
->size
)
3257 block_rsv
->full
= 0;
3260 spin_unlock(&block_rsv
->lock
);
3264 static void block_rsv_add_bytes(struct btrfs_block_rsv
*block_rsv
,
3265 u64 num_bytes
, int update_size
)
3267 spin_lock(&block_rsv
->lock
);
3268 block_rsv
->reserved
+= num_bytes
;
3270 block_rsv
->size
+= num_bytes
;
3271 else if (block_rsv
->reserved
>= block_rsv
->size
)
3272 block_rsv
->full
= 1;
3273 spin_unlock(&block_rsv
->lock
);
3276 void block_rsv_release_bytes(struct btrfs_block_rsv
*block_rsv
,
3277 struct btrfs_block_rsv
*dest
, u64 num_bytes
)
3279 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
3281 spin_lock(&block_rsv
->lock
);
3282 if (num_bytes
== (u64
)-1)
3283 num_bytes
= block_rsv
->size
;
3284 block_rsv
->size
-= num_bytes
;
3285 if (block_rsv
->reserved
>= block_rsv
->size
) {
3286 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
3287 block_rsv
->reserved
= block_rsv
->size
;
3288 block_rsv
->full
= 1;
3292 spin_unlock(&block_rsv
->lock
);
3294 if (num_bytes
> 0) {
3296 block_rsv_add_bytes(dest
, num_bytes
, 0);
3298 spin_lock(&space_info
->lock
);
3299 space_info
->bytes_reserved
-= num_bytes
;
3300 spin_unlock(&space_info
->lock
);
3305 static int block_rsv_migrate_bytes(struct btrfs_block_rsv
*src
,
3306 struct btrfs_block_rsv
*dst
, u64 num_bytes
)
3310 ret
= block_rsv_use_bytes(src
, num_bytes
);
3314 block_rsv_add_bytes(dst
, num_bytes
, 1);
3318 void btrfs_init_block_rsv(struct btrfs_block_rsv
*rsv
)
3320 memset(rsv
, 0, sizeof(*rsv
));
3321 spin_lock_init(&rsv
->lock
);
3322 atomic_set(&rsv
->usage
, 1);
3324 INIT_LIST_HEAD(&rsv
->list
);
3327 struct btrfs_block_rsv
*btrfs_alloc_block_rsv(struct btrfs_root
*root
)
3329 struct btrfs_block_rsv
*block_rsv
;
3330 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3333 block_rsv
= kmalloc(sizeof(*block_rsv
), GFP_NOFS
);
3337 btrfs_init_block_rsv(block_rsv
);
3339 alloc_target
= btrfs_get_alloc_profile(root
, 0);
3340 block_rsv
->space_info
= __find_space_info(fs_info
,
3341 BTRFS_BLOCK_GROUP_METADATA
);
3346 void btrfs_free_block_rsv(struct btrfs_root
*root
,
3347 struct btrfs_block_rsv
*rsv
)
3349 if (rsv
&& atomic_dec_and_test(&rsv
->usage
)) {
3350 btrfs_block_rsv_release(root
, rsv
, (u64
)-1);
3357 * make the block_rsv struct be able to capture freed space.
3358 * the captured space will re-add to the the block_rsv struct
3359 * after transaction commit
3361 void btrfs_add_durable_block_rsv(struct btrfs_fs_info
*fs_info
,
3362 struct btrfs_block_rsv
*block_rsv
)
3364 block_rsv
->durable
= 1;
3365 mutex_lock(&fs_info
->durable_block_rsv_mutex
);
3366 list_add_tail(&block_rsv
->list
, &fs_info
->durable_block_rsv_list
);
3367 mutex_unlock(&fs_info
->durable_block_rsv_mutex
);
3370 int btrfs_block_rsv_add(struct btrfs_trans_handle
*trans
,
3371 struct btrfs_root
*root
,
3372 struct btrfs_block_rsv
*block_rsv
,
3373 u64 num_bytes
, int *retries
)
3380 ret
= reserve_metadata_bytes(block_rsv
, num_bytes
);
3382 block_rsv_add_bytes(block_rsv
, num_bytes
, 1);
3386 ret
= should_retry_reserve(trans
, root
, block_rsv
, num_bytes
, retries
);
3393 int btrfs_block_rsv_check(struct btrfs_trans_handle
*trans
,
3394 struct btrfs_root
*root
,
3395 struct btrfs_block_rsv
*block_rsv
,
3396 u64 min_reserved
, int min_factor
)
3399 int commit_trans
= 0;
3405 spin_lock(&block_rsv
->lock
);
3407 num_bytes
= div_factor(block_rsv
->size
, min_factor
);
3408 if (min_reserved
> num_bytes
)
3409 num_bytes
= min_reserved
;
3411 if (block_rsv
->reserved
>= num_bytes
) {
3414 num_bytes
-= block_rsv
->reserved
;
3415 if (block_rsv
->durable
&&
3416 block_rsv
->freed
[0] + block_rsv
->freed
[1] >= num_bytes
)
3419 spin_unlock(&block_rsv
->lock
);
3423 if (block_rsv
->refill_used
) {
3424 ret
= reserve_metadata_bytes(block_rsv
, num_bytes
);
3426 block_rsv_add_bytes(block_rsv
, num_bytes
, 0);
3435 trans
= btrfs_join_transaction(root
, 1);
3436 BUG_ON(IS_ERR(trans
));
3437 ret
= btrfs_commit_transaction(trans
, root
);
3442 printk(KERN_INFO
"block_rsv size %llu reserved %llu freed %llu %llu\n",
3443 block_rsv
->size
, block_rsv
->reserved
,
3444 block_rsv
->freed
[0], block_rsv
->freed
[1]);
3449 int btrfs_block_rsv_migrate(struct btrfs_block_rsv
*src_rsv
,
3450 struct btrfs_block_rsv
*dst_rsv
,
3453 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
3456 void btrfs_block_rsv_release(struct btrfs_root
*root
,
3457 struct btrfs_block_rsv
*block_rsv
,
3460 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
3461 if (global_rsv
->full
|| global_rsv
== block_rsv
||
3462 block_rsv
->space_info
!= global_rsv
->space_info
)
3464 block_rsv_release_bytes(block_rsv
, global_rsv
, num_bytes
);
3468 * helper to calculate size of global block reservation.
3469 * the desired value is sum of space used by extent tree,
3470 * checksum tree and root tree
3472 static u64
calc_global_metadata_size(struct btrfs_fs_info
*fs_info
)
3474 struct btrfs_space_info
*sinfo
;
3478 int csum_size
= btrfs_super_csum_size(&fs_info
->super_copy
);
3481 * per tree used space accounting can be inaccuracy, so we
3484 spin_lock(&fs_info
->extent_root
->accounting_lock
);
3485 num_bytes
= btrfs_root_used(&fs_info
->extent_root
->root_item
);
3486 spin_unlock(&fs_info
->extent_root
->accounting_lock
);
3488 spin_lock(&fs_info
->csum_root
->accounting_lock
);
3489 num_bytes
+= btrfs_root_used(&fs_info
->csum_root
->root_item
);
3490 spin_unlock(&fs_info
->csum_root
->accounting_lock
);
3492 spin_lock(&fs_info
->tree_root
->accounting_lock
);
3493 num_bytes
+= btrfs_root_used(&fs_info
->tree_root
->root_item
);
3494 spin_unlock(&fs_info
->tree_root
->accounting_lock
);
3496 sinfo
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_DATA
);
3497 spin_lock(&sinfo
->lock
);
3498 data_used
= sinfo
->bytes_used
;
3499 spin_unlock(&sinfo
->lock
);
3501 sinfo
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
3502 spin_lock(&sinfo
->lock
);
3503 meta_used
= sinfo
->bytes_used
;
3504 spin_unlock(&sinfo
->lock
);
3506 num_bytes
= (data_used
>> fs_info
->sb
->s_blocksize_bits
) *
3508 num_bytes
+= div64_u64(data_used
+ meta_used
, 50);
3510 if (num_bytes
* 3 > meta_used
)
3511 num_bytes
= div64_u64(meta_used
, 3);
3513 return ALIGN(num_bytes
, fs_info
->extent_root
->leafsize
<< 10);
3516 static void update_global_block_rsv(struct btrfs_fs_info
*fs_info
)
3518 struct btrfs_block_rsv
*block_rsv
= &fs_info
->global_block_rsv
;
3519 struct btrfs_space_info
*sinfo
= block_rsv
->space_info
;
3522 num_bytes
= calc_global_metadata_size(fs_info
);
3524 spin_lock(&block_rsv
->lock
);
3525 spin_lock(&sinfo
->lock
);
3527 block_rsv
->size
= num_bytes
;
3529 num_bytes
= sinfo
->bytes_used
+ sinfo
->bytes_pinned
+
3530 sinfo
->bytes_reserved
+ sinfo
->bytes_readonly
;
3532 if (sinfo
->total_bytes
> num_bytes
) {
3533 num_bytes
= sinfo
->total_bytes
- num_bytes
;
3534 block_rsv
->reserved
+= num_bytes
;
3535 sinfo
->bytes_reserved
+= num_bytes
;
3538 if (block_rsv
->reserved
>= block_rsv
->size
) {
3539 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
3540 sinfo
->bytes_reserved
-= num_bytes
;
3541 block_rsv
->reserved
= block_rsv
->size
;
3542 block_rsv
->full
= 1;
3545 printk(KERN_INFO
"global block rsv size %llu reserved %llu\n",
3546 block_rsv
->size
, block_rsv
->reserved
);
3548 spin_unlock(&sinfo
->lock
);
3549 spin_unlock(&block_rsv
->lock
);
3552 static void init_global_block_rsv(struct btrfs_fs_info
*fs_info
)
3554 struct btrfs_space_info
*space_info
;
3556 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_SYSTEM
);
3557 fs_info
->chunk_block_rsv
.space_info
= space_info
;
3558 fs_info
->chunk_block_rsv
.priority
= 10;
3560 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
3561 fs_info
->global_block_rsv
.space_info
= space_info
;
3562 fs_info
->global_block_rsv
.priority
= 10;
3563 fs_info
->global_block_rsv
.refill_used
= 1;
3564 fs_info
->delalloc_block_rsv
.space_info
= space_info
;
3565 fs_info
->trans_block_rsv
.space_info
= space_info
;
3566 fs_info
->empty_block_rsv
.space_info
= space_info
;
3567 fs_info
->empty_block_rsv
.priority
= 10;
3569 fs_info
->extent_root
->block_rsv
= &fs_info
->global_block_rsv
;
3570 fs_info
->csum_root
->block_rsv
= &fs_info
->global_block_rsv
;
3571 fs_info
->dev_root
->block_rsv
= &fs_info
->global_block_rsv
;
3572 fs_info
->tree_root
->block_rsv
= &fs_info
->global_block_rsv
;
3573 fs_info
->chunk_root
->block_rsv
= &fs_info
->chunk_block_rsv
;
3575 btrfs_add_durable_block_rsv(fs_info
, &fs_info
->global_block_rsv
);
3577 btrfs_add_durable_block_rsv(fs_info
, &fs_info
->delalloc_block_rsv
);
3579 update_global_block_rsv(fs_info
);
3582 static void release_global_block_rsv(struct btrfs_fs_info
*fs_info
)
3584 block_rsv_release_bytes(&fs_info
->global_block_rsv
, NULL
, (u64
)-1);
3585 WARN_ON(fs_info
->delalloc_block_rsv
.size
> 0);
3586 WARN_ON(fs_info
->delalloc_block_rsv
.reserved
> 0);
3587 WARN_ON(fs_info
->trans_block_rsv
.size
> 0);
3588 WARN_ON(fs_info
->trans_block_rsv
.reserved
> 0);
3589 WARN_ON(fs_info
->chunk_block_rsv
.size
> 0);
3590 WARN_ON(fs_info
->chunk_block_rsv
.reserved
> 0);
3593 static u64
calc_trans_metadata_size(struct btrfs_root
*root
, int num_items
)
3595 return (root
->leafsize
+ root
->nodesize
* (BTRFS_MAX_LEVEL
- 1)) *
3599 int btrfs_trans_reserve_metadata(struct btrfs_trans_handle
*trans
,
3600 struct btrfs_root
*root
,
3601 int num_items
, int *retries
)
3606 if (num_items
== 0 || root
->fs_info
->chunk_root
== root
)
3609 num_bytes
= calc_trans_metadata_size(root
, num_items
);
3610 ret
= btrfs_block_rsv_add(trans
, root
, &root
->fs_info
->trans_block_rsv
,
3611 num_bytes
, retries
);
3613 trans
->bytes_reserved
+= num_bytes
;
3614 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
3619 void btrfs_trans_release_metadata(struct btrfs_trans_handle
*trans
,
3620 struct btrfs_root
*root
)
3622 if (!trans
->bytes_reserved
)
3625 BUG_ON(trans
->block_rsv
!= &root
->fs_info
->trans_block_rsv
);
3626 btrfs_block_rsv_release(root
, trans
->block_rsv
,
3627 trans
->bytes_reserved
);
3628 trans
->bytes_reserved
= 0;
3631 int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle
*trans
,
3632 struct inode
*inode
)
3634 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3635 struct btrfs_block_rsv
*src_rsv
= get_block_rsv(trans
, root
);
3636 struct btrfs_block_rsv
*dst_rsv
= root
->orphan_block_rsv
;
3639 * one for deleting orphan item, one for updating inode and
3640 * two for calling btrfs_truncate_inode_items.
3642 * btrfs_truncate_inode_items is a delete operation, it frees
3643 * more space than it uses in most cases. So two units of
3644 * metadata space should be enough for calling it many times.
3645 * If all of the metadata space is used, we can commit
3646 * transaction and use space it freed.
3648 u64 num_bytes
= calc_trans_metadata_size(root
, 4);
3649 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
3652 void btrfs_orphan_release_metadata(struct inode
*inode
)
3654 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3655 u64 num_bytes
= calc_trans_metadata_size(root
, 4);
3656 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
, num_bytes
);
3659 int btrfs_snap_reserve_metadata(struct btrfs_trans_handle
*trans
,
3660 struct btrfs_pending_snapshot
*pending
)
3662 struct btrfs_root
*root
= pending
->root
;
3663 struct btrfs_block_rsv
*src_rsv
= get_block_rsv(trans
, root
);
3664 struct btrfs_block_rsv
*dst_rsv
= &pending
->block_rsv
;
3666 * two for root back/forward refs, two for directory entries
3667 * and one for root of the snapshot.
3669 u64 num_bytes
= calc_trans_metadata_size(root
, 5);
3670 dst_rsv
->space_info
= src_rsv
->space_info
;
3671 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
3674 static u64
calc_csum_metadata_size(struct inode
*inode
, u64 num_bytes
)
3676 return num_bytes
>>= 3;
3679 int btrfs_delalloc_reserve_metadata(struct inode
*inode
, u64 num_bytes
)
3681 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3682 struct btrfs_block_rsv
*block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
3688 if (btrfs_transaction_in_commit(root
->fs_info
))
3689 schedule_timeout(1);
3691 num_bytes
= ALIGN(num_bytes
, root
->sectorsize
);
3693 spin_lock(&BTRFS_I(inode
)->accounting_lock
);
3694 nr_extents
= atomic_read(&BTRFS_I(inode
)->outstanding_extents
) + 1;
3695 if (nr_extents
> BTRFS_I(inode
)->reserved_extents
) {
3696 nr_extents
-= BTRFS_I(inode
)->reserved_extents
;
3697 to_reserve
= calc_trans_metadata_size(root
, nr_extents
);
3703 to_reserve
+= calc_csum_metadata_size(inode
, num_bytes
);
3704 ret
= reserve_metadata_bytes(block_rsv
, to_reserve
);
3706 spin_unlock(&BTRFS_I(inode
)->accounting_lock
);
3707 ret
= should_retry_reserve(NULL
, root
, block_rsv
, to_reserve
,
3714 BTRFS_I(inode
)->reserved_extents
+= nr_extents
;
3715 atomic_inc(&BTRFS_I(inode
)->outstanding_extents
);
3716 spin_unlock(&BTRFS_I(inode
)->accounting_lock
);
3718 block_rsv_add_bytes(block_rsv
, to_reserve
, 1);
3720 if (block_rsv
->size
> 512 * 1024 * 1024)
3721 shrink_delalloc(NULL
, root
, to_reserve
);
3726 void btrfs_delalloc_release_metadata(struct inode
*inode
, u64 num_bytes
)
3728 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3732 num_bytes
= ALIGN(num_bytes
, root
->sectorsize
);
3733 atomic_dec(&BTRFS_I(inode
)->outstanding_extents
);
3735 spin_lock(&BTRFS_I(inode
)->accounting_lock
);
3736 nr_extents
= atomic_read(&BTRFS_I(inode
)->outstanding_extents
);
3737 if (nr_extents
< BTRFS_I(inode
)->reserved_extents
) {
3738 nr_extents
= BTRFS_I(inode
)->reserved_extents
- nr_extents
;
3739 BTRFS_I(inode
)->reserved_extents
-= nr_extents
;
3743 spin_unlock(&BTRFS_I(inode
)->accounting_lock
);
3745 to_free
= calc_csum_metadata_size(inode
, num_bytes
);
3747 to_free
+= calc_trans_metadata_size(root
, nr_extents
);
3749 btrfs_block_rsv_release(root
, &root
->fs_info
->delalloc_block_rsv
,
3753 int btrfs_delalloc_reserve_space(struct inode
*inode
, u64 num_bytes
)
3757 ret
= btrfs_check_data_free_space(inode
, num_bytes
);
3761 ret
= btrfs_delalloc_reserve_metadata(inode
, num_bytes
);
3763 btrfs_free_reserved_data_space(inode
, num_bytes
);
3770 void btrfs_delalloc_release_space(struct inode
*inode
, u64 num_bytes
)
3772 btrfs_delalloc_release_metadata(inode
, num_bytes
);
3773 btrfs_free_reserved_data_space(inode
, num_bytes
);
3776 static int update_block_group(struct btrfs_trans_handle
*trans
,
3777 struct btrfs_root
*root
,
3778 u64 bytenr
, u64 num_bytes
, int alloc
)
3780 struct btrfs_block_group_cache
*cache
;
3781 struct btrfs_fs_info
*info
= root
->fs_info
;
3783 u64 total
= num_bytes
;
3787 /* block accounting for super block */
3788 spin_lock(&info
->delalloc_lock
);
3789 old_val
= btrfs_super_bytes_used(&info
->super_copy
);
3791 old_val
+= num_bytes
;
3793 old_val
-= num_bytes
;
3794 btrfs_set_super_bytes_used(&info
->super_copy
, old_val
);
3795 spin_unlock(&info
->delalloc_lock
);
3798 cache
= btrfs_lookup_block_group(info
, bytenr
);
3801 if (cache
->flags
& (BTRFS_BLOCK_GROUP_DUP
|
3802 BTRFS_BLOCK_GROUP_RAID1
|
3803 BTRFS_BLOCK_GROUP_RAID10
))
3807 byte_in_group
= bytenr
- cache
->key
.objectid
;
3808 WARN_ON(byte_in_group
> cache
->key
.offset
);
3810 spin_lock(&cache
->space_info
->lock
);
3811 spin_lock(&cache
->lock
);
3813 old_val
= btrfs_block_group_used(&cache
->item
);
3814 num_bytes
= min(total
, cache
->key
.offset
- byte_in_group
);
3816 old_val
+= num_bytes
;
3817 btrfs_set_block_group_used(&cache
->item
, old_val
);
3818 cache
->reserved
-= num_bytes
;
3819 cache
->space_info
->bytes_reserved
-= num_bytes
;
3820 cache
->space_info
->bytes_used
+= num_bytes
;
3821 cache
->space_info
->disk_used
+= num_bytes
* factor
;
3822 spin_unlock(&cache
->lock
);
3823 spin_unlock(&cache
->space_info
->lock
);
3825 old_val
-= num_bytes
;
3826 btrfs_set_block_group_used(&cache
->item
, old_val
);
3827 cache
->pinned
+= num_bytes
;
3828 cache
->space_info
->bytes_pinned
+= num_bytes
;
3829 cache
->space_info
->bytes_used
-= num_bytes
;
3830 cache
->space_info
->disk_used
-= num_bytes
* factor
;
3831 spin_unlock(&cache
->lock
);
3832 spin_unlock(&cache
->space_info
->lock
);
3834 set_extent_dirty(info
->pinned_extents
,
3835 bytenr
, bytenr
+ num_bytes
- 1,
3836 GFP_NOFS
| __GFP_NOFAIL
);
3838 btrfs_put_block_group(cache
);
3840 bytenr
+= num_bytes
;
3845 static u64
first_logical_byte(struct btrfs_root
*root
, u64 search_start
)
3847 struct btrfs_block_group_cache
*cache
;
3850 cache
= btrfs_lookup_first_block_group(root
->fs_info
, search_start
);
3854 bytenr
= cache
->key
.objectid
;
3855 btrfs_put_block_group(cache
);
3860 static int pin_down_extent(struct btrfs_root
*root
,
3861 struct btrfs_block_group_cache
*cache
,
3862 u64 bytenr
, u64 num_bytes
, int reserved
)
3864 spin_lock(&cache
->space_info
->lock
);
3865 spin_lock(&cache
->lock
);
3866 cache
->pinned
+= num_bytes
;
3867 cache
->space_info
->bytes_pinned
+= num_bytes
;
3869 cache
->reserved
-= num_bytes
;
3870 cache
->space_info
->bytes_reserved
-= num_bytes
;
3872 spin_unlock(&cache
->lock
);
3873 spin_unlock(&cache
->space_info
->lock
);
3875 set_extent_dirty(root
->fs_info
->pinned_extents
, bytenr
,
3876 bytenr
+ num_bytes
- 1, GFP_NOFS
| __GFP_NOFAIL
);
3881 * this function must be called within transaction
3883 int btrfs_pin_extent(struct btrfs_root
*root
,
3884 u64 bytenr
, u64 num_bytes
, int reserved
)
3886 struct btrfs_block_group_cache
*cache
;
3888 cache
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
3891 pin_down_extent(root
, cache
, bytenr
, num_bytes
, reserved
);
3893 btrfs_put_block_group(cache
);
3898 * update size of reserved extents. this function may return -EAGAIN
3899 * if 'reserve' is true or 'sinfo' is false.
3901 static int update_reserved_bytes(struct btrfs_block_group_cache
*cache
,
3902 u64 num_bytes
, int reserve
, int sinfo
)
3906 struct btrfs_space_info
*space_info
= cache
->space_info
;
3907 spin_lock(&space_info
->lock
);
3908 spin_lock(&cache
->lock
);
3913 cache
->reserved
+= num_bytes
;
3914 space_info
->bytes_reserved
+= num_bytes
;
3918 space_info
->bytes_readonly
+= num_bytes
;
3919 cache
->reserved
-= num_bytes
;
3920 space_info
->bytes_reserved
-= num_bytes
;
3922 spin_unlock(&cache
->lock
);
3923 spin_unlock(&space_info
->lock
);
3925 spin_lock(&cache
->lock
);
3930 cache
->reserved
+= num_bytes
;
3932 cache
->reserved
-= num_bytes
;
3934 spin_unlock(&cache
->lock
);
3939 int btrfs_prepare_extent_commit(struct btrfs_trans_handle
*trans
,
3940 struct btrfs_root
*root
)
3942 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3943 struct btrfs_caching_control
*next
;
3944 struct btrfs_caching_control
*caching_ctl
;
3945 struct btrfs_block_group_cache
*cache
;
3947 down_write(&fs_info
->extent_commit_sem
);
3949 list_for_each_entry_safe(caching_ctl
, next
,
3950 &fs_info
->caching_block_groups
, list
) {
3951 cache
= caching_ctl
->block_group
;
3952 if (block_group_cache_done(cache
)) {
3953 cache
->last_byte_to_unpin
= (u64
)-1;
3954 list_del_init(&caching_ctl
->list
);
3955 put_caching_control(caching_ctl
);
3957 cache
->last_byte_to_unpin
= caching_ctl
->progress
;
3961 if (fs_info
->pinned_extents
== &fs_info
->freed_extents
[0])
3962 fs_info
->pinned_extents
= &fs_info
->freed_extents
[1];
3964 fs_info
->pinned_extents
= &fs_info
->freed_extents
[0];
3966 up_write(&fs_info
->extent_commit_sem
);
3968 update_global_block_rsv(fs_info
);
3972 static int unpin_extent_range(struct btrfs_root
*root
, u64 start
, u64 end
)
3974 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3975 struct btrfs_block_group_cache
*cache
= NULL
;
3978 while (start
<= end
) {
3980 start
>= cache
->key
.objectid
+ cache
->key
.offset
) {
3982 btrfs_put_block_group(cache
);
3983 cache
= btrfs_lookup_block_group(fs_info
, start
);
3987 len
= cache
->key
.objectid
+ cache
->key
.offset
- start
;
3988 len
= min(len
, end
+ 1 - start
);
3990 if (start
< cache
->last_byte_to_unpin
) {
3991 len
= min(len
, cache
->last_byte_to_unpin
- start
);
3992 btrfs_add_free_space(cache
, start
, len
);
3997 spin_lock(&cache
->space_info
->lock
);
3998 spin_lock(&cache
->lock
);
3999 cache
->pinned
-= len
;
4000 cache
->space_info
->bytes_pinned
-= len
;
4002 cache
->space_info
->bytes_readonly
+= len
;
4003 } else if (cache
->reserved_pinned
> 0) {
4004 len
= min(len
, cache
->reserved_pinned
);
4005 cache
->reserved_pinned
-= len
;
4006 cache
->space_info
->bytes_reserved
+= len
;
4008 spin_unlock(&cache
->lock
);
4009 spin_unlock(&cache
->space_info
->lock
);
4013 btrfs_put_block_group(cache
);
4017 int btrfs_finish_extent_commit(struct btrfs_trans_handle
*trans
,
4018 struct btrfs_root
*root
)
4020 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4021 struct extent_io_tree
*unpin
;
4022 struct btrfs_block_rsv
*block_rsv
;
4023 struct btrfs_block_rsv
*next_rsv
;
4029 if (fs_info
->pinned_extents
== &fs_info
->freed_extents
[0])
4030 unpin
= &fs_info
->freed_extents
[1];
4032 unpin
= &fs_info
->freed_extents
[0];
4035 ret
= find_first_extent_bit(unpin
, 0, &start
, &end
,
4040 ret
= btrfs_discard_extent(root
, start
, end
+ 1 - start
);
4042 clear_extent_dirty(unpin
, start
, end
, GFP_NOFS
);
4043 unpin_extent_range(root
, start
, end
);
4047 mutex_lock(&fs_info
->durable_block_rsv_mutex
);
4048 list_for_each_entry_safe(block_rsv
, next_rsv
,
4049 &fs_info
->durable_block_rsv_list
, list
) {
4051 idx
= trans
->transid
& 0x1;
4052 if (block_rsv
->freed
[idx
] > 0) {
4053 block_rsv_add_bytes(block_rsv
,
4054 block_rsv
->freed
[idx
], 0);
4055 block_rsv
->freed
[idx
] = 0;
4057 if (atomic_read(&block_rsv
->usage
) == 0) {
4058 btrfs_block_rsv_release(root
, block_rsv
, (u64
)-1);
4060 if (block_rsv
->freed
[0] == 0 &&
4061 block_rsv
->freed
[1] == 0) {
4062 list_del_init(&block_rsv
->list
);
4066 btrfs_block_rsv_release(root
, block_rsv
, 0);
4069 mutex_unlock(&fs_info
->durable_block_rsv_mutex
);
4074 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
4075 struct btrfs_root
*root
,
4076 u64 bytenr
, u64 num_bytes
, u64 parent
,
4077 u64 root_objectid
, u64 owner_objectid
,
4078 u64 owner_offset
, int refs_to_drop
,
4079 struct btrfs_delayed_extent_op
*extent_op
)
4081 struct btrfs_key key
;
4082 struct btrfs_path
*path
;
4083 struct btrfs_fs_info
*info
= root
->fs_info
;
4084 struct btrfs_root
*extent_root
= info
->extent_root
;
4085 struct extent_buffer
*leaf
;
4086 struct btrfs_extent_item
*ei
;
4087 struct btrfs_extent_inline_ref
*iref
;
4090 int extent_slot
= 0;
4091 int found_extent
= 0;
4096 path
= btrfs_alloc_path();
4101 path
->leave_spinning
= 1;
4103 is_data
= owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
;
4104 BUG_ON(!is_data
&& refs_to_drop
!= 1);
4106 ret
= lookup_extent_backref(trans
, extent_root
, path
, &iref
,
4107 bytenr
, num_bytes
, parent
,
4108 root_objectid
, owner_objectid
,
4111 extent_slot
= path
->slots
[0];
4112 while (extent_slot
>= 0) {
4113 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
4115 if (key
.objectid
!= bytenr
)
4117 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
4118 key
.offset
== num_bytes
) {
4122 if (path
->slots
[0] - extent_slot
> 5)
4126 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4127 item_size
= btrfs_item_size_nr(path
->nodes
[0], extent_slot
);
4128 if (found_extent
&& item_size
< sizeof(*ei
))
4131 if (!found_extent
) {
4133 ret
= remove_extent_backref(trans
, extent_root
, path
,
4137 btrfs_release_path(extent_root
, path
);
4138 path
->leave_spinning
= 1;
4140 key
.objectid
= bytenr
;
4141 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
4142 key
.offset
= num_bytes
;
4144 ret
= btrfs_search_slot(trans
, extent_root
,
4147 printk(KERN_ERR
"umm, got %d back from search"
4148 ", was looking for %llu\n", ret
,
4149 (unsigned long long)bytenr
);
4150 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
4153 extent_slot
= path
->slots
[0];
4156 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
4158 printk(KERN_ERR
"btrfs unable to find ref byte nr %llu "
4159 "parent %llu root %llu owner %llu offset %llu\n",
4160 (unsigned long long)bytenr
,
4161 (unsigned long long)parent
,
4162 (unsigned long long)root_objectid
,
4163 (unsigned long long)owner_objectid
,
4164 (unsigned long long)owner_offset
);
4167 leaf
= path
->nodes
[0];
4168 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
4169 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4170 if (item_size
< sizeof(*ei
)) {
4171 BUG_ON(found_extent
|| extent_slot
!= path
->slots
[0]);
4172 ret
= convert_extent_item_v0(trans
, extent_root
, path
,
4176 btrfs_release_path(extent_root
, path
);
4177 path
->leave_spinning
= 1;
4179 key
.objectid
= bytenr
;
4180 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
4181 key
.offset
= num_bytes
;
4183 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
,
4186 printk(KERN_ERR
"umm, got %d back from search"
4187 ", was looking for %llu\n", ret
,
4188 (unsigned long long)bytenr
);
4189 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
4192 extent_slot
= path
->slots
[0];
4193 leaf
= path
->nodes
[0];
4194 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
4197 BUG_ON(item_size
< sizeof(*ei
));
4198 ei
= btrfs_item_ptr(leaf
, extent_slot
,
4199 struct btrfs_extent_item
);
4200 if (owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
) {
4201 struct btrfs_tree_block_info
*bi
;
4202 BUG_ON(item_size
< sizeof(*ei
) + sizeof(*bi
));
4203 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
4204 WARN_ON(owner_objectid
!= btrfs_tree_block_level(leaf
, bi
));
4207 refs
= btrfs_extent_refs(leaf
, ei
);
4208 BUG_ON(refs
< refs_to_drop
);
4209 refs
-= refs_to_drop
;
4213 __run_delayed_extent_op(extent_op
, leaf
, ei
);
4215 * In the case of inline back ref, reference count will
4216 * be updated by remove_extent_backref
4219 BUG_ON(!found_extent
);
4221 btrfs_set_extent_refs(leaf
, ei
, refs
);
4222 btrfs_mark_buffer_dirty(leaf
);
4225 ret
= remove_extent_backref(trans
, extent_root
, path
,
4232 BUG_ON(is_data
&& refs_to_drop
!=
4233 extent_data_ref_count(root
, path
, iref
));
4235 BUG_ON(path
->slots
[0] != extent_slot
);
4237 BUG_ON(path
->slots
[0] != extent_slot
+ 1);
4238 path
->slots
[0] = extent_slot
;
4243 ret
= btrfs_del_items(trans
, extent_root
, path
, path
->slots
[0],
4246 btrfs_release_path(extent_root
, path
);
4249 ret
= btrfs_del_csums(trans
, root
, bytenr
, num_bytes
);
4252 invalidate_mapping_pages(info
->btree_inode
->i_mapping
,
4253 bytenr
>> PAGE_CACHE_SHIFT
,
4254 (bytenr
+ num_bytes
- 1) >> PAGE_CACHE_SHIFT
);
4257 ret
= update_block_group(trans
, root
, bytenr
, num_bytes
, 0);
4260 btrfs_free_path(path
);
4265 * when we free an block, it is possible (and likely) that we free the last
4266 * delayed ref for that extent as well. This searches the delayed ref tree for
4267 * a given extent, and if there are no other delayed refs to be processed, it
4268 * removes it from the tree.
4270 static noinline
int check_ref_cleanup(struct btrfs_trans_handle
*trans
,
4271 struct btrfs_root
*root
, u64 bytenr
)
4273 struct btrfs_delayed_ref_head
*head
;
4274 struct btrfs_delayed_ref_root
*delayed_refs
;
4275 struct btrfs_delayed_ref_node
*ref
;
4276 struct rb_node
*node
;
4279 delayed_refs
= &trans
->transaction
->delayed_refs
;
4280 spin_lock(&delayed_refs
->lock
);
4281 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
4285 node
= rb_prev(&head
->node
.rb_node
);
4289 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
4291 /* there are still entries for this ref, we can't drop it */
4292 if (ref
->bytenr
== bytenr
)
4295 if (head
->extent_op
) {
4296 if (!head
->must_insert_reserved
)
4298 kfree(head
->extent_op
);
4299 head
->extent_op
= NULL
;
4303 * waiting for the lock here would deadlock. If someone else has it
4304 * locked they are already in the process of dropping it anyway
4306 if (!mutex_trylock(&head
->mutex
))
4310 * at this point we have a head with no other entries. Go
4311 * ahead and process it.
4313 head
->node
.in_tree
= 0;
4314 rb_erase(&head
->node
.rb_node
, &delayed_refs
->root
);
4316 delayed_refs
->num_entries
--;
4319 * we don't take a ref on the node because we're removing it from the
4320 * tree, so we just steal the ref the tree was holding.
4322 delayed_refs
->num_heads
--;
4323 if (list_empty(&head
->cluster
))
4324 delayed_refs
->num_heads_ready
--;
4326 list_del_init(&head
->cluster
);
4327 spin_unlock(&delayed_refs
->lock
);
4329 BUG_ON(head
->extent_op
);
4330 if (head
->must_insert_reserved
)
4333 mutex_unlock(&head
->mutex
);
4334 btrfs_put_delayed_ref(&head
->node
);
4337 spin_unlock(&delayed_refs
->lock
);
4341 void btrfs_free_tree_block(struct btrfs_trans_handle
*trans
,
4342 struct btrfs_root
*root
,
4343 struct extent_buffer
*buf
,
4344 u64 parent
, int last_ref
)
4346 struct btrfs_block_rsv
*block_rsv
;
4347 struct btrfs_block_group_cache
*cache
= NULL
;
4350 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
4351 ret
= btrfs_add_delayed_tree_ref(trans
, buf
->start
, buf
->len
,
4352 parent
, root
->root_key
.objectid
,
4353 btrfs_header_level(buf
),
4354 BTRFS_DROP_DELAYED_REF
, NULL
);
4361 block_rsv
= get_block_rsv(trans
, root
);
4362 cache
= btrfs_lookup_block_group(root
->fs_info
, buf
->start
);
4363 if (block_rsv
->space_info
!= cache
->space_info
)
4366 if (btrfs_header_generation(buf
) == trans
->transid
) {
4367 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
4368 ret
= check_ref_cleanup(trans
, root
, buf
->start
);
4373 if (btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
)) {
4374 pin_down_extent(root
, cache
, buf
->start
, buf
->len
, 1);
4378 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY
, &buf
->bflags
));
4380 btrfs_add_free_space(cache
, buf
->start
, buf
->len
);
4381 ret
= update_reserved_bytes(cache
, buf
->len
, 0, 0);
4382 if (ret
== -EAGAIN
) {
4383 /* block group became read-only */
4384 update_reserved_bytes(cache
, buf
->len
, 0, 1);
4389 spin_lock(&block_rsv
->lock
);
4390 if (block_rsv
->reserved
< block_rsv
->size
) {
4391 block_rsv
->reserved
+= buf
->len
;
4394 spin_unlock(&block_rsv
->lock
);
4397 spin_lock(&cache
->space_info
->lock
);
4398 cache
->space_info
->bytes_reserved
-= buf
->len
;
4399 spin_unlock(&cache
->space_info
->lock
);
4404 if (block_rsv
->durable
&& !cache
->ro
) {
4406 spin_lock(&cache
->lock
);
4408 cache
->reserved_pinned
+= buf
->len
;
4411 spin_unlock(&cache
->lock
);
4414 spin_lock(&block_rsv
->lock
);
4415 block_rsv
->freed
[trans
->transid
& 0x1] += buf
->len
;
4416 spin_unlock(&block_rsv
->lock
);
4420 btrfs_put_block_group(cache
);
4423 int btrfs_free_extent(struct btrfs_trans_handle
*trans
,
4424 struct btrfs_root
*root
,
4425 u64 bytenr
, u64 num_bytes
, u64 parent
,
4426 u64 root_objectid
, u64 owner
, u64 offset
)
4431 * tree log blocks never actually go into the extent allocation
4432 * tree, just update pinning info and exit early.
4434 if (root_objectid
== BTRFS_TREE_LOG_OBJECTID
) {
4435 WARN_ON(owner
>= BTRFS_FIRST_FREE_OBJECTID
);
4436 /* unlocks the pinned mutex */
4437 btrfs_pin_extent(root
, bytenr
, num_bytes
, 1);
4439 } else if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
4440 ret
= btrfs_add_delayed_tree_ref(trans
, bytenr
, num_bytes
,
4441 parent
, root_objectid
, (int)owner
,
4442 BTRFS_DROP_DELAYED_REF
, NULL
);
4445 ret
= btrfs_add_delayed_data_ref(trans
, bytenr
, num_bytes
,
4446 parent
, root_objectid
, owner
,
4447 offset
, BTRFS_DROP_DELAYED_REF
, NULL
);
4453 static u64
stripe_align(struct btrfs_root
*root
, u64 val
)
4455 u64 mask
= ((u64
)root
->stripesize
- 1);
4456 u64 ret
= (val
+ mask
) & ~mask
;
4461 * when we wait for progress in the block group caching, its because
4462 * our allocation attempt failed at least once. So, we must sleep
4463 * and let some progress happen before we try again.
4465 * This function will sleep at least once waiting for new free space to
4466 * show up, and then it will check the block group free space numbers
4467 * for our min num_bytes. Another option is to have it go ahead
4468 * and look in the rbtree for a free extent of a given size, but this
4472 wait_block_group_cache_progress(struct btrfs_block_group_cache
*cache
,
4475 struct btrfs_caching_control
*caching_ctl
;
4478 caching_ctl
= get_caching_control(cache
);
4482 wait_event(caching_ctl
->wait
, block_group_cache_done(cache
) ||
4483 (cache
->free_space
>= num_bytes
));
4485 put_caching_control(caching_ctl
);
4490 wait_block_group_cache_done(struct btrfs_block_group_cache
*cache
)
4492 struct btrfs_caching_control
*caching_ctl
;
4495 caching_ctl
= get_caching_control(cache
);
4499 wait_event(caching_ctl
->wait
, block_group_cache_done(cache
));
4501 put_caching_control(caching_ctl
);
4505 static int get_block_group_index(struct btrfs_block_group_cache
*cache
)
4508 if (cache
->flags
& BTRFS_BLOCK_GROUP_RAID10
)
4510 else if (cache
->flags
& BTRFS_BLOCK_GROUP_RAID1
)
4512 else if (cache
->flags
& BTRFS_BLOCK_GROUP_DUP
)
4514 else if (cache
->flags
& BTRFS_BLOCK_GROUP_RAID0
)
4521 enum btrfs_loop_type
{
4522 LOOP_FIND_IDEAL
= 0,
4523 LOOP_CACHING_NOWAIT
= 1,
4524 LOOP_CACHING_WAIT
= 2,
4525 LOOP_ALLOC_CHUNK
= 3,
4526 LOOP_NO_EMPTY_SIZE
= 4,
4530 * walks the btree of allocated extents and find a hole of a given size.
4531 * The key ins is changed to record the hole:
4532 * ins->objectid == block start
4533 * ins->flags = BTRFS_EXTENT_ITEM_KEY
4534 * ins->offset == number of blocks
4535 * Any available blocks before search_start are skipped.
4537 static noinline
int find_free_extent(struct btrfs_trans_handle
*trans
,
4538 struct btrfs_root
*orig_root
,
4539 u64 num_bytes
, u64 empty_size
,
4540 u64 search_start
, u64 search_end
,
4541 u64 hint_byte
, struct btrfs_key
*ins
,
4545 struct btrfs_root
*root
= orig_root
->fs_info
->extent_root
;
4546 struct btrfs_free_cluster
*last_ptr
= NULL
;
4547 struct btrfs_block_group_cache
*block_group
= NULL
;
4548 int empty_cluster
= 2 * 1024 * 1024;
4549 int allowed_chunk_alloc
= 0;
4550 int done_chunk_alloc
= 0;
4551 struct btrfs_space_info
*space_info
;
4552 int last_ptr_loop
= 0;
4555 bool found_uncached_bg
= false;
4556 bool failed_cluster_refill
= false;
4557 bool failed_alloc
= false;
4558 u64 ideal_cache_percent
= 0;
4559 u64 ideal_cache_offset
= 0;
4561 WARN_ON(num_bytes
< root
->sectorsize
);
4562 btrfs_set_key_type(ins
, BTRFS_EXTENT_ITEM_KEY
);
4566 space_info
= __find_space_info(root
->fs_info
, data
);
4568 printk(KERN_ERR
"No space info for %d\n", data
);
4572 if (orig_root
->ref_cows
|| empty_size
)
4573 allowed_chunk_alloc
= 1;
4575 if (data
& BTRFS_BLOCK_GROUP_METADATA
) {
4576 last_ptr
= &root
->fs_info
->meta_alloc_cluster
;
4577 if (!btrfs_test_opt(root
, SSD
))
4578 empty_cluster
= 64 * 1024;
4581 if ((data
& BTRFS_BLOCK_GROUP_DATA
) && btrfs_test_opt(root
, SSD
)) {
4582 last_ptr
= &root
->fs_info
->data_alloc_cluster
;
4586 spin_lock(&last_ptr
->lock
);
4587 if (last_ptr
->block_group
)
4588 hint_byte
= last_ptr
->window_start
;
4589 spin_unlock(&last_ptr
->lock
);
4592 search_start
= max(search_start
, first_logical_byte(root
, 0));
4593 search_start
= max(search_start
, hint_byte
);
4598 if (search_start
== hint_byte
) {
4600 block_group
= btrfs_lookup_block_group(root
->fs_info
,
4603 * we don't want to use the block group if it doesn't match our
4604 * allocation bits, or if its not cached.
4606 * However if we are re-searching with an ideal block group
4607 * picked out then we don't care that the block group is cached.
4609 if (block_group
&& block_group_bits(block_group
, data
) &&
4610 (block_group
->cached
!= BTRFS_CACHE_NO
||
4611 search_start
== ideal_cache_offset
)) {
4612 down_read(&space_info
->groups_sem
);
4613 if (list_empty(&block_group
->list
) ||
4616 * someone is removing this block group,
4617 * we can't jump into the have_block_group
4618 * target because our list pointers are not
4621 btrfs_put_block_group(block_group
);
4622 up_read(&space_info
->groups_sem
);
4624 index
= get_block_group_index(block_group
);
4625 goto have_block_group
;
4627 } else if (block_group
) {
4628 btrfs_put_block_group(block_group
);
4632 down_read(&space_info
->groups_sem
);
4633 list_for_each_entry(block_group
, &space_info
->block_groups
[index
],
4638 btrfs_get_block_group(block_group
);
4639 search_start
= block_group
->key
.objectid
;
4642 if (unlikely(block_group
->cached
== BTRFS_CACHE_NO
)) {
4645 free_percent
= btrfs_block_group_used(&block_group
->item
);
4646 free_percent
*= 100;
4647 free_percent
= div64_u64(free_percent
,
4648 block_group
->key
.offset
);
4649 free_percent
= 100 - free_percent
;
4650 if (free_percent
> ideal_cache_percent
&&
4651 likely(!block_group
->ro
)) {
4652 ideal_cache_offset
= block_group
->key
.objectid
;
4653 ideal_cache_percent
= free_percent
;
4657 * We only want to start kthread caching if we are at
4658 * the point where we will wait for caching to make
4659 * progress, or if our ideal search is over and we've
4660 * found somebody to start caching.
4662 if (loop
> LOOP_CACHING_NOWAIT
||
4663 (loop
> LOOP_FIND_IDEAL
&&
4664 atomic_read(&space_info
->caching_threads
) < 2)) {
4665 ret
= cache_block_group(block_group
);
4668 found_uncached_bg
= true;
4671 * If loop is set for cached only, try the next block
4674 if (loop
== LOOP_FIND_IDEAL
)
4678 cached
= block_group_cache_done(block_group
);
4679 if (unlikely(!cached
))
4680 found_uncached_bg
= true;
4682 if (unlikely(block_group
->ro
))
4686 * Ok we want to try and use the cluster allocator, so lets look
4687 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
4688 * have tried the cluster allocator plenty of times at this
4689 * point and not have found anything, so we are likely way too
4690 * fragmented for the clustering stuff to find anything, so lets
4691 * just skip it and let the allocator find whatever block it can
4694 if (last_ptr
&& loop
< LOOP_NO_EMPTY_SIZE
) {
4696 * the refill lock keeps out other
4697 * people trying to start a new cluster
4699 spin_lock(&last_ptr
->refill_lock
);
4700 if (last_ptr
->block_group
&&
4701 (last_ptr
->block_group
->ro
||
4702 !block_group_bits(last_ptr
->block_group
, data
))) {
4704 goto refill_cluster
;
4707 offset
= btrfs_alloc_from_cluster(block_group
, last_ptr
,
4708 num_bytes
, search_start
);
4710 /* we have a block, we're done */
4711 spin_unlock(&last_ptr
->refill_lock
);
4715 spin_lock(&last_ptr
->lock
);
4717 * whoops, this cluster doesn't actually point to
4718 * this block group. Get a ref on the block
4719 * group is does point to and try again
4721 if (!last_ptr_loop
&& last_ptr
->block_group
&&
4722 last_ptr
->block_group
!= block_group
) {
4724 btrfs_put_block_group(block_group
);
4725 block_group
= last_ptr
->block_group
;
4726 btrfs_get_block_group(block_group
);
4727 spin_unlock(&last_ptr
->lock
);
4728 spin_unlock(&last_ptr
->refill_lock
);
4731 search_start
= block_group
->key
.objectid
;
4733 * we know this block group is properly
4734 * in the list because
4735 * btrfs_remove_block_group, drops the
4736 * cluster before it removes the block
4737 * group from the list
4739 goto have_block_group
;
4741 spin_unlock(&last_ptr
->lock
);
4744 * this cluster didn't work out, free it and
4747 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
4751 /* allocate a cluster in this block group */
4752 ret
= btrfs_find_space_cluster(trans
, root
,
4753 block_group
, last_ptr
,
4755 empty_cluster
+ empty_size
);
4758 * now pull our allocation out of this
4761 offset
= btrfs_alloc_from_cluster(block_group
,
4762 last_ptr
, num_bytes
,
4765 /* we found one, proceed */
4766 spin_unlock(&last_ptr
->refill_lock
);
4769 } else if (!cached
&& loop
> LOOP_CACHING_NOWAIT
4770 && !failed_cluster_refill
) {
4771 spin_unlock(&last_ptr
->refill_lock
);
4773 failed_cluster_refill
= true;
4774 wait_block_group_cache_progress(block_group
,
4775 num_bytes
+ empty_cluster
+ empty_size
);
4776 goto have_block_group
;
4780 * at this point we either didn't find a cluster
4781 * or we weren't able to allocate a block from our
4782 * cluster. Free the cluster we've been trying
4783 * to use, and go to the next block group
4785 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
4786 spin_unlock(&last_ptr
->refill_lock
);
4790 offset
= btrfs_find_space_for_alloc(block_group
, search_start
,
4791 num_bytes
, empty_size
);
4793 * If we didn't find a chunk, and we haven't failed on this
4794 * block group before, and this block group is in the middle of
4795 * caching and we are ok with waiting, then go ahead and wait
4796 * for progress to be made, and set failed_alloc to true.
4798 * If failed_alloc is true then we've already waited on this
4799 * block group once and should move on to the next block group.
4801 if (!offset
&& !failed_alloc
&& !cached
&&
4802 loop
> LOOP_CACHING_NOWAIT
) {
4803 wait_block_group_cache_progress(block_group
,
4804 num_bytes
+ empty_size
);
4805 failed_alloc
= true;
4806 goto have_block_group
;
4807 } else if (!offset
) {
4811 search_start
= stripe_align(root
, offset
);
4812 /* move on to the next group */
4813 if (search_start
+ num_bytes
>= search_end
) {
4814 btrfs_add_free_space(block_group
, offset
, num_bytes
);
4818 /* move on to the next group */
4819 if (search_start
+ num_bytes
>
4820 block_group
->key
.objectid
+ block_group
->key
.offset
) {
4821 btrfs_add_free_space(block_group
, offset
, num_bytes
);
4825 ins
->objectid
= search_start
;
4826 ins
->offset
= num_bytes
;
4828 if (offset
< search_start
)
4829 btrfs_add_free_space(block_group
, offset
,
4830 search_start
- offset
);
4831 BUG_ON(offset
> search_start
);
4833 ret
= update_reserved_bytes(block_group
, num_bytes
, 1,
4834 (data
& BTRFS_BLOCK_GROUP_DATA
));
4835 if (ret
== -EAGAIN
) {
4836 btrfs_add_free_space(block_group
, offset
, num_bytes
);
4840 /* we are all good, lets return */
4841 ins
->objectid
= search_start
;
4842 ins
->offset
= num_bytes
;
4844 if (offset
< search_start
)
4845 btrfs_add_free_space(block_group
, offset
,
4846 search_start
- offset
);
4847 BUG_ON(offset
> search_start
);
4850 failed_cluster_refill
= false;
4851 failed_alloc
= false;
4852 BUG_ON(index
!= get_block_group_index(block_group
));
4853 btrfs_put_block_group(block_group
);
4855 up_read(&space_info
->groups_sem
);
4857 if (!ins
->objectid
&& ++index
< BTRFS_NR_RAID_TYPES
)
4860 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
4861 * for them to make caching progress. Also
4862 * determine the best possible bg to cache
4863 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
4864 * caching kthreads as we move along
4865 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
4866 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
4867 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
4870 if (!ins
->objectid
&& loop
< LOOP_NO_EMPTY_SIZE
&&
4871 (found_uncached_bg
|| empty_size
|| empty_cluster
||
4872 allowed_chunk_alloc
)) {
4874 if (loop
== LOOP_FIND_IDEAL
&& found_uncached_bg
) {
4875 found_uncached_bg
= false;
4877 if (!ideal_cache_percent
&&
4878 atomic_read(&space_info
->caching_threads
))
4882 * 1 of the following 2 things have happened so far
4884 * 1) We found an ideal block group for caching that
4885 * is mostly full and will cache quickly, so we might
4886 * as well wait for it.
4888 * 2) We searched for cached only and we didn't find
4889 * anything, and we didn't start any caching kthreads
4890 * either, so chances are we will loop through and
4891 * start a couple caching kthreads, and then come back
4892 * around and just wait for them. This will be slower
4893 * because we will have 2 caching kthreads reading at
4894 * the same time when we could have just started one
4895 * and waited for it to get far enough to give us an
4896 * allocation, so go ahead and go to the wait caching
4899 loop
= LOOP_CACHING_WAIT
;
4900 search_start
= ideal_cache_offset
;
4901 ideal_cache_percent
= 0;
4903 } else if (loop
== LOOP_FIND_IDEAL
) {
4905 * Didn't find a uncached bg, wait on anything we find
4908 loop
= LOOP_CACHING_WAIT
;
4912 if (loop
< LOOP_CACHING_WAIT
) {
4917 if (loop
== LOOP_ALLOC_CHUNK
) {
4922 if (allowed_chunk_alloc
) {
4923 ret
= do_chunk_alloc(trans
, root
, num_bytes
+
4924 2 * 1024 * 1024, data
, 1);
4925 allowed_chunk_alloc
= 0;
4926 done_chunk_alloc
= 1;
4927 } else if (!done_chunk_alloc
) {
4928 space_info
->force_alloc
= 1;
4931 if (loop
< LOOP_NO_EMPTY_SIZE
) {
4936 } else if (!ins
->objectid
) {
4940 /* we found what we needed */
4941 if (ins
->objectid
) {
4942 if (!(data
& BTRFS_BLOCK_GROUP_DATA
))
4943 trans
->block_group
= block_group
->key
.objectid
;
4945 btrfs_put_block_group(block_group
);
4952 static void dump_space_info(struct btrfs_space_info
*info
, u64 bytes
,
4953 int dump_block_groups
)
4955 struct btrfs_block_group_cache
*cache
;
4958 spin_lock(&info
->lock
);
4959 printk(KERN_INFO
"space_info has %llu free, is %sfull\n",
4960 (unsigned long long)(info
->total_bytes
- info
->bytes_used
-
4961 info
->bytes_pinned
- info
->bytes_reserved
-
4962 info
->bytes_readonly
),
4963 (info
->full
) ? "" : "not ");
4964 printk(KERN_INFO
"space_info total=%llu, used=%llu, pinned=%llu, "
4965 "reserved=%llu, may_use=%llu, readonly=%llu\n",
4966 (unsigned long long)info
->total_bytes
,
4967 (unsigned long long)info
->bytes_used
,
4968 (unsigned long long)info
->bytes_pinned
,
4969 (unsigned long long)info
->bytes_reserved
,
4970 (unsigned long long)info
->bytes_may_use
,
4971 (unsigned long long)info
->bytes_readonly
);
4972 spin_unlock(&info
->lock
);
4974 if (!dump_block_groups
)
4977 down_read(&info
->groups_sem
);
4979 list_for_each_entry(cache
, &info
->block_groups
[index
], list
) {
4980 spin_lock(&cache
->lock
);
4981 printk(KERN_INFO
"block group %llu has %llu bytes, %llu used "
4982 "%llu pinned %llu reserved\n",
4983 (unsigned long long)cache
->key
.objectid
,
4984 (unsigned long long)cache
->key
.offset
,
4985 (unsigned long long)btrfs_block_group_used(&cache
->item
),
4986 (unsigned long long)cache
->pinned
,
4987 (unsigned long long)cache
->reserved
);
4988 btrfs_dump_free_space(cache
, bytes
);
4989 spin_unlock(&cache
->lock
);
4991 if (++index
< BTRFS_NR_RAID_TYPES
)
4993 up_read(&info
->groups_sem
);
4996 int btrfs_reserve_extent(struct btrfs_trans_handle
*trans
,
4997 struct btrfs_root
*root
,
4998 u64 num_bytes
, u64 min_alloc_size
,
4999 u64 empty_size
, u64 hint_byte
,
5000 u64 search_end
, struct btrfs_key
*ins
,
5004 u64 search_start
= 0;
5006 data
= btrfs_get_alloc_profile(root
, data
);
5009 * the only place that sets empty_size is btrfs_realloc_node, which
5010 * is not called recursively on allocations
5012 if (empty_size
|| root
->ref_cows
)
5013 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
5014 num_bytes
+ 2 * 1024 * 1024, data
, 0);
5016 WARN_ON(num_bytes
< root
->sectorsize
);
5017 ret
= find_free_extent(trans
, root
, num_bytes
, empty_size
,
5018 search_start
, search_end
, hint_byte
,
5021 if (ret
== -ENOSPC
&& num_bytes
> min_alloc_size
) {
5022 num_bytes
= num_bytes
>> 1;
5023 num_bytes
= num_bytes
& ~(root
->sectorsize
- 1);
5024 num_bytes
= max(num_bytes
, min_alloc_size
);
5025 do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
5026 num_bytes
, data
, 1);
5029 if (ret
== -ENOSPC
) {
5030 struct btrfs_space_info
*sinfo
;
5032 sinfo
= __find_space_info(root
->fs_info
, data
);
5033 printk(KERN_ERR
"btrfs allocation failed flags %llu, "
5034 "wanted %llu\n", (unsigned long long)data
,
5035 (unsigned long long)num_bytes
);
5036 dump_space_info(sinfo
, num_bytes
, 1);
5042 int btrfs_free_reserved_extent(struct btrfs_root
*root
, u64 start
, u64 len
)
5044 struct btrfs_block_group_cache
*cache
;
5047 cache
= btrfs_lookup_block_group(root
->fs_info
, start
);
5049 printk(KERN_ERR
"Unable to find block group for %llu\n",
5050 (unsigned long long)start
);
5054 ret
= btrfs_discard_extent(root
, start
, len
);
5056 btrfs_add_free_space(cache
, start
, len
);
5057 update_reserved_bytes(cache
, len
, 0, 1);
5058 btrfs_put_block_group(cache
);
5063 static int alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
5064 struct btrfs_root
*root
,
5065 u64 parent
, u64 root_objectid
,
5066 u64 flags
, u64 owner
, u64 offset
,
5067 struct btrfs_key
*ins
, int ref_mod
)
5070 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5071 struct btrfs_extent_item
*extent_item
;
5072 struct btrfs_extent_inline_ref
*iref
;
5073 struct btrfs_path
*path
;
5074 struct extent_buffer
*leaf
;
5079 type
= BTRFS_SHARED_DATA_REF_KEY
;
5081 type
= BTRFS_EXTENT_DATA_REF_KEY
;
5083 size
= sizeof(*extent_item
) + btrfs_extent_inline_ref_size(type
);
5085 path
= btrfs_alloc_path();
5088 path
->leave_spinning
= 1;
5089 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
5093 leaf
= path
->nodes
[0];
5094 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
5095 struct btrfs_extent_item
);
5096 btrfs_set_extent_refs(leaf
, extent_item
, ref_mod
);
5097 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
5098 btrfs_set_extent_flags(leaf
, extent_item
,
5099 flags
| BTRFS_EXTENT_FLAG_DATA
);
5101 iref
= (struct btrfs_extent_inline_ref
*)(extent_item
+ 1);
5102 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
5104 struct btrfs_shared_data_ref
*ref
;
5105 ref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
5106 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
5107 btrfs_set_shared_data_ref_count(leaf
, ref
, ref_mod
);
5109 struct btrfs_extent_data_ref
*ref
;
5110 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
5111 btrfs_set_extent_data_ref_root(leaf
, ref
, root_objectid
);
5112 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
5113 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
5114 btrfs_set_extent_data_ref_count(leaf
, ref
, ref_mod
);
5117 btrfs_mark_buffer_dirty(path
->nodes
[0]);
5118 btrfs_free_path(path
);
5120 ret
= update_block_group(trans
, root
, ins
->objectid
, ins
->offset
, 1);
5122 printk(KERN_ERR
"btrfs update block group failed for %llu "
5123 "%llu\n", (unsigned long long)ins
->objectid
,
5124 (unsigned long long)ins
->offset
);
5130 static int alloc_reserved_tree_block(struct btrfs_trans_handle
*trans
,
5131 struct btrfs_root
*root
,
5132 u64 parent
, u64 root_objectid
,
5133 u64 flags
, struct btrfs_disk_key
*key
,
5134 int level
, struct btrfs_key
*ins
)
5137 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5138 struct btrfs_extent_item
*extent_item
;
5139 struct btrfs_tree_block_info
*block_info
;
5140 struct btrfs_extent_inline_ref
*iref
;
5141 struct btrfs_path
*path
;
5142 struct extent_buffer
*leaf
;
5143 u32 size
= sizeof(*extent_item
) + sizeof(*block_info
) + sizeof(*iref
);
5145 path
= btrfs_alloc_path();
5148 path
->leave_spinning
= 1;
5149 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
5153 leaf
= path
->nodes
[0];
5154 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
5155 struct btrfs_extent_item
);
5156 btrfs_set_extent_refs(leaf
, extent_item
, 1);
5157 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
5158 btrfs_set_extent_flags(leaf
, extent_item
,
5159 flags
| BTRFS_EXTENT_FLAG_TREE_BLOCK
);
5160 block_info
= (struct btrfs_tree_block_info
*)(extent_item
+ 1);
5162 btrfs_set_tree_block_key(leaf
, block_info
, key
);
5163 btrfs_set_tree_block_level(leaf
, block_info
, level
);
5165 iref
= (struct btrfs_extent_inline_ref
*)(block_info
+ 1);
5167 BUG_ON(!(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
));
5168 btrfs_set_extent_inline_ref_type(leaf
, iref
,
5169 BTRFS_SHARED_BLOCK_REF_KEY
);
5170 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
5172 btrfs_set_extent_inline_ref_type(leaf
, iref
,
5173 BTRFS_TREE_BLOCK_REF_KEY
);
5174 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
5177 btrfs_mark_buffer_dirty(leaf
);
5178 btrfs_free_path(path
);
5180 ret
= update_block_group(trans
, root
, ins
->objectid
, ins
->offset
, 1);
5182 printk(KERN_ERR
"btrfs update block group failed for %llu "
5183 "%llu\n", (unsigned long long)ins
->objectid
,
5184 (unsigned long long)ins
->offset
);
5190 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
5191 struct btrfs_root
*root
,
5192 u64 root_objectid
, u64 owner
,
5193 u64 offset
, struct btrfs_key
*ins
)
5197 BUG_ON(root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
5199 ret
= btrfs_add_delayed_data_ref(trans
, ins
->objectid
, ins
->offset
,
5200 0, root_objectid
, owner
, offset
,
5201 BTRFS_ADD_DELAYED_EXTENT
, NULL
);
5206 * this is used by the tree logging recovery code. It records that
5207 * an extent has been allocated and makes sure to clear the free
5208 * space cache bits as well
5210 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle
*trans
,
5211 struct btrfs_root
*root
,
5212 u64 root_objectid
, u64 owner
, u64 offset
,
5213 struct btrfs_key
*ins
)
5216 struct btrfs_block_group_cache
*block_group
;
5217 struct btrfs_caching_control
*caching_ctl
;
5218 u64 start
= ins
->objectid
;
5219 u64 num_bytes
= ins
->offset
;
5221 block_group
= btrfs_lookup_block_group(root
->fs_info
, ins
->objectid
);
5222 cache_block_group(block_group
);
5223 caching_ctl
= get_caching_control(block_group
);
5226 BUG_ON(!block_group_cache_done(block_group
));
5227 ret
= btrfs_remove_free_space(block_group
, start
, num_bytes
);
5230 mutex_lock(&caching_ctl
->mutex
);
5232 if (start
>= caching_ctl
->progress
) {
5233 ret
= add_excluded_extent(root
, start
, num_bytes
);
5235 } else if (start
+ num_bytes
<= caching_ctl
->progress
) {
5236 ret
= btrfs_remove_free_space(block_group
,
5240 num_bytes
= caching_ctl
->progress
- start
;
5241 ret
= btrfs_remove_free_space(block_group
,
5245 start
= caching_ctl
->progress
;
5246 num_bytes
= ins
->objectid
+ ins
->offset
-
5247 caching_ctl
->progress
;
5248 ret
= add_excluded_extent(root
, start
, num_bytes
);
5252 mutex_unlock(&caching_ctl
->mutex
);
5253 put_caching_control(caching_ctl
);
5256 ret
= update_reserved_bytes(block_group
, ins
->offset
, 1, 1);
5258 btrfs_put_block_group(block_group
);
5259 ret
= alloc_reserved_file_extent(trans
, root
, 0, root_objectid
,
5260 0, owner
, offset
, ins
, 1);
5264 struct extent_buffer
*btrfs_init_new_buffer(struct btrfs_trans_handle
*trans
,
5265 struct btrfs_root
*root
,
5266 u64 bytenr
, u32 blocksize
,
5269 struct extent_buffer
*buf
;
5271 buf
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
5273 return ERR_PTR(-ENOMEM
);
5274 btrfs_set_header_generation(buf
, trans
->transid
);
5275 btrfs_set_buffer_lockdep_class(buf
, level
);
5276 btrfs_tree_lock(buf
);
5277 clean_tree_block(trans
, root
, buf
);
5279 btrfs_set_lock_blocking(buf
);
5280 btrfs_set_buffer_uptodate(buf
);
5282 if (root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
) {
5284 * we allow two log transactions at a time, use different
5285 * EXENT bit to differentiate dirty pages.
5287 if (root
->log_transid
% 2 == 0)
5288 set_extent_dirty(&root
->dirty_log_pages
, buf
->start
,
5289 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
5291 set_extent_new(&root
->dirty_log_pages
, buf
->start
,
5292 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
5294 set_extent_dirty(&trans
->transaction
->dirty_pages
, buf
->start
,
5295 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
5297 trans
->blocks_used
++;
5298 /* this returns a buffer locked for blocking */
5302 static struct btrfs_block_rsv
*
5303 use_block_rsv(struct btrfs_trans_handle
*trans
,
5304 struct btrfs_root
*root
, u32 blocksize
)
5306 struct btrfs_block_rsv
*block_rsv
;
5309 block_rsv
= get_block_rsv(trans
, root
);
5311 if (block_rsv
->size
== 0) {
5312 ret
= reserve_metadata_bytes(block_rsv
, blocksize
);
5314 return ERR_PTR(ret
);
5318 ret
= block_rsv_use_bytes(block_rsv
, blocksize
);
5323 printk(KERN_INFO
"block_rsv size %llu reserved %llu freed %llu %llu\n",
5324 block_rsv
->size
, block_rsv
->reserved
,
5325 block_rsv
->freed
[0], block_rsv
->freed
[1]);
5327 return ERR_PTR(-ENOSPC
);
5330 static void unuse_block_rsv(struct btrfs_block_rsv
*block_rsv
, u32 blocksize
)
5332 block_rsv_add_bytes(block_rsv
, blocksize
, 0);
5333 block_rsv_release_bytes(block_rsv
, NULL
, 0);
5337 * finds a free extent and does all the dirty work required for allocation
5338 * returns the key for the extent through ins, and a tree buffer for
5339 * the first block of the extent through buf.
5341 * returns the tree buffer or NULL.
5343 struct extent_buffer
*btrfs_alloc_free_block(struct btrfs_trans_handle
*trans
,
5344 struct btrfs_root
*root
, u32 blocksize
,
5345 u64 parent
, u64 root_objectid
,
5346 struct btrfs_disk_key
*key
, int level
,
5347 u64 hint
, u64 empty_size
)
5349 struct btrfs_key ins
;
5350 struct btrfs_block_rsv
*block_rsv
;
5351 struct extent_buffer
*buf
;
5356 block_rsv
= use_block_rsv(trans
, root
, blocksize
);
5357 if (IS_ERR(block_rsv
))
5358 return ERR_CAST(block_rsv
);
5360 ret
= btrfs_reserve_extent(trans
, root
, blocksize
, blocksize
,
5361 empty_size
, hint
, (u64
)-1, &ins
, 0);
5363 unuse_block_rsv(block_rsv
, blocksize
);
5364 return ERR_PTR(ret
);
5367 buf
= btrfs_init_new_buffer(trans
, root
, ins
.objectid
,
5369 BUG_ON(IS_ERR(buf
));
5371 if (root_objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
5373 parent
= ins
.objectid
;
5374 flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
5378 if (root_objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
5379 struct btrfs_delayed_extent_op
*extent_op
;
5380 extent_op
= kmalloc(sizeof(*extent_op
), GFP_NOFS
);
5383 memcpy(&extent_op
->key
, key
, sizeof(extent_op
->key
));
5385 memset(&extent_op
->key
, 0, sizeof(extent_op
->key
));
5386 extent_op
->flags_to_set
= flags
;
5387 extent_op
->update_key
= 1;
5388 extent_op
->update_flags
= 1;
5389 extent_op
->is_data
= 0;
5391 ret
= btrfs_add_delayed_tree_ref(trans
, ins
.objectid
,
5392 ins
.offset
, parent
, root_objectid
,
5393 level
, BTRFS_ADD_DELAYED_EXTENT
,
5400 struct walk_control
{
5401 u64 refs
[BTRFS_MAX_LEVEL
];
5402 u64 flags
[BTRFS_MAX_LEVEL
];
5403 struct btrfs_key update_progress
;
5413 #define DROP_REFERENCE 1
5414 #define UPDATE_BACKREF 2
5416 static noinline
void reada_walk_down(struct btrfs_trans_handle
*trans
,
5417 struct btrfs_root
*root
,
5418 struct walk_control
*wc
,
5419 struct btrfs_path
*path
)
5428 struct btrfs_key key
;
5429 struct extent_buffer
*eb
;
5434 if (path
->slots
[wc
->level
] < wc
->reada_slot
) {
5435 wc
->reada_count
= wc
->reada_count
* 2 / 3;
5436 wc
->reada_count
= max(wc
->reada_count
, 2);
5438 wc
->reada_count
= wc
->reada_count
* 3 / 2;
5439 wc
->reada_count
= min_t(int, wc
->reada_count
,
5440 BTRFS_NODEPTRS_PER_BLOCK(root
));
5443 eb
= path
->nodes
[wc
->level
];
5444 nritems
= btrfs_header_nritems(eb
);
5445 blocksize
= btrfs_level_size(root
, wc
->level
- 1);
5447 for (slot
= path
->slots
[wc
->level
]; slot
< nritems
; slot
++) {
5448 if (nread
>= wc
->reada_count
)
5452 bytenr
= btrfs_node_blockptr(eb
, slot
);
5453 generation
= btrfs_node_ptr_generation(eb
, slot
);
5455 if (slot
== path
->slots
[wc
->level
])
5458 if (wc
->stage
== UPDATE_BACKREF
&&
5459 generation
<= root
->root_key
.offset
)
5462 /* We don't lock the tree block, it's OK to be racy here */
5463 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
, blocksize
,
5468 if (wc
->stage
== DROP_REFERENCE
) {
5472 if (wc
->level
== 1 &&
5473 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
5475 if (!wc
->update_ref
||
5476 generation
<= root
->root_key
.offset
)
5478 btrfs_node_key_to_cpu(eb
, &key
, slot
);
5479 ret
= btrfs_comp_cpu_keys(&key
,
5480 &wc
->update_progress
);
5484 if (wc
->level
== 1 &&
5485 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
5489 ret
= readahead_tree_block(root
, bytenr
, blocksize
,
5493 last
= bytenr
+ blocksize
;
5496 wc
->reada_slot
= slot
;
5500 * hepler to process tree block while walking down the tree.
5502 * when wc->stage == UPDATE_BACKREF, this function updates
5503 * back refs for pointers in the block.
5505 * NOTE: return value 1 means we should stop walking down.
5507 static noinline
int walk_down_proc(struct btrfs_trans_handle
*trans
,
5508 struct btrfs_root
*root
,
5509 struct btrfs_path
*path
,
5510 struct walk_control
*wc
, int lookup_info
)
5512 int level
= wc
->level
;
5513 struct extent_buffer
*eb
= path
->nodes
[level
];
5514 u64 flag
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
5517 if (wc
->stage
== UPDATE_BACKREF
&&
5518 btrfs_header_owner(eb
) != root
->root_key
.objectid
)
5522 * when reference count of tree block is 1, it won't increase
5523 * again. once full backref flag is set, we never clear it.
5526 ((wc
->stage
== DROP_REFERENCE
&& wc
->refs
[level
] != 1) ||
5527 (wc
->stage
== UPDATE_BACKREF
&& !(wc
->flags
[level
] & flag
)))) {
5528 BUG_ON(!path
->locks
[level
]);
5529 ret
= btrfs_lookup_extent_info(trans
, root
,
5534 BUG_ON(wc
->refs
[level
] == 0);
5537 if (wc
->stage
== DROP_REFERENCE
) {
5538 if (wc
->refs
[level
] > 1)
5541 if (path
->locks
[level
] && !wc
->keep_locks
) {
5542 btrfs_tree_unlock(eb
);
5543 path
->locks
[level
] = 0;
5548 /* wc->stage == UPDATE_BACKREF */
5549 if (!(wc
->flags
[level
] & flag
)) {
5550 BUG_ON(!path
->locks
[level
]);
5551 ret
= btrfs_inc_ref(trans
, root
, eb
, 1);
5553 ret
= btrfs_dec_ref(trans
, root
, eb
, 0);
5555 ret
= btrfs_set_disk_extent_flags(trans
, root
, eb
->start
,
5558 wc
->flags
[level
] |= flag
;
5562 * the block is shared by multiple trees, so it's not good to
5563 * keep the tree lock
5565 if (path
->locks
[level
] && level
> 0) {
5566 btrfs_tree_unlock(eb
);
5567 path
->locks
[level
] = 0;
5573 * hepler to process tree block pointer.
5575 * when wc->stage == DROP_REFERENCE, this function checks
5576 * reference count of the block pointed to. if the block
5577 * is shared and we need update back refs for the subtree
5578 * rooted at the block, this function changes wc->stage to
5579 * UPDATE_BACKREF. if the block is shared and there is no
5580 * need to update back, this function drops the reference
5583 * NOTE: return value 1 means we should stop walking down.
5585 static noinline
int do_walk_down(struct btrfs_trans_handle
*trans
,
5586 struct btrfs_root
*root
,
5587 struct btrfs_path
*path
,
5588 struct walk_control
*wc
, int *lookup_info
)
5594 struct btrfs_key key
;
5595 struct extent_buffer
*next
;
5596 int level
= wc
->level
;
5600 generation
= btrfs_node_ptr_generation(path
->nodes
[level
],
5601 path
->slots
[level
]);
5603 * if the lower level block was created before the snapshot
5604 * was created, we know there is no need to update back refs
5607 if (wc
->stage
== UPDATE_BACKREF
&&
5608 generation
<= root
->root_key
.offset
) {
5613 bytenr
= btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]);
5614 blocksize
= btrfs_level_size(root
, level
- 1);
5616 next
= btrfs_find_tree_block(root
, bytenr
, blocksize
);
5618 next
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
5623 btrfs_tree_lock(next
);
5624 btrfs_set_lock_blocking(next
);
5626 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
, blocksize
,
5627 &wc
->refs
[level
- 1],
5628 &wc
->flags
[level
- 1]);
5630 BUG_ON(wc
->refs
[level
- 1] == 0);
5633 if (wc
->stage
== DROP_REFERENCE
) {
5634 if (wc
->refs
[level
- 1] > 1) {
5636 (wc
->flags
[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF
))
5639 if (!wc
->update_ref
||
5640 generation
<= root
->root_key
.offset
)
5643 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
5644 path
->slots
[level
]);
5645 ret
= btrfs_comp_cpu_keys(&key
, &wc
->update_progress
);
5649 wc
->stage
= UPDATE_BACKREF
;
5650 wc
->shared_level
= level
- 1;
5654 (wc
->flags
[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF
))
5658 if (!btrfs_buffer_uptodate(next
, generation
)) {
5659 btrfs_tree_unlock(next
);
5660 free_extent_buffer(next
);
5666 if (reada
&& level
== 1)
5667 reada_walk_down(trans
, root
, wc
, path
);
5668 next
= read_tree_block(root
, bytenr
, blocksize
, generation
);
5669 btrfs_tree_lock(next
);
5670 btrfs_set_lock_blocking(next
);
5674 BUG_ON(level
!= btrfs_header_level(next
));
5675 path
->nodes
[level
] = next
;
5676 path
->slots
[level
] = 0;
5677 path
->locks
[level
] = 1;
5683 wc
->refs
[level
- 1] = 0;
5684 wc
->flags
[level
- 1] = 0;
5685 if (wc
->stage
== DROP_REFERENCE
) {
5686 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
) {
5687 parent
= path
->nodes
[level
]->start
;
5689 BUG_ON(root
->root_key
.objectid
!=
5690 btrfs_header_owner(path
->nodes
[level
]));
5694 ret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
, parent
,
5695 root
->root_key
.objectid
, level
- 1, 0);
5698 btrfs_tree_unlock(next
);
5699 free_extent_buffer(next
);
5705 * hepler to process tree block while walking up the tree.
5707 * when wc->stage == DROP_REFERENCE, this function drops
5708 * reference count on the block.
5710 * when wc->stage == UPDATE_BACKREF, this function changes
5711 * wc->stage back to DROP_REFERENCE if we changed wc->stage
5712 * to UPDATE_BACKREF previously while processing the block.
5714 * NOTE: return value 1 means we should stop walking up.
5716 static noinline
int walk_up_proc(struct btrfs_trans_handle
*trans
,
5717 struct btrfs_root
*root
,
5718 struct btrfs_path
*path
,
5719 struct walk_control
*wc
)
5722 int level
= wc
->level
;
5723 struct extent_buffer
*eb
= path
->nodes
[level
];
5726 if (wc
->stage
== UPDATE_BACKREF
) {
5727 BUG_ON(wc
->shared_level
< level
);
5728 if (level
< wc
->shared_level
)
5731 ret
= find_next_key(path
, level
+ 1, &wc
->update_progress
);
5735 wc
->stage
= DROP_REFERENCE
;
5736 wc
->shared_level
= -1;
5737 path
->slots
[level
] = 0;
5740 * check reference count again if the block isn't locked.
5741 * we should start walking down the tree again if reference
5744 if (!path
->locks
[level
]) {
5746 btrfs_tree_lock(eb
);
5747 btrfs_set_lock_blocking(eb
);
5748 path
->locks
[level
] = 1;
5750 ret
= btrfs_lookup_extent_info(trans
, root
,
5755 BUG_ON(wc
->refs
[level
] == 0);
5756 if (wc
->refs
[level
] == 1) {
5757 btrfs_tree_unlock(eb
);
5758 path
->locks
[level
] = 0;
5764 /* wc->stage == DROP_REFERENCE */
5765 BUG_ON(wc
->refs
[level
] > 1 && !path
->locks
[level
]);
5767 if (wc
->refs
[level
] == 1) {
5769 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
5770 ret
= btrfs_dec_ref(trans
, root
, eb
, 1);
5772 ret
= btrfs_dec_ref(trans
, root
, eb
, 0);
5775 /* make block locked assertion in clean_tree_block happy */
5776 if (!path
->locks
[level
] &&
5777 btrfs_header_generation(eb
) == trans
->transid
) {
5778 btrfs_tree_lock(eb
);
5779 btrfs_set_lock_blocking(eb
);
5780 path
->locks
[level
] = 1;
5782 clean_tree_block(trans
, root
, eb
);
5785 if (eb
== root
->node
) {
5786 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
5789 BUG_ON(root
->root_key
.objectid
!=
5790 btrfs_header_owner(eb
));
5792 if (wc
->flags
[level
+ 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
5793 parent
= path
->nodes
[level
+ 1]->start
;
5795 BUG_ON(root
->root_key
.objectid
!=
5796 btrfs_header_owner(path
->nodes
[level
+ 1]));
5799 btrfs_free_tree_block(trans
, root
, eb
, parent
, wc
->refs
[level
] == 1);
5801 wc
->refs
[level
] = 0;
5802 wc
->flags
[level
] = 0;
5806 static noinline
int walk_down_tree(struct btrfs_trans_handle
*trans
,
5807 struct btrfs_root
*root
,
5808 struct btrfs_path
*path
,
5809 struct walk_control
*wc
)
5811 int level
= wc
->level
;
5812 int lookup_info
= 1;
5815 while (level
>= 0) {
5816 ret
= walk_down_proc(trans
, root
, path
, wc
, lookup_info
);
5823 if (path
->slots
[level
] >=
5824 btrfs_header_nritems(path
->nodes
[level
]))
5827 ret
= do_walk_down(trans
, root
, path
, wc
, &lookup_info
);
5829 path
->slots
[level
]++;
5838 static noinline
int walk_up_tree(struct btrfs_trans_handle
*trans
,
5839 struct btrfs_root
*root
,
5840 struct btrfs_path
*path
,
5841 struct walk_control
*wc
, int max_level
)
5843 int level
= wc
->level
;
5846 path
->slots
[level
] = btrfs_header_nritems(path
->nodes
[level
]);
5847 while (level
< max_level
&& path
->nodes
[level
]) {
5849 if (path
->slots
[level
] + 1 <
5850 btrfs_header_nritems(path
->nodes
[level
])) {
5851 path
->slots
[level
]++;
5854 ret
= walk_up_proc(trans
, root
, path
, wc
);
5858 if (path
->locks
[level
]) {
5859 btrfs_tree_unlock(path
->nodes
[level
]);
5860 path
->locks
[level
] = 0;
5862 free_extent_buffer(path
->nodes
[level
]);
5863 path
->nodes
[level
] = NULL
;
5871 * drop a subvolume tree.
5873 * this function traverses the tree freeing any blocks that only
5874 * referenced by the tree.
5876 * when a shared tree block is found. this function decreases its
5877 * reference count by one. if update_ref is true, this function
5878 * also make sure backrefs for the shared block and all lower level
5879 * blocks are properly updated.
5881 int btrfs_drop_snapshot(struct btrfs_root
*root
,
5882 struct btrfs_block_rsv
*block_rsv
, int update_ref
)
5884 struct btrfs_path
*path
;
5885 struct btrfs_trans_handle
*trans
;
5886 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
5887 struct btrfs_root_item
*root_item
= &root
->root_item
;
5888 struct walk_control
*wc
;
5889 struct btrfs_key key
;
5894 path
= btrfs_alloc_path();
5897 wc
= kzalloc(sizeof(*wc
), GFP_NOFS
);
5900 trans
= btrfs_start_transaction(tree_root
, 0);
5902 trans
->block_rsv
= block_rsv
;
5904 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
5905 level
= btrfs_header_level(root
->node
);
5906 path
->nodes
[level
] = btrfs_lock_root_node(root
);
5907 btrfs_set_lock_blocking(path
->nodes
[level
]);
5908 path
->slots
[level
] = 0;
5909 path
->locks
[level
] = 1;
5910 memset(&wc
->update_progress
, 0,
5911 sizeof(wc
->update_progress
));
5913 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
5914 memcpy(&wc
->update_progress
, &key
,
5915 sizeof(wc
->update_progress
));
5917 level
= root_item
->drop_level
;
5919 path
->lowest_level
= level
;
5920 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5921 path
->lowest_level
= 0;
5929 * unlock our path, this is safe because only this
5930 * function is allowed to delete this snapshot
5932 btrfs_unlock_up_safe(path
, 0);
5934 level
= btrfs_header_level(root
->node
);
5936 btrfs_tree_lock(path
->nodes
[level
]);
5937 btrfs_set_lock_blocking(path
->nodes
[level
]);
5939 ret
= btrfs_lookup_extent_info(trans
, root
,
5940 path
->nodes
[level
]->start
,
5941 path
->nodes
[level
]->len
,
5945 BUG_ON(wc
->refs
[level
] == 0);
5947 if (level
== root_item
->drop_level
)
5950 btrfs_tree_unlock(path
->nodes
[level
]);
5951 WARN_ON(wc
->refs
[level
] != 1);
5957 wc
->shared_level
= -1;
5958 wc
->stage
= DROP_REFERENCE
;
5959 wc
->update_ref
= update_ref
;
5961 wc
->reada_count
= BTRFS_NODEPTRS_PER_BLOCK(root
);
5964 ret
= walk_down_tree(trans
, root
, path
, wc
);
5970 ret
= walk_up_tree(trans
, root
, path
, wc
, BTRFS_MAX_LEVEL
);
5977 BUG_ON(wc
->stage
!= DROP_REFERENCE
);
5981 if (wc
->stage
== DROP_REFERENCE
) {
5983 btrfs_node_key(path
->nodes
[level
],
5984 &root_item
->drop_progress
,
5985 path
->slots
[level
]);
5986 root_item
->drop_level
= level
;
5989 BUG_ON(wc
->level
== 0);
5990 if (btrfs_should_end_transaction(trans
, tree_root
)) {
5991 ret
= btrfs_update_root(trans
, tree_root
,
5996 btrfs_end_transaction_throttle(trans
, tree_root
);
5997 trans
= btrfs_start_transaction(tree_root
, 0);
5999 trans
->block_rsv
= block_rsv
;
6002 btrfs_release_path(root
, path
);
6005 ret
= btrfs_del_root(trans
, tree_root
, &root
->root_key
);
6008 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
6009 ret
= btrfs_find_last_root(tree_root
, root
->root_key
.objectid
,
6013 ret
= btrfs_del_orphan_item(trans
, tree_root
,
6014 root
->root_key
.objectid
);
6019 if (root
->in_radix
) {
6020 btrfs_free_fs_root(tree_root
->fs_info
, root
);
6022 free_extent_buffer(root
->node
);
6023 free_extent_buffer(root
->commit_root
);
6027 btrfs_end_transaction_throttle(trans
, tree_root
);
6029 btrfs_free_path(path
);
6034 * drop subtree rooted at tree block 'node'.
6036 * NOTE: this function will unlock and release tree block 'node'
6038 int btrfs_drop_subtree(struct btrfs_trans_handle
*trans
,
6039 struct btrfs_root
*root
,
6040 struct extent_buffer
*node
,
6041 struct extent_buffer
*parent
)
6043 struct btrfs_path
*path
;
6044 struct walk_control
*wc
;
6050 BUG_ON(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
6052 path
= btrfs_alloc_path();
6055 wc
= kzalloc(sizeof(*wc
), GFP_NOFS
);
6058 btrfs_assert_tree_locked(parent
);
6059 parent_level
= btrfs_header_level(parent
);
6060 extent_buffer_get(parent
);
6061 path
->nodes
[parent_level
] = parent
;
6062 path
->slots
[parent_level
] = btrfs_header_nritems(parent
);
6064 btrfs_assert_tree_locked(node
);
6065 level
= btrfs_header_level(node
);
6066 path
->nodes
[level
] = node
;
6067 path
->slots
[level
] = 0;
6068 path
->locks
[level
] = 1;
6070 wc
->refs
[parent_level
] = 1;
6071 wc
->flags
[parent_level
] = BTRFS_BLOCK_FLAG_FULL_BACKREF
;
6073 wc
->shared_level
= -1;
6074 wc
->stage
= DROP_REFERENCE
;
6077 wc
->reada_count
= BTRFS_NODEPTRS_PER_BLOCK(root
);
6080 wret
= walk_down_tree(trans
, root
, path
, wc
);
6086 wret
= walk_up_tree(trans
, root
, path
, wc
, parent_level
);
6094 btrfs_free_path(path
);
6099 static unsigned long calc_ra(unsigned long start
, unsigned long last
,
6102 return min(last
, start
+ nr
- 1);
6105 static noinline
int relocate_inode_pages(struct inode
*inode
, u64 start
,
6110 unsigned long first_index
;
6111 unsigned long last_index
;
6114 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6115 struct file_ra_state
*ra
;
6116 struct btrfs_ordered_extent
*ordered
;
6117 unsigned int total_read
= 0;
6118 unsigned int total_dirty
= 0;
6121 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
6123 mutex_lock(&inode
->i_mutex
);
6124 first_index
= start
>> PAGE_CACHE_SHIFT
;
6125 last_index
= (start
+ len
- 1) >> PAGE_CACHE_SHIFT
;
6127 /* make sure the dirty trick played by the caller work */
6128 ret
= invalidate_inode_pages2_range(inode
->i_mapping
,
6129 first_index
, last_index
);
6133 file_ra_state_init(ra
, inode
->i_mapping
);
6135 for (i
= first_index
; i
<= last_index
; i
++) {
6136 if (total_read
% ra
->ra_pages
== 0) {
6137 btrfs_force_ra(inode
->i_mapping
, ra
, NULL
, i
,
6138 calc_ra(i
, last_index
, ra
->ra_pages
));
6142 if (((u64
)i
<< PAGE_CACHE_SHIFT
) > i_size_read(inode
))
6144 page
= grab_cache_page(inode
->i_mapping
, i
);
6149 if (!PageUptodate(page
)) {
6150 btrfs_readpage(NULL
, page
);
6152 if (!PageUptodate(page
)) {
6154 page_cache_release(page
);
6159 wait_on_page_writeback(page
);
6161 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
6162 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
6163 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
6165 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
6167 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
6169 page_cache_release(page
);
6170 btrfs_start_ordered_extent(inode
, ordered
, 1);
6171 btrfs_put_ordered_extent(ordered
);
6174 set_page_extent_mapped(page
);
6176 if (i
== first_index
)
6177 set_extent_bits(io_tree
, page_start
, page_end
,
6178 EXTENT_BOUNDARY
, GFP_NOFS
);
6179 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
6181 set_page_dirty(page
);
6184 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
6186 page_cache_release(page
);
6191 mutex_unlock(&inode
->i_mutex
);
6192 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, total_dirty
);
6196 static noinline
int relocate_data_extent(struct inode
*reloc_inode
,
6197 struct btrfs_key
*extent_key
,
6200 struct btrfs_root
*root
= BTRFS_I(reloc_inode
)->root
;
6201 struct extent_map_tree
*em_tree
= &BTRFS_I(reloc_inode
)->extent_tree
;
6202 struct extent_map
*em
;
6203 u64 start
= extent_key
->objectid
- offset
;
6204 u64 end
= start
+ extent_key
->offset
- 1;
6206 em
= alloc_extent_map(GFP_NOFS
);
6207 BUG_ON(!em
|| IS_ERR(em
));
6210 em
->len
= extent_key
->offset
;
6211 em
->block_len
= extent_key
->offset
;
6212 em
->block_start
= extent_key
->objectid
;
6213 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6214 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
6216 /* setup extent map to cheat btrfs_readpage */
6217 lock_extent(&BTRFS_I(reloc_inode
)->io_tree
, start
, end
, GFP_NOFS
);
6220 write_lock(&em_tree
->lock
);
6221 ret
= add_extent_mapping(em_tree
, em
);
6222 write_unlock(&em_tree
->lock
);
6223 if (ret
!= -EEXIST
) {
6224 free_extent_map(em
);
6227 btrfs_drop_extent_cache(reloc_inode
, start
, end
, 0);
6229 unlock_extent(&BTRFS_I(reloc_inode
)->io_tree
, start
, end
, GFP_NOFS
);
6231 return relocate_inode_pages(reloc_inode
, start
, extent_key
->offset
);
6234 struct btrfs_ref_path
{
6236 u64 nodes
[BTRFS_MAX_LEVEL
];
6238 u64 root_generation
;
6245 struct btrfs_key node_keys
[BTRFS_MAX_LEVEL
];
6246 u64 new_nodes
[BTRFS_MAX_LEVEL
];
6249 struct disk_extent
{
6260 static int is_cowonly_root(u64 root_objectid
)
6262 if (root_objectid
== BTRFS_ROOT_TREE_OBJECTID
||
6263 root_objectid
== BTRFS_EXTENT_TREE_OBJECTID
||
6264 root_objectid
== BTRFS_CHUNK_TREE_OBJECTID
||
6265 root_objectid
== BTRFS_DEV_TREE_OBJECTID
||
6266 root_objectid
== BTRFS_TREE_LOG_OBJECTID
||
6267 root_objectid
== BTRFS_CSUM_TREE_OBJECTID
)
6272 static noinline
int __next_ref_path(struct btrfs_trans_handle
*trans
,
6273 struct btrfs_root
*extent_root
,
6274 struct btrfs_ref_path
*ref_path
,
6277 struct extent_buffer
*leaf
;
6278 struct btrfs_path
*path
;
6279 struct btrfs_extent_ref
*ref
;
6280 struct btrfs_key key
;
6281 struct btrfs_key found_key
;
6287 path
= btrfs_alloc_path();
6292 ref_path
->lowest_level
= -1;
6293 ref_path
->current_level
= -1;
6294 ref_path
->shared_level
= -1;
6298 level
= ref_path
->current_level
- 1;
6299 while (level
>= -1) {
6301 if (level
< ref_path
->lowest_level
)
6305 bytenr
= ref_path
->nodes
[level
];
6307 bytenr
= ref_path
->extent_start
;
6308 BUG_ON(bytenr
== 0);
6310 parent
= ref_path
->nodes
[level
+ 1];
6311 ref_path
->nodes
[level
+ 1] = 0;
6312 ref_path
->current_level
= level
;
6313 BUG_ON(parent
== 0);
6315 key
.objectid
= bytenr
;
6316 key
.offset
= parent
+ 1;
6317 key
.type
= BTRFS_EXTENT_REF_KEY
;
6319 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
, 0, 0);
6324 leaf
= path
->nodes
[0];
6325 nritems
= btrfs_header_nritems(leaf
);
6326 if (path
->slots
[0] >= nritems
) {
6327 ret
= btrfs_next_leaf(extent_root
, path
);
6332 leaf
= path
->nodes
[0];
6335 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6336 if (found_key
.objectid
== bytenr
&&
6337 found_key
.type
== BTRFS_EXTENT_REF_KEY
) {
6338 if (level
< ref_path
->shared_level
)
6339 ref_path
->shared_level
= level
;
6344 btrfs_release_path(extent_root
, path
);
6347 /* reached lowest level */
6351 level
= ref_path
->current_level
;
6352 while (level
< BTRFS_MAX_LEVEL
- 1) {
6356 bytenr
= ref_path
->nodes
[level
];
6358 bytenr
= ref_path
->extent_start
;
6360 BUG_ON(bytenr
== 0);
6362 key
.objectid
= bytenr
;
6364 key
.type
= BTRFS_EXTENT_REF_KEY
;
6366 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
, 0, 0);
6370 leaf
= path
->nodes
[0];
6371 nritems
= btrfs_header_nritems(leaf
);
6372 if (path
->slots
[0] >= nritems
) {
6373 ret
= btrfs_next_leaf(extent_root
, path
);
6377 /* the extent was freed by someone */
6378 if (ref_path
->lowest_level
== level
)
6380 btrfs_release_path(extent_root
, path
);
6383 leaf
= path
->nodes
[0];
6386 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6387 if (found_key
.objectid
!= bytenr
||
6388 found_key
.type
!= BTRFS_EXTENT_REF_KEY
) {
6389 /* the extent was freed by someone */
6390 if (ref_path
->lowest_level
== level
) {
6394 btrfs_release_path(extent_root
, path
);
6398 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
6399 struct btrfs_extent_ref
);
6400 ref_objectid
= btrfs_ref_objectid(leaf
, ref
);
6401 if (ref_objectid
< BTRFS_FIRST_FREE_OBJECTID
) {
6403 level
= (int)ref_objectid
;
6404 BUG_ON(level
>= BTRFS_MAX_LEVEL
);
6405 ref_path
->lowest_level
= level
;
6406 ref_path
->current_level
= level
;
6407 ref_path
->nodes
[level
] = bytenr
;
6409 WARN_ON(ref_objectid
!= level
);
6412 WARN_ON(level
!= -1);
6416 if (ref_path
->lowest_level
== level
) {
6417 ref_path
->owner_objectid
= ref_objectid
;
6418 ref_path
->num_refs
= btrfs_ref_num_refs(leaf
, ref
);
6422 * the block is tree root or the block isn't in reference
6425 if (found_key
.objectid
== found_key
.offset
||
6426 is_cowonly_root(btrfs_ref_root(leaf
, ref
))) {
6427 ref_path
->root_objectid
= btrfs_ref_root(leaf
, ref
);
6428 ref_path
->root_generation
=
6429 btrfs_ref_generation(leaf
, ref
);
6431 /* special reference from the tree log */
6432 ref_path
->nodes
[0] = found_key
.offset
;
6433 ref_path
->current_level
= 0;
6440 BUG_ON(ref_path
->nodes
[level
] != 0);
6441 ref_path
->nodes
[level
] = found_key
.offset
;
6442 ref_path
->current_level
= level
;
6445 * the reference was created in the running transaction,
6446 * no need to continue walking up.
6448 if (btrfs_ref_generation(leaf
, ref
) == trans
->transid
) {
6449 ref_path
->root_objectid
= btrfs_ref_root(leaf
, ref
);
6450 ref_path
->root_generation
=
6451 btrfs_ref_generation(leaf
, ref
);
6456 btrfs_release_path(extent_root
, path
);
6459 /* reached max tree level, but no tree root found. */
6462 btrfs_free_path(path
);
6466 static int btrfs_first_ref_path(struct btrfs_trans_handle
*trans
,
6467 struct btrfs_root
*extent_root
,
6468 struct btrfs_ref_path
*ref_path
,
6471 memset(ref_path
, 0, sizeof(*ref_path
));
6472 ref_path
->extent_start
= extent_start
;
6474 return __next_ref_path(trans
, extent_root
, ref_path
, 1);
6477 static int btrfs_next_ref_path(struct btrfs_trans_handle
*trans
,
6478 struct btrfs_root
*extent_root
,
6479 struct btrfs_ref_path
*ref_path
)
6481 return __next_ref_path(trans
, extent_root
, ref_path
, 0);
6484 static noinline
int get_new_locations(struct inode
*reloc_inode
,
6485 struct btrfs_key
*extent_key
,
6486 u64 offset
, int no_fragment
,
6487 struct disk_extent
**extents
,
6490 struct btrfs_root
*root
= BTRFS_I(reloc_inode
)->root
;
6491 struct btrfs_path
*path
;
6492 struct btrfs_file_extent_item
*fi
;
6493 struct extent_buffer
*leaf
;
6494 struct disk_extent
*exts
= *extents
;
6495 struct btrfs_key found_key
;
6500 int max
= *nr_extents
;
6503 WARN_ON(!no_fragment
&& *extents
);
6506 exts
= kmalloc(sizeof(*exts
) * max
, GFP_NOFS
);
6511 path
= btrfs_alloc_path();
6514 cur_pos
= extent_key
->objectid
- offset
;
6515 last_byte
= extent_key
->objectid
+ extent_key
->offset
;
6516 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, reloc_inode
->i_ino
,
6526 leaf
= path
->nodes
[0];
6527 nritems
= btrfs_header_nritems(leaf
);
6528 if (path
->slots
[0] >= nritems
) {
6529 ret
= btrfs_next_leaf(root
, path
);
6534 leaf
= path
->nodes
[0];
6537 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6538 if (found_key
.offset
!= cur_pos
||
6539 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
||
6540 found_key
.objectid
!= reloc_inode
->i_ino
)
6543 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
6544 struct btrfs_file_extent_item
);
6545 if (btrfs_file_extent_type(leaf
, fi
) !=
6546 BTRFS_FILE_EXTENT_REG
||
6547 btrfs_file_extent_disk_bytenr(leaf
, fi
) == 0)
6551 struct disk_extent
*old
= exts
;
6553 exts
= kzalloc(sizeof(*exts
) * max
, GFP_NOFS
);
6554 memcpy(exts
, old
, sizeof(*exts
) * nr
);
6555 if (old
!= *extents
)
6559 exts
[nr
].disk_bytenr
=
6560 btrfs_file_extent_disk_bytenr(leaf
, fi
);
6561 exts
[nr
].disk_num_bytes
=
6562 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
6563 exts
[nr
].offset
= btrfs_file_extent_offset(leaf
, fi
);
6564 exts
[nr
].num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
6565 exts
[nr
].ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
6566 exts
[nr
].compression
= btrfs_file_extent_compression(leaf
, fi
);
6567 exts
[nr
].encryption
= btrfs_file_extent_encryption(leaf
, fi
);
6568 exts
[nr
].other_encoding
= btrfs_file_extent_other_encoding(leaf
,
6570 BUG_ON(exts
[nr
].offset
> 0);
6571 BUG_ON(exts
[nr
].compression
|| exts
[nr
].encryption
);
6572 BUG_ON(exts
[nr
].num_bytes
!= exts
[nr
].disk_num_bytes
);
6574 cur_pos
+= exts
[nr
].num_bytes
;
6577 if (cur_pos
+ offset
>= last_byte
)
6587 BUG_ON(cur_pos
+ offset
> last_byte
);
6588 if (cur_pos
+ offset
< last_byte
) {
6594 btrfs_free_path(path
);
6596 if (exts
!= *extents
)
6605 static noinline
int replace_one_extent(struct btrfs_trans_handle
*trans
,
6606 struct btrfs_root
*root
,
6607 struct btrfs_path
*path
,
6608 struct btrfs_key
*extent_key
,
6609 struct btrfs_key
*leaf_key
,
6610 struct btrfs_ref_path
*ref_path
,
6611 struct disk_extent
*new_extents
,
6614 struct extent_buffer
*leaf
;
6615 struct btrfs_file_extent_item
*fi
;
6616 struct inode
*inode
= NULL
;
6617 struct btrfs_key key
;
6622 u64 search_end
= (u64
)-1;
6625 int extent_locked
= 0;
6629 memcpy(&key
, leaf_key
, sizeof(key
));
6630 if (ref_path
->owner_objectid
!= BTRFS_MULTIPLE_OBJECTIDS
) {
6631 if (key
.objectid
< ref_path
->owner_objectid
||
6632 (key
.objectid
== ref_path
->owner_objectid
&&
6633 key
.type
< BTRFS_EXTENT_DATA_KEY
)) {
6634 key
.objectid
= ref_path
->owner_objectid
;
6635 key
.type
= BTRFS_EXTENT_DATA_KEY
;
6641 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
6645 leaf
= path
->nodes
[0];
6646 nritems
= btrfs_header_nritems(leaf
);
6648 if (extent_locked
&& ret
> 0) {
6650 * the file extent item was modified by someone
6651 * before the extent got locked.
6653 unlock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
6654 lock_end
, GFP_NOFS
);
6658 if (path
->slots
[0] >= nritems
) {
6659 if (++nr_scaned
> 2)
6662 BUG_ON(extent_locked
);
6663 ret
= btrfs_next_leaf(root
, path
);
6668 leaf
= path
->nodes
[0];
6669 nritems
= btrfs_header_nritems(leaf
);
6672 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
6674 if (ref_path
->owner_objectid
!= BTRFS_MULTIPLE_OBJECTIDS
) {
6675 if ((key
.objectid
> ref_path
->owner_objectid
) ||
6676 (key
.objectid
== ref_path
->owner_objectid
&&
6677 key
.type
> BTRFS_EXTENT_DATA_KEY
) ||
6678 key
.offset
>= search_end
)
6682 if (inode
&& key
.objectid
!= inode
->i_ino
) {
6683 BUG_ON(extent_locked
);
6684 btrfs_release_path(root
, path
);
6685 mutex_unlock(&inode
->i_mutex
);
6691 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
6696 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
6697 struct btrfs_file_extent_item
);
6698 extent_type
= btrfs_file_extent_type(leaf
, fi
);
6699 if ((extent_type
!= BTRFS_FILE_EXTENT_REG
&&
6700 extent_type
!= BTRFS_FILE_EXTENT_PREALLOC
) ||
6701 (btrfs_file_extent_disk_bytenr(leaf
, fi
) !=
6702 extent_key
->objectid
)) {
6708 num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
6709 ext_offset
= btrfs_file_extent_offset(leaf
, fi
);
6711 if (search_end
== (u64
)-1) {
6712 search_end
= key
.offset
- ext_offset
+
6713 btrfs_file_extent_ram_bytes(leaf
, fi
);
6716 if (!extent_locked
) {
6717 lock_start
= key
.offset
;
6718 lock_end
= lock_start
+ num_bytes
- 1;
6720 if (lock_start
> key
.offset
||
6721 lock_end
+ 1 < key
.offset
+ num_bytes
) {
6722 unlock_extent(&BTRFS_I(inode
)->io_tree
,
6723 lock_start
, lock_end
, GFP_NOFS
);
6729 btrfs_release_path(root
, path
);
6731 inode
= btrfs_iget_locked(root
->fs_info
->sb
,
6732 key
.objectid
, root
);
6733 if (inode
->i_state
& I_NEW
) {
6734 BTRFS_I(inode
)->root
= root
;
6735 BTRFS_I(inode
)->location
.objectid
=
6737 BTRFS_I(inode
)->location
.type
=
6738 BTRFS_INODE_ITEM_KEY
;
6739 BTRFS_I(inode
)->location
.offset
= 0;
6740 btrfs_read_locked_inode(inode
);
6741 unlock_new_inode(inode
);
6744 * some code call btrfs_commit_transaction while
6745 * holding the i_mutex, so we can't use mutex_lock
6748 if (is_bad_inode(inode
) ||
6749 !mutex_trylock(&inode
->i_mutex
)) {
6752 key
.offset
= (u64
)-1;
6757 if (!extent_locked
) {
6758 struct btrfs_ordered_extent
*ordered
;
6760 btrfs_release_path(root
, path
);
6762 lock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
6763 lock_end
, GFP_NOFS
);
6764 ordered
= btrfs_lookup_first_ordered_extent(inode
,
6767 ordered
->file_offset
<= lock_end
&&
6768 ordered
->file_offset
+ ordered
->len
> lock_start
) {
6769 unlock_extent(&BTRFS_I(inode
)->io_tree
,
6770 lock_start
, lock_end
, GFP_NOFS
);
6771 btrfs_start_ordered_extent(inode
, ordered
, 1);
6772 btrfs_put_ordered_extent(ordered
);
6773 key
.offset
+= num_bytes
;
6777 btrfs_put_ordered_extent(ordered
);
6783 if (nr_extents
== 1) {
6784 /* update extent pointer in place */
6785 btrfs_set_file_extent_disk_bytenr(leaf
, fi
,
6786 new_extents
[0].disk_bytenr
);
6787 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
,
6788 new_extents
[0].disk_num_bytes
);
6789 btrfs_mark_buffer_dirty(leaf
);
6791 btrfs_drop_extent_cache(inode
, key
.offset
,
6792 key
.offset
+ num_bytes
- 1, 0);
6794 ret
= btrfs_inc_extent_ref(trans
, root
,
6795 new_extents
[0].disk_bytenr
,
6796 new_extents
[0].disk_num_bytes
,
6798 root
->root_key
.objectid
,
6803 ret
= btrfs_free_extent(trans
, root
,
6804 extent_key
->objectid
,
6807 btrfs_header_owner(leaf
),
6808 btrfs_header_generation(leaf
),
6812 btrfs_release_path(root
, path
);
6813 key
.offset
+= num_bytes
;
6821 * drop old extent pointer at first, then insert the
6822 * new pointers one bye one
6824 btrfs_release_path(root
, path
);
6825 ret
= btrfs_drop_extents(trans
, root
, inode
, key
.offset
,
6826 key
.offset
+ num_bytes
,
6827 key
.offset
, &alloc_hint
);
6830 for (i
= 0; i
< nr_extents
; i
++) {
6831 if (ext_offset
>= new_extents
[i
].num_bytes
) {
6832 ext_offset
-= new_extents
[i
].num_bytes
;
6835 extent_len
= min(new_extents
[i
].num_bytes
-
6836 ext_offset
, num_bytes
);
6838 ret
= btrfs_insert_empty_item(trans
, root
,
6843 leaf
= path
->nodes
[0];
6844 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
6845 struct btrfs_file_extent_item
);
6846 btrfs_set_file_extent_generation(leaf
, fi
,
6848 btrfs_set_file_extent_type(leaf
, fi
,
6849 BTRFS_FILE_EXTENT_REG
);
6850 btrfs_set_file_extent_disk_bytenr(leaf
, fi
,
6851 new_extents
[i
].disk_bytenr
);
6852 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
,
6853 new_extents
[i
].disk_num_bytes
);
6854 btrfs_set_file_extent_ram_bytes(leaf
, fi
,
6855 new_extents
[i
].ram_bytes
);
6857 btrfs_set_file_extent_compression(leaf
, fi
,
6858 new_extents
[i
].compression
);
6859 btrfs_set_file_extent_encryption(leaf
, fi
,
6860 new_extents
[i
].encryption
);
6861 btrfs_set_file_extent_other_encoding(leaf
, fi
,
6862 new_extents
[i
].other_encoding
);
6864 btrfs_set_file_extent_num_bytes(leaf
, fi
,
6866 ext_offset
+= new_extents
[i
].offset
;
6867 btrfs_set_file_extent_offset(leaf
, fi
,
6869 btrfs_mark_buffer_dirty(leaf
);
6871 btrfs_drop_extent_cache(inode
, key
.offset
,
6872 key
.offset
+ extent_len
- 1, 0);
6874 ret
= btrfs_inc_extent_ref(trans
, root
,
6875 new_extents
[i
].disk_bytenr
,
6876 new_extents
[i
].disk_num_bytes
,
6878 root
->root_key
.objectid
,
6879 trans
->transid
, key
.objectid
);
6881 btrfs_release_path(root
, path
);
6883 inode_add_bytes(inode
, extent_len
);
6886 num_bytes
-= extent_len
;
6887 key
.offset
+= extent_len
;
6892 BUG_ON(i
>= nr_extents
);
6896 if (extent_locked
) {
6897 unlock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
6898 lock_end
, GFP_NOFS
);
6902 if (ref_path
->owner_objectid
!= BTRFS_MULTIPLE_OBJECTIDS
&&
6903 key
.offset
>= search_end
)
6910 btrfs_release_path(root
, path
);
6912 mutex_unlock(&inode
->i_mutex
);
6913 if (extent_locked
) {
6914 unlock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
6915 lock_end
, GFP_NOFS
);
6922 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle
*trans
,
6923 struct btrfs_root
*root
,
6924 struct extent_buffer
*buf
, u64 orig_start
)
6929 BUG_ON(btrfs_header_generation(buf
) != trans
->transid
);
6930 BUG_ON(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
6932 level
= btrfs_header_level(buf
);
6934 struct btrfs_leaf_ref
*ref
;
6935 struct btrfs_leaf_ref
*orig_ref
;
6937 orig_ref
= btrfs_lookup_leaf_ref(root
, orig_start
);
6941 ref
= btrfs_alloc_leaf_ref(root
, orig_ref
->nritems
);
6943 btrfs_free_leaf_ref(root
, orig_ref
);
6947 ref
->nritems
= orig_ref
->nritems
;
6948 memcpy(ref
->extents
, orig_ref
->extents
,
6949 sizeof(ref
->extents
[0]) * ref
->nritems
);
6951 btrfs_free_leaf_ref(root
, orig_ref
);
6953 ref
->root_gen
= trans
->transid
;
6954 ref
->bytenr
= buf
->start
;
6955 ref
->owner
= btrfs_header_owner(buf
);
6956 ref
->generation
= btrfs_header_generation(buf
);
6958 ret
= btrfs_add_leaf_ref(root
, ref
, 0);
6960 btrfs_free_leaf_ref(root
, ref
);
6965 static noinline
int invalidate_extent_cache(struct btrfs_root
*root
,
6966 struct extent_buffer
*leaf
,
6967 struct btrfs_block_group_cache
*group
,
6968 struct btrfs_root
*target_root
)
6970 struct btrfs_key key
;
6971 struct inode
*inode
= NULL
;
6972 struct btrfs_file_extent_item
*fi
;
6973 struct extent_state
*cached_state
= NULL
;
6975 u64 skip_objectid
= 0;
6979 nritems
= btrfs_header_nritems(leaf
);
6980 for (i
= 0; i
< nritems
; i
++) {
6981 btrfs_item_key_to_cpu(leaf
, &key
, i
);
6982 if (key
.objectid
== skip_objectid
||
6983 key
.type
!= BTRFS_EXTENT_DATA_KEY
)
6985 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
6986 if (btrfs_file_extent_type(leaf
, fi
) ==
6987 BTRFS_FILE_EXTENT_INLINE
)
6989 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) == 0)
6991 if (!inode
|| inode
->i_ino
!= key
.objectid
) {
6993 inode
= btrfs_ilookup(target_root
->fs_info
->sb
,
6994 key
.objectid
, target_root
, 1);
6997 skip_objectid
= key
.objectid
;
7000 num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
7002 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, key
.offset
,
7003 key
.offset
+ num_bytes
- 1, 0, &cached_state
,
7005 btrfs_drop_extent_cache(inode
, key
.offset
,
7006 key
.offset
+ num_bytes
- 1, 1);
7007 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, key
.offset
,
7008 key
.offset
+ num_bytes
- 1, &cached_state
,
7016 static noinline
int replace_extents_in_leaf(struct btrfs_trans_handle
*trans
,
7017 struct btrfs_root
*root
,
7018 struct extent_buffer
*leaf
,
7019 struct btrfs_block_group_cache
*group
,
7020 struct inode
*reloc_inode
)
7022 struct btrfs_key key
;
7023 struct btrfs_key extent_key
;
7024 struct btrfs_file_extent_item
*fi
;
7025 struct btrfs_leaf_ref
*ref
;
7026 struct disk_extent
*new_extent
;
7035 new_extent
= kmalloc(sizeof(*new_extent
), GFP_NOFS
);
7036 BUG_ON(!new_extent
);
7038 ref
= btrfs_lookup_leaf_ref(root
, leaf
->start
);
7042 nritems
= btrfs_header_nritems(leaf
);
7043 for (i
= 0; i
< nritems
; i
++) {
7044 btrfs_item_key_to_cpu(leaf
, &key
, i
);
7045 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
7047 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
7048 if (btrfs_file_extent_type(leaf
, fi
) ==
7049 BTRFS_FILE_EXTENT_INLINE
)
7051 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
7052 num_bytes
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
7057 if (bytenr
>= group
->key
.objectid
+ group
->key
.offset
||
7058 bytenr
+ num_bytes
<= group
->key
.objectid
)
7061 extent_key
.objectid
= bytenr
;
7062 extent_key
.offset
= num_bytes
;
7063 extent_key
.type
= BTRFS_EXTENT_ITEM_KEY
;
7065 ret
= get_new_locations(reloc_inode
, &extent_key
,
7066 group
->key
.objectid
, 1,
7067 &new_extent
, &nr_extent
);
7072 BUG_ON(ref
->extents
[ext_index
].bytenr
!= bytenr
);
7073 BUG_ON(ref
->extents
[ext_index
].num_bytes
!= num_bytes
);
7074 ref
->extents
[ext_index
].bytenr
= new_extent
->disk_bytenr
;
7075 ref
->extents
[ext_index
].num_bytes
= new_extent
->disk_num_bytes
;
7077 btrfs_set_file_extent_disk_bytenr(leaf
, fi
,
7078 new_extent
->disk_bytenr
);
7079 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
,
7080 new_extent
->disk_num_bytes
);
7081 btrfs_mark_buffer_dirty(leaf
);
7083 ret
= btrfs_inc_extent_ref(trans
, root
,
7084 new_extent
->disk_bytenr
,
7085 new_extent
->disk_num_bytes
,
7087 root
->root_key
.objectid
,
7088 trans
->transid
, key
.objectid
);
7091 ret
= btrfs_free_extent(trans
, root
,
7092 bytenr
, num_bytes
, leaf
->start
,
7093 btrfs_header_owner(leaf
),
7094 btrfs_header_generation(leaf
),
7100 BUG_ON(ext_index
+ 1 != ref
->nritems
);
7101 btrfs_free_leaf_ref(root
, ref
);
7105 int btrfs_free_reloc_root(struct btrfs_trans_handle
*trans
,
7106 struct btrfs_root
*root
)
7108 struct btrfs_root
*reloc_root
;
7111 if (root
->reloc_root
) {
7112 reloc_root
= root
->reloc_root
;
7113 root
->reloc_root
= NULL
;
7114 list_add(&reloc_root
->dead_list
,
7115 &root
->fs_info
->dead_reloc_roots
);
7117 btrfs_set_root_bytenr(&reloc_root
->root_item
,
7118 reloc_root
->node
->start
);
7119 btrfs_set_root_level(&root
->root_item
,
7120 btrfs_header_level(reloc_root
->node
));
7121 memset(&reloc_root
->root_item
.drop_progress
, 0,
7122 sizeof(struct btrfs_disk_key
));
7123 reloc_root
->root_item
.drop_level
= 0;
7125 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
7126 &reloc_root
->root_key
,
7127 &reloc_root
->root_item
);
7133 int btrfs_drop_dead_reloc_roots(struct btrfs_root
*root
)
7135 struct btrfs_trans_handle
*trans
;
7136 struct btrfs_root
*reloc_root
;
7137 struct btrfs_root
*prev_root
= NULL
;
7138 struct list_head dead_roots
;
7142 INIT_LIST_HEAD(&dead_roots
);
7143 list_splice_init(&root
->fs_info
->dead_reloc_roots
, &dead_roots
);
7145 while (!list_empty(&dead_roots
)) {
7146 reloc_root
= list_entry(dead_roots
.prev
,
7147 struct btrfs_root
, dead_list
);
7148 list_del_init(&reloc_root
->dead_list
);
7150 BUG_ON(reloc_root
->commit_root
!= NULL
);
7152 trans
= btrfs_join_transaction(root
, 1);
7155 mutex_lock(&root
->fs_info
->drop_mutex
);
7156 ret
= btrfs_drop_snapshot(trans
, reloc_root
);
7159 mutex_unlock(&root
->fs_info
->drop_mutex
);
7161 nr
= trans
->blocks_used
;
7162 ret
= btrfs_end_transaction(trans
, root
);
7164 btrfs_btree_balance_dirty(root
, nr
);
7167 free_extent_buffer(reloc_root
->node
);
7169 ret
= btrfs_del_root(trans
, root
->fs_info
->tree_root
,
7170 &reloc_root
->root_key
);
7172 mutex_unlock(&root
->fs_info
->drop_mutex
);
7174 nr
= trans
->blocks_used
;
7175 ret
= btrfs_end_transaction(trans
, root
);
7177 btrfs_btree_balance_dirty(root
, nr
);
7180 prev_root
= reloc_root
;
7183 btrfs_remove_leaf_refs(prev_root
, (u64
)-1, 0);
7189 int btrfs_add_dead_reloc_root(struct btrfs_root
*root
)
7191 list_add(&root
->dead_list
, &root
->fs_info
->dead_reloc_roots
);
7195 int btrfs_cleanup_reloc_trees(struct btrfs_root
*root
)
7197 struct btrfs_root
*reloc_root
;
7198 struct btrfs_trans_handle
*trans
;
7199 struct btrfs_key location
;
7203 mutex_lock(&root
->fs_info
->tree_reloc_mutex
);
7204 ret
= btrfs_find_dead_roots(root
, BTRFS_TREE_RELOC_OBJECTID
, NULL
);
7206 found
= !list_empty(&root
->fs_info
->dead_reloc_roots
);
7207 mutex_unlock(&root
->fs_info
->tree_reloc_mutex
);
7210 trans
= btrfs_start_transaction(root
, 1);
7212 ret
= btrfs_commit_transaction(trans
, root
);
7216 location
.objectid
= BTRFS_DATA_RELOC_TREE_OBJECTID
;
7217 location
.offset
= (u64
)-1;
7218 location
.type
= BTRFS_ROOT_ITEM_KEY
;
7220 reloc_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
7221 BUG_ON(!reloc_root
);
7222 btrfs_orphan_cleanup(reloc_root
);
7226 static noinline
int init_reloc_tree(struct btrfs_trans_handle
*trans
,
7227 struct btrfs_root
*root
)
7229 struct btrfs_root
*reloc_root
;
7230 struct extent_buffer
*eb
;
7231 struct btrfs_root_item
*root_item
;
7232 struct btrfs_key root_key
;
7235 BUG_ON(!root
->ref_cows
);
7236 if (root
->reloc_root
)
7239 root_item
= kmalloc(sizeof(*root_item
), GFP_NOFS
);
7242 ret
= btrfs_copy_root(trans
, root
, root
->commit_root
,
7243 &eb
, BTRFS_TREE_RELOC_OBJECTID
);
7246 root_key
.objectid
= BTRFS_TREE_RELOC_OBJECTID
;
7247 root_key
.offset
= root
->root_key
.objectid
;
7248 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
7250 memcpy(root_item
, &root
->root_item
, sizeof(root_item
));
7251 btrfs_set_root_refs(root_item
, 0);
7252 btrfs_set_root_bytenr(root_item
, eb
->start
);
7253 btrfs_set_root_level(root_item
, btrfs_header_level(eb
));
7254 btrfs_set_root_generation(root_item
, trans
->transid
);
7256 btrfs_tree_unlock(eb
);
7257 free_extent_buffer(eb
);
7259 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
,
7260 &root_key
, root_item
);
7264 reloc_root
= btrfs_read_fs_root_no_radix(root
->fs_info
->tree_root
,
7266 BUG_ON(!reloc_root
);
7267 reloc_root
->last_trans
= trans
->transid
;
7268 reloc_root
->commit_root
= NULL
;
7269 reloc_root
->ref_tree
= &root
->fs_info
->reloc_ref_tree
;
7271 root
->reloc_root
= reloc_root
;
7276 * Core function of space balance.
7278 * The idea is using reloc trees to relocate tree blocks in reference
7279 * counted roots. There is one reloc tree for each subvol, and all
7280 * reloc trees share same root key objectid. Reloc trees are snapshots
7281 * of the latest committed roots of subvols (root->commit_root).
7283 * To relocate a tree block referenced by a subvol, there are two steps.
7284 * COW the block through subvol's reloc tree, then update block pointer
7285 * in the subvol to point to the new block. Since all reloc trees share
7286 * same root key objectid, doing special handing for tree blocks owned
7287 * by them is easy. Once a tree block has been COWed in one reloc tree,
7288 * we can use the resulting new block directly when the same block is
7289 * required to COW again through other reloc trees. By this way, relocated
7290 * tree blocks are shared between reloc trees, so they are also shared
7293 static noinline
int relocate_one_path(struct btrfs_trans_handle
*trans
,
7294 struct btrfs_root
*root
,
7295 struct btrfs_path
*path
,
7296 struct btrfs_key
*first_key
,
7297 struct btrfs_ref_path
*ref_path
,
7298 struct btrfs_block_group_cache
*group
,
7299 struct inode
*reloc_inode
)
7301 struct btrfs_root
*reloc_root
;
7302 struct extent_buffer
*eb
= NULL
;
7303 struct btrfs_key
*keys
;
7307 int lowest_level
= 0;
7310 if (ref_path
->owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
)
7311 lowest_level
= ref_path
->owner_objectid
;
7313 if (!root
->ref_cows
) {
7314 path
->lowest_level
= lowest_level
;
7315 ret
= btrfs_search_slot(trans
, root
, first_key
, path
, 0, 1);
7317 path
->lowest_level
= 0;
7318 btrfs_release_path(root
, path
);
7322 mutex_lock(&root
->fs_info
->tree_reloc_mutex
);
7323 ret
= init_reloc_tree(trans
, root
);
7325 reloc_root
= root
->reloc_root
;
7327 shared_level
= ref_path
->shared_level
;
7328 ref_path
->shared_level
= BTRFS_MAX_LEVEL
- 1;
7330 keys
= ref_path
->node_keys
;
7331 nodes
= ref_path
->new_nodes
;
7332 memset(&keys
[shared_level
+ 1], 0,
7333 sizeof(*keys
) * (BTRFS_MAX_LEVEL
- shared_level
- 1));
7334 memset(&nodes
[shared_level
+ 1], 0,
7335 sizeof(*nodes
) * (BTRFS_MAX_LEVEL
- shared_level
- 1));
7337 if (nodes
[lowest_level
] == 0) {
7338 path
->lowest_level
= lowest_level
;
7339 ret
= btrfs_search_slot(trans
, reloc_root
, first_key
, path
,
7342 for (level
= lowest_level
; level
< BTRFS_MAX_LEVEL
; level
++) {
7343 eb
= path
->nodes
[level
];
7344 if (!eb
|| eb
== reloc_root
->node
)
7346 nodes
[level
] = eb
->start
;
7348 btrfs_item_key_to_cpu(eb
, &keys
[level
], 0);
7350 btrfs_node_key_to_cpu(eb
, &keys
[level
], 0);
7353 ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
7354 eb
= path
->nodes
[0];
7355 ret
= replace_extents_in_leaf(trans
, reloc_root
, eb
,
7356 group
, reloc_inode
);
7359 btrfs_release_path(reloc_root
, path
);
7361 ret
= btrfs_merge_path(trans
, reloc_root
, keys
, nodes
,
7367 * replace tree blocks in the fs tree with tree blocks in
7370 ret
= btrfs_merge_path(trans
, root
, keys
, nodes
, lowest_level
);
7373 if (ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
7374 ret
= btrfs_search_slot(trans
, reloc_root
, first_key
, path
,
7377 extent_buffer_get(path
->nodes
[0]);
7378 eb
= path
->nodes
[0];
7379 btrfs_release_path(reloc_root
, path
);
7380 ret
= invalidate_extent_cache(reloc_root
, eb
, group
, root
);
7382 free_extent_buffer(eb
);
7385 mutex_unlock(&root
->fs_info
->tree_reloc_mutex
);
7386 path
->lowest_level
= 0;
7390 static noinline
int relocate_tree_block(struct btrfs_trans_handle
*trans
,
7391 struct btrfs_root
*root
,
7392 struct btrfs_path
*path
,
7393 struct btrfs_key
*first_key
,
7394 struct btrfs_ref_path
*ref_path
)
7398 ret
= relocate_one_path(trans
, root
, path
, first_key
,
7399 ref_path
, NULL
, NULL
);
7405 static noinline
int del_extent_zero(struct btrfs_trans_handle
*trans
,
7406 struct btrfs_root
*extent_root
,
7407 struct btrfs_path
*path
,
7408 struct btrfs_key
*extent_key
)
7412 ret
= btrfs_search_slot(trans
, extent_root
, extent_key
, path
, -1, 1);
7415 ret
= btrfs_del_item(trans
, extent_root
, path
);
7417 btrfs_release_path(extent_root
, path
);
7421 static noinline
struct btrfs_root
*read_ref_root(struct btrfs_fs_info
*fs_info
,
7422 struct btrfs_ref_path
*ref_path
)
7424 struct btrfs_key root_key
;
7426 root_key
.objectid
= ref_path
->root_objectid
;
7427 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
7428 if (is_cowonly_root(ref_path
->root_objectid
))
7429 root_key
.offset
= 0;
7431 root_key
.offset
= (u64
)-1;
7433 return btrfs_read_fs_root_no_name(fs_info
, &root_key
);
7436 static noinline
int relocate_one_extent(struct btrfs_root
*extent_root
,
7437 struct btrfs_path
*path
,
7438 struct btrfs_key
*extent_key
,
7439 struct btrfs_block_group_cache
*group
,
7440 struct inode
*reloc_inode
, int pass
)
7442 struct btrfs_trans_handle
*trans
;
7443 struct btrfs_root
*found_root
;
7444 struct btrfs_ref_path
*ref_path
= NULL
;
7445 struct disk_extent
*new_extents
= NULL
;
7450 struct btrfs_key first_key
;
7454 trans
= btrfs_start_transaction(extent_root
, 1);
7457 if (extent_key
->objectid
== 0) {
7458 ret
= del_extent_zero(trans
, extent_root
, path
, extent_key
);
7462 ref_path
= kmalloc(sizeof(*ref_path
), GFP_NOFS
);
7468 for (loops
= 0; ; loops
++) {
7470 ret
= btrfs_first_ref_path(trans
, extent_root
, ref_path
,
7471 extent_key
->objectid
);
7473 ret
= btrfs_next_ref_path(trans
, extent_root
, ref_path
);
7480 if (ref_path
->root_objectid
== BTRFS_TREE_LOG_OBJECTID
||
7481 ref_path
->root_objectid
== BTRFS_TREE_RELOC_OBJECTID
)
7484 found_root
= read_ref_root(extent_root
->fs_info
, ref_path
);
7485 BUG_ON(!found_root
);
7487 * for reference counted tree, only process reference paths
7488 * rooted at the latest committed root.
7490 if (found_root
->ref_cows
&&
7491 ref_path
->root_generation
!= found_root
->root_key
.offset
)
7494 if (ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
7497 * copy data extents to new locations
7499 u64 group_start
= group
->key
.objectid
;
7500 ret
= relocate_data_extent(reloc_inode
,
7509 level
= ref_path
->owner_objectid
;
7512 if (prev_block
!= ref_path
->nodes
[level
]) {
7513 struct extent_buffer
*eb
;
7514 u64 block_start
= ref_path
->nodes
[level
];
7515 u64 block_size
= btrfs_level_size(found_root
, level
);
7517 eb
= read_tree_block(found_root
, block_start
,
7519 btrfs_tree_lock(eb
);
7520 BUG_ON(level
!= btrfs_header_level(eb
));
7523 btrfs_item_key_to_cpu(eb
, &first_key
, 0);
7525 btrfs_node_key_to_cpu(eb
, &first_key
, 0);
7527 btrfs_tree_unlock(eb
);
7528 free_extent_buffer(eb
);
7529 prev_block
= block_start
;
7532 mutex_lock(&extent_root
->fs_info
->trans_mutex
);
7533 btrfs_record_root_in_trans(found_root
);
7534 mutex_unlock(&extent_root
->fs_info
->trans_mutex
);
7535 if (ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
7537 * try to update data extent references while
7538 * keeping metadata shared between snapshots.
7541 ret
= relocate_one_path(trans
, found_root
,
7542 path
, &first_key
, ref_path
,
7543 group
, reloc_inode
);
7549 * use fallback method to process the remaining
7553 u64 group_start
= group
->key
.objectid
;
7554 new_extents
= kmalloc(sizeof(*new_extents
),
7557 ret
= get_new_locations(reloc_inode
,
7565 ret
= replace_one_extent(trans
, found_root
,
7567 &first_key
, ref_path
,
7568 new_extents
, nr_extents
);
7570 ret
= relocate_tree_block(trans
, found_root
, path
,
7571 &first_key
, ref_path
);
7578 btrfs_end_transaction(trans
, extent_root
);
7585 static u64
update_block_group_flags(struct btrfs_root
*root
, u64 flags
)
7588 u64 stripped
= BTRFS_BLOCK_GROUP_RAID0
|
7589 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
;
7591 num_devices
= root
->fs_info
->fs_devices
->rw_devices
;
7592 if (num_devices
== 1) {
7593 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
7594 stripped
= flags
& ~stripped
;
7596 /* turn raid0 into single device chunks */
7597 if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
7600 /* turn mirroring into duplication */
7601 if (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
7602 BTRFS_BLOCK_GROUP_RAID10
))
7603 return stripped
| BTRFS_BLOCK_GROUP_DUP
;
7606 /* they already had raid on here, just return */
7607 if (flags
& stripped
)
7610 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
7611 stripped
= flags
& ~stripped
;
7613 /* switch duplicated blocks with raid1 */
7614 if (flags
& BTRFS_BLOCK_GROUP_DUP
)
7615 return stripped
| BTRFS_BLOCK_GROUP_RAID1
;
7617 /* turn single device chunks into raid0 */
7618 return stripped
| BTRFS_BLOCK_GROUP_RAID0
;
7623 static int set_block_group_ro(struct btrfs_block_group_cache
*cache
)
7625 struct btrfs_space_info
*sinfo
= cache
->space_info
;
7632 spin_lock(&sinfo
->lock
);
7633 spin_lock(&cache
->lock
);
7634 num_bytes
= cache
->key
.offset
- cache
->reserved
- cache
->pinned
-
7635 cache
->bytes_super
- btrfs_block_group_used(&cache
->item
);
7637 if (sinfo
->bytes_used
+ sinfo
->bytes_reserved
+ sinfo
->bytes_pinned
+
7638 sinfo
->bytes_may_use
+ sinfo
->bytes_readonly
+
7639 cache
->reserved_pinned
+ num_bytes
< sinfo
->total_bytes
) {
7640 sinfo
->bytes_readonly
+= num_bytes
;
7641 sinfo
->bytes_reserved
+= cache
->reserved_pinned
;
7642 cache
->reserved_pinned
= 0;
7646 spin_unlock(&cache
->lock
);
7647 spin_unlock(&sinfo
->lock
);
7651 int btrfs_set_block_group_ro(struct btrfs_root
*root
,
7652 struct btrfs_block_group_cache
*cache
)
7655 struct btrfs_trans_handle
*trans
;
7661 trans
= btrfs_join_transaction(root
, 1);
7662 BUG_ON(IS_ERR(trans
));
7664 alloc_flags
= update_block_group_flags(root
, cache
->flags
);
7665 if (alloc_flags
!= cache
->flags
)
7666 do_chunk_alloc(trans
, root
, 2 * 1024 * 1024, alloc_flags
, 1);
7668 ret
= set_block_group_ro(cache
);
7671 alloc_flags
= get_alloc_profile(root
, cache
->space_info
->flags
);
7672 ret
= do_chunk_alloc(trans
, root
, 2 * 1024 * 1024, alloc_flags
, 1);
7675 ret
= set_block_group_ro(cache
);
7677 btrfs_end_transaction(trans
, root
);
7681 int btrfs_set_block_group_rw(struct btrfs_root
*root
,
7682 struct btrfs_block_group_cache
*cache
)
7684 struct btrfs_space_info
*sinfo
= cache
->space_info
;
7689 spin_lock(&sinfo
->lock
);
7690 spin_lock(&cache
->lock
);
7691 num_bytes
= cache
->key
.offset
- cache
->reserved
- cache
->pinned
-
7692 cache
->bytes_super
- btrfs_block_group_used(&cache
->item
);
7693 sinfo
->bytes_readonly
-= num_bytes
;
7695 spin_unlock(&cache
->lock
);
7696 spin_unlock(&sinfo
->lock
);
7701 * checks to see if its even possible to relocate this block group.
7703 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7704 * ok to go ahead and try.
7706 int btrfs_can_relocate(struct btrfs_root
*root
, u64 bytenr
)
7708 struct btrfs_block_group_cache
*block_group
;
7709 struct btrfs_space_info
*space_info
;
7710 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
7711 struct btrfs_device
*device
;
7715 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
7717 /* odd, couldn't find the block group, leave it alone */
7721 /* no bytes used, we're good */
7722 if (!btrfs_block_group_used(&block_group
->item
))
7725 space_info
= block_group
->space_info
;
7726 spin_lock(&space_info
->lock
);
7728 full
= space_info
->full
;
7731 * if this is the last block group we have in this space, we can't
7732 * relocate it unless we're able to allocate a new chunk below.
7734 * Otherwise, we need to make sure we have room in the space to handle
7735 * all of the extents from this block group. If we can, we're good
7737 if ((space_info
->total_bytes
!= block_group
->key
.offset
) &&
7738 (space_info
->bytes_used
+ space_info
->bytes_reserved
+
7739 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
7740 btrfs_block_group_used(&block_group
->item
) <
7741 space_info
->total_bytes
)) {
7742 spin_unlock(&space_info
->lock
);
7745 spin_unlock(&space_info
->lock
);
7748 * ok we don't have enough space, but maybe we have free space on our
7749 * devices to allocate new chunks for relocation, so loop through our
7750 * alloc devices and guess if we have enough space. However, if we
7751 * were marked as full, then we know there aren't enough chunks, and we
7758 mutex_lock(&root
->fs_info
->chunk_mutex
);
7759 list_for_each_entry(device
, &fs_devices
->alloc_list
, dev_alloc_list
) {
7760 u64 min_free
= btrfs_block_group_used(&block_group
->item
);
7761 u64 dev_offset
, max_avail
;
7764 * check to make sure we can actually find a chunk with enough
7765 * space to fit our block group in.
7767 if (device
->total_bytes
> device
->bytes_used
+ min_free
) {
7768 ret
= find_free_dev_extent(NULL
, device
, min_free
,
7769 &dev_offset
, &max_avail
);
7775 mutex_unlock(&root
->fs_info
->chunk_mutex
);
7777 btrfs_put_block_group(block_group
);
7781 static int find_first_block_group(struct btrfs_root
*root
,
7782 struct btrfs_path
*path
, struct btrfs_key
*key
)
7785 struct btrfs_key found_key
;
7786 struct extent_buffer
*leaf
;
7789 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
7794 slot
= path
->slots
[0];
7795 leaf
= path
->nodes
[0];
7796 if (slot
>= btrfs_header_nritems(leaf
)) {
7797 ret
= btrfs_next_leaf(root
, path
);
7804 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
7806 if (found_key
.objectid
>= key
->objectid
&&
7807 found_key
.type
== BTRFS_BLOCK_GROUP_ITEM_KEY
) {
7817 int btrfs_free_block_groups(struct btrfs_fs_info
*info
)
7819 struct btrfs_block_group_cache
*block_group
;
7820 struct btrfs_space_info
*space_info
;
7821 struct btrfs_caching_control
*caching_ctl
;
7824 down_write(&info
->extent_commit_sem
);
7825 while (!list_empty(&info
->caching_block_groups
)) {
7826 caching_ctl
= list_entry(info
->caching_block_groups
.next
,
7827 struct btrfs_caching_control
, list
);
7828 list_del(&caching_ctl
->list
);
7829 put_caching_control(caching_ctl
);
7831 up_write(&info
->extent_commit_sem
);
7833 spin_lock(&info
->block_group_cache_lock
);
7834 while ((n
= rb_last(&info
->block_group_cache_tree
)) != NULL
) {
7835 block_group
= rb_entry(n
, struct btrfs_block_group_cache
,
7837 rb_erase(&block_group
->cache_node
,
7838 &info
->block_group_cache_tree
);
7839 spin_unlock(&info
->block_group_cache_lock
);
7841 down_write(&block_group
->space_info
->groups_sem
);
7842 list_del(&block_group
->list
);
7843 up_write(&block_group
->space_info
->groups_sem
);
7845 if (block_group
->cached
== BTRFS_CACHE_STARTED
)
7846 wait_block_group_cache_done(block_group
);
7848 btrfs_remove_free_space_cache(block_group
);
7849 btrfs_put_block_group(block_group
);
7851 spin_lock(&info
->block_group_cache_lock
);
7853 spin_unlock(&info
->block_group_cache_lock
);
7855 /* now that all the block groups are freed, go through and
7856 * free all the space_info structs. This is only called during
7857 * the final stages of unmount, and so we know nobody is
7858 * using them. We call synchronize_rcu() once before we start,
7859 * just to be on the safe side.
7863 release_global_block_rsv(info
);
7865 while(!list_empty(&info
->space_info
)) {
7866 space_info
= list_entry(info
->space_info
.next
,
7867 struct btrfs_space_info
,
7869 if (space_info
->bytes_pinned
> 0 ||
7870 space_info
->bytes_reserved
> 0) {
7872 dump_space_info(space_info
, 0, 0);
7874 list_del(&space_info
->list
);
7880 static void __link_block_group(struct btrfs_space_info
*space_info
,
7881 struct btrfs_block_group_cache
*cache
)
7883 int index
= get_block_group_index(cache
);
7885 down_write(&space_info
->groups_sem
);
7886 list_add_tail(&cache
->list
, &space_info
->block_groups
[index
]);
7887 up_write(&space_info
->groups_sem
);
7890 int btrfs_read_block_groups(struct btrfs_root
*root
)
7892 struct btrfs_path
*path
;
7894 struct btrfs_block_group_cache
*cache
;
7895 struct btrfs_fs_info
*info
= root
->fs_info
;
7896 struct btrfs_space_info
*space_info
;
7897 struct btrfs_key key
;
7898 struct btrfs_key found_key
;
7899 struct extent_buffer
*leaf
;
7901 root
= info
->extent_root
;
7904 btrfs_set_key_type(&key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
7905 path
= btrfs_alloc_path();
7910 ret
= find_first_block_group(root
, path
, &key
);
7916 leaf
= path
->nodes
[0];
7917 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
7918 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
7924 atomic_set(&cache
->count
, 1);
7925 spin_lock_init(&cache
->lock
);
7926 spin_lock_init(&cache
->tree_lock
);
7927 cache
->fs_info
= info
;
7928 INIT_LIST_HEAD(&cache
->list
);
7929 INIT_LIST_HEAD(&cache
->cluster_list
);
7932 * we only want to have 32k of ram per block group for keeping
7933 * track of free space, and if we pass 1/2 of that we want to
7934 * start converting things over to using bitmaps
7936 cache
->extents_thresh
= ((1024 * 32) / 2) /
7937 sizeof(struct btrfs_free_space
);
7939 read_extent_buffer(leaf
, &cache
->item
,
7940 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
7941 sizeof(cache
->item
));
7942 memcpy(&cache
->key
, &found_key
, sizeof(found_key
));
7944 key
.objectid
= found_key
.objectid
+ found_key
.offset
;
7945 btrfs_release_path(root
, path
);
7946 cache
->flags
= btrfs_block_group_flags(&cache
->item
);
7947 cache
->sectorsize
= root
->sectorsize
;
7950 * check for two cases, either we are full, and therefore
7951 * don't need to bother with the caching work since we won't
7952 * find any space, or we are empty, and we can just add all
7953 * the space in and be done with it. This saves us _alot_ of
7954 * time, particularly in the full case.
7956 if (found_key
.offset
== btrfs_block_group_used(&cache
->item
)) {
7957 exclude_super_stripes(root
, cache
);
7958 cache
->last_byte_to_unpin
= (u64
)-1;
7959 cache
->cached
= BTRFS_CACHE_FINISHED
;
7960 free_excluded_extents(root
, cache
);
7961 } else if (btrfs_block_group_used(&cache
->item
) == 0) {
7962 exclude_super_stripes(root
, cache
);
7963 cache
->last_byte_to_unpin
= (u64
)-1;
7964 cache
->cached
= BTRFS_CACHE_FINISHED
;
7965 add_new_free_space(cache
, root
->fs_info
,
7967 found_key
.objectid
+
7969 free_excluded_extents(root
, cache
);
7972 ret
= update_space_info(info
, cache
->flags
, found_key
.offset
,
7973 btrfs_block_group_used(&cache
->item
),
7976 cache
->space_info
= space_info
;
7977 spin_lock(&cache
->space_info
->lock
);
7978 cache
->space_info
->bytes_readonly
+= cache
->bytes_super
;
7979 spin_unlock(&cache
->space_info
->lock
);
7981 __link_block_group(space_info
, cache
);
7983 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
7986 set_avail_alloc_bits(root
->fs_info
, cache
->flags
);
7987 if (btrfs_chunk_readonly(root
, cache
->key
.objectid
))
7988 set_block_group_ro(cache
);
7991 list_for_each_entry_rcu(space_info
, &root
->fs_info
->space_info
, list
) {
7992 if (!(get_alloc_profile(root
, space_info
->flags
) &
7993 (BTRFS_BLOCK_GROUP_RAID10
|
7994 BTRFS_BLOCK_GROUP_RAID1
|
7995 BTRFS_BLOCK_GROUP_DUP
)))
7998 * avoid allocating from un-mirrored block group if there are
7999 * mirrored block groups.
8001 list_for_each_entry(cache
, &space_info
->block_groups
[3], list
)
8002 set_block_group_ro(cache
);
8003 list_for_each_entry(cache
, &space_info
->block_groups
[4], list
)
8004 set_block_group_ro(cache
);
8007 init_global_block_rsv(info
);
8010 btrfs_free_path(path
);
8014 int btrfs_make_block_group(struct btrfs_trans_handle
*trans
,
8015 struct btrfs_root
*root
, u64 bytes_used
,
8016 u64 type
, u64 chunk_objectid
, u64 chunk_offset
,
8020 struct btrfs_root
*extent_root
;
8021 struct btrfs_block_group_cache
*cache
;
8023 extent_root
= root
->fs_info
->extent_root
;
8025 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
8027 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
8031 cache
->key
.objectid
= chunk_offset
;
8032 cache
->key
.offset
= size
;
8033 cache
->key
.type
= BTRFS_BLOCK_GROUP_ITEM_KEY
;
8034 cache
->sectorsize
= root
->sectorsize
;
8037 * we only want to have 32k of ram per block group for keeping track
8038 * of free space, and if we pass 1/2 of that we want to start
8039 * converting things over to using bitmaps
8041 cache
->extents_thresh
= ((1024 * 32) / 2) /
8042 sizeof(struct btrfs_free_space
);
8043 atomic_set(&cache
->count
, 1);
8044 spin_lock_init(&cache
->lock
);
8045 spin_lock_init(&cache
->tree_lock
);
8046 INIT_LIST_HEAD(&cache
->list
);
8047 INIT_LIST_HEAD(&cache
->cluster_list
);
8049 btrfs_set_block_group_used(&cache
->item
, bytes_used
);
8050 btrfs_set_block_group_chunk_objectid(&cache
->item
, chunk_objectid
);
8051 cache
->flags
= type
;
8052 btrfs_set_block_group_flags(&cache
->item
, type
);
8054 cache
->last_byte_to_unpin
= (u64
)-1;
8055 cache
->cached
= BTRFS_CACHE_FINISHED
;
8056 exclude_super_stripes(root
, cache
);
8058 add_new_free_space(cache
, root
->fs_info
, chunk_offset
,
8059 chunk_offset
+ size
);
8061 free_excluded_extents(root
, cache
);
8063 ret
= update_space_info(root
->fs_info
, cache
->flags
, size
, bytes_used
,
8064 &cache
->space_info
);
8067 spin_lock(&cache
->space_info
->lock
);
8068 cache
->space_info
->bytes_readonly
+= cache
->bytes_super
;
8069 spin_unlock(&cache
->space_info
->lock
);
8071 __link_block_group(cache
->space_info
, cache
);
8073 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
8076 ret
= btrfs_insert_item(trans
, extent_root
, &cache
->key
, &cache
->item
,
8077 sizeof(cache
->item
));
8080 set_avail_alloc_bits(extent_root
->fs_info
, type
);
8085 int btrfs_remove_block_group(struct btrfs_trans_handle
*trans
,
8086 struct btrfs_root
*root
, u64 group_start
)
8088 struct btrfs_path
*path
;
8089 struct btrfs_block_group_cache
*block_group
;
8090 struct btrfs_free_cluster
*cluster
;
8091 struct btrfs_key key
;
8094 root
= root
->fs_info
->extent_root
;
8096 block_group
= btrfs_lookup_block_group(root
->fs_info
, group_start
);
8097 BUG_ON(!block_group
);
8098 BUG_ON(!block_group
->ro
);
8100 memcpy(&key
, &block_group
->key
, sizeof(key
));
8102 /* make sure this block group isn't part of an allocation cluster */
8103 cluster
= &root
->fs_info
->data_alloc_cluster
;
8104 spin_lock(&cluster
->refill_lock
);
8105 btrfs_return_cluster_to_free_space(block_group
, cluster
);
8106 spin_unlock(&cluster
->refill_lock
);
8109 * make sure this block group isn't part of a metadata
8110 * allocation cluster
8112 cluster
= &root
->fs_info
->meta_alloc_cluster
;
8113 spin_lock(&cluster
->refill_lock
);
8114 btrfs_return_cluster_to_free_space(block_group
, cluster
);
8115 spin_unlock(&cluster
->refill_lock
);
8117 path
= btrfs_alloc_path();
8120 spin_lock(&root
->fs_info
->block_group_cache_lock
);
8121 rb_erase(&block_group
->cache_node
,
8122 &root
->fs_info
->block_group_cache_tree
);
8123 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
8125 down_write(&block_group
->space_info
->groups_sem
);
8127 * we must use list_del_init so people can check to see if they
8128 * are still on the list after taking the semaphore
8130 list_del_init(&block_group
->list
);
8131 up_write(&block_group
->space_info
->groups_sem
);
8133 if (block_group
->cached
== BTRFS_CACHE_STARTED
)
8134 wait_block_group_cache_done(block_group
);
8136 btrfs_remove_free_space_cache(block_group
);
8138 spin_lock(&block_group
->space_info
->lock
);
8139 block_group
->space_info
->total_bytes
-= block_group
->key
.offset
;
8140 block_group
->space_info
->bytes_readonly
-= block_group
->key
.offset
;
8141 spin_unlock(&block_group
->space_info
->lock
);
8143 btrfs_clear_space_info_full(root
->fs_info
);
8145 btrfs_put_block_group(block_group
);
8146 btrfs_put_block_group(block_group
);
8148 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
8154 ret
= btrfs_del_item(trans
, root
, path
);
8156 btrfs_free_path(path
);