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"
40 #undef SCRAMBLE_DELAYED_REFS
43 * control flags for do_chunk_alloc's force field
44 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
45 * if we really need one.
47 * CHUNK_ALLOC_LIMITED means to only try and allocate one
48 * if we have very few chunks already allocated. This is
49 * used as part of the clustering code to help make sure
50 * we have a good pool of storage to cluster in, without
51 * filling the FS with empty chunks
53 * CHUNK_ALLOC_FORCE means it must try to allocate one
57 CHUNK_ALLOC_NO_FORCE
= 0,
58 CHUNK_ALLOC_LIMITED
= 1,
59 CHUNK_ALLOC_FORCE
= 2,
63 * Control how reservations are dealt with.
65 * RESERVE_FREE - freeing a reservation.
66 * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for
68 * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update
69 * bytes_may_use as the ENOSPC accounting is done elsewhere
74 RESERVE_ALLOC_NO_ACCOUNT
= 2,
77 static int update_block_group(struct btrfs_trans_handle
*trans
,
78 struct btrfs_root
*root
, u64 bytenr
,
79 u64 num_bytes
, int alloc
);
80 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
81 struct btrfs_root
*root
,
82 struct btrfs_delayed_ref_node
*node
, u64 parent
,
83 u64 root_objectid
, u64 owner_objectid
,
84 u64 owner_offset
, int refs_to_drop
,
85 struct btrfs_delayed_extent_op
*extra_op
);
86 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op
*extent_op
,
87 struct extent_buffer
*leaf
,
88 struct btrfs_extent_item
*ei
);
89 static int alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
90 struct btrfs_root
*root
,
91 u64 parent
, u64 root_objectid
,
92 u64 flags
, u64 owner
, u64 offset
,
93 struct btrfs_key
*ins
, int ref_mod
);
94 static int alloc_reserved_tree_block(struct btrfs_trans_handle
*trans
,
95 struct btrfs_root
*root
,
96 u64 parent
, u64 root_objectid
,
97 u64 flags
, struct btrfs_disk_key
*key
,
98 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 static 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 static 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 noinline
void caching_thread(struct btrfs_work
*work
)
401 struct btrfs_block_group_cache
*block_group
;
402 struct btrfs_fs_info
*fs_info
;
403 struct btrfs_caching_control
*caching_ctl
;
404 struct btrfs_root
*extent_root
;
405 struct btrfs_path
*path
;
406 struct extent_buffer
*leaf
;
407 struct btrfs_key key
;
414 caching_ctl
= container_of(work
, struct btrfs_caching_control
, work
);
415 block_group
= caching_ctl
->block_group
;
416 fs_info
= block_group
->fs_info
;
417 extent_root
= fs_info
->extent_root
;
419 path
= btrfs_alloc_path();
423 last
= max_t(u64
, block_group
->key
.objectid
, BTRFS_SUPER_INFO_OFFSET
);
425 #ifdef CONFIG_BTRFS_DEBUG
427 * If we're fragmenting we don't want to make anybody think we can
428 * allocate from this block group until we've had a chance to fragment
431 if (btrfs_should_fragment_free_space(extent_root
, block_group
))
435 * We don't want to deadlock with somebody trying to allocate a new
436 * extent for the extent root while also trying to search the extent
437 * root to add free space. So we skip locking and search the commit
438 * root, since its read-only
440 path
->skip_locking
= 1;
441 path
->search_commit_root
= 1;
446 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
448 mutex_lock(&caching_ctl
->mutex
);
449 /* need to make sure the commit_root doesn't disappear */
450 down_read(&fs_info
->commit_root_sem
);
453 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
457 leaf
= path
->nodes
[0];
458 nritems
= btrfs_header_nritems(leaf
);
461 if (btrfs_fs_closing(fs_info
) > 1) {
466 if (path
->slots
[0] < nritems
) {
467 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
469 ret
= find_next_key(path
, 0, &key
);
473 if (need_resched() ||
474 rwsem_is_contended(&fs_info
->commit_root_sem
)) {
476 caching_ctl
->progress
= last
;
477 btrfs_release_path(path
);
478 up_read(&fs_info
->commit_root_sem
);
479 mutex_unlock(&caching_ctl
->mutex
);
484 ret
= btrfs_next_leaf(extent_root
, path
);
489 leaf
= path
->nodes
[0];
490 nritems
= btrfs_header_nritems(leaf
);
494 if (key
.objectid
< last
) {
497 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
500 caching_ctl
->progress
= last
;
501 btrfs_release_path(path
);
505 if (key
.objectid
< block_group
->key
.objectid
) {
510 if (key
.objectid
>= block_group
->key
.objectid
+
511 block_group
->key
.offset
)
514 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
||
515 key
.type
== BTRFS_METADATA_ITEM_KEY
) {
516 total_found
+= add_new_free_space(block_group
,
519 if (key
.type
== BTRFS_METADATA_ITEM_KEY
)
520 last
= key
.objectid
+
521 fs_info
->tree_root
->nodesize
;
523 last
= key
.objectid
+ key
.offset
;
525 if (total_found
> (1024 * 1024 * 2)) {
528 wake_up(&caching_ctl
->wait
);
535 total_found
+= add_new_free_space(block_group
, fs_info
, last
,
536 block_group
->key
.objectid
+
537 block_group
->key
.offset
);
538 spin_lock(&block_group
->lock
);
539 block_group
->caching_ctl
= NULL
;
540 block_group
->cached
= BTRFS_CACHE_FINISHED
;
541 spin_unlock(&block_group
->lock
);
543 #ifdef CONFIG_BTRFS_DEBUG
544 if (btrfs_should_fragment_free_space(extent_root
, block_group
)) {
547 spin_lock(&block_group
->space_info
->lock
);
548 spin_lock(&block_group
->lock
);
549 bytes_used
= block_group
->key
.offset
-
550 btrfs_block_group_used(&block_group
->item
);
551 block_group
->space_info
->bytes_used
+= bytes_used
>> 1;
552 spin_unlock(&block_group
->lock
);
553 spin_unlock(&block_group
->space_info
->lock
);
554 fragment_free_space(extent_root
, block_group
);
558 caching_ctl
->progress
= (u64
)-1;
560 btrfs_free_path(path
);
561 up_read(&fs_info
->commit_root_sem
);
563 free_excluded_extents(extent_root
, block_group
);
565 mutex_unlock(&caching_ctl
->mutex
);
568 spin_lock(&block_group
->lock
);
569 block_group
->caching_ctl
= NULL
;
570 block_group
->cached
= BTRFS_CACHE_ERROR
;
571 spin_unlock(&block_group
->lock
);
573 wake_up(&caching_ctl
->wait
);
575 put_caching_control(caching_ctl
);
576 btrfs_put_block_group(block_group
);
579 static int cache_block_group(struct btrfs_block_group_cache
*cache
,
583 struct btrfs_fs_info
*fs_info
= cache
->fs_info
;
584 struct btrfs_caching_control
*caching_ctl
;
587 caching_ctl
= kzalloc(sizeof(*caching_ctl
), GFP_NOFS
);
591 INIT_LIST_HEAD(&caching_ctl
->list
);
592 mutex_init(&caching_ctl
->mutex
);
593 init_waitqueue_head(&caching_ctl
->wait
);
594 caching_ctl
->block_group
= cache
;
595 caching_ctl
->progress
= cache
->key
.objectid
;
596 atomic_set(&caching_ctl
->count
, 1);
597 btrfs_init_work(&caching_ctl
->work
, btrfs_cache_helper
,
598 caching_thread
, NULL
, NULL
);
600 spin_lock(&cache
->lock
);
602 * This should be a rare occasion, but this could happen I think in the
603 * case where one thread starts to load the space cache info, and then
604 * some other thread starts a transaction commit which tries to do an
605 * allocation while the other thread is still loading the space cache
606 * info. The previous loop should have kept us from choosing this block
607 * group, but if we've moved to the state where we will wait on caching
608 * block groups we need to first check if we're doing a fast load here,
609 * so we can wait for it to finish, otherwise we could end up allocating
610 * from a block group who's cache gets evicted for one reason or
613 while (cache
->cached
== BTRFS_CACHE_FAST
) {
614 struct btrfs_caching_control
*ctl
;
616 ctl
= cache
->caching_ctl
;
617 atomic_inc(&ctl
->count
);
618 prepare_to_wait(&ctl
->wait
, &wait
, TASK_UNINTERRUPTIBLE
);
619 spin_unlock(&cache
->lock
);
623 finish_wait(&ctl
->wait
, &wait
);
624 put_caching_control(ctl
);
625 spin_lock(&cache
->lock
);
628 if (cache
->cached
!= BTRFS_CACHE_NO
) {
629 spin_unlock(&cache
->lock
);
633 WARN_ON(cache
->caching_ctl
);
634 cache
->caching_ctl
= caching_ctl
;
635 cache
->cached
= BTRFS_CACHE_FAST
;
636 spin_unlock(&cache
->lock
);
638 if (fs_info
->mount_opt
& BTRFS_MOUNT_SPACE_CACHE
) {
639 mutex_lock(&caching_ctl
->mutex
);
640 ret
= load_free_space_cache(fs_info
, cache
);
642 spin_lock(&cache
->lock
);
644 cache
->caching_ctl
= NULL
;
645 cache
->cached
= BTRFS_CACHE_FINISHED
;
646 cache
->last_byte_to_unpin
= (u64
)-1;
647 caching_ctl
->progress
= (u64
)-1;
649 if (load_cache_only
) {
650 cache
->caching_ctl
= NULL
;
651 cache
->cached
= BTRFS_CACHE_NO
;
653 cache
->cached
= BTRFS_CACHE_STARTED
;
654 cache
->has_caching_ctl
= 1;
657 spin_unlock(&cache
->lock
);
658 #ifdef CONFIG_BTRFS_DEBUG
660 btrfs_should_fragment_free_space(fs_info
->extent_root
,
664 spin_lock(&cache
->space_info
->lock
);
665 spin_lock(&cache
->lock
);
666 bytes_used
= cache
->key
.offset
-
667 btrfs_block_group_used(&cache
->item
);
668 cache
->space_info
->bytes_used
+= bytes_used
>> 1;
669 spin_unlock(&cache
->lock
);
670 spin_unlock(&cache
->space_info
->lock
);
671 fragment_free_space(fs_info
->extent_root
, cache
);
674 mutex_unlock(&caching_ctl
->mutex
);
676 wake_up(&caching_ctl
->wait
);
678 put_caching_control(caching_ctl
);
679 free_excluded_extents(fs_info
->extent_root
, cache
);
684 * We are not going to do the fast caching, set cached to the
685 * appropriate value and wakeup any waiters.
687 spin_lock(&cache
->lock
);
688 if (load_cache_only
) {
689 cache
->caching_ctl
= NULL
;
690 cache
->cached
= BTRFS_CACHE_NO
;
692 cache
->cached
= BTRFS_CACHE_STARTED
;
693 cache
->has_caching_ctl
= 1;
695 spin_unlock(&cache
->lock
);
696 wake_up(&caching_ctl
->wait
);
699 if (load_cache_only
) {
700 put_caching_control(caching_ctl
);
704 down_write(&fs_info
->commit_root_sem
);
705 atomic_inc(&caching_ctl
->count
);
706 list_add_tail(&caching_ctl
->list
, &fs_info
->caching_block_groups
);
707 up_write(&fs_info
->commit_root_sem
);
709 btrfs_get_block_group(cache
);
711 btrfs_queue_work(fs_info
->caching_workers
, &caching_ctl
->work
);
717 * return the block group that starts at or after bytenr
719 static struct btrfs_block_group_cache
*
720 btrfs_lookup_first_block_group(struct btrfs_fs_info
*info
, u64 bytenr
)
722 struct btrfs_block_group_cache
*cache
;
724 cache
= block_group_cache_tree_search(info
, bytenr
, 0);
730 * return the block group that contains the given bytenr
732 struct btrfs_block_group_cache
*btrfs_lookup_block_group(
733 struct btrfs_fs_info
*info
,
736 struct btrfs_block_group_cache
*cache
;
738 cache
= block_group_cache_tree_search(info
, bytenr
, 1);
743 static struct btrfs_space_info
*__find_space_info(struct btrfs_fs_info
*info
,
746 struct list_head
*head
= &info
->space_info
;
747 struct btrfs_space_info
*found
;
749 flags
&= BTRFS_BLOCK_GROUP_TYPE_MASK
;
752 list_for_each_entry_rcu(found
, head
, list
) {
753 if (found
->flags
& flags
) {
763 * after adding space to the filesystem, we need to clear the full flags
764 * on all the space infos.
766 void btrfs_clear_space_info_full(struct btrfs_fs_info
*info
)
768 struct list_head
*head
= &info
->space_info
;
769 struct btrfs_space_info
*found
;
772 list_for_each_entry_rcu(found
, head
, list
)
777 /* simple helper to search for an existing data extent at a given offset */
778 int btrfs_lookup_data_extent(struct btrfs_root
*root
, u64 start
, u64 len
)
781 struct btrfs_key key
;
782 struct btrfs_path
*path
;
784 path
= btrfs_alloc_path();
788 key
.objectid
= start
;
790 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
791 ret
= btrfs_search_slot(NULL
, root
->fs_info
->extent_root
, &key
, path
,
793 btrfs_free_path(path
);
798 * helper function to lookup reference count and flags of a tree block.
800 * the head node for delayed ref is used to store the sum of all the
801 * reference count modifications queued up in the rbtree. the head
802 * node may also store the extent flags to set. This way you can check
803 * to see what the reference count and extent flags would be if all of
804 * the delayed refs are not processed.
806 int btrfs_lookup_extent_info(struct btrfs_trans_handle
*trans
,
807 struct btrfs_root
*root
, u64 bytenr
,
808 u64 offset
, int metadata
, u64
*refs
, u64
*flags
)
810 struct btrfs_delayed_ref_head
*head
;
811 struct btrfs_delayed_ref_root
*delayed_refs
;
812 struct btrfs_path
*path
;
813 struct btrfs_extent_item
*ei
;
814 struct extent_buffer
*leaf
;
815 struct btrfs_key key
;
822 * If we don't have skinny metadata, don't bother doing anything
825 if (metadata
&& !btrfs_fs_incompat(root
->fs_info
, SKINNY_METADATA
)) {
826 offset
= root
->nodesize
;
830 path
= btrfs_alloc_path();
835 path
->skip_locking
= 1;
836 path
->search_commit_root
= 1;
840 key
.objectid
= bytenr
;
843 key
.type
= BTRFS_METADATA_ITEM_KEY
;
845 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
847 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
,
852 if (ret
> 0 && metadata
&& key
.type
== BTRFS_METADATA_ITEM_KEY
) {
853 if (path
->slots
[0]) {
855 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
857 if (key
.objectid
== bytenr
&&
858 key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
859 key
.offset
== root
->nodesize
)
865 leaf
= path
->nodes
[0];
866 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
867 if (item_size
>= sizeof(*ei
)) {
868 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
869 struct btrfs_extent_item
);
870 num_refs
= btrfs_extent_refs(leaf
, ei
);
871 extent_flags
= btrfs_extent_flags(leaf
, ei
);
873 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
874 struct btrfs_extent_item_v0
*ei0
;
875 BUG_ON(item_size
!= sizeof(*ei0
));
876 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
877 struct btrfs_extent_item_v0
);
878 num_refs
= btrfs_extent_refs_v0(leaf
, ei0
);
879 /* FIXME: this isn't correct for data */
880 extent_flags
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
885 BUG_ON(num_refs
== 0);
895 delayed_refs
= &trans
->transaction
->delayed_refs
;
896 spin_lock(&delayed_refs
->lock
);
897 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
899 if (!mutex_trylock(&head
->mutex
)) {
900 atomic_inc(&head
->node
.refs
);
901 spin_unlock(&delayed_refs
->lock
);
903 btrfs_release_path(path
);
906 * Mutex was contended, block until it's released and try
909 mutex_lock(&head
->mutex
);
910 mutex_unlock(&head
->mutex
);
911 btrfs_put_delayed_ref(&head
->node
);
914 spin_lock(&head
->lock
);
915 if (head
->extent_op
&& head
->extent_op
->update_flags
)
916 extent_flags
|= head
->extent_op
->flags_to_set
;
918 BUG_ON(num_refs
== 0);
920 num_refs
+= head
->node
.ref_mod
;
921 spin_unlock(&head
->lock
);
922 mutex_unlock(&head
->mutex
);
924 spin_unlock(&delayed_refs
->lock
);
926 WARN_ON(num_refs
== 0);
930 *flags
= extent_flags
;
932 btrfs_free_path(path
);
937 * Back reference rules. Back refs have three main goals:
939 * 1) differentiate between all holders of references to an extent so that
940 * when a reference is dropped we can make sure it was a valid reference
941 * before freeing the extent.
943 * 2) Provide enough information to quickly find the holders of an extent
944 * if we notice a given block is corrupted or bad.
946 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
947 * maintenance. This is actually the same as #2, but with a slightly
948 * different use case.
950 * There are two kinds of back refs. The implicit back refs is optimized
951 * for pointers in non-shared tree blocks. For a given pointer in a block,
952 * back refs of this kind provide information about the block's owner tree
953 * and the pointer's key. These information allow us to find the block by
954 * b-tree searching. The full back refs is for pointers in tree blocks not
955 * referenced by their owner trees. The location of tree block is recorded
956 * in the back refs. Actually the full back refs is generic, and can be
957 * used in all cases the implicit back refs is used. The major shortcoming
958 * of the full back refs is its overhead. Every time a tree block gets
959 * COWed, we have to update back refs entry for all pointers in it.
961 * For a newly allocated tree block, we use implicit back refs for
962 * pointers in it. This means most tree related operations only involve
963 * implicit back refs. For a tree block created in old transaction, the
964 * only way to drop a reference to it is COW it. So we can detect the
965 * event that tree block loses its owner tree's reference and do the
966 * back refs conversion.
968 * When a tree block is COW'd through a tree, there are four cases:
970 * The reference count of the block is one and the tree is the block's
971 * owner tree. Nothing to do in this case.
973 * The reference count of the block is one and the tree is not the
974 * block's owner tree. In this case, full back refs is used for pointers
975 * in the block. Remove these full back refs, add implicit back refs for
976 * every pointers in the new block.
978 * The reference count of the block is greater than one and the tree is
979 * the block's owner tree. In this case, implicit back refs is used for
980 * pointers in the block. Add full back refs for every pointers in the
981 * block, increase lower level extents' reference counts. The original
982 * implicit back refs are entailed to the new block.
984 * The reference count of the block is greater than one and the tree is
985 * not the block's owner tree. Add implicit back refs for every pointer in
986 * the new block, increase lower level extents' reference count.
988 * Back Reference Key composing:
990 * The key objectid corresponds to the first byte in the extent,
991 * The key type is used to differentiate between types of back refs.
992 * There are different meanings of the key offset for different types
995 * File extents can be referenced by:
997 * - multiple snapshots, subvolumes, or different generations in one subvol
998 * - different files inside a single subvolume
999 * - different offsets inside a file (bookend extents in file.c)
1001 * The extent ref structure for the implicit back refs has fields for:
1003 * - Objectid of the subvolume root
1004 * - objectid of the file holding the reference
1005 * - original offset in the file
1006 * - how many bookend extents
1008 * The key offset for the implicit back refs is hash of the first
1011 * The extent ref structure for the full back refs has field for:
1013 * - number of pointers in the tree leaf
1015 * The key offset for the implicit back refs is the first byte of
1018 * When a file extent is allocated, The implicit back refs is used.
1019 * the fields are filled in:
1021 * (root_key.objectid, inode objectid, offset in file, 1)
1023 * When a file extent is removed file truncation, we find the
1024 * corresponding implicit back refs and check the following fields:
1026 * (btrfs_header_owner(leaf), inode objectid, offset in file)
1028 * Btree extents can be referenced by:
1030 * - Different subvolumes
1032 * Both the implicit back refs and the full back refs for tree blocks
1033 * only consist of key. The key offset for the implicit back refs is
1034 * objectid of block's owner tree. The key offset for the full back refs
1035 * is the first byte of parent block.
1037 * When implicit back refs is used, information about the lowest key and
1038 * level of the tree block are required. These information are stored in
1039 * tree block info structure.
1042 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1043 static int convert_extent_item_v0(struct btrfs_trans_handle
*trans
,
1044 struct btrfs_root
*root
,
1045 struct btrfs_path
*path
,
1046 u64 owner
, u32 extra_size
)
1048 struct btrfs_extent_item
*item
;
1049 struct btrfs_extent_item_v0
*ei0
;
1050 struct btrfs_extent_ref_v0
*ref0
;
1051 struct btrfs_tree_block_info
*bi
;
1052 struct extent_buffer
*leaf
;
1053 struct btrfs_key key
;
1054 struct btrfs_key found_key
;
1055 u32 new_size
= sizeof(*item
);
1059 leaf
= path
->nodes
[0];
1060 BUG_ON(btrfs_item_size_nr(leaf
, path
->slots
[0]) != sizeof(*ei0
));
1062 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1063 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1064 struct btrfs_extent_item_v0
);
1065 refs
= btrfs_extent_refs_v0(leaf
, ei0
);
1067 if (owner
== (u64
)-1) {
1069 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1070 ret
= btrfs_next_leaf(root
, path
);
1073 BUG_ON(ret
> 0); /* Corruption */
1074 leaf
= path
->nodes
[0];
1076 btrfs_item_key_to_cpu(leaf
, &found_key
,
1078 BUG_ON(key
.objectid
!= found_key
.objectid
);
1079 if (found_key
.type
!= BTRFS_EXTENT_REF_V0_KEY
) {
1083 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1084 struct btrfs_extent_ref_v0
);
1085 owner
= btrfs_ref_objectid_v0(leaf
, ref0
);
1089 btrfs_release_path(path
);
1091 if (owner
< BTRFS_FIRST_FREE_OBJECTID
)
1092 new_size
+= sizeof(*bi
);
1094 new_size
-= sizeof(*ei0
);
1095 ret
= btrfs_search_slot(trans
, root
, &key
, path
,
1096 new_size
+ extra_size
, 1);
1099 BUG_ON(ret
); /* Corruption */
1101 btrfs_extend_item(root
, path
, new_size
);
1103 leaf
= path
->nodes
[0];
1104 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1105 btrfs_set_extent_refs(leaf
, item
, refs
);
1106 /* FIXME: get real generation */
1107 btrfs_set_extent_generation(leaf
, item
, 0);
1108 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1109 btrfs_set_extent_flags(leaf
, item
,
1110 BTRFS_EXTENT_FLAG_TREE_BLOCK
|
1111 BTRFS_BLOCK_FLAG_FULL_BACKREF
);
1112 bi
= (struct btrfs_tree_block_info
*)(item
+ 1);
1113 /* FIXME: get first key of the block */
1114 memset_extent_buffer(leaf
, 0, (unsigned long)bi
, sizeof(*bi
));
1115 btrfs_set_tree_block_level(leaf
, bi
, (int)owner
);
1117 btrfs_set_extent_flags(leaf
, item
, BTRFS_EXTENT_FLAG_DATA
);
1119 btrfs_mark_buffer_dirty(leaf
);
1124 static u64
hash_extent_data_ref(u64 root_objectid
, u64 owner
, u64 offset
)
1126 u32 high_crc
= ~(u32
)0;
1127 u32 low_crc
= ~(u32
)0;
1130 lenum
= cpu_to_le64(root_objectid
);
1131 high_crc
= btrfs_crc32c(high_crc
, &lenum
, sizeof(lenum
));
1132 lenum
= cpu_to_le64(owner
);
1133 low_crc
= btrfs_crc32c(low_crc
, &lenum
, sizeof(lenum
));
1134 lenum
= cpu_to_le64(offset
);
1135 low_crc
= btrfs_crc32c(low_crc
, &lenum
, sizeof(lenum
));
1137 return ((u64
)high_crc
<< 31) ^ (u64
)low_crc
;
1140 static u64
hash_extent_data_ref_item(struct extent_buffer
*leaf
,
1141 struct btrfs_extent_data_ref
*ref
)
1143 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf
, ref
),
1144 btrfs_extent_data_ref_objectid(leaf
, ref
),
1145 btrfs_extent_data_ref_offset(leaf
, ref
));
1148 static int match_extent_data_ref(struct extent_buffer
*leaf
,
1149 struct btrfs_extent_data_ref
*ref
,
1150 u64 root_objectid
, u64 owner
, u64 offset
)
1152 if (btrfs_extent_data_ref_root(leaf
, ref
) != root_objectid
||
1153 btrfs_extent_data_ref_objectid(leaf
, ref
) != owner
||
1154 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
1159 static noinline
int lookup_extent_data_ref(struct btrfs_trans_handle
*trans
,
1160 struct btrfs_root
*root
,
1161 struct btrfs_path
*path
,
1162 u64 bytenr
, u64 parent
,
1164 u64 owner
, u64 offset
)
1166 struct btrfs_key key
;
1167 struct btrfs_extent_data_ref
*ref
;
1168 struct extent_buffer
*leaf
;
1174 key
.objectid
= bytenr
;
1176 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
1177 key
.offset
= parent
;
1179 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
1180 key
.offset
= hash_extent_data_ref(root_objectid
,
1185 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1194 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1195 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
1196 btrfs_release_path(path
);
1197 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1208 leaf
= path
->nodes
[0];
1209 nritems
= btrfs_header_nritems(leaf
);
1211 if (path
->slots
[0] >= nritems
) {
1212 ret
= btrfs_next_leaf(root
, path
);
1218 leaf
= path
->nodes
[0];
1219 nritems
= btrfs_header_nritems(leaf
);
1223 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1224 if (key
.objectid
!= bytenr
||
1225 key
.type
!= BTRFS_EXTENT_DATA_REF_KEY
)
1228 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1229 struct btrfs_extent_data_ref
);
1231 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
1234 btrfs_release_path(path
);
1246 static noinline
int insert_extent_data_ref(struct btrfs_trans_handle
*trans
,
1247 struct btrfs_root
*root
,
1248 struct btrfs_path
*path
,
1249 u64 bytenr
, u64 parent
,
1250 u64 root_objectid
, u64 owner
,
1251 u64 offset
, int refs_to_add
)
1253 struct btrfs_key key
;
1254 struct extent_buffer
*leaf
;
1259 key
.objectid
= bytenr
;
1261 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
1262 key
.offset
= parent
;
1263 size
= sizeof(struct btrfs_shared_data_ref
);
1265 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
1266 key
.offset
= hash_extent_data_ref(root_objectid
,
1268 size
= sizeof(struct btrfs_extent_data_ref
);
1271 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, size
);
1272 if (ret
&& ret
!= -EEXIST
)
1275 leaf
= path
->nodes
[0];
1277 struct btrfs_shared_data_ref
*ref
;
1278 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1279 struct btrfs_shared_data_ref
);
1281 btrfs_set_shared_data_ref_count(leaf
, ref
, refs_to_add
);
1283 num_refs
= btrfs_shared_data_ref_count(leaf
, ref
);
1284 num_refs
+= refs_to_add
;
1285 btrfs_set_shared_data_ref_count(leaf
, ref
, num_refs
);
1288 struct btrfs_extent_data_ref
*ref
;
1289 while (ret
== -EEXIST
) {
1290 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1291 struct btrfs_extent_data_ref
);
1292 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
1295 btrfs_release_path(path
);
1297 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1299 if (ret
&& ret
!= -EEXIST
)
1302 leaf
= path
->nodes
[0];
1304 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1305 struct btrfs_extent_data_ref
);
1307 btrfs_set_extent_data_ref_root(leaf
, ref
,
1309 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
1310 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
1311 btrfs_set_extent_data_ref_count(leaf
, ref
, refs_to_add
);
1313 num_refs
= btrfs_extent_data_ref_count(leaf
, ref
);
1314 num_refs
+= refs_to_add
;
1315 btrfs_set_extent_data_ref_count(leaf
, ref
, num_refs
);
1318 btrfs_mark_buffer_dirty(leaf
);
1321 btrfs_release_path(path
);
1325 static noinline
int remove_extent_data_ref(struct btrfs_trans_handle
*trans
,
1326 struct btrfs_root
*root
,
1327 struct btrfs_path
*path
,
1328 int refs_to_drop
, int *last_ref
)
1330 struct btrfs_key key
;
1331 struct btrfs_extent_data_ref
*ref1
= NULL
;
1332 struct btrfs_shared_data_ref
*ref2
= NULL
;
1333 struct extent_buffer
*leaf
;
1337 leaf
= path
->nodes
[0];
1338 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1340 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1341 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
1342 struct btrfs_extent_data_ref
);
1343 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1344 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
1345 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
1346 struct btrfs_shared_data_ref
);
1347 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1348 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1349 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
1350 struct btrfs_extent_ref_v0
*ref0
;
1351 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1352 struct btrfs_extent_ref_v0
);
1353 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
1359 BUG_ON(num_refs
< refs_to_drop
);
1360 num_refs
-= refs_to_drop
;
1362 if (num_refs
== 0) {
1363 ret
= btrfs_del_item(trans
, root
, path
);
1366 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
)
1367 btrfs_set_extent_data_ref_count(leaf
, ref1
, num_refs
);
1368 else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
)
1369 btrfs_set_shared_data_ref_count(leaf
, ref2
, num_refs
);
1370 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1372 struct btrfs_extent_ref_v0
*ref0
;
1373 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1374 struct btrfs_extent_ref_v0
);
1375 btrfs_set_ref_count_v0(leaf
, ref0
, num_refs
);
1378 btrfs_mark_buffer_dirty(leaf
);
1383 static noinline u32
extent_data_ref_count(struct btrfs_path
*path
,
1384 struct btrfs_extent_inline_ref
*iref
)
1386 struct btrfs_key key
;
1387 struct extent_buffer
*leaf
;
1388 struct btrfs_extent_data_ref
*ref1
;
1389 struct btrfs_shared_data_ref
*ref2
;
1392 leaf
= path
->nodes
[0];
1393 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1395 if (btrfs_extent_inline_ref_type(leaf
, iref
) ==
1396 BTRFS_EXTENT_DATA_REF_KEY
) {
1397 ref1
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1398 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1400 ref2
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1401 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1403 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1404 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
1405 struct btrfs_extent_data_ref
);
1406 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1407 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
1408 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
1409 struct btrfs_shared_data_ref
);
1410 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1411 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1412 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
1413 struct btrfs_extent_ref_v0
*ref0
;
1414 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1415 struct btrfs_extent_ref_v0
);
1416 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
1424 static noinline
int lookup_tree_block_ref(struct btrfs_trans_handle
*trans
,
1425 struct btrfs_root
*root
,
1426 struct btrfs_path
*path
,
1427 u64 bytenr
, u64 parent
,
1430 struct btrfs_key key
;
1433 key
.objectid
= bytenr
;
1435 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1436 key
.offset
= parent
;
1438 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
1439 key
.offset
= root_objectid
;
1442 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1445 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1446 if (ret
== -ENOENT
&& parent
) {
1447 btrfs_release_path(path
);
1448 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
1449 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1457 static noinline
int insert_tree_block_ref(struct btrfs_trans_handle
*trans
,
1458 struct btrfs_root
*root
,
1459 struct btrfs_path
*path
,
1460 u64 bytenr
, u64 parent
,
1463 struct btrfs_key key
;
1466 key
.objectid
= bytenr
;
1468 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1469 key
.offset
= parent
;
1471 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
1472 key
.offset
= root_objectid
;
1475 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, 0);
1476 btrfs_release_path(path
);
1480 static inline int extent_ref_type(u64 parent
, u64 owner
)
1483 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1485 type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1487 type
= BTRFS_TREE_BLOCK_REF_KEY
;
1490 type
= BTRFS_SHARED_DATA_REF_KEY
;
1492 type
= BTRFS_EXTENT_DATA_REF_KEY
;
1497 static int find_next_key(struct btrfs_path
*path
, int level
,
1498 struct btrfs_key
*key
)
1501 for (; level
< BTRFS_MAX_LEVEL
; level
++) {
1502 if (!path
->nodes
[level
])
1504 if (path
->slots
[level
] + 1 >=
1505 btrfs_header_nritems(path
->nodes
[level
]))
1508 btrfs_item_key_to_cpu(path
->nodes
[level
], key
,
1509 path
->slots
[level
] + 1);
1511 btrfs_node_key_to_cpu(path
->nodes
[level
], key
,
1512 path
->slots
[level
] + 1);
1519 * look for inline back ref. if back ref is found, *ref_ret is set
1520 * to the address of inline back ref, and 0 is returned.
1522 * if back ref isn't found, *ref_ret is set to the address where it
1523 * should be inserted, and -ENOENT is returned.
1525 * if insert is true and there are too many inline back refs, the path
1526 * points to the extent item, and -EAGAIN is returned.
1528 * NOTE: inline back refs are ordered in the same way that back ref
1529 * items in the tree are ordered.
1531 static noinline_for_stack
1532 int lookup_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1533 struct btrfs_root
*root
,
1534 struct btrfs_path
*path
,
1535 struct btrfs_extent_inline_ref
**ref_ret
,
1536 u64 bytenr
, u64 num_bytes
,
1537 u64 parent
, u64 root_objectid
,
1538 u64 owner
, u64 offset
, int insert
)
1540 struct btrfs_key key
;
1541 struct extent_buffer
*leaf
;
1542 struct btrfs_extent_item
*ei
;
1543 struct btrfs_extent_inline_ref
*iref
;
1553 bool skinny_metadata
= btrfs_fs_incompat(root
->fs_info
,
1556 key
.objectid
= bytenr
;
1557 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1558 key
.offset
= num_bytes
;
1560 want
= extent_ref_type(parent
, owner
);
1562 extra_size
= btrfs_extent_inline_ref_size(want
);
1563 path
->keep_locks
= 1;
1568 * Owner is our parent level, so we can just add one to get the level
1569 * for the block we are interested in.
1571 if (skinny_metadata
&& owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1572 key
.type
= BTRFS_METADATA_ITEM_KEY
;
1577 ret
= btrfs_search_slot(trans
, root
, &key
, path
, extra_size
, 1);
1584 * We may be a newly converted file system which still has the old fat
1585 * extent entries for metadata, so try and see if we have one of those.
1587 if (ret
> 0 && skinny_metadata
) {
1588 skinny_metadata
= false;
1589 if (path
->slots
[0]) {
1591 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
1593 if (key
.objectid
== bytenr
&&
1594 key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
1595 key
.offset
== num_bytes
)
1599 key
.objectid
= bytenr
;
1600 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1601 key
.offset
= num_bytes
;
1602 btrfs_release_path(path
);
1607 if (ret
&& !insert
) {
1610 } else if (WARN_ON(ret
)) {
1615 leaf
= path
->nodes
[0];
1616 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1617 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1618 if (item_size
< sizeof(*ei
)) {
1623 ret
= convert_extent_item_v0(trans
, root
, path
, owner
,
1629 leaf
= path
->nodes
[0];
1630 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1633 BUG_ON(item_size
< sizeof(*ei
));
1635 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1636 flags
= btrfs_extent_flags(leaf
, ei
);
1638 ptr
= (unsigned long)(ei
+ 1);
1639 end
= (unsigned long)ei
+ item_size
;
1641 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
&& !skinny_metadata
) {
1642 ptr
+= sizeof(struct btrfs_tree_block_info
);
1652 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1653 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1657 ptr
+= btrfs_extent_inline_ref_size(type
);
1661 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1662 struct btrfs_extent_data_ref
*dref
;
1663 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1664 if (match_extent_data_ref(leaf
, dref
, root_objectid
,
1669 if (hash_extent_data_ref_item(leaf
, dref
) <
1670 hash_extent_data_ref(root_objectid
, owner
, offset
))
1674 ref_offset
= btrfs_extent_inline_ref_offset(leaf
, iref
);
1676 if (parent
== ref_offset
) {
1680 if (ref_offset
< parent
)
1683 if (root_objectid
== ref_offset
) {
1687 if (ref_offset
< root_objectid
)
1691 ptr
+= btrfs_extent_inline_ref_size(type
);
1693 if (err
== -ENOENT
&& insert
) {
1694 if (item_size
+ extra_size
>=
1695 BTRFS_MAX_EXTENT_ITEM_SIZE(root
)) {
1700 * To add new inline back ref, we have to make sure
1701 * there is no corresponding back ref item.
1702 * For simplicity, we just do not add new inline back
1703 * ref if there is any kind of item for this block
1705 if (find_next_key(path
, 0, &key
) == 0 &&
1706 key
.objectid
== bytenr
&&
1707 key
.type
< BTRFS_BLOCK_GROUP_ITEM_KEY
) {
1712 *ref_ret
= (struct btrfs_extent_inline_ref
*)ptr
;
1715 path
->keep_locks
= 0;
1716 btrfs_unlock_up_safe(path
, 1);
1722 * helper to add new inline back ref
1724 static noinline_for_stack
1725 void setup_inline_extent_backref(struct btrfs_root
*root
,
1726 struct btrfs_path
*path
,
1727 struct btrfs_extent_inline_ref
*iref
,
1728 u64 parent
, u64 root_objectid
,
1729 u64 owner
, u64 offset
, int refs_to_add
,
1730 struct btrfs_delayed_extent_op
*extent_op
)
1732 struct extent_buffer
*leaf
;
1733 struct btrfs_extent_item
*ei
;
1736 unsigned long item_offset
;
1741 leaf
= path
->nodes
[0];
1742 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1743 item_offset
= (unsigned long)iref
- (unsigned long)ei
;
1745 type
= extent_ref_type(parent
, owner
);
1746 size
= btrfs_extent_inline_ref_size(type
);
1748 btrfs_extend_item(root
, path
, size
);
1750 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1751 refs
= btrfs_extent_refs(leaf
, ei
);
1752 refs
+= refs_to_add
;
1753 btrfs_set_extent_refs(leaf
, ei
, refs
);
1755 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1757 ptr
= (unsigned long)ei
+ item_offset
;
1758 end
= (unsigned long)ei
+ btrfs_item_size_nr(leaf
, path
->slots
[0]);
1759 if (ptr
< end
- size
)
1760 memmove_extent_buffer(leaf
, ptr
+ size
, ptr
,
1763 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1764 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
1765 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1766 struct btrfs_extent_data_ref
*dref
;
1767 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1768 btrfs_set_extent_data_ref_root(leaf
, dref
, root_objectid
);
1769 btrfs_set_extent_data_ref_objectid(leaf
, dref
, owner
);
1770 btrfs_set_extent_data_ref_offset(leaf
, dref
, offset
);
1771 btrfs_set_extent_data_ref_count(leaf
, dref
, refs_to_add
);
1772 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1773 struct btrfs_shared_data_ref
*sref
;
1774 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1775 btrfs_set_shared_data_ref_count(leaf
, sref
, refs_to_add
);
1776 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1777 } else if (type
== BTRFS_SHARED_BLOCK_REF_KEY
) {
1778 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1780 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
1782 btrfs_mark_buffer_dirty(leaf
);
1785 static int lookup_extent_backref(struct btrfs_trans_handle
*trans
,
1786 struct btrfs_root
*root
,
1787 struct btrfs_path
*path
,
1788 struct btrfs_extent_inline_ref
**ref_ret
,
1789 u64 bytenr
, u64 num_bytes
, u64 parent
,
1790 u64 root_objectid
, u64 owner
, u64 offset
)
1794 ret
= lookup_inline_extent_backref(trans
, root
, path
, ref_ret
,
1795 bytenr
, num_bytes
, parent
,
1796 root_objectid
, owner
, offset
, 0);
1800 btrfs_release_path(path
);
1803 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1804 ret
= lookup_tree_block_ref(trans
, root
, path
, bytenr
, parent
,
1807 ret
= lookup_extent_data_ref(trans
, root
, path
, bytenr
, parent
,
1808 root_objectid
, owner
, offset
);
1814 * helper to update/remove inline back ref
1816 static noinline_for_stack
1817 void update_inline_extent_backref(struct btrfs_root
*root
,
1818 struct btrfs_path
*path
,
1819 struct btrfs_extent_inline_ref
*iref
,
1821 struct btrfs_delayed_extent_op
*extent_op
,
1824 struct extent_buffer
*leaf
;
1825 struct btrfs_extent_item
*ei
;
1826 struct btrfs_extent_data_ref
*dref
= NULL
;
1827 struct btrfs_shared_data_ref
*sref
= NULL
;
1835 leaf
= path
->nodes
[0];
1836 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1837 refs
= btrfs_extent_refs(leaf
, ei
);
1838 WARN_ON(refs_to_mod
< 0 && refs
+ refs_to_mod
<= 0);
1839 refs
+= refs_to_mod
;
1840 btrfs_set_extent_refs(leaf
, ei
, refs
);
1842 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1844 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1846 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1847 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1848 refs
= btrfs_extent_data_ref_count(leaf
, dref
);
1849 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1850 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1851 refs
= btrfs_shared_data_ref_count(leaf
, sref
);
1854 BUG_ON(refs_to_mod
!= -1);
1857 BUG_ON(refs_to_mod
< 0 && refs
< -refs_to_mod
);
1858 refs
+= refs_to_mod
;
1861 if (type
== BTRFS_EXTENT_DATA_REF_KEY
)
1862 btrfs_set_extent_data_ref_count(leaf
, dref
, refs
);
1864 btrfs_set_shared_data_ref_count(leaf
, sref
, refs
);
1867 size
= btrfs_extent_inline_ref_size(type
);
1868 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1869 ptr
= (unsigned long)iref
;
1870 end
= (unsigned long)ei
+ item_size
;
1871 if (ptr
+ size
< end
)
1872 memmove_extent_buffer(leaf
, ptr
, ptr
+ size
,
1875 btrfs_truncate_item(root
, path
, item_size
, 1);
1877 btrfs_mark_buffer_dirty(leaf
);
1880 static noinline_for_stack
1881 int insert_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1882 struct btrfs_root
*root
,
1883 struct btrfs_path
*path
,
1884 u64 bytenr
, u64 num_bytes
, u64 parent
,
1885 u64 root_objectid
, u64 owner
,
1886 u64 offset
, int refs_to_add
,
1887 struct btrfs_delayed_extent_op
*extent_op
)
1889 struct btrfs_extent_inline_ref
*iref
;
1892 ret
= lookup_inline_extent_backref(trans
, root
, path
, &iref
,
1893 bytenr
, num_bytes
, parent
,
1894 root_objectid
, owner
, offset
, 1);
1896 BUG_ON(owner
< BTRFS_FIRST_FREE_OBJECTID
);
1897 update_inline_extent_backref(root
, path
, iref
,
1898 refs_to_add
, extent_op
, NULL
);
1899 } else if (ret
== -ENOENT
) {
1900 setup_inline_extent_backref(root
, path
, iref
, parent
,
1901 root_objectid
, owner
, offset
,
1902 refs_to_add
, extent_op
);
1908 static int insert_extent_backref(struct btrfs_trans_handle
*trans
,
1909 struct btrfs_root
*root
,
1910 struct btrfs_path
*path
,
1911 u64 bytenr
, u64 parent
, u64 root_objectid
,
1912 u64 owner
, u64 offset
, int refs_to_add
)
1915 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1916 BUG_ON(refs_to_add
!= 1);
1917 ret
= insert_tree_block_ref(trans
, root
, path
, bytenr
,
1918 parent
, root_objectid
);
1920 ret
= insert_extent_data_ref(trans
, root
, path
, bytenr
,
1921 parent
, root_objectid
,
1922 owner
, offset
, refs_to_add
);
1927 static int remove_extent_backref(struct btrfs_trans_handle
*trans
,
1928 struct btrfs_root
*root
,
1929 struct btrfs_path
*path
,
1930 struct btrfs_extent_inline_ref
*iref
,
1931 int refs_to_drop
, int is_data
, int *last_ref
)
1935 BUG_ON(!is_data
&& refs_to_drop
!= 1);
1937 update_inline_extent_backref(root
, path
, iref
,
1938 -refs_to_drop
, NULL
, last_ref
);
1939 } else if (is_data
) {
1940 ret
= remove_extent_data_ref(trans
, root
, path
, refs_to_drop
,
1944 ret
= btrfs_del_item(trans
, root
, path
);
1949 #define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
1950 static int btrfs_issue_discard(struct block_device
*bdev
, u64 start
, u64 len
,
1951 u64
*discarded_bytes
)
1954 u64 bytes_left
, end
;
1955 u64 aligned_start
= ALIGN(start
, 1 << 9);
1957 if (WARN_ON(start
!= aligned_start
)) {
1958 len
-= aligned_start
- start
;
1959 len
= round_down(len
, 1 << 9);
1960 start
= aligned_start
;
1963 *discarded_bytes
= 0;
1971 /* Skip any superblocks on this device. */
1972 for (j
= 0; j
< BTRFS_SUPER_MIRROR_MAX
; j
++) {
1973 u64 sb_start
= btrfs_sb_offset(j
);
1974 u64 sb_end
= sb_start
+ BTRFS_SUPER_INFO_SIZE
;
1975 u64 size
= sb_start
- start
;
1977 if (!in_range(sb_start
, start
, bytes_left
) &&
1978 !in_range(sb_end
, start
, bytes_left
) &&
1979 !in_range(start
, sb_start
, BTRFS_SUPER_INFO_SIZE
))
1983 * Superblock spans beginning of range. Adjust start and
1986 if (sb_start
<= start
) {
1987 start
+= sb_end
- start
;
1992 bytes_left
= end
- start
;
1997 ret
= blkdev_issue_discard(bdev
, start
>> 9, size
>> 9,
2000 *discarded_bytes
+= size
;
2001 else if (ret
!= -EOPNOTSUPP
)
2010 bytes_left
= end
- start
;
2014 ret
= blkdev_issue_discard(bdev
, start
>> 9, bytes_left
>> 9,
2017 *discarded_bytes
+= bytes_left
;
2022 int btrfs_discard_extent(struct btrfs_root
*root
, u64 bytenr
,
2023 u64 num_bytes
, u64
*actual_bytes
)
2026 u64 discarded_bytes
= 0;
2027 struct btrfs_bio
*bbio
= NULL
;
2030 /* Tell the block device(s) that the sectors can be discarded */
2031 ret
= btrfs_map_block(root
->fs_info
, REQ_DISCARD
,
2032 bytenr
, &num_bytes
, &bbio
, 0);
2033 /* Error condition is -ENOMEM */
2035 struct btrfs_bio_stripe
*stripe
= bbio
->stripes
;
2039 for (i
= 0; i
< bbio
->num_stripes
; i
++, stripe
++) {
2041 if (!stripe
->dev
->can_discard
)
2044 ret
= btrfs_issue_discard(stripe
->dev
->bdev
,
2049 discarded_bytes
+= bytes
;
2050 else if (ret
!= -EOPNOTSUPP
)
2051 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
2054 * Just in case we get back EOPNOTSUPP for some reason,
2055 * just ignore the return value so we don't screw up
2056 * people calling discard_extent.
2060 btrfs_put_bbio(bbio
);
2064 *actual_bytes
= discarded_bytes
;
2067 if (ret
== -EOPNOTSUPP
)
2072 /* Can return -ENOMEM */
2073 int btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
2074 struct btrfs_root
*root
,
2075 u64 bytenr
, u64 num_bytes
, u64 parent
,
2076 u64 root_objectid
, u64 owner
, u64 offset
,
2080 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2082 BUG_ON(owner
< BTRFS_FIRST_FREE_OBJECTID
&&
2083 root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
2085 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
2086 ret
= btrfs_add_delayed_tree_ref(fs_info
, trans
, bytenr
,
2088 parent
, root_objectid
, (int)owner
,
2089 BTRFS_ADD_DELAYED_REF
, NULL
, no_quota
);
2091 ret
= btrfs_add_delayed_data_ref(fs_info
, trans
, bytenr
,
2093 parent
, root_objectid
, owner
, offset
,
2094 BTRFS_ADD_DELAYED_REF
, NULL
, no_quota
);
2099 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
2100 struct btrfs_root
*root
,
2101 struct btrfs_delayed_ref_node
*node
,
2102 u64 parent
, u64 root_objectid
,
2103 u64 owner
, u64 offset
, int refs_to_add
,
2104 struct btrfs_delayed_extent_op
*extent_op
)
2106 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2107 struct btrfs_path
*path
;
2108 struct extent_buffer
*leaf
;
2109 struct btrfs_extent_item
*item
;
2110 struct btrfs_key key
;
2111 u64 bytenr
= node
->bytenr
;
2112 u64 num_bytes
= node
->num_bytes
;
2115 int no_quota
= node
->no_quota
;
2117 path
= btrfs_alloc_path();
2121 if (!is_fstree(root_objectid
) || !root
->fs_info
->quota_enabled
)
2125 path
->leave_spinning
= 1;
2126 /* this will setup the path even if it fails to insert the back ref */
2127 ret
= insert_inline_extent_backref(trans
, fs_info
->extent_root
, path
,
2128 bytenr
, num_bytes
, parent
,
2129 root_objectid
, owner
, offset
,
2130 refs_to_add
, extent_op
);
2131 if ((ret
< 0 && ret
!= -EAGAIN
) || !ret
)
2135 * Ok we had -EAGAIN which means we didn't have space to insert and
2136 * inline extent ref, so just update the reference count and add a
2139 leaf
= path
->nodes
[0];
2140 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2141 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
2142 refs
= btrfs_extent_refs(leaf
, item
);
2143 btrfs_set_extent_refs(leaf
, item
, refs
+ refs_to_add
);
2145 __run_delayed_extent_op(extent_op
, leaf
, item
);
2147 btrfs_mark_buffer_dirty(leaf
);
2148 btrfs_release_path(path
);
2151 path
->leave_spinning
= 1;
2152 /* now insert the actual backref */
2153 ret
= insert_extent_backref(trans
, root
->fs_info
->extent_root
,
2154 path
, bytenr
, parent
, root_objectid
,
2155 owner
, offset
, refs_to_add
);
2157 btrfs_abort_transaction(trans
, root
, ret
);
2159 btrfs_free_path(path
);
2163 static int run_delayed_data_ref(struct btrfs_trans_handle
*trans
,
2164 struct btrfs_root
*root
,
2165 struct btrfs_delayed_ref_node
*node
,
2166 struct btrfs_delayed_extent_op
*extent_op
,
2167 int insert_reserved
)
2170 struct btrfs_delayed_data_ref
*ref
;
2171 struct btrfs_key ins
;
2176 ins
.objectid
= node
->bytenr
;
2177 ins
.offset
= node
->num_bytes
;
2178 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
2180 ref
= btrfs_delayed_node_to_data_ref(node
);
2181 trace_run_delayed_data_ref(node
, ref
, node
->action
);
2183 if (node
->type
== BTRFS_SHARED_DATA_REF_KEY
)
2184 parent
= ref
->parent
;
2185 ref_root
= ref
->root
;
2187 if (node
->action
== BTRFS_ADD_DELAYED_REF
&& insert_reserved
) {
2189 flags
|= extent_op
->flags_to_set
;
2190 ret
= alloc_reserved_file_extent(trans
, root
,
2191 parent
, ref_root
, flags
,
2192 ref
->objectid
, ref
->offset
,
2193 &ins
, node
->ref_mod
);
2194 } else if (node
->action
== BTRFS_ADD_DELAYED_REF
) {
2195 ret
= __btrfs_inc_extent_ref(trans
, root
, node
, parent
,
2196 ref_root
, ref
->objectid
,
2197 ref
->offset
, node
->ref_mod
,
2199 } else if (node
->action
== BTRFS_DROP_DELAYED_REF
) {
2200 ret
= __btrfs_free_extent(trans
, root
, node
, parent
,
2201 ref_root
, ref
->objectid
,
2202 ref
->offset
, node
->ref_mod
,
2210 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op
*extent_op
,
2211 struct extent_buffer
*leaf
,
2212 struct btrfs_extent_item
*ei
)
2214 u64 flags
= btrfs_extent_flags(leaf
, ei
);
2215 if (extent_op
->update_flags
) {
2216 flags
|= extent_op
->flags_to_set
;
2217 btrfs_set_extent_flags(leaf
, ei
, flags
);
2220 if (extent_op
->update_key
) {
2221 struct btrfs_tree_block_info
*bi
;
2222 BUG_ON(!(flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
));
2223 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
2224 btrfs_set_tree_block_key(leaf
, bi
, &extent_op
->key
);
2228 static int run_delayed_extent_op(struct btrfs_trans_handle
*trans
,
2229 struct btrfs_root
*root
,
2230 struct btrfs_delayed_ref_node
*node
,
2231 struct btrfs_delayed_extent_op
*extent_op
)
2233 struct btrfs_key key
;
2234 struct btrfs_path
*path
;
2235 struct btrfs_extent_item
*ei
;
2236 struct extent_buffer
*leaf
;
2240 int metadata
= !extent_op
->is_data
;
2245 if (metadata
&& !btrfs_fs_incompat(root
->fs_info
, SKINNY_METADATA
))
2248 path
= btrfs_alloc_path();
2252 key
.objectid
= node
->bytenr
;
2255 key
.type
= BTRFS_METADATA_ITEM_KEY
;
2256 key
.offset
= extent_op
->level
;
2258 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2259 key
.offset
= node
->num_bytes
;
2264 path
->leave_spinning
= 1;
2265 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
,
2273 if (path
->slots
[0] > 0) {
2275 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
2277 if (key
.objectid
== node
->bytenr
&&
2278 key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
2279 key
.offset
== node
->num_bytes
)
2283 btrfs_release_path(path
);
2286 key
.objectid
= node
->bytenr
;
2287 key
.offset
= node
->num_bytes
;
2288 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2297 leaf
= path
->nodes
[0];
2298 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2299 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2300 if (item_size
< sizeof(*ei
)) {
2301 ret
= convert_extent_item_v0(trans
, root
->fs_info
->extent_root
,
2307 leaf
= path
->nodes
[0];
2308 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2311 BUG_ON(item_size
< sizeof(*ei
));
2312 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
2313 __run_delayed_extent_op(extent_op
, leaf
, ei
);
2315 btrfs_mark_buffer_dirty(leaf
);
2317 btrfs_free_path(path
);
2321 static int run_delayed_tree_ref(struct btrfs_trans_handle
*trans
,
2322 struct btrfs_root
*root
,
2323 struct btrfs_delayed_ref_node
*node
,
2324 struct btrfs_delayed_extent_op
*extent_op
,
2325 int insert_reserved
)
2328 struct btrfs_delayed_tree_ref
*ref
;
2329 struct btrfs_key ins
;
2332 bool skinny_metadata
= btrfs_fs_incompat(root
->fs_info
,
2335 ref
= btrfs_delayed_node_to_tree_ref(node
);
2336 trace_run_delayed_tree_ref(node
, ref
, node
->action
);
2338 if (node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
)
2339 parent
= ref
->parent
;
2340 ref_root
= ref
->root
;
2342 ins
.objectid
= node
->bytenr
;
2343 if (skinny_metadata
) {
2344 ins
.offset
= ref
->level
;
2345 ins
.type
= BTRFS_METADATA_ITEM_KEY
;
2347 ins
.offset
= node
->num_bytes
;
2348 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
2351 BUG_ON(node
->ref_mod
!= 1);
2352 if (node
->action
== BTRFS_ADD_DELAYED_REF
&& insert_reserved
) {
2353 BUG_ON(!extent_op
|| !extent_op
->update_flags
);
2354 ret
= alloc_reserved_tree_block(trans
, root
,
2356 extent_op
->flags_to_set
,
2360 } else if (node
->action
== BTRFS_ADD_DELAYED_REF
) {
2361 ret
= __btrfs_inc_extent_ref(trans
, root
, node
,
2365 } else if (node
->action
== BTRFS_DROP_DELAYED_REF
) {
2366 ret
= __btrfs_free_extent(trans
, root
, node
,
2368 ref
->level
, 0, 1, extent_op
);
2375 /* helper function to actually process a single delayed ref entry */
2376 static int run_one_delayed_ref(struct btrfs_trans_handle
*trans
,
2377 struct btrfs_root
*root
,
2378 struct btrfs_delayed_ref_node
*node
,
2379 struct btrfs_delayed_extent_op
*extent_op
,
2380 int insert_reserved
)
2384 if (trans
->aborted
) {
2385 if (insert_reserved
)
2386 btrfs_pin_extent(root
, node
->bytenr
,
2387 node
->num_bytes
, 1);
2391 if (btrfs_delayed_ref_is_head(node
)) {
2392 struct btrfs_delayed_ref_head
*head
;
2394 * we've hit the end of the chain and we were supposed
2395 * to insert this extent into the tree. But, it got
2396 * deleted before we ever needed to insert it, so all
2397 * we have to do is clean up the accounting
2400 head
= btrfs_delayed_node_to_head(node
);
2401 trace_run_delayed_ref_head(node
, head
, node
->action
);
2403 if (insert_reserved
) {
2404 btrfs_pin_extent(root
, node
->bytenr
,
2405 node
->num_bytes
, 1);
2406 if (head
->is_data
) {
2407 ret
= btrfs_del_csums(trans
, root
,
2415 if (node
->type
== BTRFS_TREE_BLOCK_REF_KEY
||
2416 node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
)
2417 ret
= run_delayed_tree_ref(trans
, root
, node
, extent_op
,
2419 else if (node
->type
== BTRFS_EXTENT_DATA_REF_KEY
||
2420 node
->type
== BTRFS_SHARED_DATA_REF_KEY
)
2421 ret
= run_delayed_data_ref(trans
, root
, node
, extent_op
,
2428 static inline struct btrfs_delayed_ref_node
*
2429 select_delayed_ref(struct btrfs_delayed_ref_head
*head
)
2431 struct btrfs_delayed_ref_node
*ref
;
2433 if (list_empty(&head
->ref_list
))
2437 * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
2438 * This is to prevent a ref count from going down to zero, which deletes
2439 * the extent item from the extent tree, when there still are references
2440 * to add, which would fail because they would not find the extent item.
2442 list_for_each_entry(ref
, &head
->ref_list
, list
) {
2443 if (ref
->action
== BTRFS_ADD_DELAYED_REF
)
2447 return list_entry(head
->ref_list
.next
, struct btrfs_delayed_ref_node
,
2452 * Returns 0 on success or if called with an already aborted transaction.
2453 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2455 static noinline
int __btrfs_run_delayed_refs(struct btrfs_trans_handle
*trans
,
2456 struct btrfs_root
*root
,
2459 struct btrfs_delayed_ref_root
*delayed_refs
;
2460 struct btrfs_delayed_ref_node
*ref
;
2461 struct btrfs_delayed_ref_head
*locked_ref
= NULL
;
2462 struct btrfs_delayed_extent_op
*extent_op
;
2463 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2464 ktime_t start
= ktime_get();
2466 unsigned long count
= 0;
2467 unsigned long actual_count
= 0;
2468 int must_insert_reserved
= 0;
2470 delayed_refs
= &trans
->transaction
->delayed_refs
;
2476 spin_lock(&delayed_refs
->lock
);
2477 locked_ref
= btrfs_select_ref_head(trans
);
2479 spin_unlock(&delayed_refs
->lock
);
2483 /* grab the lock that says we are going to process
2484 * all the refs for this head */
2485 ret
= btrfs_delayed_ref_lock(trans
, locked_ref
);
2486 spin_unlock(&delayed_refs
->lock
);
2488 * we may have dropped the spin lock to get the head
2489 * mutex lock, and that might have given someone else
2490 * time to free the head. If that's true, it has been
2491 * removed from our list and we can move on.
2493 if (ret
== -EAGAIN
) {
2500 spin_lock(&locked_ref
->lock
);
2503 * locked_ref is the head node, so we have to go one
2504 * node back for any delayed ref updates
2506 ref
= select_delayed_ref(locked_ref
);
2508 if (ref
&& ref
->seq
&&
2509 btrfs_check_delayed_seq(fs_info
, delayed_refs
, ref
->seq
)) {
2510 spin_unlock(&locked_ref
->lock
);
2511 btrfs_delayed_ref_unlock(locked_ref
);
2512 spin_lock(&delayed_refs
->lock
);
2513 locked_ref
->processing
= 0;
2514 delayed_refs
->num_heads_ready
++;
2515 spin_unlock(&delayed_refs
->lock
);
2523 * record the must insert reserved flag before we
2524 * drop the spin lock.
2526 must_insert_reserved
= locked_ref
->must_insert_reserved
;
2527 locked_ref
->must_insert_reserved
= 0;
2529 extent_op
= locked_ref
->extent_op
;
2530 locked_ref
->extent_op
= NULL
;
2535 /* All delayed refs have been processed, Go ahead
2536 * and send the head node to run_one_delayed_ref,
2537 * so that any accounting fixes can happen
2539 ref
= &locked_ref
->node
;
2541 if (extent_op
&& must_insert_reserved
) {
2542 btrfs_free_delayed_extent_op(extent_op
);
2547 spin_unlock(&locked_ref
->lock
);
2548 ret
= run_delayed_extent_op(trans
, root
,
2550 btrfs_free_delayed_extent_op(extent_op
);
2554 * Need to reset must_insert_reserved if
2555 * there was an error so the abort stuff
2556 * can cleanup the reserved space
2559 if (must_insert_reserved
)
2560 locked_ref
->must_insert_reserved
= 1;
2561 locked_ref
->processing
= 0;
2562 btrfs_debug(fs_info
, "run_delayed_extent_op returned %d", ret
);
2563 btrfs_delayed_ref_unlock(locked_ref
);
2570 * Need to drop our head ref lock and re-aqcuire the
2571 * delayed ref lock and then re-check to make sure
2574 spin_unlock(&locked_ref
->lock
);
2575 spin_lock(&delayed_refs
->lock
);
2576 spin_lock(&locked_ref
->lock
);
2577 if (!list_empty(&locked_ref
->ref_list
) ||
2578 locked_ref
->extent_op
) {
2579 spin_unlock(&locked_ref
->lock
);
2580 spin_unlock(&delayed_refs
->lock
);
2584 delayed_refs
->num_heads
--;
2585 rb_erase(&locked_ref
->href_node
,
2586 &delayed_refs
->href_root
);
2587 spin_unlock(&delayed_refs
->lock
);
2591 list_del(&ref
->list
);
2593 atomic_dec(&delayed_refs
->num_entries
);
2595 if (!btrfs_delayed_ref_is_head(ref
)) {
2597 * when we play the delayed ref, also correct the
2600 switch (ref
->action
) {
2601 case BTRFS_ADD_DELAYED_REF
:
2602 case BTRFS_ADD_DELAYED_EXTENT
:
2603 locked_ref
->node
.ref_mod
-= ref
->ref_mod
;
2605 case BTRFS_DROP_DELAYED_REF
:
2606 locked_ref
->node
.ref_mod
+= ref
->ref_mod
;
2612 spin_unlock(&locked_ref
->lock
);
2614 ret
= run_one_delayed_ref(trans
, root
, ref
, extent_op
,
2615 must_insert_reserved
);
2617 btrfs_free_delayed_extent_op(extent_op
);
2619 locked_ref
->processing
= 0;
2620 btrfs_delayed_ref_unlock(locked_ref
);
2621 btrfs_put_delayed_ref(ref
);
2622 btrfs_debug(fs_info
, "run_one_delayed_ref returned %d", ret
);
2627 * If this node is a head, that means all the refs in this head
2628 * have been dealt with, and we will pick the next head to deal
2629 * with, so we must unlock the head and drop it from the cluster
2630 * list before we release it.
2632 if (btrfs_delayed_ref_is_head(ref
)) {
2633 if (locked_ref
->is_data
&&
2634 locked_ref
->total_ref_mod
< 0) {
2635 spin_lock(&delayed_refs
->lock
);
2636 delayed_refs
->pending_csums
-= ref
->num_bytes
;
2637 spin_unlock(&delayed_refs
->lock
);
2639 btrfs_delayed_ref_unlock(locked_ref
);
2642 btrfs_put_delayed_ref(ref
);
2648 * We don't want to include ref heads since we can have empty ref heads
2649 * and those will drastically skew our runtime down since we just do
2650 * accounting, no actual extent tree updates.
2652 if (actual_count
> 0) {
2653 u64 runtime
= ktime_to_ns(ktime_sub(ktime_get(), start
));
2657 * We weigh the current average higher than our current runtime
2658 * to avoid large swings in the average.
2660 spin_lock(&delayed_refs
->lock
);
2661 avg
= fs_info
->avg_delayed_ref_runtime
* 3 + runtime
;
2662 fs_info
->avg_delayed_ref_runtime
= avg
>> 2; /* div by 4 */
2663 spin_unlock(&delayed_refs
->lock
);
2668 #ifdef SCRAMBLE_DELAYED_REFS
2670 * Normally delayed refs get processed in ascending bytenr order. This
2671 * correlates in most cases to the order added. To expose dependencies on this
2672 * order, we start to process the tree in the middle instead of the beginning
2674 static u64
find_middle(struct rb_root
*root
)
2676 struct rb_node
*n
= root
->rb_node
;
2677 struct btrfs_delayed_ref_node
*entry
;
2680 u64 first
= 0, last
= 0;
2684 entry
= rb_entry(n
, struct btrfs_delayed_ref_node
, rb_node
);
2685 first
= entry
->bytenr
;
2689 entry
= rb_entry(n
, struct btrfs_delayed_ref_node
, rb_node
);
2690 last
= entry
->bytenr
;
2695 entry
= rb_entry(n
, struct btrfs_delayed_ref_node
, rb_node
);
2696 WARN_ON(!entry
->in_tree
);
2698 middle
= entry
->bytenr
;
2711 static inline u64
heads_to_leaves(struct btrfs_root
*root
, u64 heads
)
2715 num_bytes
= heads
* (sizeof(struct btrfs_extent_item
) +
2716 sizeof(struct btrfs_extent_inline_ref
));
2717 if (!btrfs_fs_incompat(root
->fs_info
, SKINNY_METADATA
))
2718 num_bytes
+= heads
* sizeof(struct btrfs_tree_block_info
);
2721 * We don't ever fill up leaves all the way so multiply by 2 just to be
2722 * closer to what we're really going to want to ouse.
2724 return div_u64(num_bytes
, BTRFS_LEAF_DATA_SIZE(root
));
2728 * Takes the number of bytes to be csumm'ed and figures out how many leaves it
2729 * would require to store the csums for that many bytes.
2731 u64
btrfs_csum_bytes_to_leaves(struct btrfs_root
*root
, u64 csum_bytes
)
2734 u64 num_csums_per_leaf
;
2737 csum_size
= BTRFS_LEAF_DATA_SIZE(root
) - sizeof(struct btrfs_item
);
2738 num_csums_per_leaf
= div64_u64(csum_size
,
2739 (u64
)btrfs_super_csum_size(root
->fs_info
->super_copy
));
2740 num_csums
= div64_u64(csum_bytes
, root
->sectorsize
);
2741 num_csums
+= num_csums_per_leaf
- 1;
2742 num_csums
= div64_u64(num_csums
, num_csums_per_leaf
);
2746 int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle
*trans
,
2747 struct btrfs_root
*root
)
2749 struct btrfs_block_rsv
*global_rsv
;
2750 u64 num_heads
= trans
->transaction
->delayed_refs
.num_heads_ready
;
2751 u64 csum_bytes
= trans
->transaction
->delayed_refs
.pending_csums
;
2752 u64 num_dirty_bgs
= trans
->transaction
->num_dirty_bgs
;
2753 u64 num_bytes
, num_dirty_bgs_bytes
;
2756 num_bytes
= btrfs_calc_trans_metadata_size(root
, 1);
2757 num_heads
= heads_to_leaves(root
, num_heads
);
2759 num_bytes
+= (num_heads
- 1) * root
->nodesize
;
2761 num_bytes
+= btrfs_csum_bytes_to_leaves(root
, csum_bytes
) * root
->nodesize
;
2762 num_dirty_bgs_bytes
= btrfs_calc_trans_metadata_size(root
,
2764 global_rsv
= &root
->fs_info
->global_block_rsv
;
2767 * If we can't allocate any more chunks lets make sure we have _lots_ of
2768 * wiggle room since running delayed refs can create more delayed refs.
2770 if (global_rsv
->space_info
->full
) {
2771 num_dirty_bgs_bytes
<<= 1;
2775 spin_lock(&global_rsv
->lock
);
2776 if (global_rsv
->reserved
<= num_bytes
+ num_dirty_bgs_bytes
)
2778 spin_unlock(&global_rsv
->lock
);
2782 int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle
*trans
,
2783 struct btrfs_root
*root
)
2785 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2787 atomic_read(&trans
->transaction
->delayed_refs
.num_entries
);
2792 avg_runtime
= fs_info
->avg_delayed_ref_runtime
;
2793 val
= num_entries
* avg_runtime
;
2794 if (num_entries
* avg_runtime
>= NSEC_PER_SEC
)
2796 if (val
>= NSEC_PER_SEC
/ 2)
2799 return btrfs_check_space_for_delayed_refs(trans
, root
);
2802 struct async_delayed_refs
{
2803 struct btrfs_root
*root
;
2807 struct completion wait
;
2808 struct btrfs_work work
;
2811 static void delayed_ref_async_start(struct btrfs_work
*work
)
2813 struct async_delayed_refs
*async
;
2814 struct btrfs_trans_handle
*trans
;
2817 async
= container_of(work
, struct async_delayed_refs
, work
);
2819 trans
= btrfs_join_transaction(async
->root
);
2820 if (IS_ERR(trans
)) {
2821 async
->error
= PTR_ERR(trans
);
2826 * trans->sync means that when we call end_transaciton, we won't
2827 * wait on delayed refs
2830 ret
= btrfs_run_delayed_refs(trans
, async
->root
, async
->count
);
2834 ret
= btrfs_end_transaction(trans
, async
->root
);
2835 if (ret
&& !async
->error
)
2839 complete(&async
->wait
);
2844 int btrfs_async_run_delayed_refs(struct btrfs_root
*root
,
2845 unsigned long count
, int wait
)
2847 struct async_delayed_refs
*async
;
2850 async
= kmalloc(sizeof(*async
), GFP_NOFS
);
2854 async
->root
= root
->fs_info
->tree_root
;
2855 async
->count
= count
;
2861 init_completion(&async
->wait
);
2863 btrfs_init_work(&async
->work
, btrfs_extent_refs_helper
,
2864 delayed_ref_async_start
, NULL
, NULL
);
2866 btrfs_queue_work(root
->fs_info
->extent_workers
, &async
->work
);
2869 wait_for_completion(&async
->wait
);
2878 * this starts processing the delayed reference count updates and
2879 * extent insertions we have queued up so far. count can be
2880 * 0, which means to process everything in the tree at the start
2881 * of the run (but not newly added entries), or it can be some target
2882 * number you'd like to process.
2884 * Returns 0 on success or if called with an aborted transaction
2885 * Returns <0 on error and aborts the transaction
2887 int btrfs_run_delayed_refs(struct btrfs_trans_handle
*trans
,
2888 struct btrfs_root
*root
, unsigned long count
)
2890 struct rb_node
*node
;
2891 struct btrfs_delayed_ref_root
*delayed_refs
;
2892 struct btrfs_delayed_ref_head
*head
;
2894 int run_all
= count
== (unsigned long)-1;
2895 bool can_flush_pending_bgs
= trans
->can_flush_pending_bgs
;
2897 /* We'll clean this up in btrfs_cleanup_transaction */
2901 if (root
== root
->fs_info
->extent_root
)
2902 root
= root
->fs_info
->tree_root
;
2904 delayed_refs
= &trans
->transaction
->delayed_refs
;
2906 count
= atomic_read(&delayed_refs
->num_entries
) * 2;
2909 #ifdef SCRAMBLE_DELAYED_REFS
2910 delayed_refs
->run_delayed_start
= find_middle(&delayed_refs
->root
);
2912 trans
->can_flush_pending_bgs
= false;
2913 ret
= __btrfs_run_delayed_refs(trans
, root
, count
);
2915 btrfs_abort_transaction(trans
, root
, ret
);
2920 if (!list_empty(&trans
->new_bgs
))
2921 btrfs_create_pending_block_groups(trans
, root
);
2923 spin_lock(&delayed_refs
->lock
);
2924 node
= rb_first(&delayed_refs
->href_root
);
2926 spin_unlock(&delayed_refs
->lock
);
2929 count
= (unsigned long)-1;
2932 head
= rb_entry(node
, struct btrfs_delayed_ref_head
,
2934 if (btrfs_delayed_ref_is_head(&head
->node
)) {
2935 struct btrfs_delayed_ref_node
*ref
;
2938 atomic_inc(&ref
->refs
);
2940 spin_unlock(&delayed_refs
->lock
);
2942 * Mutex was contended, block until it's
2943 * released and try again
2945 mutex_lock(&head
->mutex
);
2946 mutex_unlock(&head
->mutex
);
2948 btrfs_put_delayed_ref(ref
);
2954 node
= rb_next(node
);
2956 spin_unlock(&delayed_refs
->lock
);
2961 assert_qgroups_uptodate(trans
);
2962 trans
->can_flush_pending_bgs
= can_flush_pending_bgs
;
2966 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle
*trans
,
2967 struct btrfs_root
*root
,
2968 u64 bytenr
, u64 num_bytes
, u64 flags
,
2969 int level
, int is_data
)
2971 struct btrfs_delayed_extent_op
*extent_op
;
2974 extent_op
= btrfs_alloc_delayed_extent_op();
2978 extent_op
->flags_to_set
= flags
;
2979 extent_op
->update_flags
= 1;
2980 extent_op
->update_key
= 0;
2981 extent_op
->is_data
= is_data
? 1 : 0;
2982 extent_op
->level
= level
;
2984 ret
= btrfs_add_delayed_extent_op(root
->fs_info
, trans
, bytenr
,
2985 num_bytes
, extent_op
);
2987 btrfs_free_delayed_extent_op(extent_op
);
2991 static noinline
int check_delayed_ref(struct btrfs_trans_handle
*trans
,
2992 struct btrfs_root
*root
,
2993 struct btrfs_path
*path
,
2994 u64 objectid
, u64 offset
, u64 bytenr
)
2996 struct btrfs_delayed_ref_head
*head
;
2997 struct btrfs_delayed_ref_node
*ref
;
2998 struct btrfs_delayed_data_ref
*data_ref
;
2999 struct btrfs_delayed_ref_root
*delayed_refs
;
3002 delayed_refs
= &trans
->transaction
->delayed_refs
;
3003 spin_lock(&delayed_refs
->lock
);
3004 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
3006 spin_unlock(&delayed_refs
->lock
);
3010 if (!mutex_trylock(&head
->mutex
)) {
3011 atomic_inc(&head
->node
.refs
);
3012 spin_unlock(&delayed_refs
->lock
);
3014 btrfs_release_path(path
);
3017 * Mutex was contended, block until it's released and let
3020 mutex_lock(&head
->mutex
);
3021 mutex_unlock(&head
->mutex
);
3022 btrfs_put_delayed_ref(&head
->node
);
3025 spin_unlock(&delayed_refs
->lock
);
3027 spin_lock(&head
->lock
);
3028 list_for_each_entry(ref
, &head
->ref_list
, list
) {
3029 /* If it's a shared ref we know a cross reference exists */
3030 if (ref
->type
!= BTRFS_EXTENT_DATA_REF_KEY
) {
3035 data_ref
= btrfs_delayed_node_to_data_ref(ref
);
3038 * If our ref doesn't match the one we're currently looking at
3039 * then we have a cross reference.
3041 if (data_ref
->root
!= root
->root_key
.objectid
||
3042 data_ref
->objectid
!= objectid
||
3043 data_ref
->offset
!= offset
) {
3048 spin_unlock(&head
->lock
);
3049 mutex_unlock(&head
->mutex
);
3053 static noinline
int check_committed_ref(struct btrfs_trans_handle
*trans
,
3054 struct btrfs_root
*root
,
3055 struct btrfs_path
*path
,
3056 u64 objectid
, u64 offset
, u64 bytenr
)
3058 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
3059 struct extent_buffer
*leaf
;
3060 struct btrfs_extent_data_ref
*ref
;
3061 struct btrfs_extent_inline_ref
*iref
;
3062 struct btrfs_extent_item
*ei
;
3063 struct btrfs_key key
;
3067 key
.objectid
= bytenr
;
3068 key
.offset
= (u64
)-1;
3069 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
3071 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
3074 BUG_ON(ret
== 0); /* Corruption */
3077 if (path
->slots
[0] == 0)
3081 leaf
= path
->nodes
[0];
3082 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3084 if (key
.objectid
!= bytenr
|| key
.type
!= BTRFS_EXTENT_ITEM_KEY
)
3088 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
3089 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3090 if (item_size
< sizeof(*ei
)) {
3091 WARN_ON(item_size
!= sizeof(struct btrfs_extent_item_v0
));
3095 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
3097 if (item_size
!= sizeof(*ei
) +
3098 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY
))
3101 if (btrfs_extent_generation(leaf
, ei
) <=
3102 btrfs_root_last_snapshot(&root
->root_item
))
3105 iref
= (struct btrfs_extent_inline_ref
*)(ei
+ 1);
3106 if (btrfs_extent_inline_ref_type(leaf
, iref
) !=
3107 BTRFS_EXTENT_DATA_REF_KEY
)
3110 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
3111 if (btrfs_extent_refs(leaf
, ei
) !=
3112 btrfs_extent_data_ref_count(leaf
, ref
) ||
3113 btrfs_extent_data_ref_root(leaf
, ref
) !=
3114 root
->root_key
.objectid
||
3115 btrfs_extent_data_ref_objectid(leaf
, ref
) != objectid
||
3116 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
3124 int btrfs_cross_ref_exist(struct btrfs_trans_handle
*trans
,
3125 struct btrfs_root
*root
,
3126 u64 objectid
, u64 offset
, u64 bytenr
)
3128 struct btrfs_path
*path
;
3132 path
= btrfs_alloc_path();
3137 ret
= check_committed_ref(trans
, root
, path
, objectid
,
3139 if (ret
&& ret
!= -ENOENT
)
3142 ret2
= check_delayed_ref(trans
, root
, path
, objectid
,
3144 } while (ret2
== -EAGAIN
);
3146 if (ret2
&& ret2
!= -ENOENT
) {
3151 if (ret
!= -ENOENT
|| ret2
!= -ENOENT
)
3154 btrfs_free_path(path
);
3155 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
3160 static int __btrfs_mod_ref(struct btrfs_trans_handle
*trans
,
3161 struct btrfs_root
*root
,
3162 struct extent_buffer
*buf
,
3163 int full_backref
, int inc
)
3170 struct btrfs_key key
;
3171 struct btrfs_file_extent_item
*fi
;
3175 int (*process_func
)(struct btrfs_trans_handle
*, struct btrfs_root
*,
3176 u64
, u64
, u64
, u64
, u64
, u64
, int);
3179 if (btrfs_test_is_dummy_root(root
))
3182 ref_root
= btrfs_header_owner(buf
);
3183 nritems
= btrfs_header_nritems(buf
);
3184 level
= btrfs_header_level(buf
);
3186 if (!test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) && level
== 0)
3190 process_func
= btrfs_inc_extent_ref
;
3192 process_func
= btrfs_free_extent
;
3195 parent
= buf
->start
;
3199 for (i
= 0; i
< nritems
; i
++) {
3201 btrfs_item_key_to_cpu(buf
, &key
, i
);
3202 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
3204 fi
= btrfs_item_ptr(buf
, i
,
3205 struct btrfs_file_extent_item
);
3206 if (btrfs_file_extent_type(buf
, fi
) ==
3207 BTRFS_FILE_EXTENT_INLINE
)
3209 bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
3213 num_bytes
= btrfs_file_extent_disk_num_bytes(buf
, fi
);
3214 key
.offset
-= btrfs_file_extent_offset(buf
, fi
);
3215 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
3216 parent
, ref_root
, key
.objectid
,
3221 bytenr
= btrfs_node_blockptr(buf
, i
);
3222 num_bytes
= root
->nodesize
;
3223 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
3224 parent
, ref_root
, level
- 1, 0,
3235 int btrfs_inc_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
3236 struct extent_buffer
*buf
, int full_backref
)
3238 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 1);
3241 int btrfs_dec_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
3242 struct extent_buffer
*buf
, int full_backref
)
3244 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 0);
3247 static int write_one_cache_group(struct btrfs_trans_handle
*trans
,
3248 struct btrfs_root
*root
,
3249 struct btrfs_path
*path
,
3250 struct btrfs_block_group_cache
*cache
)
3253 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
3255 struct extent_buffer
*leaf
;
3257 ret
= btrfs_search_slot(trans
, extent_root
, &cache
->key
, path
, 0, 1);
3264 leaf
= path
->nodes
[0];
3265 bi
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
3266 write_extent_buffer(leaf
, &cache
->item
, bi
, sizeof(cache
->item
));
3267 btrfs_mark_buffer_dirty(leaf
);
3269 btrfs_release_path(path
);
3274 static struct btrfs_block_group_cache
*
3275 next_block_group(struct btrfs_root
*root
,
3276 struct btrfs_block_group_cache
*cache
)
3278 struct rb_node
*node
;
3280 spin_lock(&root
->fs_info
->block_group_cache_lock
);
3282 /* If our block group was removed, we need a full search. */
3283 if (RB_EMPTY_NODE(&cache
->cache_node
)) {
3284 const u64 next_bytenr
= cache
->key
.objectid
+ cache
->key
.offset
;
3286 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
3287 btrfs_put_block_group(cache
);
3288 cache
= btrfs_lookup_first_block_group(root
->fs_info
,
3292 node
= rb_next(&cache
->cache_node
);
3293 btrfs_put_block_group(cache
);
3295 cache
= rb_entry(node
, struct btrfs_block_group_cache
,
3297 btrfs_get_block_group(cache
);
3300 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
3304 static int cache_save_setup(struct btrfs_block_group_cache
*block_group
,
3305 struct btrfs_trans_handle
*trans
,
3306 struct btrfs_path
*path
)
3308 struct btrfs_root
*root
= block_group
->fs_info
->tree_root
;
3309 struct inode
*inode
= NULL
;
3311 int dcs
= BTRFS_DC_ERROR
;
3317 * If this block group is smaller than 100 megs don't bother caching the
3320 if (block_group
->key
.offset
< (100 * 1024 * 1024)) {
3321 spin_lock(&block_group
->lock
);
3322 block_group
->disk_cache_state
= BTRFS_DC_WRITTEN
;
3323 spin_unlock(&block_group
->lock
);
3330 inode
= lookup_free_space_inode(root
, block_group
, path
);
3331 if (IS_ERR(inode
) && PTR_ERR(inode
) != -ENOENT
) {
3332 ret
= PTR_ERR(inode
);
3333 btrfs_release_path(path
);
3337 if (IS_ERR(inode
)) {
3341 if (block_group
->ro
)
3344 ret
= create_free_space_inode(root
, trans
, block_group
, path
);
3350 /* We've already setup this transaction, go ahead and exit */
3351 if (block_group
->cache_generation
== trans
->transid
&&
3352 i_size_read(inode
)) {
3353 dcs
= BTRFS_DC_SETUP
;
3358 * We want to set the generation to 0, that way if anything goes wrong
3359 * from here on out we know not to trust this cache when we load up next
3362 BTRFS_I(inode
)->generation
= 0;
3363 ret
= btrfs_update_inode(trans
, root
, inode
);
3366 * So theoretically we could recover from this, simply set the
3367 * super cache generation to 0 so we know to invalidate the
3368 * cache, but then we'd have to keep track of the block groups
3369 * that fail this way so we know we _have_ to reset this cache
3370 * before the next commit or risk reading stale cache. So to
3371 * limit our exposure to horrible edge cases lets just abort the
3372 * transaction, this only happens in really bad situations
3375 btrfs_abort_transaction(trans
, root
, ret
);
3380 if (i_size_read(inode
) > 0) {
3381 ret
= btrfs_check_trunc_cache_free_space(root
,
3382 &root
->fs_info
->global_block_rsv
);
3386 ret
= btrfs_truncate_free_space_cache(root
, trans
, NULL
, inode
);
3391 spin_lock(&block_group
->lock
);
3392 if (block_group
->cached
!= BTRFS_CACHE_FINISHED
||
3393 !btrfs_test_opt(root
, SPACE_CACHE
)) {
3395 * don't bother trying to write stuff out _if_
3396 * a) we're not cached,
3397 * b) we're with nospace_cache mount option.
3399 dcs
= BTRFS_DC_WRITTEN
;
3400 spin_unlock(&block_group
->lock
);
3403 spin_unlock(&block_group
->lock
);
3406 * Try to preallocate enough space based on how big the block group is.
3407 * Keep in mind this has to include any pinned space which could end up
3408 * taking up quite a bit since it's not folded into the other space
3411 num_pages
= div_u64(block_group
->key
.offset
, 256 * 1024 * 1024);
3416 num_pages
*= PAGE_CACHE_SIZE
;
3418 ret
= btrfs_check_data_free_space(inode
, num_pages
, num_pages
);
3422 ret
= btrfs_prealloc_file_range_trans(inode
, trans
, 0, 0, num_pages
,
3423 num_pages
, num_pages
,
3426 dcs
= BTRFS_DC_SETUP
;
3427 btrfs_free_reserved_data_space(inode
, num_pages
);
3432 btrfs_release_path(path
);
3434 spin_lock(&block_group
->lock
);
3435 if (!ret
&& dcs
== BTRFS_DC_SETUP
)
3436 block_group
->cache_generation
= trans
->transid
;
3437 block_group
->disk_cache_state
= dcs
;
3438 spin_unlock(&block_group
->lock
);
3443 int btrfs_setup_space_cache(struct btrfs_trans_handle
*trans
,
3444 struct btrfs_root
*root
)
3446 struct btrfs_block_group_cache
*cache
, *tmp
;
3447 struct btrfs_transaction
*cur_trans
= trans
->transaction
;
3448 struct btrfs_path
*path
;
3450 if (list_empty(&cur_trans
->dirty_bgs
) ||
3451 !btrfs_test_opt(root
, SPACE_CACHE
))
3454 path
= btrfs_alloc_path();
3458 /* Could add new block groups, use _safe just in case */
3459 list_for_each_entry_safe(cache
, tmp
, &cur_trans
->dirty_bgs
,
3461 if (cache
->disk_cache_state
== BTRFS_DC_CLEAR
)
3462 cache_save_setup(cache
, trans
, path
);
3465 btrfs_free_path(path
);
3470 * transaction commit does final block group cache writeback during a
3471 * critical section where nothing is allowed to change the FS. This is
3472 * required in order for the cache to actually match the block group,
3473 * but can introduce a lot of latency into the commit.
3475 * So, btrfs_start_dirty_block_groups is here to kick off block group
3476 * cache IO. There's a chance we'll have to redo some of it if the
3477 * block group changes again during the commit, but it greatly reduces
3478 * the commit latency by getting rid of the easy block groups while
3479 * we're still allowing others to join the commit.
3481 int btrfs_start_dirty_block_groups(struct btrfs_trans_handle
*trans
,
3482 struct btrfs_root
*root
)
3484 struct btrfs_block_group_cache
*cache
;
3485 struct btrfs_transaction
*cur_trans
= trans
->transaction
;
3488 struct btrfs_path
*path
= NULL
;
3490 struct list_head
*io
= &cur_trans
->io_bgs
;
3491 int num_started
= 0;
3494 spin_lock(&cur_trans
->dirty_bgs_lock
);
3495 if (list_empty(&cur_trans
->dirty_bgs
)) {
3496 spin_unlock(&cur_trans
->dirty_bgs_lock
);
3499 list_splice_init(&cur_trans
->dirty_bgs
, &dirty
);
3500 spin_unlock(&cur_trans
->dirty_bgs_lock
);
3504 * make sure all the block groups on our dirty list actually
3507 btrfs_create_pending_block_groups(trans
, root
);
3510 path
= btrfs_alloc_path();
3516 * cache_write_mutex is here only to save us from balance or automatic
3517 * removal of empty block groups deleting this block group while we are
3518 * writing out the cache
3520 mutex_lock(&trans
->transaction
->cache_write_mutex
);
3521 while (!list_empty(&dirty
)) {
3522 cache
= list_first_entry(&dirty
,
3523 struct btrfs_block_group_cache
,
3526 * this can happen if something re-dirties a block
3527 * group that is already under IO. Just wait for it to
3528 * finish and then do it all again
3530 if (!list_empty(&cache
->io_list
)) {
3531 list_del_init(&cache
->io_list
);
3532 btrfs_wait_cache_io(root
, trans
, cache
,
3533 &cache
->io_ctl
, path
,
3534 cache
->key
.objectid
);
3535 btrfs_put_block_group(cache
);
3540 * btrfs_wait_cache_io uses the cache->dirty_list to decide
3541 * if it should update the cache_state. Don't delete
3542 * until after we wait.
3544 * Since we're not running in the commit critical section
3545 * we need the dirty_bgs_lock to protect from update_block_group
3547 spin_lock(&cur_trans
->dirty_bgs_lock
);
3548 list_del_init(&cache
->dirty_list
);
3549 spin_unlock(&cur_trans
->dirty_bgs_lock
);
3553 cache_save_setup(cache
, trans
, path
);
3555 if (cache
->disk_cache_state
== BTRFS_DC_SETUP
) {
3556 cache
->io_ctl
.inode
= NULL
;
3557 ret
= btrfs_write_out_cache(root
, trans
, cache
, path
);
3558 if (ret
== 0 && cache
->io_ctl
.inode
) {
3563 * the cache_write_mutex is protecting
3566 list_add_tail(&cache
->io_list
, io
);
3569 * if we failed to write the cache, the
3570 * generation will be bad and life goes on
3576 ret
= write_one_cache_group(trans
, root
, path
, cache
);
3578 * Our block group might still be attached to the list
3579 * of new block groups in the transaction handle of some
3580 * other task (struct btrfs_trans_handle->new_bgs). This
3581 * means its block group item isn't yet in the extent
3582 * tree. If this happens ignore the error, as we will
3583 * try again later in the critical section of the
3584 * transaction commit.
3586 if (ret
== -ENOENT
) {
3588 spin_lock(&cur_trans
->dirty_bgs_lock
);
3589 if (list_empty(&cache
->dirty_list
)) {
3590 list_add_tail(&cache
->dirty_list
,
3591 &cur_trans
->dirty_bgs
);
3592 btrfs_get_block_group(cache
);
3594 spin_unlock(&cur_trans
->dirty_bgs_lock
);
3596 btrfs_abort_transaction(trans
, root
, ret
);
3600 /* if its not on the io list, we need to put the block group */
3602 btrfs_put_block_group(cache
);
3608 * Avoid blocking other tasks for too long. It might even save
3609 * us from writing caches for block groups that are going to be
3612 mutex_unlock(&trans
->transaction
->cache_write_mutex
);
3613 mutex_lock(&trans
->transaction
->cache_write_mutex
);
3615 mutex_unlock(&trans
->transaction
->cache_write_mutex
);
3618 * go through delayed refs for all the stuff we've just kicked off
3619 * and then loop back (just once)
3621 ret
= btrfs_run_delayed_refs(trans
, root
, 0);
3622 if (!ret
&& loops
== 0) {
3624 spin_lock(&cur_trans
->dirty_bgs_lock
);
3625 list_splice_init(&cur_trans
->dirty_bgs
, &dirty
);
3627 * dirty_bgs_lock protects us from concurrent block group
3628 * deletes too (not just cache_write_mutex).
3630 if (!list_empty(&dirty
)) {
3631 spin_unlock(&cur_trans
->dirty_bgs_lock
);
3634 spin_unlock(&cur_trans
->dirty_bgs_lock
);
3637 btrfs_free_path(path
);
3641 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle
*trans
,
3642 struct btrfs_root
*root
)
3644 struct btrfs_block_group_cache
*cache
;
3645 struct btrfs_transaction
*cur_trans
= trans
->transaction
;
3648 struct btrfs_path
*path
;
3649 struct list_head
*io
= &cur_trans
->io_bgs
;
3650 int num_started
= 0;
3652 path
= btrfs_alloc_path();
3657 * We don't need the lock here since we are protected by the transaction
3658 * commit. We want to do the cache_save_setup first and then run the
3659 * delayed refs to make sure we have the best chance at doing this all
3662 while (!list_empty(&cur_trans
->dirty_bgs
)) {
3663 cache
= list_first_entry(&cur_trans
->dirty_bgs
,
3664 struct btrfs_block_group_cache
,
3668 * this can happen if cache_save_setup re-dirties a block
3669 * group that is already under IO. Just wait for it to
3670 * finish and then do it all again
3672 if (!list_empty(&cache
->io_list
)) {
3673 list_del_init(&cache
->io_list
);
3674 btrfs_wait_cache_io(root
, trans
, cache
,
3675 &cache
->io_ctl
, path
,
3676 cache
->key
.objectid
);
3677 btrfs_put_block_group(cache
);
3681 * don't remove from the dirty list until after we've waited
3684 list_del_init(&cache
->dirty_list
);
3687 cache_save_setup(cache
, trans
, path
);
3690 ret
= btrfs_run_delayed_refs(trans
, root
, (unsigned long) -1);
3692 if (!ret
&& cache
->disk_cache_state
== BTRFS_DC_SETUP
) {
3693 cache
->io_ctl
.inode
= NULL
;
3694 ret
= btrfs_write_out_cache(root
, trans
, cache
, path
);
3695 if (ret
== 0 && cache
->io_ctl
.inode
) {
3698 list_add_tail(&cache
->io_list
, io
);
3701 * if we failed to write the cache, the
3702 * generation will be bad and life goes on
3708 ret
= write_one_cache_group(trans
, root
, path
, cache
);
3710 btrfs_abort_transaction(trans
, root
, ret
);
3713 /* if its not on the io list, we need to put the block group */
3715 btrfs_put_block_group(cache
);
3718 while (!list_empty(io
)) {
3719 cache
= list_first_entry(io
, struct btrfs_block_group_cache
,
3721 list_del_init(&cache
->io_list
);
3722 btrfs_wait_cache_io(root
, trans
, cache
,
3723 &cache
->io_ctl
, path
, cache
->key
.objectid
);
3724 btrfs_put_block_group(cache
);
3727 btrfs_free_path(path
);
3731 int btrfs_extent_readonly(struct btrfs_root
*root
, u64 bytenr
)
3733 struct btrfs_block_group_cache
*block_group
;
3736 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
3737 if (!block_group
|| block_group
->ro
)
3740 btrfs_put_block_group(block_group
);
3744 static const char *alloc_name(u64 flags
)
3747 case BTRFS_BLOCK_GROUP_METADATA
|BTRFS_BLOCK_GROUP_DATA
:
3749 case BTRFS_BLOCK_GROUP_METADATA
:
3751 case BTRFS_BLOCK_GROUP_DATA
:
3753 case BTRFS_BLOCK_GROUP_SYSTEM
:
3757 return "invalid-combination";
3761 static int update_space_info(struct btrfs_fs_info
*info
, u64 flags
,
3762 u64 total_bytes
, u64 bytes_used
,
3763 struct btrfs_space_info
**space_info
)
3765 struct btrfs_space_info
*found
;
3770 if (flags
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
3771 BTRFS_BLOCK_GROUP_RAID10
))
3776 found
= __find_space_info(info
, flags
);
3778 spin_lock(&found
->lock
);
3779 found
->total_bytes
+= total_bytes
;
3780 found
->disk_total
+= total_bytes
* factor
;
3781 found
->bytes_used
+= bytes_used
;
3782 found
->disk_used
+= bytes_used
* factor
;
3783 if (total_bytes
> 0)
3785 spin_unlock(&found
->lock
);
3786 *space_info
= found
;
3789 found
= kzalloc(sizeof(*found
), GFP_NOFS
);
3793 ret
= percpu_counter_init(&found
->total_bytes_pinned
, 0, GFP_KERNEL
);
3799 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++)
3800 INIT_LIST_HEAD(&found
->block_groups
[i
]);
3801 init_rwsem(&found
->groups_sem
);
3802 spin_lock_init(&found
->lock
);
3803 found
->flags
= flags
& BTRFS_BLOCK_GROUP_TYPE_MASK
;
3804 found
->total_bytes
= total_bytes
;
3805 found
->disk_total
= total_bytes
* factor
;
3806 found
->bytes_used
= bytes_used
;
3807 found
->disk_used
= bytes_used
* factor
;
3808 found
->bytes_pinned
= 0;
3809 found
->bytes_reserved
= 0;
3810 found
->bytes_readonly
= 0;
3811 found
->bytes_may_use
= 0;
3813 found
->max_extent_size
= 0;
3814 found
->force_alloc
= CHUNK_ALLOC_NO_FORCE
;
3815 found
->chunk_alloc
= 0;
3817 init_waitqueue_head(&found
->wait
);
3818 INIT_LIST_HEAD(&found
->ro_bgs
);
3820 ret
= kobject_init_and_add(&found
->kobj
, &space_info_ktype
,
3821 info
->space_info_kobj
, "%s",
3822 alloc_name(found
->flags
));
3828 *space_info
= found
;
3829 list_add_rcu(&found
->list
, &info
->space_info
);
3830 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3831 info
->data_sinfo
= found
;
3836 static void set_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
3838 u64 extra_flags
= chunk_to_extended(flags
) &
3839 BTRFS_EXTENDED_PROFILE_MASK
;
3841 write_seqlock(&fs_info
->profiles_lock
);
3842 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3843 fs_info
->avail_data_alloc_bits
|= extra_flags
;
3844 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
3845 fs_info
->avail_metadata_alloc_bits
|= extra_flags
;
3846 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
3847 fs_info
->avail_system_alloc_bits
|= extra_flags
;
3848 write_sequnlock(&fs_info
->profiles_lock
);
3852 * returns target flags in extended format or 0 if restripe for this
3853 * chunk_type is not in progress
3855 * should be called with either volume_mutex or balance_lock held
3857 static u64
get_restripe_target(struct btrfs_fs_info
*fs_info
, u64 flags
)
3859 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3865 if (flags
& BTRFS_BLOCK_GROUP_DATA
&&
3866 bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3867 target
= BTRFS_BLOCK_GROUP_DATA
| bctl
->data
.target
;
3868 } else if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
&&
3869 bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3870 target
= BTRFS_BLOCK_GROUP_SYSTEM
| bctl
->sys
.target
;
3871 } else if (flags
& BTRFS_BLOCK_GROUP_METADATA
&&
3872 bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3873 target
= BTRFS_BLOCK_GROUP_METADATA
| bctl
->meta
.target
;
3880 * @flags: available profiles in extended format (see ctree.h)
3882 * Returns reduced profile in chunk format. If profile changing is in
3883 * progress (either running or paused) picks the target profile (if it's
3884 * already available), otherwise falls back to plain reducing.
3886 static u64
btrfs_reduce_alloc_profile(struct btrfs_root
*root
, u64 flags
)
3888 u64 num_devices
= root
->fs_info
->fs_devices
->rw_devices
;
3894 * see if restripe for this chunk_type is in progress, if so
3895 * try to reduce to the target profile
3897 spin_lock(&root
->fs_info
->balance_lock
);
3898 target
= get_restripe_target(root
->fs_info
, flags
);
3900 /* pick target profile only if it's already available */
3901 if ((flags
& target
) & BTRFS_EXTENDED_PROFILE_MASK
) {
3902 spin_unlock(&root
->fs_info
->balance_lock
);
3903 return extended_to_chunk(target
);
3906 spin_unlock(&root
->fs_info
->balance_lock
);
3908 /* First, mask out the RAID levels which aren't possible */
3909 for (raid_type
= 0; raid_type
< BTRFS_NR_RAID_TYPES
; raid_type
++) {
3910 if (num_devices
>= btrfs_raid_array
[raid_type
].devs_min
)
3911 allowed
|= btrfs_raid_group
[raid_type
];
3915 if (allowed
& BTRFS_BLOCK_GROUP_RAID6
)
3916 allowed
= BTRFS_BLOCK_GROUP_RAID6
;
3917 else if (allowed
& BTRFS_BLOCK_GROUP_RAID5
)
3918 allowed
= BTRFS_BLOCK_GROUP_RAID5
;
3919 else if (allowed
& BTRFS_BLOCK_GROUP_RAID10
)
3920 allowed
= BTRFS_BLOCK_GROUP_RAID10
;
3921 else if (allowed
& BTRFS_BLOCK_GROUP_RAID1
)
3922 allowed
= BTRFS_BLOCK_GROUP_RAID1
;
3923 else if (allowed
& BTRFS_BLOCK_GROUP_RAID0
)
3924 allowed
= BTRFS_BLOCK_GROUP_RAID0
;
3926 flags
&= ~BTRFS_BLOCK_GROUP_PROFILE_MASK
;
3928 return extended_to_chunk(flags
| allowed
);
3931 static u64
get_alloc_profile(struct btrfs_root
*root
, u64 orig_flags
)
3938 seq
= read_seqbegin(&root
->fs_info
->profiles_lock
);
3940 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3941 flags
|= root
->fs_info
->avail_data_alloc_bits
;
3942 else if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
3943 flags
|= root
->fs_info
->avail_system_alloc_bits
;
3944 else if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
3945 flags
|= root
->fs_info
->avail_metadata_alloc_bits
;
3946 } while (read_seqretry(&root
->fs_info
->profiles_lock
, seq
));
3948 return btrfs_reduce_alloc_profile(root
, flags
);
3951 u64
btrfs_get_alloc_profile(struct btrfs_root
*root
, int data
)
3957 flags
= BTRFS_BLOCK_GROUP_DATA
;
3958 else if (root
== root
->fs_info
->chunk_root
)
3959 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
3961 flags
= BTRFS_BLOCK_GROUP_METADATA
;
3963 ret
= get_alloc_profile(root
, flags
);
3968 * This will check the space that the inode allocates from to make sure we have
3969 * enough space for bytes.
3971 int btrfs_check_data_free_space(struct inode
*inode
, u64 bytes
, u64 write_bytes
)
3973 struct btrfs_space_info
*data_sinfo
;
3974 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3975 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3978 int need_commit
= 2;
3979 int have_pinned_space
;
3981 /* make sure bytes are sectorsize aligned */
3982 bytes
= ALIGN(bytes
, root
->sectorsize
);
3984 if (btrfs_is_free_space_inode(inode
)) {
3986 ASSERT(current
->journal_info
);
3989 data_sinfo
= fs_info
->data_sinfo
;
3994 /* make sure we have enough space to handle the data first */
3995 spin_lock(&data_sinfo
->lock
);
3996 used
= data_sinfo
->bytes_used
+ data_sinfo
->bytes_reserved
+
3997 data_sinfo
->bytes_pinned
+ data_sinfo
->bytes_readonly
+
3998 data_sinfo
->bytes_may_use
;
4000 if (used
+ bytes
> data_sinfo
->total_bytes
) {
4001 struct btrfs_trans_handle
*trans
;
4004 * if we don't have enough free bytes in this space then we need
4005 * to alloc a new chunk.
4007 if (!data_sinfo
->full
) {
4010 data_sinfo
->force_alloc
= CHUNK_ALLOC_FORCE
;
4011 spin_unlock(&data_sinfo
->lock
);
4013 alloc_target
= btrfs_get_alloc_profile(root
, 1);
4015 * It is ugly that we don't call nolock join
4016 * transaction for the free space inode case here.
4017 * But it is safe because we only do the data space
4018 * reservation for the free space cache in the
4019 * transaction context, the common join transaction
4020 * just increase the counter of the current transaction
4021 * handler, doesn't try to acquire the trans_lock of
4024 trans
= btrfs_join_transaction(root
);
4026 return PTR_ERR(trans
);
4028 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
4030 CHUNK_ALLOC_NO_FORCE
);
4031 btrfs_end_transaction(trans
, root
);
4036 have_pinned_space
= 1;
4042 data_sinfo
= fs_info
->data_sinfo
;
4048 * If we don't have enough pinned space to deal with this
4049 * allocation, and no removed chunk in current transaction,
4050 * don't bother committing the transaction.
4052 have_pinned_space
= percpu_counter_compare(
4053 &data_sinfo
->total_bytes_pinned
,
4054 used
+ bytes
- data_sinfo
->total_bytes
);
4055 spin_unlock(&data_sinfo
->lock
);
4057 /* commit the current transaction and try again */
4060 !atomic_read(&root
->fs_info
->open_ioctl_trans
)) {
4063 if (need_commit
> 0)
4064 btrfs_wait_ordered_roots(fs_info
, -1);
4066 trans
= btrfs_join_transaction(root
);
4068 return PTR_ERR(trans
);
4069 if (have_pinned_space
>= 0 ||
4070 test_bit(BTRFS_TRANS_HAVE_FREE_BGS
,
4071 &trans
->transaction
->flags
) ||
4073 ret
= btrfs_commit_transaction(trans
, root
);
4077 * make sure that all running delayed iput are
4080 down_write(&root
->fs_info
->delayed_iput_sem
);
4081 up_write(&root
->fs_info
->delayed_iput_sem
);
4084 btrfs_end_transaction(trans
, root
);
4088 trace_btrfs_space_reservation(root
->fs_info
,
4089 "space_info:enospc",
4090 data_sinfo
->flags
, bytes
, 1);
4093 ret
= btrfs_qgroup_reserve(root
, write_bytes
);
4096 data_sinfo
->bytes_may_use
+= bytes
;
4097 trace_btrfs_space_reservation(root
->fs_info
, "space_info",
4098 data_sinfo
->flags
, bytes
, 1);
4100 spin_unlock(&data_sinfo
->lock
);
4106 * Called if we need to clear a data reservation for this inode.
4108 void btrfs_free_reserved_data_space(struct inode
*inode
, u64 bytes
)
4110 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4111 struct btrfs_space_info
*data_sinfo
;
4113 /* make sure bytes are sectorsize aligned */
4114 bytes
= ALIGN(bytes
, root
->sectorsize
);
4116 data_sinfo
= root
->fs_info
->data_sinfo
;
4117 spin_lock(&data_sinfo
->lock
);
4118 WARN_ON(data_sinfo
->bytes_may_use
< bytes
);
4119 data_sinfo
->bytes_may_use
-= bytes
;
4120 trace_btrfs_space_reservation(root
->fs_info
, "space_info",
4121 data_sinfo
->flags
, bytes
, 0);
4122 spin_unlock(&data_sinfo
->lock
);
4125 static void force_metadata_allocation(struct btrfs_fs_info
*info
)
4127 struct list_head
*head
= &info
->space_info
;
4128 struct btrfs_space_info
*found
;
4131 list_for_each_entry_rcu(found
, head
, list
) {
4132 if (found
->flags
& BTRFS_BLOCK_GROUP_METADATA
)
4133 found
->force_alloc
= CHUNK_ALLOC_FORCE
;
4138 static inline u64
calc_global_rsv_need_space(struct btrfs_block_rsv
*global
)
4140 return (global
->size
<< 1);
4143 static int should_alloc_chunk(struct btrfs_root
*root
,
4144 struct btrfs_space_info
*sinfo
, int force
)
4146 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
4147 u64 num_bytes
= sinfo
->total_bytes
- sinfo
->bytes_readonly
;
4148 u64 num_allocated
= sinfo
->bytes_used
+ sinfo
->bytes_reserved
;
4151 if (force
== CHUNK_ALLOC_FORCE
)
4155 * We need to take into account the global rsv because for all intents
4156 * and purposes it's used space. Don't worry about locking the
4157 * global_rsv, it doesn't change except when the transaction commits.
4159 if (sinfo
->flags
& BTRFS_BLOCK_GROUP_METADATA
)
4160 num_allocated
+= calc_global_rsv_need_space(global_rsv
);
4163 * in limited mode, we want to have some free space up to
4164 * about 1% of the FS size.
4166 if (force
== CHUNK_ALLOC_LIMITED
) {
4167 thresh
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
4168 thresh
= max_t(u64
, 64 * 1024 * 1024,
4169 div_factor_fine(thresh
, 1));
4171 if (num_bytes
- num_allocated
< thresh
)
4175 if (num_allocated
+ 2 * 1024 * 1024 < div_factor(num_bytes
, 8))
4180 static u64
get_profile_num_devs(struct btrfs_root
*root
, u64 type
)
4184 if (type
& (BTRFS_BLOCK_GROUP_RAID10
|
4185 BTRFS_BLOCK_GROUP_RAID0
|
4186 BTRFS_BLOCK_GROUP_RAID5
|
4187 BTRFS_BLOCK_GROUP_RAID6
))
4188 num_dev
= root
->fs_info
->fs_devices
->rw_devices
;
4189 else if (type
& BTRFS_BLOCK_GROUP_RAID1
)
4192 num_dev
= 1; /* DUP or single */
4198 * If @is_allocation is true, reserve space in the system space info necessary
4199 * for allocating a chunk, otherwise if it's false, reserve space necessary for
4202 void check_system_chunk(struct btrfs_trans_handle
*trans
,
4203 struct btrfs_root
*root
,
4206 struct btrfs_space_info
*info
;
4213 * Needed because we can end up allocating a system chunk and for an
4214 * atomic and race free space reservation in the chunk block reserve.
4216 ASSERT(mutex_is_locked(&root
->fs_info
->chunk_mutex
));
4218 info
= __find_space_info(root
->fs_info
, BTRFS_BLOCK_GROUP_SYSTEM
);
4219 spin_lock(&info
->lock
);
4220 left
= info
->total_bytes
- info
->bytes_used
- info
->bytes_pinned
-
4221 info
->bytes_reserved
- info
->bytes_readonly
-
4222 info
->bytes_may_use
;
4223 spin_unlock(&info
->lock
);
4225 num_devs
= get_profile_num_devs(root
, type
);
4227 /* num_devs device items to update and 1 chunk item to add or remove */
4228 thresh
= btrfs_calc_trunc_metadata_size(root
, num_devs
) +
4229 btrfs_calc_trans_metadata_size(root
, 1);
4231 if (left
< thresh
&& btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
4232 btrfs_info(root
->fs_info
, "left=%llu, need=%llu, flags=%llu",
4233 left
, thresh
, type
);
4234 dump_space_info(info
, 0, 0);
4237 if (left
< thresh
) {
4240 flags
= btrfs_get_alloc_profile(root
->fs_info
->chunk_root
, 0);
4242 * Ignore failure to create system chunk. We might end up not
4243 * needing it, as we might not need to COW all nodes/leafs from
4244 * the paths we visit in the chunk tree (they were already COWed
4245 * or created in the current transaction for example).
4247 ret
= btrfs_alloc_chunk(trans
, root
, flags
);
4251 ret
= btrfs_block_rsv_add(root
->fs_info
->chunk_root
,
4252 &root
->fs_info
->chunk_block_rsv
,
4253 thresh
, BTRFS_RESERVE_NO_FLUSH
);
4255 trans
->chunk_bytes_reserved
+= thresh
;
4259 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
4260 struct btrfs_root
*extent_root
, u64 flags
, int force
)
4262 struct btrfs_space_info
*space_info
;
4263 struct btrfs_fs_info
*fs_info
= extent_root
->fs_info
;
4264 int wait_for_alloc
= 0;
4267 /* Don't re-enter if we're already allocating a chunk */
4268 if (trans
->allocating_chunk
)
4271 space_info
= __find_space_info(extent_root
->fs_info
, flags
);
4273 ret
= update_space_info(extent_root
->fs_info
, flags
,
4275 BUG_ON(ret
); /* -ENOMEM */
4277 BUG_ON(!space_info
); /* Logic error */
4280 spin_lock(&space_info
->lock
);
4281 if (force
< space_info
->force_alloc
)
4282 force
= space_info
->force_alloc
;
4283 if (space_info
->full
) {
4284 if (should_alloc_chunk(extent_root
, space_info
, force
))
4288 spin_unlock(&space_info
->lock
);
4292 if (!should_alloc_chunk(extent_root
, space_info
, force
)) {
4293 spin_unlock(&space_info
->lock
);
4295 } else if (space_info
->chunk_alloc
) {
4298 space_info
->chunk_alloc
= 1;
4301 spin_unlock(&space_info
->lock
);
4303 mutex_lock(&fs_info
->chunk_mutex
);
4306 * The chunk_mutex is held throughout the entirety of a chunk
4307 * allocation, so once we've acquired the chunk_mutex we know that the
4308 * other guy is done and we need to recheck and see if we should
4311 if (wait_for_alloc
) {
4312 mutex_unlock(&fs_info
->chunk_mutex
);
4317 trans
->allocating_chunk
= true;
4320 * If we have mixed data/metadata chunks we want to make sure we keep
4321 * allocating mixed chunks instead of individual chunks.
4323 if (btrfs_mixed_space_info(space_info
))
4324 flags
|= (BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
);
4327 * if we're doing a data chunk, go ahead and make sure that
4328 * we keep a reasonable number of metadata chunks allocated in the
4331 if (flags
& BTRFS_BLOCK_GROUP_DATA
&& fs_info
->metadata_ratio
) {
4332 fs_info
->data_chunk_allocations
++;
4333 if (!(fs_info
->data_chunk_allocations
%
4334 fs_info
->metadata_ratio
))
4335 force_metadata_allocation(fs_info
);
4339 * Check if we have enough space in SYSTEM chunk because we may need
4340 * to update devices.
4342 check_system_chunk(trans
, extent_root
, flags
);
4344 ret
= btrfs_alloc_chunk(trans
, extent_root
, flags
);
4345 trans
->allocating_chunk
= false;
4347 spin_lock(&space_info
->lock
);
4348 if (ret
< 0 && ret
!= -ENOSPC
)
4351 space_info
->full
= 1;
4355 space_info
->force_alloc
= CHUNK_ALLOC_NO_FORCE
;
4357 space_info
->chunk_alloc
= 0;
4358 spin_unlock(&space_info
->lock
);
4359 mutex_unlock(&fs_info
->chunk_mutex
);
4361 * When we allocate a new chunk we reserve space in the chunk block
4362 * reserve to make sure we can COW nodes/leafs in the chunk tree or
4363 * add new nodes/leafs to it if we end up needing to do it when
4364 * inserting the chunk item and updating device items as part of the
4365 * second phase of chunk allocation, performed by
4366 * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a
4367 * large number of new block groups to create in our transaction
4368 * handle's new_bgs list to avoid exhausting the chunk block reserve
4369 * in extreme cases - like having a single transaction create many new
4370 * block groups when starting to write out the free space caches of all
4371 * the block groups that were made dirty during the lifetime of the
4374 if (trans
->can_flush_pending_bgs
&&
4375 trans
->chunk_bytes_reserved
>= (2 * 1024 * 1024ull)) {
4376 btrfs_create_pending_block_groups(trans
, trans
->root
);
4377 btrfs_trans_release_chunk_metadata(trans
);
4382 static int can_overcommit(struct btrfs_root
*root
,
4383 struct btrfs_space_info
*space_info
, u64 bytes
,
4384 enum btrfs_reserve_flush_enum flush
)
4386 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
4387 u64 profile
= btrfs_get_alloc_profile(root
, 0);
4392 used
= space_info
->bytes_used
+ space_info
->bytes_reserved
+
4393 space_info
->bytes_pinned
+ space_info
->bytes_readonly
;
4396 * We only want to allow over committing if we have lots of actual space
4397 * free, but if we don't have enough space to handle the global reserve
4398 * space then we could end up having a real enospc problem when trying
4399 * to allocate a chunk or some other such important allocation.
4401 spin_lock(&global_rsv
->lock
);
4402 space_size
= calc_global_rsv_need_space(global_rsv
);
4403 spin_unlock(&global_rsv
->lock
);
4404 if (used
+ space_size
>= space_info
->total_bytes
)
4407 used
+= space_info
->bytes_may_use
;
4409 spin_lock(&root
->fs_info
->free_chunk_lock
);
4410 avail
= root
->fs_info
->free_chunk_space
;
4411 spin_unlock(&root
->fs_info
->free_chunk_lock
);
4414 * If we have dup, raid1 or raid10 then only half of the free
4415 * space is actually useable. For raid56, the space info used
4416 * doesn't include the parity drive, so we don't have to
4419 if (profile
& (BTRFS_BLOCK_GROUP_DUP
|
4420 BTRFS_BLOCK_GROUP_RAID1
|
4421 BTRFS_BLOCK_GROUP_RAID10
))
4425 * If we aren't flushing all things, let us overcommit up to
4426 * 1/2th of the space. If we can flush, don't let us overcommit
4427 * too much, let it overcommit up to 1/8 of the space.
4429 if (flush
== BTRFS_RESERVE_FLUSH_ALL
)
4434 if (used
+ bytes
< space_info
->total_bytes
+ avail
)
4439 static void btrfs_writeback_inodes_sb_nr(struct btrfs_root
*root
,
4440 unsigned long nr_pages
, int nr_items
)
4442 struct super_block
*sb
= root
->fs_info
->sb
;
4444 if (down_read_trylock(&sb
->s_umount
)) {
4445 writeback_inodes_sb_nr(sb
, nr_pages
, WB_REASON_FS_FREE_SPACE
);
4446 up_read(&sb
->s_umount
);
4449 * We needn't worry the filesystem going from r/w to r/o though
4450 * we don't acquire ->s_umount mutex, because the filesystem
4451 * should guarantee the delalloc inodes list be empty after
4452 * the filesystem is readonly(all dirty pages are written to
4455 btrfs_start_delalloc_roots(root
->fs_info
, 0, nr_items
);
4456 if (!current
->journal_info
)
4457 btrfs_wait_ordered_roots(root
->fs_info
, nr_items
);
4461 static inline int calc_reclaim_items_nr(struct btrfs_root
*root
, u64 to_reclaim
)
4466 bytes
= btrfs_calc_trans_metadata_size(root
, 1);
4467 nr
= (int)div64_u64(to_reclaim
, bytes
);
4473 #define EXTENT_SIZE_PER_ITEM (256 * 1024)
4476 * shrink metadata reservation for delalloc
4478 static void shrink_delalloc(struct btrfs_root
*root
, u64 to_reclaim
, u64 orig
,
4481 struct btrfs_block_rsv
*block_rsv
;
4482 struct btrfs_space_info
*space_info
;
4483 struct btrfs_trans_handle
*trans
;
4487 unsigned long nr_pages
;
4490 enum btrfs_reserve_flush_enum flush
;
4492 /* Calc the number of the pages we need flush for space reservation */
4493 items
= calc_reclaim_items_nr(root
, to_reclaim
);
4494 to_reclaim
= items
* EXTENT_SIZE_PER_ITEM
;
4496 trans
= (struct btrfs_trans_handle
*)current
->journal_info
;
4497 block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
4498 space_info
= block_rsv
->space_info
;
4500 delalloc_bytes
= percpu_counter_sum_positive(
4501 &root
->fs_info
->delalloc_bytes
);
4502 if (delalloc_bytes
== 0) {
4506 btrfs_wait_ordered_roots(root
->fs_info
, items
);
4511 while (delalloc_bytes
&& loops
< 3) {
4512 max_reclaim
= min(delalloc_bytes
, to_reclaim
);
4513 nr_pages
= max_reclaim
>> PAGE_CACHE_SHIFT
;
4514 btrfs_writeback_inodes_sb_nr(root
, nr_pages
, items
);
4516 * We need to wait for the async pages to actually start before
4519 max_reclaim
= atomic_read(&root
->fs_info
->async_delalloc_pages
);
4523 if (max_reclaim
<= nr_pages
)
4526 max_reclaim
-= nr_pages
;
4528 wait_event(root
->fs_info
->async_submit_wait
,
4529 atomic_read(&root
->fs_info
->async_delalloc_pages
) <=
4533 flush
= BTRFS_RESERVE_FLUSH_ALL
;
4535 flush
= BTRFS_RESERVE_NO_FLUSH
;
4536 spin_lock(&space_info
->lock
);
4537 if (can_overcommit(root
, space_info
, orig
, flush
)) {
4538 spin_unlock(&space_info
->lock
);
4541 spin_unlock(&space_info
->lock
);
4544 if (wait_ordered
&& !trans
) {
4545 btrfs_wait_ordered_roots(root
->fs_info
, items
);
4547 time_left
= schedule_timeout_killable(1);
4551 delalloc_bytes
= percpu_counter_sum_positive(
4552 &root
->fs_info
->delalloc_bytes
);
4557 * maybe_commit_transaction - possibly commit the transaction if its ok to
4558 * @root - the root we're allocating for
4559 * @bytes - the number of bytes we want to reserve
4560 * @force - force the commit
4562 * This will check to make sure that committing the transaction will actually
4563 * get us somewhere and then commit the transaction if it does. Otherwise it
4564 * will return -ENOSPC.
4566 static int may_commit_transaction(struct btrfs_root
*root
,
4567 struct btrfs_space_info
*space_info
,
4568 u64 bytes
, int force
)
4570 struct btrfs_block_rsv
*delayed_rsv
= &root
->fs_info
->delayed_block_rsv
;
4571 struct btrfs_trans_handle
*trans
;
4573 trans
= (struct btrfs_trans_handle
*)current
->journal_info
;
4580 /* See if there is enough pinned space to make this reservation */
4581 if (percpu_counter_compare(&space_info
->total_bytes_pinned
,
4586 * See if there is some space in the delayed insertion reservation for
4589 if (space_info
!= delayed_rsv
->space_info
)
4592 spin_lock(&delayed_rsv
->lock
);
4593 if (percpu_counter_compare(&space_info
->total_bytes_pinned
,
4594 bytes
- delayed_rsv
->size
) >= 0) {
4595 spin_unlock(&delayed_rsv
->lock
);
4598 spin_unlock(&delayed_rsv
->lock
);
4601 trans
= btrfs_join_transaction(root
);
4605 return btrfs_commit_transaction(trans
, root
);
4609 FLUSH_DELAYED_ITEMS_NR
= 1,
4610 FLUSH_DELAYED_ITEMS
= 2,
4612 FLUSH_DELALLOC_WAIT
= 4,
4617 static int flush_space(struct btrfs_root
*root
,
4618 struct btrfs_space_info
*space_info
, u64 num_bytes
,
4619 u64 orig_bytes
, int state
)
4621 struct btrfs_trans_handle
*trans
;
4626 case FLUSH_DELAYED_ITEMS_NR
:
4627 case FLUSH_DELAYED_ITEMS
:
4628 if (state
== FLUSH_DELAYED_ITEMS_NR
)
4629 nr
= calc_reclaim_items_nr(root
, num_bytes
) * 2;
4633 trans
= btrfs_join_transaction(root
);
4634 if (IS_ERR(trans
)) {
4635 ret
= PTR_ERR(trans
);
4638 ret
= btrfs_run_delayed_items_nr(trans
, root
, nr
);
4639 btrfs_end_transaction(trans
, root
);
4641 case FLUSH_DELALLOC
:
4642 case FLUSH_DELALLOC_WAIT
:
4643 shrink_delalloc(root
, num_bytes
* 2, orig_bytes
,
4644 state
== FLUSH_DELALLOC_WAIT
);
4647 trans
= btrfs_join_transaction(root
);
4648 if (IS_ERR(trans
)) {
4649 ret
= PTR_ERR(trans
);
4652 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
4653 btrfs_get_alloc_profile(root
, 0),
4654 CHUNK_ALLOC_NO_FORCE
);
4655 btrfs_end_transaction(trans
, root
);
4660 ret
= may_commit_transaction(root
, space_info
, orig_bytes
, 0);
4671 btrfs_calc_reclaim_metadata_size(struct btrfs_root
*root
,
4672 struct btrfs_space_info
*space_info
)
4678 to_reclaim
= min_t(u64
, num_online_cpus() * 1024 * 1024,
4680 spin_lock(&space_info
->lock
);
4681 if (can_overcommit(root
, space_info
, to_reclaim
,
4682 BTRFS_RESERVE_FLUSH_ALL
)) {
4687 used
= space_info
->bytes_used
+ space_info
->bytes_reserved
+
4688 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
4689 space_info
->bytes_may_use
;
4690 if (can_overcommit(root
, space_info
, 1024 * 1024,
4691 BTRFS_RESERVE_FLUSH_ALL
))
4692 expected
= div_factor_fine(space_info
->total_bytes
, 95);
4694 expected
= div_factor_fine(space_info
->total_bytes
, 90);
4696 if (used
> expected
)
4697 to_reclaim
= used
- expected
;
4700 to_reclaim
= min(to_reclaim
, space_info
->bytes_may_use
+
4701 space_info
->bytes_reserved
);
4703 spin_unlock(&space_info
->lock
);
4708 static inline int need_do_async_reclaim(struct btrfs_space_info
*space_info
,
4709 struct btrfs_fs_info
*fs_info
, u64 used
)
4711 u64 thresh
= div_factor_fine(space_info
->total_bytes
, 98);
4713 /* If we're just plain full then async reclaim just slows us down. */
4714 if (space_info
->bytes_used
>= thresh
)
4717 return (used
>= thresh
&& !btrfs_fs_closing(fs_info
) &&
4718 !test_bit(BTRFS_FS_STATE_REMOUNTING
, &fs_info
->fs_state
));
4721 static int btrfs_need_do_async_reclaim(struct btrfs_space_info
*space_info
,
4722 struct btrfs_fs_info
*fs_info
,
4727 spin_lock(&space_info
->lock
);
4729 * We run out of space and have not got any free space via flush_space,
4730 * so don't bother doing async reclaim.
4732 if (flush_state
> COMMIT_TRANS
&& space_info
->full
) {
4733 spin_unlock(&space_info
->lock
);
4737 used
= space_info
->bytes_used
+ space_info
->bytes_reserved
+
4738 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
4739 space_info
->bytes_may_use
;
4740 if (need_do_async_reclaim(space_info
, fs_info
, used
)) {
4741 spin_unlock(&space_info
->lock
);
4744 spin_unlock(&space_info
->lock
);
4749 static void btrfs_async_reclaim_metadata_space(struct work_struct
*work
)
4751 struct btrfs_fs_info
*fs_info
;
4752 struct btrfs_space_info
*space_info
;
4756 fs_info
= container_of(work
, struct btrfs_fs_info
, async_reclaim_work
);
4757 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
4759 to_reclaim
= btrfs_calc_reclaim_metadata_size(fs_info
->fs_root
,
4764 flush_state
= FLUSH_DELAYED_ITEMS_NR
;
4766 flush_space(fs_info
->fs_root
, space_info
, to_reclaim
,
4767 to_reclaim
, flush_state
);
4769 if (!btrfs_need_do_async_reclaim(space_info
, fs_info
,
4772 } while (flush_state
< COMMIT_TRANS
);
4775 void btrfs_init_async_reclaim_work(struct work_struct
*work
)
4777 INIT_WORK(work
, btrfs_async_reclaim_metadata_space
);
4781 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
4782 * @root - the root we're allocating for
4783 * @block_rsv - the block_rsv we're allocating for
4784 * @orig_bytes - the number of bytes we want
4785 * @flush - whether or not we can flush to make our reservation
4787 * This will reserve orgi_bytes number of bytes from the space info associated
4788 * with the block_rsv. If there is not enough space it will make an attempt to
4789 * flush out space to make room. It will do this by flushing delalloc if
4790 * possible or committing the transaction. If flush is 0 then no attempts to
4791 * regain reservations will be made and this will fail if there is not enough
4794 static int reserve_metadata_bytes(struct btrfs_root
*root
,
4795 struct btrfs_block_rsv
*block_rsv
,
4797 enum btrfs_reserve_flush_enum flush
)
4799 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
4801 u64 num_bytes
= orig_bytes
;
4802 int flush_state
= FLUSH_DELAYED_ITEMS_NR
;
4804 bool flushing
= false;
4808 spin_lock(&space_info
->lock
);
4810 * We only want to wait if somebody other than us is flushing and we
4811 * are actually allowed to flush all things.
4813 while (flush
== BTRFS_RESERVE_FLUSH_ALL
&& !flushing
&&
4814 space_info
->flush
) {
4815 spin_unlock(&space_info
->lock
);
4817 * If we have a trans handle we can't wait because the flusher
4818 * may have to commit the transaction, which would mean we would
4819 * deadlock since we are waiting for the flusher to finish, but
4820 * hold the current transaction open.
4822 if (current
->journal_info
)
4824 ret
= wait_event_killable(space_info
->wait
, !space_info
->flush
);
4825 /* Must have been killed, return */
4829 spin_lock(&space_info
->lock
);
4833 used
= space_info
->bytes_used
+ space_info
->bytes_reserved
+
4834 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
4835 space_info
->bytes_may_use
;
4838 * The idea here is that we've not already over-reserved the block group
4839 * then we can go ahead and save our reservation first and then start
4840 * flushing if we need to. Otherwise if we've already overcommitted
4841 * lets start flushing stuff first and then come back and try to make
4844 if (used
<= space_info
->total_bytes
) {
4845 if (used
+ orig_bytes
<= space_info
->total_bytes
) {
4846 space_info
->bytes_may_use
+= orig_bytes
;
4847 trace_btrfs_space_reservation(root
->fs_info
,
4848 "space_info", space_info
->flags
, orig_bytes
, 1);
4852 * Ok set num_bytes to orig_bytes since we aren't
4853 * overocmmitted, this way we only try and reclaim what
4856 num_bytes
= orig_bytes
;
4860 * Ok we're over committed, set num_bytes to the overcommitted
4861 * amount plus the amount of bytes that we need for this
4864 num_bytes
= used
- space_info
->total_bytes
+
4868 if (ret
&& can_overcommit(root
, space_info
, orig_bytes
, flush
)) {
4869 space_info
->bytes_may_use
+= orig_bytes
;
4870 trace_btrfs_space_reservation(root
->fs_info
, "space_info",
4871 space_info
->flags
, orig_bytes
,
4877 * Couldn't make our reservation, save our place so while we're trying
4878 * to reclaim space we can actually use it instead of somebody else
4879 * stealing it from us.
4881 * We make the other tasks wait for the flush only when we can flush
4884 if (ret
&& flush
!= BTRFS_RESERVE_NO_FLUSH
) {
4886 space_info
->flush
= 1;
4887 } else if (!ret
&& space_info
->flags
& BTRFS_BLOCK_GROUP_METADATA
) {
4890 * We will do the space reservation dance during log replay,
4891 * which means we won't have fs_info->fs_root set, so don't do
4892 * the async reclaim as we will panic.
4894 if (!root
->fs_info
->log_root_recovering
&&
4895 need_do_async_reclaim(space_info
, root
->fs_info
, used
) &&
4896 !work_busy(&root
->fs_info
->async_reclaim_work
))
4897 queue_work(system_unbound_wq
,
4898 &root
->fs_info
->async_reclaim_work
);
4900 spin_unlock(&space_info
->lock
);
4902 if (!ret
|| flush
== BTRFS_RESERVE_NO_FLUSH
)
4905 ret
= flush_space(root
, space_info
, num_bytes
, orig_bytes
,
4910 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4911 * would happen. So skip delalloc flush.
4913 if (flush
== BTRFS_RESERVE_FLUSH_LIMIT
&&
4914 (flush_state
== FLUSH_DELALLOC
||
4915 flush_state
== FLUSH_DELALLOC_WAIT
))
4916 flush_state
= ALLOC_CHUNK
;
4920 else if (flush
== BTRFS_RESERVE_FLUSH_LIMIT
&&
4921 flush_state
< COMMIT_TRANS
)
4923 else if (flush
== BTRFS_RESERVE_FLUSH_ALL
&&
4924 flush_state
<= COMMIT_TRANS
)
4928 if (ret
== -ENOSPC
&&
4929 unlikely(root
->orphan_cleanup_state
== ORPHAN_CLEANUP_STARTED
)) {
4930 struct btrfs_block_rsv
*global_rsv
=
4931 &root
->fs_info
->global_block_rsv
;
4933 if (block_rsv
!= global_rsv
&&
4934 !block_rsv_use_bytes(global_rsv
, orig_bytes
))
4938 trace_btrfs_space_reservation(root
->fs_info
,
4939 "space_info:enospc",
4940 space_info
->flags
, orig_bytes
, 1);
4942 spin_lock(&space_info
->lock
);
4943 space_info
->flush
= 0;
4944 wake_up_all(&space_info
->wait
);
4945 spin_unlock(&space_info
->lock
);
4950 static struct btrfs_block_rsv
*get_block_rsv(
4951 const struct btrfs_trans_handle
*trans
,
4952 const struct btrfs_root
*root
)
4954 struct btrfs_block_rsv
*block_rsv
= NULL
;
4956 if (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) ||
4957 (root
== root
->fs_info
->csum_root
&& trans
->adding_csums
) ||
4958 (root
== root
->fs_info
->uuid_root
))
4959 block_rsv
= trans
->block_rsv
;
4962 block_rsv
= root
->block_rsv
;
4965 block_rsv
= &root
->fs_info
->empty_block_rsv
;
4970 static int block_rsv_use_bytes(struct btrfs_block_rsv
*block_rsv
,
4974 spin_lock(&block_rsv
->lock
);
4975 if (block_rsv
->reserved
>= num_bytes
) {
4976 block_rsv
->reserved
-= num_bytes
;
4977 if (block_rsv
->reserved
< block_rsv
->size
)
4978 block_rsv
->full
= 0;
4981 spin_unlock(&block_rsv
->lock
);
4985 static void block_rsv_add_bytes(struct btrfs_block_rsv
*block_rsv
,
4986 u64 num_bytes
, int update_size
)
4988 spin_lock(&block_rsv
->lock
);
4989 block_rsv
->reserved
+= num_bytes
;
4991 block_rsv
->size
+= num_bytes
;
4992 else if (block_rsv
->reserved
>= block_rsv
->size
)
4993 block_rsv
->full
= 1;
4994 spin_unlock(&block_rsv
->lock
);
4997 int btrfs_cond_migrate_bytes(struct btrfs_fs_info
*fs_info
,
4998 struct btrfs_block_rsv
*dest
, u64 num_bytes
,
5001 struct btrfs_block_rsv
*global_rsv
= &fs_info
->global_block_rsv
;
5004 if (global_rsv
->space_info
!= dest
->space_info
)
5007 spin_lock(&global_rsv
->lock
);
5008 min_bytes
= div_factor(global_rsv
->size
, min_factor
);
5009 if (global_rsv
->reserved
< min_bytes
+ num_bytes
) {
5010 spin_unlock(&global_rsv
->lock
);
5013 global_rsv
->reserved
-= num_bytes
;
5014 if (global_rsv
->reserved
< global_rsv
->size
)
5015 global_rsv
->full
= 0;
5016 spin_unlock(&global_rsv
->lock
);
5018 block_rsv_add_bytes(dest
, num_bytes
, 1);
5022 static void block_rsv_release_bytes(struct btrfs_fs_info
*fs_info
,
5023 struct btrfs_block_rsv
*block_rsv
,
5024 struct btrfs_block_rsv
*dest
, u64 num_bytes
)
5026 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
5028 spin_lock(&block_rsv
->lock
);
5029 if (num_bytes
== (u64
)-1)
5030 num_bytes
= block_rsv
->size
;
5031 block_rsv
->size
-= num_bytes
;
5032 if (block_rsv
->reserved
>= block_rsv
->size
) {
5033 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
5034 block_rsv
->reserved
= block_rsv
->size
;
5035 block_rsv
->full
= 1;
5039 spin_unlock(&block_rsv
->lock
);
5041 if (num_bytes
> 0) {
5043 spin_lock(&dest
->lock
);
5047 bytes_to_add
= dest
->size
- dest
->reserved
;
5048 bytes_to_add
= min(num_bytes
, bytes_to_add
);
5049 dest
->reserved
+= bytes_to_add
;
5050 if (dest
->reserved
>= dest
->size
)
5052 num_bytes
-= bytes_to_add
;
5054 spin_unlock(&dest
->lock
);
5057 spin_lock(&space_info
->lock
);
5058 space_info
->bytes_may_use
-= num_bytes
;
5059 trace_btrfs_space_reservation(fs_info
, "space_info",
5060 space_info
->flags
, num_bytes
, 0);
5061 spin_unlock(&space_info
->lock
);
5066 static int block_rsv_migrate_bytes(struct btrfs_block_rsv
*src
,
5067 struct btrfs_block_rsv
*dst
, u64 num_bytes
)
5071 ret
= block_rsv_use_bytes(src
, num_bytes
);
5075 block_rsv_add_bytes(dst
, num_bytes
, 1);
5079 void btrfs_init_block_rsv(struct btrfs_block_rsv
*rsv
, unsigned short type
)
5081 memset(rsv
, 0, sizeof(*rsv
));
5082 spin_lock_init(&rsv
->lock
);
5086 struct btrfs_block_rsv
*btrfs_alloc_block_rsv(struct btrfs_root
*root
,
5087 unsigned short type
)
5089 struct btrfs_block_rsv
*block_rsv
;
5090 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5092 block_rsv
= kmalloc(sizeof(*block_rsv
), GFP_NOFS
);
5096 btrfs_init_block_rsv(block_rsv
, type
);
5097 block_rsv
->space_info
= __find_space_info(fs_info
,
5098 BTRFS_BLOCK_GROUP_METADATA
);
5102 void btrfs_free_block_rsv(struct btrfs_root
*root
,
5103 struct btrfs_block_rsv
*rsv
)
5107 btrfs_block_rsv_release(root
, rsv
, (u64
)-1);
5111 void __btrfs_free_block_rsv(struct btrfs_block_rsv
*rsv
)
5116 int btrfs_block_rsv_add(struct btrfs_root
*root
,
5117 struct btrfs_block_rsv
*block_rsv
, u64 num_bytes
,
5118 enum btrfs_reserve_flush_enum flush
)
5125 ret
= reserve_metadata_bytes(root
, block_rsv
, num_bytes
, flush
);
5127 block_rsv_add_bytes(block_rsv
, num_bytes
, 1);
5134 int btrfs_block_rsv_check(struct btrfs_root
*root
,
5135 struct btrfs_block_rsv
*block_rsv
, int min_factor
)
5143 spin_lock(&block_rsv
->lock
);
5144 num_bytes
= div_factor(block_rsv
->size
, min_factor
);
5145 if (block_rsv
->reserved
>= num_bytes
)
5147 spin_unlock(&block_rsv
->lock
);
5152 int btrfs_block_rsv_refill(struct btrfs_root
*root
,
5153 struct btrfs_block_rsv
*block_rsv
, u64 min_reserved
,
5154 enum btrfs_reserve_flush_enum flush
)
5162 spin_lock(&block_rsv
->lock
);
5163 num_bytes
= min_reserved
;
5164 if (block_rsv
->reserved
>= num_bytes
)
5167 num_bytes
-= block_rsv
->reserved
;
5168 spin_unlock(&block_rsv
->lock
);
5173 ret
= reserve_metadata_bytes(root
, block_rsv
, num_bytes
, flush
);
5175 block_rsv_add_bytes(block_rsv
, num_bytes
, 0);
5182 int btrfs_block_rsv_migrate(struct btrfs_block_rsv
*src_rsv
,
5183 struct btrfs_block_rsv
*dst_rsv
,
5186 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
5189 void btrfs_block_rsv_release(struct btrfs_root
*root
,
5190 struct btrfs_block_rsv
*block_rsv
,
5193 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
5194 if (global_rsv
== block_rsv
||
5195 block_rsv
->space_info
!= global_rsv
->space_info
)
5197 block_rsv_release_bytes(root
->fs_info
, block_rsv
, global_rsv
,
5202 * helper to calculate size of global block reservation.
5203 * the desired value is sum of space used by extent tree,
5204 * checksum tree and root tree
5206 static u64
calc_global_metadata_size(struct btrfs_fs_info
*fs_info
)
5208 struct btrfs_space_info
*sinfo
;
5212 int csum_size
= btrfs_super_csum_size(fs_info
->super_copy
);
5214 sinfo
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_DATA
);
5215 spin_lock(&sinfo
->lock
);
5216 data_used
= sinfo
->bytes_used
;
5217 spin_unlock(&sinfo
->lock
);
5219 sinfo
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
5220 spin_lock(&sinfo
->lock
);
5221 if (sinfo
->flags
& BTRFS_BLOCK_GROUP_DATA
)
5223 meta_used
= sinfo
->bytes_used
;
5224 spin_unlock(&sinfo
->lock
);
5226 num_bytes
= (data_used
>> fs_info
->sb
->s_blocksize_bits
) *
5228 num_bytes
+= div_u64(data_used
+ meta_used
, 50);
5230 if (num_bytes
* 3 > meta_used
)
5231 num_bytes
= div_u64(meta_used
, 3);
5233 return ALIGN(num_bytes
, fs_info
->extent_root
->nodesize
<< 10);
5236 static void update_global_block_rsv(struct btrfs_fs_info
*fs_info
)
5238 struct btrfs_block_rsv
*block_rsv
= &fs_info
->global_block_rsv
;
5239 struct btrfs_space_info
*sinfo
= block_rsv
->space_info
;
5242 num_bytes
= calc_global_metadata_size(fs_info
);
5244 spin_lock(&sinfo
->lock
);
5245 spin_lock(&block_rsv
->lock
);
5247 block_rsv
->size
= min_t(u64
, num_bytes
, 512 * 1024 * 1024);
5249 num_bytes
= sinfo
->bytes_used
+ sinfo
->bytes_pinned
+
5250 sinfo
->bytes_reserved
+ sinfo
->bytes_readonly
+
5251 sinfo
->bytes_may_use
;
5253 if (sinfo
->total_bytes
> num_bytes
) {
5254 num_bytes
= sinfo
->total_bytes
- num_bytes
;
5255 block_rsv
->reserved
+= num_bytes
;
5256 sinfo
->bytes_may_use
+= num_bytes
;
5257 trace_btrfs_space_reservation(fs_info
, "space_info",
5258 sinfo
->flags
, num_bytes
, 1);
5261 if (block_rsv
->reserved
>= block_rsv
->size
) {
5262 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
5263 sinfo
->bytes_may_use
-= num_bytes
;
5264 trace_btrfs_space_reservation(fs_info
, "space_info",
5265 sinfo
->flags
, num_bytes
, 0);
5266 block_rsv
->reserved
= block_rsv
->size
;
5267 block_rsv
->full
= 1;
5270 spin_unlock(&block_rsv
->lock
);
5271 spin_unlock(&sinfo
->lock
);
5274 static void init_global_block_rsv(struct btrfs_fs_info
*fs_info
)
5276 struct btrfs_space_info
*space_info
;
5278 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_SYSTEM
);
5279 fs_info
->chunk_block_rsv
.space_info
= space_info
;
5281 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
5282 fs_info
->global_block_rsv
.space_info
= space_info
;
5283 fs_info
->delalloc_block_rsv
.space_info
= space_info
;
5284 fs_info
->trans_block_rsv
.space_info
= space_info
;
5285 fs_info
->empty_block_rsv
.space_info
= space_info
;
5286 fs_info
->delayed_block_rsv
.space_info
= space_info
;
5288 fs_info
->extent_root
->block_rsv
= &fs_info
->global_block_rsv
;
5289 fs_info
->csum_root
->block_rsv
= &fs_info
->global_block_rsv
;
5290 fs_info
->dev_root
->block_rsv
= &fs_info
->global_block_rsv
;
5291 fs_info
->tree_root
->block_rsv
= &fs_info
->global_block_rsv
;
5292 if (fs_info
->quota_root
)
5293 fs_info
->quota_root
->block_rsv
= &fs_info
->global_block_rsv
;
5294 fs_info
->chunk_root
->block_rsv
= &fs_info
->chunk_block_rsv
;
5296 update_global_block_rsv(fs_info
);
5299 static void release_global_block_rsv(struct btrfs_fs_info
*fs_info
)
5301 block_rsv_release_bytes(fs_info
, &fs_info
->global_block_rsv
, NULL
,
5303 WARN_ON(fs_info
->delalloc_block_rsv
.size
> 0);
5304 WARN_ON(fs_info
->delalloc_block_rsv
.reserved
> 0);
5305 WARN_ON(fs_info
->trans_block_rsv
.size
> 0);
5306 WARN_ON(fs_info
->trans_block_rsv
.reserved
> 0);
5307 WARN_ON(fs_info
->chunk_block_rsv
.size
> 0);
5308 WARN_ON(fs_info
->chunk_block_rsv
.reserved
> 0);
5309 WARN_ON(fs_info
->delayed_block_rsv
.size
> 0);
5310 WARN_ON(fs_info
->delayed_block_rsv
.reserved
> 0);
5313 void btrfs_trans_release_metadata(struct btrfs_trans_handle
*trans
,
5314 struct btrfs_root
*root
)
5316 if (!trans
->block_rsv
)
5319 if (!trans
->bytes_reserved
)
5322 trace_btrfs_space_reservation(root
->fs_info
, "transaction",
5323 trans
->transid
, trans
->bytes_reserved
, 0);
5324 btrfs_block_rsv_release(root
, trans
->block_rsv
, trans
->bytes_reserved
);
5325 trans
->bytes_reserved
= 0;
5329 * To be called after all the new block groups attached to the transaction
5330 * handle have been created (btrfs_create_pending_block_groups()).
5332 void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle
*trans
)
5334 struct btrfs_fs_info
*fs_info
= trans
->root
->fs_info
;
5336 if (!trans
->chunk_bytes_reserved
)
5339 WARN_ON_ONCE(!list_empty(&trans
->new_bgs
));
5341 block_rsv_release_bytes(fs_info
, &fs_info
->chunk_block_rsv
, NULL
,
5342 trans
->chunk_bytes_reserved
);
5343 trans
->chunk_bytes_reserved
= 0;
5346 /* Can only return 0 or -ENOSPC */
5347 int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle
*trans
,
5348 struct inode
*inode
)
5350 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5351 struct btrfs_block_rsv
*src_rsv
= get_block_rsv(trans
, root
);
5352 struct btrfs_block_rsv
*dst_rsv
= root
->orphan_block_rsv
;
5355 * We need to hold space in order to delete our orphan item once we've
5356 * added it, so this takes the reservation so we can release it later
5357 * when we are truly done with the orphan item.
5359 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 1);
5360 trace_btrfs_space_reservation(root
->fs_info
, "orphan",
5361 btrfs_ino(inode
), num_bytes
, 1);
5362 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
5365 void btrfs_orphan_release_metadata(struct inode
*inode
)
5367 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5368 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 1);
5369 trace_btrfs_space_reservation(root
->fs_info
, "orphan",
5370 btrfs_ino(inode
), num_bytes
, 0);
5371 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
, num_bytes
);
5375 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
5376 * root: the root of the parent directory
5377 * rsv: block reservation
5378 * items: the number of items that we need do reservation
5379 * qgroup_reserved: used to return the reserved size in qgroup
5381 * This function is used to reserve the space for snapshot/subvolume
5382 * creation and deletion. Those operations are different with the
5383 * common file/directory operations, they change two fs/file trees
5384 * and root tree, the number of items that the qgroup reserves is
5385 * different with the free space reservation. So we can not use
5386 * the space reseravtion mechanism in start_transaction().
5388 int btrfs_subvolume_reserve_metadata(struct btrfs_root
*root
,
5389 struct btrfs_block_rsv
*rsv
,
5391 u64
*qgroup_reserved
,
5392 bool use_global_rsv
)
5396 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
5398 if (root
->fs_info
->quota_enabled
) {
5399 /* One for parent inode, two for dir entries */
5400 num_bytes
= 3 * root
->nodesize
;
5401 ret
= btrfs_qgroup_reserve(root
, num_bytes
);
5408 *qgroup_reserved
= num_bytes
;
5410 num_bytes
= btrfs_calc_trans_metadata_size(root
, items
);
5411 rsv
->space_info
= __find_space_info(root
->fs_info
,
5412 BTRFS_BLOCK_GROUP_METADATA
);
5413 ret
= btrfs_block_rsv_add(root
, rsv
, num_bytes
,
5414 BTRFS_RESERVE_FLUSH_ALL
);
5416 if (ret
== -ENOSPC
&& use_global_rsv
)
5417 ret
= btrfs_block_rsv_migrate(global_rsv
, rsv
, num_bytes
);
5420 if (*qgroup_reserved
)
5421 btrfs_qgroup_free(root
, *qgroup_reserved
);
5427 void btrfs_subvolume_release_metadata(struct btrfs_root
*root
,
5428 struct btrfs_block_rsv
*rsv
,
5429 u64 qgroup_reserved
)
5431 btrfs_block_rsv_release(root
, rsv
, (u64
)-1);
5435 * drop_outstanding_extent - drop an outstanding extent
5436 * @inode: the inode we're dropping the extent for
5437 * @num_bytes: the number of bytes we're relaseing.
5439 * This is called when we are freeing up an outstanding extent, either called
5440 * after an error or after an extent is written. This will return the number of
5441 * reserved extents that need to be freed. This must be called with
5442 * BTRFS_I(inode)->lock held.
5444 static unsigned drop_outstanding_extent(struct inode
*inode
, u64 num_bytes
)
5446 unsigned drop_inode_space
= 0;
5447 unsigned dropped_extents
= 0;
5448 unsigned num_extents
= 0;
5450 num_extents
= (unsigned)div64_u64(num_bytes
+
5451 BTRFS_MAX_EXTENT_SIZE
- 1,
5452 BTRFS_MAX_EXTENT_SIZE
);
5453 ASSERT(num_extents
);
5454 ASSERT(BTRFS_I(inode
)->outstanding_extents
>= num_extents
);
5455 BTRFS_I(inode
)->outstanding_extents
-= num_extents
;
5457 if (BTRFS_I(inode
)->outstanding_extents
== 0 &&
5458 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED
,
5459 &BTRFS_I(inode
)->runtime_flags
))
5460 drop_inode_space
= 1;
5463 * If we have more or the same amount of outsanding extents than we have
5464 * reserved then we need to leave the reserved extents count alone.
5466 if (BTRFS_I(inode
)->outstanding_extents
>=
5467 BTRFS_I(inode
)->reserved_extents
)
5468 return drop_inode_space
;
5470 dropped_extents
= BTRFS_I(inode
)->reserved_extents
-
5471 BTRFS_I(inode
)->outstanding_extents
;
5472 BTRFS_I(inode
)->reserved_extents
-= dropped_extents
;
5473 return dropped_extents
+ drop_inode_space
;
5477 * calc_csum_metadata_size - return the amount of metada space that must be
5478 * reserved/free'd for the given bytes.
5479 * @inode: the inode we're manipulating
5480 * @num_bytes: the number of bytes in question
5481 * @reserve: 1 if we are reserving space, 0 if we are freeing space
5483 * This adjusts the number of csum_bytes in the inode and then returns the
5484 * correct amount of metadata that must either be reserved or freed. We
5485 * calculate how many checksums we can fit into one leaf and then divide the
5486 * number of bytes that will need to be checksumed by this value to figure out
5487 * how many checksums will be required. If we are adding bytes then the number
5488 * may go up and we will return the number of additional bytes that must be
5489 * reserved. If it is going down we will return the number of bytes that must
5492 * This must be called with BTRFS_I(inode)->lock held.
5494 static u64
calc_csum_metadata_size(struct inode
*inode
, u64 num_bytes
,
5497 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5498 u64 old_csums
, num_csums
;
5500 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
&&
5501 BTRFS_I(inode
)->csum_bytes
== 0)
5504 old_csums
= btrfs_csum_bytes_to_leaves(root
, BTRFS_I(inode
)->csum_bytes
);
5506 BTRFS_I(inode
)->csum_bytes
+= num_bytes
;
5508 BTRFS_I(inode
)->csum_bytes
-= num_bytes
;
5509 num_csums
= btrfs_csum_bytes_to_leaves(root
, BTRFS_I(inode
)->csum_bytes
);
5511 /* No change, no need to reserve more */
5512 if (old_csums
== num_csums
)
5516 return btrfs_calc_trans_metadata_size(root
,
5517 num_csums
- old_csums
);
5519 return btrfs_calc_trans_metadata_size(root
, old_csums
- num_csums
);
5522 int btrfs_delalloc_reserve_metadata(struct inode
*inode
, u64 num_bytes
)
5524 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5525 struct btrfs_block_rsv
*block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
5528 unsigned nr_extents
= 0;
5529 int extra_reserve
= 0;
5530 enum btrfs_reserve_flush_enum flush
= BTRFS_RESERVE_FLUSH_ALL
;
5532 bool delalloc_lock
= true;
5536 /* If we are a free space inode we need to not flush since we will be in
5537 * the middle of a transaction commit. We also don't need the delalloc
5538 * mutex since we won't race with anybody. We need this mostly to make
5539 * lockdep shut its filthy mouth.
5541 if (btrfs_is_free_space_inode(inode
)) {
5542 flush
= BTRFS_RESERVE_NO_FLUSH
;
5543 delalloc_lock
= false;
5546 if (flush
!= BTRFS_RESERVE_NO_FLUSH
&&
5547 btrfs_transaction_in_commit(root
->fs_info
))
5548 schedule_timeout(1);
5551 mutex_lock(&BTRFS_I(inode
)->delalloc_mutex
);
5553 num_bytes
= ALIGN(num_bytes
, root
->sectorsize
);
5555 spin_lock(&BTRFS_I(inode
)->lock
);
5556 nr_extents
= (unsigned)div64_u64(num_bytes
+
5557 BTRFS_MAX_EXTENT_SIZE
- 1,
5558 BTRFS_MAX_EXTENT_SIZE
);
5559 BTRFS_I(inode
)->outstanding_extents
+= nr_extents
;
5562 if (BTRFS_I(inode
)->outstanding_extents
>
5563 BTRFS_I(inode
)->reserved_extents
)
5564 nr_extents
= BTRFS_I(inode
)->outstanding_extents
-
5565 BTRFS_I(inode
)->reserved_extents
;
5568 * Add an item to reserve for updating the inode when we complete the
5571 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED
,
5572 &BTRFS_I(inode
)->runtime_flags
)) {
5577 to_reserve
= btrfs_calc_trans_metadata_size(root
, nr_extents
);
5578 to_reserve
+= calc_csum_metadata_size(inode
, num_bytes
, 1);
5579 csum_bytes
= BTRFS_I(inode
)->csum_bytes
;
5580 spin_unlock(&BTRFS_I(inode
)->lock
);
5582 if (root
->fs_info
->quota_enabled
) {
5583 ret
= btrfs_qgroup_reserve(root
, nr_extents
* root
->nodesize
);
5588 ret
= reserve_metadata_bytes(root
, block_rsv
, to_reserve
, flush
);
5589 if (unlikely(ret
)) {
5590 if (root
->fs_info
->quota_enabled
)
5591 btrfs_qgroup_free(root
, nr_extents
* root
->nodesize
);
5595 spin_lock(&BTRFS_I(inode
)->lock
);
5596 if (extra_reserve
) {
5597 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED
,
5598 &BTRFS_I(inode
)->runtime_flags
);
5601 BTRFS_I(inode
)->reserved_extents
+= nr_extents
;
5602 spin_unlock(&BTRFS_I(inode
)->lock
);
5605 mutex_unlock(&BTRFS_I(inode
)->delalloc_mutex
);
5608 trace_btrfs_space_reservation(root
->fs_info
, "delalloc",
5609 btrfs_ino(inode
), to_reserve
, 1);
5610 block_rsv_add_bytes(block_rsv
, to_reserve
, 1);
5615 spin_lock(&BTRFS_I(inode
)->lock
);
5616 dropped
= drop_outstanding_extent(inode
, num_bytes
);
5618 * If the inodes csum_bytes is the same as the original
5619 * csum_bytes then we know we haven't raced with any free()ers
5620 * so we can just reduce our inodes csum bytes and carry on.
5622 if (BTRFS_I(inode
)->csum_bytes
== csum_bytes
) {
5623 calc_csum_metadata_size(inode
, num_bytes
, 0);
5625 u64 orig_csum_bytes
= BTRFS_I(inode
)->csum_bytes
;
5629 * This is tricky, but first we need to figure out how much we
5630 * free'd from any free-ers that occured during this
5631 * reservation, so we reset ->csum_bytes to the csum_bytes
5632 * before we dropped our lock, and then call the free for the
5633 * number of bytes that were freed while we were trying our
5636 bytes
= csum_bytes
- BTRFS_I(inode
)->csum_bytes
;
5637 BTRFS_I(inode
)->csum_bytes
= csum_bytes
;
5638 to_free
= calc_csum_metadata_size(inode
, bytes
, 0);
5642 * Now we need to see how much we would have freed had we not
5643 * been making this reservation and our ->csum_bytes were not
5644 * artificially inflated.
5646 BTRFS_I(inode
)->csum_bytes
= csum_bytes
- num_bytes
;
5647 bytes
= csum_bytes
- orig_csum_bytes
;
5648 bytes
= calc_csum_metadata_size(inode
, bytes
, 0);
5651 * Now reset ->csum_bytes to what it should be. If bytes is
5652 * more than to_free then we would have free'd more space had we
5653 * not had an artificially high ->csum_bytes, so we need to free
5654 * the remainder. If bytes is the same or less then we don't
5655 * need to do anything, the other free-ers did the correct
5658 BTRFS_I(inode
)->csum_bytes
= orig_csum_bytes
- num_bytes
;
5659 if (bytes
> to_free
)
5660 to_free
= bytes
- to_free
;
5664 spin_unlock(&BTRFS_I(inode
)->lock
);
5666 to_free
+= btrfs_calc_trans_metadata_size(root
, dropped
);
5669 btrfs_block_rsv_release(root
, block_rsv
, to_free
);
5670 trace_btrfs_space_reservation(root
->fs_info
, "delalloc",
5671 btrfs_ino(inode
), to_free
, 0);
5674 mutex_unlock(&BTRFS_I(inode
)->delalloc_mutex
);
5679 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
5680 * @inode: the inode to release the reservation for
5681 * @num_bytes: the number of bytes we're releasing
5683 * This will release the metadata reservation for an inode. This can be called
5684 * once we complete IO for a given set of bytes to release their metadata
5687 void btrfs_delalloc_release_metadata(struct inode
*inode
, u64 num_bytes
)
5689 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5693 num_bytes
= ALIGN(num_bytes
, root
->sectorsize
);
5694 spin_lock(&BTRFS_I(inode
)->lock
);
5695 dropped
= drop_outstanding_extent(inode
, num_bytes
);
5698 to_free
= calc_csum_metadata_size(inode
, num_bytes
, 0);
5699 spin_unlock(&BTRFS_I(inode
)->lock
);
5701 to_free
+= btrfs_calc_trans_metadata_size(root
, dropped
);
5703 if (btrfs_test_is_dummy_root(root
))
5706 trace_btrfs_space_reservation(root
->fs_info
, "delalloc",
5707 btrfs_ino(inode
), to_free
, 0);
5709 btrfs_block_rsv_release(root
, &root
->fs_info
->delalloc_block_rsv
,
5714 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
5715 * @inode: inode we're writing to
5716 * @num_bytes: the number of bytes we want to allocate
5718 * This will do the following things
5720 * o reserve space in the data space info for num_bytes
5721 * o reserve space in the metadata space info based on number of outstanding
5722 * extents and how much csums will be needed
5723 * o add to the inodes ->delalloc_bytes
5724 * o add it to the fs_info's delalloc inodes list.
5726 * This will return 0 for success and -ENOSPC if there is no space left.
5728 int btrfs_delalloc_reserve_space(struct inode
*inode
, u64 num_bytes
)
5732 ret
= btrfs_check_data_free_space(inode
, num_bytes
, num_bytes
);
5736 ret
= btrfs_delalloc_reserve_metadata(inode
, num_bytes
);
5738 btrfs_free_reserved_data_space(inode
, num_bytes
);
5746 * btrfs_delalloc_release_space - release data and metadata space for delalloc
5747 * @inode: inode we're releasing space for
5748 * @num_bytes: the number of bytes we want to free up
5750 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
5751 * called in the case that we don't need the metadata AND data reservations
5752 * anymore. So if there is an error or we insert an inline extent.
5754 * This function will release the metadata space that was not used and will
5755 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
5756 * list if there are no delalloc bytes left.
5758 void btrfs_delalloc_release_space(struct inode
*inode
, u64 num_bytes
)
5760 btrfs_delalloc_release_metadata(inode
, num_bytes
);
5761 btrfs_free_reserved_data_space(inode
, num_bytes
);
5764 static int update_block_group(struct btrfs_trans_handle
*trans
,
5765 struct btrfs_root
*root
, u64 bytenr
,
5766 u64 num_bytes
, int alloc
)
5768 struct btrfs_block_group_cache
*cache
= NULL
;
5769 struct btrfs_fs_info
*info
= root
->fs_info
;
5770 u64 total
= num_bytes
;
5775 /* block accounting for super block */
5776 spin_lock(&info
->delalloc_root_lock
);
5777 old_val
= btrfs_super_bytes_used(info
->super_copy
);
5779 old_val
+= num_bytes
;
5781 old_val
-= num_bytes
;
5782 btrfs_set_super_bytes_used(info
->super_copy
, old_val
);
5783 spin_unlock(&info
->delalloc_root_lock
);
5786 cache
= btrfs_lookup_block_group(info
, bytenr
);
5789 if (cache
->flags
& (BTRFS_BLOCK_GROUP_DUP
|
5790 BTRFS_BLOCK_GROUP_RAID1
|
5791 BTRFS_BLOCK_GROUP_RAID10
))
5796 * If this block group has free space cache written out, we
5797 * need to make sure to load it if we are removing space. This
5798 * is because we need the unpinning stage to actually add the
5799 * space back to the block group, otherwise we will leak space.
5801 if (!alloc
&& cache
->cached
== BTRFS_CACHE_NO
)
5802 cache_block_group(cache
, 1);
5804 byte_in_group
= bytenr
- cache
->key
.objectid
;
5805 WARN_ON(byte_in_group
> cache
->key
.offset
);
5807 spin_lock(&cache
->space_info
->lock
);
5808 spin_lock(&cache
->lock
);
5810 if (btrfs_test_opt(root
, SPACE_CACHE
) &&
5811 cache
->disk_cache_state
< BTRFS_DC_CLEAR
)
5812 cache
->disk_cache_state
= BTRFS_DC_CLEAR
;
5814 old_val
= btrfs_block_group_used(&cache
->item
);
5815 num_bytes
= min(total
, cache
->key
.offset
- byte_in_group
);
5817 old_val
+= num_bytes
;
5818 btrfs_set_block_group_used(&cache
->item
, old_val
);
5819 cache
->reserved
-= num_bytes
;
5820 cache
->space_info
->bytes_reserved
-= num_bytes
;
5821 cache
->space_info
->bytes_used
+= num_bytes
;
5822 cache
->space_info
->disk_used
+= num_bytes
* factor
;
5823 spin_unlock(&cache
->lock
);
5824 spin_unlock(&cache
->space_info
->lock
);
5826 old_val
-= num_bytes
;
5827 btrfs_set_block_group_used(&cache
->item
, old_val
);
5828 cache
->pinned
+= num_bytes
;
5829 cache
->space_info
->bytes_pinned
+= num_bytes
;
5830 cache
->space_info
->bytes_used
-= num_bytes
;
5831 cache
->space_info
->disk_used
-= num_bytes
* factor
;
5832 spin_unlock(&cache
->lock
);
5833 spin_unlock(&cache
->space_info
->lock
);
5835 set_extent_dirty(info
->pinned_extents
,
5836 bytenr
, bytenr
+ num_bytes
- 1,
5837 GFP_NOFS
| __GFP_NOFAIL
);
5839 * No longer have used bytes in this block group, queue
5843 spin_lock(&info
->unused_bgs_lock
);
5844 if (list_empty(&cache
->bg_list
)) {
5845 btrfs_get_block_group(cache
);
5846 list_add_tail(&cache
->bg_list
,
5849 spin_unlock(&info
->unused_bgs_lock
);
5853 spin_lock(&trans
->transaction
->dirty_bgs_lock
);
5854 if (list_empty(&cache
->dirty_list
)) {
5855 list_add_tail(&cache
->dirty_list
,
5856 &trans
->transaction
->dirty_bgs
);
5857 trans
->transaction
->num_dirty_bgs
++;
5858 btrfs_get_block_group(cache
);
5860 spin_unlock(&trans
->transaction
->dirty_bgs_lock
);
5862 btrfs_put_block_group(cache
);
5864 bytenr
+= num_bytes
;
5869 static u64
first_logical_byte(struct btrfs_root
*root
, u64 search_start
)
5871 struct btrfs_block_group_cache
*cache
;
5874 spin_lock(&root
->fs_info
->block_group_cache_lock
);
5875 bytenr
= root
->fs_info
->first_logical_byte
;
5876 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
5878 if (bytenr
< (u64
)-1)
5881 cache
= btrfs_lookup_first_block_group(root
->fs_info
, search_start
);
5885 bytenr
= cache
->key
.objectid
;
5886 btrfs_put_block_group(cache
);
5891 static int pin_down_extent(struct btrfs_root
*root
,
5892 struct btrfs_block_group_cache
*cache
,
5893 u64 bytenr
, u64 num_bytes
, int reserved
)
5895 spin_lock(&cache
->space_info
->lock
);
5896 spin_lock(&cache
->lock
);
5897 cache
->pinned
+= num_bytes
;
5898 cache
->space_info
->bytes_pinned
+= num_bytes
;
5900 cache
->reserved
-= num_bytes
;
5901 cache
->space_info
->bytes_reserved
-= num_bytes
;
5903 spin_unlock(&cache
->lock
);
5904 spin_unlock(&cache
->space_info
->lock
);
5906 set_extent_dirty(root
->fs_info
->pinned_extents
, bytenr
,
5907 bytenr
+ num_bytes
- 1, GFP_NOFS
| __GFP_NOFAIL
);
5909 trace_btrfs_reserved_extent_free(root
, bytenr
, num_bytes
);
5914 * this function must be called within transaction
5916 int btrfs_pin_extent(struct btrfs_root
*root
,
5917 u64 bytenr
, u64 num_bytes
, int reserved
)
5919 struct btrfs_block_group_cache
*cache
;
5921 cache
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
5922 BUG_ON(!cache
); /* Logic error */
5924 pin_down_extent(root
, cache
, bytenr
, num_bytes
, reserved
);
5926 btrfs_put_block_group(cache
);
5931 * this function must be called within transaction
5933 int btrfs_pin_extent_for_log_replay(struct btrfs_root
*root
,
5934 u64 bytenr
, u64 num_bytes
)
5936 struct btrfs_block_group_cache
*cache
;
5939 cache
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
5944 * pull in the free space cache (if any) so that our pin
5945 * removes the free space from the cache. We have load_only set
5946 * to one because the slow code to read in the free extents does check
5947 * the pinned extents.
5949 cache_block_group(cache
, 1);
5951 pin_down_extent(root
, cache
, bytenr
, num_bytes
, 0);
5953 /* remove us from the free space cache (if we're there at all) */
5954 ret
= btrfs_remove_free_space(cache
, bytenr
, num_bytes
);
5955 btrfs_put_block_group(cache
);
5959 static int __exclude_logged_extent(struct btrfs_root
*root
, u64 start
, u64 num_bytes
)
5962 struct btrfs_block_group_cache
*block_group
;
5963 struct btrfs_caching_control
*caching_ctl
;
5965 block_group
= btrfs_lookup_block_group(root
->fs_info
, start
);
5969 cache_block_group(block_group
, 0);
5970 caching_ctl
= get_caching_control(block_group
);
5974 BUG_ON(!block_group_cache_done(block_group
));
5975 ret
= btrfs_remove_free_space(block_group
, start
, num_bytes
);
5977 mutex_lock(&caching_ctl
->mutex
);
5979 if (start
>= caching_ctl
->progress
) {
5980 ret
= add_excluded_extent(root
, start
, num_bytes
);
5981 } else if (start
+ num_bytes
<= caching_ctl
->progress
) {
5982 ret
= btrfs_remove_free_space(block_group
,
5985 num_bytes
= caching_ctl
->progress
- start
;
5986 ret
= btrfs_remove_free_space(block_group
,
5991 num_bytes
= (start
+ num_bytes
) -
5992 caching_ctl
->progress
;
5993 start
= caching_ctl
->progress
;
5994 ret
= add_excluded_extent(root
, start
, num_bytes
);
5997 mutex_unlock(&caching_ctl
->mutex
);
5998 put_caching_control(caching_ctl
);
6000 btrfs_put_block_group(block_group
);
6004 int btrfs_exclude_logged_extents(struct btrfs_root
*log
,
6005 struct extent_buffer
*eb
)
6007 struct btrfs_file_extent_item
*item
;
6008 struct btrfs_key key
;
6012 if (!btrfs_fs_incompat(log
->fs_info
, MIXED_GROUPS
))
6015 for (i
= 0; i
< btrfs_header_nritems(eb
); i
++) {
6016 btrfs_item_key_to_cpu(eb
, &key
, i
);
6017 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
6019 item
= btrfs_item_ptr(eb
, i
, struct btrfs_file_extent_item
);
6020 found_type
= btrfs_file_extent_type(eb
, item
);
6021 if (found_type
== BTRFS_FILE_EXTENT_INLINE
)
6023 if (btrfs_file_extent_disk_bytenr(eb
, item
) == 0)
6025 key
.objectid
= btrfs_file_extent_disk_bytenr(eb
, item
);
6026 key
.offset
= btrfs_file_extent_disk_num_bytes(eb
, item
);
6027 __exclude_logged_extent(log
, key
.objectid
, key
.offset
);
6034 * btrfs_update_reserved_bytes - update the block_group and space info counters
6035 * @cache: The cache we are manipulating
6036 * @num_bytes: The number of bytes in question
6037 * @reserve: One of the reservation enums
6038 * @delalloc: The blocks are allocated for the delalloc write
6040 * This is called by the allocator when it reserves space, or by somebody who is
6041 * freeing space that was never actually used on disk. For example if you
6042 * reserve some space for a new leaf in transaction A and before transaction A
6043 * commits you free that leaf, you call this with reserve set to 0 in order to
6044 * clear the reservation.
6046 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
6047 * ENOSPC accounting. For data we handle the reservation through clearing the
6048 * delalloc bits in the io_tree. We have to do this since we could end up
6049 * allocating less disk space for the amount of data we have reserved in the
6050 * case of compression.
6052 * If this is a reservation and the block group has become read only we cannot
6053 * make the reservation and return -EAGAIN, otherwise this function always
6056 static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache
*cache
,
6057 u64 num_bytes
, int reserve
, int delalloc
)
6059 struct btrfs_space_info
*space_info
= cache
->space_info
;
6062 spin_lock(&space_info
->lock
);
6063 spin_lock(&cache
->lock
);
6064 if (reserve
!= RESERVE_FREE
) {
6068 cache
->reserved
+= num_bytes
;
6069 space_info
->bytes_reserved
+= num_bytes
;
6070 if (reserve
== RESERVE_ALLOC
) {
6071 trace_btrfs_space_reservation(cache
->fs_info
,
6072 "space_info", space_info
->flags
,
6074 space_info
->bytes_may_use
-= num_bytes
;
6078 cache
->delalloc_bytes
+= num_bytes
;
6082 space_info
->bytes_readonly
+= num_bytes
;
6083 cache
->reserved
-= num_bytes
;
6084 space_info
->bytes_reserved
-= num_bytes
;
6087 cache
->delalloc_bytes
-= num_bytes
;
6089 spin_unlock(&cache
->lock
);
6090 spin_unlock(&space_info
->lock
);
6094 void btrfs_prepare_extent_commit(struct btrfs_trans_handle
*trans
,
6095 struct btrfs_root
*root
)
6097 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
6098 struct btrfs_caching_control
*next
;
6099 struct btrfs_caching_control
*caching_ctl
;
6100 struct btrfs_block_group_cache
*cache
;
6102 down_write(&fs_info
->commit_root_sem
);
6104 list_for_each_entry_safe(caching_ctl
, next
,
6105 &fs_info
->caching_block_groups
, list
) {
6106 cache
= caching_ctl
->block_group
;
6107 if (block_group_cache_done(cache
)) {
6108 cache
->last_byte_to_unpin
= (u64
)-1;
6109 list_del_init(&caching_ctl
->list
);
6110 put_caching_control(caching_ctl
);
6112 cache
->last_byte_to_unpin
= caching_ctl
->progress
;
6116 if (fs_info
->pinned_extents
== &fs_info
->freed_extents
[0])
6117 fs_info
->pinned_extents
= &fs_info
->freed_extents
[1];
6119 fs_info
->pinned_extents
= &fs_info
->freed_extents
[0];
6121 up_write(&fs_info
->commit_root_sem
);
6123 update_global_block_rsv(fs_info
);
6126 static int unpin_extent_range(struct btrfs_root
*root
, u64 start
, u64 end
,
6127 const bool return_free_space
)
6129 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
6130 struct btrfs_block_group_cache
*cache
= NULL
;
6131 struct btrfs_space_info
*space_info
;
6132 struct btrfs_block_rsv
*global_rsv
= &fs_info
->global_block_rsv
;
6136 while (start
<= end
) {
6139 start
>= cache
->key
.objectid
+ cache
->key
.offset
) {
6141 btrfs_put_block_group(cache
);
6142 cache
= btrfs_lookup_block_group(fs_info
, start
);
6143 BUG_ON(!cache
); /* Logic error */
6146 len
= cache
->key
.objectid
+ cache
->key
.offset
- start
;
6147 len
= min(len
, end
+ 1 - start
);
6149 if (start
< cache
->last_byte_to_unpin
) {
6150 len
= min(len
, cache
->last_byte_to_unpin
- start
);
6151 if (return_free_space
)
6152 btrfs_add_free_space(cache
, start
, len
);
6156 space_info
= cache
->space_info
;
6158 spin_lock(&space_info
->lock
);
6159 spin_lock(&cache
->lock
);
6160 cache
->pinned
-= len
;
6161 space_info
->bytes_pinned
-= len
;
6162 space_info
->max_extent_size
= 0;
6163 percpu_counter_add(&space_info
->total_bytes_pinned
, -len
);
6165 space_info
->bytes_readonly
+= len
;
6168 spin_unlock(&cache
->lock
);
6169 if (!readonly
&& global_rsv
->space_info
== space_info
) {
6170 spin_lock(&global_rsv
->lock
);
6171 if (!global_rsv
->full
) {
6172 len
= min(len
, global_rsv
->size
-
6173 global_rsv
->reserved
);
6174 global_rsv
->reserved
+= len
;
6175 space_info
->bytes_may_use
+= len
;
6176 if (global_rsv
->reserved
>= global_rsv
->size
)
6177 global_rsv
->full
= 1;
6179 spin_unlock(&global_rsv
->lock
);
6181 spin_unlock(&space_info
->lock
);
6185 btrfs_put_block_group(cache
);
6189 int btrfs_finish_extent_commit(struct btrfs_trans_handle
*trans
,
6190 struct btrfs_root
*root
)
6192 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
6193 struct btrfs_block_group_cache
*block_group
, *tmp
;
6194 struct list_head
*deleted_bgs
;
6195 struct extent_io_tree
*unpin
;
6200 if (fs_info
->pinned_extents
== &fs_info
->freed_extents
[0])
6201 unpin
= &fs_info
->freed_extents
[1];
6203 unpin
= &fs_info
->freed_extents
[0];
6205 while (!trans
->aborted
) {
6206 mutex_lock(&fs_info
->unused_bg_unpin_mutex
);
6207 ret
= find_first_extent_bit(unpin
, 0, &start
, &end
,
6208 EXTENT_DIRTY
, NULL
);
6210 mutex_unlock(&fs_info
->unused_bg_unpin_mutex
);
6214 if (btrfs_test_opt(root
, DISCARD
))
6215 ret
= btrfs_discard_extent(root
, start
,
6216 end
+ 1 - start
, NULL
);
6218 clear_extent_dirty(unpin
, start
, end
, GFP_NOFS
);
6219 unpin_extent_range(root
, start
, end
, true);
6220 mutex_unlock(&fs_info
->unused_bg_unpin_mutex
);
6225 * Transaction is finished. We don't need the lock anymore. We
6226 * do need to clean up the block groups in case of a transaction
6229 deleted_bgs
= &trans
->transaction
->deleted_bgs
;
6230 list_for_each_entry_safe(block_group
, tmp
, deleted_bgs
, bg_list
) {
6234 if (!trans
->aborted
)
6235 ret
= btrfs_discard_extent(root
,
6236 block_group
->key
.objectid
,
6237 block_group
->key
.offset
,
6240 list_del_init(&block_group
->bg_list
);
6241 btrfs_put_block_group_trimming(block_group
);
6242 btrfs_put_block_group(block_group
);
6245 const char *errstr
= btrfs_decode_error(ret
);
6247 "Discard failed while removing blockgroup: errno=%d %s\n",
6255 static void add_pinned_bytes(struct btrfs_fs_info
*fs_info
, u64 num_bytes
,
6256 u64 owner
, u64 root_objectid
)
6258 struct btrfs_space_info
*space_info
;
6261 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
6262 if (root_objectid
== BTRFS_CHUNK_TREE_OBJECTID
)
6263 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
6265 flags
= BTRFS_BLOCK_GROUP_METADATA
;
6267 flags
= BTRFS_BLOCK_GROUP_DATA
;
6270 space_info
= __find_space_info(fs_info
, flags
);
6271 BUG_ON(!space_info
); /* Logic bug */
6272 percpu_counter_add(&space_info
->total_bytes_pinned
, num_bytes
);
6276 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
6277 struct btrfs_root
*root
,
6278 struct btrfs_delayed_ref_node
*node
, u64 parent
,
6279 u64 root_objectid
, u64 owner_objectid
,
6280 u64 owner_offset
, int refs_to_drop
,
6281 struct btrfs_delayed_extent_op
*extent_op
)
6283 struct btrfs_key key
;
6284 struct btrfs_path
*path
;
6285 struct btrfs_fs_info
*info
= root
->fs_info
;
6286 struct btrfs_root
*extent_root
= info
->extent_root
;
6287 struct extent_buffer
*leaf
;
6288 struct btrfs_extent_item
*ei
;
6289 struct btrfs_extent_inline_ref
*iref
;
6292 int extent_slot
= 0;
6293 int found_extent
= 0;
6295 int no_quota
= node
->no_quota
;
6298 u64 bytenr
= node
->bytenr
;
6299 u64 num_bytes
= node
->num_bytes
;
6301 bool skinny_metadata
= btrfs_fs_incompat(root
->fs_info
,
6304 if (!info
->quota_enabled
|| !is_fstree(root_objectid
))
6307 path
= btrfs_alloc_path();
6312 path
->leave_spinning
= 1;
6314 is_data
= owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
;
6315 BUG_ON(!is_data
&& refs_to_drop
!= 1);
6318 skinny_metadata
= 0;
6320 ret
= lookup_extent_backref(trans
, extent_root
, path
, &iref
,
6321 bytenr
, num_bytes
, parent
,
6322 root_objectid
, owner_objectid
,
6325 extent_slot
= path
->slots
[0];
6326 while (extent_slot
>= 0) {
6327 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
6329 if (key
.objectid
!= bytenr
)
6331 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
6332 key
.offset
== num_bytes
) {
6336 if (key
.type
== BTRFS_METADATA_ITEM_KEY
&&
6337 key
.offset
== owner_objectid
) {
6341 if (path
->slots
[0] - extent_slot
> 5)
6345 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6346 item_size
= btrfs_item_size_nr(path
->nodes
[0], extent_slot
);
6347 if (found_extent
&& item_size
< sizeof(*ei
))
6350 if (!found_extent
) {
6352 ret
= remove_extent_backref(trans
, extent_root
, path
,
6354 is_data
, &last_ref
);
6356 btrfs_abort_transaction(trans
, extent_root
, ret
);
6359 btrfs_release_path(path
);
6360 path
->leave_spinning
= 1;
6362 key
.objectid
= bytenr
;
6363 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
6364 key
.offset
= num_bytes
;
6366 if (!is_data
&& skinny_metadata
) {
6367 key
.type
= BTRFS_METADATA_ITEM_KEY
;
6368 key
.offset
= owner_objectid
;
6371 ret
= btrfs_search_slot(trans
, extent_root
,
6373 if (ret
> 0 && skinny_metadata
&& path
->slots
[0]) {
6375 * Couldn't find our skinny metadata item,
6376 * see if we have ye olde extent item.
6379 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
6381 if (key
.objectid
== bytenr
&&
6382 key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
6383 key
.offset
== num_bytes
)
6387 if (ret
> 0 && skinny_metadata
) {
6388 skinny_metadata
= false;
6389 key
.objectid
= bytenr
;
6390 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
6391 key
.offset
= num_bytes
;
6392 btrfs_release_path(path
);
6393 ret
= btrfs_search_slot(trans
, extent_root
,
6398 btrfs_err(info
, "umm, got %d back from search, was looking for %llu",
6401 btrfs_print_leaf(extent_root
,
6405 btrfs_abort_transaction(trans
, extent_root
, ret
);
6408 extent_slot
= path
->slots
[0];
6410 } else if (WARN_ON(ret
== -ENOENT
)) {
6411 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
6413 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
6414 bytenr
, parent
, root_objectid
, owner_objectid
,
6416 btrfs_abort_transaction(trans
, extent_root
, ret
);
6419 btrfs_abort_transaction(trans
, extent_root
, ret
);
6423 leaf
= path
->nodes
[0];
6424 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
6425 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6426 if (item_size
< sizeof(*ei
)) {
6427 BUG_ON(found_extent
|| extent_slot
!= path
->slots
[0]);
6428 ret
= convert_extent_item_v0(trans
, extent_root
, path
,
6431 btrfs_abort_transaction(trans
, extent_root
, ret
);
6435 btrfs_release_path(path
);
6436 path
->leave_spinning
= 1;
6438 key
.objectid
= bytenr
;
6439 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
6440 key
.offset
= num_bytes
;
6442 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
,
6445 btrfs_err(info
, "umm, got %d back from search, was looking for %llu",
6447 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
6450 btrfs_abort_transaction(trans
, extent_root
, ret
);
6454 extent_slot
= path
->slots
[0];
6455 leaf
= path
->nodes
[0];
6456 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
6459 BUG_ON(item_size
< sizeof(*ei
));
6460 ei
= btrfs_item_ptr(leaf
, extent_slot
,
6461 struct btrfs_extent_item
);
6462 if (owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
&&
6463 key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
6464 struct btrfs_tree_block_info
*bi
;
6465 BUG_ON(item_size
< sizeof(*ei
) + sizeof(*bi
));
6466 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
6467 WARN_ON(owner_objectid
!= btrfs_tree_block_level(leaf
, bi
));
6470 refs
= btrfs_extent_refs(leaf
, ei
);
6471 if (refs
< refs_to_drop
) {
6472 btrfs_err(info
, "trying to drop %d refs but we only have %Lu "
6473 "for bytenr %Lu", refs_to_drop
, refs
, bytenr
);
6475 btrfs_abort_transaction(trans
, extent_root
, ret
);
6478 refs
-= refs_to_drop
;
6482 __run_delayed_extent_op(extent_op
, leaf
, ei
);
6484 * In the case of inline back ref, reference count will
6485 * be updated by remove_extent_backref
6488 BUG_ON(!found_extent
);
6490 btrfs_set_extent_refs(leaf
, ei
, refs
);
6491 btrfs_mark_buffer_dirty(leaf
);
6494 ret
= remove_extent_backref(trans
, extent_root
, path
,
6496 is_data
, &last_ref
);
6498 btrfs_abort_transaction(trans
, extent_root
, ret
);
6502 add_pinned_bytes(root
->fs_info
, -num_bytes
, owner_objectid
,
6506 BUG_ON(is_data
&& refs_to_drop
!=
6507 extent_data_ref_count(path
, iref
));
6509 BUG_ON(path
->slots
[0] != extent_slot
);
6511 BUG_ON(path
->slots
[0] != extent_slot
+ 1);
6512 path
->slots
[0] = extent_slot
;
6518 ret
= btrfs_del_items(trans
, extent_root
, path
, path
->slots
[0],
6521 btrfs_abort_transaction(trans
, extent_root
, ret
);
6524 btrfs_release_path(path
);
6527 ret
= btrfs_del_csums(trans
, root
, bytenr
, num_bytes
);
6529 btrfs_abort_transaction(trans
, extent_root
, ret
);
6534 ret
= update_block_group(trans
, root
, bytenr
, num_bytes
, 0);
6536 btrfs_abort_transaction(trans
, extent_root
, ret
);
6540 btrfs_release_path(path
);
6543 btrfs_free_path(path
);
6548 * when we free an block, it is possible (and likely) that we free the last
6549 * delayed ref for that extent as well. This searches the delayed ref tree for
6550 * a given extent, and if there are no other delayed refs to be processed, it
6551 * removes it from the tree.
6553 static noinline
int check_ref_cleanup(struct btrfs_trans_handle
*trans
,
6554 struct btrfs_root
*root
, u64 bytenr
)
6556 struct btrfs_delayed_ref_head
*head
;
6557 struct btrfs_delayed_ref_root
*delayed_refs
;
6560 delayed_refs
= &trans
->transaction
->delayed_refs
;
6561 spin_lock(&delayed_refs
->lock
);
6562 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
6564 goto out_delayed_unlock
;
6566 spin_lock(&head
->lock
);
6567 if (!list_empty(&head
->ref_list
))
6570 if (head
->extent_op
) {
6571 if (!head
->must_insert_reserved
)
6573 btrfs_free_delayed_extent_op(head
->extent_op
);
6574 head
->extent_op
= NULL
;
6578 * waiting for the lock here would deadlock. If someone else has it
6579 * locked they are already in the process of dropping it anyway
6581 if (!mutex_trylock(&head
->mutex
))
6585 * at this point we have a head with no other entries. Go
6586 * ahead and process it.
6588 head
->node
.in_tree
= 0;
6589 rb_erase(&head
->href_node
, &delayed_refs
->href_root
);
6591 atomic_dec(&delayed_refs
->num_entries
);
6594 * we don't take a ref on the node because we're removing it from the
6595 * tree, so we just steal the ref the tree was holding.
6597 delayed_refs
->num_heads
--;
6598 if (head
->processing
== 0)
6599 delayed_refs
->num_heads_ready
--;
6600 head
->processing
= 0;
6601 spin_unlock(&head
->lock
);
6602 spin_unlock(&delayed_refs
->lock
);
6604 BUG_ON(head
->extent_op
);
6605 if (head
->must_insert_reserved
)
6608 mutex_unlock(&head
->mutex
);
6609 btrfs_put_delayed_ref(&head
->node
);
6612 spin_unlock(&head
->lock
);
6615 spin_unlock(&delayed_refs
->lock
);
6619 void btrfs_free_tree_block(struct btrfs_trans_handle
*trans
,
6620 struct btrfs_root
*root
,
6621 struct extent_buffer
*buf
,
6622 u64 parent
, int last_ref
)
6627 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
6628 ret
= btrfs_add_delayed_tree_ref(root
->fs_info
, trans
,
6629 buf
->start
, buf
->len
,
6630 parent
, root
->root_key
.objectid
,
6631 btrfs_header_level(buf
),
6632 BTRFS_DROP_DELAYED_REF
, NULL
, 0);
6633 BUG_ON(ret
); /* -ENOMEM */
6639 if (btrfs_header_generation(buf
) == trans
->transid
) {
6640 struct btrfs_block_group_cache
*cache
;
6642 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
6643 ret
= check_ref_cleanup(trans
, root
, buf
->start
);
6648 cache
= btrfs_lookup_block_group(root
->fs_info
, buf
->start
);
6650 if (btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
)) {
6651 pin_down_extent(root
, cache
, buf
->start
, buf
->len
, 1);
6652 btrfs_put_block_group(cache
);
6656 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY
, &buf
->bflags
));
6658 btrfs_add_free_space(cache
, buf
->start
, buf
->len
);
6659 btrfs_update_reserved_bytes(cache
, buf
->len
, RESERVE_FREE
, 0);
6660 btrfs_put_block_group(cache
);
6661 trace_btrfs_reserved_extent_free(root
, buf
->start
, buf
->len
);
6666 add_pinned_bytes(root
->fs_info
, buf
->len
,
6667 btrfs_header_level(buf
),
6668 root
->root_key
.objectid
);
6671 * Deleting the buffer, clear the corrupt flag since it doesn't matter
6674 clear_bit(EXTENT_BUFFER_CORRUPT
, &buf
->bflags
);
6677 /* Can return -ENOMEM */
6678 int btrfs_free_extent(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
6679 u64 bytenr
, u64 num_bytes
, u64 parent
, u64 root_objectid
,
6680 u64 owner
, u64 offset
, int no_quota
)
6683 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
6685 if (btrfs_test_is_dummy_root(root
))
6688 add_pinned_bytes(root
->fs_info
, num_bytes
, owner
, root_objectid
);
6691 * tree log blocks never actually go into the extent allocation
6692 * tree, just update pinning info and exit early.
6694 if (root_objectid
== BTRFS_TREE_LOG_OBJECTID
) {
6695 WARN_ON(owner
>= BTRFS_FIRST_FREE_OBJECTID
);
6696 /* unlocks the pinned mutex */
6697 btrfs_pin_extent(root
, bytenr
, num_bytes
, 1);
6699 } else if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
6700 ret
= btrfs_add_delayed_tree_ref(fs_info
, trans
, bytenr
,
6702 parent
, root_objectid
, (int)owner
,
6703 BTRFS_DROP_DELAYED_REF
, NULL
, no_quota
);
6705 ret
= btrfs_add_delayed_data_ref(fs_info
, trans
, bytenr
,
6707 parent
, root_objectid
, owner
,
6708 offset
, BTRFS_DROP_DELAYED_REF
,
6715 * when we wait for progress in the block group caching, its because
6716 * our allocation attempt failed at least once. So, we must sleep
6717 * and let some progress happen before we try again.
6719 * This function will sleep at least once waiting for new free space to
6720 * show up, and then it will check the block group free space numbers
6721 * for our min num_bytes. Another option is to have it go ahead
6722 * and look in the rbtree for a free extent of a given size, but this
6725 * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
6726 * any of the information in this block group.
6728 static noinline
void
6729 wait_block_group_cache_progress(struct btrfs_block_group_cache
*cache
,
6732 struct btrfs_caching_control
*caching_ctl
;
6734 caching_ctl
= get_caching_control(cache
);
6738 wait_event(caching_ctl
->wait
, block_group_cache_done(cache
) ||
6739 (cache
->free_space_ctl
->free_space
>= num_bytes
));
6741 put_caching_control(caching_ctl
);
6745 wait_block_group_cache_done(struct btrfs_block_group_cache
*cache
)
6747 struct btrfs_caching_control
*caching_ctl
;
6750 caching_ctl
= get_caching_control(cache
);
6752 return (cache
->cached
== BTRFS_CACHE_ERROR
) ? -EIO
: 0;
6754 wait_event(caching_ctl
->wait
, block_group_cache_done(cache
));
6755 if (cache
->cached
== BTRFS_CACHE_ERROR
)
6757 put_caching_control(caching_ctl
);
6761 int __get_raid_index(u64 flags
)
6763 if (flags
& BTRFS_BLOCK_GROUP_RAID10
)
6764 return BTRFS_RAID_RAID10
;
6765 else if (flags
& BTRFS_BLOCK_GROUP_RAID1
)
6766 return BTRFS_RAID_RAID1
;
6767 else if (flags
& BTRFS_BLOCK_GROUP_DUP
)
6768 return BTRFS_RAID_DUP
;
6769 else if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
6770 return BTRFS_RAID_RAID0
;
6771 else if (flags
& BTRFS_BLOCK_GROUP_RAID5
)
6772 return BTRFS_RAID_RAID5
;
6773 else if (flags
& BTRFS_BLOCK_GROUP_RAID6
)
6774 return BTRFS_RAID_RAID6
;
6776 return BTRFS_RAID_SINGLE
; /* BTRFS_BLOCK_GROUP_SINGLE */
6779 int get_block_group_index(struct btrfs_block_group_cache
*cache
)
6781 return __get_raid_index(cache
->flags
);
6784 static const char *btrfs_raid_type_names
[BTRFS_NR_RAID_TYPES
] = {
6785 [BTRFS_RAID_RAID10
] = "raid10",
6786 [BTRFS_RAID_RAID1
] = "raid1",
6787 [BTRFS_RAID_DUP
] = "dup",
6788 [BTRFS_RAID_RAID0
] = "raid0",
6789 [BTRFS_RAID_SINGLE
] = "single",
6790 [BTRFS_RAID_RAID5
] = "raid5",
6791 [BTRFS_RAID_RAID6
] = "raid6",
6794 static const char *get_raid_name(enum btrfs_raid_types type
)
6796 if (type
>= BTRFS_NR_RAID_TYPES
)
6799 return btrfs_raid_type_names
[type
];
6802 enum btrfs_loop_type
{
6803 LOOP_CACHING_NOWAIT
= 0,
6804 LOOP_CACHING_WAIT
= 1,
6805 LOOP_ALLOC_CHUNK
= 2,
6806 LOOP_NO_EMPTY_SIZE
= 3,
6810 btrfs_lock_block_group(struct btrfs_block_group_cache
*cache
,
6814 down_read(&cache
->data_rwsem
);
6818 btrfs_grab_block_group(struct btrfs_block_group_cache
*cache
,
6821 btrfs_get_block_group(cache
);
6823 down_read(&cache
->data_rwsem
);
6826 static struct btrfs_block_group_cache
*
6827 btrfs_lock_cluster(struct btrfs_block_group_cache
*block_group
,
6828 struct btrfs_free_cluster
*cluster
,
6831 struct btrfs_block_group_cache
*used_bg
;
6832 bool locked
= false;
6834 spin_lock(&cluster
->refill_lock
);
6836 if (used_bg
== cluster
->block_group
)
6839 up_read(&used_bg
->data_rwsem
);
6840 btrfs_put_block_group(used_bg
);
6843 used_bg
= cluster
->block_group
;
6847 if (used_bg
== block_group
)
6850 btrfs_get_block_group(used_bg
);
6855 if (down_read_trylock(&used_bg
->data_rwsem
))
6858 spin_unlock(&cluster
->refill_lock
);
6859 down_read(&used_bg
->data_rwsem
);
6865 btrfs_release_block_group(struct btrfs_block_group_cache
*cache
,
6869 up_read(&cache
->data_rwsem
);
6870 btrfs_put_block_group(cache
);
6874 * walks the btree of allocated extents and find a hole of a given size.
6875 * The key ins is changed to record the hole:
6876 * ins->objectid == start position
6877 * ins->flags = BTRFS_EXTENT_ITEM_KEY
6878 * ins->offset == the size of the hole.
6879 * Any available blocks before search_start are skipped.
6881 * If there is no suitable free space, we will record the max size of
6882 * the free space extent currently.
6884 static noinline
int find_free_extent(struct btrfs_root
*orig_root
,
6885 u64 num_bytes
, u64 empty_size
,
6886 u64 hint_byte
, struct btrfs_key
*ins
,
6887 u64 flags
, int delalloc
)
6890 struct btrfs_root
*root
= orig_root
->fs_info
->extent_root
;
6891 struct btrfs_free_cluster
*last_ptr
= NULL
;
6892 struct btrfs_block_group_cache
*block_group
= NULL
;
6893 u64 search_start
= 0;
6894 u64 max_extent_size
= 0;
6895 int empty_cluster
= 2 * 1024 * 1024;
6896 struct btrfs_space_info
*space_info
;
6898 int index
= __get_raid_index(flags
);
6899 int alloc_type
= (flags
& BTRFS_BLOCK_GROUP_DATA
) ?
6900 RESERVE_ALLOC_NO_ACCOUNT
: RESERVE_ALLOC
;
6901 bool failed_cluster_refill
= false;
6902 bool failed_alloc
= false;
6903 bool use_cluster
= true;
6904 bool have_caching_bg
= false;
6906 WARN_ON(num_bytes
< root
->sectorsize
);
6907 ins
->type
= BTRFS_EXTENT_ITEM_KEY
;
6911 trace_find_free_extent(orig_root
, num_bytes
, empty_size
, flags
);
6913 space_info
= __find_space_info(root
->fs_info
, flags
);
6915 btrfs_err(root
->fs_info
, "No space info for %llu", flags
);
6920 * If our free space is heavily fragmented we may not be able to make
6921 * big contiguous allocations, so instead of doing the expensive search
6922 * for free space, simply return ENOSPC with our max_extent_size so we
6923 * can go ahead and search for a more manageable chunk.
6925 * If our max_extent_size is large enough for our allocation simply
6926 * disable clustering since we will likely not be able to find enough
6927 * space to create a cluster and induce latency trying.
6929 if (unlikely(space_info
->max_extent_size
)) {
6930 spin_lock(&space_info
->lock
);
6931 if (space_info
->max_extent_size
&&
6932 num_bytes
> space_info
->max_extent_size
) {
6933 ins
->offset
= space_info
->max_extent_size
;
6934 spin_unlock(&space_info
->lock
);
6936 } else if (space_info
->max_extent_size
) {
6937 use_cluster
= false;
6939 spin_unlock(&space_info
->lock
);
6943 * If the space info is for both data and metadata it means we have a
6944 * small filesystem and we can't use the clustering stuff.
6946 if (btrfs_mixed_space_info(space_info
))
6947 use_cluster
= false;
6949 if (flags
& BTRFS_BLOCK_GROUP_METADATA
&& use_cluster
) {
6950 last_ptr
= &root
->fs_info
->meta_alloc_cluster
;
6951 if (!btrfs_test_opt(root
, SSD
))
6952 empty_cluster
= 64 * 1024;
6955 if ((flags
& BTRFS_BLOCK_GROUP_DATA
) && use_cluster
&&
6956 btrfs_test_opt(root
, SSD
)) {
6957 last_ptr
= &root
->fs_info
->data_alloc_cluster
;
6961 spin_lock(&last_ptr
->lock
);
6962 if (last_ptr
->block_group
)
6963 hint_byte
= last_ptr
->window_start
;
6964 spin_unlock(&last_ptr
->lock
);
6967 search_start
= max(search_start
, first_logical_byte(root
, 0));
6968 search_start
= max(search_start
, hint_byte
);
6973 if (search_start
== hint_byte
) {
6974 block_group
= btrfs_lookup_block_group(root
->fs_info
,
6977 * we don't want to use the block group if it doesn't match our
6978 * allocation bits, or if its not cached.
6980 * However if we are re-searching with an ideal block group
6981 * picked out then we don't care that the block group is cached.
6983 if (block_group
&& block_group_bits(block_group
, flags
) &&
6984 block_group
->cached
!= BTRFS_CACHE_NO
) {
6985 down_read(&space_info
->groups_sem
);
6986 if (list_empty(&block_group
->list
) ||
6989 * someone is removing this block group,
6990 * we can't jump into the have_block_group
6991 * target because our list pointers are not
6994 btrfs_put_block_group(block_group
);
6995 up_read(&space_info
->groups_sem
);
6997 index
= get_block_group_index(block_group
);
6998 btrfs_lock_block_group(block_group
, delalloc
);
6999 goto have_block_group
;
7001 } else if (block_group
) {
7002 btrfs_put_block_group(block_group
);
7006 have_caching_bg
= false;
7007 down_read(&space_info
->groups_sem
);
7008 list_for_each_entry(block_group
, &space_info
->block_groups
[index
],
7013 btrfs_grab_block_group(block_group
, delalloc
);
7014 search_start
= block_group
->key
.objectid
;
7017 * this can happen if we end up cycling through all the
7018 * raid types, but we want to make sure we only allocate
7019 * for the proper type.
7021 if (!block_group_bits(block_group
, flags
)) {
7022 u64 extra
= BTRFS_BLOCK_GROUP_DUP
|
7023 BTRFS_BLOCK_GROUP_RAID1
|
7024 BTRFS_BLOCK_GROUP_RAID5
|
7025 BTRFS_BLOCK_GROUP_RAID6
|
7026 BTRFS_BLOCK_GROUP_RAID10
;
7029 * if they asked for extra copies and this block group
7030 * doesn't provide them, bail. This does allow us to
7031 * fill raid0 from raid1.
7033 if ((flags
& extra
) && !(block_group
->flags
& extra
))
7038 cached
= block_group_cache_done(block_group
);
7039 if (unlikely(!cached
)) {
7040 ret
= cache_block_group(block_group
, 0);
7045 if (unlikely(block_group
->cached
== BTRFS_CACHE_ERROR
))
7047 if (unlikely(block_group
->ro
))
7051 * Ok we want to try and use the cluster allocator, so
7055 struct btrfs_block_group_cache
*used_block_group
;
7056 unsigned long aligned_cluster
;
7058 * the refill lock keeps out other
7059 * people trying to start a new cluster
7061 used_block_group
= btrfs_lock_cluster(block_group
,
7064 if (!used_block_group
)
7065 goto refill_cluster
;
7067 if (used_block_group
!= block_group
&&
7068 (used_block_group
->ro
||
7069 !block_group_bits(used_block_group
, flags
)))
7070 goto release_cluster
;
7072 offset
= btrfs_alloc_from_cluster(used_block_group
,
7075 used_block_group
->key
.objectid
,
7078 /* we have a block, we're done */
7079 spin_unlock(&last_ptr
->refill_lock
);
7080 trace_btrfs_reserve_extent_cluster(root
,
7082 search_start
, num_bytes
);
7083 if (used_block_group
!= block_group
) {
7084 btrfs_release_block_group(block_group
,
7086 block_group
= used_block_group
;
7091 WARN_ON(last_ptr
->block_group
!= used_block_group
);
7093 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
7094 * set up a new clusters, so lets just skip it
7095 * and let the allocator find whatever block
7096 * it can find. If we reach this point, we
7097 * will have tried the cluster allocator
7098 * plenty of times and not have found
7099 * anything, so we are likely way too
7100 * fragmented for the clustering stuff to find
7103 * However, if the cluster is taken from the
7104 * current block group, release the cluster
7105 * first, so that we stand a better chance of
7106 * succeeding in the unclustered
7108 if (loop
>= LOOP_NO_EMPTY_SIZE
&&
7109 used_block_group
!= block_group
) {
7110 spin_unlock(&last_ptr
->refill_lock
);
7111 btrfs_release_block_group(used_block_group
,
7113 goto unclustered_alloc
;
7117 * this cluster didn't work out, free it and
7120 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
7122 if (used_block_group
!= block_group
)
7123 btrfs_release_block_group(used_block_group
,
7126 if (loop
>= LOOP_NO_EMPTY_SIZE
) {
7127 spin_unlock(&last_ptr
->refill_lock
);
7128 goto unclustered_alloc
;
7131 aligned_cluster
= max_t(unsigned long,
7132 empty_cluster
+ empty_size
,
7133 block_group
->full_stripe_len
);
7135 /* allocate a cluster in this block group */
7136 ret
= btrfs_find_space_cluster(root
, block_group
,
7137 last_ptr
, search_start
,
7142 * now pull our allocation out of this
7145 offset
= btrfs_alloc_from_cluster(block_group
,
7151 /* we found one, proceed */
7152 spin_unlock(&last_ptr
->refill_lock
);
7153 trace_btrfs_reserve_extent_cluster(root
,
7154 block_group
, search_start
,
7158 } else if (!cached
&& loop
> LOOP_CACHING_NOWAIT
7159 && !failed_cluster_refill
) {
7160 spin_unlock(&last_ptr
->refill_lock
);
7162 failed_cluster_refill
= true;
7163 wait_block_group_cache_progress(block_group
,
7164 num_bytes
+ empty_cluster
+ empty_size
);
7165 goto have_block_group
;
7169 * at this point we either didn't find a cluster
7170 * or we weren't able to allocate a block from our
7171 * cluster. Free the cluster we've been trying
7172 * to use, and go to the next block group
7174 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
7175 spin_unlock(&last_ptr
->refill_lock
);
7180 spin_lock(&block_group
->free_space_ctl
->tree_lock
);
7182 block_group
->free_space_ctl
->free_space
<
7183 num_bytes
+ empty_cluster
+ empty_size
) {
7184 if (block_group
->free_space_ctl
->free_space
>
7187 block_group
->free_space_ctl
->free_space
;
7188 spin_unlock(&block_group
->free_space_ctl
->tree_lock
);
7191 spin_unlock(&block_group
->free_space_ctl
->tree_lock
);
7193 offset
= btrfs_find_space_for_alloc(block_group
, search_start
,
7194 num_bytes
, empty_size
,
7197 * If we didn't find a chunk, and we haven't failed on this
7198 * block group before, and this block group is in the middle of
7199 * caching and we are ok with waiting, then go ahead and wait
7200 * for progress to be made, and set failed_alloc to true.
7202 * If failed_alloc is true then we've already waited on this
7203 * block group once and should move on to the next block group.
7205 if (!offset
&& !failed_alloc
&& !cached
&&
7206 loop
> LOOP_CACHING_NOWAIT
) {
7207 wait_block_group_cache_progress(block_group
,
7208 num_bytes
+ empty_size
);
7209 failed_alloc
= true;
7210 goto have_block_group
;
7211 } else if (!offset
) {
7213 have_caching_bg
= true;
7217 search_start
= ALIGN(offset
, root
->stripesize
);
7219 /* move on to the next group */
7220 if (search_start
+ num_bytes
>
7221 block_group
->key
.objectid
+ block_group
->key
.offset
) {
7222 btrfs_add_free_space(block_group
, offset
, num_bytes
);
7226 if (offset
< search_start
)
7227 btrfs_add_free_space(block_group
, offset
,
7228 search_start
- offset
);
7229 BUG_ON(offset
> search_start
);
7231 ret
= btrfs_update_reserved_bytes(block_group
, num_bytes
,
7232 alloc_type
, delalloc
);
7233 if (ret
== -EAGAIN
) {
7234 btrfs_add_free_space(block_group
, offset
, num_bytes
);
7238 /* we are all good, lets return */
7239 ins
->objectid
= search_start
;
7240 ins
->offset
= num_bytes
;
7242 trace_btrfs_reserve_extent(orig_root
, block_group
,
7243 search_start
, num_bytes
);
7244 btrfs_release_block_group(block_group
, delalloc
);
7247 failed_cluster_refill
= false;
7248 failed_alloc
= false;
7249 BUG_ON(index
!= get_block_group_index(block_group
));
7250 btrfs_release_block_group(block_group
, delalloc
);
7252 up_read(&space_info
->groups_sem
);
7254 if (!ins
->objectid
&& loop
>= LOOP_CACHING_WAIT
&& have_caching_bg
)
7257 if (!ins
->objectid
&& ++index
< BTRFS_NR_RAID_TYPES
)
7261 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
7262 * caching kthreads as we move along
7263 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
7264 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
7265 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
7268 if (!ins
->objectid
&& loop
< LOOP_NO_EMPTY_SIZE
) {
7271 if (loop
== LOOP_ALLOC_CHUNK
) {
7272 struct btrfs_trans_handle
*trans
;
7275 trans
= current
->journal_info
;
7279 trans
= btrfs_join_transaction(root
);
7281 if (IS_ERR(trans
)) {
7282 ret
= PTR_ERR(trans
);
7286 ret
= do_chunk_alloc(trans
, root
, flags
,
7289 * Do not bail out on ENOSPC since we
7290 * can do more things.
7292 if (ret
< 0 && ret
!= -ENOSPC
)
7293 btrfs_abort_transaction(trans
,
7298 btrfs_end_transaction(trans
, root
);
7303 if (loop
== LOOP_NO_EMPTY_SIZE
) {
7309 } else if (!ins
->objectid
) {
7311 } else if (ins
->objectid
) {
7315 if (ret
== -ENOSPC
) {
7316 spin_lock(&space_info
->lock
);
7317 space_info
->max_extent_size
= max_extent_size
;
7318 spin_unlock(&space_info
->lock
);
7319 ins
->offset
= max_extent_size
;
7324 static void dump_space_info(struct btrfs_space_info
*info
, u64 bytes
,
7325 int dump_block_groups
)
7327 struct btrfs_block_group_cache
*cache
;
7330 spin_lock(&info
->lock
);
7331 printk(KERN_INFO
"BTRFS: space_info %llu has %llu free, is %sfull\n",
7333 info
->total_bytes
- info
->bytes_used
- info
->bytes_pinned
-
7334 info
->bytes_reserved
- info
->bytes_readonly
,
7335 (info
->full
) ? "" : "not ");
7336 printk(KERN_INFO
"BTRFS: space_info total=%llu, used=%llu, pinned=%llu, "
7337 "reserved=%llu, may_use=%llu, readonly=%llu\n",
7338 info
->total_bytes
, info
->bytes_used
, info
->bytes_pinned
,
7339 info
->bytes_reserved
, info
->bytes_may_use
,
7340 info
->bytes_readonly
);
7341 spin_unlock(&info
->lock
);
7343 if (!dump_block_groups
)
7346 down_read(&info
->groups_sem
);
7348 list_for_each_entry(cache
, &info
->block_groups
[index
], list
) {
7349 spin_lock(&cache
->lock
);
7350 printk(KERN_INFO
"BTRFS: "
7351 "block group %llu has %llu bytes, "
7352 "%llu used %llu pinned %llu reserved %s\n",
7353 cache
->key
.objectid
, cache
->key
.offset
,
7354 btrfs_block_group_used(&cache
->item
), cache
->pinned
,
7355 cache
->reserved
, cache
->ro
? "[readonly]" : "");
7356 btrfs_dump_free_space(cache
, bytes
);
7357 spin_unlock(&cache
->lock
);
7359 if (++index
< BTRFS_NR_RAID_TYPES
)
7361 up_read(&info
->groups_sem
);
7364 int btrfs_reserve_extent(struct btrfs_root
*root
,
7365 u64 num_bytes
, u64 min_alloc_size
,
7366 u64 empty_size
, u64 hint_byte
,
7367 struct btrfs_key
*ins
, int is_data
, int delalloc
)
7369 bool final_tried
= num_bytes
== min_alloc_size
;
7373 flags
= btrfs_get_alloc_profile(root
, is_data
);
7375 WARN_ON(num_bytes
< root
->sectorsize
);
7376 ret
= find_free_extent(root
, num_bytes
, empty_size
, hint_byte
, ins
,
7379 if (ret
== -ENOSPC
) {
7380 if (!final_tried
&& ins
->offset
) {
7381 num_bytes
= min(num_bytes
>> 1, ins
->offset
);
7382 num_bytes
= round_down(num_bytes
, root
->sectorsize
);
7383 num_bytes
= max(num_bytes
, min_alloc_size
);
7384 if (num_bytes
== min_alloc_size
)
7387 } else if (btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
7388 struct btrfs_space_info
*sinfo
;
7390 sinfo
= __find_space_info(root
->fs_info
, flags
);
7391 btrfs_err(root
->fs_info
, "allocation failed flags %llu, wanted %llu",
7394 dump_space_info(sinfo
, num_bytes
, 1);
7401 static int __btrfs_free_reserved_extent(struct btrfs_root
*root
,
7403 int pin
, int delalloc
)
7405 struct btrfs_block_group_cache
*cache
;
7408 cache
= btrfs_lookup_block_group(root
->fs_info
, start
);
7410 btrfs_err(root
->fs_info
, "Unable to find block group for %llu",
7416 pin_down_extent(root
, cache
, start
, len
, 1);
7418 if (btrfs_test_opt(root
, DISCARD
))
7419 ret
= btrfs_discard_extent(root
, start
, len
, NULL
);
7420 btrfs_add_free_space(cache
, start
, len
);
7421 btrfs_update_reserved_bytes(cache
, len
, RESERVE_FREE
, delalloc
);
7424 btrfs_put_block_group(cache
);
7426 trace_btrfs_reserved_extent_free(root
, start
, len
);
7431 int btrfs_free_reserved_extent(struct btrfs_root
*root
,
7432 u64 start
, u64 len
, int delalloc
)
7434 return __btrfs_free_reserved_extent(root
, start
, len
, 0, delalloc
);
7437 int btrfs_free_and_pin_reserved_extent(struct btrfs_root
*root
,
7440 return __btrfs_free_reserved_extent(root
, start
, len
, 1, 0);
7443 static int alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
7444 struct btrfs_root
*root
,
7445 u64 parent
, u64 root_objectid
,
7446 u64 flags
, u64 owner
, u64 offset
,
7447 struct btrfs_key
*ins
, int ref_mod
)
7450 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
7451 struct btrfs_extent_item
*extent_item
;
7452 struct btrfs_extent_inline_ref
*iref
;
7453 struct btrfs_path
*path
;
7454 struct extent_buffer
*leaf
;
7459 type
= BTRFS_SHARED_DATA_REF_KEY
;
7461 type
= BTRFS_EXTENT_DATA_REF_KEY
;
7463 size
= sizeof(*extent_item
) + btrfs_extent_inline_ref_size(type
);
7465 path
= btrfs_alloc_path();
7469 path
->leave_spinning
= 1;
7470 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
7473 btrfs_free_path(path
);
7477 leaf
= path
->nodes
[0];
7478 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
7479 struct btrfs_extent_item
);
7480 btrfs_set_extent_refs(leaf
, extent_item
, ref_mod
);
7481 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
7482 btrfs_set_extent_flags(leaf
, extent_item
,
7483 flags
| BTRFS_EXTENT_FLAG_DATA
);
7485 iref
= (struct btrfs_extent_inline_ref
*)(extent_item
+ 1);
7486 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
7488 struct btrfs_shared_data_ref
*ref
;
7489 ref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
7490 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
7491 btrfs_set_shared_data_ref_count(leaf
, ref
, ref_mod
);
7493 struct btrfs_extent_data_ref
*ref
;
7494 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
7495 btrfs_set_extent_data_ref_root(leaf
, ref
, root_objectid
);
7496 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
7497 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
7498 btrfs_set_extent_data_ref_count(leaf
, ref
, ref_mod
);
7501 btrfs_mark_buffer_dirty(path
->nodes
[0]);
7502 btrfs_free_path(path
);
7504 ret
= update_block_group(trans
, root
, ins
->objectid
, ins
->offset
, 1);
7505 if (ret
) { /* -ENOENT, logic error */
7506 btrfs_err(fs_info
, "update block group failed for %llu %llu",
7507 ins
->objectid
, ins
->offset
);
7510 trace_btrfs_reserved_extent_alloc(root
, ins
->objectid
, ins
->offset
);
7514 static int alloc_reserved_tree_block(struct btrfs_trans_handle
*trans
,
7515 struct btrfs_root
*root
,
7516 u64 parent
, u64 root_objectid
,
7517 u64 flags
, struct btrfs_disk_key
*key
,
7518 int level
, struct btrfs_key
*ins
,
7522 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
7523 struct btrfs_extent_item
*extent_item
;
7524 struct btrfs_tree_block_info
*block_info
;
7525 struct btrfs_extent_inline_ref
*iref
;
7526 struct btrfs_path
*path
;
7527 struct extent_buffer
*leaf
;
7528 u32 size
= sizeof(*extent_item
) + sizeof(*iref
);
7529 u64 num_bytes
= ins
->offset
;
7530 bool skinny_metadata
= btrfs_fs_incompat(root
->fs_info
,
7533 if (!skinny_metadata
)
7534 size
+= sizeof(*block_info
);
7536 path
= btrfs_alloc_path();
7538 btrfs_free_and_pin_reserved_extent(root
, ins
->objectid
,
7543 path
->leave_spinning
= 1;
7544 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
7547 btrfs_free_path(path
);
7548 btrfs_free_and_pin_reserved_extent(root
, ins
->objectid
,
7553 leaf
= path
->nodes
[0];
7554 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
7555 struct btrfs_extent_item
);
7556 btrfs_set_extent_refs(leaf
, extent_item
, 1);
7557 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
7558 btrfs_set_extent_flags(leaf
, extent_item
,
7559 flags
| BTRFS_EXTENT_FLAG_TREE_BLOCK
);
7561 if (skinny_metadata
) {
7562 iref
= (struct btrfs_extent_inline_ref
*)(extent_item
+ 1);
7563 num_bytes
= root
->nodesize
;
7565 block_info
= (struct btrfs_tree_block_info
*)(extent_item
+ 1);
7566 btrfs_set_tree_block_key(leaf
, block_info
, key
);
7567 btrfs_set_tree_block_level(leaf
, block_info
, level
);
7568 iref
= (struct btrfs_extent_inline_ref
*)(block_info
+ 1);
7572 BUG_ON(!(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
));
7573 btrfs_set_extent_inline_ref_type(leaf
, iref
,
7574 BTRFS_SHARED_BLOCK_REF_KEY
);
7575 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
7577 btrfs_set_extent_inline_ref_type(leaf
, iref
,
7578 BTRFS_TREE_BLOCK_REF_KEY
);
7579 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
7582 btrfs_mark_buffer_dirty(leaf
);
7583 btrfs_free_path(path
);
7585 ret
= update_block_group(trans
, root
, ins
->objectid
, root
->nodesize
,
7587 if (ret
) { /* -ENOENT, logic error */
7588 btrfs_err(fs_info
, "update block group failed for %llu %llu",
7589 ins
->objectid
, ins
->offset
);
7593 trace_btrfs_reserved_extent_alloc(root
, ins
->objectid
, root
->nodesize
);
7597 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
7598 struct btrfs_root
*root
,
7599 u64 root_objectid
, u64 owner
,
7600 u64 offset
, struct btrfs_key
*ins
)
7604 BUG_ON(root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
7606 ret
= btrfs_add_delayed_data_ref(root
->fs_info
, trans
, ins
->objectid
,
7608 root_objectid
, owner
, offset
,
7609 BTRFS_ADD_DELAYED_EXTENT
, NULL
, 0);
7614 * this is used by the tree logging recovery code. It records that
7615 * an extent has been allocated and makes sure to clear the free
7616 * space cache bits as well
7618 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle
*trans
,
7619 struct btrfs_root
*root
,
7620 u64 root_objectid
, u64 owner
, u64 offset
,
7621 struct btrfs_key
*ins
)
7624 struct btrfs_block_group_cache
*block_group
;
7627 * Mixed block groups will exclude before processing the log so we only
7628 * need to do the exlude dance if this fs isn't mixed.
7630 if (!btrfs_fs_incompat(root
->fs_info
, MIXED_GROUPS
)) {
7631 ret
= __exclude_logged_extent(root
, ins
->objectid
, ins
->offset
);
7636 block_group
= btrfs_lookup_block_group(root
->fs_info
, ins
->objectid
);
7640 ret
= btrfs_update_reserved_bytes(block_group
, ins
->offset
,
7641 RESERVE_ALLOC_NO_ACCOUNT
, 0);
7642 BUG_ON(ret
); /* logic error */
7643 ret
= alloc_reserved_file_extent(trans
, root
, 0, root_objectid
,
7644 0, owner
, offset
, ins
, 1);
7645 btrfs_put_block_group(block_group
);
7649 static struct extent_buffer
*
7650 btrfs_init_new_buffer(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
7651 u64 bytenr
, int level
)
7653 struct extent_buffer
*buf
;
7655 buf
= btrfs_find_create_tree_block(root
, bytenr
);
7657 return ERR_PTR(-ENOMEM
);
7658 btrfs_set_header_generation(buf
, trans
->transid
);
7659 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, buf
, level
);
7660 btrfs_tree_lock(buf
);
7661 clean_tree_block(trans
, root
->fs_info
, buf
);
7662 clear_bit(EXTENT_BUFFER_STALE
, &buf
->bflags
);
7664 btrfs_set_lock_blocking(buf
);
7665 btrfs_set_buffer_uptodate(buf
);
7667 if (root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
) {
7668 buf
->log_index
= root
->log_transid
% 2;
7670 * we allow two log transactions at a time, use different
7671 * EXENT bit to differentiate dirty pages.
7673 if (buf
->log_index
== 0)
7674 set_extent_dirty(&root
->dirty_log_pages
, buf
->start
,
7675 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
7677 set_extent_new(&root
->dirty_log_pages
, buf
->start
,
7678 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
7680 buf
->log_index
= -1;
7681 set_extent_dirty(&trans
->transaction
->dirty_pages
, buf
->start
,
7682 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
7684 trans
->blocks_used
++;
7685 /* this returns a buffer locked for blocking */
7689 static struct btrfs_block_rsv
*
7690 use_block_rsv(struct btrfs_trans_handle
*trans
,
7691 struct btrfs_root
*root
, u32 blocksize
)
7693 struct btrfs_block_rsv
*block_rsv
;
7694 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
7696 bool global_updated
= false;
7698 block_rsv
= get_block_rsv(trans
, root
);
7700 if (unlikely(block_rsv
->size
== 0))
7703 ret
= block_rsv_use_bytes(block_rsv
, blocksize
);
7707 if (block_rsv
->failfast
)
7708 return ERR_PTR(ret
);
7710 if (block_rsv
->type
== BTRFS_BLOCK_RSV_GLOBAL
&& !global_updated
) {
7711 global_updated
= true;
7712 update_global_block_rsv(root
->fs_info
);
7716 if (btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
7717 static DEFINE_RATELIMIT_STATE(_rs
,
7718 DEFAULT_RATELIMIT_INTERVAL
* 10,
7719 /*DEFAULT_RATELIMIT_BURST*/ 1);
7720 if (__ratelimit(&_rs
))
7722 "BTRFS: block rsv returned %d\n", ret
);
7725 ret
= reserve_metadata_bytes(root
, block_rsv
, blocksize
,
7726 BTRFS_RESERVE_NO_FLUSH
);
7730 * If we couldn't reserve metadata bytes try and use some from
7731 * the global reserve if its space type is the same as the global
7734 if (block_rsv
->type
!= BTRFS_BLOCK_RSV_GLOBAL
&&
7735 block_rsv
->space_info
== global_rsv
->space_info
) {
7736 ret
= block_rsv_use_bytes(global_rsv
, blocksize
);
7740 return ERR_PTR(ret
);
7743 static void unuse_block_rsv(struct btrfs_fs_info
*fs_info
,
7744 struct btrfs_block_rsv
*block_rsv
, u32 blocksize
)
7746 block_rsv_add_bytes(block_rsv
, blocksize
, 0);
7747 block_rsv_release_bytes(fs_info
, block_rsv
, NULL
, 0);
7751 * finds a free extent and does all the dirty work required for allocation
7752 * returns the tree buffer or an ERR_PTR on error.
7754 struct extent_buffer
*btrfs_alloc_tree_block(struct btrfs_trans_handle
*trans
,
7755 struct btrfs_root
*root
,
7756 u64 parent
, u64 root_objectid
,
7757 struct btrfs_disk_key
*key
, int level
,
7758 u64 hint
, u64 empty_size
)
7760 struct btrfs_key ins
;
7761 struct btrfs_block_rsv
*block_rsv
;
7762 struct extent_buffer
*buf
;
7763 struct btrfs_delayed_extent_op
*extent_op
;
7766 u32 blocksize
= root
->nodesize
;
7767 bool skinny_metadata
= btrfs_fs_incompat(root
->fs_info
,
7770 if (btrfs_test_is_dummy_root(root
)) {
7771 buf
= btrfs_init_new_buffer(trans
, root
, root
->alloc_bytenr
,
7774 root
->alloc_bytenr
+= blocksize
;
7778 block_rsv
= use_block_rsv(trans
, root
, blocksize
);
7779 if (IS_ERR(block_rsv
))
7780 return ERR_CAST(block_rsv
);
7782 ret
= btrfs_reserve_extent(root
, blocksize
, blocksize
,
7783 empty_size
, hint
, &ins
, 0, 0);
7787 buf
= btrfs_init_new_buffer(trans
, root
, ins
.objectid
, level
);
7790 goto out_free_reserved
;
7793 if (root_objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
7795 parent
= ins
.objectid
;
7796 flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
7800 if (root_objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
7801 extent_op
= btrfs_alloc_delayed_extent_op();
7807 memcpy(&extent_op
->key
, key
, sizeof(extent_op
->key
));
7809 memset(&extent_op
->key
, 0, sizeof(extent_op
->key
));
7810 extent_op
->flags_to_set
= flags
;
7811 if (skinny_metadata
)
7812 extent_op
->update_key
= 0;
7814 extent_op
->update_key
= 1;
7815 extent_op
->update_flags
= 1;
7816 extent_op
->is_data
= 0;
7817 extent_op
->level
= level
;
7819 ret
= btrfs_add_delayed_tree_ref(root
->fs_info
, trans
,
7820 ins
.objectid
, ins
.offset
,
7821 parent
, root_objectid
, level
,
7822 BTRFS_ADD_DELAYED_EXTENT
,
7825 goto out_free_delayed
;
7830 btrfs_free_delayed_extent_op(extent_op
);
7832 free_extent_buffer(buf
);
7834 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
, 0);
7836 unuse_block_rsv(root
->fs_info
, block_rsv
, blocksize
);
7837 return ERR_PTR(ret
);
7840 struct walk_control
{
7841 u64 refs
[BTRFS_MAX_LEVEL
];
7842 u64 flags
[BTRFS_MAX_LEVEL
];
7843 struct btrfs_key update_progress
;
7854 #define DROP_REFERENCE 1
7855 #define UPDATE_BACKREF 2
7857 static noinline
void reada_walk_down(struct btrfs_trans_handle
*trans
,
7858 struct btrfs_root
*root
,
7859 struct walk_control
*wc
,
7860 struct btrfs_path
*path
)
7868 struct btrfs_key key
;
7869 struct extent_buffer
*eb
;
7874 if (path
->slots
[wc
->level
] < wc
->reada_slot
) {
7875 wc
->reada_count
= wc
->reada_count
* 2 / 3;
7876 wc
->reada_count
= max(wc
->reada_count
, 2);
7878 wc
->reada_count
= wc
->reada_count
* 3 / 2;
7879 wc
->reada_count
= min_t(int, wc
->reada_count
,
7880 BTRFS_NODEPTRS_PER_BLOCK(root
));
7883 eb
= path
->nodes
[wc
->level
];
7884 nritems
= btrfs_header_nritems(eb
);
7885 blocksize
= root
->nodesize
;
7887 for (slot
= path
->slots
[wc
->level
]; slot
< nritems
; slot
++) {
7888 if (nread
>= wc
->reada_count
)
7892 bytenr
= btrfs_node_blockptr(eb
, slot
);
7893 generation
= btrfs_node_ptr_generation(eb
, slot
);
7895 if (slot
== path
->slots
[wc
->level
])
7898 if (wc
->stage
== UPDATE_BACKREF
&&
7899 generation
<= root
->root_key
.offset
)
7902 /* We don't lock the tree block, it's OK to be racy here */
7903 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
,
7904 wc
->level
- 1, 1, &refs
,
7906 /* We don't care about errors in readahead. */
7911 if (wc
->stage
== DROP_REFERENCE
) {
7915 if (wc
->level
== 1 &&
7916 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
7918 if (!wc
->update_ref
||
7919 generation
<= root
->root_key
.offset
)
7921 btrfs_node_key_to_cpu(eb
, &key
, slot
);
7922 ret
= btrfs_comp_cpu_keys(&key
,
7923 &wc
->update_progress
);
7927 if (wc
->level
== 1 &&
7928 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
7932 readahead_tree_block(root
, bytenr
);
7935 wc
->reada_slot
= slot
;
7939 * TODO: Modify related function to add related node/leaf to dirty_extent_root,
7940 * for later qgroup accounting.
7942 * Current, this function does nothing.
7944 static int account_leaf_items(struct btrfs_trans_handle
*trans
,
7945 struct btrfs_root
*root
,
7946 struct extent_buffer
*eb
)
7948 int nr
= btrfs_header_nritems(eb
);
7950 struct btrfs_key key
;
7951 struct btrfs_file_extent_item
*fi
;
7952 u64 bytenr
, num_bytes
;
7954 for (i
= 0; i
< nr
; i
++) {
7955 btrfs_item_key_to_cpu(eb
, &key
, i
);
7957 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
7960 fi
= btrfs_item_ptr(eb
, i
, struct btrfs_file_extent_item
);
7961 /* filter out non qgroup-accountable extents */
7962 extent_type
= btrfs_file_extent_type(eb
, fi
);
7964 if (extent_type
== BTRFS_FILE_EXTENT_INLINE
)
7967 bytenr
= btrfs_file_extent_disk_bytenr(eb
, fi
);
7971 num_bytes
= btrfs_file_extent_disk_num_bytes(eb
, fi
);
7977 * Walk up the tree from the bottom, freeing leaves and any interior
7978 * nodes which have had all slots visited. If a node (leaf or
7979 * interior) is freed, the node above it will have it's slot
7980 * incremented. The root node will never be freed.
7982 * At the end of this function, we should have a path which has all
7983 * slots incremented to the next position for a search. If we need to
7984 * read a new node it will be NULL and the node above it will have the
7985 * correct slot selected for a later read.
7987 * If we increment the root nodes slot counter past the number of
7988 * elements, 1 is returned to signal completion of the search.
7990 static int adjust_slots_upwards(struct btrfs_root
*root
,
7991 struct btrfs_path
*path
, int root_level
)
7995 struct extent_buffer
*eb
;
7997 if (root_level
== 0)
8000 while (level
<= root_level
) {
8001 eb
= path
->nodes
[level
];
8002 nr
= btrfs_header_nritems(eb
);
8003 path
->slots
[level
]++;
8004 slot
= path
->slots
[level
];
8005 if (slot
>= nr
|| level
== 0) {
8007 * Don't free the root - we will detect this
8008 * condition after our loop and return a
8009 * positive value for caller to stop walking the tree.
8011 if (level
!= root_level
) {
8012 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
8013 path
->locks
[level
] = 0;
8015 free_extent_buffer(eb
);
8016 path
->nodes
[level
] = NULL
;
8017 path
->slots
[level
] = 0;
8021 * We have a valid slot to walk back down
8022 * from. Stop here so caller can process these
8031 eb
= path
->nodes
[root_level
];
8032 if (path
->slots
[root_level
] >= btrfs_header_nritems(eb
))
8039 * root_eb is the subtree root and is locked before this function is called.
8040 * TODO: Modify this function to mark all (including complete shared node)
8041 * to dirty_extent_root to allow it get accounted in qgroup.
8043 static int account_shared_subtree(struct btrfs_trans_handle
*trans
,
8044 struct btrfs_root
*root
,
8045 struct extent_buffer
*root_eb
,
8051 struct extent_buffer
*eb
= root_eb
;
8052 struct btrfs_path
*path
= NULL
;
8054 BUG_ON(root_level
< 0 || root_level
> BTRFS_MAX_LEVEL
);
8055 BUG_ON(root_eb
== NULL
);
8057 if (!root
->fs_info
->quota_enabled
)
8060 if (!extent_buffer_uptodate(root_eb
)) {
8061 ret
= btrfs_read_buffer(root_eb
, root_gen
);
8066 if (root_level
== 0) {
8067 ret
= account_leaf_items(trans
, root
, root_eb
);
8071 path
= btrfs_alloc_path();
8076 * Walk down the tree. Missing extent blocks are filled in as
8077 * we go. Metadata is accounted every time we read a new
8080 * When we reach a leaf, we account for file extent items in it,
8081 * walk back up the tree (adjusting slot pointers as we go)
8082 * and restart the search process.
8084 extent_buffer_get(root_eb
); /* For path */
8085 path
->nodes
[root_level
] = root_eb
;
8086 path
->slots
[root_level
] = 0;
8087 path
->locks
[root_level
] = 0; /* so release_path doesn't try to unlock */
8090 while (level
>= 0) {
8091 if (path
->nodes
[level
] == NULL
) {
8096 /* We need to get child blockptr/gen from
8097 * parent before we can read it. */
8098 eb
= path
->nodes
[level
+ 1];
8099 parent_slot
= path
->slots
[level
+ 1];
8100 child_bytenr
= btrfs_node_blockptr(eb
, parent_slot
);
8101 child_gen
= btrfs_node_ptr_generation(eb
, parent_slot
);
8103 eb
= read_tree_block(root
, child_bytenr
, child_gen
);
8107 } else if (!extent_buffer_uptodate(eb
)) {
8108 free_extent_buffer(eb
);
8113 path
->nodes
[level
] = eb
;
8114 path
->slots
[level
] = 0;
8116 btrfs_tree_read_lock(eb
);
8117 btrfs_set_lock_blocking_rw(eb
, BTRFS_READ_LOCK
);
8118 path
->locks
[level
] = BTRFS_READ_LOCK_BLOCKING
;
8122 ret
= account_leaf_items(trans
, root
, path
->nodes
[level
]);
8126 /* Nonzero return here means we completed our search */
8127 ret
= adjust_slots_upwards(root
, path
, root_level
);
8131 /* Restart search with new slots */
8140 btrfs_free_path(path
);
8146 * helper to process tree block while walking down the tree.
8148 * when wc->stage == UPDATE_BACKREF, this function updates
8149 * back refs for pointers in the block.
8151 * NOTE: return value 1 means we should stop walking down.
8153 static noinline
int walk_down_proc(struct btrfs_trans_handle
*trans
,
8154 struct btrfs_root
*root
,
8155 struct btrfs_path
*path
,
8156 struct walk_control
*wc
, int lookup_info
)
8158 int level
= wc
->level
;
8159 struct extent_buffer
*eb
= path
->nodes
[level
];
8160 u64 flag
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
8163 if (wc
->stage
== UPDATE_BACKREF
&&
8164 btrfs_header_owner(eb
) != root
->root_key
.objectid
)
8168 * when reference count of tree block is 1, it won't increase
8169 * again. once full backref flag is set, we never clear it.
8172 ((wc
->stage
== DROP_REFERENCE
&& wc
->refs
[level
] != 1) ||
8173 (wc
->stage
== UPDATE_BACKREF
&& !(wc
->flags
[level
] & flag
)))) {
8174 BUG_ON(!path
->locks
[level
]);
8175 ret
= btrfs_lookup_extent_info(trans
, root
,
8176 eb
->start
, level
, 1,
8179 BUG_ON(ret
== -ENOMEM
);
8182 BUG_ON(wc
->refs
[level
] == 0);
8185 if (wc
->stage
== DROP_REFERENCE
) {
8186 if (wc
->refs
[level
] > 1)
8189 if (path
->locks
[level
] && !wc
->keep_locks
) {
8190 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
8191 path
->locks
[level
] = 0;
8196 /* wc->stage == UPDATE_BACKREF */
8197 if (!(wc
->flags
[level
] & flag
)) {
8198 BUG_ON(!path
->locks
[level
]);
8199 ret
= btrfs_inc_ref(trans
, root
, eb
, 1);
8200 BUG_ON(ret
); /* -ENOMEM */
8201 ret
= btrfs_dec_ref(trans
, root
, eb
, 0);
8202 BUG_ON(ret
); /* -ENOMEM */
8203 ret
= btrfs_set_disk_extent_flags(trans
, root
, eb
->start
,
8205 btrfs_header_level(eb
), 0);
8206 BUG_ON(ret
); /* -ENOMEM */
8207 wc
->flags
[level
] |= flag
;
8211 * the block is shared by multiple trees, so it's not good to
8212 * keep the tree lock
8214 if (path
->locks
[level
] && level
> 0) {
8215 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
8216 path
->locks
[level
] = 0;
8222 * helper to process tree block pointer.
8224 * when wc->stage == DROP_REFERENCE, this function checks
8225 * reference count of the block pointed to. if the block
8226 * is shared and we need update back refs for the subtree
8227 * rooted at the block, this function changes wc->stage to
8228 * UPDATE_BACKREF. if the block is shared and there is no
8229 * need to update back, this function drops the reference
8232 * NOTE: return value 1 means we should stop walking down.
8234 static noinline
int do_walk_down(struct btrfs_trans_handle
*trans
,
8235 struct btrfs_root
*root
,
8236 struct btrfs_path
*path
,
8237 struct walk_control
*wc
, int *lookup_info
)
8243 struct btrfs_key key
;
8244 struct extent_buffer
*next
;
8245 int level
= wc
->level
;
8248 bool need_account
= false;
8250 generation
= btrfs_node_ptr_generation(path
->nodes
[level
],
8251 path
->slots
[level
]);
8253 * if the lower level block was created before the snapshot
8254 * was created, we know there is no need to update back refs
8257 if (wc
->stage
== UPDATE_BACKREF
&&
8258 generation
<= root
->root_key
.offset
) {
8263 bytenr
= btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]);
8264 blocksize
= root
->nodesize
;
8266 next
= btrfs_find_tree_block(root
->fs_info
, bytenr
);
8268 next
= btrfs_find_create_tree_block(root
, bytenr
);
8271 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, next
,
8275 btrfs_tree_lock(next
);
8276 btrfs_set_lock_blocking(next
);
8278 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
, level
- 1, 1,
8279 &wc
->refs
[level
- 1],
8280 &wc
->flags
[level
- 1]);
8282 btrfs_tree_unlock(next
);
8286 if (unlikely(wc
->refs
[level
- 1] == 0)) {
8287 btrfs_err(root
->fs_info
, "Missing references.");
8292 if (wc
->stage
== DROP_REFERENCE
) {
8293 if (wc
->refs
[level
- 1] > 1) {
8294 need_account
= true;
8296 (wc
->flags
[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF
))
8299 if (!wc
->update_ref
||
8300 generation
<= root
->root_key
.offset
)
8303 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
8304 path
->slots
[level
]);
8305 ret
= btrfs_comp_cpu_keys(&key
, &wc
->update_progress
);
8309 wc
->stage
= UPDATE_BACKREF
;
8310 wc
->shared_level
= level
- 1;
8314 (wc
->flags
[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF
))
8318 if (!btrfs_buffer_uptodate(next
, generation
, 0)) {
8319 btrfs_tree_unlock(next
);
8320 free_extent_buffer(next
);
8326 if (reada
&& level
== 1)
8327 reada_walk_down(trans
, root
, wc
, path
);
8328 next
= read_tree_block(root
, bytenr
, generation
);
8330 return PTR_ERR(next
);
8331 } else if (!extent_buffer_uptodate(next
)) {
8332 free_extent_buffer(next
);
8335 btrfs_tree_lock(next
);
8336 btrfs_set_lock_blocking(next
);
8340 BUG_ON(level
!= btrfs_header_level(next
));
8341 path
->nodes
[level
] = next
;
8342 path
->slots
[level
] = 0;
8343 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
8349 wc
->refs
[level
- 1] = 0;
8350 wc
->flags
[level
- 1] = 0;
8351 if (wc
->stage
== DROP_REFERENCE
) {
8352 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
) {
8353 parent
= path
->nodes
[level
]->start
;
8355 BUG_ON(root
->root_key
.objectid
!=
8356 btrfs_header_owner(path
->nodes
[level
]));
8361 ret
= account_shared_subtree(trans
, root
, next
,
8362 generation
, level
- 1);
8364 btrfs_err_rl(root
->fs_info
,
8366 "%d accounting shared subtree. Quota "
8367 "is out of sync, rescan required.",
8371 ret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
, parent
,
8372 root
->root_key
.objectid
, level
- 1, 0, 0);
8373 BUG_ON(ret
); /* -ENOMEM */
8375 btrfs_tree_unlock(next
);
8376 free_extent_buffer(next
);
8382 * helper to process tree block while walking up the tree.
8384 * when wc->stage == DROP_REFERENCE, this function drops
8385 * reference count on the block.
8387 * when wc->stage == UPDATE_BACKREF, this function changes
8388 * wc->stage back to DROP_REFERENCE if we changed wc->stage
8389 * to UPDATE_BACKREF previously while processing the block.
8391 * NOTE: return value 1 means we should stop walking up.
8393 static noinline
int walk_up_proc(struct btrfs_trans_handle
*trans
,
8394 struct btrfs_root
*root
,
8395 struct btrfs_path
*path
,
8396 struct walk_control
*wc
)
8399 int level
= wc
->level
;
8400 struct extent_buffer
*eb
= path
->nodes
[level
];
8403 if (wc
->stage
== UPDATE_BACKREF
) {
8404 BUG_ON(wc
->shared_level
< level
);
8405 if (level
< wc
->shared_level
)
8408 ret
= find_next_key(path
, level
+ 1, &wc
->update_progress
);
8412 wc
->stage
= DROP_REFERENCE
;
8413 wc
->shared_level
= -1;
8414 path
->slots
[level
] = 0;
8417 * check reference count again if the block isn't locked.
8418 * we should start walking down the tree again if reference
8421 if (!path
->locks
[level
]) {
8423 btrfs_tree_lock(eb
);
8424 btrfs_set_lock_blocking(eb
);
8425 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
8427 ret
= btrfs_lookup_extent_info(trans
, root
,
8428 eb
->start
, level
, 1,
8432 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
8433 path
->locks
[level
] = 0;
8436 BUG_ON(wc
->refs
[level
] == 0);
8437 if (wc
->refs
[level
] == 1) {
8438 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
8439 path
->locks
[level
] = 0;
8445 /* wc->stage == DROP_REFERENCE */
8446 BUG_ON(wc
->refs
[level
] > 1 && !path
->locks
[level
]);
8448 if (wc
->refs
[level
] == 1) {
8450 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
8451 ret
= btrfs_dec_ref(trans
, root
, eb
, 1);
8453 ret
= btrfs_dec_ref(trans
, root
, eb
, 0);
8454 BUG_ON(ret
); /* -ENOMEM */
8455 ret
= account_leaf_items(trans
, root
, eb
);
8457 btrfs_err_rl(root
->fs_info
,
8459 "%d accounting leaf items. Quota "
8460 "is out of sync, rescan required.",
8464 /* make block locked assertion in clean_tree_block happy */
8465 if (!path
->locks
[level
] &&
8466 btrfs_header_generation(eb
) == trans
->transid
) {
8467 btrfs_tree_lock(eb
);
8468 btrfs_set_lock_blocking(eb
);
8469 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
8471 clean_tree_block(trans
, root
->fs_info
, eb
);
8474 if (eb
== root
->node
) {
8475 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
8478 BUG_ON(root
->root_key
.objectid
!=
8479 btrfs_header_owner(eb
));
8481 if (wc
->flags
[level
+ 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
8482 parent
= path
->nodes
[level
+ 1]->start
;
8484 BUG_ON(root
->root_key
.objectid
!=
8485 btrfs_header_owner(path
->nodes
[level
+ 1]));
8488 btrfs_free_tree_block(trans
, root
, eb
, parent
, wc
->refs
[level
] == 1);
8490 wc
->refs
[level
] = 0;
8491 wc
->flags
[level
] = 0;
8495 static noinline
int walk_down_tree(struct btrfs_trans_handle
*trans
,
8496 struct btrfs_root
*root
,
8497 struct btrfs_path
*path
,
8498 struct walk_control
*wc
)
8500 int level
= wc
->level
;
8501 int lookup_info
= 1;
8504 while (level
>= 0) {
8505 ret
= walk_down_proc(trans
, root
, path
, wc
, lookup_info
);
8512 if (path
->slots
[level
] >=
8513 btrfs_header_nritems(path
->nodes
[level
]))
8516 ret
= do_walk_down(trans
, root
, path
, wc
, &lookup_info
);
8518 path
->slots
[level
]++;
8527 static noinline
int walk_up_tree(struct btrfs_trans_handle
*trans
,
8528 struct btrfs_root
*root
,
8529 struct btrfs_path
*path
,
8530 struct walk_control
*wc
, int max_level
)
8532 int level
= wc
->level
;
8535 path
->slots
[level
] = btrfs_header_nritems(path
->nodes
[level
]);
8536 while (level
< max_level
&& path
->nodes
[level
]) {
8538 if (path
->slots
[level
] + 1 <
8539 btrfs_header_nritems(path
->nodes
[level
])) {
8540 path
->slots
[level
]++;
8543 ret
= walk_up_proc(trans
, root
, path
, wc
);
8547 if (path
->locks
[level
]) {
8548 btrfs_tree_unlock_rw(path
->nodes
[level
],
8549 path
->locks
[level
]);
8550 path
->locks
[level
] = 0;
8552 free_extent_buffer(path
->nodes
[level
]);
8553 path
->nodes
[level
] = NULL
;
8561 * drop a subvolume tree.
8563 * this function traverses the tree freeing any blocks that only
8564 * referenced by the tree.
8566 * when a shared tree block is found. this function decreases its
8567 * reference count by one. if update_ref is true, this function
8568 * also make sure backrefs for the shared block and all lower level
8569 * blocks are properly updated.
8571 * If called with for_reloc == 0, may exit early with -EAGAIN
8573 int btrfs_drop_snapshot(struct btrfs_root
*root
,
8574 struct btrfs_block_rsv
*block_rsv
, int update_ref
,
8577 struct btrfs_path
*path
;
8578 struct btrfs_trans_handle
*trans
;
8579 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
8580 struct btrfs_root_item
*root_item
= &root
->root_item
;
8581 struct walk_control
*wc
;
8582 struct btrfs_key key
;
8586 bool root_dropped
= false;
8588 btrfs_debug(root
->fs_info
, "Drop subvolume %llu", root
->objectid
);
8590 path
= btrfs_alloc_path();
8596 wc
= kzalloc(sizeof(*wc
), GFP_NOFS
);
8598 btrfs_free_path(path
);
8603 trans
= btrfs_start_transaction(tree_root
, 0);
8604 if (IS_ERR(trans
)) {
8605 err
= PTR_ERR(trans
);
8610 trans
->block_rsv
= block_rsv
;
8612 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
8613 level
= btrfs_header_level(root
->node
);
8614 path
->nodes
[level
] = btrfs_lock_root_node(root
);
8615 btrfs_set_lock_blocking(path
->nodes
[level
]);
8616 path
->slots
[level
] = 0;
8617 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
8618 memset(&wc
->update_progress
, 0,
8619 sizeof(wc
->update_progress
));
8621 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
8622 memcpy(&wc
->update_progress
, &key
,
8623 sizeof(wc
->update_progress
));
8625 level
= root_item
->drop_level
;
8627 path
->lowest_level
= level
;
8628 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
8629 path
->lowest_level
= 0;
8637 * unlock our path, this is safe because only this
8638 * function is allowed to delete this snapshot
8640 btrfs_unlock_up_safe(path
, 0);
8642 level
= btrfs_header_level(root
->node
);
8644 btrfs_tree_lock(path
->nodes
[level
]);
8645 btrfs_set_lock_blocking(path
->nodes
[level
]);
8646 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
8648 ret
= btrfs_lookup_extent_info(trans
, root
,
8649 path
->nodes
[level
]->start
,
8650 level
, 1, &wc
->refs
[level
],
8656 BUG_ON(wc
->refs
[level
] == 0);
8658 if (level
== root_item
->drop_level
)
8661 btrfs_tree_unlock(path
->nodes
[level
]);
8662 path
->locks
[level
] = 0;
8663 WARN_ON(wc
->refs
[level
] != 1);
8669 wc
->shared_level
= -1;
8670 wc
->stage
= DROP_REFERENCE
;
8671 wc
->update_ref
= update_ref
;
8673 wc
->for_reloc
= for_reloc
;
8674 wc
->reada_count
= BTRFS_NODEPTRS_PER_BLOCK(root
);
8678 ret
= walk_down_tree(trans
, root
, path
, wc
);
8684 ret
= walk_up_tree(trans
, root
, path
, wc
, BTRFS_MAX_LEVEL
);
8691 BUG_ON(wc
->stage
!= DROP_REFERENCE
);
8695 if (wc
->stage
== DROP_REFERENCE
) {
8697 btrfs_node_key(path
->nodes
[level
],
8698 &root_item
->drop_progress
,
8699 path
->slots
[level
]);
8700 root_item
->drop_level
= level
;
8703 BUG_ON(wc
->level
== 0);
8704 if (btrfs_should_end_transaction(trans
, tree_root
) ||
8705 (!for_reloc
&& btrfs_need_cleaner_sleep(root
))) {
8706 ret
= btrfs_update_root(trans
, tree_root
,
8710 btrfs_abort_transaction(trans
, tree_root
, ret
);
8715 btrfs_end_transaction_throttle(trans
, tree_root
);
8716 if (!for_reloc
&& btrfs_need_cleaner_sleep(root
)) {
8717 pr_debug("BTRFS: drop snapshot early exit\n");
8722 trans
= btrfs_start_transaction(tree_root
, 0);
8723 if (IS_ERR(trans
)) {
8724 err
= PTR_ERR(trans
);
8728 trans
->block_rsv
= block_rsv
;
8731 btrfs_release_path(path
);
8735 ret
= btrfs_del_root(trans
, tree_root
, &root
->root_key
);
8737 btrfs_abort_transaction(trans
, tree_root
, ret
);
8741 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
8742 ret
= btrfs_find_root(tree_root
, &root
->root_key
, path
,
8745 btrfs_abort_transaction(trans
, tree_root
, ret
);
8748 } else if (ret
> 0) {
8749 /* if we fail to delete the orphan item this time
8750 * around, it'll get picked up the next time.
8752 * The most common failure here is just -ENOENT.
8754 btrfs_del_orphan_item(trans
, tree_root
,
8755 root
->root_key
.objectid
);
8759 if (test_bit(BTRFS_ROOT_IN_RADIX
, &root
->state
)) {
8760 btrfs_add_dropped_root(trans
, root
);
8762 free_extent_buffer(root
->node
);
8763 free_extent_buffer(root
->commit_root
);
8764 btrfs_put_fs_root(root
);
8766 root_dropped
= true;
8768 btrfs_end_transaction_throttle(trans
, tree_root
);
8771 btrfs_free_path(path
);
8774 * So if we need to stop dropping the snapshot for whatever reason we
8775 * need to make sure to add it back to the dead root list so that we
8776 * keep trying to do the work later. This also cleans up roots if we
8777 * don't have it in the radix (like when we recover after a power fail
8778 * or unmount) so we don't leak memory.
8780 if (!for_reloc
&& root_dropped
== false)
8781 btrfs_add_dead_root(root
);
8782 if (err
&& err
!= -EAGAIN
)
8783 btrfs_std_error(root
->fs_info
, err
, NULL
);
8788 * drop subtree rooted at tree block 'node'.
8790 * NOTE: this function will unlock and release tree block 'node'
8791 * only used by relocation code
8793 int btrfs_drop_subtree(struct btrfs_trans_handle
*trans
,
8794 struct btrfs_root
*root
,
8795 struct extent_buffer
*node
,
8796 struct extent_buffer
*parent
)
8798 struct btrfs_path
*path
;
8799 struct walk_control
*wc
;
8805 BUG_ON(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
8807 path
= btrfs_alloc_path();
8811 wc
= kzalloc(sizeof(*wc
), GFP_NOFS
);
8813 btrfs_free_path(path
);
8817 btrfs_assert_tree_locked(parent
);
8818 parent_level
= btrfs_header_level(parent
);
8819 extent_buffer_get(parent
);
8820 path
->nodes
[parent_level
] = parent
;
8821 path
->slots
[parent_level
] = btrfs_header_nritems(parent
);
8823 btrfs_assert_tree_locked(node
);
8824 level
= btrfs_header_level(node
);
8825 path
->nodes
[level
] = node
;
8826 path
->slots
[level
] = 0;
8827 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
8829 wc
->refs
[parent_level
] = 1;
8830 wc
->flags
[parent_level
] = BTRFS_BLOCK_FLAG_FULL_BACKREF
;
8832 wc
->shared_level
= -1;
8833 wc
->stage
= DROP_REFERENCE
;
8837 wc
->reada_count
= BTRFS_NODEPTRS_PER_BLOCK(root
);
8840 wret
= walk_down_tree(trans
, root
, path
, wc
);
8846 wret
= walk_up_tree(trans
, root
, path
, wc
, parent_level
);
8854 btrfs_free_path(path
);
8858 static u64
update_block_group_flags(struct btrfs_root
*root
, u64 flags
)
8864 * if restripe for this chunk_type is on pick target profile and
8865 * return, otherwise do the usual balance
8867 stripped
= get_restripe_target(root
->fs_info
, flags
);
8869 return extended_to_chunk(stripped
);
8871 num_devices
= root
->fs_info
->fs_devices
->rw_devices
;
8873 stripped
= BTRFS_BLOCK_GROUP_RAID0
|
8874 BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
|
8875 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
;
8877 if (num_devices
== 1) {
8878 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
8879 stripped
= flags
& ~stripped
;
8881 /* turn raid0 into single device chunks */
8882 if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
8885 /* turn mirroring into duplication */
8886 if (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
8887 BTRFS_BLOCK_GROUP_RAID10
))
8888 return stripped
| BTRFS_BLOCK_GROUP_DUP
;
8890 /* they already had raid on here, just return */
8891 if (flags
& stripped
)
8894 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
8895 stripped
= flags
& ~stripped
;
8897 /* switch duplicated blocks with raid1 */
8898 if (flags
& BTRFS_BLOCK_GROUP_DUP
)
8899 return stripped
| BTRFS_BLOCK_GROUP_RAID1
;
8901 /* this is drive concat, leave it alone */
8907 static int inc_block_group_ro(struct btrfs_block_group_cache
*cache
, int force
)
8909 struct btrfs_space_info
*sinfo
= cache
->space_info
;
8911 u64 min_allocable_bytes
;
8915 * We need some metadata space and system metadata space for
8916 * allocating chunks in some corner cases until we force to set
8917 * it to be readonly.
8920 (BTRFS_BLOCK_GROUP_SYSTEM
| BTRFS_BLOCK_GROUP_METADATA
)) &&
8922 min_allocable_bytes
= 1 * 1024 * 1024;
8924 min_allocable_bytes
= 0;
8926 spin_lock(&sinfo
->lock
);
8927 spin_lock(&cache
->lock
);
8935 num_bytes
= cache
->key
.offset
- cache
->reserved
- cache
->pinned
-
8936 cache
->bytes_super
- btrfs_block_group_used(&cache
->item
);
8938 if (sinfo
->bytes_used
+ sinfo
->bytes_reserved
+ sinfo
->bytes_pinned
+
8939 sinfo
->bytes_may_use
+ sinfo
->bytes_readonly
+ num_bytes
+
8940 min_allocable_bytes
<= sinfo
->total_bytes
) {
8941 sinfo
->bytes_readonly
+= num_bytes
;
8943 list_add_tail(&cache
->ro_list
, &sinfo
->ro_bgs
);
8947 spin_unlock(&cache
->lock
);
8948 spin_unlock(&sinfo
->lock
);
8952 int btrfs_inc_block_group_ro(struct btrfs_root
*root
,
8953 struct btrfs_block_group_cache
*cache
)
8956 struct btrfs_trans_handle
*trans
;
8961 trans
= btrfs_join_transaction(root
);
8963 return PTR_ERR(trans
);
8966 * we're not allowed to set block groups readonly after the dirty
8967 * block groups cache has started writing. If it already started,
8968 * back off and let this transaction commit
8970 mutex_lock(&root
->fs_info
->ro_block_group_mutex
);
8971 if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN
, &trans
->transaction
->flags
)) {
8972 u64 transid
= trans
->transid
;
8974 mutex_unlock(&root
->fs_info
->ro_block_group_mutex
);
8975 btrfs_end_transaction(trans
, root
);
8977 ret
= btrfs_wait_for_commit(root
, transid
);
8984 * if we are changing raid levels, try to allocate a corresponding
8985 * block group with the new raid level.
8987 alloc_flags
= update_block_group_flags(root
, cache
->flags
);
8988 if (alloc_flags
!= cache
->flags
) {
8989 ret
= do_chunk_alloc(trans
, root
, alloc_flags
,
8992 * ENOSPC is allowed here, we may have enough space
8993 * already allocated at the new raid level to
9002 ret
= inc_block_group_ro(cache
, 0);
9005 alloc_flags
= get_alloc_profile(root
, cache
->space_info
->flags
);
9006 ret
= do_chunk_alloc(trans
, root
, alloc_flags
,
9010 ret
= inc_block_group_ro(cache
, 0);
9012 if (cache
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
) {
9013 alloc_flags
= update_block_group_flags(root
, cache
->flags
);
9014 lock_chunks(root
->fs_info
->chunk_root
);
9015 check_system_chunk(trans
, root
, alloc_flags
);
9016 unlock_chunks(root
->fs_info
->chunk_root
);
9018 mutex_unlock(&root
->fs_info
->ro_block_group_mutex
);
9020 btrfs_end_transaction(trans
, root
);
9024 int btrfs_force_chunk_alloc(struct btrfs_trans_handle
*trans
,
9025 struct btrfs_root
*root
, u64 type
)
9027 u64 alloc_flags
= get_alloc_profile(root
, type
);
9028 return do_chunk_alloc(trans
, root
, alloc_flags
,
9033 * helper to account the unused space of all the readonly block group in the
9034 * space_info. takes mirrors into account.
9036 u64
btrfs_account_ro_block_groups_free_space(struct btrfs_space_info
*sinfo
)
9038 struct btrfs_block_group_cache
*block_group
;
9042 /* It's df, we don't care if it's racey */
9043 if (list_empty(&sinfo
->ro_bgs
))
9046 spin_lock(&sinfo
->lock
);
9047 list_for_each_entry(block_group
, &sinfo
->ro_bgs
, ro_list
) {
9048 spin_lock(&block_group
->lock
);
9050 if (!block_group
->ro
) {
9051 spin_unlock(&block_group
->lock
);
9055 if (block_group
->flags
& (BTRFS_BLOCK_GROUP_RAID1
|
9056 BTRFS_BLOCK_GROUP_RAID10
|
9057 BTRFS_BLOCK_GROUP_DUP
))
9062 free_bytes
+= (block_group
->key
.offset
-
9063 btrfs_block_group_used(&block_group
->item
)) *
9066 spin_unlock(&block_group
->lock
);
9068 spin_unlock(&sinfo
->lock
);
9073 void btrfs_dec_block_group_ro(struct btrfs_root
*root
,
9074 struct btrfs_block_group_cache
*cache
)
9076 struct btrfs_space_info
*sinfo
= cache
->space_info
;
9081 spin_lock(&sinfo
->lock
);
9082 spin_lock(&cache
->lock
);
9084 num_bytes
= cache
->key
.offset
- cache
->reserved
-
9085 cache
->pinned
- cache
->bytes_super
-
9086 btrfs_block_group_used(&cache
->item
);
9087 sinfo
->bytes_readonly
-= num_bytes
;
9088 list_del_init(&cache
->ro_list
);
9090 spin_unlock(&cache
->lock
);
9091 spin_unlock(&sinfo
->lock
);
9095 * checks to see if its even possible to relocate this block group.
9097 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
9098 * ok to go ahead and try.
9100 int btrfs_can_relocate(struct btrfs_root
*root
, u64 bytenr
)
9102 struct btrfs_block_group_cache
*block_group
;
9103 struct btrfs_space_info
*space_info
;
9104 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
9105 struct btrfs_device
*device
;
9106 struct btrfs_trans_handle
*trans
;
9115 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
9117 /* odd, couldn't find the block group, leave it alone */
9121 min_free
= btrfs_block_group_used(&block_group
->item
);
9123 /* no bytes used, we're good */
9127 space_info
= block_group
->space_info
;
9128 spin_lock(&space_info
->lock
);
9130 full
= space_info
->full
;
9133 * if this is the last block group we have in this space, we can't
9134 * relocate it unless we're able to allocate a new chunk below.
9136 * Otherwise, we need to make sure we have room in the space to handle
9137 * all of the extents from this block group. If we can, we're good
9139 if ((space_info
->total_bytes
!= block_group
->key
.offset
) &&
9140 (space_info
->bytes_used
+ space_info
->bytes_reserved
+
9141 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
9142 min_free
< space_info
->total_bytes
)) {
9143 spin_unlock(&space_info
->lock
);
9146 spin_unlock(&space_info
->lock
);
9149 * ok we don't have enough space, but maybe we have free space on our
9150 * devices to allocate new chunks for relocation, so loop through our
9151 * alloc devices and guess if we have enough space. if this block
9152 * group is going to be restriped, run checks against the target
9153 * profile instead of the current one.
9165 target
= get_restripe_target(root
->fs_info
, block_group
->flags
);
9167 index
= __get_raid_index(extended_to_chunk(target
));
9170 * this is just a balance, so if we were marked as full
9171 * we know there is no space for a new chunk
9176 index
= get_block_group_index(block_group
);
9179 if (index
== BTRFS_RAID_RAID10
) {
9183 } else if (index
== BTRFS_RAID_RAID1
) {
9185 } else if (index
== BTRFS_RAID_DUP
) {
9188 } else if (index
== BTRFS_RAID_RAID0
) {
9189 dev_min
= fs_devices
->rw_devices
;
9190 min_free
= div64_u64(min_free
, dev_min
);
9193 /* We need to do this so that we can look at pending chunks */
9194 trans
= btrfs_join_transaction(root
);
9195 if (IS_ERR(trans
)) {
9196 ret
= PTR_ERR(trans
);
9200 mutex_lock(&root
->fs_info
->chunk_mutex
);
9201 list_for_each_entry(device
, &fs_devices
->alloc_list
, dev_alloc_list
) {
9205 * check to make sure we can actually find a chunk with enough
9206 * space to fit our block group in.
9208 if (device
->total_bytes
> device
->bytes_used
+ min_free
&&
9209 !device
->is_tgtdev_for_dev_replace
) {
9210 ret
= find_free_dev_extent(trans
, device
, min_free
,
9215 if (dev_nr
>= dev_min
)
9221 mutex_unlock(&root
->fs_info
->chunk_mutex
);
9222 btrfs_end_transaction(trans
, root
);
9224 btrfs_put_block_group(block_group
);
9228 static int find_first_block_group(struct btrfs_root
*root
,
9229 struct btrfs_path
*path
, struct btrfs_key
*key
)
9232 struct btrfs_key found_key
;
9233 struct extent_buffer
*leaf
;
9236 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
9241 slot
= path
->slots
[0];
9242 leaf
= path
->nodes
[0];
9243 if (slot
>= btrfs_header_nritems(leaf
)) {
9244 ret
= btrfs_next_leaf(root
, path
);
9251 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
9253 if (found_key
.objectid
>= key
->objectid
&&
9254 found_key
.type
== BTRFS_BLOCK_GROUP_ITEM_KEY
) {
9264 void btrfs_put_block_group_cache(struct btrfs_fs_info
*info
)
9266 struct btrfs_block_group_cache
*block_group
;
9270 struct inode
*inode
;
9272 block_group
= btrfs_lookup_first_block_group(info
, last
);
9273 while (block_group
) {
9274 spin_lock(&block_group
->lock
);
9275 if (block_group
->iref
)
9277 spin_unlock(&block_group
->lock
);
9278 block_group
= next_block_group(info
->tree_root
,
9288 inode
= block_group
->inode
;
9289 block_group
->iref
= 0;
9290 block_group
->inode
= NULL
;
9291 spin_unlock(&block_group
->lock
);
9293 last
= block_group
->key
.objectid
+ block_group
->key
.offset
;
9294 btrfs_put_block_group(block_group
);
9298 int btrfs_free_block_groups(struct btrfs_fs_info
*info
)
9300 struct btrfs_block_group_cache
*block_group
;
9301 struct btrfs_space_info
*space_info
;
9302 struct btrfs_caching_control
*caching_ctl
;
9305 down_write(&info
->commit_root_sem
);
9306 while (!list_empty(&info
->caching_block_groups
)) {
9307 caching_ctl
= list_entry(info
->caching_block_groups
.next
,
9308 struct btrfs_caching_control
, list
);
9309 list_del(&caching_ctl
->list
);
9310 put_caching_control(caching_ctl
);
9312 up_write(&info
->commit_root_sem
);
9314 spin_lock(&info
->unused_bgs_lock
);
9315 while (!list_empty(&info
->unused_bgs
)) {
9316 block_group
= list_first_entry(&info
->unused_bgs
,
9317 struct btrfs_block_group_cache
,
9319 list_del_init(&block_group
->bg_list
);
9320 btrfs_put_block_group(block_group
);
9322 spin_unlock(&info
->unused_bgs_lock
);
9324 spin_lock(&info
->block_group_cache_lock
);
9325 while ((n
= rb_last(&info
->block_group_cache_tree
)) != NULL
) {
9326 block_group
= rb_entry(n
, struct btrfs_block_group_cache
,
9328 rb_erase(&block_group
->cache_node
,
9329 &info
->block_group_cache_tree
);
9330 RB_CLEAR_NODE(&block_group
->cache_node
);
9331 spin_unlock(&info
->block_group_cache_lock
);
9333 down_write(&block_group
->space_info
->groups_sem
);
9334 list_del(&block_group
->list
);
9335 up_write(&block_group
->space_info
->groups_sem
);
9337 if (block_group
->cached
== BTRFS_CACHE_STARTED
)
9338 wait_block_group_cache_done(block_group
);
9341 * We haven't cached this block group, which means we could
9342 * possibly have excluded extents on this block group.
9344 if (block_group
->cached
== BTRFS_CACHE_NO
||
9345 block_group
->cached
== BTRFS_CACHE_ERROR
)
9346 free_excluded_extents(info
->extent_root
, block_group
);
9348 btrfs_remove_free_space_cache(block_group
);
9349 btrfs_put_block_group(block_group
);
9351 spin_lock(&info
->block_group_cache_lock
);
9353 spin_unlock(&info
->block_group_cache_lock
);
9355 /* now that all the block groups are freed, go through and
9356 * free all the space_info structs. This is only called during
9357 * the final stages of unmount, and so we know nobody is
9358 * using them. We call synchronize_rcu() once before we start,
9359 * just to be on the safe side.
9363 release_global_block_rsv(info
);
9365 while (!list_empty(&info
->space_info
)) {
9368 space_info
= list_entry(info
->space_info
.next
,
9369 struct btrfs_space_info
,
9371 if (btrfs_test_opt(info
->tree_root
, ENOSPC_DEBUG
)) {
9372 if (WARN_ON(space_info
->bytes_pinned
> 0 ||
9373 space_info
->bytes_reserved
> 0 ||
9374 space_info
->bytes_may_use
> 0)) {
9375 dump_space_info(space_info
, 0, 0);
9378 list_del(&space_info
->list
);
9379 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++) {
9380 struct kobject
*kobj
;
9381 kobj
= space_info
->block_group_kobjs
[i
];
9382 space_info
->block_group_kobjs
[i
] = NULL
;
9388 kobject_del(&space_info
->kobj
);
9389 kobject_put(&space_info
->kobj
);
9394 static void __link_block_group(struct btrfs_space_info
*space_info
,
9395 struct btrfs_block_group_cache
*cache
)
9397 int index
= get_block_group_index(cache
);
9400 down_write(&space_info
->groups_sem
);
9401 if (list_empty(&space_info
->block_groups
[index
]))
9403 list_add_tail(&cache
->list
, &space_info
->block_groups
[index
]);
9404 up_write(&space_info
->groups_sem
);
9407 struct raid_kobject
*rkobj
;
9410 rkobj
= kzalloc(sizeof(*rkobj
), GFP_NOFS
);
9413 rkobj
->raid_type
= index
;
9414 kobject_init(&rkobj
->kobj
, &btrfs_raid_ktype
);
9415 ret
= kobject_add(&rkobj
->kobj
, &space_info
->kobj
,
9416 "%s", get_raid_name(index
));
9418 kobject_put(&rkobj
->kobj
);
9421 space_info
->block_group_kobjs
[index
] = &rkobj
->kobj
;
9426 pr_warn("BTRFS: failed to add kobject for block cache. ignoring.\n");
9429 static struct btrfs_block_group_cache
*
9430 btrfs_create_block_group_cache(struct btrfs_root
*root
, u64 start
, u64 size
)
9432 struct btrfs_block_group_cache
*cache
;
9434 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
9438 cache
->free_space_ctl
= kzalloc(sizeof(*cache
->free_space_ctl
),
9440 if (!cache
->free_space_ctl
) {
9445 cache
->key
.objectid
= start
;
9446 cache
->key
.offset
= size
;
9447 cache
->key
.type
= BTRFS_BLOCK_GROUP_ITEM_KEY
;
9449 cache
->sectorsize
= root
->sectorsize
;
9450 cache
->fs_info
= root
->fs_info
;
9451 cache
->full_stripe_len
= btrfs_full_stripe_len(root
,
9452 &root
->fs_info
->mapping_tree
,
9454 atomic_set(&cache
->count
, 1);
9455 spin_lock_init(&cache
->lock
);
9456 init_rwsem(&cache
->data_rwsem
);
9457 INIT_LIST_HEAD(&cache
->list
);
9458 INIT_LIST_HEAD(&cache
->cluster_list
);
9459 INIT_LIST_HEAD(&cache
->bg_list
);
9460 INIT_LIST_HEAD(&cache
->ro_list
);
9461 INIT_LIST_HEAD(&cache
->dirty_list
);
9462 INIT_LIST_HEAD(&cache
->io_list
);
9463 btrfs_init_free_space_ctl(cache
);
9464 atomic_set(&cache
->trimming
, 0);
9469 int btrfs_read_block_groups(struct btrfs_root
*root
)
9471 struct btrfs_path
*path
;
9473 struct btrfs_block_group_cache
*cache
;
9474 struct btrfs_fs_info
*info
= root
->fs_info
;
9475 struct btrfs_space_info
*space_info
;
9476 struct btrfs_key key
;
9477 struct btrfs_key found_key
;
9478 struct extent_buffer
*leaf
;
9482 root
= info
->extent_root
;
9485 key
.type
= BTRFS_BLOCK_GROUP_ITEM_KEY
;
9486 path
= btrfs_alloc_path();
9491 cache_gen
= btrfs_super_cache_generation(root
->fs_info
->super_copy
);
9492 if (btrfs_test_opt(root
, SPACE_CACHE
) &&
9493 btrfs_super_generation(root
->fs_info
->super_copy
) != cache_gen
)
9495 if (btrfs_test_opt(root
, CLEAR_CACHE
))
9499 ret
= find_first_block_group(root
, path
, &key
);
9505 leaf
= path
->nodes
[0];
9506 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
9508 cache
= btrfs_create_block_group_cache(root
, found_key
.objectid
,
9517 * When we mount with old space cache, we need to
9518 * set BTRFS_DC_CLEAR and set dirty flag.
9520 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
9521 * truncate the old free space cache inode and
9523 * b) Setting 'dirty flag' makes sure that we flush
9524 * the new space cache info onto disk.
9526 if (btrfs_test_opt(root
, SPACE_CACHE
))
9527 cache
->disk_cache_state
= BTRFS_DC_CLEAR
;
9530 read_extent_buffer(leaf
, &cache
->item
,
9531 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
9532 sizeof(cache
->item
));
9533 cache
->flags
= btrfs_block_group_flags(&cache
->item
);
9535 key
.objectid
= found_key
.objectid
+ found_key
.offset
;
9536 btrfs_release_path(path
);
9539 * We need to exclude the super stripes now so that the space
9540 * info has super bytes accounted for, otherwise we'll think
9541 * we have more space than we actually do.
9543 ret
= exclude_super_stripes(root
, cache
);
9546 * We may have excluded something, so call this just in
9549 free_excluded_extents(root
, cache
);
9550 btrfs_put_block_group(cache
);
9555 * check for two cases, either we are full, and therefore
9556 * don't need to bother with the caching work since we won't
9557 * find any space, or we are empty, and we can just add all
9558 * the space in and be done with it. This saves us _alot_ of
9559 * time, particularly in the full case.
9561 if (found_key
.offset
== btrfs_block_group_used(&cache
->item
)) {
9562 cache
->last_byte_to_unpin
= (u64
)-1;
9563 cache
->cached
= BTRFS_CACHE_FINISHED
;
9564 free_excluded_extents(root
, cache
);
9565 } else if (btrfs_block_group_used(&cache
->item
) == 0) {
9566 cache
->last_byte_to_unpin
= (u64
)-1;
9567 cache
->cached
= BTRFS_CACHE_FINISHED
;
9568 add_new_free_space(cache
, root
->fs_info
,
9570 found_key
.objectid
+
9572 free_excluded_extents(root
, cache
);
9575 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
9577 btrfs_remove_free_space_cache(cache
);
9578 btrfs_put_block_group(cache
);
9582 ret
= update_space_info(info
, cache
->flags
, found_key
.offset
,
9583 btrfs_block_group_used(&cache
->item
),
9586 btrfs_remove_free_space_cache(cache
);
9587 spin_lock(&info
->block_group_cache_lock
);
9588 rb_erase(&cache
->cache_node
,
9589 &info
->block_group_cache_tree
);
9590 RB_CLEAR_NODE(&cache
->cache_node
);
9591 spin_unlock(&info
->block_group_cache_lock
);
9592 btrfs_put_block_group(cache
);
9596 cache
->space_info
= space_info
;
9597 spin_lock(&cache
->space_info
->lock
);
9598 cache
->space_info
->bytes_readonly
+= cache
->bytes_super
;
9599 spin_unlock(&cache
->space_info
->lock
);
9601 __link_block_group(space_info
, cache
);
9603 set_avail_alloc_bits(root
->fs_info
, cache
->flags
);
9604 if (btrfs_chunk_readonly(root
, cache
->key
.objectid
)) {
9605 inc_block_group_ro(cache
, 1);
9606 } else if (btrfs_block_group_used(&cache
->item
) == 0) {
9607 spin_lock(&info
->unused_bgs_lock
);
9608 /* Should always be true but just in case. */
9609 if (list_empty(&cache
->bg_list
)) {
9610 btrfs_get_block_group(cache
);
9611 list_add_tail(&cache
->bg_list
,
9614 spin_unlock(&info
->unused_bgs_lock
);
9618 list_for_each_entry_rcu(space_info
, &root
->fs_info
->space_info
, list
) {
9619 if (!(get_alloc_profile(root
, space_info
->flags
) &
9620 (BTRFS_BLOCK_GROUP_RAID10
|
9621 BTRFS_BLOCK_GROUP_RAID1
|
9622 BTRFS_BLOCK_GROUP_RAID5
|
9623 BTRFS_BLOCK_GROUP_RAID6
|
9624 BTRFS_BLOCK_GROUP_DUP
)))
9627 * avoid allocating from un-mirrored block group if there are
9628 * mirrored block groups.
9630 list_for_each_entry(cache
,
9631 &space_info
->block_groups
[BTRFS_RAID_RAID0
],
9633 inc_block_group_ro(cache
, 1);
9634 list_for_each_entry(cache
,
9635 &space_info
->block_groups
[BTRFS_RAID_SINGLE
],
9637 inc_block_group_ro(cache
, 1);
9640 init_global_block_rsv(info
);
9643 btrfs_free_path(path
);
9647 void btrfs_create_pending_block_groups(struct btrfs_trans_handle
*trans
,
9648 struct btrfs_root
*root
)
9650 struct btrfs_block_group_cache
*block_group
, *tmp
;
9651 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
9652 struct btrfs_block_group_item item
;
9653 struct btrfs_key key
;
9655 bool can_flush_pending_bgs
= trans
->can_flush_pending_bgs
;
9657 trans
->can_flush_pending_bgs
= false;
9658 list_for_each_entry_safe(block_group
, tmp
, &trans
->new_bgs
, bg_list
) {
9662 spin_lock(&block_group
->lock
);
9663 memcpy(&item
, &block_group
->item
, sizeof(item
));
9664 memcpy(&key
, &block_group
->key
, sizeof(key
));
9665 spin_unlock(&block_group
->lock
);
9667 ret
= btrfs_insert_item(trans
, extent_root
, &key
, &item
,
9670 btrfs_abort_transaction(trans
, extent_root
, ret
);
9671 ret
= btrfs_finish_chunk_alloc(trans
, extent_root
,
9672 key
.objectid
, key
.offset
);
9674 btrfs_abort_transaction(trans
, extent_root
, ret
);
9676 list_del_init(&block_group
->bg_list
);
9678 trans
->can_flush_pending_bgs
= can_flush_pending_bgs
;
9681 int btrfs_make_block_group(struct btrfs_trans_handle
*trans
,
9682 struct btrfs_root
*root
, u64 bytes_used
,
9683 u64 type
, u64 chunk_objectid
, u64 chunk_offset
,
9687 struct btrfs_root
*extent_root
;
9688 struct btrfs_block_group_cache
*cache
;
9690 extent_root
= root
->fs_info
->extent_root
;
9692 btrfs_set_log_full_commit(root
->fs_info
, trans
);
9694 cache
= btrfs_create_block_group_cache(root
, chunk_offset
, size
);
9698 btrfs_set_block_group_used(&cache
->item
, bytes_used
);
9699 btrfs_set_block_group_chunk_objectid(&cache
->item
, chunk_objectid
);
9700 btrfs_set_block_group_flags(&cache
->item
, type
);
9702 cache
->flags
= type
;
9703 cache
->last_byte_to_unpin
= (u64
)-1;
9704 cache
->cached
= BTRFS_CACHE_FINISHED
;
9705 ret
= exclude_super_stripes(root
, cache
);
9708 * We may have excluded something, so call this just in
9711 free_excluded_extents(root
, cache
);
9712 btrfs_put_block_group(cache
);
9716 add_new_free_space(cache
, root
->fs_info
, chunk_offset
,
9717 chunk_offset
+ size
);
9719 free_excluded_extents(root
, cache
);
9721 #ifdef CONFIG_BTRFS_DEBUG
9722 if (btrfs_should_fragment_free_space(root
, cache
)) {
9723 u64 new_bytes_used
= size
- bytes_used
;
9725 bytes_used
+= new_bytes_used
>> 1;
9726 fragment_free_space(root
, cache
);
9730 * Call to ensure the corresponding space_info object is created and
9731 * assigned to our block group, but don't update its counters just yet.
9732 * We want our bg to be added to the rbtree with its ->space_info set.
9734 ret
= update_space_info(root
->fs_info
, cache
->flags
, 0, 0,
9735 &cache
->space_info
);
9737 btrfs_remove_free_space_cache(cache
);
9738 btrfs_put_block_group(cache
);
9742 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
9744 btrfs_remove_free_space_cache(cache
);
9745 btrfs_put_block_group(cache
);
9750 * Now that our block group has its ->space_info set and is inserted in
9751 * the rbtree, update the space info's counters.
9753 ret
= update_space_info(root
->fs_info
, cache
->flags
, size
, bytes_used
,
9754 &cache
->space_info
);
9756 btrfs_remove_free_space_cache(cache
);
9757 spin_lock(&root
->fs_info
->block_group_cache_lock
);
9758 rb_erase(&cache
->cache_node
,
9759 &root
->fs_info
->block_group_cache_tree
);
9760 RB_CLEAR_NODE(&cache
->cache_node
);
9761 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
9762 btrfs_put_block_group(cache
);
9765 update_global_block_rsv(root
->fs_info
);
9767 spin_lock(&cache
->space_info
->lock
);
9768 cache
->space_info
->bytes_readonly
+= cache
->bytes_super
;
9769 spin_unlock(&cache
->space_info
->lock
);
9771 __link_block_group(cache
->space_info
, cache
);
9773 list_add_tail(&cache
->bg_list
, &trans
->new_bgs
);
9775 set_avail_alloc_bits(extent_root
->fs_info
, type
);
9780 static void clear_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
9782 u64 extra_flags
= chunk_to_extended(flags
) &
9783 BTRFS_EXTENDED_PROFILE_MASK
;
9785 write_seqlock(&fs_info
->profiles_lock
);
9786 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
9787 fs_info
->avail_data_alloc_bits
&= ~extra_flags
;
9788 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
9789 fs_info
->avail_metadata_alloc_bits
&= ~extra_flags
;
9790 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
9791 fs_info
->avail_system_alloc_bits
&= ~extra_flags
;
9792 write_sequnlock(&fs_info
->profiles_lock
);
9795 int btrfs_remove_block_group(struct btrfs_trans_handle
*trans
,
9796 struct btrfs_root
*root
, u64 group_start
,
9797 struct extent_map
*em
)
9799 struct btrfs_path
*path
;
9800 struct btrfs_block_group_cache
*block_group
;
9801 struct btrfs_free_cluster
*cluster
;
9802 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
9803 struct btrfs_key key
;
9804 struct inode
*inode
;
9805 struct kobject
*kobj
= NULL
;
9809 struct btrfs_caching_control
*caching_ctl
= NULL
;
9812 root
= root
->fs_info
->extent_root
;
9814 block_group
= btrfs_lookup_block_group(root
->fs_info
, group_start
);
9815 BUG_ON(!block_group
);
9816 BUG_ON(!block_group
->ro
);
9819 * Free the reserved super bytes from this block group before
9822 free_excluded_extents(root
, block_group
);
9824 memcpy(&key
, &block_group
->key
, sizeof(key
));
9825 index
= get_block_group_index(block_group
);
9826 if (block_group
->flags
& (BTRFS_BLOCK_GROUP_DUP
|
9827 BTRFS_BLOCK_GROUP_RAID1
|
9828 BTRFS_BLOCK_GROUP_RAID10
))
9833 /* make sure this block group isn't part of an allocation cluster */
9834 cluster
= &root
->fs_info
->data_alloc_cluster
;
9835 spin_lock(&cluster
->refill_lock
);
9836 btrfs_return_cluster_to_free_space(block_group
, cluster
);
9837 spin_unlock(&cluster
->refill_lock
);
9840 * make sure this block group isn't part of a metadata
9841 * allocation cluster
9843 cluster
= &root
->fs_info
->meta_alloc_cluster
;
9844 spin_lock(&cluster
->refill_lock
);
9845 btrfs_return_cluster_to_free_space(block_group
, cluster
);
9846 spin_unlock(&cluster
->refill_lock
);
9848 path
= btrfs_alloc_path();
9855 * get the inode first so any iput calls done for the io_list
9856 * aren't the final iput (no unlinks allowed now)
9858 inode
= lookup_free_space_inode(tree_root
, block_group
, path
);
9860 mutex_lock(&trans
->transaction
->cache_write_mutex
);
9862 * make sure our free spache cache IO is done before remove the
9865 spin_lock(&trans
->transaction
->dirty_bgs_lock
);
9866 if (!list_empty(&block_group
->io_list
)) {
9867 list_del_init(&block_group
->io_list
);
9869 WARN_ON(!IS_ERR(inode
) && inode
!= block_group
->io_ctl
.inode
);
9871 spin_unlock(&trans
->transaction
->dirty_bgs_lock
);
9872 btrfs_wait_cache_io(root
, trans
, block_group
,
9873 &block_group
->io_ctl
, path
,
9874 block_group
->key
.objectid
);
9875 btrfs_put_block_group(block_group
);
9876 spin_lock(&trans
->transaction
->dirty_bgs_lock
);
9879 if (!list_empty(&block_group
->dirty_list
)) {
9880 list_del_init(&block_group
->dirty_list
);
9881 btrfs_put_block_group(block_group
);
9883 spin_unlock(&trans
->transaction
->dirty_bgs_lock
);
9884 mutex_unlock(&trans
->transaction
->cache_write_mutex
);
9886 if (!IS_ERR(inode
)) {
9887 ret
= btrfs_orphan_add(trans
, inode
);
9889 btrfs_add_delayed_iput(inode
);
9893 /* One for the block groups ref */
9894 spin_lock(&block_group
->lock
);
9895 if (block_group
->iref
) {
9896 block_group
->iref
= 0;
9897 block_group
->inode
= NULL
;
9898 spin_unlock(&block_group
->lock
);
9901 spin_unlock(&block_group
->lock
);
9903 /* One for our lookup ref */
9904 btrfs_add_delayed_iput(inode
);
9907 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
9908 key
.offset
= block_group
->key
.objectid
;
9911 ret
= btrfs_search_slot(trans
, tree_root
, &key
, path
, -1, 1);
9915 btrfs_release_path(path
);
9917 ret
= btrfs_del_item(trans
, tree_root
, path
);
9920 btrfs_release_path(path
);
9923 spin_lock(&root
->fs_info
->block_group_cache_lock
);
9924 rb_erase(&block_group
->cache_node
,
9925 &root
->fs_info
->block_group_cache_tree
);
9926 RB_CLEAR_NODE(&block_group
->cache_node
);
9928 if (root
->fs_info
->first_logical_byte
== block_group
->key
.objectid
)
9929 root
->fs_info
->first_logical_byte
= (u64
)-1;
9930 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
9932 down_write(&block_group
->space_info
->groups_sem
);
9934 * we must use list_del_init so people can check to see if they
9935 * are still on the list after taking the semaphore
9937 list_del_init(&block_group
->list
);
9938 if (list_empty(&block_group
->space_info
->block_groups
[index
])) {
9939 kobj
= block_group
->space_info
->block_group_kobjs
[index
];
9940 block_group
->space_info
->block_group_kobjs
[index
] = NULL
;
9941 clear_avail_alloc_bits(root
->fs_info
, block_group
->flags
);
9943 up_write(&block_group
->space_info
->groups_sem
);
9949 if (block_group
->has_caching_ctl
)
9950 caching_ctl
= get_caching_control(block_group
);
9951 if (block_group
->cached
== BTRFS_CACHE_STARTED
)
9952 wait_block_group_cache_done(block_group
);
9953 if (block_group
->has_caching_ctl
) {
9954 down_write(&root
->fs_info
->commit_root_sem
);
9956 struct btrfs_caching_control
*ctl
;
9958 list_for_each_entry(ctl
,
9959 &root
->fs_info
->caching_block_groups
, list
)
9960 if (ctl
->block_group
== block_group
) {
9962 atomic_inc(&caching_ctl
->count
);
9967 list_del_init(&caching_ctl
->list
);
9968 up_write(&root
->fs_info
->commit_root_sem
);
9970 /* Once for the caching bgs list and once for us. */
9971 put_caching_control(caching_ctl
);
9972 put_caching_control(caching_ctl
);
9976 spin_lock(&trans
->transaction
->dirty_bgs_lock
);
9977 if (!list_empty(&block_group
->dirty_list
)) {
9980 if (!list_empty(&block_group
->io_list
)) {
9983 spin_unlock(&trans
->transaction
->dirty_bgs_lock
);
9984 btrfs_remove_free_space_cache(block_group
);
9986 spin_lock(&block_group
->space_info
->lock
);
9987 list_del_init(&block_group
->ro_list
);
9989 if (btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
9990 WARN_ON(block_group
->space_info
->total_bytes
9991 < block_group
->key
.offset
);
9992 WARN_ON(block_group
->space_info
->bytes_readonly
9993 < block_group
->key
.offset
);
9994 WARN_ON(block_group
->space_info
->disk_total
9995 < block_group
->key
.offset
* factor
);
9997 block_group
->space_info
->total_bytes
-= block_group
->key
.offset
;
9998 block_group
->space_info
->bytes_readonly
-= block_group
->key
.offset
;
9999 block_group
->space_info
->disk_total
-= block_group
->key
.offset
* factor
;
10001 spin_unlock(&block_group
->space_info
->lock
);
10003 memcpy(&key
, &block_group
->key
, sizeof(key
));
10006 if (!list_empty(&em
->list
)) {
10007 /* We're in the transaction->pending_chunks list. */
10008 free_extent_map(em
);
10010 spin_lock(&block_group
->lock
);
10011 block_group
->removed
= 1;
10013 * At this point trimming can't start on this block group, because we
10014 * removed the block group from the tree fs_info->block_group_cache_tree
10015 * so no one can't find it anymore and even if someone already got this
10016 * block group before we removed it from the rbtree, they have already
10017 * incremented block_group->trimming - if they didn't, they won't find
10018 * any free space entries because we already removed them all when we
10019 * called btrfs_remove_free_space_cache().
10021 * And we must not remove the extent map from the fs_info->mapping_tree
10022 * to prevent the same logical address range and physical device space
10023 * ranges from being reused for a new block group. This is because our
10024 * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
10025 * completely transactionless, so while it is trimming a range the
10026 * currently running transaction might finish and a new one start,
10027 * allowing for new block groups to be created that can reuse the same
10028 * physical device locations unless we take this special care.
10030 * There may also be an implicit trim operation if the file system
10031 * is mounted with -odiscard. The same protections must remain
10032 * in place until the extents have been discarded completely when
10033 * the transaction commit has completed.
10035 remove_em
= (atomic_read(&block_group
->trimming
) == 0);
10037 * Make sure a trimmer task always sees the em in the pinned_chunks list
10038 * if it sees block_group->removed == 1 (needs to lock block_group->lock
10039 * before checking block_group->removed).
10043 * Our em might be in trans->transaction->pending_chunks which
10044 * is protected by fs_info->chunk_mutex ([lock|unlock]_chunks),
10045 * and so is the fs_info->pinned_chunks list.
10047 * So at this point we must be holding the chunk_mutex to avoid
10048 * any races with chunk allocation (more specifically at
10049 * volumes.c:contains_pending_extent()), to ensure it always
10050 * sees the em, either in the pending_chunks list or in the
10051 * pinned_chunks list.
10053 list_move_tail(&em
->list
, &root
->fs_info
->pinned_chunks
);
10055 spin_unlock(&block_group
->lock
);
10058 struct extent_map_tree
*em_tree
;
10060 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
10061 write_lock(&em_tree
->lock
);
10063 * The em might be in the pending_chunks list, so make sure the
10064 * chunk mutex is locked, since remove_extent_mapping() will
10065 * delete us from that list.
10067 remove_extent_mapping(em_tree
, em
);
10068 write_unlock(&em_tree
->lock
);
10069 /* once for the tree */
10070 free_extent_map(em
);
10073 unlock_chunks(root
);
10075 btrfs_put_block_group(block_group
);
10076 btrfs_put_block_group(block_group
);
10078 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
10084 ret
= btrfs_del_item(trans
, root
, path
);
10086 btrfs_free_path(path
);
10091 * Process the unused_bgs list and remove any that don't have any allocated
10092 * space inside of them.
10094 void btrfs_delete_unused_bgs(struct btrfs_fs_info
*fs_info
)
10096 struct btrfs_block_group_cache
*block_group
;
10097 struct btrfs_space_info
*space_info
;
10098 struct btrfs_root
*root
= fs_info
->extent_root
;
10099 struct btrfs_trans_handle
*trans
;
10102 if (!fs_info
->open
)
10105 spin_lock(&fs_info
->unused_bgs_lock
);
10106 while (!list_empty(&fs_info
->unused_bgs
)) {
10110 block_group
= list_first_entry(&fs_info
->unused_bgs
,
10111 struct btrfs_block_group_cache
,
10113 space_info
= block_group
->space_info
;
10114 list_del_init(&block_group
->bg_list
);
10115 if (ret
|| btrfs_mixed_space_info(space_info
)) {
10116 btrfs_put_block_group(block_group
);
10119 spin_unlock(&fs_info
->unused_bgs_lock
);
10121 mutex_lock(&root
->fs_info
->delete_unused_bgs_mutex
);
10123 /* Don't want to race with allocators so take the groups_sem */
10124 down_write(&space_info
->groups_sem
);
10125 spin_lock(&block_group
->lock
);
10126 if (block_group
->reserved
||
10127 btrfs_block_group_used(&block_group
->item
) ||
10130 * We want to bail if we made new allocations or have
10131 * outstanding allocations in this block group. We do
10132 * the ro check in case balance is currently acting on
10133 * this block group.
10135 spin_unlock(&block_group
->lock
);
10136 up_write(&space_info
->groups_sem
);
10139 spin_unlock(&block_group
->lock
);
10141 /* We don't want to force the issue, only flip if it's ok. */
10142 ret
= inc_block_group_ro(block_group
, 0);
10143 up_write(&space_info
->groups_sem
);
10150 * Want to do this before we do anything else so we can recover
10151 * properly if we fail to join the transaction.
10153 /* 1 for btrfs_orphan_reserve_metadata() */
10154 trans
= btrfs_start_transaction(root
, 1);
10155 if (IS_ERR(trans
)) {
10156 btrfs_dec_block_group_ro(root
, block_group
);
10157 ret
= PTR_ERR(trans
);
10162 * We could have pending pinned extents for this block group,
10163 * just delete them, we don't care about them anymore.
10165 start
= block_group
->key
.objectid
;
10166 end
= start
+ block_group
->key
.offset
- 1;
10168 * Hold the unused_bg_unpin_mutex lock to avoid racing with
10169 * btrfs_finish_extent_commit(). If we are at transaction N,
10170 * another task might be running finish_extent_commit() for the
10171 * previous transaction N - 1, and have seen a range belonging
10172 * to the block group in freed_extents[] before we were able to
10173 * clear the whole block group range from freed_extents[]. This
10174 * means that task can lookup for the block group after we
10175 * unpinned it from freed_extents[] and removed it, leading to
10176 * a BUG_ON() at btrfs_unpin_extent_range().
10178 mutex_lock(&fs_info
->unused_bg_unpin_mutex
);
10179 ret
= clear_extent_bits(&fs_info
->freed_extents
[0], start
, end
,
10180 EXTENT_DIRTY
, GFP_NOFS
);
10182 mutex_unlock(&fs_info
->unused_bg_unpin_mutex
);
10183 btrfs_dec_block_group_ro(root
, block_group
);
10186 ret
= clear_extent_bits(&fs_info
->freed_extents
[1], start
, end
,
10187 EXTENT_DIRTY
, GFP_NOFS
);
10189 mutex_unlock(&fs_info
->unused_bg_unpin_mutex
);
10190 btrfs_dec_block_group_ro(root
, block_group
);
10193 mutex_unlock(&fs_info
->unused_bg_unpin_mutex
);
10195 /* Reset pinned so btrfs_put_block_group doesn't complain */
10196 spin_lock(&space_info
->lock
);
10197 spin_lock(&block_group
->lock
);
10199 space_info
->bytes_pinned
-= block_group
->pinned
;
10200 space_info
->bytes_readonly
+= block_group
->pinned
;
10201 percpu_counter_add(&space_info
->total_bytes_pinned
,
10202 -block_group
->pinned
);
10203 block_group
->pinned
= 0;
10205 spin_unlock(&block_group
->lock
);
10206 spin_unlock(&space_info
->lock
);
10208 /* DISCARD can flip during remount */
10209 trimming
= btrfs_test_opt(root
, DISCARD
);
10211 /* Implicit trim during transaction commit. */
10213 btrfs_get_block_group_trimming(block_group
);
10216 * Btrfs_remove_chunk will abort the transaction if things go
10219 ret
= btrfs_remove_chunk(trans
, root
,
10220 block_group
->key
.objectid
);
10224 btrfs_put_block_group_trimming(block_group
);
10229 * If we're not mounted with -odiscard, we can just forget
10230 * about this block group. Otherwise we'll need to wait
10231 * until transaction commit to do the actual discard.
10234 WARN_ON(!list_empty(&block_group
->bg_list
));
10235 spin_lock(&trans
->transaction
->deleted_bgs_lock
);
10236 list_move(&block_group
->bg_list
,
10237 &trans
->transaction
->deleted_bgs
);
10238 spin_unlock(&trans
->transaction
->deleted_bgs_lock
);
10239 btrfs_get_block_group(block_group
);
10242 btrfs_end_transaction(trans
, root
);
10244 mutex_unlock(&root
->fs_info
->delete_unused_bgs_mutex
);
10245 btrfs_put_block_group(block_group
);
10246 spin_lock(&fs_info
->unused_bgs_lock
);
10248 spin_unlock(&fs_info
->unused_bgs_lock
);
10251 int btrfs_init_space_info(struct btrfs_fs_info
*fs_info
)
10253 struct btrfs_space_info
*space_info
;
10254 struct btrfs_super_block
*disk_super
;
10260 disk_super
= fs_info
->super_copy
;
10261 if (!btrfs_super_root(disk_super
))
10264 features
= btrfs_super_incompat_flags(disk_super
);
10265 if (features
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
10268 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
10269 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
10274 flags
= BTRFS_BLOCK_GROUP_METADATA
| BTRFS_BLOCK_GROUP_DATA
;
10275 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
10277 flags
= BTRFS_BLOCK_GROUP_METADATA
;
10278 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
10282 flags
= BTRFS_BLOCK_GROUP_DATA
;
10283 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
10289 int btrfs_error_unpin_extent_range(struct btrfs_root
*root
, u64 start
, u64 end
)
10291 return unpin_extent_range(root
, start
, end
, false);
10295 * It used to be that old block groups would be left around forever.
10296 * Iterating over them would be enough to trim unused space. Since we
10297 * now automatically remove them, we also need to iterate over unallocated
10300 * We don't want a transaction for this since the discard may take a
10301 * substantial amount of time. We don't require that a transaction be
10302 * running, but we do need to take a running transaction into account
10303 * to ensure that we're not discarding chunks that were released in
10304 * the current transaction.
10306 * Holding the chunks lock will prevent other threads from allocating
10307 * or releasing chunks, but it won't prevent a running transaction
10308 * from committing and releasing the memory that the pending chunks
10309 * list head uses. For that, we need to take a reference to the
10312 static int btrfs_trim_free_extents(struct btrfs_device
*device
,
10313 u64 minlen
, u64
*trimmed
)
10315 u64 start
= 0, len
= 0;
10320 /* Not writeable = nothing to do. */
10321 if (!device
->writeable
)
10324 /* No free space = nothing to do. */
10325 if (device
->total_bytes
<= device
->bytes_used
)
10331 struct btrfs_fs_info
*fs_info
= device
->dev_root
->fs_info
;
10332 struct btrfs_transaction
*trans
;
10335 ret
= mutex_lock_interruptible(&fs_info
->chunk_mutex
);
10339 down_read(&fs_info
->commit_root_sem
);
10341 spin_lock(&fs_info
->trans_lock
);
10342 trans
= fs_info
->running_transaction
;
10344 atomic_inc(&trans
->use_count
);
10345 spin_unlock(&fs_info
->trans_lock
);
10347 ret
= find_free_dev_extent_start(trans
, device
, minlen
, start
,
10350 btrfs_put_transaction(trans
);
10353 up_read(&fs_info
->commit_root_sem
);
10354 mutex_unlock(&fs_info
->chunk_mutex
);
10355 if (ret
== -ENOSPC
)
10360 ret
= btrfs_issue_discard(device
->bdev
, start
, len
, &bytes
);
10361 up_read(&fs_info
->commit_root_sem
);
10362 mutex_unlock(&fs_info
->chunk_mutex
);
10370 if (fatal_signal_pending(current
)) {
10371 ret
= -ERESTARTSYS
;
10381 int btrfs_trim_fs(struct btrfs_root
*root
, struct fstrim_range
*range
)
10383 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
10384 struct btrfs_block_group_cache
*cache
= NULL
;
10385 struct btrfs_device
*device
;
10386 struct list_head
*devices
;
10391 u64 total_bytes
= btrfs_super_total_bytes(fs_info
->super_copy
);
10395 * try to trim all FS space, our block group may start from non-zero.
10397 if (range
->len
== total_bytes
)
10398 cache
= btrfs_lookup_first_block_group(fs_info
, range
->start
);
10400 cache
= btrfs_lookup_block_group(fs_info
, range
->start
);
10403 if (cache
->key
.objectid
>= (range
->start
+ range
->len
)) {
10404 btrfs_put_block_group(cache
);
10408 start
= max(range
->start
, cache
->key
.objectid
);
10409 end
= min(range
->start
+ range
->len
,
10410 cache
->key
.objectid
+ cache
->key
.offset
);
10412 if (end
- start
>= range
->minlen
) {
10413 if (!block_group_cache_done(cache
)) {
10414 ret
= cache_block_group(cache
, 0);
10416 btrfs_put_block_group(cache
);
10419 ret
= wait_block_group_cache_done(cache
);
10421 btrfs_put_block_group(cache
);
10425 ret
= btrfs_trim_block_group(cache
,
10431 trimmed
+= group_trimmed
;
10433 btrfs_put_block_group(cache
);
10438 cache
= next_block_group(fs_info
->tree_root
, cache
);
10441 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
10442 devices
= &root
->fs_info
->fs_devices
->alloc_list
;
10443 list_for_each_entry(device
, devices
, dev_alloc_list
) {
10444 ret
= btrfs_trim_free_extents(device
, range
->minlen
,
10449 trimmed
+= group_trimmed
;
10451 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
10453 range
->len
= trimmed
;
10458 * btrfs_{start,end}_write_no_snapshoting() are similar to
10459 * mnt_{want,drop}_write(), they are used to prevent some tasks from writing
10460 * data into the page cache through nocow before the subvolume is snapshoted,
10461 * but flush the data into disk after the snapshot creation, or to prevent
10462 * operations while snapshoting is ongoing and that cause the snapshot to be
10463 * inconsistent (writes followed by expanding truncates for example).
10465 void btrfs_end_write_no_snapshoting(struct btrfs_root
*root
)
10467 percpu_counter_dec(&root
->subv_writers
->counter
);
10469 * Make sure counter is updated before we wake up waiters.
10472 if (waitqueue_active(&root
->subv_writers
->wait
))
10473 wake_up(&root
->subv_writers
->wait
);
10476 int btrfs_start_write_no_snapshoting(struct btrfs_root
*root
)
10478 if (atomic_read(&root
->will_be_snapshoted
))
10481 percpu_counter_inc(&root
->subv_writers
->counter
);
10483 * Make sure counter is updated before we check for snapshot creation.
10486 if (atomic_read(&root
->will_be_snapshoted
)) {
10487 btrfs_end_write_no_snapshoting(root
);