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>
31 #include "print-tree.h"
32 #include "transaction.h"
36 #include "free-space-cache.h"
39 #undef SCRAMBLE_DELAYED_REFS
42 * control flags for do_chunk_alloc's force field
43 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
44 * if we really need one.
46 * CHUNK_ALLOC_LIMITED means to only try and allocate one
47 * if we have very few chunks already allocated. This is
48 * used as part of the clustering code to help make sure
49 * we have a good pool of storage to cluster in, without
50 * filling the FS with empty chunks
52 * CHUNK_ALLOC_FORCE means it must try to allocate one
56 CHUNK_ALLOC_NO_FORCE
= 0,
57 CHUNK_ALLOC_LIMITED
= 1,
58 CHUNK_ALLOC_FORCE
= 2,
62 * Control how reservations are dealt with.
64 * RESERVE_FREE - freeing a reservation.
65 * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for
67 * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update
68 * bytes_may_use as the ENOSPC accounting is done elsewhere
73 RESERVE_ALLOC_NO_ACCOUNT
= 2,
76 static int update_block_group(struct btrfs_root
*root
,
77 u64 bytenr
, u64 num_bytes
, int alloc
);
78 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
79 struct btrfs_root
*root
,
80 u64 bytenr
, u64 num_bytes
, u64 parent
,
81 u64 root_objectid
, u64 owner_objectid
,
82 u64 owner_offset
, int refs_to_drop
,
83 struct btrfs_delayed_extent_op
*extra_op
);
84 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op
*extent_op
,
85 struct extent_buffer
*leaf
,
86 struct btrfs_extent_item
*ei
);
87 static int alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
88 struct btrfs_root
*root
,
89 u64 parent
, u64 root_objectid
,
90 u64 flags
, u64 owner
, u64 offset
,
91 struct btrfs_key
*ins
, int ref_mod
);
92 static int alloc_reserved_tree_block(struct btrfs_trans_handle
*trans
,
93 struct btrfs_root
*root
,
94 u64 parent
, u64 root_objectid
,
95 u64 flags
, struct btrfs_disk_key
*key
,
96 int level
, struct btrfs_key
*ins
);
97 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
98 struct btrfs_root
*extent_root
, u64 flags
,
100 static int find_next_key(struct btrfs_path
*path
, int level
,
101 struct btrfs_key
*key
);
102 static void dump_space_info(struct btrfs_space_info
*info
, u64 bytes
,
103 int dump_block_groups
);
104 static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache
*cache
,
105 u64 num_bytes
, int reserve
);
106 static int block_rsv_use_bytes(struct btrfs_block_rsv
*block_rsv
,
108 int btrfs_pin_extent(struct btrfs_root
*root
,
109 u64 bytenr
, u64 num_bytes
, int reserved
);
112 block_group_cache_done(struct btrfs_block_group_cache
*cache
)
115 return cache
->cached
== BTRFS_CACHE_FINISHED
;
118 static int block_group_bits(struct btrfs_block_group_cache
*cache
, u64 bits
)
120 return (cache
->flags
& bits
) == bits
;
123 static void btrfs_get_block_group(struct btrfs_block_group_cache
*cache
)
125 atomic_inc(&cache
->count
);
128 void btrfs_put_block_group(struct btrfs_block_group_cache
*cache
)
130 if (atomic_dec_and_test(&cache
->count
)) {
131 WARN_ON(cache
->pinned
> 0);
132 WARN_ON(cache
->reserved
> 0);
133 kfree(cache
->free_space_ctl
);
139 * this adds the block group to the fs_info rb tree for the block group
142 static int btrfs_add_block_group_cache(struct btrfs_fs_info
*info
,
143 struct btrfs_block_group_cache
*block_group
)
146 struct rb_node
*parent
= NULL
;
147 struct btrfs_block_group_cache
*cache
;
149 spin_lock(&info
->block_group_cache_lock
);
150 p
= &info
->block_group_cache_tree
.rb_node
;
154 cache
= rb_entry(parent
, struct btrfs_block_group_cache
,
156 if (block_group
->key
.objectid
< cache
->key
.objectid
) {
158 } else if (block_group
->key
.objectid
> cache
->key
.objectid
) {
161 spin_unlock(&info
->block_group_cache_lock
);
166 rb_link_node(&block_group
->cache_node
, parent
, p
);
167 rb_insert_color(&block_group
->cache_node
,
168 &info
->block_group_cache_tree
);
170 if (info
->first_logical_byte
> block_group
->key
.objectid
)
171 info
->first_logical_byte
= block_group
->key
.objectid
;
173 spin_unlock(&info
->block_group_cache_lock
);
179 * This will return the block group at or after bytenr if contains is 0, else
180 * it will return the block group that contains the bytenr
182 static struct btrfs_block_group_cache
*
183 block_group_cache_tree_search(struct btrfs_fs_info
*info
, u64 bytenr
,
186 struct btrfs_block_group_cache
*cache
, *ret
= NULL
;
190 spin_lock(&info
->block_group_cache_lock
);
191 n
= info
->block_group_cache_tree
.rb_node
;
194 cache
= rb_entry(n
, struct btrfs_block_group_cache
,
196 end
= cache
->key
.objectid
+ cache
->key
.offset
- 1;
197 start
= cache
->key
.objectid
;
199 if (bytenr
< start
) {
200 if (!contains
&& (!ret
|| start
< ret
->key
.objectid
))
203 } else if (bytenr
> start
) {
204 if (contains
&& bytenr
<= end
) {
215 btrfs_get_block_group(ret
);
216 if (bytenr
== 0 && info
->first_logical_byte
> ret
->key
.objectid
)
217 info
->first_logical_byte
= ret
->key
.objectid
;
219 spin_unlock(&info
->block_group_cache_lock
);
224 static int add_excluded_extent(struct btrfs_root
*root
,
225 u64 start
, u64 num_bytes
)
227 u64 end
= start
+ num_bytes
- 1;
228 set_extent_bits(&root
->fs_info
->freed_extents
[0],
229 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
230 set_extent_bits(&root
->fs_info
->freed_extents
[1],
231 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
235 static void free_excluded_extents(struct btrfs_root
*root
,
236 struct btrfs_block_group_cache
*cache
)
240 start
= cache
->key
.objectid
;
241 end
= start
+ cache
->key
.offset
- 1;
243 clear_extent_bits(&root
->fs_info
->freed_extents
[0],
244 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
245 clear_extent_bits(&root
->fs_info
->freed_extents
[1],
246 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
249 static int exclude_super_stripes(struct btrfs_root
*root
,
250 struct btrfs_block_group_cache
*cache
)
257 if (cache
->key
.objectid
< BTRFS_SUPER_INFO_OFFSET
) {
258 stripe_len
= BTRFS_SUPER_INFO_OFFSET
- cache
->key
.objectid
;
259 cache
->bytes_super
+= stripe_len
;
260 ret
= add_excluded_extent(root
, cache
->key
.objectid
,
266 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
267 bytenr
= btrfs_sb_offset(i
);
268 ret
= btrfs_rmap_block(&root
->fs_info
->mapping_tree
,
269 cache
->key
.objectid
, bytenr
,
270 0, &logical
, &nr
, &stripe_len
);
277 if (logical
[nr
] > cache
->key
.objectid
+
281 if (logical
[nr
] + stripe_len
<= cache
->key
.objectid
)
285 if (start
< cache
->key
.objectid
) {
286 start
= cache
->key
.objectid
;
287 len
= (logical
[nr
] + stripe_len
) - start
;
289 len
= min_t(u64
, stripe_len
,
290 cache
->key
.objectid
+
291 cache
->key
.offset
- start
);
294 cache
->bytes_super
+= len
;
295 ret
= add_excluded_extent(root
, start
, len
);
307 static struct btrfs_caching_control
*
308 get_caching_control(struct btrfs_block_group_cache
*cache
)
310 struct btrfs_caching_control
*ctl
;
312 spin_lock(&cache
->lock
);
313 if (cache
->cached
!= BTRFS_CACHE_STARTED
) {
314 spin_unlock(&cache
->lock
);
318 /* We're loading it the fast way, so we don't have a caching_ctl. */
319 if (!cache
->caching_ctl
) {
320 spin_unlock(&cache
->lock
);
324 ctl
= cache
->caching_ctl
;
325 atomic_inc(&ctl
->count
);
326 spin_unlock(&cache
->lock
);
330 static void put_caching_control(struct btrfs_caching_control
*ctl
)
332 if (atomic_dec_and_test(&ctl
->count
))
337 * this is only called by cache_block_group, since we could have freed extents
338 * we need to check the pinned_extents for any extents that can't be used yet
339 * since their free space will be released as soon as the transaction commits.
341 static u64
add_new_free_space(struct btrfs_block_group_cache
*block_group
,
342 struct btrfs_fs_info
*info
, u64 start
, u64 end
)
344 u64 extent_start
, extent_end
, size
, total_added
= 0;
347 while (start
< end
) {
348 ret
= find_first_extent_bit(info
->pinned_extents
, start
,
349 &extent_start
, &extent_end
,
350 EXTENT_DIRTY
| EXTENT_UPTODATE
,
355 if (extent_start
<= start
) {
356 start
= extent_end
+ 1;
357 } else if (extent_start
> start
&& extent_start
< end
) {
358 size
= extent_start
- start
;
360 ret
= btrfs_add_free_space(block_group
, start
,
362 BUG_ON(ret
); /* -ENOMEM or logic error */
363 start
= extent_end
+ 1;
372 ret
= btrfs_add_free_space(block_group
, start
, size
);
373 BUG_ON(ret
); /* -ENOMEM or logic error */
379 static noinline
void caching_thread(struct btrfs_work
*work
)
381 struct btrfs_block_group_cache
*block_group
;
382 struct btrfs_fs_info
*fs_info
;
383 struct btrfs_caching_control
*caching_ctl
;
384 struct btrfs_root
*extent_root
;
385 struct btrfs_path
*path
;
386 struct extent_buffer
*leaf
;
387 struct btrfs_key key
;
393 caching_ctl
= container_of(work
, struct btrfs_caching_control
, work
);
394 block_group
= caching_ctl
->block_group
;
395 fs_info
= block_group
->fs_info
;
396 extent_root
= fs_info
->extent_root
;
398 path
= btrfs_alloc_path();
402 last
= max_t(u64
, block_group
->key
.objectid
, BTRFS_SUPER_INFO_OFFSET
);
405 * We don't want to deadlock with somebody trying to allocate a new
406 * extent for the extent root while also trying to search the extent
407 * root to add free space. So we skip locking and search the commit
408 * root, since its read-only
410 path
->skip_locking
= 1;
411 path
->search_commit_root
= 1;
416 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
418 mutex_lock(&caching_ctl
->mutex
);
419 /* need to make sure the commit_root doesn't disappear */
420 down_read(&fs_info
->extent_commit_sem
);
422 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
426 leaf
= path
->nodes
[0];
427 nritems
= btrfs_header_nritems(leaf
);
430 if (btrfs_fs_closing(fs_info
) > 1) {
435 if (path
->slots
[0] < nritems
) {
436 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
438 ret
= find_next_key(path
, 0, &key
);
442 if (need_resched()) {
443 caching_ctl
->progress
= last
;
444 btrfs_release_path(path
);
445 up_read(&fs_info
->extent_commit_sem
);
446 mutex_unlock(&caching_ctl
->mutex
);
451 ret
= btrfs_next_leaf(extent_root
, path
);
456 leaf
= path
->nodes
[0];
457 nritems
= btrfs_header_nritems(leaf
);
461 if (key
.objectid
< block_group
->key
.objectid
) {
466 if (key
.objectid
>= block_group
->key
.objectid
+
467 block_group
->key
.offset
)
470 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
||
471 key
.type
== BTRFS_METADATA_ITEM_KEY
) {
472 total_found
+= add_new_free_space(block_group
,
475 if (key
.type
== BTRFS_METADATA_ITEM_KEY
)
476 last
= key
.objectid
+
477 fs_info
->tree_root
->leafsize
;
479 last
= key
.objectid
+ key
.offset
;
481 if (total_found
> (1024 * 1024 * 2)) {
483 wake_up(&caching_ctl
->wait
);
490 total_found
+= add_new_free_space(block_group
, fs_info
, last
,
491 block_group
->key
.objectid
+
492 block_group
->key
.offset
);
493 caching_ctl
->progress
= (u64
)-1;
495 spin_lock(&block_group
->lock
);
496 block_group
->caching_ctl
= NULL
;
497 block_group
->cached
= BTRFS_CACHE_FINISHED
;
498 spin_unlock(&block_group
->lock
);
501 btrfs_free_path(path
);
502 up_read(&fs_info
->extent_commit_sem
);
504 free_excluded_extents(extent_root
, block_group
);
506 mutex_unlock(&caching_ctl
->mutex
);
508 wake_up(&caching_ctl
->wait
);
510 put_caching_control(caching_ctl
);
511 btrfs_put_block_group(block_group
);
514 static int cache_block_group(struct btrfs_block_group_cache
*cache
,
518 struct btrfs_fs_info
*fs_info
= cache
->fs_info
;
519 struct btrfs_caching_control
*caching_ctl
;
522 caching_ctl
= kzalloc(sizeof(*caching_ctl
), GFP_NOFS
);
526 INIT_LIST_HEAD(&caching_ctl
->list
);
527 mutex_init(&caching_ctl
->mutex
);
528 init_waitqueue_head(&caching_ctl
->wait
);
529 caching_ctl
->block_group
= cache
;
530 caching_ctl
->progress
= cache
->key
.objectid
;
531 atomic_set(&caching_ctl
->count
, 1);
532 caching_ctl
->work
.func
= caching_thread
;
534 spin_lock(&cache
->lock
);
536 * This should be a rare occasion, but this could happen I think in the
537 * case where one thread starts to load the space cache info, and then
538 * some other thread starts a transaction commit which tries to do an
539 * allocation while the other thread is still loading the space cache
540 * info. The previous loop should have kept us from choosing this block
541 * group, but if we've moved to the state where we will wait on caching
542 * block groups we need to first check if we're doing a fast load here,
543 * so we can wait for it to finish, otherwise we could end up allocating
544 * from a block group who's cache gets evicted for one reason or
547 while (cache
->cached
== BTRFS_CACHE_FAST
) {
548 struct btrfs_caching_control
*ctl
;
550 ctl
= cache
->caching_ctl
;
551 atomic_inc(&ctl
->count
);
552 prepare_to_wait(&ctl
->wait
, &wait
, TASK_UNINTERRUPTIBLE
);
553 spin_unlock(&cache
->lock
);
557 finish_wait(&ctl
->wait
, &wait
);
558 put_caching_control(ctl
);
559 spin_lock(&cache
->lock
);
562 if (cache
->cached
!= BTRFS_CACHE_NO
) {
563 spin_unlock(&cache
->lock
);
567 WARN_ON(cache
->caching_ctl
);
568 cache
->caching_ctl
= caching_ctl
;
569 cache
->cached
= BTRFS_CACHE_FAST
;
570 spin_unlock(&cache
->lock
);
572 if (fs_info
->mount_opt
& BTRFS_MOUNT_SPACE_CACHE
) {
573 ret
= load_free_space_cache(fs_info
, cache
);
575 spin_lock(&cache
->lock
);
577 cache
->caching_ctl
= NULL
;
578 cache
->cached
= BTRFS_CACHE_FINISHED
;
579 cache
->last_byte_to_unpin
= (u64
)-1;
581 if (load_cache_only
) {
582 cache
->caching_ctl
= NULL
;
583 cache
->cached
= BTRFS_CACHE_NO
;
585 cache
->cached
= BTRFS_CACHE_STARTED
;
588 spin_unlock(&cache
->lock
);
589 wake_up(&caching_ctl
->wait
);
591 put_caching_control(caching_ctl
);
592 free_excluded_extents(fs_info
->extent_root
, cache
);
597 * We are not going to do the fast caching, set cached to the
598 * appropriate value and wakeup any waiters.
600 spin_lock(&cache
->lock
);
601 if (load_cache_only
) {
602 cache
->caching_ctl
= NULL
;
603 cache
->cached
= BTRFS_CACHE_NO
;
605 cache
->cached
= BTRFS_CACHE_STARTED
;
607 spin_unlock(&cache
->lock
);
608 wake_up(&caching_ctl
->wait
);
611 if (load_cache_only
) {
612 put_caching_control(caching_ctl
);
616 down_write(&fs_info
->extent_commit_sem
);
617 atomic_inc(&caching_ctl
->count
);
618 list_add_tail(&caching_ctl
->list
, &fs_info
->caching_block_groups
);
619 up_write(&fs_info
->extent_commit_sem
);
621 btrfs_get_block_group(cache
);
623 btrfs_queue_worker(&fs_info
->caching_workers
, &caching_ctl
->work
);
629 * return the block group that starts at or after bytenr
631 static struct btrfs_block_group_cache
*
632 btrfs_lookup_first_block_group(struct btrfs_fs_info
*info
, u64 bytenr
)
634 struct btrfs_block_group_cache
*cache
;
636 cache
= block_group_cache_tree_search(info
, bytenr
, 0);
642 * return the block group that contains the given bytenr
644 struct btrfs_block_group_cache
*btrfs_lookup_block_group(
645 struct btrfs_fs_info
*info
,
648 struct btrfs_block_group_cache
*cache
;
650 cache
= block_group_cache_tree_search(info
, bytenr
, 1);
655 static struct btrfs_space_info
*__find_space_info(struct btrfs_fs_info
*info
,
658 struct list_head
*head
= &info
->space_info
;
659 struct btrfs_space_info
*found
;
661 flags
&= BTRFS_BLOCK_GROUP_TYPE_MASK
;
664 list_for_each_entry_rcu(found
, head
, list
) {
665 if (found
->flags
& flags
) {
675 * after adding space to the filesystem, we need to clear the full flags
676 * on all the space infos.
678 void btrfs_clear_space_info_full(struct btrfs_fs_info
*info
)
680 struct list_head
*head
= &info
->space_info
;
681 struct btrfs_space_info
*found
;
684 list_for_each_entry_rcu(found
, head
, list
)
689 /* simple helper to search for an existing extent at a given offset */
690 int btrfs_lookup_extent(struct btrfs_root
*root
, u64 start
, u64 len
)
693 struct btrfs_key key
;
694 struct btrfs_path
*path
;
696 path
= btrfs_alloc_path();
700 key
.objectid
= start
;
702 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
703 ret
= btrfs_search_slot(NULL
, root
->fs_info
->extent_root
, &key
, path
,
706 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
707 if (key
.objectid
== start
&&
708 key
.type
== BTRFS_METADATA_ITEM_KEY
)
711 btrfs_free_path(path
);
716 * helper function to lookup reference count and flags of a tree block.
718 * the head node for delayed ref is used to store the sum of all the
719 * reference count modifications queued up in the rbtree. the head
720 * node may also store the extent flags to set. This way you can check
721 * to see what the reference count and extent flags would be if all of
722 * the delayed refs are not processed.
724 int btrfs_lookup_extent_info(struct btrfs_trans_handle
*trans
,
725 struct btrfs_root
*root
, u64 bytenr
,
726 u64 offset
, int metadata
, u64
*refs
, u64
*flags
)
728 struct btrfs_delayed_ref_head
*head
;
729 struct btrfs_delayed_ref_root
*delayed_refs
;
730 struct btrfs_path
*path
;
731 struct btrfs_extent_item
*ei
;
732 struct extent_buffer
*leaf
;
733 struct btrfs_key key
;
740 * If we don't have skinny metadata, don't bother doing anything
743 if (metadata
&& !btrfs_fs_incompat(root
->fs_info
, SKINNY_METADATA
)) {
744 offset
= root
->leafsize
;
748 path
= btrfs_alloc_path();
753 key
.objectid
= bytenr
;
754 key
.type
= BTRFS_METADATA_ITEM_KEY
;
757 key
.objectid
= bytenr
;
758 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
763 path
->skip_locking
= 1;
764 path
->search_commit_root
= 1;
767 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
,
772 if (ret
> 0 && metadata
&& key
.type
== BTRFS_METADATA_ITEM_KEY
) {
773 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
774 key
.offset
= root
->leafsize
;
775 btrfs_release_path(path
);
780 leaf
= path
->nodes
[0];
781 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
782 if (item_size
>= sizeof(*ei
)) {
783 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
784 struct btrfs_extent_item
);
785 num_refs
= btrfs_extent_refs(leaf
, ei
);
786 extent_flags
= btrfs_extent_flags(leaf
, ei
);
788 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
789 struct btrfs_extent_item_v0
*ei0
;
790 BUG_ON(item_size
!= sizeof(*ei0
));
791 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
792 struct btrfs_extent_item_v0
);
793 num_refs
= btrfs_extent_refs_v0(leaf
, ei0
);
794 /* FIXME: this isn't correct for data */
795 extent_flags
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
800 BUG_ON(num_refs
== 0);
810 delayed_refs
= &trans
->transaction
->delayed_refs
;
811 spin_lock(&delayed_refs
->lock
);
812 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
814 if (!mutex_trylock(&head
->mutex
)) {
815 atomic_inc(&head
->node
.refs
);
816 spin_unlock(&delayed_refs
->lock
);
818 btrfs_release_path(path
);
821 * Mutex was contended, block until it's released and try
824 mutex_lock(&head
->mutex
);
825 mutex_unlock(&head
->mutex
);
826 btrfs_put_delayed_ref(&head
->node
);
829 if (head
->extent_op
&& head
->extent_op
->update_flags
)
830 extent_flags
|= head
->extent_op
->flags_to_set
;
832 BUG_ON(num_refs
== 0);
834 num_refs
+= head
->node
.ref_mod
;
835 mutex_unlock(&head
->mutex
);
837 spin_unlock(&delayed_refs
->lock
);
839 WARN_ON(num_refs
== 0);
843 *flags
= extent_flags
;
845 btrfs_free_path(path
);
850 * Back reference rules. Back refs have three main goals:
852 * 1) differentiate between all holders of references to an extent so that
853 * when a reference is dropped we can make sure it was a valid reference
854 * before freeing the extent.
856 * 2) Provide enough information to quickly find the holders of an extent
857 * if we notice a given block is corrupted or bad.
859 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
860 * maintenance. This is actually the same as #2, but with a slightly
861 * different use case.
863 * There are two kinds of back refs. The implicit back refs is optimized
864 * for pointers in non-shared tree blocks. For a given pointer in a block,
865 * back refs of this kind provide information about the block's owner tree
866 * and the pointer's key. These information allow us to find the block by
867 * b-tree searching. The full back refs is for pointers in tree blocks not
868 * referenced by their owner trees. The location of tree block is recorded
869 * in the back refs. Actually the full back refs is generic, and can be
870 * used in all cases the implicit back refs is used. The major shortcoming
871 * of the full back refs is its overhead. Every time a tree block gets
872 * COWed, we have to update back refs entry for all pointers in it.
874 * For a newly allocated tree block, we use implicit back refs for
875 * pointers in it. This means most tree related operations only involve
876 * implicit back refs. For a tree block created in old transaction, the
877 * only way to drop a reference to it is COW it. So we can detect the
878 * event that tree block loses its owner tree's reference and do the
879 * back refs conversion.
881 * When a tree block is COW'd through a tree, there are four cases:
883 * The reference count of the block is one and the tree is the block's
884 * owner tree. Nothing to do in this case.
886 * The reference count of the block is one and the tree is not the
887 * block's owner tree. In this case, full back refs is used for pointers
888 * in the block. Remove these full back refs, add implicit back refs for
889 * every pointers in the new block.
891 * The reference count of the block is greater than one and the tree is
892 * the block's owner tree. In this case, implicit back refs is used for
893 * pointers in the block. Add full back refs for every pointers in the
894 * block, increase lower level extents' reference counts. The original
895 * implicit back refs are entailed to the new block.
897 * The reference count of the block is greater than one and the tree is
898 * not the block's owner tree. Add implicit back refs for every pointer in
899 * the new block, increase lower level extents' reference count.
901 * Back Reference Key composing:
903 * The key objectid corresponds to the first byte in the extent,
904 * The key type is used to differentiate between types of back refs.
905 * There are different meanings of the key offset for different types
908 * File extents can be referenced by:
910 * - multiple snapshots, subvolumes, or different generations in one subvol
911 * - different files inside a single subvolume
912 * - different offsets inside a file (bookend extents in file.c)
914 * The extent ref structure for the implicit back refs has fields for:
916 * - Objectid of the subvolume root
917 * - objectid of the file holding the reference
918 * - original offset in the file
919 * - how many bookend extents
921 * The key offset for the implicit back refs is hash of the first
924 * The extent ref structure for the full back refs has field for:
926 * - number of pointers in the tree leaf
928 * The key offset for the implicit back refs is the first byte of
931 * When a file extent is allocated, The implicit back refs is used.
932 * the fields are filled in:
934 * (root_key.objectid, inode objectid, offset in file, 1)
936 * When a file extent is removed file truncation, we find the
937 * corresponding implicit back refs and check the following fields:
939 * (btrfs_header_owner(leaf), inode objectid, offset in file)
941 * Btree extents can be referenced by:
943 * - Different subvolumes
945 * Both the implicit back refs and the full back refs for tree blocks
946 * only consist of key. The key offset for the implicit back refs is
947 * objectid of block's owner tree. The key offset for the full back refs
948 * is the first byte of parent block.
950 * When implicit back refs is used, information about the lowest key and
951 * level of the tree block are required. These information are stored in
952 * tree block info structure.
955 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
956 static int convert_extent_item_v0(struct btrfs_trans_handle
*trans
,
957 struct btrfs_root
*root
,
958 struct btrfs_path
*path
,
959 u64 owner
, u32 extra_size
)
961 struct btrfs_extent_item
*item
;
962 struct btrfs_extent_item_v0
*ei0
;
963 struct btrfs_extent_ref_v0
*ref0
;
964 struct btrfs_tree_block_info
*bi
;
965 struct extent_buffer
*leaf
;
966 struct btrfs_key key
;
967 struct btrfs_key found_key
;
968 u32 new_size
= sizeof(*item
);
972 leaf
= path
->nodes
[0];
973 BUG_ON(btrfs_item_size_nr(leaf
, path
->slots
[0]) != sizeof(*ei0
));
975 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
976 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
977 struct btrfs_extent_item_v0
);
978 refs
= btrfs_extent_refs_v0(leaf
, ei0
);
980 if (owner
== (u64
)-1) {
982 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
983 ret
= btrfs_next_leaf(root
, path
);
986 BUG_ON(ret
> 0); /* Corruption */
987 leaf
= path
->nodes
[0];
989 btrfs_item_key_to_cpu(leaf
, &found_key
,
991 BUG_ON(key
.objectid
!= found_key
.objectid
);
992 if (found_key
.type
!= BTRFS_EXTENT_REF_V0_KEY
) {
996 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
997 struct btrfs_extent_ref_v0
);
998 owner
= btrfs_ref_objectid_v0(leaf
, ref0
);
1002 btrfs_release_path(path
);
1004 if (owner
< BTRFS_FIRST_FREE_OBJECTID
)
1005 new_size
+= sizeof(*bi
);
1007 new_size
-= sizeof(*ei0
);
1008 ret
= btrfs_search_slot(trans
, root
, &key
, path
,
1009 new_size
+ extra_size
, 1);
1012 BUG_ON(ret
); /* Corruption */
1014 btrfs_extend_item(root
, path
, new_size
);
1016 leaf
= path
->nodes
[0];
1017 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1018 btrfs_set_extent_refs(leaf
, item
, refs
);
1019 /* FIXME: get real generation */
1020 btrfs_set_extent_generation(leaf
, item
, 0);
1021 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1022 btrfs_set_extent_flags(leaf
, item
,
1023 BTRFS_EXTENT_FLAG_TREE_BLOCK
|
1024 BTRFS_BLOCK_FLAG_FULL_BACKREF
);
1025 bi
= (struct btrfs_tree_block_info
*)(item
+ 1);
1026 /* FIXME: get first key of the block */
1027 memset_extent_buffer(leaf
, 0, (unsigned long)bi
, sizeof(*bi
));
1028 btrfs_set_tree_block_level(leaf
, bi
, (int)owner
);
1030 btrfs_set_extent_flags(leaf
, item
, BTRFS_EXTENT_FLAG_DATA
);
1032 btrfs_mark_buffer_dirty(leaf
);
1037 static u64
hash_extent_data_ref(u64 root_objectid
, u64 owner
, u64 offset
)
1039 u32 high_crc
= ~(u32
)0;
1040 u32 low_crc
= ~(u32
)0;
1043 lenum
= cpu_to_le64(root_objectid
);
1044 high_crc
= crc32c(high_crc
, &lenum
, sizeof(lenum
));
1045 lenum
= cpu_to_le64(owner
);
1046 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
1047 lenum
= cpu_to_le64(offset
);
1048 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
1050 return ((u64
)high_crc
<< 31) ^ (u64
)low_crc
;
1053 static u64
hash_extent_data_ref_item(struct extent_buffer
*leaf
,
1054 struct btrfs_extent_data_ref
*ref
)
1056 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf
, ref
),
1057 btrfs_extent_data_ref_objectid(leaf
, ref
),
1058 btrfs_extent_data_ref_offset(leaf
, ref
));
1061 static int match_extent_data_ref(struct extent_buffer
*leaf
,
1062 struct btrfs_extent_data_ref
*ref
,
1063 u64 root_objectid
, u64 owner
, u64 offset
)
1065 if (btrfs_extent_data_ref_root(leaf
, ref
) != root_objectid
||
1066 btrfs_extent_data_ref_objectid(leaf
, ref
) != owner
||
1067 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
1072 static noinline
int lookup_extent_data_ref(struct btrfs_trans_handle
*trans
,
1073 struct btrfs_root
*root
,
1074 struct btrfs_path
*path
,
1075 u64 bytenr
, u64 parent
,
1077 u64 owner
, u64 offset
)
1079 struct btrfs_key key
;
1080 struct btrfs_extent_data_ref
*ref
;
1081 struct extent_buffer
*leaf
;
1087 key
.objectid
= bytenr
;
1089 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
1090 key
.offset
= parent
;
1092 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
1093 key
.offset
= hash_extent_data_ref(root_objectid
,
1098 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1107 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1108 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
1109 btrfs_release_path(path
);
1110 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1121 leaf
= path
->nodes
[0];
1122 nritems
= btrfs_header_nritems(leaf
);
1124 if (path
->slots
[0] >= nritems
) {
1125 ret
= btrfs_next_leaf(root
, path
);
1131 leaf
= path
->nodes
[0];
1132 nritems
= btrfs_header_nritems(leaf
);
1136 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1137 if (key
.objectid
!= bytenr
||
1138 key
.type
!= BTRFS_EXTENT_DATA_REF_KEY
)
1141 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1142 struct btrfs_extent_data_ref
);
1144 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
1147 btrfs_release_path(path
);
1159 static noinline
int insert_extent_data_ref(struct btrfs_trans_handle
*trans
,
1160 struct btrfs_root
*root
,
1161 struct btrfs_path
*path
,
1162 u64 bytenr
, u64 parent
,
1163 u64 root_objectid
, u64 owner
,
1164 u64 offset
, int refs_to_add
)
1166 struct btrfs_key key
;
1167 struct extent_buffer
*leaf
;
1172 key
.objectid
= bytenr
;
1174 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
1175 key
.offset
= parent
;
1176 size
= sizeof(struct btrfs_shared_data_ref
);
1178 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
1179 key
.offset
= hash_extent_data_ref(root_objectid
,
1181 size
= sizeof(struct btrfs_extent_data_ref
);
1184 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, size
);
1185 if (ret
&& ret
!= -EEXIST
)
1188 leaf
= path
->nodes
[0];
1190 struct btrfs_shared_data_ref
*ref
;
1191 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1192 struct btrfs_shared_data_ref
);
1194 btrfs_set_shared_data_ref_count(leaf
, ref
, refs_to_add
);
1196 num_refs
= btrfs_shared_data_ref_count(leaf
, ref
);
1197 num_refs
+= refs_to_add
;
1198 btrfs_set_shared_data_ref_count(leaf
, ref
, num_refs
);
1201 struct btrfs_extent_data_ref
*ref
;
1202 while (ret
== -EEXIST
) {
1203 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1204 struct btrfs_extent_data_ref
);
1205 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
1208 btrfs_release_path(path
);
1210 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1212 if (ret
&& ret
!= -EEXIST
)
1215 leaf
= path
->nodes
[0];
1217 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1218 struct btrfs_extent_data_ref
);
1220 btrfs_set_extent_data_ref_root(leaf
, ref
,
1222 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
1223 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
1224 btrfs_set_extent_data_ref_count(leaf
, ref
, refs_to_add
);
1226 num_refs
= btrfs_extent_data_ref_count(leaf
, ref
);
1227 num_refs
+= refs_to_add
;
1228 btrfs_set_extent_data_ref_count(leaf
, ref
, num_refs
);
1231 btrfs_mark_buffer_dirty(leaf
);
1234 btrfs_release_path(path
);
1238 static noinline
int remove_extent_data_ref(struct btrfs_trans_handle
*trans
,
1239 struct btrfs_root
*root
,
1240 struct btrfs_path
*path
,
1243 struct btrfs_key key
;
1244 struct btrfs_extent_data_ref
*ref1
= NULL
;
1245 struct btrfs_shared_data_ref
*ref2
= NULL
;
1246 struct extent_buffer
*leaf
;
1250 leaf
= path
->nodes
[0];
1251 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1253 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1254 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
1255 struct btrfs_extent_data_ref
);
1256 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1257 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
1258 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
1259 struct btrfs_shared_data_ref
);
1260 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1261 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1262 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
1263 struct btrfs_extent_ref_v0
*ref0
;
1264 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1265 struct btrfs_extent_ref_v0
);
1266 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
1272 BUG_ON(num_refs
< refs_to_drop
);
1273 num_refs
-= refs_to_drop
;
1275 if (num_refs
== 0) {
1276 ret
= btrfs_del_item(trans
, root
, path
);
1278 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
)
1279 btrfs_set_extent_data_ref_count(leaf
, ref1
, num_refs
);
1280 else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
)
1281 btrfs_set_shared_data_ref_count(leaf
, ref2
, num_refs
);
1282 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1284 struct btrfs_extent_ref_v0
*ref0
;
1285 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1286 struct btrfs_extent_ref_v0
);
1287 btrfs_set_ref_count_v0(leaf
, ref0
, num_refs
);
1290 btrfs_mark_buffer_dirty(leaf
);
1295 static noinline u32
extent_data_ref_count(struct btrfs_root
*root
,
1296 struct btrfs_path
*path
,
1297 struct btrfs_extent_inline_ref
*iref
)
1299 struct btrfs_key key
;
1300 struct extent_buffer
*leaf
;
1301 struct btrfs_extent_data_ref
*ref1
;
1302 struct btrfs_shared_data_ref
*ref2
;
1305 leaf
= path
->nodes
[0];
1306 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1308 if (btrfs_extent_inline_ref_type(leaf
, iref
) ==
1309 BTRFS_EXTENT_DATA_REF_KEY
) {
1310 ref1
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1311 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1313 ref2
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1314 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1316 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1317 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
1318 struct btrfs_extent_data_ref
);
1319 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1320 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
1321 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
1322 struct btrfs_shared_data_ref
);
1323 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1324 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1325 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
1326 struct btrfs_extent_ref_v0
*ref0
;
1327 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1328 struct btrfs_extent_ref_v0
);
1329 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
1337 static noinline
int lookup_tree_block_ref(struct btrfs_trans_handle
*trans
,
1338 struct btrfs_root
*root
,
1339 struct btrfs_path
*path
,
1340 u64 bytenr
, u64 parent
,
1343 struct btrfs_key key
;
1346 key
.objectid
= bytenr
;
1348 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1349 key
.offset
= parent
;
1351 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
1352 key
.offset
= root_objectid
;
1355 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1358 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1359 if (ret
== -ENOENT
&& parent
) {
1360 btrfs_release_path(path
);
1361 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
1362 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1370 static noinline
int insert_tree_block_ref(struct btrfs_trans_handle
*trans
,
1371 struct btrfs_root
*root
,
1372 struct btrfs_path
*path
,
1373 u64 bytenr
, u64 parent
,
1376 struct btrfs_key key
;
1379 key
.objectid
= bytenr
;
1381 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1382 key
.offset
= parent
;
1384 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
1385 key
.offset
= root_objectid
;
1388 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, 0);
1389 btrfs_release_path(path
);
1393 static inline int extent_ref_type(u64 parent
, u64 owner
)
1396 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1398 type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1400 type
= BTRFS_TREE_BLOCK_REF_KEY
;
1403 type
= BTRFS_SHARED_DATA_REF_KEY
;
1405 type
= BTRFS_EXTENT_DATA_REF_KEY
;
1410 static int find_next_key(struct btrfs_path
*path
, int level
,
1411 struct btrfs_key
*key
)
1414 for (; level
< BTRFS_MAX_LEVEL
; level
++) {
1415 if (!path
->nodes
[level
])
1417 if (path
->slots
[level
] + 1 >=
1418 btrfs_header_nritems(path
->nodes
[level
]))
1421 btrfs_item_key_to_cpu(path
->nodes
[level
], key
,
1422 path
->slots
[level
] + 1);
1424 btrfs_node_key_to_cpu(path
->nodes
[level
], key
,
1425 path
->slots
[level
] + 1);
1432 * look for inline back ref. if back ref is found, *ref_ret is set
1433 * to the address of inline back ref, and 0 is returned.
1435 * if back ref isn't found, *ref_ret is set to the address where it
1436 * should be inserted, and -ENOENT is returned.
1438 * if insert is true and there are too many inline back refs, the path
1439 * points to the extent item, and -EAGAIN is returned.
1441 * NOTE: inline back refs are ordered in the same way that back ref
1442 * items in the tree are ordered.
1444 static noinline_for_stack
1445 int lookup_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1446 struct btrfs_root
*root
,
1447 struct btrfs_path
*path
,
1448 struct btrfs_extent_inline_ref
**ref_ret
,
1449 u64 bytenr
, u64 num_bytes
,
1450 u64 parent
, u64 root_objectid
,
1451 u64 owner
, u64 offset
, int insert
)
1453 struct btrfs_key key
;
1454 struct extent_buffer
*leaf
;
1455 struct btrfs_extent_item
*ei
;
1456 struct btrfs_extent_inline_ref
*iref
;
1466 bool skinny_metadata
= btrfs_fs_incompat(root
->fs_info
,
1469 key
.objectid
= bytenr
;
1470 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1471 key
.offset
= num_bytes
;
1473 want
= extent_ref_type(parent
, owner
);
1475 extra_size
= btrfs_extent_inline_ref_size(want
);
1476 path
->keep_locks
= 1;
1481 * Owner is our parent level, so we can just add one to get the level
1482 * for the block we are interested in.
1484 if (skinny_metadata
&& owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1485 key
.type
= BTRFS_METADATA_ITEM_KEY
;
1490 ret
= btrfs_search_slot(trans
, root
, &key
, path
, extra_size
, 1);
1497 * We may be a newly converted file system which still has the old fat
1498 * extent entries for metadata, so try and see if we have one of those.
1500 if (ret
> 0 && skinny_metadata
) {
1501 skinny_metadata
= false;
1502 if (path
->slots
[0]) {
1504 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
1506 if (key
.objectid
== bytenr
&&
1507 key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
1508 key
.offset
== num_bytes
)
1512 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1513 key
.offset
= num_bytes
;
1514 btrfs_release_path(path
);
1519 if (ret
&& !insert
) {
1528 leaf
= path
->nodes
[0];
1529 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1530 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1531 if (item_size
< sizeof(*ei
)) {
1536 ret
= convert_extent_item_v0(trans
, root
, path
, owner
,
1542 leaf
= path
->nodes
[0];
1543 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1546 BUG_ON(item_size
< sizeof(*ei
));
1548 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1549 flags
= btrfs_extent_flags(leaf
, ei
);
1551 ptr
= (unsigned long)(ei
+ 1);
1552 end
= (unsigned long)ei
+ item_size
;
1554 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
&& !skinny_metadata
) {
1555 ptr
+= sizeof(struct btrfs_tree_block_info
);
1565 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1566 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1570 ptr
+= btrfs_extent_inline_ref_size(type
);
1574 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1575 struct btrfs_extent_data_ref
*dref
;
1576 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1577 if (match_extent_data_ref(leaf
, dref
, root_objectid
,
1582 if (hash_extent_data_ref_item(leaf
, dref
) <
1583 hash_extent_data_ref(root_objectid
, owner
, offset
))
1587 ref_offset
= btrfs_extent_inline_ref_offset(leaf
, iref
);
1589 if (parent
== ref_offset
) {
1593 if (ref_offset
< parent
)
1596 if (root_objectid
== ref_offset
) {
1600 if (ref_offset
< root_objectid
)
1604 ptr
+= btrfs_extent_inline_ref_size(type
);
1606 if (err
== -ENOENT
&& insert
) {
1607 if (item_size
+ extra_size
>=
1608 BTRFS_MAX_EXTENT_ITEM_SIZE(root
)) {
1613 * To add new inline back ref, we have to make sure
1614 * there is no corresponding back ref item.
1615 * For simplicity, we just do not add new inline back
1616 * ref if there is any kind of item for this block
1618 if (find_next_key(path
, 0, &key
) == 0 &&
1619 key
.objectid
== bytenr
&&
1620 key
.type
< BTRFS_BLOCK_GROUP_ITEM_KEY
) {
1625 *ref_ret
= (struct btrfs_extent_inline_ref
*)ptr
;
1628 path
->keep_locks
= 0;
1629 btrfs_unlock_up_safe(path
, 1);
1635 * helper to add new inline back ref
1637 static noinline_for_stack
1638 void setup_inline_extent_backref(struct btrfs_root
*root
,
1639 struct btrfs_path
*path
,
1640 struct btrfs_extent_inline_ref
*iref
,
1641 u64 parent
, u64 root_objectid
,
1642 u64 owner
, u64 offset
, int refs_to_add
,
1643 struct btrfs_delayed_extent_op
*extent_op
)
1645 struct extent_buffer
*leaf
;
1646 struct btrfs_extent_item
*ei
;
1649 unsigned long item_offset
;
1654 leaf
= path
->nodes
[0];
1655 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1656 item_offset
= (unsigned long)iref
- (unsigned long)ei
;
1658 type
= extent_ref_type(parent
, owner
);
1659 size
= btrfs_extent_inline_ref_size(type
);
1661 btrfs_extend_item(root
, path
, size
);
1663 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1664 refs
= btrfs_extent_refs(leaf
, ei
);
1665 refs
+= refs_to_add
;
1666 btrfs_set_extent_refs(leaf
, ei
, refs
);
1668 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1670 ptr
= (unsigned long)ei
+ item_offset
;
1671 end
= (unsigned long)ei
+ btrfs_item_size_nr(leaf
, path
->slots
[0]);
1672 if (ptr
< end
- size
)
1673 memmove_extent_buffer(leaf
, ptr
+ size
, ptr
,
1676 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1677 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
1678 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1679 struct btrfs_extent_data_ref
*dref
;
1680 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1681 btrfs_set_extent_data_ref_root(leaf
, dref
, root_objectid
);
1682 btrfs_set_extent_data_ref_objectid(leaf
, dref
, owner
);
1683 btrfs_set_extent_data_ref_offset(leaf
, dref
, offset
);
1684 btrfs_set_extent_data_ref_count(leaf
, dref
, refs_to_add
);
1685 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1686 struct btrfs_shared_data_ref
*sref
;
1687 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1688 btrfs_set_shared_data_ref_count(leaf
, sref
, refs_to_add
);
1689 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1690 } else if (type
== BTRFS_SHARED_BLOCK_REF_KEY
) {
1691 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1693 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
1695 btrfs_mark_buffer_dirty(leaf
);
1698 static int lookup_extent_backref(struct btrfs_trans_handle
*trans
,
1699 struct btrfs_root
*root
,
1700 struct btrfs_path
*path
,
1701 struct btrfs_extent_inline_ref
**ref_ret
,
1702 u64 bytenr
, u64 num_bytes
, u64 parent
,
1703 u64 root_objectid
, u64 owner
, u64 offset
)
1707 ret
= lookup_inline_extent_backref(trans
, root
, path
, ref_ret
,
1708 bytenr
, num_bytes
, parent
,
1709 root_objectid
, owner
, offset
, 0);
1713 btrfs_release_path(path
);
1716 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1717 ret
= lookup_tree_block_ref(trans
, root
, path
, bytenr
, parent
,
1720 ret
= lookup_extent_data_ref(trans
, root
, path
, bytenr
, parent
,
1721 root_objectid
, owner
, offset
);
1727 * helper to update/remove inline back ref
1729 static noinline_for_stack
1730 void update_inline_extent_backref(struct btrfs_root
*root
,
1731 struct btrfs_path
*path
,
1732 struct btrfs_extent_inline_ref
*iref
,
1734 struct btrfs_delayed_extent_op
*extent_op
)
1736 struct extent_buffer
*leaf
;
1737 struct btrfs_extent_item
*ei
;
1738 struct btrfs_extent_data_ref
*dref
= NULL
;
1739 struct btrfs_shared_data_ref
*sref
= NULL
;
1747 leaf
= path
->nodes
[0];
1748 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1749 refs
= btrfs_extent_refs(leaf
, ei
);
1750 WARN_ON(refs_to_mod
< 0 && refs
+ refs_to_mod
<= 0);
1751 refs
+= refs_to_mod
;
1752 btrfs_set_extent_refs(leaf
, ei
, refs
);
1754 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1756 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1758 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1759 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1760 refs
= btrfs_extent_data_ref_count(leaf
, dref
);
1761 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1762 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1763 refs
= btrfs_shared_data_ref_count(leaf
, sref
);
1766 BUG_ON(refs_to_mod
!= -1);
1769 BUG_ON(refs_to_mod
< 0 && refs
< -refs_to_mod
);
1770 refs
+= refs_to_mod
;
1773 if (type
== BTRFS_EXTENT_DATA_REF_KEY
)
1774 btrfs_set_extent_data_ref_count(leaf
, dref
, refs
);
1776 btrfs_set_shared_data_ref_count(leaf
, sref
, refs
);
1778 size
= btrfs_extent_inline_ref_size(type
);
1779 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1780 ptr
= (unsigned long)iref
;
1781 end
= (unsigned long)ei
+ item_size
;
1782 if (ptr
+ size
< end
)
1783 memmove_extent_buffer(leaf
, ptr
, ptr
+ size
,
1786 btrfs_truncate_item(root
, path
, item_size
, 1);
1788 btrfs_mark_buffer_dirty(leaf
);
1791 static noinline_for_stack
1792 int insert_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1793 struct btrfs_root
*root
,
1794 struct btrfs_path
*path
,
1795 u64 bytenr
, u64 num_bytes
, u64 parent
,
1796 u64 root_objectid
, u64 owner
,
1797 u64 offset
, int refs_to_add
,
1798 struct btrfs_delayed_extent_op
*extent_op
)
1800 struct btrfs_extent_inline_ref
*iref
;
1803 ret
= lookup_inline_extent_backref(trans
, root
, path
, &iref
,
1804 bytenr
, num_bytes
, parent
,
1805 root_objectid
, owner
, offset
, 1);
1807 BUG_ON(owner
< BTRFS_FIRST_FREE_OBJECTID
);
1808 update_inline_extent_backref(root
, path
, iref
,
1809 refs_to_add
, extent_op
);
1810 } else if (ret
== -ENOENT
) {
1811 setup_inline_extent_backref(root
, path
, iref
, parent
,
1812 root_objectid
, owner
, offset
,
1813 refs_to_add
, extent_op
);
1819 static int insert_extent_backref(struct btrfs_trans_handle
*trans
,
1820 struct btrfs_root
*root
,
1821 struct btrfs_path
*path
,
1822 u64 bytenr
, u64 parent
, u64 root_objectid
,
1823 u64 owner
, u64 offset
, int refs_to_add
)
1826 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1827 BUG_ON(refs_to_add
!= 1);
1828 ret
= insert_tree_block_ref(trans
, root
, path
, bytenr
,
1829 parent
, root_objectid
);
1831 ret
= insert_extent_data_ref(trans
, root
, path
, bytenr
,
1832 parent
, root_objectid
,
1833 owner
, offset
, refs_to_add
);
1838 static int remove_extent_backref(struct btrfs_trans_handle
*trans
,
1839 struct btrfs_root
*root
,
1840 struct btrfs_path
*path
,
1841 struct btrfs_extent_inline_ref
*iref
,
1842 int refs_to_drop
, int is_data
)
1846 BUG_ON(!is_data
&& refs_to_drop
!= 1);
1848 update_inline_extent_backref(root
, path
, iref
,
1849 -refs_to_drop
, NULL
);
1850 } else if (is_data
) {
1851 ret
= remove_extent_data_ref(trans
, root
, path
, refs_to_drop
);
1853 ret
= btrfs_del_item(trans
, root
, path
);
1858 static int btrfs_issue_discard(struct block_device
*bdev
,
1861 return blkdev_issue_discard(bdev
, start
>> 9, len
>> 9, GFP_NOFS
, 0);
1864 static int btrfs_discard_extent(struct btrfs_root
*root
, u64 bytenr
,
1865 u64 num_bytes
, u64
*actual_bytes
)
1868 u64 discarded_bytes
= 0;
1869 struct btrfs_bio
*bbio
= NULL
;
1872 /* Tell the block device(s) that the sectors can be discarded */
1873 ret
= btrfs_map_block(root
->fs_info
, REQ_DISCARD
,
1874 bytenr
, &num_bytes
, &bbio
, 0);
1875 /* Error condition is -ENOMEM */
1877 struct btrfs_bio_stripe
*stripe
= bbio
->stripes
;
1881 for (i
= 0; i
< bbio
->num_stripes
; i
++, stripe
++) {
1882 if (!stripe
->dev
->can_discard
)
1885 ret
= btrfs_issue_discard(stripe
->dev
->bdev
,
1889 discarded_bytes
+= stripe
->length
;
1890 else if (ret
!= -EOPNOTSUPP
)
1891 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
1894 * Just in case we get back EOPNOTSUPP for some reason,
1895 * just ignore the return value so we don't screw up
1896 * people calling discard_extent.
1904 *actual_bytes
= discarded_bytes
;
1907 if (ret
== -EOPNOTSUPP
)
1912 /* Can return -ENOMEM */
1913 int btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
1914 struct btrfs_root
*root
,
1915 u64 bytenr
, u64 num_bytes
, u64 parent
,
1916 u64 root_objectid
, u64 owner
, u64 offset
, int for_cow
)
1919 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1921 BUG_ON(owner
< BTRFS_FIRST_FREE_OBJECTID
&&
1922 root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
1924 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1925 ret
= btrfs_add_delayed_tree_ref(fs_info
, trans
, bytenr
,
1927 parent
, root_objectid
, (int)owner
,
1928 BTRFS_ADD_DELAYED_REF
, NULL
, for_cow
);
1930 ret
= btrfs_add_delayed_data_ref(fs_info
, trans
, bytenr
,
1932 parent
, root_objectid
, owner
, offset
,
1933 BTRFS_ADD_DELAYED_REF
, NULL
, for_cow
);
1938 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
1939 struct btrfs_root
*root
,
1940 u64 bytenr
, u64 num_bytes
,
1941 u64 parent
, u64 root_objectid
,
1942 u64 owner
, u64 offset
, int refs_to_add
,
1943 struct btrfs_delayed_extent_op
*extent_op
)
1945 struct btrfs_path
*path
;
1946 struct extent_buffer
*leaf
;
1947 struct btrfs_extent_item
*item
;
1952 path
= btrfs_alloc_path();
1957 path
->leave_spinning
= 1;
1958 /* this will setup the path even if it fails to insert the back ref */
1959 ret
= insert_inline_extent_backref(trans
, root
->fs_info
->extent_root
,
1960 path
, bytenr
, num_bytes
, parent
,
1961 root_objectid
, owner
, offset
,
1962 refs_to_add
, extent_op
);
1966 if (ret
!= -EAGAIN
) {
1971 leaf
= path
->nodes
[0];
1972 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1973 refs
= btrfs_extent_refs(leaf
, item
);
1974 btrfs_set_extent_refs(leaf
, item
, refs
+ refs_to_add
);
1976 __run_delayed_extent_op(extent_op
, leaf
, item
);
1978 btrfs_mark_buffer_dirty(leaf
);
1979 btrfs_release_path(path
);
1982 path
->leave_spinning
= 1;
1984 /* now insert the actual backref */
1985 ret
= insert_extent_backref(trans
, root
->fs_info
->extent_root
,
1986 path
, bytenr
, parent
, root_objectid
,
1987 owner
, offset
, refs_to_add
);
1989 btrfs_abort_transaction(trans
, root
, ret
);
1991 btrfs_free_path(path
);
1995 static int run_delayed_data_ref(struct btrfs_trans_handle
*trans
,
1996 struct btrfs_root
*root
,
1997 struct btrfs_delayed_ref_node
*node
,
1998 struct btrfs_delayed_extent_op
*extent_op
,
1999 int insert_reserved
)
2002 struct btrfs_delayed_data_ref
*ref
;
2003 struct btrfs_key ins
;
2008 ins
.objectid
= node
->bytenr
;
2009 ins
.offset
= node
->num_bytes
;
2010 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
2012 ref
= btrfs_delayed_node_to_data_ref(node
);
2013 if (node
->type
== BTRFS_SHARED_DATA_REF_KEY
)
2014 parent
= ref
->parent
;
2016 ref_root
= ref
->root
;
2018 if (node
->action
== BTRFS_ADD_DELAYED_REF
&& insert_reserved
) {
2020 flags
|= extent_op
->flags_to_set
;
2021 ret
= alloc_reserved_file_extent(trans
, root
,
2022 parent
, ref_root
, flags
,
2023 ref
->objectid
, ref
->offset
,
2024 &ins
, node
->ref_mod
);
2025 } else if (node
->action
== BTRFS_ADD_DELAYED_REF
) {
2026 ret
= __btrfs_inc_extent_ref(trans
, root
, node
->bytenr
,
2027 node
->num_bytes
, parent
,
2028 ref_root
, ref
->objectid
,
2029 ref
->offset
, node
->ref_mod
,
2031 } else if (node
->action
== BTRFS_DROP_DELAYED_REF
) {
2032 ret
= __btrfs_free_extent(trans
, root
, node
->bytenr
,
2033 node
->num_bytes
, parent
,
2034 ref_root
, ref
->objectid
,
2035 ref
->offset
, node
->ref_mod
,
2043 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op
*extent_op
,
2044 struct extent_buffer
*leaf
,
2045 struct btrfs_extent_item
*ei
)
2047 u64 flags
= btrfs_extent_flags(leaf
, ei
);
2048 if (extent_op
->update_flags
) {
2049 flags
|= extent_op
->flags_to_set
;
2050 btrfs_set_extent_flags(leaf
, ei
, flags
);
2053 if (extent_op
->update_key
) {
2054 struct btrfs_tree_block_info
*bi
;
2055 BUG_ON(!(flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
));
2056 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
2057 btrfs_set_tree_block_key(leaf
, bi
, &extent_op
->key
);
2061 static int run_delayed_extent_op(struct btrfs_trans_handle
*trans
,
2062 struct btrfs_root
*root
,
2063 struct btrfs_delayed_ref_node
*node
,
2064 struct btrfs_delayed_extent_op
*extent_op
)
2066 struct btrfs_key key
;
2067 struct btrfs_path
*path
;
2068 struct btrfs_extent_item
*ei
;
2069 struct extent_buffer
*leaf
;
2073 int metadata
= (node
->type
== BTRFS_TREE_BLOCK_REF_KEY
||
2074 node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
);
2079 if (metadata
&& !btrfs_fs_incompat(root
->fs_info
, SKINNY_METADATA
))
2082 path
= btrfs_alloc_path();
2086 key
.objectid
= node
->bytenr
;
2089 struct btrfs_delayed_tree_ref
*tree_ref
;
2091 tree_ref
= btrfs_delayed_node_to_tree_ref(node
);
2092 key
.type
= BTRFS_METADATA_ITEM_KEY
;
2093 key
.offset
= tree_ref
->level
;
2095 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2096 key
.offset
= node
->num_bytes
;
2101 path
->leave_spinning
= 1;
2102 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
,
2110 btrfs_release_path(path
);
2113 key
.offset
= node
->num_bytes
;
2114 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2121 leaf
= path
->nodes
[0];
2122 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2123 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2124 if (item_size
< sizeof(*ei
)) {
2125 ret
= convert_extent_item_v0(trans
, root
->fs_info
->extent_root
,
2131 leaf
= path
->nodes
[0];
2132 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2135 BUG_ON(item_size
< sizeof(*ei
));
2136 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
2137 __run_delayed_extent_op(extent_op
, leaf
, ei
);
2139 btrfs_mark_buffer_dirty(leaf
);
2141 btrfs_free_path(path
);
2145 static int run_delayed_tree_ref(struct btrfs_trans_handle
*trans
,
2146 struct btrfs_root
*root
,
2147 struct btrfs_delayed_ref_node
*node
,
2148 struct btrfs_delayed_extent_op
*extent_op
,
2149 int insert_reserved
)
2152 struct btrfs_delayed_tree_ref
*ref
;
2153 struct btrfs_key ins
;
2156 bool skinny_metadata
= btrfs_fs_incompat(root
->fs_info
,
2159 ref
= btrfs_delayed_node_to_tree_ref(node
);
2160 if (node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
)
2161 parent
= ref
->parent
;
2163 ref_root
= ref
->root
;
2165 ins
.objectid
= node
->bytenr
;
2166 if (skinny_metadata
) {
2167 ins
.offset
= ref
->level
;
2168 ins
.type
= BTRFS_METADATA_ITEM_KEY
;
2170 ins
.offset
= node
->num_bytes
;
2171 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
2174 BUG_ON(node
->ref_mod
!= 1);
2175 if (node
->action
== BTRFS_ADD_DELAYED_REF
&& insert_reserved
) {
2176 BUG_ON(!extent_op
|| !extent_op
->update_flags
);
2177 ret
= alloc_reserved_tree_block(trans
, root
,
2179 extent_op
->flags_to_set
,
2182 } else if (node
->action
== BTRFS_ADD_DELAYED_REF
) {
2183 ret
= __btrfs_inc_extent_ref(trans
, root
, node
->bytenr
,
2184 node
->num_bytes
, parent
, ref_root
,
2185 ref
->level
, 0, 1, extent_op
);
2186 } else if (node
->action
== BTRFS_DROP_DELAYED_REF
) {
2187 ret
= __btrfs_free_extent(trans
, root
, node
->bytenr
,
2188 node
->num_bytes
, parent
, ref_root
,
2189 ref
->level
, 0, 1, extent_op
);
2196 /* helper function to actually process a single delayed ref entry */
2197 static int run_one_delayed_ref(struct btrfs_trans_handle
*trans
,
2198 struct btrfs_root
*root
,
2199 struct btrfs_delayed_ref_node
*node
,
2200 struct btrfs_delayed_extent_op
*extent_op
,
2201 int insert_reserved
)
2208 if (btrfs_delayed_ref_is_head(node
)) {
2209 struct btrfs_delayed_ref_head
*head
;
2211 * we've hit the end of the chain and we were supposed
2212 * to insert this extent into the tree. But, it got
2213 * deleted before we ever needed to insert it, so all
2214 * we have to do is clean up the accounting
2217 head
= btrfs_delayed_node_to_head(node
);
2218 if (insert_reserved
) {
2219 btrfs_pin_extent(root
, node
->bytenr
,
2220 node
->num_bytes
, 1);
2221 if (head
->is_data
) {
2222 ret
= btrfs_del_csums(trans
, root
,
2230 if (node
->type
== BTRFS_TREE_BLOCK_REF_KEY
||
2231 node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
)
2232 ret
= run_delayed_tree_ref(trans
, root
, node
, extent_op
,
2234 else if (node
->type
== BTRFS_EXTENT_DATA_REF_KEY
||
2235 node
->type
== BTRFS_SHARED_DATA_REF_KEY
)
2236 ret
= run_delayed_data_ref(trans
, root
, node
, extent_op
,
2243 static noinline
struct btrfs_delayed_ref_node
*
2244 select_delayed_ref(struct btrfs_delayed_ref_head
*head
)
2246 struct rb_node
*node
;
2247 struct btrfs_delayed_ref_node
*ref
;
2248 int action
= BTRFS_ADD_DELAYED_REF
;
2251 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2252 * this prevents ref count from going down to zero when
2253 * there still are pending delayed ref.
2255 node
= rb_prev(&head
->node
.rb_node
);
2259 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
,
2261 if (ref
->bytenr
!= head
->node
.bytenr
)
2263 if (ref
->action
== action
)
2265 node
= rb_prev(node
);
2267 if (action
== BTRFS_ADD_DELAYED_REF
) {
2268 action
= BTRFS_DROP_DELAYED_REF
;
2275 * Returns 0 on success or if called with an already aborted transaction.
2276 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2278 static noinline
int run_clustered_refs(struct btrfs_trans_handle
*trans
,
2279 struct btrfs_root
*root
,
2280 struct list_head
*cluster
)
2282 struct btrfs_delayed_ref_root
*delayed_refs
;
2283 struct btrfs_delayed_ref_node
*ref
;
2284 struct btrfs_delayed_ref_head
*locked_ref
= NULL
;
2285 struct btrfs_delayed_extent_op
*extent_op
;
2286 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2289 int must_insert_reserved
= 0;
2291 delayed_refs
= &trans
->transaction
->delayed_refs
;
2294 /* pick a new head ref from the cluster list */
2295 if (list_empty(cluster
))
2298 locked_ref
= list_entry(cluster
->next
,
2299 struct btrfs_delayed_ref_head
, cluster
);
2301 /* grab the lock that says we are going to process
2302 * all the refs for this head */
2303 ret
= btrfs_delayed_ref_lock(trans
, locked_ref
);
2306 * we may have dropped the spin lock to get the head
2307 * mutex lock, and that might have given someone else
2308 * time to free the head. If that's true, it has been
2309 * removed from our list and we can move on.
2311 if (ret
== -EAGAIN
) {
2319 * We need to try and merge add/drops of the same ref since we
2320 * can run into issues with relocate dropping the implicit ref
2321 * and then it being added back again before the drop can
2322 * finish. If we merged anything we need to re-loop so we can
2325 btrfs_merge_delayed_refs(trans
, fs_info
, delayed_refs
,
2329 * locked_ref is the head node, so we have to go one
2330 * node back for any delayed ref updates
2332 ref
= select_delayed_ref(locked_ref
);
2334 if (ref
&& ref
->seq
&&
2335 btrfs_check_delayed_seq(fs_info
, delayed_refs
, ref
->seq
)) {
2337 * there are still refs with lower seq numbers in the
2338 * process of being added. Don't run this ref yet.
2340 list_del_init(&locked_ref
->cluster
);
2341 btrfs_delayed_ref_unlock(locked_ref
);
2343 delayed_refs
->num_heads_ready
++;
2344 spin_unlock(&delayed_refs
->lock
);
2346 spin_lock(&delayed_refs
->lock
);
2351 * record the must insert reserved flag before we
2352 * drop the spin lock.
2354 must_insert_reserved
= locked_ref
->must_insert_reserved
;
2355 locked_ref
->must_insert_reserved
= 0;
2357 extent_op
= locked_ref
->extent_op
;
2358 locked_ref
->extent_op
= NULL
;
2361 /* All delayed refs have been processed, Go ahead
2362 * and send the head node to run_one_delayed_ref,
2363 * so that any accounting fixes can happen
2365 ref
= &locked_ref
->node
;
2367 if (extent_op
&& must_insert_reserved
) {
2368 btrfs_free_delayed_extent_op(extent_op
);
2373 spin_unlock(&delayed_refs
->lock
);
2375 ret
= run_delayed_extent_op(trans
, root
,
2377 btrfs_free_delayed_extent_op(extent_op
);
2380 btrfs_debug(fs_info
, "run_delayed_extent_op returned %d", ret
);
2381 spin_lock(&delayed_refs
->lock
);
2382 btrfs_delayed_ref_unlock(locked_ref
);
2391 rb_erase(&ref
->rb_node
, &delayed_refs
->root
);
2392 delayed_refs
->num_entries
--;
2393 if (!btrfs_delayed_ref_is_head(ref
)) {
2395 * when we play the delayed ref, also correct the
2398 switch (ref
->action
) {
2399 case BTRFS_ADD_DELAYED_REF
:
2400 case BTRFS_ADD_DELAYED_EXTENT
:
2401 locked_ref
->node
.ref_mod
-= ref
->ref_mod
;
2403 case BTRFS_DROP_DELAYED_REF
:
2404 locked_ref
->node
.ref_mod
+= ref
->ref_mod
;
2410 spin_unlock(&delayed_refs
->lock
);
2412 ret
= run_one_delayed_ref(trans
, root
, ref
, extent_op
,
2413 must_insert_reserved
);
2415 btrfs_free_delayed_extent_op(extent_op
);
2417 btrfs_delayed_ref_unlock(locked_ref
);
2418 btrfs_put_delayed_ref(ref
);
2419 btrfs_debug(fs_info
, "run_one_delayed_ref returned %d", ret
);
2420 spin_lock(&delayed_refs
->lock
);
2425 * If this node is a head, that means all the refs in this head
2426 * have been dealt with, and we will pick the next head to deal
2427 * with, so we must unlock the head and drop it from the cluster
2428 * list before we release it.
2430 if (btrfs_delayed_ref_is_head(ref
)) {
2431 list_del_init(&locked_ref
->cluster
);
2432 btrfs_delayed_ref_unlock(locked_ref
);
2435 btrfs_put_delayed_ref(ref
);
2439 spin_lock(&delayed_refs
->lock
);
2444 #ifdef SCRAMBLE_DELAYED_REFS
2446 * Normally delayed refs get processed in ascending bytenr order. This
2447 * correlates in most cases to the order added. To expose dependencies on this
2448 * order, we start to process the tree in the middle instead of the beginning
2450 static u64
find_middle(struct rb_root
*root
)
2452 struct rb_node
*n
= root
->rb_node
;
2453 struct btrfs_delayed_ref_node
*entry
;
2456 u64 first
= 0, last
= 0;
2460 entry
= rb_entry(n
, struct btrfs_delayed_ref_node
, rb_node
);
2461 first
= entry
->bytenr
;
2465 entry
= rb_entry(n
, struct btrfs_delayed_ref_node
, rb_node
);
2466 last
= entry
->bytenr
;
2471 entry
= rb_entry(n
, struct btrfs_delayed_ref_node
, rb_node
);
2472 WARN_ON(!entry
->in_tree
);
2474 middle
= entry
->bytenr
;
2487 int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle
*trans
,
2488 struct btrfs_fs_info
*fs_info
)
2490 struct qgroup_update
*qgroup_update
;
2493 if (list_empty(&trans
->qgroup_ref_list
) !=
2494 !trans
->delayed_ref_elem
.seq
) {
2495 /* list without seq or seq without list */
2497 "qgroup accounting update error, list is%s empty, seq is %#x.%x",
2498 list_empty(&trans
->qgroup_ref_list
) ? "" : " not",
2499 (u32
)(trans
->delayed_ref_elem
.seq
>> 32),
2500 (u32
)trans
->delayed_ref_elem
.seq
);
2504 if (!trans
->delayed_ref_elem
.seq
)
2507 while (!list_empty(&trans
->qgroup_ref_list
)) {
2508 qgroup_update
= list_first_entry(&trans
->qgroup_ref_list
,
2509 struct qgroup_update
, list
);
2510 list_del(&qgroup_update
->list
);
2512 ret
= btrfs_qgroup_account_ref(
2513 trans
, fs_info
, qgroup_update
->node
,
2514 qgroup_update
->extent_op
);
2515 kfree(qgroup_update
);
2518 btrfs_put_tree_mod_seq(fs_info
, &trans
->delayed_ref_elem
);
2523 static int refs_newer(struct btrfs_delayed_ref_root
*delayed_refs
, int seq
,
2526 int val
= atomic_read(&delayed_refs
->ref_seq
);
2528 if (val
< seq
|| val
>= seq
+ count
)
2534 * this starts processing the delayed reference count updates and
2535 * extent insertions we have queued up so far. count can be
2536 * 0, which means to process everything in the tree at the start
2537 * of the run (but not newly added entries), or it can be some target
2538 * number you'd like to process.
2540 * Returns 0 on success or if called with an aborted transaction
2541 * Returns <0 on error and aborts the transaction
2543 int btrfs_run_delayed_refs(struct btrfs_trans_handle
*trans
,
2544 struct btrfs_root
*root
, unsigned long count
)
2546 struct rb_node
*node
;
2547 struct btrfs_delayed_ref_root
*delayed_refs
;
2548 struct btrfs_delayed_ref_node
*ref
;
2549 struct list_head cluster
;
2552 int run_all
= count
== (unsigned long)-1;
2556 /* We'll clean this up in btrfs_cleanup_transaction */
2560 if (root
== root
->fs_info
->extent_root
)
2561 root
= root
->fs_info
->tree_root
;
2563 btrfs_delayed_refs_qgroup_accounting(trans
, root
->fs_info
);
2565 delayed_refs
= &trans
->transaction
->delayed_refs
;
2566 INIT_LIST_HEAD(&cluster
);
2568 count
= delayed_refs
->num_entries
* 2;
2572 if (!run_all
&& !run_most
) {
2574 int seq
= atomic_read(&delayed_refs
->ref_seq
);
2577 old
= atomic_cmpxchg(&delayed_refs
->procs_running_refs
, 0, 1);
2579 DEFINE_WAIT(__wait
);
2580 if (delayed_refs
->num_entries
< 16348)
2583 prepare_to_wait(&delayed_refs
->wait
, &__wait
,
2584 TASK_UNINTERRUPTIBLE
);
2586 old
= atomic_cmpxchg(&delayed_refs
->procs_running_refs
, 0, 1);
2589 finish_wait(&delayed_refs
->wait
, &__wait
);
2591 if (!refs_newer(delayed_refs
, seq
, 256))
2596 finish_wait(&delayed_refs
->wait
, &__wait
);
2602 atomic_inc(&delayed_refs
->procs_running_refs
);
2607 spin_lock(&delayed_refs
->lock
);
2609 #ifdef SCRAMBLE_DELAYED_REFS
2610 delayed_refs
->run_delayed_start
= find_middle(&delayed_refs
->root
);
2614 if (!(run_all
|| run_most
) &&
2615 delayed_refs
->num_heads_ready
< 64)
2619 * go find something we can process in the rbtree. We start at
2620 * the beginning of the tree, and then build a cluster
2621 * of refs to process starting at the first one we are able to
2624 delayed_start
= delayed_refs
->run_delayed_start
;
2625 ret
= btrfs_find_ref_cluster(trans
, &cluster
,
2626 delayed_refs
->run_delayed_start
);
2630 ret
= run_clustered_refs(trans
, root
, &cluster
);
2632 btrfs_release_ref_cluster(&cluster
);
2633 spin_unlock(&delayed_refs
->lock
);
2634 btrfs_abort_transaction(trans
, root
, ret
);
2635 atomic_dec(&delayed_refs
->procs_running_refs
);
2639 atomic_add(ret
, &delayed_refs
->ref_seq
);
2641 count
-= min_t(unsigned long, ret
, count
);
2646 if (delayed_start
>= delayed_refs
->run_delayed_start
) {
2649 * btrfs_find_ref_cluster looped. let's do one
2650 * more cycle. if we don't run any delayed ref
2651 * during that cycle (because we can't because
2652 * all of them are blocked), bail out.
2657 * no runnable refs left, stop trying
2664 /* refs were run, let's reset staleness detection */
2670 if (!list_empty(&trans
->new_bgs
)) {
2671 spin_unlock(&delayed_refs
->lock
);
2672 btrfs_create_pending_block_groups(trans
, root
);
2673 spin_lock(&delayed_refs
->lock
);
2676 node
= rb_first(&delayed_refs
->root
);
2679 count
= (unsigned long)-1;
2682 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
,
2684 if (btrfs_delayed_ref_is_head(ref
)) {
2685 struct btrfs_delayed_ref_head
*head
;
2687 head
= btrfs_delayed_node_to_head(ref
);
2688 atomic_inc(&ref
->refs
);
2690 spin_unlock(&delayed_refs
->lock
);
2692 * Mutex was contended, block until it's
2693 * released and try again
2695 mutex_lock(&head
->mutex
);
2696 mutex_unlock(&head
->mutex
);
2698 btrfs_put_delayed_ref(ref
);
2702 node
= rb_next(node
);
2704 spin_unlock(&delayed_refs
->lock
);
2705 schedule_timeout(1);
2709 atomic_dec(&delayed_refs
->procs_running_refs
);
2711 if (waitqueue_active(&delayed_refs
->wait
))
2712 wake_up(&delayed_refs
->wait
);
2714 spin_unlock(&delayed_refs
->lock
);
2715 assert_qgroups_uptodate(trans
);
2719 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle
*trans
,
2720 struct btrfs_root
*root
,
2721 u64 bytenr
, u64 num_bytes
, u64 flags
,
2724 struct btrfs_delayed_extent_op
*extent_op
;
2727 extent_op
= btrfs_alloc_delayed_extent_op();
2731 extent_op
->flags_to_set
= flags
;
2732 extent_op
->update_flags
= 1;
2733 extent_op
->update_key
= 0;
2734 extent_op
->is_data
= is_data
? 1 : 0;
2736 ret
= btrfs_add_delayed_extent_op(root
->fs_info
, trans
, bytenr
,
2737 num_bytes
, extent_op
);
2739 btrfs_free_delayed_extent_op(extent_op
);
2743 static noinline
int check_delayed_ref(struct btrfs_trans_handle
*trans
,
2744 struct btrfs_root
*root
,
2745 struct btrfs_path
*path
,
2746 u64 objectid
, u64 offset
, u64 bytenr
)
2748 struct btrfs_delayed_ref_head
*head
;
2749 struct btrfs_delayed_ref_node
*ref
;
2750 struct btrfs_delayed_data_ref
*data_ref
;
2751 struct btrfs_delayed_ref_root
*delayed_refs
;
2752 struct rb_node
*node
;
2756 delayed_refs
= &trans
->transaction
->delayed_refs
;
2757 spin_lock(&delayed_refs
->lock
);
2758 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
2762 if (!mutex_trylock(&head
->mutex
)) {
2763 atomic_inc(&head
->node
.refs
);
2764 spin_unlock(&delayed_refs
->lock
);
2766 btrfs_release_path(path
);
2769 * Mutex was contended, block until it's released and let
2772 mutex_lock(&head
->mutex
);
2773 mutex_unlock(&head
->mutex
);
2774 btrfs_put_delayed_ref(&head
->node
);
2778 node
= rb_prev(&head
->node
.rb_node
);
2782 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
2784 if (ref
->bytenr
!= bytenr
)
2788 if (ref
->type
!= BTRFS_EXTENT_DATA_REF_KEY
)
2791 data_ref
= btrfs_delayed_node_to_data_ref(ref
);
2793 node
= rb_prev(node
);
2797 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
2798 if (ref
->bytenr
== bytenr
&& ref
->seq
== seq
)
2802 if (data_ref
->root
!= root
->root_key
.objectid
||
2803 data_ref
->objectid
!= objectid
|| data_ref
->offset
!= offset
)
2808 mutex_unlock(&head
->mutex
);
2810 spin_unlock(&delayed_refs
->lock
);
2814 static noinline
int check_committed_ref(struct btrfs_trans_handle
*trans
,
2815 struct btrfs_root
*root
,
2816 struct btrfs_path
*path
,
2817 u64 objectid
, u64 offset
, u64 bytenr
)
2819 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
2820 struct extent_buffer
*leaf
;
2821 struct btrfs_extent_data_ref
*ref
;
2822 struct btrfs_extent_inline_ref
*iref
;
2823 struct btrfs_extent_item
*ei
;
2824 struct btrfs_key key
;
2828 key
.objectid
= bytenr
;
2829 key
.offset
= (u64
)-1;
2830 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2832 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
2835 BUG_ON(ret
== 0); /* Corruption */
2838 if (path
->slots
[0] == 0)
2842 leaf
= path
->nodes
[0];
2843 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2845 if (key
.objectid
!= bytenr
|| key
.type
!= BTRFS_EXTENT_ITEM_KEY
)
2849 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2850 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2851 if (item_size
< sizeof(*ei
)) {
2852 WARN_ON(item_size
!= sizeof(struct btrfs_extent_item_v0
));
2856 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
2858 if (item_size
!= sizeof(*ei
) +
2859 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY
))
2862 if (btrfs_extent_generation(leaf
, ei
) <=
2863 btrfs_root_last_snapshot(&root
->root_item
))
2866 iref
= (struct btrfs_extent_inline_ref
*)(ei
+ 1);
2867 if (btrfs_extent_inline_ref_type(leaf
, iref
) !=
2868 BTRFS_EXTENT_DATA_REF_KEY
)
2871 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
2872 if (btrfs_extent_refs(leaf
, ei
) !=
2873 btrfs_extent_data_ref_count(leaf
, ref
) ||
2874 btrfs_extent_data_ref_root(leaf
, ref
) !=
2875 root
->root_key
.objectid
||
2876 btrfs_extent_data_ref_objectid(leaf
, ref
) != objectid
||
2877 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
2885 int btrfs_cross_ref_exist(struct btrfs_trans_handle
*trans
,
2886 struct btrfs_root
*root
,
2887 u64 objectid
, u64 offset
, u64 bytenr
)
2889 struct btrfs_path
*path
;
2893 path
= btrfs_alloc_path();
2898 ret
= check_committed_ref(trans
, root
, path
, objectid
,
2900 if (ret
&& ret
!= -ENOENT
)
2903 ret2
= check_delayed_ref(trans
, root
, path
, objectid
,
2905 } while (ret2
== -EAGAIN
);
2907 if (ret2
&& ret2
!= -ENOENT
) {
2912 if (ret
!= -ENOENT
|| ret2
!= -ENOENT
)
2915 btrfs_free_path(path
);
2916 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
2921 static int __btrfs_mod_ref(struct btrfs_trans_handle
*trans
,
2922 struct btrfs_root
*root
,
2923 struct extent_buffer
*buf
,
2924 int full_backref
, int inc
, int for_cow
)
2931 struct btrfs_key key
;
2932 struct btrfs_file_extent_item
*fi
;
2936 int (*process_func
)(struct btrfs_trans_handle
*, struct btrfs_root
*,
2937 u64
, u64
, u64
, u64
, u64
, u64
, int);
2939 ref_root
= btrfs_header_owner(buf
);
2940 nritems
= btrfs_header_nritems(buf
);
2941 level
= btrfs_header_level(buf
);
2943 if (!root
->ref_cows
&& level
== 0)
2947 process_func
= btrfs_inc_extent_ref
;
2949 process_func
= btrfs_free_extent
;
2952 parent
= buf
->start
;
2956 for (i
= 0; i
< nritems
; i
++) {
2958 btrfs_item_key_to_cpu(buf
, &key
, i
);
2959 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
2961 fi
= btrfs_item_ptr(buf
, i
,
2962 struct btrfs_file_extent_item
);
2963 if (btrfs_file_extent_type(buf
, fi
) ==
2964 BTRFS_FILE_EXTENT_INLINE
)
2966 bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
2970 num_bytes
= btrfs_file_extent_disk_num_bytes(buf
, fi
);
2971 key
.offset
-= btrfs_file_extent_offset(buf
, fi
);
2972 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
2973 parent
, ref_root
, key
.objectid
,
2974 key
.offset
, for_cow
);
2978 bytenr
= btrfs_node_blockptr(buf
, i
);
2979 num_bytes
= btrfs_level_size(root
, level
- 1);
2980 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
2981 parent
, ref_root
, level
- 1, 0,
2992 int btrfs_inc_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2993 struct extent_buffer
*buf
, int full_backref
, int for_cow
)
2995 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 1, for_cow
);
2998 int btrfs_dec_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2999 struct extent_buffer
*buf
, int full_backref
, int for_cow
)
3001 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 0, for_cow
);
3004 static int write_one_cache_group(struct btrfs_trans_handle
*trans
,
3005 struct btrfs_root
*root
,
3006 struct btrfs_path
*path
,
3007 struct btrfs_block_group_cache
*cache
)
3010 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
3012 struct extent_buffer
*leaf
;
3014 ret
= btrfs_search_slot(trans
, extent_root
, &cache
->key
, path
, 0, 1);
3017 BUG_ON(ret
); /* Corruption */
3019 leaf
= path
->nodes
[0];
3020 bi
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
3021 write_extent_buffer(leaf
, &cache
->item
, bi
, sizeof(cache
->item
));
3022 btrfs_mark_buffer_dirty(leaf
);
3023 btrfs_release_path(path
);
3026 btrfs_abort_transaction(trans
, root
, ret
);
3033 static struct btrfs_block_group_cache
*
3034 next_block_group(struct btrfs_root
*root
,
3035 struct btrfs_block_group_cache
*cache
)
3037 struct rb_node
*node
;
3038 spin_lock(&root
->fs_info
->block_group_cache_lock
);
3039 node
= rb_next(&cache
->cache_node
);
3040 btrfs_put_block_group(cache
);
3042 cache
= rb_entry(node
, struct btrfs_block_group_cache
,
3044 btrfs_get_block_group(cache
);
3047 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
3051 static int cache_save_setup(struct btrfs_block_group_cache
*block_group
,
3052 struct btrfs_trans_handle
*trans
,
3053 struct btrfs_path
*path
)
3055 struct btrfs_root
*root
= block_group
->fs_info
->tree_root
;
3056 struct inode
*inode
= NULL
;
3058 int dcs
= BTRFS_DC_ERROR
;
3064 * If this block group is smaller than 100 megs don't bother caching the
3067 if (block_group
->key
.offset
< (100 * 1024 * 1024)) {
3068 spin_lock(&block_group
->lock
);
3069 block_group
->disk_cache_state
= BTRFS_DC_WRITTEN
;
3070 spin_unlock(&block_group
->lock
);
3075 inode
= lookup_free_space_inode(root
, block_group
, path
);
3076 if (IS_ERR(inode
) && PTR_ERR(inode
) != -ENOENT
) {
3077 ret
= PTR_ERR(inode
);
3078 btrfs_release_path(path
);
3082 if (IS_ERR(inode
)) {
3086 if (block_group
->ro
)
3089 ret
= create_free_space_inode(root
, trans
, block_group
, path
);
3095 /* We've already setup this transaction, go ahead and exit */
3096 if (block_group
->cache_generation
== trans
->transid
&&
3097 i_size_read(inode
)) {
3098 dcs
= BTRFS_DC_SETUP
;
3103 * We want to set the generation to 0, that way if anything goes wrong
3104 * from here on out we know not to trust this cache when we load up next
3107 BTRFS_I(inode
)->generation
= 0;
3108 ret
= btrfs_update_inode(trans
, root
, inode
);
3111 if (i_size_read(inode
) > 0) {
3112 ret
= btrfs_truncate_free_space_cache(root
, trans
, path
,
3118 spin_lock(&block_group
->lock
);
3119 if (block_group
->cached
!= BTRFS_CACHE_FINISHED
||
3120 !btrfs_test_opt(root
, SPACE_CACHE
)) {
3122 * don't bother trying to write stuff out _if_
3123 * a) we're not cached,
3124 * b) we're with nospace_cache mount option.
3126 dcs
= BTRFS_DC_WRITTEN
;
3127 spin_unlock(&block_group
->lock
);
3130 spin_unlock(&block_group
->lock
);
3133 * Try to preallocate enough space based on how big the block group is.
3134 * Keep in mind this has to include any pinned space which could end up
3135 * taking up quite a bit since it's not folded into the other space
3138 num_pages
= (int)div64_u64(block_group
->key
.offset
, 256 * 1024 * 1024);
3143 num_pages
*= PAGE_CACHE_SIZE
;
3145 ret
= btrfs_check_data_free_space(inode
, num_pages
);
3149 ret
= btrfs_prealloc_file_range_trans(inode
, trans
, 0, 0, num_pages
,
3150 num_pages
, num_pages
,
3153 dcs
= BTRFS_DC_SETUP
;
3154 btrfs_free_reserved_data_space(inode
, num_pages
);
3159 btrfs_release_path(path
);
3161 spin_lock(&block_group
->lock
);
3162 if (!ret
&& dcs
== BTRFS_DC_SETUP
)
3163 block_group
->cache_generation
= trans
->transid
;
3164 block_group
->disk_cache_state
= dcs
;
3165 spin_unlock(&block_group
->lock
);
3170 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle
*trans
,
3171 struct btrfs_root
*root
)
3173 struct btrfs_block_group_cache
*cache
;
3175 struct btrfs_path
*path
;
3178 path
= btrfs_alloc_path();
3184 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
3186 if (cache
->disk_cache_state
== BTRFS_DC_CLEAR
)
3188 cache
= next_block_group(root
, cache
);
3196 err
= cache_save_setup(cache
, trans
, path
);
3197 last
= cache
->key
.objectid
+ cache
->key
.offset
;
3198 btrfs_put_block_group(cache
);
3203 err
= btrfs_run_delayed_refs(trans
, root
,
3205 if (err
) /* File system offline */
3209 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
3211 if (cache
->disk_cache_state
== BTRFS_DC_CLEAR
) {
3212 btrfs_put_block_group(cache
);
3218 cache
= next_block_group(root
, cache
);
3227 if (cache
->disk_cache_state
== BTRFS_DC_SETUP
)
3228 cache
->disk_cache_state
= BTRFS_DC_NEED_WRITE
;
3230 last
= cache
->key
.objectid
+ cache
->key
.offset
;
3232 err
= write_one_cache_group(trans
, root
, path
, cache
);
3233 if (err
) /* File system offline */
3236 btrfs_put_block_group(cache
);
3241 * I don't think this is needed since we're just marking our
3242 * preallocated extent as written, but just in case it can't
3246 err
= btrfs_run_delayed_refs(trans
, root
,
3248 if (err
) /* File system offline */
3252 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
3255 * Really this shouldn't happen, but it could if we
3256 * couldn't write the entire preallocated extent and
3257 * splitting the extent resulted in a new block.
3260 btrfs_put_block_group(cache
);
3263 if (cache
->disk_cache_state
== BTRFS_DC_NEED_WRITE
)
3265 cache
= next_block_group(root
, cache
);
3274 err
= btrfs_write_out_cache(root
, trans
, cache
, path
);
3277 * If we didn't have an error then the cache state is still
3278 * NEED_WRITE, so we can set it to WRITTEN.
3280 if (!err
&& cache
->disk_cache_state
== BTRFS_DC_NEED_WRITE
)
3281 cache
->disk_cache_state
= BTRFS_DC_WRITTEN
;
3282 last
= cache
->key
.objectid
+ cache
->key
.offset
;
3283 btrfs_put_block_group(cache
);
3287 btrfs_free_path(path
);
3291 int btrfs_extent_readonly(struct btrfs_root
*root
, u64 bytenr
)
3293 struct btrfs_block_group_cache
*block_group
;
3296 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
3297 if (!block_group
|| block_group
->ro
)
3300 btrfs_put_block_group(block_group
);
3304 static int update_space_info(struct btrfs_fs_info
*info
, u64 flags
,
3305 u64 total_bytes
, u64 bytes_used
,
3306 struct btrfs_space_info
**space_info
)
3308 struct btrfs_space_info
*found
;
3312 if (flags
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
3313 BTRFS_BLOCK_GROUP_RAID10
))
3318 found
= __find_space_info(info
, flags
);
3320 spin_lock(&found
->lock
);
3321 found
->total_bytes
+= total_bytes
;
3322 found
->disk_total
+= total_bytes
* factor
;
3323 found
->bytes_used
+= bytes_used
;
3324 found
->disk_used
+= bytes_used
* factor
;
3326 spin_unlock(&found
->lock
);
3327 *space_info
= found
;
3330 found
= kzalloc(sizeof(*found
), GFP_NOFS
);
3334 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++)
3335 INIT_LIST_HEAD(&found
->block_groups
[i
]);
3336 init_rwsem(&found
->groups_sem
);
3337 spin_lock_init(&found
->lock
);
3338 found
->flags
= flags
& BTRFS_BLOCK_GROUP_TYPE_MASK
;
3339 found
->total_bytes
= total_bytes
;
3340 found
->disk_total
= total_bytes
* factor
;
3341 found
->bytes_used
= bytes_used
;
3342 found
->disk_used
= bytes_used
* factor
;
3343 found
->bytes_pinned
= 0;
3344 found
->bytes_reserved
= 0;
3345 found
->bytes_readonly
= 0;
3346 found
->bytes_may_use
= 0;
3348 found
->force_alloc
= CHUNK_ALLOC_NO_FORCE
;
3349 found
->chunk_alloc
= 0;
3351 init_waitqueue_head(&found
->wait
);
3352 *space_info
= found
;
3353 list_add_rcu(&found
->list
, &info
->space_info
);
3354 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3355 info
->data_sinfo
= found
;
3359 static void set_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
3361 u64 extra_flags
= chunk_to_extended(flags
) &
3362 BTRFS_EXTENDED_PROFILE_MASK
;
3364 write_seqlock(&fs_info
->profiles_lock
);
3365 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3366 fs_info
->avail_data_alloc_bits
|= extra_flags
;
3367 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
3368 fs_info
->avail_metadata_alloc_bits
|= extra_flags
;
3369 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
3370 fs_info
->avail_system_alloc_bits
|= extra_flags
;
3371 write_sequnlock(&fs_info
->profiles_lock
);
3375 * returns target flags in extended format or 0 if restripe for this
3376 * chunk_type is not in progress
3378 * should be called with either volume_mutex or balance_lock held
3380 static u64
get_restripe_target(struct btrfs_fs_info
*fs_info
, u64 flags
)
3382 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3388 if (flags
& BTRFS_BLOCK_GROUP_DATA
&&
3389 bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3390 target
= BTRFS_BLOCK_GROUP_DATA
| bctl
->data
.target
;
3391 } else if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
&&
3392 bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3393 target
= BTRFS_BLOCK_GROUP_SYSTEM
| bctl
->sys
.target
;
3394 } else if (flags
& BTRFS_BLOCK_GROUP_METADATA
&&
3395 bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3396 target
= BTRFS_BLOCK_GROUP_METADATA
| bctl
->meta
.target
;
3403 * @flags: available profiles in extended format (see ctree.h)
3405 * Returns reduced profile in chunk format. If profile changing is in
3406 * progress (either running or paused) picks the target profile (if it's
3407 * already available), otherwise falls back to plain reducing.
3409 static u64
btrfs_reduce_alloc_profile(struct btrfs_root
*root
, u64 flags
)
3412 * we add in the count of missing devices because we want
3413 * to make sure that any RAID levels on a degraded FS
3414 * continue to be honored.
3416 u64 num_devices
= root
->fs_info
->fs_devices
->rw_devices
+
3417 root
->fs_info
->fs_devices
->missing_devices
;
3422 * see if restripe for this chunk_type is in progress, if so
3423 * try to reduce to the target profile
3425 spin_lock(&root
->fs_info
->balance_lock
);
3426 target
= get_restripe_target(root
->fs_info
, flags
);
3428 /* pick target profile only if it's already available */
3429 if ((flags
& target
) & BTRFS_EXTENDED_PROFILE_MASK
) {
3430 spin_unlock(&root
->fs_info
->balance_lock
);
3431 return extended_to_chunk(target
);
3434 spin_unlock(&root
->fs_info
->balance_lock
);
3436 /* First, mask out the RAID levels which aren't possible */
3437 if (num_devices
== 1)
3438 flags
&= ~(BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID0
|
3439 BTRFS_BLOCK_GROUP_RAID5
);
3440 if (num_devices
< 3)
3441 flags
&= ~BTRFS_BLOCK_GROUP_RAID6
;
3442 if (num_devices
< 4)
3443 flags
&= ~BTRFS_BLOCK_GROUP_RAID10
;
3445 tmp
= flags
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID0
|
3446 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID5
|
3447 BTRFS_BLOCK_GROUP_RAID6
| BTRFS_BLOCK_GROUP_RAID10
);
3450 if (tmp
& BTRFS_BLOCK_GROUP_RAID6
)
3451 tmp
= BTRFS_BLOCK_GROUP_RAID6
;
3452 else if (tmp
& BTRFS_BLOCK_GROUP_RAID5
)
3453 tmp
= BTRFS_BLOCK_GROUP_RAID5
;
3454 else if (tmp
& BTRFS_BLOCK_GROUP_RAID10
)
3455 tmp
= BTRFS_BLOCK_GROUP_RAID10
;
3456 else if (tmp
& BTRFS_BLOCK_GROUP_RAID1
)
3457 tmp
= BTRFS_BLOCK_GROUP_RAID1
;
3458 else if (tmp
& BTRFS_BLOCK_GROUP_RAID0
)
3459 tmp
= BTRFS_BLOCK_GROUP_RAID0
;
3461 return extended_to_chunk(flags
| tmp
);
3464 static u64
get_alloc_profile(struct btrfs_root
*root
, u64 flags
)
3469 seq
= read_seqbegin(&root
->fs_info
->profiles_lock
);
3471 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3472 flags
|= root
->fs_info
->avail_data_alloc_bits
;
3473 else if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
3474 flags
|= root
->fs_info
->avail_system_alloc_bits
;
3475 else if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
3476 flags
|= root
->fs_info
->avail_metadata_alloc_bits
;
3477 } while (read_seqretry(&root
->fs_info
->profiles_lock
, seq
));
3479 return btrfs_reduce_alloc_profile(root
, flags
);
3482 u64
btrfs_get_alloc_profile(struct btrfs_root
*root
, int data
)
3488 flags
= BTRFS_BLOCK_GROUP_DATA
;
3489 else if (root
== root
->fs_info
->chunk_root
)
3490 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
3492 flags
= BTRFS_BLOCK_GROUP_METADATA
;
3494 ret
= get_alloc_profile(root
, flags
);
3499 * This will check the space that the inode allocates from to make sure we have
3500 * enough space for bytes.
3502 int btrfs_check_data_free_space(struct inode
*inode
, u64 bytes
)
3504 struct btrfs_space_info
*data_sinfo
;
3505 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3506 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3508 int ret
= 0, committed
= 0, alloc_chunk
= 1;
3510 /* make sure bytes are sectorsize aligned */
3511 bytes
= ALIGN(bytes
, root
->sectorsize
);
3513 if (root
== root
->fs_info
->tree_root
||
3514 BTRFS_I(inode
)->location
.objectid
== BTRFS_FREE_INO_OBJECTID
) {
3519 data_sinfo
= fs_info
->data_sinfo
;
3524 /* make sure we have enough space to handle the data first */
3525 spin_lock(&data_sinfo
->lock
);
3526 used
= data_sinfo
->bytes_used
+ data_sinfo
->bytes_reserved
+
3527 data_sinfo
->bytes_pinned
+ data_sinfo
->bytes_readonly
+
3528 data_sinfo
->bytes_may_use
;
3530 if (used
+ bytes
> data_sinfo
->total_bytes
) {
3531 struct btrfs_trans_handle
*trans
;
3534 * if we don't have enough free bytes in this space then we need
3535 * to alloc a new chunk.
3537 if (!data_sinfo
->full
&& alloc_chunk
) {
3540 data_sinfo
->force_alloc
= CHUNK_ALLOC_FORCE
;
3541 spin_unlock(&data_sinfo
->lock
);
3543 alloc_target
= btrfs_get_alloc_profile(root
, 1);
3544 trans
= btrfs_join_transaction(root
);
3546 return PTR_ERR(trans
);
3548 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
3550 CHUNK_ALLOC_NO_FORCE
);
3551 btrfs_end_transaction(trans
, root
);
3560 data_sinfo
= fs_info
->data_sinfo
;
3566 * If we have less pinned bytes than we want to allocate then
3567 * don't bother committing the transaction, it won't help us.
3569 if (data_sinfo
->bytes_pinned
< bytes
)
3571 spin_unlock(&data_sinfo
->lock
);
3573 /* commit the current transaction and try again */
3576 !atomic_read(&root
->fs_info
->open_ioctl_trans
)) {
3578 trans
= btrfs_join_transaction(root
);
3580 return PTR_ERR(trans
);
3581 ret
= btrfs_commit_transaction(trans
, root
);
3589 data_sinfo
->bytes_may_use
+= bytes
;
3590 trace_btrfs_space_reservation(root
->fs_info
, "space_info",
3591 data_sinfo
->flags
, bytes
, 1);
3592 spin_unlock(&data_sinfo
->lock
);
3598 * Called if we need to clear a data reservation for this inode.
3600 void btrfs_free_reserved_data_space(struct inode
*inode
, u64 bytes
)
3602 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3603 struct btrfs_space_info
*data_sinfo
;
3605 /* make sure bytes are sectorsize aligned */
3606 bytes
= ALIGN(bytes
, root
->sectorsize
);
3608 data_sinfo
= root
->fs_info
->data_sinfo
;
3609 spin_lock(&data_sinfo
->lock
);
3610 data_sinfo
->bytes_may_use
-= bytes
;
3611 trace_btrfs_space_reservation(root
->fs_info
, "space_info",
3612 data_sinfo
->flags
, bytes
, 0);
3613 spin_unlock(&data_sinfo
->lock
);
3616 static void force_metadata_allocation(struct btrfs_fs_info
*info
)
3618 struct list_head
*head
= &info
->space_info
;
3619 struct btrfs_space_info
*found
;
3622 list_for_each_entry_rcu(found
, head
, list
) {
3623 if (found
->flags
& BTRFS_BLOCK_GROUP_METADATA
)
3624 found
->force_alloc
= CHUNK_ALLOC_FORCE
;
3629 static inline u64
calc_global_rsv_need_space(struct btrfs_block_rsv
*global
)
3631 return (global
->size
<< 1);
3634 static int should_alloc_chunk(struct btrfs_root
*root
,
3635 struct btrfs_space_info
*sinfo
, int force
)
3637 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
3638 u64 num_bytes
= sinfo
->total_bytes
- sinfo
->bytes_readonly
;
3639 u64 num_allocated
= sinfo
->bytes_used
+ sinfo
->bytes_reserved
;
3642 if (force
== CHUNK_ALLOC_FORCE
)
3646 * We need to take into account the global rsv because for all intents
3647 * and purposes it's used space. Don't worry about locking the
3648 * global_rsv, it doesn't change except when the transaction commits.
3650 if (sinfo
->flags
& BTRFS_BLOCK_GROUP_METADATA
)
3651 num_allocated
+= calc_global_rsv_need_space(global_rsv
);
3654 * in limited mode, we want to have some free space up to
3655 * about 1% of the FS size.
3657 if (force
== CHUNK_ALLOC_LIMITED
) {
3658 thresh
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
3659 thresh
= max_t(u64
, 64 * 1024 * 1024,
3660 div_factor_fine(thresh
, 1));
3662 if (num_bytes
- num_allocated
< thresh
)
3666 if (num_allocated
+ 2 * 1024 * 1024 < div_factor(num_bytes
, 8))
3671 static u64
get_system_chunk_thresh(struct btrfs_root
*root
, u64 type
)
3675 if (type
& (BTRFS_BLOCK_GROUP_RAID10
|
3676 BTRFS_BLOCK_GROUP_RAID0
|
3677 BTRFS_BLOCK_GROUP_RAID5
|
3678 BTRFS_BLOCK_GROUP_RAID6
))
3679 num_dev
= root
->fs_info
->fs_devices
->rw_devices
;
3680 else if (type
& BTRFS_BLOCK_GROUP_RAID1
)
3683 num_dev
= 1; /* DUP or single */
3685 /* metadata for updaing devices and chunk tree */
3686 return btrfs_calc_trans_metadata_size(root
, num_dev
+ 1);
3689 static void check_system_chunk(struct btrfs_trans_handle
*trans
,
3690 struct btrfs_root
*root
, u64 type
)
3692 struct btrfs_space_info
*info
;
3696 info
= __find_space_info(root
->fs_info
, BTRFS_BLOCK_GROUP_SYSTEM
);
3697 spin_lock(&info
->lock
);
3698 left
= info
->total_bytes
- info
->bytes_used
- info
->bytes_pinned
-
3699 info
->bytes_reserved
- info
->bytes_readonly
;
3700 spin_unlock(&info
->lock
);
3702 thresh
= get_system_chunk_thresh(root
, type
);
3703 if (left
< thresh
&& btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
3704 btrfs_info(root
->fs_info
, "left=%llu, need=%llu, flags=%llu",
3705 left
, thresh
, type
);
3706 dump_space_info(info
, 0, 0);
3709 if (left
< thresh
) {
3712 flags
= btrfs_get_alloc_profile(root
->fs_info
->chunk_root
, 0);
3713 btrfs_alloc_chunk(trans
, root
, flags
);
3717 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
3718 struct btrfs_root
*extent_root
, u64 flags
, int force
)
3720 struct btrfs_space_info
*space_info
;
3721 struct btrfs_fs_info
*fs_info
= extent_root
->fs_info
;
3722 int wait_for_alloc
= 0;
3725 /* Don't re-enter if we're already allocating a chunk */
3726 if (trans
->allocating_chunk
)
3729 space_info
= __find_space_info(extent_root
->fs_info
, flags
);
3731 ret
= update_space_info(extent_root
->fs_info
, flags
,
3733 BUG_ON(ret
); /* -ENOMEM */
3735 BUG_ON(!space_info
); /* Logic error */
3738 spin_lock(&space_info
->lock
);
3739 if (force
< space_info
->force_alloc
)
3740 force
= space_info
->force_alloc
;
3741 if (space_info
->full
) {
3742 spin_unlock(&space_info
->lock
);
3746 if (!should_alloc_chunk(extent_root
, space_info
, force
)) {
3747 spin_unlock(&space_info
->lock
);
3749 } else if (space_info
->chunk_alloc
) {
3752 space_info
->chunk_alloc
= 1;
3755 spin_unlock(&space_info
->lock
);
3757 mutex_lock(&fs_info
->chunk_mutex
);
3760 * The chunk_mutex is held throughout the entirety of a chunk
3761 * allocation, so once we've acquired the chunk_mutex we know that the
3762 * other guy is done and we need to recheck and see if we should
3765 if (wait_for_alloc
) {
3766 mutex_unlock(&fs_info
->chunk_mutex
);
3771 trans
->allocating_chunk
= true;
3774 * If we have mixed data/metadata chunks we want to make sure we keep
3775 * allocating mixed chunks instead of individual chunks.
3777 if (btrfs_mixed_space_info(space_info
))
3778 flags
|= (BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
);
3781 * if we're doing a data chunk, go ahead and make sure that
3782 * we keep a reasonable number of metadata chunks allocated in the
3785 if (flags
& BTRFS_BLOCK_GROUP_DATA
&& fs_info
->metadata_ratio
) {
3786 fs_info
->data_chunk_allocations
++;
3787 if (!(fs_info
->data_chunk_allocations
%
3788 fs_info
->metadata_ratio
))
3789 force_metadata_allocation(fs_info
);
3793 * Check if we have enough space in SYSTEM chunk because we may need
3794 * to update devices.
3796 check_system_chunk(trans
, extent_root
, flags
);
3798 ret
= btrfs_alloc_chunk(trans
, extent_root
, flags
);
3799 trans
->allocating_chunk
= false;
3801 spin_lock(&space_info
->lock
);
3802 if (ret
< 0 && ret
!= -ENOSPC
)
3805 space_info
->full
= 1;
3809 space_info
->force_alloc
= CHUNK_ALLOC_NO_FORCE
;
3811 space_info
->chunk_alloc
= 0;
3812 spin_unlock(&space_info
->lock
);
3813 mutex_unlock(&fs_info
->chunk_mutex
);
3817 static int can_overcommit(struct btrfs_root
*root
,
3818 struct btrfs_space_info
*space_info
, u64 bytes
,
3819 enum btrfs_reserve_flush_enum flush
)
3821 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
3822 u64 profile
= btrfs_get_alloc_profile(root
, 0);
3828 used
= space_info
->bytes_used
+ space_info
->bytes_reserved
+
3829 space_info
->bytes_pinned
+ space_info
->bytes_readonly
;
3832 * We only want to allow over committing if we have lots of actual space
3833 * free, but if we don't have enough space to handle the global reserve
3834 * space then we could end up having a real enospc problem when trying
3835 * to allocate a chunk or some other such important allocation.
3837 spin_lock(&global_rsv
->lock
);
3838 space_size
= calc_global_rsv_need_space(global_rsv
);
3839 spin_unlock(&global_rsv
->lock
);
3840 if (used
+ space_size
>= space_info
->total_bytes
)
3843 used
+= space_info
->bytes_may_use
;
3845 spin_lock(&root
->fs_info
->free_chunk_lock
);
3846 avail
= root
->fs_info
->free_chunk_space
;
3847 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3850 * If we have dup, raid1 or raid10 then only half of the free
3851 * space is actually useable. For raid56, the space info used
3852 * doesn't include the parity drive, so we don't have to
3855 if (profile
& (BTRFS_BLOCK_GROUP_DUP
|
3856 BTRFS_BLOCK_GROUP_RAID1
|
3857 BTRFS_BLOCK_GROUP_RAID10
))
3860 to_add
= space_info
->total_bytes
;
3863 * If we aren't flushing all things, let us overcommit up to
3864 * 1/2th of the space. If we can flush, don't let us overcommit
3865 * too much, let it overcommit up to 1/8 of the space.
3867 if (flush
== BTRFS_RESERVE_FLUSH_ALL
)
3873 * Limit the overcommit to the amount of free space we could possibly
3874 * allocate for chunks.
3876 to_add
= min(avail
, to_add
);
3878 if (used
+ bytes
< space_info
->total_bytes
+ to_add
)
3883 static void btrfs_writeback_inodes_sb_nr(struct btrfs_root
*root
,
3884 unsigned long nr_pages
)
3886 struct super_block
*sb
= root
->fs_info
->sb
;
3889 /* If we can not start writeback, just sync all the delalloc file. */
3890 started
= try_to_writeback_inodes_sb_nr(sb
, nr_pages
,
3891 WB_REASON_FS_FREE_SPACE
);
3894 * We needn't worry the filesystem going from r/w to r/o though
3895 * we don't acquire ->s_umount mutex, because the filesystem
3896 * should guarantee the delalloc inodes list be empty after
3897 * the filesystem is readonly(all dirty pages are written to
3900 btrfs_start_delalloc_inodes(root
, 0);
3901 if (!current
->journal_info
)
3902 btrfs_wait_ordered_extents(root
, 0);
3907 * shrink metadata reservation for delalloc
3909 static void shrink_delalloc(struct btrfs_root
*root
, u64 to_reclaim
, u64 orig
,
3912 struct btrfs_block_rsv
*block_rsv
;
3913 struct btrfs_space_info
*space_info
;
3914 struct btrfs_trans_handle
*trans
;
3918 unsigned long nr_pages
= (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT
;
3920 enum btrfs_reserve_flush_enum flush
;
3922 trans
= (struct btrfs_trans_handle
*)current
->journal_info
;
3923 block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
3924 space_info
= block_rsv
->space_info
;
3927 delalloc_bytes
= percpu_counter_sum_positive(
3928 &root
->fs_info
->delalloc_bytes
);
3929 if (delalloc_bytes
== 0) {
3932 btrfs_wait_ordered_extents(root
, 0);
3936 while (delalloc_bytes
&& loops
< 3) {
3937 max_reclaim
= min(delalloc_bytes
, to_reclaim
);
3938 nr_pages
= max_reclaim
>> PAGE_CACHE_SHIFT
;
3939 btrfs_writeback_inodes_sb_nr(root
, nr_pages
);
3941 * We need to wait for the async pages to actually start before
3944 wait_event(root
->fs_info
->async_submit_wait
,
3945 !atomic_read(&root
->fs_info
->async_delalloc_pages
));
3948 flush
= BTRFS_RESERVE_FLUSH_ALL
;
3950 flush
= BTRFS_RESERVE_NO_FLUSH
;
3951 spin_lock(&space_info
->lock
);
3952 if (can_overcommit(root
, space_info
, orig
, flush
)) {
3953 spin_unlock(&space_info
->lock
);
3956 spin_unlock(&space_info
->lock
);
3959 if (wait_ordered
&& !trans
) {
3960 btrfs_wait_ordered_extents(root
, 0);
3962 time_left
= schedule_timeout_killable(1);
3967 delalloc_bytes
= percpu_counter_sum_positive(
3968 &root
->fs_info
->delalloc_bytes
);
3973 * maybe_commit_transaction - possibly commit the transaction if its ok to
3974 * @root - the root we're allocating for
3975 * @bytes - the number of bytes we want to reserve
3976 * @force - force the commit
3978 * This will check to make sure that committing the transaction will actually
3979 * get us somewhere and then commit the transaction if it does. Otherwise it
3980 * will return -ENOSPC.
3982 static int may_commit_transaction(struct btrfs_root
*root
,
3983 struct btrfs_space_info
*space_info
,
3984 u64 bytes
, int force
)
3986 struct btrfs_block_rsv
*delayed_rsv
= &root
->fs_info
->delayed_block_rsv
;
3987 struct btrfs_trans_handle
*trans
;
3989 trans
= (struct btrfs_trans_handle
*)current
->journal_info
;
3996 /* See if there is enough pinned space to make this reservation */
3997 spin_lock(&space_info
->lock
);
3998 if (space_info
->bytes_pinned
>= bytes
) {
3999 spin_unlock(&space_info
->lock
);
4002 spin_unlock(&space_info
->lock
);
4005 * See if there is some space in the delayed insertion reservation for
4008 if (space_info
!= delayed_rsv
->space_info
)
4011 spin_lock(&space_info
->lock
);
4012 spin_lock(&delayed_rsv
->lock
);
4013 if (space_info
->bytes_pinned
+ delayed_rsv
->size
< bytes
) {
4014 spin_unlock(&delayed_rsv
->lock
);
4015 spin_unlock(&space_info
->lock
);
4018 spin_unlock(&delayed_rsv
->lock
);
4019 spin_unlock(&space_info
->lock
);
4022 trans
= btrfs_join_transaction(root
);
4026 return btrfs_commit_transaction(trans
, root
);
4030 FLUSH_DELAYED_ITEMS_NR
= 1,
4031 FLUSH_DELAYED_ITEMS
= 2,
4033 FLUSH_DELALLOC_WAIT
= 4,
4038 static int flush_space(struct btrfs_root
*root
,
4039 struct btrfs_space_info
*space_info
, u64 num_bytes
,
4040 u64 orig_bytes
, int state
)
4042 struct btrfs_trans_handle
*trans
;
4047 case FLUSH_DELAYED_ITEMS_NR
:
4048 case FLUSH_DELAYED_ITEMS
:
4049 if (state
== FLUSH_DELAYED_ITEMS_NR
) {
4050 u64 bytes
= btrfs_calc_trans_metadata_size(root
, 1);
4052 nr
= (int)div64_u64(num_bytes
, bytes
);
4059 trans
= btrfs_join_transaction(root
);
4060 if (IS_ERR(trans
)) {
4061 ret
= PTR_ERR(trans
);
4064 ret
= btrfs_run_delayed_items_nr(trans
, root
, nr
);
4065 btrfs_end_transaction(trans
, root
);
4067 case FLUSH_DELALLOC
:
4068 case FLUSH_DELALLOC_WAIT
:
4069 shrink_delalloc(root
, num_bytes
, orig_bytes
,
4070 state
== FLUSH_DELALLOC_WAIT
);
4073 trans
= btrfs_join_transaction(root
);
4074 if (IS_ERR(trans
)) {
4075 ret
= PTR_ERR(trans
);
4078 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
4079 btrfs_get_alloc_profile(root
, 0),
4080 CHUNK_ALLOC_NO_FORCE
);
4081 btrfs_end_transaction(trans
, root
);
4086 ret
= may_commit_transaction(root
, space_info
, orig_bytes
, 0);
4096 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
4097 * @root - the root we're allocating for
4098 * @block_rsv - the block_rsv we're allocating for
4099 * @orig_bytes - the number of bytes we want
4100 * @flush - whether or not we can flush to make our reservation
4102 * This will reserve orgi_bytes number of bytes from the space info associated
4103 * with the block_rsv. If there is not enough space it will make an attempt to
4104 * flush out space to make room. It will do this by flushing delalloc if
4105 * possible or committing the transaction. If flush is 0 then no attempts to
4106 * regain reservations will be made and this will fail if there is not enough
4109 static int reserve_metadata_bytes(struct btrfs_root
*root
,
4110 struct btrfs_block_rsv
*block_rsv
,
4112 enum btrfs_reserve_flush_enum flush
)
4114 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
4116 u64 num_bytes
= orig_bytes
;
4117 int flush_state
= FLUSH_DELAYED_ITEMS_NR
;
4119 bool flushing
= false;
4123 spin_lock(&space_info
->lock
);
4125 * We only want to wait if somebody other than us is flushing and we
4126 * are actually allowed to flush all things.
4128 while (flush
== BTRFS_RESERVE_FLUSH_ALL
&& !flushing
&&
4129 space_info
->flush
) {
4130 spin_unlock(&space_info
->lock
);
4132 * If we have a trans handle we can't wait because the flusher
4133 * may have to commit the transaction, which would mean we would
4134 * deadlock since we are waiting for the flusher to finish, but
4135 * hold the current transaction open.
4137 if (current
->journal_info
)
4139 ret
= wait_event_killable(space_info
->wait
, !space_info
->flush
);
4140 /* Must have been killed, return */
4144 spin_lock(&space_info
->lock
);
4148 used
= space_info
->bytes_used
+ space_info
->bytes_reserved
+
4149 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
4150 space_info
->bytes_may_use
;
4153 * The idea here is that we've not already over-reserved the block group
4154 * then we can go ahead and save our reservation first and then start
4155 * flushing if we need to. Otherwise if we've already overcommitted
4156 * lets start flushing stuff first and then come back and try to make
4159 if (used
<= space_info
->total_bytes
) {
4160 if (used
+ orig_bytes
<= space_info
->total_bytes
) {
4161 space_info
->bytes_may_use
+= orig_bytes
;
4162 trace_btrfs_space_reservation(root
->fs_info
,
4163 "space_info", space_info
->flags
, orig_bytes
, 1);
4167 * Ok set num_bytes to orig_bytes since we aren't
4168 * overocmmitted, this way we only try and reclaim what
4171 num_bytes
= orig_bytes
;
4175 * Ok we're over committed, set num_bytes to the overcommitted
4176 * amount plus the amount of bytes that we need for this
4179 num_bytes
= used
- space_info
->total_bytes
+
4183 if (ret
&& can_overcommit(root
, space_info
, orig_bytes
, flush
)) {
4184 space_info
->bytes_may_use
+= orig_bytes
;
4185 trace_btrfs_space_reservation(root
->fs_info
, "space_info",
4186 space_info
->flags
, orig_bytes
,
4192 * Couldn't make our reservation, save our place so while we're trying
4193 * to reclaim space we can actually use it instead of somebody else
4194 * stealing it from us.
4196 * We make the other tasks wait for the flush only when we can flush
4199 if (ret
&& flush
!= BTRFS_RESERVE_NO_FLUSH
) {
4201 space_info
->flush
= 1;
4204 spin_unlock(&space_info
->lock
);
4206 if (!ret
|| flush
== BTRFS_RESERVE_NO_FLUSH
)
4209 ret
= flush_space(root
, space_info
, num_bytes
, orig_bytes
,
4214 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4215 * would happen. So skip delalloc flush.
4217 if (flush
== BTRFS_RESERVE_FLUSH_LIMIT
&&
4218 (flush_state
== FLUSH_DELALLOC
||
4219 flush_state
== FLUSH_DELALLOC_WAIT
))
4220 flush_state
= ALLOC_CHUNK
;
4224 else if (flush
== BTRFS_RESERVE_FLUSH_LIMIT
&&
4225 flush_state
< COMMIT_TRANS
)
4227 else if (flush
== BTRFS_RESERVE_FLUSH_ALL
&&
4228 flush_state
<= COMMIT_TRANS
)
4232 if (ret
== -ENOSPC
&&
4233 unlikely(root
->orphan_cleanup_state
== ORPHAN_CLEANUP_STARTED
)) {
4234 struct btrfs_block_rsv
*global_rsv
=
4235 &root
->fs_info
->global_block_rsv
;
4237 if (block_rsv
!= global_rsv
&&
4238 !block_rsv_use_bytes(global_rsv
, orig_bytes
))
4242 spin_lock(&space_info
->lock
);
4243 space_info
->flush
= 0;
4244 wake_up_all(&space_info
->wait
);
4245 spin_unlock(&space_info
->lock
);
4250 static struct btrfs_block_rsv
*get_block_rsv(
4251 const struct btrfs_trans_handle
*trans
,
4252 const struct btrfs_root
*root
)
4254 struct btrfs_block_rsv
*block_rsv
= NULL
;
4257 block_rsv
= trans
->block_rsv
;
4259 if (root
== root
->fs_info
->csum_root
&& trans
->adding_csums
)
4260 block_rsv
= trans
->block_rsv
;
4263 block_rsv
= root
->block_rsv
;
4266 block_rsv
= &root
->fs_info
->empty_block_rsv
;
4271 static int block_rsv_use_bytes(struct btrfs_block_rsv
*block_rsv
,
4275 spin_lock(&block_rsv
->lock
);
4276 if (block_rsv
->reserved
>= num_bytes
) {
4277 block_rsv
->reserved
-= num_bytes
;
4278 if (block_rsv
->reserved
< block_rsv
->size
)
4279 block_rsv
->full
= 0;
4282 spin_unlock(&block_rsv
->lock
);
4286 static void block_rsv_add_bytes(struct btrfs_block_rsv
*block_rsv
,
4287 u64 num_bytes
, int update_size
)
4289 spin_lock(&block_rsv
->lock
);
4290 block_rsv
->reserved
+= num_bytes
;
4292 block_rsv
->size
+= num_bytes
;
4293 else if (block_rsv
->reserved
>= block_rsv
->size
)
4294 block_rsv
->full
= 1;
4295 spin_unlock(&block_rsv
->lock
);
4298 static void block_rsv_release_bytes(struct btrfs_fs_info
*fs_info
,
4299 struct btrfs_block_rsv
*block_rsv
,
4300 struct btrfs_block_rsv
*dest
, u64 num_bytes
)
4302 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
4304 spin_lock(&block_rsv
->lock
);
4305 if (num_bytes
== (u64
)-1)
4306 num_bytes
= block_rsv
->size
;
4307 block_rsv
->size
-= num_bytes
;
4308 if (block_rsv
->reserved
>= block_rsv
->size
) {
4309 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
4310 block_rsv
->reserved
= block_rsv
->size
;
4311 block_rsv
->full
= 1;
4315 spin_unlock(&block_rsv
->lock
);
4317 if (num_bytes
> 0) {
4319 spin_lock(&dest
->lock
);
4323 bytes_to_add
= dest
->size
- dest
->reserved
;
4324 bytes_to_add
= min(num_bytes
, bytes_to_add
);
4325 dest
->reserved
+= bytes_to_add
;
4326 if (dest
->reserved
>= dest
->size
)
4328 num_bytes
-= bytes_to_add
;
4330 spin_unlock(&dest
->lock
);
4333 spin_lock(&space_info
->lock
);
4334 space_info
->bytes_may_use
-= num_bytes
;
4335 trace_btrfs_space_reservation(fs_info
, "space_info",
4336 space_info
->flags
, num_bytes
, 0);
4337 space_info
->reservation_progress
++;
4338 spin_unlock(&space_info
->lock
);
4343 static int block_rsv_migrate_bytes(struct btrfs_block_rsv
*src
,
4344 struct btrfs_block_rsv
*dst
, u64 num_bytes
)
4348 ret
= block_rsv_use_bytes(src
, num_bytes
);
4352 block_rsv_add_bytes(dst
, num_bytes
, 1);
4356 void btrfs_init_block_rsv(struct btrfs_block_rsv
*rsv
, unsigned short type
)
4358 memset(rsv
, 0, sizeof(*rsv
));
4359 spin_lock_init(&rsv
->lock
);
4363 struct btrfs_block_rsv
*btrfs_alloc_block_rsv(struct btrfs_root
*root
,
4364 unsigned short type
)
4366 struct btrfs_block_rsv
*block_rsv
;
4367 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4369 block_rsv
= kmalloc(sizeof(*block_rsv
), GFP_NOFS
);
4373 btrfs_init_block_rsv(block_rsv
, type
);
4374 block_rsv
->space_info
= __find_space_info(fs_info
,
4375 BTRFS_BLOCK_GROUP_METADATA
);
4379 void btrfs_free_block_rsv(struct btrfs_root
*root
,
4380 struct btrfs_block_rsv
*rsv
)
4384 btrfs_block_rsv_release(root
, rsv
, (u64
)-1);
4388 int btrfs_block_rsv_add(struct btrfs_root
*root
,
4389 struct btrfs_block_rsv
*block_rsv
, u64 num_bytes
,
4390 enum btrfs_reserve_flush_enum flush
)
4397 ret
= reserve_metadata_bytes(root
, block_rsv
, num_bytes
, flush
);
4399 block_rsv_add_bytes(block_rsv
, num_bytes
, 1);
4406 int btrfs_block_rsv_check(struct btrfs_root
*root
,
4407 struct btrfs_block_rsv
*block_rsv
, int min_factor
)
4415 spin_lock(&block_rsv
->lock
);
4416 num_bytes
= div_factor(block_rsv
->size
, min_factor
);
4417 if (block_rsv
->reserved
>= num_bytes
)
4419 spin_unlock(&block_rsv
->lock
);
4424 int btrfs_block_rsv_refill(struct btrfs_root
*root
,
4425 struct btrfs_block_rsv
*block_rsv
, u64 min_reserved
,
4426 enum btrfs_reserve_flush_enum flush
)
4434 spin_lock(&block_rsv
->lock
);
4435 num_bytes
= min_reserved
;
4436 if (block_rsv
->reserved
>= num_bytes
)
4439 num_bytes
-= block_rsv
->reserved
;
4440 spin_unlock(&block_rsv
->lock
);
4445 ret
= reserve_metadata_bytes(root
, block_rsv
, num_bytes
, flush
);
4447 block_rsv_add_bytes(block_rsv
, num_bytes
, 0);
4454 int btrfs_block_rsv_migrate(struct btrfs_block_rsv
*src_rsv
,
4455 struct btrfs_block_rsv
*dst_rsv
,
4458 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
4461 void btrfs_block_rsv_release(struct btrfs_root
*root
,
4462 struct btrfs_block_rsv
*block_rsv
,
4465 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
4466 if (global_rsv
->full
|| global_rsv
== block_rsv
||
4467 block_rsv
->space_info
!= global_rsv
->space_info
)
4469 block_rsv_release_bytes(root
->fs_info
, block_rsv
, global_rsv
,
4474 * helper to calculate size of global block reservation.
4475 * the desired value is sum of space used by extent tree,
4476 * checksum tree and root tree
4478 static u64
calc_global_metadata_size(struct btrfs_fs_info
*fs_info
)
4480 struct btrfs_space_info
*sinfo
;
4484 int csum_size
= btrfs_super_csum_size(fs_info
->super_copy
);
4486 sinfo
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_DATA
);
4487 spin_lock(&sinfo
->lock
);
4488 data_used
= sinfo
->bytes_used
;
4489 spin_unlock(&sinfo
->lock
);
4491 sinfo
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
4492 spin_lock(&sinfo
->lock
);
4493 if (sinfo
->flags
& BTRFS_BLOCK_GROUP_DATA
)
4495 meta_used
= sinfo
->bytes_used
;
4496 spin_unlock(&sinfo
->lock
);
4498 num_bytes
= (data_used
>> fs_info
->sb
->s_blocksize_bits
) *
4500 num_bytes
+= div64_u64(data_used
+ meta_used
, 50);
4502 if (num_bytes
* 3 > meta_used
)
4503 num_bytes
= div64_u64(meta_used
, 3);
4505 return ALIGN(num_bytes
, fs_info
->extent_root
->leafsize
<< 10);
4508 static void update_global_block_rsv(struct btrfs_fs_info
*fs_info
)
4510 struct btrfs_block_rsv
*block_rsv
= &fs_info
->global_block_rsv
;
4511 struct btrfs_space_info
*sinfo
= block_rsv
->space_info
;
4514 num_bytes
= calc_global_metadata_size(fs_info
);
4516 spin_lock(&sinfo
->lock
);
4517 spin_lock(&block_rsv
->lock
);
4519 block_rsv
->size
= min_t(u64
, num_bytes
, 512 * 1024 * 1024);
4521 num_bytes
= sinfo
->bytes_used
+ sinfo
->bytes_pinned
+
4522 sinfo
->bytes_reserved
+ sinfo
->bytes_readonly
+
4523 sinfo
->bytes_may_use
;
4525 if (sinfo
->total_bytes
> num_bytes
) {
4526 num_bytes
= sinfo
->total_bytes
- num_bytes
;
4527 block_rsv
->reserved
+= num_bytes
;
4528 sinfo
->bytes_may_use
+= num_bytes
;
4529 trace_btrfs_space_reservation(fs_info
, "space_info",
4530 sinfo
->flags
, num_bytes
, 1);
4533 if (block_rsv
->reserved
>= block_rsv
->size
) {
4534 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
4535 sinfo
->bytes_may_use
-= num_bytes
;
4536 trace_btrfs_space_reservation(fs_info
, "space_info",
4537 sinfo
->flags
, num_bytes
, 0);
4538 sinfo
->reservation_progress
++;
4539 block_rsv
->reserved
= block_rsv
->size
;
4540 block_rsv
->full
= 1;
4543 spin_unlock(&block_rsv
->lock
);
4544 spin_unlock(&sinfo
->lock
);
4547 static void init_global_block_rsv(struct btrfs_fs_info
*fs_info
)
4549 struct btrfs_space_info
*space_info
;
4551 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_SYSTEM
);
4552 fs_info
->chunk_block_rsv
.space_info
= space_info
;
4554 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
4555 fs_info
->global_block_rsv
.space_info
= space_info
;
4556 fs_info
->delalloc_block_rsv
.space_info
= space_info
;
4557 fs_info
->trans_block_rsv
.space_info
= space_info
;
4558 fs_info
->empty_block_rsv
.space_info
= space_info
;
4559 fs_info
->delayed_block_rsv
.space_info
= space_info
;
4561 fs_info
->extent_root
->block_rsv
= &fs_info
->global_block_rsv
;
4562 fs_info
->csum_root
->block_rsv
= &fs_info
->global_block_rsv
;
4563 fs_info
->dev_root
->block_rsv
= &fs_info
->global_block_rsv
;
4564 fs_info
->tree_root
->block_rsv
= &fs_info
->global_block_rsv
;
4565 fs_info
->chunk_root
->block_rsv
= &fs_info
->chunk_block_rsv
;
4567 update_global_block_rsv(fs_info
);
4570 static void release_global_block_rsv(struct btrfs_fs_info
*fs_info
)
4572 block_rsv_release_bytes(fs_info
, &fs_info
->global_block_rsv
, NULL
,
4574 WARN_ON(fs_info
->delalloc_block_rsv
.size
> 0);
4575 WARN_ON(fs_info
->delalloc_block_rsv
.reserved
> 0);
4576 WARN_ON(fs_info
->trans_block_rsv
.size
> 0);
4577 WARN_ON(fs_info
->trans_block_rsv
.reserved
> 0);
4578 WARN_ON(fs_info
->chunk_block_rsv
.size
> 0);
4579 WARN_ON(fs_info
->chunk_block_rsv
.reserved
> 0);
4580 WARN_ON(fs_info
->delayed_block_rsv
.size
> 0);
4581 WARN_ON(fs_info
->delayed_block_rsv
.reserved
> 0);
4584 void btrfs_trans_release_metadata(struct btrfs_trans_handle
*trans
,
4585 struct btrfs_root
*root
)
4587 if (!trans
->block_rsv
)
4590 if (!trans
->bytes_reserved
)
4593 trace_btrfs_space_reservation(root
->fs_info
, "transaction",
4594 trans
->transid
, trans
->bytes_reserved
, 0);
4595 btrfs_block_rsv_release(root
, trans
->block_rsv
, trans
->bytes_reserved
);
4596 trans
->bytes_reserved
= 0;
4599 /* Can only return 0 or -ENOSPC */
4600 int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle
*trans
,
4601 struct inode
*inode
)
4603 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4604 struct btrfs_block_rsv
*src_rsv
= get_block_rsv(trans
, root
);
4605 struct btrfs_block_rsv
*dst_rsv
= root
->orphan_block_rsv
;
4608 * We need to hold space in order to delete our orphan item once we've
4609 * added it, so this takes the reservation so we can release it later
4610 * when we are truly done with the orphan item.
4612 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 1);
4613 trace_btrfs_space_reservation(root
->fs_info
, "orphan",
4614 btrfs_ino(inode
), num_bytes
, 1);
4615 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
4618 void btrfs_orphan_release_metadata(struct inode
*inode
)
4620 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4621 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 1);
4622 trace_btrfs_space_reservation(root
->fs_info
, "orphan",
4623 btrfs_ino(inode
), num_bytes
, 0);
4624 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
, num_bytes
);
4628 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
4629 * root: the root of the parent directory
4630 * rsv: block reservation
4631 * items: the number of items that we need do reservation
4632 * qgroup_reserved: used to return the reserved size in qgroup
4634 * This function is used to reserve the space for snapshot/subvolume
4635 * creation and deletion. Those operations are different with the
4636 * common file/directory operations, they change two fs/file trees
4637 * and root tree, the number of items that the qgroup reserves is
4638 * different with the free space reservation. So we can not use
4639 * the space reseravtion mechanism in start_transaction().
4641 int btrfs_subvolume_reserve_metadata(struct btrfs_root
*root
,
4642 struct btrfs_block_rsv
*rsv
,
4644 u64
*qgroup_reserved
)
4649 if (root
->fs_info
->quota_enabled
) {
4650 /* One for parent inode, two for dir entries */
4651 num_bytes
= 3 * root
->leafsize
;
4652 ret
= btrfs_qgroup_reserve(root
, num_bytes
);
4659 *qgroup_reserved
= num_bytes
;
4661 num_bytes
= btrfs_calc_trans_metadata_size(root
, items
);
4662 rsv
->space_info
= __find_space_info(root
->fs_info
,
4663 BTRFS_BLOCK_GROUP_METADATA
);
4664 ret
= btrfs_block_rsv_add(root
, rsv
, num_bytes
,
4665 BTRFS_RESERVE_FLUSH_ALL
);
4667 if (*qgroup_reserved
)
4668 btrfs_qgroup_free(root
, *qgroup_reserved
);
4674 void btrfs_subvolume_release_metadata(struct btrfs_root
*root
,
4675 struct btrfs_block_rsv
*rsv
,
4676 u64 qgroup_reserved
)
4678 btrfs_block_rsv_release(root
, rsv
, (u64
)-1);
4679 if (qgroup_reserved
)
4680 btrfs_qgroup_free(root
, qgroup_reserved
);
4684 * drop_outstanding_extent - drop an outstanding extent
4685 * @inode: the inode we're dropping the extent for
4687 * This is called when we are freeing up an outstanding extent, either called
4688 * after an error or after an extent is written. This will return the number of
4689 * reserved extents that need to be freed. This must be called with
4690 * BTRFS_I(inode)->lock held.
4692 static unsigned drop_outstanding_extent(struct inode
*inode
)
4694 unsigned drop_inode_space
= 0;
4695 unsigned dropped_extents
= 0;
4697 BUG_ON(!BTRFS_I(inode
)->outstanding_extents
);
4698 BTRFS_I(inode
)->outstanding_extents
--;
4700 if (BTRFS_I(inode
)->outstanding_extents
== 0 &&
4701 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED
,
4702 &BTRFS_I(inode
)->runtime_flags
))
4703 drop_inode_space
= 1;
4706 * If we have more or the same amount of outsanding extents than we have
4707 * reserved then we need to leave the reserved extents count alone.
4709 if (BTRFS_I(inode
)->outstanding_extents
>=
4710 BTRFS_I(inode
)->reserved_extents
)
4711 return drop_inode_space
;
4713 dropped_extents
= BTRFS_I(inode
)->reserved_extents
-
4714 BTRFS_I(inode
)->outstanding_extents
;
4715 BTRFS_I(inode
)->reserved_extents
-= dropped_extents
;
4716 return dropped_extents
+ drop_inode_space
;
4720 * calc_csum_metadata_size - return the amount of metada space that must be
4721 * reserved/free'd for the given bytes.
4722 * @inode: the inode we're manipulating
4723 * @num_bytes: the number of bytes in question
4724 * @reserve: 1 if we are reserving space, 0 if we are freeing space
4726 * This adjusts the number of csum_bytes in the inode and then returns the
4727 * correct amount of metadata that must either be reserved or freed. We
4728 * calculate how many checksums we can fit into one leaf and then divide the
4729 * number of bytes that will need to be checksumed by this value to figure out
4730 * how many checksums will be required. If we are adding bytes then the number
4731 * may go up and we will return the number of additional bytes that must be
4732 * reserved. If it is going down we will return the number of bytes that must
4735 * This must be called with BTRFS_I(inode)->lock held.
4737 static u64
calc_csum_metadata_size(struct inode
*inode
, u64 num_bytes
,
4740 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4742 int num_csums_per_leaf
;
4746 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
&&
4747 BTRFS_I(inode
)->csum_bytes
== 0)
4750 old_csums
= (int)div64_u64(BTRFS_I(inode
)->csum_bytes
, root
->sectorsize
);
4752 BTRFS_I(inode
)->csum_bytes
+= num_bytes
;
4754 BTRFS_I(inode
)->csum_bytes
-= num_bytes
;
4755 csum_size
= BTRFS_LEAF_DATA_SIZE(root
) - sizeof(struct btrfs_item
);
4756 num_csums_per_leaf
= (int)div64_u64(csum_size
,
4757 sizeof(struct btrfs_csum_item
) +
4758 sizeof(struct btrfs_disk_key
));
4759 num_csums
= (int)div64_u64(BTRFS_I(inode
)->csum_bytes
, root
->sectorsize
);
4760 num_csums
= num_csums
+ num_csums_per_leaf
- 1;
4761 num_csums
= num_csums
/ num_csums_per_leaf
;
4763 old_csums
= old_csums
+ num_csums_per_leaf
- 1;
4764 old_csums
= old_csums
/ num_csums_per_leaf
;
4766 /* No change, no need to reserve more */
4767 if (old_csums
== num_csums
)
4771 return btrfs_calc_trans_metadata_size(root
,
4772 num_csums
- old_csums
);
4774 return btrfs_calc_trans_metadata_size(root
, old_csums
- num_csums
);
4777 int btrfs_delalloc_reserve_metadata(struct inode
*inode
, u64 num_bytes
)
4779 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4780 struct btrfs_block_rsv
*block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
4783 unsigned nr_extents
= 0;
4784 int extra_reserve
= 0;
4785 enum btrfs_reserve_flush_enum flush
= BTRFS_RESERVE_FLUSH_ALL
;
4787 bool delalloc_lock
= true;
4791 /* If we are a free space inode we need to not flush since we will be in
4792 * the middle of a transaction commit. We also don't need the delalloc
4793 * mutex since we won't race with anybody. We need this mostly to make
4794 * lockdep shut its filthy mouth.
4796 if (btrfs_is_free_space_inode(inode
)) {
4797 flush
= BTRFS_RESERVE_NO_FLUSH
;
4798 delalloc_lock
= false;
4801 if (flush
!= BTRFS_RESERVE_NO_FLUSH
&&
4802 btrfs_transaction_in_commit(root
->fs_info
))
4803 schedule_timeout(1);
4806 mutex_lock(&BTRFS_I(inode
)->delalloc_mutex
);
4808 num_bytes
= ALIGN(num_bytes
, root
->sectorsize
);
4810 spin_lock(&BTRFS_I(inode
)->lock
);
4811 BTRFS_I(inode
)->outstanding_extents
++;
4813 if (BTRFS_I(inode
)->outstanding_extents
>
4814 BTRFS_I(inode
)->reserved_extents
)
4815 nr_extents
= BTRFS_I(inode
)->outstanding_extents
-
4816 BTRFS_I(inode
)->reserved_extents
;
4819 * Add an item to reserve for updating the inode when we complete the
4822 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED
,
4823 &BTRFS_I(inode
)->runtime_flags
)) {
4828 to_reserve
= btrfs_calc_trans_metadata_size(root
, nr_extents
);
4829 to_reserve
+= calc_csum_metadata_size(inode
, num_bytes
, 1);
4830 csum_bytes
= BTRFS_I(inode
)->csum_bytes
;
4831 spin_unlock(&BTRFS_I(inode
)->lock
);
4833 if (root
->fs_info
->quota_enabled
) {
4834 ret
= btrfs_qgroup_reserve(root
, num_bytes
+
4835 nr_extents
* root
->leafsize
);
4840 ret
= reserve_metadata_bytes(root
, block_rsv
, to_reserve
, flush
);
4841 if (unlikely(ret
)) {
4842 if (root
->fs_info
->quota_enabled
)
4843 btrfs_qgroup_free(root
, num_bytes
+
4844 nr_extents
* root
->leafsize
);
4848 spin_lock(&BTRFS_I(inode
)->lock
);
4849 if (extra_reserve
) {
4850 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED
,
4851 &BTRFS_I(inode
)->runtime_flags
);
4854 BTRFS_I(inode
)->reserved_extents
+= nr_extents
;
4855 spin_unlock(&BTRFS_I(inode
)->lock
);
4858 mutex_unlock(&BTRFS_I(inode
)->delalloc_mutex
);
4861 trace_btrfs_space_reservation(root
->fs_info
,"delalloc",
4862 btrfs_ino(inode
), to_reserve
, 1);
4863 block_rsv_add_bytes(block_rsv
, to_reserve
, 1);
4868 spin_lock(&BTRFS_I(inode
)->lock
);
4869 dropped
= drop_outstanding_extent(inode
);
4871 * If the inodes csum_bytes is the same as the original
4872 * csum_bytes then we know we haven't raced with any free()ers
4873 * so we can just reduce our inodes csum bytes and carry on.
4875 if (BTRFS_I(inode
)->csum_bytes
== csum_bytes
) {
4876 calc_csum_metadata_size(inode
, num_bytes
, 0);
4878 u64 orig_csum_bytes
= BTRFS_I(inode
)->csum_bytes
;
4882 * This is tricky, but first we need to figure out how much we
4883 * free'd from any free-ers that occured during this
4884 * reservation, so we reset ->csum_bytes to the csum_bytes
4885 * before we dropped our lock, and then call the free for the
4886 * number of bytes that were freed while we were trying our
4889 bytes
= csum_bytes
- BTRFS_I(inode
)->csum_bytes
;
4890 BTRFS_I(inode
)->csum_bytes
= csum_bytes
;
4891 to_free
= calc_csum_metadata_size(inode
, bytes
, 0);
4895 * Now we need to see how much we would have freed had we not
4896 * been making this reservation and our ->csum_bytes were not
4897 * artificially inflated.
4899 BTRFS_I(inode
)->csum_bytes
= csum_bytes
- num_bytes
;
4900 bytes
= csum_bytes
- orig_csum_bytes
;
4901 bytes
= calc_csum_metadata_size(inode
, bytes
, 0);
4904 * Now reset ->csum_bytes to what it should be. If bytes is
4905 * more than to_free then we would have free'd more space had we
4906 * not had an artificially high ->csum_bytes, so we need to free
4907 * the remainder. If bytes is the same or less then we don't
4908 * need to do anything, the other free-ers did the correct
4911 BTRFS_I(inode
)->csum_bytes
= orig_csum_bytes
- num_bytes
;
4912 if (bytes
> to_free
)
4913 to_free
= bytes
- to_free
;
4917 spin_unlock(&BTRFS_I(inode
)->lock
);
4919 to_free
+= btrfs_calc_trans_metadata_size(root
, dropped
);
4922 btrfs_block_rsv_release(root
, block_rsv
, to_free
);
4923 trace_btrfs_space_reservation(root
->fs_info
, "delalloc",
4924 btrfs_ino(inode
), to_free
, 0);
4927 mutex_unlock(&BTRFS_I(inode
)->delalloc_mutex
);
4932 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
4933 * @inode: the inode to release the reservation for
4934 * @num_bytes: the number of bytes we're releasing
4936 * This will release the metadata reservation for an inode. This can be called
4937 * once we complete IO for a given set of bytes to release their metadata
4940 void btrfs_delalloc_release_metadata(struct inode
*inode
, u64 num_bytes
)
4942 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4946 num_bytes
= ALIGN(num_bytes
, root
->sectorsize
);
4947 spin_lock(&BTRFS_I(inode
)->lock
);
4948 dropped
= drop_outstanding_extent(inode
);
4951 to_free
= calc_csum_metadata_size(inode
, num_bytes
, 0);
4952 spin_unlock(&BTRFS_I(inode
)->lock
);
4954 to_free
+= btrfs_calc_trans_metadata_size(root
, dropped
);
4956 trace_btrfs_space_reservation(root
->fs_info
, "delalloc",
4957 btrfs_ino(inode
), to_free
, 0);
4958 if (root
->fs_info
->quota_enabled
) {
4959 btrfs_qgroup_free(root
, num_bytes
+
4960 dropped
* root
->leafsize
);
4963 btrfs_block_rsv_release(root
, &root
->fs_info
->delalloc_block_rsv
,
4968 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
4969 * @inode: inode we're writing to
4970 * @num_bytes: the number of bytes we want to allocate
4972 * This will do the following things
4974 * o reserve space in the data space info for num_bytes
4975 * o reserve space in the metadata space info based on number of outstanding
4976 * extents and how much csums will be needed
4977 * o add to the inodes ->delalloc_bytes
4978 * o add it to the fs_info's delalloc inodes list.
4980 * This will return 0 for success and -ENOSPC if there is no space left.
4982 int btrfs_delalloc_reserve_space(struct inode
*inode
, u64 num_bytes
)
4986 ret
= btrfs_check_data_free_space(inode
, num_bytes
);
4990 ret
= btrfs_delalloc_reserve_metadata(inode
, num_bytes
);
4992 btrfs_free_reserved_data_space(inode
, num_bytes
);
5000 * btrfs_delalloc_release_space - release data and metadata space for delalloc
5001 * @inode: inode we're releasing space for
5002 * @num_bytes: the number of bytes we want to free up
5004 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
5005 * called in the case that we don't need the metadata AND data reservations
5006 * anymore. So if there is an error or we insert an inline extent.
5008 * This function will release the metadata space that was not used and will
5009 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
5010 * list if there are no delalloc bytes left.
5012 void btrfs_delalloc_release_space(struct inode
*inode
, u64 num_bytes
)
5014 btrfs_delalloc_release_metadata(inode
, num_bytes
);
5015 btrfs_free_reserved_data_space(inode
, num_bytes
);
5018 static int update_block_group(struct btrfs_root
*root
,
5019 u64 bytenr
, u64 num_bytes
, int alloc
)
5021 struct btrfs_block_group_cache
*cache
= NULL
;
5022 struct btrfs_fs_info
*info
= root
->fs_info
;
5023 u64 total
= num_bytes
;
5028 /* block accounting for super block */
5029 spin_lock(&info
->delalloc_lock
);
5030 old_val
= btrfs_super_bytes_used(info
->super_copy
);
5032 old_val
+= num_bytes
;
5034 old_val
-= num_bytes
;
5035 btrfs_set_super_bytes_used(info
->super_copy
, old_val
);
5036 spin_unlock(&info
->delalloc_lock
);
5039 cache
= btrfs_lookup_block_group(info
, bytenr
);
5042 if (cache
->flags
& (BTRFS_BLOCK_GROUP_DUP
|
5043 BTRFS_BLOCK_GROUP_RAID1
|
5044 BTRFS_BLOCK_GROUP_RAID10
))
5049 * If this block group has free space cache written out, we
5050 * need to make sure to load it if we are removing space. This
5051 * is because we need the unpinning stage to actually add the
5052 * space back to the block group, otherwise we will leak space.
5054 if (!alloc
&& cache
->cached
== BTRFS_CACHE_NO
)
5055 cache_block_group(cache
, 1);
5057 byte_in_group
= bytenr
- cache
->key
.objectid
;
5058 WARN_ON(byte_in_group
> cache
->key
.offset
);
5060 spin_lock(&cache
->space_info
->lock
);
5061 spin_lock(&cache
->lock
);
5063 if (btrfs_test_opt(root
, SPACE_CACHE
) &&
5064 cache
->disk_cache_state
< BTRFS_DC_CLEAR
)
5065 cache
->disk_cache_state
= BTRFS_DC_CLEAR
;
5068 old_val
= btrfs_block_group_used(&cache
->item
);
5069 num_bytes
= min(total
, cache
->key
.offset
- byte_in_group
);
5071 old_val
+= num_bytes
;
5072 btrfs_set_block_group_used(&cache
->item
, old_val
);
5073 cache
->reserved
-= num_bytes
;
5074 cache
->space_info
->bytes_reserved
-= num_bytes
;
5075 cache
->space_info
->bytes_used
+= num_bytes
;
5076 cache
->space_info
->disk_used
+= num_bytes
* factor
;
5077 spin_unlock(&cache
->lock
);
5078 spin_unlock(&cache
->space_info
->lock
);
5080 old_val
-= num_bytes
;
5081 btrfs_set_block_group_used(&cache
->item
, old_val
);
5082 cache
->pinned
+= num_bytes
;
5083 cache
->space_info
->bytes_pinned
+= num_bytes
;
5084 cache
->space_info
->bytes_used
-= num_bytes
;
5085 cache
->space_info
->disk_used
-= num_bytes
* factor
;
5086 spin_unlock(&cache
->lock
);
5087 spin_unlock(&cache
->space_info
->lock
);
5089 set_extent_dirty(info
->pinned_extents
,
5090 bytenr
, bytenr
+ num_bytes
- 1,
5091 GFP_NOFS
| __GFP_NOFAIL
);
5093 btrfs_put_block_group(cache
);
5095 bytenr
+= num_bytes
;
5100 static u64
first_logical_byte(struct btrfs_root
*root
, u64 search_start
)
5102 struct btrfs_block_group_cache
*cache
;
5105 spin_lock(&root
->fs_info
->block_group_cache_lock
);
5106 bytenr
= root
->fs_info
->first_logical_byte
;
5107 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
5109 if (bytenr
< (u64
)-1)
5112 cache
= btrfs_lookup_first_block_group(root
->fs_info
, search_start
);
5116 bytenr
= cache
->key
.objectid
;
5117 btrfs_put_block_group(cache
);
5122 static int pin_down_extent(struct btrfs_root
*root
,
5123 struct btrfs_block_group_cache
*cache
,
5124 u64 bytenr
, u64 num_bytes
, int reserved
)
5126 spin_lock(&cache
->space_info
->lock
);
5127 spin_lock(&cache
->lock
);
5128 cache
->pinned
+= num_bytes
;
5129 cache
->space_info
->bytes_pinned
+= num_bytes
;
5131 cache
->reserved
-= num_bytes
;
5132 cache
->space_info
->bytes_reserved
-= num_bytes
;
5134 spin_unlock(&cache
->lock
);
5135 spin_unlock(&cache
->space_info
->lock
);
5137 set_extent_dirty(root
->fs_info
->pinned_extents
, bytenr
,
5138 bytenr
+ num_bytes
- 1, GFP_NOFS
| __GFP_NOFAIL
);
5143 * this function must be called within transaction
5145 int btrfs_pin_extent(struct btrfs_root
*root
,
5146 u64 bytenr
, u64 num_bytes
, int reserved
)
5148 struct btrfs_block_group_cache
*cache
;
5150 cache
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
5151 BUG_ON(!cache
); /* Logic error */
5153 pin_down_extent(root
, cache
, bytenr
, num_bytes
, reserved
);
5155 btrfs_put_block_group(cache
);
5160 * this function must be called within transaction
5162 int btrfs_pin_extent_for_log_replay(struct btrfs_root
*root
,
5163 u64 bytenr
, u64 num_bytes
)
5165 struct btrfs_block_group_cache
*cache
;
5168 cache
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
5173 * pull in the free space cache (if any) so that our pin
5174 * removes the free space from the cache. We have load_only set
5175 * to one because the slow code to read in the free extents does check
5176 * the pinned extents.
5178 cache_block_group(cache
, 1);
5180 pin_down_extent(root
, cache
, bytenr
, num_bytes
, 0);
5182 /* remove us from the free space cache (if we're there at all) */
5183 ret
= btrfs_remove_free_space(cache
, bytenr
, num_bytes
);
5184 btrfs_put_block_group(cache
);
5189 * btrfs_update_reserved_bytes - update the block_group and space info counters
5190 * @cache: The cache we are manipulating
5191 * @num_bytes: The number of bytes in question
5192 * @reserve: One of the reservation enums
5194 * This is called by the allocator when it reserves space, or by somebody who is
5195 * freeing space that was never actually used on disk. For example if you
5196 * reserve some space for a new leaf in transaction A and before transaction A
5197 * commits you free that leaf, you call this with reserve set to 0 in order to
5198 * clear the reservation.
5200 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
5201 * ENOSPC accounting. For data we handle the reservation through clearing the
5202 * delalloc bits in the io_tree. We have to do this since we could end up
5203 * allocating less disk space for the amount of data we have reserved in the
5204 * case of compression.
5206 * If this is a reservation and the block group has become read only we cannot
5207 * make the reservation and return -EAGAIN, otherwise this function always
5210 static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache
*cache
,
5211 u64 num_bytes
, int reserve
)
5213 struct btrfs_space_info
*space_info
= cache
->space_info
;
5216 spin_lock(&space_info
->lock
);
5217 spin_lock(&cache
->lock
);
5218 if (reserve
!= RESERVE_FREE
) {
5222 cache
->reserved
+= num_bytes
;
5223 space_info
->bytes_reserved
+= num_bytes
;
5224 if (reserve
== RESERVE_ALLOC
) {
5225 trace_btrfs_space_reservation(cache
->fs_info
,
5226 "space_info", space_info
->flags
,
5228 space_info
->bytes_may_use
-= num_bytes
;
5233 space_info
->bytes_readonly
+= num_bytes
;
5234 cache
->reserved
-= num_bytes
;
5235 space_info
->bytes_reserved
-= num_bytes
;
5236 space_info
->reservation_progress
++;
5238 spin_unlock(&cache
->lock
);
5239 spin_unlock(&space_info
->lock
);
5243 void btrfs_prepare_extent_commit(struct btrfs_trans_handle
*trans
,
5244 struct btrfs_root
*root
)
5246 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5247 struct btrfs_caching_control
*next
;
5248 struct btrfs_caching_control
*caching_ctl
;
5249 struct btrfs_block_group_cache
*cache
;
5251 down_write(&fs_info
->extent_commit_sem
);
5253 list_for_each_entry_safe(caching_ctl
, next
,
5254 &fs_info
->caching_block_groups
, list
) {
5255 cache
= caching_ctl
->block_group
;
5256 if (block_group_cache_done(cache
)) {
5257 cache
->last_byte_to_unpin
= (u64
)-1;
5258 list_del_init(&caching_ctl
->list
);
5259 put_caching_control(caching_ctl
);
5261 cache
->last_byte_to_unpin
= caching_ctl
->progress
;
5265 if (fs_info
->pinned_extents
== &fs_info
->freed_extents
[0])
5266 fs_info
->pinned_extents
= &fs_info
->freed_extents
[1];
5268 fs_info
->pinned_extents
= &fs_info
->freed_extents
[0];
5270 up_write(&fs_info
->extent_commit_sem
);
5272 update_global_block_rsv(fs_info
);
5275 static int unpin_extent_range(struct btrfs_root
*root
, u64 start
, u64 end
)
5277 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5278 struct btrfs_block_group_cache
*cache
= NULL
;
5279 struct btrfs_space_info
*space_info
;
5280 struct btrfs_block_rsv
*global_rsv
= &fs_info
->global_block_rsv
;
5284 while (start
<= end
) {
5287 start
>= cache
->key
.objectid
+ cache
->key
.offset
) {
5289 btrfs_put_block_group(cache
);
5290 cache
= btrfs_lookup_block_group(fs_info
, start
);
5291 BUG_ON(!cache
); /* Logic error */
5294 len
= cache
->key
.objectid
+ cache
->key
.offset
- start
;
5295 len
= min(len
, end
+ 1 - start
);
5297 if (start
< cache
->last_byte_to_unpin
) {
5298 len
= min(len
, cache
->last_byte_to_unpin
- start
);
5299 btrfs_add_free_space(cache
, start
, len
);
5303 space_info
= cache
->space_info
;
5305 spin_lock(&space_info
->lock
);
5306 spin_lock(&cache
->lock
);
5307 cache
->pinned
-= len
;
5308 space_info
->bytes_pinned
-= len
;
5310 space_info
->bytes_readonly
+= len
;
5313 spin_unlock(&cache
->lock
);
5314 if (!readonly
&& global_rsv
->space_info
== space_info
) {
5315 spin_lock(&global_rsv
->lock
);
5316 if (!global_rsv
->full
) {
5317 len
= min(len
, global_rsv
->size
-
5318 global_rsv
->reserved
);
5319 global_rsv
->reserved
+= len
;
5320 space_info
->bytes_may_use
+= len
;
5321 if (global_rsv
->reserved
>= global_rsv
->size
)
5322 global_rsv
->full
= 1;
5324 spin_unlock(&global_rsv
->lock
);
5326 spin_unlock(&space_info
->lock
);
5330 btrfs_put_block_group(cache
);
5334 int btrfs_finish_extent_commit(struct btrfs_trans_handle
*trans
,
5335 struct btrfs_root
*root
)
5337 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5338 struct extent_io_tree
*unpin
;
5346 if (fs_info
->pinned_extents
== &fs_info
->freed_extents
[0])
5347 unpin
= &fs_info
->freed_extents
[1];
5349 unpin
= &fs_info
->freed_extents
[0];
5352 ret
= find_first_extent_bit(unpin
, 0, &start
, &end
,
5353 EXTENT_DIRTY
, NULL
);
5357 if (btrfs_test_opt(root
, DISCARD
))
5358 ret
= btrfs_discard_extent(root
, start
,
5359 end
+ 1 - start
, NULL
);
5361 clear_extent_dirty(unpin
, start
, end
, GFP_NOFS
);
5362 unpin_extent_range(root
, start
, end
);
5369 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
5370 struct btrfs_root
*root
,
5371 u64 bytenr
, u64 num_bytes
, u64 parent
,
5372 u64 root_objectid
, u64 owner_objectid
,
5373 u64 owner_offset
, int refs_to_drop
,
5374 struct btrfs_delayed_extent_op
*extent_op
)
5376 struct btrfs_key key
;
5377 struct btrfs_path
*path
;
5378 struct btrfs_fs_info
*info
= root
->fs_info
;
5379 struct btrfs_root
*extent_root
= info
->extent_root
;
5380 struct extent_buffer
*leaf
;
5381 struct btrfs_extent_item
*ei
;
5382 struct btrfs_extent_inline_ref
*iref
;
5385 int extent_slot
= 0;
5386 int found_extent
= 0;
5390 bool skinny_metadata
= btrfs_fs_incompat(root
->fs_info
,
5393 path
= btrfs_alloc_path();
5398 path
->leave_spinning
= 1;
5400 is_data
= owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
;
5401 BUG_ON(!is_data
&& refs_to_drop
!= 1);
5404 skinny_metadata
= 0;
5406 ret
= lookup_extent_backref(trans
, extent_root
, path
, &iref
,
5407 bytenr
, num_bytes
, parent
,
5408 root_objectid
, owner_objectid
,
5411 extent_slot
= path
->slots
[0];
5412 while (extent_slot
>= 0) {
5413 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
5415 if (key
.objectid
!= bytenr
)
5417 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
5418 key
.offset
== num_bytes
) {
5422 if (key
.type
== BTRFS_METADATA_ITEM_KEY
&&
5423 key
.offset
== owner_objectid
) {
5427 if (path
->slots
[0] - extent_slot
> 5)
5431 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5432 item_size
= btrfs_item_size_nr(path
->nodes
[0], extent_slot
);
5433 if (found_extent
&& item_size
< sizeof(*ei
))
5436 if (!found_extent
) {
5438 ret
= remove_extent_backref(trans
, extent_root
, path
,
5442 btrfs_abort_transaction(trans
, extent_root
, ret
);
5445 btrfs_release_path(path
);
5446 path
->leave_spinning
= 1;
5448 key
.objectid
= bytenr
;
5449 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
5450 key
.offset
= num_bytes
;
5452 if (!is_data
&& skinny_metadata
) {
5453 key
.type
= BTRFS_METADATA_ITEM_KEY
;
5454 key
.offset
= owner_objectid
;
5457 ret
= btrfs_search_slot(trans
, extent_root
,
5459 if (ret
> 0 && skinny_metadata
&& path
->slots
[0]) {
5461 * Couldn't find our skinny metadata item,
5462 * see if we have ye olde extent item.
5465 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
5467 if (key
.objectid
== bytenr
&&
5468 key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
5469 key
.offset
== num_bytes
)
5473 if (ret
> 0 && skinny_metadata
) {
5474 skinny_metadata
= false;
5475 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
5476 key
.offset
= num_bytes
;
5477 btrfs_release_path(path
);
5478 ret
= btrfs_search_slot(trans
, extent_root
,
5483 btrfs_err(info
, "umm, got %d back from search, was looking for %llu",
5484 ret
, (unsigned long long)bytenr
);
5486 btrfs_print_leaf(extent_root
,
5490 btrfs_abort_transaction(trans
, extent_root
, ret
);
5493 extent_slot
= path
->slots
[0];
5495 } else if (ret
== -ENOENT
) {
5496 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
5499 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
5500 (unsigned long long)bytenr
,
5501 (unsigned long long)parent
,
5502 (unsigned long long)root_objectid
,
5503 (unsigned long long)owner_objectid
,
5504 (unsigned long long)owner_offset
);
5506 btrfs_abort_transaction(trans
, extent_root
, ret
);
5510 leaf
= path
->nodes
[0];
5511 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
5512 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5513 if (item_size
< sizeof(*ei
)) {
5514 BUG_ON(found_extent
|| extent_slot
!= path
->slots
[0]);
5515 ret
= convert_extent_item_v0(trans
, extent_root
, path
,
5518 btrfs_abort_transaction(trans
, extent_root
, ret
);
5522 btrfs_release_path(path
);
5523 path
->leave_spinning
= 1;
5525 key
.objectid
= bytenr
;
5526 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
5527 key
.offset
= num_bytes
;
5529 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
,
5532 btrfs_err(info
, "umm, got %d back from search, was looking for %llu",
5533 ret
, (unsigned long long)bytenr
);
5534 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
5537 btrfs_abort_transaction(trans
, extent_root
, ret
);
5541 extent_slot
= path
->slots
[0];
5542 leaf
= path
->nodes
[0];
5543 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
5546 BUG_ON(item_size
< sizeof(*ei
));
5547 ei
= btrfs_item_ptr(leaf
, extent_slot
,
5548 struct btrfs_extent_item
);
5549 if (owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
&&
5550 key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
5551 struct btrfs_tree_block_info
*bi
;
5552 BUG_ON(item_size
< sizeof(*ei
) + sizeof(*bi
));
5553 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
5554 WARN_ON(owner_objectid
!= btrfs_tree_block_level(leaf
, bi
));
5557 refs
= btrfs_extent_refs(leaf
, ei
);
5558 if (refs
< refs_to_drop
) {
5559 btrfs_err(info
, "trying to drop %d refs but we only have %Lu "
5560 "for bytenr %Lu\n", refs_to_drop
, refs
, bytenr
);
5562 btrfs_abort_transaction(trans
, extent_root
, ret
);
5565 refs
-= refs_to_drop
;
5569 __run_delayed_extent_op(extent_op
, leaf
, ei
);
5571 * In the case of inline back ref, reference count will
5572 * be updated by remove_extent_backref
5575 BUG_ON(!found_extent
);
5577 btrfs_set_extent_refs(leaf
, ei
, refs
);
5578 btrfs_mark_buffer_dirty(leaf
);
5581 ret
= remove_extent_backref(trans
, extent_root
, path
,
5585 btrfs_abort_transaction(trans
, extent_root
, ret
);
5591 BUG_ON(is_data
&& refs_to_drop
!=
5592 extent_data_ref_count(root
, path
, iref
));
5594 BUG_ON(path
->slots
[0] != extent_slot
);
5596 BUG_ON(path
->slots
[0] != extent_slot
+ 1);
5597 path
->slots
[0] = extent_slot
;
5602 ret
= btrfs_del_items(trans
, extent_root
, path
, path
->slots
[0],
5605 btrfs_abort_transaction(trans
, extent_root
, ret
);
5608 btrfs_release_path(path
);
5611 ret
= btrfs_del_csums(trans
, root
, bytenr
, num_bytes
);
5613 btrfs_abort_transaction(trans
, extent_root
, ret
);
5618 ret
= update_block_group(root
, bytenr
, num_bytes
, 0);
5620 btrfs_abort_transaction(trans
, extent_root
, ret
);
5625 btrfs_free_path(path
);
5630 * when we free an block, it is possible (and likely) that we free the last
5631 * delayed ref for that extent as well. This searches the delayed ref tree for
5632 * a given extent, and if there are no other delayed refs to be processed, it
5633 * removes it from the tree.
5635 static noinline
int check_ref_cleanup(struct btrfs_trans_handle
*trans
,
5636 struct btrfs_root
*root
, u64 bytenr
)
5638 struct btrfs_delayed_ref_head
*head
;
5639 struct btrfs_delayed_ref_root
*delayed_refs
;
5640 struct btrfs_delayed_ref_node
*ref
;
5641 struct rb_node
*node
;
5644 delayed_refs
= &trans
->transaction
->delayed_refs
;
5645 spin_lock(&delayed_refs
->lock
);
5646 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
5650 node
= rb_prev(&head
->node
.rb_node
);
5654 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
5656 /* there are still entries for this ref, we can't drop it */
5657 if (ref
->bytenr
== bytenr
)
5660 if (head
->extent_op
) {
5661 if (!head
->must_insert_reserved
)
5663 btrfs_free_delayed_extent_op(head
->extent_op
);
5664 head
->extent_op
= NULL
;
5668 * waiting for the lock here would deadlock. If someone else has it
5669 * locked they are already in the process of dropping it anyway
5671 if (!mutex_trylock(&head
->mutex
))
5675 * at this point we have a head with no other entries. Go
5676 * ahead and process it.
5678 head
->node
.in_tree
= 0;
5679 rb_erase(&head
->node
.rb_node
, &delayed_refs
->root
);
5681 delayed_refs
->num_entries
--;
5684 * we don't take a ref on the node because we're removing it from the
5685 * tree, so we just steal the ref the tree was holding.
5687 delayed_refs
->num_heads
--;
5688 if (list_empty(&head
->cluster
))
5689 delayed_refs
->num_heads_ready
--;
5691 list_del_init(&head
->cluster
);
5692 spin_unlock(&delayed_refs
->lock
);
5694 BUG_ON(head
->extent_op
);
5695 if (head
->must_insert_reserved
)
5698 mutex_unlock(&head
->mutex
);
5699 btrfs_put_delayed_ref(&head
->node
);
5702 spin_unlock(&delayed_refs
->lock
);
5706 void btrfs_free_tree_block(struct btrfs_trans_handle
*trans
,
5707 struct btrfs_root
*root
,
5708 struct extent_buffer
*buf
,
5709 u64 parent
, int last_ref
)
5711 struct btrfs_block_group_cache
*cache
= NULL
;
5714 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
5715 ret
= btrfs_add_delayed_tree_ref(root
->fs_info
, trans
,
5716 buf
->start
, buf
->len
,
5717 parent
, root
->root_key
.objectid
,
5718 btrfs_header_level(buf
),
5719 BTRFS_DROP_DELAYED_REF
, NULL
, 0);
5720 BUG_ON(ret
); /* -ENOMEM */
5726 cache
= btrfs_lookup_block_group(root
->fs_info
, buf
->start
);
5728 if (btrfs_header_generation(buf
) == trans
->transid
) {
5729 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
5730 ret
= check_ref_cleanup(trans
, root
, buf
->start
);
5735 if (btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
)) {
5736 pin_down_extent(root
, cache
, buf
->start
, buf
->len
, 1);
5740 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY
, &buf
->bflags
));
5742 btrfs_add_free_space(cache
, buf
->start
, buf
->len
);
5743 btrfs_update_reserved_bytes(cache
, buf
->len
, RESERVE_FREE
);
5747 * Deleting the buffer, clear the corrupt flag since it doesn't matter
5750 clear_bit(EXTENT_BUFFER_CORRUPT
, &buf
->bflags
);
5751 btrfs_put_block_group(cache
);
5754 /* Can return -ENOMEM */
5755 int btrfs_free_extent(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
5756 u64 bytenr
, u64 num_bytes
, u64 parent
, u64 root_objectid
,
5757 u64 owner
, u64 offset
, int for_cow
)
5760 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5763 * tree log blocks never actually go into the extent allocation
5764 * tree, just update pinning info and exit early.
5766 if (root_objectid
== BTRFS_TREE_LOG_OBJECTID
) {
5767 WARN_ON(owner
>= BTRFS_FIRST_FREE_OBJECTID
);
5768 /* unlocks the pinned mutex */
5769 btrfs_pin_extent(root
, bytenr
, num_bytes
, 1);
5771 } else if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
5772 ret
= btrfs_add_delayed_tree_ref(fs_info
, trans
, bytenr
,
5774 parent
, root_objectid
, (int)owner
,
5775 BTRFS_DROP_DELAYED_REF
, NULL
, for_cow
);
5777 ret
= btrfs_add_delayed_data_ref(fs_info
, trans
, bytenr
,
5779 parent
, root_objectid
, owner
,
5780 offset
, BTRFS_DROP_DELAYED_REF
,
5786 static u64
stripe_align(struct btrfs_root
*root
,
5787 struct btrfs_block_group_cache
*cache
,
5788 u64 val
, u64 num_bytes
)
5790 u64 ret
= ALIGN(val
, root
->stripesize
);
5795 * when we wait for progress in the block group caching, its because
5796 * our allocation attempt failed at least once. So, we must sleep
5797 * and let some progress happen before we try again.
5799 * This function will sleep at least once waiting for new free space to
5800 * show up, and then it will check the block group free space numbers
5801 * for our min num_bytes. Another option is to have it go ahead
5802 * and look in the rbtree for a free extent of a given size, but this
5806 wait_block_group_cache_progress(struct btrfs_block_group_cache
*cache
,
5809 struct btrfs_caching_control
*caching_ctl
;
5811 caching_ctl
= get_caching_control(cache
);
5815 wait_event(caching_ctl
->wait
, block_group_cache_done(cache
) ||
5816 (cache
->free_space_ctl
->free_space
>= num_bytes
));
5818 put_caching_control(caching_ctl
);
5823 wait_block_group_cache_done(struct btrfs_block_group_cache
*cache
)
5825 struct btrfs_caching_control
*caching_ctl
;
5827 caching_ctl
= get_caching_control(cache
);
5831 wait_event(caching_ctl
->wait
, block_group_cache_done(cache
));
5833 put_caching_control(caching_ctl
);
5837 int __get_raid_index(u64 flags
)
5839 if (flags
& BTRFS_BLOCK_GROUP_RAID10
)
5840 return BTRFS_RAID_RAID10
;
5841 else if (flags
& BTRFS_BLOCK_GROUP_RAID1
)
5842 return BTRFS_RAID_RAID1
;
5843 else if (flags
& BTRFS_BLOCK_GROUP_DUP
)
5844 return BTRFS_RAID_DUP
;
5845 else if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
5846 return BTRFS_RAID_RAID0
;
5847 else if (flags
& BTRFS_BLOCK_GROUP_RAID5
)
5848 return BTRFS_RAID_RAID5
;
5849 else if (flags
& BTRFS_BLOCK_GROUP_RAID6
)
5850 return BTRFS_RAID_RAID6
;
5852 return BTRFS_RAID_SINGLE
; /* BTRFS_BLOCK_GROUP_SINGLE */
5855 static int get_block_group_index(struct btrfs_block_group_cache
*cache
)
5857 return __get_raid_index(cache
->flags
);
5860 enum btrfs_loop_type
{
5861 LOOP_CACHING_NOWAIT
= 0,
5862 LOOP_CACHING_WAIT
= 1,
5863 LOOP_ALLOC_CHUNK
= 2,
5864 LOOP_NO_EMPTY_SIZE
= 3,
5868 * walks the btree of allocated extents and find a hole of a given size.
5869 * The key ins is changed to record the hole:
5870 * ins->objectid == block start
5871 * ins->flags = BTRFS_EXTENT_ITEM_KEY
5872 * ins->offset == number of blocks
5873 * Any available blocks before search_start are skipped.
5875 static noinline
int find_free_extent(struct btrfs_trans_handle
*trans
,
5876 struct btrfs_root
*orig_root
,
5877 u64 num_bytes
, u64 empty_size
,
5878 u64 hint_byte
, struct btrfs_key
*ins
,
5882 struct btrfs_root
*root
= orig_root
->fs_info
->extent_root
;
5883 struct btrfs_free_cluster
*last_ptr
= NULL
;
5884 struct btrfs_block_group_cache
*block_group
= NULL
;
5885 struct btrfs_block_group_cache
*used_block_group
;
5886 u64 search_start
= 0;
5887 int empty_cluster
= 2 * 1024 * 1024;
5888 struct btrfs_space_info
*space_info
;
5890 int index
= __get_raid_index(flags
);
5891 int alloc_type
= (flags
& BTRFS_BLOCK_GROUP_DATA
) ?
5892 RESERVE_ALLOC_NO_ACCOUNT
: RESERVE_ALLOC
;
5893 bool found_uncached_bg
= false;
5894 bool failed_cluster_refill
= false;
5895 bool failed_alloc
= false;
5896 bool use_cluster
= true;
5897 bool have_caching_bg
= false;
5899 WARN_ON(num_bytes
< root
->sectorsize
);
5900 btrfs_set_key_type(ins
, BTRFS_EXTENT_ITEM_KEY
);
5904 trace_find_free_extent(orig_root
, num_bytes
, empty_size
, flags
);
5906 space_info
= __find_space_info(root
->fs_info
, flags
);
5908 btrfs_err(root
->fs_info
, "No space info for %llu", flags
);
5913 * If the space info is for both data and metadata it means we have a
5914 * small filesystem and we can't use the clustering stuff.
5916 if (btrfs_mixed_space_info(space_info
))
5917 use_cluster
= false;
5919 if (flags
& BTRFS_BLOCK_GROUP_METADATA
&& use_cluster
) {
5920 last_ptr
= &root
->fs_info
->meta_alloc_cluster
;
5921 if (!btrfs_test_opt(root
, SSD
))
5922 empty_cluster
= 64 * 1024;
5925 if ((flags
& BTRFS_BLOCK_GROUP_DATA
) && use_cluster
&&
5926 btrfs_test_opt(root
, SSD
)) {
5927 last_ptr
= &root
->fs_info
->data_alloc_cluster
;
5931 spin_lock(&last_ptr
->lock
);
5932 if (last_ptr
->block_group
)
5933 hint_byte
= last_ptr
->window_start
;
5934 spin_unlock(&last_ptr
->lock
);
5937 search_start
= max(search_start
, first_logical_byte(root
, 0));
5938 search_start
= max(search_start
, hint_byte
);
5943 if (search_start
== hint_byte
) {
5944 block_group
= btrfs_lookup_block_group(root
->fs_info
,
5946 used_block_group
= block_group
;
5948 * we don't want to use the block group if it doesn't match our
5949 * allocation bits, or if its not cached.
5951 * However if we are re-searching with an ideal block group
5952 * picked out then we don't care that the block group is cached.
5954 if (block_group
&& block_group_bits(block_group
, flags
) &&
5955 block_group
->cached
!= BTRFS_CACHE_NO
) {
5956 down_read(&space_info
->groups_sem
);
5957 if (list_empty(&block_group
->list
) ||
5960 * someone is removing this block group,
5961 * we can't jump into the have_block_group
5962 * target because our list pointers are not
5965 btrfs_put_block_group(block_group
);
5966 up_read(&space_info
->groups_sem
);
5968 index
= get_block_group_index(block_group
);
5969 goto have_block_group
;
5971 } else if (block_group
) {
5972 btrfs_put_block_group(block_group
);
5976 have_caching_bg
= false;
5977 down_read(&space_info
->groups_sem
);
5978 list_for_each_entry(block_group
, &space_info
->block_groups
[index
],
5983 used_block_group
= block_group
;
5984 btrfs_get_block_group(block_group
);
5985 search_start
= block_group
->key
.objectid
;
5988 * this can happen if we end up cycling through all the
5989 * raid types, but we want to make sure we only allocate
5990 * for the proper type.
5992 if (!block_group_bits(block_group
, flags
)) {
5993 u64 extra
= BTRFS_BLOCK_GROUP_DUP
|
5994 BTRFS_BLOCK_GROUP_RAID1
|
5995 BTRFS_BLOCK_GROUP_RAID5
|
5996 BTRFS_BLOCK_GROUP_RAID6
|
5997 BTRFS_BLOCK_GROUP_RAID10
;
6000 * if they asked for extra copies and this block group
6001 * doesn't provide them, bail. This does allow us to
6002 * fill raid0 from raid1.
6004 if ((flags
& extra
) && !(block_group
->flags
& extra
))
6009 cached
= block_group_cache_done(block_group
);
6010 if (unlikely(!cached
)) {
6011 found_uncached_bg
= true;
6012 ret
= cache_block_group(block_group
, 0);
6017 if (unlikely(block_group
->ro
))
6021 * Ok we want to try and use the cluster allocator, so
6025 unsigned long aligned_cluster
;
6027 * the refill lock keeps out other
6028 * people trying to start a new cluster
6030 spin_lock(&last_ptr
->refill_lock
);
6031 used_block_group
= last_ptr
->block_group
;
6032 if (used_block_group
!= block_group
&&
6033 (!used_block_group
||
6034 used_block_group
->ro
||
6035 !block_group_bits(used_block_group
, flags
))) {
6036 used_block_group
= block_group
;
6037 goto refill_cluster
;
6040 if (used_block_group
!= block_group
)
6041 btrfs_get_block_group(used_block_group
);
6043 offset
= btrfs_alloc_from_cluster(used_block_group
,
6044 last_ptr
, num_bytes
, used_block_group
->key
.objectid
);
6046 /* we have a block, we're done */
6047 spin_unlock(&last_ptr
->refill_lock
);
6048 trace_btrfs_reserve_extent_cluster(root
,
6049 block_group
, search_start
, num_bytes
);
6053 WARN_ON(last_ptr
->block_group
!= used_block_group
);
6054 if (used_block_group
!= block_group
) {
6055 btrfs_put_block_group(used_block_group
);
6056 used_block_group
= block_group
;
6059 BUG_ON(used_block_group
!= block_group
);
6060 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
6061 * set up a new clusters, so lets just skip it
6062 * and let the allocator find whatever block
6063 * it can find. If we reach this point, we
6064 * will have tried the cluster allocator
6065 * plenty of times and not have found
6066 * anything, so we are likely way too
6067 * fragmented for the clustering stuff to find
6070 * However, if the cluster is taken from the
6071 * current block group, release the cluster
6072 * first, so that we stand a better chance of
6073 * succeeding in the unclustered
6075 if (loop
>= LOOP_NO_EMPTY_SIZE
&&
6076 last_ptr
->block_group
!= block_group
) {
6077 spin_unlock(&last_ptr
->refill_lock
);
6078 goto unclustered_alloc
;
6082 * this cluster didn't work out, free it and
6085 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
6087 if (loop
>= LOOP_NO_EMPTY_SIZE
) {
6088 spin_unlock(&last_ptr
->refill_lock
);
6089 goto unclustered_alloc
;
6092 aligned_cluster
= max_t(unsigned long,
6093 empty_cluster
+ empty_size
,
6094 block_group
->full_stripe_len
);
6096 /* allocate a cluster in this block group */
6097 ret
= btrfs_find_space_cluster(trans
, root
,
6098 block_group
, last_ptr
,
6099 search_start
, num_bytes
,
6103 * now pull our allocation out of this
6106 offset
= btrfs_alloc_from_cluster(block_group
,
6107 last_ptr
, num_bytes
,
6110 /* we found one, proceed */
6111 spin_unlock(&last_ptr
->refill_lock
);
6112 trace_btrfs_reserve_extent_cluster(root
,
6113 block_group
, search_start
,
6117 } else if (!cached
&& loop
> LOOP_CACHING_NOWAIT
6118 && !failed_cluster_refill
) {
6119 spin_unlock(&last_ptr
->refill_lock
);
6121 failed_cluster_refill
= true;
6122 wait_block_group_cache_progress(block_group
,
6123 num_bytes
+ empty_cluster
+ empty_size
);
6124 goto have_block_group
;
6128 * at this point we either didn't find a cluster
6129 * or we weren't able to allocate a block from our
6130 * cluster. Free the cluster we've been trying
6131 * to use, and go to the next block group
6133 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
6134 spin_unlock(&last_ptr
->refill_lock
);
6139 spin_lock(&block_group
->free_space_ctl
->tree_lock
);
6141 block_group
->free_space_ctl
->free_space
<
6142 num_bytes
+ empty_cluster
+ empty_size
) {
6143 spin_unlock(&block_group
->free_space_ctl
->tree_lock
);
6146 spin_unlock(&block_group
->free_space_ctl
->tree_lock
);
6148 offset
= btrfs_find_space_for_alloc(block_group
, search_start
,
6149 num_bytes
, empty_size
);
6151 * If we didn't find a chunk, and we haven't failed on this
6152 * block group before, and this block group is in the middle of
6153 * caching and we are ok with waiting, then go ahead and wait
6154 * for progress to be made, and set failed_alloc to true.
6156 * If failed_alloc is true then we've already waited on this
6157 * block group once and should move on to the next block group.
6159 if (!offset
&& !failed_alloc
&& !cached
&&
6160 loop
> LOOP_CACHING_NOWAIT
) {
6161 wait_block_group_cache_progress(block_group
,
6162 num_bytes
+ empty_size
);
6163 failed_alloc
= true;
6164 goto have_block_group
;
6165 } else if (!offset
) {
6167 have_caching_bg
= true;
6171 search_start
= stripe_align(root
, used_block_group
,
6174 /* move on to the next group */
6175 if (search_start
+ num_bytes
>
6176 used_block_group
->key
.objectid
+ used_block_group
->key
.offset
) {
6177 btrfs_add_free_space(used_block_group
, offset
, num_bytes
);
6181 if (offset
< search_start
)
6182 btrfs_add_free_space(used_block_group
, offset
,
6183 search_start
- offset
);
6184 BUG_ON(offset
> search_start
);
6186 ret
= btrfs_update_reserved_bytes(used_block_group
, num_bytes
,
6188 if (ret
== -EAGAIN
) {
6189 btrfs_add_free_space(used_block_group
, offset
, num_bytes
);
6193 /* we are all good, lets return */
6194 ins
->objectid
= search_start
;
6195 ins
->offset
= num_bytes
;
6197 trace_btrfs_reserve_extent(orig_root
, block_group
,
6198 search_start
, num_bytes
);
6199 if (used_block_group
!= block_group
)
6200 btrfs_put_block_group(used_block_group
);
6201 btrfs_put_block_group(block_group
);
6204 failed_cluster_refill
= false;
6205 failed_alloc
= false;
6206 BUG_ON(index
!= get_block_group_index(block_group
));
6207 if (used_block_group
!= block_group
)
6208 btrfs_put_block_group(used_block_group
);
6209 btrfs_put_block_group(block_group
);
6211 up_read(&space_info
->groups_sem
);
6213 if (!ins
->objectid
&& loop
>= LOOP_CACHING_WAIT
&& have_caching_bg
)
6216 if (!ins
->objectid
&& ++index
< BTRFS_NR_RAID_TYPES
)
6220 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
6221 * caching kthreads as we move along
6222 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
6223 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
6224 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
6227 if (!ins
->objectid
&& loop
< LOOP_NO_EMPTY_SIZE
) {
6230 if (loop
== LOOP_ALLOC_CHUNK
) {
6231 ret
= do_chunk_alloc(trans
, root
, flags
,
6234 * Do not bail out on ENOSPC since we
6235 * can do more things.
6237 if (ret
< 0 && ret
!= -ENOSPC
) {
6238 btrfs_abort_transaction(trans
,
6244 if (loop
== LOOP_NO_EMPTY_SIZE
) {
6250 } else if (!ins
->objectid
) {
6252 } else if (ins
->objectid
) {
6260 static void dump_space_info(struct btrfs_space_info
*info
, u64 bytes
,
6261 int dump_block_groups
)
6263 struct btrfs_block_group_cache
*cache
;
6266 spin_lock(&info
->lock
);
6267 printk(KERN_INFO
"space_info %llu has %llu free, is %sfull\n",
6268 (unsigned long long)info
->flags
,
6269 (unsigned long long)(info
->total_bytes
- info
->bytes_used
-
6270 info
->bytes_pinned
- info
->bytes_reserved
-
6271 info
->bytes_readonly
),
6272 (info
->full
) ? "" : "not ");
6273 printk(KERN_INFO
"space_info total=%llu, used=%llu, pinned=%llu, "
6274 "reserved=%llu, may_use=%llu, readonly=%llu\n",
6275 (unsigned long long)info
->total_bytes
,
6276 (unsigned long long)info
->bytes_used
,
6277 (unsigned long long)info
->bytes_pinned
,
6278 (unsigned long long)info
->bytes_reserved
,
6279 (unsigned long long)info
->bytes_may_use
,
6280 (unsigned long long)info
->bytes_readonly
);
6281 spin_unlock(&info
->lock
);
6283 if (!dump_block_groups
)
6286 down_read(&info
->groups_sem
);
6288 list_for_each_entry(cache
, &info
->block_groups
[index
], list
) {
6289 spin_lock(&cache
->lock
);
6290 printk(KERN_INFO
"block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
6291 (unsigned long long)cache
->key
.objectid
,
6292 (unsigned long long)cache
->key
.offset
,
6293 (unsigned long long)btrfs_block_group_used(&cache
->item
),
6294 (unsigned long long)cache
->pinned
,
6295 (unsigned long long)cache
->reserved
,
6296 cache
->ro
? "[readonly]" : "");
6297 btrfs_dump_free_space(cache
, bytes
);
6298 spin_unlock(&cache
->lock
);
6300 if (++index
< BTRFS_NR_RAID_TYPES
)
6302 up_read(&info
->groups_sem
);
6305 int btrfs_reserve_extent(struct btrfs_trans_handle
*trans
,
6306 struct btrfs_root
*root
,
6307 u64 num_bytes
, u64 min_alloc_size
,
6308 u64 empty_size
, u64 hint_byte
,
6309 struct btrfs_key
*ins
, int is_data
)
6311 bool final_tried
= false;
6315 flags
= btrfs_get_alloc_profile(root
, is_data
);
6317 WARN_ON(num_bytes
< root
->sectorsize
);
6318 ret
= find_free_extent(trans
, root
, num_bytes
, empty_size
,
6319 hint_byte
, ins
, flags
);
6321 if (ret
== -ENOSPC
) {
6323 num_bytes
= num_bytes
>> 1;
6324 num_bytes
= round_down(num_bytes
, root
->sectorsize
);
6325 num_bytes
= max(num_bytes
, min_alloc_size
);
6326 if (num_bytes
== min_alloc_size
)
6329 } else if (btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
6330 struct btrfs_space_info
*sinfo
;
6332 sinfo
= __find_space_info(root
->fs_info
, flags
);
6333 btrfs_err(root
->fs_info
, "allocation failed flags %llu, wanted %llu",
6334 (unsigned long long)flags
,
6335 (unsigned long long)num_bytes
);
6337 dump_space_info(sinfo
, num_bytes
, 1);
6341 trace_btrfs_reserved_extent_alloc(root
, ins
->objectid
, ins
->offset
);
6346 static int __btrfs_free_reserved_extent(struct btrfs_root
*root
,
6347 u64 start
, u64 len
, int pin
)
6349 struct btrfs_block_group_cache
*cache
;
6352 cache
= btrfs_lookup_block_group(root
->fs_info
, start
);
6354 btrfs_err(root
->fs_info
, "Unable to find block group for %llu",
6355 (unsigned long long)start
);
6359 if (btrfs_test_opt(root
, DISCARD
))
6360 ret
= btrfs_discard_extent(root
, start
, len
, NULL
);
6363 pin_down_extent(root
, cache
, start
, len
, 1);
6365 btrfs_add_free_space(cache
, start
, len
);
6366 btrfs_update_reserved_bytes(cache
, len
, RESERVE_FREE
);
6368 btrfs_put_block_group(cache
);
6370 trace_btrfs_reserved_extent_free(root
, start
, len
);
6375 int btrfs_free_reserved_extent(struct btrfs_root
*root
,
6378 return __btrfs_free_reserved_extent(root
, start
, len
, 0);
6381 int btrfs_free_and_pin_reserved_extent(struct btrfs_root
*root
,
6384 return __btrfs_free_reserved_extent(root
, start
, len
, 1);
6387 static int alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
6388 struct btrfs_root
*root
,
6389 u64 parent
, u64 root_objectid
,
6390 u64 flags
, u64 owner
, u64 offset
,
6391 struct btrfs_key
*ins
, int ref_mod
)
6394 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
6395 struct btrfs_extent_item
*extent_item
;
6396 struct btrfs_extent_inline_ref
*iref
;
6397 struct btrfs_path
*path
;
6398 struct extent_buffer
*leaf
;
6403 type
= BTRFS_SHARED_DATA_REF_KEY
;
6405 type
= BTRFS_EXTENT_DATA_REF_KEY
;
6407 size
= sizeof(*extent_item
) + btrfs_extent_inline_ref_size(type
);
6409 path
= btrfs_alloc_path();
6413 path
->leave_spinning
= 1;
6414 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
6417 btrfs_free_path(path
);
6421 leaf
= path
->nodes
[0];
6422 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
6423 struct btrfs_extent_item
);
6424 btrfs_set_extent_refs(leaf
, extent_item
, ref_mod
);
6425 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
6426 btrfs_set_extent_flags(leaf
, extent_item
,
6427 flags
| BTRFS_EXTENT_FLAG_DATA
);
6429 iref
= (struct btrfs_extent_inline_ref
*)(extent_item
+ 1);
6430 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
6432 struct btrfs_shared_data_ref
*ref
;
6433 ref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
6434 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
6435 btrfs_set_shared_data_ref_count(leaf
, ref
, ref_mod
);
6437 struct btrfs_extent_data_ref
*ref
;
6438 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
6439 btrfs_set_extent_data_ref_root(leaf
, ref
, root_objectid
);
6440 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
6441 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
6442 btrfs_set_extent_data_ref_count(leaf
, ref
, ref_mod
);
6445 btrfs_mark_buffer_dirty(path
->nodes
[0]);
6446 btrfs_free_path(path
);
6448 ret
= update_block_group(root
, ins
->objectid
, ins
->offset
, 1);
6449 if (ret
) { /* -ENOENT, logic error */
6450 btrfs_err(fs_info
, "update block group failed for %llu %llu",
6451 (unsigned long long)ins
->objectid
,
6452 (unsigned long long)ins
->offset
);
6458 static int alloc_reserved_tree_block(struct btrfs_trans_handle
*trans
,
6459 struct btrfs_root
*root
,
6460 u64 parent
, u64 root_objectid
,
6461 u64 flags
, struct btrfs_disk_key
*key
,
6462 int level
, struct btrfs_key
*ins
)
6465 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
6466 struct btrfs_extent_item
*extent_item
;
6467 struct btrfs_tree_block_info
*block_info
;
6468 struct btrfs_extent_inline_ref
*iref
;
6469 struct btrfs_path
*path
;
6470 struct extent_buffer
*leaf
;
6471 u32 size
= sizeof(*extent_item
) + sizeof(*iref
);
6472 bool skinny_metadata
= btrfs_fs_incompat(root
->fs_info
,
6475 if (!skinny_metadata
)
6476 size
+= sizeof(*block_info
);
6478 path
= btrfs_alloc_path();
6482 path
->leave_spinning
= 1;
6483 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
6486 btrfs_free_path(path
);
6490 leaf
= path
->nodes
[0];
6491 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
6492 struct btrfs_extent_item
);
6493 btrfs_set_extent_refs(leaf
, extent_item
, 1);
6494 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
6495 btrfs_set_extent_flags(leaf
, extent_item
,
6496 flags
| BTRFS_EXTENT_FLAG_TREE_BLOCK
);
6498 if (skinny_metadata
) {
6499 iref
= (struct btrfs_extent_inline_ref
*)(extent_item
+ 1);
6501 block_info
= (struct btrfs_tree_block_info
*)(extent_item
+ 1);
6502 btrfs_set_tree_block_key(leaf
, block_info
, key
);
6503 btrfs_set_tree_block_level(leaf
, block_info
, level
);
6504 iref
= (struct btrfs_extent_inline_ref
*)(block_info
+ 1);
6508 BUG_ON(!(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
));
6509 btrfs_set_extent_inline_ref_type(leaf
, iref
,
6510 BTRFS_SHARED_BLOCK_REF_KEY
);
6511 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
6513 btrfs_set_extent_inline_ref_type(leaf
, iref
,
6514 BTRFS_TREE_BLOCK_REF_KEY
);
6515 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
6518 btrfs_mark_buffer_dirty(leaf
);
6519 btrfs_free_path(path
);
6521 ret
= update_block_group(root
, ins
->objectid
, root
->leafsize
, 1);
6522 if (ret
) { /* -ENOENT, logic error */
6523 btrfs_err(fs_info
, "update block group failed for %llu %llu",
6524 (unsigned long long)ins
->objectid
,
6525 (unsigned long long)ins
->offset
);
6531 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
6532 struct btrfs_root
*root
,
6533 u64 root_objectid
, u64 owner
,
6534 u64 offset
, struct btrfs_key
*ins
)
6538 BUG_ON(root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
6540 ret
= btrfs_add_delayed_data_ref(root
->fs_info
, trans
, ins
->objectid
,
6542 root_objectid
, owner
, offset
,
6543 BTRFS_ADD_DELAYED_EXTENT
, NULL
, 0);
6548 * this is used by the tree logging recovery code. It records that
6549 * an extent has been allocated and makes sure to clear the free
6550 * space cache bits as well
6552 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle
*trans
,
6553 struct btrfs_root
*root
,
6554 u64 root_objectid
, u64 owner
, u64 offset
,
6555 struct btrfs_key
*ins
)
6558 struct btrfs_block_group_cache
*block_group
;
6559 struct btrfs_caching_control
*caching_ctl
;
6560 u64 start
= ins
->objectid
;
6561 u64 num_bytes
= ins
->offset
;
6563 block_group
= btrfs_lookup_block_group(root
->fs_info
, ins
->objectid
);
6564 cache_block_group(block_group
, 0);
6565 caching_ctl
= get_caching_control(block_group
);
6568 BUG_ON(!block_group_cache_done(block_group
));
6569 ret
= btrfs_remove_free_space(block_group
, start
, num_bytes
);
6573 mutex_lock(&caching_ctl
->mutex
);
6575 if (start
>= caching_ctl
->progress
) {
6576 ret
= add_excluded_extent(root
, start
, num_bytes
);
6577 } else if (start
+ num_bytes
<= caching_ctl
->progress
) {
6578 ret
= btrfs_remove_free_space(block_group
,
6581 num_bytes
= caching_ctl
->progress
- start
;
6582 ret
= btrfs_remove_free_space(block_group
,
6587 start
= caching_ctl
->progress
;
6588 num_bytes
= ins
->objectid
+ ins
->offset
-
6589 caching_ctl
->progress
;
6590 ret
= add_excluded_extent(root
, start
, num_bytes
);
6593 mutex_unlock(&caching_ctl
->mutex
);
6594 put_caching_control(caching_ctl
);
6599 ret
= btrfs_update_reserved_bytes(block_group
, ins
->offset
,
6600 RESERVE_ALLOC_NO_ACCOUNT
);
6601 BUG_ON(ret
); /* logic error */
6602 ret
= alloc_reserved_file_extent(trans
, root
, 0, root_objectid
,
6603 0, owner
, offset
, ins
, 1);
6605 btrfs_put_block_group(block_group
);
6609 static struct extent_buffer
*
6610 btrfs_init_new_buffer(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
6611 u64 bytenr
, u32 blocksize
, int level
)
6613 struct extent_buffer
*buf
;
6615 buf
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
6617 return ERR_PTR(-ENOMEM
);
6618 btrfs_set_header_generation(buf
, trans
->transid
);
6619 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, buf
, level
);
6620 btrfs_tree_lock(buf
);
6621 clean_tree_block(trans
, root
, buf
);
6622 clear_bit(EXTENT_BUFFER_STALE
, &buf
->bflags
);
6624 btrfs_set_lock_blocking(buf
);
6625 btrfs_set_buffer_uptodate(buf
);
6627 if (root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
) {
6629 * we allow two log transactions at a time, use different
6630 * EXENT bit to differentiate dirty pages.
6632 if (root
->log_transid
% 2 == 0)
6633 set_extent_dirty(&root
->dirty_log_pages
, buf
->start
,
6634 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
6636 set_extent_new(&root
->dirty_log_pages
, buf
->start
,
6637 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
6639 set_extent_dirty(&trans
->transaction
->dirty_pages
, buf
->start
,
6640 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
6642 trans
->blocks_used
++;
6643 /* this returns a buffer locked for blocking */
6647 static struct btrfs_block_rsv
*
6648 use_block_rsv(struct btrfs_trans_handle
*trans
,
6649 struct btrfs_root
*root
, u32 blocksize
)
6651 struct btrfs_block_rsv
*block_rsv
;
6652 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
6655 block_rsv
= get_block_rsv(trans
, root
);
6657 if (block_rsv
->size
== 0) {
6658 ret
= reserve_metadata_bytes(root
, block_rsv
, blocksize
,
6659 BTRFS_RESERVE_NO_FLUSH
);
6661 * If we couldn't reserve metadata bytes try and use some from
6662 * the global reserve.
6664 if (ret
&& block_rsv
!= global_rsv
) {
6665 ret
= block_rsv_use_bytes(global_rsv
, blocksize
);
6668 return ERR_PTR(ret
);
6670 return ERR_PTR(ret
);
6675 ret
= block_rsv_use_bytes(block_rsv
, blocksize
);
6678 if (ret
&& !block_rsv
->failfast
) {
6679 if (btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
6680 static DEFINE_RATELIMIT_STATE(_rs
,
6681 DEFAULT_RATELIMIT_INTERVAL
* 10,
6682 /*DEFAULT_RATELIMIT_BURST*/ 1);
6683 if (__ratelimit(&_rs
))
6685 "btrfs: block rsv returned %d\n", ret
);
6687 ret
= reserve_metadata_bytes(root
, block_rsv
, blocksize
,
6688 BTRFS_RESERVE_NO_FLUSH
);
6691 } else if (ret
&& block_rsv
!= global_rsv
) {
6692 ret
= block_rsv_use_bytes(global_rsv
, blocksize
);
6698 return ERR_PTR(-ENOSPC
);
6701 static void unuse_block_rsv(struct btrfs_fs_info
*fs_info
,
6702 struct btrfs_block_rsv
*block_rsv
, u32 blocksize
)
6704 block_rsv_add_bytes(block_rsv
, blocksize
, 0);
6705 block_rsv_release_bytes(fs_info
, block_rsv
, NULL
, 0);
6709 * finds a free extent and does all the dirty work required for allocation
6710 * returns the key for the extent through ins, and a tree buffer for
6711 * the first block of the extent through buf.
6713 * returns the tree buffer or NULL.
6715 struct extent_buffer
*btrfs_alloc_free_block(struct btrfs_trans_handle
*trans
,
6716 struct btrfs_root
*root
, u32 blocksize
,
6717 u64 parent
, u64 root_objectid
,
6718 struct btrfs_disk_key
*key
, int level
,
6719 u64 hint
, u64 empty_size
)
6721 struct btrfs_key ins
;
6722 struct btrfs_block_rsv
*block_rsv
;
6723 struct extent_buffer
*buf
;
6726 bool skinny_metadata
= btrfs_fs_incompat(root
->fs_info
,
6729 block_rsv
= use_block_rsv(trans
, root
, blocksize
);
6730 if (IS_ERR(block_rsv
))
6731 return ERR_CAST(block_rsv
);
6733 ret
= btrfs_reserve_extent(trans
, root
, blocksize
, blocksize
,
6734 empty_size
, hint
, &ins
, 0);
6736 unuse_block_rsv(root
->fs_info
, block_rsv
, blocksize
);
6737 return ERR_PTR(ret
);
6740 buf
= btrfs_init_new_buffer(trans
, root
, ins
.objectid
,
6742 BUG_ON(IS_ERR(buf
)); /* -ENOMEM */
6744 if (root_objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
6746 parent
= ins
.objectid
;
6747 flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
6751 if (root_objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
6752 struct btrfs_delayed_extent_op
*extent_op
;
6753 extent_op
= btrfs_alloc_delayed_extent_op();
6754 BUG_ON(!extent_op
); /* -ENOMEM */
6756 memcpy(&extent_op
->key
, key
, sizeof(extent_op
->key
));
6758 memset(&extent_op
->key
, 0, sizeof(extent_op
->key
));
6759 extent_op
->flags_to_set
= flags
;
6760 if (skinny_metadata
)
6761 extent_op
->update_key
= 0;
6763 extent_op
->update_key
= 1;
6764 extent_op
->update_flags
= 1;
6765 extent_op
->is_data
= 0;
6767 ret
= btrfs_add_delayed_tree_ref(root
->fs_info
, trans
,
6769 ins
.offset
, parent
, root_objectid
,
6770 level
, BTRFS_ADD_DELAYED_EXTENT
,
6772 BUG_ON(ret
); /* -ENOMEM */
6777 struct walk_control
{
6778 u64 refs
[BTRFS_MAX_LEVEL
];
6779 u64 flags
[BTRFS_MAX_LEVEL
];
6780 struct btrfs_key update_progress
;
6791 #define DROP_REFERENCE 1
6792 #define UPDATE_BACKREF 2
6794 static noinline
void reada_walk_down(struct btrfs_trans_handle
*trans
,
6795 struct btrfs_root
*root
,
6796 struct walk_control
*wc
,
6797 struct btrfs_path
*path
)
6805 struct btrfs_key key
;
6806 struct extent_buffer
*eb
;
6811 if (path
->slots
[wc
->level
] < wc
->reada_slot
) {
6812 wc
->reada_count
= wc
->reada_count
* 2 / 3;
6813 wc
->reada_count
= max(wc
->reada_count
, 2);
6815 wc
->reada_count
= wc
->reada_count
* 3 / 2;
6816 wc
->reada_count
= min_t(int, wc
->reada_count
,
6817 BTRFS_NODEPTRS_PER_BLOCK(root
));
6820 eb
= path
->nodes
[wc
->level
];
6821 nritems
= btrfs_header_nritems(eb
);
6822 blocksize
= btrfs_level_size(root
, wc
->level
- 1);
6824 for (slot
= path
->slots
[wc
->level
]; slot
< nritems
; slot
++) {
6825 if (nread
>= wc
->reada_count
)
6829 bytenr
= btrfs_node_blockptr(eb
, slot
);
6830 generation
= btrfs_node_ptr_generation(eb
, slot
);
6832 if (slot
== path
->slots
[wc
->level
])
6835 if (wc
->stage
== UPDATE_BACKREF
&&
6836 generation
<= root
->root_key
.offset
)
6839 /* We don't lock the tree block, it's OK to be racy here */
6840 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
,
6841 wc
->level
- 1, 1, &refs
,
6843 /* We don't care about errors in readahead. */
6848 if (wc
->stage
== DROP_REFERENCE
) {
6852 if (wc
->level
== 1 &&
6853 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
6855 if (!wc
->update_ref
||
6856 generation
<= root
->root_key
.offset
)
6858 btrfs_node_key_to_cpu(eb
, &key
, slot
);
6859 ret
= btrfs_comp_cpu_keys(&key
,
6860 &wc
->update_progress
);
6864 if (wc
->level
== 1 &&
6865 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
6869 ret
= readahead_tree_block(root
, bytenr
, blocksize
,
6875 wc
->reada_slot
= slot
;
6879 * helper to process tree block while walking down the tree.
6881 * when wc->stage == UPDATE_BACKREF, this function updates
6882 * back refs for pointers in the block.
6884 * NOTE: return value 1 means we should stop walking down.
6886 static noinline
int walk_down_proc(struct btrfs_trans_handle
*trans
,
6887 struct btrfs_root
*root
,
6888 struct btrfs_path
*path
,
6889 struct walk_control
*wc
, int lookup_info
)
6891 int level
= wc
->level
;
6892 struct extent_buffer
*eb
= path
->nodes
[level
];
6893 u64 flag
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
6896 if (wc
->stage
== UPDATE_BACKREF
&&
6897 btrfs_header_owner(eb
) != root
->root_key
.objectid
)
6901 * when reference count of tree block is 1, it won't increase
6902 * again. once full backref flag is set, we never clear it.
6905 ((wc
->stage
== DROP_REFERENCE
&& wc
->refs
[level
] != 1) ||
6906 (wc
->stage
== UPDATE_BACKREF
&& !(wc
->flags
[level
] & flag
)))) {
6907 BUG_ON(!path
->locks
[level
]);
6908 ret
= btrfs_lookup_extent_info(trans
, root
,
6909 eb
->start
, level
, 1,
6912 BUG_ON(ret
== -ENOMEM
);
6915 BUG_ON(wc
->refs
[level
] == 0);
6918 if (wc
->stage
== DROP_REFERENCE
) {
6919 if (wc
->refs
[level
] > 1)
6922 if (path
->locks
[level
] && !wc
->keep_locks
) {
6923 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
6924 path
->locks
[level
] = 0;
6929 /* wc->stage == UPDATE_BACKREF */
6930 if (!(wc
->flags
[level
] & flag
)) {
6931 BUG_ON(!path
->locks
[level
]);
6932 ret
= btrfs_inc_ref(trans
, root
, eb
, 1, wc
->for_reloc
);
6933 BUG_ON(ret
); /* -ENOMEM */
6934 ret
= btrfs_dec_ref(trans
, root
, eb
, 0, wc
->for_reloc
);
6935 BUG_ON(ret
); /* -ENOMEM */
6936 ret
= btrfs_set_disk_extent_flags(trans
, root
, eb
->start
,
6938 BUG_ON(ret
); /* -ENOMEM */
6939 wc
->flags
[level
] |= flag
;
6943 * the block is shared by multiple trees, so it's not good to
6944 * keep the tree lock
6946 if (path
->locks
[level
] && level
> 0) {
6947 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
6948 path
->locks
[level
] = 0;
6954 * helper to process tree block pointer.
6956 * when wc->stage == DROP_REFERENCE, this function checks
6957 * reference count of the block pointed to. if the block
6958 * is shared and we need update back refs for the subtree
6959 * rooted at the block, this function changes wc->stage to
6960 * UPDATE_BACKREF. if the block is shared and there is no
6961 * need to update back, this function drops the reference
6964 * NOTE: return value 1 means we should stop walking down.
6966 static noinline
int do_walk_down(struct btrfs_trans_handle
*trans
,
6967 struct btrfs_root
*root
,
6968 struct btrfs_path
*path
,
6969 struct walk_control
*wc
, int *lookup_info
)
6975 struct btrfs_key key
;
6976 struct extent_buffer
*next
;
6977 int level
= wc
->level
;
6981 generation
= btrfs_node_ptr_generation(path
->nodes
[level
],
6982 path
->slots
[level
]);
6984 * if the lower level block was created before the snapshot
6985 * was created, we know there is no need to update back refs
6988 if (wc
->stage
== UPDATE_BACKREF
&&
6989 generation
<= root
->root_key
.offset
) {
6994 bytenr
= btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]);
6995 blocksize
= btrfs_level_size(root
, level
- 1);
6997 next
= btrfs_find_tree_block(root
, bytenr
, blocksize
);
6999 next
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
7004 btrfs_tree_lock(next
);
7005 btrfs_set_lock_blocking(next
);
7007 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
, level
- 1, 1,
7008 &wc
->refs
[level
- 1],
7009 &wc
->flags
[level
- 1]);
7011 btrfs_tree_unlock(next
);
7015 if (unlikely(wc
->refs
[level
- 1] == 0)) {
7016 btrfs_err(root
->fs_info
, "Missing references.");
7021 if (wc
->stage
== DROP_REFERENCE
) {
7022 if (wc
->refs
[level
- 1] > 1) {
7024 (wc
->flags
[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF
))
7027 if (!wc
->update_ref
||
7028 generation
<= root
->root_key
.offset
)
7031 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
7032 path
->slots
[level
]);
7033 ret
= btrfs_comp_cpu_keys(&key
, &wc
->update_progress
);
7037 wc
->stage
= UPDATE_BACKREF
;
7038 wc
->shared_level
= level
- 1;
7042 (wc
->flags
[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF
))
7046 if (!btrfs_buffer_uptodate(next
, generation
, 0)) {
7047 btrfs_tree_unlock(next
);
7048 free_extent_buffer(next
);
7054 if (reada
&& level
== 1)
7055 reada_walk_down(trans
, root
, wc
, path
);
7056 next
= read_tree_block(root
, bytenr
, blocksize
, generation
);
7057 if (!next
|| !extent_buffer_uptodate(next
)) {
7058 free_extent_buffer(next
);
7061 btrfs_tree_lock(next
);
7062 btrfs_set_lock_blocking(next
);
7066 BUG_ON(level
!= btrfs_header_level(next
));
7067 path
->nodes
[level
] = next
;
7068 path
->slots
[level
] = 0;
7069 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
7075 wc
->refs
[level
- 1] = 0;
7076 wc
->flags
[level
- 1] = 0;
7077 if (wc
->stage
== DROP_REFERENCE
) {
7078 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
) {
7079 parent
= path
->nodes
[level
]->start
;
7081 BUG_ON(root
->root_key
.objectid
!=
7082 btrfs_header_owner(path
->nodes
[level
]));
7086 ret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
, parent
,
7087 root
->root_key
.objectid
, level
- 1, 0, 0);
7088 BUG_ON(ret
); /* -ENOMEM */
7090 btrfs_tree_unlock(next
);
7091 free_extent_buffer(next
);
7097 * helper to process tree block while walking up the tree.
7099 * when wc->stage == DROP_REFERENCE, this function drops
7100 * reference count on the block.
7102 * when wc->stage == UPDATE_BACKREF, this function changes
7103 * wc->stage back to DROP_REFERENCE if we changed wc->stage
7104 * to UPDATE_BACKREF previously while processing the block.
7106 * NOTE: return value 1 means we should stop walking up.
7108 static noinline
int walk_up_proc(struct btrfs_trans_handle
*trans
,
7109 struct btrfs_root
*root
,
7110 struct btrfs_path
*path
,
7111 struct walk_control
*wc
)
7114 int level
= wc
->level
;
7115 struct extent_buffer
*eb
= path
->nodes
[level
];
7118 if (wc
->stage
== UPDATE_BACKREF
) {
7119 BUG_ON(wc
->shared_level
< level
);
7120 if (level
< wc
->shared_level
)
7123 ret
= find_next_key(path
, level
+ 1, &wc
->update_progress
);
7127 wc
->stage
= DROP_REFERENCE
;
7128 wc
->shared_level
= -1;
7129 path
->slots
[level
] = 0;
7132 * check reference count again if the block isn't locked.
7133 * we should start walking down the tree again if reference
7136 if (!path
->locks
[level
]) {
7138 btrfs_tree_lock(eb
);
7139 btrfs_set_lock_blocking(eb
);
7140 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
7142 ret
= btrfs_lookup_extent_info(trans
, root
,
7143 eb
->start
, level
, 1,
7147 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
7148 path
->locks
[level
] = 0;
7151 BUG_ON(wc
->refs
[level
] == 0);
7152 if (wc
->refs
[level
] == 1) {
7153 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
7154 path
->locks
[level
] = 0;
7160 /* wc->stage == DROP_REFERENCE */
7161 BUG_ON(wc
->refs
[level
] > 1 && !path
->locks
[level
]);
7163 if (wc
->refs
[level
] == 1) {
7165 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
7166 ret
= btrfs_dec_ref(trans
, root
, eb
, 1,
7169 ret
= btrfs_dec_ref(trans
, root
, eb
, 0,
7171 BUG_ON(ret
); /* -ENOMEM */
7173 /* make block locked assertion in clean_tree_block happy */
7174 if (!path
->locks
[level
] &&
7175 btrfs_header_generation(eb
) == trans
->transid
) {
7176 btrfs_tree_lock(eb
);
7177 btrfs_set_lock_blocking(eb
);
7178 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
7180 clean_tree_block(trans
, root
, eb
);
7183 if (eb
== root
->node
) {
7184 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
7187 BUG_ON(root
->root_key
.objectid
!=
7188 btrfs_header_owner(eb
));
7190 if (wc
->flags
[level
+ 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
7191 parent
= path
->nodes
[level
+ 1]->start
;
7193 BUG_ON(root
->root_key
.objectid
!=
7194 btrfs_header_owner(path
->nodes
[level
+ 1]));
7197 btrfs_free_tree_block(trans
, root
, eb
, parent
, wc
->refs
[level
] == 1);
7199 wc
->refs
[level
] = 0;
7200 wc
->flags
[level
] = 0;
7204 static noinline
int walk_down_tree(struct btrfs_trans_handle
*trans
,
7205 struct btrfs_root
*root
,
7206 struct btrfs_path
*path
,
7207 struct walk_control
*wc
)
7209 int level
= wc
->level
;
7210 int lookup_info
= 1;
7213 while (level
>= 0) {
7214 ret
= walk_down_proc(trans
, root
, path
, wc
, lookup_info
);
7221 if (path
->slots
[level
] >=
7222 btrfs_header_nritems(path
->nodes
[level
]))
7225 ret
= do_walk_down(trans
, root
, path
, wc
, &lookup_info
);
7227 path
->slots
[level
]++;
7236 static noinline
int walk_up_tree(struct btrfs_trans_handle
*trans
,
7237 struct btrfs_root
*root
,
7238 struct btrfs_path
*path
,
7239 struct walk_control
*wc
, int max_level
)
7241 int level
= wc
->level
;
7244 path
->slots
[level
] = btrfs_header_nritems(path
->nodes
[level
]);
7245 while (level
< max_level
&& path
->nodes
[level
]) {
7247 if (path
->slots
[level
] + 1 <
7248 btrfs_header_nritems(path
->nodes
[level
])) {
7249 path
->slots
[level
]++;
7252 ret
= walk_up_proc(trans
, root
, path
, wc
);
7256 if (path
->locks
[level
]) {
7257 btrfs_tree_unlock_rw(path
->nodes
[level
],
7258 path
->locks
[level
]);
7259 path
->locks
[level
] = 0;
7261 free_extent_buffer(path
->nodes
[level
]);
7262 path
->nodes
[level
] = NULL
;
7270 * drop a subvolume tree.
7272 * this function traverses the tree freeing any blocks that only
7273 * referenced by the tree.
7275 * when a shared tree block is found. this function decreases its
7276 * reference count by one. if update_ref is true, this function
7277 * also make sure backrefs for the shared block and all lower level
7278 * blocks are properly updated.
7280 * If called with for_reloc == 0, may exit early with -EAGAIN
7282 int btrfs_drop_snapshot(struct btrfs_root
*root
,
7283 struct btrfs_block_rsv
*block_rsv
, int update_ref
,
7286 struct btrfs_path
*path
;
7287 struct btrfs_trans_handle
*trans
;
7288 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
7289 struct btrfs_root_item
*root_item
= &root
->root_item
;
7290 struct walk_control
*wc
;
7291 struct btrfs_key key
;
7296 path
= btrfs_alloc_path();
7302 wc
= kzalloc(sizeof(*wc
), GFP_NOFS
);
7304 btrfs_free_path(path
);
7309 trans
= btrfs_start_transaction(tree_root
, 0);
7310 if (IS_ERR(trans
)) {
7311 err
= PTR_ERR(trans
);
7316 trans
->block_rsv
= block_rsv
;
7318 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
7319 level
= btrfs_header_level(root
->node
);
7320 path
->nodes
[level
] = btrfs_lock_root_node(root
);
7321 btrfs_set_lock_blocking(path
->nodes
[level
]);
7322 path
->slots
[level
] = 0;
7323 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
7324 memset(&wc
->update_progress
, 0,
7325 sizeof(wc
->update_progress
));
7327 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
7328 memcpy(&wc
->update_progress
, &key
,
7329 sizeof(wc
->update_progress
));
7331 level
= root_item
->drop_level
;
7333 path
->lowest_level
= level
;
7334 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
7335 path
->lowest_level
= 0;
7343 * unlock our path, this is safe because only this
7344 * function is allowed to delete this snapshot
7346 btrfs_unlock_up_safe(path
, 0);
7348 level
= btrfs_header_level(root
->node
);
7350 btrfs_tree_lock(path
->nodes
[level
]);
7351 btrfs_set_lock_blocking(path
->nodes
[level
]);
7353 ret
= btrfs_lookup_extent_info(trans
, root
,
7354 path
->nodes
[level
]->start
,
7355 level
, 1, &wc
->refs
[level
],
7361 BUG_ON(wc
->refs
[level
] == 0);
7363 if (level
== root_item
->drop_level
)
7366 btrfs_tree_unlock(path
->nodes
[level
]);
7367 WARN_ON(wc
->refs
[level
] != 1);
7373 wc
->shared_level
= -1;
7374 wc
->stage
= DROP_REFERENCE
;
7375 wc
->update_ref
= update_ref
;
7377 wc
->for_reloc
= for_reloc
;
7378 wc
->reada_count
= BTRFS_NODEPTRS_PER_BLOCK(root
);
7381 if (!for_reloc
&& btrfs_fs_closing(root
->fs_info
)) {
7382 pr_debug("btrfs: drop snapshot early exit\n");
7387 ret
= walk_down_tree(trans
, root
, path
, wc
);
7393 ret
= walk_up_tree(trans
, root
, path
, wc
, BTRFS_MAX_LEVEL
);
7400 BUG_ON(wc
->stage
!= DROP_REFERENCE
);
7404 if (wc
->stage
== DROP_REFERENCE
) {
7406 btrfs_node_key(path
->nodes
[level
],
7407 &root_item
->drop_progress
,
7408 path
->slots
[level
]);
7409 root_item
->drop_level
= level
;
7412 BUG_ON(wc
->level
== 0);
7413 if (btrfs_should_end_transaction(trans
, tree_root
)) {
7414 ret
= btrfs_update_root(trans
, tree_root
,
7418 btrfs_abort_transaction(trans
, tree_root
, ret
);
7423 btrfs_end_transaction_throttle(trans
, tree_root
);
7424 trans
= btrfs_start_transaction(tree_root
, 0);
7425 if (IS_ERR(trans
)) {
7426 err
= PTR_ERR(trans
);
7430 trans
->block_rsv
= block_rsv
;
7433 btrfs_release_path(path
);
7437 ret
= btrfs_del_root(trans
, tree_root
, &root
->root_key
);
7439 btrfs_abort_transaction(trans
, tree_root
, ret
);
7443 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
7444 ret
= btrfs_find_last_root(tree_root
, root
->root_key
.objectid
,
7447 btrfs_abort_transaction(trans
, tree_root
, ret
);
7450 } else if (ret
> 0) {
7451 /* if we fail to delete the orphan item this time
7452 * around, it'll get picked up the next time.
7454 * The most common failure here is just -ENOENT.
7456 btrfs_del_orphan_item(trans
, tree_root
,
7457 root
->root_key
.objectid
);
7461 if (root
->in_radix
) {
7462 btrfs_free_fs_root(tree_root
->fs_info
, root
);
7464 free_extent_buffer(root
->node
);
7465 free_extent_buffer(root
->commit_root
);
7469 btrfs_end_transaction_throttle(trans
, tree_root
);
7472 btrfs_free_path(path
);
7475 btrfs_std_error(root
->fs_info
, err
);
7480 * drop subtree rooted at tree block 'node'.
7482 * NOTE: this function will unlock and release tree block 'node'
7483 * only used by relocation code
7485 int btrfs_drop_subtree(struct btrfs_trans_handle
*trans
,
7486 struct btrfs_root
*root
,
7487 struct extent_buffer
*node
,
7488 struct extent_buffer
*parent
)
7490 struct btrfs_path
*path
;
7491 struct walk_control
*wc
;
7497 BUG_ON(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
7499 path
= btrfs_alloc_path();
7503 wc
= kzalloc(sizeof(*wc
), GFP_NOFS
);
7505 btrfs_free_path(path
);
7509 btrfs_assert_tree_locked(parent
);
7510 parent_level
= btrfs_header_level(parent
);
7511 extent_buffer_get(parent
);
7512 path
->nodes
[parent_level
] = parent
;
7513 path
->slots
[parent_level
] = btrfs_header_nritems(parent
);
7515 btrfs_assert_tree_locked(node
);
7516 level
= btrfs_header_level(node
);
7517 path
->nodes
[level
] = node
;
7518 path
->slots
[level
] = 0;
7519 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
7521 wc
->refs
[parent_level
] = 1;
7522 wc
->flags
[parent_level
] = BTRFS_BLOCK_FLAG_FULL_BACKREF
;
7524 wc
->shared_level
= -1;
7525 wc
->stage
= DROP_REFERENCE
;
7529 wc
->reada_count
= BTRFS_NODEPTRS_PER_BLOCK(root
);
7532 wret
= walk_down_tree(trans
, root
, path
, wc
);
7538 wret
= walk_up_tree(trans
, root
, path
, wc
, parent_level
);
7546 btrfs_free_path(path
);
7550 static u64
update_block_group_flags(struct btrfs_root
*root
, u64 flags
)
7556 * if restripe for this chunk_type is on pick target profile and
7557 * return, otherwise do the usual balance
7559 stripped
= get_restripe_target(root
->fs_info
, flags
);
7561 return extended_to_chunk(stripped
);
7564 * we add in the count of missing devices because we want
7565 * to make sure that any RAID levels on a degraded FS
7566 * continue to be honored.
7568 num_devices
= root
->fs_info
->fs_devices
->rw_devices
+
7569 root
->fs_info
->fs_devices
->missing_devices
;
7571 stripped
= BTRFS_BLOCK_GROUP_RAID0
|
7572 BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
|
7573 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
;
7575 if (num_devices
== 1) {
7576 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
7577 stripped
= flags
& ~stripped
;
7579 /* turn raid0 into single device chunks */
7580 if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
7583 /* turn mirroring into duplication */
7584 if (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
7585 BTRFS_BLOCK_GROUP_RAID10
))
7586 return stripped
| BTRFS_BLOCK_GROUP_DUP
;
7588 /* they already had raid on here, just return */
7589 if (flags
& stripped
)
7592 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
7593 stripped
= flags
& ~stripped
;
7595 /* switch duplicated blocks with raid1 */
7596 if (flags
& BTRFS_BLOCK_GROUP_DUP
)
7597 return stripped
| BTRFS_BLOCK_GROUP_RAID1
;
7599 /* this is drive concat, leave it alone */
7605 static int set_block_group_ro(struct btrfs_block_group_cache
*cache
, int force
)
7607 struct btrfs_space_info
*sinfo
= cache
->space_info
;
7609 u64 min_allocable_bytes
;
7614 * We need some metadata space and system metadata space for
7615 * allocating chunks in some corner cases until we force to set
7616 * it to be readonly.
7619 (BTRFS_BLOCK_GROUP_SYSTEM
| BTRFS_BLOCK_GROUP_METADATA
)) &&
7621 min_allocable_bytes
= 1 * 1024 * 1024;
7623 min_allocable_bytes
= 0;
7625 spin_lock(&sinfo
->lock
);
7626 spin_lock(&cache
->lock
);
7633 num_bytes
= cache
->key
.offset
- cache
->reserved
- cache
->pinned
-
7634 cache
->bytes_super
- btrfs_block_group_used(&cache
->item
);
7636 if (sinfo
->bytes_used
+ sinfo
->bytes_reserved
+ sinfo
->bytes_pinned
+
7637 sinfo
->bytes_may_use
+ sinfo
->bytes_readonly
+ num_bytes
+
7638 min_allocable_bytes
<= sinfo
->total_bytes
) {
7639 sinfo
->bytes_readonly
+= num_bytes
;
7644 spin_unlock(&cache
->lock
);
7645 spin_unlock(&sinfo
->lock
);
7649 int btrfs_set_block_group_ro(struct btrfs_root
*root
,
7650 struct btrfs_block_group_cache
*cache
)
7653 struct btrfs_trans_handle
*trans
;
7659 trans
= btrfs_join_transaction(root
);
7661 return PTR_ERR(trans
);
7663 alloc_flags
= update_block_group_flags(root
, cache
->flags
);
7664 if (alloc_flags
!= cache
->flags
) {
7665 ret
= do_chunk_alloc(trans
, root
, alloc_flags
,
7671 ret
= set_block_group_ro(cache
, 0);
7674 alloc_flags
= get_alloc_profile(root
, cache
->space_info
->flags
);
7675 ret
= do_chunk_alloc(trans
, root
, alloc_flags
,
7679 ret
= set_block_group_ro(cache
, 0);
7681 btrfs_end_transaction(trans
, root
);
7685 int btrfs_force_chunk_alloc(struct btrfs_trans_handle
*trans
,
7686 struct btrfs_root
*root
, u64 type
)
7688 u64 alloc_flags
= get_alloc_profile(root
, type
);
7689 return do_chunk_alloc(trans
, root
, alloc_flags
,
7694 * helper to account the unused space of all the readonly block group in the
7695 * list. takes mirrors into account.
7697 static u64
__btrfs_get_ro_block_group_free_space(struct list_head
*groups_list
)
7699 struct btrfs_block_group_cache
*block_group
;
7703 list_for_each_entry(block_group
, groups_list
, list
) {
7704 spin_lock(&block_group
->lock
);
7706 if (!block_group
->ro
) {
7707 spin_unlock(&block_group
->lock
);
7711 if (block_group
->flags
& (BTRFS_BLOCK_GROUP_RAID1
|
7712 BTRFS_BLOCK_GROUP_RAID10
|
7713 BTRFS_BLOCK_GROUP_DUP
))
7718 free_bytes
+= (block_group
->key
.offset
-
7719 btrfs_block_group_used(&block_group
->item
)) *
7722 spin_unlock(&block_group
->lock
);
7729 * helper to account the unused space of all the readonly block group in the
7730 * space_info. takes mirrors into account.
7732 u64
btrfs_account_ro_block_groups_free_space(struct btrfs_space_info
*sinfo
)
7737 spin_lock(&sinfo
->lock
);
7739 for(i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++)
7740 if (!list_empty(&sinfo
->block_groups
[i
]))
7741 free_bytes
+= __btrfs_get_ro_block_group_free_space(
7742 &sinfo
->block_groups
[i
]);
7744 spin_unlock(&sinfo
->lock
);
7749 void btrfs_set_block_group_rw(struct btrfs_root
*root
,
7750 struct btrfs_block_group_cache
*cache
)
7752 struct btrfs_space_info
*sinfo
= cache
->space_info
;
7757 spin_lock(&sinfo
->lock
);
7758 spin_lock(&cache
->lock
);
7759 num_bytes
= cache
->key
.offset
- cache
->reserved
- cache
->pinned
-
7760 cache
->bytes_super
- btrfs_block_group_used(&cache
->item
);
7761 sinfo
->bytes_readonly
-= num_bytes
;
7763 spin_unlock(&cache
->lock
);
7764 spin_unlock(&sinfo
->lock
);
7768 * checks to see if its even possible to relocate this block group.
7770 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7771 * ok to go ahead and try.
7773 int btrfs_can_relocate(struct btrfs_root
*root
, u64 bytenr
)
7775 struct btrfs_block_group_cache
*block_group
;
7776 struct btrfs_space_info
*space_info
;
7777 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
7778 struct btrfs_device
*device
;
7787 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
7789 /* odd, couldn't find the block group, leave it alone */
7793 min_free
= btrfs_block_group_used(&block_group
->item
);
7795 /* no bytes used, we're good */
7799 space_info
= block_group
->space_info
;
7800 spin_lock(&space_info
->lock
);
7802 full
= space_info
->full
;
7805 * if this is the last block group we have in this space, we can't
7806 * relocate it unless we're able to allocate a new chunk below.
7808 * Otherwise, we need to make sure we have room in the space to handle
7809 * all of the extents from this block group. If we can, we're good
7811 if ((space_info
->total_bytes
!= block_group
->key
.offset
) &&
7812 (space_info
->bytes_used
+ space_info
->bytes_reserved
+
7813 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
7814 min_free
< space_info
->total_bytes
)) {
7815 spin_unlock(&space_info
->lock
);
7818 spin_unlock(&space_info
->lock
);
7821 * ok we don't have enough space, but maybe we have free space on our
7822 * devices to allocate new chunks for relocation, so loop through our
7823 * alloc devices and guess if we have enough space. if this block
7824 * group is going to be restriped, run checks against the target
7825 * profile instead of the current one.
7837 target
= get_restripe_target(root
->fs_info
, block_group
->flags
);
7839 index
= __get_raid_index(extended_to_chunk(target
));
7842 * this is just a balance, so if we were marked as full
7843 * we know there is no space for a new chunk
7848 index
= get_block_group_index(block_group
);
7851 if (index
== BTRFS_RAID_RAID10
) {
7855 } else if (index
== BTRFS_RAID_RAID1
) {
7857 } else if (index
== BTRFS_RAID_DUP
) {
7860 } else if (index
== BTRFS_RAID_RAID0
) {
7861 dev_min
= fs_devices
->rw_devices
;
7862 do_div(min_free
, dev_min
);
7865 mutex_lock(&root
->fs_info
->chunk_mutex
);
7866 list_for_each_entry(device
, &fs_devices
->alloc_list
, dev_alloc_list
) {
7870 * check to make sure we can actually find a chunk with enough
7871 * space to fit our block group in.
7873 if (device
->total_bytes
> device
->bytes_used
+ min_free
&&
7874 !device
->is_tgtdev_for_dev_replace
) {
7875 ret
= find_free_dev_extent(device
, min_free
,
7880 if (dev_nr
>= dev_min
)
7886 mutex_unlock(&root
->fs_info
->chunk_mutex
);
7888 btrfs_put_block_group(block_group
);
7892 static int find_first_block_group(struct btrfs_root
*root
,
7893 struct btrfs_path
*path
, struct btrfs_key
*key
)
7896 struct btrfs_key found_key
;
7897 struct extent_buffer
*leaf
;
7900 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
7905 slot
= path
->slots
[0];
7906 leaf
= path
->nodes
[0];
7907 if (slot
>= btrfs_header_nritems(leaf
)) {
7908 ret
= btrfs_next_leaf(root
, path
);
7915 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
7917 if (found_key
.objectid
>= key
->objectid
&&
7918 found_key
.type
== BTRFS_BLOCK_GROUP_ITEM_KEY
) {
7928 void btrfs_put_block_group_cache(struct btrfs_fs_info
*info
)
7930 struct btrfs_block_group_cache
*block_group
;
7934 struct inode
*inode
;
7936 block_group
= btrfs_lookup_first_block_group(info
, last
);
7937 while (block_group
) {
7938 spin_lock(&block_group
->lock
);
7939 if (block_group
->iref
)
7941 spin_unlock(&block_group
->lock
);
7942 block_group
= next_block_group(info
->tree_root
,
7952 inode
= block_group
->inode
;
7953 block_group
->iref
= 0;
7954 block_group
->inode
= NULL
;
7955 spin_unlock(&block_group
->lock
);
7957 last
= block_group
->key
.objectid
+ block_group
->key
.offset
;
7958 btrfs_put_block_group(block_group
);
7962 int btrfs_free_block_groups(struct btrfs_fs_info
*info
)
7964 struct btrfs_block_group_cache
*block_group
;
7965 struct btrfs_space_info
*space_info
;
7966 struct btrfs_caching_control
*caching_ctl
;
7969 down_write(&info
->extent_commit_sem
);
7970 while (!list_empty(&info
->caching_block_groups
)) {
7971 caching_ctl
= list_entry(info
->caching_block_groups
.next
,
7972 struct btrfs_caching_control
, list
);
7973 list_del(&caching_ctl
->list
);
7974 put_caching_control(caching_ctl
);
7976 up_write(&info
->extent_commit_sem
);
7978 spin_lock(&info
->block_group_cache_lock
);
7979 while ((n
= rb_last(&info
->block_group_cache_tree
)) != NULL
) {
7980 block_group
= rb_entry(n
, struct btrfs_block_group_cache
,
7982 rb_erase(&block_group
->cache_node
,
7983 &info
->block_group_cache_tree
);
7984 spin_unlock(&info
->block_group_cache_lock
);
7986 down_write(&block_group
->space_info
->groups_sem
);
7987 list_del(&block_group
->list
);
7988 up_write(&block_group
->space_info
->groups_sem
);
7990 if (block_group
->cached
== BTRFS_CACHE_STARTED
)
7991 wait_block_group_cache_done(block_group
);
7994 * We haven't cached this block group, which means we could
7995 * possibly have excluded extents on this block group.
7997 if (block_group
->cached
== BTRFS_CACHE_NO
)
7998 free_excluded_extents(info
->extent_root
, block_group
);
8000 btrfs_remove_free_space_cache(block_group
);
8001 btrfs_put_block_group(block_group
);
8003 spin_lock(&info
->block_group_cache_lock
);
8005 spin_unlock(&info
->block_group_cache_lock
);
8007 /* now that all the block groups are freed, go through and
8008 * free all the space_info structs. This is only called during
8009 * the final stages of unmount, and so we know nobody is
8010 * using them. We call synchronize_rcu() once before we start,
8011 * just to be on the safe side.
8015 release_global_block_rsv(info
);
8017 while(!list_empty(&info
->space_info
)) {
8018 space_info
= list_entry(info
->space_info
.next
,
8019 struct btrfs_space_info
,
8021 if (btrfs_test_opt(info
->tree_root
, ENOSPC_DEBUG
)) {
8022 if (space_info
->bytes_pinned
> 0 ||
8023 space_info
->bytes_reserved
> 0 ||
8024 space_info
->bytes_may_use
> 0) {
8026 dump_space_info(space_info
, 0, 0);
8029 list_del(&space_info
->list
);
8035 static void __link_block_group(struct btrfs_space_info
*space_info
,
8036 struct btrfs_block_group_cache
*cache
)
8038 int index
= get_block_group_index(cache
);
8040 down_write(&space_info
->groups_sem
);
8041 list_add_tail(&cache
->list
, &space_info
->block_groups
[index
]);
8042 up_write(&space_info
->groups_sem
);
8045 int btrfs_read_block_groups(struct btrfs_root
*root
)
8047 struct btrfs_path
*path
;
8049 struct btrfs_block_group_cache
*cache
;
8050 struct btrfs_fs_info
*info
= root
->fs_info
;
8051 struct btrfs_space_info
*space_info
;
8052 struct btrfs_key key
;
8053 struct btrfs_key found_key
;
8054 struct extent_buffer
*leaf
;
8058 root
= info
->extent_root
;
8061 btrfs_set_key_type(&key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
8062 path
= btrfs_alloc_path();
8067 cache_gen
= btrfs_super_cache_generation(root
->fs_info
->super_copy
);
8068 if (btrfs_test_opt(root
, SPACE_CACHE
) &&
8069 btrfs_super_generation(root
->fs_info
->super_copy
) != cache_gen
)
8071 if (btrfs_test_opt(root
, CLEAR_CACHE
))
8075 ret
= find_first_block_group(root
, path
, &key
);
8080 leaf
= path
->nodes
[0];
8081 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
8082 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
8087 cache
->free_space_ctl
= kzalloc(sizeof(*cache
->free_space_ctl
),
8089 if (!cache
->free_space_ctl
) {
8095 atomic_set(&cache
->count
, 1);
8096 spin_lock_init(&cache
->lock
);
8097 cache
->fs_info
= info
;
8098 INIT_LIST_HEAD(&cache
->list
);
8099 INIT_LIST_HEAD(&cache
->cluster_list
);
8103 * When we mount with old space cache, we need to
8104 * set BTRFS_DC_CLEAR and set dirty flag.
8106 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
8107 * truncate the old free space cache inode and
8109 * b) Setting 'dirty flag' makes sure that we flush
8110 * the new space cache info onto disk.
8112 cache
->disk_cache_state
= BTRFS_DC_CLEAR
;
8113 if (btrfs_test_opt(root
, SPACE_CACHE
))
8117 read_extent_buffer(leaf
, &cache
->item
,
8118 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
8119 sizeof(cache
->item
));
8120 memcpy(&cache
->key
, &found_key
, sizeof(found_key
));
8122 key
.objectid
= found_key
.objectid
+ found_key
.offset
;
8123 btrfs_release_path(path
);
8124 cache
->flags
= btrfs_block_group_flags(&cache
->item
);
8125 cache
->sectorsize
= root
->sectorsize
;
8126 cache
->full_stripe_len
= btrfs_full_stripe_len(root
,
8127 &root
->fs_info
->mapping_tree
,
8128 found_key
.objectid
);
8129 btrfs_init_free_space_ctl(cache
);
8132 * We need to exclude the super stripes now so that the space
8133 * info has super bytes accounted for, otherwise we'll think
8134 * we have more space than we actually do.
8136 ret
= exclude_super_stripes(root
, cache
);
8139 * We may have excluded something, so call this just in
8142 free_excluded_extents(root
, cache
);
8143 kfree(cache
->free_space_ctl
);
8149 * check for two cases, either we are full, and therefore
8150 * don't need to bother with the caching work since we won't
8151 * find any space, or we are empty, and we can just add all
8152 * the space in and be done with it. This saves us _alot_ of
8153 * time, particularly in the full case.
8155 if (found_key
.offset
== btrfs_block_group_used(&cache
->item
)) {
8156 cache
->last_byte_to_unpin
= (u64
)-1;
8157 cache
->cached
= BTRFS_CACHE_FINISHED
;
8158 free_excluded_extents(root
, cache
);
8159 } else if (btrfs_block_group_used(&cache
->item
) == 0) {
8160 cache
->last_byte_to_unpin
= (u64
)-1;
8161 cache
->cached
= BTRFS_CACHE_FINISHED
;
8162 add_new_free_space(cache
, root
->fs_info
,
8164 found_key
.objectid
+
8166 free_excluded_extents(root
, cache
);
8169 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
8171 btrfs_remove_free_space_cache(cache
);
8172 btrfs_put_block_group(cache
);
8176 ret
= update_space_info(info
, cache
->flags
, found_key
.offset
,
8177 btrfs_block_group_used(&cache
->item
),
8180 btrfs_remove_free_space_cache(cache
);
8181 spin_lock(&info
->block_group_cache_lock
);
8182 rb_erase(&cache
->cache_node
,
8183 &info
->block_group_cache_tree
);
8184 spin_unlock(&info
->block_group_cache_lock
);
8185 btrfs_put_block_group(cache
);
8189 cache
->space_info
= space_info
;
8190 spin_lock(&cache
->space_info
->lock
);
8191 cache
->space_info
->bytes_readonly
+= cache
->bytes_super
;
8192 spin_unlock(&cache
->space_info
->lock
);
8194 __link_block_group(space_info
, cache
);
8196 set_avail_alloc_bits(root
->fs_info
, cache
->flags
);
8197 if (btrfs_chunk_readonly(root
, cache
->key
.objectid
))
8198 set_block_group_ro(cache
, 1);
8201 list_for_each_entry_rcu(space_info
, &root
->fs_info
->space_info
, list
) {
8202 if (!(get_alloc_profile(root
, space_info
->flags
) &
8203 (BTRFS_BLOCK_GROUP_RAID10
|
8204 BTRFS_BLOCK_GROUP_RAID1
|
8205 BTRFS_BLOCK_GROUP_RAID5
|
8206 BTRFS_BLOCK_GROUP_RAID6
|
8207 BTRFS_BLOCK_GROUP_DUP
)))
8210 * avoid allocating from un-mirrored block group if there are
8211 * mirrored block groups.
8213 list_for_each_entry(cache
, &space_info
->block_groups
[3], list
)
8214 set_block_group_ro(cache
, 1);
8215 list_for_each_entry(cache
, &space_info
->block_groups
[4], list
)
8216 set_block_group_ro(cache
, 1);
8219 init_global_block_rsv(info
);
8222 btrfs_free_path(path
);
8226 void btrfs_create_pending_block_groups(struct btrfs_trans_handle
*trans
,
8227 struct btrfs_root
*root
)
8229 struct btrfs_block_group_cache
*block_group
, *tmp
;
8230 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
8231 struct btrfs_block_group_item item
;
8232 struct btrfs_key key
;
8235 list_for_each_entry_safe(block_group
, tmp
, &trans
->new_bgs
,
8237 list_del_init(&block_group
->new_bg_list
);
8242 spin_lock(&block_group
->lock
);
8243 memcpy(&item
, &block_group
->item
, sizeof(item
));
8244 memcpy(&key
, &block_group
->key
, sizeof(key
));
8245 spin_unlock(&block_group
->lock
);
8247 ret
= btrfs_insert_item(trans
, extent_root
, &key
, &item
,
8250 btrfs_abort_transaction(trans
, extent_root
, ret
);
8254 int btrfs_make_block_group(struct btrfs_trans_handle
*trans
,
8255 struct btrfs_root
*root
, u64 bytes_used
,
8256 u64 type
, u64 chunk_objectid
, u64 chunk_offset
,
8260 struct btrfs_root
*extent_root
;
8261 struct btrfs_block_group_cache
*cache
;
8263 extent_root
= root
->fs_info
->extent_root
;
8265 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
8267 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
8270 cache
->free_space_ctl
= kzalloc(sizeof(*cache
->free_space_ctl
),
8272 if (!cache
->free_space_ctl
) {
8277 cache
->key
.objectid
= chunk_offset
;
8278 cache
->key
.offset
= size
;
8279 cache
->key
.type
= BTRFS_BLOCK_GROUP_ITEM_KEY
;
8280 cache
->sectorsize
= root
->sectorsize
;
8281 cache
->fs_info
= root
->fs_info
;
8282 cache
->full_stripe_len
= btrfs_full_stripe_len(root
,
8283 &root
->fs_info
->mapping_tree
,
8286 atomic_set(&cache
->count
, 1);
8287 spin_lock_init(&cache
->lock
);
8288 INIT_LIST_HEAD(&cache
->list
);
8289 INIT_LIST_HEAD(&cache
->cluster_list
);
8290 INIT_LIST_HEAD(&cache
->new_bg_list
);
8292 btrfs_init_free_space_ctl(cache
);
8294 btrfs_set_block_group_used(&cache
->item
, bytes_used
);
8295 btrfs_set_block_group_chunk_objectid(&cache
->item
, chunk_objectid
);
8296 cache
->flags
= type
;
8297 btrfs_set_block_group_flags(&cache
->item
, type
);
8299 cache
->last_byte_to_unpin
= (u64
)-1;
8300 cache
->cached
= BTRFS_CACHE_FINISHED
;
8301 ret
= exclude_super_stripes(root
, cache
);
8304 * We may have excluded something, so call this just in
8307 free_excluded_extents(root
, cache
);
8308 kfree(cache
->free_space_ctl
);
8313 add_new_free_space(cache
, root
->fs_info
, chunk_offset
,
8314 chunk_offset
+ size
);
8316 free_excluded_extents(root
, cache
);
8318 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
8320 btrfs_remove_free_space_cache(cache
);
8321 btrfs_put_block_group(cache
);
8325 ret
= update_space_info(root
->fs_info
, cache
->flags
, size
, bytes_used
,
8326 &cache
->space_info
);
8328 btrfs_remove_free_space_cache(cache
);
8329 spin_lock(&root
->fs_info
->block_group_cache_lock
);
8330 rb_erase(&cache
->cache_node
,
8331 &root
->fs_info
->block_group_cache_tree
);
8332 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
8333 btrfs_put_block_group(cache
);
8336 update_global_block_rsv(root
->fs_info
);
8338 spin_lock(&cache
->space_info
->lock
);
8339 cache
->space_info
->bytes_readonly
+= cache
->bytes_super
;
8340 spin_unlock(&cache
->space_info
->lock
);
8342 __link_block_group(cache
->space_info
, cache
);
8344 list_add_tail(&cache
->new_bg_list
, &trans
->new_bgs
);
8346 set_avail_alloc_bits(extent_root
->fs_info
, type
);
8351 static void clear_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
8353 u64 extra_flags
= chunk_to_extended(flags
) &
8354 BTRFS_EXTENDED_PROFILE_MASK
;
8356 write_seqlock(&fs_info
->profiles_lock
);
8357 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
8358 fs_info
->avail_data_alloc_bits
&= ~extra_flags
;
8359 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
8360 fs_info
->avail_metadata_alloc_bits
&= ~extra_flags
;
8361 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
8362 fs_info
->avail_system_alloc_bits
&= ~extra_flags
;
8363 write_sequnlock(&fs_info
->profiles_lock
);
8366 int btrfs_remove_block_group(struct btrfs_trans_handle
*trans
,
8367 struct btrfs_root
*root
, u64 group_start
)
8369 struct btrfs_path
*path
;
8370 struct btrfs_block_group_cache
*block_group
;
8371 struct btrfs_free_cluster
*cluster
;
8372 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
8373 struct btrfs_key key
;
8374 struct inode
*inode
;
8379 root
= root
->fs_info
->extent_root
;
8381 block_group
= btrfs_lookup_block_group(root
->fs_info
, group_start
);
8382 BUG_ON(!block_group
);
8383 BUG_ON(!block_group
->ro
);
8386 * Free the reserved super bytes from this block group before
8389 free_excluded_extents(root
, block_group
);
8391 memcpy(&key
, &block_group
->key
, sizeof(key
));
8392 index
= get_block_group_index(block_group
);
8393 if (block_group
->flags
& (BTRFS_BLOCK_GROUP_DUP
|
8394 BTRFS_BLOCK_GROUP_RAID1
|
8395 BTRFS_BLOCK_GROUP_RAID10
))
8400 /* make sure this block group isn't part of an allocation cluster */
8401 cluster
= &root
->fs_info
->data_alloc_cluster
;
8402 spin_lock(&cluster
->refill_lock
);
8403 btrfs_return_cluster_to_free_space(block_group
, cluster
);
8404 spin_unlock(&cluster
->refill_lock
);
8407 * make sure this block group isn't part of a metadata
8408 * allocation cluster
8410 cluster
= &root
->fs_info
->meta_alloc_cluster
;
8411 spin_lock(&cluster
->refill_lock
);
8412 btrfs_return_cluster_to_free_space(block_group
, cluster
);
8413 spin_unlock(&cluster
->refill_lock
);
8415 path
= btrfs_alloc_path();
8421 inode
= lookup_free_space_inode(tree_root
, block_group
, path
);
8422 if (!IS_ERR(inode
)) {
8423 ret
= btrfs_orphan_add(trans
, inode
);
8425 btrfs_add_delayed_iput(inode
);
8429 /* One for the block groups ref */
8430 spin_lock(&block_group
->lock
);
8431 if (block_group
->iref
) {
8432 block_group
->iref
= 0;
8433 block_group
->inode
= NULL
;
8434 spin_unlock(&block_group
->lock
);
8437 spin_unlock(&block_group
->lock
);
8439 /* One for our lookup ref */
8440 btrfs_add_delayed_iput(inode
);
8443 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
8444 key
.offset
= block_group
->key
.objectid
;
8447 ret
= btrfs_search_slot(trans
, tree_root
, &key
, path
, -1, 1);
8451 btrfs_release_path(path
);
8453 ret
= btrfs_del_item(trans
, tree_root
, path
);
8456 btrfs_release_path(path
);
8459 spin_lock(&root
->fs_info
->block_group_cache_lock
);
8460 rb_erase(&block_group
->cache_node
,
8461 &root
->fs_info
->block_group_cache_tree
);
8463 if (root
->fs_info
->first_logical_byte
== block_group
->key
.objectid
)
8464 root
->fs_info
->first_logical_byte
= (u64
)-1;
8465 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
8467 down_write(&block_group
->space_info
->groups_sem
);
8469 * we must use list_del_init so people can check to see if they
8470 * are still on the list after taking the semaphore
8472 list_del_init(&block_group
->list
);
8473 if (list_empty(&block_group
->space_info
->block_groups
[index
]))
8474 clear_avail_alloc_bits(root
->fs_info
, block_group
->flags
);
8475 up_write(&block_group
->space_info
->groups_sem
);
8477 if (block_group
->cached
== BTRFS_CACHE_STARTED
)
8478 wait_block_group_cache_done(block_group
);
8480 btrfs_remove_free_space_cache(block_group
);
8482 spin_lock(&block_group
->space_info
->lock
);
8483 block_group
->space_info
->total_bytes
-= block_group
->key
.offset
;
8484 block_group
->space_info
->bytes_readonly
-= block_group
->key
.offset
;
8485 block_group
->space_info
->disk_total
-= block_group
->key
.offset
* factor
;
8486 spin_unlock(&block_group
->space_info
->lock
);
8488 memcpy(&key
, &block_group
->key
, sizeof(key
));
8490 btrfs_clear_space_info_full(root
->fs_info
);
8492 btrfs_put_block_group(block_group
);
8493 btrfs_put_block_group(block_group
);
8495 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
8501 ret
= btrfs_del_item(trans
, root
, path
);
8503 btrfs_free_path(path
);
8507 int btrfs_init_space_info(struct btrfs_fs_info
*fs_info
)
8509 struct btrfs_space_info
*space_info
;
8510 struct btrfs_super_block
*disk_super
;
8516 disk_super
= fs_info
->super_copy
;
8517 if (!btrfs_super_root(disk_super
))
8520 features
= btrfs_super_incompat_flags(disk_super
);
8521 if (features
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
8524 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
8525 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
8530 flags
= BTRFS_BLOCK_GROUP_METADATA
| BTRFS_BLOCK_GROUP_DATA
;
8531 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
8533 flags
= BTRFS_BLOCK_GROUP_METADATA
;
8534 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
8538 flags
= BTRFS_BLOCK_GROUP_DATA
;
8539 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
8545 int btrfs_error_unpin_extent_range(struct btrfs_root
*root
, u64 start
, u64 end
)
8547 return unpin_extent_range(root
, start
, end
);
8550 int btrfs_error_discard_extent(struct btrfs_root
*root
, u64 bytenr
,
8551 u64 num_bytes
, u64
*actual_bytes
)
8553 return btrfs_discard_extent(root
, bytenr
, num_bytes
, actual_bytes
);
8556 int btrfs_trim_fs(struct btrfs_root
*root
, struct fstrim_range
*range
)
8558 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
8559 struct btrfs_block_group_cache
*cache
= NULL
;
8564 u64 total_bytes
= btrfs_super_total_bytes(fs_info
->super_copy
);
8568 * try to trim all FS space, our block group may start from non-zero.
8570 if (range
->len
== total_bytes
)
8571 cache
= btrfs_lookup_first_block_group(fs_info
, range
->start
);
8573 cache
= btrfs_lookup_block_group(fs_info
, range
->start
);
8576 if (cache
->key
.objectid
>= (range
->start
+ range
->len
)) {
8577 btrfs_put_block_group(cache
);
8581 start
= max(range
->start
, cache
->key
.objectid
);
8582 end
= min(range
->start
+ range
->len
,
8583 cache
->key
.objectid
+ cache
->key
.offset
);
8585 if (end
- start
>= range
->minlen
) {
8586 if (!block_group_cache_done(cache
)) {
8587 ret
= cache_block_group(cache
, 0);
8589 wait_block_group_cache_done(cache
);
8591 ret
= btrfs_trim_block_group(cache
,
8597 trimmed
+= group_trimmed
;
8599 btrfs_put_block_group(cache
);
8604 cache
= next_block_group(fs_info
->tree_root
, cache
);
8607 range
->len
= trimmed
;