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
))
336 * this is only called by cache_block_group, since we could have freed extents
337 * we need to check the pinned_extents for any extents that can't be used yet
338 * since their free space will be released as soon as the transaction commits.
340 u64
add_new_free_space(struct btrfs_block_group_cache
*block_group
,
341 struct btrfs_fs_info
*info
, u64 start
, u64 end
)
343 u64 extent_start
, extent_end
, size
, total_added
= 0;
346 while (start
< end
) {
347 ret
= find_first_extent_bit(info
->pinned_extents
, start
,
348 &extent_start
, &extent_end
,
349 EXTENT_DIRTY
| EXTENT_UPTODATE
,
354 if (extent_start
<= start
) {
355 start
= extent_end
+ 1;
356 } else if (extent_start
> start
&& extent_start
< end
) {
357 size
= extent_start
- start
;
359 ret
= btrfs_add_free_space(block_group
, start
,
361 BUG_ON(ret
); /* -ENOMEM or logic error */
362 start
= extent_end
+ 1;
371 ret
= btrfs_add_free_space(block_group
, start
, size
);
372 BUG_ON(ret
); /* -ENOMEM or logic error */
378 static int load_extent_tree_free(struct btrfs_caching_control
*caching_ctl
)
380 struct btrfs_block_group_cache
*block_group
;
381 struct btrfs_fs_info
*fs_info
;
382 struct btrfs_root
*extent_root
;
383 struct btrfs_path
*path
;
384 struct extent_buffer
*leaf
;
385 struct btrfs_key key
;
391 block_group
= caching_ctl
->block_group
;
392 fs_info
= block_group
->fs_info
;
393 extent_root
= fs_info
->extent_root
;
395 path
= btrfs_alloc_path();
399 last
= max_t(u64
, block_group
->key
.objectid
, BTRFS_SUPER_INFO_OFFSET
);
402 * We don't want to deadlock with somebody trying to allocate a new
403 * extent for the extent root while also trying to search the extent
404 * root to add free space. So we skip locking and search the commit
405 * root, since its read-only
407 path
->skip_locking
= 1;
408 path
->search_commit_root
= 1;
413 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
416 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
420 leaf
= path
->nodes
[0];
421 nritems
= btrfs_header_nritems(leaf
);
424 if (btrfs_fs_closing(fs_info
) > 1) {
429 if (path
->slots
[0] < nritems
) {
430 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
432 ret
= find_next_key(path
, 0, &key
);
436 if (need_resched() ||
437 rwsem_is_contended(&fs_info
->commit_root_sem
)) {
438 caching_ctl
->progress
= last
;
439 btrfs_release_path(path
);
440 up_read(&fs_info
->commit_root_sem
);
441 mutex_unlock(&caching_ctl
->mutex
);
443 mutex_lock(&caching_ctl
->mutex
);
444 down_read(&fs_info
->commit_root_sem
);
448 ret
= btrfs_next_leaf(extent_root
, path
);
453 leaf
= path
->nodes
[0];
454 nritems
= btrfs_header_nritems(leaf
);
458 if (key
.objectid
< last
) {
461 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
463 caching_ctl
->progress
= last
;
464 btrfs_release_path(path
);
468 if (key
.objectid
< block_group
->key
.objectid
) {
473 if (key
.objectid
>= block_group
->key
.objectid
+
474 block_group
->key
.offset
)
477 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
||
478 key
.type
== BTRFS_METADATA_ITEM_KEY
) {
479 total_found
+= add_new_free_space(block_group
,
482 if (key
.type
== BTRFS_METADATA_ITEM_KEY
)
483 last
= key
.objectid
+
484 fs_info
->tree_root
->nodesize
;
486 last
= key
.objectid
+ key
.offset
;
488 if (total_found
> CACHING_CTL_WAKE_UP
) {
490 wake_up(&caching_ctl
->wait
);
497 total_found
+= add_new_free_space(block_group
, fs_info
, last
,
498 block_group
->key
.objectid
+
499 block_group
->key
.offset
);
500 caching_ctl
->progress
= (u64
)-1;
503 btrfs_free_path(path
);
507 static noinline
void caching_thread(struct btrfs_work
*work
)
509 struct btrfs_block_group_cache
*block_group
;
510 struct btrfs_fs_info
*fs_info
;
511 struct btrfs_caching_control
*caching_ctl
;
514 caching_ctl
= container_of(work
, struct btrfs_caching_control
, work
);
515 block_group
= caching_ctl
->block_group
;
516 fs_info
= block_group
->fs_info
;
518 mutex_lock(&caching_ctl
->mutex
);
519 down_read(&fs_info
->commit_root_sem
);
521 ret
= load_extent_tree_free(caching_ctl
);
523 spin_lock(&block_group
->lock
);
524 block_group
->caching_ctl
= NULL
;
525 block_group
->cached
= ret
? BTRFS_CACHE_ERROR
: BTRFS_CACHE_FINISHED
;
526 spin_unlock(&block_group
->lock
);
528 up_read(&fs_info
->commit_root_sem
);
529 free_excluded_extents(fs_info
->extent_root
, block_group
);
530 mutex_unlock(&caching_ctl
->mutex
);
532 wake_up(&caching_ctl
->wait
);
534 put_caching_control(caching_ctl
);
535 btrfs_put_block_group(block_group
);
538 static int cache_block_group(struct btrfs_block_group_cache
*cache
,
542 struct btrfs_fs_info
*fs_info
= cache
->fs_info
;
543 struct btrfs_caching_control
*caching_ctl
;
546 caching_ctl
= kzalloc(sizeof(*caching_ctl
), GFP_NOFS
);
550 INIT_LIST_HEAD(&caching_ctl
->list
);
551 mutex_init(&caching_ctl
->mutex
);
552 init_waitqueue_head(&caching_ctl
->wait
);
553 caching_ctl
->block_group
= cache
;
554 caching_ctl
->progress
= cache
->key
.objectid
;
555 atomic_set(&caching_ctl
->count
, 1);
556 btrfs_init_work(&caching_ctl
->work
, btrfs_cache_helper
,
557 caching_thread
, NULL
, NULL
);
559 spin_lock(&cache
->lock
);
561 * This should be a rare occasion, but this could happen I think in the
562 * case where one thread starts to load the space cache info, and then
563 * some other thread starts a transaction commit which tries to do an
564 * allocation while the other thread is still loading the space cache
565 * info. The previous loop should have kept us from choosing this block
566 * group, but if we've moved to the state where we will wait on caching
567 * block groups we need to first check if we're doing a fast load here,
568 * so we can wait for it to finish, otherwise we could end up allocating
569 * from a block group who's cache gets evicted for one reason or
572 while (cache
->cached
== BTRFS_CACHE_FAST
) {
573 struct btrfs_caching_control
*ctl
;
575 ctl
= cache
->caching_ctl
;
576 atomic_inc(&ctl
->count
);
577 prepare_to_wait(&ctl
->wait
, &wait
, TASK_UNINTERRUPTIBLE
);
578 spin_unlock(&cache
->lock
);
582 finish_wait(&ctl
->wait
, &wait
);
583 put_caching_control(ctl
);
584 spin_lock(&cache
->lock
);
587 if (cache
->cached
!= BTRFS_CACHE_NO
) {
588 spin_unlock(&cache
->lock
);
592 WARN_ON(cache
->caching_ctl
);
593 cache
->caching_ctl
= caching_ctl
;
594 cache
->cached
= BTRFS_CACHE_FAST
;
595 spin_unlock(&cache
->lock
);
597 if (fs_info
->mount_opt
& BTRFS_MOUNT_SPACE_CACHE
) {
598 mutex_lock(&caching_ctl
->mutex
);
599 ret
= load_free_space_cache(fs_info
, cache
);
601 spin_lock(&cache
->lock
);
603 cache
->caching_ctl
= NULL
;
604 cache
->cached
= BTRFS_CACHE_FINISHED
;
605 cache
->last_byte_to_unpin
= (u64
)-1;
606 caching_ctl
->progress
= (u64
)-1;
608 if (load_cache_only
) {
609 cache
->caching_ctl
= NULL
;
610 cache
->cached
= BTRFS_CACHE_NO
;
612 cache
->cached
= BTRFS_CACHE_STARTED
;
613 cache
->has_caching_ctl
= 1;
616 spin_unlock(&cache
->lock
);
617 mutex_unlock(&caching_ctl
->mutex
);
619 wake_up(&caching_ctl
->wait
);
621 put_caching_control(caching_ctl
);
622 free_excluded_extents(fs_info
->extent_root
, cache
);
627 * We are not going to do the fast caching, set cached to the
628 * appropriate value and wakeup any waiters.
630 spin_lock(&cache
->lock
);
631 if (load_cache_only
) {
632 cache
->caching_ctl
= NULL
;
633 cache
->cached
= BTRFS_CACHE_NO
;
635 cache
->cached
= BTRFS_CACHE_STARTED
;
636 cache
->has_caching_ctl
= 1;
638 spin_unlock(&cache
->lock
);
639 wake_up(&caching_ctl
->wait
);
642 if (load_cache_only
) {
643 put_caching_control(caching_ctl
);
647 down_write(&fs_info
->commit_root_sem
);
648 atomic_inc(&caching_ctl
->count
);
649 list_add_tail(&caching_ctl
->list
, &fs_info
->caching_block_groups
);
650 up_write(&fs_info
->commit_root_sem
);
652 btrfs_get_block_group(cache
);
654 btrfs_queue_work(fs_info
->caching_workers
, &caching_ctl
->work
);
660 * return the block group that starts at or after bytenr
662 static struct btrfs_block_group_cache
*
663 btrfs_lookup_first_block_group(struct btrfs_fs_info
*info
, u64 bytenr
)
665 struct btrfs_block_group_cache
*cache
;
667 cache
= block_group_cache_tree_search(info
, bytenr
, 0);
673 * return the block group that contains the given bytenr
675 struct btrfs_block_group_cache
*btrfs_lookup_block_group(
676 struct btrfs_fs_info
*info
,
679 struct btrfs_block_group_cache
*cache
;
681 cache
= block_group_cache_tree_search(info
, bytenr
, 1);
686 static struct btrfs_space_info
*__find_space_info(struct btrfs_fs_info
*info
,
689 struct list_head
*head
= &info
->space_info
;
690 struct btrfs_space_info
*found
;
692 flags
&= BTRFS_BLOCK_GROUP_TYPE_MASK
;
695 list_for_each_entry_rcu(found
, head
, list
) {
696 if (found
->flags
& flags
) {
706 * after adding space to the filesystem, we need to clear the full flags
707 * on all the space infos.
709 void btrfs_clear_space_info_full(struct btrfs_fs_info
*info
)
711 struct list_head
*head
= &info
->space_info
;
712 struct btrfs_space_info
*found
;
715 list_for_each_entry_rcu(found
, head
, list
)
720 /* simple helper to search for an existing data extent at a given offset */
721 int btrfs_lookup_data_extent(struct btrfs_root
*root
, u64 start
, u64 len
)
724 struct btrfs_key key
;
725 struct btrfs_path
*path
;
727 path
= btrfs_alloc_path();
731 key
.objectid
= start
;
733 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
734 ret
= btrfs_search_slot(NULL
, root
->fs_info
->extent_root
, &key
, path
,
736 btrfs_free_path(path
);
741 * helper function to lookup reference count and flags of a tree block.
743 * the head node for delayed ref is used to store the sum of all the
744 * reference count modifications queued up in the rbtree. the head
745 * node may also store the extent flags to set. This way you can check
746 * to see what the reference count and extent flags would be if all of
747 * the delayed refs are not processed.
749 int btrfs_lookup_extent_info(struct btrfs_trans_handle
*trans
,
750 struct btrfs_root
*root
, u64 bytenr
,
751 u64 offset
, int metadata
, u64
*refs
, u64
*flags
)
753 struct btrfs_delayed_ref_head
*head
;
754 struct btrfs_delayed_ref_root
*delayed_refs
;
755 struct btrfs_path
*path
;
756 struct btrfs_extent_item
*ei
;
757 struct extent_buffer
*leaf
;
758 struct btrfs_key key
;
765 * If we don't have skinny metadata, don't bother doing anything
768 if (metadata
&& !btrfs_fs_incompat(root
->fs_info
, SKINNY_METADATA
)) {
769 offset
= root
->nodesize
;
773 path
= btrfs_alloc_path();
778 path
->skip_locking
= 1;
779 path
->search_commit_root
= 1;
783 key
.objectid
= bytenr
;
786 key
.type
= BTRFS_METADATA_ITEM_KEY
;
788 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
790 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
,
795 if (ret
> 0 && metadata
&& key
.type
== BTRFS_METADATA_ITEM_KEY
) {
796 if (path
->slots
[0]) {
798 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
800 if (key
.objectid
== bytenr
&&
801 key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
802 key
.offset
== root
->nodesize
)
808 leaf
= path
->nodes
[0];
809 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
810 if (item_size
>= sizeof(*ei
)) {
811 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
812 struct btrfs_extent_item
);
813 num_refs
= btrfs_extent_refs(leaf
, ei
);
814 extent_flags
= btrfs_extent_flags(leaf
, ei
);
816 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
817 struct btrfs_extent_item_v0
*ei0
;
818 BUG_ON(item_size
!= sizeof(*ei0
));
819 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
820 struct btrfs_extent_item_v0
);
821 num_refs
= btrfs_extent_refs_v0(leaf
, ei0
);
822 /* FIXME: this isn't correct for data */
823 extent_flags
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
828 BUG_ON(num_refs
== 0);
838 delayed_refs
= &trans
->transaction
->delayed_refs
;
839 spin_lock(&delayed_refs
->lock
);
840 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
842 if (!mutex_trylock(&head
->mutex
)) {
843 atomic_inc(&head
->node
.refs
);
844 spin_unlock(&delayed_refs
->lock
);
846 btrfs_release_path(path
);
849 * Mutex was contended, block until it's released and try
852 mutex_lock(&head
->mutex
);
853 mutex_unlock(&head
->mutex
);
854 btrfs_put_delayed_ref(&head
->node
);
857 spin_lock(&head
->lock
);
858 if (head
->extent_op
&& head
->extent_op
->update_flags
)
859 extent_flags
|= head
->extent_op
->flags_to_set
;
861 BUG_ON(num_refs
== 0);
863 num_refs
+= head
->node
.ref_mod
;
864 spin_unlock(&head
->lock
);
865 mutex_unlock(&head
->mutex
);
867 spin_unlock(&delayed_refs
->lock
);
869 WARN_ON(num_refs
== 0);
873 *flags
= extent_flags
;
875 btrfs_free_path(path
);
880 * Back reference rules. Back refs have three main goals:
882 * 1) differentiate between all holders of references to an extent so that
883 * when a reference is dropped we can make sure it was a valid reference
884 * before freeing the extent.
886 * 2) Provide enough information to quickly find the holders of an extent
887 * if we notice a given block is corrupted or bad.
889 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
890 * maintenance. This is actually the same as #2, but with a slightly
891 * different use case.
893 * There are two kinds of back refs. The implicit back refs is optimized
894 * for pointers in non-shared tree blocks. For a given pointer in a block,
895 * back refs of this kind provide information about the block's owner tree
896 * and the pointer's key. These information allow us to find the block by
897 * b-tree searching. The full back refs is for pointers in tree blocks not
898 * referenced by their owner trees. The location of tree block is recorded
899 * in the back refs. Actually the full back refs is generic, and can be
900 * used in all cases the implicit back refs is used. The major shortcoming
901 * of the full back refs is its overhead. Every time a tree block gets
902 * COWed, we have to update back refs entry for all pointers in it.
904 * For a newly allocated tree block, we use implicit back refs for
905 * pointers in it. This means most tree related operations only involve
906 * implicit back refs. For a tree block created in old transaction, the
907 * only way to drop a reference to it is COW it. So we can detect the
908 * event that tree block loses its owner tree's reference and do the
909 * back refs conversion.
911 * When a tree block is COW'd through a tree, there are four cases:
913 * The reference count of the block is one and the tree is the block's
914 * owner tree. Nothing to do in this case.
916 * The reference count of the block is one and the tree is not the
917 * block's owner tree. In this case, full back refs is used for pointers
918 * in the block. Remove these full back refs, add implicit back refs for
919 * every pointers in the new block.
921 * The reference count of the block is greater than one and the tree is
922 * the block's owner tree. In this case, implicit back refs is used for
923 * pointers in the block. Add full back refs for every pointers in the
924 * block, increase lower level extents' reference counts. The original
925 * implicit back refs are entailed to the new block.
927 * The reference count of the block is greater than one and the tree is
928 * not the block's owner tree. Add implicit back refs for every pointer in
929 * the new block, increase lower level extents' reference count.
931 * Back Reference Key composing:
933 * The key objectid corresponds to the first byte in the extent,
934 * The key type is used to differentiate between types of back refs.
935 * There are different meanings of the key offset for different types
938 * File extents can be referenced by:
940 * - multiple snapshots, subvolumes, or different generations in one subvol
941 * - different files inside a single subvolume
942 * - different offsets inside a file (bookend extents in file.c)
944 * The extent ref structure for the implicit back refs has fields for:
946 * - Objectid of the subvolume root
947 * - objectid of the file holding the reference
948 * - original offset in the file
949 * - how many bookend extents
951 * The key offset for the implicit back refs is hash of the first
954 * The extent ref structure for the full back refs has field for:
956 * - number of pointers in the tree leaf
958 * The key offset for the implicit back refs is the first byte of
961 * When a file extent is allocated, The implicit back refs is used.
962 * the fields are filled in:
964 * (root_key.objectid, inode objectid, offset in file, 1)
966 * When a file extent is removed file truncation, we find the
967 * corresponding implicit back refs and check the following fields:
969 * (btrfs_header_owner(leaf), inode objectid, offset in file)
971 * Btree extents can be referenced by:
973 * - Different subvolumes
975 * Both the implicit back refs and the full back refs for tree blocks
976 * only consist of key. The key offset for the implicit back refs is
977 * objectid of block's owner tree. The key offset for the full back refs
978 * is the first byte of parent block.
980 * When implicit back refs is used, information about the lowest key and
981 * level of the tree block are required. These information are stored in
982 * tree block info structure.
985 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
986 static int convert_extent_item_v0(struct btrfs_trans_handle
*trans
,
987 struct btrfs_root
*root
,
988 struct btrfs_path
*path
,
989 u64 owner
, u32 extra_size
)
991 struct btrfs_extent_item
*item
;
992 struct btrfs_extent_item_v0
*ei0
;
993 struct btrfs_extent_ref_v0
*ref0
;
994 struct btrfs_tree_block_info
*bi
;
995 struct extent_buffer
*leaf
;
996 struct btrfs_key key
;
997 struct btrfs_key found_key
;
998 u32 new_size
= sizeof(*item
);
1002 leaf
= path
->nodes
[0];
1003 BUG_ON(btrfs_item_size_nr(leaf
, path
->slots
[0]) != sizeof(*ei0
));
1005 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1006 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1007 struct btrfs_extent_item_v0
);
1008 refs
= btrfs_extent_refs_v0(leaf
, ei0
);
1010 if (owner
== (u64
)-1) {
1012 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1013 ret
= btrfs_next_leaf(root
, path
);
1016 BUG_ON(ret
> 0); /* Corruption */
1017 leaf
= path
->nodes
[0];
1019 btrfs_item_key_to_cpu(leaf
, &found_key
,
1021 BUG_ON(key
.objectid
!= found_key
.objectid
);
1022 if (found_key
.type
!= BTRFS_EXTENT_REF_V0_KEY
) {
1026 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1027 struct btrfs_extent_ref_v0
);
1028 owner
= btrfs_ref_objectid_v0(leaf
, ref0
);
1032 btrfs_release_path(path
);
1034 if (owner
< BTRFS_FIRST_FREE_OBJECTID
)
1035 new_size
+= sizeof(*bi
);
1037 new_size
-= sizeof(*ei0
);
1038 ret
= btrfs_search_slot(trans
, root
, &key
, path
,
1039 new_size
+ extra_size
, 1);
1042 BUG_ON(ret
); /* Corruption */
1044 btrfs_extend_item(root
, path
, new_size
);
1046 leaf
= path
->nodes
[0];
1047 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1048 btrfs_set_extent_refs(leaf
, item
, refs
);
1049 /* FIXME: get real generation */
1050 btrfs_set_extent_generation(leaf
, item
, 0);
1051 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1052 btrfs_set_extent_flags(leaf
, item
,
1053 BTRFS_EXTENT_FLAG_TREE_BLOCK
|
1054 BTRFS_BLOCK_FLAG_FULL_BACKREF
);
1055 bi
= (struct btrfs_tree_block_info
*)(item
+ 1);
1056 /* FIXME: get first key of the block */
1057 memset_extent_buffer(leaf
, 0, (unsigned long)bi
, sizeof(*bi
));
1058 btrfs_set_tree_block_level(leaf
, bi
, (int)owner
);
1060 btrfs_set_extent_flags(leaf
, item
, BTRFS_EXTENT_FLAG_DATA
);
1062 btrfs_mark_buffer_dirty(leaf
);
1067 static u64
hash_extent_data_ref(u64 root_objectid
, u64 owner
, u64 offset
)
1069 u32 high_crc
= ~(u32
)0;
1070 u32 low_crc
= ~(u32
)0;
1073 lenum
= cpu_to_le64(root_objectid
);
1074 high_crc
= btrfs_crc32c(high_crc
, &lenum
, sizeof(lenum
));
1075 lenum
= cpu_to_le64(owner
);
1076 low_crc
= btrfs_crc32c(low_crc
, &lenum
, sizeof(lenum
));
1077 lenum
= cpu_to_le64(offset
);
1078 low_crc
= btrfs_crc32c(low_crc
, &lenum
, sizeof(lenum
));
1080 return ((u64
)high_crc
<< 31) ^ (u64
)low_crc
;
1083 static u64
hash_extent_data_ref_item(struct extent_buffer
*leaf
,
1084 struct btrfs_extent_data_ref
*ref
)
1086 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf
, ref
),
1087 btrfs_extent_data_ref_objectid(leaf
, ref
),
1088 btrfs_extent_data_ref_offset(leaf
, ref
));
1091 static int match_extent_data_ref(struct extent_buffer
*leaf
,
1092 struct btrfs_extent_data_ref
*ref
,
1093 u64 root_objectid
, u64 owner
, u64 offset
)
1095 if (btrfs_extent_data_ref_root(leaf
, ref
) != root_objectid
||
1096 btrfs_extent_data_ref_objectid(leaf
, ref
) != owner
||
1097 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
1102 static noinline
int lookup_extent_data_ref(struct btrfs_trans_handle
*trans
,
1103 struct btrfs_root
*root
,
1104 struct btrfs_path
*path
,
1105 u64 bytenr
, u64 parent
,
1107 u64 owner
, u64 offset
)
1109 struct btrfs_key key
;
1110 struct btrfs_extent_data_ref
*ref
;
1111 struct extent_buffer
*leaf
;
1117 key
.objectid
= bytenr
;
1119 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
1120 key
.offset
= parent
;
1122 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
1123 key
.offset
= hash_extent_data_ref(root_objectid
,
1128 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1137 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1138 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
1139 btrfs_release_path(path
);
1140 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1151 leaf
= path
->nodes
[0];
1152 nritems
= btrfs_header_nritems(leaf
);
1154 if (path
->slots
[0] >= nritems
) {
1155 ret
= btrfs_next_leaf(root
, path
);
1161 leaf
= path
->nodes
[0];
1162 nritems
= btrfs_header_nritems(leaf
);
1166 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1167 if (key
.objectid
!= bytenr
||
1168 key
.type
!= BTRFS_EXTENT_DATA_REF_KEY
)
1171 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1172 struct btrfs_extent_data_ref
);
1174 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
1177 btrfs_release_path(path
);
1189 static noinline
int insert_extent_data_ref(struct btrfs_trans_handle
*trans
,
1190 struct btrfs_root
*root
,
1191 struct btrfs_path
*path
,
1192 u64 bytenr
, u64 parent
,
1193 u64 root_objectid
, u64 owner
,
1194 u64 offset
, int refs_to_add
)
1196 struct btrfs_key key
;
1197 struct extent_buffer
*leaf
;
1202 key
.objectid
= bytenr
;
1204 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
1205 key
.offset
= parent
;
1206 size
= sizeof(struct btrfs_shared_data_ref
);
1208 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
1209 key
.offset
= hash_extent_data_ref(root_objectid
,
1211 size
= sizeof(struct btrfs_extent_data_ref
);
1214 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, size
);
1215 if (ret
&& ret
!= -EEXIST
)
1218 leaf
= path
->nodes
[0];
1220 struct btrfs_shared_data_ref
*ref
;
1221 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1222 struct btrfs_shared_data_ref
);
1224 btrfs_set_shared_data_ref_count(leaf
, ref
, refs_to_add
);
1226 num_refs
= btrfs_shared_data_ref_count(leaf
, ref
);
1227 num_refs
+= refs_to_add
;
1228 btrfs_set_shared_data_ref_count(leaf
, ref
, num_refs
);
1231 struct btrfs_extent_data_ref
*ref
;
1232 while (ret
== -EEXIST
) {
1233 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1234 struct btrfs_extent_data_ref
);
1235 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
1238 btrfs_release_path(path
);
1240 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1242 if (ret
&& ret
!= -EEXIST
)
1245 leaf
= path
->nodes
[0];
1247 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1248 struct btrfs_extent_data_ref
);
1250 btrfs_set_extent_data_ref_root(leaf
, ref
,
1252 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
1253 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
1254 btrfs_set_extent_data_ref_count(leaf
, ref
, refs_to_add
);
1256 num_refs
= btrfs_extent_data_ref_count(leaf
, ref
);
1257 num_refs
+= refs_to_add
;
1258 btrfs_set_extent_data_ref_count(leaf
, ref
, num_refs
);
1261 btrfs_mark_buffer_dirty(leaf
);
1264 btrfs_release_path(path
);
1268 static noinline
int remove_extent_data_ref(struct btrfs_trans_handle
*trans
,
1269 struct btrfs_root
*root
,
1270 struct btrfs_path
*path
,
1271 int refs_to_drop
, int *last_ref
)
1273 struct btrfs_key key
;
1274 struct btrfs_extent_data_ref
*ref1
= NULL
;
1275 struct btrfs_shared_data_ref
*ref2
= NULL
;
1276 struct extent_buffer
*leaf
;
1280 leaf
= path
->nodes
[0];
1281 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1283 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1284 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
1285 struct btrfs_extent_data_ref
);
1286 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1287 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
1288 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
1289 struct btrfs_shared_data_ref
);
1290 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1291 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1292 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
1293 struct btrfs_extent_ref_v0
*ref0
;
1294 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1295 struct btrfs_extent_ref_v0
);
1296 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
1302 BUG_ON(num_refs
< refs_to_drop
);
1303 num_refs
-= refs_to_drop
;
1305 if (num_refs
== 0) {
1306 ret
= btrfs_del_item(trans
, root
, path
);
1309 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
)
1310 btrfs_set_extent_data_ref_count(leaf
, ref1
, num_refs
);
1311 else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
)
1312 btrfs_set_shared_data_ref_count(leaf
, ref2
, num_refs
);
1313 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1315 struct btrfs_extent_ref_v0
*ref0
;
1316 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1317 struct btrfs_extent_ref_v0
);
1318 btrfs_set_ref_count_v0(leaf
, ref0
, num_refs
);
1321 btrfs_mark_buffer_dirty(leaf
);
1326 static noinline u32
extent_data_ref_count(struct btrfs_path
*path
,
1327 struct btrfs_extent_inline_ref
*iref
)
1329 struct btrfs_key key
;
1330 struct extent_buffer
*leaf
;
1331 struct btrfs_extent_data_ref
*ref1
;
1332 struct btrfs_shared_data_ref
*ref2
;
1335 leaf
= path
->nodes
[0];
1336 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1338 if (btrfs_extent_inline_ref_type(leaf
, iref
) ==
1339 BTRFS_EXTENT_DATA_REF_KEY
) {
1340 ref1
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1341 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1343 ref2
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1344 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1346 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1347 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
1348 struct btrfs_extent_data_ref
);
1349 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1350 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
1351 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
1352 struct btrfs_shared_data_ref
);
1353 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1354 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1355 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
1356 struct btrfs_extent_ref_v0
*ref0
;
1357 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1358 struct btrfs_extent_ref_v0
);
1359 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
1367 static noinline
int lookup_tree_block_ref(struct btrfs_trans_handle
*trans
,
1368 struct btrfs_root
*root
,
1369 struct btrfs_path
*path
,
1370 u64 bytenr
, u64 parent
,
1373 struct btrfs_key key
;
1376 key
.objectid
= bytenr
;
1378 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1379 key
.offset
= parent
;
1381 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
1382 key
.offset
= root_objectid
;
1385 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1388 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1389 if (ret
== -ENOENT
&& parent
) {
1390 btrfs_release_path(path
);
1391 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
1392 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1400 static noinline
int insert_tree_block_ref(struct btrfs_trans_handle
*trans
,
1401 struct btrfs_root
*root
,
1402 struct btrfs_path
*path
,
1403 u64 bytenr
, u64 parent
,
1406 struct btrfs_key key
;
1409 key
.objectid
= bytenr
;
1411 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1412 key
.offset
= parent
;
1414 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
1415 key
.offset
= root_objectid
;
1418 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, 0);
1419 btrfs_release_path(path
);
1423 static inline int extent_ref_type(u64 parent
, u64 owner
)
1426 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1428 type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1430 type
= BTRFS_TREE_BLOCK_REF_KEY
;
1433 type
= BTRFS_SHARED_DATA_REF_KEY
;
1435 type
= BTRFS_EXTENT_DATA_REF_KEY
;
1440 static int find_next_key(struct btrfs_path
*path
, int level
,
1441 struct btrfs_key
*key
)
1444 for (; level
< BTRFS_MAX_LEVEL
; level
++) {
1445 if (!path
->nodes
[level
])
1447 if (path
->slots
[level
] + 1 >=
1448 btrfs_header_nritems(path
->nodes
[level
]))
1451 btrfs_item_key_to_cpu(path
->nodes
[level
], key
,
1452 path
->slots
[level
] + 1);
1454 btrfs_node_key_to_cpu(path
->nodes
[level
], key
,
1455 path
->slots
[level
] + 1);
1462 * look for inline back ref. if back ref is found, *ref_ret is set
1463 * to the address of inline back ref, and 0 is returned.
1465 * if back ref isn't found, *ref_ret is set to the address where it
1466 * should be inserted, and -ENOENT is returned.
1468 * if insert is true and there are too many inline back refs, the path
1469 * points to the extent item, and -EAGAIN is returned.
1471 * NOTE: inline back refs are ordered in the same way that back ref
1472 * items in the tree are ordered.
1474 static noinline_for_stack
1475 int lookup_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1476 struct btrfs_root
*root
,
1477 struct btrfs_path
*path
,
1478 struct btrfs_extent_inline_ref
**ref_ret
,
1479 u64 bytenr
, u64 num_bytes
,
1480 u64 parent
, u64 root_objectid
,
1481 u64 owner
, u64 offset
, int insert
)
1483 struct btrfs_key key
;
1484 struct extent_buffer
*leaf
;
1485 struct btrfs_extent_item
*ei
;
1486 struct btrfs_extent_inline_ref
*iref
;
1496 bool skinny_metadata
= btrfs_fs_incompat(root
->fs_info
,
1499 key
.objectid
= bytenr
;
1500 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1501 key
.offset
= num_bytes
;
1503 want
= extent_ref_type(parent
, owner
);
1505 extra_size
= btrfs_extent_inline_ref_size(want
);
1506 path
->keep_locks
= 1;
1511 * Owner is our parent level, so we can just add one to get the level
1512 * for the block we are interested in.
1514 if (skinny_metadata
&& owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1515 key
.type
= BTRFS_METADATA_ITEM_KEY
;
1520 ret
= btrfs_search_slot(trans
, root
, &key
, path
, extra_size
, 1);
1527 * We may be a newly converted file system which still has the old fat
1528 * extent entries for metadata, so try and see if we have one of those.
1530 if (ret
> 0 && skinny_metadata
) {
1531 skinny_metadata
= false;
1532 if (path
->slots
[0]) {
1534 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
1536 if (key
.objectid
== bytenr
&&
1537 key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
1538 key
.offset
== num_bytes
)
1542 key
.objectid
= bytenr
;
1543 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1544 key
.offset
= num_bytes
;
1545 btrfs_release_path(path
);
1550 if (ret
&& !insert
) {
1553 } else if (WARN_ON(ret
)) {
1558 leaf
= path
->nodes
[0];
1559 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1560 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1561 if (item_size
< sizeof(*ei
)) {
1566 ret
= convert_extent_item_v0(trans
, root
, path
, owner
,
1572 leaf
= path
->nodes
[0];
1573 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1576 BUG_ON(item_size
< sizeof(*ei
));
1578 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1579 flags
= btrfs_extent_flags(leaf
, ei
);
1581 ptr
= (unsigned long)(ei
+ 1);
1582 end
= (unsigned long)ei
+ item_size
;
1584 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
&& !skinny_metadata
) {
1585 ptr
+= sizeof(struct btrfs_tree_block_info
);
1595 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1596 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1600 ptr
+= btrfs_extent_inline_ref_size(type
);
1604 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1605 struct btrfs_extent_data_ref
*dref
;
1606 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1607 if (match_extent_data_ref(leaf
, dref
, root_objectid
,
1612 if (hash_extent_data_ref_item(leaf
, dref
) <
1613 hash_extent_data_ref(root_objectid
, owner
, offset
))
1617 ref_offset
= btrfs_extent_inline_ref_offset(leaf
, iref
);
1619 if (parent
== ref_offset
) {
1623 if (ref_offset
< parent
)
1626 if (root_objectid
== ref_offset
) {
1630 if (ref_offset
< root_objectid
)
1634 ptr
+= btrfs_extent_inline_ref_size(type
);
1636 if (err
== -ENOENT
&& insert
) {
1637 if (item_size
+ extra_size
>=
1638 BTRFS_MAX_EXTENT_ITEM_SIZE(root
)) {
1643 * To add new inline back ref, we have to make sure
1644 * there is no corresponding back ref item.
1645 * For simplicity, we just do not add new inline back
1646 * ref if there is any kind of item for this block
1648 if (find_next_key(path
, 0, &key
) == 0 &&
1649 key
.objectid
== bytenr
&&
1650 key
.type
< BTRFS_BLOCK_GROUP_ITEM_KEY
) {
1655 *ref_ret
= (struct btrfs_extent_inline_ref
*)ptr
;
1658 path
->keep_locks
= 0;
1659 btrfs_unlock_up_safe(path
, 1);
1665 * helper to add new inline back ref
1667 static noinline_for_stack
1668 void setup_inline_extent_backref(struct btrfs_root
*root
,
1669 struct btrfs_path
*path
,
1670 struct btrfs_extent_inline_ref
*iref
,
1671 u64 parent
, u64 root_objectid
,
1672 u64 owner
, u64 offset
, int refs_to_add
,
1673 struct btrfs_delayed_extent_op
*extent_op
)
1675 struct extent_buffer
*leaf
;
1676 struct btrfs_extent_item
*ei
;
1679 unsigned long item_offset
;
1684 leaf
= path
->nodes
[0];
1685 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1686 item_offset
= (unsigned long)iref
- (unsigned long)ei
;
1688 type
= extent_ref_type(parent
, owner
);
1689 size
= btrfs_extent_inline_ref_size(type
);
1691 btrfs_extend_item(root
, path
, size
);
1693 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1694 refs
= btrfs_extent_refs(leaf
, ei
);
1695 refs
+= refs_to_add
;
1696 btrfs_set_extent_refs(leaf
, ei
, refs
);
1698 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1700 ptr
= (unsigned long)ei
+ item_offset
;
1701 end
= (unsigned long)ei
+ btrfs_item_size_nr(leaf
, path
->slots
[0]);
1702 if (ptr
< end
- size
)
1703 memmove_extent_buffer(leaf
, ptr
+ size
, ptr
,
1706 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1707 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
1708 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1709 struct btrfs_extent_data_ref
*dref
;
1710 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1711 btrfs_set_extent_data_ref_root(leaf
, dref
, root_objectid
);
1712 btrfs_set_extent_data_ref_objectid(leaf
, dref
, owner
);
1713 btrfs_set_extent_data_ref_offset(leaf
, dref
, offset
);
1714 btrfs_set_extent_data_ref_count(leaf
, dref
, refs_to_add
);
1715 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1716 struct btrfs_shared_data_ref
*sref
;
1717 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1718 btrfs_set_shared_data_ref_count(leaf
, sref
, refs_to_add
);
1719 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1720 } else if (type
== BTRFS_SHARED_BLOCK_REF_KEY
) {
1721 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1723 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
1725 btrfs_mark_buffer_dirty(leaf
);
1728 static int lookup_extent_backref(struct btrfs_trans_handle
*trans
,
1729 struct btrfs_root
*root
,
1730 struct btrfs_path
*path
,
1731 struct btrfs_extent_inline_ref
**ref_ret
,
1732 u64 bytenr
, u64 num_bytes
, u64 parent
,
1733 u64 root_objectid
, u64 owner
, u64 offset
)
1737 ret
= lookup_inline_extent_backref(trans
, root
, path
, ref_ret
,
1738 bytenr
, num_bytes
, parent
,
1739 root_objectid
, owner
, offset
, 0);
1743 btrfs_release_path(path
);
1746 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1747 ret
= lookup_tree_block_ref(trans
, root
, path
, bytenr
, parent
,
1750 ret
= lookup_extent_data_ref(trans
, root
, path
, bytenr
, parent
,
1751 root_objectid
, owner
, offset
);
1757 * helper to update/remove inline back ref
1759 static noinline_for_stack
1760 void update_inline_extent_backref(struct btrfs_root
*root
,
1761 struct btrfs_path
*path
,
1762 struct btrfs_extent_inline_ref
*iref
,
1764 struct btrfs_delayed_extent_op
*extent_op
,
1767 struct extent_buffer
*leaf
;
1768 struct btrfs_extent_item
*ei
;
1769 struct btrfs_extent_data_ref
*dref
= NULL
;
1770 struct btrfs_shared_data_ref
*sref
= NULL
;
1778 leaf
= path
->nodes
[0];
1779 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1780 refs
= btrfs_extent_refs(leaf
, ei
);
1781 WARN_ON(refs_to_mod
< 0 && refs
+ refs_to_mod
<= 0);
1782 refs
+= refs_to_mod
;
1783 btrfs_set_extent_refs(leaf
, ei
, refs
);
1785 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1787 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1789 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1790 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1791 refs
= btrfs_extent_data_ref_count(leaf
, dref
);
1792 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1793 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1794 refs
= btrfs_shared_data_ref_count(leaf
, sref
);
1797 BUG_ON(refs_to_mod
!= -1);
1800 BUG_ON(refs_to_mod
< 0 && refs
< -refs_to_mod
);
1801 refs
+= refs_to_mod
;
1804 if (type
== BTRFS_EXTENT_DATA_REF_KEY
)
1805 btrfs_set_extent_data_ref_count(leaf
, dref
, refs
);
1807 btrfs_set_shared_data_ref_count(leaf
, sref
, refs
);
1810 size
= btrfs_extent_inline_ref_size(type
);
1811 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1812 ptr
= (unsigned long)iref
;
1813 end
= (unsigned long)ei
+ item_size
;
1814 if (ptr
+ size
< end
)
1815 memmove_extent_buffer(leaf
, ptr
, ptr
+ size
,
1818 btrfs_truncate_item(root
, path
, item_size
, 1);
1820 btrfs_mark_buffer_dirty(leaf
);
1823 static noinline_for_stack
1824 int insert_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1825 struct btrfs_root
*root
,
1826 struct btrfs_path
*path
,
1827 u64 bytenr
, u64 num_bytes
, u64 parent
,
1828 u64 root_objectid
, u64 owner
,
1829 u64 offset
, int refs_to_add
,
1830 struct btrfs_delayed_extent_op
*extent_op
)
1832 struct btrfs_extent_inline_ref
*iref
;
1835 ret
= lookup_inline_extent_backref(trans
, root
, path
, &iref
,
1836 bytenr
, num_bytes
, parent
,
1837 root_objectid
, owner
, offset
, 1);
1839 BUG_ON(owner
< BTRFS_FIRST_FREE_OBJECTID
);
1840 update_inline_extent_backref(root
, path
, iref
,
1841 refs_to_add
, extent_op
, NULL
);
1842 } else if (ret
== -ENOENT
) {
1843 setup_inline_extent_backref(root
, path
, iref
, parent
,
1844 root_objectid
, owner
, offset
,
1845 refs_to_add
, extent_op
);
1851 static int insert_extent_backref(struct btrfs_trans_handle
*trans
,
1852 struct btrfs_root
*root
,
1853 struct btrfs_path
*path
,
1854 u64 bytenr
, u64 parent
, u64 root_objectid
,
1855 u64 owner
, u64 offset
, int refs_to_add
)
1858 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1859 BUG_ON(refs_to_add
!= 1);
1860 ret
= insert_tree_block_ref(trans
, root
, path
, bytenr
,
1861 parent
, root_objectid
);
1863 ret
= insert_extent_data_ref(trans
, root
, path
, bytenr
,
1864 parent
, root_objectid
,
1865 owner
, offset
, refs_to_add
);
1870 static int remove_extent_backref(struct btrfs_trans_handle
*trans
,
1871 struct btrfs_root
*root
,
1872 struct btrfs_path
*path
,
1873 struct btrfs_extent_inline_ref
*iref
,
1874 int refs_to_drop
, int is_data
, int *last_ref
)
1878 BUG_ON(!is_data
&& refs_to_drop
!= 1);
1880 update_inline_extent_backref(root
, path
, iref
,
1881 -refs_to_drop
, NULL
, last_ref
);
1882 } else if (is_data
) {
1883 ret
= remove_extent_data_ref(trans
, root
, path
, refs_to_drop
,
1887 ret
= btrfs_del_item(trans
, root
, path
);
1892 #define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
1893 static int btrfs_issue_discard(struct block_device
*bdev
, u64 start
, u64 len
,
1894 u64
*discarded_bytes
)
1897 u64 bytes_left
, end
;
1898 u64 aligned_start
= ALIGN(start
, 1 << 9);
1900 if (WARN_ON(start
!= aligned_start
)) {
1901 len
-= aligned_start
- start
;
1902 len
= round_down(len
, 1 << 9);
1903 start
= aligned_start
;
1906 *discarded_bytes
= 0;
1914 /* Skip any superblocks on this device. */
1915 for (j
= 0; j
< BTRFS_SUPER_MIRROR_MAX
; j
++) {
1916 u64 sb_start
= btrfs_sb_offset(j
);
1917 u64 sb_end
= sb_start
+ BTRFS_SUPER_INFO_SIZE
;
1918 u64 size
= sb_start
- start
;
1920 if (!in_range(sb_start
, start
, bytes_left
) &&
1921 !in_range(sb_end
, start
, bytes_left
) &&
1922 !in_range(start
, sb_start
, BTRFS_SUPER_INFO_SIZE
))
1926 * Superblock spans beginning of range. Adjust start and
1929 if (sb_start
<= start
) {
1930 start
+= sb_end
- start
;
1935 bytes_left
= end
- start
;
1940 ret
= blkdev_issue_discard(bdev
, start
>> 9, size
>> 9,
1943 *discarded_bytes
+= size
;
1944 else if (ret
!= -EOPNOTSUPP
)
1953 bytes_left
= end
- start
;
1957 ret
= blkdev_issue_discard(bdev
, start
>> 9, bytes_left
>> 9,
1960 *discarded_bytes
+= bytes_left
;
1965 int btrfs_discard_extent(struct btrfs_root
*root
, u64 bytenr
,
1966 u64 num_bytes
, u64
*actual_bytes
)
1969 u64 discarded_bytes
= 0;
1970 struct btrfs_bio
*bbio
= NULL
;
1973 /* Tell the block device(s) that the sectors can be discarded */
1974 ret
= btrfs_map_block(root
->fs_info
, REQ_DISCARD
,
1975 bytenr
, &num_bytes
, &bbio
, 0);
1976 /* Error condition is -ENOMEM */
1978 struct btrfs_bio_stripe
*stripe
= bbio
->stripes
;
1982 for (i
= 0; i
< bbio
->num_stripes
; i
++, stripe
++) {
1984 if (!stripe
->dev
->can_discard
)
1987 ret
= btrfs_issue_discard(stripe
->dev
->bdev
,
1992 discarded_bytes
+= bytes
;
1993 else if (ret
!= -EOPNOTSUPP
)
1994 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
1997 * Just in case we get back EOPNOTSUPP for some reason,
1998 * just ignore the return value so we don't screw up
1999 * people calling discard_extent.
2003 btrfs_put_bbio(bbio
);
2007 *actual_bytes
= discarded_bytes
;
2010 if (ret
== -EOPNOTSUPP
)
2015 /* Can return -ENOMEM */
2016 int btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
2017 struct btrfs_root
*root
,
2018 u64 bytenr
, u64 num_bytes
, u64 parent
,
2019 u64 root_objectid
, u64 owner
, u64 offset
,
2023 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2025 BUG_ON(owner
< BTRFS_FIRST_FREE_OBJECTID
&&
2026 root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
2028 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
2029 ret
= btrfs_add_delayed_tree_ref(fs_info
, trans
, bytenr
,
2031 parent
, root_objectid
, (int)owner
,
2032 BTRFS_ADD_DELAYED_REF
, NULL
, no_quota
);
2034 ret
= btrfs_add_delayed_data_ref(fs_info
, trans
, bytenr
,
2036 parent
, root_objectid
, owner
, offset
,
2037 BTRFS_ADD_DELAYED_REF
, NULL
, no_quota
);
2042 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
2043 struct btrfs_root
*root
,
2044 struct btrfs_delayed_ref_node
*node
,
2045 u64 parent
, u64 root_objectid
,
2046 u64 owner
, u64 offset
, int refs_to_add
,
2047 struct btrfs_delayed_extent_op
*extent_op
)
2049 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2050 struct btrfs_path
*path
;
2051 struct extent_buffer
*leaf
;
2052 struct btrfs_extent_item
*item
;
2053 struct btrfs_key key
;
2054 u64 bytenr
= node
->bytenr
;
2055 u64 num_bytes
= node
->num_bytes
;
2058 int no_quota
= node
->no_quota
;
2060 path
= btrfs_alloc_path();
2064 if (!is_fstree(root_objectid
) || !root
->fs_info
->quota_enabled
)
2068 path
->leave_spinning
= 1;
2069 /* this will setup the path even if it fails to insert the back ref */
2070 ret
= insert_inline_extent_backref(trans
, fs_info
->extent_root
, path
,
2071 bytenr
, num_bytes
, parent
,
2072 root_objectid
, owner
, offset
,
2073 refs_to_add
, extent_op
);
2074 if ((ret
< 0 && ret
!= -EAGAIN
) || !ret
)
2078 * Ok we had -EAGAIN which means we didn't have space to insert and
2079 * inline extent ref, so just update the reference count and add a
2082 leaf
= path
->nodes
[0];
2083 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2084 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
2085 refs
= btrfs_extent_refs(leaf
, item
);
2086 btrfs_set_extent_refs(leaf
, item
, refs
+ refs_to_add
);
2088 __run_delayed_extent_op(extent_op
, leaf
, item
);
2090 btrfs_mark_buffer_dirty(leaf
);
2091 btrfs_release_path(path
);
2094 path
->leave_spinning
= 1;
2095 /* now insert the actual backref */
2096 ret
= insert_extent_backref(trans
, root
->fs_info
->extent_root
,
2097 path
, bytenr
, parent
, root_objectid
,
2098 owner
, offset
, refs_to_add
);
2100 btrfs_abort_transaction(trans
, root
, ret
);
2102 btrfs_free_path(path
);
2106 static int run_delayed_data_ref(struct btrfs_trans_handle
*trans
,
2107 struct btrfs_root
*root
,
2108 struct btrfs_delayed_ref_node
*node
,
2109 struct btrfs_delayed_extent_op
*extent_op
,
2110 int insert_reserved
)
2113 struct btrfs_delayed_data_ref
*ref
;
2114 struct btrfs_key ins
;
2119 ins
.objectid
= node
->bytenr
;
2120 ins
.offset
= node
->num_bytes
;
2121 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
2123 ref
= btrfs_delayed_node_to_data_ref(node
);
2124 trace_run_delayed_data_ref(node
, ref
, node
->action
);
2126 if (node
->type
== BTRFS_SHARED_DATA_REF_KEY
)
2127 parent
= ref
->parent
;
2128 ref_root
= ref
->root
;
2130 if (node
->action
== BTRFS_ADD_DELAYED_REF
&& insert_reserved
) {
2132 flags
|= extent_op
->flags_to_set
;
2133 ret
= alloc_reserved_file_extent(trans
, root
,
2134 parent
, ref_root
, flags
,
2135 ref
->objectid
, ref
->offset
,
2136 &ins
, node
->ref_mod
);
2137 } else if (node
->action
== BTRFS_ADD_DELAYED_REF
) {
2138 ret
= __btrfs_inc_extent_ref(trans
, root
, node
, parent
,
2139 ref_root
, ref
->objectid
,
2140 ref
->offset
, node
->ref_mod
,
2142 } else if (node
->action
== BTRFS_DROP_DELAYED_REF
) {
2143 ret
= __btrfs_free_extent(trans
, root
, node
, parent
,
2144 ref_root
, ref
->objectid
,
2145 ref
->offset
, node
->ref_mod
,
2153 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op
*extent_op
,
2154 struct extent_buffer
*leaf
,
2155 struct btrfs_extent_item
*ei
)
2157 u64 flags
= btrfs_extent_flags(leaf
, ei
);
2158 if (extent_op
->update_flags
) {
2159 flags
|= extent_op
->flags_to_set
;
2160 btrfs_set_extent_flags(leaf
, ei
, flags
);
2163 if (extent_op
->update_key
) {
2164 struct btrfs_tree_block_info
*bi
;
2165 BUG_ON(!(flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
));
2166 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
2167 btrfs_set_tree_block_key(leaf
, bi
, &extent_op
->key
);
2171 static int run_delayed_extent_op(struct btrfs_trans_handle
*trans
,
2172 struct btrfs_root
*root
,
2173 struct btrfs_delayed_ref_node
*node
,
2174 struct btrfs_delayed_extent_op
*extent_op
)
2176 struct btrfs_key key
;
2177 struct btrfs_path
*path
;
2178 struct btrfs_extent_item
*ei
;
2179 struct extent_buffer
*leaf
;
2183 int metadata
= !extent_op
->is_data
;
2188 if (metadata
&& !btrfs_fs_incompat(root
->fs_info
, SKINNY_METADATA
))
2191 path
= btrfs_alloc_path();
2195 key
.objectid
= node
->bytenr
;
2198 key
.type
= BTRFS_METADATA_ITEM_KEY
;
2199 key
.offset
= extent_op
->level
;
2201 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2202 key
.offset
= node
->num_bytes
;
2207 path
->leave_spinning
= 1;
2208 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
,
2216 if (path
->slots
[0] > 0) {
2218 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
2220 if (key
.objectid
== node
->bytenr
&&
2221 key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
2222 key
.offset
== node
->num_bytes
)
2226 btrfs_release_path(path
);
2229 key
.objectid
= node
->bytenr
;
2230 key
.offset
= node
->num_bytes
;
2231 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2240 leaf
= path
->nodes
[0];
2241 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2242 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2243 if (item_size
< sizeof(*ei
)) {
2244 ret
= convert_extent_item_v0(trans
, root
->fs_info
->extent_root
,
2250 leaf
= path
->nodes
[0];
2251 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2254 BUG_ON(item_size
< sizeof(*ei
));
2255 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
2256 __run_delayed_extent_op(extent_op
, leaf
, ei
);
2258 btrfs_mark_buffer_dirty(leaf
);
2260 btrfs_free_path(path
);
2264 static int run_delayed_tree_ref(struct btrfs_trans_handle
*trans
,
2265 struct btrfs_root
*root
,
2266 struct btrfs_delayed_ref_node
*node
,
2267 struct btrfs_delayed_extent_op
*extent_op
,
2268 int insert_reserved
)
2271 struct btrfs_delayed_tree_ref
*ref
;
2272 struct btrfs_key ins
;
2275 bool skinny_metadata
= btrfs_fs_incompat(root
->fs_info
,
2278 ref
= btrfs_delayed_node_to_tree_ref(node
);
2279 trace_run_delayed_tree_ref(node
, ref
, node
->action
);
2281 if (node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
)
2282 parent
= ref
->parent
;
2283 ref_root
= ref
->root
;
2285 ins
.objectid
= node
->bytenr
;
2286 if (skinny_metadata
) {
2287 ins
.offset
= ref
->level
;
2288 ins
.type
= BTRFS_METADATA_ITEM_KEY
;
2290 ins
.offset
= node
->num_bytes
;
2291 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
2294 BUG_ON(node
->ref_mod
!= 1);
2295 if (node
->action
== BTRFS_ADD_DELAYED_REF
&& insert_reserved
) {
2296 BUG_ON(!extent_op
|| !extent_op
->update_flags
);
2297 ret
= alloc_reserved_tree_block(trans
, root
,
2299 extent_op
->flags_to_set
,
2303 } else if (node
->action
== BTRFS_ADD_DELAYED_REF
) {
2304 ret
= __btrfs_inc_extent_ref(trans
, root
, node
,
2308 } else if (node
->action
== BTRFS_DROP_DELAYED_REF
) {
2309 ret
= __btrfs_free_extent(trans
, root
, node
,
2311 ref
->level
, 0, 1, extent_op
);
2318 /* helper function to actually process a single delayed ref entry */
2319 static int run_one_delayed_ref(struct btrfs_trans_handle
*trans
,
2320 struct btrfs_root
*root
,
2321 struct btrfs_delayed_ref_node
*node
,
2322 struct btrfs_delayed_extent_op
*extent_op
,
2323 int insert_reserved
)
2327 if (trans
->aborted
) {
2328 if (insert_reserved
)
2329 btrfs_pin_extent(root
, node
->bytenr
,
2330 node
->num_bytes
, 1);
2334 if (btrfs_delayed_ref_is_head(node
)) {
2335 struct btrfs_delayed_ref_head
*head
;
2337 * we've hit the end of the chain and we were supposed
2338 * to insert this extent into the tree. But, it got
2339 * deleted before we ever needed to insert it, so all
2340 * we have to do is clean up the accounting
2343 head
= btrfs_delayed_node_to_head(node
);
2344 trace_run_delayed_ref_head(node
, head
, node
->action
);
2346 if (insert_reserved
) {
2347 btrfs_pin_extent(root
, node
->bytenr
,
2348 node
->num_bytes
, 1);
2349 if (head
->is_data
) {
2350 ret
= btrfs_del_csums(trans
, root
,
2358 if (node
->type
== BTRFS_TREE_BLOCK_REF_KEY
||
2359 node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
)
2360 ret
= run_delayed_tree_ref(trans
, root
, node
, extent_op
,
2362 else if (node
->type
== BTRFS_EXTENT_DATA_REF_KEY
||
2363 node
->type
== BTRFS_SHARED_DATA_REF_KEY
)
2364 ret
= run_delayed_data_ref(trans
, root
, node
, extent_op
,
2371 static inline struct btrfs_delayed_ref_node
*
2372 select_delayed_ref(struct btrfs_delayed_ref_head
*head
)
2374 struct btrfs_delayed_ref_node
*ref
;
2376 if (list_empty(&head
->ref_list
))
2380 * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
2381 * This is to prevent a ref count from going down to zero, which deletes
2382 * the extent item from the extent tree, when there still are references
2383 * to add, which would fail because they would not find the extent item.
2385 list_for_each_entry(ref
, &head
->ref_list
, list
) {
2386 if (ref
->action
== BTRFS_ADD_DELAYED_REF
)
2390 return list_entry(head
->ref_list
.next
, struct btrfs_delayed_ref_node
,
2395 * Returns 0 on success or if called with an already aborted transaction.
2396 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2398 static noinline
int __btrfs_run_delayed_refs(struct btrfs_trans_handle
*trans
,
2399 struct btrfs_root
*root
,
2402 struct btrfs_delayed_ref_root
*delayed_refs
;
2403 struct btrfs_delayed_ref_node
*ref
;
2404 struct btrfs_delayed_ref_head
*locked_ref
= NULL
;
2405 struct btrfs_delayed_extent_op
*extent_op
;
2406 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2407 ktime_t start
= ktime_get();
2409 unsigned long count
= 0;
2410 unsigned long actual_count
= 0;
2411 int must_insert_reserved
= 0;
2413 delayed_refs
= &trans
->transaction
->delayed_refs
;
2419 spin_lock(&delayed_refs
->lock
);
2420 locked_ref
= btrfs_select_ref_head(trans
);
2422 spin_unlock(&delayed_refs
->lock
);
2426 /* grab the lock that says we are going to process
2427 * all the refs for this head */
2428 ret
= btrfs_delayed_ref_lock(trans
, locked_ref
);
2429 spin_unlock(&delayed_refs
->lock
);
2431 * we may have dropped the spin lock to get the head
2432 * mutex lock, and that might have given someone else
2433 * time to free the head. If that's true, it has been
2434 * removed from our list and we can move on.
2436 if (ret
== -EAGAIN
) {
2443 spin_lock(&locked_ref
->lock
);
2446 * locked_ref is the head node, so we have to go one
2447 * node back for any delayed ref updates
2449 ref
= select_delayed_ref(locked_ref
);
2451 if (ref
&& ref
->seq
&&
2452 btrfs_check_delayed_seq(fs_info
, delayed_refs
, ref
->seq
)) {
2453 spin_unlock(&locked_ref
->lock
);
2454 btrfs_delayed_ref_unlock(locked_ref
);
2455 spin_lock(&delayed_refs
->lock
);
2456 locked_ref
->processing
= 0;
2457 delayed_refs
->num_heads_ready
++;
2458 spin_unlock(&delayed_refs
->lock
);
2466 * record the must insert reserved flag before we
2467 * drop the spin lock.
2469 must_insert_reserved
= locked_ref
->must_insert_reserved
;
2470 locked_ref
->must_insert_reserved
= 0;
2472 extent_op
= locked_ref
->extent_op
;
2473 locked_ref
->extent_op
= NULL
;
2478 /* All delayed refs have been processed, Go ahead
2479 * and send the head node to run_one_delayed_ref,
2480 * so that any accounting fixes can happen
2482 ref
= &locked_ref
->node
;
2484 if (extent_op
&& must_insert_reserved
) {
2485 btrfs_free_delayed_extent_op(extent_op
);
2490 spin_unlock(&locked_ref
->lock
);
2491 ret
= run_delayed_extent_op(trans
, root
,
2493 btrfs_free_delayed_extent_op(extent_op
);
2497 * Need to reset must_insert_reserved if
2498 * there was an error so the abort stuff
2499 * can cleanup the reserved space
2502 if (must_insert_reserved
)
2503 locked_ref
->must_insert_reserved
= 1;
2504 locked_ref
->processing
= 0;
2505 btrfs_debug(fs_info
, "run_delayed_extent_op returned %d", ret
);
2506 btrfs_delayed_ref_unlock(locked_ref
);
2513 * Need to drop our head ref lock and re-aqcuire the
2514 * delayed ref lock and then re-check to make sure
2517 spin_unlock(&locked_ref
->lock
);
2518 spin_lock(&delayed_refs
->lock
);
2519 spin_lock(&locked_ref
->lock
);
2520 if (!list_empty(&locked_ref
->ref_list
) ||
2521 locked_ref
->extent_op
) {
2522 spin_unlock(&locked_ref
->lock
);
2523 spin_unlock(&delayed_refs
->lock
);
2527 delayed_refs
->num_heads
--;
2528 rb_erase(&locked_ref
->href_node
,
2529 &delayed_refs
->href_root
);
2530 spin_unlock(&delayed_refs
->lock
);
2534 list_del(&ref
->list
);
2536 atomic_dec(&delayed_refs
->num_entries
);
2538 if (!btrfs_delayed_ref_is_head(ref
)) {
2540 * when we play the delayed ref, also correct the
2543 switch (ref
->action
) {
2544 case BTRFS_ADD_DELAYED_REF
:
2545 case BTRFS_ADD_DELAYED_EXTENT
:
2546 locked_ref
->node
.ref_mod
-= ref
->ref_mod
;
2548 case BTRFS_DROP_DELAYED_REF
:
2549 locked_ref
->node
.ref_mod
+= ref
->ref_mod
;
2555 spin_unlock(&locked_ref
->lock
);
2557 ret
= run_one_delayed_ref(trans
, root
, ref
, extent_op
,
2558 must_insert_reserved
);
2560 btrfs_free_delayed_extent_op(extent_op
);
2562 locked_ref
->processing
= 0;
2563 btrfs_delayed_ref_unlock(locked_ref
);
2564 btrfs_put_delayed_ref(ref
);
2565 btrfs_debug(fs_info
, "run_one_delayed_ref returned %d", ret
);
2570 * If this node is a head, that means all the refs in this head
2571 * have been dealt with, and we will pick the next head to deal
2572 * with, so we must unlock the head and drop it from the cluster
2573 * list before we release it.
2575 if (btrfs_delayed_ref_is_head(ref
)) {
2576 if (locked_ref
->is_data
&&
2577 locked_ref
->total_ref_mod
< 0) {
2578 spin_lock(&delayed_refs
->lock
);
2579 delayed_refs
->pending_csums
-= ref
->num_bytes
;
2580 spin_unlock(&delayed_refs
->lock
);
2582 btrfs_delayed_ref_unlock(locked_ref
);
2585 btrfs_put_delayed_ref(ref
);
2591 * We don't want to include ref heads since we can have empty ref heads
2592 * and those will drastically skew our runtime down since we just do
2593 * accounting, no actual extent tree updates.
2595 if (actual_count
> 0) {
2596 u64 runtime
= ktime_to_ns(ktime_sub(ktime_get(), start
));
2600 * We weigh the current average higher than our current runtime
2601 * to avoid large swings in the average.
2603 spin_lock(&delayed_refs
->lock
);
2604 avg
= fs_info
->avg_delayed_ref_runtime
* 3 + runtime
;
2605 fs_info
->avg_delayed_ref_runtime
= avg
>> 2; /* div by 4 */
2606 spin_unlock(&delayed_refs
->lock
);
2611 #ifdef SCRAMBLE_DELAYED_REFS
2613 * Normally delayed refs get processed in ascending bytenr order. This
2614 * correlates in most cases to the order added. To expose dependencies on this
2615 * order, we start to process the tree in the middle instead of the beginning
2617 static u64
find_middle(struct rb_root
*root
)
2619 struct rb_node
*n
= root
->rb_node
;
2620 struct btrfs_delayed_ref_node
*entry
;
2623 u64 first
= 0, last
= 0;
2627 entry
= rb_entry(n
, struct btrfs_delayed_ref_node
, rb_node
);
2628 first
= entry
->bytenr
;
2632 entry
= rb_entry(n
, struct btrfs_delayed_ref_node
, rb_node
);
2633 last
= entry
->bytenr
;
2638 entry
= rb_entry(n
, struct btrfs_delayed_ref_node
, rb_node
);
2639 WARN_ON(!entry
->in_tree
);
2641 middle
= entry
->bytenr
;
2654 static inline u64
heads_to_leaves(struct btrfs_root
*root
, u64 heads
)
2658 num_bytes
= heads
* (sizeof(struct btrfs_extent_item
) +
2659 sizeof(struct btrfs_extent_inline_ref
));
2660 if (!btrfs_fs_incompat(root
->fs_info
, SKINNY_METADATA
))
2661 num_bytes
+= heads
* sizeof(struct btrfs_tree_block_info
);
2664 * We don't ever fill up leaves all the way so multiply by 2 just to be
2665 * closer to what we're really going to want to ouse.
2667 return div_u64(num_bytes
, BTRFS_LEAF_DATA_SIZE(root
));
2671 * Takes the number of bytes to be csumm'ed and figures out how many leaves it
2672 * would require to store the csums for that many bytes.
2674 u64
btrfs_csum_bytes_to_leaves(struct btrfs_root
*root
, u64 csum_bytes
)
2677 u64 num_csums_per_leaf
;
2680 csum_size
= BTRFS_LEAF_DATA_SIZE(root
) - sizeof(struct btrfs_item
);
2681 num_csums_per_leaf
= div64_u64(csum_size
,
2682 (u64
)btrfs_super_csum_size(root
->fs_info
->super_copy
));
2683 num_csums
= div64_u64(csum_bytes
, root
->sectorsize
);
2684 num_csums
+= num_csums_per_leaf
- 1;
2685 num_csums
= div64_u64(num_csums
, num_csums_per_leaf
);
2689 int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle
*trans
,
2690 struct btrfs_root
*root
)
2692 struct btrfs_block_rsv
*global_rsv
;
2693 u64 num_heads
= trans
->transaction
->delayed_refs
.num_heads_ready
;
2694 u64 csum_bytes
= trans
->transaction
->delayed_refs
.pending_csums
;
2695 u64 num_dirty_bgs
= trans
->transaction
->num_dirty_bgs
;
2696 u64 num_bytes
, num_dirty_bgs_bytes
;
2699 num_bytes
= btrfs_calc_trans_metadata_size(root
, 1);
2700 num_heads
= heads_to_leaves(root
, num_heads
);
2702 num_bytes
+= (num_heads
- 1) * root
->nodesize
;
2704 num_bytes
+= btrfs_csum_bytes_to_leaves(root
, csum_bytes
) * root
->nodesize
;
2705 num_dirty_bgs_bytes
= btrfs_calc_trans_metadata_size(root
,
2707 global_rsv
= &root
->fs_info
->global_block_rsv
;
2710 * If we can't allocate any more chunks lets make sure we have _lots_ of
2711 * wiggle room since running delayed refs can create more delayed refs.
2713 if (global_rsv
->space_info
->full
) {
2714 num_dirty_bgs_bytes
<<= 1;
2718 spin_lock(&global_rsv
->lock
);
2719 if (global_rsv
->reserved
<= num_bytes
+ num_dirty_bgs_bytes
)
2721 spin_unlock(&global_rsv
->lock
);
2725 int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle
*trans
,
2726 struct btrfs_root
*root
)
2728 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2730 atomic_read(&trans
->transaction
->delayed_refs
.num_entries
);
2735 avg_runtime
= fs_info
->avg_delayed_ref_runtime
;
2736 val
= num_entries
* avg_runtime
;
2737 if (num_entries
* avg_runtime
>= NSEC_PER_SEC
)
2739 if (val
>= NSEC_PER_SEC
/ 2)
2742 return btrfs_check_space_for_delayed_refs(trans
, root
);
2745 struct async_delayed_refs
{
2746 struct btrfs_root
*root
;
2750 struct completion wait
;
2751 struct btrfs_work work
;
2754 static void delayed_ref_async_start(struct btrfs_work
*work
)
2756 struct async_delayed_refs
*async
;
2757 struct btrfs_trans_handle
*trans
;
2760 async
= container_of(work
, struct async_delayed_refs
, work
);
2762 trans
= btrfs_join_transaction(async
->root
);
2763 if (IS_ERR(trans
)) {
2764 async
->error
= PTR_ERR(trans
);
2769 * trans->sync means that when we call end_transaciton, we won't
2770 * wait on delayed refs
2773 ret
= btrfs_run_delayed_refs(trans
, async
->root
, async
->count
);
2777 ret
= btrfs_end_transaction(trans
, async
->root
);
2778 if (ret
&& !async
->error
)
2782 complete(&async
->wait
);
2787 int btrfs_async_run_delayed_refs(struct btrfs_root
*root
,
2788 unsigned long count
, int wait
)
2790 struct async_delayed_refs
*async
;
2793 async
= kmalloc(sizeof(*async
), GFP_NOFS
);
2797 async
->root
= root
->fs_info
->tree_root
;
2798 async
->count
= count
;
2804 init_completion(&async
->wait
);
2806 btrfs_init_work(&async
->work
, btrfs_extent_refs_helper
,
2807 delayed_ref_async_start
, NULL
, NULL
);
2809 btrfs_queue_work(root
->fs_info
->extent_workers
, &async
->work
);
2812 wait_for_completion(&async
->wait
);
2821 * this starts processing the delayed reference count updates and
2822 * extent insertions we have queued up so far. count can be
2823 * 0, which means to process everything in the tree at the start
2824 * of the run (but not newly added entries), or it can be some target
2825 * number you'd like to process.
2827 * Returns 0 on success or if called with an aborted transaction
2828 * Returns <0 on error and aborts the transaction
2830 int btrfs_run_delayed_refs(struct btrfs_trans_handle
*trans
,
2831 struct btrfs_root
*root
, unsigned long count
)
2833 struct rb_node
*node
;
2834 struct btrfs_delayed_ref_root
*delayed_refs
;
2835 struct btrfs_delayed_ref_head
*head
;
2837 int run_all
= count
== (unsigned long)-1;
2838 bool can_flush_pending_bgs
= trans
->can_flush_pending_bgs
;
2840 /* We'll clean this up in btrfs_cleanup_transaction */
2844 if (root
== root
->fs_info
->extent_root
)
2845 root
= root
->fs_info
->tree_root
;
2847 delayed_refs
= &trans
->transaction
->delayed_refs
;
2849 count
= atomic_read(&delayed_refs
->num_entries
) * 2;
2852 #ifdef SCRAMBLE_DELAYED_REFS
2853 delayed_refs
->run_delayed_start
= find_middle(&delayed_refs
->root
);
2855 trans
->can_flush_pending_bgs
= false;
2856 ret
= __btrfs_run_delayed_refs(trans
, root
, count
);
2858 btrfs_abort_transaction(trans
, root
, ret
);
2863 if (!list_empty(&trans
->new_bgs
))
2864 btrfs_create_pending_block_groups(trans
, root
);
2866 spin_lock(&delayed_refs
->lock
);
2867 node
= rb_first(&delayed_refs
->href_root
);
2869 spin_unlock(&delayed_refs
->lock
);
2872 count
= (unsigned long)-1;
2875 head
= rb_entry(node
, struct btrfs_delayed_ref_head
,
2877 if (btrfs_delayed_ref_is_head(&head
->node
)) {
2878 struct btrfs_delayed_ref_node
*ref
;
2881 atomic_inc(&ref
->refs
);
2883 spin_unlock(&delayed_refs
->lock
);
2885 * Mutex was contended, block until it's
2886 * released and try again
2888 mutex_lock(&head
->mutex
);
2889 mutex_unlock(&head
->mutex
);
2891 btrfs_put_delayed_ref(ref
);
2897 node
= rb_next(node
);
2899 spin_unlock(&delayed_refs
->lock
);
2904 assert_qgroups_uptodate(trans
);
2905 trans
->can_flush_pending_bgs
= can_flush_pending_bgs
;
2909 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle
*trans
,
2910 struct btrfs_root
*root
,
2911 u64 bytenr
, u64 num_bytes
, u64 flags
,
2912 int level
, int is_data
)
2914 struct btrfs_delayed_extent_op
*extent_op
;
2917 extent_op
= btrfs_alloc_delayed_extent_op();
2921 extent_op
->flags_to_set
= flags
;
2922 extent_op
->update_flags
= 1;
2923 extent_op
->update_key
= 0;
2924 extent_op
->is_data
= is_data
? 1 : 0;
2925 extent_op
->level
= level
;
2927 ret
= btrfs_add_delayed_extent_op(root
->fs_info
, trans
, bytenr
,
2928 num_bytes
, extent_op
);
2930 btrfs_free_delayed_extent_op(extent_op
);
2934 static noinline
int check_delayed_ref(struct btrfs_trans_handle
*trans
,
2935 struct btrfs_root
*root
,
2936 struct btrfs_path
*path
,
2937 u64 objectid
, u64 offset
, u64 bytenr
)
2939 struct btrfs_delayed_ref_head
*head
;
2940 struct btrfs_delayed_ref_node
*ref
;
2941 struct btrfs_delayed_data_ref
*data_ref
;
2942 struct btrfs_delayed_ref_root
*delayed_refs
;
2945 delayed_refs
= &trans
->transaction
->delayed_refs
;
2946 spin_lock(&delayed_refs
->lock
);
2947 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
2949 spin_unlock(&delayed_refs
->lock
);
2953 if (!mutex_trylock(&head
->mutex
)) {
2954 atomic_inc(&head
->node
.refs
);
2955 spin_unlock(&delayed_refs
->lock
);
2957 btrfs_release_path(path
);
2960 * Mutex was contended, block until it's released and let
2963 mutex_lock(&head
->mutex
);
2964 mutex_unlock(&head
->mutex
);
2965 btrfs_put_delayed_ref(&head
->node
);
2968 spin_unlock(&delayed_refs
->lock
);
2970 spin_lock(&head
->lock
);
2971 list_for_each_entry(ref
, &head
->ref_list
, list
) {
2972 /* If it's a shared ref we know a cross reference exists */
2973 if (ref
->type
!= BTRFS_EXTENT_DATA_REF_KEY
) {
2978 data_ref
= btrfs_delayed_node_to_data_ref(ref
);
2981 * If our ref doesn't match the one we're currently looking at
2982 * then we have a cross reference.
2984 if (data_ref
->root
!= root
->root_key
.objectid
||
2985 data_ref
->objectid
!= objectid
||
2986 data_ref
->offset
!= offset
) {
2991 spin_unlock(&head
->lock
);
2992 mutex_unlock(&head
->mutex
);
2996 static noinline
int check_committed_ref(struct btrfs_trans_handle
*trans
,
2997 struct btrfs_root
*root
,
2998 struct btrfs_path
*path
,
2999 u64 objectid
, u64 offset
, u64 bytenr
)
3001 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
3002 struct extent_buffer
*leaf
;
3003 struct btrfs_extent_data_ref
*ref
;
3004 struct btrfs_extent_inline_ref
*iref
;
3005 struct btrfs_extent_item
*ei
;
3006 struct btrfs_key key
;
3010 key
.objectid
= bytenr
;
3011 key
.offset
= (u64
)-1;
3012 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
3014 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
3017 BUG_ON(ret
== 0); /* Corruption */
3020 if (path
->slots
[0] == 0)
3024 leaf
= path
->nodes
[0];
3025 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3027 if (key
.objectid
!= bytenr
|| key
.type
!= BTRFS_EXTENT_ITEM_KEY
)
3031 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
3032 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3033 if (item_size
< sizeof(*ei
)) {
3034 WARN_ON(item_size
!= sizeof(struct btrfs_extent_item_v0
));
3038 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
3040 if (item_size
!= sizeof(*ei
) +
3041 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY
))
3044 if (btrfs_extent_generation(leaf
, ei
) <=
3045 btrfs_root_last_snapshot(&root
->root_item
))
3048 iref
= (struct btrfs_extent_inline_ref
*)(ei
+ 1);
3049 if (btrfs_extent_inline_ref_type(leaf
, iref
) !=
3050 BTRFS_EXTENT_DATA_REF_KEY
)
3053 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
3054 if (btrfs_extent_refs(leaf
, ei
) !=
3055 btrfs_extent_data_ref_count(leaf
, ref
) ||
3056 btrfs_extent_data_ref_root(leaf
, ref
) !=
3057 root
->root_key
.objectid
||
3058 btrfs_extent_data_ref_objectid(leaf
, ref
) != objectid
||
3059 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
3067 int btrfs_cross_ref_exist(struct btrfs_trans_handle
*trans
,
3068 struct btrfs_root
*root
,
3069 u64 objectid
, u64 offset
, u64 bytenr
)
3071 struct btrfs_path
*path
;
3075 path
= btrfs_alloc_path();
3080 ret
= check_committed_ref(trans
, root
, path
, objectid
,
3082 if (ret
&& ret
!= -ENOENT
)
3085 ret2
= check_delayed_ref(trans
, root
, path
, objectid
,
3087 } while (ret2
== -EAGAIN
);
3089 if (ret2
&& ret2
!= -ENOENT
) {
3094 if (ret
!= -ENOENT
|| ret2
!= -ENOENT
)
3097 btrfs_free_path(path
);
3098 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
3103 static int __btrfs_mod_ref(struct btrfs_trans_handle
*trans
,
3104 struct btrfs_root
*root
,
3105 struct extent_buffer
*buf
,
3106 int full_backref
, int inc
)
3113 struct btrfs_key key
;
3114 struct btrfs_file_extent_item
*fi
;
3118 int (*process_func
)(struct btrfs_trans_handle
*, struct btrfs_root
*,
3119 u64
, u64
, u64
, u64
, u64
, u64
, int);
3122 if (btrfs_test_is_dummy_root(root
))
3125 ref_root
= btrfs_header_owner(buf
);
3126 nritems
= btrfs_header_nritems(buf
);
3127 level
= btrfs_header_level(buf
);
3129 if (!test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) && level
== 0)
3133 process_func
= btrfs_inc_extent_ref
;
3135 process_func
= btrfs_free_extent
;
3138 parent
= buf
->start
;
3142 for (i
= 0; i
< nritems
; i
++) {
3144 btrfs_item_key_to_cpu(buf
, &key
, i
);
3145 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
3147 fi
= btrfs_item_ptr(buf
, i
,
3148 struct btrfs_file_extent_item
);
3149 if (btrfs_file_extent_type(buf
, fi
) ==
3150 BTRFS_FILE_EXTENT_INLINE
)
3152 bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
3156 num_bytes
= btrfs_file_extent_disk_num_bytes(buf
, fi
);
3157 key
.offset
-= btrfs_file_extent_offset(buf
, fi
);
3158 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
3159 parent
, ref_root
, key
.objectid
,
3164 bytenr
= btrfs_node_blockptr(buf
, i
);
3165 num_bytes
= root
->nodesize
;
3166 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
3167 parent
, ref_root
, level
- 1, 0,
3178 int btrfs_inc_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
3179 struct extent_buffer
*buf
, int full_backref
)
3181 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 1);
3184 int btrfs_dec_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
3185 struct extent_buffer
*buf
, int full_backref
)
3187 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 0);
3190 static int write_one_cache_group(struct btrfs_trans_handle
*trans
,
3191 struct btrfs_root
*root
,
3192 struct btrfs_path
*path
,
3193 struct btrfs_block_group_cache
*cache
)
3196 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
3198 struct extent_buffer
*leaf
;
3200 ret
= btrfs_search_slot(trans
, extent_root
, &cache
->key
, path
, 0, 1);
3207 leaf
= path
->nodes
[0];
3208 bi
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
3209 write_extent_buffer(leaf
, &cache
->item
, bi
, sizeof(cache
->item
));
3210 btrfs_mark_buffer_dirty(leaf
);
3212 btrfs_release_path(path
);
3217 static struct btrfs_block_group_cache
*
3218 next_block_group(struct btrfs_root
*root
,
3219 struct btrfs_block_group_cache
*cache
)
3221 struct rb_node
*node
;
3223 spin_lock(&root
->fs_info
->block_group_cache_lock
);
3225 /* If our block group was removed, we need a full search. */
3226 if (RB_EMPTY_NODE(&cache
->cache_node
)) {
3227 const u64 next_bytenr
= cache
->key
.objectid
+ cache
->key
.offset
;
3229 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
3230 btrfs_put_block_group(cache
);
3231 cache
= btrfs_lookup_first_block_group(root
->fs_info
,
3235 node
= rb_next(&cache
->cache_node
);
3236 btrfs_put_block_group(cache
);
3238 cache
= rb_entry(node
, struct btrfs_block_group_cache
,
3240 btrfs_get_block_group(cache
);
3243 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
3247 static int cache_save_setup(struct btrfs_block_group_cache
*block_group
,
3248 struct btrfs_trans_handle
*trans
,
3249 struct btrfs_path
*path
)
3251 struct btrfs_root
*root
= block_group
->fs_info
->tree_root
;
3252 struct inode
*inode
= NULL
;
3254 int dcs
= BTRFS_DC_ERROR
;
3260 * If this block group is smaller than 100 megs don't bother caching the
3263 if (block_group
->key
.offset
< (100 * 1024 * 1024)) {
3264 spin_lock(&block_group
->lock
);
3265 block_group
->disk_cache_state
= BTRFS_DC_WRITTEN
;
3266 spin_unlock(&block_group
->lock
);
3273 inode
= lookup_free_space_inode(root
, block_group
, path
);
3274 if (IS_ERR(inode
) && PTR_ERR(inode
) != -ENOENT
) {
3275 ret
= PTR_ERR(inode
);
3276 btrfs_release_path(path
);
3280 if (IS_ERR(inode
)) {
3284 if (block_group
->ro
)
3287 ret
= create_free_space_inode(root
, trans
, block_group
, path
);
3293 /* We've already setup this transaction, go ahead and exit */
3294 if (block_group
->cache_generation
== trans
->transid
&&
3295 i_size_read(inode
)) {
3296 dcs
= BTRFS_DC_SETUP
;
3301 * We want to set the generation to 0, that way if anything goes wrong
3302 * from here on out we know not to trust this cache when we load up next
3305 BTRFS_I(inode
)->generation
= 0;
3306 ret
= btrfs_update_inode(trans
, root
, inode
);
3309 * So theoretically we could recover from this, simply set the
3310 * super cache generation to 0 so we know to invalidate the
3311 * cache, but then we'd have to keep track of the block groups
3312 * that fail this way so we know we _have_ to reset this cache
3313 * before the next commit or risk reading stale cache. So to
3314 * limit our exposure to horrible edge cases lets just abort the
3315 * transaction, this only happens in really bad situations
3318 btrfs_abort_transaction(trans
, root
, ret
);
3323 if (i_size_read(inode
) > 0) {
3324 ret
= btrfs_check_trunc_cache_free_space(root
,
3325 &root
->fs_info
->global_block_rsv
);
3329 ret
= btrfs_truncate_free_space_cache(root
, trans
, NULL
, inode
);
3334 spin_lock(&block_group
->lock
);
3335 if (block_group
->cached
!= BTRFS_CACHE_FINISHED
||
3336 !btrfs_test_opt(root
, SPACE_CACHE
)) {
3338 * don't bother trying to write stuff out _if_
3339 * a) we're not cached,
3340 * b) we're with nospace_cache mount option.
3342 dcs
= BTRFS_DC_WRITTEN
;
3343 spin_unlock(&block_group
->lock
);
3346 spin_unlock(&block_group
->lock
);
3349 * Try to preallocate enough space based on how big the block group is.
3350 * Keep in mind this has to include any pinned space which could end up
3351 * taking up quite a bit since it's not folded into the other space
3354 num_pages
= div_u64(block_group
->key
.offset
, 256 * 1024 * 1024);
3359 num_pages
*= PAGE_CACHE_SIZE
;
3361 ret
= btrfs_check_data_free_space(inode
, num_pages
, num_pages
);
3365 ret
= btrfs_prealloc_file_range_trans(inode
, trans
, 0, 0, num_pages
,
3366 num_pages
, num_pages
,
3369 dcs
= BTRFS_DC_SETUP
;
3370 btrfs_free_reserved_data_space(inode
, num_pages
);
3375 btrfs_release_path(path
);
3377 spin_lock(&block_group
->lock
);
3378 if (!ret
&& dcs
== BTRFS_DC_SETUP
)
3379 block_group
->cache_generation
= trans
->transid
;
3380 block_group
->disk_cache_state
= dcs
;
3381 spin_unlock(&block_group
->lock
);
3386 int btrfs_setup_space_cache(struct btrfs_trans_handle
*trans
,
3387 struct btrfs_root
*root
)
3389 struct btrfs_block_group_cache
*cache
, *tmp
;
3390 struct btrfs_transaction
*cur_trans
= trans
->transaction
;
3391 struct btrfs_path
*path
;
3393 if (list_empty(&cur_trans
->dirty_bgs
) ||
3394 !btrfs_test_opt(root
, SPACE_CACHE
))
3397 path
= btrfs_alloc_path();
3401 /* Could add new block groups, use _safe just in case */
3402 list_for_each_entry_safe(cache
, tmp
, &cur_trans
->dirty_bgs
,
3404 if (cache
->disk_cache_state
== BTRFS_DC_CLEAR
)
3405 cache_save_setup(cache
, trans
, path
);
3408 btrfs_free_path(path
);
3413 * transaction commit does final block group cache writeback during a
3414 * critical section where nothing is allowed to change the FS. This is
3415 * required in order for the cache to actually match the block group,
3416 * but can introduce a lot of latency into the commit.
3418 * So, btrfs_start_dirty_block_groups is here to kick off block group
3419 * cache IO. There's a chance we'll have to redo some of it if the
3420 * block group changes again during the commit, but it greatly reduces
3421 * the commit latency by getting rid of the easy block groups while
3422 * we're still allowing others to join the commit.
3424 int btrfs_start_dirty_block_groups(struct btrfs_trans_handle
*trans
,
3425 struct btrfs_root
*root
)
3427 struct btrfs_block_group_cache
*cache
;
3428 struct btrfs_transaction
*cur_trans
= trans
->transaction
;
3431 struct btrfs_path
*path
= NULL
;
3433 struct list_head
*io
= &cur_trans
->io_bgs
;
3434 int num_started
= 0;
3437 spin_lock(&cur_trans
->dirty_bgs_lock
);
3438 if (list_empty(&cur_trans
->dirty_bgs
)) {
3439 spin_unlock(&cur_trans
->dirty_bgs_lock
);
3442 list_splice_init(&cur_trans
->dirty_bgs
, &dirty
);
3443 spin_unlock(&cur_trans
->dirty_bgs_lock
);
3447 * make sure all the block groups on our dirty list actually
3450 btrfs_create_pending_block_groups(trans
, root
);
3453 path
= btrfs_alloc_path();
3459 * cache_write_mutex is here only to save us from balance or automatic
3460 * removal of empty block groups deleting this block group while we are
3461 * writing out the cache
3463 mutex_lock(&trans
->transaction
->cache_write_mutex
);
3464 while (!list_empty(&dirty
)) {
3465 cache
= list_first_entry(&dirty
,
3466 struct btrfs_block_group_cache
,
3469 * this can happen if something re-dirties a block
3470 * group that is already under IO. Just wait for it to
3471 * finish and then do it all again
3473 if (!list_empty(&cache
->io_list
)) {
3474 list_del_init(&cache
->io_list
);
3475 btrfs_wait_cache_io(root
, trans
, cache
,
3476 &cache
->io_ctl
, path
,
3477 cache
->key
.objectid
);
3478 btrfs_put_block_group(cache
);
3483 * btrfs_wait_cache_io uses the cache->dirty_list to decide
3484 * if it should update the cache_state. Don't delete
3485 * until after we wait.
3487 * Since we're not running in the commit critical section
3488 * we need the dirty_bgs_lock to protect from update_block_group
3490 spin_lock(&cur_trans
->dirty_bgs_lock
);
3491 list_del_init(&cache
->dirty_list
);
3492 spin_unlock(&cur_trans
->dirty_bgs_lock
);
3496 cache_save_setup(cache
, trans
, path
);
3498 if (cache
->disk_cache_state
== BTRFS_DC_SETUP
) {
3499 cache
->io_ctl
.inode
= NULL
;
3500 ret
= btrfs_write_out_cache(root
, trans
, cache
, path
);
3501 if (ret
== 0 && cache
->io_ctl
.inode
) {
3506 * the cache_write_mutex is protecting
3509 list_add_tail(&cache
->io_list
, io
);
3512 * if we failed to write the cache, the
3513 * generation will be bad and life goes on
3519 ret
= write_one_cache_group(trans
, root
, path
, cache
);
3521 * Our block group might still be attached to the list
3522 * of new block groups in the transaction handle of some
3523 * other task (struct btrfs_trans_handle->new_bgs). This
3524 * means its block group item isn't yet in the extent
3525 * tree. If this happens ignore the error, as we will
3526 * try again later in the critical section of the
3527 * transaction commit.
3529 if (ret
== -ENOENT
) {
3531 spin_lock(&cur_trans
->dirty_bgs_lock
);
3532 if (list_empty(&cache
->dirty_list
)) {
3533 list_add_tail(&cache
->dirty_list
,
3534 &cur_trans
->dirty_bgs
);
3535 btrfs_get_block_group(cache
);
3537 spin_unlock(&cur_trans
->dirty_bgs_lock
);
3539 btrfs_abort_transaction(trans
, root
, ret
);
3543 /* if its not on the io list, we need to put the block group */
3545 btrfs_put_block_group(cache
);
3551 * Avoid blocking other tasks for too long. It might even save
3552 * us from writing caches for block groups that are going to be
3555 mutex_unlock(&trans
->transaction
->cache_write_mutex
);
3556 mutex_lock(&trans
->transaction
->cache_write_mutex
);
3558 mutex_unlock(&trans
->transaction
->cache_write_mutex
);
3561 * go through delayed refs for all the stuff we've just kicked off
3562 * and then loop back (just once)
3564 ret
= btrfs_run_delayed_refs(trans
, root
, 0);
3565 if (!ret
&& loops
== 0) {
3567 spin_lock(&cur_trans
->dirty_bgs_lock
);
3568 list_splice_init(&cur_trans
->dirty_bgs
, &dirty
);
3570 * dirty_bgs_lock protects us from concurrent block group
3571 * deletes too (not just cache_write_mutex).
3573 if (!list_empty(&dirty
)) {
3574 spin_unlock(&cur_trans
->dirty_bgs_lock
);
3577 spin_unlock(&cur_trans
->dirty_bgs_lock
);
3580 btrfs_free_path(path
);
3584 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle
*trans
,
3585 struct btrfs_root
*root
)
3587 struct btrfs_block_group_cache
*cache
;
3588 struct btrfs_transaction
*cur_trans
= trans
->transaction
;
3591 struct btrfs_path
*path
;
3592 struct list_head
*io
= &cur_trans
->io_bgs
;
3593 int num_started
= 0;
3595 path
= btrfs_alloc_path();
3600 * We don't need the lock here since we are protected by the transaction
3601 * commit. We want to do the cache_save_setup first and then run the
3602 * delayed refs to make sure we have the best chance at doing this all
3605 while (!list_empty(&cur_trans
->dirty_bgs
)) {
3606 cache
= list_first_entry(&cur_trans
->dirty_bgs
,
3607 struct btrfs_block_group_cache
,
3611 * this can happen if cache_save_setup re-dirties a block
3612 * group that is already under IO. Just wait for it to
3613 * finish and then do it all again
3615 if (!list_empty(&cache
->io_list
)) {
3616 list_del_init(&cache
->io_list
);
3617 btrfs_wait_cache_io(root
, trans
, cache
,
3618 &cache
->io_ctl
, path
,
3619 cache
->key
.objectid
);
3620 btrfs_put_block_group(cache
);
3624 * don't remove from the dirty list until after we've waited
3627 list_del_init(&cache
->dirty_list
);
3630 cache_save_setup(cache
, trans
, path
);
3633 ret
= btrfs_run_delayed_refs(trans
, root
, (unsigned long) -1);
3635 if (!ret
&& cache
->disk_cache_state
== BTRFS_DC_SETUP
) {
3636 cache
->io_ctl
.inode
= NULL
;
3637 ret
= btrfs_write_out_cache(root
, trans
, cache
, path
);
3638 if (ret
== 0 && cache
->io_ctl
.inode
) {
3641 list_add_tail(&cache
->io_list
, io
);
3644 * if we failed to write the cache, the
3645 * generation will be bad and life goes on
3651 ret
= write_one_cache_group(trans
, root
, path
, cache
);
3653 btrfs_abort_transaction(trans
, root
, ret
);
3656 /* if its not on the io list, we need to put the block group */
3658 btrfs_put_block_group(cache
);
3661 while (!list_empty(io
)) {
3662 cache
= list_first_entry(io
, struct btrfs_block_group_cache
,
3664 list_del_init(&cache
->io_list
);
3665 btrfs_wait_cache_io(root
, trans
, cache
,
3666 &cache
->io_ctl
, path
, cache
->key
.objectid
);
3667 btrfs_put_block_group(cache
);
3670 btrfs_free_path(path
);
3674 int btrfs_extent_readonly(struct btrfs_root
*root
, u64 bytenr
)
3676 struct btrfs_block_group_cache
*block_group
;
3679 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
3680 if (!block_group
|| block_group
->ro
)
3683 btrfs_put_block_group(block_group
);
3687 static const char *alloc_name(u64 flags
)
3690 case BTRFS_BLOCK_GROUP_METADATA
|BTRFS_BLOCK_GROUP_DATA
:
3692 case BTRFS_BLOCK_GROUP_METADATA
:
3694 case BTRFS_BLOCK_GROUP_DATA
:
3696 case BTRFS_BLOCK_GROUP_SYSTEM
:
3700 return "invalid-combination";
3704 static int update_space_info(struct btrfs_fs_info
*info
, u64 flags
,
3705 u64 total_bytes
, u64 bytes_used
,
3706 struct btrfs_space_info
**space_info
)
3708 struct btrfs_space_info
*found
;
3713 if (flags
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
3714 BTRFS_BLOCK_GROUP_RAID10
))
3719 found
= __find_space_info(info
, flags
);
3721 spin_lock(&found
->lock
);
3722 found
->total_bytes
+= total_bytes
;
3723 found
->disk_total
+= total_bytes
* factor
;
3724 found
->bytes_used
+= bytes_used
;
3725 found
->disk_used
+= bytes_used
* factor
;
3726 if (total_bytes
> 0)
3728 spin_unlock(&found
->lock
);
3729 *space_info
= found
;
3732 found
= kzalloc(sizeof(*found
), GFP_NOFS
);
3736 ret
= percpu_counter_init(&found
->total_bytes_pinned
, 0, GFP_KERNEL
);
3742 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++)
3743 INIT_LIST_HEAD(&found
->block_groups
[i
]);
3744 init_rwsem(&found
->groups_sem
);
3745 spin_lock_init(&found
->lock
);
3746 found
->flags
= flags
& BTRFS_BLOCK_GROUP_TYPE_MASK
;
3747 found
->total_bytes
= total_bytes
;
3748 found
->disk_total
= total_bytes
* factor
;
3749 found
->bytes_used
= bytes_used
;
3750 found
->disk_used
= bytes_used
* factor
;
3751 found
->bytes_pinned
= 0;
3752 found
->bytes_reserved
= 0;
3753 found
->bytes_readonly
= 0;
3754 found
->bytes_may_use
= 0;
3756 found
->force_alloc
= CHUNK_ALLOC_NO_FORCE
;
3757 found
->chunk_alloc
= 0;
3759 init_waitqueue_head(&found
->wait
);
3760 INIT_LIST_HEAD(&found
->ro_bgs
);
3762 ret
= kobject_init_and_add(&found
->kobj
, &space_info_ktype
,
3763 info
->space_info_kobj
, "%s",
3764 alloc_name(found
->flags
));
3770 *space_info
= found
;
3771 list_add_rcu(&found
->list
, &info
->space_info
);
3772 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3773 info
->data_sinfo
= found
;
3778 static void set_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
3780 u64 extra_flags
= chunk_to_extended(flags
) &
3781 BTRFS_EXTENDED_PROFILE_MASK
;
3783 write_seqlock(&fs_info
->profiles_lock
);
3784 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3785 fs_info
->avail_data_alloc_bits
|= extra_flags
;
3786 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
3787 fs_info
->avail_metadata_alloc_bits
|= extra_flags
;
3788 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
3789 fs_info
->avail_system_alloc_bits
|= extra_flags
;
3790 write_sequnlock(&fs_info
->profiles_lock
);
3794 * returns target flags in extended format or 0 if restripe for this
3795 * chunk_type is not in progress
3797 * should be called with either volume_mutex or balance_lock held
3799 static u64
get_restripe_target(struct btrfs_fs_info
*fs_info
, u64 flags
)
3801 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3807 if (flags
& BTRFS_BLOCK_GROUP_DATA
&&
3808 bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3809 target
= BTRFS_BLOCK_GROUP_DATA
| bctl
->data
.target
;
3810 } else if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
&&
3811 bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3812 target
= BTRFS_BLOCK_GROUP_SYSTEM
| bctl
->sys
.target
;
3813 } else if (flags
& BTRFS_BLOCK_GROUP_METADATA
&&
3814 bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3815 target
= BTRFS_BLOCK_GROUP_METADATA
| bctl
->meta
.target
;
3822 * @flags: available profiles in extended format (see ctree.h)
3824 * Returns reduced profile in chunk format. If profile changing is in
3825 * progress (either running or paused) picks the target profile (if it's
3826 * already available), otherwise falls back to plain reducing.
3828 static u64
btrfs_reduce_alloc_profile(struct btrfs_root
*root
, u64 flags
)
3830 u64 num_devices
= root
->fs_info
->fs_devices
->rw_devices
;
3835 * see if restripe for this chunk_type is in progress, if so
3836 * try to reduce to the target profile
3838 spin_lock(&root
->fs_info
->balance_lock
);
3839 target
= get_restripe_target(root
->fs_info
, flags
);
3841 /* pick target profile only if it's already available */
3842 if ((flags
& target
) & BTRFS_EXTENDED_PROFILE_MASK
) {
3843 spin_unlock(&root
->fs_info
->balance_lock
);
3844 return extended_to_chunk(target
);
3847 spin_unlock(&root
->fs_info
->balance_lock
);
3849 /* First, mask out the RAID levels which aren't possible */
3850 if (num_devices
== 1)
3851 flags
&= ~(BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID0
|
3852 BTRFS_BLOCK_GROUP_RAID5
);
3853 if (num_devices
< 3)
3854 flags
&= ~BTRFS_BLOCK_GROUP_RAID6
;
3855 if (num_devices
< 4)
3856 flags
&= ~BTRFS_BLOCK_GROUP_RAID10
;
3858 tmp
= flags
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID0
|
3859 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID5
|
3860 BTRFS_BLOCK_GROUP_RAID6
| BTRFS_BLOCK_GROUP_RAID10
);
3863 if (tmp
& BTRFS_BLOCK_GROUP_RAID6
)
3864 tmp
= BTRFS_BLOCK_GROUP_RAID6
;
3865 else if (tmp
& BTRFS_BLOCK_GROUP_RAID5
)
3866 tmp
= BTRFS_BLOCK_GROUP_RAID5
;
3867 else if (tmp
& BTRFS_BLOCK_GROUP_RAID10
)
3868 tmp
= BTRFS_BLOCK_GROUP_RAID10
;
3869 else if (tmp
& BTRFS_BLOCK_GROUP_RAID1
)
3870 tmp
= BTRFS_BLOCK_GROUP_RAID1
;
3871 else if (tmp
& BTRFS_BLOCK_GROUP_RAID0
)
3872 tmp
= BTRFS_BLOCK_GROUP_RAID0
;
3874 return extended_to_chunk(flags
| tmp
);
3877 static u64
get_alloc_profile(struct btrfs_root
*root
, u64 orig_flags
)
3884 seq
= read_seqbegin(&root
->fs_info
->profiles_lock
);
3886 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3887 flags
|= root
->fs_info
->avail_data_alloc_bits
;
3888 else if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
3889 flags
|= root
->fs_info
->avail_system_alloc_bits
;
3890 else if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
3891 flags
|= root
->fs_info
->avail_metadata_alloc_bits
;
3892 } while (read_seqretry(&root
->fs_info
->profiles_lock
, seq
));
3894 return btrfs_reduce_alloc_profile(root
, flags
);
3897 u64
btrfs_get_alloc_profile(struct btrfs_root
*root
, int data
)
3903 flags
= BTRFS_BLOCK_GROUP_DATA
;
3904 else if (root
== root
->fs_info
->chunk_root
)
3905 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
3907 flags
= BTRFS_BLOCK_GROUP_METADATA
;
3909 ret
= get_alloc_profile(root
, flags
);
3914 * This will check the space that the inode allocates from to make sure we have
3915 * enough space for bytes.
3917 int btrfs_check_data_free_space(struct inode
*inode
, u64 bytes
, u64 write_bytes
)
3919 struct btrfs_space_info
*data_sinfo
;
3920 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3921 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3924 int need_commit
= 2;
3925 int have_pinned_space
;
3927 /* make sure bytes are sectorsize aligned */
3928 bytes
= ALIGN(bytes
, root
->sectorsize
);
3930 if (btrfs_is_free_space_inode(inode
)) {
3932 ASSERT(current
->journal_info
);
3935 data_sinfo
= fs_info
->data_sinfo
;
3940 /* make sure we have enough space to handle the data first */
3941 spin_lock(&data_sinfo
->lock
);
3942 used
= data_sinfo
->bytes_used
+ data_sinfo
->bytes_reserved
+
3943 data_sinfo
->bytes_pinned
+ data_sinfo
->bytes_readonly
+
3944 data_sinfo
->bytes_may_use
;
3946 if (used
+ bytes
> data_sinfo
->total_bytes
) {
3947 struct btrfs_trans_handle
*trans
;
3950 * if we don't have enough free bytes in this space then we need
3951 * to alloc a new chunk.
3953 if (!data_sinfo
->full
) {
3956 data_sinfo
->force_alloc
= CHUNK_ALLOC_FORCE
;
3957 spin_unlock(&data_sinfo
->lock
);
3959 alloc_target
= btrfs_get_alloc_profile(root
, 1);
3961 * It is ugly that we don't call nolock join
3962 * transaction for the free space inode case here.
3963 * But it is safe because we only do the data space
3964 * reservation for the free space cache in the
3965 * transaction context, the common join transaction
3966 * just increase the counter of the current transaction
3967 * handler, doesn't try to acquire the trans_lock of
3970 trans
= btrfs_join_transaction(root
);
3972 return PTR_ERR(trans
);
3974 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
3976 CHUNK_ALLOC_NO_FORCE
);
3977 btrfs_end_transaction(trans
, root
);
3982 have_pinned_space
= 1;
3988 data_sinfo
= fs_info
->data_sinfo
;
3994 * If we don't have enough pinned space to deal with this
3995 * allocation, and no removed chunk in current transaction,
3996 * don't bother committing the transaction.
3998 have_pinned_space
= percpu_counter_compare(
3999 &data_sinfo
->total_bytes_pinned
,
4000 used
+ bytes
- data_sinfo
->total_bytes
);
4001 spin_unlock(&data_sinfo
->lock
);
4003 /* commit the current transaction and try again */
4006 !atomic_read(&root
->fs_info
->open_ioctl_trans
)) {
4009 if (need_commit
> 0)
4010 btrfs_wait_ordered_roots(fs_info
, -1);
4012 trans
= btrfs_join_transaction(root
);
4014 return PTR_ERR(trans
);
4015 if (have_pinned_space
>= 0 ||
4016 trans
->transaction
->have_free_bgs
||
4018 ret
= btrfs_commit_transaction(trans
, root
);
4022 * make sure that all running delayed iput are
4025 down_write(&root
->fs_info
->delayed_iput_sem
);
4026 up_write(&root
->fs_info
->delayed_iput_sem
);
4029 btrfs_end_transaction(trans
, root
);
4033 trace_btrfs_space_reservation(root
->fs_info
,
4034 "space_info:enospc",
4035 data_sinfo
->flags
, bytes
, 1);
4038 ret
= btrfs_qgroup_reserve(root
, write_bytes
);
4041 data_sinfo
->bytes_may_use
+= bytes
;
4042 trace_btrfs_space_reservation(root
->fs_info
, "space_info",
4043 data_sinfo
->flags
, bytes
, 1);
4045 spin_unlock(&data_sinfo
->lock
);
4051 * Called if we need to clear a data reservation for this inode.
4053 void btrfs_free_reserved_data_space(struct inode
*inode
, u64 bytes
)
4055 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4056 struct btrfs_space_info
*data_sinfo
;
4058 /* make sure bytes are sectorsize aligned */
4059 bytes
= ALIGN(bytes
, root
->sectorsize
);
4061 data_sinfo
= root
->fs_info
->data_sinfo
;
4062 spin_lock(&data_sinfo
->lock
);
4063 WARN_ON(data_sinfo
->bytes_may_use
< bytes
);
4064 data_sinfo
->bytes_may_use
-= bytes
;
4065 trace_btrfs_space_reservation(root
->fs_info
, "space_info",
4066 data_sinfo
->flags
, bytes
, 0);
4067 spin_unlock(&data_sinfo
->lock
);
4070 static void force_metadata_allocation(struct btrfs_fs_info
*info
)
4072 struct list_head
*head
= &info
->space_info
;
4073 struct btrfs_space_info
*found
;
4076 list_for_each_entry_rcu(found
, head
, list
) {
4077 if (found
->flags
& BTRFS_BLOCK_GROUP_METADATA
)
4078 found
->force_alloc
= CHUNK_ALLOC_FORCE
;
4083 static inline u64
calc_global_rsv_need_space(struct btrfs_block_rsv
*global
)
4085 return (global
->size
<< 1);
4088 static int should_alloc_chunk(struct btrfs_root
*root
,
4089 struct btrfs_space_info
*sinfo
, int force
)
4091 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
4092 u64 num_bytes
= sinfo
->total_bytes
- sinfo
->bytes_readonly
;
4093 u64 num_allocated
= sinfo
->bytes_used
+ sinfo
->bytes_reserved
;
4096 if (force
== CHUNK_ALLOC_FORCE
)
4100 * We need to take into account the global rsv because for all intents
4101 * and purposes it's used space. Don't worry about locking the
4102 * global_rsv, it doesn't change except when the transaction commits.
4104 if (sinfo
->flags
& BTRFS_BLOCK_GROUP_METADATA
)
4105 num_allocated
+= calc_global_rsv_need_space(global_rsv
);
4108 * in limited mode, we want to have some free space up to
4109 * about 1% of the FS size.
4111 if (force
== CHUNK_ALLOC_LIMITED
) {
4112 thresh
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
4113 thresh
= max_t(u64
, 64 * 1024 * 1024,
4114 div_factor_fine(thresh
, 1));
4116 if (num_bytes
- num_allocated
< thresh
)
4120 if (num_allocated
+ 2 * 1024 * 1024 < div_factor(num_bytes
, 8))
4125 static u64
get_profile_num_devs(struct btrfs_root
*root
, u64 type
)
4129 if (type
& (BTRFS_BLOCK_GROUP_RAID10
|
4130 BTRFS_BLOCK_GROUP_RAID0
|
4131 BTRFS_BLOCK_GROUP_RAID5
|
4132 BTRFS_BLOCK_GROUP_RAID6
))
4133 num_dev
= root
->fs_info
->fs_devices
->rw_devices
;
4134 else if (type
& BTRFS_BLOCK_GROUP_RAID1
)
4137 num_dev
= 1; /* DUP or single */
4143 * If @is_allocation is true, reserve space in the system space info necessary
4144 * for allocating a chunk, otherwise if it's false, reserve space necessary for
4147 void check_system_chunk(struct btrfs_trans_handle
*trans
,
4148 struct btrfs_root
*root
,
4151 struct btrfs_space_info
*info
;
4158 * Needed because we can end up allocating a system chunk and for an
4159 * atomic and race free space reservation in the chunk block reserve.
4161 ASSERT(mutex_is_locked(&root
->fs_info
->chunk_mutex
));
4163 info
= __find_space_info(root
->fs_info
, BTRFS_BLOCK_GROUP_SYSTEM
);
4164 spin_lock(&info
->lock
);
4165 left
= info
->total_bytes
- info
->bytes_used
- info
->bytes_pinned
-
4166 info
->bytes_reserved
- info
->bytes_readonly
-
4167 info
->bytes_may_use
;
4168 spin_unlock(&info
->lock
);
4170 num_devs
= get_profile_num_devs(root
, type
);
4172 /* num_devs device items to update and 1 chunk item to add or remove */
4173 thresh
= btrfs_calc_trunc_metadata_size(root
, num_devs
) +
4174 btrfs_calc_trans_metadata_size(root
, 1);
4176 if (left
< thresh
&& btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
4177 btrfs_info(root
->fs_info
, "left=%llu, need=%llu, flags=%llu",
4178 left
, thresh
, type
);
4179 dump_space_info(info
, 0, 0);
4182 if (left
< thresh
) {
4185 flags
= btrfs_get_alloc_profile(root
->fs_info
->chunk_root
, 0);
4187 * Ignore failure to create system chunk. We might end up not
4188 * needing it, as we might not need to COW all nodes/leafs from
4189 * the paths we visit in the chunk tree (they were already COWed
4190 * or created in the current transaction for example).
4192 ret
= btrfs_alloc_chunk(trans
, root
, flags
);
4196 ret
= btrfs_block_rsv_add(root
->fs_info
->chunk_root
,
4197 &root
->fs_info
->chunk_block_rsv
,
4198 thresh
, BTRFS_RESERVE_NO_FLUSH
);
4200 trans
->chunk_bytes_reserved
+= thresh
;
4204 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
4205 struct btrfs_root
*extent_root
, u64 flags
, int force
)
4207 struct btrfs_space_info
*space_info
;
4208 struct btrfs_fs_info
*fs_info
= extent_root
->fs_info
;
4209 int wait_for_alloc
= 0;
4212 /* Don't re-enter if we're already allocating a chunk */
4213 if (trans
->allocating_chunk
)
4216 space_info
= __find_space_info(extent_root
->fs_info
, flags
);
4218 ret
= update_space_info(extent_root
->fs_info
, flags
,
4220 BUG_ON(ret
); /* -ENOMEM */
4222 BUG_ON(!space_info
); /* Logic error */
4225 spin_lock(&space_info
->lock
);
4226 if (force
< space_info
->force_alloc
)
4227 force
= space_info
->force_alloc
;
4228 if (space_info
->full
) {
4229 if (should_alloc_chunk(extent_root
, space_info
, force
))
4233 spin_unlock(&space_info
->lock
);
4237 if (!should_alloc_chunk(extent_root
, space_info
, force
)) {
4238 spin_unlock(&space_info
->lock
);
4240 } else if (space_info
->chunk_alloc
) {
4243 space_info
->chunk_alloc
= 1;
4246 spin_unlock(&space_info
->lock
);
4248 mutex_lock(&fs_info
->chunk_mutex
);
4251 * The chunk_mutex is held throughout the entirety of a chunk
4252 * allocation, so once we've acquired the chunk_mutex we know that the
4253 * other guy is done and we need to recheck and see if we should
4256 if (wait_for_alloc
) {
4257 mutex_unlock(&fs_info
->chunk_mutex
);
4262 trans
->allocating_chunk
= true;
4265 * If we have mixed data/metadata chunks we want to make sure we keep
4266 * allocating mixed chunks instead of individual chunks.
4268 if (btrfs_mixed_space_info(space_info
))
4269 flags
|= (BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
);
4272 * if we're doing a data chunk, go ahead and make sure that
4273 * we keep a reasonable number of metadata chunks allocated in the
4276 if (flags
& BTRFS_BLOCK_GROUP_DATA
&& fs_info
->metadata_ratio
) {
4277 fs_info
->data_chunk_allocations
++;
4278 if (!(fs_info
->data_chunk_allocations
%
4279 fs_info
->metadata_ratio
))
4280 force_metadata_allocation(fs_info
);
4284 * Check if we have enough space in SYSTEM chunk because we may need
4285 * to update devices.
4287 check_system_chunk(trans
, extent_root
, flags
);
4289 ret
= btrfs_alloc_chunk(trans
, extent_root
, flags
);
4290 trans
->allocating_chunk
= false;
4292 spin_lock(&space_info
->lock
);
4293 if (ret
< 0 && ret
!= -ENOSPC
)
4296 space_info
->full
= 1;
4300 space_info
->force_alloc
= CHUNK_ALLOC_NO_FORCE
;
4302 space_info
->chunk_alloc
= 0;
4303 spin_unlock(&space_info
->lock
);
4304 mutex_unlock(&fs_info
->chunk_mutex
);
4306 * When we allocate a new chunk we reserve space in the chunk block
4307 * reserve to make sure we can COW nodes/leafs in the chunk tree or
4308 * add new nodes/leafs to it if we end up needing to do it when
4309 * inserting the chunk item and updating device items as part of the
4310 * second phase of chunk allocation, performed by
4311 * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a
4312 * large number of new block groups to create in our transaction
4313 * handle's new_bgs list to avoid exhausting the chunk block reserve
4314 * in extreme cases - like having a single transaction create many new
4315 * block groups when starting to write out the free space caches of all
4316 * the block groups that were made dirty during the lifetime of the
4319 if (trans
->can_flush_pending_bgs
&&
4320 trans
->chunk_bytes_reserved
>= (2 * 1024 * 1024ull)) {
4321 btrfs_create_pending_block_groups(trans
, trans
->root
);
4322 btrfs_trans_release_chunk_metadata(trans
);
4327 static int can_overcommit(struct btrfs_root
*root
,
4328 struct btrfs_space_info
*space_info
, u64 bytes
,
4329 enum btrfs_reserve_flush_enum flush
)
4331 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
4332 u64 profile
= btrfs_get_alloc_profile(root
, 0);
4337 used
= space_info
->bytes_used
+ space_info
->bytes_reserved
+
4338 space_info
->bytes_pinned
+ space_info
->bytes_readonly
;
4341 * We only want to allow over committing if we have lots of actual space
4342 * free, but if we don't have enough space to handle the global reserve
4343 * space then we could end up having a real enospc problem when trying
4344 * to allocate a chunk or some other such important allocation.
4346 spin_lock(&global_rsv
->lock
);
4347 space_size
= calc_global_rsv_need_space(global_rsv
);
4348 spin_unlock(&global_rsv
->lock
);
4349 if (used
+ space_size
>= space_info
->total_bytes
)
4352 used
+= space_info
->bytes_may_use
;
4354 spin_lock(&root
->fs_info
->free_chunk_lock
);
4355 avail
= root
->fs_info
->free_chunk_space
;
4356 spin_unlock(&root
->fs_info
->free_chunk_lock
);
4359 * If we have dup, raid1 or raid10 then only half of the free
4360 * space is actually useable. For raid56, the space info used
4361 * doesn't include the parity drive, so we don't have to
4364 if (profile
& (BTRFS_BLOCK_GROUP_DUP
|
4365 BTRFS_BLOCK_GROUP_RAID1
|
4366 BTRFS_BLOCK_GROUP_RAID10
))
4370 * If we aren't flushing all things, let us overcommit up to
4371 * 1/2th of the space. If we can flush, don't let us overcommit
4372 * too much, let it overcommit up to 1/8 of the space.
4374 if (flush
== BTRFS_RESERVE_FLUSH_ALL
)
4379 if (used
+ bytes
< space_info
->total_bytes
+ avail
)
4384 static void btrfs_writeback_inodes_sb_nr(struct btrfs_root
*root
,
4385 unsigned long nr_pages
, int nr_items
)
4387 struct super_block
*sb
= root
->fs_info
->sb
;
4389 if (down_read_trylock(&sb
->s_umount
)) {
4390 writeback_inodes_sb_nr(sb
, nr_pages
, WB_REASON_FS_FREE_SPACE
);
4391 up_read(&sb
->s_umount
);
4394 * We needn't worry the filesystem going from r/w to r/o though
4395 * we don't acquire ->s_umount mutex, because the filesystem
4396 * should guarantee the delalloc inodes list be empty after
4397 * the filesystem is readonly(all dirty pages are written to
4400 btrfs_start_delalloc_roots(root
->fs_info
, 0, nr_items
);
4401 if (!current
->journal_info
)
4402 btrfs_wait_ordered_roots(root
->fs_info
, nr_items
);
4406 static inline int calc_reclaim_items_nr(struct btrfs_root
*root
, u64 to_reclaim
)
4411 bytes
= btrfs_calc_trans_metadata_size(root
, 1);
4412 nr
= (int)div64_u64(to_reclaim
, bytes
);
4418 #define EXTENT_SIZE_PER_ITEM (256 * 1024)
4421 * shrink metadata reservation for delalloc
4423 static void shrink_delalloc(struct btrfs_root
*root
, u64 to_reclaim
, u64 orig
,
4426 struct btrfs_block_rsv
*block_rsv
;
4427 struct btrfs_space_info
*space_info
;
4428 struct btrfs_trans_handle
*trans
;
4432 unsigned long nr_pages
;
4435 enum btrfs_reserve_flush_enum flush
;
4437 /* Calc the number of the pages we need flush for space reservation */
4438 items
= calc_reclaim_items_nr(root
, to_reclaim
);
4439 to_reclaim
= items
* EXTENT_SIZE_PER_ITEM
;
4441 trans
= (struct btrfs_trans_handle
*)current
->journal_info
;
4442 block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
4443 space_info
= block_rsv
->space_info
;
4445 delalloc_bytes
= percpu_counter_sum_positive(
4446 &root
->fs_info
->delalloc_bytes
);
4447 if (delalloc_bytes
== 0) {
4451 btrfs_wait_ordered_roots(root
->fs_info
, items
);
4456 while (delalloc_bytes
&& loops
< 3) {
4457 max_reclaim
= min(delalloc_bytes
, to_reclaim
);
4458 nr_pages
= max_reclaim
>> PAGE_CACHE_SHIFT
;
4459 btrfs_writeback_inodes_sb_nr(root
, nr_pages
, items
);
4461 * We need to wait for the async pages to actually start before
4464 max_reclaim
= atomic_read(&root
->fs_info
->async_delalloc_pages
);
4468 if (max_reclaim
<= nr_pages
)
4471 max_reclaim
-= nr_pages
;
4473 wait_event(root
->fs_info
->async_submit_wait
,
4474 atomic_read(&root
->fs_info
->async_delalloc_pages
) <=
4478 flush
= BTRFS_RESERVE_FLUSH_ALL
;
4480 flush
= BTRFS_RESERVE_NO_FLUSH
;
4481 spin_lock(&space_info
->lock
);
4482 if (can_overcommit(root
, space_info
, orig
, flush
)) {
4483 spin_unlock(&space_info
->lock
);
4486 spin_unlock(&space_info
->lock
);
4489 if (wait_ordered
&& !trans
) {
4490 btrfs_wait_ordered_roots(root
->fs_info
, items
);
4492 time_left
= schedule_timeout_killable(1);
4496 delalloc_bytes
= percpu_counter_sum_positive(
4497 &root
->fs_info
->delalloc_bytes
);
4502 * maybe_commit_transaction - possibly commit the transaction if its ok to
4503 * @root - the root we're allocating for
4504 * @bytes - the number of bytes we want to reserve
4505 * @force - force the commit
4507 * This will check to make sure that committing the transaction will actually
4508 * get us somewhere and then commit the transaction if it does. Otherwise it
4509 * will return -ENOSPC.
4511 static int may_commit_transaction(struct btrfs_root
*root
,
4512 struct btrfs_space_info
*space_info
,
4513 u64 bytes
, int force
)
4515 struct btrfs_block_rsv
*delayed_rsv
= &root
->fs_info
->delayed_block_rsv
;
4516 struct btrfs_trans_handle
*trans
;
4518 trans
= (struct btrfs_trans_handle
*)current
->journal_info
;
4525 /* See if there is enough pinned space to make this reservation */
4526 if (percpu_counter_compare(&space_info
->total_bytes_pinned
,
4531 * See if there is some space in the delayed insertion reservation for
4534 if (space_info
!= delayed_rsv
->space_info
)
4537 spin_lock(&delayed_rsv
->lock
);
4538 if (percpu_counter_compare(&space_info
->total_bytes_pinned
,
4539 bytes
- delayed_rsv
->size
) >= 0) {
4540 spin_unlock(&delayed_rsv
->lock
);
4543 spin_unlock(&delayed_rsv
->lock
);
4546 trans
= btrfs_join_transaction(root
);
4550 return btrfs_commit_transaction(trans
, root
);
4554 FLUSH_DELAYED_ITEMS_NR
= 1,
4555 FLUSH_DELAYED_ITEMS
= 2,
4557 FLUSH_DELALLOC_WAIT
= 4,
4562 static int flush_space(struct btrfs_root
*root
,
4563 struct btrfs_space_info
*space_info
, u64 num_bytes
,
4564 u64 orig_bytes
, int state
)
4566 struct btrfs_trans_handle
*trans
;
4571 case FLUSH_DELAYED_ITEMS_NR
:
4572 case FLUSH_DELAYED_ITEMS
:
4573 if (state
== FLUSH_DELAYED_ITEMS_NR
)
4574 nr
= calc_reclaim_items_nr(root
, num_bytes
) * 2;
4578 trans
= btrfs_join_transaction(root
);
4579 if (IS_ERR(trans
)) {
4580 ret
= PTR_ERR(trans
);
4583 ret
= btrfs_run_delayed_items_nr(trans
, root
, nr
);
4584 btrfs_end_transaction(trans
, root
);
4586 case FLUSH_DELALLOC
:
4587 case FLUSH_DELALLOC_WAIT
:
4588 shrink_delalloc(root
, num_bytes
* 2, orig_bytes
,
4589 state
== FLUSH_DELALLOC_WAIT
);
4592 trans
= btrfs_join_transaction(root
);
4593 if (IS_ERR(trans
)) {
4594 ret
= PTR_ERR(trans
);
4597 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
4598 btrfs_get_alloc_profile(root
, 0),
4599 CHUNK_ALLOC_NO_FORCE
);
4600 btrfs_end_transaction(trans
, root
);
4605 ret
= may_commit_transaction(root
, space_info
, orig_bytes
, 0);
4616 btrfs_calc_reclaim_metadata_size(struct btrfs_root
*root
,
4617 struct btrfs_space_info
*space_info
)
4623 to_reclaim
= min_t(u64
, num_online_cpus() * 1024 * 1024,
4625 spin_lock(&space_info
->lock
);
4626 if (can_overcommit(root
, space_info
, to_reclaim
,
4627 BTRFS_RESERVE_FLUSH_ALL
)) {
4632 used
= space_info
->bytes_used
+ space_info
->bytes_reserved
+
4633 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
4634 space_info
->bytes_may_use
;
4635 if (can_overcommit(root
, space_info
, 1024 * 1024,
4636 BTRFS_RESERVE_FLUSH_ALL
))
4637 expected
= div_factor_fine(space_info
->total_bytes
, 95);
4639 expected
= div_factor_fine(space_info
->total_bytes
, 90);
4641 if (used
> expected
)
4642 to_reclaim
= used
- expected
;
4645 to_reclaim
= min(to_reclaim
, space_info
->bytes_may_use
+
4646 space_info
->bytes_reserved
);
4648 spin_unlock(&space_info
->lock
);
4653 static inline int need_do_async_reclaim(struct btrfs_space_info
*space_info
,
4654 struct btrfs_fs_info
*fs_info
, u64 used
)
4656 u64 thresh
= div_factor_fine(space_info
->total_bytes
, 98);
4658 /* If we're just plain full then async reclaim just slows us down. */
4659 if (space_info
->bytes_used
>= thresh
)
4662 return (used
>= thresh
&& !btrfs_fs_closing(fs_info
) &&
4663 !test_bit(BTRFS_FS_STATE_REMOUNTING
, &fs_info
->fs_state
));
4666 static int btrfs_need_do_async_reclaim(struct btrfs_space_info
*space_info
,
4667 struct btrfs_fs_info
*fs_info
,
4672 spin_lock(&space_info
->lock
);
4674 * We run out of space and have not got any free space via flush_space,
4675 * so don't bother doing async reclaim.
4677 if (flush_state
> COMMIT_TRANS
&& space_info
->full
) {
4678 spin_unlock(&space_info
->lock
);
4682 used
= space_info
->bytes_used
+ space_info
->bytes_reserved
+
4683 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
4684 space_info
->bytes_may_use
;
4685 if (need_do_async_reclaim(space_info
, fs_info
, used
)) {
4686 spin_unlock(&space_info
->lock
);
4689 spin_unlock(&space_info
->lock
);
4694 static void btrfs_async_reclaim_metadata_space(struct work_struct
*work
)
4696 struct btrfs_fs_info
*fs_info
;
4697 struct btrfs_space_info
*space_info
;
4701 fs_info
= container_of(work
, struct btrfs_fs_info
, async_reclaim_work
);
4702 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
4704 to_reclaim
= btrfs_calc_reclaim_metadata_size(fs_info
->fs_root
,
4709 flush_state
= FLUSH_DELAYED_ITEMS_NR
;
4711 flush_space(fs_info
->fs_root
, space_info
, to_reclaim
,
4712 to_reclaim
, flush_state
);
4714 if (!btrfs_need_do_async_reclaim(space_info
, fs_info
,
4717 } while (flush_state
< COMMIT_TRANS
);
4720 void btrfs_init_async_reclaim_work(struct work_struct
*work
)
4722 INIT_WORK(work
, btrfs_async_reclaim_metadata_space
);
4726 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
4727 * @root - the root we're allocating for
4728 * @block_rsv - the block_rsv we're allocating for
4729 * @orig_bytes - the number of bytes we want
4730 * @flush - whether or not we can flush to make our reservation
4732 * This will reserve orgi_bytes number of bytes from the space info associated
4733 * with the block_rsv. If there is not enough space it will make an attempt to
4734 * flush out space to make room. It will do this by flushing delalloc if
4735 * possible or committing the transaction. If flush is 0 then no attempts to
4736 * regain reservations will be made and this will fail if there is not enough
4739 static int reserve_metadata_bytes(struct btrfs_root
*root
,
4740 struct btrfs_block_rsv
*block_rsv
,
4742 enum btrfs_reserve_flush_enum flush
)
4744 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
4746 u64 num_bytes
= orig_bytes
;
4747 int flush_state
= FLUSH_DELAYED_ITEMS_NR
;
4749 bool flushing
= false;
4753 spin_lock(&space_info
->lock
);
4755 * We only want to wait if somebody other than us is flushing and we
4756 * are actually allowed to flush all things.
4758 while (flush
== BTRFS_RESERVE_FLUSH_ALL
&& !flushing
&&
4759 space_info
->flush
) {
4760 spin_unlock(&space_info
->lock
);
4762 * If we have a trans handle we can't wait because the flusher
4763 * may have to commit the transaction, which would mean we would
4764 * deadlock since we are waiting for the flusher to finish, but
4765 * hold the current transaction open.
4767 if (current
->journal_info
)
4769 ret
= wait_event_killable(space_info
->wait
, !space_info
->flush
);
4770 /* Must have been killed, return */
4774 spin_lock(&space_info
->lock
);
4778 used
= space_info
->bytes_used
+ space_info
->bytes_reserved
+
4779 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
4780 space_info
->bytes_may_use
;
4783 * The idea here is that we've not already over-reserved the block group
4784 * then we can go ahead and save our reservation first and then start
4785 * flushing if we need to. Otherwise if we've already overcommitted
4786 * lets start flushing stuff first and then come back and try to make
4789 if (used
<= space_info
->total_bytes
) {
4790 if (used
+ orig_bytes
<= space_info
->total_bytes
) {
4791 space_info
->bytes_may_use
+= orig_bytes
;
4792 trace_btrfs_space_reservation(root
->fs_info
,
4793 "space_info", space_info
->flags
, orig_bytes
, 1);
4797 * Ok set num_bytes to orig_bytes since we aren't
4798 * overocmmitted, this way we only try and reclaim what
4801 num_bytes
= orig_bytes
;
4805 * Ok we're over committed, set num_bytes to the overcommitted
4806 * amount plus the amount of bytes that we need for this
4809 num_bytes
= used
- space_info
->total_bytes
+
4813 if (ret
&& can_overcommit(root
, space_info
, orig_bytes
, flush
)) {
4814 space_info
->bytes_may_use
+= orig_bytes
;
4815 trace_btrfs_space_reservation(root
->fs_info
, "space_info",
4816 space_info
->flags
, orig_bytes
,
4822 * Couldn't make our reservation, save our place so while we're trying
4823 * to reclaim space we can actually use it instead of somebody else
4824 * stealing it from us.
4826 * We make the other tasks wait for the flush only when we can flush
4829 if (ret
&& flush
!= BTRFS_RESERVE_NO_FLUSH
) {
4831 space_info
->flush
= 1;
4832 } else if (!ret
&& space_info
->flags
& BTRFS_BLOCK_GROUP_METADATA
) {
4835 * We will do the space reservation dance during log replay,
4836 * which means we won't have fs_info->fs_root set, so don't do
4837 * the async reclaim as we will panic.
4839 if (!root
->fs_info
->log_root_recovering
&&
4840 need_do_async_reclaim(space_info
, root
->fs_info
, used
) &&
4841 !work_busy(&root
->fs_info
->async_reclaim_work
))
4842 queue_work(system_unbound_wq
,
4843 &root
->fs_info
->async_reclaim_work
);
4845 spin_unlock(&space_info
->lock
);
4847 if (!ret
|| flush
== BTRFS_RESERVE_NO_FLUSH
)
4850 ret
= flush_space(root
, space_info
, num_bytes
, orig_bytes
,
4855 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4856 * would happen. So skip delalloc flush.
4858 if (flush
== BTRFS_RESERVE_FLUSH_LIMIT
&&
4859 (flush_state
== FLUSH_DELALLOC
||
4860 flush_state
== FLUSH_DELALLOC_WAIT
))
4861 flush_state
= ALLOC_CHUNK
;
4865 else if (flush
== BTRFS_RESERVE_FLUSH_LIMIT
&&
4866 flush_state
< COMMIT_TRANS
)
4868 else if (flush
== BTRFS_RESERVE_FLUSH_ALL
&&
4869 flush_state
<= COMMIT_TRANS
)
4873 if (ret
== -ENOSPC
&&
4874 unlikely(root
->orphan_cleanup_state
== ORPHAN_CLEANUP_STARTED
)) {
4875 struct btrfs_block_rsv
*global_rsv
=
4876 &root
->fs_info
->global_block_rsv
;
4878 if (block_rsv
!= global_rsv
&&
4879 !block_rsv_use_bytes(global_rsv
, orig_bytes
))
4883 trace_btrfs_space_reservation(root
->fs_info
,
4884 "space_info:enospc",
4885 space_info
->flags
, orig_bytes
, 1);
4887 spin_lock(&space_info
->lock
);
4888 space_info
->flush
= 0;
4889 wake_up_all(&space_info
->wait
);
4890 spin_unlock(&space_info
->lock
);
4895 static struct btrfs_block_rsv
*get_block_rsv(
4896 const struct btrfs_trans_handle
*trans
,
4897 const struct btrfs_root
*root
)
4899 struct btrfs_block_rsv
*block_rsv
= NULL
;
4901 if (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
))
4902 block_rsv
= trans
->block_rsv
;
4904 if (root
== root
->fs_info
->csum_root
&& trans
->adding_csums
)
4905 block_rsv
= trans
->block_rsv
;
4907 if (root
== root
->fs_info
->uuid_root
)
4908 block_rsv
= trans
->block_rsv
;
4911 block_rsv
= root
->block_rsv
;
4914 block_rsv
= &root
->fs_info
->empty_block_rsv
;
4919 static int block_rsv_use_bytes(struct btrfs_block_rsv
*block_rsv
,
4923 spin_lock(&block_rsv
->lock
);
4924 if (block_rsv
->reserved
>= num_bytes
) {
4925 block_rsv
->reserved
-= num_bytes
;
4926 if (block_rsv
->reserved
< block_rsv
->size
)
4927 block_rsv
->full
= 0;
4930 spin_unlock(&block_rsv
->lock
);
4934 static void block_rsv_add_bytes(struct btrfs_block_rsv
*block_rsv
,
4935 u64 num_bytes
, int update_size
)
4937 spin_lock(&block_rsv
->lock
);
4938 block_rsv
->reserved
+= num_bytes
;
4940 block_rsv
->size
+= num_bytes
;
4941 else if (block_rsv
->reserved
>= block_rsv
->size
)
4942 block_rsv
->full
= 1;
4943 spin_unlock(&block_rsv
->lock
);
4946 int btrfs_cond_migrate_bytes(struct btrfs_fs_info
*fs_info
,
4947 struct btrfs_block_rsv
*dest
, u64 num_bytes
,
4950 struct btrfs_block_rsv
*global_rsv
= &fs_info
->global_block_rsv
;
4953 if (global_rsv
->space_info
!= dest
->space_info
)
4956 spin_lock(&global_rsv
->lock
);
4957 min_bytes
= div_factor(global_rsv
->size
, min_factor
);
4958 if (global_rsv
->reserved
< min_bytes
+ num_bytes
) {
4959 spin_unlock(&global_rsv
->lock
);
4962 global_rsv
->reserved
-= num_bytes
;
4963 if (global_rsv
->reserved
< global_rsv
->size
)
4964 global_rsv
->full
= 0;
4965 spin_unlock(&global_rsv
->lock
);
4967 block_rsv_add_bytes(dest
, num_bytes
, 1);
4971 static void block_rsv_release_bytes(struct btrfs_fs_info
*fs_info
,
4972 struct btrfs_block_rsv
*block_rsv
,
4973 struct btrfs_block_rsv
*dest
, u64 num_bytes
)
4975 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
4977 spin_lock(&block_rsv
->lock
);
4978 if (num_bytes
== (u64
)-1)
4979 num_bytes
= block_rsv
->size
;
4980 block_rsv
->size
-= num_bytes
;
4981 if (block_rsv
->reserved
>= block_rsv
->size
) {
4982 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
4983 block_rsv
->reserved
= block_rsv
->size
;
4984 block_rsv
->full
= 1;
4988 spin_unlock(&block_rsv
->lock
);
4990 if (num_bytes
> 0) {
4992 spin_lock(&dest
->lock
);
4996 bytes_to_add
= dest
->size
- dest
->reserved
;
4997 bytes_to_add
= min(num_bytes
, bytes_to_add
);
4998 dest
->reserved
+= bytes_to_add
;
4999 if (dest
->reserved
>= dest
->size
)
5001 num_bytes
-= bytes_to_add
;
5003 spin_unlock(&dest
->lock
);
5006 spin_lock(&space_info
->lock
);
5007 space_info
->bytes_may_use
-= num_bytes
;
5008 trace_btrfs_space_reservation(fs_info
, "space_info",
5009 space_info
->flags
, num_bytes
, 0);
5010 spin_unlock(&space_info
->lock
);
5015 static int block_rsv_migrate_bytes(struct btrfs_block_rsv
*src
,
5016 struct btrfs_block_rsv
*dst
, u64 num_bytes
)
5020 ret
= block_rsv_use_bytes(src
, num_bytes
);
5024 block_rsv_add_bytes(dst
, num_bytes
, 1);
5028 void btrfs_init_block_rsv(struct btrfs_block_rsv
*rsv
, unsigned short type
)
5030 memset(rsv
, 0, sizeof(*rsv
));
5031 spin_lock_init(&rsv
->lock
);
5035 struct btrfs_block_rsv
*btrfs_alloc_block_rsv(struct btrfs_root
*root
,
5036 unsigned short type
)
5038 struct btrfs_block_rsv
*block_rsv
;
5039 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5041 block_rsv
= kmalloc(sizeof(*block_rsv
), GFP_NOFS
);
5045 btrfs_init_block_rsv(block_rsv
, type
);
5046 block_rsv
->space_info
= __find_space_info(fs_info
,
5047 BTRFS_BLOCK_GROUP_METADATA
);
5051 void btrfs_free_block_rsv(struct btrfs_root
*root
,
5052 struct btrfs_block_rsv
*rsv
)
5056 btrfs_block_rsv_release(root
, rsv
, (u64
)-1);
5060 void __btrfs_free_block_rsv(struct btrfs_block_rsv
*rsv
)
5065 int btrfs_block_rsv_add(struct btrfs_root
*root
,
5066 struct btrfs_block_rsv
*block_rsv
, u64 num_bytes
,
5067 enum btrfs_reserve_flush_enum flush
)
5074 ret
= reserve_metadata_bytes(root
, block_rsv
, num_bytes
, flush
);
5076 block_rsv_add_bytes(block_rsv
, num_bytes
, 1);
5083 int btrfs_block_rsv_check(struct btrfs_root
*root
,
5084 struct btrfs_block_rsv
*block_rsv
, int min_factor
)
5092 spin_lock(&block_rsv
->lock
);
5093 num_bytes
= div_factor(block_rsv
->size
, min_factor
);
5094 if (block_rsv
->reserved
>= num_bytes
)
5096 spin_unlock(&block_rsv
->lock
);
5101 int btrfs_block_rsv_refill(struct btrfs_root
*root
,
5102 struct btrfs_block_rsv
*block_rsv
, u64 min_reserved
,
5103 enum btrfs_reserve_flush_enum flush
)
5111 spin_lock(&block_rsv
->lock
);
5112 num_bytes
= min_reserved
;
5113 if (block_rsv
->reserved
>= num_bytes
)
5116 num_bytes
-= block_rsv
->reserved
;
5117 spin_unlock(&block_rsv
->lock
);
5122 ret
= reserve_metadata_bytes(root
, block_rsv
, num_bytes
, flush
);
5124 block_rsv_add_bytes(block_rsv
, num_bytes
, 0);
5131 int btrfs_block_rsv_migrate(struct btrfs_block_rsv
*src_rsv
,
5132 struct btrfs_block_rsv
*dst_rsv
,
5135 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
5138 void btrfs_block_rsv_release(struct btrfs_root
*root
,
5139 struct btrfs_block_rsv
*block_rsv
,
5142 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
5143 if (global_rsv
== block_rsv
||
5144 block_rsv
->space_info
!= global_rsv
->space_info
)
5146 block_rsv_release_bytes(root
->fs_info
, block_rsv
, global_rsv
,
5151 * helper to calculate size of global block reservation.
5152 * the desired value is sum of space used by extent tree,
5153 * checksum tree and root tree
5155 static u64
calc_global_metadata_size(struct btrfs_fs_info
*fs_info
)
5157 struct btrfs_space_info
*sinfo
;
5161 int csum_size
= btrfs_super_csum_size(fs_info
->super_copy
);
5163 sinfo
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_DATA
);
5164 spin_lock(&sinfo
->lock
);
5165 data_used
= sinfo
->bytes_used
;
5166 spin_unlock(&sinfo
->lock
);
5168 sinfo
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
5169 spin_lock(&sinfo
->lock
);
5170 if (sinfo
->flags
& BTRFS_BLOCK_GROUP_DATA
)
5172 meta_used
= sinfo
->bytes_used
;
5173 spin_unlock(&sinfo
->lock
);
5175 num_bytes
= (data_used
>> fs_info
->sb
->s_blocksize_bits
) *
5177 num_bytes
+= div_u64(data_used
+ meta_used
, 50);
5179 if (num_bytes
* 3 > meta_used
)
5180 num_bytes
= div_u64(meta_used
, 3);
5182 return ALIGN(num_bytes
, fs_info
->extent_root
->nodesize
<< 10);
5185 static void update_global_block_rsv(struct btrfs_fs_info
*fs_info
)
5187 struct btrfs_block_rsv
*block_rsv
= &fs_info
->global_block_rsv
;
5188 struct btrfs_space_info
*sinfo
= block_rsv
->space_info
;
5191 num_bytes
= calc_global_metadata_size(fs_info
);
5193 spin_lock(&sinfo
->lock
);
5194 spin_lock(&block_rsv
->lock
);
5196 block_rsv
->size
= min_t(u64
, num_bytes
, 512 * 1024 * 1024);
5198 num_bytes
= sinfo
->bytes_used
+ sinfo
->bytes_pinned
+
5199 sinfo
->bytes_reserved
+ sinfo
->bytes_readonly
+
5200 sinfo
->bytes_may_use
;
5202 if (sinfo
->total_bytes
> num_bytes
) {
5203 num_bytes
= sinfo
->total_bytes
- num_bytes
;
5204 block_rsv
->reserved
+= num_bytes
;
5205 sinfo
->bytes_may_use
+= num_bytes
;
5206 trace_btrfs_space_reservation(fs_info
, "space_info",
5207 sinfo
->flags
, num_bytes
, 1);
5210 if (block_rsv
->reserved
>= block_rsv
->size
) {
5211 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
5212 sinfo
->bytes_may_use
-= num_bytes
;
5213 trace_btrfs_space_reservation(fs_info
, "space_info",
5214 sinfo
->flags
, num_bytes
, 0);
5215 block_rsv
->reserved
= block_rsv
->size
;
5216 block_rsv
->full
= 1;
5219 spin_unlock(&block_rsv
->lock
);
5220 spin_unlock(&sinfo
->lock
);
5223 static void init_global_block_rsv(struct btrfs_fs_info
*fs_info
)
5225 struct btrfs_space_info
*space_info
;
5227 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_SYSTEM
);
5228 fs_info
->chunk_block_rsv
.space_info
= space_info
;
5230 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
5231 fs_info
->global_block_rsv
.space_info
= space_info
;
5232 fs_info
->delalloc_block_rsv
.space_info
= space_info
;
5233 fs_info
->trans_block_rsv
.space_info
= space_info
;
5234 fs_info
->empty_block_rsv
.space_info
= space_info
;
5235 fs_info
->delayed_block_rsv
.space_info
= space_info
;
5237 fs_info
->extent_root
->block_rsv
= &fs_info
->global_block_rsv
;
5238 fs_info
->csum_root
->block_rsv
= &fs_info
->global_block_rsv
;
5239 fs_info
->dev_root
->block_rsv
= &fs_info
->global_block_rsv
;
5240 fs_info
->tree_root
->block_rsv
= &fs_info
->global_block_rsv
;
5241 if (fs_info
->quota_root
)
5242 fs_info
->quota_root
->block_rsv
= &fs_info
->global_block_rsv
;
5243 fs_info
->chunk_root
->block_rsv
= &fs_info
->chunk_block_rsv
;
5245 update_global_block_rsv(fs_info
);
5248 static void release_global_block_rsv(struct btrfs_fs_info
*fs_info
)
5250 block_rsv_release_bytes(fs_info
, &fs_info
->global_block_rsv
, NULL
,
5252 WARN_ON(fs_info
->delalloc_block_rsv
.size
> 0);
5253 WARN_ON(fs_info
->delalloc_block_rsv
.reserved
> 0);
5254 WARN_ON(fs_info
->trans_block_rsv
.size
> 0);
5255 WARN_ON(fs_info
->trans_block_rsv
.reserved
> 0);
5256 WARN_ON(fs_info
->chunk_block_rsv
.size
> 0);
5257 WARN_ON(fs_info
->chunk_block_rsv
.reserved
> 0);
5258 WARN_ON(fs_info
->delayed_block_rsv
.size
> 0);
5259 WARN_ON(fs_info
->delayed_block_rsv
.reserved
> 0);
5262 void btrfs_trans_release_metadata(struct btrfs_trans_handle
*trans
,
5263 struct btrfs_root
*root
)
5265 if (!trans
->block_rsv
)
5268 if (!trans
->bytes_reserved
)
5271 trace_btrfs_space_reservation(root
->fs_info
, "transaction",
5272 trans
->transid
, trans
->bytes_reserved
, 0);
5273 btrfs_block_rsv_release(root
, trans
->block_rsv
, trans
->bytes_reserved
);
5274 trans
->bytes_reserved
= 0;
5278 * To be called after all the new block groups attached to the transaction
5279 * handle have been created (btrfs_create_pending_block_groups()).
5281 void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle
*trans
)
5283 struct btrfs_fs_info
*fs_info
= trans
->root
->fs_info
;
5285 if (!trans
->chunk_bytes_reserved
)
5288 WARN_ON_ONCE(!list_empty(&trans
->new_bgs
));
5290 block_rsv_release_bytes(fs_info
, &fs_info
->chunk_block_rsv
, NULL
,
5291 trans
->chunk_bytes_reserved
);
5292 trans
->chunk_bytes_reserved
= 0;
5295 /* Can only return 0 or -ENOSPC */
5296 int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle
*trans
,
5297 struct inode
*inode
)
5299 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5300 struct btrfs_block_rsv
*src_rsv
= get_block_rsv(trans
, root
);
5301 struct btrfs_block_rsv
*dst_rsv
= root
->orphan_block_rsv
;
5304 * We need to hold space in order to delete our orphan item once we've
5305 * added it, so this takes the reservation so we can release it later
5306 * when we are truly done with the orphan item.
5308 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 1);
5309 trace_btrfs_space_reservation(root
->fs_info
, "orphan",
5310 btrfs_ino(inode
), num_bytes
, 1);
5311 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
5314 void btrfs_orphan_release_metadata(struct inode
*inode
)
5316 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5317 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 1);
5318 trace_btrfs_space_reservation(root
->fs_info
, "orphan",
5319 btrfs_ino(inode
), num_bytes
, 0);
5320 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
, num_bytes
);
5324 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
5325 * root: the root of the parent directory
5326 * rsv: block reservation
5327 * items: the number of items that we need do reservation
5328 * qgroup_reserved: used to return the reserved size in qgroup
5330 * This function is used to reserve the space for snapshot/subvolume
5331 * creation and deletion. Those operations are different with the
5332 * common file/directory operations, they change two fs/file trees
5333 * and root tree, the number of items that the qgroup reserves is
5334 * different with the free space reservation. So we can not use
5335 * the space reseravtion mechanism in start_transaction().
5337 int btrfs_subvolume_reserve_metadata(struct btrfs_root
*root
,
5338 struct btrfs_block_rsv
*rsv
,
5340 u64
*qgroup_reserved
,
5341 bool use_global_rsv
)
5345 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
5347 if (root
->fs_info
->quota_enabled
) {
5348 /* One for parent inode, two for dir entries */
5349 num_bytes
= 3 * root
->nodesize
;
5350 ret
= btrfs_qgroup_reserve(root
, num_bytes
);
5357 *qgroup_reserved
= num_bytes
;
5359 num_bytes
= btrfs_calc_trans_metadata_size(root
, items
);
5360 rsv
->space_info
= __find_space_info(root
->fs_info
,
5361 BTRFS_BLOCK_GROUP_METADATA
);
5362 ret
= btrfs_block_rsv_add(root
, rsv
, num_bytes
,
5363 BTRFS_RESERVE_FLUSH_ALL
);
5365 if (ret
== -ENOSPC
&& use_global_rsv
)
5366 ret
= btrfs_block_rsv_migrate(global_rsv
, rsv
, num_bytes
);
5369 if (*qgroup_reserved
)
5370 btrfs_qgroup_free(root
, *qgroup_reserved
);
5376 void btrfs_subvolume_release_metadata(struct btrfs_root
*root
,
5377 struct btrfs_block_rsv
*rsv
,
5378 u64 qgroup_reserved
)
5380 btrfs_block_rsv_release(root
, rsv
, (u64
)-1);
5384 * drop_outstanding_extent - drop an outstanding extent
5385 * @inode: the inode we're dropping the extent for
5386 * @num_bytes: the number of bytes we're relaseing.
5388 * This is called when we are freeing up an outstanding extent, either called
5389 * after an error or after an extent is written. This will return the number of
5390 * reserved extents that need to be freed. This must be called with
5391 * BTRFS_I(inode)->lock held.
5393 static unsigned drop_outstanding_extent(struct inode
*inode
, u64 num_bytes
)
5395 unsigned drop_inode_space
= 0;
5396 unsigned dropped_extents
= 0;
5397 unsigned num_extents
= 0;
5399 num_extents
= (unsigned)div64_u64(num_bytes
+
5400 BTRFS_MAX_EXTENT_SIZE
- 1,
5401 BTRFS_MAX_EXTENT_SIZE
);
5402 ASSERT(num_extents
);
5403 ASSERT(BTRFS_I(inode
)->outstanding_extents
>= num_extents
);
5404 BTRFS_I(inode
)->outstanding_extents
-= num_extents
;
5406 if (BTRFS_I(inode
)->outstanding_extents
== 0 &&
5407 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED
,
5408 &BTRFS_I(inode
)->runtime_flags
))
5409 drop_inode_space
= 1;
5412 * If we have more or the same amount of outsanding extents than we have
5413 * reserved then we need to leave the reserved extents count alone.
5415 if (BTRFS_I(inode
)->outstanding_extents
>=
5416 BTRFS_I(inode
)->reserved_extents
)
5417 return drop_inode_space
;
5419 dropped_extents
= BTRFS_I(inode
)->reserved_extents
-
5420 BTRFS_I(inode
)->outstanding_extents
;
5421 BTRFS_I(inode
)->reserved_extents
-= dropped_extents
;
5422 return dropped_extents
+ drop_inode_space
;
5426 * calc_csum_metadata_size - return the amount of metada space that must be
5427 * reserved/free'd for the given bytes.
5428 * @inode: the inode we're manipulating
5429 * @num_bytes: the number of bytes in question
5430 * @reserve: 1 if we are reserving space, 0 if we are freeing space
5432 * This adjusts the number of csum_bytes in the inode and then returns the
5433 * correct amount of metadata that must either be reserved or freed. We
5434 * calculate how many checksums we can fit into one leaf and then divide the
5435 * number of bytes that will need to be checksumed by this value to figure out
5436 * how many checksums will be required. If we are adding bytes then the number
5437 * may go up and we will return the number of additional bytes that must be
5438 * reserved. If it is going down we will return the number of bytes that must
5441 * This must be called with BTRFS_I(inode)->lock held.
5443 static u64
calc_csum_metadata_size(struct inode
*inode
, u64 num_bytes
,
5446 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5447 u64 old_csums
, num_csums
;
5449 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
&&
5450 BTRFS_I(inode
)->csum_bytes
== 0)
5453 old_csums
= btrfs_csum_bytes_to_leaves(root
, BTRFS_I(inode
)->csum_bytes
);
5455 BTRFS_I(inode
)->csum_bytes
+= num_bytes
;
5457 BTRFS_I(inode
)->csum_bytes
-= num_bytes
;
5458 num_csums
= btrfs_csum_bytes_to_leaves(root
, BTRFS_I(inode
)->csum_bytes
);
5460 /* No change, no need to reserve more */
5461 if (old_csums
== num_csums
)
5465 return btrfs_calc_trans_metadata_size(root
,
5466 num_csums
- old_csums
);
5468 return btrfs_calc_trans_metadata_size(root
, old_csums
- num_csums
);
5471 int btrfs_delalloc_reserve_metadata(struct inode
*inode
, u64 num_bytes
)
5473 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5474 struct btrfs_block_rsv
*block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
5477 unsigned nr_extents
= 0;
5478 int extra_reserve
= 0;
5479 enum btrfs_reserve_flush_enum flush
= BTRFS_RESERVE_FLUSH_ALL
;
5481 bool delalloc_lock
= true;
5485 /* If we are a free space inode we need to not flush since we will be in
5486 * the middle of a transaction commit. We also don't need the delalloc
5487 * mutex since we won't race with anybody. We need this mostly to make
5488 * lockdep shut its filthy mouth.
5490 if (btrfs_is_free_space_inode(inode
)) {
5491 flush
= BTRFS_RESERVE_NO_FLUSH
;
5492 delalloc_lock
= false;
5495 if (flush
!= BTRFS_RESERVE_NO_FLUSH
&&
5496 btrfs_transaction_in_commit(root
->fs_info
))
5497 schedule_timeout(1);
5500 mutex_lock(&BTRFS_I(inode
)->delalloc_mutex
);
5502 num_bytes
= ALIGN(num_bytes
, root
->sectorsize
);
5504 spin_lock(&BTRFS_I(inode
)->lock
);
5505 nr_extents
= (unsigned)div64_u64(num_bytes
+
5506 BTRFS_MAX_EXTENT_SIZE
- 1,
5507 BTRFS_MAX_EXTENT_SIZE
);
5508 BTRFS_I(inode
)->outstanding_extents
+= nr_extents
;
5511 if (BTRFS_I(inode
)->outstanding_extents
>
5512 BTRFS_I(inode
)->reserved_extents
)
5513 nr_extents
= BTRFS_I(inode
)->outstanding_extents
-
5514 BTRFS_I(inode
)->reserved_extents
;
5517 * Add an item to reserve for updating the inode when we complete the
5520 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED
,
5521 &BTRFS_I(inode
)->runtime_flags
)) {
5526 to_reserve
= btrfs_calc_trans_metadata_size(root
, nr_extents
);
5527 to_reserve
+= calc_csum_metadata_size(inode
, num_bytes
, 1);
5528 csum_bytes
= BTRFS_I(inode
)->csum_bytes
;
5529 spin_unlock(&BTRFS_I(inode
)->lock
);
5531 if (root
->fs_info
->quota_enabled
) {
5532 ret
= btrfs_qgroup_reserve(root
, nr_extents
* root
->nodesize
);
5537 ret
= reserve_metadata_bytes(root
, block_rsv
, to_reserve
, flush
);
5538 if (unlikely(ret
)) {
5539 if (root
->fs_info
->quota_enabled
)
5540 btrfs_qgroup_free(root
, nr_extents
* root
->nodesize
);
5544 spin_lock(&BTRFS_I(inode
)->lock
);
5545 if (extra_reserve
) {
5546 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED
,
5547 &BTRFS_I(inode
)->runtime_flags
);
5550 BTRFS_I(inode
)->reserved_extents
+= nr_extents
;
5551 spin_unlock(&BTRFS_I(inode
)->lock
);
5554 mutex_unlock(&BTRFS_I(inode
)->delalloc_mutex
);
5557 trace_btrfs_space_reservation(root
->fs_info
, "delalloc",
5558 btrfs_ino(inode
), to_reserve
, 1);
5559 block_rsv_add_bytes(block_rsv
, to_reserve
, 1);
5564 spin_lock(&BTRFS_I(inode
)->lock
);
5565 dropped
= drop_outstanding_extent(inode
, num_bytes
);
5567 * If the inodes csum_bytes is the same as the original
5568 * csum_bytes then we know we haven't raced with any free()ers
5569 * so we can just reduce our inodes csum bytes and carry on.
5571 if (BTRFS_I(inode
)->csum_bytes
== csum_bytes
) {
5572 calc_csum_metadata_size(inode
, num_bytes
, 0);
5574 u64 orig_csum_bytes
= BTRFS_I(inode
)->csum_bytes
;
5578 * This is tricky, but first we need to figure out how much we
5579 * free'd from any free-ers that occured during this
5580 * reservation, so we reset ->csum_bytes to the csum_bytes
5581 * before we dropped our lock, and then call the free for the
5582 * number of bytes that were freed while we were trying our
5585 bytes
= csum_bytes
- BTRFS_I(inode
)->csum_bytes
;
5586 BTRFS_I(inode
)->csum_bytes
= csum_bytes
;
5587 to_free
= calc_csum_metadata_size(inode
, bytes
, 0);
5591 * Now we need to see how much we would have freed had we not
5592 * been making this reservation and our ->csum_bytes were not
5593 * artificially inflated.
5595 BTRFS_I(inode
)->csum_bytes
= csum_bytes
- num_bytes
;
5596 bytes
= csum_bytes
- orig_csum_bytes
;
5597 bytes
= calc_csum_metadata_size(inode
, bytes
, 0);
5600 * Now reset ->csum_bytes to what it should be. If bytes is
5601 * more than to_free then we would have free'd more space had we
5602 * not had an artificially high ->csum_bytes, so we need to free
5603 * the remainder. If bytes is the same or less then we don't
5604 * need to do anything, the other free-ers did the correct
5607 BTRFS_I(inode
)->csum_bytes
= orig_csum_bytes
- num_bytes
;
5608 if (bytes
> to_free
)
5609 to_free
= bytes
- to_free
;
5613 spin_unlock(&BTRFS_I(inode
)->lock
);
5615 to_free
+= btrfs_calc_trans_metadata_size(root
, dropped
);
5618 btrfs_block_rsv_release(root
, block_rsv
, to_free
);
5619 trace_btrfs_space_reservation(root
->fs_info
, "delalloc",
5620 btrfs_ino(inode
), to_free
, 0);
5623 mutex_unlock(&BTRFS_I(inode
)->delalloc_mutex
);
5628 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
5629 * @inode: the inode to release the reservation for
5630 * @num_bytes: the number of bytes we're releasing
5632 * This will release the metadata reservation for an inode. This can be called
5633 * once we complete IO for a given set of bytes to release their metadata
5636 void btrfs_delalloc_release_metadata(struct inode
*inode
, u64 num_bytes
)
5638 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5642 num_bytes
= ALIGN(num_bytes
, root
->sectorsize
);
5643 spin_lock(&BTRFS_I(inode
)->lock
);
5644 dropped
= drop_outstanding_extent(inode
, num_bytes
);
5647 to_free
= calc_csum_metadata_size(inode
, num_bytes
, 0);
5648 spin_unlock(&BTRFS_I(inode
)->lock
);
5650 to_free
+= btrfs_calc_trans_metadata_size(root
, dropped
);
5652 if (btrfs_test_is_dummy_root(root
))
5655 trace_btrfs_space_reservation(root
->fs_info
, "delalloc",
5656 btrfs_ino(inode
), to_free
, 0);
5658 btrfs_block_rsv_release(root
, &root
->fs_info
->delalloc_block_rsv
,
5663 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
5664 * @inode: inode we're writing to
5665 * @num_bytes: the number of bytes we want to allocate
5667 * This will do the following things
5669 * o reserve space in the data space info for num_bytes
5670 * o reserve space in the metadata space info based on number of outstanding
5671 * extents and how much csums will be needed
5672 * o add to the inodes ->delalloc_bytes
5673 * o add it to the fs_info's delalloc inodes list.
5675 * This will return 0 for success and -ENOSPC if there is no space left.
5677 int btrfs_delalloc_reserve_space(struct inode
*inode
, u64 num_bytes
)
5681 ret
= btrfs_check_data_free_space(inode
, num_bytes
, num_bytes
);
5685 ret
= btrfs_delalloc_reserve_metadata(inode
, num_bytes
);
5687 btrfs_free_reserved_data_space(inode
, num_bytes
);
5695 * btrfs_delalloc_release_space - release data and metadata space for delalloc
5696 * @inode: inode we're releasing space for
5697 * @num_bytes: the number of bytes we want to free up
5699 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
5700 * called in the case that we don't need the metadata AND data reservations
5701 * anymore. So if there is an error or we insert an inline extent.
5703 * This function will release the metadata space that was not used and will
5704 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
5705 * list if there are no delalloc bytes left.
5707 void btrfs_delalloc_release_space(struct inode
*inode
, u64 num_bytes
)
5709 btrfs_delalloc_release_metadata(inode
, num_bytes
);
5710 btrfs_free_reserved_data_space(inode
, num_bytes
);
5713 static int update_block_group(struct btrfs_trans_handle
*trans
,
5714 struct btrfs_root
*root
, u64 bytenr
,
5715 u64 num_bytes
, int alloc
)
5717 struct btrfs_block_group_cache
*cache
= NULL
;
5718 struct btrfs_fs_info
*info
= root
->fs_info
;
5719 u64 total
= num_bytes
;
5724 /* block accounting for super block */
5725 spin_lock(&info
->delalloc_root_lock
);
5726 old_val
= btrfs_super_bytes_used(info
->super_copy
);
5728 old_val
+= num_bytes
;
5730 old_val
-= num_bytes
;
5731 btrfs_set_super_bytes_used(info
->super_copy
, old_val
);
5732 spin_unlock(&info
->delalloc_root_lock
);
5735 cache
= btrfs_lookup_block_group(info
, bytenr
);
5738 if (cache
->flags
& (BTRFS_BLOCK_GROUP_DUP
|
5739 BTRFS_BLOCK_GROUP_RAID1
|
5740 BTRFS_BLOCK_GROUP_RAID10
))
5745 * If this block group has free space cache written out, we
5746 * need to make sure to load it if we are removing space. This
5747 * is because we need the unpinning stage to actually add the
5748 * space back to the block group, otherwise we will leak space.
5750 if (!alloc
&& cache
->cached
== BTRFS_CACHE_NO
)
5751 cache_block_group(cache
, 1);
5753 byte_in_group
= bytenr
- cache
->key
.objectid
;
5754 WARN_ON(byte_in_group
> cache
->key
.offset
);
5756 spin_lock(&cache
->space_info
->lock
);
5757 spin_lock(&cache
->lock
);
5759 if (btrfs_test_opt(root
, SPACE_CACHE
) &&
5760 cache
->disk_cache_state
< BTRFS_DC_CLEAR
)
5761 cache
->disk_cache_state
= BTRFS_DC_CLEAR
;
5763 old_val
= btrfs_block_group_used(&cache
->item
);
5764 num_bytes
= min(total
, cache
->key
.offset
- byte_in_group
);
5766 old_val
+= num_bytes
;
5767 btrfs_set_block_group_used(&cache
->item
, old_val
);
5768 cache
->reserved
-= num_bytes
;
5769 cache
->space_info
->bytes_reserved
-= num_bytes
;
5770 cache
->space_info
->bytes_used
+= num_bytes
;
5771 cache
->space_info
->disk_used
+= num_bytes
* factor
;
5772 spin_unlock(&cache
->lock
);
5773 spin_unlock(&cache
->space_info
->lock
);
5775 old_val
-= num_bytes
;
5776 btrfs_set_block_group_used(&cache
->item
, old_val
);
5777 cache
->pinned
+= num_bytes
;
5778 cache
->space_info
->bytes_pinned
+= num_bytes
;
5779 cache
->space_info
->bytes_used
-= num_bytes
;
5780 cache
->space_info
->disk_used
-= num_bytes
* factor
;
5781 spin_unlock(&cache
->lock
);
5782 spin_unlock(&cache
->space_info
->lock
);
5784 set_extent_dirty(info
->pinned_extents
,
5785 bytenr
, bytenr
+ num_bytes
- 1,
5786 GFP_NOFS
| __GFP_NOFAIL
);
5788 * No longer have used bytes in this block group, queue
5792 spin_lock(&info
->unused_bgs_lock
);
5793 if (list_empty(&cache
->bg_list
)) {
5794 btrfs_get_block_group(cache
);
5795 list_add_tail(&cache
->bg_list
,
5798 spin_unlock(&info
->unused_bgs_lock
);
5802 spin_lock(&trans
->transaction
->dirty_bgs_lock
);
5803 if (list_empty(&cache
->dirty_list
)) {
5804 list_add_tail(&cache
->dirty_list
,
5805 &trans
->transaction
->dirty_bgs
);
5806 trans
->transaction
->num_dirty_bgs
++;
5807 btrfs_get_block_group(cache
);
5809 spin_unlock(&trans
->transaction
->dirty_bgs_lock
);
5811 btrfs_put_block_group(cache
);
5813 bytenr
+= num_bytes
;
5818 static u64
first_logical_byte(struct btrfs_root
*root
, u64 search_start
)
5820 struct btrfs_block_group_cache
*cache
;
5823 spin_lock(&root
->fs_info
->block_group_cache_lock
);
5824 bytenr
= root
->fs_info
->first_logical_byte
;
5825 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
5827 if (bytenr
< (u64
)-1)
5830 cache
= btrfs_lookup_first_block_group(root
->fs_info
, search_start
);
5834 bytenr
= cache
->key
.objectid
;
5835 btrfs_put_block_group(cache
);
5840 static int pin_down_extent(struct btrfs_root
*root
,
5841 struct btrfs_block_group_cache
*cache
,
5842 u64 bytenr
, u64 num_bytes
, int reserved
)
5844 spin_lock(&cache
->space_info
->lock
);
5845 spin_lock(&cache
->lock
);
5846 cache
->pinned
+= num_bytes
;
5847 cache
->space_info
->bytes_pinned
+= num_bytes
;
5849 cache
->reserved
-= num_bytes
;
5850 cache
->space_info
->bytes_reserved
-= num_bytes
;
5852 spin_unlock(&cache
->lock
);
5853 spin_unlock(&cache
->space_info
->lock
);
5855 set_extent_dirty(root
->fs_info
->pinned_extents
, bytenr
,
5856 bytenr
+ num_bytes
- 1, GFP_NOFS
| __GFP_NOFAIL
);
5858 trace_btrfs_reserved_extent_free(root
, bytenr
, num_bytes
);
5863 * this function must be called within transaction
5865 int btrfs_pin_extent(struct btrfs_root
*root
,
5866 u64 bytenr
, u64 num_bytes
, int reserved
)
5868 struct btrfs_block_group_cache
*cache
;
5870 cache
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
5871 BUG_ON(!cache
); /* Logic error */
5873 pin_down_extent(root
, cache
, bytenr
, num_bytes
, reserved
);
5875 btrfs_put_block_group(cache
);
5880 * this function must be called within transaction
5882 int btrfs_pin_extent_for_log_replay(struct btrfs_root
*root
,
5883 u64 bytenr
, u64 num_bytes
)
5885 struct btrfs_block_group_cache
*cache
;
5888 cache
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
5893 * pull in the free space cache (if any) so that our pin
5894 * removes the free space from the cache. We have load_only set
5895 * to one because the slow code to read in the free extents does check
5896 * the pinned extents.
5898 cache_block_group(cache
, 1);
5900 pin_down_extent(root
, cache
, bytenr
, num_bytes
, 0);
5902 /* remove us from the free space cache (if we're there at all) */
5903 ret
= btrfs_remove_free_space(cache
, bytenr
, num_bytes
);
5904 btrfs_put_block_group(cache
);
5908 static int __exclude_logged_extent(struct btrfs_root
*root
, u64 start
, u64 num_bytes
)
5911 struct btrfs_block_group_cache
*block_group
;
5912 struct btrfs_caching_control
*caching_ctl
;
5914 block_group
= btrfs_lookup_block_group(root
->fs_info
, start
);
5918 cache_block_group(block_group
, 0);
5919 caching_ctl
= get_caching_control(block_group
);
5923 BUG_ON(!block_group_cache_done(block_group
));
5924 ret
= btrfs_remove_free_space(block_group
, start
, num_bytes
);
5926 mutex_lock(&caching_ctl
->mutex
);
5928 if (start
>= caching_ctl
->progress
) {
5929 ret
= add_excluded_extent(root
, start
, num_bytes
);
5930 } else if (start
+ num_bytes
<= caching_ctl
->progress
) {
5931 ret
= btrfs_remove_free_space(block_group
,
5934 num_bytes
= caching_ctl
->progress
- start
;
5935 ret
= btrfs_remove_free_space(block_group
,
5940 num_bytes
= (start
+ num_bytes
) -
5941 caching_ctl
->progress
;
5942 start
= caching_ctl
->progress
;
5943 ret
= add_excluded_extent(root
, start
, num_bytes
);
5946 mutex_unlock(&caching_ctl
->mutex
);
5947 put_caching_control(caching_ctl
);
5949 btrfs_put_block_group(block_group
);
5953 int btrfs_exclude_logged_extents(struct btrfs_root
*log
,
5954 struct extent_buffer
*eb
)
5956 struct btrfs_file_extent_item
*item
;
5957 struct btrfs_key key
;
5961 if (!btrfs_fs_incompat(log
->fs_info
, MIXED_GROUPS
))
5964 for (i
= 0; i
< btrfs_header_nritems(eb
); i
++) {
5965 btrfs_item_key_to_cpu(eb
, &key
, i
);
5966 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
5968 item
= btrfs_item_ptr(eb
, i
, struct btrfs_file_extent_item
);
5969 found_type
= btrfs_file_extent_type(eb
, item
);
5970 if (found_type
== BTRFS_FILE_EXTENT_INLINE
)
5972 if (btrfs_file_extent_disk_bytenr(eb
, item
) == 0)
5974 key
.objectid
= btrfs_file_extent_disk_bytenr(eb
, item
);
5975 key
.offset
= btrfs_file_extent_disk_num_bytes(eb
, item
);
5976 __exclude_logged_extent(log
, key
.objectid
, key
.offset
);
5983 * btrfs_update_reserved_bytes - update the block_group and space info counters
5984 * @cache: The cache we are manipulating
5985 * @num_bytes: The number of bytes in question
5986 * @reserve: One of the reservation enums
5987 * @delalloc: The blocks are allocated for the delalloc write
5989 * This is called by the allocator when it reserves space, or by somebody who is
5990 * freeing space that was never actually used on disk. For example if you
5991 * reserve some space for a new leaf in transaction A and before transaction A
5992 * commits you free that leaf, you call this with reserve set to 0 in order to
5993 * clear the reservation.
5995 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
5996 * ENOSPC accounting. For data we handle the reservation through clearing the
5997 * delalloc bits in the io_tree. We have to do this since we could end up
5998 * allocating less disk space for the amount of data we have reserved in the
5999 * case of compression.
6001 * If this is a reservation and the block group has become read only we cannot
6002 * make the reservation and return -EAGAIN, otherwise this function always
6005 static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache
*cache
,
6006 u64 num_bytes
, int reserve
, int delalloc
)
6008 struct btrfs_space_info
*space_info
= cache
->space_info
;
6011 spin_lock(&space_info
->lock
);
6012 spin_lock(&cache
->lock
);
6013 if (reserve
!= RESERVE_FREE
) {
6017 cache
->reserved
+= num_bytes
;
6018 space_info
->bytes_reserved
+= num_bytes
;
6019 if (reserve
== RESERVE_ALLOC
) {
6020 trace_btrfs_space_reservation(cache
->fs_info
,
6021 "space_info", space_info
->flags
,
6023 space_info
->bytes_may_use
-= num_bytes
;
6027 cache
->delalloc_bytes
+= num_bytes
;
6031 space_info
->bytes_readonly
+= num_bytes
;
6032 cache
->reserved
-= num_bytes
;
6033 space_info
->bytes_reserved
-= num_bytes
;
6036 cache
->delalloc_bytes
-= num_bytes
;
6038 spin_unlock(&cache
->lock
);
6039 spin_unlock(&space_info
->lock
);
6043 void btrfs_prepare_extent_commit(struct btrfs_trans_handle
*trans
,
6044 struct btrfs_root
*root
)
6046 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
6047 struct btrfs_caching_control
*next
;
6048 struct btrfs_caching_control
*caching_ctl
;
6049 struct btrfs_block_group_cache
*cache
;
6051 down_write(&fs_info
->commit_root_sem
);
6053 list_for_each_entry_safe(caching_ctl
, next
,
6054 &fs_info
->caching_block_groups
, list
) {
6055 cache
= caching_ctl
->block_group
;
6056 if (block_group_cache_done(cache
)) {
6057 cache
->last_byte_to_unpin
= (u64
)-1;
6058 list_del_init(&caching_ctl
->list
);
6059 put_caching_control(caching_ctl
);
6061 cache
->last_byte_to_unpin
= caching_ctl
->progress
;
6065 if (fs_info
->pinned_extents
== &fs_info
->freed_extents
[0])
6066 fs_info
->pinned_extents
= &fs_info
->freed_extents
[1];
6068 fs_info
->pinned_extents
= &fs_info
->freed_extents
[0];
6070 up_write(&fs_info
->commit_root_sem
);
6072 update_global_block_rsv(fs_info
);
6075 static int unpin_extent_range(struct btrfs_root
*root
, u64 start
, u64 end
,
6076 const bool return_free_space
)
6078 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
6079 struct btrfs_block_group_cache
*cache
= NULL
;
6080 struct btrfs_space_info
*space_info
;
6081 struct btrfs_block_rsv
*global_rsv
= &fs_info
->global_block_rsv
;
6085 while (start
<= end
) {
6088 start
>= cache
->key
.objectid
+ cache
->key
.offset
) {
6090 btrfs_put_block_group(cache
);
6091 cache
= btrfs_lookup_block_group(fs_info
, start
);
6092 BUG_ON(!cache
); /* Logic error */
6095 len
= cache
->key
.objectid
+ cache
->key
.offset
- start
;
6096 len
= min(len
, end
+ 1 - start
);
6098 if (start
< cache
->last_byte_to_unpin
) {
6099 len
= min(len
, cache
->last_byte_to_unpin
- start
);
6100 if (return_free_space
)
6101 btrfs_add_free_space(cache
, start
, len
);
6105 space_info
= cache
->space_info
;
6107 spin_lock(&space_info
->lock
);
6108 spin_lock(&cache
->lock
);
6109 cache
->pinned
-= len
;
6110 space_info
->bytes_pinned
-= len
;
6111 percpu_counter_add(&space_info
->total_bytes_pinned
, -len
);
6113 space_info
->bytes_readonly
+= len
;
6116 spin_unlock(&cache
->lock
);
6117 if (!readonly
&& global_rsv
->space_info
== space_info
) {
6118 spin_lock(&global_rsv
->lock
);
6119 if (!global_rsv
->full
) {
6120 len
= min(len
, global_rsv
->size
-
6121 global_rsv
->reserved
);
6122 global_rsv
->reserved
+= len
;
6123 space_info
->bytes_may_use
+= len
;
6124 if (global_rsv
->reserved
>= global_rsv
->size
)
6125 global_rsv
->full
= 1;
6127 spin_unlock(&global_rsv
->lock
);
6129 spin_unlock(&space_info
->lock
);
6133 btrfs_put_block_group(cache
);
6137 int btrfs_finish_extent_commit(struct btrfs_trans_handle
*trans
,
6138 struct btrfs_root
*root
)
6140 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
6141 struct btrfs_block_group_cache
*block_group
, *tmp
;
6142 struct list_head
*deleted_bgs
;
6143 struct extent_io_tree
*unpin
;
6148 if (fs_info
->pinned_extents
== &fs_info
->freed_extents
[0])
6149 unpin
= &fs_info
->freed_extents
[1];
6151 unpin
= &fs_info
->freed_extents
[0];
6153 while (!trans
->aborted
) {
6154 mutex_lock(&fs_info
->unused_bg_unpin_mutex
);
6155 ret
= find_first_extent_bit(unpin
, 0, &start
, &end
,
6156 EXTENT_DIRTY
, NULL
);
6158 mutex_unlock(&fs_info
->unused_bg_unpin_mutex
);
6162 if (btrfs_test_opt(root
, DISCARD
))
6163 ret
= btrfs_discard_extent(root
, start
,
6164 end
+ 1 - start
, NULL
);
6166 clear_extent_dirty(unpin
, start
, end
, GFP_NOFS
);
6167 unpin_extent_range(root
, start
, end
, true);
6168 mutex_unlock(&fs_info
->unused_bg_unpin_mutex
);
6173 * Transaction is finished. We don't need the lock anymore. We
6174 * do need to clean up the block groups in case of a transaction
6177 deleted_bgs
= &trans
->transaction
->deleted_bgs
;
6178 list_for_each_entry_safe(block_group
, tmp
, deleted_bgs
, bg_list
) {
6182 if (!trans
->aborted
)
6183 ret
= btrfs_discard_extent(root
,
6184 block_group
->key
.objectid
,
6185 block_group
->key
.offset
,
6188 list_del_init(&block_group
->bg_list
);
6189 btrfs_put_block_group_trimming(block_group
);
6190 btrfs_put_block_group(block_group
);
6193 const char *errstr
= btrfs_decode_error(ret
);
6195 "Discard failed while removing blockgroup: errno=%d %s\n",
6203 static void add_pinned_bytes(struct btrfs_fs_info
*fs_info
, u64 num_bytes
,
6204 u64 owner
, u64 root_objectid
)
6206 struct btrfs_space_info
*space_info
;
6209 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
6210 if (root_objectid
== BTRFS_CHUNK_TREE_OBJECTID
)
6211 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
6213 flags
= BTRFS_BLOCK_GROUP_METADATA
;
6215 flags
= BTRFS_BLOCK_GROUP_DATA
;
6218 space_info
= __find_space_info(fs_info
, flags
);
6219 BUG_ON(!space_info
); /* Logic bug */
6220 percpu_counter_add(&space_info
->total_bytes_pinned
, num_bytes
);
6224 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
6225 struct btrfs_root
*root
,
6226 struct btrfs_delayed_ref_node
*node
, u64 parent
,
6227 u64 root_objectid
, u64 owner_objectid
,
6228 u64 owner_offset
, int refs_to_drop
,
6229 struct btrfs_delayed_extent_op
*extent_op
)
6231 struct btrfs_key key
;
6232 struct btrfs_path
*path
;
6233 struct btrfs_fs_info
*info
= root
->fs_info
;
6234 struct btrfs_root
*extent_root
= info
->extent_root
;
6235 struct extent_buffer
*leaf
;
6236 struct btrfs_extent_item
*ei
;
6237 struct btrfs_extent_inline_ref
*iref
;
6240 int extent_slot
= 0;
6241 int found_extent
= 0;
6243 int no_quota
= node
->no_quota
;
6246 u64 bytenr
= node
->bytenr
;
6247 u64 num_bytes
= node
->num_bytes
;
6249 bool skinny_metadata
= btrfs_fs_incompat(root
->fs_info
,
6252 if (!info
->quota_enabled
|| !is_fstree(root_objectid
))
6255 path
= btrfs_alloc_path();
6260 path
->leave_spinning
= 1;
6262 is_data
= owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
;
6263 BUG_ON(!is_data
&& refs_to_drop
!= 1);
6266 skinny_metadata
= 0;
6268 ret
= lookup_extent_backref(trans
, extent_root
, path
, &iref
,
6269 bytenr
, num_bytes
, parent
,
6270 root_objectid
, owner_objectid
,
6273 extent_slot
= path
->slots
[0];
6274 while (extent_slot
>= 0) {
6275 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
6277 if (key
.objectid
!= bytenr
)
6279 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
6280 key
.offset
== num_bytes
) {
6284 if (key
.type
== BTRFS_METADATA_ITEM_KEY
&&
6285 key
.offset
== owner_objectid
) {
6289 if (path
->slots
[0] - extent_slot
> 5)
6293 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6294 item_size
= btrfs_item_size_nr(path
->nodes
[0], extent_slot
);
6295 if (found_extent
&& item_size
< sizeof(*ei
))
6298 if (!found_extent
) {
6300 ret
= remove_extent_backref(trans
, extent_root
, path
,
6302 is_data
, &last_ref
);
6304 btrfs_abort_transaction(trans
, extent_root
, ret
);
6307 btrfs_release_path(path
);
6308 path
->leave_spinning
= 1;
6310 key
.objectid
= bytenr
;
6311 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
6312 key
.offset
= num_bytes
;
6314 if (!is_data
&& skinny_metadata
) {
6315 key
.type
= BTRFS_METADATA_ITEM_KEY
;
6316 key
.offset
= owner_objectid
;
6319 ret
= btrfs_search_slot(trans
, extent_root
,
6321 if (ret
> 0 && skinny_metadata
&& path
->slots
[0]) {
6323 * Couldn't find our skinny metadata item,
6324 * see if we have ye olde extent item.
6327 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
6329 if (key
.objectid
== bytenr
&&
6330 key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
6331 key
.offset
== num_bytes
)
6335 if (ret
> 0 && skinny_metadata
) {
6336 skinny_metadata
= false;
6337 key
.objectid
= bytenr
;
6338 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
6339 key
.offset
= num_bytes
;
6340 btrfs_release_path(path
);
6341 ret
= btrfs_search_slot(trans
, extent_root
,
6346 btrfs_err(info
, "umm, got %d back from search, was looking for %llu",
6349 btrfs_print_leaf(extent_root
,
6353 btrfs_abort_transaction(trans
, extent_root
, ret
);
6356 extent_slot
= path
->slots
[0];
6358 } else if (WARN_ON(ret
== -ENOENT
)) {
6359 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
6361 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
6362 bytenr
, parent
, root_objectid
, owner_objectid
,
6364 btrfs_abort_transaction(trans
, extent_root
, ret
);
6367 btrfs_abort_transaction(trans
, extent_root
, ret
);
6371 leaf
= path
->nodes
[0];
6372 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
6373 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6374 if (item_size
< sizeof(*ei
)) {
6375 BUG_ON(found_extent
|| extent_slot
!= path
->slots
[0]);
6376 ret
= convert_extent_item_v0(trans
, extent_root
, path
,
6379 btrfs_abort_transaction(trans
, extent_root
, ret
);
6383 btrfs_release_path(path
);
6384 path
->leave_spinning
= 1;
6386 key
.objectid
= bytenr
;
6387 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
6388 key
.offset
= num_bytes
;
6390 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
,
6393 btrfs_err(info
, "umm, got %d back from search, was looking for %llu",
6395 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
6398 btrfs_abort_transaction(trans
, extent_root
, ret
);
6402 extent_slot
= path
->slots
[0];
6403 leaf
= path
->nodes
[0];
6404 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
6407 BUG_ON(item_size
< sizeof(*ei
));
6408 ei
= btrfs_item_ptr(leaf
, extent_slot
,
6409 struct btrfs_extent_item
);
6410 if (owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
&&
6411 key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
6412 struct btrfs_tree_block_info
*bi
;
6413 BUG_ON(item_size
< sizeof(*ei
) + sizeof(*bi
));
6414 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
6415 WARN_ON(owner_objectid
!= btrfs_tree_block_level(leaf
, bi
));
6418 refs
= btrfs_extent_refs(leaf
, ei
);
6419 if (refs
< refs_to_drop
) {
6420 btrfs_err(info
, "trying to drop %d refs but we only have %Lu "
6421 "for bytenr %Lu", refs_to_drop
, refs
, bytenr
);
6423 btrfs_abort_transaction(trans
, extent_root
, ret
);
6426 refs
-= refs_to_drop
;
6430 __run_delayed_extent_op(extent_op
, leaf
, ei
);
6432 * In the case of inline back ref, reference count will
6433 * be updated by remove_extent_backref
6436 BUG_ON(!found_extent
);
6438 btrfs_set_extent_refs(leaf
, ei
, refs
);
6439 btrfs_mark_buffer_dirty(leaf
);
6442 ret
= remove_extent_backref(trans
, extent_root
, path
,
6444 is_data
, &last_ref
);
6446 btrfs_abort_transaction(trans
, extent_root
, ret
);
6450 add_pinned_bytes(root
->fs_info
, -num_bytes
, owner_objectid
,
6454 BUG_ON(is_data
&& refs_to_drop
!=
6455 extent_data_ref_count(path
, iref
));
6457 BUG_ON(path
->slots
[0] != extent_slot
);
6459 BUG_ON(path
->slots
[0] != extent_slot
+ 1);
6460 path
->slots
[0] = extent_slot
;
6466 ret
= btrfs_del_items(trans
, extent_root
, path
, path
->slots
[0],
6469 btrfs_abort_transaction(trans
, extent_root
, ret
);
6472 btrfs_release_path(path
);
6475 ret
= btrfs_del_csums(trans
, root
, bytenr
, num_bytes
);
6477 btrfs_abort_transaction(trans
, extent_root
, ret
);
6482 ret
= update_block_group(trans
, root
, bytenr
, num_bytes
, 0);
6484 btrfs_abort_transaction(trans
, extent_root
, ret
);
6488 btrfs_release_path(path
);
6491 btrfs_free_path(path
);
6496 * when we free an block, it is possible (and likely) that we free the last
6497 * delayed ref for that extent as well. This searches the delayed ref tree for
6498 * a given extent, and if there are no other delayed refs to be processed, it
6499 * removes it from the tree.
6501 static noinline
int check_ref_cleanup(struct btrfs_trans_handle
*trans
,
6502 struct btrfs_root
*root
, u64 bytenr
)
6504 struct btrfs_delayed_ref_head
*head
;
6505 struct btrfs_delayed_ref_root
*delayed_refs
;
6508 delayed_refs
= &trans
->transaction
->delayed_refs
;
6509 spin_lock(&delayed_refs
->lock
);
6510 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
6512 goto out_delayed_unlock
;
6514 spin_lock(&head
->lock
);
6515 if (!list_empty(&head
->ref_list
))
6518 if (head
->extent_op
) {
6519 if (!head
->must_insert_reserved
)
6521 btrfs_free_delayed_extent_op(head
->extent_op
);
6522 head
->extent_op
= NULL
;
6526 * waiting for the lock here would deadlock. If someone else has it
6527 * locked they are already in the process of dropping it anyway
6529 if (!mutex_trylock(&head
->mutex
))
6533 * at this point we have a head with no other entries. Go
6534 * ahead and process it.
6536 head
->node
.in_tree
= 0;
6537 rb_erase(&head
->href_node
, &delayed_refs
->href_root
);
6539 atomic_dec(&delayed_refs
->num_entries
);
6542 * we don't take a ref on the node because we're removing it from the
6543 * tree, so we just steal the ref the tree was holding.
6545 delayed_refs
->num_heads
--;
6546 if (head
->processing
== 0)
6547 delayed_refs
->num_heads_ready
--;
6548 head
->processing
= 0;
6549 spin_unlock(&head
->lock
);
6550 spin_unlock(&delayed_refs
->lock
);
6552 BUG_ON(head
->extent_op
);
6553 if (head
->must_insert_reserved
)
6556 mutex_unlock(&head
->mutex
);
6557 btrfs_put_delayed_ref(&head
->node
);
6560 spin_unlock(&head
->lock
);
6563 spin_unlock(&delayed_refs
->lock
);
6567 void btrfs_free_tree_block(struct btrfs_trans_handle
*trans
,
6568 struct btrfs_root
*root
,
6569 struct extent_buffer
*buf
,
6570 u64 parent
, int last_ref
)
6575 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
6576 ret
= btrfs_add_delayed_tree_ref(root
->fs_info
, trans
,
6577 buf
->start
, buf
->len
,
6578 parent
, root
->root_key
.objectid
,
6579 btrfs_header_level(buf
),
6580 BTRFS_DROP_DELAYED_REF
, NULL
, 0);
6581 BUG_ON(ret
); /* -ENOMEM */
6587 if (btrfs_header_generation(buf
) == trans
->transid
) {
6588 struct btrfs_block_group_cache
*cache
;
6590 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
6591 ret
= check_ref_cleanup(trans
, root
, buf
->start
);
6596 cache
= btrfs_lookup_block_group(root
->fs_info
, buf
->start
);
6598 if (btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
)) {
6599 pin_down_extent(root
, cache
, buf
->start
, buf
->len
, 1);
6600 btrfs_put_block_group(cache
);
6604 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY
, &buf
->bflags
));
6606 btrfs_add_free_space(cache
, buf
->start
, buf
->len
);
6607 btrfs_update_reserved_bytes(cache
, buf
->len
, RESERVE_FREE
, 0);
6608 btrfs_put_block_group(cache
);
6609 trace_btrfs_reserved_extent_free(root
, buf
->start
, buf
->len
);
6614 add_pinned_bytes(root
->fs_info
, buf
->len
,
6615 btrfs_header_level(buf
),
6616 root
->root_key
.objectid
);
6619 * Deleting the buffer, clear the corrupt flag since it doesn't matter
6622 clear_bit(EXTENT_BUFFER_CORRUPT
, &buf
->bflags
);
6625 /* Can return -ENOMEM */
6626 int btrfs_free_extent(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
6627 u64 bytenr
, u64 num_bytes
, u64 parent
, u64 root_objectid
,
6628 u64 owner
, u64 offset
, int no_quota
)
6631 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
6633 if (btrfs_test_is_dummy_root(root
))
6636 add_pinned_bytes(root
->fs_info
, num_bytes
, owner
, root_objectid
);
6639 * tree log blocks never actually go into the extent allocation
6640 * tree, just update pinning info and exit early.
6642 if (root_objectid
== BTRFS_TREE_LOG_OBJECTID
) {
6643 WARN_ON(owner
>= BTRFS_FIRST_FREE_OBJECTID
);
6644 /* unlocks the pinned mutex */
6645 btrfs_pin_extent(root
, bytenr
, num_bytes
, 1);
6647 } else if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
6648 ret
= btrfs_add_delayed_tree_ref(fs_info
, trans
, bytenr
,
6650 parent
, root_objectid
, (int)owner
,
6651 BTRFS_DROP_DELAYED_REF
, NULL
, no_quota
);
6653 ret
= btrfs_add_delayed_data_ref(fs_info
, trans
, bytenr
,
6655 parent
, root_objectid
, owner
,
6656 offset
, BTRFS_DROP_DELAYED_REF
,
6663 * when we wait for progress in the block group caching, its because
6664 * our allocation attempt failed at least once. So, we must sleep
6665 * and let some progress happen before we try again.
6667 * This function will sleep at least once waiting for new free space to
6668 * show up, and then it will check the block group free space numbers
6669 * for our min num_bytes. Another option is to have it go ahead
6670 * and look in the rbtree for a free extent of a given size, but this
6673 * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
6674 * any of the information in this block group.
6676 static noinline
void
6677 wait_block_group_cache_progress(struct btrfs_block_group_cache
*cache
,
6680 struct btrfs_caching_control
*caching_ctl
;
6682 caching_ctl
= get_caching_control(cache
);
6686 wait_event(caching_ctl
->wait
, block_group_cache_done(cache
) ||
6687 (cache
->free_space_ctl
->free_space
>= num_bytes
));
6689 put_caching_control(caching_ctl
);
6693 wait_block_group_cache_done(struct btrfs_block_group_cache
*cache
)
6695 struct btrfs_caching_control
*caching_ctl
;
6698 caching_ctl
= get_caching_control(cache
);
6700 return (cache
->cached
== BTRFS_CACHE_ERROR
) ? -EIO
: 0;
6702 wait_event(caching_ctl
->wait
, block_group_cache_done(cache
));
6703 if (cache
->cached
== BTRFS_CACHE_ERROR
)
6705 put_caching_control(caching_ctl
);
6709 int __get_raid_index(u64 flags
)
6711 if (flags
& BTRFS_BLOCK_GROUP_RAID10
)
6712 return BTRFS_RAID_RAID10
;
6713 else if (flags
& BTRFS_BLOCK_GROUP_RAID1
)
6714 return BTRFS_RAID_RAID1
;
6715 else if (flags
& BTRFS_BLOCK_GROUP_DUP
)
6716 return BTRFS_RAID_DUP
;
6717 else if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
6718 return BTRFS_RAID_RAID0
;
6719 else if (flags
& BTRFS_BLOCK_GROUP_RAID5
)
6720 return BTRFS_RAID_RAID5
;
6721 else if (flags
& BTRFS_BLOCK_GROUP_RAID6
)
6722 return BTRFS_RAID_RAID6
;
6724 return BTRFS_RAID_SINGLE
; /* BTRFS_BLOCK_GROUP_SINGLE */
6727 int get_block_group_index(struct btrfs_block_group_cache
*cache
)
6729 return __get_raid_index(cache
->flags
);
6732 static const char *btrfs_raid_type_names
[BTRFS_NR_RAID_TYPES
] = {
6733 [BTRFS_RAID_RAID10
] = "raid10",
6734 [BTRFS_RAID_RAID1
] = "raid1",
6735 [BTRFS_RAID_DUP
] = "dup",
6736 [BTRFS_RAID_RAID0
] = "raid0",
6737 [BTRFS_RAID_SINGLE
] = "single",
6738 [BTRFS_RAID_RAID5
] = "raid5",
6739 [BTRFS_RAID_RAID6
] = "raid6",
6742 static const char *get_raid_name(enum btrfs_raid_types type
)
6744 if (type
>= BTRFS_NR_RAID_TYPES
)
6747 return btrfs_raid_type_names
[type
];
6750 enum btrfs_loop_type
{
6751 LOOP_CACHING_NOWAIT
= 0,
6752 LOOP_CACHING_WAIT
= 1,
6753 LOOP_ALLOC_CHUNK
= 2,
6754 LOOP_NO_EMPTY_SIZE
= 3,
6758 btrfs_lock_block_group(struct btrfs_block_group_cache
*cache
,
6762 down_read(&cache
->data_rwsem
);
6766 btrfs_grab_block_group(struct btrfs_block_group_cache
*cache
,
6769 btrfs_get_block_group(cache
);
6771 down_read(&cache
->data_rwsem
);
6774 static struct btrfs_block_group_cache
*
6775 btrfs_lock_cluster(struct btrfs_block_group_cache
*block_group
,
6776 struct btrfs_free_cluster
*cluster
,
6779 struct btrfs_block_group_cache
*used_bg
;
6780 bool locked
= false;
6782 spin_lock(&cluster
->refill_lock
);
6784 if (used_bg
== cluster
->block_group
)
6787 up_read(&used_bg
->data_rwsem
);
6788 btrfs_put_block_group(used_bg
);
6791 used_bg
= cluster
->block_group
;
6795 if (used_bg
== block_group
)
6798 btrfs_get_block_group(used_bg
);
6803 if (down_read_trylock(&used_bg
->data_rwsem
))
6806 spin_unlock(&cluster
->refill_lock
);
6807 down_read(&used_bg
->data_rwsem
);
6813 btrfs_release_block_group(struct btrfs_block_group_cache
*cache
,
6817 up_read(&cache
->data_rwsem
);
6818 btrfs_put_block_group(cache
);
6822 * walks the btree of allocated extents and find a hole of a given size.
6823 * The key ins is changed to record the hole:
6824 * ins->objectid == start position
6825 * ins->flags = BTRFS_EXTENT_ITEM_KEY
6826 * ins->offset == the size of the hole.
6827 * Any available blocks before search_start are skipped.
6829 * If there is no suitable free space, we will record the max size of
6830 * the free space extent currently.
6832 static noinline
int find_free_extent(struct btrfs_root
*orig_root
,
6833 u64 num_bytes
, u64 empty_size
,
6834 u64 hint_byte
, struct btrfs_key
*ins
,
6835 u64 flags
, int delalloc
)
6838 struct btrfs_root
*root
= orig_root
->fs_info
->extent_root
;
6839 struct btrfs_free_cluster
*last_ptr
= NULL
;
6840 struct btrfs_block_group_cache
*block_group
= NULL
;
6841 u64 search_start
= 0;
6842 u64 max_extent_size
= 0;
6843 int empty_cluster
= 2 * 1024 * 1024;
6844 struct btrfs_space_info
*space_info
;
6846 int index
= __get_raid_index(flags
);
6847 int alloc_type
= (flags
& BTRFS_BLOCK_GROUP_DATA
) ?
6848 RESERVE_ALLOC_NO_ACCOUNT
: RESERVE_ALLOC
;
6849 bool failed_cluster_refill
= false;
6850 bool failed_alloc
= false;
6851 bool use_cluster
= true;
6852 bool have_caching_bg
= false;
6854 WARN_ON(num_bytes
< root
->sectorsize
);
6855 ins
->type
= BTRFS_EXTENT_ITEM_KEY
;
6859 trace_find_free_extent(orig_root
, num_bytes
, empty_size
, flags
);
6861 space_info
= __find_space_info(root
->fs_info
, flags
);
6863 btrfs_err(root
->fs_info
, "No space info for %llu", flags
);
6868 * If the space info is for both data and metadata it means we have a
6869 * small filesystem and we can't use the clustering stuff.
6871 if (btrfs_mixed_space_info(space_info
))
6872 use_cluster
= false;
6874 if (flags
& BTRFS_BLOCK_GROUP_METADATA
&& use_cluster
) {
6875 last_ptr
= &root
->fs_info
->meta_alloc_cluster
;
6876 if (!btrfs_test_opt(root
, SSD
))
6877 empty_cluster
= 64 * 1024;
6880 if ((flags
& BTRFS_BLOCK_GROUP_DATA
) && use_cluster
&&
6881 btrfs_test_opt(root
, SSD
)) {
6882 last_ptr
= &root
->fs_info
->data_alloc_cluster
;
6886 spin_lock(&last_ptr
->lock
);
6887 if (last_ptr
->block_group
)
6888 hint_byte
= last_ptr
->window_start
;
6889 spin_unlock(&last_ptr
->lock
);
6892 search_start
= max(search_start
, first_logical_byte(root
, 0));
6893 search_start
= max(search_start
, hint_byte
);
6898 if (search_start
== hint_byte
) {
6899 block_group
= btrfs_lookup_block_group(root
->fs_info
,
6902 * we don't want to use the block group if it doesn't match our
6903 * allocation bits, or if its not cached.
6905 * However if we are re-searching with an ideal block group
6906 * picked out then we don't care that the block group is cached.
6908 if (block_group
&& block_group_bits(block_group
, flags
) &&
6909 block_group
->cached
!= BTRFS_CACHE_NO
) {
6910 down_read(&space_info
->groups_sem
);
6911 if (list_empty(&block_group
->list
) ||
6914 * someone is removing this block group,
6915 * we can't jump into the have_block_group
6916 * target because our list pointers are not
6919 btrfs_put_block_group(block_group
);
6920 up_read(&space_info
->groups_sem
);
6922 index
= get_block_group_index(block_group
);
6923 btrfs_lock_block_group(block_group
, delalloc
);
6924 goto have_block_group
;
6926 } else if (block_group
) {
6927 btrfs_put_block_group(block_group
);
6931 have_caching_bg
= false;
6932 down_read(&space_info
->groups_sem
);
6933 list_for_each_entry(block_group
, &space_info
->block_groups
[index
],
6938 btrfs_grab_block_group(block_group
, delalloc
);
6939 search_start
= block_group
->key
.objectid
;
6942 * this can happen if we end up cycling through all the
6943 * raid types, but we want to make sure we only allocate
6944 * for the proper type.
6946 if (!block_group_bits(block_group
, flags
)) {
6947 u64 extra
= BTRFS_BLOCK_GROUP_DUP
|
6948 BTRFS_BLOCK_GROUP_RAID1
|
6949 BTRFS_BLOCK_GROUP_RAID5
|
6950 BTRFS_BLOCK_GROUP_RAID6
|
6951 BTRFS_BLOCK_GROUP_RAID10
;
6954 * if they asked for extra copies and this block group
6955 * doesn't provide them, bail. This does allow us to
6956 * fill raid0 from raid1.
6958 if ((flags
& extra
) && !(block_group
->flags
& extra
))
6963 cached
= block_group_cache_done(block_group
);
6964 if (unlikely(!cached
)) {
6965 ret
= cache_block_group(block_group
, 0);
6970 if (unlikely(block_group
->cached
== BTRFS_CACHE_ERROR
))
6972 if (unlikely(block_group
->ro
))
6976 * Ok we want to try and use the cluster allocator, so
6980 struct btrfs_block_group_cache
*used_block_group
;
6981 unsigned long aligned_cluster
;
6983 * the refill lock keeps out other
6984 * people trying to start a new cluster
6986 used_block_group
= btrfs_lock_cluster(block_group
,
6989 if (!used_block_group
)
6990 goto refill_cluster
;
6992 if (used_block_group
!= block_group
&&
6993 (used_block_group
->ro
||
6994 !block_group_bits(used_block_group
, flags
)))
6995 goto release_cluster
;
6997 offset
= btrfs_alloc_from_cluster(used_block_group
,
7000 used_block_group
->key
.objectid
,
7003 /* we have a block, we're done */
7004 spin_unlock(&last_ptr
->refill_lock
);
7005 trace_btrfs_reserve_extent_cluster(root
,
7007 search_start
, num_bytes
);
7008 if (used_block_group
!= block_group
) {
7009 btrfs_release_block_group(block_group
,
7011 block_group
= used_block_group
;
7016 WARN_ON(last_ptr
->block_group
!= used_block_group
);
7018 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
7019 * set up a new clusters, so lets just skip it
7020 * and let the allocator find whatever block
7021 * it can find. If we reach this point, we
7022 * will have tried the cluster allocator
7023 * plenty of times and not have found
7024 * anything, so we are likely way too
7025 * fragmented for the clustering stuff to find
7028 * However, if the cluster is taken from the
7029 * current block group, release the cluster
7030 * first, so that we stand a better chance of
7031 * succeeding in the unclustered
7033 if (loop
>= LOOP_NO_EMPTY_SIZE
&&
7034 used_block_group
!= block_group
) {
7035 spin_unlock(&last_ptr
->refill_lock
);
7036 btrfs_release_block_group(used_block_group
,
7038 goto unclustered_alloc
;
7042 * this cluster didn't work out, free it and
7045 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
7047 if (used_block_group
!= block_group
)
7048 btrfs_release_block_group(used_block_group
,
7051 if (loop
>= LOOP_NO_EMPTY_SIZE
) {
7052 spin_unlock(&last_ptr
->refill_lock
);
7053 goto unclustered_alloc
;
7056 aligned_cluster
= max_t(unsigned long,
7057 empty_cluster
+ empty_size
,
7058 block_group
->full_stripe_len
);
7060 /* allocate a cluster in this block group */
7061 ret
= btrfs_find_space_cluster(root
, block_group
,
7062 last_ptr
, search_start
,
7067 * now pull our allocation out of this
7070 offset
= btrfs_alloc_from_cluster(block_group
,
7076 /* we found one, proceed */
7077 spin_unlock(&last_ptr
->refill_lock
);
7078 trace_btrfs_reserve_extent_cluster(root
,
7079 block_group
, search_start
,
7083 } else if (!cached
&& loop
> LOOP_CACHING_NOWAIT
7084 && !failed_cluster_refill
) {
7085 spin_unlock(&last_ptr
->refill_lock
);
7087 failed_cluster_refill
= true;
7088 wait_block_group_cache_progress(block_group
,
7089 num_bytes
+ empty_cluster
+ empty_size
);
7090 goto have_block_group
;
7094 * at this point we either didn't find a cluster
7095 * or we weren't able to allocate a block from our
7096 * cluster. Free the cluster we've been trying
7097 * to use, and go to the next block group
7099 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
7100 spin_unlock(&last_ptr
->refill_lock
);
7105 spin_lock(&block_group
->free_space_ctl
->tree_lock
);
7107 block_group
->free_space_ctl
->free_space
<
7108 num_bytes
+ empty_cluster
+ empty_size
) {
7109 if (block_group
->free_space_ctl
->free_space
>
7112 block_group
->free_space_ctl
->free_space
;
7113 spin_unlock(&block_group
->free_space_ctl
->tree_lock
);
7116 spin_unlock(&block_group
->free_space_ctl
->tree_lock
);
7118 offset
= btrfs_find_space_for_alloc(block_group
, search_start
,
7119 num_bytes
, empty_size
,
7122 * If we didn't find a chunk, and we haven't failed on this
7123 * block group before, and this block group is in the middle of
7124 * caching and we are ok with waiting, then go ahead and wait
7125 * for progress to be made, and set failed_alloc to true.
7127 * If failed_alloc is true then we've already waited on this
7128 * block group once and should move on to the next block group.
7130 if (!offset
&& !failed_alloc
&& !cached
&&
7131 loop
> LOOP_CACHING_NOWAIT
) {
7132 wait_block_group_cache_progress(block_group
,
7133 num_bytes
+ empty_size
);
7134 failed_alloc
= true;
7135 goto have_block_group
;
7136 } else if (!offset
) {
7138 have_caching_bg
= true;
7142 search_start
= ALIGN(offset
, root
->stripesize
);
7144 /* move on to the next group */
7145 if (search_start
+ num_bytes
>
7146 block_group
->key
.objectid
+ block_group
->key
.offset
) {
7147 btrfs_add_free_space(block_group
, offset
, num_bytes
);
7151 if (offset
< search_start
)
7152 btrfs_add_free_space(block_group
, offset
,
7153 search_start
- offset
);
7154 BUG_ON(offset
> search_start
);
7156 ret
= btrfs_update_reserved_bytes(block_group
, num_bytes
,
7157 alloc_type
, delalloc
);
7158 if (ret
== -EAGAIN
) {
7159 btrfs_add_free_space(block_group
, offset
, num_bytes
);
7163 /* we are all good, lets return */
7164 ins
->objectid
= search_start
;
7165 ins
->offset
= num_bytes
;
7167 trace_btrfs_reserve_extent(orig_root
, block_group
,
7168 search_start
, num_bytes
);
7169 btrfs_release_block_group(block_group
, delalloc
);
7172 failed_cluster_refill
= false;
7173 failed_alloc
= false;
7174 BUG_ON(index
!= get_block_group_index(block_group
));
7175 btrfs_release_block_group(block_group
, delalloc
);
7177 up_read(&space_info
->groups_sem
);
7179 if (!ins
->objectid
&& loop
>= LOOP_CACHING_WAIT
&& have_caching_bg
)
7182 if (!ins
->objectid
&& ++index
< BTRFS_NR_RAID_TYPES
)
7186 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
7187 * caching kthreads as we move along
7188 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
7189 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
7190 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
7193 if (!ins
->objectid
&& loop
< LOOP_NO_EMPTY_SIZE
) {
7196 if (loop
== LOOP_ALLOC_CHUNK
) {
7197 struct btrfs_trans_handle
*trans
;
7200 trans
= current
->journal_info
;
7204 trans
= btrfs_join_transaction(root
);
7206 if (IS_ERR(trans
)) {
7207 ret
= PTR_ERR(trans
);
7211 ret
= do_chunk_alloc(trans
, root
, flags
,
7214 * Do not bail out on ENOSPC since we
7215 * can do more things.
7217 if (ret
< 0 && ret
!= -ENOSPC
)
7218 btrfs_abort_transaction(trans
,
7223 btrfs_end_transaction(trans
, root
);
7228 if (loop
== LOOP_NO_EMPTY_SIZE
) {
7234 } else if (!ins
->objectid
) {
7236 } else if (ins
->objectid
) {
7241 ins
->offset
= max_extent_size
;
7245 static void dump_space_info(struct btrfs_space_info
*info
, u64 bytes
,
7246 int dump_block_groups
)
7248 struct btrfs_block_group_cache
*cache
;
7251 spin_lock(&info
->lock
);
7252 printk(KERN_INFO
"BTRFS: space_info %llu has %llu free, is %sfull\n",
7254 info
->total_bytes
- info
->bytes_used
- info
->bytes_pinned
-
7255 info
->bytes_reserved
- info
->bytes_readonly
,
7256 (info
->full
) ? "" : "not ");
7257 printk(KERN_INFO
"BTRFS: space_info total=%llu, used=%llu, pinned=%llu, "
7258 "reserved=%llu, may_use=%llu, readonly=%llu\n",
7259 info
->total_bytes
, info
->bytes_used
, info
->bytes_pinned
,
7260 info
->bytes_reserved
, info
->bytes_may_use
,
7261 info
->bytes_readonly
);
7262 spin_unlock(&info
->lock
);
7264 if (!dump_block_groups
)
7267 down_read(&info
->groups_sem
);
7269 list_for_each_entry(cache
, &info
->block_groups
[index
], list
) {
7270 spin_lock(&cache
->lock
);
7271 printk(KERN_INFO
"BTRFS: "
7272 "block group %llu has %llu bytes, "
7273 "%llu used %llu pinned %llu reserved %s\n",
7274 cache
->key
.objectid
, cache
->key
.offset
,
7275 btrfs_block_group_used(&cache
->item
), cache
->pinned
,
7276 cache
->reserved
, cache
->ro
? "[readonly]" : "");
7277 btrfs_dump_free_space(cache
, bytes
);
7278 spin_unlock(&cache
->lock
);
7280 if (++index
< BTRFS_NR_RAID_TYPES
)
7282 up_read(&info
->groups_sem
);
7285 int btrfs_reserve_extent(struct btrfs_root
*root
,
7286 u64 num_bytes
, u64 min_alloc_size
,
7287 u64 empty_size
, u64 hint_byte
,
7288 struct btrfs_key
*ins
, int is_data
, int delalloc
)
7290 bool final_tried
= false;
7294 flags
= btrfs_get_alloc_profile(root
, is_data
);
7296 WARN_ON(num_bytes
< root
->sectorsize
);
7297 ret
= find_free_extent(root
, num_bytes
, empty_size
, hint_byte
, ins
,
7300 if (ret
== -ENOSPC
) {
7301 if (!final_tried
&& ins
->offset
) {
7302 num_bytes
= min(num_bytes
>> 1, ins
->offset
);
7303 num_bytes
= round_down(num_bytes
, root
->sectorsize
);
7304 num_bytes
= max(num_bytes
, min_alloc_size
);
7305 if (num_bytes
== min_alloc_size
)
7308 } else if (btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
7309 struct btrfs_space_info
*sinfo
;
7311 sinfo
= __find_space_info(root
->fs_info
, flags
);
7312 btrfs_err(root
->fs_info
, "allocation failed flags %llu, wanted %llu",
7315 dump_space_info(sinfo
, num_bytes
, 1);
7322 static int __btrfs_free_reserved_extent(struct btrfs_root
*root
,
7324 int pin
, int delalloc
)
7326 struct btrfs_block_group_cache
*cache
;
7329 cache
= btrfs_lookup_block_group(root
->fs_info
, start
);
7331 btrfs_err(root
->fs_info
, "Unable to find block group for %llu",
7337 pin_down_extent(root
, cache
, start
, len
, 1);
7339 if (btrfs_test_opt(root
, DISCARD
))
7340 ret
= btrfs_discard_extent(root
, start
, len
, NULL
);
7341 btrfs_add_free_space(cache
, start
, len
);
7342 btrfs_update_reserved_bytes(cache
, len
, RESERVE_FREE
, delalloc
);
7345 btrfs_put_block_group(cache
);
7347 trace_btrfs_reserved_extent_free(root
, start
, len
);
7352 int btrfs_free_reserved_extent(struct btrfs_root
*root
,
7353 u64 start
, u64 len
, int delalloc
)
7355 return __btrfs_free_reserved_extent(root
, start
, len
, 0, delalloc
);
7358 int btrfs_free_and_pin_reserved_extent(struct btrfs_root
*root
,
7361 return __btrfs_free_reserved_extent(root
, start
, len
, 1, 0);
7364 static int alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
7365 struct btrfs_root
*root
,
7366 u64 parent
, u64 root_objectid
,
7367 u64 flags
, u64 owner
, u64 offset
,
7368 struct btrfs_key
*ins
, int ref_mod
)
7371 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
7372 struct btrfs_extent_item
*extent_item
;
7373 struct btrfs_extent_inline_ref
*iref
;
7374 struct btrfs_path
*path
;
7375 struct extent_buffer
*leaf
;
7380 type
= BTRFS_SHARED_DATA_REF_KEY
;
7382 type
= BTRFS_EXTENT_DATA_REF_KEY
;
7384 size
= sizeof(*extent_item
) + btrfs_extent_inline_ref_size(type
);
7386 path
= btrfs_alloc_path();
7390 path
->leave_spinning
= 1;
7391 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
7394 btrfs_free_path(path
);
7398 leaf
= path
->nodes
[0];
7399 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
7400 struct btrfs_extent_item
);
7401 btrfs_set_extent_refs(leaf
, extent_item
, ref_mod
);
7402 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
7403 btrfs_set_extent_flags(leaf
, extent_item
,
7404 flags
| BTRFS_EXTENT_FLAG_DATA
);
7406 iref
= (struct btrfs_extent_inline_ref
*)(extent_item
+ 1);
7407 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
7409 struct btrfs_shared_data_ref
*ref
;
7410 ref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
7411 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
7412 btrfs_set_shared_data_ref_count(leaf
, ref
, ref_mod
);
7414 struct btrfs_extent_data_ref
*ref
;
7415 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
7416 btrfs_set_extent_data_ref_root(leaf
, ref
, root_objectid
);
7417 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
7418 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
7419 btrfs_set_extent_data_ref_count(leaf
, ref
, ref_mod
);
7422 btrfs_mark_buffer_dirty(path
->nodes
[0]);
7423 btrfs_free_path(path
);
7425 ret
= update_block_group(trans
, root
, ins
->objectid
, ins
->offset
, 1);
7426 if (ret
) { /* -ENOENT, logic error */
7427 btrfs_err(fs_info
, "update block group failed for %llu %llu",
7428 ins
->objectid
, ins
->offset
);
7431 trace_btrfs_reserved_extent_alloc(root
, ins
->objectid
, ins
->offset
);
7435 static int alloc_reserved_tree_block(struct btrfs_trans_handle
*trans
,
7436 struct btrfs_root
*root
,
7437 u64 parent
, u64 root_objectid
,
7438 u64 flags
, struct btrfs_disk_key
*key
,
7439 int level
, struct btrfs_key
*ins
,
7443 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
7444 struct btrfs_extent_item
*extent_item
;
7445 struct btrfs_tree_block_info
*block_info
;
7446 struct btrfs_extent_inline_ref
*iref
;
7447 struct btrfs_path
*path
;
7448 struct extent_buffer
*leaf
;
7449 u32 size
= sizeof(*extent_item
) + sizeof(*iref
);
7450 u64 num_bytes
= ins
->offset
;
7451 bool skinny_metadata
= btrfs_fs_incompat(root
->fs_info
,
7454 if (!skinny_metadata
)
7455 size
+= sizeof(*block_info
);
7457 path
= btrfs_alloc_path();
7459 btrfs_free_and_pin_reserved_extent(root
, ins
->objectid
,
7464 path
->leave_spinning
= 1;
7465 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
7468 btrfs_free_path(path
);
7469 btrfs_free_and_pin_reserved_extent(root
, ins
->objectid
,
7474 leaf
= path
->nodes
[0];
7475 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
7476 struct btrfs_extent_item
);
7477 btrfs_set_extent_refs(leaf
, extent_item
, 1);
7478 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
7479 btrfs_set_extent_flags(leaf
, extent_item
,
7480 flags
| BTRFS_EXTENT_FLAG_TREE_BLOCK
);
7482 if (skinny_metadata
) {
7483 iref
= (struct btrfs_extent_inline_ref
*)(extent_item
+ 1);
7484 num_bytes
= root
->nodesize
;
7486 block_info
= (struct btrfs_tree_block_info
*)(extent_item
+ 1);
7487 btrfs_set_tree_block_key(leaf
, block_info
, key
);
7488 btrfs_set_tree_block_level(leaf
, block_info
, level
);
7489 iref
= (struct btrfs_extent_inline_ref
*)(block_info
+ 1);
7493 BUG_ON(!(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
));
7494 btrfs_set_extent_inline_ref_type(leaf
, iref
,
7495 BTRFS_SHARED_BLOCK_REF_KEY
);
7496 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
7498 btrfs_set_extent_inline_ref_type(leaf
, iref
,
7499 BTRFS_TREE_BLOCK_REF_KEY
);
7500 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
7503 btrfs_mark_buffer_dirty(leaf
);
7504 btrfs_free_path(path
);
7506 ret
= update_block_group(trans
, root
, ins
->objectid
, root
->nodesize
,
7508 if (ret
) { /* -ENOENT, logic error */
7509 btrfs_err(fs_info
, "update block group failed for %llu %llu",
7510 ins
->objectid
, ins
->offset
);
7514 trace_btrfs_reserved_extent_alloc(root
, ins
->objectid
, root
->nodesize
);
7518 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
7519 struct btrfs_root
*root
,
7520 u64 root_objectid
, u64 owner
,
7521 u64 offset
, struct btrfs_key
*ins
)
7525 BUG_ON(root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
7527 ret
= btrfs_add_delayed_data_ref(root
->fs_info
, trans
, ins
->objectid
,
7529 root_objectid
, owner
, offset
,
7530 BTRFS_ADD_DELAYED_EXTENT
, NULL
, 0);
7535 * this is used by the tree logging recovery code. It records that
7536 * an extent has been allocated and makes sure to clear the free
7537 * space cache bits as well
7539 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle
*trans
,
7540 struct btrfs_root
*root
,
7541 u64 root_objectid
, u64 owner
, u64 offset
,
7542 struct btrfs_key
*ins
)
7545 struct btrfs_block_group_cache
*block_group
;
7548 * Mixed block groups will exclude before processing the log so we only
7549 * need to do the exlude dance if this fs isn't mixed.
7551 if (!btrfs_fs_incompat(root
->fs_info
, MIXED_GROUPS
)) {
7552 ret
= __exclude_logged_extent(root
, ins
->objectid
, ins
->offset
);
7557 block_group
= btrfs_lookup_block_group(root
->fs_info
, ins
->objectid
);
7561 ret
= btrfs_update_reserved_bytes(block_group
, ins
->offset
,
7562 RESERVE_ALLOC_NO_ACCOUNT
, 0);
7563 BUG_ON(ret
); /* logic error */
7564 ret
= alloc_reserved_file_extent(trans
, root
, 0, root_objectid
,
7565 0, owner
, offset
, ins
, 1);
7566 btrfs_put_block_group(block_group
);
7570 static struct extent_buffer
*
7571 btrfs_init_new_buffer(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
7572 u64 bytenr
, int level
)
7574 struct extent_buffer
*buf
;
7576 buf
= btrfs_find_create_tree_block(root
, bytenr
);
7578 return ERR_PTR(-ENOMEM
);
7579 btrfs_set_header_generation(buf
, trans
->transid
);
7580 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, buf
, level
);
7581 btrfs_tree_lock(buf
);
7582 clean_tree_block(trans
, root
->fs_info
, buf
);
7583 clear_bit(EXTENT_BUFFER_STALE
, &buf
->bflags
);
7585 btrfs_set_lock_blocking(buf
);
7586 btrfs_set_buffer_uptodate(buf
);
7588 if (root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
) {
7589 buf
->log_index
= root
->log_transid
% 2;
7591 * we allow two log transactions at a time, use different
7592 * EXENT bit to differentiate dirty pages.
7594 if (buf
->log_index
== 0)
7595 set_extent_dirty(&root
->dirty_log_pages
, buf
->start
,
7596 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
7598 set_extent_new(&root
->dirty_log_pages
, buf
->start
,
7599 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
7601 buf
->log_index
= -1;
7602 set_extent_dirty(&trans
->transaction
->dirty_pages
, buf
->start
,
7603 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
7605 trans
->blocks_used
++;
7606 /* this returns a buffer locked for blocking */
7610 static struct btrfs_block_rsv
*
7611 use_block_rsv(struct btrfs_trans_handle
*trans
,
7612 struct btrfs_root
*root
, u32 blocksize
)
7614 struct btrfs_block_rsv
*block_rsv
;
7615 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
7617 bool global_updated
= false;
7619 block_rsv
= get_block_rsv(trans
, root
);
7621 if (unlikely(block_rsv
->size
== 0))
7624 ret
= block_rsv_use_bytes(block_rsv
, blocksize
);
7628 if (block_rsv
->failfast
)
7629 return ERR_PTR(ret
);
7631 if (block_rsv
->type
== BTRFS_BLOCK_RSV_GLOBAL
&& !global_updated
) {
7632 global_updated
= true;
7633 update_global_block_rsv(root
->fs_info
);
7637 if (btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
7638 static DEFINE_RATELIMIT_STATE(_rs
,
7639 DEFAULT_RATELIMIT_INTERVAL
* 10,
7640 /*DEFAULT_RATELIMIT_BURST*/ 1);
7641 if (__ratelimit(&_rs
))
7643 "BTRFS: block rsv returned %d\n", ret
);
7646 ret
= reserve_metadata_bytes(root
, block_rsv
, blocksize
,
7647 BTRFS_RESERVE_NO_FLUSH
);
7651 * If we couldn't reserve metadata bytes try and use some from
7652 * the global reserve if its space type is the same as the global
7655 if (block_rsv
->type
!= BTRFS_BLOCK_RSV_GLOBAL
&&
7656 block_rsv
->space_info
== global_rsv
->space_info
) {
7657 ret
= block_rsv_use_bytes(global_rsv
, blocksize
);
7661 return ERR_PTR(ret
);
7664 static void unuse_block_rsv(struct btrfs_fs_info
*fs_info
,
7665 struct btrfs_block_rsv
*block_rsv
, u32 blocksize
)
7667 block_rsv_add_bytes(block_rsv
, blocksize
, 0);
7668 block_rsv_release_bytes(fs_info
, block_rsv
, NULL
, 0);
7672 * finds a free extent and does all the dirty work required for allocation
7673 * returns the tree buffer or an ERR_PTR on error.
7675 struct extent_buffer
*btrfs_alloc_tree_block(struct btrfs_trans_handle
*trans
,
7676 struct btrfs_root
*root
,
7677 u64 parent
, u64 root_objectid
,
7678 struct btrfs_disk_key
*key
, int level
,
7679 u64 hint
, u64 empty_size
)
7681 struct btrfs_key ins
;
7682 struct btrfs_block_rsv
*block_rsv
;
7683 struct extent_buffer
*buf
;
7684 struct btrfs_delayed_extent_op
*extent_op
;
7687 u32 blocksize
= root
->nodesize
;
7688 bool skinny_metadata
= btrfs_fs_incompat(root
->fs_info
,
7691 if (btrfs_test_is_dummy_root(root
)) {
7692 buf
= btrfs_init_new_buffer(trans
, root
, root
->alloc_bytenr
,
7695 root
->alloc_bytenr
+= blocksize
;
7699 block_rsv
= use_block_rsv(trans
, root
, blocksize
);
7700 if (IS_ERR(block_rsv
))
7701 return ERR_CAST(block_rsv
);
7703 ret
= btrfs_reserve_extent(root
, blocksize
, blocksize
,
7704 empty_size
, hint
, &ins
, 0, 0);
7708 buf
= btrfs_init_new_buffer(trans
, root
, ins
.objectid
, level
);
7711 goto out_free_reserved
;
7714 if (root_objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
7716 parent
= ins
.objectid
;
7717 flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
7721 if (root_objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
7722 extent_op
= btrfs_alloc_delayed_extent_op();
7728 memcpy(&extent_op
->key
, key
, sizeof(extent_op
->key
));
7730 memset(&extent_op
->key
, 0, sizeof(extent_op
->key
));
7731 extent_op
->flags_to_set
= flags
;
7732 if (skinny_metadata
)
7733 extent_op
->update_key
= 0;
7735 extent_op
->update_key
= 1;
7736 extent_op
->update_flags
= 1;
7737 extent_op
->is_data
= 0;
7738 extent_op
->level
= level
;
7740 ret
= btrfs_add_delayed_tree_ref(root
->fs_info
, trans
,
7741 ins
.objectid
, ins
.offset
,
7742 parent
, root_objectid
, level
,
7743 BTRFS_ADD_DELAYED_EXTENT
,
7746 goto out_free_delayed
;
7751 btrfs_free_delayed_extent_op(extent_op
);
7753 free_extent_buffer(buf
);
7755 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
, 0);
7757 unuse_block_rsv(root
->fs_info
, block_rsv
, blocksize
);
7758 return ERR_PTR(ret
);
7761 struct walk_control
{
7762 u64 refs
[BTRFS_MAX_LEVEL
];
7763 u64 flags
[BTRFS_MAX_LEVEL
];
7764 struct btrfs_key update_progress
;
7775 #define DROP_REFERENCE 1
7776 #define UPDATE_BACKREF 2
7778 static noinline
void reada_walk_down(struct btrfs_trans_handle
*trans
,
7779 struct btrfs_root
*root
,
7780 struct walk_control
*wc
,
7781 struct btrfs_path
*path
)
7789 struct btrfs_key key
;
7790 struct extent_buffer
*eb
;
7795 if (path
->slots
[wc
->level
] < wc
->reada_slot
) {
7796 wc
->reada_count
= wc
->reada_count
* 2 / 3;
7797 wc
->reada_count
= max(wc
->reada_count
, 2);
7799 wc
->reada_count
= wc
->reada_count
* 3 / 2;
7800 wc
->reada_count
= min_t(int, wc
->reada_count
,
7801 BTRFS_NODEPTRS_PER_BLOCK(root
));
7804 eb
= path
->nodes
[wc
->level
];
7805 nritems
= btrfs_header_nritems(eb
);
7806 blocksize
= root
->nodesize
;
7808 for (slot
= path
->slots
[wc
->level
]; slot
< nritems
; slot
++) {
7809 if (nread
>= wc
->reada_count
)
7813 bytenr
= btrfs_node_blockptr(eb
, slot
);
7814 generation
= btrfs_node_ptr_generation(eb
, slot
);
7816 if (slot
== path
->slots
[wc
->level
])
7819 if (wc
->stage
== UPDATE_BACKREF
&&
7820 generation
<= root
->root_key
.offset
)
7823 /* We don't lock the tree block, it's OK to be racy here */
7824 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
,
7825 wc
->level
- 1, 1, &refs
,
7827 /* We don't care about errors in readahead. */
7832 if (wc
->stage
== DROP_REFERENCE
) {
7836 if (wc
->level
== 1 &&
7837 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
7839 if (!wc
->update_ref
||
7840 generation
<= root
->root_key
.offset
)
7842 btrfs_node_key_to_cpu(eb
, &key
, slot
);
7843 ret
= btrfs_comp_cpu_keys(&key
,
7844 &wc
->update_progress
);
7848 if (wc
->level
== 1 &&
7849 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
7853 readahead_tree_block(root
, bytenr
);
7856 wc
->reada_slot
= slot
;
7860 * TODO: Modify related function to add related node/leaf to dirty_extent_root,
7861 * for later qgroup accounting.
7863 * Current, this function does nothing.
7865 static int account_leaf_items(struct btrfs_trans_handle
*trans
,
7866 struct btrfs_root
*root
,
7867 struct extent_buffer
*eb
)
7869 int nr
= btrfs_header_nritems(eb
);
7871 struct btrfs_key key
;
7872 struct btrfs_file_extent_item
*fi
;
7873 u64 bytenr
, num_bytes
;
7875 for (i
= 0; i
< nr
; i
++) {
7876 btrfs_item_key_to_cpu(eb
, &key
, i
);
7878 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
7881 fi
= btrfs_item_ptr(eb
, i
, struct btrfs_file_extent_item
);
7882 /* filter out non qgroup-accountable extents */
7883 extent_type
= btrfs_file_extent_type(eb
, fi
);
7885 if (extent_type
== BTRFS_FILE_EXTENT_INLINE
)
7888 bytenr
= btrfs_file_extent_disk_bytenr(eb
, fi
);
7892 num_bytes
= btrfs_file_extent_disk_num_bytes(eb
, fi
);
7898 * Walk up the tree from the bottom, freeing leaves and any interior
7899 * nodes which have had all slots visited. If a node (leaf or
7900 * interior) is freed, the node above it will have it's slot
7901 * incremented. The root node will never be freed.
7903 * At the end of this function, we should have a path which has all
7904 * slots incremented to the next position for a search. If we need to
7905 * read a new node it will be NULL and the node above it will have the
7906 * correct slot selected for a later read.
7908 * If we increment the root nodes slot counter past the number of
7909 * elements, 1 is returned to signal completion of the search.
7911 static int adjust_slots_upwards(struct btrfs_root
*root
,
7912 struct btrfs_path
*path
, int root_level
)
7916 struct extent_buffer
*eb
;
7918 if (root_level
== 0)
7921 while (level
<= root_level
) {
7922 eb
= path
->nodes
[level
];
7923 nr
= btrfs_header_nritems(eb
);
7924 path
->slots
[level
]++;
7925 slot
= path
->slots
[level
];
7926 if (slot
>= nr
|| level
== 0) {
7928 * Don't free the root - we will detect this
7929 * condition after our loop and return a
7930 * positive value for caller to stop walking the tree.
7932 if (level
!= root_level
) {
7933 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
7934 path
->locks
[level
] = 0;
7936 free_extent_buffer(eb
);
7937 path
->nodes
[level
] = NULL
;
7938 path
->slots
[level
] = 0;
7942 * We have a valid slot to walk back down
7943 * from. Stop here so caller can process these
7952 eb
= path
->nodes
[root_level
];
7953 if (path
->slots
[root_level
] >= btrfs_header_nritems(eb
))
7960 * root_eb is the subtree root and is locked before this function is called.
7961 * TODO: Modify this function to mark all (including complete shared node)
7962 * to dirty_extent_root to allow it get accounted in qgroup.
7964 static int account_shared_subtree(struct btrfs_trans_handle
*trans
,
7965 struct btrfs_root
*root
,
7966 struct extent_buffer
*root_eb
,
7972 struct extent_buffer
*eb
= root_eb
;
7973 struct btrfs_path
*path
= NULL
;
7975 BUG_ON(root_level
< 0 || root_level
> BTRFS_MAX_LEVEL
);
7976 BUG_ON(root_eb
== NULL
);
7978 if (!root
->fs_info
->quota_enabled
)
7981 if (!extent_buffer_uptodate(root_eb
)) {
7982 ret
= btrfs_read_buffer(root_eb
, root_gen
);
7987 if (root_level
== 0) {
7988 ret
= account_leaf_items(trans
, root
, root_eb
);
7992 path
= btrfs_alloc_path();
7997 * Walk down the tree. Missing extent blocks are filled in as
7998 * we go. Metadata is accounted every time we read a new
8001 * When we reach a leaf, we account for file extent items in it,
8002 * walk back up the tree (adjusting slot pointers as we go)
8003 * and restart the search process.
8005 extent_buffer_get(root_eb
); /* For path */
8006 path
->nodes
[root_level
] = root_eb
;
8007 path
->slots
[root_level
] = 0;
8008 path
->locks
[root_level
] = 0; /* so release_path doesn't try to unlock */
8011 while (level
>= 0) {
8012 if (path
->nodes
[level
] == NULL
) {
8017 /* We need to get child blockptr/gen from
8018 * parent before we can read it. */
8019 eb
= path
->nodes
[level
+ 1];
8020 parent_slot
= path
->slots
[level
+ 1];
8021 child_bytenr
= btrfs_node_blockptr(eb
, parent_slot
);
8022 child_gen
= btrfs_node_ptr_generation(eb
, parent_slot
);
8024 eb
= read_tree_block(root
, child_bytenr
, child_gen
);
8028 } else if (!extent_buffer_uptodate(eb
)) {
8029 free_extent_buffer(eb
);
8034 path
->nodes
[level
] = eb
;
8035 path
->slots
[level
] = 0;
8037 btrfs_tree_read_lock(eb
);
8038 btrfs_set_lock_blocking_rw(eb
, BTRFS_READ_LOCK
);
8039 path
->locks
[level
] = BTRFS_READ_LOCK_BLOCKING
;
8043 ret
= account_leaf_items(trans
, root
, path
->nodes
[level
]);
8047 /* Nonzero return here means we completed our search */
8048 ret
= adjust_slots_upwards(root
, path
, root_level
);
8052 /* Restart search with new slots */
8061 btrfs_free_path(path
);
8067 * helper to process tree block while walking down the tree.
8069 * when wc->stage == UPDATE_BACKREF, this function updates
8070 * back refs for pointers in the block.
8072 * NOTE: return value 1 means we should stop walking down.
8074 static noinline
int walk_down_proc(struct btrfs_trans_handle
*trans
,
8075 struct btrfs_root
*root
,
8076 struct btrfs_path
*path
,
8077 struct walk_control
*wc
, int lookup_info
)
8079 int level
= wc
->level
;
8080 struct extent_buffer
*eb
= path
->nodes
[level
];
8081 u64 flag
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
8084 if (wc
->stage
== UPDATE_BACKREF
&&
8085 btrfs_header_owner(eb
) != root
->root_key
.objectid
)
8089 * when reference count of tree block is 1, it won't increase
8090 * again. once full backref flag is set, we never clear it.
8093 ((wc
->stage
== DROP_REFERENCE
&& wc
->refs
[level
] != 1) ||
8094 (wc
->stage
== UPDATE_BACKREF
&& !(wc
->flags
[level
] & flag
)))) {
8095 BUG_ON(!path
->locks
[level
]);
8096 ret
= btrfs_lookup_extent_info(trans
, root
,
8097 eb
->start
, level
, 1,
8100 BUG_ON(ret
== -ENOMEM
);
8103 BUG_ON(wc
->refs
[level
] == 0);
8106 if (wc
->stage
== DROP_REFERENCE
) {
8107 if (wc
->refs
[level
] > 1)
8110 if (path
->locks
[level
] && !wc
->keep_locks
) {
8111 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
8112 path
->locks
[level
] = 0;
8117 /* wc->stage == UPDATE_BACKREF */
8118 if (!(wc
->flags
[level
] & flag
)) {
8119 BUG_ON(!path
->locks
[level
]);
8120 ret
= btrfs_inc_ref(trans
, root
, eb
, 1);
8121 BUG_ON(ret
); /* -ENOMEM */
8122 ret
= btrfs_dec_ref(trans
, root
, eb
, 0);
8123 BUG_ON(ret
); /* -ENOMEM */
8124 ret
= btrfs_set_disk_extent_flags(trans
, root
, eb
->start
,
8126 btrfs_header_level(eb
), 0);
8127 BUG_ON(ret
); /* -ENOMEM */
8128 wc
->flags
[level
] |= flag
;
8132 * the block is shared by multiple trees, so it's not good to
8133 * keep the tree lock
8135 if (path
->locks
[level
] && level
> 0) {
8136 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
8137 path
->locks
[level
] = 0;
8143 * helper to process tree block pointer.
8145 * when wc->stage == DROP_REFERENCE, this function checks
8146 * reference count of the block pointed to. if the block
8147 * is shared and we need update back refs for the subtree
8148 * rooted at the block, this function changes wc->stage to
8149 * UPDATE_BACKREF. if the block is shared and there is no
8150 * need to update back, this function drops the reference
8153 * NOTE: return value 1 means we should stop walking down.
8155 static noinline
int do_walk_down(struct btrfs_trans_handle
*trans
,
8156 struct btrfs_root
*root
,
8157 struct btrfs_path
*path
,
8158 struct walk_control
*wc
, int *lookup_info
)
8164 struct btrfs_key key
;
8165 struct extent_buffer
*next
;
8166 int level
= wc
->level
;
8169 bool need_account
= false;
8171 generation
= btrfs_node_ptr_generation(path
->nodes
[level
],
8172 path
->slots
[level
]);
8174 * if the lower level block was created before the snapshot
8175 * was created, we know there is no need to update back refs
8178 if (wc
->stage
== UPDATE_BACKREF
&&
8179 generation
<= root
->root_key
.offset
) {
8184 bytenr
= btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]);
8185 blocksize
= root
->nodesize
;
8187 next
= btrfs_find_tree_block(root
->fs_info
, bytenr
);
8189 next
= btrfs_find_create_tree_block(root
, bytenr
);
8192 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, next
,
8196 btrfs_tree_lock(next
);
8197 btrfs_set_lock_blocking(next
);
8199 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
, level
- 1, 1,
8200 &wc
->refs
[level
- 1],
8201 &wc
->flags
[level
- 1]);
8203 btrfs_tree_unlock(next
);
8207 if (unlikely(wc
->refs
[level
- 1] == 0)) {
8208 btrfs_err(root
->fs_info
, "Missing references.");
8213 if (wc
->stage
== DROP_REFERENCE
) {
8214 if (wc
->refs
[level
- 1] > 1) {
8215 need_account
= true;
8217 (wc
->flags
[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF
))
8220 if (!wc
->update_ref
||
8221 generation
<= root
->root_key
.offset
)
8224 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
8225 path
->slots
[level
]);
8226 ret
= btrfs_comp_cpu_keys(&key
, &wc
->update_progress
);
8230 wc
->stage
= UPDATE_BACKREF
;
8231 wc
->shared_level
= level
- 1;
8235 (wc
->flags
[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF
))
8239 if (!btrfs_buffer_uptodate(next
, generation
, 0)) {
8240 btrfs_tree_unlock(next
);
8241 free_extent_buffer(next
);
8247 if (reada
&& level
== 1)
8248 reada_walk_down(trans
, root
, wc
, path
);
8249 next
= read_tree_block(root
, bytenr
, generation
);
8251 return PTR_ERR(next
);
8252 } else if (!extent_buffer_uptodate(next
)) {
8253 free_extent_buffer(next
);
8256 btrfs_tree_lock(next
);
8257 btrfs_set_lock_blocking(next
);
8261 BUG_ON(level
!= btrfs_header_level(next
));
8262 path
->nodes
[level
] = next
;
8263 path
->slots
[level
] = 0;
8264 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
8270 wc
->refs
[level
- 1] = 0;
8271 wc
->flags
[level
- 1] = 0;
8272 if (wc
->stage
== DROP_REFERENCE
) {
8273 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
) {
8274 parent
= path
->nodes
[level
]->start
;
8276 BUG_ON(root
->root_key
.objectid
!=
8277 btrfs_header_owner(path
->nodes
[level
]));
8282 ret
= account_shared_subtree(trans
, root
, next
,
8283 generation
, level
- 1);
8285 printk_ratelimited(KERN_ERR
"BTRFS: %s Error "
8286 "%d accounting shared subtree. Quota "
8287 "is out of sync, rescan required.\n",
8288 root
->fs_info
->sb
->s_id
, ret
);
8291 ret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
, parent
,
8292 root
->root_key
.objectid
, level
- 1, 0, 0);
8293 BUG_ON(ret
); /* -ENOMEM */
8295 btrfs_tree_unlock(next
);
8296 free_extent_buffer(next
);
8302 * helper to process tree block while walking up the tree.
8304 * when wc->stage == DROP_REFERENCE, this function drops
8305 * reference count on the block.
8307 * when wc->stage == UPDATE_BACKREF, this function changes
8308 * wc->stage back to DROP_REFERENCE if we changed wc->stage
8309 * to UPDATE_BACKREF previously while processing the block.
8311 * NOTE: return value 1 means we should stop walking up.
8313 static noinline
int walk_up_proc(struct btrfs_trans_handle
*trans
,
8314 struct btrfs_root
*root
,
8315 struct btrfs_path
*path
,
8316 struct walk_control
*wc
)
8319 int level
= wc
->level
;
8320 struct extent_buffer
*eb
= path
->nodes
[level
];
8323 if (wc
->stage
== UPDATE_BACKREF
) {
8324 BUG_ON(wc
->shared_level
< level
);
8325 if (level
< wc
->shared_level
)
8328 ret
= find_next_key(path
, level
+ 1, &wc
->update_progress
);
8332 wc
->stage
= DROP_REFERENCE
;
8333 wc
->shared_level
= -1;
8334 path
->slots
[level
] = 0;
8337 * check reference count again if the block isn't locked.
8338 * we should start walking down the tree again if reference
8341 if (!path
->locks
[level
]) {
8343 btrfs_tree_lock(eb
);
8344 btrfs_set_lock_blocking(eb
);
8345 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
8347 ret
= btrfs_lookup_extent_info(trans
, root
,
8348 eb
->start
, level
, 1,
8352 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
8353 path
->locks
[level
] = 0;
8356 BUG_ON(wc
->refs
[level
] == 0);
8357 if (wc
->refs
[level
] == 1) {
8358 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
8359 path
->locks
[level
] = 0;
8365 /* wc->stage == DROP_REFERENCE */
8366 BUG_ON(wc
->refs
[level
] > 1 && !path
->locks
[level
]);
8368 if (wc
->refs
[level
] == 1) {
8370 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
8371 ret
= btrfs_dec_ref(trans
, root
, eb
, 1);
8373 ret
= btrfs_dec_ref(trans
, root
, eb
, 0);
8374 BUG_ON(ret
); /* -ENOMEM */
8375 ret
= account_leaf_items(trans
, root
, eb
);
8377 printk_ratelimited(KERN_ERR
"BTRFS: %s Error "
8378 "%d accounting leaf items. Quota "
8379 "is out of sync, rescan required.\n",
8380 root
->fs_info
->sb
->s_id
, ret
);
8383 /* make block locked assertion in clean_tree_block happy */
8384 if (!path
->locks
[level
] &&
8385 btrfs_header_generation(eb
) == trans
->transid
) {
8386 btrfs_tree_lock(eb
);
8387 btrfs_set_lock_blocking(eb
);
8388 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
8390 clean_tree_block(trans
, root
->fs_info
, eb
);
8393 if (eb
== root
->node
) {
8394 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
8397 BUG_ON(root
->root_key
.objectid
!=
8398 btrfs_header_owner(eb
));
8400 if (wc
->flags
[level
+ 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
8401 parent
= path
->nodes
[level
+ 1]->start
;
8403 BUG_ON(root
->root_key
.objectid
!=
8404 btrfs_header_owner(path
->nodes
[level
+ 1]));
8407 btrfs_free_tree_block(trans
, root
, eb
, parent
, wc
->refs
[level
] == 1);
8409 wc
->refs
[level
] = 0;
8410 wc
->flags
[level
] = 0;
8414 static noinline
int walk_down_tree(struct btrfs_trans_handle
*trans
,
8415 struct btrfs_root
*root
,
8416 struct btrfs_path
*path
,
8417 struct walk_control
*wc
)
8419 int level
= wc
->level
;
8420 int lookup_info
= 1;
8423 while (level
>= 0) {
8424 ret
= walk_down_proc(trans
, root
, path
, wc
, lookup_info
);
8431 if (path
->slots
[level
] >=
8432 btrfs_header_nritems(path
->nodes
[level
]))
8435 ret
= do_walk_down(trans
, root
, path
, wc
, &lookup_info
);
8437 path
->slots
[level
]++;
8446 static noinline
int walk_up_tree(struct btrfs_trans_handle
*trans
,
8447 struct btrfs_root
*root
,
8448 struct btrfs_path
*path
,
8449 struct walk_control
*wc
, int max_level
)
8451 int level
= wc
->level
;
8454 path
->slots
[level
] = btrfs_header_nritems(path
->nodes
[level
]);
8455 while (level
< max_level
&& path
->nodes
[level
]) {
8457 if (path
->slots
[level
] + 1 <
8458 btrfs_header_nritems(path
->nodes
[level
])) {
8459 path
->slots
[level
]++;
8462 ret
= walk_up_proc(trans
, root
, path
, wc
);
8466 if (path
->locks
[level
]) {
8467 btrfs_tree_unlock_rw(path
->nodes
[level
],
8468 path
->locks
[level
]);
8469 path
->locks
[level
] = 0;
8471 free_extent_buffer(path
->nodes
[level
]);
8472 path
->nodes
[level
] = NULL
;
8480 * drop a subvolume tree.
8482 * this function traverses the tree freeing any blocks that only
8483 * referenced by the tree.
8485 * when a shared tree block is found. this function decreases its
8486 * reference count by one. if update_ref is true, this function
8487 * also make sure backrefs for the shared block and all lower level
8488 * blocks are properly updated.
8490 * If called with for_reloc == 0, may exit early with -EAGAIN
8492 int btrfs_drop_snapshot(struct btrfs_root
*root
,
8493 struct btrfs_block_rsv
*block_rsv
, int update_ref
,
8496 struct btrfs_path
*path
;
8497 struct btrfs_trans_handle
*trans
;
8498 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
8499 struct btrfs_root_item
*root_item
= &root
->root_item
;
8500 struct walk_control
*wc
;
8501 struct btrfs_key key
;
8505 bool root_dropped
= false;
8507 btrfs_debug(root
->fs_info
, "Drop subvolume %llu", root
->objectid
);
8509 path
= btrfs_alloc_path();
8515 wc
= kzalloc(sizeof(*wc
), GFP_NOFS
);
8517 btrfs_free_path(path
);
8522 trans
= btrfs_start_transaction(tree_root
, 0);
8523 if (IS_ERR(trans
)) {
8524 err
= PTR_ERR(trans
);
8529 trans
->block_rsv
= block_rsv
;
8531 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
8532 level
= btrfs_header_level(root
->node
);
8533 path
->nodes
[level
] = btrfs_lock_root_node(root
);
8534 btrfs_set_lock_blocking(path
->nodes
[level
]);
8535 path
->slots
[level
] = 0;
8536 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
8537 memset(&wc
->update_progress
, 0,
8538 sizeof(wc
->update_progress
));
8540 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
8541 memcpy(&wc
->update_progress
, &key
,
8542 sizeof(wc
->update_progress
));
8544 level
= root_item
->drop_level
;
8546 path
->lowest_level
= level
;
8547 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
8548 path
->lowest_level
= 0;
8556 * unlock our path, this is safe because only this
8557 * function is allowed to delete this snapshot
8559 btrfs_unlock_up_safe(path
, 0);
8561 level
= btrfs_header_level(root
->node
);
8563 btrfs_tree_lock(path
->nodes
[level
]);
8564 btrfs_set_lock_blocking(path
->nodes
[level
]);
8565 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
8567 ret
= btrfs_lookup_extent_info(trans
, root
,
8568 path
->nodes
[level
]->start
,
8569 level
, 1, &wc
->refs
[level
],
8575 BUG_ON(wc
->refs
[level
] == 0);
8577 if (level
== root_item
->drop_level
)
8580 btrfs_tree_unlock(path
->nodes
[level
]);
8581 path
->locks
[level
] = 0;
8582 WARN_ON(wc
->refs
[level
] != 1);
8588 wc
->shared_level
= -1;
8589 wc
->stage
= DROP_REFERENCE
;
8590 wc
->update_ref
= update_ref
;
8592 wc
->for_reloc
= for_reloc
;
8593 wc
->reada_count
= BTRFS_NODEPTRS_PER_BLOCK(root
);
8597 ret
= walk_down_tree(trans
, root
, path
, wc
);
8603 ret
= walk_up_tree(trans
, root
, path
, wc
, BTRFS_MAX_LEVEL
);
8610 BUG_ON(wc
->stage
!= DROP_REFERENCE
);
8614 if (wc
->stage
== DROP_REFERENCE
) {
8616 btrfs_node_key(path
->nodes
[level
],
8617 &root_item
->drop_progress
,
8618 path
->slots
[level
]);
8619 root_item
->drop_level
= level
;
8622 BUG_ON(wc
->level
== 0);
8623 if (btrfs_should_end_transaction(trans
, tree_root
) ||
8624 (!for_reloc
&& btrfs_need_cleaner_sleep(root
))) {
8625 ret
= btrfs_update_root(trans
, tree_root
,
8629 btrfs_abort_transaction(trans
, tree_root
, ret
);
8634 btrfs_end_transaction_throttle(trans
, tree_root
);
8635 if (!for_reloc
&& btrfs_need_cleaner_sleep(root
)) {
8636 pr_debug("BTRFS: drop snapshot early exit\n");
8641 trans
= btrfs_start_transaction(tree_root
, 0);
8642 if (IS_ERR(trans
)) {
8643 err
= PTR_ERR(trans
);
8647 trans
->block_rsv
= block_rsv
;
8650 btrfs_release_path(path
);
8654 ret
= btrfs_del_root(trans
, tree_root
, &root
->root_key
);
8656 btrfs_abort_transaction(trans
, tree_root
, ret
);
8660 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
8661 ret
= btrfs_find_root(tree_root
, &root
->root_key
, path
,
8664 btrfs_abort_transaction(trans
, tree_root
, ret
);
8667 } else if (ret
> 0) {
8668 /* if we fail to delete the orphan item this time
8669 * around, it'll get picked up the next time.
8671 * The most common failure here is just -ENOENT.
8673 btrfs_del_orphan_item(trans
, tree_root
,
8674 root
->root_key
.objectid
);
8678 if (test_bit(BTRFS_ROOT_IN_RADIX
, &root
->state
)) {
8679 btrfs_add_dropped_root(trans
, root
);
8681 free_extent_buffer(root
->node
);
8682 free_extent_buffer(root
->commit_root
);
8683 btrfs_put_fs_root(root
);
8685 root_dropped
= true;
8687 btrfs_end_transaction_throttle(trans
, tree_root
);
8690 btrfs_free_path(path
);
8693 * So if we need to stop dropping the snapshot for whatever reason we
8694 * need to make sure to add it back to the dead root list so that we
8695 * keep trying to do the work later. This also cleans up roots if we
8696 * don't have it in the radix (like when we recover after a power fail
8697 * or unmount) so we don't leak memory.
8699 if (!for_reloc
&& root_dropped
== false)
8700 btrfs_add_dead_root(root
);
8701 if (err
&& err
!= -EAGAIN
)
8702 btrfs_std_error(root
->fs_info
, err
);
8707 * drop subtree rooted at tree block 'node'.
8709 * NOTE: this function will unlock and release tree block 'node'
8710 * only used by relocation code
8712 int btrfs_drop_subtree(struct btrfs_trans_handle
*trans
,
8713 struct btrfs_root
*root
,
8714 struct extent_buffer
*node
,
8715 struct extent_buffer
*parent
)
8717 struct btrfs_path
*path
;
8718 struct walk_control
*wc
;
8724 BUG_ON(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
8726 path
= btrfs_alloc_path();
8730 wc
= kzalloc(sizeof(*wc
), GFP_NOFS
);
8732 btrfs_free_path(path
);
8736 btrfs_assert_tree_locked(parent
);
8737 parent_level
= btrfs_header_level(parent
);
8738 extent_buffer_get(parent
);
8739 path
->nodes
[parent_level
] = parent
;
8740 path
->slots
[parent_level
] = btrfs_header_nritems(parent
);
8742 btrfs_assert_tree_locked(node
);
8743 level
= btrfs_header_level(node
);
8744 path
->nodes
[level
] = node
;
8745 path
->slots
[level
] = 0;
8746 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
8748 wc
->refs
[parent_level
] = 1;
8749 wc
->flags
[parent_level
] = BTRFS_BLOCK_FLAG_FULL_BACKREF
;
8751 wc
->shared_level
= -1;
8752 wc
->stage
= DROP_REFERENCE
;
8756 wc
->reada_count
= BTRFS_NODEPTRS_PER_BLOCK(root
);
8759 wret
= walk_down_tree(trans
, root
, path
, wc
);
8765 wret
= walk_up_tree(trans
, root
, path
, wc
, parent_level
);
8773 btrfs_free_path(path
);
8777 static u64
update_block_group_flags(struct btrfs_root
*root
, u64 flags
)
8783 * if restripe for this chunk_type is on pick target profile and
8784 * return, otherwise do the usual balance
8786 stripped
= get_restripe_target(root
->fs_info
, flags
);
8788 return extended_to_chunk(stripped
);
8790 num_devices
= root
->fs_info
->fs_devices
->rw_devices
;
8792 stripped
= BTRFS_BLOCK_GROUP_RAID0
|
8793 BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
|
8794 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
;
8796 if (num_devices
== 1) {
8797 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
8798 stripped
= flags
& ~stripped
;
8800 /* turn raid0 into single device chunks */
8801 if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
8804 /* turn mirroring into duplication */
8805 if (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
8806 BTRFS_BLOCK_GROUP_RAID10
))
8807 return stripped
| BTRFS_BLOCK_GROUP_DUP
;
8809 /* they already had raid on here, just return */
8810 if (flags
& stripped
)
8813 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
8814 stripped
= flags
& ~stripped
;
8816 /* switch duplicated blocks with raid1 */
8817 if (flags
& BTRFS_BLOCK_GROUP_DUP
)
8818 return stripped
| BTRFS_BLOCK_GROUP_RAID1
;
8820 /* this is drive concat, leave it alone */
8826 static int inc_block_group_ro(struct btrfs_block_group_cache
*cache
, int force
)
8828 struct btrfs_space_info
*sinfo
= cache
->space_info
;
8830 u64 min_allocable_bytes
;
8834 * We need some metadata space and system metadata space for
8835 * allocating chunks in some corner cases until we force to set
8836 * it to be readonly.
8839 (BTRFS_BLOCK_GROUP_SYSTEM
| BTRFS_BLOCK_GROUP_METADATA
)) &&
8841 min_allocable_bytes
= 1 * 1024 * 1024;
8843 min_allocable_bytes
= 0;
8845 spin_lock(&sinfo
->lock
);
8846 spin_lock(&cache
->lock
);
8854 num_bytes
= cache
->key
.offset
- cache
->reserved
- cache
->pinned
-
8855 cache
->bytes_super
- btrfs_block_group_used(&cache
->item
);
8857 if (sinfo
->bytes_used
+ sinfo
->bytes_reserved
+ sinfo
->bytes_pinned
+
8858 sinfo
->bytes_may_use
+ sinfo
->bytes_readonly
+ num_bytes
+
8859 min_allocable_bytes
<= sinfo
->total_bytes
) {
8860 sinfo
->bytes_readonly
+= num_bytes
;
8862 list_add_tail(&cache
->ro_list
, &sinfo
->ro_bgs
);
8866 spin_unlock(&cache
->lock
);
8867 spin_unlock(&sinfo
->lock
);
8871 int btrfs_inc_block_group_ro(struct btrfs_root
*root
,
8872 struct btrfs_block_group_cache
*cache
)
8875 struct btrfs_trans_handle
*trans
;
8880 trans
= btrfs_join_transaction(root
);
8882 return PTR_ERR(trans
);
8885 * we're not allowed to set block groups readonly after the dirty
8886 * block groups cache has started writing. If it already started,
8887 * back off and let this transaction commit
8889 mutex_lock(&root
->fs_info
->ro_block_group_mutex
);
8890 if (trans
->transaction
->dirty_bg_run
) {
8891 u64 transid
= trans
->transid
;
8893 mutex_unlock(&root
->fs_info
->ro_block_group_mutex
);
8894 btrfs_end_transaction(trans
, root
);
8896 ret
= btrfs_wait_for_commit(root
, transid
);
8903 * if we are changing raid levels, try to allocate a corresponding
8904 * block group with the new raid level.
8906 alloc_flags
= update_block_group_flags(root
, cache
->flags
);
8907 if (alloc_flags
!= cache
->flags
) {
8908 ret
= do_chunk_alloc(trans
, root
, alloc_flags
,
8911 * ENOSPC is allowed here, we may have enough space
8912 * already allocated at the new raid level to
8921 ret
= inc_block_group_ro(cache
, 0);
8924 alloc_flags
= get_alloc_profile(root
, cache
->space_info
->flags
);
8925 ret
= do_chunk_alloc(trans
, root
, alloc_flags
,
8929 ret
= inc_block_group_ro(cache
, 0);
8931 if (cache
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
) {
8932 alloc_flags
= update_block_group_flags(root
, cache
->flags
);
8933 lock_chunks(root
->fs_info
->chunk_root
);
8934 check_system_chunk(trans
, root
, alloc_flags
);
8935 unlock_chunks(root
->fs_info
->chunk_root
);
8937 mutex_unlock(&root
->fs_info
->ro_block_group_mutex
);
8939 btrfs_end_transaction(trans
, root
);
8943 int btrfs_force_chunk_alloc(struct btrfs_trans_handle
*trans
,
8944 struct btrfs_root
*root
, u64 type
)
8946 u64 alloc_flags
= get_alloc_profile(root
, type
);
8947 return do_chunk_alloc(trans
, root
, alloc_flags
,
8952 * helper to account the unused space of all the readonly block group in the
8953 * space_info. takes mirrors into account.
8955 u64
btrfs_account_ro_block_groups_free_space(struct btrfs_space_info
*sinfo
)
8957 struct btrfs_block_group_cache
*block_group
;
8961 /* It's df, we don't care if it's racey */
8962 if (list_empty(&sinfo
->ro_bgs
))
8965 spin_lock(&sinfo
->lock
);
8966 list_for_each_entry(block_group
, &sinfo
->ro_bgs
, ro_list
) {
8967 spin_lock(&block_group
->lock
);
8969 if (!block_group
->ro
) {
8970 spin_unlock(&block_group
->lock
);
8974 if (block_group
->flags
& (BTRFS_BLOCK_GROUP_RAID1
|
8975 BTRFS_BLOCK_GROUP_RAID10
|
8976 BTRFS_BLOCK_GROUP_DUP
))
8981 free_bytes
+= (block_group
->key
.offset
-
8982 btrfs_block_group_used(&block_group
->item
)) *
8985 spin_unlock(&block_group
->lock
);
8987 spin_unlock(&sinfo
->lock
);
8992 void btrfs_dec_block_group_ro(struct btrfs_root
*root
,
8993 struct btrfs_block_group_cache
*cache
)
8995 struct btrfs_space_info
*sinfo
= cache
->space_info
;
9000 spin_lock(&sinfo
->lock
);
9001 spin_lock(&cache
->lock
);
9003 num_bytes
= cache
->key
.offset
- cache
->reserved
-
9004 cache
->pinned
- cache
->bytes_super
-
9005 btrfs_block_group_used(&cache
->item
);
9006 sinfo
->bytes_readonly
-= num_bytes
;
9007 list_del_init(&cache
->ro_list
);
9009 spin_unlock(&cache
->lock
);
9010 spin_unlock(&sinfo
->lock
);
9014 * checks to see if its even possible to relocate this block group.
9016 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
9017 * ok to go ahead and try.
9019 int btrfs_can_relocate(struct btrfs_root
*root
, u64 bytenr
)
9021 struct btrfs_block_group_cache
*block_group
;
9022 struct btrfs_space_info
*space_info
;
9023 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
9024 struct btrfs_device
*device
;
9025 struct btrfs_trans_handle
*trans
;
9034 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
9036 /* odd, couldn't find the block group, leave it alone */
9040 min_free
= btrfs_block_group_used(&block_group
->item
);
9042 /* no bytes used, we're good */
9046 space_info
= block_group
->space_info
;
9047 spin_lock(&space_info
->lock
);
9049 full
= space_info
->full
;
9052 * if this is the last block group we have in this space, we can't
9053 * relocate it unless we're able to allocate a new chunk below.
9055 * Otherwise, we need to make sure we have room in the space to handle
9056 * all of the extents from this block group. If we can, we're good
9058 if ((space_info
->total_bytes
!= block_group
->key
.offset
) &&
9059 (space_info
->bytes_used
+ space_info
->bytes_reserved
+
9060 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
9061 min_free
< space_info
->total_bytes
)) {
9062 spin_unlock(&space_info
->lock
);
9065 spin_unlock(&space_info
->lock
);
9068 * ok we don't have enough space, but maybe we have free space on our
9069 * devices to allocate new chunks for relocation, so loop through our
9070 * alloc devices and guess if we have enough space. if this block
9071 * group is going to be restriped, run checks against the target
9072 * profile instead of the current one.
9084 target
= get_restripe_target(root
->fs_info
, block_group
->flags
);
9086 index
= __get_raid_index(extended_to_chunk(target
));
9089 * this is just a balance, so if we were marked as full
9090 * we know there is no space for a new chunk
9095 index
= get_block_group_index(block_group
);
9098 if (index
== BTRFS_RAID_RAID10
) {
9102 } else if (index
== BTRFS_RAID_RAID1
) {
9104 } else if (index
== BTRFS_RAID_DUP
) {
9107 } else if (index
== BTRFS_RAID_RAID0
) {
9108 dev_min
= fs_devices
->rw_devices
;
9109 min_free
= div64_u64(min_free
, dev_min
);
9112 /* We need to do this so that we can look at pending chunks */
9113 trans
= btrfs_join_transaction(root
);
9114 if (IS_ERR(trans
)) {
9115 ret
= PTR_ERR(trans
);
9119 mutex_lock(&root
->fs_info
->chunk_mutex
);
9120 list_for_each_entry(device
, &fs_devices
->alloc_list
, dev_alloc_list
) {
9124 * check to make sure we can actually find a chunk with enough
9125 * space to fit our block group in.
9127 if (device
->total_bytes
> device
->bytes_used
+ min_free
&&
9128 !device
->is_tgtdev_for_dev_replace
) {
9129 ret
= find_free_dev_extent(trans
, device
, min_free
,
9134 if (dev_nr
>= dev_min
)
9140 mutex_unlock(&root
->fs_info
->chunk_mutex
);
9141 btrfs_end_transaction(trans
, root
);
9143 btrfs_put_block_group(block_group
);
9147 static int find_first_block_group(struct btrfs_root
*root
,
9148 struct btrfs_path
*path
, struct btrfs_key
*key
)
9151 struct btrfs_key found_key
;
9152 struct extent_buffer
*leaf
;
9155 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
9160 slot
= path
->slots
[0];
9161 leaf
= path
->nodes
[0];
9162 if (slot
>= btrfs_header_nritems(leaf
)) {
9163 ret
= btrfs_next_leaf(root
, path
);
9170 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
9172 if (found_key
.objectid
>= key
->objectid
&&
9173 found_key
.type
== BTRFS_BLOCK_GROUP_ITEM_KEY
) {
9183 void btrfs_put_block_group_cache(struct btrfs_fs_info
*info
)
9185 struct btrfs_block_group_cache
*block_group
;
9189 struct inode
*inode
;
9191 block_group
= btrfs_lookup_first_block_group(info
, last
);
9192 while (block_group
) {
9193 spin_lock(&block_group
->lock
);
9194 if (block_group
->iref
)
9196 spin_unlock(&block_group
->lock
);
9197 block_group
= next_block_group(info
->tree_root
,
9207 inode
= block_group
->inode
;
9208 block_group
->iref
= 0;
9209 block_group
->inode
= NULL
;
9210 spin_unlock(&block_group
->lock
);
9212 last
= block_group
->key
.objectid
+ block_group
->key
.offset
;
9213 btrfs_put_block_group(block_group
);
9217 int btrfs_free_block_groups(struct btrfs_fs_info
*info
)
9219 struct btrfs_block_group_cache
*block_group
;
9220 struct btrfs_space_info
*space_info
;
9221 struct btrfs_caching_control
*caching_ctl
;
9224 down_write(&info
->commit_root_sem
);
9225 while (!list_empty(&info
->caching_block_groups
)) {
9226 caching_ctl
= list_entry(info
->caching_block_groups
.next
,
9227 struct btrfs_caching_control
, list
);
9228 list_del(&caching_ctl
->list
);
9229 put_caching_control(caching_ctl
);
9231 up_write(&info
->commit_root_sem
);
9233 spin_lock(&info
->unused_bgs_lock
);
9234 while (!list_empty(&info
->unused_bgs
)) {
9235 block_group
= list_first_entry(&info
->unused_bgs
,
9236 struct btrfs_block_group_cache
,
9238 list_del_init(&block_group
->bg_list
);
9239 btrfs_put_block_group(block_group
);
9241 spin_unlock(&info
->unused_bgs_lock
);
9243 spin_lock(&info
->block_group_cache_lock
);
9244 while ((n
= rb_last(&info
->block_group_cache_tree
)) != NULL
) {
9245 block_group
= rb_entry(n
, struct btrfs_block_group_cache
,
9247 rb_erase(&block_group
->cache_node
,
9248 &info
->block_group_cache_tree
);
9249 RB_CLEAR_NODE(&block_group
->cache_node
);
9250 spin_unlock(&info
->block_group_cache_lock
);
9252 down_write(&block_group
->space_info
->groups_sem
);
9253 list_del(&block_group
->list
);
9254 up_write(&block_group
->space_info
->groups_sem
);
9256 if (block_group
->cached
== BTRFS_CACHE_STARTED
)
9257 wait_block_group_cache_done(block_group
);
9260 * We haven't cached this block group, which means we could
9261 * possibly have excluded extents on this block group.
9263 if (block_group
->cached
== BTRFS_CACHE_NO
||
9264 block_group
->cached
== BTRFS_CACHE_ERROR
)
9265 free_excluded_extents(info
->extent_root
, block_group
);
9267 btrfs_remove_free_space_cache(block_group
);
9268 btrfs_put_block_group(block_group
);
9270 spin_lock(&info
->block_group_cache_lock
);
9272 spin_unlock(&info
->block_group_cache_lock
);
9274 /* now that all the block groups are freed, go through and
9275 * free all the space_info structs. This is only called during
9276 * the final stages of unmount, and so we know nobody is
9277 * using them. We call synchronize_rcu() once before we start,
9278 * just to be on the safe side.
9282 release_global_block_rsv(info
);
9284 while (!list_empty(&info
->space_info
)) {
9287 space_info
= list_entry(info
->space_info
.next
,
9288 struct btrfs_space_info
,
9290 if (btrfs_test_opt(info
->tree_root
, ENOSPC_DEBUG
)) {
9291 if (WARN_ON(space_info
->bytes_pinned
> 0 ||
9292 space_info
->bytes_reserved
> 0 ||
9293 space_info
->bytes_may_use
> 0)) {
9294 dump_space_info(space_info
, 0, 0);
9297 list_del(&space_info
->list
);
9298 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++) {
9299 struct kobject
*kobj
;
9300 kobj
= space_info
->block_group_kobjs
[i
];
9301 space_info
->block_group_kobjs
[i
] = NULL
;
9307 kobject_del(&space_info
->kobj
);
9308 kobject_put(&space_info
->kobj
);
9313 static void __link_block_group(struct btrfs_space_info
*space_info
,
9314 struct btrfs_block_group_cache
*cache
)
9316 int index
= get_block_group_index(cache
);
9319 down_write(&space_info
->groups_sem
);
9320 if (list_empty(&space_info
->block_groups
[index
]))
9322 list_add_tail(&cache
->list
, &space_info
->block_groups
[index
]);
9323 up_write(&space_info
->groups_sem
);
9326 struct raid_kobject
*rkobj
;
9329 rkobj
= kzalloc(sizeof(*rkobj
), GFP_NOFS
);
9332 rkobj
->raid_type
= index
;
9333 kobject_init(&rkobj
->kobj
, &btrfs_raid_ktype
);
9334 ret
= kobject_add(&rkobj
->kobj
, &space_info
->kobj
,
9335 "%s", get_raid_name(index
));
9337 kobject_put(&rkobj
->kobj
);
9340 space_info
->block_group_kobjs
[index
] = &rkobj
->kobj
;
9345 pr_warn("BTRFS: failed to add kobject for block cache. ignoring.\n");
9348 static struct btrfs_block_group_cache
*
9349 btrfs_create_block_group_cache(struct btrfs_root
*root
, u64 start
, u64 size
)
9351 struct btrfs_block_group_cache
*cache
;
9353 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
9357 cache
->free_space_ctl
= kzalloc(sizeof(*cache
->free_space_ctl
),
9359 if (!cache
->free_space_ctl
) {
9364 cache
->key
.objectid
= start
;
9365 cache
->key
.offset
= size
;
9366 cache
->key
.type
= BTRFS_BLOCK_GROUP_ITEM_KEY
;
9368 cache
->sectorsize
= root
->sectorsize
;
9369 cache
->fs_info
= root
->fs_info
;
9370 cache
->full_stripe_len
= btrfs_full_stripe_len(root
,
9371 &root
->fs_info
->mapping_tree
,
9373 atomic_set(&cache
->count
, 1);
9374 spin_lock_init(&cache
->lock
);
9375 init_rwsem(&cache
->data_rwsem
);
9376 INIT_LIST_HEAD(&cache
->list
);
9377 INIT_LIST_HEAD(&cache
->cluster_list
);
9378 INIT_LIST_HEAD(&cache
->bg_list
);
9379 INIT_LIST_HEAD(&cache
->ro_list
);
9380 INIT_LIST_HEAD(&cache
->dirty_list
);
9381 INIT_LIST_HEAD(&cache
->io_list
);
9382 btrfs_init_free_space_ctl(cache
);
9383 atomic_set(&cache
->trimming
, 0);
9384 mutex_init(&cache
->free_space_lock
);
9389 int btrfs_read_block_groups(struct btrfs_root
*root
)
9391 struct btrfs_path
*path
;
9393 struct btrfs_block_group_cache
*cache
;
9394 struct btrfs_fs_info
*info
= root
->fs_info
;
9395 struct btrfs_space_info
*space_info
;
9396 struct btrfs_key key
;
9397 struct btrfs_key found_key
;
9398 struct extent_buffer
*leaf
;
9402 root
= info
->extent_root
;
9405 key
.type
= BTRFS_BLOCK_GROUP_ITEM_KEY
;
9406 path
= btrfs_alloc_path();
9411 cache_gen
= btrfs_super_cache_generation(root
->fs_info
->super_copy
);
9412 if (btrfs_test_opt(root
, SPACE_CACHE
) &&
9413 btrfs_super_generation(root
->fs_info
->super_copy
) != cache_gen
)
9415 if (btrfs_test_opt(root
, CLEAR_CACHE
))
9419 ret
= find_first_block_group(root
, path
, &key
);
9425 leaf
= path
->nodes
[0];
9426 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
9428 cache
= btrfs_create_block_group_cache(root
, found_key
.objectid
,
9437 * When we mount with old space cache, we need to
9438 * set BTRFS_DC_CLEAR and set dirty flag.
9440 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
9441 * truncate the old free space cache inode and
9443 * b) Setting 'dirty flag' makes sure that we flush
9444 * the new space cache info onto disk.
9446 if (btrfs_test_opt(root
, SPACE_CACHE
))
9447 cache
->disk_cache_state
= BTRFS_DC_CLEAR
;
9450 read_extent_buffer(leaf
, &cache
->item
,
9451 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
9452 sizeof(cache
->item
));
9453 cache
->flags
= btrfs_block_group_flags(&cache
->item
);
9455 key
.objectid
= found_key
.objectid
+ found_key
.offset
;
9456 btrfs_release_path(path
);
9459 * We need to exclude the super stripes now so that the space
9460 * info has super bytes accounted for, otherwise we'll think
9461 * we have more space than we actually do.
9463 ret
= exclude_super_stripes(root
, cache
);
9466 * We may have excluded something, so call this just in
9469 free_excluded_extents(root
, cache
);
9470 btrfs_put_block_group(cache
);
9475 * check for two cases, either we are full, and therefore
9476 * don't need to bother with the caching work since we won't
9477 * find any space, or we are empty, and we can just add all
9478 * the space in and be done with it. This saves us _alot_ of
9479 * time, particularly in the full case.
9481 if (found_key
.offset
== btrfs_block_group_used(&cache
->item
)) {
9482 cache
->last_byte_to_unpin
= (u64
)-1;
9483 cache
->cached
= BTRFS_CACHE_FINISHED
;
9484 free_excluded_extents(root
, cache
);
9485 } else if (btrfs_block_group_used(&cache
->item
) == 0) {
9486 cache
->last_byte_to_unpin
= (u64
)-1;
9487 cache
->cached
= BTRFS_CACHE_FINISHED
;
9488 add_new_free_space(cache
, root
->fs_info
,
9490 found_key
.objectid
+
9492 free_excluded_extents(root
, cache
);
9495 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
9497 btrfs_remove_free_space_cache(cache
);
9498 btrfs_put_block_group(cache
);
9502 ret
= update_space_info(info
, cache
->flags
, found_key
.offset
,
9503 btrfs_block_group_used(&cache
->item
),
9506 btrfs_remove_free_space_cache(cache
);
9507 spin_lock(&info
->block_group_cache_lock
);
9508 rb_erase(&cache
->cache_node
,
9509 &info
->block_group_cache_tree
);
9510 RB_CLEAR_NODE(&cache
->cache_node
);
9511 spin_unlock(&info
->block_group_cache_lock
);
9512 btrfs_put_block_group(cache
);
9516 cache
->space_info
= space_info
;
9517 spin_lock(&cache
->space_info
->lock
);
9518 cache
->space_info
->bytes_readonly
+= cache
->bytes_super
;
9519 spin_unlock(&cache
->space_info
->lock
);
9521 __link_block_group(space_info
, cache
);
9523 set_avail_alloc_bits(root
->fs_info
, cache
->flags
);
9524 if (btrfs_chunk_readonly(root
, cache
->key
.objectid
)) {
9525 inc_block_group_ro(cache
, 1);
9526 } else if (btrfs_block_group_used(&cache
->item
) == 0) {
9527 spin_lock(&info
->unused_bgs_lock
);
9528 /* Should always be true but just in case. */
9529 if (list_empty(&cache
->bg_list
)) {
9530 btrfs_get_block_group(cache
);
9531 list_add_tail(&cache
->bg_list
,
9534 spin_unlock(&info
->unused_bgs_lock
);
9538 list_for_each_entry_rcu(space_info
, &root
->fs_info
->space_info
, list
) {
9539 if (!(get_alloc_profile(root
, space_info
->flags
) &
9540 (BTRFS_BLOCK_GROUP_RAID10
|
9541 BTRFS_BLOCK_GROUP_RAID1
|
9542 BTRFS_BLOCK_GROUP_RAID5
|
9543 BTRFS_BLOCK_GROUP_RAID6
|
9544 BTRFS_BLOCK_GROUP_DUP
)))
9547 * avoid allocating from un-mirrored block group if there are
9548 * mirrored block groups.
9550 list_for_each_entry(cache
,
9551 &space_info
->block_groups
[BTRFS_RAID_RAID0
],
9553 inc_block_group_ro(cache
, 1);
9554 list_for_each_entry(cache
,
9555 &space_info
->block_groups
[BTRFS_RAID_SINGLE
],
9557 inc_block_group_ro(cache
, 1);
9560 init_global_block_rsv(info
);
9563 btrfs_free_path(path
);
9567 void btrfs_create_pending_block_groups(struct btrfs_trans_handle
*trans
,
9568 struct btrfs_root
*root
)
9570 struct btrfs_block_group_cache
*block_group
, *tmp
;
9571 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
9572 struct btrfs_block_group_item item
;
9573 struct btrfs_key key
;
9575 bool can_flush_pending_bgs
= trans
->can_flush_pending_bgs
;
9577 trans
->can_flush_pending_bgs
= false;
9578 list_for_each_entry_safe(block_group
, tmp
, &trans
->new_bgs
, bg_list
) {
9582 spin_lock(&block_group
->lock
);
9583 memcpy(&item
, &block_group
->item
, sizeof(item
));
9584 memcpy(&key
, &block_group
->key
, sizeof(key
));
9585 spin_unlock(&block_group
->lock
);
9587 ret
= btrfs_insert_item(trans
, extent_root
, &key
, &item
,
9590 btrfs_abort_transaction(trans
, extent_root
, ret
);
9591 ret
= btrfs_finish_chunk_alloc(trans
, extent_root
,
9592 key
.objectid
, key
.offset
);
9594 btrfs_abort_transaction(trans
, extent_root
, ret
);
9596 list_del_init(&block_group
->bg_list
);
9598 trans
->can_flush_pending_bgs
= can_flush_pending_bgs
;
9601 int btrfs_make_block_group(struct btrfs_trans_handle
*trans
,
9602 struct btrfs_root
*root
, u64 bytes_used
,
9603 u64 type
, u64 chunk_objectid
, u64 chunk_offset
,
9607 struct btrfs_root
*extent_root
;
9608 struct btrfs_block_group_cache
*cache
;
9610 extent_root
= root
->fs_info
->extent_root
;
9612 btrfs_set_log_full_commit(root
->fs_info
, trans
);
9614 cache
= btrfs_create_block_group_cache(root
, chunk_offset
, size
);
9618 btrfs_set_block_group_used(&cache
->item
, bytes_used
);
9619 btrfs_set_block_group_chunk_objectid(&cache
->item
, chunk_objectid
);
9620 btrfs_set_block_group_flags(&cache
->item
, type
);
9622 cache
->flags
= type
;
9623 cache
->last_byte_to_unpin
= (u64
)-1;
9624 cache
->cached
= BTRFS_CACHE_FINISHED
;
9625 ret
= exclude_super_stripes(root
, cache
);
9628 * We may have excluded something, so call this just in
9631 free_excluded_extents(root
, cache
);
9632 btrfs_put_block_group(cache
);
9636 add_new_free_space(cache
, root
->fs_info
, chunk_offset
,
9637 chunk_offset
+ size
);
9639 free_excluded_extents(root
, cache
);
9642 * Call to ensure the corresponding space_info object is created and
9643 * assigned to our block group, but don't update its counters just yet.
9644 * We want our bg to be added to the rbtree with its ->space_info set.
9646 ret
= update_space_info(root
->fs_info
, cache
->flags
, 0, 0,
9647 &cache
->space_info
);
9649 btrfs_remove_free_space_cache(cache
);
9650 btrfs_put_block_group(cache
);
9654 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
9656 btrfs_remove_free_space_cache(cache
);
9657 btrfs_put_block_group(cache
);
9662 * Now that our block group has its ->space_info set and is inserted in
9663 * the rbtree, update the space info's counters.
9665 ret
= update_space_info(root
->fs_info
, cache
->flags
, size
, bytes_used
,
9666 &cache
->space_info
);
9668 btrfs_remove_free_space_cache(cache
);
9669 spin_lock(&root
->fs_info
->block_group_cache_lock
);
9670 rb_erase(&cache
->cache_node
,
9671 &root
->fs_info
->block_group_cache_tree
);
9672 RB_CLEAR_NODE(&cache
->cache_node
);
9673 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
9674 btrfs_put_block_group(cache
);
9677 update_global_block_rsv(root
->fs_info
);
9679 spin_lock(&cache
->space_info
->lock
);
9680 cache
->space_info
->bytes_readonly
+= cache
->bytes_super
;
9681 spin_unlock(&cache
->space_info
->lock
);
9683 __link_block_group(cache
->space_info
, cache
);
9685 list_add_tail(&cache
->bg_list
, &trans
->new_bgs
);
9687 set_avail_alloc_bits(extent_root
->fs_info
, type
);
9692 static void clear_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
9694 u64 extra_flags
= chunk_to_extended(flags
) &
9695 BTRFS_EXTENDED_PROFILE_MASK
;
9697 write_seqlock(&fs_info
->profiles_lock
);
9698 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
9699 fs_info
->avail_data_alloc_bits
&= ~extra_flags
;
9700 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
9701 fs_info
->avail_metadata_alloc_bits
&= ~extra_flags
;
9702 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
9703 fs_info
->avail_system_alloc_bits
&= ~extra_flags
;
9704 write_sequnlock(&fs_info
->profiles_lock
);
9707 int btrfs_remove_block_group(struct btrfs_trans_handle
*trans
,
9708 struct btrfs_root
*root
, u64 group_start
,
9709 struct extent_map
*em
)
9711 struct btrfs_path
*path
;
9712 struct btrfs_block_group_cache
*block_group
;
9713 struct btrfs_free_cluster
*cluster
;
9714 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
9715 struct btrfs_key key
;
9716 struct inode
*inode
;
9717 struct kobject
*kobj
= NULL
;
9721 struct btrfs_caching_control
*caching_ctl
= NULL
;
9724 root
= root
->fs_info
->extent_root
;
9726 block_group
= btrfs_lookup_block_group(root
->fs_info
, group_start
);
9727 BUG_ON(!block_group
);
9728 BUG_ON(!block_group
->ro
);
9731 * Free the reserved super bytes from this block group before
9734 free_excluded_extents(root
, block_group
);
9736 memcpy(&key
, &block_group
->key
, sizeof(key
));
9737 index
= get_block_group_index(block_group
);
9738 if (block_group
->flags
& (BTRFS_BLOCK_GROUP_DUP
|
9739 BTRFS_BLOCK_GROUP_RAID1
|
9740 BTRFS_BLOCK_GROUP_RAID10
))
9745 /* make sure this block group isn't part of an allocation cluster */
9746 cluster
= &root
->fs_info
->data_alloc_cluster
;
9747 spin_lock(&cluster
->refill_lock
);
9748 btrfs_return_cluster_to_free_space(block_group
, cluster
);
9749 spin_unlock(&cluster
->refill_lock
);
9752 * make sure this block group isn't part of a metadata
9753 * allocation cluster
9755 cluster
= &root
->fs_info
->meta_alloc_cluster
;
9756 spin_lock(&cluster
->refill_lock
);
9757 btrfs_return_cluster_to_free_space(block_group
, cluster
);
9758 spin_unlock(&cluster
->refill_lock
);
9760 path
= btrfs_alloc_path();
9767 * get the inode first so any iput calls done for the io_list
9768 * aren't the final iput (no unlinks allowed now)
9770 inode
= lookup_free_space_inode(tree_root
, block_group
, path
);
9772 mutex_lock(&trans
->transaction
->cache_write_mutex
);
9774 * make sure our free spache cache IO is done before remove the
9777 spin_lock(&trans
->transaction
->dirty_bgs_lock
);
9778 if (!list_empty(&block_group
->io_list
)) {
9779 list_del_init(&block_group
->io_list
);
9781 WARN_ON(!IS_ERR(inode
) && inode
!= block_group
->io_ctl
.inode
);
9783 spin_unlock(&trans
->transaction
->dirty_bgs_lock
);
9784 btrfs_wait_cache_io(root
, trans
, block_group
,
9785 &block_group
->io_ctl
, path
,
9786 block_group
->key
.objectid
);
9787 btrfs_put_block_group(block_group
);
9788 spin_lock(&trans
->transaction
->dirty_bgs_lock
);
9791 if (!list_empty(&block_group
->dirty_list
)) {
9792 list_del_init(&block_group
->dirty_list
);
9793 btrfs_put_block_group(block_group
);
9795 spin_unlock(&trans
->transaction
->dirty_bgs_lock
);
9796 mutex_unlock(&trans
->transaction
->cache_write_mutex
);
9798 if (!IS_ERR(inode
)) {
9799 ret
= btrfs_orphan_add(trans
, inode
);
9801 btrfs_add_delayed_iput(inode
);
9805 /* One for the block groups ref */
9806 spin_lock(&block_group
->lock
);
9807 if (block_group
->iref
) {
9808 block_group
->iref
= 0;
9809 block_group
->inode
= NULL
;
9810 spin_unlock(&block_group
->lock
);
9813 spin_unlock(&block_group
->lock
);
9815 /* One for our lookup ref */
9816 btrfs_add_delayed_iput(inode
);
9819 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
9820 key
.offset
= block_group
->key
.objectid
;
9823 ret
= btrfs_search_slot(trans
, tree_root
, &key
, path
, -1, 1);
9827 btrfs_release_path(path
);
9829 ret
= btrfs_del_item(trans
, tree_root
, path
);
9832 btrfs_release_path(path
);
9835 spin_lock(&root
->fs_info
->block_group_cache_lock
);
9836 rb_erase(&block_group
->cache_node
,
9837 &root
->fs_info
->block_group_cache_tree
);
9838 RB_CLEAR_NODE(&block_group
->cache_node
);
9840 if (root
->fs_info
->first_logical_byte
== block_group
->key
.objectid
)
9841 root
->fs_info
->first_logical_byte
= (u64
)-1;
9842 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
9844 down_write(&block_group
->space_info
->groups_sem
);
9846 * we must use list_del_init so people can check to see if they
9847 * are still on the list after taking the semaphore
9849 list_del_init(&block_group
->list
);
9850 if (list_empty(&block_group
->space_info
->block_groups
[index
])) {
9851 kobj
= block_group
->space_info
->block_group_kobjs
[index
];
9852 block_group
->space_info
->block_group_kobjs
[index
] = NULL
;
9853 clear_avail_alloc_bits(root
->fs_info
, block_group
->flags
);
9855 up_write(&block_group
->space_info
->groups_sem
);
9861 if (block_group
->has_caching_ctl
)
9862 caching_ctl
= get_caching_control(block_group
);
9863 if (block_group
->cached
== BTRFS_CACHE_STARTED
)
9864 wait_block_group_cache_done(block_group
);
9865 if (block_group
->has_caching_ctl
) {
9866 down_write(&root
->fs_info
->commit_root_sem
);
9868 struct btrfs_caching_control
*ctl
;
9870 list_for_each_entry(ctl
,
9871 &root
->fs_info
->caching_block_groups
, list
)
9872 if (ctl
->block_group
== block_group
) {
9874 atomic_inc(&caching_ctl
->count
);
9879 list_del_init(&caching_ctl
->list
);
9880 up_write(&root
->fs_info
->commit_root_sem
);
9882 /* Once for the caching bgs list and once for us. */
9883 put_caching_control(caching_ctl
);
9884 put_caching_control(caching_ctl
);
9888 spin_lock(&trans
->transaction
->dirty_bgs_lock
);
9889 if (!list_empty(&block_group
->dirty_list
)) {
9892 if (!list_empty(&block_group
->io_list
)) {
9895 spin_unlock(&trans
->transaction
->dirty_bgs_lock
);
9896 btrfs_remove_free_space_cache(block_group
);
9898 spin_lock(&block_group
->space_info
->lock
);
9899 list_del_init(&block_group
->ro_list
);
9901 if (btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
9902 WARN_ON(block_group
->space_info
->total_bytes
9903 < block_group
->key
.offset
);
9904 WARN_ON(block_group
->space_info
->bytes_readonly
9905 < block_group
->key
.offset
);
9906 WARN_ON(block_group
->space_info
->disk_total
9907 < block_group
->key
.offset
* factor
);
9909 block_group
->space_info
->total_bytes
-= block_group
->key
.offset
;
9910 block_group
->space_info
->bytes_readonly
-= block_group
->key
.offset
;
9911 block_group
->space_info
->disk_total
-= block_group
->key
.offset
* factor
;
9913 spin_unlock(&block_group
->space_info
->lock
);
9915 memcpy(&key
, &block_group
->key
, sizeof(key
));
9918 if (!list_empty(&em
->list
)) {
9919 /* We're in the transaction->pending_chunks list. */
9920 free_extent_map(em
);
9922 spin_lock(&block_group
->lock
);
9923 block_group
->removed
= 1;
9925 * At this point trimming can't start on this block group, because we
9926 * removed the block group from the tree fs_info->block_group_cache_tree
9927 * so no one can't find it anymore and even if someone already got this
9928 * block group before we removed it from the rbtree, they have already
9929 * incremented block_group->trimming - if they didn't, they won't find
9930 * any free space entries because we already removed them all when we
9931 * called btrfs_remove_free_space_cache().
9933 * And we must not remove the extent map from the fs_info->mapping_tree
9934 * to prevent the same logical address range and physical device space
9935 * ranges from being reused for a new block group. This is because our
9936 * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
9937 * completely transactionless, so while it is trimming a range the
9938 * currently running transaction might finish and a new one start,
9939 * allowing for new block groups to be created that can reuse the same
9940 * physical device locations unless we take this special care.
9942 * There may also be an implicit trim operation if the file system
9943 * is mounted with -odiscard. The same protections must remain
9944 * in place until the extents have been discarded completely when
9945 * the transaction commit has completed.
9947 remove_em
= (atomic_read(&block_group
->trimming
) == 0);
9949 * Make sure a trimmer task always sees the em in the pinned_chunks list
9950 * if it sees block_group->removed == 1 (needs to lock block_group->lock
9951 * before checking block_group->removed).
9955 * Our em might be in trans->transaction->pending_chunks which
9956 * is protected by fs_info->chunk_mutex ([lock|unlock]_chunks),
9957 * and so is the fs_info->pinned_chunks list.
9959 * So at this point we must be holding the chunk_mutex to avoid
9960 * any races with chunk allocation (more specifically at
9961 * volumes.c:contains_pending_extent()), to ensure it always
9962 * sees the em, either in the pending_chunks list or in the
9963 * pinned_chunks list.
9965 list_move_tail(&em
->list
, &root
->fs_info
->pinned_chunks
);
9967 spin_unlock(&block_group
->lock
);
9970 struct extent_map_tree
*em_tree
;
9972 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
9973 write_lock(&em_tree
->lock
);
9975 * The em might be in the pending_chunks list, so make sure the
9976 * chunk mutex is locked, since remove_extent_mapping() will
9977 * delete us from that list.
9979 remove_extent_mapping(em_tree
, em
);
9980 write_unlock(&em_tree
->lock
);
9981 /* once for the tree */
9982 free_extent_map(em
);
9985 unlock_chunks(root
);
9987 btrfs_put_block_group(block_group
);
9988 btrfs_put_block_group(block_group
);
9990 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
9996 ret
= btrfs_del_item(trans
, root
, path
);
9998 btrfs_free_path(path
);
10003 * Process the unused_bgs list and remove any that don't have any allocated
10004 * space inside of them.
10006 void btrfs_delete_unused_bgs(struct btrfs_fs_info
*fs_info
)
10008 struct btrfs_block_group_cache
*block_group
;
10009 struct btrfs_space_info
*space_info
;
10010 struct btrfs_root
*root
= fs_info
->extent_root
;
10011 struct btrfs_trans_handle
*trans
;
10014 if (!fs_info
->open
)
10017 spin_lock(&fs_info
->unused_bgs_lock
);
10018 while (!list_empty(&fs_info
->unused_bgs
)) {
10022 block_group
= list_first_entry(&fs_info
->unused_bgs
,
10023 struct btrfs_block_group_cache
,
10025 space_info
= block_group
->space_info
;
10026 list_del_init(&block_group
->bg_list
);
10027 if (ret
|| btrfs_mixed_space_info(space_info
)) {
10028 btrfs_put_block_group(block_group
);
10031 spin_unlock(&fs_info
->unused_bgs_lock
);
10033 mutex_lock(&root
->fs_info
->delete_unused_bgs_mutex
);
10035 /* Don't want to race with allocators so take the groups_sem */
10036 down_write(&space_info
->groups_sem
);
10037 spin_lock(&block_group
->lock
);
10038 if (block_group
->reserved
||
10039 btrfs_block_group_used(&block_group
->item
) ||
10042 * We want to bail if we made new allocations or have
10043 * outstanding allocations in this block group. We do
10044 * the ro check in case balance is currently acting on
10045 * this block group.
10047 spin_unlock(&block_group
->lock
);
10048 up_write(&space_info
->groups_sem
);
10051 spin_unlock(&block_group
->lock
);
10053 /* We don't want to force the issue, only flip if it's ok. */
10054 ret
= inc_block_group_ro(block_group
, 0);
10055 up_write(&space_info
->groups_sem
);
10062 * Want to do this before we do anything else so we can recover
10063 * properly if we fail to join the transaction.
10065 /* 1 for btrfs_orphan_reserve_metadata() */
10066 trans
= btrfs_start_transaction(root
, 1);
10067 if (IS_ERR(trans
)) {
10068 btrfs_dec_block_group_ro(root
, block_group
);
10069 ret
= PTR_ERR(trans
);
10074 * We could have pending pinned extents for this block group,
10075 * just delete them, we don't care about them anymore.
10077 start
= block_group
->key
.objectid
;
10078 end
= start
+ block_group
->key
.offset
- 1;
10080 * Hold the unused_bg_unpin_mutex lock to avoid racing with
10081 * btrfs_finish_extent_commit(). If we are at transaction N,
10082 * another task might be running finish_extent_commit() for the
10083 * previous transaction N - 1, and have seen a range belonging
10084 * to the block group in freed_extents[] before we were able to
10085 * clear the whole block group range from freed_extents[]. This
10086 * means that task can lookup for the block group after we
10087 * unpinned it from freed_extents[] and removed it, leading to
10088 * a BUG_ON() at btrfs_unpin_extent_range().
10090 mutex_lock(&fs_info
->unused_bg_unpin_mutex
);
10091 ret
= clear_extent_bits(&fs_info
->freed_extents
[0], start
, end
,
10092 EXTENT_DIRTY
, GFP_NOFS
);
10094 mutex_unlock(&fs_info
->unused_bg_unpin_mutex
);
10095 btrfs_dec_block_group_ro(root
, block_group
);
10098 ret
= clear_extent_bits(&fs_info
->freed_extents
[1], start
, end
,
10099 EXTENT_DIRTY
, GFP_NOFS
);
10101 mutex_unlock(&fs_info
->unused_bg_unpin_mutex
);
10102 btrfs_dec_block_group_ro(root
, block_group
);
10105 mutex_unlock(&fs_info
->unused_bg_unpin_mutex
);
10107 /* Reset pinned so btrfs_put_block_group doesn't complain */
10108 spin_lock(&space_info
->lock
);
10109 spin_lock(&block_group
->lock
);
10111 space_info
->bytes_pinned
-= block_group
->pinned
;
10112 space_info
->bytes_readonly
+= block_group
->pinned
;
10113 percpu_counter_add(&space_info
->total_bytes_pinned
,
10114 -block_group
->pinned
);
10115 block_group
->pinned
= 0;
10117 spin_unlock(&block_group
->lock
);
10118 spin_unlock(&space_info
->lock
);
10120 /* DISCARD can flip during remount */
10121 trimming
= btrfs_test_opt(root
, DISCARD
);
10123 /* Implicit trim during transaction commit. */
10125 btrfs_get_block_group_trimming(block_group
);
10128 * Btrfs_remove_chunk will abort the transaction if things go
10131 ret
= btrfs_remove_chunk(trans
, root
,
10132 block_group
->key
.objectid
);
10136 btrfs_put_block_group_trimming(block_group
);
10141 * If we're not mounted with -odiscard, we can just forget
10142 * about this block group. Otherwise we'll need to wait
10143 * until transaction commit to do the actual discard.
10146 WARN_ON(!list_empty(&block_group
->bg_list
));
10147 spin_lock(&trans
->transaction
->deleted_bgs_lock
);
10148 list_move(&block_group
->bg_list
,
10149 &trans
->transaction
->deleted_bgs
);
10150 spin_unlock(&trans
->transaction
->deleted_bgs_lock
);
10151 btrfs_get_block_group(block_group
);
10154 btrfs_end_transaction(trans
, root
);
10156 mutex_unlock(&root
->fs_info
->delete_unused_bgs_mutex
);
10157 btrfs_put_block_group(block_group
);
10158 spin_lock(&fs_info
->unused_bgs_lock
);
10160 spin_unlock(&fs_info
->unused_bgs_lock
);
10163 int btrfs_init_space_info(struct btrfs_fs_info
*fs_info
)
10165 struct btrfs_space_info
*space_info
;
10166 struct btrfs_super_block
*disk_super
;
10172 disk_super
= fs_info
->super_copy
;
10173 if (!btrfs_super_root(disk_super
))
10176 features
= btrfs_super_incompat_flags(disk_super
);
10177 if (features
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
10180 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
10181 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
10186 flags
= BTRFS_BLOCK_GROUP_METADATA
| BTRFS_BLOCK_GROUP_DATA
;
10187 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
10189 flags
= BTRFS_BLOCK_GROUP_METADATA
;
10190 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
10194 flags
= BTRFS_BLOCK_GROUP_DATA
;
10195 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
10201 int btrfs_error_unpin_extent_range(struct btrfs_root
*root
, u64 start
, u64 end
)
10203 return unpin_extent_range(root
, start
, end
, false);
10207 * It used to be that old block groups would be left around forever.
10208 * Iterating over them would be enough to trim unused space. Since we
10209 * now automatically remove them, we also need to iterate over unallocated
10212 * We don't want a transaction for this since the discard may take a
10213 * substantial amount of time. We don't require that a transaction be
10214 * running, but we do need to take a running transaction into account
10215 * to ensure that we're not discarding chunks that were released in
10216 * the current transaction.
10218 * Holding the chunks lock will prevent other threads from allocating
10219 * or releasing chunks, but it won't prevent a running transaction
10220 * from committing and releasing the memory that the pending chunks
10221 * list head uses. For that, we need to take a reference to the
10224 static int btrfs_trim_free_extents(struct btrfs_device
*device
,
10225 u64 minlen
, u64
*trimmed
)
10227 u64 start
= 0, len
= 0;
10232 /* Not writeable = nothing to do. */
10233 if (!device
->writeable
)
10236 /* No free space = nothing to do. */
10237 if (device
->total_bytes
<= device
->bytes_used
)
10243 struct btrfs_fs_info
*fs_info
= device
->dev_root
->fs_info
;
10244 struct btrfs_transaction
*trans
;
10247 ret
= mutex_lock_interruptible(&fs_info
->chunk_mutex
);
10251 down_read(&fs_info
->commit_root_sem
);
10253 spin_lock(&fs_info
->trans_lock
);
10254 trans
= fs_info
->running_transaction
;
10256 atomic_inc(&trans
->use_count
);
10257 spin_unlock(&fs_info
->trans_lock
);
10259 ret
= find_free_dev_extent_start(trans
, device
, minlen
, start
,
10262 btrfs_put_transaction(trans
);
10265 up_read(&fs_info
->commit_root_sem
);
10266 mutex_unlock(&fs_info
->chunk_mutex
);
10267 if (ret
== -ENOSPC
)
10272 ret
= btrfs_issue_discard(device
->bdev
, start
, len
, &bytes
);
10273 up_read(&fs_info
->commit_root_sem
);
10274 mutex_unlock(&fs_info
->chunk_mutex
);
10282 if (fatal_signal_pending(current
)) {
10283 ret
= -ERESTARTSYS
;
10293 int btrfs_trim_fs(struct btrfs_root
*root
, struct fstrim_range
*range
)
10295 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
10296 struct btrfs_block_group_cache
*cache
= NULL
;
10297 struct btrfs_device
*device
;
10298 struct list_head
*devices
;
10303 u64 total_bytes
= btrfs_super_total_bytes(fs_info
->super_copy
);
10307 * try to trim all FS space, our block group may start from non-zero.
10309 if (range
->len
== total_bytes
)
10310 cache
= btrfs_lookup_first_block_group(fs_info
, range
->start
);
10312 cache
= btrfs_lookup_block_group(fs_info
, range
->start
);
10315 if (cache
->key
.objectid
>= (range
->start
+ range
->len
)) {
10316 btrfs_put_block_group(cache
);
10320 start
= max(range
->start
, cache
->key
.objectid
);
10321 end
= min(range
->start
+ range
->len
,
10322 cache
->key
.objectid
+ cache
->key
.offset
);
10324 if (end
- start
>= range
->minlen
) {
10325 if (!block_group_cache_done(cache
)) {
10326 ret
= cache_block_group(cache
, 0);
10328 btrfs_put_block_group(cache
);
10331 ret
= wait_block_group_cache_done(cache
);
10333 btrfs_put_block_group(cache
);
10337 ret
= btrfs_trim_block_group(cache
,
10343 trimmed
+= group_trimmed
;
10345 btrfs_put_block_group(cache
);
10350 cache
= next_block_group(fs_info
->tree_root
, cache
);
10353 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
10354 devices
= &root
->fs_info
->fs_devices
->alloc_list
;
10355 list_for_each_entry(device
, devices
, dev_alloc_list
) {
10356 ret
= btrfs_trim_free_extents(device
, range
->minlen
,
10361 trimmed
+= group_trimmed
;
10363 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
10365 range
->len
= trimmed
;
10370 * btrfs_{start,end}_write_no_snapshoting() are similar to
10371 * mnt_{want,drop}_write(), they are used to prevent some tasks from writing
10372 * data into the page cache through nocow before the subvolume is snapshoted,
10373 * but flush the data into disk after the snapshot creation, or to prevent
10374 * operations while snapshoting is ongoing and that cause the snapshot to be
10375 * inconsistent (writes followed by expanding truncates for example).
10377 void btrfs_end_write_no_snapshoting(struct btrfs_root
*root
)
10379 percpu_counter_dec(&root
->subv_writers
->counter
);
10381 * Make sure counter is updated before we wake up
10385 if (waitqueue_active(&root
->subv_writers
->wait
))
10386 wake_up(&root
->subv_writers
->wait
);
10389 int btrfs_start_write_no_snapshoting(struct btrfs_root
*root
)
10391 if (atomic_read(&root
->will_be_snapshoted
))
10394 percpu_counter_inc(&root
->subv_writers
->counter
);
10396 * Make sure counter is updated before we check for snapshot creation.
10399 if (atomic_read(&root
->will_be_snapshoted
)) {
10400 btrfs_end_write_no_snapshoting(root
);