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>
26 #include <linux/ratelimit.h>
27 #include <linux/percpu_counter.h>
32 #include "print-tree.h"
33 #include "transaction.h"
37 #include "free-space-cache.h"
40 #undef SCRAMBLE_DELAYED_REFS
43 * control flags for do_chunk_alloc's force field
44 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
45 * if we really need one.
47 * CHUNK_ALLOC_LIMITED means to only try and allocate one
48 * if we have very few chunks already allocated. This is
49 * used as part of the clustering code to help make sure
50 * we have a good pool of storage to cluster in, without
51 * filling the FS with empty chunks
53 * CHUNK_ALLOC_FORCE means it must try to allocate one
57 CHUNK_ALLOC_NO_FORCE
= 0,
58 CHUNK_ALLOC_LIMITED
= 1,
59 CHUNK_ALLOC_FORCE
= 2,
63 * Control how reservations are dealt with.
65 * RESERVE_FREE - freeing a reservation.
66 * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for
68 * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update
69 * bytes_may_use as the ENOSPC accounting is done elsewhere
74 RESERVE_ALLOC_NO_ACCOUNT
= 2,
77 static int update_block_group(struct btrfs_root
*root
,
78 u64 bytenr
, u64 num_bytes
, int alloc
);
79 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
80 struct btrfs_root
*root
,
81 u64 bytenr
, u64 num_bytes
, u64 parent
,
82 u64 root_objectid
, u64 owner_objectid
,
83 u64 owner_offset
, int refs_to_drop
,
84 struct btrfs_delayed_extent_op
*extra_op
);
85 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op
*extent_op
,
86 struct extent_buffer
*leaf
,
87 struct btrfs_extent_item
*ei
);
88 static int alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
89 struct btrfs_root
*root
,
90 u64 parent
, u64 root_objectid
,
91 u64 flags
, u64 owner
, u64 offset
,
92 struct btrfs_key
*ins
, int ref_mod
);
93 static int alloc_reserved_tree_block(struct btrfs_trans_handle
*trans
,
94 struct btrfs_root
*root
,
95 u64 parent
, u64 root_objectid
,
96 u64 flags
, struct btrfs_disk_key
*key
,
97 int level
, struct btrfs_key
*ins
);
98 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
99 struct btrfs_root
*extent_root
, u64 flags
,
101 static int find_next_key(struct btrfs_path
*path
, int level
,
102 struct btrfs_key
*key
);
103 static void dump_space_info(struct btrfs_space_info
*info
, u64 bytes
,
104 int dump_block_groups
);
105 static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache
*cache
,
106 u64 num_bytes
, int reserve
);
107 static int block_rsv_use_bytes(struct btrfs_block_rsv
*block_rsv
,
109 int btrfs_pin_extent(struct btrfs_root
*root
,
110 u64 bytenr
, u64 num_bytes
, int reserved
);
113 block_group_cache_done(struct btrfs_block_group_cache
*cache
)
116 return cache
->cached
== BTRFS_CACHE_FINISHED
;
119 static int block_group_bits(struct btrfs_block_group_cache
*cache
, u64 bits
)
121 return (cache
->flags
& bits
) == bits
;
124 static void btrfs_get_block_group(struct btrfs_block_group_cache
*cache
)
126 atomic_inc(&cache
->count
);
129 void btrfs_put_block_group(struct btrfs_block_group_cache
*cache
)
131 if (atomic_dec_and_test(&cache
->count
)) {
132 WARN_ON(cache
->pinned
> 0);
133 WARN_ON(cache
->reserved
> 0);
134 kfree(cache
->free_space_ctl
);
140 * this adds the block group to the fs_info rb tree for the block group
143 static int btrfs_add_block_group_cache(struct btrfs_fs_info
*info
,
144 struct btrfs_block_group_cache
*block_group
)
147 struct rb_node
*parent
= NULL
;
148 struct btrfs_block_group_cache
*cache
;
150 spin_lock(&info
->block_group_cache_lock
);
151 p
= &info
->block_group_cache_tree
.rb_node
;
155 cache
= rb_entry(parent
, struct btrfs_block_group_cache
,
157 if (block_group
->key
.objectid
< cache
->key
.objectid
) {
159 } else if (block_group
->key
.objectid
> cache
->key
.objectid
) {
162 spin_unlock(&info
->block_group_cache_lock
);
167 rb_link_node(&block_group
->cache_node
, parent
, p
);
168 rb_insert_color(&block_group
->cache_node
,
169 &info
->block_group_cache_tree
);
171 if (info
->first_logical_byte
> block_group
->key
.objectid
)
172 info
->first_logical_byte
= block_group
->key
.objectid
;
174 spin_unlock(&info
->block_group_cache_lock
);
180 * This will return the block group at or after bytenr if contains is 0, else
181 * it will return the block group that contains the bytenr
183 static struct btrfs_block_group_cache
*
184 block_group_cache_tree_search(struct btrfs_fs_info
*info
, u64 bytenr
,
187 struct btrfs_block_group_cache
*cache
, *ret
= NULL
;
191 spin_lock(&info
->block_group_cache_lock
);
192 n
= info
->block_group_cache_tree
.rb_node
;
195 cache
= rb_entry(n
, struct btrfs_block_group_cache
,
197 end
= cache
->key
.objectid
+ cache
->key
.offset
- 1;
198 start
= cache
->key
.objectid
;
200 if (bytenr
< start
) {
201 if (!contains
&& (!ret
|| start
< ret
->key
.objectid
))
204 } else if (bytenr
> start
) {
205 if (contains
&& bytenr
<= end
) {
216 btrfs_get_block_group(ret
);
217 if (bytenr
== 0 && info
->first_logical_byte
> ret
->key
.objectid
)
218 info
->first_logical_byte
= ret
->key
.objectid
;
220 spin_unlock(&info
->block_group_cache_lock
);
225 static int add_excluded_extent(struct btrfs_root
*root
,
226 u64 start
, u64 num_bytes
)
228 u64 end
= start
+ num_bytes
- 1;
229 set_extent_bits(&root
->fs_info
->freed_extents
[0],
230 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
231 set_extent_bits(&root
->fs_info
->freed_extents
[1],
232 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
236 static void free_excluded_extents(struct btrfs_root
*root
,
237 struct btrfs_block_group_cache
*cache
)
241 start
= cache
->key
.objectid
;
242 end
= start
+ cache
->key
.offset
- 1;
244 clear_extent_bits(&root
->fs_info
->freed_extents
[0],
245 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
246 clear_extent_bits(&root
->fs_info
->freed_extents
[1],
247 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
250 static int exclude_super_stripes(struct btrfs_root
*root
,
251 struct btrfs_block_group_cache
*cache
)
258 if (cache
->key
.objectid
< BTRFS_SUPER_INFO_OFFSET
) {
259 stripe_len
= BTRFS_SUPER_INFO_OFFSET
- cache
->key
.objectid
;
260 cache
->bytes_super
+= stripe_len
;
261 ret
= add_excluded_extent(root
, cache
->key
.objectid
,
267 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
268 bytenr
= btrfs_sb_offset(i
);
269 ret
= btrfs_rmap_block(&root
->fs_info
->mapping_tree
,
270 cache
->key
.objectid
, bytenr
,
271 0, &logical
, &nr
, &stripe_len
);
278 if (logical
[nr
] > cache
->key
.objectid
+
282 if (logical
[nr
] + stripe_len
<= cache
->key
.objectid
)
286 if (start
< cache
->key
.objectid
) {
287 start
= cache
->key
.objectid
;
288 len
= (logical
[nr
] + stripe_len
) - start
;
290 len
= min_t(u64
, stripe_len
,
291 cache
->key
.objectid
+
292 cache
->key
.offset
- start
);
295 cache
->bytes_super
+= len
;
296 ret
= add_excluded_extent(root
, start
, len
);
308 static struct btrfs_caching_control
*
309 get_caching_control(struct btrfs_block_group_cache
*cache
)
311 struct btrfs_caching_control
*ctl
;
313 spin_lock(&cache
->lock
);
314 if (cache
->cached
!= BTRFS_CACHE_STARTED
) {
315 spin_unlock(&cache
->lock
);
319 /* We're loading it the fast way, so we don't have a caching_ctl. */
320 if (!cache
->caching_ctl
) {
321 spin_unlock(&cache
->lock
);
325 ctl
= cache
->caching_ctl
;
326 atomic_inc(&ctl
->count
);
327 spin_unlock(&cache
->lock
);
331 static void put_caching_control(struct btrfs_caching_control
*ctl
)
333 if (atomic_dec_and_test(&ctl
->count
))
338 * this is only called by cache_block_group, since we could have freed extents
339 * we need to check the pinned_extents for any extents that can't be used yet
340 * since their free space will be released as soon as the transaction commits.
342 static u64
add_new_free_space(struct btrfs_block_group_cache
*block_group
,
343 struct btrfs_fs_info
*info
, u64 start
, u64 end
)
345 u64 extent_start
, extent_end
, size
, total_added
= 0;
348 while (start
< end
) {
349 ret
= find_first_extent_bit(info
->pinned_extents
, start
,
350 &extent_start
, &extent_end
,
351 EXTENT_DIRTY
| EXTENT_UPTODATE
,
356 if (extent_start
<= start
) {
357 start
= extent_end
+ 1;
358 } else if (extent_start
> start
&& extent_start
< end
) {
359 size
= extent_start
- start
;
361 ret
= btrfs_add_free_space(block_group
, start
,
363 BUG_ON(ret
); /* -ENOMEM or logic error */
364 start
= extent_end
+ 1;
373 ret
= btrfs_add_free_space(block_group
, start
, size
);
374 BUG_ON(ret
); /* -ENOMEM or logic error */
380 static noinline
void caching_thread(struct btrfs_work
*work
)
382 struct btrfs_block_group_cache
*block_group
;
383 struct btrfs_fs_info
*fs_info
;
384 struct btrfs_caching_control
*caching_ctl
;
385 struct btrfs_root
*extent_root
;
386 struct btrfs_path
*path
;
387 struct extent_buffer
*leaf
;
388 struct btrfs_key key
;
394 caching_ctl
= container_of(work
, struct btrfs_caching_control
, work
);
395 block_group
= caching_ctl
->block_group
;
396 fs_info
= block_group
->fs_info
;
397 extent_root
= fs_info
->extent_root
;
399 path
= btrfs_alloc_path();
403 last
= max_t(u64
, block_group
->key
.objectid
, BTRFS_SUPER_INFO_OFFSET
);
406 * We don't want to deadlock with somebody trying to allocate a new
407 * extent for the extent root while also trying to search the extent
408 * root to add free space. So we skip locking and search the commit
409 * root, since its read-only
411 path
->skip_locking
= 1;
412 path
->search_commit_root
= 1;
417 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
419 mutex_lock(&caching_ctl
->mutex
);
420 /* need to make sure the commit_root doesn't disappear */
421 down_read(&fs_info
->extent_commit_sem
);
423 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
427 leaf
= path
->nodes
[0];
428 nritems
= btrfs_header_nritems(leaf
);
431 if (btrfs_fs_closing(fs_info
) > 1) {
436 if (path
->slots
[0] < nritems
) {
437 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
439 ret
= find_next_key(path
, 0, &key
);
443 if (need_resched()) {
444 caching_ctl
->progress
= last
;
445 btrfs_release_path(path
);
446 up_read(&fs_info
->extent_commit_sem
);
447 mutex_unlock(&caching_ctl
->mutex
);
452 ret
= btrfs_next_leaf(extent_root
, path
);
457 leaf
= path
->nodes
[0];
458 nritems
= btrfs_header_nritems(leaf
);
462 if (key
.objectid
< block_group
->key
.objectid
) {
467 if (key
.objectid
>= block_group
->key
.objectid
+
468 block_group
->key
.offset
)
471 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
||
472 key
.type
== BTRFS_METADATA_ITEM_KEY
) {
473 total_found
+= add_new_free_space(block_group
,
476 if (key
.type
== BTRFS_METADATA_ITEM_KEY
)
477 last
= key
.objectid
+
478 fs_info
->tree_root
->leafsize
;
480 last
= key
.objectid
+ key
.offset
;
482 if (total_found
> (1024 * 1024 * 2)) {
484 wake_up(&caching_ctl
->wait
);
491 total_found
+= add_new_free_space(block_group
, fs_info
, last
,
492 block_group
->key
.objectid
+
493 block_group
->key
.offset
);
494 caching_ctl
->progress
= (u64
)-1;
496 spin_lock(&block_group
->lock
);
497 block_group
->caching_ctl
= NULL
;
498 block_group
->cached
= BTRFS_CACHE_FINISHED
;
499 spin_unlock(&block_group
->lock
);
502 btrfs_free_path(path
);
503 up_read(&fs_info
->extent_commit_sem
);
505 free_excluded_extents(extent_root
, block_group
);
507 mutex_unlock(&caching_ctl
->mutex
);
509 wake_up(&caching_ctl
->wait
);
511 put_caching_control(caching_ctl
);
512 btrfs_put_block_group(block_group
);
515 static int cache_block_group(struct btrfs_block_group_cache
*cache
,
519 struct btrfs_fs_info
*fs_info
= cache
->fs_info
;
520 struct btrfs_caching_control
*caching_ctl
;
523 caching_ctl
= kzalloc(sizeof(*caching_ctl
), GFP_NOFS
);
527 INIT_LIST_HEAD(&caching_ctl
->list
);
528 mutex_init(&caching_ctl
->mutex
);
529 init_waitqueue_head(&caching_ctl
->wait
);
530 caching_ctl
->block_group
= cache
;
531 caching_ctl
->progress
= cache
->key
.objectid
;
532 atomic_set(&caching_ctl
->count
, 1);
533 caching_ctl
->work
.func
= caching_thread
;
535 spin_lock(&cache
->lock
);
537 * This should be a rare occasion, but this could happen I think in the
538 * case where one thread starts to load the space cache info, and then
539 * some other thread starts a transaction commit which tries to do an
540 * allocation while the other thread is still loading the space cache
541 * info. The previous loop should have kept us from choosing this block
542 * group, but if we've moved to the state where we will wait on caching
543 * block groups we need to first check if we're doing a fast load here,
544 * so we can wait for it to finish, otherwise we could end up allocating
545 * from a block group who's cache gets evicted for one reason or
548 while (cache
->cached
== BTRFS_CACHE_FAST
) {
549 struct btrfs_caching_control
*ctl
;
551 ctl
= cache
->caching_ctl
;
552 atomic_inc(&ctl
->count
);
553 prepare_to_wait(&ctl
->wait
, &wait
, TASK_UNINTERRUPTIBLE
);
554 spin_unlock(&cache
->lock
);
558 finish_wait(&ctl
->wait
, &wait
);
559 put_caching_control(ctl
);
560 spin_lock(&cache
->lock
);
563 if (cache
->cached
!= BTRFS_CACHE_NO
) {
564 spin_unlock(&cache
->lock
);
568 WARN_ON(cache
->caching_ctl
);
569 cache
->caching_ctl
= caching_ctl
;
570 cache
->cached
= BTRFS_CACHE_FAST
;
571 spin_unlock(&cache
->lock
);
573 if (fs_info
->mount_opt
& BTRFS_MOUNT_SPACE_CACHE
) {
574 ret
= load_free_space_cache(fs_info
, cache
);
576 spin_lock(&cache
->lock
);
578 cache
->caching_ctl
= NULL
;
579 cache
->cached
= BTRFS_CACHE_FINISHED
;
580 cache
->last_byte_to_unpin
= (u64
)-1;
582 if (load_cache_only
) {
583 cache
->caching_ctl
= NULL
;
584 cache
->cached
= BTRFS_CACHE_NO
;
586 cache
->cached
= BTRFS_CACHE_STARTED
;
589 spin_unlock(&cache
->lock
);
590 wake_up(&caching_ctl
->wait
);
592 put_caching_control(caching_ctl
);
593 free_excluded_extents(fs_info
->extent_root
, cache
);
598 * We are not going to do the fast caching, set cached to the
599 * appropriate value and wakeup any waiters.
601 spin_lock(&cache
->lock
);
602 if (load_cache_only
) {
603 cache
->caching_ctl
= NULL
;
604 cache
->cached
= BTRFS_CACHE_NO
;
606 cache
->cached
= BTRFS_CACHE_STARTED
;
608 spin_unlock(&cache
->lock
);
609 wake_up(&caching_ctl
->wait
);
612 if (load_cache_only
) {
613 put_caching_control(caching_ctl
);
617 down_write(&fs_info
->extent_commit_sem
);
618 atomic_inc(&caching_ctl
->count
);
619 list_add_tail(&caching_ctl
->list
, &fs_info
->caching_block_groups
);
620 up_write(&fs_info
->extent_commit_sem
);
622 btrfs_get_block_group(cache
);
624 btrfs_queue_worker(&fs_info
->caching_workers
, &caching_ctl
->work
);
630 * return the block group that starts at or after bytenr
632 static struct btrfs_block_group_cache
*
633 btrfs_lookup_first_block_group(struct btrfs_fs_info
*info
, u64 bytenr
)
635 struct btrfs_block_group_cache
*cache
;
637 cache
= block_group_cache_tree_search(info
, bytenr
, 0);
643 * return the block group that contains the given bytenr
645 struct btrfs_block_group_cache
*btrfs_lookup_block_group(
646 struct btrfs_fs_info
*info
,
649 struct btrfs_block_group_cache
*cache
;
651 cache
= block_group_cache_tree_search(info
, bytenr
, 1);
656 static struct btrfs_space_info
*__find_space_info(struct btrfs_fs_info
*info
,
659 struct list_head
*head
= &info
->space_info
;
660 struct btrfs_space_info
*found
;
662 flags
&= BTRFS_BLOCK_GROUP_TYPE_MASK
;
665 list_for_each_entry_rcu(found
, head
, list
) {
666 if (found
->flags
& flags
) {
676 * after adding space to the filesystem, we need to clear the full flags
677 * on all the space infos.
679 void btrfs_clear_space_info_full(struct btrfs_fs_info
*info
)
681 struct list_head
*head
= &info
->space_info
;
682 struct btrfs_space_info
*found
;
685 list_for_each_entry_rcu(found
, head
, list
)
690 /* simple helper to search for an existing extent at a given offset */
691 int btrfs_lookup_extent(struct btrfs_root
*root
, u64 start
, u64 len
)
694 struct btrfs_key key
;
695 struct btrfs_path
*path
;
697 path
= btrfs_alloc_path();
701 key
.objectid
= start
;
703 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
704 ret
= btrfs_search_slot(NULL
, root
->fs_info
->extent_root
, &key
, path
,
707 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
708 if (key
.objectid
== start
&&
709 key
.type
== BTRFS_METADATA_ITEM_KEY
)
712 btrfs_free_path(path
);
717 * helper function to lookup reference count and flags of a tree block.
719 * the head node for delayed ref is used to store the sum of all the
720 * reference count modifications queued up in the rbtree. the head
721 * node may also store the extent flags to set. This way you can check
722 * to see what the reference count and extent flags would be if all of
723 * the delayed refs are not processed.
725 int btrfs_lookup_extent_info(struct btrfs_trans_handle
*trans
,
726 struct btrfs_root
*root
, u64 bytenr
,
727 u64 offset
, int metadata
, u64
*refs
, u64
*flags
)
729 struct btrfs_delayed_ref_head
*head
;
730 struct btrfs_delayed_ref_root
*delayed_refs
;
731 struct btrfs_path
*path
;
732 struct btrfs_extent_item
*ei
;
733 struct extent_buffer
*leaf
;
734 struct btrfs_key key
;
741 * If we don't have skinny metadata, don't bother doing anything
744 if (metadata
&& !btrfs_fs_incompat(root
->fs_info
, SKINNY_METADATA
)) {
745 offset
= root
->leafsize
;
749 path
= btrfs_alloc_path();
754 key
.objectid
= bytenr
;
755 key
.type
= BTRFS_METADATA_ITEM_KEY
;
758 key
.objectid
= bytenr
;
759 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
764 path
->skip_locking
= 1;
765 path
->search_commit_root
= 1;
768 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
,
773 if (ret
> 0 && metadata
&& key
.type
== BTRFS_METADATA_ITEM_KEY
) {
775 if (path
->slots
[0]) {
777 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
779 if (key
.objectid
== bytenr
&&
780 key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
781 key
.offset
== root
->leafsize
)
785 key
.objectid
= bytenr
;
786 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
787 key
.offset
= root
->leafsize
;
788 btrfs_release_path(path
);
794 leaf
= path
->nodes
[0];
795 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
796 if (item_size
>= sizeof(*ei
)) {
797 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
798 struct btrfs_extent_item
);
799 num_refs
= btrfs_extent_refs(leaf
, ei
);
800 extent_flags
= btrfs_extent_flags(leaf
, ei
);
802 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
803 struct btrfs_extent_item_v0
*ei0
;
804 BUG_ON(item_size
!= sizeof(*ei0
));
805 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
806 struct btrfs_extent_item_v0
);
807 num_refs
= btrfs_extent_refs_v0(leaf
, ei0
);
808 /* FIXME: this isn't correct for data */
809 extent_flags
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
814 BUG_ON(num_refs
== 0);
824 delayed_refs
= &trans
->transaction
->delayed_refs
;
825 spin_lock(&delayed_refs
->lock
);
826 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
828 if (!mutex_trylock(&head
->mutex
)) {
829 atomic_inc(&head
->node
.refs
);
830 spin_unlock(&delayed_refs
->lock
);
832 btrfs_release_path(path
);
835 * Mutex was contended, block until it's released and try
838 mutex_lock(&head
->mutex
);
839 mutex_unlock(&head
->mutex
);
840 btrfs_put_delayed_ref(&head
->node
);
843 if (head
->extent_op
&& head
->extent_op
->update_flags
)
844 extent_flags
|= head
->extent_op
->flags_to_set
;
846 BUG_ON(num_refs
== 0);
848 num_refs
+= head
->node
.ref_mod
;
849 mutex_unlock(&head
->mutex
);
851 spin_unlock(&delayed_refs
->lock
);
853 WARN_ON(num_refs
== 0);
857 *flags
= extent_flags
;
859 btrfs_free_path(path
);
864 * Back reference rules. Back refs have three main goals:
866 * 1) differentiate between all holders of references to an extent so that
867 * when a reference is dropped we can make sure it was a valid reference
868 * before freeing the extent.
870 * 2) Provide enough information to quickly find the holders of an extent
871 * if we notice a given block is corrupted or bad.
873 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
874 * maintenance. This is actually the same as #2, but with a slightly
875 * different use case.
877 * There are two kinds of back refs. The implicit back refs is optimized
878 * for pointers in non-shared tree blocks. For a given pointer in a block,
879 * back refs of this kind provide information about the block's owner tree
880 * and the pointer's key. These information allow us to find the block by
881 * b-tree searching. The full back refs is for pointers in tree blocks not
882 * referenced by their owner trees. The location of tree block is recorded
883 * in the back refs. Actually the full back refs is generic, and can be
884 * used in all cases the implicit back refs is used. The major shortcoming
885 * of the full back refs is its overhead. Every time a tree block gets
886 * COWed, we have to update back refs entry for all pointers in it.
888 * For a newly allocated tree block, we use implicit back refs for
889 * pointers in it. This means most tree related operations only involve
890 * implicit back refs. For a tree block created in old transaction, the
891 * only way to drop a reference to it is COW it. So we can detect the
892 * event that tree block loses its owner tree's reference and do the
893 * back refs conversion.
895 * When a tree block is COW'd through a tree, there are four cases:
897 * The reference count of the block is one and the tree is the block's
898 * owner tree. Nothing to do in this case.
900 * The reference count of the block is one and the tree is not the
901 * block's owner tree. In this case, full back refs is used for pointers
902 * in the block. Remove these full back refs, add implicit back refs for
903 * every pointers in the new block.
905 * The reference count of the block is greater than one and the tree is
906 * the block's owner tree. In this case, implicit back refs is used for
907 * pointers in the block. Add full back refs for every pointers in the
908 * block, increase lower level extents' reference counts. The original
909 * implicit back refs are entailed to the new block.
911 * The reference count of the block is greater than one and the tree is
912 * not the block's owner tree. Add implicit back refs for every pointer in
913 * the new block, increase lower level extents' reference count.
915 * Back Reference Key composing:
917 * The key objectid corresponds to the first byte in the extent,
918 * The key type is used to differentiate between types of back refs.
919 * There are different meanings of the key offset for different types
922 * File extents can be referenced by:
924 * - multiple snapshots, subvolumes, or different generations in one subvol
925 * - different files inside a single subvolume
926 * - different offsets inside a file (bookend extents in file.c)
928 * The extent ref structure for the implicit back refs has fields for:
930 * - Objectid of the subvolume root
931 * - objectid of the file holding the reference
932 * - original offset in the file
933 * - how many bookend extents
935 * The key offset for the implicit back refs is hash of the first
938 * The extent ref structure for the full back refs has field for:
940 * - number of pointers in the tree leaf
942 * The key offset for the implicit back refs is the first byte of
945 * When a file extent is allocated, The implicit back refs is used.
946 * the fields are filled in:
948 * (root_key.objectid, inode objectid, offset in file, 1)
950 * When a file extent is removed file truncation, we find the
951 * corresponding implicit back refs and check the following fields:
953 * (btrfs_header_owner(leaf), inode objectid, offset in file)
955 * Btree extents can be referenced by:
957 * - Different subvolumes
959 * Both the implicit back refs and the full back refs for tree blocks
960 * only consist of key. The key offset for the implicit back refs is
961 * objectid of block's owner tree. The key offset for the full back refs
962 * is the first byte of parent block.
964 * When implicit back refs is used, information about the lowest key and
965 * level of the tree block are required. These information are stored in
966 * tree block info structure.
969 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
970 static int convert_extent_item_v0(struct btrfs_trans_handle
*trans
,
971 struct btrfs_root
*root
,
972 struct btrfs_path
*path
,
973 u64 owner
, u32 extra_size
)
975 struct btrfs_extent_item
*item
;
976 struct btrfs_extent_item_v0
*ei0
;
977 struct btrfs_extent_ref_v0
*ref0
;
978 struct btrfs_tree_block_info
*bi
;
979 struct extent_buffer
*leaf
;
980 struct btrfs_key key
;
981 struct btrfs_key found_key
;
982 u32 new_size
= sizeof(*item
);
986 leaf
= path
->nodes
[0];
987 BUG_ON(btrfs_item_size_nr(leaf
, path
->slots
[0]) != sizeof(*ei0
));
989 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
990 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
991 struct btrfs_extent_item_v0
);
992 refs
= btrfs_extent_refs_v0(leaf
, ei0
);
994 if (owner
== (u64
)-1) {
996 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
997 ret
= btrfs_next_leaf(root
, path
);
1000 BUG_ON(ret
> 0); /* Corruption */
1001 leaf
= path
->nodes
[0];
1003 btrfs_item_key_to_cpu(leaf
, &found_key
,
1005 BUG_ON(key
.objectid
!= found_key
.objectid
);
1006 if (found_key
.type
!= BTRFS_EXTENT_REF_V0_KEY
) {
1010 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1011 struct btrfs_extent_ref_v0
);
1012 owner
= btrfs_ref_objectid_v0(leaf
, ref0
);
1016 btrfs_release_path(path
);
1018 if (owner
< BTRFS_FIRST_FREE_OBJECTID
)
1019 new_size
+= sizeof(*bi
);
1021 new_size
-= sizeof(*ei0
);
1022 ret
= btrfs_search_slot(trans
, root
, &key
, path
,
1023 new_size
+ extra_size
, 1);
1026 BUG_ON(ret
); /* Corruption */
1028 btrfs_extend_item(root
, path
, new_size
);
1030 leaf
= path
->nodes
[0];
1031 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1032 btrfs_set_extent_refs(leaf
, item
, refs
);
1033 /* FIXME: get real generation */
1034 btrfs_set_extent_generation(leaf
, item
, 0);
1035 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1036 btrfs_set_extent_flags(leaf
, item
,
1037 BTRFS_EXTENT_FLAG_TREE_BLOCK
|
1038 BTRFS_BLOCK_FLAG_FULL_BACKREF
);
1039 bi
= (struct btrfs_tree_block_info
*)(item
+ 1);
1040 /* FIXME: get first key of the block */
1041 memset_extent_buffer(leaf
, 0, (unsigned long)bi
, sizeof(*bi
));
1042 btrfs_set_tree_block_level(leaf
, bi
, (int)owner
);
1044 btrfs_set_extent_flags(leaf
, item
, BTRFS_EXTENT_FLAG_DATA
);
1046 btrfs_mark_buffer_dirty(leaf
);
1051 static u64
hash_extent_data_ref(u64 root_objectid
, u64 owner
, u64 offset
)
1053 u32 high_crc
= ~(u32
)0;
1054 u32 low_crc
= ~(u32
)0;
1057 lenum
= cpu_to_le64(root_objectid
);
1058 high_crc
= crc32c(high_crc
, &lenum
, sizeof(lenum
));
1059 lenum
= cpu_to_le64(owner
);
1060 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
1061 lenum
= cpu_to_le64(offset
);
1062 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
1064 return ((u64
)high_crc
<< 31) ^ (u64
)low_crc
;
1067 static u64
hash_extent_data_ref_item(struct extent_buffer
*leaf
,
1068 struct btrfs_extent_data_ref
*ref
)
1070 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf
, ref
),
1071 btrfs_extent_data_ref_objectid(leaf
, ref
),
1072 btrfs_extent_data_ref_offset(leaf
, ref
));
1075 static int match_extent_data_ref(struct extent_buffer
*leaf
,
1076 struct btrfs_extent_data_ref
*ref
,
1077 u64 root_objectid
, u64 owner
, u64 offset
)
1079 if (btrfs_extent_data_ref_root(leaf
, ref
) != root_objectid
||
1080 btrfs_extent_data_ref_objectid(leaf
, ref
) != owner
||
1081 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
1086 static noinline
int lookup_extent_data_ref(struct btrfs_trans_handle
*trans
,
1087 struct btrfs_root
*root
,
1088 struct btrfs_path
*path
,
1089 u64 bytenr
, u64 parent
,
1091 u64 owner
, u64 offset
)
1093 struct btrfs_key key
;
1094 struct btrfs_extent_data_ref
*ref
;
1095 struct extent_buffer
*leaf
;
1101 key
.objectid
= bytenr
;
1103 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
1104 key
.offset
= parent
;
1106 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
1107 key
.offset
= hash_extent_data_ref(root_objectid
,
1112 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1121 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1122 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
1123 btrfs_release_path(path
);
1124 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1135 leaf
= path
->nodes
[0];
1136 nritems
= btrfs_header_nritems(leaf
);
1138 if (path
->slots
[0] >= nritems
) {
1139 ret
= btrfs_next_leaf(root
, path
);
1145 leaf
= path
->nodes
[0];
1146 nritems
= btrfs_header_nritems(leaf
);
1150 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1151 if (key
.objectid
!= bytenr
||
1152 key
.type
!= BTRFS_EXTENT_DATA_REF_KEY
)
1155 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1156 struct btrfs_extent_data_ref
);
1158 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
1161 btrfs_release_path(path
);
1173 static noinline
int insert_extent_data_ref(struct btrfs_trans_handle
*trans
,
1174 struct btrfs_root
*root
,
1175 struct btrfs_path
*path
,
1176 u64 bytenr
, u64 parent
,
1177 u64 root_objectid
, u64 owner
,
1178 u64 offset
, int refs_to_add
)
1180 struct btrfs_key key
;
1181 struct extent_buffer
*leaf
;
1186 key
.objectid
= bytenr
;
1188 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
1189 key
.offset
= parent
;
1190 size
= sizeof(struct btrfs_shared_data_ref
);
1192 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
1193 key
.offset
= hash_extent_data_ref(root_objectid
,
1195 size
= sizeof(struct btrfs_extent_data_ref
);
1198 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, size
);
1199 if (ret
&& ret
!= -EEXIST
)
1202 leaf
= path
->nodes
[0];
1204 struct btrfs_shared_data_ref
*ref
;
1205 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1206 struct btrfs_shared_data_ref
);
1208 btrfs_set_shared_data_ref_count(leaf
, ref
, refs_to_add
);
1210 num_refs
= btrfs_shared_data_ref_count(leaf
, ref
);
1211 num_refs
+= refs_to_add
;
1212 btrfs_set_shared_data_ref_count(leaf
, ref
, num_refs
);
1215 struct btrfs_extent_data_ref
*ref
;
1216 while (ret
== -EEXIST
) {
1217 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1218 struct btrfs_extent_data_ref
);
1219 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
1222 btrfs_release_path(path
);
1224 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1226 if (ret
&& ret
!= -EEXIST
)
1229 leaf
= path
->nodes
[0];
1231 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1232 struct btrfs_extent_data_ref
);
1234 btrfs_set_extent_data_ref_root(leaf
, ref
,
1236 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
1237 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
1238 btrfs_set_extent_data_ref_count(leaf
, ref
, refs_to_add
);
1240 num_refs
= btrfs_extent_data_ref_count(leaf
, ref
);
1241 num_refs
+= refs_to_add
;
1242 btrfs_set_extent_data_ref_count(leaf
, ref
, num_refs
);
1245 btrfs_mark_buffer_dirty(leaf
);
1248 btrfs_release_path(path
);
1252 static noinline
int remove_extent_data_ref(struct btrfs_trans_handle
*trans
,
1253 struct btrfs_root
*root
,
1254 struct btrfs_path
*path
,
1257 struct btrfs_key key
;
1258 struct btrfs_extent_data_ref
*ref1
= NULL
;
1259 struct btrfs_shared_data_ref
*ref2
= NULL
;
1260 struct extent_buffer
*leaf
;
1264 leaf
= path
->nodes
[0];
1265 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1267 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1268 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
1269 struct btrfs_extent_data_ref
);
1270 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1271 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
1272 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
1273 struct btrfs_shared_data_ref
);
1274 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1275 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1276 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
1277 struct btrfs_extent_ref_v0
*ref0
;
1278 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1279 struct btrfs_extent_ref_v0
);
1280 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
1286 BUG_ON(num_refs
< refs_to_drop
);
1287 num_refs
-= refs_to_drop
;
1289 if (num_refs
== 0) {
1290 ret
= btrfs_del_item(trans
, root
, path
);
1292 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
)
1293 btrfs_set_extent_data_ref_count(leaf
, ref1
, num_refs
);
1294 else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
)
1295 btrfs_set_shared_data_ref_count(leaf
, ref2
, num_refs
);
1296 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1298 struct btrfs_extent_ref_v0
*ref0
;
1299 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1300 struct btrfs_extent_ref_v0
);
1301 btrfs_set_ref_count_v0(leaf
, ref0
, num_refs
);
1304 btrfs_mark_buffer_dirty(leaf
);
1309 static noinline u32
extent_data_ref_count(struct btrfs_root
*root
,
1310 struct btrfs_path
*path
,
1311 struct btrfs_extent_inline_ref
*iref
)
1313 struct btrfs_key key
;
1314 struct extent_buffer
*leaf
;
1315 struct btrfs_extent_data_ref
*ref1
;
1316 struct btrfs_shared_data_ref
*ref2
;
1319 leaf
= path
->nodes
[0];
1320 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1322 if (btrfs_extent_inline_ref_type(leaf
, iref
) ==
1323 BTRFS_EXTENT_DATA_REF_KEY
) {
1324 ref1
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1325 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1327 ref2
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1328 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1330 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1331 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
1332 struct btrfs_extent_data_ref
);
1333 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1334 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
1335 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
1336 struct btrfs_shared_data_ref
);
1337 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1338 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1339 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
1340 struct btrfs_extent_ref_v0
*ref0
;
1341 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1342 struct btrfs_extent_ref_v0
);
1343 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
1351 static noinline
int lookup_tree_block_ref(struct btrfs_trans_handle
*trans
,
1352 struct btrfs_root
*root
,
1353 struct btrfs_path
*path
,
1354 u64 bytenr
, u64 parent
,
1357 struct btrfs_key key
;
1360 key
.objectid
= bytenr
;
1362 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1363 key
.offset
= parent
;
1365 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
1366 key
.offset
= root_objectid
;
1369 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1372 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1373 if (ret
== -ENOENT
&& parent
) {
1374 btrfs_release_path(path
);
1375 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
1376 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1384 static noinline
int insert_tree_block_ref(struct btrfs_trans_handle
*trans
,
1385 struct btrfs_root
*root
,
1386 struct btrfs_path
*path
,
1387 u64 bytenr
, u64 parent
,
1390 struct btrfs_key key
;
1393 key
.objectid
= bytenr
;
1395 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1396 key
.offset
= parent
;
1398 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
1399 key
.offset
= root_objectid
;
1402 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, 0);
1403 btrfs_release_path(path
);
1407 static inline int extent_ref_type(u64 parent
, u64 owner
)
1410 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1412 type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1414 type
= BTRFS_TREE_BLOCK_REF_KEY
;
1417 type
= BTRFS_SHARED_DATA_REF_KEY
;
1419 type
= BTRFS_EXTENT_DATA_REF_KEY
;
1424 static int find_next_key(struct btrfs_path
*path
, int level
,
1425 struct btrfs_key
*key
)
1428 for (; level
< BTRFS_MAX_LEVEL
; level
++) {
1429 if (!path
->nodes
[level
])
1431 if (path
->slots
[level
] + 1 >=
1432 btrfs_header_nritems(path
->nodes
[level
]))
1435 btrfs_item_key_to_cpu(path
->nodes
[level
], key
,
1436 path
->slots
[level
] + 1);
1438 btrfs_node_key_to_cpu(path
->nodes
[level
], key
,
1439 path
->slots
[level
] + 1);
1446 * look for inline back ref. if back ref is found, *ref_ret is set
1447 * to the address of inline back ref, and 0 is returned.
1449 * if back ref isn't found, *ref_ret is set to the address where it
1450 * should be inserted, and -ENOENT is returned.
1452 * if insert is true and there are too many inline back refs, the path
1453 * points to the extent item, and -EAGAIN is returned.
1455 * NOTE: inline back refs are ordered in the same way that back ref
1456 * items in the tree are ordered.
1458 static noinline_for_stack
1459 int lookup_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1460 struct btrfs_root
*root
,
1461 struct btrfs_path
*path
,
1462 struct btrfs_extent_inline_ref
**ref_ret
,
1463 u64 bytenr
, u64 num_bytes
,
1464 u64 parent
, u64 root_objectid
,
1465 u64 owner
, u64 offset
, int insert
)
1467 struct btrfs_key key
;
1468 struct extent_buffer
*leaf
;
1469 struct btrfs_extent_item
*ei
;
1470 struct btrfs_extent_inline_ref
*iref
;
1480 bool skinny_metadata
= btrfs_fs_incompat(root
->fs_info
,
1483 key
.objectid
= bytenr
;
1484 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1485 key
.offset
= num_bytes
;
1487 want
= extent_ref_type(parent
, owner
);
1489 extra_size
= btrfs_extent_inline_ref_size(want
);
1490 path
->keep_locks
= 1;
1495 * Owner is our parent level, so we can just add one to get the level
1496 * for the block we are interested in.
1498 if (skinny_metadata
&& owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1499 key
.type
= BTRFS_METADATA_ITEM_KEY
;
1504 ret
= btrfs_search_slot(trans
, root
, &key
, path
, extra_size
, 1);
1511 * We may be a newly converted file system which still has the old fat
1512 * extent entries for metadata, so try and see if we have one of those.
1514 if (ret
> 0 && skinny_metadata
) {
1515 skinny_metadata
= false;
1516 if (path
->slots
[0]) {
1518 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
1520 if (key
.objectid
== bytenr
&&
1521 key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
1522 key
.offset
== num_bytes
)
1526 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1527 key
.offset
= num_bytes
;
1528 btrfs_release_path(path
);
1533 if (ret
&& !insert
) {
1542 leaf
= path
->nodes
[0];
1543 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1544 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1545 if (item_size
< sizeof(*ei
)) {
1550 ret
= convert_extent_item_v0(trans
, root
, path
, owner
,
1556 leaf
= path
->nodes
[0];
1557 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1560 BUG_ON(item_size
< sizeof(*ei
));
1562 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1563 flags
= btrfs_extent_flags(leaf
, ei
);
1565 ptr
= (unsigned long)(ei
+ 1);
1566 end
= (unsigned long)ei
+ item_size
;
1568 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
&& !skinny_metadata
) {
1569 ptr
+= sizeof(struct btrfs_tree_block_info
);
1579 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1580 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1584 ptr
+= btrfs_extent_inline_ref_size(type
);
1588 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1589 struct btrfs_extent_data_ref
*dref
;
1590 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1591 if (match_extent_data_ref(leaf
, dref
, root_objectid
,
1596 if (hash_extent_data_ref_item(leaf
, dref
) <
1597 hash_extent_data_ref(root_objectid
, owner
, offset
))
1601 ref_offset
= btrfs_extent_inline_ref_offset(leaf
, iref
);
1603 if (parent
== ref_offset
) {
1607 if (ref_offset
< parent
)
1610 if (root_objectid
== ref_offset
) {
1614 if (ref_offset
< root_objectid
)
1618 ptr
+= btrfs_extent_inline_ref_size(type
);
1620 if (err
== -ENOENT
&& insert
) {
1621 if (item_size
+ extra_size
>=
1622 BTRFS_MAX_EXTENT_ITEM_SIZE(root
)) {
1627 * To add new inline back ref, we have to make sure
1628 * there is no corresponding back ref item.
1629 * For simplicity, we just do not add new inline back
1630 * ref if there is any kind of item for this block
1632 if (find_next_key(path
, 0, &key
) == 0 &&
1633 key
.objectid
== bytenr
&&
1634 key
.type
< BTRFS_BLOCK_GROUP_ITEM_KEY
) {
1639 *ref_ret
= (struct btrfs_extent_inline_ref
*)ptr
;
1642 path
->keep_locks
= 0;
1643 btrfs_unlock_up_safe(path
, 1);
1649 * helper to add new inline back ref
1651 static noinline_for_stack
1652 void setup_inline_extent_backref(struct btrfs_root
*root
,
1653 struct btrfs_path
*path
,
1654 struct btrfs_extent_inline_ref
*iref
,
1655 u64 parent
, u64 root_objectid
,
1656 u64 owner
, u64 offset
, int refs_to_add
,
1657 struct btrfs_delayed_extent_op
*extent_op
)
1659 struct extent_buffer
*leaf
;
1660 struct btrfs_extent_item
*ei
;
1663 unsigned long item_offset
;
1668 leaf
= path
->nodes
[0];
1669 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1670 item_offset
= (unsigned long)iref
- (unsigned long)ei
;
1672 type
= extent_ref_type(parent
, owner
);
1673 size
= btrfs_extent_inline_ref_size(type
);
1675 btrfs_extend_item(root
, path
, size
);
1677 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1678 refs
= btrfs_extent_refs(leaf
, ei
);
1679 refs
+= refs_to_add
;
1680 btrfs_set_extent_refs(leaf
, ei
, refs
);
1682 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1684 ptr
= (unsigned long)ei
+ item_offset
;
1685 end
= (unsigned long)ei
+ btrfs_item_size_nr(leaf
, path
->slots
[0]);
1686 if (ptr
< end
- size
)
1687 memmove_extent_buffer(leaf
, ptr
+ size
, ptr
,
1690 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1691 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
1692 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1693 struct btrfs_extent_data_ref
*dref
;
1694 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1695 btrfs_set_extent_data_ref_root(leaf
, dref
, root_objectid
);
1696 btrfs_set_extent_data_ref_objectid(leaf
, dref
, owner
);
1697 btrfs_set_extent_data_ref_offset(leaf
, dref
, offset
);
1698 btrfs_set_extent_data_ref_count(leaf
, dref
, refs_to_add
);
1699 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1700 struct btrfs_shared_data_ref
*sref
;
1701 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1702 btrfs_set_shared_data_ref_count(leaf
, sref
, refs_to_add
);
1703 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1704 } else if (type
== BTRFS_SHARED_BLOCK_REF_KEY
) {
1705 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1707 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
1709 btrfs_mark_buffer_dirty(leaf
);
1712 static int lookup_extent_backref(struct btrfs_trans_handle
*trans
,
1713 struct btrfs_root
*root
,
1714 struct btrfs_path
*path
,
1715 struct btrfs_extent_inline_ref
**ref_ret
,
1716 u64 bytenr
, u64 num_bytes
, u64 parent
,
1717 u64 root_objectid
, u64 owner
, u64 offset
)
1721 ret
= lookup_inline_extent_backref(trans
, root
, path
, ref_ret
,
1722 bytenr
, num_bytes
, parent
,
1723 root_objectid
, owner
, offset
, 0);
1727 btrfs_release_path(path
);
1730 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1731 ret
= lookup_tree_block_ref(trans
, root
, path
, bytenr
, parent
,
1734 ret
= lookup_extent_data_ref(trans
, root
, path
, bytenr
, parent
,
1735 root_objectid
, owner
, offset
);
1741 * helper to update/remove inline back ref
1743 static noinline_for_stack
1744 void update_inline_extent_backref(struct btrfs_root
*root
,
1745 struct btrfs_path
*path
,
1746 struct btrfs_extent_inline_ref
*iref
,
1748 struct btrfs_delayed_extent_op
*extent_op
)
1750 struct extent_buffer
*leaf
;
1751 struct btrfs_extent_item
*ei
;
1752 struct btrfs_extent_data_ref
*dref
= NULL
;
1753 struct btrfs_shared_data_ref
*sref
= NULL
;
1761 leaf
= path
->nodes
[0];
1762 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1763 refs
= btrfs_extent_refs(leaf
, ei
);
1764 WARN_ON(refs_to_mod
< 0 && refs
+ refs_to_mod
<= 0);
1765 refs
+= refs_to_mod
;
1766 btrfs_set_extent_refs(leaf
, ei
, refs
);
1768 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1770 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1772 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1773 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1774 refs
= btrfs_extent_data_ref_count(leaf
, dref
);
1775 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1776 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1777 refs
= btrfs_shared_data_ref_count(leaf
, sref
);
1780 BUG_ON(refs_to_mod
!= -1);
1783 BUG_ON(refs_to_mod
< 0 && refs
< -refs_to_mod
);
1784 refs
+= refs_to_mod
;
1787 if (type
== BTRFS_EXTENT_DATA_REF_KEY
)
1788 btrfs_set_extent_data_ref_count(leaf
, dref
, refs
);
1790 btrfs_set_shared_data_ref_count(leaf
, sref
, refs
);
1792 size
= btrfs_extent_inline_ref_size(type
);
1793 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1794 ptr
= (unsigned long)iref
;
1795 end
= (unsigned long)ei
+ item_size
;
1796 if (ptr
+ size
< end
)
1797 memmove_extent_buffer(leaf
, ptr
, ptr
+ size
,
1800 btrfs_truncate_item(root
, path
, item_size
, 1);
1802 btrfs_mark_buffer_dirty(leaf
);
1805 static noinline_for_stack
1806 int insert_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1807 struct btrfs_root
*root
,
1808 struct btrfs_path
*path
,
1809 u64 bytenr
, u64 num_bytes
, u64 parent
,
1810 u64 root_objectid
, u64 owner
,
1811 u64 offset
, int refs_to_add
,
1812 struct btrfs_delayed_extent_op
*extent_op
)
1814 struct btrfs_extent_inline_ref
*iref
;
1817 ret
= lookup_inline_extent_backref(trans
, root
, path
, &iref
,
1818 bytenr
, num_bytes
, parent
,
1819 root_objectid
, owner
, offset
, 1);
1821 BUG_ON(owner
< BTRFS_FIRST_FREE_OBJECTID
);
1822 update_inline_extent_backref(root
, path
, iref
,
1823 refs_to_add
, extent_op
);
1824 } else if (ret
== -ENOENT
) {
1825 setup_inline_extent_backref(root
, path
, iref
, parent
,
1826 root_objectid
, owner
, offset
,
1827 refs_to_add
, extent_op
);
1833 static int insert_extent_backref(struct btrfs_trans_handle
*trans
,
1834 struct btrfs_root
*root
,
1835 struct btrfs_path
*path
,
1836 u64 bytenr
, u64 parent
, u64 root_objectid
,
1837 u64 owner
, u64 offset
, int refs_to_add
)
1840 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1841 BUG_ON(refs_to_add
!= 1);
1842 ret
= insert_tree_block_ref(trans
, root
, path
, bytenr
,
1843 parent
, root_objectid
);
1845 ret
= insert_extent_data_ref(trans
, root
, path
, bytenr
,
1846 parent
, root_objectid
,
1847 owner
, offset
, refs_to_add
);
1852 static int remove_extent_backref(struct btrfs_trans_handle
*trans
,
1853 struct btrfs_root
*root
,
1854 struct btrfs_path
*path
,
1855 struct btrfs_extent_inline_ref
*iref
,
1856 int refs_to_drop
, int is_data
)
1860 BUG_ON(!is_data
&& refs_to_drop
!= 1);
1862 update_inline_extent_backref(root
, path
, iref
,
1863 -refs_to_drop
, NULL
);
1864 } else if (is_data
) {
1865 ret
= remove_extent_data_ref(trans
, root
, path
, refs_to_drop
);
1867 ret
= btrfs_del_item(trans
, root
, path
);
1872 static int btrfs_issue_discard(struct block_device
*bdev
,
1875 return blkdev_issue_discard(bdev
, start
>> 9, len
>> 9, GFP_NOFS
, 0);
1878 static int btrfs_discard_extent(struct btrfs_root
*root
, u64 bytenr
,
1879 u64 num_bytes
, u64
*actual_bytes
)
1882 u64 discarded_bytes
= 0;
1883 struct btrfs_bio
*bbio
= NULL
;
1886 /* Tell the block device(s) that the sectors can be discarded */
1887 ret
= btrfs_map_block(root
->fs_info
, REQ_DISCARD
,
1888 bytenr
, &num_bytes
, &bbio
, 0);
1889 /* Error condition is -ENOMEM */
1891 struct btrfs_bio_stripe
*stripe
= bbio
->stripes
;
1895 for (i
= 0; i
< bbio
->num_stripes
; i
++, stripe
++) {
1896 if (!stripe
->dev
->can_discard
)
1899 ret
= btrfs_issue_discard(stripe
->dev
->bdev
,
1903 discarded_bytes
+= stripe
->length
;
1904 else if (ret
!= -EOPNOTSUPP
)
1905 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
1908 * Just in case we get back EOPNOTSUPP for some reason,
1909 * just ignore the return value so we don't screw up
1910 * people calling discard_extent.
1918 *actual_bytes
= discarded_bytes
;
1921 if (ret
== -EOPNOTSUPP
)
1926 /* Can return -ENOMEM */
1927 int btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
1928 struct btrfs_root
*root
,
1929 u64 bytenr
, u64 num_bytes
, u64 parent
,
1930 u64 root_objectid
, u64 owner
, u64 offset
, int for_cow
)
1933 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1935 BUG_ON(owner
< BTRFS_FIRST_FREE_OBJECTID
&&
1936 root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
1938 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1939 ret
= btrfs_add_delayed_tree_ref(fs_info
, trans
, bytenr
,
1941 parent
, root_objectid
, (int)owner
,
1942 BTRFS_ADD_DELAYED_REF
, NULL
, for_cow
);
1944 ret
= btrfs_add_delayed_data_ref(fs_info
, trans
, bytenr
,
1946 parent
, root_objectid
, owner
, offset
,
1947 BTRFS_ADD_DELAYED_REF
, NULL
, for_cow
);
1952 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
1953 struct btrfs_root
*root
,
1954 u64 bytenr
, u64 num_bytes
,
1955 u64 parent
, u64 root_objectid
,
1956 u64 owner
, u64 offset
, int refs_to_add
,
1957 struct btrfs_delayed_extent_op
*extent_op
)
1959 struct btrfs_path
*path
;
1960 struct extent_buffer
*leaf
;
1961 struct btrfs_extent_item
*item
;
1966 path
= btrfs_alloc_path();
1971 path
->leave_spinning
= 1;
1972 /* this will setup the path even if it fails to insert the back ref */
1973 ret
= insert_inline_extent_backref(trans
, root
->fs_info
->extent_root
,
1974 path
, bytenr
, num_bytes
, parent
,
1975 root_objectid
, owner
, offset
,
1976 refs_to_add
, extent_op
);
1980 if (ret
!= -EAGAIN
) {
1985 leaf
= path
->nodes
[0];
1986 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1987 refs
= btrfs_extent_refs(leaf
, item
);
1988 btrfs_set_extent_refs(leaf
, item
, refs
+ refs_to_add
);
1990 __run_delayed_extent_op(extent_op
, leaf
, item
);
1992 btrfs_mark_buffer_dirty(leaf
);
1993 btrfs_release_path(path
);
1996 path
->leave_spinning
= 1;
1998 /* now insert the actual backref */
1999 ret
= insert_extent_backref(trans
, root
->fs_info
->extent_root
,
2000 path
, bytenr
, parent
, root_objectid
,
2001 owner
, offset
, refs_to_add
);
2003 btrfs_abort_transaction(trans
, root
, ret
);
2005 btrfs_free_path(path
);
2009 static int run_delayed_data_ref(struct btrfs_trans_handle
*trans
,
2010 struct btrfs_root
*root
,
2011 struct btrfs_delayed_ref_node
*node
,
2012 struct btrfs_delayed_extent_op
*extent_op
,
2013 int insert_reserved
)
2016 struct btrfs_delayed_data_ref
*ref
;
2017 struct btrfs_key ins
;
2022 ins
.objectid
= node
->bytenr
;
2023 ins
.offset
= node
->num_bytes
;
2024 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
2026 ref
= btrfs_delayed_node_to_data_ref(node
);
2027 if (node
->type
== BTRFS_SHARED_DATA_REF_KEY
)
2028 parent
= ref
->parent
;
2030 ref_root
= ref
->root
;
2032 if (node
->action
== BTRFS_ADD_DELAYED_REF
&& insert_reserved
) {
2034 flags
|= extent_op
->flags_to_set
;
2035 ret
= alloc_reserved_file_extent(trans
, root
,
2036 parent
, ref_root
, flags
,
2037 ref
->objectid
, ref
->offset
,
2038 &ins
, node
->ref_mod
);
2039 } else if (node
->action
== BTRFS_ADD_DELAYED_REF
) {
2040 ret
= __btrfs_inc_extent_ref(trans
, root
, node
->bytenr
,
2041 node
->num_bytes
, parent
,
2042 ref_root
, ref
->objectid
,
2043 ref
->offset
, node
->ref_mod
,
2045 } else if (node
->action
== BTRFS_DROP_DELAYED_REF
) {
2046 ret
= __btrfs_free_extent(trans
, root
, node
->bytenr
,
2047 node
->num_bytes
, parent
,
2048 ref_root
, ref
->objectid
,
2049 ref
->offset
, node
->ref_mod
,
2057 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op
*extent_op
,
2058 struct extent_buffer
*leaf
,
2059 struct btrfs_extent_item
*ei
)
2061 u64 flags
= btrfs_extent_flags(leaf
, ei
);
2062 if (extent_op
->update_flags
) {
2063 flags
|= extent_op
->flags_to_set
;
2064 btrfs_set_extent_flags(leaf
, ei
, flags
);
2067 if (extent_op
->update_key
) {
2068 struct btrfs_tree_block_info
*bi
;
2069 BUG_ON(!(flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
));
2070 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
2071 btrfs_set_tree_block_key(leaf
, bi
, &extent_op
->key
);
2075 static int run_delayed_extent_op(struct btrfs_trans_handle
*trans
,
2076 struct btrfs_root
*root
,
2077 struct btrfs_delayed_ref_node
*node
,
2078 struct btrfs_delayed_extent_op
*extent_op
)
2080 struct btrfs_key key
;
2081 struct btrfs_path
*path
;
2082 struct btrfs_extent_item
*ei
;
2083 struct extent_buffer
*leaf
;
2087 int metadata
= !extent_op
->is_data
;
2092 if (metadata
&& !btrfs_fs_incompat(root
->fs_info
, SKINNY_METADATA
))
2095 path
= btrfs_alloc_path();
2099 key
.objectid
= node
->bytenr
;
2102 key
.type
= BTRFS_METADATA_ITEM_KEY
;
2103 key
.offset
= extent_op
->level
;
2105 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2106 key
.offset
= node
->num_bytes
;
2111 path
->leave_spinning
= 1;
2112 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
,
2120 btrfs_release_path(path
);
2123 key
.offset
= node
->num_bytes
;
2124 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2131 leaf
= path
->nodes
[0];
2132 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2133 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2134 if (item_size
< sizeof(*ei
)) {
2135 ret
= convert_extent_item_v0(trans
, root
->fs_info
->extent_root
,
2141 leaf
= path
->nodes
[0];
2142 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2145 BUG_ON(item_size
< sizeof(*ei
));
2146 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
2147 __run_delayed_extent_op(extent_op
, leaf
, ei
);
2149 btrfs_mark_buffer_dirty(leaf
);
2151 btrfs_free_path(path
);
2155 static int run_delayed_tree_ref(struct btrfs_trans_handle
*trans
,
2156 struct btrfs_root
*root
,
2157 struct btrfs_delayed_ref_node
*node
,
2158 struct btrfs_delayed_extent_op
*extent_op
,
2159 int insert_reserved
)
2162 struct btrfs_delayed_tree_ref
*ref
;
2163 struct btrfs_key ins
;
2166 bool skinny_metadata
= btrfs_fs_incompat(root
->fs_info
,
2169 ref
= btrfs_delayed_node_to_tree_ref(node
);
2170 if (node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
)
2171 parent
= ref
->parent
;
2173 ref_root
= ref
->root
;
2175 ins
.objectid
= node
->bytenr
;
2176 if (skinny_metadata
) {
2177 ins
.offset
= ref
->level
;
2178 ins
.type
= BTRFS_METADATA_ITEM_KEY
;
2180 ins
.offset
= node
->num_bytes
;
2181 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
2184 BUG_ON(node
->ref_mod
!= 1);
2185 if (node
->action
== BTRFS_ADD_DELAYED_REF
&& insert_reserved
) {
2186 BUG_ON(!extent_op
|| !extent_op
->update_flags
);
2187 ret
= alloc_reserved_tree_block(trans
, root
,
2189 extent_op
->flags_to_set
,
2192 } else if (node
->action
== BTRFS_ADD_DELAYED_REF
) {
2193 ret
= __btrfs_inc_extent_ref(trans
, root
, node
->bytenr
,
2194 node
->num_bytes
, parent
, ref_root
,
2195 ref
->level
, 0, 1, extent_op
);
2196 } else if (node
->action
== BTRFS_DROP_DELAYED_REF
) {
2197 ret
= __btrfs_free_extent(trans
, root
, node
->bytenr
,
2198 node
->num_bytes
, parent
, ref_root
,
2199 ref
->level
, 0, 1, extent_op
);
2206 /* helper function to actually process a single delayed ref entry */
2207 static int run_one_delayed_ref(struct btrfs_trans_handle
*trans
,
2208 struct btrfs_root
*root
,
2209 struct btrfs_delayed_ref_node
*node
,
2210 struct btrfs_delayed_extent_op
*extent_op
,
2211 int insert_reserved
)
2218 if (btrfs_delayed_ref_is_head(node
)) {
2219 struct btrfs_delayed_ref_head
*head
;
2221 * we've hit the end of the chain and we were supposed
2222 * to insert this extent into the tree. But, it got
2223 * deleted before we ever needed to insert it, so all
2224 * we have to do is clean up the accounting
2227 head
= btrfs_delayed_node_to_head(node
);
2228 if (insert_reserved
) {
2229 btrfs_pin_extent(root
, node
->bytenr
,
2230 node
->num_bytes
, 1);
2231 if (head
->is_data
) {
2232 ret
= btrfs_del_csums(trans
, root
,
2240 if (node
->type
== BTRFS_TREE_BLOCK_REF_KEY
||
2241 node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
)
2242 ret
= run_delayed_tree_ref(trans
, root
, node
, extent_op
,
2244 else if (node
->type
== BTRFS_EXTENT_DATA_REF_KEY
||
2245 node
->type
== BTRFS_SHARED_DATA_REF_KEY
)
2246 ret
= run_delayed_data_ref(trans
, root
, node
, extent_op
,
2253 static noinline
struct btrfs_delayed_ref_node
*
2254 select_delayed_ref(struct btrfs_delayed_ref_head
*head
)
2256 struct rb_node
*node
;
2257 struct btrfs_delayed_ref_node
*ref
;
2258 int action
= BTRFS_ADD_DELAYED_REF
;
2261 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2262 * this prevents ref count from going down to zero when
2263 * there still are pending delayed ref.
2265 node
= rb_prev(&head
->node
.rb_node
);
2269 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
,
2271 if (ref
->bytenr
!= head
->node
.bytenr
)
2273 if (ref
->action
== action
)
2275 node
= rb_prev(node
);
2277 if (action
== BTRFS_ADD_DELAYED_REF
) {
2278 action
= BTRFS_DROP_DELAYED_REF
;
2285 * Returns 0 on success or if called with an already aborted transaction.
2286 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2288 static noinline
int run_clustered_refs(struct btrfs_trans_handle
*trans
,
2289 struct btrfs_root
*root
,
2290 struct list_head
*cluster
)
2292 struct btrfs_delayed_ref_root
*delayed_refs
;
2293 struct btrfs_delayed_ref_node
*ref
;
2294 struct btrfs_delayed_ref_head
*locked_ref
= NULL
;
2295 struct btrfs_delayed_extent_op
*extent_op
;
2296 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2299 int must_insert_reserved
= 0;
2301 delayed_refs
= &trans
->transaction
->delayed_refs
;
2304 /* pick a new head ref from the cluster list */
2305 if (list_empty(cluster
))
2308 locked_ref
= list_entry(cluster
->next
,
2309 struct btrfs_delayed_ref_head
, cluster
);
2311 /* grab the lock that says we are going to process
2312 * all the refs for this head */
2313 ret
= btrfs_delayed_ref_lock(trans
, locked_ref
);
2316 * we may have dropped the spin lock to get the head
2317 * mutex lock, and that might have given someone else
2318 * time to free the head. If that's true, it has been
2319 * removed from our list and we can move on.
2321 if (ret
== -EAGAIN
) {
2329 * We need to try and merge add/drops of the same ref since we
2330 * can run into issues with relocate dropping the implicit ref
2331 * and then it being added back again before the drop can
2332 * finish. If we merged anything we need to re-loop so we can
2335 btrfs_merge_delayed_refs(trans
, fs_info
, delayed_refs
,
2339 * locked_ref is the head node, so we have to go one
2340 * node back for any delayed ref updates
2342 ref
= select_delayed_ref(locked_ref
);
2344 if (ref
&& ref
->seq
&&
2345 btrfs_check_delayed_seq(fs_info
, delayed_refs
, ref
->seq
)) {
2347 * there are still refs with lower seq numbers in the
2348 * process of being added. Don't run this ref yet.
2350 list_del_init(&locked_ref
->cluster
);
2351 btrfs_delayed_ref_unlock(locked_ref
);
2353 delayed_refs
->num_heads_ready
++;
2354 spin_unlock(&delayed_refs
->lock
);
2356 spin_lock(&delayed_refs
->lock
);
2361 * record the must insert reserved flag before we
2362 * drop the spin lock.
2364 must_insert_reserved
= locked_ref
->must_insert_reserved
;
2365 locked_ref
->must_insert_reserved
= 0;
2367 extent_op
= locked_ref
->extent_op
;
2368 locked_ref
->extent_op
= NULL
;
2371 /* All delayed refs have been processed, Go ahead
2372 * and send the head node to run_one_delayed_ref,
2373 * so that any accounting fixes can happen
2375 ref
= &locked_ref
->node
;
2377 if (extent_op
&& must_insert_reserved
) {
2378 btrfs_free_delayed_extent_op(extent_op
);
2383 spin_unlock(&delayed_refs
->lock
);
2385 ret
= run_delayed_extent_op(trans
, root
,
2387 btrfs_free_delayed_extent_op(extent_op
);
2390 btrfs_debug(fs_info
, "run_delayed_extent_op returned %d", ret
);
2391 spin_lock(&delayed_refs
->lock
);
2392 btrfs_delayed_ref_unlock(locked_ref
);
2401 rb_erase(&ref
->rb_node
, &delayed_refs
->root
);
2402 delayed_refs
->num_entries
--;
2403 if (!btrfs_delayed_ref_is_head(ref
)) {
2405 * when we play the delayed ref, also correct the
2408 switch (ref
->action
) {
2409 case BTRFS_ADD_DELAYED_REF
:
2410 case BTRFS_ADD_DELAYED_EXTENT
:
2411 locked_ref
->node
.ref_mod
-= ref
->ref_mod
;
2413 case BTRFS_DROP_DELAYED_REF
:
2414 locked_ref
->node
.ref_mod
+= ref
->ref_mod
;
2420 spin_unlock(&delayed_refs
->lock
);
2422 ret
= run_one_delayed_ref(trans
, root
, ref
, extent_op
,
2423 must_insert_reserved
);
2425 btrfs_free_delayed_extent_op(extent_op
);
2427 btrfs_delayed_ref_unlock(locked_ref
);
2428 btrfs_put_delayed_ref(ref
);
2429 btrfs_debug(fs_info
, "run_one_delayed_ref returned %d", ret
);
2430 spin_lock(&delayed_refs
->lock
);
2435 * If this node is a head, that means all the refs in this head
2436 * have been dealt with, and we will pick the next head to deal
2437 * with, so we must unlock the head and drop it from the cluster
2438 * list before we release it.
2440 if (btrfs_delayed_ref_is_head(ref
)) {
2441 list_del_init(&locked_ref
->cluster
);
2442 btrfs_delayed_ref_unlock(locked_ref
);
2445 btrfs_put_delayed_ref(ref
);
2449 spin_lock(&delayed_refs
->lock
);
2454 #ifdef SCRAMBLE_DELAYED_REFS
2456 * Normally delayed refs get processed in ascending bytenr order. This
2457 * correlates in most cases to the order added. To expose dependencies on this
2458 * order, we start to process the tree in the middle instead of the beginning
2460 static u64
find_middle(struct rb_root
*root
)
2462 struct rb_node
*n
= root
->rb_node
;
2463 struct btrfs_delayed_ref_node
*entry
;
2466 u64 first
= 0, last
= 0;
2470 entry
= rb_entry(n
, struct btrfs_delayed_ref_node
, rb_node
);
2471 first
= entry
->bytenr
;
2475 entry
= rb_entry(n
, struct btrfs_delayed_ref_node
, rb_node
);
2476 last
= entry
->bytenr
;
2481 entry
= rb_entry(n
, struct btrfs_delayed_ref_node
, rb_node
);
2482 WARN_ON(!entry
->in_tree
);
2484 middle
= entry
->bytenr
;
2497 int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle
*trans
,
2498 struct btrfs_fs_info
*fs_info
)
2500 struct qgroup_update
*qgroup_update
;
2503 if (list_empty(&trans
->qgroup_ref_list
) !=
2504 !trans
->delayed_ref_elem
.seq
) {
2505 /* list without seq or seq without list */
2507 "qgroup accounting update error, list is%s empty, seq is %#x.%x",
2508 list_empty(&trans
->qgroup_ref_list
) ? "" : " not",
2509 (u32
)(trans
->delayed_ref_elem
.seq
>> 32),
2510 (u32
)trans
->delayed_ref_elem
.seq
);
2514 if (!trans
->delayed_ref_elem
.seq
)
2517 while (!list_empty(&trans
->qgroup_ref_list
)) {
2518 qgroup_update
= list_first_entry(&trans
->qgroup_ref_list
,
2519 struct qgroup_update
, list
);
2520 list_del(&qgroup_update
->list
);
2522 ret
= btrfs_qgroup_account_ref(
2523 trans
, fs_info
, qgroup_update
->node
,
2524 qgroup_update
->extent_op
);
2525 kfree(qgroup_update
);
2528 btrfs_put_tree_mod_seq(fs_info
, &trans
->delayed_ref_elem
);
2533 static int refs_newer(struct btrfs_delayed_ref_root
*delayed_refs
, int seq
,
2536 int val
= atomic_read(&delayed_refs
->ref_seq
);
2538 if (val
< seq
|| val
>= seq
+ count
)
2543 static inline u64
heads_to_leaves(struct btrfs_root
*root
, u64 heads
)
2547 num_bytes
= heads
* (sizeof(struct btrfs_extent_item
) +
2548 sizeof(struct btrfs_extent_inline_ref
));
2549 if (!btrfs_fs_incompat(root
->fs_info
, SKINNY_METADATA
))
2550 num_bytes
+= heads
* sizeof(struct btrfs_tree_block_info
);
2553 * We don't ever fill up leaves all the way so multiply by 2 just to be
2554 * closer to what we're really going to want to ouse.
2556 return div64_u64(num_bytes
, BTRFS_LEAF_DATA_SIZE(root
));
2559 int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle
*trans
,
2560 struct btrfs_root
*root
)
2562 struct btrfs_block_rsv
*global_rsv
;
2563 u64 num_heads
= trans
->transaction
->delayed_refs
.num_heads_ready
;
2567 num_bytes
= btrfs_calc_trans_metadata_size(root
, 1);
2568 num_heads
= heads_to_leaves(root
, num_heads
);
2570 num_bytes
+= (num_heads
- 1) * root
->leafsize
;
2572 global_rsv
= &root
->fs_info
->global_block_rsv
;
2575 * If we can't allocate any more chunks lets make sure we have _lots_ of
2576 * wiggle room since running delayed refs can create more delayed refs.
2578 if (global_rsv
->space_info
->full
)
2581 spin_lock(&global_rsv
->lock
);
2582 if (global_rsv
->reserved
<= num_bytes
)
2584 spin_unlock(&global_rsv
->lock
);
2589 * this starts processing the delayed reference count updates and
2590 * extent insertions we have queued up so far. count can be
2591 * 0, which means to process everything in the tree at the start
2592 * of the run (but not newly added entries), or it can be some target
2593 * number you'd like to process.
2595 * Returns 0 on success or if called with an aborted transaction
2596 * Returns <0 on error and aborts the transaction
2598 int btrfs_run_delayed_refs(struct btrfs_trans_handle
*trans
,
2599 struct btrfs_root
*root
, unsigned long count
)
2601 struct rb_node
*node
;
2602 struct btrfs_delayed_ref_root
*delayed_refs
;
2603 struct btrfs_delayed_ref_node
*ref
;
2604 struct list_head cluster
;
2607 int run_all
= count
== (unsigned long)-1;
2611 /* We'll clean this up in btrfs_cleanup_transaction */
2615 if (root
== root
->fs_info
->extent_root
)
2616 root
= root
->fs_info
->tree_root
;
2618 btrfs_delayed_refs_qgroup_accounting(trans
, root
->fs_info
);
2620 delayed_refs
= &trans
->transaction
->delayed_refs
;
2621 INIT_LIST_HEAD(&cluster
);
2623 count
= delayed_refs
->num_entries
* 2;
2627 if (!run_all
&& !run_most
) {
2629 int seq
= atomic_read(&delayed_refs
->ref_seq
);
2632 old
= atomic_cmpxchg(&delayed_refs
->procs_running_refs
, 0, 1);
2634 DEFINE_WAIT(__wait
);
2635 if (delayed_refs
->flushing
||
2636 !btrfs_should_throttle_delayed_refs(trans
, root
))
2639 prepare_to_wait(&delayed_refs
->wait
, &__wait
,
2640 TASK_UNINTERRUPTIBLE
);
2642 old
= atomic_cmpxchg(&delayed_refs
->procs_running_refs
, 0, 1);
2645 finish_wait(&delayed_refs
->wait
, &__wait
);
2647 if (!refs_newer(delayed_refs
, seq
, 256))
2652 finish_wait(&delayed_refs
->wait
, &__wait
);
2658 atomic_inc(&delayed_refs
->procs_running_refs
);
2663 spin_lock(&delayed_refs
->lock
);
2665 #ifdef SCRAMBLE_DELAYED_REFS
2666 delayed_refs
->run_delayed_start
= find_middle(&delayed_refs
->root
);
2670 if (!(run_all
|| run_most
) &&
2671 !btrfs_should_throttle_delayed_refs(trans
, root
))
2675 * go find something we can process in the rbtree. We start at
2676 * the beginning of the tree, and then build a cluster
2677 * of refs to process starting at the first one we are able to
2680 delayed_start
= delayed_refs
->run_delayed_start
;
2681 ret
= btrfs_find_ref_cluster(trans
, &cluster
,
2682 delayed_refs
->run_delayed_start
);
2686 ret
= run_clustered_refs(trans
, root
, &cluster
);
2688 btrfs_release_ref_cluster(&cluster
);
2689 spin_unlock(&delayed_refs
->lock
);
2690 btrfs_abort_transaction(trans
, root
, ret
);
2691 atomic_dec(&delayed_refs
->procs_running_refs
);
2692 wake_up(&delayed_refs
->wait
);
2696 atomic_add(ret
, &delayed_refs
->ref_seq
);
2698 count
-= min_t(unsigned long, ret
, count
);
2703 if (delayed_start
>= delayed_refs
->run_delayed_start
) {
2706 * btrfs_find_ref_cluster looped. let's do one
2707 * more cycle. if we don't run any delayed ref
2708 * during that cycle (because we can't because
2709 * all of them are blocked), bail out.
2714 * no runnable refs left, stop trying
2721 /* refs were run, let's reset staleness detection */
2727 if (!list_empty(&trans
->new_bgs
)) {
2728 spin_unlock(&delayed_refs
->lock
);
2729 btrfs_create_pending_block_groups(trans
, root
);
2730 spin_lock(&delayed_refs
->lock
);
2733 node
= rb_first(&delayed_refs
->root
);
2736 count
= (unsigned long)-1;
2739 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
,
2741 if (btrfs_delayed_ref_is_head(ref
)) {
2742 struct btrfs_delayed_ref_head
*head
;
2744 head
= btrfs_delayed_node_to_head(ref
);
2745 atomic_inc(&ref
->refs
);
2747 spin_unlock(&delayed_refs
->lock
);
2749 * Mutex was contended, block until it's
2750 * released and try again
2752 mutex_lock(&head
->mutex
);
2753 mutex_unlock(&head
->mutex
);
2755 btrfs_put_delayed_ref(ref
);
2759 node
= rb_next(node
);
2761 spin_unlock(&delayed_refs
->lock
);
2762 schedule_timeout(1);
2766 atomic_dec(&delayed_refs
->procs_running_refs
);
2768 if (waitqueue_active(&delayed_refs
->wait
))
2769 wake_up(&delayed_refs
->wait
);
2771 spin_unlock(&delayed_refs
->lock
);
2772 assert_qgroups_uptodate(trans
);
2776 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle
*trans
,
2777 struct btrfs_root
*root
,
2778 u64 bytenr
, u64 num_bytes
, u64 flags
,
2779 int level
, int is_data
)
2781 struct btrfs_delayed_extent_op
*extent_op
;
2784 extent_op
= btrfs_alloc_delayed_extent_op();
2788 extent_op
->flags_to_set
= flags
;
2789 extent_op
->update_flags
= 1;
2790 extent_op
->update_key
= 0;
2791 extent_op
->is_data
= is_data
? 1 : 0;
2792 extent_op
->level
= level
;
2794 ret
= btrfs_add_delayed_extent_op(root
->fs_info
, trans
, bytenr
,
2795 num_bytes
, extent_op
);
2797 btrfs_free_delayed_extent_op(extent_op
);
2801 static noinline
int check_delayed_ref(struct btrfs_trans_handle
*trans
,
2802 struct btrfs_root
*root
,
2803 struct btrfs_path
*path
,
2804 u64 objectid
, u64 offset
, u64 bytenr
)
2806 struct btrfs_delayed_ref_head
*head
;
2807 struct btrfs_delayed_ref_node
*ref
;
2808 struct btrfs_delayed_data_ref
*data_ref
;
2809 struct btrfs_delayed_ref_root
*delayed_refs
;
2810 struct rb_node
*node
;
2814 delayed_refs
= &trans
->transaction
->delayed_refs
;
2815 spin_lock(&delayed_refs
->lock
);
2816 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
2820 if (!mutex_trylock(&head
->mutex
)) {
2821 atomic_inc(&head
->node
.refs
);
2822 spin_unlock(&delayed_refs
->lock
);
2824 btrfs_release_path(path
);
2827 * Mutex was contended, block until it's released and let
2830 mutex_lock(&head
->mutex
);
2831 mutex_unlock(&head
->mutex
);
2832 btrfs_put_delayed_ref(&head
->node
);
2836 node
= rb_prev(&head
->node
.rb_node
);
2840 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
2842 if (ref
->bytenr
!= bytenr
)
2846 if (ref
->type
!= BTRFS_EXTENT_DATA_REF_KEY
)
2849 data_ref
= btrfs_delayed_node_to_data_ref(ref
);
2851 node
= rb_prev(node
);
2855 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
2856 if (ref
->bytenr
== bytenr
&& ref
->seq
== seq
)
2860 if (data_ref
->root
!= root
->root_key
.objectid
||
2861 data_ref
->objectid
!= objectid
|| data_ref
->offset
!= offset
)
2866 mutex_unlock(&head
->mutex
);
2868 spin_unlock(&delayed_refs
->lock
);
2872 static noinline
int check_committed_ref(struct btrfs_trans_handle
*trans
,
2873 struct btrfs_root
*root
,
2874 struct btrfs_path
*path
,
2875 u64 objectid
, u64 offset
, u64 bytenr
)
2877 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
2878 struct extent_buffer
*leaf
;
2879 struct btrfs_extent_data_ref
*ref
;
2880 struct btrfs_extent_inline_ref
*iref
;
2881 struct btrfs_extent_item
*ei
;
2882 struct btrfs_key key
;
2886 key
.objectid
= bytenr
;
2887 key
.offset
= (u64
)-1;
2888 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2890 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
2893 BUG_ON(ret
== 0); /* Corruption */
2896 if (path
->slots
[0] == 0)
2900 leaf
= path
->nodes
[0];
2901 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2903 if (key
.objectid
!= bytenr
|| key
.type
!= BTRFS_EXTENT_ITEM_KEY
)
2907 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2908 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2909 if (item_size
< sizeof(*ei
)) {
2910 WARN_ON(item_size
!= sizeof(struct btrfs_extent_item_v0
));
2914 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
2916 if (item_size
!= sizeof(*ei
) +
2917 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY
))
2920 if (btrfs_extent_generation(leaf
, ei
) <=
2921 btrfs_root_last_snapshot(&root
->root_item
))
2924 iref
= (struct btrfs_extent_inline_ref
*)(ei
+ 1);
2925 if (btrfs_extent_inline_ref_type(leaf
, iref
) !=
2926 BTRFS_EXTENT_DATA_REF_KEY
)
2929 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
2930 if (btrfs_extent_refs(leaf
, ei
) !=
2931 btrfs_extent_data_ref_count(leaf
, ref
) ||
2932 btrfs_extent_data_ref_root(leaf
, ref
) !=
2933 root
->root_key
.objectid
||
2934 btrfs_extent_data_ref_objectid(leaf
, ref
) != objectid
||
2935 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
2943 int btrfs_cross_ref_exist(struct btrfs_trans_handle
*trans
,
2944 struct btrfs_root
*root
,
2945 u64 objectid
, u64 offset
, u64 bytenr
)
2947 struct btrfs_path
*path
;
2951 path
= btrfs_alloc_path();
2956 ret
= check_committed_ref(trans
, root
, path
, objectid
,
2958 if (ret
&& ret
!= -ENOENT
)
2961 ret2
= check_delayed_ref(trans
, root
, path
, objectid
,
2963 } while (ret2
== -EAGAIN
);
2965 if (ret2
&& ret2
!= -ENOENT
) {
2970 if (ret
!= -ENOENT
|| ret2
!= -ENOENT
)
2973 btrfs_free_path(path
);
2974 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
2979 static int __btrfs_mod_ref(struct btrfs_trans_handle
*trans
,
2980 struct btrfs_root
*root
,
2981 struct extent_buffer
*buf
,
2982 int full_backref
, int inc
, int for_cow
)
2989 struct btrfs_key key
;
2990 struct btrfs_file_extent_item
*fi
;
2994 int (*process_func
)(struct btrfs_trans_handle
*, struct btrfs_root
*,
2995 u64
, u64
, u64
, u64
, u64
, u64
, int);
2997 ref_root
= btrfs_header_owner(buf
);
2998 nritems
= btrfs_header_nritems(buf
);
2999 level
= btrfs_header_level(buf
);
3001 if (!root
->ref_cows
&& level
== 0)
3005 process_func
= btrfs_inc_extent_ref
;
3007 process_func
= btrfs_free_extent
;
3010 parent
= buf
->start
;
3014 for (i
= 0; i
< nritems
; i
++) {
3016 btrfs_item_key_to_cpu(buf
, &key
, i
);
3017 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
3019 fi
= btrfs_item_ptr(buf
, i
,
3020 struct btrfs_file_extent_item
);
3021 if (btrfs_file_extent_type(buf
, fi
) ==
3022 BTRFS_FILE_EXTENT_INLINE
)
3024 bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
3028 num_bytes
= btrfs_file_extent_disk_num_bytes(buf
, fi
);
3029 key
.offset
-= btrfs_file_extent_offset(buf
, fi
);
3030 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
3031 parent
, ref_root
, key
.objectid
,
3032 key
.offset
, for_cow
);
3036 bytenr
= btrfs_node_blockptr(buf
, i
);
3037 num_bytes
= btrfs_level_size(root
, level
- 1);
3038 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
3039 parent
, ref_root
, level
- 1, 0,
3050 int btrfs_inc_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
3051 struct extent_buffer
*buf
, int full_backref
, int for_cow
)
3053 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 1, for_cow
);
3056 int btrfs_dec_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
3057 struct extent_buffer
*buf
, int full_backref
, int for_cow
)
3059 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 0, for_cow
);
3062 static int write_one_cache_group(struct btrfs_trans_handle
*trans
,
3063 struct btrfs_root
*root
,
3064 struct btrfs_path
*path
,
3065 struct btrfs_block_group_cache
*cache
)
3068 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
3070 struct extent_buffer
*leaf
;
3072 ret
= btrfs_search_slot(trans
, extent_root
, &cache
->key
, path
, 0, 1);
3075 BUG_ON(ret
); /* Corruption */
3077 leaf
= path
->nodes
[0];
3078 bi
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
3079 write_extent_buffer(leaf
, &cache
->item
, bi
, sizeof(cache
->item
));
3080 btrfs_mark_buffer_dirty(leaf
);
3081 btrfs_release_path(path
);
3084 btrfs_abort_transaction(trans
, root
, ret
);
3091 static struct btrfs_block_group_cache
*
3092 next_block_group(struct btrfs_root
*root
,
3093 struct btrfs_block_group_cache
*cache
)
3095 struct rb_node
*node
;
3096 spin_lock(&root
->fs_info
->block_group_cache_lock
);
3097 node
= rb_next(&cache
->cache_node
);
3098 btrfs_put_block_group(cache
);
3100 cache
= rb_entry(node
, struct btrfs_block_group_cache
,
3102 btrfs_get_block_group(cache
);
3105 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
3109 static int cache_save_setup(struct btrfs_block_group_cache
*block_group
,
3110 struct btrfs_trans_handle
*trans
,
3111 struct btrfs_path
*path
)
3113 struct btrfs_root
*root
= block_group
->fs_info
->tree_root
;
3114 struct inode
*inode
= NULL
;
3116 int dcs
= BTRFS_DC_ERROR
;
3122 * If this block group is smaller than 100 megs don't bother caching the
3125 if (block_group
->key
.offset
< (100 * 1024 * 1024)) {
3126 spin_lock(&block_group
->lock
);
3127 block_group
->disk_cache_state
= BTRFS_DC_WRITTEN
;
3128 spin_unlock(&block_group
->lock
);
3133 inode
= lookup_free_space_inode(root
, block_group
, path
);
3134 if (IS_ERR(inode
) && PTR_ERR(inode
) != -ENOENT
) {
3135 ret
= PTR_ERR(inode
);
3136 btrfs_release_path(path
);
3140 if (IS_ERR(inode
)) {
3144 if (block_group
->ro
)
3147 ret
= create_free_space_inode(root
, trans
, block_group
, path
);
3153 /* We've already setup this transaction, go ahead and exit */
3154 if (block_group
->cache_generation
== trans
->transid
&&
3155 i_size_read(inode
)) {
3156 dcs
= BTRFS_DC_SETUP
;
3161 * We want to set the generation to 0, that way if anything goes wrong
3162 * from here on out we know not to trust this cache when we load up next
3165 BTRFS_I(inode
)->generation
= 0;
3166 ret
= btrfs_update_inode(trans
, root
, inode
);
3169 if (i_size_read(inode
) > 0) {
3170 ret
= btrfs_check_trunc_cache_free_space(root
,
3171 &root
->fs_info
->global_block_rsv
);
3175 ret
= btrfs_truncate_free_space_cache(root
, trans
, path
,
3181 spin_lock(&block_group
->lock
);
3182 if (block_group
->cached
!= BTRFS_CACHE_FINISHED
||
3183 !btrfs_test_opt(root
, SPACE_CACHE
)) {
3185 * don't bother trying to write stuff out _if_
3186 * a) we're not cached,
3187 * b) we're with nospace_cache mount option.
3189 dcs
= BTRFS_DC_WRITTEN
;
3190 spin_unlock(&block_group
->lock
);
3193 spin_unlock(&block_group
->lock
);
3196 * Try to preallocate enough space based on how big the block group is.
3197 * Keep in mind this has to include any pinned space which could end up
3198 * taking up quite a bit since it's not folded into the other space
3201 num_pages
= (int)div64_u64(block_group
->key
.offset
, 256 * 1024 * 1024);
3206 num_pages
*= PAGE_CACHE_SIZE
;
3208 ret
= btrfs_check_data_free_space(inode
, num_pages
);
3212 ret
= btrfs_prealloc_file_range_trans(inode
, trans
, 0, 0, num_pages
,
3213 num_pages
, num_pages
,
3216 dcs
= BTRFS_DC_SETUP
;
3217 btrfs_free_reserved_data_space(inode
, num_pages
);
3222 btrfs_release_path(path
);
3224 spin_lock(&block_group
->lock
);
3225 if (!ret
&& dcs
== BTRFS_DC_SETUP
)
3226 block_group
->cache_generation
= trans
->transid
;
3227 block_group
->disk_cache_state
= dcs
;
3228 spin_unlock(&block_group
->lock
);
3233 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle
*trans
,
3234 struct btrfs_root
*root
)
3236 struct btrfs_block_group_cache
*cache
;
3238 struct btrfs_path
*path
;
3241 path
= btrfs_alloc_path();
3247 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
3249 if (cache
->disk_cache_state
== BTRFS_DC_CLEAR
)
3251 cache
= next_block_group(root
, cache
);
3259 err
= cache_save_setup(cache
, trans
, path
);
3260 last
= cache
->key
.objectid
+ cache
->key
.offset
;
3261 btrfs_put_block_group(cache
);
3266 err
= btrfs_run_delayed_refs(trans
, root
,
3268 if (err
) /* File system offline */
3272 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
3274 if (cache
->disk_cache_state
== BTRFS_DC_CLEAR
) {
3275 btrfs_put_block_group(cache
);
3281 cache
= next_block_group(root
, cache
);
3290 if (cache
->disk_cache_state
== BTRFS_DC_SETUP
)
3291 cache
->disk_cache_state
= BTRFS_DC_NEED_WRITE
;
3293 last
= cache
->key
.objectid
+ cache
->key
.offset
;
3295 err
= write_one_cache_group(trans
, root
, path
, cache
);
3296 if (err
) /* File system offline */
3299 btrfs_put_block_group(cache
);
3304 * I don't think this is needed since we're just marking our
3305 * preallocated extent as written, but just in case it can't
3309 err
= btrfs_run_delayed_refs(trans
, root
,
3311 if (err
) /* File system offline */
3315 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
3318 * Really this shouldn't happen, but it could if we
3319 * couldn't write the entire preallocated extent and
3320 * splitting the extent resulted in a new block.
3323 btrfs_put_block_group(cache
);
3326 if (cache
->disk_cache_state
== BTRFS_DC_NEED_WRITE
)
3328 cache
= next_block_group(root
, cache
);
3337 err
= btrfs_write_out_cache(root
, trans
, cache
, path
);
3340 * If we didn't have an error then the cache state is still
3341 * NEED_WRITE, so we can set it to WRITTEN.
3343 if (!err
&& cache
->disk_cache_state
== BTRFS_DC_NEED_WRITE
)
3344 cache
->disk_cache_state
= BTRFS_DC_WRITTEN
;
3345 last
= cache
->key
.objectid
+ cache
->key
.offset
;
3346 btrfs_put_block_group(cache
);
3350 btrfs_free_path(path
);
3354 int btrfs_extent_readonly(struct btrfs_root
*root
, u64 bytenr
)
3356 struct btrfs_block_group_cache
*block_group
;
3359 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
3360 if (!block_group
|| block_group
->ro
)
3363 btrfs_put_block_group(block_group
);
3367 static int update_space_info(struct btrfs_fs_info
*info
, u64 flags
,
3368 u64 total_bytes
, u64 bytes_used
,
3369 struct btrfs_space_info
**space_info
)
3371 struct btrfs_space_info
*found
;
3376 if (flags
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
3377 BTRFS_BLOCK_GROUP_RAID10
))
3382 found
= __find_space_info(info
, flags
);
3384 spin_lock(&found
->lock
);
3385 found
->total_bytes
+= total_bytes
;
3386 found
->disk_total
+= total_bytes
* factor
;
3387 found
->bytes_used
+= bytes_used
;
3388 found
->disk_used
+= bytes_used
* factor
;
3390 spin_unlock(&found
->lock
);
3391 *space_info
= found
;
3394 found
= kzalloc(sizeof(*found
), GFP_NOFS
);
3398 ret
= percpu_counter_init(&found
->total_bytes_pinned
, 0);
3404 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++)
3405 INIT_LIST_HEAD(&found
->block_groups
[i
]);
3406 init_rwsem(&found
->groups_sem
);
3407 spin_lock_init(&found
->lock
);
3408 found
->flags
= flags
& BTRFS_BLOCK_GROUP_TYPE_MASK
;
3409 found
->total_bytes
= total_bytes
;
3410 found
->disk_total
= total_bytes
* factor
;
3411 found
->bytes_used
= bytes_used
;
3412 found
->disk_used
= bytes_used
* factor
;
3413 found
->bytes_pinned
= 0;
3414 found
->bytes_reserved
= 0;
3415 found
->bytes_readonly
= 0;
3416 found
->bytes_may_use
= 0;
3418 found
->force_alloc
= CHUNK_ALLOC_NO_FORCE
;
3419 found
->chunk_alloc
= 0;
3421 init_waitqueue_head(&found
->wait
);
3422 *space_info
= found
;
3423 list_add_rcu(&found
->list
, &info
->space_info
);
3424 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3425 info
->data_sinfo
= found
;
3429 static void set_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
3431 u64 extra_flags
= chunk_to_extended(flags
) &
3432 BTRFS_EXTENDED_PROFILE_MASK
;
3434 write_seqlock(&fs_info
->profiles_lock
);
3435 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3436 fs_info
->avail_data_alloc_bits
|= extra_flags
;
3437 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
3438 fs_info
->avail_metadata_alloc_bits
|= extra_flags
;
3439 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
3440 fs_info
->avail_system_alloc_bits
|= extra_flags
;
3441 write_sequnlock(&fs_info
->profiles_lock
);
3445 * returns target flags in extended format or 0 if restripe for this
3446 * chunk_type is not in progress
3448 * should be called with either volume_mutex or balance_lock held
3450 static u64
get_restripe_target(struct btrfs_fs_info
*fs_info
, u64 flags
)
3452 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3458 if (flags
& BTRFS_BLOCK_GROUP_DATA
&&
3459 bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3460 target
= BTRFS_BLOCK_GROUP_DATA
| bctl
->data
.target
;
3461 } else if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
&&
3462 bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3463 target
= BTRFS_BLOCK_GROUP_SYSTEM
| bctl
->sys
.target
;
3464 } else if (flags
& BTRFS_BLOCK_GROUP_METADATA
&&
3465 bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3466 target
= BTRFS_BLOCK_GROUP_METADATA
| bctl
->meta
.target
;
3473 * @flags: available profiles in extended format (see ctree.h)
3475 * Returns reduced profile in chunk format. If profile changing is in
3476 * progress (either running or paused) picks the target profile (if it's
3477 * already available), otherwise falls back to plain reducing.
3479 static u64
btrfs_reduce_alloc_profile(struct btrfs_root
*root
, u64 flags
)
3482 * we add in the count of missing devices because we want
3483 * to make sure that any RAID levels on a degraded FS
3484 * continue to be honored.
3486 u64 num_devices
= root
->fs_info
->fs_devices
->rw_devices
+
3487 root
->fs_info
->fs_devices
->missing_devices
;
3492 * see if restripe for this chunk_type is in progress, if so
3493 * try to reduce to the target profile
3495 spin_lock(&root
->fs_info
->balance_lock
);
3496 target
= get_restripe_target(root
->fs_info
, flags
);
3498 /* pick target profile only if it's already available */
3499 if ((flags
& target
) & BTRFS_EXTENDED_PROFILE_MASK
) {
3500 spin_unlock(&root
->fs_info
->balance_lock
);
3501 return extended_to_chunk(target
);
3504 spin_unlock(&root
->fs_info
->balance_lock
);
3506 /* First, mask out the RAID levels which aren't possible */
3507 if (num_devices
== 1)
3508 flags
&= ~(BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID0
|
3509 BTRFS_BLOCK_GROUP_RAID5
);
3510 if (num_devices
< 3)
3511 flags
&= ~BTRFS_BLOCK_GROUP_RAID6
;
3512 if (num_devices
< 4)
3513 flags
&= ~BTRFS_BLOCK_GROUP_RAID10
;
3515 tmp
= flags
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID0
|
3516 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID5
|
3517 BTRFS_BLOCK_GROUP_RAID6
| BTRFS_BLOCK_GROUP_RAID10
);
3520 if (tmp
& BTRFS_BLOCK_GROUP_RAID6
)
3521 tmp
= BTRFS_BLOCK_GROUP_RAID6
;
3522 else if (tmp
& BTRFS_BLOCK_GROUP_RAID5
)
3523 tmp
= BTRFS_BLOCK_GROUP_RAID5
;
3524 else if (tmp
& BTRFS_BLOCK_GROUP_RAID10
)
3525 tmp
= BTRFS_BLOCK_GROUP_RAID10
;
3526 else if (tmp
& BTRFS_BLOCK_GROUP_RAID1
)
3527 tmp
= BTRFS_BLOCK_GROUP_RAID1
;
3528 else if (tmp
& BTRFS_BLOCK_GROUP_RAID0
)
3529 tmp
= BTRFS_BLOCK_GROUP_RAID0
;
3531 return extended_to_chunk(flags
| tmp
);
3534 static u64
get_alloc_profile(struct btrfs_root
*root
, u64 flags
)
3539 seq
= read_seqbegin(&root
->fs_info
->profiles_lock
);
3541 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3542 flags
|= root
->fs_info
->avail_data_alloc_bits
;
3543 else if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
3544 flags
|= root
->fs_info
->avail_system_alloc_bits
;
3545 else if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
3546 flags
|= root
->fs_info
->avail_metadata_alloc_bits
;
3547 } while (read_seqretry(&root
->fs_info
->profiles_lock
, seq
));
3549 return btrfs_reduce_alloc_profile(root
, flags
);
3552 u64
btrfs_get_alloc_profile(struct btrfs_root
*root
, int data
)
3558 flags
= BTRFS_BLOCK_GROUP_DATA
;
3559 else if (root
== root
->fs_info
->chunk_root
)
3560 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
3562 flags
= BTRFS_BLOCK_GROUP_METADATA
;
3564 ret
= get_alloc_profile(root
, flags
);
3569 * This will check the space that the inode allocates from to make sure we have
3570 * enough space for bytes.
3572 int btrfs_check_data_free_space(struct inode
*inode
, u64 bytes
)
3574 struct btrfs_space_info
*data_sinfo
;
3575 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3576 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3578 int ret
= 0, committed
= 0, alloc_chunk
= 1;
3580 /* make sure bytes are sectorsize aligned */
3581 bytes
= ALIGN(bytes
, root
->sectorsize
);
3583 if (root
== root
->fs_info
->tree_root
||
3584 BTRFS_I(inode
)->location
.objectid
== BTRFS_FREE_INO_OBJECTID
) {
3589 data_sinfo
= fs_info
->data_sinfo
;
3594 /* make sure we have enough space to handle the data first */
3595 spin_lock(&data_sinfo
->lock
);
3596 used
= data_sinfo
->bytes_used
+ data_sinfo
->bytes_reserved
+
3597 data_sinfo
->bytes_pinned
+ data_sinfo
->bytes_readonly
+
3598 data_sinfo
->bytes_may_use
;
3600 if (used
+ bytes
> data_sinfo
->total_bytes
) {
3601 struct btrfs_trans_handle
*trans
;
3604 * if we don't have enough free bytes in this space then we need
3605 * to alloc a new chunk.
3607 if (!data_sinfo
->full
&& alloc_chunk
) {
3610 data_sinfo
->force_alloc
= CHUNK_ALLOC_FORCE
;
3611 spin_unlock(&data_sinfo
->lock
);
3613 alloc_target
= btrfs_get_alloc_profile(root
, 1);
3614 trans
= btrfs_join_transaction(root
);
3616 return PTR_ERR(trans
);
3618 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
3620 CHUNK_ALLOC_NO_FORCE
);
3621 btrfs_end_transaction(trans
, root
);
3630 data_sinfo
= fs_info
->data_sinfo
;
3636 * If we don't have enough pinned space to deal with this
3637 * allocation don't bother committing the transaction.
3639 if (percpu_counter_compare(&data_sinfo
->total_bytes_pinned
,
3642 spin_unlock(&data_sinfo
->lock
);
3644 /* commit the current transaction and try again */
3647 !atomic_read(&root
->fs_info
->open_ioctl_trans
)) {
3650 trans
= btrfs_join_transaction(root
);
3652 return PTR_ERR(trans
);
3653 ret
= btrfs_commit_transaction(trans
, root
);
3661 data_sinfo
->bytes_may_use
+= bytes
;
3662 trace_btrfs_space_reservation(root
->fs_info
, "space_info",
3663 data_sinfo
->flags
, bytes
, 1);
3664 spin_unlock(&data_sinfo
->lock
);
3670 * Called if we need to clear a data reservation for this inode.
3672 void btrfs_free_reserved_data_space(struct inode
*inode
, u64 bytes
)
3674 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3675 struct btrfs_space_info
*data_sinfo
;
3677 /* make sure bytes are sectorsize aligned */
3678 bytes
= ALIGN(bytes
, root
->sectorsize
);
3680 data_sinfo
= root
->fs_info
->data_sinfo
;
3681 spin_lock(&data_sinfo
->lock
);
3682 WARN_ON(data_sinfo
->bytes_may_use
< bytes
);
3683 data_sinfo
->bytes_may_use
-= bytes
;
3684 trace_btrfs_space_reservation(root
->fs_info
, "space_info",
3685 data_sinfo
->flags
, bytes
, 0);
3686 spin_unlock(&data_sinfo
->lock
);
3689 static void force_metadata_allocation(struct btrfs_fs_info
*info
)
3691 struct list_head
*head
= &info
->space_info
;
3692 struct btrfs_space_info
*found
;
3695 list_for_each_entry_rcu(found
, head
, list
) {
3696 if (found
->flags
& BTRFS_BLOCK_GROUP_METADATA
)
3697 found
->force_alloc
= CHUNK_ALLOC_FORCE
;
3702 static inline u64
calc_global_rsv_need_space(struct btrfs_block_rsv
*global
)
3704 return (global
->size
<< 1);
3707 static int should_alloc_chunk(struct btrfs_root
*root
,
3708 struct btrfs_space_info
*sinfo
, int force
)
3710 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
3711 u64 num_bytes
= sinfo
->total_bytes
- sinfo
->bytes_readonly
;
3712 u64 num_allocated
= sinfo
->bytes_used
+ sinfo
->bytes_reserved
;
3715 if (force
== CHUNK_ALLOC_FORCE
)
3719 * We need to take into account the global rsv because for all intents
3720 * and purposes it's used space. Don't worry about locking the
3721 * global_rsv, it doesn't change except when the transaction commits.
3723 if (sinfo
->flags
& BTRFS_BLOCK_GROUP_METADATA
)
3724 num_allocated
+= calc_global_rsv_need_space(global_rsv
);
3727 * in limited mode, we want to have some free space up to
3728 * about 1% of the FS size.
3730 if (force
== CHUNK_ALLOC_LIMITED
) {
3731 thresh
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
3732 thresh
= max_t(u64
, 64 * 1024 * 1024,
3733 div_factor_fine(thresh
, 1));
3735 if (num_bytes
- num_allocated
< thresh
)
3739 if (num_allocated
+ 2 * 1024 * 1024 < div_factor(num_bytes
, 8))
3744 static u64
get_system_chunk_thresh(struct btrfs_root
*root
, u64 type
)
3748 if (type
& (BTRFS_BLOCK_GROUP_RAID10
|
3749 BTRFS_BLOCK_GROUP_RAID0
|
3750 BTRFS_BLOCK_GROUP_RAID5
|
3751 BTRFS_BLOCK_GROUP_RAID6
))
3752 num_dev
= root
->fs_info
->fs_devices
->rw_devices
;
3753 else if (type
& BTRFS_BLOCK_GROUP_RAID1
)
3756 num_dev
= 1; /* DUP or single */
3758 /* metadata for updaing devices and chunk tree */
3759 return btrfs_calc_trans_metadata_size(root
, num_dev
+ 1);
3762 static void check_system_chunk(struct btrfs_trans_handle
*trans
,
3763 struct btrfs_root
*root
, u64 type
)
3765 struct btrfs_space_info
*info
;
3769 info
= __find_space_info(root
->fs_info
, BTRFS_BLOCK_GROUP_SYSTEM
);
3770 spin_lock(&info
->lock
);
3771 left
= info
->total_bytes
- info
->bytes_used
- info
->bytes_pinned
-
3772 info
->bytes_reserved
- info
->bytes_readonly
;
3773 spin_unlock(&info
->lock
);
3775 thresh
= get_system_chunk_thresh(root
, type
);
3776 if (left
< thresh
&& btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
3777 btrfs_info(root
->fs_info
, "left=%llu, need=%llu, flags=%llu",
3778 left
, thresh
, type
);
3779 dump_space_info(info
, 0, 0);
3782 if (left
< thresh
) {
3785 flags
= btrfs_get_alloc_profile(root
->fs_info
->chunk_root
, 0);
3786 btrfs_alloc_chunk(trans
, root
, flags
);
3790 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
3791 struct btrfs_root
*extent_root
, u64 flags
, int force
)
3793 struct btrfs_space_info
*space_info
;
3794 struct btrfs_fs_info
*fs_info
= extent_root
->fs_info
;
3795 int wait_for_alloc
= 0;
3798 /* Don't re-enter if we're already allocating a chunk */
3799 if (trans
->allocating_chunk
)
3802 space_info
= __find_space_info(extent_root
->fs_info
, flags
);
3804 ret
= update_space_info(extent_root
->fs_info
, flags
,
3806 BUG_ON(ret
); /* -ENOMEM */
3808 BUG_ON(!space_info
); /* Logic error */
3811 spin_lock(&space_info
->lock
);
3812 if (force
< space_info
->force_alloc
)
3813 force
= space_info
->force_alloc
;
3814 if (space_info
->full
) {
3815 spin_unlock(&space_info
->lock
);
3819 if (!should_alloc_chunk(extent_root
, space_info
, force
)) {
3820 spin_unlock(&space_info
->lock
);
3822 } else if (space_info
->chunk_alloc
) {
3825 space_info
->chunk_alloc
= 1;
3828 spin_unlock(&space_info
->lock
);
3830 mutex_lock(&fs_info
->chunk_mutex
);
3833 * The chunk_mutex is held throughout the entirety of a chunk
3834 * allocation, so once we've acquired the chunk_mutex we know that the
3835 * other guy is done and we need to recheck and see if we should
3838 if (wait_for_alloc
) {
3839 mutex_unlock(&fs_info
->chunk_mutex
);
3844 trans
->allocating_chunk
= true;
3847 * If we have mixed data/metadata chunks we want to make sure we keep
3848 * allocating mixed chunks instead of individual chunks.
3850 if (btrfs_mixed_space_info(space_info
))
3851 flags
|= (BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
);
3854 * if we're doing a data chunk, go ahead and make sure that
3855 * we keep a reasonable number of metadata chunks allocated in the
3858 if (flags
& BTRFS_BLOCK_GROUP_DATA
&& fs_info
->metadata_ratio
) {
3859 fs_info
->data_chunk_allocations
++;
3860 if (!(fs_info
->data_chunk_allocations
%
3861 fs_info
->metadata_ratio
))
3862 force_metadata_allocation(fs_info
);
3866 * Check if we have enough space in SYSTEM chunk because we may need
3867 * to update devices.
3869 check_system_chunk(trans
, extent_root
, flags
);
3871 ret
= btrfs_alloc_chunk(trans
, extent_root
, flags
);
3872 trans
->allocating_chunk
= false;
3874 spin_lock(&space_info
->lock
);
3875 if (ret
< 0 && ret
!= -ENOSPC
)
3878 space_info
->full
= 1;
3882 space_info
->force_alloc
= CHUNK_ALLOC_NO_FORCE
;
3884 space_info
->chunk_alloc
= 0;
3885 spin_unlock(&space_info
->lock
);
3886 mutex_unlock(&fs_info
->chunk_mutex
);
3890 static int can_overcommit(struct btrfs_root
*root
,
3891 struct btrfs_space_info
*space_info
, u64 bytes
,
3892 enum btrfs_reserve_flush_enum flush
)
3894 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
3895 u64 profile
= btrfs_get_alloc_profile(root
, 0);
3901 used
= space_info
->bytes_used
+ space_info
->bytes_reserved
+
3902 space_info
->bytes_pinned
+ space_info
->bytes_readonly
;
3905 * We only want to allow over committing if we have lots of actual space
3906 * free, but if we don't have enough space to handle the global reserve
3907 * space then we could end up having a real enospc problem when trying
3908 * to allocate a chunk or some other such important allocation.
3910 spin_lock(&global_rsv
->lock
);
3911 space_size
= calc_global_rsv_need_space(global_rsv
);
3912 spin_unlock(&global_rsv
->lock
);
3913 if (used
+ space_size
>= space_info
->total_bytes
)
3916 used
+= space_info
->bytes_may_use
;
3918 spin_lock(&root
->fs_info
->free_chunk_lock
);
3919 avail
= root
->fs_info
->free_chunk_space
;
3920 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3923 * If we have dup, raid1 or raid10 then only half of the free
3924 * space is actually useable. For raid56, the space info used
3925 * doesn't include the parity drive, so we don't have to
3928 if (profile
& (BTRFS_BLOCK_GROUP_DUP
|
3929 BTRFS_BLOCK_GROUP_RAID1
|
3930 BTRFS_BLOCK_GROUP_RAID10
))
3933 to_add
= space_info
->total_bytes
;
3936 * If we aren't flushing all things, let us overcommit up to
3937 * 1/2th of the space. If we can flush, don't let us overcommit
3938 * too much, let it overcommit up to 1/8 of the space.
3940 if (flush
== BTRFS_RESERVE_FLUSH_ALL
)
3946 * Limit the overcommit to the amount of free space we could possibly
3947 * allocate for chunks.
3949 to_add
= min(avail
, to_add
);
3951 if (used
+ bytes
< space_info
->total_bytes
+ to_add
)
3956 static void btrfs_writeback_inodes_sb_nr(struct btrfs_root
*root
,
3957 unsigned long nr_pages
)
3959 struct super_block
*sb
= root
->fs_info
->sb
;
3961 if (down_read_trylock(&sb
->s_umount
)) {
3962 writeback_inodes_sb_nr(sb
, nr_pages
, WB_REASON_FS_FREE_SPACE
);
3963 up_read(&sb
->s_umount
);
3966 * We needn't worry the filesystem going from r/w to r/o though
3967 * we don't acquire ->s_umount mutex, because the filesystem
3968 * should guarantee the delalloc inodes list be empty after
3969 * the filesystem is readonly(all dirty pages are written to
3972 btrfs_start_all_delalloc_inodes(root
->fs_info
, 0);
3973 if (!current
->journal_info
)
3974 btrfs_wait_all_ordered_extents(root
->fs_info
, 0);
3979 * shrink metadata reservation for delalloc
3981 static void shrink_delalloc(struct btrfs_root
*root
, u64 to_reclaim
, u64 orig
,
3984 struct btrfs_block_rsv
*block_rsv
;
3985 struct btrfs_space_info
*space_info
;
3986 struct btrfs_trans_handle
*trans
;
3990 unsigned long nr_pages
= (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT
;
3992 enum btrfs_reserve_flush_enum flush
;
3994 trans
= (struct btrfs_trans_handle
*)current
->journal_info
;
3995 block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
3996 space_info
= block_rsv
->space_info
;
3999 delalloc_bytes
= percpu_counter_sum_positive(
4000 &root
->fs_info
->delalloc_bytes
);
4001 if (delalloc_bytes
== 0) {
4004 btrfs_wait_all_ordered_extents(root
->fs_info
, 0);
4008 while (delalloc_bytes
&& loops
< 3) {
4009 max_reclaim
= min(delalloc_bytes
, to_reclaim
);
4010 nr_pages
= max_reclaim
>> PAGE_CACHE_SHIFT
;
4011 btrfs_writeback_inodes_sb_nr(root
, nr_pages
);
4013 * We need to wait for the async pages to actually start before
4016 wait_event(root
->fs_info
->async_submit_wait
,
4017 !atomic_read(&root
->fs_info
->async_delalloc_pages
));
4020 flush
= BTRFS_RESERVE_FLUSH_ALL
;
4022 flush
= BTRFS_RESERVE_NO_FLUSH
;
4023 spin_lock(&space_info
->lock
);
4024 if (can_overcommit(root
, space_info
, orig
, flush
)) {
4025 spin_unlock(&space_info
->lock
);
4028 spin_unlock(&space_info
->lock
);
4031 if (wait_ordered
&& !trans
) {
4032 btrfs_wait_all_ordered_extents(root
->fs_info
, 0);
4034 time_left
= schedule_timeout_killable(1);
4039 delalloc_bytes
= percpu_counter_sum_positive(
4040 &root
->fs_info
->delalloc_bytes
);
4045 * maybe_commit_transaction - possibly commit the transaction if its ok to
4046 * @root - the root we're allocating for
4047 * @bytes - the number of bytes we want to reserve
4048 * @force - force the commit
4050 * This will check to make sure that committing the transaction will actually
4051 * get us somewhere and then commit the transaction if it does. Otherwise it
4052 * will return -ENOSPC.
4054 static int may_commit_transaction(struct btrfs_root
*root
,
4055 struct btrfs_space_info
*space_info
,
4056 u64 bytes
, int force
)
4058 struct btrfs_block_rsv
*delayed_rsv
= &root
->fs_info
->delayed_block_rsv
;
4059 struct btrfs_trans_handle
*trans
;
4061 trans
= (struct btrfs_trans_handle
*)current
->journal_info
;
4068 /* See if there is enough pinned space to make this reservation */
4069 spin_lock(&space_info
->lock
);
4070 if (percpu_counter_compare(&space_info
->total_bytes_pinned
,
4072 spin_unlock(&space_info
->lock
);
4075 spin_unlock(&space_info
->lock
);
4078 * See if there is some space in the delayed insertion reservation for
4081 if (space_info
!= delayed_rsv
->space_info
)
4084 spin_lock(&space_info
->lock
);
4085 spin_lock(&delayed_rsv
->lock
);
4086 if (percpu_counter_compare(&space_info
->total_bytes_pinned
,
4087 bytes
- delayed_rsv
->size
) >= 0) {
4088 spin_unlock(&delayed_rsv
->lock
);
4089 spin_unlock(&space_info
->lock
);
4092 spin_unlock(&delayed_rsv
->lock
);
4093 spin_unlock(&space_info
->lock
);
4096 trans
= btrfs_join_transaction(root
);
4100 return btrfs_commit_transaction(trans
, root
);
4104 FLUSH_DELAYED_ITEMS_NR
= 1,
4105 FLUSH_DELAYED_ITEMS
= 2,
4107 FLUSH_DELALLOC_WAIT
= 4,
4112 static int flush_space(struct btrfs_root
*root
,
4113 struct btrfs_space_info
*space_info
, u64 num_bytes
,
4114 u64 orig_bytes
, int state
)
4116 struct btrfs_trans_handle
*trans
;
4121 case FLUSH_DELAYED_ITEMS_NR
:
4122 case FLUSH_DELAYED_ITEMS
:
4123 if (state
== FLUSH_DELAYED_ITEMS_NR
) {
4124 u64 bytes
= btrfs_calc_trans_metadata_size(root
, 1);
4126 nr
= (int)div64_u64(num_bytes
, bytes
);
4133 trans
= btrfs_join_transaction(root
);
4134 if (IS_ERR(trans
)) {
4135 ret
= PTR_ERR(trans
);
4138 ret
= btrfs_run_delayed_items_nr(trans
, root
, nr
);
4139 btrfs_end_transaction(trans
, root
);
4141 case FLUSH_DELALLOC
:
4142 case FLUSH_DELALLOC_WAIT
:
4143 shrink_delalloc(root
, num_bytes
, orig_bytes
,
4144 state
== FLUSH_DELALLOC_WAIT
);
4147 trans
= btrfs_join_transaction(root
);
4148 if (IS_ERR(trans
)) {
4149 ret
= PTR_ERR(trans
);
4152 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
4153 btrfs_get_alloc_profile(root
, 0),
4154 CHUNK_ALLOC_NO_FORCE
);
4155 btrfs_end_transaction(trans
, root
);
4160 ret
= may_commit_transaction(root
, space_info
, orig_bytes
, 0);
4170 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
4171 * @root - the root we're allocating for
4172 * @block_rsv - the block_rsv we're allocating for
4173 * @orig_bytes - the number of bytes we want
4174 * @flush - whether or not we can flush to make our reservation
4176 * This will reserve orgi_bytes number of bytes from the space info associated
4177 * with the block_rsv. If there is not enough space it will make an attempt to
4178 * flush out space to make room. It will do this by flushing delalloc if
4179 * possible or committing the transaction. If flush is 0 then no attempts to
4180 * regain reservations will be made and this will fail if there is not enough
4183 static int reserve_metadata_bytes(struct btrfs_root
*root
,
4184 struct btrfs_block_rsv
*block_rsv
,
4186 enum btrfs_reserve_flush_enum flush
)
4188 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
4190 u64 num_bytes
= orig_bytes
;
4191 int flush_state
= FLUSH_DELAYED_ITEMS_NR
;
4193 bool flushing
= false;
4197 spin_lock(&space_info
->lock
);
4199 * We only want to wait if somebody other than us is flushing and we
4200 * are actually allowed to flush all things.
4202 while (flush
== BTRFS_RESERVE_FLUSH_ALL
&& !flushing
&&
4203 space_info
->flush
) {
4204 spin_unlock(&space_info
->lock
);
4206 * If we have a trans handle we can't wait because the flusher
4207 * may have to commit the transaction, which would mean we would
4208 * deadlock since we are waiting for the flusher to finish, but
4209 * hold the current transaction open.
4211 if (current
->journal_info
)
4213 ret
= wait_event_killable(space_info
->wait
, !space_info
->flush
);
4214 /* Must have been killed, return */
4218 spin_lock(&space_info
->lock
);
4222 used
= space_info
->bytes_used
+ space_info
->bytes_reserved
+
4223 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
4224 space_info
->bytes_may_use
;
4227 * The idea here is that we've not already over-reserved the block group
4228 * then we can go ahead and save our reservation first and then start
4229 * flushing if we need to. Otherwise if we've already overcommitted
4230 * lets start flushing stuff first and then come back and try to make
4233 if (used
<= space_info
->total_bytes
) {
4234 if (used
+ orig_bytes
<= space_info
->total_bytes
) {
4235 space_info
->bytes_may_use
+= orig_bytes
;
4236 trace_btrfs_space_reservation(root
->fs_info
,
4237 "space_info", space_info
->flags
, orig_bytes
, 1);
4241 * Ok set num_bytes to orig_bytes since we aren't
4242 * overocmmitted, this way we only try and reclaim what
4245 num_bytes
= orig_bytes
;
4249 * Ok we're over committed, set num_bytes to the overcommitted
4250 * amount plus the amount of bytes that we need for this
4253 num_bytes
= used
- space_info
->total_bytes
+
4257 if (ret
&& can_overcommit(root
, space_info
, orig_bytes
, flush
)) {
4258 space_info
->bytes_may_use
+= orig_bytes
;
4259 trace_btrfs_space_reservation(root
->fs_info
, "space_info",
4260 space_info
->flags
, orig_bytes
,
4266 * Couldn't make our reservation, save our place so while we're trying
4267 * to reclaim space we can actually use it instead of somebody else
4268 * stealing it from us.
4270 * We make the other tasks wait for the flush only when we can flush
4273 if (ret
&& flush
!= BTRFS_RESERVE_NO_FLUSH
) {
4275 space_info
->flush
= 1;
4278 spin_unlock(&space_info
->lock
);
4280 if (!ret
|| flush
== BTRFS_RESERVE_NO_FLUSH
)
4283 ret
= flush_space(root
, space_info
, num_bytes
, orig_bytes
,
4288 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4289 * would happen. So skip delalloc flush.
4291 if (flush
== BTRFS_RESERVE_FLUSH_LIMIT
&&
4292 (flush_state
== FLUSH_DELALLOC
||
4293 flush_state
== FLUSH_DELALLOC_WAIT
))
4294 flush_state
= ALLOC_CHUNK
;
4298 else if (flush
== BTRFS_RESERVE_FLUSH_LIMIT
&&
4299 flush_state
< COMMIT_TRANS
)
4301 else if (flush
== BTRFS_RESERVE_FLUSH_ALL
&&
4302 flush_state
<= COMMIT_TRANS
)
4306 if (ret
== -ENOSPC
&&
4307 unlikely(root
->orphan_cleanup_state
== ORPHAN_CLEANUP_STARTED
)) {
4308 struct btrfs_block_rsv
*global_rsv
=
4309 &root
->fs_info
->global_block_rsv
;
4311 if (block_rsv
!= global_rsv
&&
4312 !block_rsv_use_bytes(global_rsv
, orig_bytes
))
4316 spin_lock(&space_info
->lock
);
4317 space_info
->flush
= 0;
4318 wake_up_all(&space_info
->wait
);
4319 spin_unlock(&space_info
->lock
);
4324 static struct btrfs_block_rsv
*get_block_rsv(
4325 const struct btrfs_trans_handle
*trans
,
4326 const struct btrfs_root
*root
)
4328 struct btrfs_block_rsv
*block_rsv
= NULL
;
4331 block_rsv
= trans
->block_rsv
;
4333 if (root
== root
->fs_info
->csum_root
&& trans
->adding_csums
)
4334 block_rsv
= trans
->block_rsv
;
4337 block_rsv
= root
->block_rsv
;
4340 block_rsv
= &root
->fs_info
->empty_block_rsv
;
4345 static int block_rsv_use_bytes(struct btrfs_block_rsv
*block_rsv
,
4349 spin_lock(&block_rsv
->lock
);
4350 if (block_rsv
->reserved
>= num_bytes
) {
4351 block_rsv
->reserved
-= num_bytes
;
4352 if (block_rsv
->reserved
< block_rsv
->size
)
4353 block_rsv
->full
= 0;
4356 spin_unlock(&block_rsv
->lock
);
4360 static void block_rsv_add_bytes(struct btrfs_block_rsv
*block_rsv
,
4361 u64 num_bytes
, int update_size
)
4363 spin_lock(&block_rsv
->lock
);
4364 block_rsv
->reserved
+= num_bytes
;
4366 block_rsv
->size
+= num_bytes
;
4367 else if (block_rsv
->reserved
>= block_rsv
->size
)
4368 block_rsv
->full
= 1;
4369 spin_unlock(&block_rsv
->lock
);
4372 int btrfs_cond_migrate_bytes(struct btrfs_fs_info
*fs_info
,
4373 struct btrfs_block_rsv
*dest
, u64 num_bytes
,
4376 struct btrfs_block_rsv
*global_rsv
= &fs_info
->global_block_rsv
;
4379 if (global_rsv
->space_info
!= dest
->space_info
)
4382 spin_lock(&global_rsv
->lock
);
4383 min_bytes
= div_factor(global_rsv
->size
, min_factor
);
4384 if (global_rsv
->reserved
< min_bytes
+ num_bytes
) {
4385 spin_unlock(&global_rsv
->lock
);
4388 global_rsv
->reserved
-= num_bytes
;
4389 if (global_rsv
->reserved
< global_rsv
->size
)
4390 global_rsv
->full
= 0;
4391 spin_unlock(&global_rsv
->lock
);
4393 block_rsv_add_bytes(dest
, num_bytes
, 1);
4397 static void block_rsv_release_bytes(struct btrfs_fs_info
*fs_info
,
4398 struct btrfs_block_rsv
*block_rsv
,
4399 struct btrfs_block_rsv
*dest
, u64 num_bytes
)
4401 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
4403 spin_lock(&block_rsv
->lock
);
4404 if (num_bytes
== (u64
)-1)
4405 num_bytes
= block_rsv
->size
;
4406 block_rsv
->size
-= num_bytes
;
4407 if (block_rsv
->reserved
>= block_rsv
->size
) {
4408 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
4409 block_rsv
->reserved
= block_rsv
->size
;
4410 block_rsv
->full
= 1;
4414 spin_unlock(&block_rsv
->lock
);
4416 if (num_bytes
> 0) {
4418 spin_lock(&dest
->lock
);
4422 bytes_to_add
= dest
->size
- dest
->reserved
;
4423 bytes_to_add
= min(num_bytes
, bytes_to_add
);
4424 dest
->reserved
+= bytes_to_add
;
4425 if (dest
->reserved
>= dest
->size
)
4427 num_bytes
-= bytes_to_add
;
4429 spin_unlock(&dest
->lock
);
4432 spin_lock(&space_info
->lock
);
4433 space_info
->bytes_may_use
-= num_bytes
;
4434 trace_btrfs_space_reservation(fs_info
, "space_info",
4435 space_info
->flags
, num_bytes
, 0);
4436 space_info
->reservation_progress
++;
4437 spin_unlock(&space_info
->lock
);
4442 static int block_rsv_migrate_bytes(struct btrfs_block_rsv
*src
,
4443 struct btrfs_block_rsv
*dst
, u64 num_bytes
)
4447 ret
= block_rsv_use_bytes(src
, num_bytes
);
4451 block_rsv_add_bytes(dst
, num_bytes
, 1);
4455 void btrfs_init_block_rsv(struct btrfs_block_rsv
*rsv
, unsigned short type
)
4457 memset(rsv
, 0, sizeof(*rsv
));
4458 spin_lock_init(&rsv
->lock
);
4462 struct btrfs_block_rsv
*btrfs_alloc_block_rsv(struct btrfs_root
*root
,
4463 unsigned short type
)
4465 struct btrfs_block_rsv
*block_rsv
;
4466 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4468 block_rsv
= kmalloc(sizeof(*block_rsv
), GFP_NOFS
);
4472 btrfs_init_block_rsv(block_rsv
, type
);
4473 block_rsv
->space_info
= __find_space_info(fs_info
,
4474 BTRFS_BLOCK_GROUP_METADATA
);
4478 void btrfs_free_block_rsv(struct btrfs_root
*root
,
4479 struct btrfs_block_rsv
*rsv
)
4483 btrfs_block_rsv_release(root
, rsv
, (u64
)-1);
4487 int btrfs_block_rsv_add(struct btrfs_root
*root
,
4488 struct btrfs_block_rsv
*block_rsv
, u64 num_bytes
,
4489 enum btrfs_reserve_flush_enum flush
)
4496 ret
= reserve_metadata_bytes(root
, block_rsv
, num_bytes
, flush
);
4498 block_rsv_add_bytes(block_rsv
, num_bytes
, 1);
4505 int btrfs_block_rsv_check(struct btrfs_root
*root
,
4506 struct btrfs_block_rsv
*block_rsv
, int min_factor
)
4514 spin_lock(&block_rsv
->lock
);
4515 num_bytes
= div_factor(block_rsv
->size
, min_factor
);
4516 if (block_rsv
->reserved
>= num_bytes
)
4518 spin_unlock(&block_rsv
->lock
);
4523 int btrfs_block_rsv_refill(struct btrfs_root
*root
,
4524 struct btrfs_block_rsv
*block_rsv
, u64 min_reserved
,
4525 enum btrfs_reserve_flush_enum flush
)
4533 spin_lock(&block_rsv
->lock
);
4534 num_bytes
= min_reserved
;
4535 if (block_rsv
->reserved
>= num_bytes
)
4538 num_bytes
-= block_rsv
->reserved
;
4539 spin_unlock(&block_rsv
->lock
);
4544 ret
= reserve_metadata_bytes(root
, block_rsv
, num_bytes
, flush
);
4546 block_rsv_add_bytes(block_rsv
, num_bytes
, 0);
4553 int btrfs_block_rsv_migrate(struct btrfs_block_rsv
*src_rsv
,
4554 struct btrfs_block_rsv
*dst_rsv
,
4557 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
4560 void btrfs_block_rsv_release(struct btrfs_root
*root
,
4561 struct btrfs_block_rsv
*block_rsv
,
4564 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
4565 if (global_rsv
->full
|| global_rsv
== block_rsv
||
4566 block_rsv
->space_info
!= global_rsv
->space_info
)
4568 block_rsv_release_bytes(root
->fs_info
, block_rsv
, global_rsv
,
4573 * helper to calculate size of global block reservation.
4574 * the desired value is sum of space used by extent tree,
4575 * checksum tree and root tree
4577 static u64
calc_global_metadata_size(struct btrfs_fs_info
*fs_info
)
4579 struct btrfs_space_info
*sinfo
;
4583 int csum_size
= btrfs_super_csum_size(fs_info
->super_copy
);
4585 sinfo
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_DATA
);
4586 spin_lock(&sinfo
->lock
);
4587 data_used
= sinfo
->bytes_used
;
4588 spin_unlock(&sinfo
->lock
);
4590 sinfo
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
4591 spin_lock(&sinfo
->lock
);
4592 if (sinfo
->flags
& BTRFS_BLOCK_GROUP_DATA
)
4594 meta_used
= sinfo
->bytes_used
;
4595 spin_unlock(&sinfo
->lock
);
4597 num_bytes
= (data_used
>> fs_info
->sb
->s_blocksize_bits
) *
4599 num_bytes
+= div64_u64(data_used
+ meta_used
, 50);
4601 if (num_bytes
* 3 > meta_used
)
4602 num_bytes
= div64_u64(meta_used
, 3);
4604 return ALIGN(num_bytes
, fs_info
->extent_root
->leafsize
<< 10);
4607 static void update_global_block_rsv(struct btrfs_fs_info
*fs_info
)
4609 struct btrfs_block_rsv
*block_rsv
= &fs_info
->global_block_rsv
;
4610 struct btrfs_space_info
*sinfo
= block_rsv
->space_info
;
4613 num_bytes
= calc_global_metadata_size(fs_info
);
4615 spin_lock(&sinfo
->lock
);
4616 spin_lock(&block_rsv
->lock
);
4618 block_rsv
->size
= min_t(u64
, num_bytes
, 512 * 1024 * 1024);
4620 num_bytes
= sinfo
->bytes_used
+ sinfo
->bytes_pinned
+
4621 sinfo
->bytes_reserved
+ sinfo
->bytes_readonly
+
4622 sinfo
->bytes_may_use
;
4624 if (sinfo
->total_bytes
> num_bytes
) {
4625 num_bytes
= sinfo
->total_bytes
- num_bytes
;
4626 block_rsv
->reserved
+= num_bytes
;
4627 sinfo
->bytes_may_use
+= num_bytes
;
4628 trace_btrfs_space_reservation(fs_info
, "space_info",
4629 sinfo
->flags
, num_bytes
, 1);
4632 if (block_rsv
->reserved
>= block_rsv
->size
) {
4633 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
4634 sinfo
->bytes_may_use
-= num_bytes
;
4635 trace_btrfs_space_reservation(fs_info
, "space_info",
4636 sinfo
->flags
, num_bytes
, 0);
4637 sinfo
->reservation_progress
++;
4638 block_rsv
->reserved
= block_rsv
->size
;
4639 block_rsv
->full
= 1;
4642 spin_unlock(&block_rsv
->lock
);
4643 spin_unlock(&sinfo
->lock
);
4646 static void init_global_block_rsv(struct btrfs_fs_info
*fs_info
)
4648 struct btrfs_space_info
*space_info
;
4650 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_SYSTEM
);
4651 fs_info
->chunk_block_rsv
.space_info
= space_info
;
4653 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
4654 fs_info
->global_block_rsv
.space_info
= space_info
;
4655 fs_info
->delalloc_block_rsv
.space_info
= space_info
;
4656 fs_info
->trans_block_rsv
.space_info
= space_info
;
4657 fs_info
->empty_block_rsv
.space_info
= space_info
;
4658 fs_info
->delayed_block_rsv
.space_info
= space_info
;
4660 fs_info
->extent_root
->block_rsv
= &fs_info
->global_block_rsv
;
4661 fs_info
->csum_root
->block_rsv
= &fs_info
->global_block_rsv
;
4662 fs_info
->dev_root
->block_rsv
= &fs_info
->global_block_rsv
;
4663 fs_info
->tree_root
->block_rsv
= &fs_info
->global_block_rsv
;
4664 if (fs_info
->quota_root
)
4665 fs_info
->quota_root
->block_rsv
= &fs_info
->global_block_rsv
;
4666 fs_info
->chunk_root
->block_rsv
= &fs_info
->chunk_block_rsv
;
4668 update_global_block_rsv(fs_info
);
4671 static void release_global_block_rsv(struct btrfs_fs_info
*fs_info
)
4673 block_rsv_release_bytes(fs_info
, &fs_info
->global_block_rsv
, NULL
,
4675 WARN_ON(fs_info
->delalloc_block_rsv
.size
> 0);
4676 WARN_ON(fs_info
->delalloc_block_rsv
.reserved
> 0);
4677 WARN_ON(fs_info
->trans_block_rsv
.size
> 0);
4678 WARN_ON(fs_info
->trans_block_rsv
.reserved
> 0);
4679 WARN_ON(fs_info
->chunk_block_rsv
.size
> 0);
4680 WARN_ON(fs_info
->chunk_block_rsv
.reserved
> 0);
4681 WARN_ON(fs_info
->delayed_block_rsv
.size
> 0);
4682 WARN_ON(fs_info
->delayed_block_rsv
.reserved
> 0);
4685 void btrfs_trans_release_metadata(struct btrfs_trans_handle
*trans
,
4686 struct btrfs_root
*root
)
4688 if (!trans
->block_rsv
)
4691 if (!trans
->bytes_reserved
)
4694 trace_btrfs_space_reservation(root
->fs_info
, "transaction",
4695 trans
->transid
, trans
->bytes_reserved
, 0);
4696 btrfs_block_rsv_release(root
, trans
->block_rsv
, trans
->bytes_reserved
);
4697 trans
->bytes_reserved
= 0;
4700 /* Can only return 0 or -ENOSPC */
4701 int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle
*trans
,
4702 struct inode
*inode
)
4704 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4705 struct btrfs_block_rsv
*src_rsv
= get_block_rsv(trans
, root
);
4706 struct btrfs_block_rsv
*dst_rsv
= root
->orphan_block_rsv
;
4709 * We need to hold space in order to delete our orphan item once we've
4710 * added it, so this takes the reservation so we can release it later
4711 * when we are truly done with the orphan item.
4713 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 1);
4714 trace_btrfs_space_reservation(root
->fs_info
, "orphan",
4715 btrfs_ino(inode
), num_bytes
, 1);
4716 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
4719 void btrfs_orphan_release_metadata(struct inode
*inode
)
4721 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4722 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 1);
4723 trace_btrfs_space_reservation(root
->fs_info
, "orphan",
4724 btrfs_ino(inode
), num_bytes
, 0);
4725 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
, num_bytes
);
4729 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
4730 * root: the root of the parent directory
4731 * rsv: block reservation
4732 * items: the number of items that we need do reservation
4733 * qgroup_reserved: used to return the reserved size in qgroup
4735 * This function is used to reserve the space for snapshot/subvolume
4736 * creation and deletion. Those operations are different with the
4737 * common file/directory operations, they change two fs/file trees
4738 * and root tree, the number of items that the qgroup reserves is
4739 * different with the free space reservation. So we can not use
4740 * the space reseravtion mechanism in start_transaction().
4742 int btrfs_subvolume_reserve_metadata(struct btrfs_root
*root
,
4743 struct btrfs_block_rsv
*rsv
,
4745 u64
*qgroup_reserved
,
4746 bool use_global_rsv
)
4750 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
4752 if (root
->fs_info
->quota_enabled
) {
4753 /* One for parent inode, two for dir entries */
4754 num_bytes
= 3 * root
->leafsize
;
4755 ret
= btrfs_qgroup_reserve(root
, num_bytes
);
4762 *qgroup_reserved
= num_bytes
;
4764 num_bytes
= btrfs_calc_trans_metadata_size(root
, items
);
4765 rsv
->space_info
= __find_space_info(root
->fs_info
,
4766 BTRFS_BLOCK_GROUP_METADATA
);
4767 ret
= btrfs_block_rsv_add(root
, rsv
, num_bytes
,
4768 BTRFS_RESERVE_FLUSH_ALL
);
4770 if (ret
== -ENOSPC
&& use_global_rsv
)
4771 ret
= btrfs_block_rsv_migrate(global_rsv
, rsv
, num_bytes
);
4774 if (*qgroup_reserved
)
4775 btrfs_qgroup_free(root
, *qgroup_reserved
);
4781 void btrfs_subvolume_release_metadata(struct btrfs_root
*root
,
4782 struct btrfs_block_rsv
*rsv
,
4783 u64 qgroup_reserved
)
4785 btrfs_block_rsv_release(root
, rsv
, (u64
)-1);
4786 if (qgroup_reserved
)
4787 btrfs_qgroup_free(root
, qgroup_reserved
);
4791 * drop_outstanding_extent - drop an outstanding extent
4792 * @inode: the inode we're dropping the extent for
4794 * This is called when we are freeing up an outstanding extent, either called
4795 * after an error or after an extent is written. This will return the number of
4796 * reserved extents that need to be freed. This must be called with
4797 * BTRFS_I(inode)->lock held.
4799 static unsigned drop_outstanding_extent(struct inode
*inode
)
4801 unsigned drop_inode_space
= 0;
4802 unsigned dropped_extents
= 0;
4804 BUG_ON(!BTRFS_I(inode
)->outstanding_extents
);
4805 BTRFS_I(inode
)->outstanding_extents
--;
4807 if (BTRFS_I(inode
)->outstanding_extents
== 0 &&
4808 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED
,
4809 &BTRFS_I(inode
)->runtime_flags
))
4810 drop_inode_space
= 1;
4813 * If we have more or the same amount of outsanding extents than we have
4814 * reserved then we need to leave the reserved extents count alone.
4816 if (BTRFS_I(inode
)->outstanding_extents
>=
4817 BTRFS_I(inode
)->reserved_extents
)
4818 return drop_inode_space
;
4820 dropped_extents
= BTRFS_I(inode
)->reserved_extents
-
4821 BTRFS_I(inode
)->outstanding_extents
;
4822 BTRFS_I(inode
)->reserved_extents
-= dropped_extents
;
4823 return dropped_extents
+ drop_inode_space
;
4827 * calc_csum_metadata_size - return the amount of metada space that must be
4828 * reserved/free'd for the given bytes.
4829 * @inode: the inode we're manipulating
4830 * @num_bytes: the number of bytes in question
4831 * @reserve: 1 if we are reserving space, 0 if we are freeing space
4833 * This adjusts the number of csum_bytes in the inode and then returns the
4834 * correct amount of metadata that must either be reserved or freed. We
4835 * calculate how many checksums we can fit into one leaf and then divide the
4836 * number of bytes that will need to be checksumed by this value to figure out
4837 * how many checksums will be required. If we are adding bytes then the number
4838 * may go up and we will return the number of additional bytes that must be
4839 * reserved. If it is going down we will return the number of bytes that must
4842 * This must be called with BTRFS_I(inode)->lock held.
4844 static u64
calc_csum_metadata_size(struct inode
*inode
, u64 num_bytes
,
4847 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4849 int num_csums_per_leaf
;
4853 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
&&
4854 BTRFS_I(inode
)->csum_bytes
== 0)
4857 old_csums
= (int)div64_u64(BTRFS_I(inode
)->csum_bytes
, root
->sectorsize
);
4859 BTRFS_I(inode
)->csum_bytes
+= num_bytes
;
4861 BTRFS_I(inode
)->csum_bytes
-= num_bytes
;
4862 csum_size
= BTRFS_LEAF_DATA_SIZE(root
) - sizeof(struct btrfs_item
);
4863 num_csums_per_leaf
= (int)div64_u64(csum_size
,
4864 sizeof(struct btrfs_csum_item
) +
4865 sizeof(struct btrfs_disk_key
));
4866 num_csums
= (int)div64_u64(BTRFS_I(inode
)->csum_bytes
, root
->sectorsize
);
4867 num_csums
= num_csums
+ num_csums_per_leaf
- 1;
4868 num_csums
= num_csums
/ num_csums_per_leaf
;
4870 old_csums
= old_csums
+ num_csums_per_leaf
- 1;
4871 old_csums
= old_csums
/ num_csums_per_leaf
;
4873 /* No change, no need to reserve more */
4874 if (old_csums
== num_csums
)
4878 return btrfs_calc_trans_metadata_size(root
,
4879 num_csums
- old_csums
);
4881 return btrfs_calc_trans_metadata_size(root
, old_csums
- num_csums
);
4884 int btrfs_delalloc_reserve_metadata(struct inode
*inode
, u64 num_bytes
)
4886 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4887 struct btrfs_block_rsv
*block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
4890 unsigned nr_extents
= 0;
4891 int extra_reserve
= 0;
4892 enum btrfs_reserve_flush_enum flush
= BTRFS_RESERVE_FLUSH_ALL
;
4894 bool delalloc_lock
= true;
4898 /* If we are a free space inode we need to not flush since we will be in
4899 * the middle of a transaction commit. We also don't need the delalloc
4900 * mutex since we won't race with anybody. We need this mostly to make
4901 * lockdep shut its filthy mouth.
4903 if (btrfs_is_free_space_inode(inode
)) {
4904 flush
= BTRFS_RESERVE_NO_FLUSH
;
4905 delalloc_lock
= false;
4908 if (flush
!= BTRFS_RESERVE_NO_FLUSH
&&
4909 btrfs_transaction_in_commit(root
->fs_info
))
4910 schedule_timeout(1);
4913 mutex_lock(&BTRFS_I(inode
)->delalloc_mutex
);
4915 num_bytes
= ALIGN(num_bytes
, root
->sectorsize
);
4917 spin_lock(&BTRFS_I(inode
)->lock
);
4918 BTRFS_I(inode
)->outstanding_extents
++;
4920 if (BTRFS_I(inode
)->outstanding_extents
>
4921 BTRFS_I(inode
)->reserved_extents
)
4922 nr_extents
= BTRFS_I(inode
)->outstanding_extents
-
4923 BTRFS_I(inode
)->reserved_extents
;
4926 * Add an item to reserve for updating the inode when we complete the
4929 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED
,
4930 &BTRFS_I(inode
)->runtime_flags
)) {
4935 to_reserve
= btrfs_calc_trans_metadata_size(root
, nr_extents
);
4936 to_reserve
+= calc_csum_metadata_size(inode
, num_bytes
, 1);
4937 csum_bytes
= BTRFS_I(inode
)->csum_bytes
;
4938 spin_unlock(&BTRFS_I(inode
)->lock
);
4940 if (root
->fs_info
->quota_enabled
) {
4941 ret
= btrfs_qgroup_reserve(root
, num_bytes
+
4942 nr_extents
* root
->leafsize
);
4947 ret
= reserve_metadata_bytes(root
, block_rsv
, to_reserve
, flush
);
4948 if (unlikely(ret
)) {
4949 if (root
->fs_info
->quota_enabled
)
4950 btrfs_qgroup_free(root
, num_bytes
+
4951 nr_extents
* root
->leafsize
);
4955 spin_lock(&BTRFS_I(inode
)->lock
);
4956 if (extra_reserve
) {
4957 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED
,
4958 &BTRFS_I(inode
)->runtime_flags
);
4961 BTRFS_I(inode
)->reserved_extents
+= nr_extents
;
4962 spin_unlock(&BTRFS_I(inode
)->lock
);
4965 mutex_unlock(&BTRFS_I(inode
)->delalloc_mutex
);
4968 trace_btrfs_space_reservation(root
->fs_info
,"delalloc",
4969 btrfs_ino(inode
), to_reserve
, 1);
4970 block_rsv_add_bytes(block_rsv
, to_reserve
, 1);
4975 spin_lock(&BTRFS_I(inode
)->lock
);
4976 dropped
= drop_outstanding_extent(inode
);
4978 * If the inodes csum_bytes is the same as the original
4979 * csum_bytes then we know we haven't raced with any free()ers
4980 * so we can just reduce our inodes csum bytes and carry on.
4982 if (BTRFS_I(inode
)->csum_bytes
== csum_bytes
) {
4983 calc_csum_metadata_size(inode
, num_bytes
, 0);
4985 u64 orig_csum_bytes
= BTRFS_I(inode
)->csum_bytes
;
4989 * This is tricky, but first we need to figure out how much we
4990 * free'd from any free-ers that occured during this
4991 * reservation, so we reset ->csum_bytes to the csum_bytes
4992 * before we dropped our lock, and then call the free for the
4993 * number of bytes that were freed while we were trying our
4996 bytes
= csum_bytes
- BTRFS_I(inode
)->csum_bytes
;
4997 BTRFS_I(inode
)->csum_bytes
= csum_bytes
;
4998 to_free
= calc_csum_metadata_size(inode
, bytes
, 0);
5002 * Now we need to see how much we would have freed had we not
5003 * been making this reservation and our ->csum_bytes were not
5004 * artificially inflated.
5006 BTRFS_I(inode
)->csum_bytes
= csum_bytes
- num_bytes
;
5007 bytes
= csum_bytes
- orig_csum_bytes
;
5008 bytes
= calc_csum_metadata_size(inode
, bytes
, 0);
5011 * Now reset ->csum_bytes to what it should be. If bytes is
5012 * more than to_free then we would have free'd more space had we
5013 * not had an artificially high ->csum_bytes, so we need to free
5014 * the remainder. If bytes is the same or less then we don't
5015 * need to do anything, the other free-ers did the correct
5018 BTRFS_I(inode
)->csum_bytes
= orig_csum_bytes
- num_bytes
;
5019 if (bytes
> to_free
)
5020 to_free
= bytes
- to_free
;
5024 spin_unlock(&BTRFS_I(inode
)->lock
);
5026 to_free
+= btrfs_calc_trans_metadata_size(root
, dropped
);
5029 btrfs_block_rsv_release(root
, block_rsv
, to_free
);
5030 trace_btrfs_space_reservation(root
->fs_info
, "delalloc",
5031 btrfs_ino(inode
), to_free
, 0);
5034 mutex_unlock(&BTRFS_I(inode
)->delalloc_mutex
);
5039 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
5040 * @inode: the inode to release the reservation for
5041 * @num_bytes: the number of bytes we're releasing
5043 * This will release the metadata reservation for an inode. This can be called
5044 * once we complete IO for a given set of bytes to release their metadata
5047 void btrfs_delalloc_release_metadata(struct inode
*inode
, u64 num_bytes
)
5049 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5053 num_bytes
= ALIGN(num_bytes
, root
->sectorsize
);
5054 spin_lock(&BTRFS_I(inode
)->lock
);
5055 dropped
= drop_outstanding_extent(inode
);
5058 to_free
= calc_csum_metadata_size(inode
, num_bytes
, 0);
5059 spin_unlock(&BTRFS_I(inode
)->lock
);
5061 to_free
+= btrfs_calc_trans_metadata_size(root
, dropped
);
5063 trace_btrfs_space_reservation(root
->fs_info
, "delalloc",
5064 btrfs_ino(inode
), to_free
, 0);
5065 if (root
->fs_info
->quota_enabled
) {
5066 btrfs_qgroup_free(root
, num_bytes
+
5067 dropped
* root
->leafsize
);
5070 btrfs_block_rsv_release(root
, &root
->fs_info
->delalloc_block_rsv
,
5075 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
5076 * @inode: inode we're writing to
5077 * @num_bytes: the number of bytes we want to allocate
5079 * This will do the following things
5081 * o reserve space in the data space info for num_bytes
5082 * o reserve space in the metadata space info based on number of outstanding
5083 * extents and how much csums will be needed
5084 * o add to the inodes ->delalloc_bytes
5085 * o add it to the fs_info's delalloc inodes list.
5087 * This will return 0 for success and -ENOSPC if there is no space left.
5089 int btrfs_delalloc_reserve_space(struct inode
*inode
, u64 num_bytes
)
5093 ret
= btrfs_check_data_free_space(inode
, num_bytes
);
5097 ret
= btrfs_delalloc_reserve_metadata(inode
, num_bytes
);
5099 btrfs_free_reserved_data_space(inode
, num_bytes
);
5107 * btrfs_delalloc_release_space - release data and metadata space for delalloc
5108 * @inode: inode we're releasing space for
5109 * @num_bytes: the number of bytes we want to free up
5111 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
5112 * called in the case that we don't need the metadata AND data reservations
5113 * anymore. So if there is an error or we insert an inline extent.
5115 * This function will release the metadata space that was not used and will
5116 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
5117 * list if there are no delalloc bytes left.
5119 void btrfs_delalloc_release_space(struct inode
*inode
, u64 num_bytes
)
5121 btrfs_delalloc_release_metadata(inode
, num_bytes
);
5122 btrfs_free_reserved_data_space(inode
, num_bytes
);
5125 static int update_block_group(struct btrfs_root
*root
,
5126 u64 bytenr
, u64 num_bytes
, int alloc
)
5128 struct btrfs_block_group_cache
*cache
= NULL
;
5129 struct btrfs_fs_info
*info
= root
->fs_info
;
5130 u64 total
= num_bytes
;
5135 /* block accounting for super block */
5136 spin_lock(&info
->delalloc_root_lock
);
5137 old_val
= btrfs_super_bytes_used(info
->super_copy
);
5139 old_val
+= num_bytes
;
5141 old_val
-= num_bytes
;
5142 btrfs_set_super_bytes_used(info
->super_copy
, old_val
);
5143 spin_unlock(&info
->delalloc_root_lock
);
5146 cache
= btrfs_lookup_block_group(info
, bytenr
);
5149 if (cache
->flags
& (BTRFS_BLOCK_GROUP_DUP
|
5150 BTRFS_BLOCK_GROUP_RAID1
|
5151 BTRFS_BLOCK_GROUP_RAID10
))
5156 * If this block group has free space cache written out, we
5157 * need to make sure to load it if we are removing space. This
5158 * is because we need the unpinning stage to actually add the
5159 * space back to the block group, otherwise we will leak space.
5161 if (!alloc
&& cache
->cached
== BTRFS_CACHE_NO
)
5162 cache_block_group(cache
, 1);
5164 byte_in_group
= bytenr
- cache
->key
.objectid
;
5165 WARN_ON(byte_in_group
> cache
->key
.offset
);
5167 spin_lock(&cache
->space_info
->lock
);
5168 spin_lock(&cache
->lock
);
5170 if (btrfs_test_opt(root
, SPACE_CACHE
) &&
5171 cache
->disk_cache_state
< BTRFS_DC_CLEAR
)
5172 cache
->disk_cache_state
= BTRFS_DC_CLEAR
;
5175 old_val
= btrfs_block_group_used(&cache
->item
);
5176 num_bytes
= min(total
, cache
->key
.offset
- byte_in_group
);
5178 old_val
+= num_bytes
;
5179 btrfs_set_block_group_used(&cache
->item
, old_val
);
5180 cache
->reserved
-= num_bytes
;
5181 cache
->space_info
->bytes_reserved
-= num_bytes
;
5182 cache
->space_info
->bytes_used
+= num_bytes
;
5183 cache
->space_info
->disk_used
+= num_bytes
* factor
;
5184 spin_unlock(&cache
->lock
);
5185 spin_unlock(&cache
->space_info
->lock
);
5187 old_val
-= num_bytes
;
5188 btrfs_set_block_group_used(&cache
->item
, old_val
);
5189 cache
->pinned
+= num_bytes
;
5190 cache
->space_info
->bytes_pinned
+= num_bytes
;
5191 cache
->space_info
->bytes_used
-= num_bytes
;
5192 cache
->space_info
->disk_used
-= num_bytes
* factor
;
5193 spin_unlock(&cache
->lock
);
5194 spin_unlock(&cache
->space_info
->lock
);
5196 set_extent_dirty(info
->pinned_extents
,
5197 bytenr
, bytenr
+ num_bytes
- 1,
5198 GFP_NOFS
| __GFP_NOFAIL
);
5200 btrfs_put_block_group(cache
);
5202 bytenr
+= num_bytes
;
5207 static u64
first_logical_byte(struct btrfs_root
*root
, u64 search_start
)
5209 struct btrfs_block_group_cache
*cache
;
5212 spin_lock(&root
->fs_info
->block_group_cache_lock
);
5213 bytenr
= root
->fs_info
->first_logical_byte
;
5214 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
5216 if (bytenr
< (u64
)-1)
5219 cache
= btrfs_lookup_first_block_group(root
->fs_info
, search_start
);
5223 bytenr
= cache
->key
.objectid
;
5224 btrfs_put_block_group(cache
);
5229 static int pin_down_extent(struct btrfs_root
*root
,
5230 struct btrfs_block_group_cache
*cache
,
5231 u64 bytenr
, u64 num_bytes
, int reserved
)
5233 spin_lock(&cache
->space_info
->lock
);
5234 spin_lock(&cache
->lock
);
5235 cache
->pinned
+= num_bytes
;
5236 cache
->space_info
->bytes_pinned
+= num_bytes
;
5238 cache
->reserved
-= num_bytes
;
5239 cache
->space_info
->bytes_reserved
-= num_bytes
;
5241 spin_unlock(&cache
->lock
);
5242 spin_unlock(&cache
->space_info
->lock
);
5244 set_extent_dirty(root
->fs_info
->pinned_extents
, bytenr
,
5245 bytenr
+ num_bytes
- 1, GFP_NOFS
| __GFP_NOFAIL
);
5250 * this function must be called within transaction
5252 int btrfs_pin_extent(struct btrfs_root
*root
,
5253 u64 bytenr
, u64 num_bytes
, int reserved
)
5255 struct btrfs_block_group_cache
*cache
;
5257 cache
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
5258 BUG_ON(!cache
); /* Logic error */
5260 pin_down_extent(root
, cache
, bytenr
, num_bytes
, reserved
);
5262 btrfs_put_block_group(cache
);
5267 * this function must be called within transaction
5269 int btrfs_pin_extent_for_log_replay(struct btrfs_root
*root
,
5270 u64 bytenr
, u64 num_bytes
)
5272 struct btrfs_block_group_cache
*cache
;
5275 cache
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
5280 * pull in the free space cache (if any) so that our pin
5281 * removes the free space from the cache. We have load_only set
5282 * to one because the slow code to read in the free extents does check
5283 * the pinned extents.
5285 cache_block_group(cache
, 1);
5287 pin_down_extent(root
, cache
, bytenr
, num_bytes
, 0);
5289 /* remove us from the free space cache (if we're there at all) */
5290 ret
= btrfs_remove_free_space(cache
, bytenr
, num_bytes
);
5291 btrfs_put_block_group(cache
);
5295 static int __exclude_logged_extent(struct btrfs_root
*root
, u64 start
, u64 num_bytes
)
5298 struct btrfs_block_group_cache
*block_group
;
5299 struct btrfs_caching_control
*caching_ctl
;
5301 block_group
= btrfs_lookup_block_group(root
->fs_info
, start
);
5305 cache_block_group(block_group
, 0);
5306 caching_ctl
= get_caching_control(block_group
);
5310 BUG_ON(!block_group_cache_done(block_group
));
5311 ret
= btrfs_remove_free_space(block_group
, start
, num_bytes
);
5313 mutex_lock(&caching_ctl
->mutex
);
5315 if (start
>= caching_ctl
->progress
) {
5316 ret
= add_excluded_extent(root
, start
, num_bytes
);
5317 } else if (start
+ num_bytes
<= caching_ctl
->progress
) {
5318 ret
= btrfs_remove_free_space(block_group
,
5321 num_bytes
= caching_ctl
->progress
- start
;
5322 ret
= btrfs_remove_free_space(block_group
,
5327 num_bytes
= (start
+ num_bytes
) -
5328 caching_ctl
->progress
;
5329 start
= caching_ctl
->progress
;
5330 ret
= add_excluded_extent(root
, start
, num_bytes
);
5333 mutex_unlock(&caching_ctl
->mutex
);
5334 put_caching_control(caching_ctl
);
5336 btrfs_put_block_group(block_group
);
5340 int btrfs_exclude_logged_extents(struct btrfs_root
*log
,
5341 struct extent_buffer
*eb
)
5343 struct btrfs_file_extent_item
*item
;
5344 struct btrfs_key key
;
5348 if (!btrfs_fs_incompat(log
->fs_info
, MIXED_GROUPS
))
5351 for (i
= 0; i
< btrfs_header_nritems(eb
); i
++) {
5352 btrfs_item_key_to_cpu(eb
, &key
, i
);
5353 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
5355 item
= btrfs_item_ptr(eb
, i
, struct btrfs_file_extent_item
);
5356 found_type
= btrfs_file_extent_type(eb
, item
);
5357 if (found_type
== BTRFS_FILE_EXTENT_INLINE
)
5359 if (btrfs_file_extent_disk_bytenr(eb
, item
) == 0)
5361 key
.objectid
= btrfs_file_extent_disk_bytenr(eb
, item
);
5362 key
.offset
= btrfs_file_extent_disk_num_bytes(eb
, item
);
5363 __exclude_logged_extent(log
, key
.objectid
, key
.offset
);
5370 * btrfs_update_reserved_bytes - update the block_group and space info counters
5371 * @cache: The cache we are manipulating
5372 * @num_bytes: The number of bytes in question
5373 * @reserve: One of the reservation enums
5375 * This is called by the allocator when it reserves space, or by somebody who is
5376 * freeing space that was never actually used on disk. For example if you
5377 * reserve some space for a new leaf in transaction A and before transaction A
5378 * commits you free that leaf, you call this with reserve set to 0 in order to
5379 * clear the reservation.
5381 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
5382 * ENOSPC accounting. For data we handle the reservation through clearing the
5383 * delalloc bits in the io_tree. We have to do this since we could end up
5384 * allocating less disk space for the amount of data we have reserved in the
5385 * case of compression.
5387 * If this is a reservation and the block group has become read only we cannot
5388 * make the reservation and return -EAGAIN, otherwise this function always
5391 static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache
*cache
,
5392 u64 num_bytes
, int reserve
)
5394 struct btrfs_space_info
*space_info
= cache
->space_info
;
5397 spin_lock(&space_info
->lock
);
5398 spin_lock(&cache
->lock
);
5399 if (reserve
!= RESERVE_FREE
) {
5403 cache
->reserved
+= num_bytes
;
5404 space_info
->bytes_reserved
+= num_bytes
;
5405 if (reserve
== RESERVE_ALLOC
) {
5406 trace_btrfs_space_reservation(cache
->fs_info
,
5407 "space_info", space_info
->flags
,
5409 space_info
->bytes_may_use
-= num_bytes
;
5414 space_info
->bytes_readonly
+= num_bytes
;
5415 cache
->reserved
-= num_bytes
;
5416 space_info
->bytes_reserved
-= num_bytes
;
5417 space_info
->reservation_progress
++;
5419 spin_unlock(&cache
->lock
);
5420 spin_unlock(&space_info
->lock
);
5424 void btrfs_prepare_extent_commit(struct btrfs_trans_handle
*trans
,
5425 struct btrfs_root
*root
)
5427 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5428 struct btrfs_caching_control
*next
;
5429 struct btrfs_caching_control
*caching_ctl
;
5430 struct btrfs_block_group_cache
*cache
;
5431 struct btrfs_space_info
*space_info
;
5433 down_write(&fs_info
->extent_commit_sem
);
5435 list_for_each_entry_safe(caching_ctl
, next
,
5436 &fs_info
->caching_block_groups
, list
) {
5437 cache
= caching_ctl
->block_group
;
5438 if (block_group_cache_done(cache
)) {
5439 cache
->last_byte_to_unpin
= (u64
)-1;
5440 list_del_init(&caching_ctl
->list
);
5441 put_caching_control(caching_ctl
);
5443 cache
->last_byte_to_unpin
= caching_ctl
->progress
;
5447 if (fs_info
->pinned_extents
== &fs_info
->freed_extents
[0])
5448 fs_info
->pinned_extents
= &fs_info
->freed_extents
[1];
5450 fs_info
->pinned_extents
= &fs_info
->freed_extents
[0];
5452 up_write(&fs_info
->extent_commit_sem
);
5454 list_for_each_entry_rcu(space_info
, &fs_info
->space_info
, list
)
5455 percpu_counter_set(&space_info
->total_bytes_pinned
, 0);
5457 update_global_block_rsv(fs_info
);
5460 static int unpin_extent_range(struct btrfs_root
*root
, u64 start
, u64 end
)
5462 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5463 struct btrfs_block_group_cache
*cache
= NULL
;
5464 struct btrfs_space_info
*space_info
;
5465 struct btrfs_block_rsv
*global_rsv
= &fs_info
->global_block_rsv
;
5469 while (start
<= end
) {
5472 start
>= cache
->key
.objectid
+ cache
->key
.offset
) {
5474 btrfs_put_block_group(cache
);
5475 cache
= btrfs_lookup_block_group(fs_info
, start
);
5476 BUG_ON(!cache
); /* Logic error */
5479 len
= cache
->key
.objectid
+ cache
->key
.offset
- start
;
5480 len
= min(len
, end
+ 1 - start
);
5482 if (start
< cache
->last_byte_to_unpin
) {
5483 len
= min(len
, cache
->last_byte_to_unpin
- start
);
5484 btrfs_add_free_space(cache
, start
, len
);
5488 space_info
= cache
->space_info
;
5490 spin_lock(&space_info
->lock
);
5491 spin_lock(&cache
->lock
);
5492 cache
->pinned
-= len
;
5493 space_info
->bytes_pinned
-= len
;
5495 space_info
->bytes_readonly
+= len
;
5498 spin_unlock(&cache
->lock
);
5499 if (!readonly
&& global_rsv
->space_info
== space_info
) {
5500 spin_lock(&global_rsv
->lock
);
5501 if (!global_rsv
->full
) {
5502 len
= min(len
, global_rsv
->size
-
5503 global_rsv
->reserved
);
5504 global_rsv
->reserved
+= len
;
5505 space_info
->bytes_may_use
+= len
;
5506 if (global_rsv
->reserved
>= global_rsv
->size
)
5507 global_rsv
->full
= 1;
5509 spin_unlock(&global_rsv
->lock
);
5511 spin_unlock(&space_info
->lock
);
5515 btrfs_put_block_group(cache
);
5519 int btrfs_finish_extent_commit(struct btrfs_trans_handle
*trans
,
5520 struct btrfs_root
*root
)
5522 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5523 struct extent_io_tree
*unpin
;
5531 if (fs_info
->pinned_extents
== &fs_info
->freed_extents
[0])
5532 unpin
= &fs_info
->freed_extents
[1];
5534 unpin
= &fs_info
->freed_extents
[0];
5537 ret
= find_first_extent_bit(unpin
, 0, &start
, &end
,
5538 EXTENT_DIRTY
, NULL
);
5542 if (btrfs_test_opt(root
, DISCARD
))
5543 ret
= btrfs_discard_extent(root
, start
,
5544 end
+ 1 - start
, NULL
);
5546 clear_extent_dirty(unpin
, start
, end
, GFP_NOFS
);
5547 unpin_extent_range(root
, start
, end
);
5554 static void add_pinned_bytes(struct btrfs_fs_info
*fs_info
, u64 num_bytes
,
5555 u64 owner
, u64 root_objectid
)
5557 struct btrfs_space_info
*space_info
;
5560 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
5561 if (root_objectid
== BTRFS_CHUNK_TREE_OBJECTID
)
5562 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
5564 flags
= BTRFS_BLOCK_GROUP_METADATA
;
5566 flags
= BTRFS_BLOCK_GROUP_DATA
;
5569 space_info
= __find_space_info(fs_info
, flags
);
5570 BUG_ON(!space_info
); /* Logic bug */
5571 percpu_counter_add(&space_info
->total_bytes_pinned
, num_bytes
);
5575 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
5576 struct btrfs_root
*root
,
5577 u64 bytenr
, u64 num_bytes
, u64 parent
,
5578 u64 root_objectid
, u64 owner_objectid
,
5579 u64 owner_offset
, int refs_to_drop
,
5580 struct btrfs_delayed_extent_op
*extent_op
)
5582 struct btrfs_key key
;
5583 struct btrfs_path
*path
;
5584 struct btrfs_fs_info
*info
= root
->fs_info
;
5585 struct btrfs_root
*extent_root
= info
->extent_root
;
5586 struct extent_buffer
*leaf
;
5587 struct btrfs_extent_item
*ei
;
5588 struct btrfs_extent_inline_ref
*iref
;
5591 int extent_slot
= 0;
5592 int found_extent
= 0;
5596 bool skinny_metadata
= btrfs_fs_incompat(root
->fs_info
,
5599 path
= btrfs_alloc_path();
5604 path
->leave_spinning
= 1;
5606 is_data
= owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
;
5607 BUG_ON(!is_data
&& refs_to_drop
!= 1);
5610 skinny_metadata
= 0;
5612 ret
= lookup_extent_backref(trans
, extent_root
, path
, &iref
,
5613 bytenr
, num_bytes
, parent
,
5614 root_objectid
, owner_objectid
,
5617 extent_slot
= path
->slots
[0];
5618 while (extent_slot
>= 0) {
5619 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
5621 if (key
.objectid
!= bytenr
)
5623 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
5624 key
.offset
== num_bytes
) {
5628 if (key
.type
== BTRFS_METADATA_ITEM_KEY
&&
5629 key
.offset
== owner_objectid
) {
5633 if (path
->slots
[0] - extent_slot
> 5)
5637 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5638 item_size
= btrfs_item_size_nr(path
->nodes
[0], extent_slot
);
5639 if (found_extent
&& item_size
< sizeof(*ei
))
5642 if (!found_extent
) {
5644 ret
= remove_extent_backref(trans
, extent_root
, path
,
5648 btrfs_abort_transaction(trans
, extent_root
, ret
);
5651 btrfs_release_path(path
);
5652 path
->leave_spinning
= 1;
5654 key
.objectid
= bytenr
;
5655 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
5656 key
.offset
= num_bytes
;
5658 if (!is_data
&& skinny_metadata
) {
5659 key
.type
= BTRFS_METADATA_ITEM_KEY
;
5660 key
.offset
= owner_objectid
;
5663 ret
= btrfs_search_slot(trans
, extent_root
,
5665 if (ret
> 0 && skinny_metadata
&& path
->slots
[0]) {
5667 * Couldn't find our skinny metadata item,
5668 * see if we have ye olde extent item.
5671 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
5673 if (key
.objectid
== bytenr
&&
5674 key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
5675 key
.offset
== num_bytes
)
5679 if (ret
> 0 && skinny_metadata
) {
5680 skinny_metadata
= false;
5681 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
5682 key
.offset
= num_bytes
;
5683 btrfs_release_path(path
);
5684 ret
= btrfs_search_slot(trans
, extent_root
,
5689 btrfs_err(info
, "umm, got %d back from search, was looking for %llu",
5690 ret
, (unsigned long long)bytenr
);
5692 btrfs_print_leaf(extent_root
,
5696 btrfs_abort_transaction(trans
, extent_root
, ret
);
5699 extent_slot
= path
->slots
[0];
5701 } else if (ret
== -ENOENT
) {
5702 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
5705 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
5706 (unsigned long long)bytenr
,
5707 (unsigned long long)parent
,
5708 (unsigned long long)root_objectid
,
5709 (unsigned long long)owner_objectid
,
5710 (unsigned long long)owner_offset
);
5712 btrfs_abort_transaction(trans
, extent_root
, ret
);
5716 leaf
= path
->nodes
[0];
5717 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
5718 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5719 if (item_size
< sizeof(*ei
)) {
5720 BUG_ON(found_extent
|| extent_slot
!= path
->slots
[0]);
5721 ret
= convert_extent_item_v0(trans
, extent_root
, path
,
5724 btrfs_abort_transaction(trans
, extent_root
, ret
);
5728 btrfs_release_path(path
);
5729 path
->leave_spinning
= 1;
5731 key
.objectid
= bytenr
;
5732 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
5733 key
.offset
= num_bytes
;
5735 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
,
5738 btrfs_err(info
, "umm, got %d back from search, was looking for %llu",
5739 ret
, (unsigned long long)bytenr
);
5740 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
5743 btrfs_abort_transaction(trans
, extent_root
, ret
);
5747 extent_slot
= path
->slots
[0];
5748 leaf
= path
->nodes
[0];
5749 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
5752 BUG_ON(item_size
< sizeof(*ei
));
5753 ei
= btrfs_item_ptr(leaf
, extent_slot
,
5754 struct btrfs_extent_item
);
5755 if (owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
&&
5756 key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
5757 struct btrfs_tree_block_info
*bi
;
5758 BUG_ON(item_size
< sizeof(*ei
) + sizeof(*bi
));
5759 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
5760 WARN_ON(owner_objectid
!= btrfs_tree_block_level(leaf
, bi
));
5763 refs
= btrfs_extent_refs(leaf
, ei
);
5764 if (refs
< refs_to_drop
) {
5765 btrfs_err(info
, "trying to drop %d refs but we only have %Lu "
5766 "for bytenr %Lu\n", refs_to_drop
, refs
, bytenr
);
5768 btrfs_abort_transaction(trans
, extent_root
, ret
);
5771 refs
-= refs_to_drop
;
5775 __run_delayed_extent_op(extent_op
, leaf
, ei
);
5777 * In the case of inline back ref, reference count will
5778 * be updated by remove_extent_backref
5781 BUG_ON(!found_extent
);
5783 btrfs_set_extent_refs(leaf
, ei
, refs
);
5784 btrfs_mark_buffer_dirty(leaf
);
5787 ret
= remove_extent_backref(trans
, extent_root
, path
,
5791 btrfs_abort_transaction(trans
, extent_root
, ret
);
5795 add_pinned_bytes(root
->fs_info
, -num_bytes
, owner_objectid
,
5799 BUG_ON(is_data
&& refs_to_drop
!=
5800 extent_data_ref_count(root
, path
, iref
));
5802 BUG_ON(path
->slots
[0] != extent_slot
);
5804 BUG_ON(path
->slots
[0] != extent_slot
+ 1);
5805 path
->slots
[0] = extent_slot
;
5810 ret
= btrfs_del_items(trans
, extent_root
, path
, path
->slots
[0],
5813 btrfs_abort_transaction(trans
, extent_root
, ret
);
5816 btrfs_release_path(path
);
5819 ret
= btrfs_del_csums(trans
, root
, bytenr
, num_bytes
);
5821 btrfs_abort_transaction(trans
, extent_root
, ret
);
5826 ret
= update_block_group(root
, bytenr
, num_bytes
, 0);
5828 btrfs_abort_transaction(trans
, extent_root
, ret
);
5833 btrfs_free_path(path
);
5838 * when we free an block, it is possible (and likely) that we free the last
5839 * delayed ref for that extent as well. This searches the delayed ref tree for
5840 * a given extent, and if there are no other delayed refs to be processed, it
5841 * removes it from the tree.
5843 static noinline
int check_ref_cleanup(struct btrfs_trans_handle
*trans
,
5844 struct btrfs_root
*root
, u64 bytenr
)
5846 struct btrfs_delayed_ref_head
*head
;
5847 struct btrfs_delayed_ref_root
*delayed_refs
;
5848 struct btrfs_delayed_ref_node
*ref
;
5849 struct rb_node
*node
;
5852 delayed_refs
= &trans
->transaction
->delayed_refs
;
5853 spin_lock(&delayed_refs
->lock
);
5854 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
5858 node
= rb_prev(&head
->node
.rb_node
);
5862 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
5864 /* there are still entries for this ref, we can't drop it */
5865 if (ref
->bytenr
== bytenr
)
5868 if (head
->extent_op
) {
5869 if (!head
->must_insert_reserved
)
5871 btrfs_free_delayed_extent_op(head
->extent_op
);
5872 head
->extent_op
= NULL
;
5876 * waiting for the lock here would deadlock. If someone else has it
5877 * locked they are already in the process of dropping it anyway
5879 if (!mutex_trylock(&head
->mutex
))
5883 * at this point we have a head with no other entries. Go
5884 * ahead and process it.
5886 head
->node
.in_tree
= 0;
5887 rb_erase(&head
->node
.rb_node
, &delayed_refs
->root
);
5889 delayed_refs
->num_entries
--;
5892 * we don't take a ref on the node because we're removing it from the
5893 * tree, so we just steal the ref the tree was holding.
5895 delayed_refs
->num_heads
--;
5896 if (list_empty(&head
->cluster
))
5897 delayed_refs
->num_heads_ready
--;
5899 list_del_init(&head
->cluster
);
5900 spin_unlock(&delayed_refs
->lock
);
5902 BUG_ON(head
->extent_op
);
5903 if (head
->must_insert_reserved
)
5906 mutex_unlock(&head
->mutex
);
5907 btrfs_put_delayed_ref(&head
->node
);
5910 spin_unlock(&delayed_refs
->lock
);
5914 void btrfs_free_tree_block(struct btrfs_trans_handle
*trans
,
5915 struct btrfs_root
*root
,
5916 struct extent_buffer
*buf
,
5917 u64 parent
, int last_ref
)
5919 struct btrfs_block_group_cache
*cache
= NULL
;
5923 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
5924 ret
= btrfs_add_delayed_tree_ref(root
->fs_info
, trans
,
5925 buf
->start
, buf
->len
,
5926 parent
, root
->root_key
.objectid
,
5927 btrfs_header_level(buf
),
5928 BTRFS_DROP_DELAYED_REF
, NULL
, 0);
5929 BUG_ON(ret
); /* -ENOMEM */
5935 cache
= btrfs_lookup_block_group(root
->fs_info
, buf
->start
);
5937 if (btrfs_header_generation(buf
) == trans
->transid
) {
5938 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
5939 ret
= check_ref_cleanup(trans
, root
, buf
->start
);
5944 if (btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
)) {
5945 pin_down_extent(root
, cache
, buf
->start
, buf
->len
, 1);
5949 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY
, &buf
->bflags
));
5951 btrfs_add_free_space(cache
, buf
->start
, buf
->len
);
5952 btrfs_update_reserved_bytes(cache
, buf
->len
, RESERVE_FREE
);
5957 add_pinned_bytes(root
->fs_info
, buf
->len
,
5958 btrfs_header_level(buf
),
5959 root
->root_key
.objectid
);
5962 * Deleting the buffer, clear the corrupt flag since it doesn't matter
5965 clear_bit(EXTENT_BUFFER_CORRUPT
, &buf
->bflags
);
5966 btrfs_put_block_group(cache
);
5969 /* Can return -ENOMEM */
5970 int btrfs_free_extent(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
5971 u64 bytenr
, u64 num_bytes
, u64 parent
, u64 root_objectid
,
5972 u64 owner
, u64 offset
, int for_cow
)
5975 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5977 add_pinned_bytes(root
->fs_info
, num_bytes
, owner
, root_objectid
);
5980 * tree log blocks never actually go into the extent allocation
5981 * tree, just update pinning info and exit early.
5983 if (root_objectid
== BTRFS_TREE_LOG_OBJECTID
) {
5984 WARN_ON(owner
>= BTRFS_FIRST_FREE_OBJECTID
);
5985 /* unlocks the pinned mutex */
5986 btrfs_pin_extent(root
, bytenr
, num_bytes
, 1);
5988 } else if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
5989 ret
= btrfs_add_delayed_tree_ref(fs_info
, trans
, bytenr
,
5991 parent
, root_objectid
, (int)owner
,
5992 BTRFS_DROP_DELAYED_REF
, NULL
, for_cow
);
5994 ret
= btrfs_add_delayed_data_ref(fs_info
, trans
, bytenr
,
5996 parent
, root_objectid
, owner
,
5997 offset
, BTRFS_DROP_DELAYED_REF
,
6003 static u64
stripe_align(struct btrfs_root
*root
,
6004 struct btrfs_block_group_cache
*cache
,
6005 u64 val
, u64 num_bytes
)
6007 u64 ret
= ALIGN(val
, root
->stripesize
);
6012 * when we wait for progress in the block group caching, its because
6013 * our allocation attempt failed at least once. So, we must sleep
6014 * and let some progress happen before we try again.
6016 * This function will sleep at least once waiting for new free space to
6017 * show up, and then it will check the block group free space numbers
6018 * for our min num_bytes. Another option is to have it go ahead
6019 * and look in the rbtree for a free extent of a given size, but this
6023 wait_block_group_cache_progress(struct btrfs_block_group_cache
*cache
,
6026 struct btrfs_caching_control
*caching_ctl
;
6028 caching_ctl
= get_caching_control(cache
);
6032 wait_event(caching_ctl
->wait
, block_group_cache_done(cache
) ||
6033 (cache
->free_space_ctl
->free_space
>= num_bytes
));
6035 put_caching_control(caching_ctl
);
6040 wait_block_group_cache_done(struct btrfs_block_group_cache
*cache
)
6042 struct btrfs_caching_control
*caching_ctl
;
6044 caching_ctl
= get_caching_control(cache
);
6048 wait_event(caching_ctl
->wait
, block_group_cache_done(cache
));
6050 put_caching_control(caching_ctl
);
6054 int __get_raid_index(u64 flags
)
6056 if (flags
& BTRFS_BLOCK_GROUP_RAID10
)
6057 return BTRFS_RAID_RAID10
;
6058 else if (flags
& BTRFS_BLOCK_GROUP_RAID1
)
6059 return BTRFS_RAID_RAID1
;
6060 else if (flags
& BTRFS_BLOCK_GROUP_DUP
)
6061 return BTRFS_RAID_DUP
;
6062 else if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
6063 return BTRFS_RAID_RAID0
;
6064 else if (flags
& BTRFS_BLOCK_GROUP_RAID5
)
6065 return BTRFS_RAID_RAID5
;
6066 else if (flags
& BTRFS_BLOCK_GROUP_RAID6
)
6067 return BTRFS_RAID_RAID6
;
6069 return BTRFS_RAID_SINGLE
; /* BTRFS_BLOCK_GROUP_SINGLE */
6072 static int get_block_group_index(struct btrfs_block_group_cache
*cache
)
6074 return __get_raid_index(cache
->flags
);
6077 enum btrfs_loop_type
{
6078 LOOP_CACHING_NOWAIT
= 0,
6079 LOOP_CACHING_WAIT
= 1,
6080 LOOP_ALLOC_CHUNK
= 2,
6081 LOOP_NO_EMPTY_SIZE
= 3,
6085 * walks the btree of allocated extents and find a hole of a given size.
6086 * The key ins is changed to record the hole:
6087 * ins->objectid == block start
6088 * ins->flags = BTRFS_EXTENT_ITEM_KEY
6089 * ins->offset == number of blocks
6090 * Any available blocks before search_start are skipped.
6092 static noinline
int find_free_extent(struct btrfs_trans_handle
*trans
,
6093 struct btrfs_root
*orig_root
,
6094 u64 num_bytes
, u64 empty_size
,
6095 u64 hint_byte
, struct btrfs_key
*ins
,
6099 struct btrfs_root
*root
= orig_root
->fs_info
->extent_root
;
6100 struct btrfs_free_cluster
*last_ptr
= NULL
;
6101 struct btrfs_block_group_cache
*block_group
= NULL
;
6102 struct btrfs_block_group_cache
*used_block_group
;
6103 u64 search_start
= 0;
6104 int empty_cluster
= 2 * 1024 * 1024;
6105 struct btrfs_space_info
*space_info
;
6107 int index
= __get_raid_index(flags
);
6108 int alloc_type
= (flags
& BTRFS_BLOCK_GROUP_DATA
) ?
6109 RESERVE_ALLOC_NO_ACCOUNT
: RESERVE_ALLOC
;
6110 bool found_uncached_bg
= false;
6111 bool failed_cluster_refill
= false;
6112 bool failed_alloc
= false;
6113 bool use_cluster
= true;
6114 bool have_caching_bg
= false;
6116 WARN_ON(num_bytes
< root
->sectorsize
);
6117 btrfs_set_key_type(ins
, BTRFS_EXTENT_ITEM_KEY
);
6121 trace_find_free_extent(orig_root
, num_bytes
, empty_size
, flags
);
6123 space_info
= __find_space_info(root
->fs_info
, flags
);
6125 btrfs_err(root
->fs_info
, "No space info for %llu", flags
);
6130 * If the space info is for both data and metadata it means we have a
6131 * small filesystem and we can't use the clustering stuff.
6133 if (btrfs_mixed_space_info(space_info
))
6134 use_cluster
= false;
6136 if (flags
& BTRFS_BLOCK_GROUP_METADATA
&& use_cluster
) {
6137 last_ptr
= &root
->fs_info
->meta_alloc_cluster
;
6138 if (!btrfs_test_opt(root
, SSD
))
6139 empty_cluster
= 64 * 1024;
6142 if ((flags
& BTRFS_BLOCK_GROUP_DATA
) && use_cluster
&&
6143 btrfs_test_opt(root
, SSD
)) {
6144 last_ptr
= &root
->fs_info
->data_alloc_cluster
;
6148 spin_lock(&last_ptr
->lock
);
6149 if (last_ptr
->block_group
)
6150 hint_byte
= last_ptr
->window_start
;
6151 spin_unlock(&last_ptr
->lock
);
6154 search_start
= max(search_start
, first_logical_byte(root
, 0));
6155 search_start
= max(search_start
, hint_byte
);
6160 if (search_start
== hint_byte
) {
6161 block_group
= btrfs_lookup_block_group(root
->fs_info
,
6163 used_block_group
= block_group
;
6165 * we don't want to use the block group if it doesn't match our
6166 * allocation bits, or if its not cached.
6168 * However if we are re-searching with an ideal block group
6169 * picked out then we don't care that the block group is cached.
6171 if (block_group
&& block_group_bits(block_group
, flags
) &&
6172 block_group
->cached
!= BTRFS_CACHE_NO
) {
6173 down_read(&space_info
->groups_sem
);
6174 if (list_empty(&block_group
->list
) ||
6177 * someone is removing this block group,
6178 * we can't jump into the have_block_group
6179 * target because our list pointers are not
6182 btrfs_put_block_group(block_group
);
6183 up_read(&space_info
->groups_sem
);
6185 index
= get_block_group_index(block_group
);
6186 goto have_block_group
;
6188 } else if (block_group
) {
6189 btrfs_put_block_group(block_group
);
6193 have_caching_bg
= false;
6194 down_read(&space_info
->groups_sem
);
6195 list_for_each_entry(block_group
, &space_info
->block_groups
[index
],
6200 used_block_group
= block_group
;
6201 btrfs_get_block_group(block_group
);
6202 search_start
= block_group
->key
.objectid
;
6205 * this can happen if we end up cycling through all the
6206 * raid types, but we want to make sure we only allocate
6207 * for the proper type.
6209 if (!block_group_bits(block_group
, flags
)) {
6210 u64 extra
= BTRFS_BLOCK_GROUP_DUP
|
6211 BTRFS_BLOCK_GROUP_RAID1
|
6212 BTRFS_BLOCK_GROUP_RAID5
|
6213 BTRFS_BLOCK_GROUP_RAID6
|
6214 BTRFS_BLOCK_GROUP_RAID10
;
6217 * if they asked for extra copies and this block group
6218 * doesn't provide them, bail. This does allow us to
6219 * fill raid0 from raid1.
6221 if ((flags
& extra
) && !(block_group
->flags
& extra
))
6226 cached
= block_group_cache_done(block_group
);
6227 if (unlikely(!cached
)) {
6228 found_uncached_bg
= true;
6229 ret
= cache_block_group(block_group
, 0);
6234 if (unlikely(block_group
->ro
))
6238 * Ok we want to try and use the cluster allocator, so
6242 unsigned long aligned_cluster
;
6244 * the refill lock keeps out other
6245 * people trying to start a new cluster
6247 spin_lock(&last_ptr
->refill_lock
);
6248 used_block_group
= last_ptr
->block_group
;
6249 if (used_block_group
!= block_group
&&
6250 (!used_block_group
||
6251 used_block_group
->ro
||
6252 !block_group_bits(used_block_group
, flags
))) {
6253 used_block_group
= block_group
;
6254 goto refill_cluster
;
6257 if (used_block_group
!= block_group
)
6258 btrfs_get_block_group(used_block_group
);
6260 offset
= btrfs_alloc_from_cluster(used_block_group
,
6261 last_ptr
, num_bytes
, used_block_group
->key
.objectid
);
6263 /* we have a block, we're done */
6264 spin_unlock(&last_ptr
->refill_lock
);
6265 trace_btrfs_reserve_extent_cluster(root
,
6266 block_group
, search_start
, num_bytes
);
6270 WARN_ON(last_ptr
->block_group
!= used_block_group
);
6271 if (used_block_group
!= block_group
) {
6272 btrfs_put_block_group(used_block_group
);
6273 used_block_group
= block_group
;
6276 BUG_ON(used_block_group
!= block_group
);
6277 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
6278 * set up a new clusters, so lets just skip it
6279 * and let the allocator find whatever block
6280 * it can find. If we reach this point, we
6281 * will have tried the cluster allocator
6282 * plenty of times and not have found
6283 * anything, so we are likely way too
6284 * fragmented for the clustering stuff to find
6287 * However, if the cluster is taken from the
6288 * current block group, release the cluster
6289 * first, so that we stand a better chance of
6290 * succeeding in the unclustered
6292 if (loop
>= LOOP_NO_EMPTY_SIZE
&&
6293 last_ptr
->block_group
!= block_group
) {
6294 spin_unlock(&last_ptr
->refill_lock
);
6295 goto unclustered_alloc
;
6299 * this cluster didn't work out, free it and
6302 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
6304 if (loop
>= LOOP_NO_EMPTY_SIZE
) {
6305 spin_unlock(&last_ptr
->refill_lock
);
6306 goto unclustered_alloc
;
6309 aligned_cluster
= max_t(unsigned long,
6310 empty_cluster
+ empty_size
,
6311 block_group
->full_stripe_len
);
6313 /* allocate a cluster in this block group */
6314 ret
= btrfs_find_space_cluster(trans
, root
,
6315 block_group
, last_ptr
,
6316 search_start
, num_bytes
,
6320 * now pull our allocation out of this
6323 offset
= btrfs_alloc_from_cluster(block_group
,
6324 last_ptr
, num_bytes
,
6327 /* we found one, proceed */
6328 spin_unlock(&last_ptr
->refill_lock
);
6329 trace_btrfs_reserve_extent_cluster(root
,
6330 block_group
, search_start
,
6334 } else if (!cached
&& loop
> LOOP_CACHING_NOWAIT
6335 && !failed_cluster_refill
) {
6336 spin_unlock(&last_ptr
->refill_lock
);
6338 failed_cluster_refill
= true;
6339 wait_block_group_cache_progress(block_group
,
6340 num_bytes
+ empty_cluster
+ empty_size
);
6341 goto have_block_group
;
6345 * at this point we either didn't find a cluster
6346 * or we weren't able to allocate a block from our
6347 * cluster. Free the cluster we've been trying
6348 * to use, and go to the next block group
6350 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
6351 spin_unlock(&last_ptr
->refill_lock
);
6356 spin_lock(&block_group
->free_space_ctl
->tree_lock
);
6358 block_group
->free_space_ctl
->free_space
<
6359 num_bytes
+ empty_cluster
+ empty_size
) {
6360 spin_unlock(&block_group
->free_space_ctl
->tree_lock
);
6363 spin_unlock(&block_group
->free_space_ctl
->tree_lock
);
6365 offset
= btrfs_find_space_for_alloc(block_group
, search_start
,
6366 num_bytes
, empty_size
);
6368 * If we didn't find a chunk, and we haven't failed on this
6369 * block group before, and this block group is in the middle of
6370 * caching and we are ok with waiting, then go ahead and wait
6371 * for progress to be made, and set failed_alloc to true.
6373 * If failed_alloc is true then we've already waited on this
6374 * block group once and should move on to the next block group.
6376 if (!offset
&& !failed_alloc
&& !cached
&&
6377 loop
> LOOP_CACHING_NOWAIT
) {
6378 wait_block_group_cache_progress(block_group
,
6379 num_bytes
+ empty_size
);
6380 failed_alloc
= true;
6381 goto have_block_group
;
6382 } else if (!offset
) {
6384 have_caching_bg
= true;
6388 search_start
= stripe_align(root
, used_block_group
,
6391 /* move on to the next group */
6392 if (search_start
+ num_bytes
>
6393 used_block_group
->key
.objectid
+ used_block_group
->key
.offset
) {
6394 btrfs_add_free_space(used_block_group
, offset
, num_bytes
);
6398 if (offset
< search_start
)
6399 btrfs_add_free_space(used_block_group
, offset
,
6400 search_start
- offset
);
6401 BUG_ON(offset
> search_start
);
6403 ret
= btrfs_update_reserved_bytes(used_block_group
, num_bytes
,
6405 if (ret
== -EAGAIN
) {
6406 btrfs_add_free_space(used_block_group
, offset
, num_bytes
);
6410 /* we are all good, lets return */
6411 ins
->objectid
= search_start
;
6412 ins
->offset
= num_bytes
;
6414 trace_btrfs_reserve_extent(orig_root
, block_group
,
6415 search_start
, num_bytes
);
6416 if (used_block_group
!= block_group
)
6417 btrfs_put_block_group(used_block_group
);
6418 btrfs_put_block_group(block_group
);
6421 failed_cluster_refill
= false;
6422 failed_alloc
= false;
6423 BUG_ON(index
!= get_block_group_index(block_group
));
6424 if (used_block_group
!= block_group
)
6425 btrfs_put_block_group(used_block_group
);
6426 btrfs_put_block_group(block_group
);
6428 up_read(&space_info
->groups_sem
);
6430 if (!ins
->objectid
&& loop
>= LOOP_CACHING_WAIT
&& have_caching_bg
)
6433 if (!ins
->objectid
&& ++index
< BTRFS_NR_RAID_TYPES
)
6437 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
6438 * caching kthreads as we move along
6439 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
6440 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
6441 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
6444 if (!ins
->objectid
&& loop
< LOOP_NO_EMPTY_SIZE
) {
6447 if (loop
== LOOP_ALLOC_CHUNK
) {
6448 ret
= do_chunk_alloc(trans
, root
, flags
,
6451 * Do not bail out on ENOSPC since we
6452 * can do more things.
6454 if (ret
< 0 && ret
!= -ENOSPC
) {
6455 btrfs_abort_transaction(trans
,
6461 if (loop
== LOOP_NO_EMPTY_SIZE
) {
6467 } else if (!ins
->objectid
) {
6469 } else if (ins
->objectid
) {
6477 static void dump_space_info(struct btrfs_space_info
*info
, u64 bytes
,
6478 int dump_block_groups
)
6480 struct btrfs_block_group_cache
*cache
;
6483 spin_lock(&info
->lock
);
6484 printk(KERN_INFO
"space_info %llu has %llu free, is %sfull\n",
6485 (unsigned long long)info
->flags
,
6486 (unsigned long long)(info
->total_bytes
- info
->bytes_used
-
6487 info
->bytes_pinned
- info
->bytes_reserved
-
6488 info
->bytes_readonly
),
6489 (info
->full
) ? "" : "not ");
6490 printk(KERN_INFO
"space_info total=%llu, used=%llu, pinned=%llu, "
6491 "reserved=%llu, may_use=%llu, readonly=%llu\n",
6492 (unsigned long long)info
->total_bytes
,
6493 (unsigned long long)info
->bytes_used
,
6494 (unsigned long long)info
->bytes_pinned
,
6495 (unsigned long long)info
->bytes_reserved
,
6496 (unsigned long long)info
->bytes_may_use
,
6497 (unsigned long long)info
->bytes_readonly
);
6498 spin_unlock(&info
->lock
);
6500 if (!dump_block_groups
)
6503 down_read(&info
->groups_sem
);
6505 list_for_each_entry(cache
, &info
->block_groups
[index
], list
) {
6506 spin_lock(&cache
->lock
);
6507 printk(KERN_INFO
"block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
6508 (unsigned long long)cache
->key
.objectid
,
6509 (unsigned long long)cache
->key
.offset
,
6510 (unsigned long long)btrfs_block_group_used(&cache
->item
),
6511 (unsigned long long)cache
->pinned
,
6512 (unsigned long long)cache
->reserved
,
6513 cache
->ro
? "[readonly]" : "");
6514 btrfs_dump_free_space(cache
, bytes
);
6515 spin_unlock(&cache
->lock
);
6517 if (++index
< BTRFS_NR_RAID_TYPES
)
6519 up_read(&info
->groups_sem
);
6522 int btrfs_reserve_extent(struct btrfs_trans_handle
*trans
,
6523 struct btrfs_root
*root
,
6524 u64 num_bytes
, u64 min_alloc_size
,
6525 u64 empty_size
, u64 hint_byte
,
6526 struct btrfs_key
*ins
, int is_data
)
6528 bool final_tried
= false;
6532 flags
= btrfs_get_alloc_profile(root
, is_data
);
6534 WARN_ON(num_bytes
< root
->sectorsize
);
6535 ret
= find_free_extent(trans
, root
, num_bytes
, empty_size
,
6536 hint_byte
, ins
, flags
);
6538 if (ret
== -ENOSPC
) {
6540 num_bytes
= num_bytes
>> 1;
6541 num_bytes
= round_down(num_bytes
, root
->sectorsize
);
6542 num_bytes
= max(num_bytes
, min_alloc_size
);
6543 if (num_bytes
== min_alloc_size
)
6546 } else if (btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
6547 struct btrfs_space_info
*sinfo
;
6549 sinfo
= __find_space_info(root
->fs_info
, flags
);
6550 btrfs_err(root
->fs_info
, "allocation failed flags %llu, wanted %llu",
6551 (unsigned long long)flags
,
6552 (unsigned long long)num_bytes
);
6554 dump_space_info(sinfo
, num_bytes
, 1);
6558 trace_btrfs_reserved_extent_alloc(root
, ins
->objectid
, ins
->offset
);
6563 static int __btrfs_free_reserved_extent(struct btrfs_root
*root
,
6564 u64 start
, u64 len
, int pin
)
6566 struct btrfs_block_group_cache
*cache
;
6569 cache
= btrfs_lookup_block_group(root
->fs_info
, start
);
6571 btrfs_err(root
->fs_info
, "Unable to find block group for %llu",
6572 (unsigned long long)start
);
6576 if (btrfs_test_opt(root
, DISCARD
))
6577 ret
= btrfs_discard_extent(root
, start
, len
, NULL
);
6580 pin_down_extent(root
, cache
, start
, len
, 1);
6582 btrfs_add_free_space(cache
, start
, len
);
6583 btrfs_update_reserved_bytes(cache
, len
, RESERVE_FREE
);
6585 btrfs_put_block_group(cache
);
6587 trace_btrfs_reserved_extent_free(root
, start
, len
);
6592 int btrfs_free_reserved_extent(struct btrfs_root
*root
,
6595 return __btrfs_free_reserved_extent(root
, start
, len
, 0);
6598 int btrfs_free_and_pin_reserved_extent(struct btrfs_root
*root
,
6601 return __btrfs_free_reserved_extent(root
, start
, len
, 1);
6604 static int alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
6605 struct btrfs_root
*root
,
6606 u64 parent
, u64 root_objectid
,
6607 u64 flags
, u64 owner
, u64 offset
,
6608 struct btrfs_key
*ins
, int ref_mod
)
6611 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
6612 struct btrfs_extent_item
*extent_item
;
6613 struct btrfs_extent_inline_ref
*iref
;
6614 struct btrfs_path
*path
;
6615 struct extent_buffer
*leaf
;
6620 type
= BTRFS_SHARED_DATA_REF_KEY
;
6622 type
= BTRFS_EXTENT_DATA_REF_KEY
;
6624 size
= sizeof(*extent_item
) + btrfs_extent_inline_ref_size(type
);
6626 path
= btrfs_alloc_path();
6630 path
->leave_spinning
= 1;
6631 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
6634 btrfs_free_path(path
);
6638 leaf
= path
->nodes
[0];
6639 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
6640 struct btrfs_extent_item
);
6641 btrfs_set_extent_refs(leaf
, extent_item
, ref_mod
);
6642 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
6643 btrfs_set_extent_flags(leaf
, extent_item
,
6644 flags
| BTRFS_EXTENT_FLAG_DATA
);
6646 iref
= (struct btrfs_extent_inline_ref
*)(extent_item
+ 1);
6647 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
6649 struct btrfs_shared_data_ref
*ref
;
6650 ref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
6651 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
6652 btrfs_set_shared_data_ref_count(leaf
, ref
, ref_mod
);
6654 struct btrfs_extent_data_ref
*ref
;
6655 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
6656 btrfs_set_extent_data_ref_root(leaf
, ref
, root_objectid
);
6657 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
6658 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
6659 btrfs_set_extent_data_ref_count(leaf
, ref
, ref_mod
);
6662 btrfs_mark_buffer_dirty(path
->nodes
[0]);
6663 btrfs_free_path(path
);
6665 ret
= update_block_group(root
, ins
->objectid
, ins
->offset
, 1);
6666 if (ret
) { /* -ENOENT, logic error */
6667 btrfs_err(fs_info
, "update block group failed for %llu %llu",
6668 (unsigned long long)ins
->objectid
,
6669 (unsigned long long)ins
->offset
);
6675 static int alloc_reserved_tree_block(struct btrfs_trans_handle
*trans
,
6676 struct btrfs_root
*root
,
6677 u64 parent
, u64 root_objectid
,
6678 u64 flags
, struct btrfs_disk_key
*key
,
6679 int level
, struct btrfs_key
*ins
)
6682 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
6683 struct btrfs_extent_item
*extent_item
;
6684 struct btrfs_tree_block_info
*block_info
;
6685 struct btrfs_extent_inline_ref
*iref
;
6686 struct btrfs_path
*path
;
6687 struct extent_buffer
*leaf
;
6688 u32 size
= sizeof(*extent_item
) + sizeof(*iref
);
6689 bool skinny_metadata
= btrfs_fs_incompat(root
->fs_info
,
6692 if (!skinny_metadata
)
6693 size
+= sizeof(*block_info
);
6695 path
= btrfs_alloc_path();
6699 path
->leave_spinning
= 1;
6700 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
6703 btrfs_free_path(path
);
6707 leaf
= path
->nodes
[0];
6708 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
6709 struct btrfs_extent_item
);
6710 btrfs_set_extent_refs(leaf
, extent_item
, 1);
6711 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
6712 btrfs_set_extent_flags(leaf
, extent_item
,
6713 flags
| BTRFS_EXTENT_FLAG_TREE_BLOCK
);
6715 if (skinny_metadata
) {
6716 iref
= (struct btrfs_extent_inline_ref
*)(extent_item
+ 1);
6718 block_info
= (struct btrfs_tree_block_info
*)(extent_item
+ 1);
6719 btrfs_set_tree_block_key(leaf
, block_info
, key
);
6720 btrfs_set_tree_block_level(leaf
, block_info
, level
);
6721 iref
= (struct btrfs_extent_inline_ref
*)(block_info
+ 1);
6725 BUG_ON(!(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
));
6726 btrfs_set_extent_inline_ref_type(leaf
, iref
,
6727 BTRFS_SHARED_BLOCK_REF_KEY
);
6728 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
6730 btrfs_set_extent_inline_ref_type(leaf
, iref
,
6731 BTRFS_TREE_BLOCK_REF_KEY
);
6732 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
6735 btrfs_mark_buffer_dirty(leaf
);
6736 btrfs_free_path(path
);
6738 ret
= update_block_group(root
, ins
->objectid
, root
->leafsize
, 1);
6739 if (ret
) { /* -ENOENT, logic error */
6740 btrfs_err(fs_info
, "update block group failed for %llu %llu",
6741 (unsigned long long)ins
->objectid
,
6742 (unsigned long long)ins
->offset
);
6748 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
6749 struct btrfs_root
*root
,
6750 u64 root_objectid
, u64 owner
,
6751 u64 offset
, struct btrfs_key
*ins
)
6755 BUG_ON(root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
6757 ret
= btrfs_add_delayed_data_ref(root
->fs_info
, trans
, ins
->objectid
,
6759 root_objectid
, owner
, offset
,
6760 BTRFS_ADD_DELAYED_EXTENT
, NULL
, 0);
6765 * this is used by the tree logging recovery code. It records that
6766 * an extent has been allocated and makes sure to clear the free
6767 * space cache bits as well
6769 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle
*trans
,
6770 struct btrfs_root
*root
,
6771 u64 root_objectid
, u64 owner
, u64 offset
,
6772 struct btrfs_key
*ins
)
6775 struct btrfs_block_group_cache
*block_group
;
6778 * Mixed block groups will exclude before processing the log so we only
6779 * need to do the exlude dance if this fs isn't mixed.
6781 if (!btrfs_fs_incompat(root
->fs_info
, MIXED_GROUPS
)) {
6782 ret
= __exclude_logged_extent(root
, ins
->objectid
, ins
->offset
);
6787 block_group
= btrfs_lookup_block_group(root
->fs_info
, ins
->objectid
);
6791 ret
= btrfs_update_reserved_bytes(block_group
, ins
->offset
,
6792 RESERVE_ALLOC_NO_ACCOUNT
);
6793 BUG_ON(ret
); /* logic error */
6794 ret
= alloc_reserved_file_extent(trans
, root
, 0, root_objectid
,
6795 0, owner
, offset
, ins
, 1);
6796 btrfs_put_block_group(block_group
);
6800 static struct extent_buffer
*
6801 btrfs_init_new_buffer(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
6802 u64 bytenr
, u32 blocksize
, int level
)
6804 struct extent_buffer
*buf
;
6806 buf
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
6808 return ERR_PTR(-ENOMEM
);
6809 btrfs_set_header_generation(buf
, trans
->transid
);
6810 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, buf
, level
);
6811 btrfs_tree_lock(buf
);
6812 clean_tree_block(trans
, root
, buf
);
6813 clear_bit(EXTENT_BUFFER_STALE
, &buf
->bflags
);
6815 btrfs_set_lock_blocking(buf
);
6816 btrfs_set_buffer_uptodate(buf
);
6818 if (root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
) {
6820 * we allow two log transactions at a time, use different
6821 * EXENT bit to differentiate dirty pages.
6823 if (root
->log_transid
% 2 == 0)
6824 set_extent_dirty(&root
->dirty_log_pages
, buf
->start
,
6825 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
6827 set_extent_new(&root
->dirty_log_pages
, buf
->start
,
6828 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
6830 set_extent_dirty(&trans
->transaction
->dirty_pages
, buf
->start
,
6831 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
6833 trans
->blocks_used
++;
6834 /* this returns a buffer locked for blocking */
6838 static struct btrfs_block_rsv
*
6839 use_block_rsv(struct btrfs_trans_handle
*trans
,
6840 struct btrfs_root
*root
, u32 blocksize
)
6842 struct btrfs_block_rsv
*block_rsv
;
6843 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
6845 bool global_updated
= false;
6847 block_rsv
= get_block_rsv(trans
, root
);
6849 if (unlikely(block_rsv
->size
== 0))
6852 ret
= block_rsv_use_bytes(block_rsv
, blocksize
);
6856 if (block_rsv
->failfast
)
6857 return ERR_PTR(ret
);
6859 if (block_rsv
->type
== BTRFS_BLOCK_RSV_GLOBAL
&& !global_updated
) {
6860 global_updated
= true;
6861 update_global_block_rsv(root
->fs_info
);
6865 if (btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
6866 static DEFINE_RATELIMIT_STATE(_rs
,
6867 DEFAULT_RATELIMIT_INTERVAL
* 10,
6868 /*DEFAULT_RATELIMIT_BURST*/ 1);
6869 if (__ratelimit(&_rs
))
6871 "btrfs: block rsv returned %d\n", ret
);
6874 ret
= reserve_metadata_bytes(root
, block_rsv
, blocksize
,
6875 BTRFS_RESERVE_NO_FLUSH
);
6879 * If we couldn't reserve metadata bytes try and use some from
6880 * the global reserve if its space type is the same as the global
6883 if (block_rsv
->type
!= BTRFS_BLOCK_RSV_GLOBAL
&&
6884 block_rsv
->space_info
== global_rsv
->space_info
) {
6885 ret
= block_rsv_use_bytes(global_rsv
, blocksize
);
6889 return ERR_PTR(ret
);
6892 static void unuse_block_rsv(struct btrfs_fs_info
*fs_info
,
6893 struct btrfs_block_rsv
*block_rsv
, u32 blocksize
)
6895 block_rsv_add_bytes(block_rsv
, blocksize
, 0);
6896 block_rsv_release_bytes(fs_info
, block_rsv
, NULL
, 0);
6900 * finds a free extent and does all the dirty work required for allocation
6901 * returns the key for the extent through ins, and a tree buffer for
6902 * the first block of the extent through buf.
6904 * returns the tree buffer or NULL.
6906 struct extent_buffer
*btrfs_alloc_free_block(struct btrfs_trans_handle
*trans
,
6907 struct btrfs_root
*root
, u32 blocksize
,
6908 u64 parent
, u64 root_objectid
,
6909 struct btrfs_disk_key
*key
, int level
,
6910 u64 hint
, u64 empty_size
)
6912 struct btrfs_key ins
;
6913 struct btrfs_block_rsv
*block_rsv
;
6914 struct extent_buffer
*buf
;
6917 bool skinny_metadata
= btrfs_fs_incompat(root
->fs_info
,
6920 block_rsv
= use_block_rsv(trans
, root
, blocksize
);
6921 if (IS_ERR(block_rsv
))
6922 return ERR_CAST(block_rsv
);
6924 ret
= btrfs_reserve_extent(trans
, root
, blocksize
, blocksize
,
6925 empty_size
, hint
, &ins
, 0);
6927 unuse_block_rsv(root
->fs_info
, block_rsv
, blocksize
);
6928 return ERR_PTR(ret
);
6931 buf
= btrfs_init_new_buffer(trans
, root
, ins
.objectid
,
6933 BUG_ON(IS_ERR(buf
)); /* -ENOMEM */
6935 if (root_objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
6937 parent
= ins
.objectid
;
6938 flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
6942 if (root_objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
6943 struct btrfs_delayed_extent_op
*extent_op
;
6944 extent_op
= btrfs_alloc_delayed_extent_op();
6945 BUG_ON(!extent_op
); /* -ENOMEM */
6947 memcpy(&extent_op
->key
, key
, sizeof(extent_op
->key
));
6949 memset(&extent_op
->key
, 0, sizeof(extent_op
->key
));
6950 extent_op
->flags_to_set
= flags
;
6951 if (skinny_metadata
)
6952 extent_op
->update_key
= 0;
6954 extent_op
->update_key
= 1;
6955 extent_op
->update_flags
= 1;
6956 extent_op
->is_data
= 0;
6957 extent_op
->level
= level
;
6959 ret
= btrfs_add_delayed_tree_ref(root
->fs_info
, trans
,
6961 ins
.offset
, parent
, root_objectid
,
6962 level
, BTRFS_ADD_DELAYED_EXTENT
,
6964 BUG_ON(ret
); /* -ENOMEM */
6969 struct walk_control
{
6970 u64 refs
[BTRFS_MAX_LEVEL
];
6971 u64 flags
[BTRFS_MAX_LEVEL
];
6972 struct btrfs_key update_progress
;
6983 #define DROP_REFERENCE 1
6984 #define UPDATE_BACKREF 2
6986 static noinline
void reada_walk_down(struct btrfs_trans_handle
*trans
,
6987 struct btrfs_root
*root
,
6988 struct walk_control
*wc
,
6989 struct btrfs_path
*path
)
6997 struct btrfs_key key
;
6998 struct extent_buffer
*eb
;
7003 if (path
->slots
[wc
->level
] < wc
->reada_slot
) {
7004 wc
->reada_count
= wc
->reada_count
* 2 / 3;
7005 wc
->reada_count
= max(wc
->reada_count
, 2);
7007 wc
->reada_count
= wc
->reada_count
* 3 / 2;
7008 wc
->reada_count
= min_t(int, wc
->reada_count
,
7009 BTRFS_NODEPTRS_PER_BLOCK(root
));
7012 eb
= path
->nodes
[wc
->level
];
7013 nritems
= btrfs_header_nritems(eb
);
7014 blocksize
= btrfs_level_size(root
, wc
->level
- 1);
7016 for (slot
= path
->slots
[wc
->level
]; slot
< nritems
; slot
++) {
7017 if (nread
>= wc
->reada_count
)
7021 bytenr
= btrfs_node_blockptr(eb
, slot
);
7022 generation
= btrfs_node_ptr_generation(eb
, slot
);
7024 if (slot
== path
->slots
[wc
->level
])
7027 if (wc
->stage
== UPDATE_BACKREF
&&
7028 generation
<= root
->root_key
.offset
)
7031 /* We don't lock the tree block, it's OK to be racy here */
7032 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
,
7033 wc
->level
- 1, 1, &refs
,
7035 /* We don't care about errors in readahead. */
7040 if (wc
->stage
== DROP_REFERENCE
) {
7044 if (wc
->level
== 1 &&
7045 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
7047 if (!wc
->update_ref
||
7048 generation
<= root
->root_key
.offset
)
7050 btrfs_node_key_to_cpu(eb
, &key
, slot
);
7051 ret
= btrfs_comp_cpu_keys(&key
,
7052 &wc
->update_progress
);
7056 if (wc
->level
== 1 &&
7057 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
7061 ret
= readahead_tree_block(root
, bytenr
, blocksize
,
7067 wc
->reada_slot
= slot
;
7071 * helper to process tree block while walking down the tree.
7073 * when wc->stage == UPDATE_BACKREF, this function updates
7074 * back refs for pointers in the block.
7076 * NOTE: return value 1 means we should stop walking down.
7078 static noinline
int walk_down_proc(struct btrfs_trans_handle
*trans
,
7079 struct btrfs_root
*root
,
7080 struct btrfs_path
*path
,
7081 struct walk_control
*wc
, int lookup_info
)
7083 int level
= wc
->level
;
7084 struct extent_buffer
*eb
= path
->nodes
[level
];
7085 u64 flag
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
7088 if (wc
->stage
== UPDATE_BACKREF
&&
7089 btrfs_header_owner(eb
) != root
->root_key
.objectid
)
7093 * when reference count of tree block is 1, it won't increase
7094 * again. once full backref flag is set, we never clear it.
7097 ((wc
->stage
== DROP_REFERENCE
&& wc
->refs
[level
] != 1) ||
7098 (wc
->stage
== UPDATE_BACKREF
&& !(wc
->flags
[level
] & flag
)))) {
7099 BUG_ON(!path
->locks
[level
]);
7100 ret
= btrfs_lookup_extent_info(trans
, root
,
7101 eb
->start
, level
, 1,
7104 BUG_ON(ret
== -ENOMEM
);
7107 BUG_ON(wc
->refs
[level
] == 0);
7110 if (wc
->stage
== DROP_REFERENCE
) {
7111 if (wc
->refs
[level
] > 1)
7114 if (path
->locks
[level
] && !wc
->keep_locks
) {
7115 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
7116 path
->locks
[level
] = 0;
7121 /* wc->stage == UPDATE_BACKREF */
7122 if (!(wc
->flags
[level
] & flag
)) {
7123 BUG_ON(!path
->locks
[level
]);
7124 ret
= btrfs_inc_ref(trans
, root
, eb
, 1, wc
->for_reloc
);
7125 BUG_ON(ret
); /* -ENOMEM */
7126 ret
= btrfs_dec_ref(trans
, root
, eb
, 0, wc
->for_reloc
);
7127 BUG_ON(ret
); /* -ENOMEM */
7128 ret
= btrfs_set_disk_extent_flags(trans
, root
, eb
->start
,
7130 btrfs_header_level(eb
), 0);
7131 BUG_ON(ret
); /* -ENOMEM */
7132 wc
->flags
[level
] |= flag
;
7136 * the block is shared by multiple trees, so it's not good to
7137 * keep the tree lock
7139 if (path
->locks
[level
] && level
> 0) {
7140 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
7141 path
->locks
[level
] = 0;
7147 * helper to process tree block pointer.
7149 * when wc->stage == DROP_REFERENCE, this function checks
7150 * reference count of the block pointed to. if the block
7151 * is shared and we need update back refs for the subtree
7152 * rooted at the block, this function changes wc->stage to
7153 * UPDATE_BACKREF. if the block is shared and there is no
7154 * need to update back, this function drops the reference
7157 * NOTE: return value 1 means we should stop walking down.
7159 static noinline
int do_walk_down(struct btrfs_trans_handle
*trans
,
7160 struct btrfs_root
*root
,
7161 struct btrfs_path
*path
,
7162 struct walk_control
*wc
, int *lookup_info
)
7168 struct btrfs_key key
;
7169 struct extent_buffer
*next
;
7170 int level
= wc
->level
;
7174 generation
= btrfs_node_ptr_generation(path
->nodes
[level
],
7175 path
->slots
[level
]);
7177 * if the lower level block was created before the snapshot
7178 * was created, we know there is no need to update back refs
7181 if (wc
->stage
== UPDATE_BACKREF
&&
7182 generation
<= root
->root_key
.offset
) {
7187 bytenr
= btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]);
7188 blocksize
= btrfs_level_size(root
, level
- 1);
7190 next
= btrfs_find_tree_block(root
, bytenr
, blocksize
);
7192 next
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
7195 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, next
,
7199 btrfs_tree_lock(next
);
7200 btrfs_set_lock_blocking(next
);
7202 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
, level
- 1, 1,
7203 &wc
->refs
[level
- 1],
7204 &wc
->flags
[level
- 1]);
7206 btrfs_tree_unlock(next
);
7210 if (unlikely(wc
->refs
[level
- 1] == 0)) {
7211 btrfs_err(root
->fs_info
, "Missing references.");
7216 if (wc
->stage
== DROP_REFERENCE
) {
7217 if (wc
->refs
[level
- 1] > 1) {
7219 (wc
->flags
[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF
))
7222 if (!wc
->update_ref
||
7223 generation
<= root
->root_key
.offset
)
7226 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
7227 path
->slots
[level
]);
7228 ret
= btrfs_comp_cpu_keys(&key
, &wc
->update_progress
);
7232 wc
->stage
= UPDATE_BACKREF
;
7233 wc
->shared_level
= level
- 1;
7237 (wc
->flags
[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF
))
7241 if (!btrfs_buffer_uptodate(next
, generation
, 0)) {
7242 btrfs_tree_unlock(next
);
7243 free_extent_buffer(next
);
7249 if (reada
&& level
== 1)
7250 reada_walk_down(trans
, root
, wc
, path
);
7251 next
= read_tree_block(root
, bytenr
, blocksize
, generation
);
7252 if (!next
|| !extent_buffer_uptodate(next
)) {
7253 free_extent_buffer(next
);
7256 btrfs_tree_lock(next
);
7257 btrfs_set_lock_blocking(next
);
7261 BUG_ON(level
!= btrfs_header_level(next
));
7262 path
->nodes
[level
] = next
;
7263 path
->slots
[level
] = 0;
7264 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
7270 wc
->refs
[level
- 1] = 0;
7271 wc
->flags
[level
- 1] = 0;
7272 if (wc
->stage
== DROP_REFERENCE
) {
7273 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
) {
7274 parent
= path
->nodes
[level
]->start
;
7276 BUG_ON(root
->root_key
.objectid
!=
7277 btrfs_header_owner(path
->nodes
[level
]));
7281 ret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
, parent
,
7282 root
->root_key
.objectid
, level
- 1, 0, 0);
7283 BUG_ON(ret
); /* -ENOMEM */
7285 btrfs_tree_unlock(next
);
7286 free_extent_buffer(next
);
7292 * helper to process tree block while walking up the tree.
7294 * when wc->stage == DROP_REFERENCE, this function drops
7295 * reference count on the block.
7297 * when wc->stage == UPDATE_BACKREF, this function changes
7298 * wc->stage back to DROP_REFERENCE if we changed wc->stage
7299 * to UPDATE_BACKREF previously while processing the block.
7301 * NOTE: return value 1 means we should stop walking up.
7303 static noinline
int walk_up_proc(struct btrfs_trans_handle
*trans
,
7304 struct btrfs_root
*root
,
7305 struct btrfs_path
*path
,
7306 struct walk_control
*wc
)
7309 int level
= wc
->level
;
7310 struct extent_buffer
*eb
= path
->nodes
[level
];
7313 if (wc
->stage
== UPDATE_BACKREF
) {
7314 BUG_ON(wc
->shared_level
< level
);
7315 if (level
< wc
->shared_level
)
7318 ret
= find_next_key(path
, level
+ 1, &wc
->update_progress
);
7322 wc
->stage
= DROP_REFERENCE
;
7323 wc
->shared_level
= -1;
7324 path
->slots
[level
] = 0;
7327 * check reference count again if the block isn't locked.
7328 * we should start walking down the tree again if reference
7331 if (!path
->locks
[level
]) {
7333 btrfs_tree_lock(eb
);
7334 btrfs_set_lock_blocking(eb
);
7335 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
7337 ret
= btrfs_lookup_extent_info(trans
, root
,
7338 eb
->start
, level
, 1,
7342 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
7343 path
->locks
[level
] = 0;
7346 BUG_ON(wc
->refs
[level
] == 0);
7347 if (wc
->refs
[level
] == 1) {
7348 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
7349 path
->locks
[level
] = 0;
7355 /* wc->stage == DROP_REFERENCE */
7356 BUG_ON(wc
->refs
[level
] > 1 && !path
->locks
[level
]);
7358 if (wc
->refs
[level
] == 1) {
7360 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
7361 ret
= btrfs_dec_ref(trans
, root
, eb
, 1,
7364 ret
= btrfs_dec_ref(trans
, root
, eb
, 0,
7366 BUG_ON(ret
); /* -ENOMEM */
7368 /* make block locked assertion in clean_tree_block happy */
7369 if (!path
->locks
[level
] &&
7370 btrfs_header_generation(eb
) == trans
->transid
) {
7371 btrfs_tree_lock(eb
);
7372 btrfs_set_lock_blocking(eb
);
7373 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
7375 clean_tree_block(trans
, root
, eb
);
7378 if (eb
== root
->node
) {
7379 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
7382 BUG_ON(root
->root_key
.objectid
!=
7383 btrfs_header_owner(eb
));
7385 if (wc
->flags
[level
+ 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
7386 parent
= path
->nodes
[level
+ 1]->start
;
7388 BUG_ON(root
->root_key
.objectid
!=
7389 btrfs_header_owner(path
->nodes
[level
+ 1]));
7392 btrfs_free_tree_block(trans
, root
, eb
, parent
, wc
->refs
[level
] == 1);
7394 wc
->refs
[level
] = 0;
7395 wc
->flags
[level
] = 0;
7399 static noinline
int walk_down_tree(struct btrfs_trans_handle
*trans
,
7400 struct btrfs_root
*root
,
7401 struct btrfs_path
*path
,
7402 struct walk_control
*wc
)
7404 int level
= wc
->level
;
7405 int lookup_info
= 1;
7408 while (level
>= 0) {
7409 ret
= walk_down_proc(trans
, root
, path
, wc
, lookup_info
);
7416 if (path
->slots
[level
] >=
7417 btrfs_header_nritems(path
->nodes
[level
]))
7420 ret
= do_walk_down(trans
, root
, path
, wc
, &lookup_info
);
7422 path
->slots
[level
]++;
7431 static noinline
int walk_up_tree(struct btrfs_trans_handle
*trans
,
7432 struct btrfs_root
*root
,
7433 struct btrfs_path
*path
,
7434 struct walk_control
*wc
, int max_level
)
7436 int level
= wc
->level
;
7439 path
->slots
[level
] = btrfs_header_nritems(path
->nodes
[level
]);
7440 while (level
< max_level
&& path
->nodes
[level
]) {
7442 if (path
->slots
[level
] + 1 <
7443 btrfs_header_nritems(path
->nodes
[level
])) {
7444 path
->slots
[level
]++;
7447 ret
= walk_up_proc(trans
, root
, path
, wc
);
7451 if (path
->locks
[level
]) {
7452 btrfs_tree_unlock_rw(path
->nodes
[level
],
7453 path
->locks
[level
]);
7454 path
->locks
[level
] = 0;
7456 free_extent_buffer(path
->nodes
[level
]);
7457 path
->nodes
[level
] = NULL
;
7465 * drop a subvolume tree.
7467 * this function traverses the tree freeing any blocks that only
7468 * referenced by the tree.
7470 * when a shared tree block is found. this function decreases its
7471 * reference count by one. if update_ref is true, this function
7472 * also make sure backrefs for the shared block and all lower level
7473 * blocks are properly updated.
7475 * If called with for_reloc == 0, may exit early with -EAGAIN
7477 int btrfs_drop_snapshot(struct btrfs_root
*root
,
7478 struct btrfs_block_rsv
*block_rsv
, int update_ref
,
7481 struct btrfs_path
*path
;
7482 struct btrfs_trans_handle
*trans
;
7483 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
7484 struct btrfs_root_item
*root_item
= &root
->root_item
;
7485 struct walk_control
*wc
;
7486 struct btrfs_key key
;
7490 bool root_dropped
= false;
7492 path
= btrfs_alloc_path();
7498 wc
= kzalloc(sizeof(*wc
), GFP_NOFS
);
7500 btrfs_free_path(path
);
7505 trans
= btrfs_start_transaction(tree_root
, 0);
7506 if (IS_ERR(trans
)) {
7507 err
= PTR_ERR(trans
);
7512 trans
->block_rsv
= block_rsv
;
7514 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
7515 level
= btrfs_header_level(root
->node
);
7516 path
->nodes
[level
] = btrfs_lock_root_node(root
);
7517 btrfs_set_lock_blocking(path
->nodes
[level
]);
7518 path
->slots
[level
] = 0;
7519 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
7520 memset(&wc
->update_progress
, 0,
7521 sizeof(wc
->update_progress
));
7523 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
7524 memcpy(&wc
->update_progress
, &key
,
7525 sizeof(wc
->update_progress
));
7527 level
= root_item
->drop_level
;
7529 path
->lowest_level
= level
;
7530 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
7531 path
->lowest_level
= 0;
7539 * unlock our path, this is safe because only this
7540 * function is allowed to delete this snapshot
7542 btrfs_unlock_up_safe(path
, 0);
7544 level
= btrfs_header_level(root
->node
);
7546 btrfs_tree_lock(path
->nodes
[level
]);
7547 btrfs_set_lock_blocking(path
->nodes
[level
]);
7548 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
7550 ret
= btrfs_lookup_extent_info(trans
, root
,
7551 path
->nodes
[level
]->start
,
7552 level
, 1, &wc
->refs
[level
],
7558 BUG_ON(wc
->refs
[level
] == 0);
7560 if (level
== root_item
->drop_level
)
7563 btrfs_tree_unlock(path
->nodes
[level
]);
7564 path
->locks
[level
] = 0;
7565 WARN_ON(wc
->refs
[level
] != 1);
7571 wc
->shared_level
= -1;
7572 wc
->stage
= DROP_REFERENCE
;
7573 wc
->update_ref
= update_ref
;
7575 wc
->for_reloc
= for_reloc
;
7576 wc
->reada_count
= BTRFS_NODEPTRS_PER_BLOCK(root
);
7580 ret
= walk_down_tree(trans
, root
, path
, wc
);
7586 ret
= walk_up_tree(trans
, root
, path
, wc
, BTRFS_MAX_LEVEL
);
7593 BUG_ON(wc
->stage
!= DROP_REFERENCE
);
7597 if (wc
->stage
== DROP_REFERENCE
) {
7599 btrfs_node_key(path
->nodes
[level
],
7600 &root_item
->drop_progress
,
7601 path
->slots
[level
]);
7602 root_item
->drop_level
= level
;
7605 BUG_ON(wc
->level
== 0);
7606 if (btrfs_should_end_transaction(trans
, tree_root
) ||
7607 (!for_reloc
&& btrfs_need_cleaner_sleep(root
))) {
7608 ret
= btrfs_update_root(trans
, tree_root
,
7612 btrfs_abort_transaction(trans
, tree_root
, ret
);
7617 btrfs_end_transaction_throttle(trans
, tree_root
);
7618 if (!for_reloc
&& btrfs_need_cleaner_sleep(root
)) {
7619 pr_debug("btrfs: drop snapshot early exit\n");
7624 trans
= btrfs_start_transaction(tree_root
, 0);
7625 if (IS_ERR(trans
)) {
7626 err
= PTR_ERR(trans
);
7630 trans
->block_rsv
= block_rsv
;
7633 btrfs_release_path(path
);
7637 ret
= btrfs_del_root(trans
, tree_root
, &root
->root_key
);
7639 btrfs_abort_transaction(trans
, tree_root
, ret
);
7643 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
7644 ret
= btrfs_find_root(tree_root
, &root
->root_key
, path
,
7647 btrfs_abort_transaction(trans
, tree_root
, ret
);
7650 } else if (ret
> 0) {
7651 /* if we fail to delete the orphan item this time
7652 * around, it'll get picked up the next time.
7654 * The most common failure here is just -ENOENT.
7656 btrfs_del_orphan_item(trans
, tree_root
,
7657 root
->root_key
.objectid
);
7661 if (root
->in_radix
) {
7662 btrfs_drop_and_free_fs_root(tree_root
->fs_info
, root
);
7664 free_extent_buffer(root
->node
);
7665 free_extent_buffer(root
->commit_root
);
7666 btrfs_put_fs_root(root
);
7668 root_dropped
= true;
7670 btrfs_end_transaction_throttle(trans
, tree_root
);
7673 btrfs_free_path(path
);
7676 * So if we need to stop dropping the snapshot for whatever reason we
7677 * need to make sure to add it back to the dead root list so that we
7678 * keep trying to do the work later. This also cleans up roots if we
7679 * don't have it in the radix (like when we recover after a power fail
7680 * or unmount) so we don't leak memory.
7682 if (root_dropped
== false)
7683 btrfs_add_dead_root(root
);
7685 btrfs_std_error(root
->fs_info
, err
);
7690 * drop subtree rooted at tree block 'node'.
7692 * NOTE: this function will unlock and release tree block 'node'
7693 * only used by relocation code
7695 int btrfs_drop_subtree(struct btrfs_trans_handle
*trans
,
7696 struct btrfs_root
*root
,
7697 struct extent_buffer
*node
,
7698 struct extent_buffer
*parent
)
7700 struct btrfs_path
*path
;
7701 struct walk_control
*wc
;
7707 BUG_ON(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
7709 path
= btrfs_alloc_path();
7713 wc
= kzalloc(sizeof(*wc
), GFP_NOFS
);
7715 btrfs_free_path(path
);
7719 btrfs_assert_tree_locked(parent
);
7720 parent_level
= btrfs_header_level(parent
);
7721 extent_buffer_get(parent
);
7722 path
->nodes
[parent_level
] = parent
;
7723 path
->slots
[parent_level
] = btrfs_header_nritems(parent
);
7725 btrfs_assert_tree_locked(node
);
7726 level
= btrfs_header_level(node
);
7727 path
->nodes
[level
] = node
;
7728 path
->slots
[level
] = 0;
7729 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
7731 wc
->refs
[parent_level
] = 1;
7732 wc
->flags
[parent_level
] = BTRFS_BLOCK_FLAG_FULL_BACKREF
;
7734 wc
->shared_level
= -1;
7735 wc
->stage
= DROP_REFERENCE
;
7739 wc
->reada_count
= BTRFS_NODEPTRS_PER_BLOCK(root
);
7742 wret
= walk_down_tree(trans
, root
, path
, wc
);
7748 wret
= walk_up_tree(trans
, root
, path
, wc
, parent_level
);
7756 btrfs_free_path(path
);
7760 static u64
update_block_group_flags(struct btrfs_root
*root
, u64 flags
)
7766 * if restripe for this chunk_type is on pick target profile and
7767 * return, otherwise do the usual balance
7769 stripped
= get_restripe_target(root
->fs_info
, flags
);
7771 return extended_to_chunk(stripped
);
7774 * we add in the count of missing devices because we want
7775 * to make sure that any RAID levels on a degraded FS
7776 * continue to be honored.
7778 num_devices
= root
->fs_info
->fs_devices
->rw_devices
+
7779 root
->fs_info
->fs_devices
->missing_devices
;
7781 stripped
= BTRFS_BLOCK_GROUP_RAID0
|
7782 BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
|
7783 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
;
7785 if (num_devices
== 1) {
7786 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
7787 stripped
= flags
& ~stripped
;
7789 /* turn raid0 into single device chunks */
7790 if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
7793 /* turn mirroring into duplication */
7794 if (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
7795 BTRFS_BLOCK_GROUP_RAID10
))
7796 return stripped
| BTRFS_BLOCK_GROUP_DUP
;
7798 /* they already had raid on here, just return */
7799 if (flags
& stripped
)
7802 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
7803 stripped
= flags
& ~stripped
;
7805 /* switch duplicated blocks with raid1 */
7806 if (flags
& BTRFS_BLOCK_GROUP_DUP
)
7807 return stripped
| BTRFS_BLOCK_GROUP_RAID1
;
7809 /* this is drive concat, leave it alone */
7815 static int set_block_group_ro(struct btrfs_block_group_cache
*cache
, int force
)
7817 struct btrfs_space_info
*sinfo
= cache
->space_info
;
7819 u64 min_allocable_bytes
;
7824 * We need some metadata space and system metadata space for
7825 * allocating chunks in some corner cases until we force to set
7826 * it to be readonly.
7829 (BTRFS_BLOCK_GROUP_SYSTEM
| BTRFS_BLOCK_GROUP_METADATA
)) &&
7831 min_allocable_bytes
= 1 * 1024 * 1024;
7833 min_allocable_bytes
= 0;
7835 spin_lock(&sinfo
->lock
);
7836 spin_lock(&cache
->lock
);
7843 num_bytes
= cache
->key
.offset
- cache
->reserved
- cache
->pinned
-
7844 cache
->bytes_super
- btrfs_block_group_used(&cache
->item
);
7846 if (sinfo
->bytes_used
+ sinfo
->bytes_reserved
+ sinfo
->bytes_pinned
+
7847 sinfo
->bytes_may_use
+ sinfo
->bytes_readonly
+ num_bytes
+
7848 min_allocable_bytes
<= sinfo
->total_bytes
) {
7849 sinfo
->bytes_readonly
+= num_bytes
;
7854 spin_unlock(&cache
->lock
);
7855 spin_unlock(&sinfo
->lock
);
7859 int btrfs_set_block_group_ro(struct btrfs_root
*root
,
7860 struct btrfs_block_group_cache
*cache
)
7863 struct btrfs_trans_handle
*trans
;
7869 trans
= btrfs_join_transaction(root
);
7871 return PTR_ERR(trans
);
7873 alloc_flags
= update_block_group_flags(root
, cache
->flags
);
7874 if (alloc_flags
!= cache
->flags
) {
7875 ret
= do_chunk_alloc(trans
, root
, alloc_flags
,
7881 ret
= set_block_group_ro(cache
, 0);
7884 alloc_flags
= get_alloc_profile(root
, cache
->space_info
->flags
);
7885 ret
= do_chunk_alloc(trans
, root
, alloc_flags
,
7889 ret
= set_block_group_ro(cache
, 0);
7891 btrfs_end_transaction(trans
, root
);
7895 int btrfs_force_chunk_alloc(struct btrfs_trans_handle
*trans
,
7896 struct btrfs_root
*root
, u64 type
)
7898 u64 alloc_flags
= get_alloc_profile(root
, type
);
7899 return do_chunk_alloc(trans
, root
, alloc_flags
,
7904 * helper to account the unused space of all the readonly block group in the
7905 * list. takes mirrors into account.
7907 static u64
__btrfs_get_ro_block_group_free_space(struct list_head
*groups_list
)
7909 struct btrfs_block_group_cache
*block_group
;
7913 list_for_each_entry(block_group
, groups_list
, list
) {
7914 spin_lock(&block_group
->lock
);
7916 if (!block_group
->ro
) {
7917 spin_unlock(&block_group
->lock
);
7921 if (block_group
->flags
& (BTRFS_BLOCK_GROUP_RAID1
|
7922 BTRFS_BLOCK_GROUP_RAID10
|
7923 BTRFS_BLOCK_GROUP_DUP
))
7928 free_bytes
+= (block_group
->key
.offset
-
7929 btrfs_block_group_used(&block_group
->item
)) *
7932 spin_unlock(&block_group
->lock
);
7939 * helper to account the unused space of all the readonly block group in the
7940 * space_info. takes mirrors into account.
7942 u64
btrfs_account_ro_block_groups_free_space(struct btrfs_space_info
*sinfo
)
7947 spin_lock(&sinfo
->lock
);
7949 for(i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++)
7950 if (!list_empty(&sinfo
->block_groups
[i
]))
7951 free_bytes
+= __btrfs_get_ro_block_group_free_space(
7952 &sinfo
->block_groups
[i
]);
7954 spin_unlock(&sinfo
->lock
);
7959 void btrfs_set_block_group_rw(struct btrfs_root
*root
,
7960 struct btrfs_block_group_cache
*cache
)
7962 struct btrfs_space_info
*sinfo
= cache
->space_info
;
7967 spin_lock(&sinfo
->lock
);
7968 spin_lock(&cache
->lock
);
7969 num_bytes
= cache
->key
.offset
- cache
->reserved
- cache
->pinned
-
7970 cache
->bytes_super
- btrfs_block_group_used(&cache
->item
);
7971 sinfo
->bytes_readonly
-= num_bytes
;
7973 spin_unlock(&cache
->lock
);
7974 spin_unlock(&sinfo
->lock
);
7978 * checks to see if its even possible to relocate this block group.
7980 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7981 * ok to go ahead and try.
7983 int btrfs_can_relocate(struct btrfs_root
*root
, u64 bytenr
)
7985 struct btrfs_block_group_cache
*block_group
;
7986 struct btrfs_space_info
*space_info
;
7987 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
7988 struct btrfs_device
*device
;
7989 struct btrfs_trans_handle
*trans
;
7998 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
8000 /* odd, couldn't find the block group, leave it alone */
8004 min_free
= btrfs_block_group_used(&block_group
->item
);
8006 /* no bytes used, we're good */
8010 space_info
= block_group
->space_info
;
8011 spin_lock(&space_info
->lock
);
8013 full
= space_info
->full
;
8016 * if this is the last block group we have in this space, we can't
8017 * relocate it unless we're able to allocate a new chunk below.
8019 * Otherwise, we need to make sure we have room in the space to handle
8020 * all of the extents from this block group. If we can, we're good
8022 if ((space_info
->total_bytes
!= block_group
->key
.offset
) &&
8023 (space_info
->bytes_used
+ space_info
->bytes_reserved
+
8024 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
8025 min_free
< space_info
->total_bytes
)) {
8026 spin_unlock(&space_info
->lock
);
8029 spin_unlock(&space_info
->lock
);
8032 * ok we don't have enough space, but maybe we have free space on our
8033 * devices to allocate new chunks for relocation, so loop through our
8034 * alloc devices and guess if we have enough space. if this block
8035 * group is going to be restriped, run checks against the target
8036 * profile instead of the current one.
8048 target
= get_restripe_target(root
->fs_info
, block_group
->flags
);
8050 index
= __get_raid_index(extended_to_chunk(target
));
8053 * this is just a balance, so if we were marked as full
8054 * we know there is no space for a new chunk
8059 index
= get_block_group_index(block_group
);
8062 if (index
== BTRFS_RAID_RAID10
) {
8066 } else if (index
== BTRFS_RAID_RAID1
) {
8068 } else if (index
== BTRFS_RAID_DUP
) {
8071 } else if (index
== BTRFS_RAID_RAID0
) {
8072 dev_min
= fs_devices
->rw_devices
;
8073 do_div(min_free
, dev_min
);
8076 /* We need to do this so that we can look at pending chunks */
8077 trans
= btrfs_join_transaction(root
);
8078 if (IS_ERR(trans
)) {
8079 ret
= PTR_ERR(trans
);
8083 mutex_lock(&root
->fs_info
->chunk_mutex
);
8084 list_for_each_entry(device
, &fs_devices
->alloc_list
, dev_alloc_list
) {
8088 * check to make sure we can actually find a chunk with enough
8089 * space to fit our block group in.
8091 if (device
->total_bytes
> device
->bytes_used
+ min_free
&&
8092 !device
->is_tgtdev_for_dev_replace
) {
8093 ret
= find_free_dev_extent(trans
, device
, min_free
,
8098 if (dev_nr
>= dev_min
)
8104 mutex_unlock(&root
->fs_info
->chunk_mutex
);
8105 btrfs_end_transaction(trans
, root
);
8107 btrfs_put_block_group(block_group
);
8111 static int find_first_block_group(struct btrfs_root
*root
,
8112 struct btrfs_path
*path
, struct btrfs_key
*key
)
8115 struct btrfs_key found_key
;
8116 struct extent_buffer
*leaf
;
8119 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
8124 slot
= path
->slots
[0];
8125 leaf
= path
->nodes
[0];
8126 if (slot
>= btrfs_header_nritems(leaf
)) {
8127 ret
= btrfs_next_leaf(root
, path
);
8134 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
8136 if (found_key
.objectid
>= key
->objectid
&&
8137 found_key
.type
== BTRFS_BLOCK_GROUP_ITEM_KEY
) {
8147 void btrfs_put_block_group_cache(struct btrfs_fs_info
*info
)
8149 struct btrfs_block_group_cache
*block_group
;
8153 struct inode
*inode
;
8155 block_group
= btrfs_lookup_first_block_group(info
, last
);
8156 while (block_group
) {
8157 spin_lock(&block_group
->lock
);
8158 if (block_group
->iref
)
8160 spin_unlock(&block_group
->lock
);
8161 block_group
= next_block_group(info
->tree_root
,
8171 inode
= block_group
->inode
;
8172 block_group
->iref
= 0;
8173 block_group
->inode
= NULL
;
8174 spin_unlock(&block_group
->lock
);
8176 last
= block_group
->key
.objectid
+ block_group
->key
.offset
;
8177 btrfs_put_block_group(block_group
);
8181 int btrfs_free_block_groups(struct btrfs_fs_info
*info
)
8183 struct btrfs_block_group_cache
*block_group
;
8184 struct btrfs_space_info
*space_info
;
8185 struct btrfs_caching_control
*caching_ctl
;
8188 down_write(&info
->extent_commit_sem
);
8189 while (!list_empty(&info
->caching_block_groups
)) {
8190 caching_ctl
= list_entry(info
->caching_block_groups
.next
,
8191 struct btrfs_caching_control
, list
);
8192 list_del(&caching_ctl
->list
);
8193 put_caching_control(caching_ctl
);
8195 up_write(&info
->extent_commit_sem
);
8197 spin_lock(&info
->block_group_cache_lock
);
8198 while ((n
= rb_last(&info
->block_group_cache_tree
)) != NULL
) {
8199 block_group
= rb_entry(n
, struct btrfs_block_group_cache
,
8201 rb_erase(&block_group
->cache_node
,
8202 &info
->block_group_cache_tree
);
8203 spin_unlock(&info
->block_group_cache_lock
);
8205 down_write(&block_group
->space_info
->groups_sem
);
8206 list_del(&block_group
->list
);
8207 up_write(&block_group
->space_info
->groups_sem
);
8209 if (block_group
->cached
== BTRFS_CACHE_STARTED
)
8210 wait_block_group_cache_done(block_group
);
8213 * We haven't cached this block group, which means we could
8214 * possibly have excluded extents on this block group.
8216 if (block_group
->cached
== BTRFS_CACHE_NO
)
8217 free_excluded_extents(info
->extent_root
, block_group
);
8219 btrfs_remove_free_space_cache(block_group
);
8220 btrfs_put_block_group(block_group
);
8222 spin_lock(&info
->block_group_cache_lock
);
8224 spin_unlock(&info
->block_group_cache_lock
);
8226 /* now that all the block groups are freed, go through and
8227 * free all the space_info structs. This is only called during
8228 * the final stages of unmount, and so we know nobody is
8229 * using them. We call synchronize_rcu() once before we start,
8230 * just to be on the safe side.
8234 release_global_block_rsv(info
);
8236 while(!list_empty(&info
->space_info
)) {
8237 space_info
= list_entry(info
->space_info
.next
,
8238 struct btrfs_space_info
,
8240 if (btrfs_test_opt(info
->tree_root
, ENOSPC_DEBUG
)) {
8241 if (space_info
->bytes_pinned
> 0 ||
8242 space_info
->bytes_reserved
> 0 ||
8243 space_info
->bytes_may_use
> 0) {
8245 dump_space_info(space_info
, 0, 0);
8248 percpu_counter_destroy(&space_info
->total_bytes_pinned
);
8249 list_del(&space_info
->list
);
8255 static void __link_block_group(struct btrfs_space_info
*space_info
,
8256 struct btrfs_block_group_cache
*cache
)
8258 int index
= get_block_group_index(cache
);
8260 down_write(&space_info
->groups_sem
);
8261 list_add_tail(&cache
->list
, &space_info
->block_groups
[index
]);
8262 up_write(&space_info
->groups_sem
);
8265 int btrfs_read_block_groups(struct btrfs_root
*root
)
8267 struct btrfs_path
*path
;
8269 struct btrfs_block_group_cache
*cache
;
8270 struct btrfs_fs_info
*info
= root
->fs_info
;
8271 struct btrfs_space_info
*space_info
;
8272 struct btrfs_key key
;
8273 struct btrfs_key found_key
;
8274 struct extent_buffer
*leaf
;
8278 root
= info
->extent_root
;
8281 btrfs_set_key_type(&key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
8282 path
= btrfs_alloc_path();
8287 cache_gen
= btrfs_super_cache_generation(root
->fs_info
->super_copy
);
8288 if (btrfs_test_opt(root
, SPACE_CACHE
) &&
8289 btrfs_super_generation(root
->fs_info
->super_copy
) != cache_gen
)
8291 if (btrfs_test_opt(root
, CLEAR_CACHE
))
8295 ret
= find_first_block_group(root
, path
, &key
);
8300 leaf
= path
->nodes
[0];
8301 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
8302 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
8307 cache
->free_space_ctl
= kzalloc(sizeof(*cache
->free_space_ctl
),
8309 if (!cache
->free_space_ctl
) {
8315 atomic_set(&cache
->count
, 1);
8316 spin_lock_init(&cache
->lock
);
8317 cache
->fs_info
= info
;
8318 INIT_LIST_HEAD(&cache
->list
);
8319 INIT_LIST_HEAD(&cache
->cluster_list
);
8323 * When we mount with old space cache, we need to
8324 * set BTRFS_DC_CLEAR and set dirty flag.
8326 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
8327 * truncate the old free space cache inode and
8329 * b) Setting 'dirty flag' makes sure that we flush
8330 * the new space cache info onto disk.
8332 cache
->disk_cache_state
= BTRFS_DC_CLEAR
;
8333 if (btrfs_test_opt(root
, SPACE_CACHE
))
8337 read_extent_buffer(leaf
, &cache
->item
,
8338 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
8339 sizeof(cache
->item
));
8340 memcpy(&cache
->key
, &found_key
, sizeof(found_key
));
8342 key
.objectid
= found_key
.objectid
+ found_key
.offset
;
8343 btrfs_release_path(path
);
8344 cache
->flags
= btrfs_block_group_flags(&cache
->item
);
8345 cache
->sectorsize
= root
->sectorsize
;
8346 cache
->full_stripe_len
= btrfs_full_stripe_len(root
,
8347 &root
->fs_info
->mapping_tree
,
8348 found_key
.objectid
);
8349 btrfs_init_free_space_ctl(cache
);
8352 * We need to exclude the super stripes now so that the space
8353 * info has super bytes accounted for, otherwise we'll think
8354 * we have more space than we actually do.
8356 ret
= exclude_super_stripes(root
, cache
);
8359 * We may have excluded something, so call this just in
8362 free_excluded_extents(root
, cache
);
8363 kfree(cache
->free_space_ctl
);
8369 * check for two cases, either we are full, and therefore
8370 * don't need to bother with the caching work since we won't
8371 * find any space, or we are empty, and we can just add all
8372 * the space in and be done with it. This saves us _alot_ of
8373 * time, particularly in the full case.
8375 if (found_key
.offset
== btrfs_block_group_used(&cache
->item
)) {
8376 cache
->last_byte_to_unpin
= (u64
)-1;
8377 cache
->cached
= BTRFS_CACHE_FINISHED
;
8378 free_excluded_extents(root
, cache
);
8379 } else if (btrfs_block_group_used(&cache
->item
) == 0) {
8380 cache
->last_byte_to_unpin
= (u64
)-1;
8381 cache
->cached
= BTRFS_CACHE_FINISHED
;
8382 add_new_free_space(cache
, root
->fs_info
,
8384 found_key
.objectid
+
8386 free_excluded_extents(root
, cache
);
8389 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
8391 btrfs_remove_free_space_cache(cache
);
8392 btrfs_put_block_group(cache
);
8396 ret
= update_space_info(info
, cache
->flags
, found_key
.offset
,
8397 btrfs_block_group_used(&cache
->item
),
8400 btrfs_remove_free_space_cache(cache
);
8401 spin_lock(&info
->block_group_cache_lock
);
8402 rb_erase(&cache
->cache_node
,
8403 &info
->block_group_cache_tree
);
8404 spin_unlock(&info
->block_group_cache_lock
);
8405 btrfs_put_block_group(cache
);
8409 cache
->space_info
= space_info
;
8410 spin_lock(&cache
->space_info
->lock
);
8411 cache
->space_info
->bytes_readonly
+= cache
->bytes_super
;
8412 spin_unlock(&cache
->space_info
->lock
);
8414 __link_block_group(space_info
, cache
);
8416 set_avail_alloc_bits(root
->fs_info
, cache
->flags
);
8417 if (btrfs_chunk_readonly(root
, cache
->key
.objectid
))
8418 set_block_group_ro(cache
, 1);
8421 list_for_each_entry_rcu(space_info
, &root
->fs_info
->space_info
, list
) {
8422 if (!(get_alloc_profile(root
, space_info
->flags
) &
8423 (BTRFS_BLOCK_GROUP_RAID10
|
8424 BTRFS_BLOCK_GROUP_RAID1
|
8425 BTRFS_BLOCK_GROUP_RAID5
|
8426 BTRFS_BLOCK_GROUP_RAID6
|
8427 BTRFS_BLOCK_GROUP_DUP
)))
8430 * avoid allocating from un-mirrored block group if there are
8431 * mirrored block groups.
8433 list_for_each_entry(cache
, &space_info
->block_groups
[3], list
)
8434 set_block_group_ro(cache
, 1);
8435 list_for_each_entry(cache
, &space_info
->block_groups
[4], list
)
8436 set_block_group_ro(cache
, 1);
8439 init_global_block_rsv(info
);
8442 btrfs_free_path(path
);
8446 void btrfs_create_pending_block_groups(struct btrfs_trans_handle
*trans
,
8447 struct btrfs_root
*root
)
8449 struct btrfs_block_group_cache
*block_group
, *tmp
;
8450 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
8451 struct btrfs_block_group_item item
;
8452 struct btrfs_key key
;
8455 list_for_each_entry_safe(block_group
, tmp
, &trans
->new_bgs
,
8457 list_del_init(&block_group
->new_bg_list
);
8462 spin_lock(&block_group
->lock
);
8463 memcpy(&item
, &block_group
->item
, sizeof(item
));
8464 memcpy(&key
, &block_group
->key
, sizeof(key
));
8465 spin_unlock(&block_group
->lock
);
8467 ret
= btrfs_insert_item(trans
, extent_root
, &key
, &item
,
8470 btrfs_abort_transaction(trans
, extent_root
, ret
);
8471 ret
= btrfs_finish_chunk_alloc(trans
, extent_root
,
8472 key
.objectid
, key
.offset
);
8474 btrfs_abort_transaction(trans
, extent_root
, ret
);
8478 int btrfs_make_block_group(struct btrfs_trans_handle
*trans
,
8479 struct btrfs_root
*root
, u64 bytes_used
,
8480 u64 type
, u64 chunk_objectid
, u64 chunk_offset
,
8484 struct btrfs_root
*extent_root
;
8485 struct btrfs_block_group_cache
*cache
;
8487 extent_root
= root
->fs_info
->extent_root
;
8489 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
8491 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
8494 cache
->free_space_ctl
= kzalloc(sizeof(*cache
->free_space_ctl
),
8496 if (!cache
->free_space_ctl
) {
8501 cache
->key
.objectid
= chunk_offset
;
8502 cache
->key
.offset
= size
;
8503 cache
->key
.type
= BTRFS_BLOCK_GROUP_ITEM_KEY
;
8504 cache
->sectorsize
= root
->sectorsize
;
8505 cache
->fs_info
= root
->fs_info
;
8506 cache
->full_stripe_len
= btrfs_full_stripe_len(root
,
8507 &root
->fs_info
->mapping_tree
,
8510 atomic_set(&cache
->count
, 1);
8511 spin_lock_init(&cache
->lock
);
8512 INIT_LIST_HEAD(&cache
->list
);
8513 INIT_LIST_HEAD(&cache
->cluster_list
);
8514 INIT_LIST_HEAD(&cache
->new_bg_list
);
8516 btrfs_init_free_space_ctl(cache
);
8518 btrfs_set_block_group_used(&cache
->item
, bytes_used
);
8519 btrfs_set_block_group_chunk_objectid(&cache
->item
, chunk_objectid
);
8520 cache
->flags
= type
;
8521 btrfs_set_block_group_flags(&cache
->item
, type
);
8523 cache
->last_byte_to_unpin
= (u64
)-1;
8524 cache
->cached
= BTRFS_CACHE_FINISHED
;
8525 ret
= exclude_super_stripes(root
, cache
);
8528 * We may have excluded something, so call this just in
8531 free_excluded_extents(root
, cache
);
8532 kfree(cache
->free_space_ctl
);
8537 add_new_free_space(cache
, root
->fs_info
, chunk_offset
,
8538 chunk_offset
+ size
);
8540 free_excluded_extents(root
, cache
);
8542 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
8544 btrfs_remove_free_space_cache(cache
);
8545 btrfs_put_block_group(cache
);
8549 ret
= update_space_info(root
->fs_info
, cache
->flags
, size
, bytes_used
,
8550 &cache
->space_info
);
8552 btrfs_remove_free_space_cache(cache
);
8553 spin_lock(&root
->fs_info
->block_group_cache_lock
);
8554 rb_erase(&cache
->cache_node
,
8555 &root
->fs_info
->block_group_cache_tree
);
8556 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
8557 btrfs_put_block_group(cache
);
8560 update_global_block_rsv(root
->fs_info
);
8562 spin_lock(&cache
->space_info
->lock
);
8563 cache
->space_info
->bytes_readonly
+= cache
->bytes_super
;
8564 spin_unlock(&cache
->space_info
->lock
);
8566 __link_block_group(cache
->space_info
, cache
);
8568 list_add_tail(&cache
->new_bg_list
, &trans
->new_bgs
);
8570 set_avail_alloc_bits(extent_root
->fs_info
, type
);
8575 static void clear_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
8577 u64 extra_flags
= chunk_to_extended(flags
) &
8578 BTRFS_EXTENDED_PROFILE_MASK
;
8580 write_seqlock(&fs_info
->profiles_lock
);
8581 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
8582 fs_info
->avail_data_alloc_bits
&= ~extra_flags
;
8583 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
8584 fs_info
->avail_metadata_alloc_bits
&= ~extra_flags
;
8585 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
8586 fs_info
->avail_system_alloc_bits
&= ~extra_flags
;
8587 write_sequnlock(&fs_info
->profiles_lock
);
8590 int btrfs_remove_block_group(struct btrfs_trans_handle
*trans
,
8591 struct btrfs_root
*root
, u64 group_start
)
8593 struct btrfs_path
*path
;
8594 struct btrfs_block_group_cache
*block_group
;
8595 struct btrfs_free_cluster
*cluster
;
8596 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
8597 struct btrfs_key key
;
8598 struct inode
*inode
;
8603 root
= root
->fs_info
->extent_root
;
8605 block_group
= btrfs_lookup_block_group(root
->fs_info
, group_start
);
8606 BUG_ON(!block_group
);
8607 BUG_ON(!block_group
->ro
);
8610 * Free the reserved super bytes from this block group before
8613 free_excluded_extents(root
, block_group
);
8615 memcpy(&key
, &block_group
->key
, sizeof(key
));
8616 index
= get_block_group_index(block_group
);
8617 if (block_group
->flags
& (BTRFS_BLOCK_GROUP_DUP
|
8618 BTRFS_BLOCK_GROUP_RAID1
|
8619 BTRFS_BLOCK_GROUP_RAID10
))
8624 /* make sure this block group isn't part of an allocation cluster */
8625 cluster
= &root
->fs_info
->data_alloc_cluster
;
8626 spin_lock(&cluster
->refill_lock
);
8627 btrfs_return_cluster_to_free_space(block_group
, cluster
);
8628 spin_unlock(&cluster
->refill_lock
);
8631 * make sure this block group isn't part of a metadata
8632 * allocation cluster
8634 cluster
= &root
->fs_info
->meta_alloc_cluster
;
8635 spin_lock(&cluster
->refill_lock
);
8636 btrfs_return_cluster_to_free_space(block_group
, cluster
);
8637 spin_unlock(&cluster
->refill_lock
);
8639 path
= btrfs_alloc_path();
8645 inode
= lookup_free_space_inode(tree_root
, block_group
, path
);
8646 if (!IS_ERR(inode
)) {
8647 ret
= btrfs_orphan_add(trans
, inode
);
8649 btrfs_add_delayed_iput(inode
);
8653 /* One for the block groups ref */
8654 spin_lock(&block_group
->lock
);
8655 if (block_group
->iref
) {
8656 block_group
->iref
= 0;
8657 block_group
->inode
= NULL
;
8658 spin_unlock(&block_group
->lock
);
8661 spin_unlock(&block_group
->lock
);
8663 /* One for our lookup ref */
8664 btrfs_add_delayed_iput(inode
);
8667 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
8668 key
.offset
= block_group
->key
.objectid
;
8671 ret
= btrfs_search_slot(trans
, tree_root
, &key
, path
, -1, 1);
8675 btrfs_release_path(path
);
8677 ret
= btrfs_del_item(trans
, tree_root
, path
);
8680 btrfs_release_path(path
);
8683 spin_lock(&root
->fs_info
->block_group_cache_lock
);
8684 rb_erase(&block_group
->cache_node
,
8685 &root
->fs_info
->block_group_cache_tree
);
8687 if (root
->fs_info
->first_logical_byte
== block_group
->key
.objectid
)
8688 root
->fs_info
->first_logical_byte
= (u64
)-1;
8689 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
8691 down_write(&block_group
->space_info
->groups_sem
);
8693 * we must use list_del_init so people can check to see if they
8694 * are still on the list after taking the semaphore
8696 list_del_init(&block_group
->list
);
8697 if (list_empty(&block_group
->space_info
->block_groups
[index
]))
8698 clear_avail_alloc_bits(root
->fs_info
, block_group
->flags
);
8699 up_write(&block_group
->space_info
->groups_sem
);
8701 if (block_group
->cached
== BTRFS_CACHE_STARTED
)
8702 wait_block_group_cache_done(block_group
);
8704 btrfs_remove_free_space_cache(block_group
);
8706 spin_lock(&block_group
->space_info
->lock
);
8707 block_group
->space_info
->total_bytes
-= block_group
->key
.offset
;
8708 block_group
->space_info
->bytes_readonly
-= block_group
->key
.offset
;
8709 block_group
->space_info
->disk_total
-= block_group
->key
.offset
* factor
;
8710 spin_unlock(&block_group
->space_info
->lock
);
8712 memcpy(&key
, &block_group
->key
, sizeof(key
));
8714 btrfs_clear_space_info_full(root
->fs_info
);
8716 btrfs_put_block_group(block_group
);
8717 btrfs_put_block_group(block_group
);
8719 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
8725 ret
= btrfs_del_item(trans
, root
, path
);
8727 btrfs_free_path(path
);
8731 int btrfs_init_space_info(struct btrfs_fs_info
*fs_info
)
8733 struct btrfs_space_info
*space_info
;
8734 struct btrfs_super_block
*disk_super
;
8740 disk_super
= fs_info
->super_copy
;
8741 if (!btrfs_super_root(disk_super
))
8744 features
= btrfs_super_incompat_flags(disk_super
);
8745 if (features
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
8748 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
8749 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
8754 flags
= BTRFS_BLOCK_GROUP_METADATA
| BTRFS_BLOCK_GROUP_DATA
;
8755 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
8757 flags
= BTRFS_BLOCK_GROUP_METADATA
;
8758 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
8762 flags
= BTRFS_BLOCK_GROUP_DATA
;
8763 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
8769 int btrfs_error_unpin_extent_range(struct btrfs_root
*root
, u64 start
, u64 end
)
8771 return unpin_extent_range(root
, start
, end
);
8774 int btrfs_error_discard_extent(struct btrfs_root
*root
, u64 bytenr
,
8775 u64 num_bytes
, u64
*actual_bytes
)
8777 return btrfs_discard_extent(root
, bytenr
, num_bytes
, actual_bytes
);
8780 int btrfs_trim_fs(struct btrfs_root
*root
, struct fstrim_range
*range
)
8782 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
8783 struct btrfs_block_group_cache
*cache
= NULL
;
8788 u64 total_bytes
= btrfs_super_total_bytes(fs_info
->super_copy
);
8792 * try to trim all FS space, our block group may start from non-zero.
8794 if (range
->len
== total_bytes
)
8795 cache
= btrfs_lookup_first_block_group(fs_info
, range
->start
);
8797 cache
= btrfs_lookup_block_group(fs_info
, range
->start
);
8800 if (cache
->key
.objectid
>= (range
->start
+ range
->len
)) {
8801 btrfs_put_block_group(cache
);
8805 start
= max(range
->start
, cache
->key
.objectid
);
8806 end
= min(range
->start
+ range
->len
,
8807 cache
->key
.objectid
+ cache
->key
.offset
);
8809 if (end
- start
>= range
->minlen
) {
8810 if (!block_group_cache_done(cache
)) {
8811 ret
= cache_block_group(cache
, 0);
8813 btrfs_put_block_group(cache
);
8816 ret
= wait_block_group_cache_done(cache
);
8818 btrfs_put_block_group(cache
);
8822 ret
= btrfs_trim_block_group(cache
,
8828 trimmed
+= group_trimmed
;
8830 btrfs_put_block_group(cache
);
8835 cache
= next_block_group(fs_info
->tree_root
, cache
);
8838 range
->len
= trimmed
;