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>
31 #include "print-tree.h"
35 #include "free-space-cache.h"
36 #include "free-space-tree.h"
41 #undef SCRAMBLE_DELAYED_REFS
44 * control flags for do_chunk_alloc's force field
45 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
46 * if we really need one.
48 * CHUNK_ALLOC_LIMITED means to only try and allocate one
49 * if we have very few chunks already allocated. This is
50 * used as part of the clustering code to help make sure
51 * we have a good pool of storage to cluster in, without
52 * filling the FS with empty chunks
54 * CHUNK_ALLOC_FORCE means it must try to allocate one
58 CHUNK_ALLOC_NO_FORCE
= 0,
59 CHUNK_ALLOC_LIMITED
= 1,
60 CHUNK_ALLOC_FORCE
= 2,
64 * Control how reservations are dealt with.
66 * RESERVE_FREE - freeing a reservation.
67 * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for
69 * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update
70 * bytes_may_use as the ENOSPC accounting is done elsewhere
75 RESERVE_ALLOC_NO_ACCOUNT
= 2,
78 static int update_block_group(struct btrfs_trans_handle
*trans
,
79 struct btrfs_root
*root
, u64 bytenr
,
80 u64 num_bytes
, int alloc
);
81 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
82 struct btrfs_root
*root
,
83 struct btrfs_delayed_ref_node
*node
, u64 parent
,
84 u64 root_objectid
, u64 owner_objectid
,
85 u64 owner_offset
, int refs_to_drop
,
86 struct btrfs_delayed_extent_op
*extra_op
);
87 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op
*extent_op
,
88 struct extent_buffer
*leaf
,
89 struct btrfs_extent_item
*ei
);
90 static int alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
91 struct btrfs_root
*root
,
92 u64 parent
, u64 root_objectid
,
93 u64 flags
, u64 owner
, u64 offset
,
94 struct btrfs_key
*ins
, int ref_mod
);
95 static int alloc_reserved_tree_block(struct btrfs_trans_handle
*trans
,
96 struct btrfs_root
*root
,
97 u64 parent
, u64 root_objectid
,
98 u64 flags
, struct btrfs_disk_key
*key
,
99 int level
, struct btrfs_key
*ins
);
100 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
101 struct btrfs_root
*extent_root
, u64 flags
,
103 static int find_next_key(struct btrfs_path
*path
, int level
,
104 struct btrfs_key
*key
);
105 static void dump_space_info(struct btrfs_space_info
*info
, u64 bytes
,
106 int dump_block_groups
);
107 static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache
*cache
,
108 u64 num_bytes
, int reserve
,
110 static int block_rsv_use_bytes(struct btrfs_block_rsv
*block_rsv
,
112 int btrfs_pin_extent(struct btrfs_root
*root
,
113 u64 bytenr
, u64 num_bytes
, int reserved
);
116 block_group_cache_done(struct btrfs_block_group_cache
*cache
)
119 return cache
->cached
== BTRFS_CACHE_FINISHED
||
120 cache
->cached
== BTRFS_CACHE_ERROR
;
123 static int block_group_bits(struct btrfs_block_group_cache
*cache
, u64 bits
)
125 return (cache
->flags
& bits
) == bits
;
128 void btrfs_get_block_group(struct btrfs_block_group_cache
*cache
)
130 atomic_inc(&cache
->count
);
133 void btrfs_put_block_group(struct btrfs_block_group_cache
*cache
)
135 if (atomic_dec_and_test(&cache
->count
)) {
136 WARN_ON(cache
->pinned
> 0);
137 WARN_ON(cache
->reserved
> 0);
138 kfree(cache
->free_space_ctl
);
144 * this adds the block group to the fs_info rb tree for the block group
147 static int btrfs_add_block_group_cache(struct btrfs_fs_info
*info
,
148 struct btrfs_block_group_cache
*block_group
)
151 struct rb_node
*parent
= NULL
;
152 struct btrfs_block_group_cache
*cache
;
154 spin_lock(&info
->block_group_cache_lock
);
155 p
= &info
->block_group_cache_tree
.rb_node
;
159 cache
= rb_entry(parent
, struct btrfs_block_group_cache
,
161 if (block_group
->key
.objectid
< cache
->key
.objectid
) {
163 } else if (block_group
->key
.objectid
> cache
->key
.objectid
) {
166 spin_unlock(&info
->block_group_cache_lock
);
171 rb_link_node(&block_group
->cache_node
, parent
, p
);
172 rb_insert_color(&block_group
->cache_node
,
173 &info
->block_group_cache_tree
);
175 if (info
->first_logical_byte
> block_group
->key
.objectid
)
176 info
->first_logical_byte
= block_group
->key
.objectid
;
178 spin_unlock(&info
->block_group_cache_lock
);
184 * This will return the block group at or after bytenr if contains is 0, else
185 * it will return the block group that contains the bytenr
187 static struct btrfs_block_group_cache
*
188 block_group_cache_tree_search(struct btrfs_fs_info
*info
, u64 bytenr
,
191 struct btrfs_block_group_cache
*cache
, *ret
= NULL
;
195 spin_lock(&info
->block_group_cache_lock
);
196 n
= info
->block_group_cache_tree
.rb_node
;
199 cache
= rb_entry(n
, struct btrfs_block_group_cache
,
201 end
= cache
->key
.objectid
+ cache
->key
.offset
- 1;
202 start
= cache
->key
.objectid
;
204 if (bytenr
< start
) {
205 if (!contains
&& (!ret
|| start
< ret
->key
.objectid
))
208 } else if (bytenr
> start
) {
209 if (contains
&& bytenr
<= end
) {
220 btrfs_get_block_group(ret
);
221 if (bytenr
== 0 && info
->first_logical_byte
> ret
->key
.objectid
)
222 info
->first_logical_byte
= ret
->key
.objectid
;
224 spin_unlock(&info
->block_group_cache_lock
);
229 static int add_excluded_extent(struct btrfs_root
*root
,
230 u64 start
, u64 num_bytes
)
232 u64 end
= start
+ num_bytes
- 1;
233 set_extent_bits(&root
->fs_info
->freed_extents
[0],
234 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
235 set_extent_bits(&root
->fs_info
->freed_extents
[1],
236 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
240 static void free_excluded_extents(struct btrfs_root
*root
,
241 struct btrfs_block_group_cache
*cache
)
245 start
= cache
->key
.objectid
;
246 end
= start
+ cache
->key
.offset
- 1;
248 clear_extent_bits(&root
->fs_info
->freed_extents
[0],
249 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
250 clear_extent_bits(&root
->fs_info
->freed_extents
[1],
251 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
254 static int exclude_super_stripes(struct btrfs_root
*root
,
255 struct btrfs_block_group_cache
*cache
)
262 if (cache
->key
.objectid
< BTRFS_SUPER_INFO_OFFSET
) {
263 stripe_len
= BTRFS_SUPER_INFO_OFFSET
- cache
->key
.objectid
;
264 cache
->bytes_super
+= stripe_len
;
265 ret
= add_excluded_extent(root
, cache
->key
.objectid
,
271 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
272 bytenr
= btrfs_sb_offset(i
);
273 ret
= btrfs_rmap_block(&root
->fs_info
->mapping_tree
,
274 cache
->key
.objectid
, bytenr
,
275 0, &logical
, &nr
, &stripe_len
);
282 if (logical
[nr
] > cache
->key
.objectid
+
286 if (logical
[nr
] + stripe_len
<= cache
->key
.objectid
)
290 if (start
< cache
->key
.objectid
) {
291 start
= cache
->key
.objectid
;
292 len
= (logical
[nr
] + stripe_len
) - start
;
294 len
= min_t(u64
, stripe_len
,
295 cache
->key
.objectid
+
296 cache
->key
.offset
- start
);
299 cache
->bytes_super
+= len
;
300 ret
= add_excluded_extent(root
, start
, len
);
312 static struct btrfs_caching_control
*
313 get_caching_control(struct btrfs_block_group_cache
*cache
)
315 struct btrfs_caching_control
*ctl
;
317 spin_lock(&cache
->lock
);
318 if (!cache
->caching_ctl
) {
319 spin_unlock(&cache
->lock
);
323 ctl
= cache
->caching_ctl
;
324 atomic_inc(&ctl
->count
);
325 spin_unlock(&cache
->lock
);
329 static void put_caching_control(struct btrfs_caching_control
*ctl
)
331 if (atomic_dec_and_test(&ctl
->count
))
335 #ifdef CONFIG_BTRFS_DEBUG
336 static void fragment_free_space(struct btrfs_root
*root
,
337 struct btrfs_block_group_cache
*block_group
)
339 u64 start
= block_group
->key
.objectid
;
340 u64 len
= block_group
->key
.offset
;
341 u64 chunk
= block_group
->flags
& BTRFS_BLOCK_GROUP_METADATA
?
342 root
->nodesize
: root
->sectorsize
;
343 u64 step
= chunk
<< 1;
345 while (len
> chunk
) {
346 btrfs_remove_free_space(block_group
, start
, chunk
);
357 * this is only called by cache_block_group, since we could have freed extents
358 * we need to check the pinned_extents for any extents that can't be used yet
359 * since their free space will be released as soon as the transaction commits.
361 u64
add_new_free_space(struct btrfs_block_group_cache
*block_group
,
362 struct btrfs_fs_info
*info
, u64 start
, u64 end
)
364 u64 extent_start
, extent_end
, size
, total_added
= 0;
367 while (start
< end
) {
368 ret
= find_first_extent_bit(info
->pinned_extents
, start
,
369 &extent_start
, &extent_end
,
370 EXTENT_DIRTY
| EXTENT_UPTODATE
,
375 if (extent_start
<= start
) {
376 start
= extent_end
+ 1;
377 } else if (extent_start
> start
&& extent_start
< end
) {
378 size
= extent_start
- start
;
380 ret
= btrfs_add_free_space(block_group
, start
,
382 BUG_ON(ret
); /* -ENOMEM or logic error */
383 start
= extent_end
+ 1;
392 ret
= btrfs_add_free_space(block_group
, start
, size
);
393 BUG_ON(ret
); /* -ENOMEM or logic error */
399 static int load_extent_tree_free(struct btrfs_caching_control
*caching_ctl
)
401 struct btrfs_block_group_cache
*block_group
;
402 struct btrfs_fs_info
*fs_info
;
403 struct btrfs_root
*extent_root
;
404 struct btrfs_path
*path
;
405 struct extent_buffer
*leaf
;
406 struct btrfs_key key
;
413 block_group
= caching_ctl
->block_group
;
414 fs_info
= block_group
->fs_info
;
415 extent_root
= fs_info
->extent_root
;
417 path
= btrfs_alloc_path();
421 last
= max_t(u64
, block_group
->key
.objectid
, BTRFS_SUPER_INFO_OFFSET
);
423 #ifdef CONFIG_BTRFS_DEBUG
425 * If we're fragmenting we don't want to make anybody think we can
426 * allocate from this block group until we've had a chance to fragment
429 if (btrfs_should_fragment_free_space(extent_root
, block_group
))
433 * We don't want to deadlock with somebody trying to allocate a new
434 * extent for the extent root while also trying to search the extent
435 * root to add free space. So we skip locking and search the commit
436 * root, since its read-only
438 path
->skip_locking
= 1;
439 path
->search_commit_root
= 1;
444 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
447 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
451 leaf
= path
->nodes
[0];
452 nritems
= btrfs_header_nritems(leaf
);
455 if (btrfs_fs_closing(fs_info
) > 1) {
460 if (path
->slots
[0] < nritems
) {
461 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
463 ret
= find_next_key(path
, 0, &key
);
467 if (need_resched() ||
468 rwsem_is_contended(&fs_info
->commit_root_sem
)) {
470 caching_ctl
->progress
= last
;
471 btrfs_release_path(path
);
472 up_read(&fs_info
->commit_root_sem
);
473 mutex_unlock(&caching_ctl
->mutex
);
475 mutex_lock(&caching_ctl
->mutex
);
476 down_read(&fs_info
->commit_root_sem
);
480 ret
= btrfs_next_leaf(extent_root
, path
);
485 leaf
= path
->nodes
[0];
486 nritems
= btrfs_header_nritems(leaf
);
490 if (key
.objectid
< last
) {
493 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
496 caching_ctl
->progress
= last
;
497 btrfs_release_path(path
);
501 if (key
.objectid
< block_group
->key
.objectid
) {
506 if (key
.objectid
>= block_group
->key
.objectid
+
507 block_group
->key
.offset
)
510 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
||
511 key
.type
== BTRFS_METADATA_ITEM_KEY
) {
512 total_found
+= add_new_free_space(block_group
,
515 if (key
.type
== BTRFS_METADATA_ITEM_KEY
)
516 last
= key
.objectid
+
517 fs_info
->tree_root
->nodesize
;
519 last
= key
.objectid
+ key
.offset
;
521 if (total_found
> CACHING_CTL_WAKE_UP
) {
524 wake_up(&caching_ctl
->wait
);
531 total_found
+= add_new_free_space(block_group
, fs_info
, last
,
532 block_group
->key
.objectid
+
533 block_group
->key
.offset
);
534 caching_ctl
->progress
= (u64
)-1;
537 btrfs_free_path(path
);
541 static noinline
void caching_thread(struct btrfs_work
*work
)
543 struct btrfs_block_group_cache
*block_group
;
544 struct btrfs_fs_info
*fs_info
;
545 struct btrfs_caching_control
*caching_ctl
;
548 caching_ctl
= container_of(work
, struct btrfs_caching_control
, work
);
549 block_group
= caching_ctl
->block_group
;
550 fs_info
= block_group
->fs_info
;
552 mutex_lock(&caching_ctl
->mutex
);
553 down_read(&fs_info
->commit_root_sem
);
555 if (btrfs_fs_compat_ro(fs_info
, FREE_SPACE_TREE
))
556 ret
= load_free_space_tree(caching_ctl
);
558 ret
= load_extent_tree_free(caching_ctl
);
560 spin_lock(&block_group
->lock
);
561 block_group
->caching_ctl
= NULL
;
562 block_group
->cached
= ret
? BTRFS_CACHE_ERROR
: BTRFS_CACHE_FINISHED
;
563 spin_unlock(&block_group
->lock
);
565 #ifdef CONFIG_BTRFS_DEBUG
566 if (btrfs_should_fragment_free_space(extent_root
, block_group
)) {
569 spin_lock(&block_group
->space_info
->lock
);
570 spin_lock(&block_group
->lock
);
571 bytes_used
= block_group
->key
.offset
-
572 btrfs_block_group_used(&block_group
->item
);
573 block_group
->space_info
->bytes_used
+= bytes_used
>> 1;
574 spin_unlock(&block_group
->lock
);
575 spin_unlock(&block_group
->space_info
->lock
);
576 fragment_free_space(extent_root
, block_group
);
580 caching_ctl
->progress
= (u64
)-1;
582 up_read(&fs_info
->commit_root_sem
);
583 free_excluded_extents(fs_info
->extent_root
, block_group
);
584 mutex_unlock(&caching_ctl
->mutex
);
586 wake_up(&caching_ctl
->wait
);
588 put_caching_control(caching_ctl
);
589 btrfs_put_block_group(block_group
);
592 static int cache_block_group(struct btrfs_block_group_cache
*cache
,
596 struct btrfs_fs_info
*fs_info
= cache
->fs_info
;
597 struct btrfs_caching_control
*caching_ctl
;
600 caching_ctl
= kzalloc(sizeof(*caching_ctl
), GFP_NOFS
);
604 INIT_LIST_HEAD(&caching_ctl
->list
);
605 mutex_init(&caching_ctl
->mutex
);
606 init_waitqueue_head(&caching_ctl
->wait
);
607 caching_ctl
->block_group
= cache
;
608 caching_ctl
->progress
= cache
->key
.objectid
;
609 atomic_set(&caching_ctl
->count
, 1);
610 btrfs_init_work(&caching_ctl
->work
, btrfs_cache_helper
,
611 caching_thread
, NULL
, NULL
);
613 spin_lock(&cache
->lock
);
615 * This should be a rare occasion, but this could happen I think in the
616 * case where one thread starts to load the space cache info, and then
617 * some other thread starts a transaction commit which tries to do an
618 * allocation while the other thread is still loading the space cache
619 * info. The previous loop should have kept us from choosing this block
620 * group, but if we've moved to the state where we will wait on caching
621 * block groups we need to first check if we're doing a fast load here,
622 * so we can wait for it to finish, otherwise we could end up allocating
623 * from a block group who's cache gets evicted for one reason or
626 while (cache
->cached
== BTRFS_CACHE_FAST
) {
627 struct btrfs_caching_control
*ctl
;
629 ctl
= cache
->caching_ctl
;
630 atomic_inc(&ctl
->count
);
631 prepare_to_wait(&ctl
->wait
, &wait
, TASK_UNINTERRUPTIBLE
);
632 spin_unlock(&cache
->lock
);
636 finish_wait(&ctl
->wait
, &wait
);
637 put_caching_control(ctl
);
638 spin_lock(&cache
->lock
);
641 if (cache
->cached
!= BTRFS_CACHE_NO
) {
642 spin_unlock(&cache
->lock
);
646 WARN_ON(cache
->caching_ctl
);
647 cache
->caching_ctl
= caching_ctl
;
648 cache
->cached
= BTRFS_CACHE_FAST
;
649 spin_unlock(&cache
->lock
);
651 if (fs_info
->mount_opt
& BTRFS_MOUNT_SPACE_CACHE
) {
652 mutex_lock(&caching_ctl
->mutex
);
653 ret
= load_free_space_cache(fs_info
, cache
);
655 spin_lock(&cache
->lock
);
657 cache
->caching_ctl
= NULL
;
658 cache
->cached
= BTRFS_CACHE_FINISHED
;
659 cache
->last_byte_to_unpin
= (u64
)-1;
660 caching_ctl
->progress
= (u64
)-1;
662 if (load_cache_only
) {
663 cache
->caching_ctl
= NULL
;
664 cache
->cached
= BTRFS_CACHE_NO
;
666 cache
->cached
= BTRFS_CACHE_STARTED
;
667 cache
->has_caching_ctl
= 1;
670 spin_unlock(&cache
->lock
);
671 #ifdef CONFIG_BTRFS_DEBUG
673 btrfs_should_fragment_free_space(fs_info
->extent_root
,
677 spin_lock(&cache
->space_info
->lock
);
678 spin_lock(&cache
->lock
);
679 bytes_used
= cache
->key
.offset
-
680 btrfs_block_group_used(&cache
->item
);
681 cache
->space_info
->bytes_used
+= bytes_used
>> 1;
682 spin_unlock(&cache
->lock
);
683 spin_unlock(&cache
->space_info
->lock
);
684 fragment_free_space(fs_info
->extent_root
, cache
);
687 mutex_unlock(&caching_ctl
->mutex
);
689 wake_up(&caching_ctl
->wait
);
691 put_caching_control(caching_ctl
);
692 free_excluded_extents(fs_info
->extent_root
, cache
);
697 * We're either using the free space tree or no caching at all.
698 * Set cached to the appropriate value and wakeup any waiters.
700 spin_lock(&cache
->lock
);
701 if (load_cache_only
) {
702 cache
->caching_ctl
= NULL
;
703 cache
->cached
= BTRFS_CACHE_NO
;
705 cache
->cached
= BTRFS_CACHE_STARTED
;
706 cache
->has_caching_ctl
= 1;
708 spin_unlock(&cache
->lock
);
709 wake_up(&caching_ctl
->wait
);
712 if (load_cache_only
) {
713 put_caching_control(caching_ctl
);
717 down_write(&fs_info
->commit_root_sem
);
718 atomic_inc(&caching_ctl
->count
);
719 list_add_tail(&caching_ctl
->list
, &fs_info
->caching_block_groups
);
720 up_write(&fs_info
->commit_root_sem
);
722 btrfs_get_block_group(cache
);
724 btrfs_queue_work(fs_info
->caching_workers
, &caching_ctl
->work
);
730 * return the block group that starts at or after bytenr
732 static struct btrfs_block_group_cache
*
733 btrfs_lookup_first_block_group(struct btrfs_fs_info
*info
, u64 bytenr
)
735 struct btrfs_block_group_cache
*cache
;
737 cache
= block_group_cache_tree_search(info
, bytenr
, 0);
743 * return the block group that contains the given bytenr
745 struct btrfs_block_group_cache
*btrfs_lookup_block_group(
746 struct btrfs_fs_info
*info
,
749 struct btrfs_block_group_cache
*cache
;
751 cache
= block_group_cache_tree_search(info
, bytenr
, 1);
756 static struct btrfs_space_info
*__find_space_info(struct btrfs_fs_info
*info
,
759 struct list_head
*head
= &info
->space_info
;
760 struct btrfs_space_info
*found
;
762 flags
&= BTRFS_BLOCK_GROUP_TYPE_MASK
;
765 list_for_each_entry_rcu(found
, head
, list
) {
766 if (found
->flags
& flags
) {
776 * after adding space to the filesystem, we need to clear the full flags
777 * on all the space infos.
779 void btrfs_clear_space_info_full(struct btrfs_fs_info
*info
)
781 struct list_head
*head
= &info
->space_info
;
782 struct btrfs_space_info
*found
;
785 list_for_each_entry_rcu(found
, head
, list
)
790 /* simple helper to search for an existing data extent at a given offset */
791 int btrfs_lookup_data_extent(struct btrfs_root
*root
, u64 start
, u64 len
)
794 struct btrfs_key key
;
795 struct btrfs_path
*path
;
797 path
= btrfs_alloc_path();
801 key
.objectid
= start
;
803 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
804 ret
= btrfs_search_slot(NULL
, root
->fs_info
->extent_root
, &key
, path
,
806 btrfs_free_path(path
);
811 * helper function to lookup reference count and flags of a tree block.
813 * the head node for delayed ref is used to store the sum of all the
814 * reference count modifications queued up in the rbtree. the head
815 * node may also store the extent flags to set. This way you can check
816 * to see what the reference count and extent flags would be if all of
817 * the delayed refs are not processed.
819 int btrfs_lookup_extent_info(struct btrfs_trans_handle
*trans
,
820 struct btrfs_root
*root
, u64 bytenr
,
821 u64 offset
, int metadata
, u64
*refs
, u64
*flags
)
823 struct btrfs_delayed_ref_head
*head
;
824 struct btrfs_delayed_ref_root
*delayed_refs
;
825 struct btrfs_path
*path
;
826 struct btrfs_extent_item
*ei
;
827 struct extent_buffer
*leaf
;
828 struct btrfs_key key
;
835 * If we don't have skinny metadata, don't bother doing anything
838 if (metadata
&& !btrfs_fs_incompat(root
->fs_info
, SKINNY_METADATA
)) {
839 offset
= root
->nodesize
;
843 path
= btrfs_alloc_path();
848 path
->skip_locking
= 1;
849 path
->search_commit_root
= 1;
853 key
.objectid
= bytenr
;
856 key
.type
= BTRFS_METADATA_ITEM_KEY
;
858 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
860 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
,
865 if (ret
> 0 && metadata
&& key
.type
== BTRFS_METADATA_ITEM_KEY
) {
866 if (path
->slots
[0]) {
868 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
870 if (key
.objectid
== bytenr
&&
871 key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
872 key
.offset
== root
->nodesize
)
878 leaf
= path
->nodes
[0];
879 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
880 if (item_size
>= sizeof(*ei
)) {
881 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
882 struct btrfs_extent_item
);
883 num_refs
= btrfs_extent_refs(leaf
, ei
);
884 extent_flags
= btrfs_extent_flags(leaf
, ei
);
886 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
887 struct btrfs_extent_item_v0
*ei0
;
888 BUG_ON(item_size
!= sizeof(*ei0
));
889 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
890 struct btrfs_extent_item_v0
);
891 num_refs
= btrfs_extent_refs_v0(leaf
, ei0
);
892 /* FIXME: this isn't correct for data */
893 extent_flags
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
898 BUG_ON(num_refs
== 0);
908 delayed_refs
= &trans
->transaction
->delayed_refs
;
909 spin_lock(&delayed_refs
->lock
);
910 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
912 if (!mutex_trylock(&head
->mutex
)) {
913 atomic_inc(&head
->node
.refs
);
914 spin_unlock(&delayed_refs
->lock
);
916 btrfs_release_path(path
);
919 * Mutex was contended, block until it's released and try
922 mutex_lock(&head
->mutex
);
923 mutex_unlock(&head
->mutex
);
924 btrfs_put_delayed_ref(&head
->node
);
927 spin_lock(&head
->lock
);
928 if (head
->extent_op
&& head
->extent_op
->update_flags
)
929 extent_flags
|= head
->extent_op
->flags_to_set
;
931 BUG_ON(num_refs
== 0);
933 num_refs
+= head
->node
.ref_mod
;
934 spin_unlock(&head
->lock
);
935 mutex_unlock(&head
->mutex
);
937 spin_unlock(&delayed_refs
->lock
);
939 WARN_ON(num_refs
== 0);
943 *flags
= extent_flags
;
945 btrfs_free_path(path
);
950 * Back reference rules. Back refs have three main goals:
952 * 1) differentiate between all holders of references to an extent so that
953 * when a reference is dropped we can make sure it was a valid reference
954 * before freeing the extent.
956 * 2) Provide enough information to quickly find the holders of an extent
957 * if we notice a given block is corrupted or bad.
959 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
960 * maintenance. This is actually the same as #2, but with a slightly
961 * different use case.
963 * There are two kinds of back refs. The implicit back refs is optimized
964 * for pointers in non-shared tree blocks. For a given pointer in a block,
965 * back refs of this kind provide information about the block's owner tree
966 * and the pointer's key. These information allow us to find the block by
967 * b-tree searching. The full back refs is for pointers in tree blocks not
968 * referenced by their owner trees. The location of tree block is recorded
969 * in the back refs. Actually the full back refs is generic, and can be
970 * used in all cases the implicit back refs is used. The major shortcoming
971 * of the full back refs is its overhead. Every time a tree block gets
972 * COWed, we have to update back refs entry for all pointers in it.
974 * For a newly allocated tree block, we use implicit back refs for
975 * pointers in it. This means most tree related operations only involve
976 * implicit back refs. For a tree block created in old transaction, the
977 * only way to drop a reference to it is COW it. So we can detect the
978 * event that tree block loses its owner tree's reference and do the
979 * back refs conversion.
981 * When a tree block is COW'd through a tree, there are four cases:
983 * The reference count of the block is one and the tree is the block's
984 * owner tree. Nothing to do in this case.
986 * The reference count of the block is one and the tree is not the
987 * block's owner tree. In this case, full back refs is used for pointers
988 * in the block. Remove these full back refs, add implicit back refs for
989 * every pointers in the new block.
991 * The reference count of the block is greater than one and the tree is
992 * the block's owner tree. In this case, implicit back refs is used for
993 * pointers in the block. Add full back refs for every pointers in the
994 * block, increase lower level extents' reference counts. The original
995 * implicit back refs are entailed to the new block.
997 * The reference count of the block is greater than one and the tree is
998 * not the block's owner tree. Add implicit back refs for every pointer in
999 * the new block, increase lower level extents' reference count.
1001 * Back Reference Key composing:
1003 * The key objectid corresponds to the first byte in the extent,
1004 * The key type is used to differentiate between types of back refs.
1005 * There are different meanings of the key offset for different types
1008 * File extents can be referenced by:
1010 * - multiple snapshots, subvolumes, or different generations in one subvol
1011 * - different files inside a single subvolume
1012 * - different offsets inside a file (bookend extents in file.c)
1014 * The extent ref structure for the implicit back refs has fields for:
1016 * - Objectid of the subvolume root
1017 * - objectid of the file holding the reference
1018 * - original offset in the file
1019 * - how many bookend extents
1021 * The key offset for the implicit back refs is hash of the first
1024 * The extent ref structure for the full back refs has field for:
1026 * - number of pointers in the tree leaf
1028 * The key offset for the implicit back refs is the first byte of
1031 * When a file extent is allocated, The implicit back refs is used.
1032 * the fields are filled in:
1034 * (root_key.objectid, inode objectid, offset in file, 1)
1036 * When a file extent is removed file truncation, we find the
1037 * corresponding implicit back refs and check the following fields:
1039 * (btrfs_header_owner(leaf), inode objectid, offset in file)
1041 * Btree extents can be referenced by:
1043 * - Different subvolumes
1045 * Both the implicit back refs and the full back refs for tree blocks
1046 * only consist of key. The key offset for the implicit back refs is
1047 * objectid of block's owner tree. The key offset for the full back refs
1048 * is the first byte of parent block.
1050 * When implicit back refs is used, information about the lowest key and
1051 * level of the tree block are required. These information are stored in
1052 * tree block info structure.
1055 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1056 static int convert_extent_item_v0(struct btrfs_trans_handle
*trans
,
1057 struct btrfs_root
*root
,
1058 struct btrfs_path
*path
,
1059 u64 owner
, u32 extra_size
)
1061 struct btrfs_extent_item
*item
;
1062 struct btrfs_extent_item_v0
*ei0
;
1063 struct btrfs_extent_ref_v0
*ref0
;
1064 struct btrfs_tree_block_info
*bi
;
1065 struct extent_buffer
*leaf
;
1066 struct btrfs_key key
;
1067 struct btrfs_key found_key
;
1068 u32 new_size
= sizeof(*item
);
1072 leaf
= path
->nodes
[0];
1073 BUG_ON(btrfs_item_size_nr(leaf
, path
->slots
[0]) != sizeof(*ei0
));
1075 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1076 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1077 struct btrfs_extent_item_v0
);
1078 refs
= btrfs_extent_refs_v0(leaf
, ei0
);
1080 if (owner
== (u64
)-1) {
1082 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1083 ret
= btrfs_next_leaf(root
, path
);
1086 BUG_ON(ret
> 0); /* Corruption */
1087 leaf
= path
->nodes
[0];
1089 btrfs_item_key_to_cpu(leaf
, &found_key
,
1091 BUG_ON(key
.objectid
!= found_key
.objectid
);
1092 if (found_key
.type
!= BTRFS_EXTENT_REF_V0_KEY
) {
1096 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1097 struct btrfs_extent_ref_v0
);
1098 owner
= btrfs_ref_objectid_v0(leaf
, ref0
);
1102 btrfs_release_path(path
);
1104 if (owner
< BTRFS_FIRST_FREE_OBJECTID
)
1105 new_size
+= sizeof(*bi
);
1107 new_size
-= sizeof(*ei0
);
1108 ret
= btrfs_search_slot(trans
, root
, &key
, path
,
1109 new_size
+ extra_size
, 1);
1112 BUG_ON(ret
); /* Corruption */
1114 btrfs_extend_item(root
, path
, new_size
);
1116 leaf
= path
->nodes
[0];
1117 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1118 btrfs_set_extent_refs(leaf
, item
, refs
);
1119 /* FIXME: get real generation */
1120 btrfs_set_extent_generation(leaf
, item
, 0);
1121 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1122 btrfs_set_extent_flags(leaf
, item
,
1123 BTRFS_EXTENT_FLAG_TREE_BLOCK
|
1124 BTRFS_BLOCK_FLAG_FULL_BACKREF
);
1125 bi
= (struct btrfs_tree_block_info
*)(item
+ 1);
1126 /* FIXME: get first key of the block */
1127 memset_extent_buffer(leaf
, 0, (unsigned long)bi
, sizeof(*bi
));
1128 btrfs_set_tree_block_level(leaf
, bi
, (int)owner
);
1130 btrfs_set_extent_flags(leaf
, item
, BTRFS_EXTENT_FLAG_DATA
);
1132 btrfs_mark_buffer_dirty(leaf
);
1137 static u64
hash_extent_data_ref(u64 root_objectid
, u64 owner
, u64 offset
)
1139 u32 high_crc
= ~(u32
)0;
1140 u32 low_crc
= ~(u32
)0;
1143 lenum
= cpu_to_le64(root_objectid
);
1144 high_crc
= btrfs_crc32c(high_crc
, &lenum
, sizeof(lenum
));
1145 lenum
= cpu_to_le64(owner
);
1146 low_crc
= btrfs_crc32c(low_crc
, &lenum
, sizeof(lenum
));
1147 lenum
= cpu_to_le64(offset
);
1148 low_crc
= btrfs_crc32c(low_crc
, &lenum
, sizeof(lenum
));
1150 return ((u64
)high_crc
<< 31) ^ (u64
)low_crc
;
1153 static u64
hash_extent_data_ref_item(struct extent_buffer
*leaf
,
1154 struct btrfs_extent_data_ref
*ref
)
1156 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf
, ref
),
1157 btrfs_extent_data_ref_objectid(leaf
, ref
),
1158 btrfs_extent_data_ref_offset(leaf
, ref
));
1161 static int match_extent_data_ref(struct extent_buffer
*leaf
,
1162 struct btrfs_extent_data_ref
*ref
,
1163 u64 root_objectid
, u64 owner
, u64 offset
)
1165 if (btrfs_extent_data_ref_root(leaf
, ref
) != root_objectid
||
1166 btrfs_extent_data_ref_objectid(leaf
, ref
) != owner
||
1167 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
1172 static noinline
int lookup_extent_data_ref(struct btrfs_trans_handle
*trans
,
1173 struct btrfs_root
*root
,
1174 struct btrfs_path
*path
,
1175 u64 bytenr
, u64 parent
,
1177 u64 owner
, u64 offset
)
1179 struct btrfs_key key
;
1180 struct btrfs_extent_data_ref
*ref
;
1181 struct extent_buffer
*leaf
;
1187 key
.objectid
= bytenr
;
1189 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
1190 key
.offset
= parent
;
1192 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
1193 key
.offset
= hash_extent_data_ref(root_objectid
,
1198 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1207 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1208 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
1209 btrfs_release_path(path
);
1210 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1221 leaf
= path
->nodes
[0];
1222 nritems
= btrfs_header_nritems(leaf
);
1224 if (path
->slots
[0] >= nritems
) {
1225 ret
= btrfs_next_leaf(root
, path
);
1231 leaf
= path
->nodes
[0];
1232 nritems
= btrfs_header_nritems(leaf
);
1236 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1237 if (key
.objectid
!= bytenr
||
1238 key
.type
!= BTRFS_EXTENT_DATA_REF_KEY
)
1241 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1242 struct btrfs_extent_data_ref
);
1244 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
1247 btrfs_release_path(path
);
1259 static noinline
int insert_extent_data_ref(struct btrfs_trans_handle
*trans
,
1260 struct btrfs_root
*root
,
1261 struct btrfs_path
*path
,
1262 u64 bytenr
, u64 parent
,
1263 u64 root_objectid
, u64 owner
,
1264 u64 offset
, int refs_to_add
)
1266 struct btrfs_key key
;
1267 struct extent_buffer
*leaf
;
1272 key
.objectid
= bytenr
;
1274 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
1275 key
.offset
= parent
;
1276 size
= sizeof(struct btrfs_shared_data_ref
);
1278 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
1279 key
.offset
= hash_extent_data_ref(root_objectid
,
1281 size
= sizeof(struct btrfs_extent_data_ref
);
1284 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, size
);
1285 if (ret
&& ret
!= -EEXIST
)
1288 leaf
= path
->nodes
[0];
1290 struct btrfs_shared_data_ref
*ref
;
1291 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1292 struct btrfs_shared_data_ref
);
1294 btrfs_set_shared_data_ref_count(leaf
, ref
, refs_to_add
);
1296 num_refs
= btrfs_shared_data_ref_count(leaf
, ref
);
1297 num_refs
+= refs_to_add
;
1298 btrfs_set_shared_data_ref_count(leaf
, ref
, num_refs
);
1301 struct btrfs_extent_data_ref
*ref
;
1302 while (ret
== -EEXIST
) {
1303 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1304 struct btrfs_extent_data_ref
);
1305 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
1308 btrfs_release_path(path
);
1310 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1312 if (ret
&& ret
!= -EEXIST
)
1315 leaf
= path
->nodes
[0];
1317 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1318 struct btrfs_extent_data_ref
);
1320 btrfs_set_extent_data_ref_root(leaf
, ref
,
1322 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
1323 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
1324 btrfs_set_extent_data_ref_count(leaf
, ref
, refs_to_add
);
1326 num_refs
= btrfs_extent_data_ref_count(leaf
, ref
);
1327 num_refs
+= refs_to_add
;
1328 btrfs_set_extent_data_ref_count(leaf
, ref
, num_refs
);
1331 btrfs_mark_buffer_dirty(leaf
);
1334 btrfs_release_path(path
);
1338 static noinline
int remove_extent_data_ref(struct btrfs_trans_handle
*trans
,
1339 struct btrfs_root
*root
,
1340 struct btrfs_path
*path
,
1341 int refs_to_drop
, int *last_ref
)
1343 struct btrfs_key key
;
1344 struct btrfs_extent_data_ref
*ref1
= NULL
;
1345 struct btrfs_shared_data_ref
*ref2
= NULL
;
1346 struct extent_buffer
*leaf
;
1350 leaf
= path
->nodes
[0];
1351 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1353 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1354 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
1355 struct btrfs_extent_data_ref
);
1356 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1357 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
1358 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
1359 struct btrfs_shared_data_ref
);
1360 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1361 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1362 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
1363 struct btrfs_extent_ref_v0
*ref0
;
1364 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1365 struct btrfs_extent_ref_v0
);
1366 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
1372 BUG_ON(num_refs
< refs_to_drop
);
1373 num_refs
-= refs_to_drop
;
1375 if (num_refs
== 0) {
1376 ret
= btrfs_del_item(trans
, root
, path
);
1379 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
)
1380 btrfs_set_extent_data_ref_count(leaf
, ref1
, num_refs
);
1381 else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
)
1382 btrfs_set_shared_data_ref_count(leaf
, ref2
, num_refs
);
1383 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1385 struct btrfs_extent_ref_v0
*ref0
;
1386 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1387 struct btrfs_extent_ref_v0
);
1388 btrfs_set_ref_count_v0(leaf
, ref0
, num_refs
);
1391 btrfs_mark_buffer_dirty(leaf
);
1396 static noinline u32
extent_data_ref_count(struct btrfs_path
*path
,
1397 struct btrfs_extent_inline_ref
*iref
)
1399 struct btrfs_key key
;
1400 struct extent_buffer
*leaf
;
1401 struct btrfs_extent_data_ref
*ref1
;
1402 struct btrfs_shared_data_ref
*ref2
;
1405 leaf
= path
->nodes
[0];
1406 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1408 if (btrfs_extent_inline_ref_type(leaf
, iref
) ==
1409 BTRFS_EXTENT_DATA_REF_KEY
) {
1410 ref1
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1411 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1413 ref2
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1414 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1416 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1417 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
1418 struct btrfs_extent_data_ref
);
1419 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1420 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
1421 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
1422 struct btrfs_shared_data_ref
);
1423 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1424 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1425 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
1426 struct btrfs_extent_ref_v0
*ref0
;
1427 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1428 struct btrfs_extent_ref_v0
);
1429 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
1437 static noinline
int lookup_tree_block_ref(struct btrfs_trans_handle
*trans
,
1438 struct btrfs_root
*root
,
1439 struct btrfs_path
*path
,
1440 u64 bytenr
, u64 parent
,
1443 struct btrfs_key key
;
1446 key
.objectid
= bytenr
;
1448 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1449 key
.offset
= parent
;
1451 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
1452 key
.offset
= root_objectid
;
1455 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1458 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1459 if (ret
== -ENOENT
&& parent
) {
1460 btrfs_release_path(path
);
1461 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
1462 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1470 static noinline
int insert_tree_block_ref(struct btrfs_trans_handle
*trans
,
1471 struct btrfs_root
*root
,
1472 struct btrfs_path
*path
,
1473 u64 bytenr
, u64 parent
,
1476 struct btrfs_key key
;
1479 key
.objectid
= bytenr
;
1481 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1482 key
.offset
= parent
;
1484 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
1485 key
.offset
= root_objectid
;
1488 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, 0);
1489 btrfs_release_path(path
);
1493 static inline int extent_ref_type(u64 parent
, u64 owner
)
1496 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1498 type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1500 type
= BTRFS_TREE_BLOCK_REF_KEY
;
1503 type
= BTRFS_SHARED_DATA_REF_KEY
;
1505 type
= BTRFS_EXTENT_DATA_REF_KEY
;
1510 static int find_next_key(struct btrfs_path
*path
, int level
,
1511 struct btrfs_key
*key
)
1514 for (; level
< BTRFS_MAX_LEVEL
; level
++) {
1515 if (!path
->nodes
[level
])
1517 if (path
->slots
[level
] + 1 >=
1518 btrfs_header_nritems(path
->nodes
[level
]))
1521 btrfs_item_key_to_cpu(path
->nodes
[level
], key
,
1522 path
->slots
[level
] + 1);
1524 btrfs_node_key_to_cpu(path
->nodes
[level
], key
,
1525 path
->slots
[level
] + 1);
1532 * look for inline back ref. if back ref is found, *ref_ret is set
1533 * to the address of inline back ref, and 0 is returned.
1535 * if back ref isn't found, *ref_ret is set to the address where it
1536 * should be inserted, and -ENOENT is returned.
1538 * if insert is true and there are too many inline back refs, the path
1539 * points to the extent item, and -EAGAIN is returned.
1541 * NOTE: inline back refs are ordered in the same way that back ref
1542 * items in the tree are ordered.
1544 static noinline_for_stack
1545 int lookup_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1546 struct btrfs_root
*root
,
1547 struct btrfs_path
*path
,
1548 struct btrfs_extent_inline_ref
**ref_ret
,
1549 u64 bytenr
, u64 num_bytes
,
1550 u64 parent
, u64 root_objectid
,
1551 u64 owner
, u64 offset
, int insert
)
1553 struct btrfs_key key
;
1554 struct extent_buffer
*leaf
;
1555 struct btrfs_extent_item
*ei
;
1556 struct btrfs_extent_inline_ref
*iref
;
1566 bool skinny_metadata
= btrfs_fs_incompat(root
->fs_info
,
1569 key
.objectid
= bytenr
;
1570 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1571 key
.offset
= num_bytes
;
1573 want
= extent_ref_type(parent
, owner
);
1575 extra_size
= btrfs_extent_inline_ref_size(want
);
1576 path
->keep_locks
= 1;
1581 * Owner is our parent level, so we can just add one to get the level
1582 * for the block we are interested in.
1584 if (skinny_metadata
&& owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1585 key
.type
= BTRFS_METADATA_ITEM_KEY
;
1590 ret
= btrfs_search_slot(trans
, root
, &key
, path
, extra_size
, 1);
1597 * We may be a newly converted file system which still has the old fat
1598 * extent entries for metadata, so try and see if we have one of those.
1600 if (ret
> 0 && skinny_metadata
) {
1601 skinny_metadata
= false;
1602 if (path
->slots
[0]) {
1604 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
1606 if (key
.objectid
== bytenr
&&
1607 key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
1608 key
.offset
== num_bytes
)
1612 key
.objectid
= bytenr
;
1613 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1614 key
.offset
= num_bytes
;
1615 btrfs_release_path(path
);
1620 if (ret
&& !insert
) {
1623 } else if (WARN_ON(ret
)) {
1628 leaf
= path
->nodes
[0];
1629 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1630 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1631 if (item_size
< sizeof(*ei
)) {
1636 ret
= convert_extent_item_v0(trans
, root
, path
, owner
,
1642 leaf
= path
->nodes
[0];
1643 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1646 BUG_ON(item_size
< sizeof(*ei
));
1648 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1649 flags
= btrfs_extent_flags(leaf
, ei
);
1651 ptr
= (unsigned long)(ei
+ 1);
1652 end
= (unsigned long)ei
+ item_size
;
1654 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
&& !skinny_metadata
) {
1655 ptr
+= sizeof(struct btrfs_tree_block_info
);
1665 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1666 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1670 ptr
+= btrfs_extent_inline_ref_size(type
);
1674 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1675 struct btrfs_extent_data_ref
*dref
;
1676 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1677 if (match_extent_data_ref(leaf
, dref
, root_objectid
,
1682 if (hash_extent_data_ref_item(leaf
, dref
) <
1683 hash_extent_data_ref(root_objectid
, owner
, offset
))
1687 ref_offset
= btrfs_extent_inline_ref_offset(leaf
, iref
);
1689 if (parent
== ref_offset
) {
1693 if (ref_offset
< parent
)
1696 if (root_objectid
== ref_offset
) {
1700 if (ref_offset
< root_objectid
)
1704 ptr
+= btrfs_extent_inline_ref_size(type
);
1706 if (err
== -ENOENT
&& insert
) {
1707 if (item_size
+ extra_size
>=
1708 BTRFS_MAX_EXTENT_ITEM_SIZE(root
)) {
1713 * To add new inline back ref, we have to make sure
1714 * there is no corresponding back ref item.
1715 * For simplicity, we just do not add new inline back
1716 * ref if there is any kind of item for this block
1718 if (find_next_key(path
, 0, &key
) == 0 &&
1719 key
.objectid
== bytenr
&&
1720 key
.type
< BTRFS_BLOCK_GROUP_ITEM_KEY
) {
1725 *ref_ret
= (struct btrfs_extent_inline_ref
*)ptr
;
1728 path
->keep_locks
= 0;
1729 btrfs_unlock_up_safe(path
, 1);
1735 * helper to add new inline back ref
1737 static noinline_for_stack
1738 void setup_inline_extent_backref(struct btrfs_root
*root
,
1739 struct btrfs_path
*path
,
1740 struct btrfs_extent_inline_ref
*iref
,
1741 u64 parent
, u64 root_objectid
,
1742 u64 owner
, u64 offset
, int refs_to_add
,
1743 struct btrfs_delayed_extent_op
*extent_op
)
1745 struct extent_buffer
*leaf
;
1746 struct btrfs_extent_item
*ei
;
1749 unsigned long item_offset
;
1754 leaf
= path
->nodes
[0];
1755 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1756 item_offset
= (unsigned long)iref
- (unsigned long)ei
;
1758 type
= extent_ref_type(parent
, owner
);
1759 size
= btrfs_extent_inline_ref_size(type
);
1761 btrfs_extend_item(root
, path
, size
);
1763 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1764 refs
= btrfs_extent_refs(leaf
, ei
);
1765 refs
+= refs_to_add
;
1766 btrfs_set_extent_refs(leaf
, ei
, refs
);
1768 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1770 ptr
= (unsigned long)ei
+ item_offset
;
1771 end
= (unsigned long)ei
+ btrfs_item_size_nr(leaf
, path
->slots
[0]);
1772 if (ptr
< end
- size
)
1773 memmove_extent_buffer(leaf
, ptr
+ size
, ptr
,
1776 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1777 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
1778 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1779 struct btrfs_extent_data_ref
*dref
;
1780 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1781 btrfs_set_extent_data_ref_root(leaf
, dref
, root_objectid
);
1782 btrfs_set_extent_data_ref_objectid(leaf
, dref
, owner
);
1783 btrfs_set_extent_data_ref_offset(leaf
, dref
, offset
);
1784 btrfs_set_extent_data_ref_count(leaf
, dref
, refs_to_add
);
1785 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1786 struct btrfs_shared_data_ref
*sref
;
1787 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1788 btrfs_set_shared_data_ref_count(leaf
, sref
, refs_to_add
);
1789 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1790 } else if (type
== BTRFS_SHARED_BLOCK_REF_KEY
) {
1791 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1793 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
1795 btrfs_mark_buffer_dirty(leaf
);
1798 static int lookup_extent_backref(struct btrfs_trans_handle
*trans
,
1799 struct btrfs_root
*root
,
1800 struct btrfs_path
*path
,
1801 struct btrfs_extent_inline_ref
**ref_ret
,
1802 u64 bytenr
, u64 num_bytes
, u64 parent
,
1803 u64 root_objectid
, u64 owner
, u64 offset
)
1807 ret
= lookup_inline_extent_backref(trans
, root
, path
, ref_ret
,
1808 bytenr
, num_bytes
, parent
,
1809 root_objectid
, owner
, offset
, 0);
1813 btrfs_release_path(path
);
1816 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1817 ret
= lookup_tree_block_ref(trans
, root
, path
, bytenr
, parent
,
1820 ret
= lookup_extent_data_ref(trans
, root
, path
, bytenr
, parent
,
1821 root_objectid
, owner
, offset
);
1827 * helper to update/remove inline back ref
1829 static noinline_for_stack
1830 void update_inline_extent_backref(struct btrfs_root
*root
,
1831 struct btrfs_path
*path
,
1832 struct btrfs_extent_inline_ref
*iref
,
1834 struct btrfs_delayed_extent_op
*extent_op
,
1837 struct extent_buffer
*leaf
;
1838 struct btrfs_extent_item
*ei
;
1839 struct btrfs_extent_data_ref
*dref
= NULL
;
1840 struct btrfs_shared_data_ref
*sref
= NULL
;
1848 leaf
= path
->nodes
[0];
1849 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1850 refs
= btrfs_extent_refs(leaf
, ei
);
1851 WARN_ON(refs_to_mod
< 0 && refs
+ refs_to_mod
<= 0);
1852 refs
+= refs_to_mod
;
1853 btrfs_set_extent_refs(leaf
, ei
, refs
);
1855 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1857 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1859 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1860 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1861 refs
= btrfs_extent_data_ref_count(leaf
, dref
);
1862 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1863 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1864 refs
= btrfs_shared_data_ref_count(leaf
, sref
);
1867 BUG_ON(refs_to_mod
!= -1);
1870 BUG_ON(refs_to_mod
< 0 && refs
< -refs_to_mod
);
1871 refs
+= refs_to_mod
;
1874 if (type
== BTRFS_EXTENT_DATA_REF_KEY
)
1875 btrfs_set_extent_data_ref_count(leaf
, dref
, refs
);
1877 btrfs_set_shared_data_ref_count(leaf
, sref
, refs
);
1880 size
= btrfs_extent_inline_ref_size(type
);
1881 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1882 ptr
= (unsigned long)iref
;
1883 end
= (unsigned long)ei
+ item_size
;
1884 if (ptr
+ size
< end
)
1885 memmove_extent_buffer(leaf
, ptr
, ptr
+ size
,
1888 btrfs_truncate_item(root
, path
, item_size
, 1);
1890 btrfs_mark_buffer_dirty(leaf
);
1893 static noinline_for_stack
1894 int insert_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1895 struct btrfs_root
*root
,
1896 struct btrfs_path
*path
,
1897 u64 bytenr
, u64 num_bytes
, u64 parent
,
1898 u64 root_objectid
, u64 owner
,
1899 u64 offset
, int refs_to_add
,
1900 struct btrfs_delayed_extent_op
*extent_op
)
1902 struct btrfs_extent_inline_ref
*iref
;
1905 ret
= lookup_inline_extent_backref(trans
, root
, path
, &iref
,
1906 bytenr
, num_bytes
, parent
,
1907 root_objectid
, owner
, offset
, 1);
1909 BUG_ON(owner
< BTRFS_FIRST_FREE_OBJECTID
);
1910 update_inline_extent_backref(root
, path
, iref
,
1911 refs_to_add
, extent_op
, NULL
);
1912 } else if (ret
== -ENOENT
) {
1913 setup_inline_extent_backref(root
, path
, iref
, parent
,
1914 root_objectid
, owner
, offset
,
1915 refs_to_add
, extent_op
);
1921 static int insert_extent_backref(struct btrfs_trans_handle
*trans
,
1922 struct btrfs_root
*root
,
1923 struct btrfs_path
*path
,
1924 u64 bytenr
, u64 parent
, u64 root_objectid
,
1925 u64 owner
, u64 offset
, int refs_to_add
)
1928 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1929 BUG_ON(refs_to_add
!= 1);
1930 ret
= insert_tree_block_ref(trans
, root
, path
, bytenr
,
1931 parent
, root_objectid
);
1933 ret
= insert_extent_data_ref(trans
, root
, path
, bytenr
,
1934 parent
, root_objectid
,
1935 owner
, offset
, refs_to_add
);
1940 static int remove_extent_backref(struct btrfs_trans_handle
*trans
,
1941 struct btrfs_root
*root
,
1942 struct btrfs_path
*path
,
1943 struct btrfs_extent_inline_ref
*iref
,
1944 int refs_to_drop
, int is_data
, int *last_ref
)
1948 BUG_ON(!is_data
&& refs_to_drop
!= 1);
1950 update_inline_extent_backref(root
, path
, iref
,
1951 -refs_to_drop
, NULL
, last_ref
);
1952 } else if (is_data
) {
1953 ret
= remove_extent_data_ref(trans
, root
, path
, refs_to_drop
,
1957 ret
= btrfs_del_item(trans
, root
, path
);
1962 #define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
1963 static int btrfs_issue_discard(struct block_device
*bdev
, u64 start
, u64 len
,
1964 u64
*discarded_bytes
)
1967 u64 bytes_left
, end
;
1968 u64 aligned_start
= ALIGN(start
, 1 << 9);
1970 if (WARN_ON(start
!= aligned_start
)) {
1971 len
-= aligned_start
- start
;
1972 len
= round_down(len
, 1 << 9);
1973 start
= aligned_start
;
1976 *discarded_bytes
= 0;
1984 /* Skip any superblocks on this device. */
1985 for (j
= 0; j
< BTRFS_SUPER_MIRROR_MAX
; j
++) {
1986 u64 sb_start
= btrfs_sb_offset(j
);
1987 u64 sb_end
= sb_start
+ BTRFS_SUPER_INFO_SIZE
;
1988 u64 size
= sb_start
- start
;
1990 if (!in_range(sb_start
, start
, bytes_left
) &&
1991 !in_range(sb_end
, start
, bytes_left
) &&
1992 !in_range(start
, sb_start
, BTRFS_SUPER_INFO_SIZE
))
1996 * Superblock spans beginning of range. Adjust start and
1999 if (sb_start
<= start
) {
2000 start
+= sb_end
- start
;
2005 bytes_left
= end
- start
;
2010 ret
= blkdev_issue_discard(bdev
, start
>> 9, size
>> 9,
2013 *discarded_bytes
+= size
;
2014 else if (ret
!= -EOPNOTSUPP
)
2023 bytes_left
= end
- start
;
2027 ret
= blkdev_issue_discard(bdev
, start
>> 9, bytes_left
>> 9,
2030 *discarded_bytes
+= bytes_left
;
2035 int btrfs_discard_extent(struct btrfs_root
*root
, u64 bytenr
,
2036 u64 num_bytes
, u64
*actual_bytes
)
2039 u64 discarded_bytes
= 0;
2040 struct btrfs_bio
*bbio
= NULL
;
2043 /* Tell the block device(s) that the sectors can be discarded */
2044 ret
= btrfs_map_block(root
->fs_info
, REQ_DISCARD
,
2045 bytenr
, &num_bytes
, &bbio
, 0);
2046 /* Error condition is -ENOMEM */
2048 struct btrfs_bio_stripe
*stripe
= bbio
->stripes
;
2052 for (i
= 0; i
< bbio
->num_stripes
; i
++, stripe
++) {
2054 if (!stripe
->dev
->can_discard
)
2057 ret
= btrfs_issue_discard(stripe
->dev
->bdev
,
2062 discarded_bytes
+= bytes
;
2063 else if (ret
!= -EOPNOTSUPP
)
2064 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
2067 * Just in case we get back EOPNOTSUPP for some reason,
2068 * just ignore the return value so we don't screw up
2069 * people calling discard_extent.
2073 btrfs_put_bbio(bbio
);
2077 *actual_bytes
= discarded_bytes
;
2080 if (ret
== -EOPNOTSUPP
)
2085 /* Can return -ENOMEM */
2086 int btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
2087 struct btrfs_root
*root
,
2088 u64 bytenr
, u64 num_bytes
, u64 parent
,
2089 u64 root_objectid
, u64 owner
, u64 offset
)
2092 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2094 BUG_ON(owner
< BTRFS_FIRST_FREE_OBJECTID
&&
2095 root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
2097 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
2098 ret
= btrfs_add_delayed_tree_ref(fs_info
, trans
, bytenr
,
2100 parent
, root_objectid
, (int)owner
,
2101 BTRFS_ADD_DELAYED_REF
, NULL
);
2103 ret
= btrfs_add_delayed_data_ref(fs_info
, trans
, bytenr
,
2104 num_bytes
, parent
, root_objectid
,
2106 BTRFS_ADD_DELAYED_REF
, NULL
);
2111 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
2112 struct btrfs_root
*root
,
2113 struct btrfs_delayed_ref_node
*node
,
2114 u64 parent
, u64 root_objectid
,
2115 u64 owner
, u64 offset
, int refs_to_add
,
2116 struct btrfs_delayed_extent_op
*extent_op
)
2118 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2119 struct btrfs_path
*path
;
2120 struct extent_buffer
*leaf
;
2121 struct btrfs_extent_item
*item
;
2122 struct btrfs_key key
;
2123 u64 bytenr
= node
->bytenr
;
2124 u64 num_bytes
= node
->num_bytes
;
2128 path
= btrfs_alloc_path();
2133 path
->leave_spinning
= 1;
2134 /* this will setup the path even if it fails to insert the back ref */
2135 ret
= insert_inline_extent_backref(trans
, fs_info
->extent_root
, path
,
2136 bytenr
, num_bytes
, parent
,
2137 root_objectid
, owner
, offset
,
2138 refs_to_add
, extent_op
);
2139 if ((ret
< 0 && ret
!= -EAGAIN
) || !ret
)
2143 * Ok we had -EAGAIN which means we didn't have space to insert and
2144 * inline extent ref, so just update the reference count and add a
2147 leaf
= path
->nodes
[0];
2148 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2149 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
2150 refs
= btrfs_extent_refs(leaf
, item
);
2151 btrfs_set_extent_refs(leaf
, item
, refs
+ refs_to_add
);
2153 __run_delayed_extent_op(extent_op
, leaf
, item
);
2155 btrfs_mark_buffer_dirty(leaf
);
2156 btrfs_release_path(path
);
2159 path
->leave_spinning
= 1;
2160 /* now insert the actual backref */
2161 ret
= insert_extent_backref(trans
, root
->fs_info
->extent_root
,
2162 path
, bytenr
, parent
, root_objectid
,
2163 owner
, offset
, refs_to_add
);
2165 btrfs_abort_transaction(trans
, root
, ret
);
2167 btrfs_free_path(path
);
2171 static int run_delayed_data_ref(struct btrfs_trans_handle
*trans
,
2172 struct btrfs_root
*root
,
2173 struct btrfs_delayed_ref_node
*node
,
2174 struct btrfs_delayed_extent_op
*extent_op
,
2175 int insert_reserved
)
2178 struct btrfs_delayed_data_ref
*ref
;
2179 struct btrfs_key ins
;
2184 ins
.objectid
= node
->bytenr
;
2185 ins
.offset
= node
->num_bytes
;
2186 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
2188 ref
= btrfs_delayed_node_to_data_ref(node
);
2189 trace_run_delayed_data_ref(node
, ref
, node
->action
);
2191 if (node
->type
== BTRFS_SHARED_DATA_REF_KEY
)
2192 parent
= ref
->parent
;
2193 ref_root
= ref
->root
;
2195 if (node
->action
== BTRFS_ADD_DELAYED_REF
&& insert_reserved
) {
2197 flags
|= extent_op
->flags_to_set
;
2198 ret
= alloc_reserved_file_extent(trans
, root
,
2199 parent
, ref_root
, flags
,
2200 ref
->objectid
, ref
->offset
,
2201 &ins
, node
->ref_mod
);
2202 } else if (node
->action
== BTRFS_ADD_DELAYED_REF
) {
2203 ret
= __btrfs_inc_extent_ref(trans
, root
, node
, parent
,
2204 ref_root
, ref
->objectid
,
2205 ref
->offset
, node
->ref_mod
,
2207 } else if (node
->action
== BTRFS_DROP_DELAYED_REF
) {
2208 ret
= __btrfs_free_extent(trans
, root
, node
, parent
,
2209 ref_root
, ref
->objectid
,
2210 ref
->offset
, node
->ref_mod
,
2218 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op
*extent_op
,
2219 struct extent_buffer
*leaf
,
2220 struct btrfs_extent_item
*ei
)
2222 u64 flags
= btrfs_extent_flags(leaf
, ei
);
2223 if (extent_op
->update_flags
) {
2224 flags
|= extent_op
->flags_to_set
;
2225 btrfs_set_extent_flags(leaf
, ei
, flags
);
2228 if (extent_op
->update_key
) {
2229 struct btrfs_tree_block_info
*bi
;
2230 BUG_ON(!(flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
));
2231 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
2232 btrfs_set_tree_block_key(leaf
, bi
, &extent_op
->key
);
2236 static int run_delayed_extent_op(struct btrfs_trans_handle
*trans
,
2237 struct btrfs_root
*root
,
2238 struct btrfs_delayed_ref_node
*node
,
2239 struct btrfs_delayed_extent_op
*extent_op
)
2241 struct btrfs_key key
;
2242 struct btrfs_path
*path
;
2243 struct btrfs_extent_item
*ei
;
2244 struct extent_buffer
*leaf
;
2248 int metadata
= !extent_op
->is_data
;
2253 if (metadata
&& !btrfs_fs_incompat(root
->fs_info
, SKINNY_METADATA
))
2256 path
= btrfs_alloc_path();
2260 key
.objectid
= node
->bytenr
;
2263 key
.type
= BTRFS_METADATA_ITEM_KEY
;
2264 key
.offset
= extent_op
->level
;
2266 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2267 key
.offset
= node
->num_bytes
;
2272 path
->leave_spinning
= 1;
2273 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
,
2281 if (path
->slots
[0] > 0) {
2283 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
2285 if (key
.objectid
== node
->bytenr
&&
2286 key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
2287 key
.offset
== node
->num_bytes
)
2291 btrfs_release_path(path
);
2294 key
.objectid
= node
->bytenr
;
2295 key
.offset
= node
->num_bytes
;
2296 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2305 leaf
= path
->nodes
[0];
2306 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2307 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2308 if (item_size
< sizeof(*ei
)) {
2309 ret
= convert_extent_item_v0(trans
, root
->fs_info
->extent_root
,
2315 leaf
= path
->nodes
[0];
2316 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2319 BUG_ON(item_size
< sizeof(*ei
));
2320 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
2321 __run_delayed_extent_op(extent_op
, leaf
, ei
);
2323 btrfs_mark_buffer_dirty(leaf
);
2325 btrfs_free_path(path
);
2329 static int run_delayed_tree_ref(struct btrfs_trans_handle
*trans
,
2330 struct btrfs_root
*root
,
2331 struct btrfs_delayed_ref_node
*node
,
2332 struct btrfs_delayed_extent_op
*extent_op
,
2333 int insert_reserved
)
2336 struct btrfs_delayed_tree_ref
*ref
;
2337 struct btrfs_key ins
;
2340 bool skinny_metadata
= btrfs_fs_incompat(root
->fs_info
,
2343 ref
= btrfs_delayed_node_to_tree_ref(node
);
2344 trace_run_delayed_tree_ref(node
, ref
, node
->action
);
2346 if (node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
)
2347 parent
= ref
->parent
;
2348 ref_root
= ref
->root
;
2350 ins
.objectid
= node
->bytenr
;
2351 if (skinny_metadata
) {
2352 ins
.offset
= ref
->level
;
2353 ins
.type
= BTRFS_METADATA_ITEM_KEY
;
2355 ins
.offset
= node
->num_bytes
;
2356 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
2359 BUG_ON(node
->ref_mod
!= 1);
2360 if (node
->action
== BTRFS_ADD_DELAYED_REF
&& insert_reserved
) {
2361 BUG_ON(!extent_op
|| !extent_op
->update_flags
);
2362 ret
= alloc_reserved_tree_block(trans
, root
,
2364 extent_op
->flags_to_set
,
2367 } else if (node
->action
== BTRFS_ADD_DELAYED_REF
) {
2368 ret
= __btrfs_inc_extent_ref(trans
, root
, node
,
2372 } else if (node
->action
== BTRFS_DROP_DELAYED_REF
) {
2373 ret
= __btrfs_free_extent(trans
, root
, node
,
2375 ref
->level
, 0, 1, extent_op
);
2382 /* helper function to actually process a single delayed ref entry */
2383 static int run_one_delayed_ref(struct btrfs_trans_handle
*trans
,
2384 struct btrfs_root
*root
,
2385 struct btrfs_delayed_ref_node
*node
,
2386 struct btrfs_delayed_extent_op
*extent_op
,
2387 int insert_reserved
)
2391 if (trans
->aborted
) {
2392 if (insert_reserved
)
2393 btrfs_pin_extent(root
, node
->bytenr
,
2394 node
->num_bytes
, 1);
2398 if (btrfs_delayed_ref_is_head(node
)) {
2399 struct btrfs_delayed_ref_head
*head
;
2401 * we've hit the end of the chain and we were supposed
2402 * to insert this extent into the tree. But, it got
2403 * deleted before we ever needed to insert it, so all
2404 * we have to do is clean up the accounting
2407 head
= btrfs_delayed_node_to_head(node
);
2408 trace_run_delayed_ref_head(node
, head
, node
->action
);
2410 if (insert_reserved
) {
2411 btrfs_pin_extent(root
, node
->bytenr
,
2412 node
->num_bytes
, 1);
2413 if (head
->is_data
) {
2414 ret
= btrfs_del_csums(trans
, root
,
2420 /* Also free its reserved qgroup space */
2421 btrfs_qgroup_free_delayed_ref(root
->fs_info
,
2422 head
->qgroup_ref_root
,
2423 head
->qgroup_reserved
);
2427 if (node
->type
== BTRFS_TREE_BLOCK_REF_KEY
||
2428 node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
)
2429 ret
= run_delayed_tree_ref(trans
, root
, node
, extent_op
,
2431 else if (node
->type
== BTRFS_EXTENT_DATA_REF_KEY
||
2432 node
->type
== BTRFS_SHARED_DATA_REF_KEY
)
2433 ret
= run_delayed_data_ref(trans
, root
, node
, extent_op
,
2440 static inline struct btrfs_delayed_ref_node
*
2441 select_delayed_ref(struct btrfs_delayed_ref_head
*head
)
2443 struct btrfs_delayed_ref_node
*ref
;
2445 if (list_empty(&head
->ref_list
))
2449 * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
2450 * This is to prevent a ref count from going down to zero, which deletes
2451 * the extent item from the extent tree, when there still are references
2452 * to add, which would fail because they would not find the extent item.
2454 list_for_each_entry(ref
, &head
->ref_list
, list
) {
2455 if (ref
->action
== BTRFS_ADD_DELAYED_REF
)
2459 return list_entry(head
->ref_list
.next
, struct btrfs_delayed_ref_node
,
2464 * Returns 0 on success or if called with an already aborted transaction.
2465 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2467 static noinline
int __btrfs_run_delayed_refs(struct btrfs_trans_handle
*trans
,
2468 struct btrfs_root
*root
,
2471 struct btrfs_delayed_ref_root
*delayed_refs
;
2472 struct btrfs_delayed_ref_node
*ref
;
2473 struct btrfs_delayed_ref_head
*locked_ref
= NULL
;
2474 struct btrfs_delayed_extent_op
*extent_op
;
2475 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2476 ktime_t start
= ktime_get();
2478 unsigned long count
= 0;
2479 unsigned long actual_count
= 0;
2480 int must_insert_reserved
= 0;
2482 delayed_refs
= &trans
->transaction
->delayed_refs
;
2488 spin_lock(&delayed_refs
->lock
);
2489 locked_ref
= btrfs_select_ref_head(trans
);
2491 spin_unlock(&delayed_refs
->lock
);
2495 /* grab the lock that says we are going to process
2496 * all the refs for this head */
2497 ret
= btrfs_delayed_ref_lock(trans
, locked_ref
);
2498 spin_unlock(&delayed_refs
->lock
);
2500 * we may have dropped the spin lock to get the head
2501 * mutex lock, and that might have given someone else
2502 * time to free the head. If that's true, it has been
2503 * removed from our list and we can move on.
2505 if (ret
== -EAGAIN
) {
2513 * We need to try and merge add/drops of the same ref since we
2514 * can run into issues with relocate dropping the implicit ref
2515 * and then it being added back again before the drop can
2516 * finish. If we merged anything we need to re-loop so we can
2518 * Or we can get node references of the same type that weren't
2519 * merged when created due to bumps in the tree mod seq, and
2520 * we need to merge them to prevent adding an inline extent
2521 * backref before dropping it (triggering a BUG_ON at
2522 * insert_inline_extent_backref()).
2524 spin_lock(&locked_ref
->lock
);
2525 btrfs_merge_delayed_refs(trans
, fs_info
, delayed_refs
,
2529 * locked_ref is the head node, so we have to go one
2530 * node back for any delayed ref updates
2532 ref
= select_delayed_ref(locked_ref
);
2534 if (ref
&& ref
->seq
&&
2535 btrfs_check_delayed_seq(fs_info
, delayed_refs
, ref
->seq
)) {
2536 spin_unlock(&locked_ref
->lock
);
2537 btrfs_delayed_ref_unlock(locked_ref
);
2538 spin_lock(&delayed_refs
->lock
);
2539 locked_ref
->processing
= 0;
2540 delayed_refs
->num_heads_ready
++;
2541 spin_unlock(&delayed_refs
->lock
);
2549 * record the must insert reserved flag before we
2550 * drop the spin lock.
2552 must_insert_reserved
= locked_ref
->must_insert_reserved
;
2553 locked_ref
->must_insert_reserved
= 0;
2555 extent_op
= locked_ref
->extent_op
;
2556 locked_ref
->extent_op
= NULL
;
2561 /* All delayed refs have been processed, Go ahead
2562 * and send the head node to run_one_delayed_ref,
2563 * so that any accounting fixes can happen
2565 ref
= &locked_ref
->node
;
2567 if (extent_op
&& must_insert_reserved
) {
2568 btrfs_free_delayed_extent_op(extent_op
);
2573 spin_unlock(&locked_ref
->lock
);
2574 ret
= run_delayed_extent_op(trans
, root
,
2576 btrfs_free_delayed_extent_op(extent_op
);
2580 * Need to reset must_insert_reserved if
2581 * there was an error so the abort stuff
2582 * can cleanup the reserved space
2585 if (must_insert_reserved
)
2586 locked_ref
->must_insert_reserved
= 1;
2587 locked_ref
->processing
= 0;
2588 btrfs_debug(fs_info
, "run_delayed_extent_op returned %d", ret
);
2589 btrfs_delayed_ref_unlock(locked_ref
);
2596 * Need to drop our head ref lock and re-aqcuire the
2597 * delayed ref lock and then re-check to make sure
2600 spin_unlock(&locked_ref
->lock
);
2601 spin_lock(&delayed_refs
->lock
);
2602 spin_lock(&locked_ref
->lock
);
2603 if (!list_empty(&locked_ref
->ref_list
) ||
2604 locked_ref
->extent_op
) {
2605 spin_unlock(&locked_ref
->lock
);
2606 spin_unlock(&delayed_refs
->lock
);
2610 delayed_refs
->num_heads
--;
2611 rb_erase(&locked_ref
->href_node
,
2612 &delayed_refs
->href_root
);
2613 spin_unlock(&delayed_refs
->lock
);
2617 list_del(&ref
->list
);
2619 atomic_dec(&delayed_refs
->num_entries
);
2621 if (!btrfs_delayed_ref_is_head(ref
)) {
2623 * when we play the delayed ref, also correct the
2626 switch (ref
->action
) {
2627 case BTRFS_ADD_DELAYED_REF
:
2628 case BTRFS_ADD_DELAYED_EXTENT
:
2629 locked_ref
->node
.ref_mod
-= ref
->ref_mod
;
2631 case BTRFS_DROP_DELAYED_REF
:
2632 locked_ref
->node
.ref_mod
+= ref
->ref_mod
;
2638 spin_unlock(&locked_ref
->lock
);
2640 ret
= run_one_delayed_ref(trans
, root
, ref
, extent_op
,
2641 must_insert_reserved
);
2643 btrfs_free_delayed_extent_op(extent_op
);
2645 locked_ref
->processing
= 0;
2646 btrfs_delayed_ref_unlock(locked_ref
);
2647 btrfs_put_delayed_ref(ref
);
2648 btrfs_debug(fs_info
, "run_one_delayed_ref returned %d", ret
);
2653 * If this node is a head, that means all the refs in this head
2654 * have been dealt with, and we will pick the next head to deal
2655 * with, so we must unlock the head and drop it from the cluster
2656 * list before we release it.
2658 if (btrfs_delayed_ref_is_head(ref
)) {
2659 if (locked_ref
->is_data
&&
2660 locked_ref
->total_ref_mod
< 0) {
2661 spin_lock(&delayed_refs
->lock
);
2662 delayed_refs
->pending_csums
-= ref
->num_bytes
;
2663 spin_unlock(&delayed_refs
->lock
);
2665 btrfs_delayed_ref_unlock(locked_ref
);
2668 btrfs_put_delayed_ref(ref
);
2674 * We don't want to include ref heads since we can have empty ref heads
2675 * and those will drastically skew our runtime down since we just do
2676 * accounting, no actual extent tree updates.
2678 if (actual_count
> 0) {
2679 u64 runtime
= ktime_to_ns(ktime_sub(ktime_get(), start
));
2683 * We weigh the current average higher than our current runtime
2684 * to avoid large swings in the average.
2686 spin_lock(&delayed_refs
->lock
);
2687 avg
= fs_info
->avg_delayed_ref_runtime
* 3 + runtime
;
2688 fs_info
->avg_delayed_ref_runtime
= avg
>> 2; /* div by 4 */
2689 spin_unlock(&delayed_refs
->lock
);
2694 #ifdef SCRAMBLE_DELAYED_REFS
2696 * Normally delayed refs get processed in ascending bytenr order. This
2697 * correlates in most cases to the order added. To expose dependencies on this
2698 * order, we start to process the tree in the middle instead of the beginning
2700 static u64
find_middle(struct rb_root
*root
)
2702 struct rb_node
*n
= root
->rb_node
;
2703 struct btrfs_delayed_ref_node
*entry
;
2706 u64 first
= 0, last
= 0;
2710 entry
= rb_entry(n
, struct btrfs_delayed_ref_node
, rb_node
);
2711 first
= entry
->bytenr
;
2715 entry
= rb_entry(n
, struct btrfs_delayed_ref_node
, rb_node
);
2716 last
= entry
->bytenr
;
2721 entry
= rb_entry(n
, struct btrfs_delayed_ref_node
, rb_node
);
2722 WARN_ON(!entry
->in_tree
);
2724 middle
= entry
->bytenr
;
2737 static inline u64
heads_to_leaves(struct btrfs_root
*root
, u64 heads
)
2741 num_bytes
= heads
* (sizeof(struct btrfs_extent_item
) +
2742 sizeof(struct btrfs_extent_inline_ref
));
2743 if (!btrfs_fs_incompat(root
->fs_info
, SKINNY_METADATA
))
2744 num_bytes
+= heads
* sizeof(struct btrfs_tree_block_info
);
2747 * We don't ever fill up leaves all the way so multiply by 2 just to be
2748 * closer to what we're really going to want to ouse.
2750 return div_u64(num_bytes
, BTRFS_LEAF_DATA_SIZE(root
));
2754 * Takes the number of bytes to be csumm'ed and figures out how many leaves it
2755 * would require to store the csums for that many bytes.
2757 u64
btrfs_csum_bytes_to_leaves(struct btrfs_root
*root
, u64 csum_bytes
)
2760 u64 num_csums_per_leaf
;
2763 csum_size
= BTRFS_LEAF_DATA_SIZE(root
) - sizeof(struct btrfs_item
);
2764 num_csums_per_leaf
= div64_u64(csum_size
,
2765 (u64
)btrfs_super_csum_size(root
->fs_info
->super_copy
));
2766 num_csums
= div64_u64(csum_bytes
, root
->sectorsize
);
2767 num_csums
+= num_csums_per_leaf
- 1;
2768 num_csums
= div64_u64(num_csums
, num_csums_per_leaf
);
2772 int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle
*trans
,
2773 struct btrfs_root
*root
)
2775 struct btrfs_block_rsv
*global_rsv
;
2776 u64 num_heads
= trans
->transaction
->delayed_refs
.num_heads_ready
;
2777 u64 csum_bytes
= trans
->transaction
->delayed_refs
.pending_csums
;
2778 u64 num_dirty_bgs
= trans
->transaction
->num_dirty_bgs
;
2779 u64 num_bytes
, num_dirty_bgs_bytes
;
2782 num_bytes
= btrfs_calc_trans_metadata_size(root
, 1);
2783 num_heads
= heads_to_leaves(root
, num_heads
);
2785 num_bytes
+= (num_heads
- 1) * root
->nodesize
;
2787 num_bytes
+= btrfs_csum_bytes_to_leaves(root
, csum_bytes
) * root
->nodesize
;
2788 num_dirty_bgs_bytes
= btrfs_calc_trans_metadata_size(root
,
2790 global_rsv
= &root
->fs_info
->global_block_rsv
;
2793 * If we can't allocate any more chunks lets make sure we have _lots_ of
2794 * wiggle room since running delayed refs can create more delayed refs.
2796 if (global_rsv
->space_info
->full
) {
2797 num_dirty_bgs_bytes
<<= 1;
2801 spin_lock(&global_rsv
->lock
);
2802 if (global_rsv
->reserved
<= num_bytes
+ num_dirty_bgs_bytes
)
2804 spin_unlock(&global_rsv
->lock
);
2808 int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle
*trans
,
2809 struct btrfs_root
*root
)
2811 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2813 atomic_read(&trans
->transaction
->delayed_refs
.num_entries
);
2818 avg_runtime
= fs_info
->avg_delayed_ref_runtime
;
2819 val
= num_entries
* avg_runtime
;
2820 if (num_entries
* avg_runtime
>= NSEC_PER_SEC
)
2822 if (val
>= NSEC_PER_SEC
/ 2)
2825 return btrfs_check_space_for_delayed_refs(trans
, root
);
2828 struct async_delayed_refs
{
2829 struct btrfs_root
*root
;
2833 struct completion wait
;
2834 struct btrfs_work work
;
2837 static void delayed_ref_async_start(struct btrfs_work
*work
)
2839 struct async_delayed_refs
*async
;
2840 struct btrfs_trans_handle
*trans
;
2843 async
= container_of(work
, struct async_delayed_refs
, work
);
2845 trans
= btrfs_join_transaction(async
->root
);
2846 if (IS_ERR(trans
)) {
2847 async
->error
= PTR_ERR(trans
);
2852 * trans->sync means that when we call end_transaciton, we won't
2853 * wait on delayed refs
2856 ret
= btrfs_run_delayed_refs(trans
, async
->root
, async
->count
);
2860 ret
= btrfs_end_transaction(trans
, async
->root
);
2861 if (ret
&& !async
->error
)
2865 complete(&async
->wait
);
2870 int btrfs_async_run_delayed_refs(struct btrfs_root
*root
,
2871 unsigned long count
, int wait
)
2873 struct async_delayed_refs
*async
;
2876 async
= kmalloc(sizeof(*async
), GFP_NOFS
);
2880 async
->root
= root
->fs_info
->tree_root
;
2881 async
->count
= count
;
2887 init_completion(&async
->wait
);
2889 btrfs_init_work(&async
->work
, btrfs_extent_refs_helper
,
2890 delayed_ref_async_start
, NULL
, NULL
);
2892 btrfs_queue_work(root
->fs_info
->extent_workers
, &async
->work
);
2895 wait_for_completion(&async
->wait
);
2904 * this starts processing the delayed reference count updates and
2905 * extent insertions we have queued up so far. count can be
2906 * 0, which means to process everything in the tree at the start
2907 * of the run (but not newly added entries), or it can be some target
2908 * number you'd like to process.
2910 * Returns 0 on success or if called with an aborted transaction
2911 * Returns <0 on error and aborts the transaction
2913 int btrfs_run_delayed_refs(struct btrfs_trans_handle
*trans
,
2914 struct btrfs_root
*root
, unsigned long count
)
2916 struct rb_node
*node
;
2917 struct btrfs_delayed_ref_root
*delayed_refs
;
2918 struct btrfs_delayed_ref_head
*head
;
2920 int run_all
= count
== (unsigned long)-1;
2921 bool can_flush_pending_bgs
= trans
->can_flush_pending_bgs
;
2923 /* We'll clean this up in btrfs_cleanup_transaction */
2927 if (root
== root
->fs_info
->extent_root
)
2928 root
= root
->fs_info
->tree_root
;
2930 delayed_refs
= &trans
->transaction
->delayed_refs
;
2932 count
= atomic_read(&delayed_refs
->num_entries
) * 2;
2935 #ifdef SCRAMBLE_DELAYED_REFS
2936 delayed_refs
->run_delayed_start
= find_middle(&delayed_refs
->root
);
2938 trans
->can_flush_pending_bgs
= false;
2939 ret
= __btrfs_run_delayed_refs(trans
, root
, count
);
2941 btrfs_abort_transaction(trans
, root
, ret
);
2946 if (!list_empty(&trans
->new_bgs
))
2947 btrfs_create_pending_block_groups(trans
, root
);
2949 spin_lock(&delayed_refs
->lock
);
2950 node
= rb_first(&delayed_refs
->href_root
);
2952 spin_unlock(&delayed_refs
->lock
);
2955 count
= (unsigned long)-1;
2958 head
= rb_entry(node
, struct btrfs_delayed_ref_head
,
2960 if (btrfs_delayed_ref_is_head(&head
->node
)) {
2961 struct btrfs_delayed_ref_node
*ref
;
2964 atomic_inc(&ref
->refs
);
2966 spin_unlock(&delayed_refs
->lock
);
2968 * Mutex was contended, block until it's
2969 * released and try again
2971 mutex_lock(&head
->mutex
);
2972 mutex_unlock(&head
->mutex
);
2974 btrfs_put_delayed_ref(ref
);
2980 node
= rb_next(node
);
2982 spin_unlock(&delayed_refs
->lock
);
2987 assert_qgroups_uptodate(trans
);
2988 trans
->can_flush_pending_bgs
= can_flush_pending_bgs
;
2992 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle
*trans
,
2993 struct btrfs_root
*root
,
2994 u64 bytenr
, u64 num_bytes
, u64 flags
,
2995 int level
, int is_data
)
2997 struct btrfs_delayed_extent_op
*extent_op
;
3000 extent_op
= btrfs_alloc_delayed_extent_op();
3004 extent_op
->flags_to_set
= flags
;
3005 extent_op
->update_flags
= 1;
3006 extent_op
->update_key
= 0;
3007 extent_op
->is_data
= is_data
? 1 : 0;
3008 extent_op
->level
= level
;
3010 ret
= btrfs_add_delayed_extent_op(root
->fs_info
, trans
, bytenr
,
3011 num_bytes
, extent_op
);
3013 btrfs_free_delayed_extent_op(extent_op
);
3017 static noinline
int check_delayed_ref(struct btrfs_trans_handle
*trans
,
3018 struct btrfs_root
*root
,
3019 struct btrfs_path
*path
,
3020 u64 objectid
, u64 offset
, u64 bytenr
)
3022 struct btrfs_delayed_ref_head
*head
;
3023 struct btrfs_delayed_ref_node
*ref
;
3024 struct btrfs_delayed_data_ref
*data_ref
;
3025 struct btrfs_delayed_ref_root
*delayed_refs
;
3028 delayed_refs
= &trans
->transaction
->delayed_refs
;
3029 spin_lock(&delayed_refs
->lock
);
3030 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
3032 spin_unlock(&delayed_refs
->lock
);
3036 if (!mutex_trylock(&head
->mutex
)) {
3037 atomic_inc(&head
->node
.refs
);
3038 spin_unlock(&delayed_refs
->lock
);
3040 btrfs_release_path(path
);
3043 * Mutex was contended, block until it's released and let
3046 mutex_lock(&head
->mutex
);
3047 mutex_unlock(&head
->mutex
);
3048 btrfs_put_delayed_ref(&head
->node
);
3051 spin_unlock(&delayed_refs
->lock
);
3053 spin_lock(&head
->lock
);
3054 list_for_each_entry(ref
, &head
->ref_list
, list
) {
3055 /* If it's a shared ref we know a cross reference exists */
3056 if (ref
->type
!= BTRFS_EXTENT_DATA_REF_KEY
) {
3061 data_ref
= btrfs_delayed_node_to_data_ref(ref
);
3064 * If our ref doesn't match the one we're currently looking at
3065 * then we have a cross reference.
3067 if (data_ref
->root
!= root
->root_key
.objectid
||
3068 data_ref
->objectid
!= objectid
||
3069 data_ref
->offset
!= offset
) {
3074 spin_unlock(&head
->lock
);
3075 mutex_unlock(&head
->mutex
);
3079 static noinline
int check_committed_ref(struct btrfs_trans_handle
*trans
,
3080 struct btrfs_root
*root
,
3081 struct btrfs_path
*path
,
3082 u64 objectid
, u64 offset
, u64 bytenr
)
3084 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
3085 struct extent_buffer
*leaf
;
3086 struct btrfs_extent_data_ref
*ref
;
3087 struct btrfs_extent_inline_ref
*iref
;
3088 struct btrfs_extent_item
*ei
;
3089 struct btrfs_key key
;
3093 key
.objectid
= bytenr
;
3094 key
.offset
= (u64
)-1;
3095 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
3097 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
3100 BUG_ON(ret
== 0); /* Corruption */
3103 if (path
->slots
[0] == 0)
3107 leaf
= path
->nodes
[0];
3108 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3110 if (key
.objectid
!= bytenr
|| key
.type
!= BTRFS_EXTENT_ITEM_KEY
)
3114 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
3115 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3116 if (item_size
< sizeof(*ei
)) {
3117 WARN_ON(item_size
!= sizeof(struct btrfs_extent_item_v0
));
3121 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
3123 if (item_size
!= sizeof(*ei
) +
3124 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY
))
3127 if (btrfs_extent_generation(leaf
, ei
) <=
3128 btrfs_root_last_snapshot(&root
->root_item
))
3131 iref
= (struct btrfs_extent_inline_ref
*)(ei
+ 1);
3132 if (btrfs_extent_inline_ref_type(leaf
, iref
) !=
3133 BTRFS_EXTENT_DATA_REF_KEY
)
3136 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
3137 if (btrfs_extent_refs(leaf
, ei
) !=
3138 btrfs_extent_data_ref_count(leaf
, ref
) ||
3139 btrfs_extent_data_ref_root(leaf
, ref
) !=
3140 root
->root_key
.objectid
||
3141 btrfs_extent_data_ref_objectid(leaf
, ref
) != objectid
||
3142 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
3150 int btrfs_cross_ref_exist(struct btrfs_trans_handle
*trans
,
3151 struct btrfs_root
*root
,
3152 u64 objectid
, u64 offset
, u64 bytenr
)
3154 struct btrfs_path
*path
;
3158 path
= btrfs_alloc_path();
3163 ret
= check_committed_ref(trans
, root
, path
, objectid
,
3165 if (ret
&& ret
!= -ENOENT
)
3168 ret2
= check_delayed_ref(trans
, root
, path
, objectid
,
3170 } while (ret2
== -EAGAIN
);
3172 if (ret2
&& ret2
!= -ENOENT
) {
3177 if (ret
!= -ENOENT
|| ret2
!= -ENOENT
)
3180 btrfs_free_path(path
);
3181 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
3186 static int __btrfs_mod_ref(struct btrfs_trans_handle
*trans
,
3187 struct btrfs_root
*root
,
3188 struct extent_buffer
*buf
,
3189 int full_backref
, int inc
)
3196 struct btrfs_key key
;
3197 struct btrfs_file_extent_item
*fi
;
3201 int (*process_func
)(struct btrfs_trans_handle
*, struct btrfs_root
*,
3202 u64
, u64
, u64
, u64
, u64
, u64
);
3205 if (btrfs_test_is_dummy_root(root
))
3208 ref_root
= btrfs_header_owner(buf
);
3209 nritems
= btrfs_header_nritems(buf
);
3210 level
= btrfs_header_level(buf
);
3212 if (!test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) && level
== 0)
3216 process_func
= btrfs_inc_extent_ref
;
3218 process_func
= btrfs_free_extent
;
3221 parent
= buf
->start
;
3225 for (i
= 0; i
< nritems
; i
++) {
3227 btrfs_item_key_to_cpu(buf
, &key
, i
);
3228 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
3230 fi
= btrfs_item_ptr(buf
, i
,
3231 struct btrfs_file_extent_item
);
3232 if (btrfs_file_extent_type(buf
, fi
) ==
3233 BTRFS_FILE_EXTENT_INLINE
)
3235 bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
3239 num_bytes
= btrfs_file_extent_disk_num_bytes(buf
, fi
);
3240 key
.offset
-= btrfs_file_extent_offset(buf
, fi
);
3241 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
3242 parent
, ref_root
, key
.objectid
,
3247 bytenr
= btrfs_node_blockptr(buf
, i
);
3248 num_bytes
= root
->nodesize
;
3249 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
3250 parent
, ref_root
, level
- 1, 0);
3260 int btrfs_inc_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
3261 struct extent_buffer
*buf
, int full_backref
)
3263 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 1);
3266 int btrfs_dec_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
3267 struct extent_buffer
*buf
, int full_backref
)
3269 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 0);
3272 static int write_one_cache_group(struct btrfs_trans_handle
*trans
,
3273 struct btrfs_root
*root
,
3274 struct btrfs_path
*path
,
3275 struct btrfs_block_group_cache
*cache
)
3278 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
3280 struct extent_buffer
*leaf
;
3282 ret
= btrfs_search_slot(trans
, extent_root
, &cache
->key
, path
, 0, 1);
3289 leaf
= path
->nodes
[0];
3290 bi
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
3291 write_extent_buffer(leaf
, &cache
->item
, bi
, sizeof(cache
->item
));
3292 btrfs_mark_buffer_dirty(leaf
);
3294 btrfs_release_path(path
);
3299 static struct btrfs_block_group_cache
*
3300 next_block_group(struct btrfs_root
*root
,
3301 struct btrfs_block_group_cache
*cache
)
3303 struct rb_node
*node
;
3305 spin_lock(&root
->fs_info
->block_group_cache_lock
);
3307 /* If our block group was removed, we need a full search. */
3308 if (RB_EMPTY_NODE(&cache
->cache_node
)) {
3309 const u64 next_bytenr
= cache
->key
.objectid
+ cache
->key
.offset
;
3311 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
3312 btrfs_put_block_group(cache
);
3313 cache
= btrfs_lookup_first_block_group(root
->fs_info
,
3317 node
= rb_next(&cache
->cache_node
);
3318 btrfs_put_block_group(cache
);
3320 cache
= rb_entry(node
, struct btrfs_block_group_cache
,
3322 btrfs_get_block_group(cache
);
3325 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
3329 static int cache_save_setup(struct btrfs_block_group_cache
*block_group
,
3330 struct btrfs_trans_handle
*trans
,
3331 struct btrfs_path
*path
)
3333 struct btrfs_root
*root
= block_group
->fs_info
->tree_root
;
3334 struct inode
*inode
= NULL
;
3336 int dcs
= BTRFS_DC_ERROR
;
3342 * If this block group is smaller than 100 megs don't bother caching the
3345 if (block_group
->key
.offset
< (100 * 1024 * 1024)) {
3346 spin_lock(&block_group
->lock
);
3347 block_group
->disk_cache_state
= BTRFS_DC_WRITTEN
;
3348 spin_unlock(&block_group
->lock
);
3355 inode
= lookup_free_space_inode(root
, block_group
, path
);
3356 if (IS_ERR(inode
) && PTR_ERR(inode
) != -ENOENT
) {
3357 ret
= PTR_ERR(inode
);
3358 btrfs_release_path(path
);
3362 if (IS_ERR(inode
)) {
3366 if (block_group
->ro
)
3369 ret
= create_free_space_inode(root
, trans
, block_group
, path
);
3375 /* We've already setup this transaction, go ahead and exit */
3376 if (block_group
->cache_generation
== trans
->transid
&&
3377 i_size_read(inode
)) {
3378 dcs
= BTRFS_DC_SETUP
;
3383 * We want to set the generation to 0, that way if anything goes wrong
3384 * from here on out we know not to trust this cache when we load up next
3387 BTRFS_I(inode
)->generation
= 0;
3388 ret
= btrfs_update_inode(trans
, root
, inode
);
3391 * So theoretically we could recover from this, simply set the
3392 * super cache generation to 0 so we know to invalidate the
3393 * cache, but then we'd have to keep track of the block groups
3394 * that fail this way so we know we _have_ to reset this cache
3395 * before the next commit or risk reading stale cache. So to
3396 * limit our exposure to horrible edge cases lets just abort the
3397 * transaction, this only happens in really bad situations
3400 btrfs_abort_transaction(trans
, root
, ret
);
3405 if (i_size_read(inode
) > 0) {
3406 ret
= btrfs_check_trunc_cache_free_space(root
,
3407 &root
->fs_info
->global_block_rsv
);
3411 ret
= btrfs_truncate_free_space_cache(root
, trans
, NULL
, inode
);
3416 spin_lock(&block_group
->lock
);
3417 if (block_group
->cached
!= BTRFS_CACHE_FINISHED
||
3418 !btrfs_test_opt(root
, SPACE_CACHE
)) {
3420 * don't bother trying to write stuff out _if_
3421 * a) we're not cached,
3422 * b) we're with nospace_cache mount option.
3424 dcs
= BTRFS_DC_WRITTEN
;
3425 spin_unlock(&block_group
->lock
);
3428 spin_unlock(&block_group
->lock
);
3431 * We hit an ENOSPC when setting up the cache in this transaction, just
3432 * skip doing the setup, we've already cleared the cache so we're safe.
3434 if (test_bit(BTRFS_TRANS_CACHE_ENOSPC
, &trans
->transaction
->flags
)) {
3440 * Try to preallocate enough space based on how big the block group is.
3441 * Keep in mind this has to include any pinned space which could end up
3442 * taking up quite a bit since it's not folded into the other space
3445 num_pages
= div_u64(block_group
->key
.offset
, 256 * 1024 * 1024);
3450 num_pages
*= PAGE_CACHE_SIZE
;
3452 ret
= btrfs_check_data_free_space(inode
, 0, num_pages
);
3456 ret
= btrfs_prealloc_file_range_trans(inode
, trans
, 0, 0, num_pages
,
3457 num_pages
, num_pages
,
3460 * Our cache requires contiguous chunks so that we don't modify a bunch
3461 * of metadata or split extents when writing the cache out, which means
3462 * we can enospc if we are heavily fragmented in addition to just normal
3463 * out of space conditions. So if we hit this just skip setting up any
3464 * other block groups for this transaction, maybe we'll unpin enough
3465 * space the next time around.
3468 dcs
= BTRFS_DC_SETUP
;
3469 else if (ret
== -ENOSPC
)
3470 set_bit(BTRFS_TRANS_CACHE_ENOSPC
, &trans
->transaction
->flags
);
3471 btrfs_free_reserved_data_space(inode
, 0, num_pages
);
3476 btrfs_release_path(path
);
3478 spin_lock(&block_group
->lock
);
3479 if (!ret
&& dcs
== BTRFS_DC_SETUP
)
3480 block_group
->cache_generation
= trans
->transid
;
3481 block_group
->disk_cache_state
= dcs
;
3482 spin_unlock(&block_group
->lock
);
3487 int btrfs_setup_space_cache(struct btrfs_trans_handle
*trans
,
3488 struct btrfs_root
*root
)
3490 struct btrfs_block_group_cache
*cache
, *tmp
;
3491 struct btrfs_transaction
*cur_trans
= trans
->transaction
;
3492 struct btrfs_path
*path
;
3494 if (list_empty(&cur_trans
->dirty_bgs
) ||
3495 !btrfs_test_opt(root
, SPACE_CACHE
))
3498 path
= btrfs_alloc_path();
3502 /* Could add new block groups, use _safe just in case */
3503 list_for_each_entry_safe(cache
, tmp
, &cur_trans
->dirty_bgs
,
3505 if (cache
->disk_cache_state
== BTRFS_DC_CLEAR
)
3506 cache_save_setup(cache
, trans
, path
);
3509 btrfs_free_path(path
);
3514 * transaction commit does final block group cache writeback during a
3515 * critical section where nothing is allowed to change the FS. This is
3516 * required in order for the cache to actually match the block group,
3517 * but can introduce a lot of latency into the commit.
3519 * So, btrfs_start_dirty_block_groups is here to kick off block group
3520 * cache IO. There's a chance we'll have to redo some of it if the
3521 * block group changes again during the commit, but it greatly reduces
3522 * the commit latency by getting rid of the easy block groups while
3523 * we're still allowing others to join the commit.
3525 int btrfs_start_dirty_block_groups(struct btrfs_trans_handle
*trans
,
3526 struct btrfs_root
*root
)
3528 struct btrfs_block_group_cache
*cache
;
3529 struct btrfs_transaction
*cur_trans
= trans
->transaction
;
3532 struct btrfs_path
*path
= NULL
;
3534 struct list_head
*io
= &cur_trans
->io_bgs
;
3535 int num_started
= 0;
3538 spin_lock(&cur_trans
->dirty_bgs_lock
);
3539 if (list_empty(&cur_trans
->dirty_bgs
)) {
3540 spin_unlock(&cur_trans
->dirty_bgs_lock
);
3543 list_splice_init(&cur_trans
->dirty_bgs
, &dirty
);
3544 spin_unlock(&cur_trans
->dirty_bgs_lock
);
3548 * make sure all the block groups on our dirty list actually
3551 btrfs_create_pending_block_groups(trans
, root
);
3554 path
= btrfs_alloc_path();
3560 * cache_write_mutex is here only to save us from balance or automatic
3561 * removal of empty block groups deleting this block group while we are
3562 * writing out the cache
3564 mutex_lock(&trans
->transaction
->cache_write_mutex
);
3565 while (!list_empty(&dirty
)) {
3566 cache
= list_first_entry(&dirty
,
3567 struct btrfs_block_group_cache
,
3570 * this can happen if something re-dirties a block
3571 * group that is already under IO. Just wait for it to
3572 * finish and then do it all again
3574 if (!list_empty(&cache
->io_list
)) {
3575 list_del_init(&cache
->io_list
);
3576 btrfs_wait_cache_io(root
, trans
, cache
,
3577 &cache
->io_ctl
, path
,
3578 cache
->key
.objectid
);
3579 btrfs_put_block_group(cache
);
3584 * btrfs_wait_cache_io uses the cache->dirty_list to decide
3585 * if it should update the cache_state. Don't delete
3586 * until after we wait.
3588 * Since we're not running in the commit critical section
3589 * we need the dirty_bgs_lock to protect from update_block_group
3591 spin_lock(&cur_trans
->dirty_bgs_lock
);
3592 list_del_init(&cache
->dirty_list
);
3593 spin_unlock(&cur_trans
->dirty_bgs_lock
);
3597 cache_save_setup(cache
, trans
, path
);
3599 if (cache
->disk_cache_state
== BTRFS_DC_SETUP
) {
3600 cache
->io_ctl
.inode
= NULL
;
3601 ret
= btrfs_write_out_cache(root
, trans
, cache
, path
);
3602 if (ret
== 0 && cache
->io_ctl
.inode
) {
3607 * the cache_write_mutex is protecting
3610 list_add_tail(&cache
->io_list
, io
);
3613 * if we failed to write the cache, the
3614 * generation will be bad and life goes on
3620 ret
= write_one_cache_group(trans
, root
, path
, cache
);
3622 * Our block group might still be attached to the list
3623 * of new block groups in the transaction handle of some
3624 * other task (struct btrfs_trans_handle->new_bgs). This
3625 * means its block group item isn't yet in the extent
3626 * tree. If this happens ignore the error, as we will
3627 * try again later in the critical section of the
3628 * transaction commit.
3630 if (ret
== -ENOENT
) {
3632 spin_lock(&cur_trans
->dirty_bgs_lock
);
3633 if (list_empty(&cache
->dirty_list
)) {
3634 list_add_tail(&cache
->dirty_list
,
3635 &cur_trans
->dirty_bgs
);
3636 btrfs_get_block_group(cache
);
3638 spin_unlock(&cur_trans
->dirty_bgs_lock
);
3640 btrfs_abort_transaction(trans
, root
, ret
);
3644 /* if its not on the io list, we need to put the block group */
3646 btrfs_put_block_group(cache
);
3652 * Avoid blocking other tasks for too long. It might even save
3653 * us from writing caches for block groups that are going to be
3656 mutex_unlock(&trans
->transaction
->cache_write_mutex
);
3657 mutex_lock(&trans
->transaction
->cache_write_mutex
);
3659 mutex_unlock(&trans
->transaction
->cache_write_mutex
);
3662 * go through delayed refs for all the stuff we've just kicked off
3663 * and then loop back (just once)
3665 ret
= btrfs_run_delayed_refs(trans
, root
, 0);
3666 if (!ret
&& loops
== 0) {
3668 spin_lock(&cur_trans
->dirty_bgs_lock
);
3669 list_splice_init(&cur_trans
->dirty_bgs
, &dirty
);
3671 * dirty_bgs_lock protects us from concurrent block group
3672 * deletes too (not just cache_write_mutex).
3674 if (!list_empty(&dirty
)) {
3675 spin_unlock(&cur_trans
->dirty_bgs_lock
);
3678 spin_unlock(&cur_trans
->dirty_bgs_lock
);
3681 btrfs_free_path(path
);
3685 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle
*trans
,
3686 struct btrfs_root
*root
)
3688 struct btrfs_block_group_cache
*cache
;
3689 struct btrfs_transaction
*cur_trans
= trans
->transaction
;
3692 struct btrfs_path
*path
;
3693 struct list_head
*io
= &cur_trans
->io_bgs
;
3694 int num_started
= 0;
3696 path
= btrfs_alloc_path();
3701 * We don't need the lock here since we are protected by the transaction
3702 * commit. We want to do the cache_save_setup first and then run the
3703 * delayed refs to make sure we have the best chance at doing this all
3706 while (!list_empty(&cur_trans
->dirty_bgs
)) {
3707 cache
= list_first_entry(&cur_trans
->dirty_bgs
,
3708 struct btrfs_block_group_cache
,
3712 * this can happen if cache_save_setup re-dirties a block
3713 * group that is already under IO. Just wait for it to
3714 * finish and then do it all again
3716 if (!list_empty(&cache
->io_list
)) {
3717 list_del_init(&cache
->io_list
);
3718 btrfs_wait_cache_io(root
, trans
, cache
,
3719 &cache
->io_ctl
, path
,
3720 cache
->key
.objectid
);
3721 btrfs_put_block_group(cache
);
3725 * don't remove from the dirty list until after we've waited
3728 list_del_init(&cache
->dirty_list
);
3731 cache_save_setup(cache
, trans
, path
);
3734 ret
= btrfs_run_delayed_refs(trans
, root
, (unsigned long) -1);
3736 if (!ret
&& cache
->disk_cache_state
== BTRFS_DC_SETUP
) {
3737 cache
->io_ctl
.inode
= NULL
;
3738 ret
= btrfs_write_out_cache(root
, trans
, cache
, path
);
3739 if (ret
== 0 && cache
->io_ctl
.inode
) {
3742 list_add_tail(&cache
->io_list
, io
);
3745 * if we failed to write the cache, the
3746 * generation will be bad and life goes on
3752 ret
= write_one_cache_group(trans
, root
, path
, cache
);
3754 btrfs_abort_transaction(trans
, root
, ret
);
3757 /* if its not on the io list, we need to put the block group */
3759 btrfs_put_block_group(cache
);
3762 while (!list_empty(io
)) {
3763 cache
= list_first_entry(io
, struct btrfs_block_group_cache
,
3765 list_del_init(&cache
->io_list
);
3766 btrfs_wait_cache_io(root
, trans
, cache
,
3767 &cache
->io_ctl
, path
, cache
->key
.objectid
);
3768 btrfs_put_block_group(cache
);
3771 btrfs_free_path(path
);
3775 int btrfs_extent_readonly(struct btrfs_root
*root
, u64 bytenr
)
3777 struct btrfs_block_group_cache
*block_group
;
3780 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
3781 if (!block_group
|| block_group
->ro
)
3784 btrfs_put_block_group(block_group
);
3788 static const char *alloc_name(u64 flags
)
3791 case BTRFS_BLOCK_GROUP_METADATA
|BTRFS_BLOCK_GROUP_DATA
:
3793 case BTRFS_BLOCK_GROUP_METADATA
:
3795 case BTRFS_BLOCK_GROUP_DATA
:
3797 case BTRFS_BLOCK_GROUP_SYSTEM
:
3801 return "invalid-combination";
3805 static int update_space_info(struct btrfs_fs_info
*info
, u64 flags
,
3806 u64 total_bytes
, u64 bytes_used
,
3807 struct btrfs_space_info
**space_info
)
3809 struct btrfs_space_info
*found
;
3814 if (flags
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
3815 BTRFS_BLOCK_GROUP_RAID10
))
3820 found
= __find_space_info(info
, flags
);
3822 spin_lock(&found
->lock
);
3823 found
->total_bytes
+= total_bytes
;
3824 found
->disk_total
+= total_bytes
* factor
;
3825 found
->bytes_used
+= bytes_used
;
3826 found
->disk_used
+= bytes_used
* factor
;
3827 if (total_bytes
> 0)
3829 spin_unlock(&found
->lock
);
3830 *space_info
= found
;
3833 found
= kzalloc(sizeof(*found
), GFP_NOFS
);
3837 ret
= percpu_counter_init(&found
->total_bytes_pinned
, 0, GFP_KERNEL
);
3843 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++)
3844 INIT_LIST_HEAD(&found
->block_groups
[i
]);
3845 init_rwsem(&found
->groups_sem
);
3846 spin_lock_init(&found
->lock
);
3847 found
->flags
= flags
& BTRFS_BLOCK_GROUP_TYPE_MASK
;
3848 found
->total_bytes
= total_bytes
;
3849 found
->disk_total
= total_bytes
* factor
;
3850 found
->bytes_used
= bytes_used
;
3851 found
->disk_used
= bytes_used
* factor
;
3852 found
->bytes_pinned
= 0;
3853 found
->bytes_reserved
= 0;
3854 found
->bytes_readonly
= 0;
3855 found
->bytes_may_use
= 0;
3857 found
->max_extent_size
= 0;
3858 found
->force_alloc
= CHUNK_ALLOC_NO_FORCE
;
3859 found
->chunk_alloc
= 0;
3861 init_waitqueue_head(&found
->wait
);
3862 INIT_LIST_HEAD(&found
->ro_bgs
);
3864 ret
= kobject_init_and_add(&found
->kobj
, &space_info_ktype
,
3865 info
->space_info_kobj
, "%s",
3866 alloc_name(found
->flags
));
3872 *space_info
= found
;
3873 list_add_rcu(&found
->list
, &info
->space_info
);
3874 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3875 info
->data_sinfo
= found
;
3880 static void set_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
3882 u64 extra_flags
= chunk_to_extended(flags
) &
3883 BTRFS_EXTENDED_PROFILE_MASK
;
3885 write_seqlock(&fs_info
->profiles_lock
);
3886 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3887 fs_info
->avail_data_alloc_bits
|= extra_flags
;
3888 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
3889 fs_info
->avail_metadata_alloc_bits
|= extra_flags
;
3890 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
3891 fs_info
->avail_system_alloc_bits
|= extra_flags
;
3892 write_sequnlock(&fs_info
->profiles_lock
);
3896 * returns target flags in extended format or 0 if restripe for this
3897 * chunk_type is not in progress
3899 * should be called with either volume_mutex or balance_lock held
3901 static u64
get_restripe_target(struct btrfs_fs_info
*fs_info
, u64 flags
)
3903 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3909 if (flags
& BTRFS_BLOCK_GROUP_DATA
&&
3910 bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3911 target
= BTRFS_BLOCK_GROUP_DATA
| bctl
->data
.target
;
3912 } else if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
&&
3913 bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3914 target
= BTRFS_BLOCK_GROUP_SYSTEM
| bctl
->sys
.target
;
3915 } else if (flags
& BTRFS_BLOCK_GROUP_METADATA
&&
3916 bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3917 target
= BTRFS_BLOCK_GROUP_METADATA
| bctl
->meta
.target
;
3924 * @flags: available profiles in extended format (see ctree.h)
3926 * Returns reduced profile in chunk format. If profile changing is in
3927 * progress (either running or paused) picks the target profile (if it's
3928 * already available), otherwise falls back to plain reducing.
3930 static u64
btrfs_reduce_alloc_profile(struct btrfs_root
*root
, u64 flags
)
3932 u64 num_devices
= root
->fs_info
->fs_devices
->rw_devices
;
3938 * see if restripe for this chunk_type is in progress, if so
3939 * try to reduce to the target profile
3941 spin_lock(&root
->fs_info
->balance_lock
);
3942 target
= get_restripe_target(root
->fs_info
, flags
);
3944 /* pick target profile only if it's already available */
3945 if ((flags
& target
) & BTRFS_EXTENDED_PROFILE_MASK
) {
3946 spin_unlock(&root
->fs_info
->balance_lock
);
3947 return extended_to_chunk(target
);
3950 spin_unlock(&root
->fs_info
->balance_lock
);
3952 /* First, mask out the RAID levels which aren't possible */
3953 for (raid_type
= 0; raid_type
< BTRFS_NR_RAID_TYPES
; raid_type
++) {
3954 if (num_devices
>= btrfs_raid_array
[raid_type
].devs_min
)
3955 allowed
|= btrfs_raid_group
[raid_type
];
3959 if (allowed
& BTRFS_BLOCK_GROUP_RAID6
)
3960 allowed
= BTRFS_BLOCK_GROUP_RAID6
;
3961 else if (allowed
& BTRFS_BLOCK_GROUP_RAID5
)
3962 allowed
= BTRFS_BLOCK_GROUP_RAID5
;
3963 else if (allowed
& BTRFS_BLOCK_GROUP_RAID10
)
3964 allowed
= BTRFS_BLOCK_GROUP_RAID10
;
3965 else if (allowed
& BTRFS_BLOCK_GROUP_RAID1
)
3966 allowed
= BTRFS_BLOCK_GROUP_RAID1
;
3967 else if (allowed
& BTRFS_BLOCK_GROUP_RAID0
)
3968 allowed
= BTRFS_BLOCK_GROUP_RAID0
;
3970 flags
&= ~BTRFS_BLOCK_GROUP_PROFILE_MASK
;
3972 return extended_to_chunk(flags
| allowed
);
3975 static u64
get_alloc_profile(struct btrfs_root
*root
, u64 orig_flags
)
3982 seq
= read_seqbegin(&root
->fs_info
->profiles_lock
);
3984 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3985 flags
|= root
->fs_info
->avail_data_alloc_bits
;
3986 else if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
3987 flags
|= root
->fs_info
->avail_system_alloc_bits
;
3988 else if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
3989 flags
|= root
->fs_info
->avail_metadata_alloc_bits
;
3990 } while (read_seqretry(&root
->fs_info
->profiles_lock
, seq
));
3992 return btrfs_reduce_alloc_profile(root
, flags
);
3995 u64
btrfs_get_alloc_profile(struct btrfs_root
*root
, int data
)
4001 flags
= BTRFS_BLOCK_GROUP_DATA
;
4002 else if (root
== root
->fs_info
->chunk_root
)
4003 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
4005 flags
= BTRFS_BLOCK_GROUP_METADATA
;
4007 ret
= get_alloc_profile(root
, flags
);
4011 int btrfs_alloc_data_chunk_ondemand(struct inode
*inode
, u64 bytes
)
4013 struct btrfs_space_info
*data_sinfo
;
4014 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4015 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4018 int need_commit
= 2;
4019 int have_pinned_space
;
4021 /* make sure bytes are sectorsize aligned */
4022 bytes
= ALIGN(bytes
, root
->sectorsize
);
4024 if (btrfs_is_free_space_inode(inode
)) {
4026 ASSERT(current
->journal_info
);
4029 data_sinfo
= fs_info
->data_sinfo
;
4034 /* make sure we have enough space to handle the data first */
4035 spin_lock(&data_sinfo
->lock
);
4036 used
= data_sinfo
->bytes_used
+ data_sinfo
->bytes_reserved
+
4037 data_sinfo
->bytes_pinned
+ data_sinfo
->bytes_readonly
+
4038 data_sinfo
->bytes_may_use
;
4040 if (used
+ bytes
> data_sinfo
->total_bytes
) {
4041 struct btrfs_trans_handle
*trans
;
4044 * if we don't have enough free bytes in this space then we need
4045 * to alloc a new chunk.
4047 if (!data_sinfo
->full
) {
4050 data_sinfo
->force_alloc
= CHUNK_ALLOC_FORCE
;
4051 spin_unlock(&data_sinfo
->lock
);
4053 alloc_target
= btrfs_get_alloc_profile(root
, 1);
4055 * It is ugly that we don't call nolock join
4056 * transaction for the free space inode case here.
4057 * But it is safe because we only do the data space
4058 * reservation for the free space cache in the
4059 * transaction context, the common join transaction
4060 * just increase the counter of the current transaction
4061 * handler, doesn't try to acquire the trans_lock of
4064 trans
= btrfs_join_transaction(root
);
4066 return PTR_ERR(trans
);
4068 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
4070 CHUNK_ALLOC_NO_FORCE
);
4071 btrfs_end_transaction(trans
, root
);
4076 have_pinned_space
= 1;
4082 data_sinfo
= fs_info
->data_sinfo
;
4088 * If we don't have enough pinned space to deal with this
4089 * allocation, and no removed chunk in current transaction,
4090 * don't bother committing the transaction.
4092 have_pinned_space
= percpu_counter_compare(
4093 &data_sinfo
->total_bytes_pinned
,
4094 used
+ bytes
- data_sinfo
->total_bytes
);
4095 spin_unlock(&data_sinfo
->lock
);
4097 /* commit the current transaction and try again */
4100 !atomic_read(&root
->fs_info
->open_ioctl_trans
)) {
4103 if (need_commit
> 0)
4104 btrfs_wait_ordered_roots(fs_info
, -1);
4106 trans
= btrfs_join_transaction(root
);
4108 return PTR_ERR(trans
);
4109 if (have_pinned_space
>= 0 ||
4110 test_bit(BTRFS_TRANS_HAVE_FREE_BGS
,
4111 &trans
->transaction
->flags
) ||
4113 ret
= btrfs_commit_transaction(trans
, root
);
4117 * make sure that all running delayed iput are
4120 down_write(&root
->fs_info
->delayed_iput_sem
);
4121 up_write(&root
->fs_info
->delayed_iput_sem
);
4124 btrfs_end_transaction(trans
, root
);
4128 trace_btrfs_space_reservation(root
->fs_info
,
4129 "space_info:enospc",
4130 data_sinfo
->flags
, bytes
, 1);
4133 data_sinfo
->bytes_may_use
+= bytes
;
4134 trace_btrfs_space_reservation(root
->fs_info
, "space_info",
4135 data_sinfo
->flags
, bytes
, 1);
4136 spin_unlock(&data_sinfo
->lock
);
4142 * New check_data_free_space() with ability for precious data reservation
4143 * Will replace old btrfs_check_data_free_space(), but for patch split,
4144 * add a new function first and then replace it.
4146 int btrfs_check_data_free_space(struct inode
*inode
, u64 start
, u64 len
)
4148 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4151 /* align the range */
4152 len
= round_up(start
+ len
, root
->sectorsize
) -
4153 round_down(start
, root
->sectorsize
);
4154 start
= round_down(start
, root
->sectorsize
);
4156 ret
= btrfs_alloc_data_chunk_ondemand(inode
, len
);
4161 * Use new btrfs_qgroup_reserve_data to reserve precious data space
4163 * TODO: Find a good method to avoid reserve data space for NOCOW
4164 * range, but don't impact performance on quota disable case.
4166 ret
= btrfs_qgroup_reserve_data(inode
, start
, len
);
4171 * Called if we need to clear a data reservation for this inode
4172 * Normally in a error case.
4174 * This one will *NOT* use accurate qgroup reserved space API, just for case
4175 * which we can't sleep and is sure it won't affect qgroup reserved space.
4176 * Like clear_bit_hook().
4178 void btrfs_free_reserved_data_space_noquota(struct inode
*inode
, u64 start
,
4181 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4182 struct btrfs_space_info
*data_sinfo
;
4184 /* Make sure the range is aligned to sectorsize */
4185 len
= round_up(start
+ len
, root
->sectorsize
) -
4186 round_down(start
, root
->sectorsize
);
4187 start
= round_down(start
, root
->sectorsize
);
4189 data_sinfo
= root
->fs_info
->data_sinfo
;
4190 spin_lock(&data_sinfo
->lock
);
4191 if (WARN_ON(data_sinfo
->bytes_may_use
< len
))
4192 data_sinfo
->bytes_may_use
= 0;
4194 data_sinfo
->bytes_may_use
-= len
;
4195 trace_btrfs_space_reservation(root
->fs_info
, "space_info",
4196 data_sinfo
->flags
, len
, 0);
4197 spin_unlock(&data_sinfo
->lock
);
4201 * Called if we need to clear a data reservation for this inode
4202 * Normally in a error case.
4204 * This one will handle the per-indoe data rsv map for accurate reserved
4207 void btrfs_free_reserved_data_space(struct inode
*inode
, u64 start
, u64 len
)
4209 btrfs_free_reserved_data_space_noquota(inode
, start
, len
);
4210 btrfs_qgroup_free_data(inode
, start
, len
);
4213 static void force_metadata_allocation(struct btrfs_fs_info
*info
)
4215 struct list_head
*head
= &info
->space_info
;
4216 struct btrfs_space_info
*found
;
4219 list_for_each_entry_rcu(found
, head
, list
) {
4220 if (found
->flags
& BTRFS_BLOCK_GROUP_METADATA
)
4221 found
->force_alloc
= CHUNK_ALLOC_FORCE
;
4226 static inline u64
calc_global_rsv_need_space(struct btrfs_block_rsv
*global
)
4228 return (global
->size
<< 1);
4231 static int should_alloc_chunk(struct btrfs_root
*root
,
4232 struct btrfs_space_info
*sinfo
, int force
)
4234 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
4235 u64 num_bytes
= sinfo
->total_bytes
- sinfo
->bytes_readonly
;
4236 u64 num_allocated
= sinfo
->bytes_used
+ sinfo
->bytes_reserved
;
4239 if (force
== CHUNK_ALLOC_FORCE
)
4243 * We need to take into account the global rsv because for all intents
4244 * and purposes it's used space. Don't worry about locking the
4245 * global_rsv, it doesn't change except when the transaction commits.
4247 if (sinfo
->flags
& BTRFS_BLOCK_GROUP_METADATA
)
4248 num_allocated
+= calc_global_rsv_need_space(global_rsv
);
4251 * in limited mode, we want to have some free space up to
4252 * about 1% of the FS size.
4254 if (force
== CHUNK_ALLOC_LIMITED
) {
4255 thresh
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
4256 thresh
= max_t(u64
, 64 * 1024 * 1024,
4257 div_factor_fine(thresh
, 1));
4259 if (num_bytes
- num_allocated
< thresh
)
4263 if (num_allocated
+ 2 * 1024 * 1024 < div_factor(num_bytes
, 8))
4268 static u64
get_profile_num_devs(struct btrfs_root
*root
, u64 type
)
4272 if (type
& (BTRFS_BLOCK_GROUP_RAID10
|
4273 BTRFS_BLOCK_GROUP_RAID0
|
4274 BTRFS_BLOCK_GROUP_RAID5
|
4275 BTRFS_BLOCK_GROUP_RAID6
))
4276 num_dev
= root
->fs_info
->fs_devices
->rw_devices
;
4277 else if (type
& BTRFS_BLOCK_GROUP_RAID1
)
4280 num_dev
= 1; /* DUP or single */
4286 * If @is_allocation is true, reserve space in the system space info necessary
4287 * for allocating a chunk, otherwise if it's false, reserve space necessary for
4290 void check_system_chunk(struct btrfs_trans_handle
*trans
,
4291 struct btrfs_root
*root
,
4294 struct btrfs_space_info
*info
;
4301 * Needed because we can end up allocating a system chunk and for an
4302 * atomic and race free space reservation in the chunk block reserve.
4304 ASSERT(mutex_is_locked(&root
->fs_info
->chunk_mutex
));
4306 info
= __find_space_info(root
->fs_info
, BTRFS_BLOCK_GROUP_SYSTEM
);
4307 spin_lock(&info
->lock
);
4308 left
= info
->total_bytes
- info
->bytes_used
- info
->bytes_pinned
-
4309 info
->bytes_reserved
- info
->bytes_readonly
-
4310 info
->bytes_may_use
;
4311 spin_unlock(&info
->lock
);
4313 num_devs
= get_profile_num_devs(root
, type
);
4315 /* num_devs device items to update and 1 chunk item to add or remove */
4316 thresh
= btrfs_calc_trunc_metadata_size(root
, num_devs
) +
4317 btrfs_calc_trans_metadata_size(root
, 1);
4319 if (left
< thresh
&& btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
4320 btrfs_info(root
->fs_info
, "left=%llu, need=%llu, flags=%llu",
4321 left
, thresh
, type
);
4322 dump_space_info(info
, 0, 0);
4325 if (left
< thresh
) {
4328 flags
= btrfs_get_alloc_profile(root
->fs_info
->chunk_root
, 0);
4330 * Ignore failure to create system chunk. We might end up not
4331 * needing it, as we might not need to COW all nodes/leafs from
4332 * the paths we visit in the chunk tree (they were already COWed
4333 * or created in the current transaction for example).
4335 ret
= btrfs_alloc_chunk(trans
, root
, flags
);
4339 ret
= btrfs_block_rsv_add(root
->fs_info
->chunk_root
,
4340 &root
->fs_info
->chunk_block_rsv
,
4341 thresh
, BTRFS_RESERVE_NO_FLUSH
);
4343 trans
->chunk_bytes_reserved
+= thresh
;
4347 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
4348 struct btrfs_root
*extent_root
, u64 flags
, int force
)
4350 struct btrfs_space_info
*space_info
;
4351 struct btrfs_fs_info
*fs_info
= extent_root
->fs_info
;
4352 int wait_for_alloc
= 0;
4355 /* Don't re-enter if we're already allocating a chunk */
4356 if (trans
->allocating_chunk
)
4359 space_info
= __find_space_info(extent_root
->fs_info
, flags
);
4361 ret
= update_space_info(extent_root
->fs_info
, flags
,
4363 BUG_ON(ret
); /* -ENOMEM */
4365 BUG_ON(!space_info
); /* Logic error */
4368 spin_lock(&space_info
->lock
);
4369 if (force
< space_info
->force_alloc
)
4370 force
= space_info
->force_alloc
;
4371 if (space_info
->full
) {
4372 if (should_alloc_chunk(extent_root
, space_info
, force
))
4376 spin_unlock(&space_info
->lock
);
4380 if (!should_alloc_chunk(extent_root
, space_info
, force
)) {
4381 spin_unlock(&space_info
->lock
);
4383 } else if (space_info
->chunk_alloc
) {
4386 space_info
->chunk_alloc
= 1;
4389 spin_unlock(&space_info
->lock
);
4391 mutex_lock(&fs_info
->chunk_mutex
);
4394 * The chunk_mutex is held throughout the entirety of a chunk
4395 * allocation, so once we've acquired the chunk_mutex we know that the
4396 * other guy is done and we need to recheck and see if we should
4399 if (wait_for_alloc
) {
4400 mutex_unlock(&fs_info
->chunk_mutex
);
4405 trans
->allocating_chunk
= true;
4408 * If we have mixed data/metadata chunks we want to make sure we keep
4409 * allocating mixed chunks instead of individual chunks.
4411 if (btrfs_mixed_space_info(space_info
))
4412 flags
|= (BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
);
4415 * if we're doing a data chunk, go ahead and make sure that
4416 * we keep a reasonable number of metadata chunks allocated in the
4419 if (flags
& BTRFS_BLOCK_GROUP_DATA
&& fs_info
->metadata_ratio
) {
4420 fs_info
->data_chunk_allocations
++;
4421 if (!(fs_info
->data_chunk_allocations
%
4422 fs_info
->metadata_ratio
))
4423 force_metadata_allocation(fs_info
);
4427 * Check if we have enough space in SYSTEM chunk because we may need
4428 * to update devices.
4430 check_system_chunk(trans
, extent_root
, flags
);
4432 ret
= btrfs_alloc_chunk(trans
, extent_root
, flags
);
4433 trans
->allocating_chunk
= false;
4435 spin_lock(&space_info
->lock
);
4436 if (ret
< 0 && ret
!= -ENOSPC
)
4439 space_info
->full
= 1;
4443 space_info
->force_alloc
= CHUNK_ALLOC_NO_FORCE
;
4445 space_info
->chunk_alloc
= 0;
4446 spin_unlock(&space_info
->lock
);
4447 mutex_unlock(&fs_info
->chunk_mutex
);
4449 * When we allocate a new chunk we reserve space in the chunk block
4450 * reserve to make sure we can COW nodes/leafs in the chunk tree or
4451 * add new nodes/leafs to it if we end up needing to do it when
4452 * inserting the chunk item and updating device items as part of the
4453 * second phase of chunk allocation, performed by
4454 * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a
4455 * large number of new block groups to create in our transaction
4456 * handle's new_bgs list to avoid exhausting the chunk block reserve
4457 * in extreme cases - like having a single transaction create many new
4458 * block groups when starting to write out the free space caches of all
4459 * the block groups that were made dirty during the lifetime of the
4462 if (trans
->can_flush_pending_bgs
&&
4463 trans
->chunk_bytes_reserved
>= (2 * 1024 * 1024ull)) {
4464 btrfs_create_pending_block_groups(trans
, trans
->root
);
4465 btrfs_trans_release_chunk_metadata(trans
);
4470 static int can_overcommit(struct btrfs_root
*root
,
4471 struct btrfs_space_info
*space_info
, u64 bytes
,
4472 enum btrfs_reserve_flush_enum flush
)
4474 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
4475 u64 profile
= btrfs_get_alloc_profile(root
, 0);
4480 used
= space_info
->bytes_used
+ space_info
->bytes_reserved
+
4481 space_info
->bytes_pinned
+ space_info
->bytes_readonly
;
4484 * We only want to allow over committing if we have lots of actual space
4485 * free, but if we don't have enough space to handle the global reserve
4486 * space then we could end up having a real enospc problem when trying
4487 * to allocate a chunk or some other such important allocation.
4489 spin_lock(&global_rsv
->lock
);
4490 space_size
= calc_global_rsv_need_space(global_rsv
);
4491 spin_unlock(&global_rsv
->lock
);
4492 if (used
+ space_size
>= space_info
->total_bytes
)
4495 used
+= space_info
->bytes_may_use
;
4497 spin_lock(&root
->fs_info
->free_chunk_lock
);
4498 avail
= root
->fs_info
->free_chunk_space
;
4499 spin_unlock(&root
->fs_info
->free_chunk_lock
);
4502 * If we have dup, raid1 or raid10 then only half of the free
4503 * space is actually useable. For raid56, the space info used
4504 * doesn't include the parity drive, so we don't have to
4507 if (profile
& (BTRFS_BLOCK_GROUP_DUP
|
4508 BTRFS_BLOCK_GROUP_RAID1
|
4509 BTRFS_BLOCK_GROUP_RAID10
))
4513 * If we aren't flushing all things, let us overcommit up to
4514 * 1/2th of the space. If we can flush, don't let us overcommit
4515 * too much, let it overcommit up to 1/8 of the space.
4517 if (flush
== BTRFS_RESERVE_FLUSH_ALL
)
4522 if (used
+ bytes
< space_info
->total_bytes
+ avail
)
4527 static void btrfs_writeback_inodes_sb_nr(struct btrfs_root
*root
,
4528 unsigned long nr_pages
, int nr_items
)
4530 struct super_block
*sb
= root
->fs_info
->sb
;
4532 if (down_read_trylock(&sb
->s_umount
)) {
4533 writeback_inodes_sb_nr(sb
, nr_pages
, WB_REASON_FS_FREE_SPACE
);
4534 up_read(&sb
->s_umount
);
4537 * We needn't worry the filesystem going from r/w to r/o though
4538 * we don't acquire ->s_umount mutex, because the filesystem
4539 * should guarantee the delalloc inodes list be empty after
4540 * the filesystem is readonly(all dirty pages are written to
4543 btrfs_start_delalloc_roots(root
->fs_info
, 0, nr_items
);
4544 if (!current
->journal_info
)
4545 btrfs_wait_ordered_roots(root
->fs_info
, nr_items
);
4549 static inline int calc_reclaim_items_nr(struct btrfs_root
*root
, u64 to_reclaim
)
4554 bytes
= btrfs_calc_trans_metadata_size(root
, 1);
4555 nr
= (int)div64_u64(to_reclaim
, bytes
);
4561 #define EXTENT_SIZE_PER_ITEM (256 * 1024)
4564 * shrink metadata reservation for delalloc
4566 static void shrink_delalloc(struct btrfs_root
*root
, u64 to_reclaim
, u64 orig
,
4569 struct btrfs_block_rsv
*block_rsv
;
4570 struct btrfs_space_info
*space_info
;
4571 struct btrfs_trans_handle
*trans
;
4575 unsigned long nr_pages
;
4578 enum btrfs_reserve_flush_enum flush
;
4580 /* Calc the number of the pages we need flush for space reservation */
4581 items
= calc_reclaim_items_nr(root
, to_reclaim
);
4582 to_reclaim
= items
* EXTENT_SIZE_PER_ITEM
;
4584 trans
= (struct btrfs_trans_handle
*)current
->journal_info
;
4585 block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
4586 space_info
= block_rsv
->space_info
;
4588 delalloc_bytes
= percpu_counter_sum_positive(
4589 &root
->fs_info
->delalloc_bytes
);
4590 if (delalloc_bytes
== 0) {
4594 btrfs_wait_ordered_roots(root
->fs_info
, items
);
4599 while (delalloc_bytes
&& loops
< 3) {
4600 max_reclaim
= min(delalloc_bytes
, to_reclaim
);
4601 nr_pages
= max_reclaim
>> PAGE_CACHE_SHIFT
;
4602 btrfs_writeback_inodes_sb_nr(root
, nr_pages
, items
);
4604 * We need to wait for the async pages to actually start before
4607 max_reclaim
= atomic_read(&root
->fs_info
->async_delalloc_pages
);
4611 if (max_reclaim
<= nr_pages
)
4614 max_reclaim
-= nr_pages
;
4616 wait_event(root
->fs_info
->async_submit_wait
,
4617 atomic_read(&root
->fs_info
->async_delalloc_pages
) <=
4621 flush
= BTRFS_RESERVE_FLUSH_ALL
;
4623 flush
= BTRFS_RESERVE_NO_FLUSH
;
4624 spin_lock(&space_info
->lock
);
4625 if (can_overcommit(root
, space_info
, orig
, flush
)) {
4626 spin_unlock(&space_info
->lock
);
4629 spin_unlock(&space_info
->lock
);
4632 if (wait_ordered
&& !trans
) {
4633 btrfs_wait_ordered_roots(root
->fs_info
, items
);
4635 time_left
= schedule_timeout_killable(1);
4639 delalloc_bytes
= percpu_counter_sum_positive(
4640 &root
->fs_info
->delalloc_bytes
);
4645 * maybe_commit_transaction - possibly commit the transaction if its ok to
4646 * @root - the root we're allocating for
4647 * @bytes - the number of bytes we want to reserve
4648 * @force - force the commit
4650 * This will check to make sure that committing the transaction will actually
4651 * get us somewhere and then commit the transaction if it does. Otherwise it
4652 * will return -ENOSPC.
4654 static int may_commit_transaction(struct btrfs_root
*root
,
4655 struct btrfs_space_info
*space_info
,
4656 u64 bytes
, int force
)
4658 struct btrfs_block_rsv
*delayed_rsv
= &root
->fs_info
->delayed_block_rsv
;
4659 struct btrfs_trans_handle
*trans
;
4661 trans
= (struct btrfs_trans_handle
*)current
->journal_info
;
4668 /* See if there is enough pinned space to make this reservation */
4669 if (percpu_counter_compare(&space_info
->total_bytes_pinned
,
4674 * See if there is some space in the delayed insertion reservation for
4677 if (space_info
!= delayed_rsv
->space_info
)
4680 spin_lock(&delayed_rsv
->lock
);
4681 if (percpu_counter_compare(&space_info
->total_bytes_pinned
,
4682 bytes
- delayed_rsv
->size
) >= 0) {
4683 spin_unlock(&delayed_rsv
->lock
);
4686 spin_unlock(&delayed_rsv
->lock
);
4689 trans
= btrfs_join_transaction(root
);
4693 return btrfs_commit_transaction(trans
, root
);
4697 FLUSH_DELAYED_ITEMS_NR
= 1,
4698 FLUSH_DELAYED_ITEMS
= 2,
4700 FLUSH_DELALLOC_WAIT
= 4,
4705 static int flush_space(struct btrfs_root
*root
,
4706 struct btrfs_space_info
*space_info
, u64 num_bytes
,
4707 u64 orig_bytes
, int state
)
4709 struct btrfs_trans_handle
*trans
;
4714 case FLUSH_DELAYED_ITEMS_NR
:
4715 case FLUSH_DELAYED_ITEMS
:
4716 if (state
== FLUSH_DELAYED_ITEMS_NR
)
4717 nr
= calc_reclaim_items_nr(root
, num_bytes
) * 2;
4721 trans
= btrfs_join_transaction(root
);
4722 if (IS_ERR(trans
)) {
4723 ret
= PTR_ERR(trans
);
4726 ret
= btrfs_run_delayed_items_nr(trans
, root
, nr
);
4727 btrfs_end_transaction(trans
, root
);
4729 case FLUSH_DELALLOC
:
4730 case FLUSH_DELALLOC_WAIT
:
4731 shrink_delalloc(root
, num_bytes
* 2, orig_bytes
,
4732 state
== FLUSH_DELALLOC_WAIT
);
4735 trans
= btrfs_join_transaction(root
);
4736 if (IS_ERR(trans
)) {
4737 ret
= PTR_ERR(trans
);
4740 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
4741 btrfs_get_alloc_profile(root
, 0),
4742 CHUNK_ALLOC_NO_FORCE
);
4743 btrfs_end_transaction(trans
, root
);
4748 ret
= may_commit_transaction(root
, space_info
, orig_bytes
, 0);
4759 btrfs_calc_reclaim_metadata_size(struct btrfs_root
*root
,
4760 struct btrfs_space_info
*space_info
)
4766 to_reclaim
= min_t(u64
, num_online_cpus() * 1024 * 1024,
4768 spin_lock(&space_info
->lock
);
4769 if (can_overcommit(root
, space_info
, to_reclaim
,
4770 BTRFS_RESERVE_FLUSH_ALL
)) {
4775 used
= space_info
->bytes_used
+ space_info
->bytes_reserved
+
4776 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
4777 space_info
->bytes_may_use
;
4778 if (can_overcommit(root
, space_info
, 1024 * 1024,
4779 BTRFS_RESERVE_FLUSH_ALL
))
4780 expected
= div_factor_fine(space_info
->total_bytes
, 95);
4782 expected
= div_factor_fine(space_info
->total_bytes
, 90);
4784 if (used
> expected
)
4785 to_reclaim
= used
- expected
;
4788 to_reclaim
= min(to_reclaim
, space_info
->bytes_may_use
+
4789 space_info
->bytes_reserved
);
4791 spin_unlock(&space_info
->lock
);
4796 static inline int need_do_async_reclaim(struct btrfs_space_info
*space_info
,
4797 struct btrfs_fs_info
*fs_info
, u64 used
)
4799 u64 thresh
= div_factor_fine(space_info
->total_bytes
, 98);
4801 /* If we're just plain full then async reclaim just slows us down. */
4802 if (space_info
->bytes_used
>= thresh
)
4805 return (used
>= thresh
&& !btrfs_fs_closing(fs_info
) &&
4806 !test_bit(BTRFS_FS_STATE_REMOUNTING
, &fs_info
->fs_state
));
4809 static int btrfs_need_do_async_reclaim(struct btrfs_space_info
*space_info
,
4810 struct btrfs_fs_info
*fs_info
,
4815 spin_lock(&space_info
->lock
);
4817 * We run out of space and have not got any free space via flush_space,
4818 * so don't bother doing async reclaim.
4820 if (flush_state
> COMMIT_TRANS
&& space_info
->full
) {
4821 spin_unlock(&space_info
->lock
);
4825 used
= space_info
->bytes_used
+ space_info
->bytes_reserved
+
4826 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
4827 space_info
->bytes_may_use
;
4828 if (need_do_async_reclaim(space_info
, fs_info
, used
)) {
4829 spin_unlock(&space_info
->lock
);
4832 spin_unlock(&space_info
->lock
);
4837 static void btrfs_async_reclaim_metadata_space(struct work_struct
*work
)
4839 struct btrfs_fs_info
*fs_info
;
4840 struct btrfs_space_info
*space_info
;
4844 fs_info
= container_of(work
, struct btrfs_fs_info
, async_reclaim_work
);
4845 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
4847 to_reclaim
= btrfs_calc_reclaim_metadata_size(fs_info
->fs_root
,
4852 flush_state
= FLUSH_DELAYED_ITEMS_NR
;
4854 flush_space(fs_info
->fs_root
, space_info
, to_reclaim
,
4855 to_reclaim
, flush_state
);
4857 if (!btrfs_need_do_async_reclaim(space_info
, fs_info
,
4860 } while (flush_state
< COMMIT_TRANS
);
4863 void btrfs_init_async_reclaim_work(struct work_struct
*work
)
4865 INIT_WORK(work
, btrfs_async_reclaim_metadata_space
);
4869 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
4870 * @root - the root we're allocating for
4871 * @block_rsv - the block_rsv we're allocating for
4872 * @orig_bytes - the number of bytes we want
4873 * @flush - whether or not we can flush to make our reservation
4875 * This will reserve orgi_bytes number of bytes from the space info associated
4876 * with the block_rsv. If there is not enough space it will make an attempt to
4877 * flush out space to make room. It will do this by flushing delalloc if
4878 * possible or committing the transaction. If flush is 0 then no attempts to
4879 * regain reservations will be made and this will fail if there is not enough
4882 static int reserve_metadata_bytes(struct btrfs_root
*root
,
4883 struct btrfs_block_rsv
*block_rsv
,
4885 enum btrfs_reserve_flush_enum flush
)
4887 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
4889 u64 num_bytes
= orig_bytes
;
4890 int flush_state
= FLUSH_DELAYED_ITEMS_NR
;
4892 bool flushing
= false;
4896 spin_lock(&space_info
->lock
);
4898 * We only want to wait if somebody other than us is flushing and we
4899 * are actually allowed to flush all things.
4901 while (flush
== BTRFS_RESERVE_FLUSH_ALL
&& !flushing
&&
4902 space_info
->flush
) {
4903 spin_unlock(&space_info
->lock
);
4905 * If we have a trans handle we can't wait because the flusher
4906 * may have to commit the transaction, which would mean we would
4907 * deadlock since we are waiting for the flusher to finish, but
4908 * hold the current transaction open.
4910 if (current
->journal_info
)
4912 ret
= wait_event_killable(space_info
->wait
, !space_info
->flush
);
4913 /* Must have been killed, return */
4917 spin_lock(&space_info
->lock
);
4921 used
= space_info
->bytes_used
+ space_info
->bytes_reserved
+
4922 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
4923 space_info
->bytes_may_use
;
4926 * The idea here is that we've not already over-reserved the block group
4927 * then we can go ahead and save our reservation first and then start
4928 * flushing if we need to. Otherwise if we've already overcommitted
4929 * lets start flushing stuff first and then come back and try to make
4932 if (used
<= space_info
->total_bytes
) {
4933 if (used
+ orig_bytes
<= space_info
->total_bytes
) {
4934 space_info
->bytes_may_use
+= orig_bytes
;
4935 trace_btrfs_space_reservation(root
->fs_info
,
4936 "space_info", space_info
->flags
, orig_bytes
, 1);
4940 * Ok set num_bytes to orig_bytes since we aren't
4941 * overocmmitted, this way we only try and reclaim what
4944 num_bytes
= orig_bytes
;
4948 * Ok we're over committed, set num_bytes to the overcommitted
4949 * amount plus the amount of bytes that we need for this
4952 num_bytes
= used
- space_info
->total_bytes
+
4956 if (ret
&& can_overcommit(root
, space_info
, orig_bytes
, flush
)) {
4957 space_info
->bytes_may_use
+= orig_bytes
;
4958 trace_btrfs_space_reservation(root
->fs_info
, "space_info",
4959 space_info
->flags
, orig_bytes
,
4965 * Couldn't make our reservation, save our place so while we're trying
4966 * to reclaim space we can actually use it instead of somebody else
4967 * stealing it from us.
4969 * We make the other tasks wait for the flush only when we can flush
4972 if (ret
&& flush
!= BTRFS_RESERVE_NO_FLUSH
) {
4974 space_info
->flush
= 1;
4975 } else if (!ret
&& space_info
->flags
& BTRFS_BLOCK_GROUP_METADATA
) {
4978 * We will do the space reservation dance during log replay,
4979 * which means we won't have fs_info->fs_root set, so don't do
4980 * the async reclaim as we will panic.
4982 if (!root
->fs_info
->log_root_recovering
&&
4983 need_do_async_reclaim(space_info
, root
->fs_info
, used
) &&
4984 !work_busy(&root
->fs_info
->async_reclaim_work
))
4985 queue_work(system_unbound_wq
,
4986 &root
->fs_info
->async_reclaim_work
);
4988 spin_unlock(&space_info
->lock
);
4990 if (!ret
|| flush
== BTRFS_RESERVE_NO_FLUSH
)
4993 ret
= flush_space(root
, space_info
, num_bytes
, orig_bytes
,
4998 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4999 * would happen. So skip delalloc flush.
5001 if (flush
== BTRFS_RESERVE_FLUSH_LIMIT
&&
5002 (flush_state
== FLUSH_DELALLOC
||
5003 flush_state
== FLUSH_DELALLOC_WAIT
))
5004 flush_state
= ALLOC_CHUNK
;
5008 else if (flush
== BTRFS_RESERVE_FLUSH_LIMIT
&&
5009 flush_state
< COMMIT_TRANS
)
5011 else if (flush
== BTRFS_RESERVE_FLUSH_ALL
&&
5012 flush_state
<= COMMIT_TRANS
)
5016 if (ret
== -ENOSPC
&&
5017 unlikely(root
->orphan_cleanup_state
== ORPHAN_CLEANUP_STARTED
)) {
5018 struct btrfs_block_rsv
*global_rsv
=
5019 &root
->fs_info
->global_block_rsv
;
5021 if (block_rsv
!= global_rsv
&&
5022 !block_rsv_use_bytes(global_rsv
, orig_bytes
))
5026 trace_btrfs_space_reservation(root
->fs_info
,
5027 "space_info:enospc",
5028 space_info
->flags
, orig_bytes
, 1);
5030 spin_lock(&space_info
->lock
);
5031 space_info
->flush
= 0;
5032 wake_up_all(&space_info
->wait
);
5033 spin_unlock(&space_info
->lock
);
5038 static struct btrfs_block_rsv
*get_block_rsv(
5039 const struct btrfs_trans_handle
*trans
,
5040 const struct btrfs_root
*root
)
5042 struct btrfs_block_rsv
*block_rsv
= NULL
;
5044 if (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) ||
5045 (root
== root
->fs_info
->csum_root
&& trans
->adding_csums
) ||
5046 (root
== root
->fs_info
->uuid_root
))
5047 block_rsv
= trans
->block_rsv
;
5050 block_rsv
= root
->block_rsv
;
5053 block_rsv
= &root
->fs_info
->empty_block_rsv
;
5058 static int block_rsv_use_bytes(struct btrfs_block_rsv
*block_rsv
,
5062 spin_lock(&block_rsv
->lock
);
5063 if (block_rsv
->reserved
>= num_bytes
) {
5064 block_rsv
->reserved
-= num_bytes
;
5065 if (block_rsv
->reserved
< block_rsv
->size
)
5066 block_rsv
->full
= 0;
5069 spin_unlock(&block_rsv
->lock
);
5073 static void block_rsv_add_bytes(struct btrfs_block_rsv
*block_rsv
,
5074 u64 num_bytes
, int update_size
)
5076 spin_lock(&block_rsv
->lock
);
5077 block_rsv
->reserved
+= num_bytes
;
5079 block_rsv
->size
+= num_bytes
;
5080 else if (block_rsv
->reserved
>= block_rsv
->size
)
5081 block_rsv
->full
= 1;
5082 spin_unlock(&block_rsv
->lock
);
5085 int btrfs_cond_migrate_bytes(struct btrfs_fs_info
*fs_info
,
5086 struct btrfs_block_rsv
*dest
, u64 num_bytes
,
5089 struct btrfs_block_rsv
*global_rsv
= &fs_info
->global_block_rsv
;
5092 if (global_rsv
->space_info
!= dest
->space_info
)
5095 spin_lock(&global_rsv
->lock
);
5096 min_bytes
= div_factor(global_rsv
->size
, min_factor
);
5097 if (global_rsv
->reserved
< min_bytes
+ num_bytes
) {
5098 spin_unlock(&global_rsv
->lock
);
5101 global_rsv
->reserved
-= num_bytes
;
5102 if (global_rsv
->reserved
< global_rsv
->size
)
5103 global_rsv
->full
= 0;
5104 spin_unlock(&global_rsv
->lock
);
5106 block_rsv_add_bytes(dest
, num_bytes
, 1);
5110 static void block_rsv_release_bytes(struct btrfs_fs_info
*fs_info
,
5111 struct btrfs_block_rsv
*block_rsv
,
5112 struct btrfs_block_rsv
*dest
, u64 num_bytes
)
5114 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
5116 spin_lock(&block_rsv
->lock
);
5117 if (num_bytes
== (u64
)-1)
5118 num_bytes
= block_rsv
->size
;
5119 block_rsv
->size
-= num_bytes
;
5120 if (block_rsv
->reserved
>= block_rsv
->size
) {
5121 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
5122 block_rsv
->reserved
= block_rsv
->size
;
5123 block_rsv
->full
= 1;
5127 spin_unlock(&block_rsv
->lock
);
5129 if (num_bytes
> 0) {
5131 spin_lock(&dest
->lock
);
5135 bytes_to_add
= dest
->size
- dest
->reserved
;
5136 bytes_to_add
= min(num_bytes
, bytes_to_add
);
5137 dest
->reserved
+= bytes_to_add
;
5138 if (dest
->reserved
>= dest
->size
)
5140 num_bytes
-= bytes_to_add
;
5142 spin_unlock(&dest
->lock
);
5145 spin_lock(&space_info
->lock
);
5146 space_info
->bytes_may_use
-= num_bytes
;
5147 trace_btrfs_space_reservation(fs_info
, "space_info",
5148 space_info
->flags
, num_bytes
, 0);
5149 spin_unlock(&space_info
->lock
);
5154 static int block_rsv_migrate_bytes(struct btrfs_block_rsv
*src
,
5155 struct btrfs_block_rsv
*dst
, u64 num_bytes
)
5159 ret
= block_rsv_use_bytes(src
, num_bytes
);
5163 block_rsv_add_bytes(dst
, num_bytes
, 1);
5167 void btrfs_init_block_rsv(struct btrfs_block_rsv
*rsv
, unsigned short type
)
5169 memset(rsv
, 0, sizeof(*rsv
));
5170 spin_lock_init(&rsv
->lock
);
5174 struct btrfs_block_rsv
*btrfs_alloc_block_rsv(struct btrfs_root
*root
,
5175 unsigned short type
)
5177 struct btrfs_block_rsv
*block_rsv
;
5178 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5180 block_rsv
= kmalloc(sizeof(*block_rsv
), GFP_NOFS
);
5184 btrfs_init_block_rsv(block_rsv
, type
);
5185 block_rsv
->space_info
= __find_space_info(fs_info
,
5186 BTRFS_BLOCK_GROUP_METADATA
);
5190 void btrfs_free_block_rsv(struct btrfs_root
*root
,
5191 struct btrfs_block_rsv
*rsv
)
5195 btrfs_block_rsv_release(root
, rsv
, (u64
)-1);
5199 void __btrfs_free_block_rsv(struct btrfs_block_rsv
*rsv
)
5204 int btrfs_block_rsv_add(struct btrfs_root
*root
,
5205 struct btrfs_block_rsv
*block_rsv
, u64 num_bytes
,
5206 enum btrfs_reserve_flush_enum flush
)
5213 ret
= reserve_metadata_bytes(root
, block_rsv
, num_bytes
, flush
);
5215 block_rsv_add_bytes(block_rsv
, num_bytes
, 1);
5222 int btrfs_block_rsv_check(struct btrfs_root
*root
,
5223 struct btrfs_block_rsv
*block_rsv
, int min_factor
)
5231 spin_lock(&block_rsv
->lock
);
5232 num_bytes
= div_factor(block_rsv
->size
, min_factor
);
5233 if (block_rsv
->reserved
>= num_bytes
)
5235 spin_unlock(&block_rsv
->lock
);
5240 int btrfs_block_rsv_refill(struct btrfs_root
*root
,
5241 struct btrfs_block_rsv
*block_rsv
, u64 min_reserved
,
5242 enum btrfs_reserve_flush_enum flush
)
5250 spin_lock(&block_rsv
->lock
);
5251 num_bytes
= min_reserved
;
5252 if (block_rsv
->reserved
>= num_bytes
)
5255 num_bytes
-= block_rsv
->reserved
;
5256 spin_unlock(&block_rsv
->lock
);
5261 ret
= reserve_metadata_bytes(root
, block_rsv
, num_bytes
, flush
);
5263 block_rsv_add_bytes(block_rsv
, num_bytes
, 0);
5270 int btrfs_block_rsv_migrate(struct btrfs_block_rsv
*src_rsv
,
5271 struct btrfs_block_rsv
*dst_rsv
,
5274 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
5277 void btrfs_block_rsv_release(struct btrfs_root
*root
,
5278 struct btrfs_block_rsv
*block_rsv
,
5281 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
5282 if (global_rsv
== block_rsv
||
5283 block_rsv
->space_info
!= global_rsv
->space_info
)
5285 block_rsv_release_bytes(root
->fs_info
, block_rsv
, global_rsv
,
5290 * helper to calculate size of global block reservation.
5291 * the desired value is sum of space used by extent tree,
5292 * checksum tree and root tree
5294 static u64
calc_global_metadata_size(struct btrfs_fs_info
*fs_info
)
5296 struct btrfs_space_info
*sinfo
;
5300 int csum_size
= btrfs_super_csum_size(fs_info
->super_copy
);
5302 sinfo
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_DATA
);
5303 spin_lock(&sinfo
->lock
);
5304 data_used
= sinfo
->bytes_used
;
5305 spin_unlock(&sinfo
->lock
);
5307 sinfo
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
5308 spin_lock(&sinfo
->lock
);
5309 if (sinfo
->flags
& BTRFS_BLOCK_GROUP_DATA
)
5311 meta_used
= sinfo
->bytes_used
;
5312 spin_unlock(&sinfo
->lock
);
5314 num_bytes
= (data_used
>> fs_info
->sb
->s_blocksize_bits
) *
5316 num_bytes
+= div_u64(data_used
+ meta_used
, 50);
5318 if (num_bytes
* 3 > meta_used
)
5319 num_bytes
= div_u64(meta_used
, 3);
5321 return ALIGN(num_bytes
, fs_info
->extent_root
->nodesize
<< 10);
5324 static void update_global_block_rsv(struct btrfs_fs_info
*fs_info
)
5326 struct btrfs_block_rsv
*block_rsv
= &fs_info
->global_block_rsv
;
5327 struct btrfs_space_info
*sinfo
= block_rsv
->space_info
;
5330 num_bytes
= calc_global_metadata_size(fs_info
);
5332 spin_lock(&sinfo
->lock
);
5333 spin_lock(&block_rsv
->lock
);
5335 block_rsv
->size
= min_t(u64
, num_bytes
, 512 * 1024 * 1024);
5337 num_bytes
= sinfo
->bytes_used
+ sinfo
->bytes_pinned
+
5338 sinfo
->bytes_reserved
+ sinfo
->bytes_readonly
+
5339 sinfo
->bytes_may_use
;
5341 if (sinfo
->total_bytes
> num_bytes
) {
5342 num_bytes
= sinfo
->total_bytes
- num_bytes
;
5343 block_rsv
->reserved
+= num_bytes
;
5344 sinfo
->bytes_may_use
+= num_bytes
;
5345 trace_btrfs_space_reservation(fs_info
, "space_info",
5346 sinfo
->flags
, num_bytes
, 1);
5349 if (block_rsv
->reserved
>= block_rsv
->size
) {
5350 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
5351 sinfo
->bytes_may_use
-= num_bytes
;
5352 trace_btrfs_space_reservation(fs_info
, "space_info",
5353 sinfo
->flags
, num_bytes
, 0);
5354 block_rsv
->reserved
= block_rsv
->size
;
5355 block_rsv
->full
= 1;
5358 spin_unlock(&block_rsv
->lock
);
5359 spin_unlock(&sinfo
->lock
);
5362 static void init_global_block_rsv(struct btrfs_fs_info
*fs_info
)
5364 struct btrfs_space_info
*space_info
;
5366 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_SYSTEM
);
5367 fs_info
->chunk_block_rsv
.space_info
= space_info
;
5369 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
5370 fs_info
->global_block_rsv
.space_info
= space_info
;
5371 fs_info
->delalloc_block_rsv
.space_info
= space_info
;
5372 fs_info
->trans_block_rsv
.space_info
= space_info
;
5373 fs_info
->empty_block_rsv
.space_info
= space_info
;
5374 fs_info
->delayed_block_rsv
.space_info
= space_info
;
5376 fs_info
->extent_root
->block_rsv
= &fs_info
->global_block_rsv
;
5377 fs_info
->csum_root
->block_rsv
= &fs_info
->global_block_rsv
;
5378 fs_info
->dev_root
->block_rsv
= &fs_info
->global_block_rsv
;
5379 fs_info
->tree_root
->block_rsv
= &fs_info
->global_block_rsv
;
5380 if (fs_info
->quota_root
)
5381 fs_info
->quota_root
->block_rsv
= &fs_info
->global_block_rsv
;
5382 fs_info
->chunk_root
->block_rsv
= &fs_info
->chunk_block_rsv
;
5384 update_global_block_rsv(fs_info
);
5387 static void release_global_block_rsv(struct btrfs_fs_info
*fs_info
)
5389 block_rsv_release_bytes(fs_info
, &fs_info
->global_block_rsv
, NULL
,
5391 WARN_ON(fs_info
->delalloc_block_rsv
.size
> 0);
5392 WARN_ON(fs_info
->delalloc_block_rsv
.reserved
> 0);
5393 WARN_ON(fs_info
->trans_block_rsv
.size
> 0);
5394 WARN_ON(fs_info
->trans_block_rsv
.reserved
> 0);
5395 WARN_ON(fs_info
->chunk_block_rsv
.size
> 0);
5396 WARN_ON(fs_info
->chunk_block_rsv
.reserved
> 0);
5397 WARN_ON(fs_info
->delayed_block_rsv
.size
> 0);
5398 WARN_ON(fs_info
->delayed_block_rsv
.reserved
> 0);
5401 void btrfs_trans_release_metadata(struct btrfs_trans_handle
*trans
,
5402 struct btrfs_root
*root
)
5404 if (!trans
->block_rsv
)
5407 if (!trans
->bytes_reserved
)
5410 trace_btrfs_space_reservation(root
->fs_info
, "transaction",
5411 trans
->transid
, trans
->bytes_reserved
, 0);
5412 btrfs_block_rsv_release(root
, trans
->block_rsv
, trans
->bytes_reserved
);
5413 trans
->bytes_reserved
= 0;
5417 * To be called after all the new block groups attached to the transaction
5418 * handle have been created (btrfs_create_pending_block_groups()).
5420 void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle
*trans
)
5422 struct btrfs_fs_info
*fs_info
= trans
->root
->fs_info
;
5424 if (!trans
->chunk_bytes_reserved
)
5427 WARN_ON_ONCE(!list_empty(&trans
->new_bgs
));
5429 block_rsv_release_bytes(fs_info
, &fs_info
->chunk_block_rsv
, NULL
,
5430 trans
->chunk_bytes_reserved
);
5431 trans
->chunk_bytes_reserved
= 0;
5434 /* Can only return 0 or -ENOSPC */
5435 int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle
*trans
,
5436 struct inode
*inode
)
5438 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5439 struct btrfs_block_rsv
*src_rsv
= get_block_rsv(trans
, root
);
5440 struct btrfs_block_rsv
*dst_rsv
= root
->orphan_block_rsv
;
5443 * We need to hold space in order to delete our orphan item once we've
5444 * added it, so this takes the reservation so we can release it later
5445 * when we are truly done with the orphan item.
5447 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 1);
5448 trace_btrfs_space_reservation(root
->fs_info
, "orphan",
5449 btrfs_ino(inode
), num_bytes
, 1);
5450 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
5453 void btrfs_orphan_release_metadata(struct inode
*inode
)
5455 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5456 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 1);
5457 trace_btrfs_space_reservation(root
->fs_info
, "orphan",
5458 btrfs_ino(inode
), num_bytes
, 0);
5459 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
, num_bytes
);
5463 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
5464 * root: the root of the parent directory
5465 * rsv: block reservation
5466 * items: the number of items that we need do reservation
5467 * qgroup_reserved: used to return the reserved size in qgroup
5469 * This function is used to reserve the space for snapshot/subvolume
5470 * creation and deletion. Those operations are different with the
5471 * common file/directory operations, they change two fs/file trees
5472 * and root tree, the number of items that the qgroup reserves is
5473 * different with the free space reservation. So we can not use
5474 * the space reseravtion mechanism in start_transaction().
5476 int btrfs_subvolume_reserve_metadata(struct btrfs_root
*root
,
5477 struct btrfs_block_rsv
*rsv
,
5479 u64
*qgroup_reserved
,
5480 bool use_global_rsv
)
5484 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
5486 if (root
->fs_info
->quota_enabled
) {
5487 /* One for parent inode, two for dir entries */
5488 num_bytes
= 3 * root
->nodesize
;
5489 ret
= btrfs_qgroup_reserve_meta(root
, num_bytes
);
5496 *qgroup_reserved
= num_bytes
;
5498 num_bytes
= btrfs_calc_trans_metadata_size(root
, items
);
5499 rsv
->space_info
= __find_space_info(root
->fs_info
,
5500 BTRFS_BLOCK_GROUP_METADATA
);
5501 ret
= btrfs_block_rsv_add(root
, rsv
, num_bytes
,
5502 BTRFS_RESERVE_FLUSH_ALL
);
5504 if (ret
== -ENOSPC
&& use_global_rsv
)
5505 ret
= btrfs_block_rsv_migrate(global_rsv
, rsv
, num_bytes
);
5507 if (ret
&& *qgroup_reserved
)
5508 btrfs_qgroup_free_meta(root
, *qgroup_reserved
);
5513 void btrfs_subvolume_release_metadata(struct btrfs_root
*root
,
5514 struct btrfs_block_rsv
*rsv
,
5515 u64 qgroup_reserved
)
5517 btrfs_block_rsv_release(root
, rsv
, (u64
)-1);
5521 * drop_outstanding_extent - drop an outstanding extent
5522 * @inode: the inode we're dropping the extent for
5523 * @num_bytes: the number of bytes we're relaseing.
5525 * This is called when we are freeing up an outstanding extent, either called
5526 * after an error or after an extent is written. This will return the number of
5527 * reserved extents that need to be freed. This must be called with
5528 * BTRFS_I(inode)->lock held.
5530 static unsigned drop_outstanding_extent(struct inode
*inode
, u64 num_bytes
)
5532 unsigned drop_inode_space
= 0;
5533 unsigned dropped_extents
= 0;
5534 unsigned num_extents
= 0;
5536 num_extents
= (unsigned)div64_u64(num_bytes
+
5537 BTRFS_MAX_EXTENT_SIZE
- 1,
5538 BTRFS_MAX_EXTENT_SIZE
);
5539 ASSERT(num_extents
);
5540 ASSERT(BTRFS_I(inode
)->outstanding_extents
>= num_extents
);
5541 BTRFS_I(inode
)->outstanding_extents
-= num_extents
;
5543 if (BTRFS_I(inode
)->outstanding_extents
== 0 &&
5544 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED
,
5545 &BTRFS_I(inode
)->runtime_flags
))
5546 drop_inode_space
= 1;
5549 * If we have more or the same amount of outsanding extents than we have
5550 * reserved then we need to leave the reserved extents count alone.
5552 if (BTRFS_I(inode
)->outstanding_extents
>=
5553 BTRFS_I(inode
)->reserved_extents
)
5554 return drop_inode_space
;
5556 dropped_extents
= BTRFS_I(inode
)->reserved_extents
-
5557 BTRFS_I(inode
)->outstanding_extents
;
5558 BTRFS_I(inode
)->reserved_extents
-= dropped_extents
;
5559 return dropped_extents
+ drop_inode_space
;
5563 * calc_csum_metadata_size - return the amount of metada space that must be
5564 * reserved/free'd for the given bytes.
5565 * @inode: the inode we're manipulating
5566 * @num_bytes: the number of bytes in question
5567 * @reserve: 1 if we are reserving space, 0 if we are freeing space
5569 * This adjusts the number of csum_bytes in the inode and then returns the
5570 * correct amount of metadata that must either be reserved or freed. We
5571 * calculate how many checksums we can fit into one leaf and then divide the
5572 * number of bytes that will need to be checksumed by this value to figure out
5573 * how many checksums will be required. If we are adding bytes then the number
5574 * may go up and we will return the number of additional bytes that must be
5575 * reserved. If it is going down we will return the number of bytes that must
5578 * This must be called with BTRFS_I(inode)->lock held.
5580 static u64
calc_csum_metadata_size(struct inode
*inode
, u64 num_bytes
,
5583 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5584 u64 old_csums
, num_csums
;
5586 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
&&
5587 BTRFS_I(inode
)->csum_bytes
== 0)
5590 old_csums
= btrfs_csum_bytes_to_leaves(root
, BTRFS_I(inode
)->csum_bytes
);
5592 BTRFS_I(inode
)->csum_bytes
+= num_bytes
;
5594 BTRFS_I(inode
)->csum_bytes
-= num_bytes
;
5595 num_csums
= btrfs_csum_bytes_to_leaves(root
, BTRFS_I(inode
)->csum_bytes
);
5597 /* No change, no need to reserve more */
5598 if (old_csums
== num_csums
)
5602 return btrfs_calc_trans_metadata_size(root
,
5603 num_csums
- old_csums
);
5605 return btrfs_calc_trans_metadata_size(root
, old_csums
- num_csums
);
5608 int btrfs_delalloc_reserve_metadata(struct inode
*inode
, u64 num_bytes
)
5610 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5611 struct btrfs_block_rsv
*block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
5614 unsigned nr_extents
= 0;
5615 int extra_reserve
= 0;
5616 enum btrfs_reserve_flush_enum flush
= BTRFS_RESERVE_FLUSH_ALL
;
5618 bool delalloc_lock
= true;
5622 /* If we are a free space inode we need to not flush since we will be in
5623 * the middle of a transaction commit. We also don't need the delalloc
5624 * mutex since we won't race with anybody. We need this mostly to make
5625 * lockdep shut its filthy mouth.
5627 if (btrfs_is_free_space_inode(inode
)) {
5628 flush
= BTRFS_RESERVE_NO_FLUSH
;
5629 delalloc_lock
= false;
5632 if (flush
!= BTRFS_RESERVE_NO_FLUSH
&&
5633 btrfs_transaction_in_commit(root
->fs_info
))
5634 schedule_timeout(1);
5637 mutex_lock(&BTRFS_I(inode
)->delalloc_mutex
);
5639 num_bytes
= ALIGN(num_bytes
, root
->sectorsize
);
5641 spin_lock(&BTRFS_I(inode
)->lock
);
5642 nr_extents
= (unsigned)div64_u64(num_bytes
+
5643 BTRFS_MAX_EXTENT_SIZE
- 1,
5644 BTRFS_MAX_EXTENT_SIZE
);
5645 BTRFS_I(inode
)->outstanding_extents
+= nr_extents
;
5648 if (BTRFS_I(inode
)->outstanding_extents
>
5649 BTRFS_I(inode
)->reserved_extents
)
5650 nr_extents
= BTRFS_I(inode
)->outstanding_extents
-
5651 BTRFS_I(inode
)->reserved_extents
;
5654 * Add an item to reserve for updating the inode when we complete the
5657 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED
,
5658 &BTRFS_I(inode
)->runtime_flags
)) {
5663 to_reserve
= btrfs_calc_trans_metadata_size(root
, nr_extents
);
5664 to_reserve
+= calc_csum_metadata_size(inode
, num_bytes
, 1);
5665 csum_bytes
= BTRFS_I(inode
)->csum_bytes
;
5666 spin_unlock(&BTRFS_I(inode
)->lock
);
5668 if (root
->fs_info
->quota_enabled
) {
5669 ret
= btrfs_qgroup_reserve_meta(root
,
5670 nr_extents
* root
->nodesize
);
5675 ret
= reserve_metadata_bytes(root
, block_rsv
, to_reserve
, flush
);
5676 if (unlikely(ret
)) {
5677 btrfs_qgroup_free_meta(root
, nr_extents
* root
->nodesize
);
5681 spin_lock(&BTRFS_I(inode
)->lock
);
5682 if (extra_reserve
) {
5683 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED
,
5684 &BTRFS_I(inode
)->runtime_flags
);
5687 BTRFS_I(inode
)->reserved_extents
+= nr_extents
;
5688 spin_unlock(&BTRFS_I(inode
)->lock
);
5691 mutex_unlock(&BTRFS_I(inode
)->delalloc_mutex
);
5694 trace_btrfs_space_reservation(root
->fs_info
, "delalloc",
5695 btrfs_ino(inode
), to_reserve
, 1);
5696 block_rsv_add_bytes(block_rsv
, to_reserve
, 1);
5701 spin_lock(&BTRFS_I(inode
)->lock
);
5702 dropped
= drop_outstanding_extent(inode
, num_bytes
);
5704 * If the inodes csum_bytes is the same as the original
5705 * csum_bytes then we know we haven't raced with any free()ers
5706 * so we can just reduce our inodes csum bytes and carry on.
5708 if (BTRFS_I(inode
)->csum_bytes
== csum_bytes
) {
5709 calc_csum_metadata_size(inode
, num_bytes
, 0);
5711 u64 orig_csum_bytes
= BTRFS_I(inode
)->csum_bytes
;
5715 * This is tricky, but first we need to figure out how much we
5716 * free'd from any free-ers that occured during this
5717 * reservation, so we reset ->csum_bytes to the csum_bytes
5718 * before we dropped our lock, and then call the free for the
5719 * number of bytes that were freed while we were trying our
5722 bytes
= csum_bytes
- BTRFS_I(inode
)->csum_bytes
;
5723 BTRFS_I(inode
)->csum_bytes
= csum_bytes
;
5724 to_free
= calc_csum_metadata_size(inode
, bytes
, 0);
5728 * Now we need to see how much we would have freed had we not
5729 * been making this reservation and our ->csum_bytes were not
5730 * artificially inflated.
5732 BTRFS_I(inode
)->csum_bytes
= csum_bytes
- num_bytes
;
5733 bytes
= csum_bytes
- orig_csum_bytes
;
5734 bytes
= calc_csum_metadata_size(inode
, bytes
, 0);
5737 * Now reset ->csum_bytes to what it should be. If bytes is
5738 * more than to_free then we would have free'd more space had we
5739 * not had an artificially high ->csum_bytes, so we need to free
5740 * the remainder. If bytes is the same or less then we don't
5741 * need to do anything, the other free-ers did the correct
5744 BTRFS_I(inode
)->csum_bytes
= orig_csum_bytes
- num_bytes
;
5745 if (bytes
> to_free
)
5746 to_free
= bytes
- to_free
;
5750 spin_unlock(&BTRFS_I(inode
)->lock
);
5752 to_free
+= btrfs_calc_trans_metadata_size(root
, dropped
);
5755 btrfs_block_rsv_release(root
, block_rsv
, to_free
);
5756 trace_btrfs_space_reservation(root
->fs_info
, "delalloc",
5757 btrfs_ino(inode
), to_free
, 0);
5760 mutex_unlock(&BTRFS_I(inode
)->delalloc_mutex
);
5765 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
5766 * @inode: the inode to release the reservation for
5767 * @num_bytes: the number of bytes we're releasing
5769 * This will release the metadata reservation for an inode. This can be called
5770 * once we complete IO for a given set of bytes to release their metadata
5773 void btrfs_delalloc_release_metadata(struct inode
*inode
, u64 num_bytes
)
5775 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5779 num_bytes
= ALIGN(num_bytes
, root
->sectorsize
);
5780 spin_lock(&BTRFS_I(inode
)->lock
);
5781 dropped
= drop_outstanding_extent(inode
, num_bytes
);
5784 to_free
= calc_csum_metadata_size(inode
, num_bytes
, 0);
5785 spin_unlock(&BTRFS_I(inode
)->lock
);
5787 to_free
+= btrfs_calc_trans_metadata_size(root
, dropped
);
5789 if (btrfs_test_is_dummy_root(root
))
5792 trace_btrfs_space_reservation(root
->fs_info
, "delalloc",
5793 btrfs_ino(inode
), to_free
, 0);
5795 btrfs_block_rsv_release(root
, &root
->fs_info
->delalloc_block_rsv
,
5800 * btrfs_delalloc_reserve_space - reserve data and metadata space for
5802 * @inode: inode we're writing to
5803 * @start: start range we are writing to
5804 * @len: how long the range we are writing to
5806 * TODO: This function will finally replace old btrfs_delalloc_reserve_space()
5808 * This will do the following things
5810 * o reserve space in data space info for num bytes
5811 * and reserve precious corresponding qgroup space
5812 * (Done in check_data_free_space)
5814 * o reserve space for metadata space, based on the number of outstanding
5815 * extents and how much csums will be needed
5816 * also reserve metadata space in a per root over-reserve method.
5817 * o add to the inodes->delalloc_bytes
5818 * o add it to the fs_info's delalloc inodes list.
5819 * (Above 3 all done in delalloc_reserve_metadata)
5821 * Return 0 for success
5822 * Return <0 for error(-ENOSPC or -EQUOT)
5824 int btrfs_delalloc_reserve_space(struct inode
*inode
, u64 start
, u64 len
)
5828 ret
= btrfs_check_data_free_space(inode
, start
, len
);
5831 ret
= btrfs_delalloc_reserve_metadata(inode
, len
);
5833 btrfs_free_reserved_data_space(inode
, start
, len
);
5838 * btrfs_delalloc_release_space - release data and metadata space for delalloc
5839 * @inode: inode we're releasing space for
5840 * @start: start position of the space already reserved
5841 * @len: the len of the space already reserved
5843 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
5844 * called in the case that we don't need the metadata AND data reservations
5845 * anymore. So if there is an error or we insert an inline extent.
5847 * This function will release the metadata space that was not used and will
5848 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
5849 * list if there are no delalloc bytes left.
5850 * Also it will handle the qgroup reserved space.
5852 void btrfs_delalloc_release_space(struct inode
*inode
, u64 start
, u64 len
)
5854 btrfs_delalloc_release_metadata(inode
, len
);
5855 btrfs_free_reserved_data_space(inode
, start
, len
);
5858 static int update_block_group(struct btrfs_trans_handle
*trans
,
5859 struct btrfs_root
*root
, u64 bytenr
,
5860 u64 num_bytes
, int alloc
)
5862 struct btrfs_block_group_cache
*cache
= NULL
;
5863 struct btrfs_fs_info
*info
= root
->fs_info
;
5864 u64 total
= num_bytes
;
5869 /* block accounting for super block */
5870 spin_lock(&info
->delalloc_root_lock
);
5871 old_val
= btrfs_super_bytes_used(info
->super_copy
);
5873 old_val
+= num_bytes
;
5875 old_val
-= num_bytes
;
5876 btrfs_set_super_bytes_used(info
->super_copy
, old_val
);
5877 spin_unlock(&info
->delalloc_root_lock
);
5880 cache
= btrfs_lookup_block_group(info
, bytenr
);
5883 if (cache
->flags
& (BTRFS_BLOCK_GROUP_DUP
|
5884 BTRFS_BLOCK_GROUP_RAID1
|
5885 BTRFS_BLOCK_GROUP_RAID10
))
5890 * If this block group has free space cache written out, we
5891 * need to make sure to load it if we are removing space. This
5892 * is because we need the unpinning stage to actually add the
5893 * space back to the block group, otherwise we will leak space.
5895 if (!alloc
&& cache
->cached
== BTRFS_CACHE_NO
)
5896 cache_block_group(cache
, 1);
5898 byte_in_group
= bytenr
- cache
->key
.objectid
;
5899 WARN_ON(byte_in_group
> cache
->key
.offset
);
5901 spin_lock(&cache
->space_info
->lock
);
5902 spin_lock(&cache
->lock
);
5904 if (btrfs_test_opt(root
, SPACE_CACHE
) &&
5905 cache
->disk_cache_state
< BTRFS_DC_CLEAR
)
5906 cache
->disk_cache_state
= BTRFS_DC_CLEAR
;
5908 old_val
= btrfs_block_group_used(&cache
->item
);
5909 num_bytes
= min(total
, cache
->key
.offset
- byte_in_group
);
5911 old_val
+= num_bytes
;
5912 btrfs_set_block_group_used(&cache
->item
, old_val
);
5913 cache
->reserved
-= num_bytes
;
5914 cache
->space_info
->bytes_reserved
-= num_bytes
;
5915 cache
->space_info
->bytes_used
+= num_bytes
;
5916 cache
->space_info
->disk_used
+= num_bytes
* factor
;
5917 spin_unlock(&cache
->lock
);
5918 spin_unlock(&cache
->space_info
->lock
);
5920 old_val
-= num_bytes
;
5921 btrfs_set_block_group_used(&cache
->item
, old_val
);
5922 cache
->pinned
+= num_bytes
;
5923 cache
->space_info
->bytes_pinned
+= num_bytes
;
5924 cache
->space_info
->bytes_used
-= num_bytes
;
5925 cache
->space_info
->disk_used
-= num_bytes
* factor
;
5926 spin_unlock(&cache
->lock
);
5927 spin_unlock(&cache
->space_info
->lock
);
5929 set_extent_dirty(info
->pinned_extents
,
5930 bytenr
, bytenr
+ num_bytes
- 1,
5931 GFP_NOFS
| __GFP_NOFAIL
);
5934 spin_lock(&trans
->transaction
->dirty_bgs_lock
);
5935 if (list_empty(&cache
->dirty_list
)) {
5936 list_add_tail(&cache
->dirty_list
,
5937 &trans
->transaction
->dirty_bgs
);
5938 trans
->transaction
->num_dirty_bgs
++;
5939 btrfs_get_block_group(cache
);
5941 spin_unlock(&trans
->transaction
->dirty_bgs_lock
);
5944 * No longer have used bytes in this block group, queue it for
5945 * deletion. We do this after adding the block group to the
5946 * dirty list to avoid races between cleaner kthread and space
5949 if (!alloc
&& old_val
== 0) {
5950 spin_lock(&info
->unused_bgs_lock
);
5951 if (list_empty(&cache
->bg_list
)) {
5952 btrfs_get_block_group(cache
);
5953 list_add_tail(&cache
->bg_list
,
5956 spin_unlock(&info
->unused_bgs_lock
);
5959 btrfs_put_block_group(cache
);
5961 bytenr
+= num_bytes
;
5966 static u64
first_logical_byte(struct btrfs_root
*root
, u64 search_start
)
5968 struct btrfs_block_group_cache
*cache
;
5971 spin_lock(&root
->fs_info
->block_group_cache_lock
);
5972 bytenr
= root
->fs_info
->first_logical_byte
;
5973 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
5975 if (bytenr
< (u64
)-1)
5978 cache
= btrfs_lookup_first_block_group(root
->fs_info
, search_start
);
5982 bytenr
= cache
->key
.objectid
;
5983 btrfs_put_block_group(cache
);
5988 static int pin_down_extent(struct btrfs_root
*root
,
5989 struct btrfs_block_group_cache
*cache
,
5990 u64 bytenr
, u64 num_bytes
, int reserved
)
5992 spin_lock(&cache
->space_info
->lock
);
5993 spin_lock(&cache
->lock
);
5994 cache
->pinned
+= num_bytes
;
5995 cache
->space_info
->bytes_pinned
+= num_bytes
;
5997 cache
->reserved
-= num_bytes
;
5998 cache
->space_info
->bytes_reserved
-= num_bytes
;
6000 spin_unlock(&cache
->lock
);
6001 spin_unlock(&cache
->space_info
->lock
);
6003 set_extent_dirty(root
->fs_info
->pinned_extents
, bytenr
,
6004 bytenr
+ num_bytes
- 1, GFP_NOFS
| __GFP_NOFAIL
);
6006 trace_btrfs_reserved_extent_free(root
, bytenr
, num_bytes
);
6011 * this function must be called within transaction
6013 int btrfs_pin_extent(struct btrfs_root
*root
,
6014 u64 bytenr
, u64 num_bytes
, int reserved
)
6016 struct btrfs_block_group_cache
*cache
;
6018 cache
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
6019 BUG_ON(!cache
); /* Logic error */
6021 pin_down_extent(root
, cache
, bytenr
, num_bytes
, reserved
);
6023 btrfs_put_block_group(cache
);
6028 * this function must be called within transaction
6030 int btrfs_pin_extent_for_log_replay(struct btrfs_root
*root
,
6031 u64 bytenr
, u64 num_bytes
)
6033 struct btrfs_block_group_cache
*cache
;
6036 cache
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
6041 * pull in the free space cache (if any) so that our pin
6042 * removes the free space from the cache. We have load_only set
6043 * to one because the slow code to read in the free extents does check
6044 * the pinned extents.
6046 cache_block_group(cache
, 1);
6048 pin_down_extent(root
, cache
, bytenr
, num_bytes
, 0);
6050 /* remove us from the free space cache (if we're there at all) */
6051 ret
= btrfs_remove_free_space(cache
, bytenr
, num_bytes
);
6052 btrfs_put_block_group(cache
);
6056 static int __exclude_logged_extent(struct btrfs_root
*root
, u64 start
, u64 num_bytes
)
6059 struct btrfs_block_group_cache
*block_group
;
6060 struct btrfs_caching_control
*caching_ctl
;
6062 block_group
= btrfs_lookup_block_group(root
->fs_info
, start
);
6066 cache_block_group(block_group
, 0);
6067 caching_ctl
= get_caching_control(block_group
);
6071 BUG_ON(!block_group_cache_done(block_group
));
6072 ret
= btrfs_remove_free_space(block_group
, start
, num_bytes
);
6074 mutex_lock(&caching_ctl
->mutex
);
6076 if (start
>= caching_ctl
->progress
) {
6077 ret
= add_excluded_extent(root
, start
, num_bytes
);
6078 } else if (start
+ num_bytes
<= caching_ctl
->progress
) {
6079 ret
= btrfs_remove_free_space(block_group
,
6082 num_bytes
= caching_ctl
->progress
- start
;
6083 ret
= btrfs_remove_free_space(block_group
,
6088 num_bytes
= (start
+ num_bytes
) -
6089 caching_ctl
->progress
;
6090 start
= caching_ctl
->progress
;
6091 ret
= add_excluded_extent(root
, start
, num_bytes
);
6094 mutex_unlock(&caching_ctl
->mutex
);
6095 put_caching_control(caching_ctl
);
6097 btrfs_put_block_group(block_group
);
6101 int btrfs_exclude_logged_extents(struct btrfs_root
*log
,
6102 struct extent_buffer
*eb
)
6104 struct btrfs_file_extent_item
*item
;
6105 struct btrfs_key key
;
6109 if (!btrfs_fs_incompat(log
->fs_info
, MIXED_GROUPS
))
6112 for (i
= 0; i
< btrfs_header_nritems(eb
); i
++) {
6113 btrfs_item_key_to_cpu(eb
, &key
, i
);
6114 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
6116 item
= btrfs_item_ptr(eb
, i
, struct btrfs_file_extent_item
);
6117 found_type
= btrfs_file_extent_type(eb
, item
);
6118 if (found_type
== BTRFS_FILE_EXTENT_INLINE
)
6120 if (btrfs_file_extent_disk_bytenr(eb
, item
) == 0)
6122 key
.objectid
= btrfs_file_extent_disk_bytenr(eb
, item
);
6123 key
.offset
= btrfs_file_extent_disk_num_bytes(eb
, item
);
6124 __exclude_logged_extent(log
, key
.objectid
, key
.offset
);
6131 * btrfs_update_reserved_bytes - update the block_group and space info counters
6132 * @cache: The cache we are manipulating
6133 * @num_bytes: The number of bytes in question
6134 * @reserve: One of the reservation enums
6135 * @delalloc: The blocks are allocated for the delalloc write
6137 * This is called by the allocator when it reserves space, or by somebody who is
6138 * freeing space that was never actually used on disk. For example if you
6139 * reserve some space for a new leaf in transaction A and before transaction A
6140 * commits you free that leaf, you call this with reserve set to 0 in order to
6141 * clear the reservation.
6143 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
6144 * ENOSPC accounting. For data we handle the reservation through clearing the
6145 * delalloc bits in the io_tree. We have to do this since we could end up
6146 * allocating less disk space for the amount of data we have reserved in the
6147 * case of compression.
6149 * If this is a reservation and the block group has become read only we cannot
6150 * make the reservation and return -EAGAIN, otherwise this function always
6153 static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache
*cache
,
6154 u64 num_bytes
, int reserve
, int delalloc
)
6156 struct btrfs_space_info
*space_info
= cache
->space_info
;
6159 spin_lock(&space_info
->lock
);
6160 spin_lock(&cache
->lock
);
6161 if (reserve
!= RESERVE_FREE
) {
6165 cache
->reserved
+= num_bytes
;
6166 space_info
->bytes_reserved
+= num_bytes
;
6167 if (reserve
== RESERVE_ALLOC
) {
6168 trace_btrfs_space_reservation(cache
->fs_info
,
6169 "space_info", space_info
->flags
,
6171 space_info
->bytes_may_use
-= num_bytes
;
6175 cache
->delalloc_bytes
+= num_bytes
;
6179 space_info
->bytes_readonly
+= num_bytes
;
6180 cache
->reserved
-= num_bytes
;
6181 space_info
->bytes_reserved
-= num_bytes
;
6184 cache
->delalloc_bytes
-= num_bytes
;
6186 spin_unlock(&cache
->lock
);
6187 spin_unlock(&space_info
->lock
);
6191 void btrfs_prepare_extent_commit(struct btrfs_trans_handle
*trans
,
6192 struct btrfs_root
*root
)
6194 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
6195 struct btrfs_caching_control
*next
;
6196 struct btrfs_caching_control
*caching_ctl
;
6197 struct btrfs_block_group_cache
*cache
;
6199 down_write(&fs_info
->commit_root_sem
);
6201 list_for_each_entry_safe(caching_ctl
, next
,
6202 &fs_info
->caching_block_groups
, list
) {
6203 cache
= caching_ctl
->block_group
;
6204 if (block_group_cache_done(cache
)) {
6205 cache
->last_byte_to_unpin
= (u64
)-1;
6206 list_del_init(&caching_ctl
->list
);
6207 put_caching_control(caching_ctl
);
6209 cache
->last_byte_to_unpin
= caching_ctl
->progress
;
6213 if (fs_info
->pinned_extents
== &fs_info
->freed_extents
[0])
6214 fs_info
->pinned_extents
= &fs_info
->freed_extents
[1];
6216 fs_info
->pinned_extents
= &fs_info
->freed_extents
[0];
6218 up_write(&fs_info
->commit_root_sem
);
6220 update_global_block_rsv(fs_info
);
6224 * Returns the free cluster for the given space info and sets empty_cluster to
6225 * what it should be based on the mount options.
6227 static struct btrfs_free_cluster
*
6228 fetch_cluster_info(struct btrfs_root
*root
, struct btrfs_space_info
*space_info
,
6231 struct btrfs_free_cluster
*ret
= NULL
;
6232 bool ssd
= btrfs_test_opt(root
, SSD
);
6235 if (btrfs_mixed_space_info(space_info
))
6239 *empty_cluster
= 2 * 1024 * 1024;
6240 if (space_info
->flags
& BTRFS_BLOCK_GROUP_METADATA
) {
6241 ret
= &root
->fs_info
->meta_alloc_cluster
;
6243 *empty_cluster
= 64 * 1024;
6244 } else if ((space_info
->flags
& BTRFS_BLOCK_GROUP_DATA
) && ssd
) {
6245 ret
= &root
->fs_info
->data_alloc_cluster
;
6251 static int unpin_extent_range(struct btrfs_root
*root
, u64 start
, u64 end
,
6252 const bool return_free_space
)
6254 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
6255 struct btrfs_block_group_cache
*cache
= NULL
;
6256 struct btrfs_space_info
*space_info
;
6257 struct btrfs_block_rsv
*global_rsv
= &fs_info
->global_block_rsv
;
6258 struct btrfs_free_cluster
*cluster
= NULL
;
6260 u64 total_unpinned
= 0;
6261 u64 empty_cluster
= 0;
6264 while (start
<= end
) {
6267 start
>= cache
->key
.objectid
+ cache
->key
.offset
) {
6269 btrfs_put_block_group(cache
);
6271 cache
= btrfs_lookup_block_group(fs_info
, start
);
6272 BUG_ON(!cache
); /* Logic error */
6274 cluster
= fetch_cluster_info(root
,
6277 empty_cluster
<<= 1;
6280 len
= cache
->key
.objectid
+ cache
->key
.offset
- start
;
6281 len
= min(len
, end
+ 1 - start
);
6283 if (start
< cache
->last_byte_to_unpin
) {
6284 len
= min(len
, cache
->last_byte_to_unpin
- start
);
6285 if (return_free_space
)
6286 btrfs_add_free_space(cache
, start
, len
);
6290 total_unpinned
+= len
;
6291 space_info
= cache
->space_info
;
6294 * If this space cluster has been marked as fragmented and we've
6295 * unpinned enough in this block group to potentially allow a
6296 * cluster to be created inside of it go ahead and clear the
6299 if (cluster
&& cluster
->fragmented
&&
6300 total_unpinned
> empty_cluster
) {
6301 spin_lock(&cluster
->lock
);
6302 cluster
->fragmented
= 0;
6303 spin_unlock(&cluster
->lock
);
6306 spin_lock(&space_info
->lock
);
6307 spin_lock(&cache
->lock
);
6308 cache
->pinned
-= len
;
6309 space_info
->bytes_pinned
-= len
;
6310 space_info
->max_extent_size
= 0;
6311 percpu_counter_add(&space_info
->total_bytes_pinned
, -len
);
6313 space_info
->bytes_readonly
+= len
;
6316 spin_unlock(&cache
->lock
);
6317 if (!readonly
&& global_rsv
->space_info
== space_info
) {
6318 spin_lock(&global_rsv
->lock
);
6319 if (!global_rsv
->full
) {
6320 len
= min(len
, global_rsv
->size
-
6321 global_rsv
->reserved
);
6322 global_rsv
->reserved
+= len
;
6323 space_info
->bytes_may_use
+= len
;
6324 if (global_rsv
->reserved
>= global_rsv
->size
)
6325 global_rsv
->full
= 1;
6327 spin_unlock(&global_rsv
->lock
);
6329 spin_unlock(&space_info
->lock
);
6333 btrfs_put_block_group(cache
);
6337 int btrfs_finish_extent_commit(struct btrfs_trans_handle
*trans
,
6338 struct btrfs_root
*root
)
6340 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
6341 struct btrfs_block_group_cache
*block_group
, *tmp
;
6342 struct list_head
*deleted_bgs
;
6343 struct extent_io_tree
*unpin
;
6348 if (fs_info
->pinned_extents
== &fs_info
->freed_extents
[0])
6349 unpin
= &fs_info
->freed_extents
[1];
6351 unpin
= &fs_info
->freed_extents
[0];
6353 while (!trans
->aborted
) {
6354 mutex_lock(&fs_info
->unused_bg_unpin_mutex
);
6355 ret
= find_first_extent_bit(unpin
, 0, &start
, &end
,
6356 EXTENT_DIRTY
, NULL
);
6358 mutex_unlock(&fs_info
->unused_bg_unpin_mutex
);
6362 if (btrfs_test_opt(root
, DISCARD
))
6363 ret
= btrfs_discard_extent(root
, start
,
6364 end
+ 1 - start
, NULL
);
6366 clear_extent_dirty(unpin
, start
, end
, GFP_NOFS
);
6367 unpin_extent_range(root
, start
, end
, true);
6368 mutex_unlock(&fs_info
->unused_bg_unpin_mutex
);
6373 * Transaction is finished. We don't need the lock anymore. We
6374 * do need to clean up the block groups in case of a transaction
6377 deleted_bgs
= &trans
->transaction
->deleted_bgs
;
6378 list_for_each_entry_safe(block_group
, tmp
, deleted_bgs
, bg_list
) {
6382 if (!trans
->aborted
)
6383 ret
= btrfs_discard_extent(root
,
6384 block_group
->key
.objectid
,
6385 block_group
->key
.offset
,
6388 list_del_init(&block_group
->bg_list
);
6389 btrfs_put_block_group_trimming(block_group
);
6390 btrfs_put_block_group(block_group
);
6393 const char *errstr
= btrfs_decode_error(ret
);
6395 "Discard failed while removing blockgroup: errno=%d %s\n",
6403 static void add_pinned_bytes(struct btrfs_fs_info
*fs_info
, u64 num_bytes
,
6404 u64 owner
, u64 root_objectid
)
6406 struct btrfs_space_info
*space_info
;
6409 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
6410 if (root_objectid
== BTRFS_CHUNK_TREE_OBJECTID
)
6411 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
6413 flags
= BTRFS_BLOCK_GROUP_METADATA
;
6415 flags
= BTRFS_BLOCK_GROUP_DATA
;
6418 space_info
= __find_space_info(fs_info
, flags
);
6419 BUG_ON(!space_info
); /* Logic bug */
6420 percpu_counter_add(&space_info
->total_bytes_pinned
, num_bytes
);
6424 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
6425 struct btrfs_root
*root
,
6426 struct btrfs_delayed_ref_node
*node
, u64 parent
,
6427 u64 root_objectid
, u64 owner_objectid
,
6428 u64 owner_offset
, int refs_to_drop
,
6429 struct btrfs_delayed_extent_op
*extent_op
)
6431 struct btrfs_key key
;
6432 struct btrfs_path
*path
;
6433 struct btrfs_fs_info
*info
= root
->fs_info
;
6434 struct btrfs_root
*extent_root
= info
->extent_root
;
6435 struct extent_buffer
*leaf
;
6436 struct btrfs_extent_item
*ei
;
6437 struct btrfs_extent_inline_ref
*iref
;
6440 int extent_slot
= 0;
6441 int found_extent
= 0;
6445 u64 bytenr
= node
->bytenr
;
6446 u64 num_bytes
= node
->num_bytes
;
6448 bool skinny_metadata
= btrfs_fs_incompat(root
->fs_info
,
6451 path
= btrfs_alloc_path();
6456 path
->leave_spinning
= 1;
6458 is_data
= owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
;
6459 BUG_ON(!is_data
&& refs_to_drop
!= 1);
6462 skinny_metadata
= 0;
6464 ret
= lookup_extent_backref(trans
, extent_root
, path
, &iref
,
6465 bytenr
, num_bytes
, parent
,
6466 root_objectid
, owner_objectid
,
6469 extent_slot
= path
->slots
[0];
6470 while (extent_slot
>= 0) {
6471 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
6473 if (key
.objectid
!= bytenr
)
6475 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
6476 key
.offset
== num_bytes
) {
6480 if (key
.type
== BTRFS_METADATA_ITEM_KEY
&&
6481 key
.offset
== owner_objectid
) {
6485 if (path
->slots
[0] - extent_slot
> 5)
6489 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6490 item_size
= btrfs_item_size_nr(path
->nodes
[0], extent_slot
);
6491 if (found_extent
&& item_size
< sizeof(*ei
))
6494 if (!found_extent
) {
6496 ret
= remove_extent_backref(trans
, extent_root
, path
,
6498 is_data
, &last_ref
);
6500 btrfs_abort_transaction(trans
, extent_root
, ret
);
6503 btrfs_release_path(path
);
6504 path
->leave_spinning
= 1;
6506 key
.objectid
= bytenr
;
6507 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
6508 key
.offset
= num_bytes
;
6510 if (!is_data
&& skinny_metadata
) {
6511 key
.type
= BTRFS_METADATA_ITEM_KEY
;
6512 key
.offset
= owner_objectid
;
6515 ret
= btrfs_search_slot(trans
, extent_root
,
6517 if (ret
> 0 && skinny_metadata
&& path
->slots
[0]) {
6519 * Couldn't find our skinny metadata item,
6520 * see if we have ye olde extent item.
6523 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
6525 if (key
.objectid
== bytenr
&&
6526 key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
6527 key
.offset
== num_bytes
)
6531 if (ret
> 0 && skinny_metadata
) {
6532 skinny_metadata
= false;
6533 key
.objectid
= bytenr
;
6534 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
6535 key
.offset
= num_bytes
;
6536 btrfs_release_path(path
);
6537 ret
= btrfs_search_slot(trans
, extent_root
,
6542 btrfs_err(info
, "umm, got %d back from search, was looking for %llu",
6545 btrfs_print_leaf(extent_root
,
6549 btrfs_abort_transaction(trans
, extent_root
, ret
);
6552 extent_slot
= path
->slots
[0];
6554 } else if (WARN_ON(ret
== -ENOENT
)) {
6555 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
6557 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
6558 bytenr
, parent
, root_objectid
, owner_objectid
,
6560 btrfs_abort_transaction(trans
, extent_root
, ret
);
6563 btrfs_abort_transaction(trans
, extent_root
, ret
);
6567 leaf
= path
->nodes
[0];
6568 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
6569 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6570 if (item_size
< sizeof(*ei
)) {
6571 BUG_ON(found_extent
|| extent_slot
!= path
->slots
[0]);
6572 ret
= convert_extent_item_v0(trans
, extent_root
, path
,
6575 btrfs_abort_transaction(trans
, extent_root
, ret
);
6579 btrfs_release_path(path
);
6580 path
->leave_spinning
= 1;
6582 key
.objectid
= bytenr
;
6583 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
6584 key
.offset
= num_bytes
;
6586 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
,
6589 btrfs_err(info
, "umm, got %d back from search, was looking for %llu",
6591 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
6594 btrfs_abort_transaction(trans
, extent_root
, ret
);
6598 extent_slot
= path
->slots
[0];
6599 leaf
= path
->nodes
[0];
6600 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
6603 BUG_ON(item_size
< sizeof(*ei
));
6604 ei
= btrfs_item_ptr(leaf
, extent_slot
,
6605 struct btrfs_extent_item
);
6606 if (owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
&&
6607 key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
6608 struct btrfs_tree_block_info
*bi
;
6609 BUG_ON(item_size
< sizeof(*ei
) + sizeof(*bi
));
6610 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
6611 WARN_ON(owner_objectid
!= btrfs_tree_block_level(leaf
, bi
));
6614 refs
= btrfs_extent_refs(leaf
, ei
);
6615 if (refs
< refs_to_drop
) {
6616 btrfs_err(info
, "trying to drop %d refs but we only have %Lu "
6617 "for bytenr %Lu", refs_to_drop
, refs
, bytenr
);
6619 btrfs_abort_transaction(trans
, extent_root
, ret
);
6622 refs
-= refs_to_drop
;
6626 __run_delayed_extent_op(extent_op
, leaf
, ei
);
6628 * In the case of inline back ref, reference count will
6629 * be updated by remove_extent_backref
6632 BUG_ON(!found_extent
);
6634 btrfs_set_extent_refs(leaf
, ei
, refs
);
6635 btrfs_mark_buffer_dirty(leaf
);
6638 ret
= remove_extent_backref(trans
, extent_root
, path
,
6640 is_data
, &last_ref
);
6642 btrfs_abort_transaction(trans
, extent_root
, ret
);
6646 add_pinned_bytes(root
->fs_info
, -num_bytes
, owner_objectid
,
6650 BUG_ON(is_data
&& refs_to_drop
!=
6651 extent_data_ref_count(path
, iref
));
6653 BUG_ON(path
->slots
[0] != extent_slot
);
6655 BUG_ON(path
->slots
[0] != extent_slot
+ 1);
6656 path
->slots
[0] = extent_slot
;
6662 ret
= btrfs_del_items(trans
, extent_root
, path
, path
->slots
[0],
6665 btrfs_abort_transaction(trans
, extent_root
, ret
);
6668 btrfs_release_path(path
);
6671 ret
= btrfs_del_csums(trans
, root
, bytenr
, num_bytes
);
6673 btrfs_abort_transaction(trans
, extent_root
, ret
);
6678 ret
= add_to_free_space_tree(trans
, root
->fs_info
, bytenr
,
6681 btrfs_abort_transaction(trans
, extent_root
, ret
);
6685 ret
= update_block_group(trans
, root
, bytenr
, num_bytes
, 0);
6687 btrfs_abort_transaction(trans
, extent_root
, ret
);
6691 btrfs_release_path(path
);
6694 btrfs_free_path(path
);
6699 * when we free an block, it is possible (and likely) that we free the last
6700 * delayed ref for that extent as well. This searches the delayed ref tree for
6701 * a given extent, and if there are no other delayed refs to be processed, it
6702 * removes it from the tree.
6704 static noinline
int check_ref_cleanup(struct btrfs_trans_handle
*trans
,
6705 struct btrfs_root
*root
, u64 bytenr
)
6707 struct btrfs_delayed_ref_head
*head
;
6708 struct btrfs_delayed_ref_root
*delayed_refs
;
6711 delayed_refs
= &trans
->transaction
->delayed_refs
;
6712 spin_lock(&delayed_refs
->lock
);
6713 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
6715 goto out_delayed_unlock
;
6717 spin_lock(&head
->lock
);
6718 if (!list_empty(&head
->ref_list
))
6721 if (head
->extent_op
) {
6722 if (!head
->must_insert_reserved
)
6724 btrfs_free_delayed_extent_op(head
->extent_op
);
6725 head
->extent_op
= NULL
;
6729 * waiting for the lock here would deadlock. If someone else has it
6730 * locked they are already in the process of dropping it anyway
6732 if (!mutex_trylock(&head
->mutex
))
6736 * at this point we have a head with no other entries. Go
6737 * ahead and process it.
6739 head
->node
.in_tree
= 0;
6740 rb_erase(&head
->href_node
, &delayed_refs
->href_root
);
6742 atomic_dec(&delayed_refs
->num_entries
);
6745 * we don't take a ref on the node because we're removing it from the
6746 * tree, so we just steal the ref the tree was holding.
6748 delayed_refs
->num_heads
--;
6749 if (head
->processing
== 0)
6750 delayed_refs
->num_heads_ready
--;
6751 head
->processing
= 0;
6752 spin_unlock(&head
->lock
);
6753 spin_unlock(&delayed_refs
->lock
);
6755 BUG_ON(head
->extent_op
);
6756 if (head
->must_insert_reserved
)
6759 mutex_unlock(&head
->mutex
);
6760 btrfs_put_delayed_ref(&head
->node
);
6763 spin_unlock(&head
->lock
);
6766 spin_unlock(&delayed_refs
->lock
);
6770 void btrfs_free_tree_block(struct btrfs_trans_handle
*trans
,
6771 struct btrfs_root
*root
,
6772 struct extent_buffer
*buf
,
6773 u64 parent
, int last_ref
)
6778 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
6779 ret
= btrfs_add_delayed_tree_ref(root
->fs_info
, trans
,
6780 buf
->start
, buf
->len
,
6781 parent
, root
->root_key
.objectid
,
6782 btrfs_header_level(buf
),
6783 BTRFS_DROP_DELAYED_REF
, NULL
);
6784 BUG_ON(ret
); /* -ENOMEM */
6790 if (btrfs_header_generation(buf
) == trans
->transid
) {
6791 struct btrfs_block_group_cache
*cache
;
6793 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
6794 ret
= check_ref_cleanup(trans
, root
, buf
->start
);
6799 cache
= btrfs_lookup_block_group(root
->fs_info
, buf
->start
);
6801 if (btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
)) {
6802 pin_down_extent(root
, cache
, buf
->start
, buf
->len
, 1);
6803 btrfs_put_block_group(cache
);
6807 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY
, &buf
->bflags
));
6809 btrfs_add_free_space(cache
, buf
->start
, buf
->len
);
6810 btrfs_update_reserved_bytes(cache
, buf
->len
, RESERVE_FREE
, 0);
6811 btrfs_put_block_group(cache
);
6812 trace_btrfs_reserved_extent_free(root
, buf
->start
, buf
->len
);
6817 add_pinned_bytes(root
->fs_info
, buf
->len
,
6818 btrfs_header_level(buf
),
6819 root
->root_key
.objectid
);
6822 * Deleting the buffer, clear the corrupt flag since it doesn't matter
6825 clear_bit(EXTENT_BUFFER_CORRUPT
, &buf
->bflags
);
6828 /* Can return -ENOMEM */
6829 int btrfs_free_extent(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
6830 u64 bytenr
, u64 num_bytes
, u64 parent
, u64 root_objectid
,
6831 u64 owner
, u64 offset
)
6834 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
6836 if (btrfs_test_is_dummy_root(root
))
6839 add_pinned_bytes(root
->fs_info
, num_bytes
, owner
, root_objectid
);
6842 * tree log blocks never actually go into the extent allocation
6843 * tree, just update pinning info and exit early.
6845 if (root_objectid
== BTRFS_TREE_LOG_OBJECTID
) {
6846 WARN_ON(owner
>= BTRFS_FIRST_FREE_OBJECTID
);
6847 /* unlocks the pinned mutex */
6848 btrfs_pin_extent(root
, bytenr
, num_bytes
, 1);
6850 } else if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
6851 ret
= btrfs_add_delayed_tree_ref(fs_info
, trans
, bytenr
,
6853 parent
, root_objectid
, (int)owner
,
6854 BTRFS_DROP_DELAYED_REF
, NULL
);
6856 ret
= btrfs_add_delayed_data_ref(fs_info
, trans
, bytenr
,
6858 parent
, root_objectid
, owner
,
6860 BTRFS_DROP_DELAYED_REF
, NULL
);
6866 * when we wait for progress in the block group caching, its because
6867 * our allocation attempt failed at least once. So, we must sleep
6868 * and let some progress happen before we try again.
6870 * This function will sleep at least once waiting for new free space to
6871 * show up, and then it will check the block group free space numbers
6872 * for our min num_bytes. Another option is to have it go ahead
6873 * and look in the rbtree for a free extent of a given size, but this
6876 * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
6877 * any of the information in this block group.
6879 static noinline
void
6880 wait_block_group_cache_progress(struct btrfs_block_group_cache
*cache
,
6883 struct btrfs_caching_control
*caching_ctl
;
6885 caching_ctl
= get_caching_control(cache
);
6889 wait_event(caching_ctl
->wait
, block_group_cache_done(cache
) ||
6890 (cache
->free_space_ctl
->free_space
>= num_bytes
));
6892 put_caching_control(caching_ctl
);
6896 wait_block_group_cache_done(struct btrfs_block_group_cache
*cache
)
6898 struct btrfs_caching_control
*caching_ctl
;
6901 caching_ctl
= get_caching_control(cache
);
6903 return (cache
->cached
== BTRFS_CACHE_ERROR
) ? -EIO
: 0;
6905 wait_event(caching_ctl
->wait
, block_group_cache_done(cache
));
6906 if (cache
->cached
== BTRFS_CACHE_ERROR
)
6908 put_caching_control(caching_ctl
);
6912 int __get_raid_index(u64 flags
)
6914 if (flags
& BTRFS_BLOCK_GROUP_RAID10
)
6915 return BTRFS_RAID_RAID10
;
6916 else if (flags
& BTRFS_BLOCK_GROUP_RAID1
)
6917 return BTRFS_RAID_RAID1
;
6918 else if (flags
& BTRFS_BLOCK_GROUP_DUP
)
6919 return BTRFS_RAID_DUP
;
6920 else if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
6921 return BTRFS_RAID_RAID0
;
6922 else if (flags
& BTRFS_BLOCK_GROUP_RAID5
)
6923 return BTRFS_RAID_RAID5
;
6924 else if (flags
& BTRFS_BLOCK_GROUP_RAID6
)
6925 return BTRFS_RAID_RAID6
;
6927 return BTRFS_RAID_SINGLE
; /* BTRFS_BLOCK_GROUP_SINGLE */
6930 int get_block_group_index(struct btrfs_block_group_cache
*cache
)
6932 return __get_raid_index(cache
->flags
);
6935 static const char *btrfs_raid_type_names
[BTRFS_NR_RAID_TYPES
] = {
6936 [BTRFS_RAID_RAID10
] = "raid10",
6937 [BTRFS_RAID_RAID1
] = "raid1",
6938 [BTRFS_RAID_DUP
] = "dup",
6939 [BTRFS_RAID_RAID0
] = "raid0",
6940 [BTRFS_RAID_SINGLE
] = "single",
6941 [BTRFS_RAID_RAID5
] = "raid5",
6942 [BTRFS_RAID_RAID6
] = "raid6",
6945 static const char *get_raid_name(enum btrfs_raid_types type
)
6947 if (type
>= BTRFS_NR_RAID_TYPES
)
6950 return btrfs_raid_type_names
[type
];
6953 enum btrfs_loop_type
{
6954 LOOP_CACHING_NOWAIT
= 0,
6955 LOOP_CACHING_WAIT
= 1,
6956 LOOP_ALLOC_CHUNK
= 2,
6957 LOOP_NO_EMPTY_SIZE
= 3,
6961 btrfs_lock_block_group(struct btrfs_block_group_cache
*cache
,
6965 down_read(&cache
->data_rwsem
);
6969 btrfs_grab_block_group(struct btrfs_block_group_cache
*cache
,
6972 btrfs_get_block_group(cache
);
6974 down_read(&cache
->data_rwsem
);
6977 static struct btrfs_block_group_cache
*
6978 btrfs_lock_cluster(struct btrfs_block_group_cache
*block_group
,
6979 struct btrfs_free_cluster
*cluster
,
6982 struct btrfs_block_group_cache
*used_bg
;
6983 bool locked
= false;
6985 spin_lock(&cluster
->refill_lock
);
6987 if (used_bg
== cluster
->block_group
)
6990 up_read(&used_bg
->data_rwsem
);
6991 btrfs_put_block_group(used_bg
);
6994 used_bg
= cluster
->block_group
;
6998 if (used_bg
== block_group
)
7001 btrfs_get_block_group(used_bg
);
7006 if (down_read_trylock(&used_bg
->data_rwsem
))
7009 spin_unlock(&cluster
->refill_lock
);
7010 down_read(&used_bg
->data_rwsem
);
7016 btrfs_release_block_group(struct btrfs_block_group_cache
*cache
,
7020 up_read(&cache
->data_rwsem
);
7021 btrfs_put_block_group(cache
);
7025 * walks the btree of allocated extents and find a hole of a given size.
7026 * The key ins is changed to record the hole:
7027 * ins->objectid == start position
7028 * ins->flags = BTRFS_EXTENT_ITEM_KEY
7029 * ins->offset == the size of the hole.
7030 * Any available blocks before search_start are skipped.
7032 * If there is no suitable free space, we will record the max size of
7033 * the free space extent currently.
7035 static noinline
int find_free_extent(struct btrfs_root
*orig_root
,
7036 u64 num_bytes
, u64 empty_size
,
7037 u64 hint_byte
, struct btrfs_key
*ins
,
7038 u64 flags
, int delalloc
)
7041 struct btrfs_root
*root
= orig_root
->fs_info
->extent_root
;
7042 struct btrfs_free_cluster
*last_ptr
= NULL
;
7043 struct btrfs_block_group_cache
*block_group
= NULL
;
7044 u64 search_start
= 0;
7045 u64 max_extent_size
= 0;
7046 u64 empty_cluster
= 0;
7047 struct btrfs_space_info
*space_info
;
7049 int index
= __get_raid_index(flags
);
7050 int alloc_type
= (flags
& BTRFS_BLOCK_GROUP_DATA
) ?
7051 RESERVE_ALLOC_NO_ACCOUNT
: RESERVE_ALLOC
;
7052 bool failed_cluster_refill
= false;
7053 bool failed_alloc
= false;
7054 bool use_cluster
= true;
7055 bool have_caching_bg
= false;
7056 bool orig_have_caching_bg
= false;
7057 bool full_search
= false;
7059 WARN_ON(num_bytes
< root
->sectorsize
);
7060 ins
->type
= BTRFS_EXTENT_ITEM_KEY
;
7064 trace_find_free_extent(orig_root
, num_bytes
, empty_size
, flags
);
7066 space_info
= __find_space_info(root
->fs_info
, flags
);
7068 btrfs_err(root
->fs_info
, "No space info for %llu", flags
);
7073 * If our free space is heavily fragmented we may not be able to make
7074 * big contiguous allocations, so instead of doing the expensive search
7075 * for free space, simply return ENOSPC with our max_extent_size so we
7076 * can go ahead and search for a more manageable chunk.
7078 * If our max_extent_size is large enough for our allocation simply
7079 * disable clustering since we will likely not be able to find enough
7080 * space to create a cluster and induce latency trying.
7082 if (unlikely(space_info
->max_extent_size
)) {
7083 spin_lock(&space_info
->lock
);
7084 if (space_info
->max_extent_size
&&
7085 num_bytes
> space_info
->max_extent_size
) {
7086 ins
->offset
= space_info
->max_extent_size
;
7087 spin_unlock(&space_info
->lock
);
7089 } else if (space_info
->max_extent_size
) {
7090 use_cluster
= false;
7092 spin_unlock(&space_info
->lock
);
7095 last_ptr
= fetch_cluster_info(orig_root
, space_info
, &empty_cluster
);
7097 spin_lock(&last_ptr
->lock
);
7098 if (last_ptr
->block_group
)
7099 hint_byte
= last_ptr
->window_start
;
7100 if (last_ptr
->fragmented
) {
7102 * We still set window_start so we can keep track of the
7103 * last place we found an allocation to try and save
7106 hint_byte
= last_ptr
->window_start
;
7107 use_cluster
= false;
7109 spin_unlock(&last_ptr
->lock
);
7112 search_start
= max(search_start
, first_logical_byte(root
, 0));
7113 search_start
= max(search_start
, hint_byte
);
7114 if (search_start
== hint_byte
) {
7115 block_group
= btrfs_lookup_block_group(root
->fs_info
,
7118 * we don't want to use the block group if it doesn't match our
7119 * allocation bits, or if its not cached.
7121 * However if we are re-searching with an ideal block group
7122 * picked out then we don't care that the block group is cached.
7124 if (block_group
&& block_group_bits(block_group
, flags
) &&
7125 block_group
->cached
!= BTRFS_CACHE_NO
) {
7126 down_read(&space_info
->groups_sem
);
7127 if (list_empty(&block_group
->list
) ||
7130 * someone is removing this block group,
7131 * we can't jump into the have_block_group
7132 * target because our list pointers are not
7135 btrfs_put_block_group(block_group
);
7136 up_read(&space_info
->groups_sem
);
7138 index
= get_block_group_index(block_group
);
7139 btrfs_lock_block_group(block_group
, delalloc
);
7140 goto have_block_group
;
7142 } else if (block_group
) {
7143 btrfs_put_block_group(block_group
);
7147 have_caching_bg
= false;
7148 if (index
== 0 || index
== __get_raid_index(flags
))
7150 down_read(&space_info
->groups_sem
);
7151 list_for_each_entry(block_group
, &space_info
->block_groups
[index
],
7156 btrfs_grab_block_group(block_group
, delalloc
);
7157 search_start
= block_group
->key
.objectid
;
7160 * this can happen if we end up cycling through all the
7161 * raid types, but we want to make sure we only allocate
7162 * for the proper type.
7164 if (!block_group_bits(block_group
, flags
)) {
7165 u64 extra
= BTRFS_BLOCK_GROUP_DUP
|
7166 BTRFS_BLOCK_GROUP_RAID1
|
7167 BTRFS_BLOCK_GROUP_RAID5
|
7168 BTRFS_BLOCK_GROUP_RAID6
|
7169 BTRFS_BLOCK_GROUP_RAID10
;
7172 * if they asked for extra copies and this block group
7173 * doesn't provide them, bail. This does allow us to
7174 * fill raid0 from raid1.
7176 if ((flags
& extra
) && !(block_group
->flags
& extra
))
7181 cached
= block_group_cache_done(block_group
);
7182 if (unlikely(!cached
)) {
7183 have_caching_bg
= true;
7184 ret
= cache_block_group(block_group
, 0);
7189 if (unlikely(block_group
->cached
== BTRFS_CACHE_ERROR
))
7191 if (unlikely(block_group
->ro
))
7195 * Ok we want to try and use the cluster allocator, so
7198 if (last_ptr
&& use_cluster
) {
7199 struct btrfs_block_group_cache
*used_block_group
;
7200 unsigned long aligned_cluster
;
7202 * the refill lock keeps out other
7203 * people trying to start a new cluster
7205 used_block_group
= btrfs_lock_cluster(block_group
,
7208 if (!used_block_group
)
7209 goto refill_cluster
;
7211 if (used_block_group
!= block_group
&&
7212 (used_block_group
->ro
||
7213 !block_group_bits(used_block_group
, flags
)))
7214 goto release_cluster
;
7216 offset
= btrfs_alloc_from_cluster(used_block_group
,
7219 used_block_group
->key
.objectid
,
7222 /* we have a block, we're done */
7223 spin_unlock(&last_ptr
->refill_lock
);
7224 trace_btrfs_reserve_extent_cluster(root
,
7226 search_start
, num_bytes
);
7227 if (used_block_group
!= block_group
) {
7228 btrfs_release_block_group(block_group
,
7230 block_group
= used_block_group
;
7235 WARN_ON(last_ptr
->block_group
!= used_block_group
);
7237 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
7238 * set up a new clusters, so lets just skip it
7239 * and let the allocator find whatever block
7240 * it can find. If we reach this point, we
7241 * will have tried the cluster allocator
7242 * plenty of times and not have found
7243 * anything, so we are likely way too
7244 * fragmented for the clustering stuff to find
7247 * However, if the cluster is taken from the
7248 * current block group, release the cluster
7249 * first, so that we stand a better chance of
7250 * succeeding in the unclustered
7252 if (loop
>= LOOP_NO_EMPTY_SIZE
&&
7253 used_block_group
!= block_group
) {
7254 spin_unlock(&last_ptr
->refill_lock
);
7255 btrfs_release_block_group(used_block_group
,
7257 goto unclustered_alloc
;
7261 * this cluster didn't work out, free it and
7264 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
7266 if (used_block_group
!= block_group
)
7267 btrfs_release_block_group(used_block_group
,
7270 if (loop
>= LOOP_NO_EMPTY_SIZE
) {
7271 spin_unlock(&last_ptr
->refill_lock
);
7272 goto unclustered_alloc
;
7275 aligned_cluster
= max_t(unsigned long,
7276 empty_cluster
+ empty_size
,
7277 block_group
->full_stripe_len
);
7279 /* allocate a cluster in this block group */
7280 ret
= btrfs_find_space_cluster(root
, block_group
,
7281 last_ptr
, search_start
,
7286 * now pull our allocation out of this
7289 offset
= btrfs_alloc_from_cluster(block_group
,
7295 /* we found one, proceed */
7296 spin_unlock(&last_ptr
->refill_lock
);
7297 trace_btrfs_reserve_extent_cluster(root
,
7298 block_group
, search_start
,
7302 } else if (!cached
&& loop
> LOOP_CACHING_NOWAIT
7303 && !failed_cluster_refill
) {
7304 spin_unlock(&last_ptr
->refill_lock
);
7306 failed_cluster_refill
= true;
7307 wait_block_group_cache_progress(block_group
,
7308 num_bytes
+ empty_cluster
+ empty_size
);
7309 goto have_block_group
;
7313 * at this point we either didn't find a cluster
7314 * or we weren't able to allocate a block from our
7315 * cluster. Free the cluster we've been trying
7316 * to use, and go to the next block group
7318 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
7319 spin_unlock(&last_ptr
->refill_lock
);
7325 * We are doing an unclustered alloc, set the fragmented flag so
7326 * we don't bother trying to setup a cluster again until we get
7329 if (unlikely(last_ptr
)) {
7330 spin_lock(&last_ptr
->lock
);
7331 last_ptr
->fragmented
= 1;
7332 spin_unlock(&last_ptr
->lock
);
7334 spin_lock(&block_group
->free_space_ctl
->tree_lock
);
7336 block_group
->free_space_ctl
->free_space
<
7337 num_bytes
+ empty_cluster
+ empty_size
) {
7338 if (block_group
->free_space_ctl
->free_space
>
7341 block_group
->free_space_ctl
->free_space
;
7342 spin_unlock(&block_group
->free_space_ctl
->tree_lock
);
7345 spin_unlock(&block_group
->free_space_ctl
->tree_lock
);
7347 offset
= btrfs_find_space_for_alloc(block_group
, search_start
,
7348 num_bytes
, empty_size
,
7351 * If we didn't find a chunk, and we haven't failed on this
7352 * block group before, and this block group is in the middle of
7353 * caching and we are ok with waiting, then go ahead and wait
7354 * for progress to be made, and set failed_alloc to true.
7356 * If failed_alloc is true then we've already waited on this
7357 * block group once and should move on to the next block group.
7359 if (!offset
&& !failed_alloc
&& !cached
&&
7360 loop
> LOOP_CACHING_NOWAIT
) {
7361 wait_block_group_cache_progress(block_group
,
7362 num_bytes
+ empty_size
);
7363 failed_alloc
= true;
7364 goto have_block_group
;
7365 } else if (!offset
) {
7369 search_start
= ALIGN(offset
, root
->stripesize
);
7371 /* move on to the next group */
7372 if (search_start
+ num_bytes
>
7373 block_group
->key
.objectid
+ block_group
->key
.offset
) {
7374 btrfs_add_free_space(block_group
, offset
, num_bytes
);
7378 if (offset
< search_start
)
7379 btrfs_add_free_space(block_group
, offset
,
7380 search_start
- offset
);
7381 BUG_ON(offset
> search_start
);
7383 ret
= btrfs_update_reserved_bytes(block_group
, num_bytes
,
7384 alloc_type
, delalloc
);
7385 if (ret
== -EAGAIN
) {
7386 btrfs_add_free_space(block_group
, offset
, num_bytes
);
7390 /* we are all good, lets return */
7391 ins
->objectid
= search_start
;
7392 ins
->offset
= num_bytes
;
7394 trace_btrfs_reserve_extent(orig_root
, block_group
,
7395 search_start
, num_bytes
);
7396 btrfs_release_block_group(block_group
, delalloc
);
7399 failed_cluster_refill
= false;
7400 failed_alloc
= false;
7401 BUG_ON(index
!= get_block_group_index(block_group
));
7402 btrfs_release_block_group(block_group
, delalloc
);
7404 up_read(&space_info
->groups_sem
);
7406 if ((loop
== LOOP_CACHING_NOWAIT
) && have_caching_bg
7407 && !orig_have_caching_bg
)
7408 orig_have_caching_bg
= true;
7410 if (!ins
->objectid
&& loop
>= LOOP_CACHING_WAIT
&& have_caching_bg
)
7413 if (!ins
->objectid
&& ++index
< BTRFS_NR_RAID_TYPES
)
7417 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
7418 * caching kthreads as we move along
7419 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
7420 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
7421 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
7424 if (!ins
->objectid
&& loop
< LOOP_NO_EMPTY_SIZE
) {
7426 if (loop
== LOOP_CACHING_NOWAIT
) {
7428 * We want to skip the LOOP_CACHING_WAIT step if we
7429 * don't have any unached bgs and we've alrelady done a
7430 * full search through.
7432 if (orig_have_caching_bg
|| !full_search
)
7433 loop
= LOOP_CACHING_WAIT
;
7435 loop
= LOOP_ALLOC_CHUNK
;
7440 if (loop
== LOOP_ALLOC_CHUNK
) {
7441 struct btrfs_trans_handle
*trans
;
7444 trans
= current
->journal_info
;
7448 trans
= btrfs_join_transaction(root
);
7450 if (IS_ERR(trans
)) {
7451 ret
= PTR_ERR(trans
);
7455 ret
= do_chunk_alloc(trans
, root
, flags
,
7459 * If we can't allocate a new chunk we've already looped
7460 * through at least once, move on to the NO_EMPTY_SIZE
7464 loop
= LOOP_NO_EMPTY_SIZE
;
7467 * Do not bail out on ENOSPC since we
7468 * can do more things.
7470 if (ret
< 0 && ret
!= -ENOSPC
)
7471 btrfs_abort_transaction(trans
,
7476 btrfs_end_transaction(trans
, root
);
7481 if (loop
== LOOP_NO_EMPTY_SIZE
) {
7483 * Don't loop again if we already have no empty_size and
7486 if (empty_size
== 0 &&
7487 empty_cluster
== 0) {
7496 } else if (!ins
->objectid
) {
7498 } else if (ins
->objectid
) {
7499 if (!use_cluster
&& last_ptr
) {
7500 spin_lock(&last_ptr
->lock
);
7501 last_ptr
->window_start
= ins
->objectid
;
7502 spin_unlock(&last_ptr
->lock
);
7507 if (ret
== -ENOSPC
) {
7508 spin_lock(&space_info
->lock
);
7509 space_info
->max_extent_size
= max_extent_size
;
7510 spin_unlock(&space_info
->lock
);
7511 ins
->offset
= max_extent_size
;
7516 static void dump_space_info(struct btrfs_space_info
*info
, u64 bytes
,
7517 int dump_block_groups
)
7519 struct btrfs_block_group_cache
*cache
;
7522 spin_lock(&info
->lock
);
7523 printk(KERN_INFO
"BTRFS: space_info %llu has %llu free, is %sfull\n",
7525 info
->total_bytes
- info
->bytes_used
- info
->bytes_pinned
-
7526 info
->bytes_reserved
- info
->bytes_readonly
,
7527 (info
->full
) ? "" : "not ");
7528 printk(KERN_INFO
"BTRFS: space_info total=%llu, used=%llu, pinned=%llu, "
7529 "reserved=%llu, may_use=%llu, readonly=%llu\n",
7530 info
->total_bytes
, info
->bytes_used
, info
->bytes_pinned
,
7531 info
->bytes_reserved
, info
->bytes_may_use
,
7532 info
->bytes_readonly
);
7533 spin_unlock(&info
->lock
);
7535 if (!dump_block_groups
)
7538 down_read(&info
->groups_sem
);
7540 list_for_each_entry(cache
, &info
->block_groups
[index
], list
) {
7541 spin_lock(&cache
->lock
);
7542 printk(KERN_INFO
"BTRFS: "
7543 "block group %llu has %llu bytes, "
7544 "%llu used %llu pinned %llu reserved %s\n",
7545 cache
->key
.objectid
, cache
->key
.offset
,
7546 btrfs_block_group_used(&cache
->item
), cache
->pinned
,
7547 cache
->reserved
, cache
->ro
? "[readonly]" : "");
7548 btrfs_dump_free_space(cache
, bytes
);
7549 spin_unlock(&cache
->lock
);
7551 if (++index
< BTRFS_NR_RAID_TYPES
)
7553 up_read(&info
->groups_sem
);
7556 int btrfs_reserve_extent(struct btrfs_root
*root
,
7557 u64 num_bytes
, u64 min_alloc_size
,
7558 u64 empty_size
, u64 hint_byte
,
7559 struct btrfs_key
*ins
, int is_data
, int delalloc
)
7561 bool final_tried
= num_bytes
== min_alloc_size
;
7565 flags
= btrfs_get_alloc_profile(root
, is_data
);
7567 WARN_ON(num_bytes
< root
->sectorsize
);
7568 ret
= find_free_extent(root
, num_bytes
, empty_size
, hint_byte
, ins
,
7571 if (ret
== -ENOSPC
) {
7572 if (!final_tried
&& ins
->offset
) {
7573 num_bytes
= min(num_bytes
>> 1, ins
->offset
);
7574 num_bytes
= round_down(num_bytes
, root
->sectorsize
);
7575 num_bytes
= max(num_bytes
, min_alloc_size
);
7576 if (num_bytes
== min_alloc_size
)
7579 } else if (btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
7580 struct btrfs_space_info
*sinfo
;
7582 sinfo
= __find_space_info(root
->fs_info
, flags
);
7583 btrfs_err(root
->fs_info
, "allocation failed flags %llu, wanted %llu",
7586 dump_space_info(sinfo
, num_bytes
, 1);
7593 static int __btrfs_free_reserved_extent(struct btrfs_root
*root
,
7595 int pin
, int delalloc
)
7597 struct btrfs_block_group_cache
*cache
;
7600 cache
= btrfs_lookup_block_group(root
->fs_info
, start
);
7602 btrfs_err(root
->fs_info
, "Unable to find block group for %llu",
7608 pin_down_extent(root
, cache
, start
, len
, 1);
7610 if (btrfs_test_opt(root
, DISCARD
))
7611 ret
= btrfs_discard_extent(root
, start
, len
, NULL
);
7612 btrfs_add_free_space(cache
, start
, len
);
7613 btrfs_update_reserved_bytes(cache
, len
, RESERVE_FREE
, delalloc
);
7616 btrfs_put_block_group(cache
);
7618 trace_btrfs_reserved_extent_free(root
, start
, len
);
7623 int btrfs_free_reserved_extent(struct btrfs_root
*root
,
7624 u64 start
, u64 len
, int delalloc
)
7626 return __btrfs_free_reserved_extent(root
, start
, len
, 0, delalloc
);
7629 int btrfs_free_and_pin_reserved_extent(struct btrfs_root
*root
,
7632 return __btrfs_free_reserved_extent(root
, start
, len
, 1, 0);
7635 static int alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
7636 struct btrfs_root
*root
,
7637 u64 parent
, u64 root_objectid
,
7638 u64 flags
, u64 owner
, u64 offset
,
7639 struct btrfs_key
*ins
, int ref_mod
)
7642 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
7643 struct btrfs_extent_item
*extent_item
;
7644 struct btrfs_extent_inline_ref
*iref
;
7645 struct btrfs_path
*path
;
7646 struct extent_buffer
*leaf
;
7651 type
= BTRFS_SHARED_DATA_REF_KEY
;
7653 type
= BTRFS_EXTENT_DATA_REF_KEY
;
7655 size
= sizeof(*extent_item
) + btrfs_extent_inline_ref_size(type
);
7657 path
= btrfs_alloc_path();
7661 path
->leave_spinning
= 1;
7662 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
7665 btrfs_free_path(path
);
7669 leaf
= path
->nodes
[0];
7670 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
7671 struct btrfs_extent_item
);
7672 btrfs_set_extent_refs(leaf
, extent_item
, ref_mod
);
7673 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
7674 btrfs_set_extent_flags(leaf
, extent_item
,
7675 flags
| BTRFS_EXTENT_FLAG_DATA
);
7677 iref
= (struct btrfs_extent_inline_ref
*)(extent_item
+ 1);
7678 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
7680 struct btrfs_shared_data_ref
*ref
;
7681 ref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
7682 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
7683 btrfs_set_shared_data_ref_count(leaf
, ref
, ref_mod
);
7685 struct btrfs_extent_data_ref
*ref
;
7686 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
7687 btrfs_set_extent_data_ref_root(leaf
, ref
, root_objectid
);
7688 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
7689 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
7690 btrfs_set_extent_data_ref_count(leaf
, ref
, ref_mod
);
7693 btrfs_mark_buffer_dirty(path
->nodes
[0]);
7694 btrfs_free_path(path
);
7696 ret
= remove_from_free_space_tree(trans
, fs_info
, ins
->objectid
,
7701 ret
= update_block_group(trans
, root
, ins
->objectid
, ins
->offset
, 1);
7702 if (ret
) { /* -ENOENT, logic error */
7703 btrfs_err(fs_info
, "update block group failed for %llu %llu",
7704 ins
->objectid
, ins
->offset
);
7707 trace_btrfs_reserved_extent_alloc(root
, ins
->objectid
, ins
->offset
);
7711 static int alloc_reserved_tree_block(struct btrfs_trans_handle
*trans
,
7712 struct btrfs_root
*root
,
7713 u64 parent
, u64 root_objectid
,
7714 u64 flags
, struct btrfs_disk_key
*key
,
7715 int level
, struct btrfs_key
*ins
)
7718 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
7719 struct btrfs_extent_item
*extent_item
;
7720 struct btrfs_tree_block_info
*block_info
;
7721 struct btrfs_extent_inline_ref
*iref
;
7722 struct btrfs_path
*path
;
7723 struct extent_buffer
*leaf
;
7724 u32 size
= sizeof(*extent_item
) + sizeof(*iref
);
7725 u64 num_bytes
= ins
->offset
;
7726 bool skinny_metadata
= btrfs_fs_incompat(root
->fs_info
,
7729 if (!skinny_metadata
)
7730 size
+= sizeof(*block_info
);
7732 path
= btrfs_alloc_path();
7734 btrfs_free_and_pin_reserved_extent(root
, ins
->objectid
,
7739 path
->leave_spinning
= 1;
7740 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
7743 btrfs_free_path(path
);
7744 btrfs_free_and_pin_reserved_extent(root
, ins
->objectid
,
7749 leaf
= path
->nodes
[0];
7750 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
7751 struct btrfs_extent_item
);
7752 btrfs_set_extent_refs(leaf
, extent_item
, 1);
7753 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
7754 btrfs_set_extent_flags(leaf
, extent_item
,
7755 flags
| BTRFS_EXTENT_FLAG_TREE_BLOCK
);
7757 if (skinny_metadata
) {
7758 iref
= (struct btrfs_extent_inline_ref
*)(extent_item
+ 1);
7759 num_bytes
= root
->nodesize
;
7761 block_info
= (struct btrfs_tree_block_info
*)(extent_item
+ 1);
7762 btrfs_set_tree_block_key(leaf
, block_info
, key
);
7763 btrfs_set_tree_block_level(leaf
, block_info
, level
);
7764 iref
= (struct btrfs_extent_inline_ref
*)(block_info
+ 1);
7768 BUG_ON(!(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
));
7769 btrfs_set_extent_inline_ref_type(leaf
, iref
,
7770 BTRFS_SHARED_BLOCK_REF_KEY
);
7771 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
7773 btrfs_set_extent_inline_ref_type(leaf
, iref
,
7774 BTRFS_TREE_BLOCK_REF_KEY
);
7775 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
7778 btrfs_mark_buffer_dirty(leaf
);
7779 btrfs_free_path(path
);
7781 ret
= remove_from_free_space_tree(trans
, fs_info
, ins
->objectid
,
7786 ret
= update_block_group(trans
, root
, ins
->objectid
, root
->nodesize
,
7788 if (ret
) { /* -ENOENT, logic error */
7789 btrfs_err(fs_info
, "update block group failed for %llu %llu",
7790 ins
->objectid
, ins
->offset
);
7794 trace_btrfs_reserved_extent_alloc(root
, ins
->objectid
, root
->nodesize
);
7798 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
7799 struct btrfs_root
*root
,
7800 u64 root_objectid
, u64 owner
,
7801 u64 offset
, u64 ram_bytes
,
7802 struct btrfs_key
*ins
)
7806 BUG_ON(root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
7808 ret
= btrfs_add_delayed_data_ref(root
->fs_info
, trans
, ins
->objectid
,
7810 root_objectid
, owner
, offset
,
7811 ram_bytes
, BTRFS_ADD_DELAYED_EXTENT
,
7817 * this is used by the tree logging recovery code. It records that
7818 * an extent has been allocated and makes sure to clear the free
7819 * space cache bits as well
7821 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle
*trans
,
7822 struct btrfs_root
*root
,
7823 u64 root_objectid
, u64 owner
, u64 offset
,
7824 struct btrfs_key
*ins
)
7827 struct btrfs_block_group_cache
*block_group
;
7830 * Mixed block groups will exclude before processing the log so we only
7831 * need to do the exlude dance if this fs isn't mixed.
7833 if (!btrfs_fs_incompat(root
->fs_info
, MIXED_GROUPS
)) {
7834 ret
= __exclude_logged_extent(root
, ins
->objectid
, ins
->offset
);
7839 block_group
= btrfs_lookup_block_group(root
->fs_info
, ins
->objectid
);
7843 ret
= btrfs_update_reserved_bytes(block_group
, ins
->offset
,
7844 RESERVE_ALLOC_NO_ACCOUNT
, 0);
7845 BUG_ON(ret
); /* logic error */
7846 ret
= alloc_reserved_file_extent(trans
, root
, 0, root_objectid
,
7847 0, owner
, offset
, ins
, 1);
7848 btrfs_put_block_group(block_group
);
7852 static struct extent_buffer
*
7853 btrfs_init_new_buffer(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
7854 u64 bytenr
, int level
)
7856 struct extent_buffer
*buf
;
7858 buf
= btrfs_find_create_tree_block(root
, bytenr
);
7860 return ERR_PTR(-ENOMEM
);
7861 btrfs_set_header_generation(buf
, trans
->transid
);
7862 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, buf
, level
);
7863 btrfs_tree_lock(buf
);
7864 clean_tree_block(trans
, root
->fs_info
, buf
);
7865 clear_bit(EXTENT_BUFFER_STALE
, &buf
->bflags
);
7867 btrfs_set_lock_blocking(buf
);
7868 btrfs_set_buffer_uptodate(buf
);
7870 if (root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
) {
7871 buf
->log_index
= root
->log_transid
% 2;
7873 * we allow two log transactions at a time, use different
7874 * EXENT bit to differentiate dirty pages.
7876 if (buf
->log_index
== 0)
7877 set_extent_dirty(&root
->dirty_log_pages
, buf
->start
,
7878 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
7880 set_extent_new(&root
->dirty_log_pages
, buf
->start
,
7881 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
7883 buf
->log_index
= -1;
7884 set_extent_dirty(&trans
->transaction
->dirty_pages
, buf
->start
,
7885 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
7887 trans
->blocks_used
++;
7888 /* this returns a buffer locked for blocking */
7892 static struct btrfs_block_rsv
*
7893 use_block_rsv(struct btrfs_trans_handle
*trans
,
7894 struct btrfs_root
*root
, u32 blocksize
)
7896 struct btrfs_block_rsv
*block_rsv
;
7897 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
7899 bool global_updated
= false;
7901 block_rsv
= get_block_rsv(trans
, root
);
7903 if (unlikely(block_rsv
->size
== 0))
7906 ret
= block_rsv_use_bytes(block_rsv
, blocksize
);
7910 if (block_rsv
->failfast
)
7911 return ERR_PTR(ret
);
7913 if (block_rsv
->type
== BTRFS_BLOCK_RSV_GLOBAL
&& !global_updated
) {
7914 global_updated
= true;
7915 update_global_block_rsv(root
->fs_info
);
7919 if (btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
7920 static DEFINE_RATELIMIT_STATE(_rs
,
7921 DEFAULT_RATELIMIT_INTERVAL
* 10,
7922 /*DEFAULT_RATELIMIT_BURST*/ 1);
7923 if (__ratelimit(&_rs
))
7925 "BTRFS: block rsv returned %d\n", ret
);
7928 ret
= reserve_metadata_bytes(root
, block_rsv
, blocksize
,
7929 BTRFS_RESERVE_NO_FLUSH
);
7933 * If we couldn't reserve metadata bytes try and use some from
7934 * the global reserve if its space type is the same as the global
7937 if (block_rsv
->type
!= BTRFS_BLOCK_RSV_GLOBAL
&&
7938 block_rsv
->space_info
== global_rsv
->space_info
) {
7939 ret
= block_rsv_use_bytes(global_rsv
, blocksize
);
7943 return ERR_PTR(ret
);
7946 static void unuse_block_rsv(struct btrfs_fs_info
*fs_info
,
7947 struct btrfs_block_rsv
*block_rsv
, u32 blocksize
)
7949 block_rsv_add_bytes(block_rsv
, blocksize
, 0);
7950 block_rsv_release_bytes(fs_info
, block_rsv
, NULL
, 0);
7954 * finds a free extent and does all the dirty work required for allocation
7955 * returns the tree buffer or an ERR_PTR on error.
7957 struct extent_buffer
*btrfs_alloc_tree_block(struct btrfs_trans_handle
*trans
,
7958 struct btrfs_root
*root
,
7959 u64 parent
, u64 root_objectid
,
7960 struct btrfs_disk_key
*key
, int level
,
7961 u64 hint
, u64 empty_size
)
7963 struct btrfs_key ins
;
7964 struct btrfs_block_rsv
*block_rsv
;
7965 struct extent_buffer
*buf
;
7966 struct btrfs_delayed_extent_op
*extent_op
;
7969 u32 blocksize
= root
->nodesize
;
7970 bool skinny_metadata
= btrfs_fs_incompat(root
->fs_info
,
7973 if (btrfs_test_is_dummy_root(root
)) {
7974 buf
= btrfs_init_new_buffer(trans
, root
, root
->alloc_bytenr
,
7977 root
->alloc_bytenr
+= blocksize
;
7981 block_rsv
= use_block_rsv(trans
, root
, blocksize
);
7982 if (IS_ERR(block_rsv
))
7983 return ERR_CAST(block_rsv
);
7985 ret
= btrfs_reserve_extent(root
, blocksize
, blocksize
,
7986 empty_size
, hint
, &ins
, 0, 0);
7990 buf
= btrfs_init_new_buffer(trans
, root
, ins
.objectid
, level
);
7993 goto out_free_reserved
;
7996 if (root_objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
7998 parent
= ins
.objectid
;
7999 flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
8003 if (root_objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
8004 extent_op
= btrfs_alloc_delayed_extent_op();
8010 memcpy(&extent_op
->key
, key
, sizeof(extent_op
->key
));
8012 memset(&extent_op
->key
, 0, sizeof(extent_op
->key
));
8013 extent_op
->flags_to_set
= flags
;
8014 if (skinny_metadata
)
8015 extent_op
->update_key
= 0;
8017 extent_op
->update_key
= 1;
8018 extent_op
->update_flags
= 1;
8019 extent_op
->is_data
= 0;
8020 extent_op
->level
= level
;
8022 ret
= btrfs_add_delayed_tree_ref(root
->fs_info
, trans
,
8023 ins
.objectid
, ins
.offset
,
8024 parent
, root_objectid
, level
,
8025 BTRFS_ADD_DELAYED_EXTENT
,
8028 goto out_free_delayed
;
8033 btrfs_free_delayed_extent_op(extent_op
);
8035 free_extent_buffer(buf
);
8037 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
, 0);
8039 unuse_block_rsv(root
->fs_info
, block_rsv
, blocksize
);
8040 return ERR_PTR(ret
);
8043 struct walk_control
{
8044 u64 refs
[BTRFS_MAX_LEVEL
];
8045 u64 flags
[BTRFS_MAX_LEVEL
];
8046 struct btrfs_key update_progress
;
8057 #define DROP_REFERENCE 1
8058 #define UPDATE_BACKREF 2
8060 static noinline
void reada_walk_down(struct btrfs_trans_handle
*trans
,
8061 struct btrfs_root
*root
,
8062 struct walk_control
*wc
,
8063 struct btrfs_path
*path
)
8071 struct btrfs_key key
;
8072 struct extent_buffer
*eb
;
8077 if (path
->slots
[wc
->level
] < wc
->reada_slot
) {
8078 wc
->reada_count
= wc
->reada_count
* 2 / 3;
8079 wc
->reada_count
= max(wc
->reada_count
, 2);
8081 wc
->reada_count
= wc
->reada_count
* 3 / 2;
8082 wc
->reada_count
= min_t(int, wc
->reada_count
,
8083 BTRFS_NODEPTRS_PER_BLOCK(root
));
8086 eb
= path
->nodes
[wc
->level
];
8087 nritems
= btrfs_header_nritems(eb
);
8088 blocksize
= root
->nodesize
;
8090 for (slot
= path
->slots
[wc
->level
]; slot
< nritems
; slot
++) {
8091 if (nread
>= wc
->reada_count
)
8095 bytenr
= btrfs_node_blockptr(eb
, slot
);
8096 generation
= btrfs_node_ptr_generation(eb
, slot
);
8098 if (slot
== path
->slots
[wc
->level
])
8101 if (wc
->stage
== UPDATE_BACKREF
&&
8102 generation
<= root
->root_key
.offset
)
8105 /* We don't lock the tree block, it's OK to be racy here */
8106 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
,
8107 wc
->level
- 1, 1, &refs
,
8109 /* We don't care about errors in readahead. */
8114 if (wc
->stage
== DROP_REFERENCE
) {
8118 if (wc
->level
== 1 &&
8119 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
8121 if (!wc
->update_ref
||
8122 generation
<= root
->root_key
.offset
)
8124 btrfs_node_key_to_cpu(eb
, &key
, slot
);
8125 ret
= btrfs_comp_cpu_keys(&key
,
8126 &wc
->update_progress
);
8130 if (wc
->level
== 1 &&
8131 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
8135 readahead_tree_block(root
, bytenr
);
8138 wc
->reada_slot
= slot
;
8142 * These may not be seen by the usual inc/dec ref code so we have to
8145 static int record_one_subtree_extent(struct btrfs_trans_handle
*trans
,
8146 struct btrfs_root
*root
, u64 bytenr
,
8149 struct btrfs_qgroup_extent_record
*qrecord
;
8150 struct btrfs_delayed_ref_root
*delayed_refs
;
8152 qrecord
= kmalloc(sizeof(*qrecord
), GFP_NOFS
);
8156 qrecord
->bytenr
= bytenr
;
8157 qrecord
->num_bytes
= num_bytes
;
8158 qrecord
->old_roots
= NULL
;
8160 delayed_refs
= &trans
->transaction
->delayed_refs
;
8161 spin_lock(&delayed_refs
->lock
);
8162 if (btrfs_qgroup_insert_dirty_extent(delayed_refs
, qrecord
))
8164 spin_unlock(&delayed_refs
->lock
);
8169 static int account_leaf_items(struct btrfs_trans_handle
*trans
,
8170 struct btrfs_root
*root
,
8171 struct extent_buffer
*eb
)
8173 int nr
= btrfs_header_nritems(eb
);
8174 int i
, extent_type
, ret
;
8175 struct btrfs_key key
;
8176 struct btrfs_file_extent_item
*fi
;
8177 u64 bytenr
, num_bytes
;
8179 /* We can be called directly from walk_up_proc() */
8180 if (!root
->fs_info
->quota_enabled
)
8183 for (i
= 0; i
< nr
; i
++) {
8184 btrfs_item_key_to_cpu(eb
, &key
, i
);
8186 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
8189 fi
= btrfs_item_ptr(eb
, i
, struct btrfs_file_extent_item
);
8190 /* filter out non qgroup-accountable extents */
8191 extent_type
= btrfs_file_extent_type(eb
, fi
);
8193 if (extent_type
== BTRFS_FILE_EXTENT_INLINE
)
8196 bytenr
= btrfs_file_extent_disk_bytenr(eb
, fi
);
8200 num_bytes
= btrfs_file_extent_disk_num_bytes(eb
, fi
);
8202 ret
= record_one_subtree_extent(trans
, root
, bytenr
, num_bytes
);
8210 * Walk up the tree from the bottom, freeing leaves and any interior
8211 * nodes which have had all slots visited. If a node (leaf or
8212 * interior) is freed, the node above it will have it's slot
8213 * incremented. The root node will never be freed.
8215 * At the end of this function, we should have a path which has all
8216 * slots incremented to the next position for a search. If we need to
8217 * read a new node it will be NULL and the node above it will have the
8218 * correct slot selected for a later read.
8220 * If we increment the root nodes slot counter past the number of
8221 * elements, 1 is returned to signal completion of the search.
8223 static int adjust_slots_upwards(struct btrfs_root
*root
,
8224 struct btrfs_path
*path
, int root_level
)
8228 struct extent_buffer
*eb
;
8230 if (root_level
== 0)
8233 while (level
<= root_level
) {
8234 eb
= path
->nodes
[level
];
8235 nr
= btrfs_header_nritems(eb
);
8236 path
->slots
[level
]++;
8237 slot
= path
->slots
[level
];
8238 if (slot
>= nr
|| level
== 0) {
8240 * Don't free the root - we will detect this
8241 * condition after our loop and return a
8242 * positive value for caller to stop walking the tree.
8244 if (level
!= root_level
) {
8245 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
8246 path
->locks
[level
] = 0;
8248 free_extent_buffer(eb
);
8249 path
->nodes
[level
] = NULL
;
8250 path
->slots
[level
] = 0;
8254 * We have a valid slot to walk back down
8255 * from. Stop here so caller can process these
8264 eb
= path
->nodes
[root_level
];
8265 if (path
->slots
[root_level
] >= btrfs_header_nritems(eb
))
8272 * root_eb is the subtree root and is locked before this function is called.
8274 static int account_shared_subtree(struct btrfs_trans_handle
*trans
,
8275 struct btrfs_root
*root
,
8276 struct extent_buffer
*root_eb
,
8282 struct extent_buffer
*eb
= root_eb
;
8283 struct btrfs_path
*path
= NULL
;
8285 BUG_ON(root_level
< 0 || root_level
> BTRFS_MAX_LEVEL
);
8286 BUG_ON(root_eb
== NULL
);
8288 if (!root
->fs_info
->quota_enabled
)
8291 if (!extent_buffer_uptodate(root_eb
)) {
8292 ret
= btrfs_read_buffer(root_eb
, root_gen
);
8297 if (root_level
== 0) {
8298 ret
= account_leaf_items(trans
, root
, root_eb
);
8302 path
= btrfs_alloc_path();
8307 * Walk down the tree. Missing extent blocks are filled in as
8308 * we go. Metadata is accounted every time we read a new
8311 * When we reach a leaf, we account for file extent items in it,
8312 * walk back up the tree (adjusting slot pointers as we go)
8313 * and restart the search process.
8315 extent_buffer_get(root_eb
); /* For path */
8316 path
->nodes
[root_level
] = root_eb
;
8317 path
->slots
[root_level
] = 0;
8318 path
->locks
[root_level
] = 0; /* so release_path doesn't try to unlock */
8321 while (level
>= 0) {
8322 if (path
->nodes
[level
] == NULL
) {
8327 /* We need to get child blockptr/gen from
8328 * parent before we can read it. */
8329 eb
= path
->nodes
[level
+ 1];
8330 parent_slot
= path
->slots
[level
+ 1];
8331 child_bytenr
= btrfs_node_blockptr(eb
, parent_slot
);
8332 child_gen
= btrfs_node_ptr_generation(eb
, parent_slot
);
8334 eb
= read_tree_block(root
, child_bytenr
, child_gen
);
8338 } else if (!extent_buffer_uptodate(eb
)) {
8339 free_extent_buffer(eb
);
8344 path
->nodes
[level
] = eb
;
8345 path
->slots
[level
] = 0;
8347 btrfs_tree_read_lock(eb
);
8348 btrfs_set_lock_blocking_rw(eb
, BTRFS_READ_LOCK
);
8349 path
->locks
[level
] = BTRFS_READ_LOCK_BLOCKING
;
8351 ret
= record_one_subtree_extent(trans
, root
, child_bytenr
,
8358 ret
= account_leaf_items(trans
, root
, path
->nodes
[level
]);
8362 /* Nonzero return here means we completed our search */
8363 ret
= adjust_slots_upwards(root
, path
, root_level
);
8367 /* Restart search with new slots */
8376 btrfs_free_path(path
);
8382 * helper to process tree block while walking down the tree.
8384 * when wc->stage == UPDATE_BACKREF, this function updates
8385 * back refs for pointers in the block.
8387 * NOTE: return value 1 means we should stop walking down.
8389 static noinline
int walk_down_proc(struct btrfs_trans_handle
*trans
,
8390 struct btrfs_root
*root
,
8391 struct btrfs_path
*path
,
8392 struct walk_control
*wc
, int lookup_info
)
8394 int level
= wc
->level
;
8395 struct extent_buffer
*eb
= path
->nodes
[level
];
8396 u64 flag
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
8399 if (wc
->stage
== UPDATE_BACKREF
&&
8400 btrfs_header_owner(eb
) != root
->root_key
.objectid
)
8404 * when reference count of tree block is 1, it won't increase
8405 * again. once full backref flag is set, we never clear it.
8408 ((wc
->stage
== DROP_REFERENCE
&& wc
->refs
[level
] != 1) ||
8409 (wc
->stage
== UPDATE_BACKREF
&& !(wc
->flags
[level
] & flag
)))) {
8410 BUG_ON(!path
->locks
[level
]);
8411 ret
= btrfs_lookup_extent_info(trans
, root
,
8412 eb
->start
, level
, 1,
8415 BUG_ON(ret
== -ENOMEM
);
8418 BUG_ON(wc
->refs
[level
] == 0);
8421 if (wc
->stage
== DROP_REFERENCE
) {
8422 if (wc
->refs
[level
] > 1)
8425 if (path
->locks
[level
] && !wc
->keep_locks
) {
8426 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
8427 path
->locks
[level
] = 0;
8432 /* wc->stage == UPDATE_BACKREF */
8433 if (!(wc
->flags
[level
] & flag
)) {
8434 BUG_ON(!path
->locks
[level
]);
8435 ret
= btrfs_inc_ref(trans
, root
, eb
, 1);
8436 BUG_ON(ret
); /* -ENOMEM */
8437 ret
= btrfs_dec_ref(trans
, root
, eb
, 0);
8438 BUG_ON(ret
); /* -ENOMEM */
8439 ret
= btrfs_set_disk_extent_flags(trans
, root
, eb
->start
,
8441 btrfs_header_level(eb
), 0);
8442 BUG_ON(ret
); /* -ENOMEM */
8443 wc
->flags
[level
] |= flag
;
8447 * the block is shared by multiple trees, so it's not good to
8448 * keep the tree lock
8450 if (path
->locks
[level
] && level
> 0) {
8451 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
8452 path
->locks
[level
] = 0;
8458 * helper to process tree block pointer.
8460 * when wc->stage == DROP_REFERENCE, this function checks
8461 * reference count of the block pointed to. if the block
8462 * is shared and we need update back refs for the subtree
8463 * rooted at the block, this function changes wc->stage to
8464 * UPDATE_BACKREF. if the block is shared and there is no
8465 * need to update back, this function drops the reference
8468 * NOTE: return value 1 means we should stop walking down.
8470 static noinline
int do_walk_down(struct btrfs_trans_handle
*trans
,
8471 struct btrfs_root
*root
,
8472 struct btrfs_path
*path
,
8473 struct walk_control
*wc
, int *lookup_info
)
8479 struct btrfs_key key
;
8480 struct extent_buffer
*next
;
8481 int level
= wc
->level
;
8484 bool need_account
= false;
8486 generation
= btrfs_node_ptr_generation(path
->nodes
[level
],
8487 path
->slots
[level
]);
8489 * if the lower level block was created before the snapshot
8490 * was created, we know there is no need to update back refs
8493 if (wc
->stage
== UPDATE_BACKREF
&&
8494 generation
<= root
->root_key
.offset
) {
8499 bytenr
= btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]);
8500 blocksize
= root
->nodesize
;
8502 next
= btrfs_find_tree_block(root
->fs_info
, bytenr
);
8504 next
= btrfs_find_create_tree_block(root
, bytenr
);
8507 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, next
,
8511 btrfs_tree_lock(next
);
8512 btrfs_set_lock_blocking(next
);
8514 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
, level
- 1, 1,
8515 &wc
->refs
[level
- 1],
8516 &wc
->flags
[level
- 1]);
8518 btrfs_tree_unlock(next
);
8522 if (unlikely(wc
->refs
[level
- 1] == 0)) {
8523 btrfs_err(root
->fs_info
, "Missing references.");
8528 if (wc
->stage
== DROP_REFERENCE
) {
8529 if (wc
->refs
[level
- 1] > 1) {
8530 need_account
= true;
8532 (wc
->flags
[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF
))
8535 if (!wc
->update_ref
||
8536 generation
<= root
->root_key
.offset
)
8539 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
8540 path
->slots
[level
]);
8541 ret
= btrfs_comp_cpu_keys(&key
, &wc
->update_progress
);
8545 wc
->stage
= UPDATE_BACKREF
;
8546 wc
->shared_level
= level
- 1;
8550 (wc
->flags
[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF
))
8554 if (!btrfs_buffer_uptodate(next
, generation
, 0)) {
8555 btrfs_tree_unlock(next
);
8556 free_extent_buffer(next
);
8562 if (reada
&& level
== 1)
8563 reada_walk_down(trans
, root
, wc
, path
);
8564 next
= read_tree_block(root
, bytenr
, generation
);
8566 return PTR_ERR(next
);
8567 } else if (!extent_buffer_uptodate(next
)) {
8568 free_extent_buffer(next
);
8571 btrfs_tree_lock(next
);
8572 btrfs_set_lock_blocking(next
);
8576 BUG_ON(level
!= btrfs_header_level(next
));
8577 path
->nodes
[level
] = next
;
8578 path
->slots
[level
] = 0;
8579 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
8585 wc
->refs
[level
- 1] = 0;
8586 wc
->flags
[level
- 1] = 0;
8587 if (wc
->stage
== DROP_REFERENCE
) {
8588 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
) {
8589 parent
= path
->nodes
[level
]->start
;
8591 BUG_ON(root
->root_key
.objectid
!=
8592 btrfs_header_owner(path
->nodes
[level
]));
8597 ret
= account_shared_subtree(trans
, root
, next
,
8598 generation
, level
- 1);
8600 btrfs_err_rl(root
->fs_info
,
8602 "%d accounting shared subtree. Quota "
8603 "is out of sync, rescan required.",
8607 ret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
, parent
,
8608 root
->root_key
.objectid
, level
- 1, 0);
8609 BUG_ON(ret
); /* -ENOMEM */
8611 btrfs_tree_unlock(next
);
8612 free_extent_buffer(next
);
8618 * helper to process tree block while walking up the tree.
8620 * when wc->stage == DROP_REFERENCE, this function drops
8621 * reference count on the block.
8623 * when wc->stage == UPDATE_BACKREF, this function changes
8624 * wc->stage back to DROP_REFERENCE if we changed wc->stage
8625 * to UPDATE_BACKREF previously while processing the block.
8627 * NOTE: return value 1 means we should stop walking up.
8629 static noinline
int walk_up_proc(struct btrfs_trans_handle
*trans
,
8630 struct btrfs_root
*root
,
8631 struct btrfs_path
*path
,
8632 struct walk_control
*wc
)
8635 int level
= wc
->level
;
8636 struct extent_buffer
*eb
= path
->nodes
[level
];
8639 if (wc
->stage
== UPDATE_BACKREF
) {
8640 BUG_ON(wc
->shared_level
< level
);
8641 if (level
< wc
->shared_level
)
8644 ret
= find_next_key(path
, level
+ 1, &wc
->update_progress
);
8648 wc
->stage
= DROP_REFERENCE
;
8649 wc
->shared_level
= -1;
8650 path
->slots
[level
] = 0;
8653 * check reference count again if the block isn't locked.
8654 * we should start walking down the tree again if reference
8657 if (!path
->locks
[level
]) {
8659 btrfs_tree_lock(eb
);
8660 btrfs_set_lock_blocking(eb
);
8661 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
8663 ret
= btrfs_lookup_extent_info(trans
, root
,
8664 eb
->start
, level
, 1,
8668 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
8669 path
->locks
[level
] = 0;
8672 BUG_ON(wc
->refs
[level
] == 0);
8673 if (wc
->refs
[level
] == 1) {
8674 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
8675 path
->locks
[level
] = 0;
8681 /* wc->stage == DROP_REFERENCE */
8682 BUG_ON(wc
->refs
[level
] > 1 && !path
->locks
[level
]);
8684 if (wc
->refs
[level
] == 1) {
8686 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
8687 ret
= btrfs_dec_ref(trans
, root
, eb
, 1);
8689 ret
= btrfs_dec_ref(trans
, root
, eb
, 0);
8690 BUG_ON(ret
); /* -ENOMEM */
8691 ret
= account_leaf_items(trans
, root
, eb
);
8693 btrfs_err_rl(root
->fs_info
,
8695 "%d accounting leaf items. Quota "
8696 "is out of sync, rescan required.",
8700 /* make block locked assertion in clean_tree_block happy */
8701 if (!path
->locks
[level
] &&
8702 btrfs_header_generation(eb
) == trans
->transid
) {
8703 btrfs_tree_lock(eb
);
8704 btrfs_set_lock_blocking(eb
);
8705 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
8707 clean_tree_block(trans
, root
->fs_info
, eb
);
8710 if (eb
== root
->node
) {
8711 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
8714 BUG_ON(root
->root_key
.objectid
!=
8715 btrfs_header_owner(eb
));
8717 if (wc
->flags
[level
+ 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
8718 parent
= path
->nodes
[level
+ 1]->start
;
8720 BUG_ON(root
->root_key
.objectid
!=
8721 btrfs_header_owner(path
->nodes
[level
+ 1]));
8724 btrfs_free_tree_block(trans
, root
, eb
, parent
, wc
->refs
[level
] == 1);
8726 wc
->refs
[level
] = 0;
8727 wc
->flags
[level
] = 0;
8731 static noinline
int walk_down_tree(struct btrfs_trans_handle
*trans
,
8732 struct btrfs_root
*root
,
8733 struct btrfs_path
*path
,
8734 struct walk_control
*wc
)
8736 int level
= wc
->level
;
8737 int lookup_info
= 1;
8740 while (level
>= 0) {
8741 ret
= walk_down_proc(trans
, root
, path
, wc
, lookup_info
);
8748 if (path
->slots
[level
] >=
8749 btrfs_header_nritems(path
->nodes
[level
]))
8752 ret
= do_walk_down(trans
, root
, path
, wc
, &lookup_info
);
8754 path
->slots
[level
]++;
8763 static noinline
int walk_up_tree(struct btrfs_trans_handle
*trans
,
8764 struct btrfs_root
*root
,
8765 struct btrfs_path
*path
,
8766 struct walk_control
*wc
, int max_level
)
8768 int level
= wc
->level
;
8771 path
->slots
[level
] = btrfs_header_nritems(path
->nodes
[level
]);
8772 while (level
< max_level
&& path
->nodes
[level
]) {
8774 if (path
->slots
[level
] + 1 <
8775 btrfs_header_nritems(path
->nodes
[level
])) {
8776 path
->slots
[level
]++;
8779 ret
= walk_up_proc(trans
, root
, path
, wc
);
8783 if (path
->locks
[level
]) {
8784 btrfs_tree_unlock_rw(path
->nodes
[level
],
8785 path
->locks
[level
]);
8786 path
->locks
[level
] = 0;
8788 free_extent_buffer(path
->nodes
[level
]);
8789 path
->nodes
[level
] = NULL
;
8797 * drop a subvolume tree.
8799 * this function traverses the tree freeing any blocks that only
8800 * referenced by the tree.
8802 * when a shared tree block is found. this function decreases its
8803 * reference count by one. if update_ref is true, this function
8804 * also make sure backrefs for the shared block and all lower level
8805 * blocks are properly updated.
8807 * If called with for_reloc == 0, may exit early with -EAGAIN
8809 int btrfs_drop_snapshot(struct btrfs_root
*root
,
8810 struct btrfs_block_rsv
*block_rsv
, int update_ref
,
8813 struct btrfs_path
*path
;
8814 struct btrfs_trans_handle
*trans
;
8815 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
8816 struct btrfs_root_item
*root_item
= &root
->root_item
;
8817 struct walk_control
*wc
;
8818 struct btrfs_key key
;
8822 bool root_dropped
= false;
8824 btrfs_debug(root
->fs_info
, "Drop subvolume %llu", root
->objectid
);
8826 path
= btrfs_alloc_path();
8832 wc
= kzalloc(sizeof(*wc
), GFP_NOFS
);
8834 btrfs_free_path(path
);
8839 trans
= btrfs_start_transaction(tree_root
, 0);
8840 if (IS_ERR(trans
)) {
8841 err
= PTR_ERR(trans
);
8846 trans
->block_rsv
= block_rsv
;
8848 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
8849 level
= btrfs_header_level(root
->node
);
8850 path
->nodes
[level
] = btrfs_lock_root_node(root
);
8851 btrfs_set_lock_blocking(path
->nodes
[level
]);
8852 path
->slots
[level
] = 0;
8853 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
8854 memset(&wc
->update_progress
, 0,
8855 sizeof(wc
->update_progress
));
8857 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
8858 memcpy(&wc
->update_progress
, &key
,
8859 sizeof(wc
->update_progress
));
8861 level
= root_item
->drop_level
;
8863 path
->lowest_level
= level
;
8864 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
8865 path
->lowest_level
= 0;
8873 * unlock our path, this is safe because only this
8874 * function is allowed to delete this snapshot
8876 btrfs_unlock_up_safe(path
, 0);
8878 level
= btrfs_header_level(root
->node
);
8880 btrfs_tree_lock(path
->nodes
[level
]);
8881 btrfs_set_lock_blocking(path
->nodes
[level
]);
8882 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
8884 ret
= btrfs_lookup_extent_info(trans
, root
,
8885 path
->nodes
[level
]->start
,
8886 level
, 1, &wc
->refs
[level
],
8892 BUG_ON(wc
->refs
[level
] == 0);
8894 if (level
== root_item
->drop_level
)
8897 btrfs_tree_unlock(path
->nodes
[level
]);
8898 path
->locks
[level
] = 0;
8899 WARN_ON(wc
->refs
[level
] != 1);
8905 wc
->shared_level
= -1;
8906 wc
->stage
= DROP_REFERENCE
;
8907 wc
->update_ref
= update_ref
;
8909 wc
->for_reloc
= for_reloc
;
8910 wc
->reada_count
= BTRFS_NODEPTRS_PER_BLOCK(root
);
8914 ret
= walk_down_tree(trans
, root
, path
, wc
);
8920 ret
= walk_up_tree(trans
, root
, path
, wc
, BTRFS_MAX_LEVEL
);
8927 BUG_ON(wc
->stage
!= DROP_REFERENCE
);
8931 if (wc
->stage
== DROP_REFERENCE
) {
8933 btrfs_node_key(path
->nodes
[level
],
8934 &root_item
->drop_progress
,
8935 path
->slots
[level
]);
8936 root_item
->drop_level
= level
;
8939 BUG_ON(wc
->level
== 0);
8940 if (btrfs_should_end_transaction(trans
, tree_root
) ||
8941 (!for_reloc
&& btrfs_need_cleaner_sleep(root
))) {
8942 ret
= btrfs_update_root(trans
, tree_root
,
8946 btrfs_abort_transaction(trans
, tree_root
, ret
);
8951 btrfs_end_transaction_throttle(trans
, tree_root
);
8952 if (!for_reloc
&& btrfs_need_cleaner_sleep(root
)) {
8953 pr_debug("BTRFS: drop snapshot early exit\n");
8958 trans
= btrfs_start_transaction(tree_root
, 0);
8959 if (IS_ERR(trans
)) {
8960 err
= PTR_ERR(trans
);
8964 trans
->block_rsv
= block_rsv
;
8967 btrfs_release_path(path
);
8971 ret
= btrfs_del_root(trans
, tree_root
, &root
->root_key
);
8973 btrfs_abort_transaction(trans
, tree_root
, ret
);
8977 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
8978 ret
= btrfs_find_root(tree_root
, &root
->root_key
, path
,
8981 btrfs_abort_transaction(trans
, tree_root
, ret
);
8984 } else if (ret
> 0) {
8985 /* if we fail to delete the orphan item this time
8986 * around, it'll get picked up the next time.
8988 * The most common failure here is just -ENOENT.
8990 btrfs_del_orphan_item(trans
, tree_root
,
8991 root
->root_key
.objectid
);
8995 if (test_bit(BTRFS_ROOT_IN_RADIX
, &root
->state
)) {
8996 btrfs_add_dropped_root(trans
, root
);
8998 free_extent_buffer(root
->node
);
8999 free_extent_buffer(root
->commit_root
);
9000 btrfs_put_fs_root(root
);
9002 root_dropped
= true;
9004 btrfs_end_transaction_throttle(trans
, tree_root
);
9007 btrfs_free_path(path
);
9010 * So if we need to stop dropping the snapshot for whatever reason we
9011 * need to make sure to add it back to the dead root list so that we
9012 * keep trying to do the work later. This also cleans up roots if we
9013 * don't have it in the radix (like when we recover after a power fail
9014 * or unmount) so we don't leak memory.
9016 if (!for_reloc
&& root_dropped
== false)
9017 btrfs_add_dead_root(root
);
9018 if (err
&& err
!= -EAGAIN
)
9019 btrfs_std_error(root
->fs_info
, err
, NULL
);
9024 * drop subtree rooted at tree block 'node'.
9026 * NOTE: this function will unlock and release tree block 'node'
9027 * only used by relocation code
9029 int btrfs_drop_subtree(struct btrfs_trans_handle
*trans
,
9030 struct btrfs_root
*root
,
9031 struct extent_buffer
*node
,
9032 struct extent_buffer
*parent
)
9034 struct btrfs_path
*path
;
9035 struct walk_control
*wc
;
9041 BUG_ON(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
9043 path
= btrfs_alloc_path();
9047 wc
= kzalloc(sizeof(*wc
), GFP_NOFS
);
9049 btrfs_free_path(path
);
9053 btrfs_assert_tree_locked(parent
);
9054 parent_level
= btrfs_header_level(parent
);
9055 extent_buffer_get(parent
);
9056 path
->nodes
[parent_level
] = parent
;
9057 path
->slots
[parent_level
] = btrfs_header_nritems(parent
);
9059 btrfs_assert_tree_locked(node
);
9060 level
= btrfs_header_level(node
);
9061 path
->nodes
[level
] = node
;
9062 path
->slots
[level
] = 0;
9063 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
9065 wc
->refs
[parent_level
] = 1;
9066 wc
->flags
[parent_level
] = BTRFS_BLOCK_FLAG_FULL_BACKREF
;
9068 wc
->shared_level
= -1;
9069 wc
->stage
= DROP_REFERENCE
;
9073 wc
->reada_count
= BTRFS_NODEPTRS_PER_BLOCK(root
);
9076 wret
= walk_down_tree(trans
, root
, path
, wc
);
9082 wret
= walk_up_tree(trans
, root
, path
, wc
, parent_level
);
9090 btrfs_free_path(path
);
9094 static u64
update_block_group_flags(struct btrfs_root
*root
, u64 flags
)
9100 * if restripe for this chunk_type is on pick target profile and
9101 * return, otherwise do the usual balance
9103 stripped
= get_restripe_target(root
->fs_info
, flags
);
9105 return extended_to_chunk(stripped
);
9107 num_devices
= root
->fs_info
->fs_devices
->rw_devices
;
9109 stripped
= BTRFS_BLOCK_GROUP_RAID0
|
9110 BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
|
9111 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
;
9113 if (num_devices
== 1) {
9114 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
9115 stripped
= flags
& ~stripped
;
9117 /* turn raid0 into single device chunks */
9118 if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
9121 /* turn mirroring into duplication */
9122 if (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
9123 BTRFS_BLOCK_GROUP_RAID10
))
9124 return stripped
| BTRFS_BLOCK_GROUP_DUP
;
9126 /* they already had raid on here, just return */
9127 if (flags
& stripped
)
9130 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
9131 stripped
= flags
& ~stripped
;
9133 /* switch duplicated blocks with raid1 */
9134 if (flags
& BTRFS_BLOCK_GROUP_DUP
)
9135 return stripped
| BTRFS_BLOCK_GROUP_RAID1
;
9137 /* this is drive concat, leave it alone */
9143 static int inc_block_group_ro(struct btrfs_block_group_cache
*cache
, int force
)
9145 struct btrfs_space_info
*sinfo
= cache
->space_info
;
9147 u64 min_allocable_bytes
;
9151 * We need some metadata space and system metadata space for
9152 * allocating chunks in some corner cases until we force to set
9153 * it to be readonly.
9156 (BTRFS_BLOCK_GROUP_SYSTEM
| BTRFS_BLOCK_GROUP_METADATA
)) &&
9158 min_allocable_bytes
= 1 * 1024 * 1024;
9160 min_allocable_bytes
= 0;
9162 spin_lock(&sinfo
->lock
);
9163 spin_lock(&cache
->lock
);
9171 num_bytes
= cache
->key
.offset
- cache
->reserved
- cache
->pinned
-
9172 cache
->bytes_super
- btrfs_block_group_used(&cache
->item
);
9174 if (sinfo
->bytes_used
+ sinfo
->bytes_reserved
+ sinfo
->bytes_pinned
+
9175 sinfo
->bytes_may_use
+ sinfo
->bytes_readonly
+ num_bytes
+
9176 min_allocable_bytes
<= sinfo
->total_bytes
) {
9177 sinfo
->bytes_readonly
+= num_bytes
;
9179 list_add_tail(&cache
->ro_list
, &sinfo
->ro_bgs
);
9183 spin_unlock(&cache
->lock
);
9184 spin_unlock(&sinfo
->lock
);
9188 int btrfs_inc_block_group_ro(struct btrfs_root
*root
,
9189 struct btrfs_block_group_cache
*cache
)
9192 struct btrfs_trans_handle
*trans
;
9197 trans
= btrfs_join_transaction(root
);
9199 return PTR_ERR(trans
);
9202 * we're not allowed to set block groups readonly after the dirty
9203 * block groups cache has started writing. If it already started,
9204 * back off and let this transaction commit
9206 mutex_lock(&root
->fs_info
->ro_block_group_mutex
);
9207 if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN
, &trans
->transaction
->flags
)) {
9208 u64 transid
= trans
->transid
;
9210 mutex_unlock(&root
->fs_info
->ro_block_group_mutex
);
9211 btrfs_end_transaction(trans
, root
);
9213 ret
= btrfs_wait_for_commit(root
, transid
);
9220 * if we are changing raid levels, try to allocate a corresponding
9221 * block group with the new raid level.
9223 alloc_flags
= update_block_group_flags(root
, cache
->flags
);
9224 if (alloc_flags
!= cache
->flags
) {
9225 ret
= do_chunk_alloc(trans
, root
, alloc_flags
,
9228 * ENOSPC is allowed here, we may have enough space
9229 * already allocated at the new raid level to
9238 ret
= inc_block_group_ro(cache
, 0);
9241 alloc_flags
= get_alloc_profile(root
, cache
->space_info
->flags
);
9242 ret
= do_chunk_alloc(trans
, root
, alloc_flags
,
9246 ret
= inc_block_group_ro(cache
, 0);
9248 if (cache
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
) {
9249 alloc_flags
= update_block_group_flags(root
, cache
->flags
);
9250 lock_chunks(root
->fs_info
->chunk_root
);
9251 check_system_chunk(trans
, root
, alloc_flags
);
9252 unlock_chunks(root
->fs_info
->chunk_root
);
9254 mutex_unlock(&root
->fs_info
->ro_block_group_mutex
);
9256 btrfs_end_transaction(trans
, root
);
9260 int btrfs_force_chunk_alloc(struct btrfs_trans_handle
*trans
,
9261 struct btrfs_root
*root
, u64 type
)
9263 u64 alloc_flags
= get_alloc_profile(root
, type
);
9264 return do_chunk_alloc(trans
, root
, alloc_flags
,
9269 * helper to account the unused space of all the readonly block group in the
9270 * space_info. takes mirrors into account.
9272 u64
btrfs_account_ro_block_groups_free_space(struct btrfs_space_info
*sinfo
)
9274 struct btrfs_block_group_cache
*block_group
;
9278 /* It's df, we don't care if it's racey */
9279 if (list_empty(&sinfo
->ro_bgs
))
9282 spin_lock(&sinfo
->lock
);
9283 list_for_each_entry(block_group
, &sinfo
->ro_bgs
, ro_list
) {
9284 spin_lock(&block_group
->lock
);
9286 if (!block_group
->ro
) {
9287 spin_unlock(&block_group
->lock
);
9291 if (block_group
->flags
& (BTRFS_BLOCK_GROUP_RAID1
|
9292 BTRFS_BLOCK_GROUP_RAID10
|
9293 BTRFS_BLOCK_GROUP_DUP
))
9298 free_bytes
+= (block_group
->key
.offset
-
9299 btrfs_block_group_used(&block_group
->item
)) *
9302 spin_unlock(&block_group
->lock
);
9304 spin_unlock(&sinfo
->lock
);
9309 void btrfs_dec_block_group_ro(struct btrfs_root
*root
,
9310 struct btrfs_block_group_cache
*cache
)
9312 struct btrfs_space_info
*sinfo
= cache
->space_info
;
9317 spin_lock(&sinfo
->lock
);
9318 spin_lock(&cache
->lock
);
9320 num_bytes
= cache
->key
.offset
- cache
->reserved
-
9321 cache
->pinned
- cache
->bytes_super
-
9322 btrfs_block_group_used(&cache
->item
);
9323 sinfo
->bytes_readonly
-= num_bytes
;
9324 list_del_init(&cache
->ro_list
);
9326 spin_unlock(&cache
->lock
);
9327 spin_unlock(&sinfo
->lock
);
9331 * checks to see if its even possible to relocate this block group.
9333 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
9334 * ok to go ahead and try.
9336 int btrfs_can_relocate(struct btrfs_root
*root
, u64 bytenr
)
9338 struct btrfs_block_group_cache
*block_group
;
9339 struct btrfs_space_info
*space_info
;
9340 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
9341 struct btrfs_device
*device
;
9342 struct btrfs_trans_handle
*trans
;
9351 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
9353 /* odd, couldn't find the block group, leave it alone */
9357 min_free
= btrfs_block_group_used(&block_group
->item
);
9359 /* no bytes used, we're good */
9363 space_info
= block_group
->space_info
;
9364 spin_lock(&space_info
->lock
);
9366 full
= space_info
->full
;
9369 * if this is the last block group we have in this space, we can't
9370 * relocate it unless we're able to allocate a new chunk below.
9372 * Otherwise, we need to make sure we have room in the space to handle
9373 * all of the extents from this block group. If we can, we're good
9375 if ((space_info
->total_bytes
!= block_group
->key
.offset
) &&
9376 (space_info
->bytes_used
+ space_info
->bytes_reserved
+
9377 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
9378 min_free
< space_info
->total_bytes
)) {
9379 spin_unlock(&space_info
->lock
);
9382 spin_unlock(&space_info
->lock
);
9385 * ok we don't have enough space, but maybe we have free space on our
9386 * devices to allocate new chunks for relocation, so loop through our
9387 * alloc devices and guess if we have enough space. if this block
9388 * group is going to be restriped, run checks against the target
9389 * profile instead of the current one.
9401 target
= get_restripe_target(root
->fs_info
, block_group
->flags
);
9403 index
= __get_raid_index(extended_to_chunk(target
));
9406 * this is just a balance, so if we were marked as full
9407 * we know there is no space for a new chunk
9412 index
= get_block_group_index(block_group
);
9415 if (index
== BTRFS_RAID_RAID10
) {
9419 } else if (index
== BTRFS_RAID_RAID1
) {
9421 } else if (index
== BTRFS_RAID_DUP
) {
9424 } else if (index
== BTRFS_RAID_RAID0
) {
9425 dev_min
= fs_devices
->rw_devices
;
9426 min_free
= div64_u64(min_free
, dev_min
);
9429 /* We need to do this so that we can look at pending chunks */
9430 trans
= btrfs_join_transaction(root
);
9431 if (IS_ERR(trans
)) {
9432 ret
= PTR_ERR(trans
);
9436 mutex_lock(&root
->fs_info
->chunk_mutex
);
9437 list_for_each_entry(device
, &fs_devices
->alloc_list
, dev_alloc_list
) {
9441 * check to make sure we can actually find a chunk with enough
9442 * space to fit our block group in.
9444 if (device
->total_bytes
> device
->bytes_used
+ min_free
&&
9445 !device
->is_tgtdev_for_dev_replace
) {
9446 ret
= find_free_dev_extent(trans
, device
, min_free
,
9451 if (dev_nr
>= dev_min
)
9457 mutex_unlock(&root
->fs_info
->chunk_mutex
);
9458 btrfs_end_transaction(trans
, root
);
9460 btrfs_put_block_group(block_group
);
9464 static int find_first_block_group(struct btrfs_root
*root
,
9465 struct btrfs_path
*path
, struct btrfs_key
*key
)
9468 struct btrfs_key found_key
;
9469 struct extent_buffer
*leaf
;
9472 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
9477 slot
= path
->slots
[0];
9478 leaf
= path
->nodes
[0];
9479 if (slot
>= btrfs_header_nritems(leaf
)) {
9480 ret
= btrfs_next_leaf(root
, path
);
9487 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
9489 if (found_key
.objectid
>= key
->objectid
&&
9490 found_key
.type
== BTRFS_BLOCK_GROUP_ITEM_KEY
) {
9500 void btrfs_put_block_group_cache(struct btrfs_fs_info
*info
)
9502 struct btrfs_block_group_cache
*block_group
;
9506 struct inode
*inode
;
9508 block_group
= btrfs_lookup_first_block_group(info
, last
);
9509 while (block_group
) {
9510 spin_lock(&block_group
->lock
);
9511 if (block_group
->iref
)
9513 spin_unlock(&block_group
->lock
);
9514 block_group
= next_block_group(info
->tree_root
,
9524 inode
= block_group
->inode
;
9525 block_group
->iref
= 0;
9526 block_group
->inode
= NULL
;
9527 spin_unlock(&block_group
->lock
);
9529 last
= block_group
->key
.objectid
+ block_group
->key
.offset
;
9530 btrfs_put_block_group(block_group
);
9534 int btrfs_free_block_groups(struct btrfs_fs_info
*info
)
9536 struct btrfs_block_group_cache
*block_group
;
9537 struct btrfs_space_info
*space_info
;
9538 struct btrfs_caching_control
*caching_ctl
;
9541 down_write(&info
->commit_root_sem
);
9542 while (!list_empty(&info
->caching_block_groups
)) {
9543 caching_ctl
= list_entry(info
->caching_block_groups
.next
,
9544 struct btrfs_caching_control
, list
);
9545 list_del(&caching_ctl
->list
);
9546 put_caching_control(caching_ctl
);
9548 up_write(&info
->commit_root_sem
);
9550 spin_lock(&info
->unused_bgs_lock
);
9551 while (!list_empty(&info
->unused_bgs
)) {
9552 block_group
= list_first_entry(&info
->unused_bgs
,
9553 struct btrfs_block_group_cache
,
9555 list_del_init(&block_group
->bg_list
);
9556 btrfs_put_block_group(block_group
);
9558 spin_unlock(&info
->unused_bgs_lock
);
9560 spin_lock(&info
->block_group_cache_lock
);
9561 while ((n
= rb_last(&info
->block_group_cache_tree
)) != NULL
) {
9562 block_group
= rb_entry(n
, struct btrfs_block_group_cache
,
9564 rb_erase(&block_group
->cache_node
,
9565 &info
->block_group_cache_tree
);
9566 RB_CLEAR_NODE(&block_group
->cache_node
);
9567 spin_unlock(&info
->block_group_cache_lock
);
9569 down_write(&block_group
->space_info
->groups_sem
);
9570 list_del(&block_group
->list
);
9571 up_write(&block_group
->space_info
->groups_sem
);
9573 if (block_group
->cached
== BTRFS_CACHE_STARTED
)
9574 wait_block_group_cache_done(block_group
);
9577 * We haven't cached this block group, which means we could
9578 * possibly have excluded extents on this block group.
9580 if (block_group
->cached
== BTRFS_CACHE_NO
||
9581 block_group
->cached
== BTRFS_CACHE_ERROR
)
9582 free_excluded_extents(info
->extent_root
, block_group
);
9584 btrfs_remove_free_space_cache(block_group
);
9585 btrfs_put_block_group(block_group
);
9587 spin_lock(&info
->block_group_cache_lock
);
9589 spin_unlock(&info
->block_group_cache_lock
);
9591 /* now that all the block groups are freed, go through and
9592 * free all the space_info structs. This is only called during
9593 * the final stages of unmount, and so we know nobody is
9594 * using them. We call synchronize_rcu() once before we start,
9595 * just to be on the safe side.
9599 release_global_block_rsv(info
);
9601 while (!list_empty(&info
->space_info
)) {
9604 space_info
= list_entry(info
->space_info
.next
,
9605 struct btrfs_space_info
,
9607 if (btrfs_test_opt(info
->tree_root
, ENOSPC_DEBUG
)) {
9608 if (WARN_ON(space_info
->bytes_pinned
> 0 ||
9609 space_info
->bytes_reserved
> 0 ||
9610 space_info
->bytes_may_use
> 0)) {
9611 dump_space_info(space_info
, 0, 0);
9614 list_del(&space_info
->list
);
9615 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++) {
9616 struct kobject
*kobj
;
9617 kobj
= space_info
->block_group_kobjs
[i
];
9618 space_info
->block_group_kobjs
[i
] = NULL
;
9624 kobject_del(&space_info
->kobj
);
9625 kobject_put(&space_info
->kobj
);
9630 static void __link_block_group(struct btrfs_space_info
*space_info
,
9631 struct btrfs_block_group_cache
*cache
)
9633 int index
= get_block_group_index(cache
);
9636 down_write(&space_info
->groups_sem
);
9637 if (list_empty(&space_info
->block_groups
[index
]))
9639 list_add_tail(&cache
->list
, &space_info
->block_groups
[index
]);
9640 up_write(&space_info
->groups_sem
);
9643 struct raid_kobject
*rkobj
;
9646 rkobj
= kzalloc(sizeof(*rkobj
), GFP_NOFS
);
9649 rkobj
->raid_type
= index
;
9650 kobject_init(&rkobj
->kobj
, &btrfs_raid_ktype
);
9651 ret
= kobject_add(&rkobj
->kobj
, &space_info
->kobj
,
9652 "%s", get_raid_name(index
));
9654 kobject_put(&rkobj
->kobj
);
9657 space_info
->block_group_kobjs
[index
] = &rkobj
->kobj
;
9662 pr_warn("BTRFS: failed to add kobject for block cache. ignoring.\n");
9665 static struct btrfs_block_group_cache
*
9666 btrfs_create_block_group_cache(struct btrfs_root
*root
, u64 start
, u64 size
)
9668 struct btrfs_block_group_cache
*cache
;
9670 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
9674 cache
->free_space_ctl
= kzalloc(sizeof(*cache
->free_space_ctl
),
9676 if (!cache
->free_space_ctl
) {
9681 cache
->key
.objectid
= start
;
9682 cache
->key
.offset
= size
;
9683 cache
->key
.type
= BTRFS_BLOCK_GROUP_ITEM_KEY
;
9685 cache
->sectorsize
= root
->sectorsize
;
9686 cache
->fs_info
= root
->fs_info
;
9687 cache
->full_stripe_len
= btrfs_full_stripe_len(root
,
9688 &root
->fs_info
->mapping_tree
,
9690 set_free_space_tree_thresholds(cache
);
9692 atomic_set(&cache
->count
, 1);
9693 spin_lock_init(&cache
->lock
);
9694 init_rwsem(&cache
->data_rwsem
);
9695 INIT_LIST_HEAD(&cache
->list
);
9696 INIT_LIST_HEAD(&cache
->cluster_list
);
9697 INIT_LIST_HEAD(&cache
->bg_list
);
9698 INIT_LIST_HEAD(&cache
->ro_list
);
9699 INIT_LIST_HEAD(&cache
->dirty_list
);
9700 INIT_LIST_HEAD(&cache
->io_list
);
9701 btrfs_init_free_space_ctl(cache
);
9702 atomic_set(&cache
->trimming
, 0);
9703 mutex_init(&cache
->free_space_lock
);
9708 int btrfs_read_block_groups(struct btrfs_root
*root
)
9710 struct btrfs_path
*path
;
9712 struct btrfs_block_group_cache
*cache
;
9713 struct btrfs_fs_info
*info
= root
->fs_info
;
9714 struct btrfs_space_info
*space_info
;
9715 struct btrfs_key key
;
9716 struct btrfs_key found_key
;
9717 struct extent_buffer
*leaf
;
9721 root
= info
->extent_root
;
9724 key
.type
= BTRFS_BLOCK_GROUP_ITEM_KEY
;
9725 path
= btrfs_alloc_path();
9730 cache_gen
= btrfs_super_cache_generation(root
->fs_info
->super_copy
);
9731 if (btrfs_test_opt(root
, SPACE_CACHE
) &&
9732 btrfs_super_generation(root
->fs_info
->super_copy
) != cache_gen
)
9734 if (btrfs_test_opt(root
, CLEAR_CACHE
))
9738 ret
= find_first_block_group(root
, path
, &key
);
9744 leaf
= path
->nodes
[0];
9745 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
9747 cache
= btrfs_create_block_group_cache(root
, found_key
.objectid
,
9756 * When we mount with old space cache, we need to
9757 * set BTRFS_DC_CLEAR and set dirty flag.
9759 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
9760 * truncate the old free space cache inode and
9762 * b) Setting 'dirty flag' makes sure that we flush
9763 * the new space cache info onto disk.
9765 if (btrfs_test_opt(root
, SPACE_CACHE
))
9766 cache
->disk_cache_state
= BTRFS_DC_CLEAR
;
9769 read_extent_buffer(leaf
, &cache
->item
,
9770 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
9771 sizeof(cache
->item
));
9772 cache
->flags
= btrfs_block_group_flags(&cache
->item
);
9774 key
.objectid
= found_key
.objectid
+ found_key
.offset
;
9775 btrfs_release_path(path
);
9778 * We need to exclude the super stripes now so that the space
9779 * info has super bytes accounted for, otherwise we'll think
9780 * we have more space than we actually do.
9782 ret
= exclude_super_stripes(root
, cache
);
9785 * We may have excluded something, so call this just in
9788 free_excluded_extents(root
, cache
);
9789 btrfs_put_block_group(cache
);
9794 * check for two cases, either we are full, and therefore
9795 * don't need to bother with the caching work since we won't
9796 * find any space, or we are empty, and we can just add all
9797 * the space in and be done with it. This saves us _alot_ of
9798 * time, particularly in the full case.
9800 if (found_key
.offset
== btrfs_block_group_used(&cache
->item
)) {
9801 cache
->last_byte_to_unpin
= (u64
)-1;
9802 cache
->cached
= BTRFS_CACHE_FINISHED
;
9803 free_excluded_extents(root
, cache
);
9804 } else if (btrfs_block_group_used(&cache
->item
) == 0) {
9805 cache
->last_byte_to_unpin
= (u64
)-1;
9806 cache
->cached
= BTRFS_CACHE_FINISHED
;
9807 add_new_free_space(cache
, root
->fs_info
,
9809 found_key
.objectid
+
9811 free_excluded_extents(root
, cache
);
9814 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
9816 btrfs_remove_free_space_cache(cache
);
9817 btrfs_put_block_group(cache
);
9821 ret
= update_space_info(info
, cache
->flags
, found_key
.offset
,
9822 btrfs_block_group_used(&cache
->item
),
9825 btrfs_remove_free_space_cache(cache
);
9826 spin_lock(&info
->block_group_cache_lock
);
9827 rb_erase(&cache
->cache_node
,
9828 &info
->block_group_cache_tree
);
9829 RB_CLEAR_NODE(&cache
->cache_node
);
9830 spin_unlock(&info
->block_group_cache_lock
);
9831 btrfs_put_block_group(cache
);
9835 cache
->space_info
= space_info
;
9836 spin_lock(&cache
->space_info
->lock
);
9837 cache
->space_info
->bytes_readonly
+= cache
->bytes_super
;
9838 spin_unlock(&cache
->space_info
->lock
);
9840 __link_block_group(space_info
, cache
);
9842 set_avail_alloc_bits(root
->fs_info
, cache
->flags
);
9843 if (btrfs_chunk_readonly(root
, cache
->key
.objectid
)) {
9844 inc_block_group_ro(cache
, 1);
9845 } else if (btrfs_block_group_used(&cache
->item
) == 0) {
9846 spin_lock(&info
->unused_bgs_lock
);
9847 /* Should always be true but just in case. */
9848 if (list_empty(&cache
->bg_list
)) {
9849 btrfs_get_block_group(cache
);
9850 list_add_tail(&cache
->bg_list
,
9853 spin_unlock(&info
->unused_bgs_lock
);
9857 list_for_each_entry_rcu(space_info
, &root
->fs_info
->space_info
, list
) {
9858 if (!(get_alloc_profile(root
, space_info
->flags
) &
9859 (BTRFS_BLOCK_GROUP_RAID10
|
9860 BTRFS_BLOCK_GROUP_RAID1
|
9861 BTRFS_BLOCK_GROUP_RAID5
|
9862 BTRFS_BLOCK_GROUP_RAID6
|
9863 BTRFS_BLOCK_GROUP_DUP
)))
9866 * avoid allocating from un-mirrored block group if there are
9867 * mirrored block groups.
9869 list_for_each_entry(cache
,
9870 &space_info
->block_groups
[BTRFS_RAID_RAID0
],
9872 inc_block_group_ro(cache
, 1);
9873 list_for_each_entry(cache
,
9874 &space_info
->block_groups
[BTRFS_RAID_SINGLE
],
9876 inc_block_group_ro(cache
, 1);
9879 init_global_block_rsv(info
);
9882 btrfs_free_path(path
);
9886 void btrfs_create_pending_block_groups(struct btrfs_trans_handle
*trans
,
9887 struct btrfs_root
*root
)
9889 struct btrfs_block_group_cache
*block_group
, *tmp
;
9890 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
9891 struct btrfs_block_group_item item
;
9892 struct btrfs_key key
;
9894 bool can_flush_pending_bgs
= trans
->can_flush_pending_bgs
;
9896 trans
->can_flush_pending_bgs
= false;
9897 list_for_each_entry_safe(block_group
, tmp
, &trans
->new_bgs
, bg_list
) {
9901 spin_lock(&block_group
->lock
);
9902 memcpy(&item
, &block_group
->item
, sizeof(item
));
9903 memcpy(&key
, &block_group
->key
, sizeof(key
));
9904 spin_unlock(&block_group
->lock
);
9906 ret
= btrfs_insert_item(trans
, extent_root
, &key
, &item
,
9909 btrfs_abort_transaction(trans
, extent_root
, ret
);
9910 ret
= btrfs_finish_chunk_alloc(trans
, extent_root
,
9911 key
.objectid
, key
.offset
);
9913 btrfs_abort_transaction(trans
, extent_root
, ret
);
9914 add_block_group_free_space(trans
, root
->fs_info
, block_group
);
9915 /* already aborted the transaction if it failed. */
9917 list_del_init(&block_group
->bg_list
);
9919 trans
->can_flush_pending_bgs
= can_flush_pending_bgs
;
9922 int btrfs_make_block_group(struct btrfs_trans_handle
*trans
,
9923 struct btrfs_root
*root
, u64 bytes_used
,
9924 u64 type
, u64 chunk_objectid
, u64 chunk_offset
,
9928 struct btrfs_root
*extent_root
;
9929 struct btrfs_block_group_cache
*cache
;
9931 extent_root
= root
->fs_info
->extent_root
;
9933 btrfs_set_log_full_commit(root
->fs_info
, trans
);
9935 cache
= btrfs_create_block_group_cache(root
, chunk_offset
, size
);
9939 btrfs_set_block_group_used(&cache
->item
, bytes_used
);
9940 btrfs_set_block_group_chunk_objectid(&cache
->item
, chunk_objectid
);
9941 btrfs_set_block_group_flags(&cache
->item
, type
);
9943 cache
->flags
= type
;
9944 cache
->last_byte_to_unpin
= (u64
)-1;
9945 cache
->cached
= BTRFS_CACHE_FINISHED
;
9946 cache
->needs_free_space
= 1;
9947 ret
= exclude_super_stripes(root
, cache
);
9950 * We may have excluded something, so call this just in
9953 free_excluded_extents(root
, cache
);
9954 btrfs_put_block_group(cache
);
9958 add_new_free_space(cache
, root
->fs_info
, chunk_offset
,
9959 chunk_offset
+ size
);
9961 free_excluded_extents(root
, cache
);
9963 #ifdef CONFIG_BTRFS_DEBUG
9964 if (btrfs_should_fragment_free_space(root
, cache
)) {
9965 u64 new_bytes_used
= size
- bytes_used
;
9967 bytes_used
+= new_bytes_used
>> 1;
9968 fragment_free_space(root
, cache
);
9972 * Call to ensure the corresponding space_info object is created and
9973 * assigned to our block group, but don't update its counters just yet.
9974 * We want our bg to be added to the rbtree with its ->space_info set.
9976 ret
= update_space_info(root
->fs_info
, cache
->flags
, 0, 0,
9977 &cache
->space_info
);
9979 btrfs_remove_free_space_cache(cache
);
9980 btrfs_put_block_group(cache
);
9984 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
9986 btrfs_remove_free_space_cache(cache
);
9987 btrfs_put_block_group(cache
);
9992 * Now that our block group has its ->space_info set and is inserted in
9993 * the rbtree, update the space info's counters.
9995 ret
= update_space_info(root
->fs_info
, cache
->flags
, size
, bytes_used
,
9996 &cache
->space_info
);
9998 btrfs_remove_free_space_cache(cache
);
9999 spin_lock(&root
->fs_info
->block_group_cache_lock
);
10000 rb_erase(&cache
->cache_node
,
10001 &root
->fs_info
->block_group_cache_tree
);
10002 RB_CLEAR_NODE(&cache
->cache_node
);
10003 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
10004 btrfs_put_block_group(cache
);
10007 update_global_block_rsv(root
->fs_info
);
10009 spin_lock(&cache
->space_info
->lock
);
10010 cache
->space_info
->bytes_readonly
+= cache
->bytes_super
;
10011 spin_unlock(&cache
->space_info
->lock
);
10013 __link_block_group(cache
->space_info
, cache
);
10015 list_add_tail(&cache
->bg_list
, &trans
->new_bgs
);
10017 set_avail_alloc_bits(extent_root
->fs_info
, type
);
10022 static void clear_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
10024 u64 extra_flags
= chunk_to_extended(flags
) &
10025 BTRFS_EXTENDED_PROFILE_MASK
;
10027 write_seqlock(&fs_info
->profiles_lock
);
10028 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
10029 fs_info
->avail_data_alloc_bits
&= ~extra_flags
;
10030 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
10031 fs_info
->avail_metadata_alloc_bits
&= ~extra_flags
;
10032 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
10033 fs_info
->avail_system_alloc_bits
&= ~extra_flags
;
10034 write_sequnlock(&fs_info
->profiles_lock
);
10037 int btrfs_remove_block_group(struct btrfs_trans_handle
*trans
,
10038 struct btrfs_root
*root
, u64 group_start
,
10039 struct extent_map
*em
)
10041 struct btrfs_path
*path
;
10042 struct btrfs_block_group_cache
*block_group
;
10043 struct btrfs_free_cluster
*cluster
;
10044 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
10045 struct btrfs_key key
;
10046 struct inode
*inode
;
10047 struct kobject
*kobj
= NULL
;
10051 struct btrfs_caching_control
*caching_ctl
= NULL
;
10054 root
= root
->fs_info
->extent_root
;
10056 block_group
= btrfs_lookup_block_group(root
->fs_info
, group_start
);
10057 BUG_ON(!block_group
);
10058 BUG_ON(!block_group
->ro
);
10061 * Free the reserved super bytes from this block group before
10064 free_excluded_extents(root
, block_group
);
10066 memcpy(&key
, &block_group
->key
, sizeof(key
));
10067 index
= get_block_group_index(block_group
);
10068 if (block_group
->flags
& (BTRFS_BLOCK_GROUP_DUP
|
10069 BTRFS_BLOCK_GROUP_RAID1
|
10070 BTRFS_BLOCK_GROUP_RAID10
))
10075 /* make sure this block group isn't part of an allocation cluster */
10076 cluster
= &root
->fs_info
->data_alloc_cluster
;
10077 spin_lock(&cluster
->refill_lock
);
10078 btrfs_return_cluster_to_free_space(block_group
, cluster
);
10079 spin_unlock(&cluster
->refill_lock
);
10082 * make sure this block group isn't part of a metadata
10083 * allocation cluster
10085 cluster
= &root
->fs_info
->meta_alloc_cluster
;
10086 spin_lock(&cluster
->refill_lock
);
10087 btrfs_return_cluster_to_free_space(block_group
, cluster
);
10088 spin_unlock(&cluster
->refill_lock
);
10090 path
= btrfs_alloc_path();
10097 * get the inode first so any iput calls done for the io_list
10098 * aren't the final iput (no unlinks allowed now)
10100 inode
= lookup_free_space_inode(tree_root
, block_group
, path
);
10102 mutex_lock(&trans
->transaction
->cache_write_mutex
);
10104 * make sure our free spache cache IO is done before remove the
10107 spin_lock(&trans
->transaction
->dirty_bgs_lock
);
10108 if (!list_empty(&block_group
->io_list
)) {
10109 list_del_init(&block_group
->io_list
);
10111 WARN_ON(!IS_ERR(inode
) && inode
!= block_group
->io_ctl
.inode
);
10113 spin_unlock(&trans
->transaction
->dirty_bgs_lock
);
10114 btrfs_wait_cache_io(root
, trans
, block_group
,
10115 &block_group
->io_ctl
, path
,
10116 block_group
->key
.objectid
);
10117 btrfs_put_block_group(block_group
);
10118 spin_lock(&trans
->transaction
->dirty_bgs_lock
);
10121 if (!list_empty(&block_group
->dirty_list
)) {
10122 list_del_init(&block_group
->dirty_list
);
10123 btrfs_put_block_group(block_group
);
10125 spin_unlock(&trans
->transaction
->dirty_bgs_lock
);
10126 mutex_unlock(&trans
->transaction
->cache_write_mutex
);
10128 if (!IS_ERR(inode
)) {
10129 ret
= btrfs_orphan_add(trans
, inode
);
10131 btrfs_add_delayed_iput(inode
);
10134 clear_nlink(inode
);
10135 /* One for the block groups ref */
10136 spin_lock(&block_group
->lock
);
10137 if (block_group
->iref
) {
10138 block_group
->iref
= 0;
10139 block_group
->inode
= NULL
;
10140 spin_unlock(&block_group
->lock
);
10143 spin_unlock(&block_group
->lock
);
10145 /* One for our lookup ref */
10146 btrfs_add_delayed_iput(inode
);
10149 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
10150 key
.offset
= block_group
->key
.objectid
;
10153 ret
= btrfs_search_slot(trans
, tree_root
, &key
, path
, -1, 1);
10157 btrfs_release_path(path
);
10159 ret
= btrfs_del_item(trans
, tree_root
, path
);
10162 btrfs_release_path(path
);
10165 spin_lock(&root
->fs_info
->block_group_cache_lock
);
10166 rb_erase(&block_group
->cache_node
,
10167 &root
->fs_info
->block_group_cache_tree
);
10168 RB_CLEAR_NODE(&block_group
->cache_node
);
10170 if (root
->fs_info
->first_logical_byte
== block_group
->key
.objectid
)
10171 root
->fs_info
->first_logical_byte
= (u64
)-1;
10172 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
10174 down_write(&block_group
->space_info
->groups_sem
);
10176 * we must use list_del_init so people can check to see if they
10177 * are still on the list after taking the semaphore
10179 list_del_init(&block_group
->list
);
10180 if (list_empty(&block_group
->space_info
->block_groups
[index
])) {
10181 kobj
= block_group
->space_info
->block_group_kobjs
[index
];
10182 block_group
->space_info
->block_group_kobjs
[index
] = NULL
;
10183 clear_avail_alloc_bits(root
->fs_info
, block_group
->flags
);
10185 up_write(&block_group
->space_info
->groups_sem
);
10191 if (block_group
->has_caching_ctl
)
10192 caching_ctl
= get_caching_control(block_group
);
10193 if (block_group
->cached
== BTRFS_CACHE_STARTED
)
10194 wait_block_group_cache_done(block_group
);
10195 if (block_group
->has_caching_ctl
) {
10196 down_write(&root
->fs_info
->commit_root_sem
);
10197 if (!caching_ctl
) {
10198 struct btrfs_caching_control
*ctl
;
10200 list_for_each_entry(ctl
,
10201 &root
->fs_info
->caching_block_groups
, list
)
10202 if (ctl
->block_group
== block_group
) {
10204 atomic_inc(&caching_ctl
->count
);
10209 list_del_init(&caching_ctl
->list
);
10210 up_write(&root
->fs_info
->commit_root_sem
);
10212 /* Once for the caching bgs list and once for us. */
10213 put_caching_control(caching_ctl
);
10214 put_caching_control(caching_ctl
);
10218 spin_lock(&trans
->transaction
->dirty_bgs_lock
);
10219 if (!list_empty(&block_group
->dirty_list
)) {
10222 if (!list_empty(&block_group
->io_list
)) {
10225 spin_unlock(&trans
->transaction
->dirty_bgs_lock
);
10226 btrfs_remove_free_space_cache(block_group
);
10228 spin_lock(&block_group
->space_info
->lock
);
10229 list_del_init(&block_group
->ro_list
);
10231 if (btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
10232 WARN_ON(block_group
->space_info
->total_bytes
10233 < block_group
->key
.offset
);
10234 WARN_ON(block_group
->space_info
->bytes_readonly
10235 < block_group
->key
.offset
);
10236 WARN_ON(block_group
->space_info
->disk_total
10237 < block_group
->key
.offset
* factor
);
10239 block_group
->space_info
->total_bytes
-= block_group
->key
.offset
;
10240 block_group
->space_info
->bytes_readonly
-= block_group
->key
.offset
;
10241 block_group
->space_info
->disk_total
-= block_group
->key
.offset
* factor
;
10243 spin_unlock(&block_group
->space_info
->lock
);
10245 memcpy(&key
, &block_group
->key
, sizeof(key
));
10248 if (!list_empty(&em
->list
)) {
10249 /* We're in the transaction->pending_chunks list. */
10250 free_extent_map(em
);
10252 spin_lock(&block_group
->lock
);
10253 block_group
->removed
= 1;
10255 * At this point trimming can't start on this block group, because we
10256 * removed the block group from the tree fs_info->block_group_cache_tree
10257 * so no one can't find it anymore and even if someone already got this
10258 * block group before we removed it from the rbtree, they have already
10259 * incremented block_group->trimming - if they didn't, they won't find
10260 * any free space entries because we already removed them all when we
10261 * called btrfs_remove_free_space_cache().
10263 * And we must not remove the extent map from the fs_info->mapping_tree
10264 * to prevent the same logical address range and physical device space
10265 * ranges from being reused for a new block group. This is because our
10266 * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
10267 * completely transactionless, so while it is trimming a range the
10268 * currently running transaction might finish and a new one start,
10269 * allowing for new block groups to be created that can reuse the same
10270 * physical device locations unless we take this special care.
10272 * There may also be an implicit trim operation if the file system
10273 * is mounted with -odiscard. The same protections must remain
10274 * in place until the extents have been discarded completely when
10275 * the transaction commit has completed.
10277 remove_em
= (atomic_read(&block_group
->trimming
) == 0);
10279 * Make sure a trimmer task always sees the em in the pinned_chunks list
10280 * if it sees block_group->removed == 1 (needs to lock block_group->lock
10281 * before checking block_group->removed).
10285 * Our em might be in trans->transaction->pending_chunks which
10286 * is protected by fs_info->chunk_mutex ([lock|unlock]_chunks),
10287 * and so is the fs_info->pinned_chunks list.
10289 * So at this point we must be holding the chunk_mutex to avoid
10290 * any races with chunk allocation (more specifically at
10291 * volumes.c:contains_pending_extent()), to ensure it always
10292 * sees the em, either in the pending_chunks list or in the
10293 * pinned_chunks list.
10295 list_move_tail(&em
->list
, &root
->fs_info
->pinned_chunks
);
10297 spin_unlock(&block_group
->lock
);
10300 struct extent_map_tree
*em_tree
;
10302 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
10303 write_lock(&em_tree
->lock
);
10305 * The em might be in the pending_chunks list, so make sure the
10306 * chunk mutex is locked, since remove_extent_mapping() will
10307 * delete us from that list.
10309 remove_extent_mapping(em_tree
, em
);
10310 write_unlock(&em_tree
->lock
);
10311 /* once for the tree */
10312 free_extent_map(em
);
10315 unlock_chunks(root
);
10317 ret
= remove_block_group_free_space(trans
, root
->fs_info
, block_group
);
10321 btrfs_put_block_group(block_group
);
10322 btrfs_put_block_group(block_group
);
10324 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
10330 ret
= btrfs_del_item(trans
, root
, path
);
10332 btrfs_free_path(path
);
10336 struct btrfs_trans_handle
*
10337 btrfs_start_trans_remove_block_group(struct btrfs_fs_info
*fs_info
,
10338 const u64 chunk_offset
)
10340 struct extent_map_tree
*em_tree
= &fs_info
->mapping_tree
.map_tree
;
10341 struct extent_map
*em
;
10342 struct map_lookup
*map
;
10343 unsigned int num_items
;
10345 read_lock(&em_tree
->lock
);
10346 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
10347 read_unlock(&em_tree
->lock
);
10348 ASSERT(em
&& em
->start
== chunk_offset
);
10351 * We need to reserve 3 + N units from the metadata space info in order
10352 * to remove a block group (done at btrfs_remove_chunk() and at
10353 * btrfs_remove_block_group()), which are used for:
10355 * 1 unit for adding the free space inode's orphan (located in the tree
10357 * 1 unit for deleting the block group item (located in the extent
10359 * 1 unit for deleting the free space item (located in tree of tree
10361 * N units for deleting N device extent items corresponding to each
10362 * stripe (located in the device tree).
10364 * In order to remove a block group we also need to reserve units in the
10365 * system space info in order to update the chunk tree (update one or
10366 * more device items and remove one chunk item), but this is done at
10367 * btrfs_remove_chunk() through a call to check_system_chunk().
10369 map
= (struct map_lookup
*)em
->bdev
;
10370 num_items
= 3 + map
->num_stripes
;
10371 free_extent_map(em
);
10373 return btrfs_start_transaction_fallback_global_rsv(fs_info
->extent_root
,
10378 * Process the unused_bgs list and remove any that don't have any allocated
10379 * space inside of them.
10381 void btrfs_delete_unused_bgs(struct btrfs_fs_info
*fs_info
)
10383 struct btrfs_block_group_cache
*block_group
;
10384 struct btrfs_space_info
*space_info
;
10385 struct btrfs_root
*root
= fs_info
->extent_root
;
10386 struct btrfs_trans_handle
*trans
;
10389 if (!fs_info
->open
)
10392 spin_lock(&fs_info
->unused_bgs_lock
);
10393 while (!list_empty(&fs_info
->unused_bgs
)) {
10397 block_group
= list_first_entry(&fs_info
->unused_bgs
,
10398 struct btrfs_block_group_cache
,
10400 list_del_init(&block_group
->bg_list
);
10402 space_info
= block_group
->space_info
;
10404 if (ret
|| btrfs_mixed_space_info(space_info
)) {
10405 btrfs_put_block_group(block_group
);
10408 spin_unlock(&fs_info
->unused_bgs_lock
);
10410 mutex_lock(&fs_info
->delete_unused_bgs_mutex
);
10412 /* Don't want to race with allocators so take the groups_sem */
10413 down_write(&space_info
->groups_sem
);
10414 spin_lock(&block_group
->lock
);
10415 if (block_group
->reserved
||
10416 btrfs_block_group_used(&block_group
->item
) ||
10418 list_is_singular(&block_group
->list
)) {
10420 * We want to bail if we made new allocations or have
10421 * outstanding allocations in this block group. We do
10422 * the ro check in case balance is currently acting on
10423 * this block group.
10425 spin_unlock(&block_group
->lock
);
10426 up_write(&space_info
->groups_sem
);
10429 spin_unlock(&block_group
->lock
);
10431 /* We don't want to force the issue, only flip if it's ok. */
10432 ret
= inc_block_group_ro(block_group
, 0);
10433 up_write(&space_info
->groups_sem
);
10440 * Want to do this before we do anything else so we can recover
10441 * properly if we fail to join the transaction.
10443 trans
= btrfs_start_trans_remove_block_group(fs_info
,
10444 block_group
->key
.objectid
);
10445 if (IS_ERR(trans
)) {
10446 btrfs_dec_block_group_ro(root
, block_group
);
10447 ret
= PTR_ERR(trans
);
10452 * We could have pending pinned extents for this block group,
10453 * just delete them, we don't care about them anymore.
10455 start
= block_group
->key
.objectid
;
10456 end
= start
+ block_group
->key
.offset
- 1;
10458 * Hold the unused_bg_unpin_mutex lock to avoid racing with
10459 * btrfs_finish_extent_commit(). If we are at transaction N,
10460 * another task might be running finish_extent_commit() for the
10461 * previous transaction N - 1, and have seen a range belonging
10462 * to the block group in freed_extents[] before we were able to
10463 * clear the whole block group range from freed_extents[]. This
10464 * means that task can lookup for the block group after we
10465 * unpinned it from freed_extents[] and removed it, leading to
10466 * a BUG_ON() at btrfs_unpin_extent_range().
10468 mutex_lock(&fs_info
->unused_bg_unpin_mutex
);
10469 ret
= clear_extent_bits(&fs_info
->freed_extents
[0], start
, end
,
10470 EXTENT_DIRTY
, GFP_NOFS
);
10472 mutex_unlock(&fs_info
->unused_bg_unpin_mutex
);
10473 btrfs_dec_block_group_ro(root
, block_group
);
10476 ret
= clear_extent_bits(&fs_info
->freed_extents
[1], start
, end
,
10477 EXTENT_DIRTY
, GFP_NOFS
);
10479 mutex_unlock(&fs_info
->unused_bg_unpin_mutex
);
10480 btrfs_dec_block_group_ro(root
, block_group
);
10483 mutex_unlock(&fs_info
->unused_bg_unpin_mutex
);
10485 /* Reset pinned so btrfs_put_block_group doesn't complain */
10486 spin_lock(&space_info
->lock
);
10487 spin_lock(&block_group
->lock
);
10489 space_info
->bytes_pinned
-= block_group
->pinned
;
10490 space_info
->bytes_readonly
+= block_group
->pinned
;
10491 percpu_counter_add(&space_info
->total_bytes_pinned
,
10492 -block_group
->pinned
);
10493 block_group
->pinned
= 0;
10495 spin_unlock(&block_group
->lock
);
10496 spin_unlock(&space_info
->lock
);
10498 /* DISCARD can flip during remount */
10499 trimming
= btrfs_test_opt(root
, DISCARD
);
10501 /* Implicit trim during transaction commit. */
10503 btrfs_get_block_group_trimming(block_group
);
10506 * Btrfs_remove_chunk will abort the transaction if things go
10509 ret
= btrfs_remove_chunk(trans
, root
,
10510 block_group
->key
.objectid
);
10514 btrfs_put_block_group_trimming(block_group
);
10519 * If we're not mounted with -odiscard, we can just forget
10520 * about this block group. Otherwise we'll need to wait
10521 * until transaction commit to do the actual discard.
10524 WARN_ON(!list_empty(&block_group
->bg_list
));
10525 spin_lock(&trans
->transaction
->deleted_bgs_lock
);
10526 list_move(&block_group
->bg_list
,
10527 &trans
->transaction
->deleted_bgs
);
10528 spin_unlock(&trans
->transaction
->deleted_bgs_lock
);
10529 btrfs_get_block_group(block_group
);
10532 btrfs_end_transaction(trans
, root
);
10534 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
10535 btrfs_put_block_group(block_group
);
10536 spin_lock(&fs_info
->unused_bgs_lock
);
10538 spin_unlock(&fs_info
->unused_bgs_lock
);
10541 int btrfs_init_space_info(struct btrfs_fs_info
*fs_info
)
10543 struct btrfs_space_info
*space_info
;
10544 struct btrfs_super_block
*disk_super
;
10550 disk_super
= fs_info
->super_copy
;
10551 if (!btrfs_super_root(disk_super
))
10554 features
= btrfs_super_incompat_flags(disk_super
);
10555 if (features
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
10558 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
10559 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
10564 flags
= BTRFS_BLOCK_GROUP_METADATA
| BTRFS_BLOCK_GROUP_DATA
;
10565 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
10567 flags
= BTRFS_BLOCK_GROUP_METADATA
;
10568 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
10572 flags
= BTRFS_BLOCK_GROUP_DATA
;
10573 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
10579 int btrfs_error_unpin_extent_range(struct btrfs_root
*root
, u64 start
, u64 end
)
10581 return unpin_extent_range(root
, start
, end
, false);
10585 * It used to be that old block groups would be left around forever.
10586 * Iterating over them would be enough to trim unused space. Since we
10587 * now automatically remove them, we also need to iterate over unallocated
10590 * We don't want a transaction for this since the discard may take a
10591 * substantial amount of time. We don't require that a transaction be
10592 * running, but we do need to take a running transaction into account
10593 * to ensure that we're not discarding chunks that were released in
10594 * the current transaction.
10596 * Holding the chunks lock will prevent other threads from allocating
10597 * or releasing chunks, but it won't prevent a running transaction
10598 * from committing and releasing the memory that the pending chunks
10599 * list head uses. For that, we need to take a reference to the
10602 static int btrfs_trim_free_extents(struct btrfs_device
*device
,
10603 u64 minlen
, u64
*trimmed
)
10605 u64 start
= 0, len
= 0;
10610 /* Not writeable = nothing to do. */
10611 if (!device
->writeable
)
10614 /* No free space = nothing to do. */
10615 if (device
->total_bytes
<= device
->bytes_used
)
10621 struct btrfs_fs_info
*fs_info
= device
->dev_root
->fs_info
;
10622 struct btrfs_transaction
*trans
;
10625 ret
= mutex_lock_interruptible(&fs_info
->chunk_mutex
);
10629 down_read(&fs_info
->commit_root_sem
);
10631 spin_lock(&fs_info
->trans_lock
);
10632 trans
= fs_info
->running_transaction
;
10634 atomic_inc(&trans
->use_count
);
10635 spin_unlock(&fs_info
->trans_lock
);
10637 ret
= find_free_dev_extent_start(trans
, device
, minlen
, start
,
10640 btrfs_put_transaction(trans
);
10643 up_read(&fs_info
->commit_root_sem
);
10644 mutex_unlock(&fs_info
->chunk_mutex
);
10645 if (ret
== -ENOSPC
)
10650 ret
= btrfs_issue_discard(device
->bdev
, start
, len
, &bytes
);
10651 up_read(&fs_info
->commit_root_sem
);
10652 mutex_unlock(&fs_info
->chunk_mutex
);
10660 if (fatal_signal_pending(current
)) {
10661 ret
= -ERESTARTSYS
;
10671 int btrfs_trim_fs(struct btrfs_root
*root
, struct fstrim_range
*range
)
10673 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
10674 struct btrfs_block_group_cache
*cache
= NULL
;
10675 struct btrfs_device
*device
;
10676 struct list_head
*devices
;
10681 u64 total_bytes
= btrfs_super_total_bytes(fs_info
->super_copy
);
10685 * try to trim all FS space, our block group may start from non-zero.
10687 if (range
->len
== total_bytes
)
10688 cache
= btrfs_lookup_first_block_group(fs_info
, range
->start
);
10690 cache
= btrfs_lookup_block_group(fs_info
, range
->start
);
10693 if (cache
->key
.objectid
>= (range
->start
+ range
->len
)) {
10694 btrfs_put_block_group(cache
);
10698 start
= max(range
->start
, cache
->key
.objectid
);
10699 end
= min(range
->start
+ range
->len
,
10700 cache
->key
.objectid
+ cache
->key
.offset
);
10702 if (end
- start
>= range
->minlen
) {
10703 if (!block_group_cache_done(cache
)) {
10704 ret
= cache_block_group(cache
, 0);
10706 btrfs_put_block_group(cache
);
10709 ret
= wait_block_group_cache_done(cache
);
10711 btrfs_put_block_group(cache
);
10715 ret
= btrfs_trim_block_group(cache
,
10721 trimmed
+= group_trimmed
;
10723 btrfs_put_block_group(cache
);
10728 cache
= next_block_group(fs_info
->tree_root
, cache
);
10731 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
10732 devices
= &root
->fs_info
->fs_devices
->alloc_list
;
10733 list_for_each_entry(device
, devices
, dev_alloc_list
) {
10734 ret
= btrfs_trim_free_extents(device
, range
->minlen
,
10739 trimmed
+= group_trimmed
;
10741 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
10743 range
->len
= trimmed
;
10748 * btrfs_{start,end}_write_no_snapshoting() are similar to
10749 * mnt_{want,drop}_write(), they are used to prevent some tasks from writing
10750 * data into the page cache through nocow before the subvolume is snapshoted,
10751 * but flush the data into disk after the snapshot creation, or to prevent
10752 * operations while snapshoting is ongoing and that cause the snapshot to be
10753 * inconsistent (writes followed by expanding truncates for example).
10755 void btrfs_end_write_no_snapshoting(struct btrfs_root
*root
)
10757 percpu_counter_dec(&root
->subv_writers
->counter
);
10759 * Make sure counter is updated before we wake up waiters.
10762 if (waitqueue_active(&root
->subv_writers
->wait
))
10763 wake_up(&root
->subv_writers
->wait
);
10766 int btrfs_start_write_no_snapshoting(struct btrfs_root
*root
)
10768 if (atomic_read(&root
->will_be_snapshoted
))
10771 percpu_counter_inc(&root
->subv_writers
->counter
);
10773 * Make sure counter is updated before we check for snapshot creation.
10776 if (atomic_read(&root
->will_be_snapshoted
)) {
10777 btrfs_end_write_no_snapshoting(root
);