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"
35 #include "free-space-cache.h"
38 #undef SCRAMBLE_DELAYED_REFS
41 * control flags for do_chunk_alloc's force field
42 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
43 * if we really need one.
45 * CHUNK_ALLOC_LIMITED means to only try and allocate one
46 * if we have very few chunks already allocated. This is
47 * used as part of the clustering code to help make sure
48 * we have a good pool of storage to cluster in, without
49 * filling the FS with empty chunks
51 * CHUNK_ALLOC_FORCE means it must try to allocate one
55 CHUNK_ALLOC_NO_FORCE
= 0,
56 CHUNK_ALLOC_LIMITED
= 1,
57 CHUNK_ALLOC_FORCE
= 2,
61 * Control how reservations are dealt with.
63 * RESERVE_FREE - freeing a reservation.
64 * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for
66 * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update
67 * bytes_may_use as the ENOSPC accounting is done elsewhere
72 RESERVE_ALLOC_NO_ACCOUNT
= 2,
75 static int update_block_group(struct btrfs_trans_handle
*trans
,
76 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
);
108 block_group_cache_done(struct btrfs_block_group_cache
*cache
)
111 return cache
->cached
== BTRFS_CACHE_FINISHED
;
114 static int block_group_bits(struct btrfs_block_group_cache
*cache
, u64 bits
)
116 return (cache
->flags
& bits
) == bits
;
119 static void btrfs_get_block_group(struct btrfs_block_group_cache
*cache
)
121 atomic_inc(&cache
->count
);
124 void btrfs_put_block_group(struct btrfs_block_group_cache
*cache
)
126 if (atomic_dec_and_test(&cache
->count
)) {
127 WARN_ON(cache
->pinned
> 0);
128 WARN_ON(cache
->reserved
> 0);
129 kfree(cache
->free_space_ctl
);
135 * this adds the block group to the fs_info rb tree for the block group
138 static int btrfs_add_block_group_cache(struct btrfs_fs_info
*info
,
139 struct btrfs_block_group_cache
*block_group
)
142 struct rb_node
*parent
= NULL
;
143 struct btrfs_block_group_cache
*cache
;
145 spin_lock(&info
->block_group_cache_lock
);
146 p
= &info
->block_group_cache_tree
.rb_node
;
150 cache
= rb_entry(parent
, struct btrfs_block_group_cache
,
152 if (block_group
->key
.objectid
< cache
->key
.objectid
) {
154 } else if (block_group
->key
.objectid
> cache
->key
.objectid
) {
157 spin_unlock(&info
->block_group_cache_lock
);
162 rb_link_node(&block_group
->cache_node
, parent
, p
);
163 rb_insert_color(&block_group
->cache_node
,
164 &info
->block_group_cache_tree
);
165 spin_unlock(&info
->block_group_cache_lock
);
171 * This will return the block group at or after bytenr if contains is 0, else
172 * it will return the block group that contains the bytenr
174 static struct btrfs_block_group_cache
*
175 block_group_cache_tree_search(struct btrfs_fs_info
*info
, u64 bytenr
,
178 struct btrfs_block_group_cache
*cache
, *ret
= NULL
;
182 spin_lock(&info
->block_group_cache_lock
);
183 n
= info
->block_group_cache_tree
.rb_node
;
186 cache
= rb_entry(n
, struct btrfs_block_group_cache
,
188 end
= cache
->key
.objectid
+ cache
->key
.offset
- 1;
189 start
= cache
->key
.objectid
;
191 if (bytenr
< start
) {
192 if (!contains
&& (!ret
|| start
< ret
->key
.objectid
))
195 } else if (bytenr
> start
) {
196 if (contains
&& bytenr
<= end
) {
207 btrfs_get_block_group(ret
);
208 spin_unlock(&info
->block_group_cache_lock
);
213 static int add_excluded_extent(struct btrfs_root
*root
,
214 u64 start
, u64 num_bytes
)
216 u64 end
= start
+ num_bytes
- 1;
217 set_extent_bits(&root
->fs_info
->freed_extents
[0],
218 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
219 set_extent_bits(&root
->fs_info
->freed_extents
[1],
220 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
224 static void free_excluded_extents(struct btrfs_root
*root
,
225 struct btrfs_block_group_cache
*cache
)
229 start
= cache
->key
.objectid
;
230 end
= start
+ cache
->key
.offset
- 1;
232 clear_extent_bits(&root
->fs_info
->freed_extents
[0],
233 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
234 clear_extent_bits(&root
->fs_info
->freed_extents
[1],
235 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
238 static int exclude_super_stripes(struct btrfs_root
*root
,
239 struct btrfs_block_group_cache
*cache
)
246 if (cache
->key
.objectid
< BTRFS_SUPER_INFO_OFFSET
) {
247 stripe_len
= BTRFS_SUPER_INFO_OFFSET
- cache
->key
.objectid
;
248 cache
->bytes_super
+= stripe_len
;
249 ret
= add_excluded_extent(root
, cache
->key
.objectid
,
251 BUG_ON(ret
); /* -ENOMEM */
254 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
255 bytenr
= btrfs_sb_offset(i
);
256 ret
= btrfs_rmap_block(&root
->fs_info
->mapping_tree
,
257 cache
->key
.objectid
, bytenr
,
258 0, &logical
, &nr
, &stripe_len
);
259 BUG_ON(ret
); /* -ENOMEM */
262 cache
->bytes_super
+= stripe_len
;
263 ret
= add_excluded_extent(root
, logical
[nr
],
265 BUG_ON(ret
); /* -ENOMEM */
273 static struct btrfs_caching_control
*
274 get_caching_control(struct btrfs_block_group_cache
*cache
)
276 struct btrfs_caching_control
*ctl
;
278 spin_lock(&cache
->lock
);
279 if (cache
->cached
!= BTRFS_CACHE_STARTED
) {
280 spin_unlock(&cache
->lock
);
284 /* We're loading it the fast way, so we don't have a caching_ctl. */
285 if (!cache
->caching_ctl
) {
286 spin_unlock(&cache
->lock
);
290 ctl
= cache
->caching_ctl
;
291 atomic_inc(&ctl
->count
);
292 spin_unlock(&cache
->lock
);
296 static void put_caching_control(struct btrfs_caching_control
*ctl
)
298 if (atomic_dec_and_test(&ctl
->count
))
303 * this is only called by cache_block_group, since we could have freed extents
304 * we need to check the pinned_extents for any extents that can't be used yet
305 * since their free space will be released as soon as the transaction commits.
307 static u64
add_new_free_space(struct btrfs_block_group_cache
*block_group
,
308 struct btrfs_fs_info
*info
, u64 start
, u64 end
)
310 u64 extent_start
, extent_end
, size
, total_added
= 0;
313 while (start
< end
) {
314 ret
= find_first_extent_bit(info
->pinned_extents
, start
,
315 &extent_start
, &extent_end
,
316 EXTENT_DIRTY
| EXTENT_UPTODATE
,
321 if (extent_start
<= start
) {
322 start
= extent_end
+ 1;
323 } else if (extent_start
> start
&& extent_start
< end
) {
324 size
= extent_start
- start
;
326 ret
= btrfs_add_free_space(block_group
, start
,
328 BUG_ON(ret
); /* -ENOMEM or logic error */
329 start
= extent_end
+ 1;
338 ret
= btrfs_add_free_space(block_group
, start
, size
);
339 BUG_ON(ret
); /* -ENOMEM or logic error */
345 static noinline
void caching_thread(struct btrfs_work
*work
)
347 struct btrfs_block_group_cache
*block_group
;
348 struct btrfs_fs_info
*fs_info
;
349 struct btrfs_caching_control
*caching_ctl
;
350 struct btrfs_root
*extent_root
;
351 struct btrfs_path
*path
;
352 struct extent_buffer
*leaf
;
353 struct btrfs_key key
;
359 caching_ctl
= container_of(work
, struct btrfs_caching_control
, work
);
360 block_group
= caching_ctl
->block_group
;
361 fs_info
= block_group
->fs_info
;
362 extent_root
= fs_info
->extent_root
;
364 path
= btrfs_alloc_path();
368 last
= max_t(u64
, block_group
->key
.objectid
, BTRFS_SUPER_INFO_OFFSET
);
371 * We don't want to deadlock with somebody trying to allocate a new
372 * extent for the extent root while also trying to search the extent
373 * root to add free space. So we skip locking and search the commit
374 * root, since its read-only
376 path
->skip_locking
= 1;
377 path
->search_commit_root
= 1;
382 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
384 mutex_lock(&caching_ctl
->mutex
);
385 /* need to make sure the commit_root doesn't disappear */
386 down_read(&fs_info
->extent_commit_sem
);
388 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
392 leaf
= path
->nodes
[0];
393 nritems
= btrfs_header_nritems(leaf
);
396 if (btrfs_fs_closing(fs_info
) > 1) {
401 if (path
->slots
[0] < nritems
) {
402 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
404 ret
= find_next_key(path
, 0, &key
);
408 if (need_resched() ||
409 btrfs_next_leaf(extent_root
, path
)) {
410 caching_ctl
->progress
= last
;
411 btrfs_release_path(path
);
412 up_read(&fs_info
->extent_commit_sem
);
413 mutex_unlock(&caching_ctl
->mutex
);
417 leaf
= path
->nodes
[0];
418 nritems
= btrfs_header_nritems(leaf
);
422 if (key
.objectid
< block_group
->key
.objectid
) {
427 if (key
.objectid
>= block_group
->key
.objectid
+
428 block_group
->key
.offset
)
431 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
432 total_found
+= add_new_free_space(block_group
,
435 last
= key
.objectid
+ key
.offset
;
437 if (total_found
> (1024 * 1024 * 2)) {
439 wake_up(&caching_ctl
->wait
);
446 total_found
+= add_new_free_space(block_group
, fs_info
, last
,
447 block_group
->key
.objectid
+
448 block_group
->key
.offset
);
449 caching_ctl
->progress
= (u64
)-1;
451 spin_lock(&block_group
->lock
);
452 block_group
->caching_ctl
= NULL
;
453 block_group
->cached
= BTRFS_CACHE_FINISHED
;
454 spin_unlock(&block_group
->lock
);
457 btrfs_free_path(path
);
458 up_read(&fs_info
->extent_commit_sem
);
460 free_excluded_extents(extent_root
, block_group
);
462 mutex_unlock(&caching_ctl
->mutex
);
464 wake_up(&caching_ctl
->wait
);
466 put_caching_control(caching_ctl
);
467 btrfs_put_block_group(block_group
);
470 static int cache_block_group(struct btrfs_block_group_cache
*cache
,
471 struct btrfs_trans_handle
*trans
,
472 struct btrfs_root
*root
,
476 struct btrfs_fs_info
*fs_info
= cache
->fs_info
;
477 struct btrfs_caching_control
*caching_ctl
;
480 caching_ctl
= kzalloc(sizeof(*caching_ctl
), GFP_NOFS
);
484 INIT_LIST_HEAD(&caching_ctl
->list
);
485 mutex_init(&caching_ctl
->mutex
);
486 init_waitqueue_head(&caching_ctl
->wait
);
487 caching_ctl
->block_group
= cache
;
488 caching_ctl
->progress
= cache
->key
.objectid
;
489 atomic_set(&caching_ctl
->count
, 1);
490 caching_ctl
->work
.func
= caching_thread
;
492 spin_lock(&cache
->lock
);
494 * This should be a rare occasion, but this could happen I think in the
495 * case where one thread starts to load the space cache info, and then
496 * some other thread starts a transaction commit which tries to do an
497 * allocation while the other thread is still loading the space cache
498 * info. The previous loop should have kept us from choosing this block
499 * group, but if we've moved to the state where we will wait on caching
500 * block groups we need to first check if we're doing a fast load here,
501 * so we can wait for it to finish, otherwise we could end up allocating
502 * from a block group who's cache gets evicted for one reason or
505 while (cache
->cached
== BTRFS_CACHE_FAST
) {
506 struct btrfs_caching_control
*ctl
;
508 ctl
= cache
->caching_ctl
;
509 atomic_inc(&ctl
->count
);
510 prepare_to_wait(&ctl
->wait
, &wait
, TASK_UNINTERRUPTIBLE
);
511 spin_unlock(&cache
->lock
);
515 finish_wait(&ctl
->wait
, &wait
);
516 put_caching_control(ctl
);
517 spin_lock(&cache
->lock
);
520 if (cache
->cached
!= BTRFS_CACHE_NO
) {
521 spin_unlock(&cache
->lock
);
525 WARN_ON(cache
->caching_ctl
);
526 cache
->caching_ctl
= caching_ctl
;
527 cache
->cached
= BTRFS_CACHE_FAST
;
528 spin_unlock(&cache
->lock
);
531 * We can't do the read from on-disk cache during a commit since we need
532 * to have the normal tree locking. Also if we are currently trying to
533 * allocate blocks for the tree root we can't do the fast caching since
534 * we likely hold important locks.
536 if (fs_info
->mount_opt
& BTRFS_MOUNT_SPACE_CACHE
) {
537 ret
= load_free_space_cache(fs_info
, cache
);
539 spin_lock(&cache
->lock
);
541 cache
->caching_ctl
= NULL
;
542 cache
->cached
= BTRFS_CACHE_FINISHED
;
543 cache
->last_byte_to_unpin
= (u64
)-1;
545 if (load_cache_only
) {
546 cache
->caching_ctl
= NULL
;
547 cache
->cached
= BTRFS_CACHE_NO
;
549 cache
->cached
= BTRFS_CACHE_STARTED
;
552 spin_unlock(&cache
->lock
);
553 wake_up(&caching_ctl
->wait
);
555 put_caching_control(caching_ctl
);
556 free_excluded_extents(fs_info
->extent_root
, cache
);
561 * We are not going to do the fast caching, set cached to the
562 * appropriate value and wakeup any waiters.
564 spin_lock(&cache
->lock
);
565 if (load_cache_only
) {
566 cache
->caching_ctl
= NULL
;
567 cache
->cached
= BTRFS_CACHE_NO
;
569 cache
->cached
= BTRFS_CACHE_STARTED
;
571 spin_unlock(&cache
->lock
);
572 wake_up(&caching_ctl
->wait
);
575 if (load_cache_only
) {
576 put_caching_control(caching_ctl
);
580 down_write(&fs_info
->extent_commit_sem
);
581 atomic_inc(&caching_ctl
->count
);
582 list_add_tail(&caching_ctl
->list
, &fs_info
->caching_block_groups
);
583 up_write(&fs_info
->extent_commit_sem
);
585 btrfs_get_block_group(cache
);
587 btrfs_queue_worker(&fs_info
->caching_workers
, &caching_ctl
->work
);
593 * return the block group that starts at or after bytenr
595 static struct btrfs_block_group_cache
*
596 btrfs_lookup_first_block_group(struct btrfs_fs_info
*info
, u64 bytenr
)
598 struct btrfs_block_group_cache
*cache
;
600 cache
= block_group_cache_tree_search(info
, bytenr
, 0);
606 * return the block group that contains the given bytenr
608 struct btrfs_block_group_cache
*btrfs_lookup_block_group(
609 struct btrfs_fs_info
*info
,
612 struct btrfs_block_group_cache
*cache
;
614 cache
= block_group_cache_tree_search(info
, bytenr
, 1);
619 static struct btrfs_space_info
*__find_space_info(struct btrfs_fs_info
*info
,
622 struct list_head
*head
= &info
->space_info
;
623 struct btrfs_space_info
*found
;
625 flags
&= BTRFS_BLOCK_GROUP_TYPE_MASK
;
628 list_for_each_entry_rcu(found
, head
, list
) {
629 if (found
->flags
& flags
) {
639 * after adding space to the filesystem, we need to clear the full flags
640 * on all the space infos.
642 void btrfs_clear_space_info_full(struct btrfs_fs_info
*info
)
644 struct list_head
*head
= &info
->space_info
;
645 struct btrfs_space_info
*found
;
648 list_for_each_entry_rcu(found
, head
, list
)
653 u64
btrfs_find_block_group(struct btrfs_root
*root
,
654 u64 search_start
, u64 search_hint
, int owner
)
656 struct btrfs_block_group_cache
*cache
;
658 u64 last
= max(search_hint
, search_start
);
665 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
669 spin_lock(&cache
->lock
);
670 last
= cache
->key
.objectid
+ cache
->key
.offset
;
671 used
= btrfs_block_group_used(&cache
->item
);
673 if ((full_search
|| !cache
->ro
) &&
674 block_group_bits(cache
, BTRFS_BLOCK_GROUP_METADATA
)) {
675 if (used
+ cache
->pinned
+ cache
->reserved
<
676 div_factor(cache
->key
.offset
, factor
)) {
677 group_start
= cache
->key
.objectid
;
678 spin_unlock(&cache
->lock
);
679 btrfs_put_block_group(cache
);
683 spin_unlock(&cache
->lock
);
684 btrfs_put_block_group(cache
);
692 if (!full_search
&& factor
< 10) {
702 /* simple helper to search for an existing extent at a given offset */
703 int btrfs_lookup_extent(struct btrfs_root
*root
, u64 start
, u64 len
)
706 struct btrfs_key key
;
707 struct btrfs_path
*path
;
709 path
= btrfs_alloc_path();
713 key
.objectid
= start
;
715 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
716 ret
= btrfs_search_slot(NULL
, root
->fs_info
->extent_root
, &key
, path
,
718 btrfs_free_path(path
);
723 * helper function to lookup reference count and flags of extent.
725 * the head node for delayed ref is used to store the sum of all the
726 * reference count modifications queued up in the rbtree. the head
727 * node may also store the extent flags to set. This way you can check
728 * to see what the reference count and extent flags would be if all of
729 * the delayed refs are not processed.
731 int btrfs_lookup_extent_info(struct btrfs_trans_handle
*trans
,
732 struct btrfs_root
*root
, u64 bytenr
,
733 u64 num_bytes
, u64
*refs
, u64
*flags
)
735 struct btrfs_delayed_ref_head
*head
;
736 struct btrfs_delayed_ref_root
*delayed_refs
;
737 struct btrfs_path
*path
;
738 struct btrfs_extent_item
*ei
;
739 struct extent_buffer
*leaf
;
740 struct btrfs_key key
;
746 path
= btrfs_alloc_path();
750 key
.objectid
= bytenr
;
751 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
752 key
.offset
= num_bytes
;
754 path
->skip_locking
= 1;
755 path
->search_commit_root
= 1;
758 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
,
764 leaf
= path
->nodes
[0];
765 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
766 if (item_size
>= sizeof(*ei
)) {
767 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
768 struct btrfs_extent_item
);
769 num_refs
= btrfs_extent_refs(leaf
, ei
);
770 extent_flags
= btrfs_extent_flags(leaf
, ei
);
772 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
773 struct btrfs_extent_item_v0
*ei0
;
774 BUG_ON(item_size
!= sizeof(*ei0
));
775 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
776 struct btrfs_extent_item_v0
);
777 num_refs
= btrfs_extent_refs_v0(leaf
, ei0
);
778 /* FIXME: this isn't correct for data */
779 extent_flags
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
784 BUG_ON(num_refs
== 0);
794 delayed_refs
= &trans
->transaction
->delayed_refs
;
795 spin_lock(&delayed_refs
->lock
);
796 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
798 if (!mutex_trylock(&head
->mutex
)) {
799 atomic_inc(&head
->node
.refs
);
800 spin_unlock(&delayed_refs
->lock
);
802 btrfs_release_path(path
);
805 * Mutex was contended, block until it's released and try
808 mutex_lock(&head
->mutex
);
809 mutex_unlock(&head
->mutex
);
810 btrfs_put_delayed_ref(&head
->node
);
813 if (head
->extent_op
&& head
->extent_op
->update_flags
)
814 extent_flags
|= head
->extent_op
->flags_to_set
;
816 BUG_ON(num_refs
== 0);
818 num_refs
+= head
->node
.ref_mod
;
819 mutex_unlock(&head
->mutex
);
821 spin_unlock(&delayed_refs
->lock
);
823 WARN_ON(num_refs
== 0);
827 *flags
= extent_flags
;
829 btrfs_free_path(path
);
834 * Back reference rules. Back refs have three main goals:
836 * 1) differentiate between all holders of references to an extent so that
837 * when a reference is dropped we can make sure it was a valid reference
838 * before freeing the extent.
840 * 2) Provide enough information to quickly find the holders of an extent
841 * if we notice a given block is corrupted or bad.
843 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
844 * maintenance. This is actually the same as #2, but with a slightly
845 * different use case.
847 * There are two kinds of back refs. The implicit back refs is optimized
848 * for pointers in non-shared tree blocks. For a given pointer in a block,
849 * back refs of this kind provide information about the block's owner tree
850 * and the pointer's key. These information allow us to find the block by
851 * b-tree searching. The full back refs is for pointers in tree blocks not
852 * referenced by their owner trees. The location of tree block is recorded
853 * in the back refs. Actually the full back refs is generic, and can be
854 * used in all cases the implicit back refs is used. The major shortcoming
855 * of the full back refs is its overhead. Every time a tree block gets
856 * COWed, we have to update back refs entry for all pointers in it.
858 * For a newly allocated tree block, we use implicit back refs for
859 * pointers in it. This means most tree related operations only involve
860 * implicit back refs. For a tree block created in old transaction, the
861 * only way to drop a reference to it is COW it. So we can detect the
862 * event that tree block loses its owner tree's reference and do the
863 * back refs conversion.
865 * When a tree block is COW'd through a tree, there are four cases:
867 * The reference count of the block is one and the tree is the block's
868 * owner tree. Nothing to do in this case.
870 * The reference count of the block is one and the tree is not the
871 * block's owner tree. In this case, full back refs is used for pointers
872 * in the block. Remove these full back refs, add implicit back refs for
873 * every pointers in the new block.
875 * The reference count of the block is greater than one and the tree is
876 * the block's owner tree. In this case, implicit back refs is used for
877 * pointers in the block. Add full back refs for every pointers in the
878 * block, increase lower level extents' reference counts. The original
879 * implicit back refs are entailed to the new block.
881 * The reference count of the block is greater than one and the tree is
882 * not the block's owner tree. Add implicit back refs for every pointer in
883 * the new block, increase lower level extents' reference count.
885 * Back Reference Key composing:
887 * The key objectid corresponds to the first byte in the extent,
888 * The key type is used to differentiate between types of back refs.
889 * There are different meanings of the key offset for different types
892 * File extents can be referenced by:
894 * - multiple snapshots, subvolumes, or different generations in one subvol
895 * - different files inside a single subvolume
896 * - different offsets inside a file (bookend extents in file.c)
898 * The extent ref structure for the implicit back refs has fields for:
900 * - Objectid of the subvolume root
901 * - objectid of the file holding the reference
902 * - original offset in the file
903 * - how many bookend extents
905 * The key offset for the implicit back refs is hash of the first
908 * The extent ref structure for the full back refs has field for:
910 * - number of pointers in the tree leaf
912 * The key offset for the implicit back refs is the first byte of
915 * When a file extent is allocated, The implicit back refs is used.
916 * the fields are filled in:
918 * (root_key.objectid, inode objectid, offset in file, 1)
920 * When a file extent is removed file truncation, we find the
921 * corresponding implicit back refs and check the following fields:
923 * (btrfs_header_owner(leaf), inode objectid, offset in file)
925 * Btree extents can be referenced by:
927 * - Different subvolumes
929 * Both the implicit back refs and the full back refs for tree blocks
930 * only consist of key. The key offset for the implicit back refs is
931 * objectid of block's owner tree. The key offset for the full back refs
932 * is the first byte of parent block.
934 * When implicit back refs is used, information about the lowest key and
935 * level of the tree block are required. These information are stored in
936 * tree block info structure.
939 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
940 static int convert_extent_item_v0(struct btrfs_trans_handle
*trans
,
941 struct btrfs_root
*root
,
942 struct btrfs_path
*path
,
943 u64 owner
, u32 extra_size
)
945 struct btrfs_extent_item
*item
;
946 struct btrfs_extent_item_v0
*ei0
;
947 struct btrfs_extent_ref_v0
*ref0
;
948 struct btrfs_tree_block_info
*bi
;
949 struct extent_buffer
*leaf
;
950 struct btrfs_key key
;
951 struct btrfs_key found_key
;
952 u32 new_size
= sizeof(*item
);
956 leaf
= path
->nodes
[0];
957 BUG_ON(btrfs_item_size_nr(leaf
, path
->slots
[0]) != sizeof(*ei0
));
959 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
960 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
961 struct btrfs_extent_item_v0
);
962 refs
= btrfs_extent_refs_v0(leaf
, ei0
);
964 if (owner
== (u64
)-1) {
966 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
967 ret
= btrfs_next_leaf(root
, path
);
970 BUG_ON(ret
> 0); /* Corruption */
971 leaf
= path
->nodes
[0];
973 btrfs_item_key_to_cpu(leaf
, &found_key
,
975 BUG_ON(key
.objectid
!= found_key
.objectid
);
976 if (found_key
.type
!= BTRFS_EXTENT_REF_V0_KEY
) {
980 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
981 struct btrfs_extent_ref_v0
);
982 owner
= btrfs_ref_objectid_v0(leaf
, ref0
);
986 btrfs_release_path(path
);
988 if (owner
< BTRFS_FIRST_FREE_OBJECTID
)
989 new_size
+= sizeof(*bi
);
991 new_size
-= sizeof(*ei0
);
992 ret
= btrfs_search_slot(trans
, root
, &key
, path
,
993 new_size
+ extra_size
, 1);
996 BUG_ON(ret
); /* Corruption */
998 btrfs_extend_item(trans
, root
, path
, new_size
);
1000 leaf
= path
->nodes
[0];
1001 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1002 btrfs_set_extent_refs(leaf
, item
, refs
);
1003 /* FIXME: get real generation */
1004 btrfs_set_extent_generation(leaf
, item
, 0);
1005 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1006 btrfs_set_extent_flags(leaf
, item
,
1007 BTRFS_EXTENT_FLAG_TREE_BLOCK
|
1008 BTRFS_BLOCK_FLAG_FULL_BACKREF
);
1009 bi
= (struct btrfs_tree_block_info
*)(item
+ 1);
1010 /* FIXME: get first key of the block */
1011 memset_extent_buffer(leaf
, 0, (unsigned long)bi
, sizeof(*bi
));
1012 btrfs_set_tree_block_level(leaf
, bi
, (int)owner
);
1014 btrfs_set_extent_flags(leaf
, item
, BTRFS_EXTENT_FLAG_DATA
);
1016 btrfs_mark_buffer_dirty(leaf
);
1021 static u64
hash_extent_data_ref(u64 root_objectid
, u64 owner
, u64 offset
)
1023 u32 high_crc
= ~(u32
)0;
1024 u32 low_crc
= ~(u32
)0;
1027 lenum
= cpu_to_le64(root_objectid
);
1028 high_crc
= crc32c(high_crc
, &lenum
, sizeof(lenum
));
1029 lenum
= cpu_to_le64(owner
);
1030 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
1031 lenum
= cpu_to_le64(offset
);
1032 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
1034 return ((u64
)high_crc
<< 31) ^ (u64
)low_crc
;
1037 static u64
hash_extent_data_ref_item(struct extent_buffer
*leaf
,
1038 struct btrfs_extent_data_ref
*ref
)
1040 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf
, ref
),
1041 btrfs_extent_data_ref_objectid(leaf
, ref
),
1042 btrfs_extent_data_ref_offset(leaf
, ref
));
1045 static int match_extent_data_ref(struct extent_buffer
*leaf
,
1046 struct btrfs_extent_data_ref
*ref
,
1047 u64 root_objectid
, u64 owner
, u64 offset
)
1049 if (btrfs_extent_data_ref_root(leaf
, ref
) != root_objectid
||
1050 btrfs_extent_data_ref_objectid(leaf
, ref
) != owner
||
1051 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
1056 static noinline
int lookup_extent_data_ref(struct btrfs_trans_handle
*trans
,
1057 struct btrfs_root
*root
,
1058 struct btrfs_path
*path
,
1059 u64 bytenr
, u64 parent
,
1061 u64 owner
, u64 offset
)
1063 struct btrfs_key key
;
1064 struct btrfs_extent_data_ref
*ref
;
1065 struct extent_buffer
*leaf
;
1071 key
.objectid
= bytenr
;
1073 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
1074 key
.offset
= parent
;
1076 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
1077 key
.offset
= hash_extent_data_ref(root_objectid
,
1082 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1091 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1092 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
1093 btrfs_release_path(path
);
1094 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1105 leaf
= path
->nodes
[0];
1106 nritems
= btrfs_header_nritems(leaf
);
1108 if (path
->slots
[0] >= nritems
) {
1109 ret
= btrfs_next_leaf(root
, path
);
1115 leaf
= path
->nodes
[0];
1116 nritems
= btrfs_header_nritems(leaf
);
1120 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1121 if (key
.objectid
!= bytenr
||
1122 key
.type
!= BTRFS_EXTENT_DATA_REF_KEY
)
1125 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1126 struct btrfs_extent_data_ref
);
1128 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
1131 btrfs_release_path(path
);
1143 static noinline
int insert_extent_data_ref(struct btrfs_trans_handle
*trans
,
1144 struct btrfs_root
*root
,
1145 struct btrfs_path
*path
,
1146 u64 bytenr
, u64 parent
,
1147 u64 root_objectid
, u64 owner
,
1148 u64 offset
, int refs_to_add
)
1150 struct btrfs_key key
;
1151 struct extent_buffer
*leaf
;
1156 key
.objectid
= bytenr
;
1158 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
1159 key
.offset
= parent
;
1160 size
= sizeof(struct btrfs_shared_data_ref
);
1162 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
1163 key
.offset
= hash_extent_data_ref(root_objectid
,
1165 size
= sizeof(struct btrfs_extent_data_ref
);
1168 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, size
);
1169 if (ret
&& ret
!= -EEXIST
)
1172 leaf
= path
->nodes
[0];
1174 struct btrfs_shared_data_ref
*ref
;
1175 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1176 struct btrfs_shared_data_ref
);
1178 btrfs_set_shared_data_ref_count(leaf
, ref
, refs_to_add
);
1180 num_refs
= btrfs_shared_data_ref_count(leaf
, ref
);
1181 num_refs
+= refs_to_add
;
1182 btrfs_set_shared_data_ref_count(leaf
, ref
, num_refs
);
1185 struct btrfs_extent_data_ref
*ref
;
1186 while (ret
== -EEXIST
) {
1187 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1188 struct btrfs_extent_data_ref
);
1189 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
1192 btrfs_release_path(path
);
1194 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1196 if (ret
&& ret
!= -EEXIST
)
1199 leaf
= path
->nodes
[0];
1201 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1202 struct btrfs_extent_data_ref
);
1204 btrfs_set_extent_data_ref_root(leaf
, ref
,
1206 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
1207 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
1208 btrfs_set_extent_data_ref_count(leaf
, ref
, refs_to_add
);
1210 num_refs
= btrfs_extent_data_ref_count(leaf
, ref
);
1211 num_refs
+= refs_to_add
;
1212 btrfs_set_extent_data_ref_count(leaf
, ref
, num_refs
);
1215 btrfs_mark_buffer_dirty(leaf
);
1218 btrfs_release_path(path
);
1222 static noinline
int remove_extent_data_ref(struct btrfs_trans_handle
*trans
,
1223 struct btrfs_root
*root
,
1224 struct btrfs_path
*path
,
1227 struct btrfs_key key
;
1228 struct btrfs_extent_data_ref
*ref1
= NULL
;
1229 struct btrfs_shared_data_ref
*ref2
= NULL
;
1230 struct extent_buffer
*leaf
;
1234 leaf
= path
->nodes
[0];
1235 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1237 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1238 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
1239 struct btrfs_extent_data_ref
);
1240 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1241 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
1242 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
1243 struct btrfs_shared_data_ref
);
1244 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1245 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1246 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
1247 struct btrfs_extent_ref_v0
*ref0
;
1248 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1249 struct btrfs_extent_ref_v0
);
1250 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
1256 BUG_ON(num_refs
< refs_to_drop
);
1257 num_refs
-= refs_to_drop
;
1259 if (num_refs
== 0) {
1260 ret
= btrfs_del_item(trans
, root
, path
);
1262 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
)
1263 btrfs_set_extent_data_ref_count(leaf
, ref1
, num_refs
);
1264 else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
)
1265 btrfs_set_shared_data_ref_count(leaf
, ref2
, num_refs
);
1266 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1268 struct btrfs_extent_ref_v0
*ref0
;
1269 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1270 struct btrfs_extent_ref_v0
);
1271 btrfs_set_ref_count_v0(leaf
, ref0
, num_refs
);
1274 btrfs_mark_buffer_dirty(leaf
);
1279 static noinline u32
extent_data_ref_count(struct btrfs_root
*root
,
1280 struct btrfs_path
*path
,
1281 struct btrfs_extent_inline_ref
*iref
)
1283 struct btrfs_key key
;
1284 struct extent_buffer
*leaf
;
1285 struct btrfs_extent_data_ref
*ref1
;
1286 struct btrfs_shared_data_ref
*ref2
;
1289 leaf
= path
->nodes
[0];
1290 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1292 if (btrfs_extent_inline_ref_type(leaf
, iref
) ==
1293 BTRFS_EXTENT_DATA_REF_KEY
) {
1294 ref1
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1295 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1297 ref2
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1298 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1300 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1301 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
1302 struct btrfs_extent_data_ref
);
1303 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1304 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
1305 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
1306 struct btrfs_shared_data_ref
);
1307 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1308 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1309 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
1310 struct btrfs_extent_ref_v0
*ref0
;
1311 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1312 struct btrfs_extent_ref_v0
);
1313 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
1321 static noinline
int lookup_tree_block_ref(struct btrfs_trans_handle
*trans
,
1322 struct btrfs_root
*root
,
1323 struct btrfs_path
*path
,
1324 u64 bytenr
, u64 parent
,
1327 struct btrfs_key key
;
1330 key
.objectid
= bytenr
;
1332 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1333 key
.offset
= parent
;
1335 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
1336 key
.offset
= root_objectid
;
1339 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1342 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1343 if (ret
== -ENOENT
&& parent
) {
1344 btrfs_release_path(path
);
1345 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
1346 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1354 static noinline
int insert_tree_block_ref(struct btrfs_trans_handle
*trans
,
1355 struct btrfs_root
*root
,
1356 struct btrfs_path
*path
,
1357 u64 bytenr
, u64 parent
,
1360 struct btrfs_key key
;
1363 key
.objectid
= bytenr
;
1365 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1366 key
.offset
= parent
;
1368 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
1369 key
.offset
= root_objectid
;
1372 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, 0);
1373 btrfs_release_path(path
);
1377 static inline int extent_ref_type(u64 parent
, u64 owner
)
1380 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1382 type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1384 type
= BTRFS_TREE_BLOCK_REF_KEY
;
1387 type
= BTRFS_SHARED_DATA_REF_KEY
;
1389 type
= BTRFS_EXTENT_DATA_REF_KEY
;
1394 static int find_next_key(struct btrfs_path
*path
, int level
,
1395 struct btrfs_key
*key
)
1398 for (; level
< BTRFS_MAX_LEVEL
; level
++) {
1399 if (!path
->nodes
[level
])
1401 if (path
->slots
[level
] + 1 >=
1402 btrfs_header_nritems(path
->nodes
[level
]))
1405 btrfs_item_key_to_cpu(path
->nodes
[level
], key
,
1406 path
->slots
[level
] + 1);
1408 btrfs_node_key_to_cpu(path
->nodes
[level
], key
,
1409 path
->slots
[level
] + 1);
1416 * look for inline back ref. if back ref is found, *ref_ret is set
1417 * to the address of inline back ref, and 0 is returned.
1419 * if back ref isn't found, *ref_ret is set to the address where it
1420 * should be inserted, and -ENOENT is returned.
1422 * if insert is true and there are too many inline back refs, the path
1423 * points to the extent item, and -EAGAIN is returned.
1425 * NOTE: inline back refs are ordered in the same way that back ref
1426 * items in the tree are ordered.
1428 static noinline_for_stack
1429 int lookup_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1430 struct btrfs_root
*root
,
1431 struct btrfs_path
*path
,
1432 struct btrfs_extent_inline_ref
**ref_ret
,
1433 u64 bytenr
, u64 num_bytes
,
1434 u64 parent
, u64 root_objectid
,
1435 u64 owner
, u64 offset
, int insert
)
1437 struct btrfs_key key
;
1438 struct extent_buffer
*leaf
;
1439 struct btrfs_extent_item
*ei
;
1440 struct btrfs_extent_inline_ref
*iref
;
1451 key
.objectid
= bytenr
;
1452 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1453 key
.offset
= num_bytes
;
1455 want
= extent_ref_type(parent
, owner
);
1457 extra_size
= btrfs_extent_inline_ref_size(want
);
1458 path
->keep_locks
= 1;
1461 ret
= btrfs_search_slot(trans
, root
, &key
, path
, extra_size
, 1);
1466 if (ret
&& !insert
) {
1470 BUG_ON(ret
); /* Corruption */
1472 leaf
= path
->nodes
[0];
1473 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1474 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1475 if (item_size
< sizeof(*ei
)) {
1480 ret
= convert_extent_item_v0(trans
, root
, path
, owner
,
1486 leaf
= path
->nodes
[0];
1487 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1490 BUG_ON(item_size
< sizeof(*ei
));
1492 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1493 flags
= btrfs_extent_flags(leaf
, ei
);
1495 ptr
= (unsigned long)(ei
+ 1);
1496 end
= (unsigned long)ei
+ item_size
;
1498 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
1499 ptr
+= sizeof(struct btrfs_tree_block_info
);
1502 BUG_ON(!(flags
& BTRFS_EXTENT_FLAG_DATA
));
1511 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1512 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1516 ptr
+= btrfs_extent_inline_ref_size(type
);
1520 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1521 struct btrfs_extent_data_ref
*dref
;
1522 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1523 if (match_extent_data_ref(leaf
, dref
, root_objectid
,
1528 if (hash_extent_data_ref_item(leaf
, dref
) <
1529 hash_extent_data_ref(root_objectid
, owner
, offset
))
1533 ref_offset
= btrfs_extent_inline_ref_offset(leaf
, iref
);
1535 if (parent
== ref_offset
) {
1539 if (ref_offset
< parent
)
1542 if (root_objectid
== ref_offset
) {
1546 if (ref_offset
< root_objectid
)
1550 ptr
+= btrfs_extent_inline_ref_size(type
);
1552 if (err
== -ENOENT
&& insert
) {
1553 if (item_size
+ extra_size
>=
1554 BTRFS_MAX_EXTENT_ITEM_SIZE(root
)) {
1559 * To add new inline back ref, we have to make sure
1560 * there is no corresponding back ref item.
1561 * For simplicity, we just do not add new inline back
1562 * ref if there is any kind of item for this block
1564 if (find_next_key(path
, 0, &key
) == 0 &&
1565 key
.objectid
== bytenr
&&
1566 key
.type
< BTRFS_BLOCK_GROUP_ITEM_KEY
) {
1571 *ref_ret
= (struct btrfs_extent_inline_ref
*)ptr
;
1574 path
->keep_locks
= 0;
1575 btrfs_unlock_up_safe(path
, 1);
1581 * helper to add new inline back ref
1583 static noinline_for_stack
1584 void setup_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1585 struct btrfs_root
*root
,
1586 struct btrfs_path
*path
,
1587 struct btrfs_extent_inline_ref
*iref
,
1588 u64 parent
, u64 root_objectid
,
1589 u64 owner
, u64 offset
, int refs_to_add
,
1590 struct btrfs_delayed_extent_op
*extent_op
)
1592 struct extent_buffer
*leaf
;
1593 struct btrfs_extent_item
*ei
;
1596 unsigned long item_offset
;
1601 leaf
= path
->nodes
[0];
1602 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1603 item_offset
= (unsigned long)iref
- (unsigned long)ei
;
1605 type
= extent_ref_type(parent
, owner
);
1606 size
= btrfs_extent_inline_ref_size(type
);
1608 btrfs_extend_item(trans
, root
, path
, size
);
1610 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1611 refs
= btrfs_extent_refs(leaf
, ei
);
1612 refs
+= refs_to_add
;
1613 btrfs_set_extent_refs(leaf
, ei
, refs
);
1615 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1617 ptr
= (unsigned long)ei
+ item_offset
;
1618 end
= (unsigned long)ei
+ btrfs_item_size_nr(leaf
, path
->slots
[0]);
1619 if (ptr
< end
- size
)
1620 memmove_extent_buffer(leaf
, ptr
+ size
, ptr
,
1623 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1624 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
1625 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1626 struct btrfs_extent_data_ref
*dref
;
1627 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1628 btrfs_set_extent_data_ref_root(leaf
, dref
, root_objectid
);
1629 btrfs_set_extent_data_ref_objectid(leaf
, dref
, owner
);
1630 btrfs_set_extent_data_ref_offset(leaf
, dref
, offset
);
1631 btrfs_set_extent_data_ref_count(leaf
, dref
, refs_to_add
);
1632 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1633 struct btrfs_shared_data_ref
*sref
;
1634 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1635 btrfs_set_shared_data_ref_count(leaf
, sref
, refs_to_add
);
1636 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1637 } else if (type
== BTRFS_SHARED_BLOCK_REF_KEY
) {
1638 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1640 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
1642 btrfs_mark_buffer_dirty(leaf
);
1645 static int lookup_extent_backref(struct btrfs_trans_handle
*trans
,
1646 struct btrfs_root
*root
,
1647 struct btrfs_path
*path
,
1648 struct btrfs_extent_inline_ref
**ref_ret
,
1649 u64 bytenr
, u64 num_bytes
, u64 parent
,
1650 u64 root_objectid
, u64 owner
, u64 offset
)
1654 ret
= lookup_inline_extent_backref(trans
, root
, path
, ref_ret
,
1655 bytenr
, num_bytes
, parent
,
1656 root_objectid
, owner
, offset
, 0);
1660 btrfs_release_path(path
);
1663 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1664 ret
= lookup_tree_block_ref(trans
, root
, path
, bytenr
, parent
,
1667 ret
= lookup_extent_data_ref(trans
, root
, path
, bytenr
, parent
,
1668 root_objectid
, owner
, offset
);
1674 * helper to update/remove inline back ref
1676 static noinline_for_stack
1677 void update_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1678 struct btrfs_root
*root
,
1679 struct btrfs_path
*path
,
1680 struct btrfs_extent_inline_ref
*iref
,
1682 struct btrfs_delayed_extent_op
*extent_op
)
1684 struct extent_buffer
*leaf
;
1685 struct btrfs_extent_item
*ei
;
1686 struct btrfs_extent_data_ref
*dref
= NULL
;
1687 struct btrfs_shared_data_ref
*sref
= NULL
;
1695 leaf
= path
->nodes
[0];
1696 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1697 refs
= btrfs_extent_refs(leaf
, ei
);
1698 WARN_ON(refs_to_mod
< 0 && refs
+ refs_to_mod
<= 0);
1699 refs
+= refs_to_mod
;
1700 btrfs_set_extent_refs(leaf
, ei
, refs
);
1702 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1704 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1706 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1707 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1708 refs
= btrfs_extent_data_ref_count(leaf
, dref
);
1709 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1710 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1711 refs
= btrfs_shared_data_ref_count(leaf
, sref
);
1714 BUG_ON(refs_to_mod
!= -1);
1717 BUG_ON(refs_to_mod
< 0 && refs
< -refs_to_mod
);
1718 refs
+= refs_to_mod
;
1721 if (type
== BTRFS_EXTENT_DATA_REF_KEY
)
1722 btrfs_set_extent_data_ref_count(leaf
, dref
, refs
);
1724 btrfs_set_shared_data_ref_count(leaf
, sref
, refs
);
1726 size
= btrfs_extent_inline_ref_size(type
);
1727 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1728 ptr
= (unsigned long)iref
;
1729 end
= (unsigned long)ei
+ item_size
;
1730 if (ptr
+ size
< end
)
1731 memmove_extent_buffer(leaf
, ptr
, ptr
+ size
,
1734 btrfs_truncate_item(trans
, root
, path
, item_size
, 1);
1736 btrfs_mark_buffer_dirty(leaf
);
1739 static noinline_for_stack
1740 int insert_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1741 struct btrfs_root
*root
,
1742 struct btrfs_path
*path
,
1743 u64 bytenr
, u64 num_bytes
, u64 parent
,
1744 u64 root_objectid
, u64 owner
,
1745 u64 offset
, int refs_to_add
,
1746 struct btrfs_delayed_extent_op
*extent_op
)
1748 struct btrfs_extent_inline_ref
*iref
;
1751 ret
= lookup_inline_extent_backref(trans
, root
, path
, &iref
,
1752 bytenr
, num_bytes
, parent
,
1753 root_objectid
, owner
, offset
, 1);
1755 BUG_ON(owner
< BTRFS_FIRST_FREE_OBJECTID
);
1756 update_inline_extent_backref(trans
, root
, path
, iref
,
1757 refs_to_add
, extent_op
);
1758 } else if (ret
== -ENOENT
) {
1759 setup_inline_extent_backref(trans
, root
, path
, iref
, parent
,
1760 root_objectid
, owner
, offset
,
1761 refs_to_add
, extent_op
);
1767 static int insert_extent_backref(struct btrfs_trans_handle
*trans
,
1768 struct btrfs_root
*root
,
1769 struct btrfs_path
*path
,
1770 u64 bytenr
, u64 parent
, u64 root_objectid
,
1771 u64 owner
, u64 offset
, int refs_to_add
)
1774 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1775 BUG_ON(refs_to_add
!= 1);
1776 ret
= insert_tree_block_ref(trans
, root
, path
, bytenr
,
1777 parent
, root_objectid
);
1779 ret
= insert_extent_data_ref(trans
, root
, path
, bytenr
,
1780 parent
, root_objectid
,
1781 owner
, offset
, refs_to_add
);
1786 static int remove_extent_backref(struct btrfs_trans_handle
*trans
,
1787 struct btrfs_root
*root
,
1788 struct btrfs_path
*path
,
1789 struct btrfs_extent_inline_ref
*iref
,
1790 int refs_to_drop
, int is_data
)
1794 BUG_ON(!is_data
&& refs_to_drop
!= 1);
1796 update_inline_extent_backref(trans
, root
, path
, iref
,
1797 -refs_to_drop
, NULL
);
1798 } else if (is_data
) {
1799 ret
= remove_extent_data_ref(trans
, root
, path
, refs_to_drop
);
1801 ret
= btrfs_del_item(trans
, root
, path
);
1806 static int btrfs_issue_discard(struct block_device
*bdev
,
1809 return blkdev_issue_discard(bdev
, start
>> 9, len
>> 9, GFP_NOFS
, 0);
1812 static int btrfs_discard_extent(struct btrfs_root
*root
, u64 bytenr
,
1813 u64 num_bytes
, u64
*actual_bytes
)
1816 u64 discarded_bytes
= 0;
1817 struct btrfs_bio
*bbio
= NULL
;
1820 /* Tell the block device(s) that the sectors can be discarded */
1821 ret
= btrfs_map_block(root
->fs_info
, REQ_DISCARD
,
1822 bytenr
, &num_bytes
, &bbio
, 0);
1823 /* Error condition is -ENOMEM */
1825 struct btrfs_bio_stripe
*stripe
= bbio
->stripes
;
1829 for (i
= 0; i
< bbio
->num_stripes
; i
++, stripe
++) {
1830 if (!stripe
->dev
->can_discard
)
1833 ret
= btrfs_issue_discard(stripe
->dev
->bdev
,
1837 discarded_bytes
+= stripe
->length
;
1838 else if (ret
!= -EOPNOTSUPP
)
1839 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
1842 * Just in case we get back EOPNOTSUPP for some reason,
1843 * just ignore the return value so we don't screw up
1844 * people calling discard_extent.
1852 *actual_bytes
= discarded_bytes
;
1858 /* Can return -ENOMEM */
1859 int btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
1860 struct btrfs_root
*root
,
1861 u64 bytenr
, u64 num_bytes
, u64 parent
,
1862 u64 root_objectid
, u64 owner
, u64 offset
, int for_cow
)
1865 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1867 BUG_ON(owner
< BTRFS_FIRST_FREE_OBJECTID
&&
1868 root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
1870 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1871 ret
= btrfs_add_delayed_tree_ref(fs_info
, trans
, bytenr
,
1873 parent
, root_objectid
, (int)owner
,
1874 BTRFS_ADD_DELAYED_REF
, NULL
, for_cow
);
1876 ret
= btrfs_add_delayed_data_ref(fs_info
, trans
, bytenr
,
1878 parent
, root_objectid
, owner
, offset
,
1879 BTRFS_ADD_DELAYED_REF
, NULL
, for_cow
);
1884 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
1885 struct btrfs_root
*root
,
1886 u64 bytenr
, u64 num_bytes
,
1887 u64 parent
, u64 root_objectid
,
1888 u64 owner
, u64 offset
, int refs_to_add
,
1889 struct btrfs_delayed_extent_op
*extent_op
)
1891 struct btrfs_path
*path
;
1892 struct extent_buffer
*leaf
;
1893 struct btrfs_extent_item
*item
;
1898 path
= btrfs_alloc_path();
1903 path
->leave_spinning
= 1;
1904 /* this will setup the path even if it fails to insert the back ref */
1905 ret
= insert_inline_extent_backref(trans
, root
->fs_info
->extent_root
,
1906 path
, bytenr
, num_bytes
, parent
,
1907 root_objectid
, owner
, offset
,
1908 refs_to_add
, extent_op
);
1912 if (ret
!= -EAGAIN
) {
1917 leaf
= path
->nodes
[0];
1918 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1919 refs
= btrfs_extent_refs(leaf
, item
);
1920 btrfs_set_extent_refs(leaf
, item
, refs
+ refs_to_add
);
1922 __run_delayed_extent_op(extent_op
, leaf
, item
);
1924 btrfs_mark_buffer_dirty(leaf
);
1925 btrfs_release_path(path
);
1928 path
->leave_spinning
= 1;
1930 /* now insert the actual backref */
1931 ret
= insert_extent_backref(trans
, root
->fs_info
->extent_root
,
1932 path
, bytenr
, parent
, root_objectid
,
1933 owner
, offset
, refs_to_add
);
1935 btrfs_abort_transaction(trans
, root
, ret
);
1937 btrfs_free_path(path
);
1941 static int run_delayed_data_ref(struct btrfs_trans_handle
*trans
,
1942 struct btrfs_root
*root
,
1943 struct btrfs_delayed_ref_node
*node
,
1944 struct btrfs_delayed_extent_op
*extent_op
,
1945 int insert_reserved
)
1948 struct btrfs_delayed_data_ref
*ref
;
1949 struct btrfs_key ins
;
1954 ins
.objectid
= node
->bytenr
;
1955 ins
.offset
= node
->num_bytes
;
1956 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1958 ref
= btrfs_delayed_node_to_data_ref(node
);
1959 if (node
->type
== BTRFS_SHARED_DATA_REF_KEY
)
1960 parent
= ref
->parent
;
1962 ref_root
= ref
->root
;
1964 if (node
->action
== BTRFS_ADD_DELAYED_REF
&& insert_reserved
) {
1966 BUG_ON(extent_op
->update_key
);
1967 flags
|= extent_op
->flags_to_set
;
1969 ret
= alloc_reserved_file_extent(trans
, root
,
1970 parent
, ref_root
, flags
,
1971 ref
->objectid
, ref
->offset
,
1972 &ins
, node
->ref_mod
);
1973 } else if (node
->action
== BTRFS_ADD_DELAYED_REF
) {
1974 ret
= __btrfs_inc_extent_ref(trans
, root
, node
->bytenr
,
1975 node
->num_bytes
, parent
,
1976 ref_root
, ref
->objectid
,
1977 ref
->offset
, node
->ref_mod
,
1979 } else if (node
->action
== BTRFS_DROP_DELAYED_REF
) {
1980 ret
= __btrfs_free_extent(trans
, root
, node
->bytenr
,
1981 node
->num_bytes
, parent
,
1982 ref_root
, ref
->objectid
,
1983 ref
->offset
, node
->ref_mod
,
1991 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op
*extent_op
,
1992 struct extent_buffer
*leaf
,
1993 struct btrfs_extent_item
*ei
)
1995 u64 flags
= btrfs_extent_flags(leaf
, ei
);
1996 if (extent_op
->update_flags
) {
1997 flags
|= extent_op
->flags_to_set
;
1998 btrfs_set_extent_flags(leaf
, ei
, flags
);
2001 if (extent_op
->update_key
) {
2002 struct btrfs_tree_block_info
*bi
;
2003 BUG_ON(!(flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
));
2004 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
2005 btrfs_set_tree_block_key(leaf
, bi
, &extent_op
->key
);
2009 static int run_delayed_extent_op(struct btrfs_trans_handle
*trans
,
2010 struct btrfs_root
*root
,
2011 struct btrfs_delayed_ref_node
*node
,
2012 struct btrfs_delayed_extent_op
*extent_op
)
2014 struct btrfs_key key
;
2015 struct btrfs_path
*path
;
2016 struct btrfs_extent_item
*ei
;
2017 struct extent_buffer
*leaf
;
2025 path
= btrfs_alloc_path();
2029 key
.objectid
= node
->bytenr
;
2030 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2031 key
.offset
= node
->num_bytes
;
2034 path
->leave_spinning
= 1;
2035 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
,
2046 leaf
= path
->nodes
[0];
2047 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2048 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2049 if (item_size
< sizeof(*ei
)) {
2050 ret
= convert_extent_item_v0(trans
, root
->fs_info
->extent_root
,
2056 leaf
= path
->nodes
[0];
2057 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2060 BUG_ON(item_size
< sizeof(*ei
));
2061 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
2062 __run_delayed_extent_op(extent_op
, leaf
, ei
);
2064 btrfs_mark_buffer_dirty(leaf
);
2066 btrfs_free_path(path
);
2070 static int run_delayed_tree_ref(struct btrfs_trans_handle
*trans
,
2071 struct btrfs_root
*root
,
2072 struct btrfs_delayed_ref_node
*node
,
2073 struct btrfs_delayed_extent_op
*extent_op
,
2074 int insert_reserved
)
2077 struct btrfs_delayed_tree_ref
*ref
;
2078 struct btrfs_key ins
;
2082 ins
.objectid
= node
->bytenr
;
2083 ins
.offset
= node
->num_bytes
;
2084 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
2086 ref
= btrfs_delayed_node_to_tree_ref(node
);
2087 if (node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
)
2088 parent
= ref
->parent
;
2090 ref_root
= ref
->root
;
2092 BUG_ON(node
->ref_mod
!= 1);
2093 if (node
->action
== BTRFS_ADD_DELAYED_REF
&& insert_reserved
) {
2094 BUG_ON(!extent_op
|| !extent_op
->update_flags
||
2095 !extent_op
->update_key
);
2096 ret
= alloc_reserved_tree_block(trans
, root
,
2098 extent_op
->flags_to_set
,
2101 } else if (node
->action
== BTRFS_ADD_DELAYED_REF
) {
2102 ret
= __btrfs_inc_extent_ref(trans
, root
, node
->bytenr
,
2103 node
->num_bytes
, parent
, ref_root
,
2104 ref
->level
, 0, 1, extent_op
);
2105 } else if (node
->action
== BTRFS_DROP_DELAYED_REF
) {
2106 ret
= __btrfs_free_extent(trans
, root
, node
->bytenr
,
2107 node
->num_bytes
, parent
, ref_root
,
2108 ref
->level
, 0, 1, extent_op
);
2115 /* helper function to actually process a single delayed ref entry */
2116 static int run_one_delayed_ref(struct btrfs_trans_handle
*trans
,
2117 struct btrfs_root
*root
,
2118 struct btrfs_delayed_ref_node
*node
,
2119 struct btrfs_delayed_extent_op
*extent_op
,
2120 int insert_reserved
)
2127 if (btrfs_delayed_ref_is_head(node
)) {
2128 struct btrfs_delayed_ref_head
*head
;
2130 * we've hit the end of the chain and we were supposed
2131 * to insert this extent into the tree. But, it got
2132 * deleted before we ever needed to insert it, so all
2133 * we have to do is clean up the accounting
2136 head
= btrfs_delayed_node_to_head(node
);
2137 if (insert_reserved
) {
2138 btrfs_pin_extent(root
, node
->bytenr
,
2139 node
->num_bytes
, 1);
2140 if (head
->is_data
) {
2141 ret
= btrfs_del_csums(trans
, root
,
2149 if (node
->type
== BTRFS_TREE_BLOCK_REF_KEY
||
2150 node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
)
2151 ret
= run_delayed_tree_ref(trans
, root
, node
, extent_op
,
2153 else if (node
->type
== BTRFS_EXTENT_DATA_REF_KEY
||
2154 node
->type
== BTRFS_SHARED_DATA_REF_KEY
)
2155 ret
= run_delayed_data_ref(trans
, root
, node
, extent_op
,
2162 static noinline
struct btrfs_delayed_ref_node
*
2163 select_delayed_ref(struct btrfs_delayed_ref_head
*head
)
2165 struct rb_node
*node
;
2166 struct btrfs_delayed_ref_node
*ref
;
2167 int action
= BTRFS_ADD_DELAYED_REF
;
2170 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2171 * this prevents ref count from going down to zero when
2172 * there still are pending delayed ref.
2174 node
= rb_prev(&head
->node
.rb_node
);
2178 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
,
2180 if (ref
->bytenr
!= head
->node
.bytenr
)
2182 if (ref
->action
== action
)
2184 node
= rb_prev(node
);
2186 if (action
== BTRFS_ADD_DELAYED_REF
) {
2187 action
= BTRFS_DROP_DELAYED_REF
;
2194 * Returns 0 on success or if called with an already aborted transaction.
2195 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2197 static noinline
int run_clustered_refs(struct btrfs_trans_handle
*trans
,
2198 struct btrfs_root
*root
,
2199 struct list_head
*cluster
)
2201 struct btrfs_delayed_ref_root
*delayed_refs
;
2202 struct btrfs_delayed_ref_node
*ref
;
2203 struct btrfs_delayed_ref_head
*locked_ref
= NULL
;
2204 struct btrfs_delayed_extent_op
*extent_op
;
2205 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2208 int must_insert_reserved
= 0;
2210 delayed_refs
= &trans
->transaction
->delayed_refs
;
2213 /* pick a new head ref from the cluster list */
2214 if (list_empty(cluster
))
2217 locked_ref
= list_entry(cluster
->next
,
2218 struct btrfs_delayed_ref_head
, cluster
);
2220 /* grab the lock that says we are going to process
2221 * all the refs for this head */
2222 ret
= btrfs_delayed_ref_lock(trans
, locked_ref
);
2225 * we may have dropped the spin lock to get the head
2226 * mutex lock, and that might have given someone else
2227 * time to free the head. If that's true, it has been
2228 * removed from our list and we can move on.
2230 if (ret
== -EAGAIN
) {
2238 * We need to try and merge add/drops of the same ref since we
2239 * can run into issues with relocate dropping the implicit ref
2240 * and then it being added back again before the drop can
2241 * finish. If we merged anything we need to re-loop so we can
2244 btrfs_merge_delayed_refs(trans
, fs_info
, delayed_refs
,
2248 * locked_ref is the head node, so we have to go one
2249 * node back for any delayed ref updates
2251 ref
= select_delayed_ref(locked_ref
);
2253 if (ref
&& ref
->seq
&&
2254 btrfs_check_delayed_seq(fs_info
, delayed_refs
, ref
->seq
)) {
2256 * there are still refs with lower seq numbers in the
2257 * process of being added. Don't run this ref yet.
2259 list_del_init(&locked_ref
->cluster
);
2260 btrfs_delayed_ref_unlock(locked_ref
);
2262 delayed_refs
->num_heads_ready
++;
2263 spin_unlock(&delayed_refs
->lock
);
2265 spin_lock(&delayed_refs
->lock
);
2270 * record the must insert reserved flag before we
2271 * drop the spin lock.
2273 must_insert_reserved
= locked_ref
->must_insert_reserved
;
2274 locked_ref
->must_insert_reserved
= 0;
2276 extent_op
= locked_ref
->extent_op
;
2277 locked_ref
->extent_op
= NULL
;
2280 /* All delayed refs have been processed, Go ahead
2281 * and send the head node to run_one_delayed_ref,
2282 * so that any accounting fixes can happen
2284 ref
= &locked_ref
->node
;
2286 if (extent_op
&& must_insert_reserved
) {
2287 btrfs_free_delayed_extent_op(extent_op
);
2292 spin_unlock(&delayed_refs
->lock
);
2294 ret
= run_delayed_extent_op(trans
, root
,
2296 btrfs_free_delayed_extent_op(extent_op
);
2300 "btrfs: run_delayed_extent_op "
2301 "returned %d\n", ret
);
2302 spin_lock(&delayed_refs
->lock
);
2303 btrfs_delayed_ref_unlock(locked_ref
);
2312 rb_erase(&ref
->rb_node
, &delayed_refs
->root
);
2313 delayed_refs
->num_entries
--;
2314 if (!btrfs_delayed_ref_is_head(ref
)) {
2316 * when we play the delayed ref, also correct the
2319 switch (ref
->action
) {
2320 case BTRFS_ADD_DELAYED_REF
:
2321 case BTRFS_ADD_DELAYED_EXTENT
:
2322 locked_ref
->node
.ref_mod
-= ref
->ref_mod
;
2324 case BTRFS_DROP_DELAYED_REF
:
2325 locked_ref
->node
.ref_mod
+= ref
->ref_mod
;
2331 spin_unlock(&delayed_refs
->lock
);
2333 ret
= run_one_delayed_ref(trans
, root
, ref
, extent_op
,
2334 must_insert_reserved
);
2336 btrfs_free_delayed_extent_op(extent_op
);
2338 btrfs_delayed_ref_unlock(locked_ref
);
2339 btrfs_put_delayed_ref(ref
);
2341 "btrfs: run_one_delayed_ref returned %d\n", ret
);
2342 spin_lock(&delayed_refs
->lock
);
2347 * If this node is a head, that means all the refs in this head
2348 * have been dealt with, and we will pick the next head to deal
2349 * with, so we must unlock the head and drop it from the cluster
2350 * list before we release it.
2352 if (btrfs_delayed_ref_is_head(ref
)) {
2353 list_del_init(&locked_ref
->cluster
);
2354 btrfs_delayed_ref_unlock(locked_ref
);
2357 btrfs_put_delayed_ref(ref
);
2361 spin_lock(&delayed_refs
->lock
);
2366 #ifdef SCRAMBLE_DELAYED_REFS
2368 * Normally delayed refs get processed in ascending bytenr order. This
2369 * correlates in most cases to the order added. To expose dependencies on this
2370 * order, we start to process the tree in the middle instead of the beginning
2372 static u64
find_middle(struct rb_root
*root
)
2374 struct rb_node
*n
= root
->rb_node
;
2375 struct btrfs_delayed_ref_node
*entry
;
2378 u64 first
= 0, last
= 0;
2382 entry
= rb_entry(n
, struct btrfs_delayed_ref_node
, rb_node
);
2383 first
= entry
->bytenr
;
2387 entry
= rb_entry(n
, struct btrfs_delayed_ref_node
, rb_node
);
2388 last
= entry
->bytenr
;
2393 entry
= rb_entry(n
, struct btrfs_delayed_ref_node
, rb_node
);
2394 WARN_ON(!entry
->in_tree
);
2396 middle
= entry
->bytenr
;
2409 int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle
*trans
,
2410 struct btrfs_fs_info
*fs_info
)
2412 struct qgroup_update
*qgroup_update
;
2415 if (list_empty(&trans
->qgroup_ref_list
) !=
2416 !trans
->delayed_ref_elem
.seq
) {
2417 /* list without seq or seq without list */
2418 printk(KERN_ERR
"btrfs: qgroup accounting update error, list is%s empty, seq is %llu\n",
2419 list_empty(&trans
->qgroup_ref_list
) ? "" : " not",
2420 trans
->delayed_ref_elem
.seq
);
2424 if (!trans
->delayed_ref_elem
.seq
)
2427 while (!list_empty(&trans
->qgroup_ref_list
)) {
2428 qgroup_update
= list_first_entry(&trans
->qgroup_ref_list
,
2429 struct qgroup_update
, list
);
2430 list_del(&qgroup_update
->list
);
2432 ret
= btrfs_qgroup_account_ref(
2433 trans
, fs_info
, qgroup_update
->node
,
2434 qgroup_update
->extent_op
);
2435 kfree(qgroup_update
);
2438 btrfs_put_tree_mod_seq(fs_info
, &trans
->delayed_ref_elem
);
2444 * this starts processing the delayed reference count updates and
2445 * extent insertions we have queued up so far. count can be
2446 * 0, which means to process everything in the tree at the start
2447 * of the run (but not newly added entries), or it can be some target
2448 * number you'd like to process.
2450 * Returns 0 on success or if called with an aborted transaction
2451 * Returns <0 on error and aborts the transaction
2453 int btrfs_run_delayed_refs(struct btrfs_trans_handle
*trans
,
2454 struct btrfs_root
*root
, unsigned long count
)
2456 struct rb_node
*node
;
2457 struct btrfs_delayed_ref_root
*delayed_refs
;
2458 struct btrfs_delayed_ref_node
*ref
;
2459 struct list_head cluster
;
2462 int run_all
= count
== (unsigned long)-1;
2466 /* We'll clean this up in btrfs_cleanup_transaction */
2470 if (root
== root
->fs_info
->extent_root
)
2471 root
= root
->fs_info
->tree_root
;
2473 btrfs_delayed_refs_qgroup_accounting(trans
, root
->fs_info
);
2475 delayed_refs
= &trans
->transaction
->delayed_refs
;
2476 INIT_LIST_HEAD(&cluster
);
2479 spin_lock(&delayed_refs
->lock
);
2481 #ifdef SCRAMBLE_DELAYED_REFS
2482 delayed_refs
->run_delayed_start
= find_middle(&delayed_refs
->root
);
2486 count
= delayed_refs
->num_entries
* 2;
2490 if (!(run_all
|| run_most
) &&
2491 delayed_refs
->num_heads_ready
< 64)
2495 * go find something we can process in the rbtree. We start at
2496 * the beginning of the tree, and then build a cluster
2497 * of refs to process starting at the first one we are able to
2500 delayed_start
= delayed_refs
->run_delayed_start
;
2501 ret
= btrfs_find_ref_cluster(trans
, &cluster
,
2502 delayed_refs
->run_delayed_start
);
2506 ret
= run_clustered_refs(trans
, root
, &cluster
);
2508 btrfs_release_ref_cluster(&cluster
);
2509 spin_unlock(&delayed_refs
->lock
);
2510 btrfs_abort_transaction(trans
, root
, ret
);
2514 count
-= min_t(unsigned long, ret
, count
);
2519 if (delayed_start
>= delayed_refs
->run_delayed_start
) {
2522 * btrfs_find_ref_cluster looped. let's do one
2523 * more cycle. if we don't run any delayed ref
2524 * during that cycle (because we can't because
2525 * all of them are blocked), bail out.
2530 * no runnable refs left, stop trying
2537 /* refs were run, let's reset staleness detection */
2543 if (!list_empty(&trans
->new_bgs
)) {
2544 spin_unlock(&delayed_refs
->lock
);
2545 btrfs_create_pending_block_groups(trans
, root
);
2546 spin_lock(&delayed_refs
->lock
);
2549 node
= rb_first(&delayed_refs
->root
);
2552 count
= (unsigned long)-1;
2555 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
,
2557 if (btrfs_delayed_ref_is_head(ref
)) {
2558 struct btrfs_delayed_ref_head
*head
;
2560 head
= btrfs_delayed_node_to_head(ref
);
2561 atomic_inc(&ref
->refs
);
2563 spin_unlock(&delayed_refs
->lock
);
2565 * Mutex was contended, block until it's
2566 * released and try again
2568 mutex_lock(&head
->mutex
);
2569 mutex_unlock(&head
->mutex
);
2571 btrfs_put_delayed_ref(ref
);
2575 node
= rb_next(node
);
2577 spin_unlock(&delayed_refs
->lock
);
2578 schedule_timeout(1);
2582 spin_unlock(&delayed_refs
->lock
);
2583 assert_qgroups_uptodate(trans
);
2587 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle
*trans
,
2588 struct btrfs_root
*root
,
2589 u64 bytenr
, u64 num_bytes
, u64 flags
,
2592 struct btrfs_delayed_extent_op
*extent_op
;
2595 extent_op
= btrfs_alloc_delayed_extent_op();
2599 extent_op
->flags_to_set
= flags
;
2600 extent_op
->update_flags
= 1;
2601 extent_op
->update_key
= 0;
2602 extent_op
->is_data
= is_data
? 1 : 0;
2604 ret
= btrfs_add_delayed_extent_op(root
->fs_info
, trans
, bytenr
,
2605 num_bytes
, extent_op
);
2607 btrfs_free_delayed_extent_op(extent_op
);
2611 static noinline
int check_delayed_ref(struct btrfs_trans_handle
*trans
,
2612 struct btrfs_root
*root
,
2613 struct btrfs_path
*path
,
2614 u64 objectid
, u64 offset
, u64 bytenr
)
2616 struct btrfs_delayed_ref_head
*head
;
2617 struct btrfs_delayed_ref_node
*ref
;
2618 struct btrfs_delayed_data_ref
*data_ref
;
2619 struct btrfs_delayed_ref_root
*delayed_refs
;
2620 struct rb_node
*node
;
2624 delayed_refs
= &trans
->transaction
->delayed_refs
;
2625 spin_lock(&delayed_refs
->lock
);
2626 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
2630 if (!mutex_trylock(&head
->mutex
)) {
2631 atomic_inc(&head
->node
.refs
);
2632 spin_unlock(&delayed_refs
->lock
);
2634 btrfs_release_path(path
);
2637 * Mutex was contended, block until it's released and let
2640 mutex_lock(&head
->mutex
);
2641 mutex_unlock(&head
->mutex
);
2642 btrfs_put_delayed_ref(&head
->node
);
2646 node
= rb_prev(&head
->node
.rb_node
);
2650 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
2652 if (ref
->bytenr
!= bytenr
)
2656 if (ref
->type
!= BTRFS_EXTENT_DATA_REF_KEY
)
2659 data_ref
= btrfs_delayed_node_to_data_ref(ref
);
2661 node
= rb_prev(node
);
2665 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
2666 if (ref
->bytenr
== bytenr
&& ref
->seq
== seq
)
2670 if (data_ref
->root
!= root
->root_key
.objectid
||
2671 data_ref
->objectid
!= objectid
|| data_ref
->offset
!= offset
)
2676 mutex_unlock(&head
->mutex
);
2678 spin_unlock(&delayed_refs
->lock
);
2682 static noinline
int check_committed_ref(struct btrfs_trans_handle
*trans
,
2683 struct btrfs_root
*root
,
2684 struct btrfs_path
*path
,
2685 u64 objectid
, u64 offset
, u64 bytenr
)
2687 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
2688 struct extent_buffer
*leaf
;
2689 struct btrfs_extent_data_ref
*ref
;
2690 struct btrfs_extent_inline_ref
*iref
;
2691 struct btrfs_extent_item
*ei
;
2692 struct btrfs_key key
;
2696 key
.objectid
= bytenr
;
2697 key
.offset
= (u64
)-1;
2698 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2700 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
2703 BUG_ON(ret
== 0); /* Corruption */
2706 if (path
->slots
[0] == 0)
2710 leaf
= path
->nodes
[0];
2711 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2713 if (key
.objectid
!= bytenr
|| key
.type
!= BTRFS_EXTENT_ITEM_KEY
)
2717 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2718 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2719 if (item_size
< sizeof(*ei
)) {
2720 WARN_ON(item_size
!= sizeof(struct btrfs_extent_item_v0
));
2724 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
2726 if (item_size
!= sizeof(*ei
) +
2727 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY
))
2730 if (btrfs_extent_generation(leaf
, ei
) <=
2731 btrfs_root_last_snapshot(&root
->root_item
))
2734 iref
= (struct btrfs_extent_inline_ref
*)(ei
+ 1);
2735 if (btrfs_extent_inline_ref_type(leaf
, iref
) !=
2736 BTRFS_EXTENT_DATA_REF_KEY
)
2739 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
2740 if (btrfs_extent_refs(leaf
, ei
) !=
2741 btrfs_extent_data_ref_count(leaf
, ref
) ||
2742 btrfs_extent_data_ref_root(leaf
, ref
) !=
2743 root
->root_key
.objectid
||
2744 btrfs_extent_data_ref_objectid(leaf
, ref
) != objectid
||
2745 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
2753 int btrfs_cross_ref_exist(struct btrfs_trans_handle
*trans
,
2754 struct btrfs_root
*root
,
2755 u64 objectid
, u64 offset
, u64 bytenr
)
2757 struct btrfs_path
*path
;
2761 path
= btrfs_alloc_path();
2766 ret
= check_committed_ref(trans
, root
, path
, objectid
,
2768 if (ret
&& ret
!= -ENOENT
)
2771 ret2
= check_delayed_ref(trans
, root
, path
, objectid
,
2773 } while (ret2
== -EAGAIN
);
2775 if (ret2
&& ret2
!= -ENOENT
) {
2780 if (ret
!= -ENOENT
|| ret2
!= -ENOENT
)
2783 btrfs_free_path(path
);
2784 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
2789 static int __btrfs_mod_ref(struct btrfs_trans_handle
*trans
,
2790 struct btrfs_root
*root
,
2791 struct extent_buffer
*buf
,
2792 int full_backref
, int inc
, int for_cow
)
2799 struct btrfs_key key
;
2800 struct btrfs_file_extent_item
*fi
;
2804 int (*process_func
)(struct btrfs_trans_handle
*, struct btrfs_root
*,
2805 u64
, u64
, u64
, u64
, u64
, u64
, int);
2807 ref_root
= btrfs_header_owner(buf
);
2808 nritems
= btrfs_header_nritems(buf
);
2809 level
= btrfs_header_level(buf
);
2811 if (!root
->ref_cows
&& level
== 0)
2815 process_func
= btrfs_inc_extent_ref
;
2817 process_func
= btrfs_free_extent
;
2820 parent
= buf
->start
;
2824 for (i
= 0; i
< nritems
; i
++) {
2826 btrfs_item_key_to_cpu(buf
, &key
, i
);
2827 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
2829 fi
= btrfs_item_ptr(buf
, i
,
2830 struct btrfs_file_extent_item
);
2831 if (btrfs_file_extent_type(buf
, fi
) ==
2832 BTRFS_FILE_EXTENT_INLINE
)
2834 bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
2838 num_bytes
= btrfs_file_extent_disk_num_bytes(buf
, fi
);
2839 key
.offset
-= btrfs_file_extent_offset(buf
, fi
);
2840 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
2841 parent
, ref_root
, key
.objectid
,
2842 key
.offset
, for_cow
);
2846 bytenr
= btrfs_node_blockptr(buf
, i
);
2847 num_bytes
= btrfs_level_size(root
, level
- 1);
2848 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
2849 parent
, ref_root
, level
- 1, 0,
2860 int btrfs_inc_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2861 struct extent_buffer
*buf
, int full_backref
, int for_cow
)
2863 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 1, for_cow
);
2866 int btrfs_dec_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2867 struct extent_buffer
*buf
, int full_backref
, int for_cow
)
2869 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 0, for_cow
);
2872 static int write_one_cache_group(struct btrfs_trans_handle
*trans
,
2873 struct btrfs_root
*root
,
2874 struct btrfs_path
*path
,
2875 struct btrfs_block_group_cache
*cache
)
2878 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
2880 struct extent_buffer
*leaf
;
2882 ret
= btrfs_search_slot(trans
, extent_root
, &cache
->key
, path
, 0, 1);
2885 BUG_ON(ret
); /* Corruption */
2887 leaf
= path
->nodes
[0];
2888 bi
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
2889 write_extent_buffer(leaf
, &cache
->item
, bi
, sizeof(cache
->item
));
2890 btrfs_mark_buffer_dirty(leaf
);
2891 btrfs_release_path(path
);
2894 btrfs_abort_transaction(trans
, root
, ret
);
2901 static struct btrfs_block_group_cache
*
2902 next_block_group(struct btrfs_root
*root
,
2903 struct btrfs_block_group_cache
*cache
)
2905 struct rb_node
*node
;
2906 spin_lock(&root
->fs_info
->block_group_cache_lock
);
2907 node
= rb_next(&cache
->cache_node
);
2908 btrfs_put_block_group(cache
);
2910 cache
= rb_entry(node
, struct btrfs_block_group_cache
,
2912 btrfs_get_block_group(cache
);
2915 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
2919 static int cache_save_setup(struct btrfs_block_group_cache
*block_group
,
2920 struct btrfs_trans_handle
*trans
,
2921 struct btrfs_path
*path
)
2923 struct btrfs_root
*root
= block_group
->fs_info
->tree_root
;
2924 struct inode
*inode
= NULL
;
2926 int dcs
= BTRFS_DC_ERROR
;
2932 * If this block group is smaller than 100 megs don't bother caching the
2935 if (block_group
->key
.offset
< (100 * 1024 * 1024)) {
2936 spin_lock(&block_group
->lock
);
2937 block_group
->disk_cache_state
= BTRFS_DC_WRITTEN
;
2938 spin_unlock(&block_group
->lock
);
2943 inode
= lookup_free_space_inode(root
, block_group
, path
);
2944 if (IS_ERR(inode
) && PTR_ERR(inode
) != -ENOENT
) {
2945 ret
= PTR_ERR(inode
);
2946 btrfs_release_path(path
);
2950 if (IS_ERR(inode
)) {
2954 if (block_group
->ro
)
2957 ret
= create_free_space_inode(root
, trans
, block_group
, path
);
2963 /* We've already setup this transaction, go ahead and exit */
2964 if (block_group
->cache_generation
== trans
->transid
&&
2965 i_size_read(inode
)) {
2966 dcs
= BTRFS_DC_SETUP
;
2971 * We want to set the generation to 0, that way if anything goes wrong
2972 * from here on out we know not to trust this cache when we load up next
2975 BTRFS_I(inode
)->generation
= 0;
2976 ret
= btrfs_update_inode(trans
, root
, inode
);
2979 if (i_size_read(inode
) > 0) {
2980 ret
= btrfs_truncate_free_space_cache(root
, trans
, path
,
2986 spin_lock(&block_group
->lock
);
2987 if (block_group
->cached
!= BTRFS_CACHE_FINISHED
||
2988 !btrfs_test_opt(root
, SPACE_CACHE
)) {
2990 * don't bother trying to write stuff out _if_
2991 * a) we're not cached,
2992 * b) we're with nospace_cache mount option.
2994 dcs
= BTRFS_DC_WRITTEN
;
2995 spin_unlock(&block_group
->lock
);
2998 spin_unlock(&block_group
->lock
);
3001 * Try to preallocate enough space based on how big the block group is.
3002 * Keep in mind this has to include any pinned space which could end up
3003 * taking up quite a bit since it's not folded into the other space
3006 num_pages
= (int)div64_u64(block_group
->key
.offset
, 256 * 1024 * 1024);
3011 num_pages
*= PAGE_CACHE_SIZE
;
3013 ret
= btrfs_check_data_free_space(inode
, num_pages
);
3017 ret
= btrfs_prealloc_file_range_trans(inode
, trans
, 0, 0, num_pages
,
3018 num_pages
, num_pages
,
3021 dcs
= BTRFS_DC_SETUP
;
3022 btrfs_free_reserved_data_space(inode
, num_pages
);
3027 btrfs_release_path(path
);
3029 spin_lock(&block_group
->lock
);
3030 if (!ret
&& dcs
== BTRFS_DC_SETUP
)
3031 block_group
->cache_generation
= trans
->transid
;
3032 block_group
->disk_cache_state
= dcs
;
3033 spin_unlock(&block_group
->lock
);
3038 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle
*trans
,
3039 struct btrfs_root
*root
)
3041 struct btrfs_block_group_cache
*cache
;
3043 struct btrfs_path
*path
;
3046 path
= btrfs_alloc_path();
3052 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
3054 if (cache
->disk_cache_state
== BTRFS_DC_CLEAR
)
3056 cache
= next_block_group(root
, cache
);
3064 err
= cache_save_setup(cache
, trans
, path
);
3065 last
= cache
->key
.objectid
+ cache
->key
.offset
;
3066 btrfs_put_block_group(cache
);
3071 err
= btrfs_run_delayed_refs(trans
, root
,
3073 if (err
) /* File system offline */
3077 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
3079 if (cache
->disk_cache_state
== BTRFS_DC_CLEAR
) {
3080 btrfs_put_block_group(cache
);
3086 cache
= next_block_group(root
, cache
);
3095 if (cache
->disk_cache_state
== BTRFS_DC_SETUP
)
3096 cache
->disk_cache_state
= BTRFS_DC_NEED_WRITE
;
3098 last
= cache
->key
.objectid
+ cache
->key
.offset
;
3100 err
= write_one_cache_group(trans
, root
, path
, cache
);
3101 if (err
) /* File system offline */
3104 btrfs_put_block_group(cache
);
3109 * I don't think this is needed since we're just marking our
3110 * preallocated extent as written, but just in case it can't
3114 err
= btrfs_run_delayed_refs(trans
, root
,
3116 if (err
) /* File system offline */
3120 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
3123 * Really this shouldn't happen, but it could if we
3124 * couldn't write the entire preallocated extent and
3125 * splitting the extent resulted in a new block.
3128 btrfs_put_block_group(cache
);
3131 if (cache
->disk_cache_state
== BTRFS_DC_NEED_WRITE
)
3133 cache
= next_block_group(root
, cache
);
3142 err
= btrfs_write_out_cache(root
, trans
, cache
, path
);
3145 * If we didn't have an error then the cache state is still
3146 * NEED_WRITE, so we can set it to WRITTEN.
3148 if (!err
&& cache
->disk_cache_state
== BTRFS_DC_NEED_WRITE
)
3149 cache
->disk_cache_state
= BTRFS_DC_WRITTEN
;
3150 last
= cache
->key
.objectid
+ cache
->key
.offset
;
3151 btrfs_put_block_group(cache
);
3155 btrfs_free_path(path
);
3159 int btrfs_extent_readonly(struct btrfs_root
*root
, u64 bytenr
)
3161 struct btrfs_block_group_cache
*block_group
;
3164 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
3165 if (!block_group
|| block_group
->ro
)
3168 btrfs_put_block_group(block_group
);
3172 static int update_space_info(struct btrfs_fs_info
*info
, u64 flags
,
3173 u64 total_bytes
, u64 bytes_used
,
3174 struct btrfs_space_info
**space_info
)
3176 struct btrfs_space_info
*found
;
3180 if (flags
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
3181 BTRFS_BLOCK_GROUP_RAID10
))
3186 found
= __find_space_info(info
, flags
);
3188 spin_lock(&found
->lock
);
3189 found
->total_bytes
+= total_bytes
;
3190 found
->disk_total
+= total_bytes
* factor
;
3191 found
->bytes_used
+= bytes_used
;
3192 found
->disk_used
+= bytes_used
* factor
;
3194 spin_unlock(&found
->lock
);
3195 *space_info
= found
;
3198 found
= kzalloc(sizeof(*found
), GFP_NOFS
);
3202 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++)
3203 INIT_LIST_HEAD(&found
->block_groups
[i
]);
3204 init_rwsem(&found
->groups_sem
);
3205 spin_lock_init(&found
->lock
);
3206 found
->flags
= flags
& BTRFS_BLOCK_GROUP_TYPE_MASK
;
3207 found
->total_bytes
= total_bytes
;
3208 found
->disk_total
= total_bytes
* factor
;
3209 found
->bytes_used
= bytes_used
;
3210 found
->disk_used
= bytes_used
* factor
;
3211 found
->bytes_pinned
= 0;
3212 found
->bytes_reserved
= 0;
3213 found
->bytes_readonly
= 0;
3214 found
->bytes_may_use
= 0;
3216 found
->force_alloc
= CHUNK_ALLOC_NO_FORCE
;
3217 found
->chunk_alloc
= 0;
3219 init_waitqueue_head(&found
->wait
);
3220 *space_info
= found
;
3221 list_add_rcu(&found
->list
, &info
->space_info
);
3222 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3223 info
->data_sinfo
= found
;
3227 static void set_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
3229 u64 extra_flags
= chunk_to_extended(flags
) &
3230 BTRFS_EXTENDED_PROFILE_MASK
;
3232 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3233 fs_info
->avail_data_alloc_bits
|= extra_flags
;
3234 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
3235 fs_info
->avail_metadata_alloc_bits
|= extra_flags
;
3236 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
3237 fs_info
->avail_system_alloc_bits
|= extra_flags
;
3241 * returns target flags in extended format or 0 if restripe for this
3242 * chunk_type is not in progress
3244 * should be called with either volume_mutex or balance_lock held
3246 static u64
get_restripe_target(struct btrfs_fs_info
*fs_info
, u64 flags
)
3248 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3254 if (flags
& BTRFS_BLOCK_GROUP_DATA
&&
3255 bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3256 target
= BTRFS_BLOCK_GROUP_DATA
| bctl
->data
.target
;
3257 } else if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
&&
3258 bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3259 target
= BTRFS_BLOCK_GROUP_SYSTEM
| bctl
->sys
.target
;
3260 } else if (flags
& BTRFS_BLOCK_GROUP_METADATA
&&
3261 bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3262 target
= BTRFS_BLOCK_GROUP_METADATA
| bctl
->meta
.target
;
3269 * @flags: available profiles in extended format (see ctree.h)
3271 * Returns reduced profile in chunk format. If profile changing is in
3272 * progress (either running or paused) picks the target profile (if it's
3273 * already available), otherwise falls back to plain reducing.
3275 u64
btrfs_reduce_alloc_profile(struct btrfs_root
*root
, u64 flags
)
3278 * we add in the count of missing devices because we want
3279 * to make sure that any RAID levels on a degraded FS
3280 * continue to be honored.
3282 u64 num_devices
= root
->fs_info
->fs_devices
->rw_devices
+
3283 root
->fs_info
->fs_devices
->missing_devices
;
3287 * see if restripe for this chunk_type is in progress, if so
3288 * try to reduce to the target profile
3290 spin_lock(&root
->fs_info
->balance_lock
);
3291 target
= get_restripe_target(root
->fs_info
, flags
);
3293 /* pick target profile only if it's already available */
3294 if ((flags
& target
) & BTRFS_EXTENDED_PROFILE_MASK
) {
3295 spin_unlock(&root
->fs_info
->balance_lock
);
3296 return extended_to_chunk(target
);
3299 spin_unlock(&root
->fs_info
->balance_lock
);
3301 if (num_devices
== 1)
3302 flags
&= ~(BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID0
);
3303 if (num_devices
< 4)
3304 flags
&= ~BTRFS_BLOCK_GROUP_RAID10
;
3306 if ((flags
& BTRFS_BLOCK_GROUP_DUP
) &&
3307 (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
3308 BTRFS_BLOCK_GROUP_RAID10
))) {
3309 flags
&= ~BTRFS_BLOCK_GROUP_DUP
;
3312 if ((flags
& BTRFS_BLOCK_GROUP_RAID1
) &&
3313 (flags
& BTRFS_BLOCK_GROUP_RAID10
)) {
3314 flags
&= ~BTRFS_BLOCK_GROUP_RAID1
;
3317 if ((flags
& BTRFS_BLOCK_GROUP_RAID0
) &&
3318 ((flags
& BTRFS_BLOCK_GROUP_RAID1
) |
3319 (flags
& BTRFS_BLOCK_GROUP_RAID10
) |
3320 (flags
& BTRFS_BLOCK_GROUP_DUP
))) {
3321 flags
&= ~BTRFS_BLOCK_GROUP_RAID0
;
3324 return extended_to_chunk(flags
);
3327 static u64
get_alloc_profile(struct btrfs_root
*root
, u64 flags
)
3329 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3330 flags
|= root
->fs_info
->avail_data_alloc_bits
;
3331 else if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
3332 flags
|= root
->fs_info
->avail_system_alloc_bits
;
3333 else if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
3334 flags
|= root
->fs_info
->avail_metadata_alloc_bits
;
3336 return btrfs_reduce_alloc_profile(root
, flags
);
3339 u64
btrfs_get_alloc_profile(struct btrfs_root
*root
, int data
)
3344 flags
= BTRFS_BLOCK_GROUP_DATA
;
3345 else if (root
== root
->fs_info
->chunk_root
)
3346 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
3348 flags
= BTRFS_BLOCK_GROUP_METADATA
;
3350 return get_alloc_profile(root
, flags
);
3354 * This will check the space that the inode allocates from to make sure we have
3355 * enough space for bytes.
3357 int btrfs_check_data_free_space(struct inode
*inode
, u64 bytes
)
3359 struct btrfs_space_info
*data_sinfo
;
3360 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3361 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3363 int ret
= 0, committed
= 0, alloc_chunk
= 1;
3365 /* make sure bytes are sectorsize aligned */
3366 bytes
= (bytes
+ root
->sectorsize
- 1) & ~((u64
)root
->sectorsize
- 1);
3368 if (root
== root
->fs_info
->tree_root
||
3369 BTRFS_I(inode
)->location
.objectid
== BTRFS_FREE_INO_OBJECTID
) {
3374 data_sinfo
= fs_info
->data_sinfo
;
3379 /* make sure we have enough space to handle the data first */
3380 spin_lock(&data_sinfo
->lock
);
3381 used
= data_sinfo
->bytes_used
+ data_sinfo
->bytes_reserved
+
3382 data_sinfo
->bytes_pinned
+ data_sinfo
->bytes_readonly
+
3383 data_sinfo
->bytes_may_use
;
3385 if (used
+ bytes
> data_sinfo
->total_bytes
) {
3386 struct btrfs_trans_handle
*trans
;
3389 * if we don't have enough free bytes in this space then we need
3390 * to alloc a new chunk.
3392 if (!data_sinfo
->full
&& alloc_chunk
) {
3395 data_sinfo
->force_alloc
= CHUNK_ALLOC_FORCE
;
3396 spin_unlock(&data_sinfo
->lock
);
3398 alloc_target
= btrfs_get_alloc_profile(root
, 1);
3399 trans
= btrfs_join_transaction(root
);
3401 return PTR_ERR(trans
);
3403 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
3405 CHUNK_ALLOC_NO_FORCE
);
3406 btrfs_end_transaction(trans
, root
);
3415 data_sinfo
= fs_info
->data_sinfo
;
3421 * If we have less pinned bytes than we want to allocate then
3422 * don't bother committing the transaction, it won't help us.
3424 if (data_sinfo
->bytes_pinned
< bytes
)
3426 spin_unlock(&data_sinfo
->lock
);
3428 /* commit the current transaction and try again */
3431 !atomic_read(&root
->fs_info
->open_ioctl_trans
)) {
3433 trans
= btrfs_join_transaction(root
);
3435 return PTR_ERR(trans
);
3436 ret
= btrfs_commit_transaction(trans
, root
);
3444 data_sinfo
->bytes_may_use
+= bytes
;
3445 trace_btrfs_space_reservation(root
->fs_info
, "space_info",
3446 data_sinfo
->flags
, bytes
, 1);
3447 spin_unlock(&data_sinfo
->lock
);
3453 * Called if we need to clear a data reservation for this inode.
3455 void btrfs_free_reserved_data_space(struct inode
*inode
, u64 bytes
)
3457 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3458 struct btrfs_space_info
*data_sinfo
;
3460 /* make sure bytes are sectorsize aligned */
3461 bytes
= (bytes
+ root
->sectorsize
- 1) & ~((u64
)root
->sectorsize
- 1);
3463 data_sinfo
= root
->fs_info
->data_sinfo
;
3464 spin_lock(&data_sinfo
->lock
);
3465 data_sinfo
->bytes_may_use
-= bytes
;
3466 trace_btrfs_space_reservation(root
->fs_info
, "space_info",
3467 data_sinfo
->flags
, bytes
, 0);
3468 spin_unlock(&data_sinfo
->lock
);
3471 static void force_metadata_allocation(struct btrfs_fs_info
*info
)
3473 struct list_head
*head
= &info
->space_info
;
3474 struct btrfs_space_info
*found
;
3477 list_for_each_entry_rcu(found
, head
, list
) {
3478 if (found
->flags
& BTRFS_BLOCK_GROUP_METADATA
)
3479 found
->force_alloc
= CHUNK_ALLOC_FORCE
;
3484 static int should_alloc_chunk(struct btrfs_root
*root
,
3485 struct btrfs_space_info
*sinfo
, int force
)
3487 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
3488 u64 num_bytes
= sinfo
->total_bytes
- sinfo
->bytes_readonly
;
3489 u64 num_allocated
= sinfo
->bytes_used
+ sinfo
->bytes_reserved
;
3492 if (force
== CHUNK_ALLOC_FORCE
)
3496 * We need to take into account the global rsv because for all intents
3497 * and purposes it's used space. Don't worry about locking the
3498 * global_rsv, it doesn't change except when the transaction commits.
3500 if (sinfo
->flags
& BTRFS_BLOCK_GROUP_METADATA
)
3501 num_allocated
+= global_rsv
->size
;
3504 * in limited mode, we want to have some free space up to
3505 * about 1% of the FS size.
3507 if (force
== CHUNK_ALLOC_LIMITED
) {
3508 thresh
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
3509 thresh
= max_t(u64
, 64 * 1024 * 1024,
3510 div_factor_fine(thresh
, 1));
3512 if (num_bytes
- num_allocated
< thresh
)
3516 if (num_allocated
+ 2 * 1024 * 1024 < div_factor(num_bytes
, 8))
3521 static u64
get_system_chunk_thresh(struct btrfs_root
*root
, u64 type
)
3525 if (type
& BTRFS_BLOCK_GROUP_RAID10
||
3526 type
& BTRFS_BLOCK_GROUP_RAID0
)
3527 num_dev
= root
->fs_info
->fs_devices
->rw_devices
;
3528 else if (type
& BTRFS_BLOCK_GROUP_RAID1
)
3531 num_dev
= 1; /* DUP or single */
3533 /* metadata for updaing devices and chunk tree */
3534 return btrfs_calc_trans_metadata_size(root
, num_dev
+ 1);
3537 static void check_system_chunk(struct btrfs_trans_handle
*trans
,
3538 struct btrfs_root
*root
, u64 type
)
3540 struct btrfs_space_info
*info
;
3544 info
= __find_space_info(root
->fs_info
, BTRFS_BLOCK_GROUP_SYSTEM
);
3545 spin_lock(&info
->lock
);
3546 left
= info
->total_bytes
- info
->bytes_used
- info
->bytes_pinned
-
3547 info
->bytes_reserved
- info
->bytes_readonly
;
3548 spin_unlock(&info
->lock
);
3550 thresh
= get_system_chunk_thresh(root
, type
);
3551 if (left
< thresh
&& btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
3552 printk(KERN_INFO
"left=%llu, need=%llu, flags=%llu\n",
3553 left
, thresh
, type
);
3554 dump_space_info(info
, 0, 0);
3557 if (left
< thresh
) {
3560 flags
= btrfs_get_alloc_profile(root
->fs_info
->chunk_root
, 0);
3561 btrfs_alloc_chunk(trans
, root
, flags
);
3565 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
3566 struct btrfs_root
*extent_root
, u64 flags
, int force
)
3568 struct btrfs_space_info
*space_info
;
3569 struct btrfs_fs_info
*fs_info
= extent_root
->fs_info
;
3570 int wait_for_alloc
= 0;
3573 space_info
= __find_space_info(extent_root
->fs_info
, flags
);
3575 ret
= update_space_info(extent_root
->fs_info
, flags
,
3577 BUG_ON(ret
); /* -ENOMEM */
3579 BUG_ON(!space_info
); /* Logic error */
3582 spin_lock(&space_info
->lock
);
3583 if (force
< space_info
->force_alloc
)
3584 force
= space_info
->force_alloc
;
3585 if (space_info
->full
) {
3586 spin_unlock(&space_info
->lock
);
3590 if (!should_alloc_chunk(extent_root
, space_info
, force
)) {
3591 spin_unlock(&space_info
->lock
);
3593 } else if (space_info
->chunk_alloc
) {
3596 space_info
->chunk_alloc
= 1;
3599 spin_unlock(&space_info
->lock
);
3601 mutex_lock(&fs_info
->chunk_mutex
);
3604 * The chunk_mutex is held throughout the entirety of a chunk
3605 * allocation, so once we've acquired the chunk_mutex we know that the
3606 * other guy is done and we need to recheck and see if we should
3609 if (wait_for_alloc
) {
3610 mutex_unlock(&fs_info
->chunk_mutex
);
3616 * If we have mixed data/metadata chunks we want to make sure we keep
3617 * allocating mixed chunks instead of individual chunks.
3619 if (btrfs_mixed_space_info(space_info
))
3620 flags
|= (BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
);
3623 * if we're doing a data chunk, go ahead and make sure that
3624 * we keep a reasonable number of metadata chunks allocated in the
3627 if (flags
& BTRFS_BLOCK_GROUP_DATA
&& fs_info
->metadata_ratio
) {
3628 fs_info
->data_chunk_allocations
++;
3629 if (!(fs_info
->data_chunk_allocations
%
3630 fs_info
->metadata_ratio
))
3631 force_metadata_allocation(fs_info
);
3635 * Check if we have enough space in SYSTEM chunk because we may need
3636 * to update devices.
3638 check_system_chunk(trans
, extent_root
, flags
);
3640 ret
= btrfs_alloc_chunk(trans
, extent_root
, flags
);
3641 if (ret
< 0 && ret
!= -ENOSPC
)
3644 spin_lock(&space_info
->lock
);
3646 space_info
->full
= 1;
3650 space_info
->force_alloc
= CHUNK_ALLOC_NO_FORCE
;
3651 space_info
->chunk_alloc
= 0;
3652 spin_unlock(&space_info
->lock
);
3654 mutex_unlock(&fs_info
->chunk_mutex
);
3658 static int can_overcommit(struct btrfs_root
*root
,
3659 struct btrfs_space_info
*space_info
, u64 bytes
,
3660 enum btrfs_reserve_flush_enum flush
)
3662 u64 profile
= btrfs_get_alloc_profile(root
, 0);
3666 used
= space_info
->bytes_used
+ space_info
->bytes_reserved
+
3667 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
3668 space_info
->bytes_may_use
;
3670 spin_lock(&root
->fs_info
->free_chunk_lock
);
3671 avail
= root
->fs_info
->free_chunk_space
;
3672 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3675 * If we have dup, raid1 or raid10 then only half of the free
3676 * space is actually useable.
3678 if (profile
& (BTRFS_BLOCK_GROUP_DUP
|
3679 BTRFS_BLOCK_GROUP_RAID1
|
3680 BTRFS_BLOCK_GROUP_RAID10
))
3684 * If we aren't flushing all things, let us overcommit up to
3685 * 1/2th of the space. If we can flush, don't let us overcommit
3686 * too much, let it overcommit up to 1/8 of the space.
3688 if (flush
== BTRFS_RESERVE_FLUSH_ALL
)
3693 if (used
+ bytes
< space_info
->total_bytes
+ avail
)
3698 static inline int writeback_inodes_sb_nr_if_idle_safe(struct super_block
*sb
,
3699 unsigned long nr_pages
,
3700 enum wb_reason reason
)
3702 /* the flusher is dealing with the dirty inodes now. */
3703 if (writeback_in_progress(sb
->s_bdi
))
3706 if (down_read_trylock(&sb
->s_umount
)) {
3707 writeback_inodes_sb_nr(sb
, nr_pages
, reason
);
3708 up_read(&sb
->s_umount
);
3715 void btrfs_writeback_inodes_sb_nr(struct btrfs_root
*root
,
3716 unsigned long nr_pages
)
3718 struct super_block
*sb
= root
->fs_info
->sb
;
3721 /* If we can not start writeback, just sync all the delalloc file. */
3722 started
= writeback_inodes_sb_nr_if_idle_safe(sb
, nr_pages
,
3723 WB_REASON_FS_FREE_SPACE
);
3726 * We needn't worry the filesystem going from r/w to r/o though
3727 * we don't acquire ->s_umount mutex, because the filesystem
3728 * should guarantee the delalloc inodes list be empty after
3729 * the filesystem is readonly(all dirty pages are written to
3732 btrfs_start_delalloc_inodes(root
, 0);
3733 btrfs_wait_ordered_extents(root
, 0);
3738 * shrink metadata reservation for delalloc
3740 static void shrink_delalloc(struct btrfs_root
*root
, u64 to_reclaim
, u64 orig
,
3743 struct btrfs_block_rsv
*block_rsv
;
3744 struct btrfs_space_info
*space_info
;
3745 struct btrfs_trans_handle
*trans
;
3749 unsigned long nr_pages
= (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT
;
3751 enum btrfs_reserve_flush_enum flush
;
3753 trans
= (struct btrfs_trans_handle
*)current
->journal_info
;
3754 block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
3755 space_info
= block_rsv
->space_info
;
3758 delalloc_bytes
= root
->fs_info
->delalloc_bytes
;
3759 if (delalloc_bytes
== 0) {
3762 btrfs_wait_ordered_extents(root
, 0);
3766 while (delalloc_bytes
&& loops
< 3) {
3767 max_reclaim
= min(delalloc_bytes
, to_reclaim
);
3768 nr_pages
= max_reclaim
>> PAGE_CACHE_SHIFT
;
3769 btrfs_writeback_inodes_sb_nr(root
, nr_pages
);
3771 * We need to wait for the async pages to actually start before
3774 wait_event(root
->fs_info
->async_submit_wait
,
3775 !atomic_read(&root
->fs_info
->async_delalloc_pages
));
3778 flush
= BTRFS_RESERVE_FLUSH_ALL
;
3780 flush
= BTRFS_RESERVE_NO_FLUSH
;
3781 spin_lock(&space_info
->lock
);
3782 if (can_overcommit(root
, space_info
, orig
, flush
)) {
3783 spin_unlock(&space_info
->lock
);
3786 spin_unlock(&space_info
->lock
);
3789 if (wait_ordered
&& !trans
) {
3790 btrfs_wait_ordered_extents(root
, 0);
3792 time_left
= schedule_timeout_killable(1);
3797 delalloc_bytes
= root
->fs_info
->delalloc_bytes
;
3802 * maybe_commit_transaction - possibly commit the transaction if its ok to
3803 * @root - the root we're allocating for
3804 * @bytes - the number of bytes we want to reserve
3805 * @force - force the commit
3807 * This will check to make sure that committing the transaction will actually
3808 * get us somewhere and then commit the transaction if it does. Otherwise it
3809 * will return -ENOSPC.
3811 static int may_commit_transaction(struct btrfs_root
*root
,
3812 struct btrfs_space_info
*space_info
,
3813 u64 bytes
, int force
)
3815 struct btrfs_block_rsv
*delayed_rsv
= &root
->fs_info
->delayed_block_rsv
;
3816 struct btrfs_trans_handle
*trans
;
3818 trans
= (struct btrfs_trans_handle
*)current
->journal_info
;
3825 /* See if there is enough pinned space to make this reservation */
3826 spin_lock(&space_info
->lock
);
3827 if (space_info
->bytes_pinned
>= bytes
) {
3828 spin_unlock(&space_info
->lock
);
3831 spin_unlock(&space_info
->lock
);
3834 * See if there is some space in the delayed insertion reservation for
3837 if (space_info
!= delayed_rsv
->space_info
)
3840 spin_lock(&space_info
->lock
);
3841 spin_lock(&delayed_rsv
->lock
);
3842 if (space_info
->bytes_pinned
+ delayed_rsv
->size
< bytes
) {
3843 spin_unlock(&delayed_rsv
->lock
);
3844 spin_unlock(&space_info
->lock
);
3847 spin_unlock(&delayed_rsv
->lock
);
3848 spin_unlock(&space_info
->lock
);
3851 trans
= btrfs_join_transaction(root
);
3855 return btrfs_commit_transaction(trans
, root
);
3859 FLUSH_DELAYED_ITEMS_NR
= 1,
3860 FLUSH_DELAYED_ITEMS
= 2,
3862 FLUSH_DELALLOC_WAIT
= 4,
3867 static int flush_space(struct btrfs_root
*root
,
3868 struct btrfs_space_info
*space_info
, u64 num_bytes
,
3869 u64 orig_bytes
, int state
)
3871 struct btrfs_trans_handle
*trans
;
3876 case FLUSH_DELAYED_ITEMS_NR
:
3877 case FLUSH_DELAYED_ITEMS
:
3878 if (state
== FLUSH_DELAYED_ITEMS_NR
) {
3879 u64 bytes
= btrfs_calc_trans_metadata_size(root
, 1);
3881 nr
= (int)div64_u64(num_bytes
, bytes
);
3888 trans
= btrfs_join_transaction(root
);
3889 if (IS_ERR(trans
)) {
3890 ret
= PTR_ERR(trans
);
3893 ret
= btrfs_run_delayed_items_nr(trans
, root
, nr
);
3894 btrfs_end_transaction(trans
, root
);
3896 case FLUSH_DELALLOC
:
3897 case FLUSH_DELALLOC_WAIT
:
3898 shrink_delalloc(root
, num_bytes
, orig_bytes
,
3899 state
== FLUSH_DELALLOC_WAIT
);
3902 trans
= btrfs_join_transaction(root
);
3903 if (IS_ERR(trans
)) {
3904 ret
= PTR_ERR(trans
);
3907 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
3908 btrfs_get_alloc_profile(root
, 0),
3909 CHUNK_ALLOC_NO_FORCE
);
3910 btrfs_end_transaction(trans
, root
);
3915 ret
= may_commit_transaction(root
, space_info
, orig_bytes
, 0);
3925 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
3926 * @root - the root we're allocating for
3927 * @block_rsv - the block_rsv we're allocating for
3928 * @orig_bytes - the number of bytes we want
3929 * @flush - wether or not we can flush to make our reservation
3931 * This will reserve orgi_bytes number of bytes from the space info associated
3932 * with the block_rsv. If there is not enough space it will make an attempt to
3933 * flush out space to make room. It will do this by flushing delalloc if
3934 * possible or committing the transaction. If flush is 0 then no attempts to
3935 * regain reservations will be made and this will fail if there is not enough
3938 static int reserve_metadata_bytes(struct btrfs_root
*root
,
3939 struct btrfs_block_rsv
*block_rsv
,
3941 enum btrfs_reserve_flush_enum flush
)
3943 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
3945 u64 num_bytes
= orig_bytes
;
3946 int flush_state
= FLUSH_DELAYED_ITEMS_NR
;
3948 bool flushing
= false;
3952 spin_lock(&space_info
->lock
);
3954 * We only want to wait if somebody other than us is flushing and we
3955 * are actually allowed to flush all things.
3957 while (flush
== BTRFS_RESERVE_FLUSH_ALL
&& !flushing
&&
3958 space_info
->flush
) {
3959 spin_unlock(&space_info
->lock
);
3961 * If we have a trans handle we can't wait because the flusher
3962 * may have to commit the transaction, which would mean we would
3963 * deadlock since we are waiting for the flusher to finish, but
3964 * hold the current transaction open.
3966 if (current
->journal_info
)
3968 ret
= wait_event_killable(space_info
->wait
, !space_info
->flush
);
3969 /* Must have been killed, return */
3973 spin_lock(&space_info
->lock
);
3977 used
= space_info
->bytes_used
+ space_info
->bytes_reserved
+
3978 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
3979 space_info
->bytes_may_use
;
3982 * The idea here is that we've not already over-reserved the block group
3983 * then we can go ahead and save our reservation first and then start
3984 * flushing if we need to. Otherwise if we've already overcommitted
3985 * lets start flushing stuff first and then come back and try to make
3988 if (used
<= space_info
->total_bytes
) {
3989 if (used
+ orig_bytes
<= space_info
->total_bytes
) {
3990 space_info
->bytes_may_use
+= orig_bytes
;
3991 trace_btrfs_space_reservation(root
->fs_info
,
3992 "space_info", space_info
->flags
, orig_bytes
, 1);
3996 * Ok set num_bytes to orig_bytes since we aren't
3997 * overocmmitted, this way we only try and reclaim what
4000 num_bytes
= orig_bytes
;
4004 * Ok we're over committed, set num_bytes to the overcommitted
4005 * amount plus the amount of bytes that we need for this
4008 num_bytes
= used
- space_info
->total_bytes
+
4012 if (ret
&& can_overcommit(root
, space_info
, orig_bytes
, flush
)) {
4013 space_info
->bytes_may_use
+= orig_bytes
;
4014 trace_btrfs_space_reservation(root
->fs_info
, "space_info",
4015 space_info
->flags
, orig_bytes
,
4021 * Couldn't make our reservation, save our place so while we're trying
4022 * to reclaim space we can actually use it instead of somebody else
4023 * stealing it from us.
4025 * We make the other tasks wait for the flush only when we can flush
4028 if (ret
&& flush
!= BTRFS_RESERVE_NO_FLUSH
) {
4030 space_info
->flush
= 1;
4033 spin_unlock(&space_info
->lock
);
4035 if (!ret
|| flush
== BTRFS_RESERVE_NO_FLUSH
)
4038 ret
= flush_space(root
, space_info
, num_bytes
, orig_bytes
,
4043 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4044 * would happen. So skip delalloc flush.
4046 if (flush
== BTRFS_RESERVE_FLUSH_LIMIT
&&
4047 (flush_state
== FLUSH_DELALLOC
||
4048 flush_state
== FLUSH_DELALLOC_WAIT
))
4049 flush_state
= ALLOC_CHUNK
;
4053 else if (flush
== BTRFS_RESERVE_FLUSH_LIMIT
&&
4054 flush_state
< COMMIT_TRANS
)
4056 else if (flush
== BTRFS_RESERVE_FLUSH_ALL
&&
4057 flush_state
<= COMMIT_TRANS
)
4062 spin_lock(&space_info
->lock
);
4063 space_info
->flush
= 0;
4064 wake_up_all(&space_info
->wait
);
4065 spin_unlock(&space_info
->lock
);
4070 static struct btrfs_block_rsv
*get_block_rsv(
4071 const struct btrfs_trans_handle
*trans
,
4072 const struct btrfs_root
*root
)
4074 struct btrfs_block_rsv
*block_rsv
= NULL
;
4077 block_rsv
= trans
->block_rsv
;
4079 if (root
== root
->fs_info
->csum_root
&& trans
->adding_csums
)
4080 block_rsv
= trans
->block_rsv
;
4083 block_rsv
= root
->block_rsv
;
4086 block_rsv
= &root
->fs_info
->empty_block_rsv
;
4091 static int block_rsv_use_bytes(struct btrfs_block_rsv
*block_rsv
,
4095 spin_lock(&block_rsv
->lock
);
4096 if (block_rsv
->reserved
>= num_bytes
) {
4097 block_rsv
->reserved
-= num_bytes
;
4098 if (block_rsv
->reserved
< block_rsv
->size
)
4099 block_rsv
->full
= 0;
4102 spin_unlock(&block_rsv
->lock
);
4106 static void block_rsv_add_bytes(struct btrfs_block_rsv
*block_rsv
,
4107 u64 num_bytes
, int update_size
)
4109 spin_lock(&block_rsv
->lock
);
4110 block_rsv
->reserved
+= num_bytes
;
4112 block_rsv
->size
+= num_bytes
;
4113 else if (block_rsv
->reserved
>= block_rsv
->size
)
4114 block_rsv
->full
= 1;
4115 spin_unlock(&block_rsv
->lock
);
4118 static void block_rsv_release_bytes(struct btrfs_fs_info
*fs_info
,
4119 struct btrfs_block_rsv
*block_rsv
,
4120 struct btrfs_block_rsv
*dest
, u64 num_bytes
)
4122 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
4124 spin_lock(&block_rsv
->lock
);
4125 if (num_bytes
== (u64
)-1)
4126 num_bytes
= block_rsv
->size
;
4127 block_rsv
->size
-= num_bytes
;
4128 if (block_rsv
->reserved
>= block_rsv
->size
) {
4129 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
4130 block_rsv
->reserved
= block_rsv
->size
;
4131 block_rsv
->full
= 1;
4135 spin_unlock(&block_rsv
->lock
);
4137 if (num_bytes
> 0) {
4139 spin_lock(&dest
->lock
);
4143 bytes_to_add
= dest
->size
- dest
->reserved
;
4144 bytes_to_add
= min(num_bytes
, bytes_to_add
);
4145 dest
->reserved
+= bytes_to_add
;
4146 if (dest
->reserved
>= dest
->size
)
4148 num_bytes
-= bytes_to_add
;
4150 spin_unlock(&dest
->lock
);
4153 spin_lock(&space_info
->lock
);
4154 space_info
->bytes_may_use
-= num_bytes
;
4155 trace_btrfs_space_reservation(fs_info
, "space_info",
4156 space_info
->flags
, num_bytes
, 0);
4157 space_info
->reservation_progress
++;
4158 spin_unlock(&space_info
->lock
);
4163 static int block_rsv_migrate_bytes(struct btrfs_block_rsv
*src
,
4164 struct btrfs_block_rsv
*dst
, u64 num_bytes
)
4168 ret
= block_rsv_use_bytes(src
, num_bytes
);
4172 block_rsv_add_bytes(dst
, num_bytes
, 1);
4176 void btrfs_init_block_rsv(struct btrfs_block_rsv
*rsv
, unsigned short type
)
4178 memset(rsv
, 0, sizeof(*rsv
));
4179 spin_lock_init(&rsv
->lock
);
4183 struct btrfs_block_rsv
*btrfs_alloc_block_rsv(struct btrfs_root
*root
,
4184 unsigned short type
)
4186 struct btrfs_block_rsv
*block_rsv
;
4187 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4189 block_rsv
= kmalloc(sizeof(*block_rsv
), GFP_NOFS
);
4193 btrfs_init_block_rsv(block_rsv
, type
);
4194 block_rsv
->space_info
= __find_space_info(fs_info
,
4195 BTRFS_BLOCK_GROUP_METADATA
);
4199 void btrfs_free_block_rsv(struct btrfs_root
*root
,
4200 struct btrfs_block_rsv
*rsv
)
4204 btrfs_block_rsv_release(root
, rsv
, (u64
)-1);
4208 int btrfs_block_rsv_add(struct btrfs_root
*root
,
4209 struct btrfs_block_rsv
*block_rsv
, u64 num_bytes
,
4210 enum btrfs_reserve_flush_enum flush
)
4217 ret
= reserve_metadata_bytes(root
, block_rsv
, num_bytes
, flush
);
4219 block_rsv_add_bytes(block_rsv
, num_bytes
, 1);
4226 int btrfs_block_rsv_check(struct btrfs_root
*root
,
4227 struct btrfs_block_rsv
*block_rsv
, int min_factor
)
4235 spin_lock(&block_rsv
->lock
);
4236 num_bytes
= div_factor(block_rsv
->size
, min_factor
);
4237 if (block_rsv
->reserved
>= num_bytes
)
4239 spin_unlock(&block_rsv
->lock
);
4244 int btrfs_block_rsv_refill(struct btrfs_root
*root
,
4245 struct btrfs_block_rsv
*block_rsv
, u64 min_reserved
,
4246 enum btrfs_reserve_flush_enum flush
)
4254 spin_lock(&block_rsv
->lock
);
4255 num_bytes
= min_reserved
;
4256 if (block_rsv
->reserved
>= num_bytes
)
4259 num_bytes
-= block_rsv
->reserved
;
4260 spin_unlock(&block_rsv
->lock
);
4265 ret
= reserve_metadata_bytes(root
, block_rsv
, num_bytes
, flush
);
4267 block_rsv_add_bytes(block_rsv
, num_bytes
, 0);
4274 int btrfs_block_rsv_migrate(struct btrfs_block_rsv
*src_rsv
,
4275 struct btrfs_block_rsv
*dst_rsv
,
4278 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
4281 void btrfs_block_rsv_release(struct btrfs_root
*root
,
4282 struct btrfs_block_rsv
*block_rsv
,
4285 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
4286 if (global_rsv
->full
|| global_rsv
== block_rsv
||
4287 block_rsv
->space_info
!= global_rsv
->space_info
)
4289 block_rsv_release_bytes(root
->fs_info
, block_rsv
, global_rsv
,
4294 * helper to calculate size of global block reservation.
4295 * the desired value is sum of space used by extent tree,
4296 * checksum tree and root tree
4298 static u64
calc_global_metadata_size(struct btrfs_fs_info
*fs_info
)
4300 struct btrfs_space_info
*sinfo
;
4304 int csum_size
= btrfs_super_csum_size(fs_info
->super_copy
);
4306 sinfo
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_DATA
);
4307 spin_lock(&sinfo
->lock
);
4308 data_used
= sinfo
->bytes_used
;
4309 spin_unlock(&sinfo
->lock
);
4311 sinfo
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
4312 spin_lock(&sinfo
->lock
);
4313 if (sinfo
->flags
& BTRFS_BLOCK_GROUP_DATA
)
4315 meta_used
= sinfo
->bytes_used
;
4316 spin_unlock(&sinfo
->lock
);
4318 num_bytes
= (data_used
>> fs_info
->sb
->s_blocksize_bits
) *
4320 num_bytes
+= div64_u64(data_used
+ meta_used
, 50);
4322 if (num_bytes
* 3 > meta_used
)
4323 num_bytes
= div64_u64(meta_used
, 3);
4325 return ALIGN(num_bytes
, fs_info
->extent_root
->leafsize
<< 10);
4328 static void update_global_block_rsv(struct btrfs_fs_info
*fs_info
)
4330 struct btrfs_block_rsv
*block_rsv
= &fs_info
->global_block_rsv
;
4331 struct btrfs_space_info
*sinfo
= block_rsv
->space_info
;
4334 num_bytes
= calc_global_metadata_size(fs_info
);
4336 spin_lock(&sinfo
->lock
);
4337 spin_lock(&block_rsv
->lock
);
4339 block_rsv
->size
= num_bytes
;
4341 num_bytes
= sinfo
->bytes_used
+ sinfo
->bytes_pinned
+
4342 sinfo
->bytes_reserved
+ sinfo
->bytes_readonly
+
4343 sinfo
->bytes_may_use
;
4345 if (sinfo
->total_bytes
> num_bytes
) {
4346 num_bytes
= sinfo
->total_bytes
- num_bytes
;
4347 block_rsv
->reserved
+= num_bytes
;
4348 sinfo
->bytes_may_use
+= num_bytes
;
4349 trace_btrfs_space_reservation(fs_info
, "space_info",
4350 sinfo
->flags
, num_bytes
, 1);
4353 if (block_rsv
->reserved
>= block_rsv
->size
) {
4354 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
4355 sinfo
->bytes_may_use
-= num_bytes
;
4356 trace_btrfs_space_reservation(fs_info
, "space_info",
4357 sinfo
->flags
, num_bytes
, 0);
4358 sinfo
->reservation_progress
++;
4359 block_rsv
->reserved
= block_rsv
->size
;
4360 block_rsv
->full
= 1;
4363 spin_unlock(&block_rsv
->lock
);
4364 spin_unlock(&sinfo
->lock
);
4367 static void init_global_block_rsv(struct btrfs_fs_info
*fs_info
)
4369 struct btrfs_space_info
*space_info
;
4371 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_SYSTEM
);
4372 fs_info
->chunk_block_rsv
.space_info
= space_info
;
4374 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
4375 fs_info
->global_block_rsv
.space_info
= space_info
;
4376 fs_info
->delalloc_block_rsv
.space_info
= space_info
;
4377 fs_info
->trans_block_rsv
.space_info
= space_info
;
4378 fs_info
->empty_block_rsv
.space_info
= space_info
;
4379 fs_info
->delayed_block_rsv
.space_info
= space_info
;
4381 fs_info
->extent_root
->block_rsv
= &fs_info
->global_block_rsv
;
4382 fs_info
->csum_root
->block_rsv
= &fs_info
->global_block_rsv
;
4383 fs_info
->dev_root
->block_rsv
= &fs_info
->global_block_rsv
;
4384 fs_info
->tree_root
->block_rsv
= &fs_info
->global_block_rsv
;
4385 fs_info
->chunk_root
->block_rsv
= &fs_info
->chunk_block_rsv
;
4387 update_global_block_rsv(fs_info
);
4390 static void release_global_block_rsv(struct btrfs_fs_info
*fs_info
)
4392 block_rsv_release_bytes(fs_info
, &fs_info
->global_block_rsv
, NULL
,
4394 WARN_ON(fs_info
->delalloc_block_rsv
.size
> 0);
4395 WARN_ON(fs_info
->delalloc_block_rsv
.reserved
> 0);
4396 WARN_ON(fs_info
->trans_block_rsv
.size
> 0);
4397 WARN_ON(fs_info
->trans_block_rsv
.reserved
> 0);
4398 WARN_ON(fs_info
->chunk_block_rsv
.size
> 0);
4399 WARN_ON(fs_info
->chunk_block_rsv
.reserved
> 0);
4400 WARN_ON(fs_info
->delayed_block_rsv
.size
> 0);
4401 WARN_ON(fs_info
->delayed_block_rsv
.reserved
> 0);
4404 void btrfs_trans_release_metadata(struct btrfs_trans_handle
*trans
,
4405 struct btrfs_root
*root
)
4407 if (!trans
->block_rsv
)
4410 if (!trans
->bytes_reserved
)
4413 trace_btrfs_space_reservation(root
->fs_info
, "transaction",
4414 trans
->transid
, trans
->bytes_reserved
, 0);
4415 btrfs_block_rsv_release(root
, trans
->block_rsv
, trans
->bytes_reserved
);
4416 trans
->bytes_reserved
= 0;
4419 /* Can only return 0 or -ENOSPC */
4420 int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle
*trans
,
4421 struct inode
*inode
)
4423 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4424 struct btrfs_block_rsv
*src_rsv
= get_block_rsv(trans
, root
);
4425 struct btrfs_block_rsv
*dst_rsv
= root
->orphan_block_rsv
;
4428 * We need to hold space in order to delete our orphan item once we've
4429 * added it, so this takes the reservation so we can release it later
4430 * when we are truly done with the orphan item.
4432 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 1);
4433 trace_btrfs_space_reservation(root
->fs_info
, "orphan",
4434 btrfs_ino(inode
), num_bytes
, 1);
4435 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
4438 void btrfs_orphan_release_metadata(struct inode
*inode
)
4440 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4441 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 1);
4442 trace_btrfs_space_reservation(root
->fs_info
, "orphan",
4443 btrfs_ino(inode
), num_bytes
, 0);
4444 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
, num_bytes
);
4447 int btrfs_snap_reserve_metadata(struct btrfs_trans_handle
*trans
,
4448 struct btrfs_pending_snapshot
*pending
)
4450 struct btrfs_root
*root
= pending
->root
;
4451 struct btrfs_block_rsv
*src_rsv
= get_block_rsv(trans
, root
);
4452 struct btrfs_block_rsv
*dst_rsv
= &pending
->block_rsv
;
4454 * two for root back/forward refs, two for directory entries,
4455 * one for root of the snapshot and one for parent inode.
4457 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 6);
4458 dst_rsv
->space_info
= src_rsv
->space_info
;
4459 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
4463 * drop_outstanding_extent - drop an outstanding extent
4464 * @inode: the inode we're dropping the extent for
4466 * This is called when we are freeing up an outstanding extent, either called
4467 * after an error or after an extent is written. This will return the number of
4468 * reserved extents that need to be freed. This must be called with
4469 * BTRFS_I(inode)->lock held.
4471 static unsigned drop_outstanding_extent(struct inode
*inode
)
4473 unsigned drop_inode_space
= 0;
4474 unsigned dropped_extents
= 0;
4476 BUG_ON(!BTRFS_I(inode
)->outstanding_extents
);
4477 BTRFS_I(inode
)->outstanding_extents
--;
4479 if (BTRFS_I(inode
)->outstanding_extents
== 0 &&
4480 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED
,
4481 &BTRFS_I(inode
)->runtime_flags
))
4482 drop_inode_space
= 1;
4485 * If we have more or the same amount of outsanding extents than we have
4486 * reserved then we need to leave the reserved extents count alone.
4488 if (BTRFS_I(inode
)->outstanding_extents
>=
4489 BTRFS_I(inode
)->reserved_extents
)
4490 return drop_inode_space
;
4492 dropped_extents
= BTRFS_I(inode
)->reserved_extents
-
4493 BTRFS_I(inode
)->outstanding_extents
;
4494 BTRFS_I(inode
)->reserved_extents
-= dropped_extents
;
4495 return dropped_extents
+ drop_inode_space
;
4499 * calc_csum_metadata_size - return the amount of metada space that must be
4500 * reserved/free'd for the given bytes.
4501 * @inode: the inode we're manipulating
4502 * @num_bytes: the number of bytes in question
4503 * @reserve: 1 if we are reserving space, 0 if we are freeing space
4505 * This adjusts the number of csum_bytes in the inode and then returns the
4506 * correct amount of metadata that must either be reserved or freed. We
4507 * calculate how many checksums we can fit into one leaf and then divide the
4508 * number of bytes that will need to be checksumed by this value to figure out
4509 * how many checksums will be required. If we are adding bytes then the number
4510 * may go up and we will return the number of additional bytes that must be
4511 * reserved. If it is going down we will return the number of bytes that must
4514 * This must be called with BTRFS_I(inode)->lock held.
4516 static u64
calc_csum_metadata_size(struct inode
*inode
, u64 num_bytes
,
4519 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4521 int num_csums_per_leaf
;
4525 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
&&
4526 BTRFS_I(inode
)->csum_bytes
== 0)
4529 old_csums
= (int)div64_u64(BTRFS_I(inode
)->csum_bytes
, root
->sectorsize
);
4531 BTRFS_I(inode
)->csum_bytes
+= num_bytes
;
4533 BTRFS_I(inode
)->csum_bytes
-= num_bytes
;
4534 csum_size
= BTRFS_LEAF_DATA_SIZE(root
) - sizeof(struct btrfs_item
);
4535 num_csums_per_leaf
= (int)div64_u64(csum_size
,
4536 sizeof(struct btrfs_csum_item
) +
4537 sizeof(struct btrfs_disk_key
));
4538 num_csums
= (int)div64_u64(BTRFS_I(inode
)->csum_bytes
, root
->sectorsize
);
4539 num_csums
= num_csums
+ num_csums_per_leaf
- 1;
4540 num_csums
= num_csums
/ num_csums_per_leaf
;
4542 old_csums
= old_csums
+ num_csums_per_leaf
- 1;
4543 old_csums
= old_csums
/ num_csums_per_leaf
;
4545 /* No change, no need to reserve more */
4546 if (old_csums
== num_csums
)
4550 return btrfs_calc_trans_metadata_size(root
,
4551 num_csums
- old_csums
);
4553 return btrfs_calc_trans_metadata_size(root
, old_csums
- num_csums
);
4556 int btrfs_delalloc_reserve_metadata(struct inode
*inode
, u64 num_bytes
)
4558 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4559 struct btrfs_block_rsv
*block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
4562 unsigned nr_extents
= 0;
4563 int extra_reserve
= 0;
4564 enum btrfs_reserve_flush_enum flush
= BTRFS_RESERVE_FLUSH_ALL
;
4566 bool delalloc_lock
= true;
4568 /* If we are a free space inode we need to not flush since we will be in
4569 * the middle of a transaction commit. We also don't need the delalloc
4570 * mutex since we won't race with anybody. We need this mostly to make
4571 * lockdep shut its filthy mouth.
4573 if (btrfs_is_free_space_inode(inode
)) {
4574 flush
= BTRFS_RESERVE_NO_FLUSH
;
4575 delalloc_lock
= false;
4578 if (flush
!= BTRFS_RESERVE_NO_FLUSH
&&
4579 btrfs_transaction_in_commit(root
->fs_info
))
4580 schedule_timeout(1);
4583 mutex_lock(&BTRFS_I(inode
)->delalloc_mutex
);
4585 num_bytes
= ALIGN(num_bytes
, root
->sectorsize
);
4587 spin_lock(&BTRFS_I(inode
)->lock
);
4588 BTRFS_I(inode
)->outstanding_extents
++;
4590 if (BTRFS_I(inode
)->outstanding_extents
>
4591 BTRFS_I(inode
)->reserved_extents
)
4592 nr_extents
= BTRFS_I(inode
)->outstanding_extents
-
4593 BTRFS_I(inode
)->reserved_extents
;
4596 * Add an item to reserve for updating the inode when we complete the
4599 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED
,
4600 &BTRFS_I(inode
)->runtime_flags
)) {
4605 to_reserve
= btrfs_calc_trans_metadata_size(root
, nr_extents
);
4606 to_reserve
+= calc_csum_metadata_size(inode
, num_bytes
, 1);
4607 csum_bytes
= BTRFS_I(inode
)->csum_bytes
;
4608 spin_unlock(&BTRFS_I(inode
)->lock
);
4610 if (root
->fs_info
->quota_enabled
)
4611 ret
= btrfs_qgroup_reserve(root
, num_bytes
+
4612 nr_extents
* root
->leafsize
);
4615 * ret != 0 here means the qgroup reservation failed, we go straight to
4616 * the shared error handling then.
4619 ret
= reserve_metadata_bytes(root
, block_rsv
,
4626 spin_lock(&BTRFS_I(inode
)->lock
);
4627 dropped
= drop_outstanding_extent(inode
);
4629 * If the inodes csum_bytes is the same as the original
4630 * csum_bytes then we know we haven't raced with any free()ers
4631 * so we can just reduce our inodes csum bytes and carry on.
4632 * Otherwise we have to do the normal free thing to account for
4633 * the case that the free side didn't free up its reserve
4634 * because of this outstanding reservation.
4636 if (BTRFS_I(inode
)->csum_bytes
== csum_bytes
)
4637 calc_csum_metadata_size(inode
, num_bytes
, 0);
4639 to_free
= calc_csum_metadata_size(inode
, num_bytes
, 0);
4640 spin_unlock(&BTRFS_I(inode
)->lock
);
4642 to_free
+= btrfs_calc_trans_metadata_size(root
, dropped
);
4645 btrfs_block_rsv_release(root
, block_rsv
, to_free
);
4646 trace_btrfs_space_reservation(root
->fs_info
,
4651 if (root
->fs_info
->quota_enabled
) {
4652 btrfs_qgroup_free(root
, num_bytes
+
4653 nr_extents
* root
->leafsize
);
4656 mutex_unlock(&BTRFS_I(inode
)->delalloc_mutex
);
4660 spin_lock(&BTRFS_I(inode
)->lock
);
4661 if (extra_reserve
) {
4662 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED
,
4663 &BTRFS_I(inode
)->runtime_flags
);
4666 BTRFS_I(inode
)->reserved_extents
+= nr_extents
;
4667 spin_unlock(&BTRFS_I(inode
)->lock
);
4670 mutex_unlock(&BTRFS_I(inode
)->delalloc_mutex
);
4673 trace_btrfs_space_reservation(root
->fs_info
,"delalloc",
4674 btrfs_ino(inode
), to_reserve
, 1);
4675 block_rsv_add_bytes(block_rsv
, to_reserve
, 1);
4681 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
4682 * @inode: the inode to release the reservation for
4683 * @num_bytes: the number of bytes we're releasing
4685 * This will release the metadata reservation for an inode. This can be called
4686 * once we complete IO for a given set of bytes to release their metadata
4689 void btrfs_delalloc_release_metadata(struct inode
*inode
, u64 num_bytes
)
4691 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4695 num_bytes
= ALIGN(num_bytes
, root
->sectorsize
);
4696 spin_lock(&BTRFS_I(inode
)->lock
);
4697 dropped
= drop_outstanding_extent(inode
);
4699 to_free
= calc_csum_metadata_size(inode
, num_bytes
, 0);
4700 spin_unlock(&BTRFS_I(inode
)->lock
);
4702 to_free
+= btrfs_calc_trans_metadata_size(root
, dropped
);
4704 trace_btrfs_space_reservation(root
->fs_info
, "delalloc",
4705 btrfs_ino(inode
), to_free
, 0);
4706 if (root
->fs_info
->quota_enabled
) {
4707 btrfs_qgroup_free(root
, num_bytes
+
4708 dropped
* root
->leafsize
);
4711 btrfs_block_rsv_release(root
, &root
->fs_info
->delalloc_block_rsv
,
4716 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
4717 * @inode: inode we're writing to
4718 * @num_bytes: the number of bytes we want to allocate
4720 * This will do the following things
4722 * o reserve space in the data space info for num_bytes
4723 * o reserve space in the metadata space info based on number of outstanding
4724 * extents and how much csums will be needed
4725 * o add to the inodes ->delalloc_bytes
4726 * o add it to the fs_info's delalloc inodes list.
4728 * This will return 0 for success and -ENOSPC if there is no space left.
4730 int btrfs_delalloc_reserve_space(struct inode
*inode
, u64 num_bytes
)
4734 ret
= btrfs_check_data_free_space(inode
, num_bytes
);
4738 ret
= btrfs_delalloc_reserve_metadata(inode
, num_bytes
);
4740 btrfs_free_reserved_data_space(inode
, num_bytes
);
4748 * btrfs_delalloc_release_space - release data and metadata space for delalloc
4749 * @inode: inode we're releasing space for
4750 * @num_bytes: the number of bytes we want to free up
4752 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
4753 * called in the case that we don't need the metadata AND data reservations
4754 * anymore. So if there is an error or we insert an inline extent.
4756 * This function will release the metadata space that was not used and will
4757 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
4758 * list if there are no delalloc bytes left.
4760 void btrfs_delalloc_release_space(struct inode
*inode
, u64 num_bytes
)
4762 btrfs_delalloc_release_metadata(inode
, num_bytes
);
4763 btrfs_free_reserved_data_space(inode
, num_bytes
);
4766 static int update_block_group(struct btrfs_trans_handle
*trans
,
4767 struct btrfs_root
*root
,
4768 u64 bytenr
, u64 num_bytes
, int alloc
)
4770 struct btrfs_block_group_cache
*cache
= NULL
;
4771 struct btrfs_fs_info
*info
= root
->fs_info
;
4772 u64 total
= num_bytes
;
4777 /* block accounting for super block */
4778 spin_lock(&info
->delalloc_lock
);
4779 old_val
= btrfs_super_bytes_used(info
->super_copy
);
4781 old_val
+= num_bytes
;
4783 old_val
-= num_bytes
;
4784 btrfs_set_super_bytes_used(info
->super_copy
, old_val
);
4785 spin_unlock(&info
->delalloc_lock
);
4788 cache
= btrfs_lookup_block_group(info
, bytenr
);
4791 if (cache
->flags
& (BTRFS_BLOCK_GROUP_DUP
|
4792 BTRFS_BLOCK_GROUP_RAID1
|
4793 BTRFS_BLOCK_GROUP_RAID10
))
4798 * If this block group has free space cache written out, we
4799 * need to make sure to load it if we are removing space. This
4800 * is because we need the unpinning stage to actually add the
4801 * space back to the block group, otherwise we will leak space.
4803 if (!alloc
&& cache
->cached
== BTRFS_CACHE_NO
)
4804 cache_block_group(cache
, trans
, NULL
, 1);
4806 byte_in_group
= bytenr
- cache
->key
.objectid
;
4807 WARN_ON(byte_in_group
> cache
->key
.offset
);
4809 spin_lock(&cache
->space_info
->lock
);
4810 spin_lock(&cache
->lock
);
4812 if (btrfs_test_opt(root
, SPACE_CACHE
) &&
4813 cache
->disk_cache_state
< BTRFS_DC_CLEAR
)
4814 cache
->disk_cache_state
= BTRFS_DC_CLEAR
;
4817 old_val
= btrfs_block_group_used(&cache
->item
);
4818 num_bytes
= min(total
, cache
->key
.offset
- byte_in_group
);
4820 old_val
+= num_bytes
;
4821 btrfs_set_block_group_used(&cache
->item
, old_val
);
4822 cache
->reserved
-= num_bytes
;
4823 cache
->space_info
->bytes_reserved
-= num_bytes
;
4824 cache
->space_info
->bytes_used
+= num_bytes
;
4825 cache
->space_info
->disk_used
+= num_bytes
* factor
;
4826 spin_unlock(&cache
->lock
);
4827 spin_unlock(&cache
->space_info
->lock
);
4829 old_val
-= num_bytes
;
4830 btrfs_set_block_group_used(&cache
->item
, old_val
);
4831 cache
->pinned
+= num_bytes
;
4832 cache
->space_info
->bytes_pinned
+= num_bytes
;
4833 cache
->space_info
->bytes_used
-= num_bytes
;
4834 cache
->space_info
->disk_used
-= num_bytes
* factor
;
4835 spin_unlock(&cache
->lock
);
4836 spin_unlock(&cache
->space_info
->lock
);
4838 set_extent_dirty(info
->pinned_extents
,
4839 bytenr
, bytenr
+ num_bytes
- 1,
4840 GFP_NOFS
| __GFP_NOFAIL
);
4842 btrfs_put_block_group(cache
);
4844 bytenr
+= num_bytes
;
4849 static u64
first_logical_byte(struct btrfs_root
*root
, u64 search_start
)
4851 struct btrfs_block_group_cache
*cache
;
4854 cache
= btrfs_lookup_first_block_group(root
->fs_info
, search_start
);
4858 bytenr
= cache
->key
.objectid
;
4859 btrfs_put_block_group(cache
);
4864 static int pin_down_extent(struct btrfs_root
*root
,
4865 struct btrfs_block_group_cache
*cache
,
4866 u64 bytenr
, u64 num_bytes
, int reserved
)
4868 spin_lock(&cache
->space_info
->lock
);
4869 spin_lock(&cache
->lock
);
4870 cache
->pinned
+= num_bytes
;
4871 cache
->space_info
->bytes_pinned
+= num_bytes
;
4873 cache
->reserved
-= num_bytes
;
4874 cache
->space_info
->bytes_reserved
-= num_bytes
;
4876 spin_unlock(&cache
->lock
);
4877 spin_unlock(&cache
->space_info
->lock
);
4879 set_extent_dirty(root
->fs_info
->pinned_extents
, bytenr
,
4880 bytenr
+ num_bytes
- 1, GFP_NOFS
| __GFP_NOFAIL
);
4885 * this function must be called within transaction
4887 int btrfs_pin_extent(struct btrfs_root
*root
,
4888 u64 bytenr
, u64 num_bytes
, int reserved
)
4890 struct btrfs_block_group_cache
*cache
;
4892 cache
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
4893 BUG_ON(!cache
); /* Logic error */
4895 pin_down_extent(root
, cache
, bytenr
, num_bytes
, reserved
);
4897 btrfs_put_block_group(cache
);
4902 * this function must be called within transaction
4904 int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle
*trans
,
4905 struct btrfs_root
*root
,
4906 u64 bytenr
, u64 num_bytes
)
4908 struct btrfs_block_group_cache
*cache
;
4910 cache
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
4911 BUG_ON(!cache
); /* Logic error */
4914 * pull in the free space cache (if any) so that our pin
4915 * removes the free space from the cache. We have load_only set
4916 * to one because the slow code to read in the free extents does check
4917 * the pinned extents.
4919 cache_block_group(cache
, trans
, root
, 1);
4921 pin_down_extent(root
, cache
, bytenr
, num_bytes
, 0);
4923 /* remove us from the free space cache (if we're there at all) */
4924 btrfs_remove_free_space(cache
, bytenr
, num_bytes
);
4925 btrfs_put_block_group(cache
);
4930 * btrfs_update_reserved_bytes - update the block_group and space info counters
4931 * @cache: The cache we are manipulating
4932 * @num_bytes: The number of bytes in question
4933 * @reserve: One of the reservation enums
4935 * This is called by the allocator when it reserves space, or by somebody who is
4936 * freeing space that was never actually used on disk. For example if you
4937 * reserve some space for a new leaf in transaction A and before transaction A
4938 * commits you free that leaf, you call this with reserve set to 0 in order to
4939 * clear the reservation.
4941 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
4942 * ENOSPC accounting. For data we handle the reservation through clearing the
4943 * delalloc bits in the io_tree. We have to do this since we could end up
4944 * allocating less disk space for the amount of data we have reserved in the
4945 * case of compression.
4947 * If this is a reservation and the block group has become read only we cannot
4948 * make the reservation and return -EAGAIN, otherwise this function always
4951 static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache
*cache
,
4952 u64 num_bytes
, int reserve
)
4954 struct btrfs_space_info
*space_info
= cache
->space_info
;
4957 spin_lock(&space_info
->lock
);
4958 spin_lock(&cache
->lock
);
4959 if (reserve
!= RESERVE_FREE
) {
4963 cache
->reserved
+= num_bytes
;
4964 space_info
->bytes_reserved
+= num_bytes
;
4965 if (reserve
== RESERVE_ALLOC
) {
4966 trace_btrfs_space_reservation(cache
->fs_info
,
4967 "space_info", space_info
->flags
,
4969 space_info
->bytes_may_use
-= num_bytes
;
4974 space_info
->bytes_readonly
+= num_bytes
;
4975 cache
->reserved
-= num_bytes
;
4976 space_info
->bytes_reserved
-= num_bytes
;
4977 space_info
->reservation_progress
++;
4979 spin_unlock(&cache
->lock
);
4980 spin_unlock(&space_info
->lock
);
4984 void btrfs_prepare_extent_commit(struct btrfs_trans_handle
*trans
,
4985 struct btrfs_root
*root
)
4987 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4988 struct btrfs_caching_control
*next
;
4989 struct btrfs_caching_control
*caching_ctl
;
4990 struct btrfs_block_group_cache
*cache
;
4992 down_write(&fs_info
->extent_commit_sem
);
4994 list_for_each_entry_safe(caching_ctl
, next
,
4995 &fs_info
->caching_block_groups
, list
) {
4996 cache
= caching_ctl
->block_group
;
4997 if (block_group_cache_done(cache
)) {
4998 cache
->last_byte_to_unpin
= (u64
)-1;
4999 list_del_init(&caching_ctl
->list
);
5000 put_caching_control(caching_ctl
);
5002 cache
->last_byte_to_unpin
= caching_ctl
->progress
;
5006 if (fs_info
->pinned_extents
== &fs_info
->freed_extents
[0])
5007 fs_info
->pinned_extents
= &fs_info
->freed_extents
[1];
5009 fs_info
->pinned_extents
= &fs_info
->freed_extents
[0];
5011 up_write(&fs_info
->extent_commit_sem
);
5013 update_global_block_rsv(fs_info
);
5016 static int unpin_extent_range(struct btrfs_root
*root
, u64 start
, u64 end
)
5018 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5019 struct btrfs_block_group_cache
*cache
= NULL
;
5020 struct btrfs_space_info
*space_info
;
5021 struct btrfs_block_rsv
*global_rsv
= &fs_info
->global_block_rsv
;
5025 while (start
<= end
) {
5028 start
>= cache
->key
.objectid
+ cache
->key
.offset
) {
5030 btrfs_put_block_group(cache
);
5031 cache
= btrfs_lookup_block_group(fs_info
, start
);
5032 BUG_ON(!cache
); /* Logic error */
5035 len
= cache
->key
.objectid
+ cache
->key
.offset
- start
;
5036 len
= min(len
, end
+ 1 - start
);
5038 if (start
< cache
->last_byte_to_unpin
) {
5039 len
= min(len
, cache
->last_byte_to_unpin
- start
);
5040 btrfs_add_free_space(cache
, start
, len
);
5044 space_info
= cache
->space_info
;
5046 spin_lock(&space_info
->lock
);
5047 spin_lock(&cache
->lock
);
5048 cache
->pinned
-= len
;
5049 space_info
->bytes_pinned
-= len
;
5051 space_info
->bytes_readonly
+= len
;
5054 spin_unlock(&cache
->lock
);
5055 if (!readonly
&& global_rsv
->space_info
== space_info
) {
5056 spin_lock(&global_rsv
->lock
);
5057 if (!global_rsv
->full
) {
5058 len
= min(len
, global_rsv
->size
-
5059 global_rsv
->reserved
);
5060 global_rsv
->reserved
+= len
;
5061 space_info
->bytes_may_use
+= len
;
5062 if (global_rsv
->reserved
>= global_rsv
->size
)
5063 global_rsv
->full
= 1;
5065 spin_unlock(&global_rsv
->lock
);
5067 spin_unlock(&space_info
->lock
);
5071 btrfs_put_block_group(cache
);
5075 int btrfs_finish_extent_commit(struct btrfs_trans_handle
*trans
,
5076 struct btrfs_root
*root
)
5078 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5079 struct extent_io_tree
*unpin
;
5087 if (fs_info
->pinned_extents
== &fs_info
->freed_extents
[0])
5088 unpin
= &fs_info
->freed_extents
[1];
5090 unpin
= &fs_info
->freed_extents
[0];
5093 ret
= find_first_extent_bit(unpin
, 0, &start
, &end
,
5094 EXTENT_DIRTY
, NULL
);
5098 if (btrfs_test_opt(root
, DISCARD
))
5099 ret
= btrfs_discard_extent(root
, start
,
5100 end
+ 1 - start
, NULL
);
5102 clear_extent_dirty(unpin
, start
, end
, GFP_NOFS
);
5103 unpin_extent_range(root
, start
, end
);
5110 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
5111 struct btrfs_root
*root
,
5112 u64 bytenr
, u64 num_bytes
, u64 parent
,
5113 u64 root_objectid
, u64 owner_objectid
,
5114 u64 owner_offset
, int refs_to_drop
,
5115 struct btrfs_delayed_extent_op
*extent_op
)
5117 struct btrfs_key key
;
5118 struct btrfs_path
*path
;
5119 struct btrfs_fs_info
*info
= root
->fs_info
;
5120 struct btrfs_root
*extent_root
= info
->extent_root
;
5121 struct extent_buffer
*leaf
;
5122 struct btrfs_extent_item
*ei
;
5123 struct btrfs_extent_inline_ref
*iref
;
5126 int extent_slot
= 0;
5127 int found_extent
= 0;
5132 path
= btrfs_alloc_path();
5137 path
->leave_spinning
= 1;
5139 is_data
= owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
;
5140 BUG_ON(!is_data
&& refs_to_drop
!= 1);
5142 ret
= lookup_extent_backref(trans
, extent_root
, path
, &iref
,
5143 bytenr
, num_bytes
, parent
,
5144 root_objectid
, owner_objectid
,
5147 extent_slot
= path
->slots
[0];
5148 while (extent_slot
>= 0) {
5149 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
5151 if (key
.objectid
!= bytenr
)
5153 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
5154 key
.offset
== num_bytes
) {
5158 if (path
->slots
[0] - extent_slot
> 5)
5162 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5163 item_size
= btrfs_item_size_nr(path
->nodes
[0], extent_slot
);
5164 if (found_extent
&& item_size
< sizeof(*ei
))
5167 if (!found_extent
) {
5169 ret
= remove_extent_backref(trans
, extent_root
, path
,
5173 btrfs_abort_transaction(trans
, extent_root
, ret
);
5176 btrfs_release_path(path
);
5177 path
->leave_spinning
= 1;
5179 key
.objectid
= bytenr
;
5180 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
5181 key
.offset
= num_bytes
;
5183 ret
= btrfs_search_slot(trans
, extent_root
,
5186 printk(KERN_ERR
"umm, got %d back from search"
5187 ", was looking for %llu\n", ret
,
5188 (unsigned long long)bytenr
);
5190 btrfs_print_leaf(extent_root
,
5194 btrfs_abort_transaction(trans
, extent_root
, ret
);
5197 extent_slot
= path
->slots
[0];
5199 } else if (ret
== -ENOENT
) {
5200 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
5202 printk(KERN_ERR
"btrfs unable to find ref byte nr %llu "
5203 "parent %llu root %llu owner %llu offset %llu\n",
5204 (unsigned long long)bytenr
,
5205 (unsigned long long)parent
,
5206 (unsigned long long)root_objectid
,
5207 (unsigned long long)owner_objectid
,
5208 (unsigned long long)owner_offset
);
5210 btrfs_abort_transaction(trans
, extent_root
, ret
);
5214 leaf
= path
->nodes
[0];
5215 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
5216 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5217 if (item_size
< sizeof(*ei
)) {
5218 BUG_ON(found_extent
|| extent_slot
!= path
->slots
[0]);
5219 ret
= convert_extent_item_v0(trans
, extent_root
, path
,
5222 btrfs_abort_transaction(trans
, extent_root
, ret
);
5226 btrfs_release_path(path
);
5227 path
->leave_spinning
= 1;
5229 key
.objectid
= bytenr
;
5230 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
5231 key
.offset
= num_bytes
;
5233 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
,
5236 printk(KERN_ERR
"umm, got %d back from search"
5237 ", was looking for %llu\n", ret
,
5238 (unsigned long long)bytenr
);
5239 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
5242 btrfs_abort_transaction(trans
, extent_root
, ret
);
5246 extent_slot
= path
->slots
[0];
5247 leaf
= path
->nodes
[0];
5248 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
5251 BUG_ON(item_size
< sizeof(*ei
));
5252 ei
= btrfs_item_ptr(leaf
, extent_slot
,
5253 struct btrfs_extent_item
);
5254 if (owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
) {
5255 struct btrfs_tree_block_info
*bi
;
5256 BUG_ON(item_size
< sizeof(*ei
) + sizeof(*bi
));
5257 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
5258 WARN_ON(owner_objectid
!= btrfs_tree_block_level(leaf
, bi
));
5261 refs
= btrfs_extent_refs(leaf
, ei
);
5262 BUG_ON(refs
< refs_to_drop
);
5263 refs
-= refs_to_drop
;
5267 __run_delayed_extent_op(extent_op
, leaf
, ei
);
5269 * In the case of inline back ref, reference count will
5270 * be updated by remove_extent_backref
5273 BUG_ON(!found_extent
);
5275 btrfs_set_extent_refs(leaf
, ei
, refs
);
5276 btrfs_mark_buffer_dirty(leaf
);
5279 ret
= remove_extent_backref(trans
, extent_root
, path
,
5283 btrfs_abort_transaction(trans
, extent_root
, ret
);
5289 BUG_ON(is_data
&& refs_to_drop
!=
5290 extent_data_ref_count(root
, path
, iref
));
5292 BUG_ON(path
->slots
[0] != extent_slot
);
5294 BUG_ON(path
->slots
[0] != extent_slot
+ 1);
5295 path
->slots
[0] = extent_slot
;
5300 ret
= btrfs_del_items(trans
, extent_root
, path
, path
->slots
[0],
5303 btrfs_abort_transaction(trans
, extent_root
, ret
);
5306 btrfs_release_path(path
);
5309 ret
= btrfs_del_csums(trans
, root
, bytenr
, num_bytes
);
5311 btrfs_abort_transaction(trans
, extent_root
, ret
);
5316 ret
= update_block_group(trans
, root
, bytenr
, num_bytes
, 0);
5318 btrfs_abort_transaction(trans
, extent_root
, ret
);
5323 btrfs_free_path(path
);
5328 * when we free an block, it is possible (and likely) that we free the last
5329 * delayed ref for that extent as well. This searches the delayed ref tree for
5330 * a given extent, and if there are no other delayed refs to be processed, it
5331 * removes it from the tree.
5333 static noinline
int check_ref_cleanup(struct btrfs_trans_handle
*trans
,
5334 struct btrfs_root
*root
, u64 bytenr
)
5336 struct btrfs_delayed_ref_head
*head
;
5337 struct btrfs_delayed_ref_root
*delayed_refs
;
5338 struct btrfs_delayed_ref_node
*ref
;
5339 struct rb_node
*node
;
5342 delayed_refs
= &trans
->transaction
->delayed_refs
;
5343 spin_lock(&delayed_refs
->lock
);
5344 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
5348 node
= rb_prev(&head
->node
.rb_node
);
5352 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
5354 /* there are still entries for this ref, we can't drop it */
5355 if (ref
->bytenr
== bytenr
)
5358 if (head
->extent_op
) {
5359 if (!head
->must_insert_reserved
)
5361 btrfs_free_delayed_extent_op(head
->extent_op
);
5362 head
->extent_op
= NULL
;
5366 * waiting for the lock here would deadlock. If someone else has it
5367 * locked they are already in the process of dropping it anyway
5369 if (!mutex_trylock(&head
->mutex
))
5373 * at this point we have a head with no other entries. Go
5374 * ahead and process it.
5376 head
->node
.in_tree
= 0;
5377 rb_erase(&head
->node
.rb_node
, &delayed_refs
->root
);
5379 delayed_refs
->num_entries
--;
5382 * we don't take a ref on the node because we're removing it from the
5383 * tree, so we just steal the ref the tree was holding.
5385 delayed_refs
->num_heads
--;
5386 if (list_empty(&head
->cluster
))
5387 delayed_refs
->num_heads_ready
--;
5389 list_del_init(&head
->cluster
);
5390 spin_unlock(&delayed_refs
->lock
);
5392 BUG_ON(head
->extent_op
);
5393 if (head
->must_insert_reserved
)
5396 mutex_unlock(&head
->mutex
);
5397 btrfs_put_delayed_ref(&head
->node
);
5400 spin_unlock(&delayed_refs
->lock
);
5404 void btrfs_free_tree_block(struct btrfs_trans_handle
*trans
,
5405 struct btrfs_root
*root
,
5406 struct extent_buffer
*buf
,
5407 u64 parent
, int last_ref
)
5409 struct btrfs_block_group_cache
*cache
= NULL
;
5412 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
5413 ret
= btrfs_add_delayed_tree_ref(root
->fs_info
, trans
,
5414 buf
->start
, buf
->len
,
5415 parent
, root
->root_key
.objectid
,
5416 btrfs_header_level(buf
),
5417 BTRFS_DROP_DELAYED_REF
, NULL
, 0);
5418 BUG_ON(ret
); /* -ENOMEM */
5424 cache
= btrfs_lookup_block_group(root
->fs_info
, buf
->start
);
5426 if (btrfs_header_generation(buf
) == trans
->transid
) {
5427 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
5428 ret
= check_ref_cleanup(trans
, root
, buf
->start
);
5433 if (btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
)) {
5434 pin_down_extent(root
, cache
, buf
->start
, buf
->len
, 1);
5438 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY
, &buf
->bflags
));
5440 btrfs_add_free_space(cache
, buf
->start
, buf
->len
);
5441 btrfs_update_reserved_bytes(cache
, buf
->len
, RESERVE_FREE
);
5445 * Deleting the buffer, clear the corrupt flag since it doesn't matter
5448 clear_bit(EXTENT_BUFFER_CORRUPT
, &buf
->bflags
);
5449 btrfs_put_block_group(cache
);
5452 /* Can return -ENOMEM */
5453 int btrfs_free_extent(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
5454 u64 bytenr
, u64 num_bytes
, u64 parent
, u64 root_objectid
,
5455 u64 owner
, u64 offset
, int for_cow
)
5458 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5461 * tree log blocks never actually go into the extent allocation
5462 * tree, just update pinning info and exit early.
5464 if (root_objectid
== BTRFS_TREE_LOG_OBJECTID
) {
5465 WARN_ON(owner
>= BTRFS_FIRST_FREE_OBJECTID
);
5466 /* unlocks the pinned mutex */
5467 btrfs_pin_extent(root
, bytenr
, num_bytes
, 1);
5469 } else if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
5470 ret
= btrfs_add_delayed_tree_ref(fs_info
, trans
, bytenr
,
5472 parent
, root_objectid
, (int)owner
,
5473 BTRFS_DROP_DELAYED_REF
, NULL
, for_cow
);
5475 ret
= btrfs_add_delayed_data_ref(fs_info
, trans
, bytenr
,
5477 parent
, root_objectid
, owner
,
5478 offset
, BTRFS_DROP_DELAYED_REF
,
5484 static u64
stripe_align(struct btrfs_root
*root
, u64 val
)
5486 u64 mask
= ((u64
)root
->stripesize
- 1);
5487 u64 ret
= (val
+ mask
) & ~mask
;
5492 * when we wait for progress in the block group caching, its because
5493 * our allocation attempt failed at least once. So, we must sleep
5494 * and let some progress happen before we try again.
5496 * This function will sleep at least once waiting for new free space to
5497 * show up, and then it will check the block group free space numbers
5498 * for our min num_bytes. Another option is to have it go ahead
5499 * and look in the rbtree for a free extent of a given size, but this
5503 wait_block_group_cache_progress(struct btrfs_block_group_cache
*cache
,
5506 struct btrfs_caching_control
*caching_ctl
;
5509 caching_ctl
= get_caching_control(cache
);
5513 wait_event(caching_ctl
->wait
, block_group_cache_done(cache
) ||
5514 (cache
->free_space_ctl
->free_space
>= num_bytes
));
5516 put_caching_control(caching_ctl
);
5521 wait_block_group_cache_done(struct btrfs_block_group_cache
*cache
)
5523 struct btrfs_caching_control
*caching_ctl
;
5526 caching_ctl
= get_caching_control(cache
);
5530 wait_event(caching_ctl
->wait
, block_group_cache_done(cache
));
5532 put_caching_control(caching_ctl
);
5536 int __get_raid_index(u64 flags
)
5540 if (flags
& BTRFS_BLOCK_GROUP_RAID10
)
5542 else if (flags
& BTRFS_BLOCK_GROUP_RAID1
)
5544 else if (flags
& BTRFS_BLOCK_GROUP_DUP
)
5546 else if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
5554 static int get_block_group_index(struct btrfs_block_group_cache
*cache
)
5556 return __get_raid_index(cache
->flags
);
5559 enum btrfs_loop_type
{
5560 LOOP_CACHING_NOWAIT
= 0,
5561 LOOP_CACHING_WAIT
= 1,
5562 LOOP_ALLOC_CHUNK
= 2,
5563 LOOP_NO_EMPTY_SIZE
= 3,
5567 * walks the btree of allocated extents and find a hole of a given size.
5568 * The key ins is changed to record the hole:
5569 * ins->objectid == block start
5570 * ins->flags = BTRFS_EXTENT_ITEM_KEY
5571 * ins->offset == number of blocks
5572 * Any available blocks before search_start are skipped.
5574 static noinline
int find_free_extent(struct btrfs_trans_handle
*trans
,
5575 struct btrfs_root
*orig_root
,
5576 u64 num_bytes
, u64 empty_size
,
5577 u64 hint_byte
, struct btrfs_key
*ins
,
5581 struct btrfs_root
*root
= orig_root
->fs_info
->extent_root
;
5582 struct btrfs_free_cluster
*last_ptr
= NULL
;
5583 struct btrfs_block_group_cache
*block_group
= NULL
;
5584 struct btrfs_block_group_cache
*used_block_group
;
5585 u64 search_start
= 0;
5586 int empty_cluster
= 2 * 1024 * 1024;
5587 struct btrfs_space_info
*space_info
;
5589 int index
= __get_raid_index(data
);
5590 int alloc_type
= (data
& BTRFS_BLOCK_GROUP_DATA
) ?
5591 RESERVE_ALLOC_NO_ACCOUNT
: RESERVE_ALLOC
;
5592 bool found_uncached_bg
= false;
5593 bool failed_cluster_refill
= false;
5594 bool failed_alloc
= false;
5595 bool use_cluster
= true;
5596 bool have_caching_bg
= false;
5598 WARN_ON(num_bytes
< root
->sectorsize
);
5599 btrfs_set_key_type(ins
, BTRFS_EXTENT_ITEM_KEY
);
5603 trace_find_free_extent(orig_root
, num_bytes
, empty_size
, data
);
5605 space_info
= __find_space_info(root
->fs_info
, data
);
5607 printk(KERN_ERR
"No space info for %llu\n", data
);
5612 * If the space info is for both data and metadata it means we have a
5613 * small filesystem and we can't use the clustering stuff.
5615 if (btrfs_mixed_space_info(space_info
))
5616 use_cluster
= false;
5618 if (data
& BTRFS_BLOCK_GROUP_METADATA
&& use_cluster
) {
5619 last_ptr
= &root
->fs_info
->meta_alloc_cluster
;
5620 if (!btrfs_test_opt(root
, SSD
))
5621 empty_cluster
= 64 * 1024;
5624 if ((data
& BTRFS_BLOCK_GROUP_DATA
) && use_cluster
&&
5625 btrfs_test_opt(root
, SSD
)) {
5626 last_ptr
= &root
->fs_info
->data_alloc_cluster
;
5630 spin_lock(&last_ptr
->lock
);
5631 if (last_ptr
->block_group
)
5632 hint_byte
= last_ptr
->window_start
;
5633 spin_unlock(&last_ptr
->lock
);
5636 search_start
= max(search_start
, first_logical_byte(root
, 0));
5637 search_start
= max(search_start
, hint_byte
);
5642 if (search_start
== hint_byte
) {
5643 block_group
= btrfs_lookup_block_group(root
->fs_info
,
5645 used_block_group
= block_group
;
5647 * we don't want to use the block group if it doesn't match our
5648 * allocation bits, or if its not cached.
5650 * However if we are re-searching with an ideal block group
5651 * picked out then we don't care that the block group is cached.
5653 if (block_group
&& block_group_bits(block_group
, data
) &&
5654 block_group
->cached
!= BTRFS_CACHE_NO
) {
5655 down_read(&space_info
->groups_sem
);
5656 if (list_empty(&block_group
->list
) ||
5659 * someone is removing this block group,
5660 * we can't jump into the have_block_group
5661 * target because our list pointers are not
5664 btrfs_put_block_group(block_group
);
5665 up_read(&space_info
->groups_sem
);
5667 index
= get_block_group_index(block_group
);
5668 goto have_block_group
;
5670 } else if (block_group
) {
5671 btrfs_put_block_group(block_group
);
5675 have_caching_bg
= false;
5676 down_read(&space_info
->groups_sem
);
5677 list_for_each_entry(block_group
, &space_info
->block_groups
[index
],
5682 used_block_group
= block_group
;
5683 btrfs_get_block_group(block_group
);
5684 search_start
= block_group
->key
.objectid
;
5687 * this can happen if we end up cycling through all the
5688 * raid types, but we want to make sure we only allocate
5689 * for the proper type.
5691 if (!block_group_bits(block_group
, data
)) {
5692 u64 extra
= BTRFS_BLOCK_GROUP_DUP
|
5693 BTRFS_BLOCK_GROUP_RAID1
|
5694 BTRFS_BLOCK_GROUP_RAID10
;
5697 * if they asked for extra copies and this block group
5698 * doesn't provide them, bail. This does allow us to
5699 * fill raid0 from raid1.
5701 if ((data
& extra
) && !(block_group
->flags
& extra
))
5706 cached
= block_group_cache_done(block_group
);
5707 if (unlikely(!cached
)) {
5708 found_uncached_bg
= true;
5709 ret
= cache_block_group(block_group
, trans
,
5715 if (unlikely(block_group
->ro
))
5719 * Ok we want to try and use the cluster allocator, so
5724 * the refill lock keeps out other
5725 * people trying to start a new cluster
5727 spin_lock(&last_ptr
->refill_lock
);
5728 used_block_group
= last_ptr
->block_group
;
5729 if (used_block_group
!= block_group
&&
5730 (!used_block_group
||
5731 used_block_group
->ro
||
5732 !block_group_bits(used_block_group
, data
))) {
5733 used_block_group
= block_group
;
5734 goto refill_cluster
;
5737 if (used_block_group
!= block_group
)
5738 btrfs_get_block_group(used_block_group
);
5740 offset
= btrfs_alloc_from_cluster(used_block_group
,
5741 last_ptr
, num_bytes
, used_block_group
->key
.objectid
);
5743 /* we have a block, we're done */
5744 spin_unlock(&last_ptr
->refill_lock
);
5745 trace_btrfs_reserve_extent_cluster(root
,
5746 block_group
, search_start
, num_bytes
);
5750 WARN_ON(last_ptr
->block_group
!= used_block_group
);
5751 if (used_block_group
!= block_group
) {
5752 btrfs_put_block_group(used_block_group
);
5753 used_block_group
= block_group
;
5756 BUG_ON(used_block_group
!= block_group
);
5757 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
5758 * set up a new clusters, so lets just skip it
5759 * and let the allocator find whatever block
5760 * it can find. If we reach this point, we
5761 * will have tried the cluster allocator
5762 * plenty of times and not have found
5763 * anything, so we are likely way too
5764 * fragmented for the clustering stuff to find
5767 * However, if the cluster is taken from the
5768 * current block group, release the cluster
5769 * first, so that we stand a better chance of
5770 * succeeding in the unclustered
5772 if (loop
>= LOOP_NO_EMPTY_SIZE
&&
5773 last_ptr
->block_group
!= block_group
) {
5774 spin_unlock(&last_ptr
->refill_lock
);
5775 goto unclustered_alloc
;
5779 * this cluster didn't work out, free it and
5782 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
5784 if (loop
>= LOOP_NO_EMPTY_SIZE
) {
5785 spin_unlock(&last_ptr
->refill_lock
);
5786 goto unclustered_alloc
;
5789 /* allocate a cluster in this block group */
5790 ret
= btrfs_find_space_cluster(trans
, root
,
5791 block_group
, last_ptr
,
5792 search_start
, num_bytes
,
5793 empty_cluster
+ empty_size
);
5796 * now pull our allocation out of this
5799 offset
= btrfs_alloc_from_cluster(block_group
,
5800 last_ptr
, num_bytes
,
5803 /* we found one, proceed */
5804 spin_unlock(&last_ptr
->refill_lock
);
5805 trace_btrfs_reserve_extent_cluster(root
,
5806 block_group
, search_start
,
5810 } else if (!cached
&& loop
> LOOP_CACHING_NOWAIT
5811 && !failed_cluster_refill
) {
5812 spin_unlock(&last_ptr
->refill_lock
);
5814 failed_cluster_refill
= true;
5815 wait_block_group_cache_progress(block_group
,
5816 num_bytes
+ empty_cluster
+ empty_size
);
5817 goto have_block_group
;
5821 * at this point we either didn't find a cluster
5822 * or we weren't able to allocate a block from our
5823 * cluster. Free the cluster we've been trying
5824 * to use, and go to the next block group
5826 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
5827 spin_unlock(&last_ptr
->refill_lock
);
5832 spin_lock(&block_group
->free_space_ctl
->tree_lock
);
5834 block_group
->free_space_ctl
->free_space
<
5835 num_bytes
+ empty_cluster
+ empty_size
) {
5836 spin_unlock(&block_group
->free_space_ctl
->tree_lock
);
5839 spin_unlock(&block_group
->free_space_ctl
->tree_lock
);
5841 offset
= btrfs_find_space_for_alloc(block_group
, search_start
,
5842 num_bytes
, empty_size
);
5844 * If we didn't find a chunk, and we haven't failed on this
5845 * block group before, and this block group is in the middle of
5846 * caching and we are ok with waiting, then go ahead and wait
5847 * for progress to be made, and set failed_alloc to true.
5849 * If failed_alloc is true then we've already waited on this
5850 * block group once and should move on to the next block group.
5852 if (!offset
&& !failed_alloc
&& !cached
&&
5853 loop
> LOOP_CACHING_NOWAIT
) {
5854 wait_block_group_cache_progress(block_group
,
5855 num_bytes
+ empty_size
);
5856 failed_alloc
= true;
5857 goto have_block_group
;
5858 } else if (!offset
) {
5860 have_caching_bg
= true;
5864 search_start
= stripe_align(root
, offset
);
5866 /* move on to the next group */
5867 if (search_start
+ num_bytes
>
5868 used_block_group
->key
.objectid
+ used_block_group
->key
.offset
) {
5869 btrfs_add_free_space(used_block_group
, offset
, num_bytes
);
5873 if (offset
< search_start
)
5874 btrfs_add_free_space(used_block_group
, offset
,
5875 search_start
- offset
);
5876 BUG_ON(offset
> search_start
);
5878 ret
= btrfs_update_reserved_bytes(used_block_group
, num_bytes
,
5880 if (ret
== -EAGAIN
) {
5881 btrfs_add_free_space(used_block_group
, offset
, num_bytes
);
5885 /* we are all good, lets return */
5886 ins
->objectid
= search_start
;
5887 ins
->offset
= num_bytes
;
5889 trace_btrfs_reserve_extent(orig_root
, block_group
,
5890 search_start
, num_bytes
);
5891 if (used_block_group
!= block_group
)
5892 btrfs_put_block_group(used_block_group
);
5893 btrfs_put_block_group(block_group
);
5896 failed_cluster_refill
= false;
5897 failed_alloc
= false;
5898 BUG_ON(index
!= get_block_group_index(block_group
));
5899 if (used_block_group
!= block_group
)
5900 btrfs_put_block_group(used_block_group
);
5901 btrfs_put_block_group(block_group
);
5903 up_read(&space_info
->groups_sem
);
5905 if (!ins
->objectid
&& loop
>= LOOP_CACHING_WAIT
&& have_caching_bg
)
5908 if (!ins
->objectid
&& ++index
< BTRFS_NR_RAID_TYPES
)
5912 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5913 * caching kthreads as we move along
5914 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5915 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5916 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5919 if (!ins
->objectid
&& loop
< LOOP_NO_EMPTY_SIZE
) {
5922 if (loop
== LOOP_ALLOC_CHUNK
) {
5923 ret
= do_chunk_alloc(trans
, root
, data
,
5926 * Do not bail out on ENOSPC since we
5927 * can do more things.
5929 if (ret
< 0 && ret
!= -ENOSPC
) {
5930 btrfs_abort_transaction(trans
,
5936 if (loop
== LOOP_NO_EMPTY_SIZE
) {
5942 } else if (!ins
->objectid
) {
5944 } else if (ins
->objectid
) {
5952 static void dump_space_info(struct btrfs_space_info
*info
, u64 bytes
,
5953 int dump_block_groups
)
5955 struct btrfs_block_group_cache
*cache
;
5958 spin_lock(&info
->lock
);
5959 printk(KERN_INFO
"space_info %llu has %llu free, is %sfull\n",
5960 (unsigned long long)info
->flags
,
5961 (unsigned long long)(info
->total_bytes
- info
->bytes_used
-
5962 info
->bytes_pinned
- info
->bytes_reserved
-
5963 info
->bytes_readonly
),
5964 (info
->full
) ? "" : "not ");
5965 printk(KERN_INFO
"space_info total=%llu, used=%llu, pinned=%llu, "
5966 "reserved=%llu, may_use=%llu, readonly=%llu\n",
5967 (unsigned long long)info
->total_bytes
,
5968 (unsigned long long)info
->bytes_used
,
5969 (unsigned long long)info
->bytes_pinned
,
5970 (unsigned long long)info
->bytes_reserved
,
5971 (unsigned long long)info
->bytes_may_use
,
5972 (unsigned long long)info
->bytes_readonly
);
5973 spin_unlock(&info
->lock
);
5975 if (!dump_block_groups
)
5978 down_read(&info
->groups_sem
);
5980 list_for_each_entry(cache
, &info
->block_groups
[index
], list
) {
5981 spin_lock(&cache
->lock
);
5982 printk(KERN_INFO
"block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
5983 (unsigned long long)cache
->key
.objectid
,
5984 (unsigned long long)cache
->key
.offset
,
5985 (unsigned long long)btrfs_block_group_used(&cache
->item
),
5986 (unsigned long long)cache
->pinned
,
5987 (unsigned long long)cache
->reserved
,
5988 cache
->ro
? "[readonly]" : "");
5989 btrfs_dump_free_space(cache
, bytes
);
5990 spin_unlock(&cache
->lock
);
5992 if (++index
< BTRFS_NR_RAID_TYPES
)
5994 up_read(&info
->groups_sem
);
5997 int btrfs_reserve_extent(struct btrfs_trans_handle
*trans
,
5998 struct btrfs_root
*root
,
5999 u64 num_bytes
, u64 min_alloc_size
,
6000 u64 empty_size
, u64 hint_byte
,
6001 struct btrfs_key
*ins
, u64 data
)
6003 bool final_tried
= false;
6006 data
= btrfs_get_alloc_profile(root
, data
);
6008 WARN_ON(num_bytes
< root
->sectorsize
);
6009 ret
= find_free_extent(trans
, root
, num_bytes
, empty_size
,
6010 hint_byte
, ins
, data
);
6012 if (ret
== -ENOSPC
) {
6014 num_bytes
= num_bytes
>> 1;
6015 num_bytes
= num_bytes
& ~(root
->sectorsize
- 1);
6016 num_bytes
= max(num_bytes
, min_alloc_size
);
6017 if (num_bytes
== min_alloc_size
)
6020 } else if (btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
6021 struct btrfs_space_info
*sinfo
;
6023 sinfo
= __find_space_info(root
->fs_info
, data
);
6024 printk(KERN_ERR
"btrfs allocation failed flags %llu, "
6025 "wanted %llu\n", (unsigned long long)data
,
6026 (unsigned long long)num_bytes
);
6028 dump_space_info(sinfo
, num_bytes
, 1);
6032 trace_btrfs_reserved_extent_alloc(root
, ins
->objectid
, ins
->offset
);
6037 static int __btrfs_free_reserved_extent(struct btrfs_root
*root
,
6038 u64 start
, u64 len
, int pin
)
6040 struct btrfs_block_group_cache
*cache
;
6043 cache
= btrfs_lookup_block_group(root
->fs_info
, start
);
6045 printk(KERN_ERR
"Unable to find block group for %llu\n",
6046 (unsigned long long)start
);
6050 if (btrfs_test_opt(root
, DISCARD
))
6051 ret
= btrfs_discard_extent(root
, start
, len
, NULL
);
6054 pin_down_extent(root
, cache
, start
, len
, 1);
6056 btrfs_add_free_space(cache
, start
, len
);
6057 btrfs_update_reserved_bytes(cache
, len
, RESERVE_FREE
);
6059 btrfs_put_block_group(cache
);
6061 trace_btrfs_reserved_extent_free(root
, start
, len
);
6066 int btrfs_free_reserved_extent(struct btrfs_root
*root
,
6069 return __btrfs_free_reserved_extent(root
, start
, len
, 0);
6072 int btrfs_free_and_pin_reserved_extent(struct btrfs_root
*root
,
6075 return __btrfs_free_reserved_extent(root
, start
, len
, 1);
6078 static int alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
6079 struct btrfs_root
*root
,
6080 u64 parent
, u64 root_objectid
,
6081 u64 flags
, u64 owner
, u64 offset
,
6082 struct btrfs_key
*ins
, int ref_mod
)
6085 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
6086 struct btrfs_extent_item
*extent_item
;
6087 struct btrfs_extent_inline_ref
*iref
;
6088 struct btrfs_path
*path
;
6089 struct extent_buffer
*leaf
;
6094 type
= BTRFS_SHARED_DATA_REF_KEY
;
6096 type
= BTRFS_EXTENT_DATA_REF_KEY
;
6098 size
= sizeof(*extent_item
) + btrfs_extent_inline_ref_size(type
);
6100 path
= btrfs_alloc_path();
6104 path
->leave_spinning
= 1;
6105 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
6108 btrfs_free_path(path
);
6112 leaf
= path
->nodes
[0];
6113 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
6114 struct btrfs_extent_item
);
6115 btrfs_set_extent_refs(leaf
, extent_item
, ref_mod
);
6116 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
6117 btrfs_set_extent_flags(leaf
, extent_item
,
6118 flags
| BTRFS_EXTENT_FLAG_DATA
);
6120 iref
= (struct btrfs_extent_inline_ref
*)(extent_item
+ 1);
6121 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
6123 struct btrfs_shared_data_ref
*ref
;
6124 ref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
6125 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
6126 btrfs_set_shared_data_ref_count(leaf
, ref
, ref_mod
);
6128 struct btrfs_extent_data_ref
*ref
;
6129 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
6130 btrfs_set_extent_data_ref_root(leaf
, ref
, root_objectid
);
6131 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
6132 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
6133 btrfs_set_extent_data_ref_count(leaf
, ref
, ref_mod
);
6136 btrfs_mark_buffer_dirty(path
->nodes
[0]);
6137 btrfs_free_path(path
);
6139 ret
= update_block_group(trans
, root
, ins
->objectid
, ins
->offset
, 1);
6140 if (ret
) { /* -ENOENT, logic error */
6141 printk(KERN_ERR
"btrfs update block group failed for %llu "
6142 "%llu\n", (unsigned long long)ins
->objectid
,
6143 (unsigned long long)ins
->offset
);
6149 static int alloc_reserved_tree_block(struct btrfs_trans_handle
*trans
,
6150 struct btrfs_root
*root
,
6151 u64 parent
, u64 root_objectid
,
6152 u64 flags
, struct btrfs_disk_key
*key
,
6153 int level
, struct btrfs_key
*ins
)
6156 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
6157 struct btrfs_extent_item
*extent_item
;
6158 struct btrfs_tree_block_info
*block_info
;
6159 struct btrfs_extent_inline_ref
*iref
;
6160 struct btrfs_path
*path
;
6161 struct extent_buffer
*leaf
;
6162 u32 size
= sizeof(*extent_item
) + sizeof(*block_info
) + sizeof(*iref
);
6164 path
= btrfs_alloc_path();
6168 path
->leave_spinning
= 1;
6169 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
6172 btrfs_free_path(path
);
6176 leaf
= path
->nodes
[0];
6177 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
6178 struct btrfs_extent_item
);
6179 btrfs_set_extent_refs(leaf
, extent_item
, 1);
6180 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
6181 btrfs_set_extent_flags(leaf
, extent_item
,
6182 flags
| BTRFS_EXTENT_FLAG_TREE_BLOCK
);
6183 block_info
= (struct btrfs_tree_block_info
*)(extent_item
+ 1);
6185 btrfs_set_tree_block_key(leaf
, block_info
, key
);
6186 btrfs_set_tree_block_level(leaf
, block_info
, level
);
6188 iref
= (struct btrfs_extent_inline_ref
*)(block_info
+ 1);
6190 BUG_ON(!(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
));
6191 btrfs_set_extent_inline_ref_type(leaf
, iref
,
6192 BTRFS_SHARED_BLOCK_REF_KEY
);
6193 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
6195 btrfs_set_extent_inline_ref_type(leaf
, iref
,
6196 BTRFS_TREE_BLOCK_REF_KEY
);
6197 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
6200 btrfs_mark_buffer_dirty(leaf
);
6201 btrfs_free_path(path
);
6203 ret
= update_block_group(trans
, root
, ins
->objectid
, ins
->offset
, 1);
6204 if (ret
) { /* -ENOENT, logic error */
6205 printk(KERN_ERR
"btrfs update block group failed for %llu "
6206 "%llu\n", (unsigned long long)ins
->objectid
,
6207 (unsigned long long)ins
->offset
);
6213 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
6214 struct btrfs_root
*root
,
6215 u64 root_objectid
, u64 owner
,
6216 u64 offset
, struct btrfs_key
*ins
)
6220 BUG_ON(root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
6222 ret
= btrfs_add_delayed_data_ref(root
->fs_info
, trans
, ins
->objectid
,
6224 root_objectid
, owner
, offset
,
6225 BTRFS_ADD_DELAYED_EXTENT
, NULL
, 0);
6230 * this is used by the tree logging recovery code. It records that
6231 * an extent has been allocated and makes sure to clear the free
6232 * space cache bits as well
6234 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle
*trans
,
6235 struct btrfs_root
*root
,
6236 u64 root_objectid
, u64 owner
, u64 offset
,
6237 struct btrfs_key
*ins
)
6240 struct btrfs_block_group_cache
*block_group
;
6241 struct btrfs_caching_control
*caching_ctl
;
6242 u64 start
= ins
->objectid
;
6243 u64 num_bytes
= ins
->offset
;
6245 block_group
= btrfs_lookup_block_group(root
->fs_info
, ins
->objectid
);
6246 cache_block_group(block_group
, trans
, NULL
, 0);
6247 caching_ctl
= get_caching_control(block_group
);
6250 BUG_ON(!block_group_cache_done(block_group
));
6251 ret
= btrfs_remove_free_space(block_group
, start
, num_bytes
);
6252 BUG_ON(ret
); /* -ENOMEM */
6254 mutex_lock(&caching_ctl
->mutex
);
6256 if (start
>= caching_ctl
->progress
) {
6257 ret
= add_excluded_extent(root
, start
, num_bytes
);
6258 BUG_ON(ret
); /* -ENOMEM */
6259 } else if (start
+ num_bytes
<= caching_ctl
->progress
) {
6260 ret
= btrfs_remove_free_space(block_group
,
6262 BUG_ON(ret
); /* -ENOMEM */
6264 num_bytes
= caching_ctl
->progress
- start
;
6265 ret
= btrfs_remove_free_space(block_group
,
6267 BUG_ON(ret
); /* -ENOMEM */
6269 start
= caching_ctl
->progress
;
6270 num_bytes
= ins
->objectid
+ ins
->offset
-
6271 caching_ctl
->progress
;
6272 ret
= add_excluded_extent(root
, start
, num_bytes
);
6273 BUG_ON(ret
); /* -ENOMEM */
6276 mutex_unlock(&caching_ctl
->mutex
);
6277 put_caching_control(caching_ctl
);
6280 ret
= btrfs_update_reserved_bytes(block_group
, ins
->offset
,
6281 RESERVE_ALLOC_NO_ACCOUNT
);
6282 BUG_ON(ret
); /* logic error */
6283 btrfs_put_block_group(block_group
);
6284 ret
= alloc_reserved_file_extent(trans
, root
, 0, root_objectid
,
6285 0, owner
, offset
, ins
, 1);
6289 struct extent_buffer
*btrfs_init_new_buffer(struct btrfs_trans_handle
*trans
,
6290 struct btrfs_root
*root
,
6291 u64 bytenr
, u32 blocksize
,
6294 struct extent_buffer
*buf
;
6296 buf
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
6298 return ERR_PTR(-ENOMEM
);
6299 btrfs_set_header_generation(buf
, trans
->transid
);
6300 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, buf
, level
);
6301 btrfs_tree_lock(buf
);
6302 clean_tree_block(trans
, root
, buf
);
6303 clear_bit(EXTENT_BUFFER_STALE
, &buf
->bflags
);
6305 btrfs_set_lock_blocking(buf
);
6306 btrfs_set_buffer_uptodate(buf
);
6308 if (root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
) {
6310 * we allow two log transactions at a time, use different
6311 * EXENT bit to differentiate dirty pages.
6313 if (root
->log_transid
% 2 == 0)
6314 set_extent_dirty(&root
->dirty_log_pages
, buf
->start
,
6315 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
6317 set_extent_new(&root
->dirty_log_pages
, buf
->start
,
6318 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
6320 set_extent_dirty(&trans
->transaction
->dirty_pages
, buf
->start
,
6321 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
6323 trans
->blocks_used
++;
6324 /* this returns a buffer locked for blocking */
6328 static struct btrfs_block_rsv
*
6329 use_block_rsv(struct btrfs_trans_handle
*trans
,
6330 struct btrfs_root
*root
, u32 blocksize
)
6332 struct btrfs_block_rsv
*block_rsv
;
6333 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
6336 block_rsv
= get_block_rsv(trans
, root
);
6338 if (block_rsv
->size
== 0) {
6339 ret
= reserve_metadata_bytes(root
, block_rsv
, blocksize
,
6340 BTRFS_RESERVE_NO_FLUSH
);
6342 * If we couldn't reserve metadata bytes try and use some from
6343 * the global reserve.
6345 if (ret
&& block_rsv
!= global_rsv
) {
6346 ret
= block_rsv_use_bytes(global_rsv
, blocksize
);
6349 return ERR_PTR(ret
);
6351 return ERR_PTR(ret
);
6356 ret
= block_rsv_use_bytes(block_rsv
, blocksize
);
6359 if (ret
&& !block_rsv
->failfast
) {
6360 static DEFINE_RATELIMIT_STATE(_rs
,
6361 DEFAULT_RATELIMIT_INTERVAL
,
6362 /*DEFAULT_RATELIMIT_BURST*/ 2);
6363 if (__ratelimit(&_rs
))
6364 WARN(1, KERN_DEBUG
"btrfs: block rsv returned %d\n",
6366 ret
= reserve_metadata_bytes(root
, block_rsv
, blocksize
,
6367 BTRFS_RESERVE_NO_FLUSH
);
6370 } else if (ret
&& block_rsv
!= global_rsv
) {
6371 ret
= block_rsv_use_bytes(global_rsv
, blocksize
);
6377 return ERR_PTR(-ENOSPC
);
6380 static void unuse_block_rsv(struct btrfs_fs_info
*fs_info
,
6381 struct btrfs_block_rsv
*block_rsv
, u32 blocksize
)
6383 block_rsv_add_bytes(block_rsv
, blocksize
, 0);
6384 block_rsv_release_bytes(fs_info
, block_rsv
, NULL
, 0);
6388 * finds a free extent and does all the dirty work required for allocation
6389 * returns the key for the extent through ins, and a tree buffer for
6390 * the first block of the extent through buf.
6392 * returns the tree buffer or NULL.
6394 struct extent_buffer
*btrfs_alloc_free_block(struct btrfs_trans_handle
*trans
,
6395 struct btrfs_root
*root
, u32 blocksize
,
6396 u64 parent
, u64 root_objectid
,
6397 struct btrfs_disk_key
*key
, int level
,
6398 u64 hint
, u64 empty_size
)
6400 struct btrfs_key ins
;
6401 struct btrfs_block_rsv
*block_rsv
;
6402 struct extent_buffer
*buf
;
6407 block_rsv
= use_block_rsv(trans
, root
, blocksize
);
6408 if (IS_ERR(block_rsv
))
6409 return ERR_CAST(block_rsv
);
6411 ret
= btrfs_reserve_extent(trans
, root
, blocksize
, blocksize
,
6412 empty_size
, hint
, &ins
, 0);
6414 unuse_block_rsv(root
->fs_info
, block_rsv
, blocksize
);
6415 return ERR_PTR(ret
);
6418 buf
= btrfs_init_new_buffer(trans
, root
, ins
.objectid
,
6420 BUG_ON(IS_ERR(buf
)); /* -ENOMEM */
6422 if (root_objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
6424 parent
= ins
.objectid
;
6425 flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
6429 if (root_objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
6430 struct btrfs_delayed_extent_op
*extent_op
;
6431 extent_op
= btrfs_alloc_delayed_extent_op();
6432 BUG_ON(!extent_op
); /* -ENOMEM */
6434 memcpy(&extent_op
->key
, key
, sizeof(extent_op
->key
));
6436 memset(&extent_op
->key
, 0, sizeof(extent_op
->key
));
6437 extent_op
->flags_to_set
= flags
;
6438 extent_op
->update_key
= 1;
6439 extent_op
->update_flags
= 1;
6440 extent_op
->is_data
= 0;
6442 ret
= btrfs_add_delayed_tree_ref(root
->fs_info
, trans
,
6444 ins
.offset
, parent
, root_objectid
,
6445 level
, BTRFS_ADD_DELAYED_EXTENT
,
6447 BUG_ON(ret
); /* -ENOMEM */
6452 struct walk_control
{
6453 u64 refs
[BTRFS_MAX_LEVEL
];
6454 u64 flags
[BTRFS_MAX_LEVEL
];
6455 struct btrfs_key update_progress
;
6466 #define DROP_REFERENCE 1
6467 #define UPDATE_BACKREF 2
6469 static noinline
void reada_walk_down(struct btrfs_trans_handle
*trans
,
6470 struct btrfs_root
*root
,
6471 struct walk_control
*wc
,
6472 struct btrfs_path
*path
)
6480 struct btrfs_key key
;
6481 struct extent_buffer
*eb
;
6486 if (path
->slots
[wc
->level
] < wc
->reada_slot
) {
6487 wc
->reada_count
= wc
->reada_count
* 2 / 3;
6488 wc
->reada_count
= max(wc
->reada_count
, 2);
6490 wc
->reada_count
= wc
->reada_count
* 3 / 2;
6491 wc
->reada_count
= min_t(int, wc
->reada_count
,
6492 BTRFS_NODEPTRS_PER_BLOCK(root
));
6495 eb
= path
->nodes
[wc
->level
];
6496 nritems
= btrfs_header_nritems(eb
);
6497 blocksize
= btrfs_level_size(root
, wc
->level
- 1);
6499 for (slot
= path
->slots
[wc
->level
]; slot
< nritems
; slot
++) {
6500 if (nread
>= wc
->reada_count
)
6504 bytenr
= btrfs_node_blockptr(eb
, slot
);
6505 generation
= btrfs_node_ptr_generation(eb
, slot
);
6507 if (slot
== path
->slots
[wc
->level
])
6510 if (wc
->stage
== UPDATE_BACKREF
&&
6511 generation
<= root
->root_key
.offset
)
6514 /* We don't lock the tree block, it's OK to be racy here */
6515 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
, blocksize
,
6517 /* We don't care about errors in readahead. */
6522 if (wc
->stage
== DROP_REFERENCE
) {
6526 if (wc
->level
== 1 &&
6527 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
6529 if (!wc
->update_ref
||
6530 generation
<= root
->root_key
.offset
)
6532 btrfs_node_key_to_cpu(eb
, &key
, slot
);
6533 ret
= btrfs_comp_cpu_keys(&key
,
6534 &wc
->update_progress
);
6538 if (wc
->level
== 1 &&
6539 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
6543 ret
= readahead_tree_block(root
, bytenr
, blocksize
,
6549 wc
->reada_slot
= slot
;
6553 * hepler to process tree block while walking down the tree.
6555 * when wc->stage == UPDATE_BACKREF, this function updates
6556 * back refs for pointers in the block.
6558 * NOTE: return value 1 means we should stop walking down.
6560 static noinline
int walk_down_proc(struct btrfs_trans_handle
*trans
,
6561 struct btrfs_root
*root
,
6562 struct btrfs_path
*path
,
6563 struct walk_control
*wc
, int lookup_info
)
6565 int level
= wc
->level
;
6566 struct extent_buffer
*eb
= path
->nodes
[level
];
6567 u64 flag
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
6570 if (wc
->stage
== UPDATE_BACKREF
&&
6571 btrfs_header_owner(eb
) != root
->root_key
.objectid
)
6575 * when reference count of tree block is 1, it won't increase
6576 * again. once full backref flag is set, we never clear it.
6579 ((wc
->stage
== DROP_REFERENCE
&& wc
->refs
[level
] != 1) ||
6580 (wc
->stage
== UPDATE_BACKREF
&& !(wc
->flags
[level
] & flag
)))) {
6581 BUG_ON(!path
->locks
[level
]);
6582 ret
= btrfs_lookup_extent_info(trans
, root
,
6586 BUG_ON(ret
== -ENOMEM
);
6589 BUG_ON(wc
->refs
[level
] == 0);
6592 if (wc
->stage
== DROP_REFERENCE
) {
6593 if (wc
->refs
[level
] > 1)
6596 if (path
->locks
[level
] && !wc
->keep_locks
) {
6597 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
6598 path
->locks
[level
] = 0;
6603 /* wc->stage == UPDATE_BACKREF */
6604 if (!(wc
->flags
[level
] & flag
)) {
6605 BUG_ON(!path
->locks
[level
]);
6606 ret
= btrfs_inc_ref(trans
, root
, eb
, 1, wc
->for_reloc
);
6607 BUG_ON(ret
); /* -ENOMEM */
6608 ret
= btrfs_dec_ref(trans
, root
, eb
, 0, wc
->for_reloc
);
6609 BUG_ON(ret
); /* -ENOMEM */
6610 ret
= btrfs_set_disk_extent_flags(trans
, root
, eb
->start
,
6612 BUG_ON(ret
); /* -ENOMEM */
6613 wc
->flags
[level
] |= flag
;
6617 * the block is shared by multiple trees, so it's not good to
6618 * keep the tree lock
6620 if (path
->locks
[level
] && level
> 0) {
6621 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
6622 path
->locks
[level
] = 0;
6628 * hepler to process tree block pointer.
6630 * when wc->stage == DROP_REFERENCE, this function checks
6631 * reference count of the block pointed to. if the block
6632 * is shared and we need update back refs for the subtree
6633 * rooted at the block, this function changes wc->stage to
6634 * UPDATE_BACKREF. if the block is shared and there is no
6635 * need to update back, this function drops the reference
6638 * NOTE: return value 1 means we should stop walking down.
6640 static noinline
int do_walk_down(struct btrfs_trans_handle
*trans
,
6641 struct btrfs_root
*root
,
6642 struct btrfs_path
*path
,
6643 struct walk_control
*wc
, int *lookup_info
)
6649 struct btrfs_key key
;
6650 struct extent_buffer
*next
;
6651 int level
= wc
->level
;
6655 generation
= btrfs_node_ptr_generation(path
->nodes
[level
],
6656 path
->slots
[level
]);
6658 * if the lower level block was created before the snapshot
6659 * was created, we know there is no need to update back refs
6662 if (wc
->stage
== UPDATE_BACKREF
&&
6663 generation
<= root
->root_key
.offset
) {
6668 bytenr
= btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]);
6669 blocksize
= btrfs_level_size(root
, level
- 1);
6671 next
= btrfs_find_tree_block(root
, bytenr
, blocksize
);
6673 next
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
6678 btrfs_tree_lock(next
);
6679 btrfs_set_lock_blocking(next
);
6681 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
, blocksize
,
6682 &wc
->refs
[level
- 1],
6683 &wc
->flags
[level
- 1]);
6685 btrfs_tree_unlock(next
);
6689 BUG_ON(wc
->refs
[level
- 1] == 0);
6692 if (wc
->stage
== DROP_REFERENCE
) {
6693 if (wc
->refs
[level
- 1] > 1) {
6695 (wc
->flags
[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF
))
6698 if (!wc
->update_ref
||
6699 generation
<= root
->root_key
.offset
)
6702 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
6703 path
->slots
[level
]);
6704 ret
= btrfs_comp_cpu_keys(&key
, &wc
->update_progress
);
6708 wc
->stage
= UPDATE_BACKREF
;
6709 wc
->shared_level
= level
- 1;
6713 (wc
->flags
[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF
))
6717 if (!btrfs_buffer_uptodate(next
, generation
, 0)) {
6718 btrfs_tree_unlock(next
);
6719 free_extent_buffer(next
);
6725 if (reada
&& level
== 1)
6726 reada_walk_down(trans
, root
, wc
, path
);
6727 next
= read_tree_block(root
, bytenr
, blocksize
, generation
);
6730 btrfs_tree_lock(next
);
6731 btrfs_set_lock_blocking(next
);
6735 BUG_ON(level
!= btrfs_header_level(next
));
6736 path
->nodes
[level
] = next
;
6737 path
->slots
[level
] = 0;
6738 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
6744 wc
->refs
[level
- 1] = 0;
6745 wc
->flags
[level
- 1] = 0;
6746 if (wc
->stage
== DROP_REFERENCE
) {
6747 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
) {
6748 parent
= path
->nodes
[level
]->start
;
6750 BUG_ON(root
->root_key
.objectid
!=
6751 btrfs_header_owner(path
->nodes
[level
]));
6755 ret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
, parent
,
6756 root
->root_key
.objectid
, level
- 1, 0, 0);
6757 BUG_ON(ret
); /* -ENOMEM */
6759 btrfs_tree_unlock(next
);
6760 free_extent_buffer(next
);
6766 * hepler to process tree block while walking up the tree.
6768 * when wc->stage == DROP_REFERENCE, this function drops
6769 * reference count on the block.
6771 * when wc->stage == UPDATE_BACKREF, this function changes
6772 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6773 * to UPDATE_BACKREF previously while processing the block.
6775 * NOTE: return value 1 means we should stop walking up.
6777 static noinline
int walk_up_proc(struct btrfs_trans_handle
*trans
,
6778 struct btrfs_root
*root
,
6779 struct btrfs_path
*path
,
6780 struct walk_control
*wc
)
6783 int level
= wc
->level
;
6784 struct extent_buffer
*eb
= path
->nodes
[level
];
6787 if (wc
->stage
== UPDATE_BACKREF
) {
6788 BUG_ON(wc
->shared_level
< level
);
6789 if (level
< wc
->shared_level
)
6792 ret
= find_next_key(path
, level
+ 1, &wc
->update_progress
);
6796 wc
->stage
= DROP_REFERENCE
;
6797 wc
->shared_level
= -1;
6798 path
->slots
[level
] = 0;
6801 * check reference count again if the block isn't locked.
6802 * we should start walking down the tree again if reference
6805 if (!path
->locks
[level
]) {
6807 btrfs_tree_lock(eb
);
6808 btrfs_set_lock_blocking(eb
);
6809 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
6811 ret
= btrfs_lookup_extent_info(trans
, root
,
6816 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
6817 path
->locks
[level
] = 0;
6820 BUG_ON(wc
->refs
[level
] == 0);
6821 if (wc
->refs
[level
] == 1) {
6822 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
6823 path
->locks
[level
] = 0;
6829 /* wc->stage == DROP_REFERENCE */
6830 BUG_ON(wc
->refs
[level
] > 1 && !path
->locks
[level
]);
6832 if (wc
->refs
[level
] == 1) {
6834 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
6835 ret
= btrfs_dec_ref(trans
, root
, eb
, 1,
6838 ret
= btrfs_dec_ref(trans
, root
, eb
, 0,
6840 BUG_ON(ret
); /* -ENOMEM */
6842 /* make block locked assertion in clean_tree_block happy */
6843 if (!path
->locks
[level
] &&
6844 btrfs_header_generation(eb
) == trans
->transid
) {
6845 btrfs_tree_lock(eb
);
6846 btrfs_set_lock_blocking(eb
);
6847 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
6849 clean_tree_block(trans
, root
, eb
);
6852 if (eb
== root
->node
) {
6853 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
6856 BUG_ON(root
->root_key
.objectid
!=
6857 btrfs_header_owner(eb
));
6859 if (wc
->flags
[level
+ 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
6860 parent
= path
->nodes
[level
+ 1]->start
;
6862 BUG_ON(root
->root_key
.objectid
!=
6863 btrfs_header_owner(path
->nodes
[level
+ 1]));
6866 btrfs_free_tree_block(trans
, root
, eb
, parent
, wc
->refs
[level
] == 1);
6868 wc
->refs
[level
] = 0;
6869 wc
->flags
[level
] = 0;
6873 static noinline
int walk_down_tree(struct btrfs_trans_handle
*trans
,
6874 struct btrfs_root
*root
,
6875 struct btrfs_path
*path
,
6876 struct walk_control
*wc
)
6878 int level
= wc
->level
;
6879 int lookup_info
= 1;
6882 while (level
>= 0) {
6883 ret
= walk_down_proc(trans
, root
, path
, wc
, lookup_info
);
6890 if (path
->slots
[level
] >=
6891 btrfs_header_nritems(path
->nodes
[level
]))
6894 ret
= do_walk_down(trans
, root
, path
, wc
, &lookup_info
);
6896 path
->slots
[level
]++;
6905 static noinline
int walk_up_tree(struct btrfs_trans_handle
*trans
,
6906 struct btrfs_root
*root
,
6907 struct btrfs_path
*path
,
6908 struct walk_control
*wc
, int max_level
)
6910 int level
= wc
->level
;
6913 path
->slots
[level
] = btrfs_header_nritems(path
->nodes
[level
]);
6914 while (level
< max_level
&& path
->nodes
[level
]) {
6916 if (path
->slots
[level
] + 1 <
6917 btrfs_header_nritems(path
->nodes
[level
])) {
6918 path
->slots
[level
]++;
6921 ret
= walk_up_proc(trans
, root
, path
, wc
);
6925 if (path
->locks
[level
]) {
6926 btrfs_tree_unlock_rw(path
->nodes
[level
],
6927 path
->locks
[level
]);
6928 path
->locks
[level
] = 0;
6930 free_extent_buffer(path
->nodes
[level
]);
6931 path
->nodes
[level
] = NULL
;
6939 * drop a subvolume tree.
6941 * this function traverses the tree freeing any blocks that only
6942 * referenced by the tree.
6944 * when a shared tree block is found. this function decreases its
6945 * reference count by one. if update_ref is true, this function
6946 * also make sure backrefs for the shared block and all lower level
6947 * blocks are properly updated.
6949 int btrfs_drop_snapshot(struct btrfs_root
*root
,
6950 struct btrfs_block_rsv
*block_rsv
, int update_ref
,
6953 struct btrfs_path
*path
;
6954 struct btrfs_trans_handle
*trans
;
6955 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
6956 struct btrfs_root_item
*root_item
= &root
->root_item
;
6957 struct walk_control
*wc
;
6958 struct btrfs_key key
;
6963 path
= btrfs_alloc_path();
6969 wc
= kzalloc(sizeof(*wc
), GFP_NOFS
);
6971 btrfs_free_path(path
);
6976 trans
= btrfs_start_transaction(tree_root
, 0);
6977 if (IS_ERR(trans
)) {
6978 err
= PTR_ERR(trans
);
6983 trans
->block_rsv
= block_rsv
;
6985 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
6986 level
= btrfs_header_level(root
->node
);
6987 path
->nodes
[level
] = btrfs_lock_root_node(root
);
6988 btrfs_set_lock_blocking(path
->nodes
[level
]);
6989 path
->slots
[level
] = 0;
6990 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
6991 memset(&wc
->update_progress
, 0,
6992 sizeof(wc
->update_progress
));
6994 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
6995 memcpy(&wc
->update_progress
, &key
,
6996 sizeof(wc
->update_progress
));
6998 level
= root_item
->drop_level
;
7000 path
->lowest_level
= level
;
7001 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
7002 path
->lowest_level
= 0;
7010 * unlock our path, this is safe because only this
7011 * function is allowed to delete this snapshot
7013 btrfs_unlock_up_safe(path
, 0);
7015 level
= btrfs_header_level(root
->node
);
7017 btrfs_tree_lock(path
->nodes
[level
]);
7018 btrfs_set_lock_blocking(path
->nodes
[level
]);
7020 ret
= btrfs_lookup_extent_info(trans
, root
,
7021 path
->nodes
[level
]->start
,
7022 path
->nodes
[level
]->len
,
7029 BUG_ON(wc
->refs
[level
] == 0);
7031 if (level
== root_item
->drop_level
)
7034 btrfs_tree_unlock(path
->nodes
[level
]);
7035 WARN_ON(wc
->refs
[level
] != 1);
7041 wc
->shared_level
= -1;
7042 wc
->stage
= DROP_REFERENCE
;
7043 wc
->update_ref
= update_ref
;
7045 wc
->for_reloc
= for_reloc
;
7046 wc
->reada_count
= BTRFS_NODEPTRS_PER_BLOCK(root
);
7049 ret
= walk_down_tree(trans
, root
, path
, wc
);
7055 ret
= walk_up_tree(trans
, root
, path
, wc
, BTRFS_MAX_LEVEL
);
7062 BUG_ON(wc
->stage
!= DROP_REFERENCE
);
7066 if (wc
->stage
== DROP_REFERENCE
) {
7068 btrfs_node_key(path
->nodes
[level
],
7069 &root_item
->drop_progress
,
7070 path
->slots
[level
]);
7071 root_item
->drop_level
= level
;
7074 BUG_ON(wc
->level
== 0);
7075 if (btrfs_should_end_transaction(trans
, tree_root
)) {
7076 ret
= btrfs_update_root(trans
, tree_root
,
7080 btrfs_abort_transaction(trans
, tree_root
, ret
);
7085 btrfs_end_transaction_throttle(trans
, tree_root
);
7086 trans
= btrfs_start_transaction(tree_root
, 0);
7087 if (IS_ERR(trans
)) {
7088 err
= PTR_ERR(trans
);
7092 trans
->block_rsv
= block_rsv
;
7095 btrfs_release_path(path
);
7099 ret
= btrfs_del_root(trans
, tree_root
, &root
->root_key
);
7101 btrfs_abort_transaction(trans
, tree_root
, ret
);
7105 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
7106 ret
= btrfs_find_last_root(tree_root
, root
->root_key
.objectid
,
7109 btrfs_abort_transaction(trans
, tree_root
, ret
);
7112 } else if (ret
> 0) {
7113 /* if we fail to delete the orphan item this time
7114 * around, it'll get picked up the next time.
7116 * The most common failure here is just -ENOENT.
7118 btrfs_del_orphan_item(trans
, tree_root
,
7119 root
->root_key
.objectid
);
7123 if (root
->in_radix
) {
7124 btrfs_free_fs_root(tree_root
->fs_info
, root
);
7126 free_extent_buffer(root
->node
);
7127 free_extent_buffer(root
->commit_root
);
7131 btrfs_end_transaction_throttle(trans
, tree_root
);
7134 btrfs_free_path(path
);
7137 btrfs_std_error(root
->fs_info
, err
);
7142 * drop subtree rooted at tree block 'node'.
7144 * NOTE: this function will unlock and release tree block 'node'
7145 * only used by relocation code
7147 int btrfs_drop_subtree(struct btrfs_trans_handle
*trans
,
7148 struct btrfs_root
*root
,
7149 struct extent_buffer
*node
,
7150 struct extent_buffer
*parent
)
7152 struct btrfs_path
*path
;
7153 struct walk_control
*wc
;
7159 BUG_ON(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
7161 path
= btrfs_alloc_path();
7165 wc
= kzalloc(sizeof(*wc
), GFP_NOFS
);
7167 btrfs_free_path(path
);
7171 btrfs_assert_tree_locked(parent
);
7172 parent_level
= btrfs_header_level(parent
);
7173 extent_buffer_get(parent
);
7174 path
->nodes
[parent_level
] = parent
;
7175 path
->slots
[parent_level
] = btrfs_header_nritems(parent
);
7177 btrfs_assert_tree_locked(node
);
7178 level
= btrfs_header_level(node
);
7179 path
->nodes
[level
] = node
;
7180 path
->slots
[level
] = 0;
7181 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
7183 wc
->refs
[parent_level
] = 1;
7184 wc
->flags
[parent_level
] = BTRFS_BLOCK_FLAG_FULL_BACKREF
;
7186 wc
->shared_level
= -1;
7187 wc
->stage
= DROP_REFERENCE
;
7191 wc
->reada_count
= BTRFS_NODEPTRS_PER_BLOCK(root
);
7194 wret
= walk_down_tree(trans
, root
, path
, wc
);
7200 wret
= walk_up_tree(trans
, root
, path
, wc
, parent_level
);
7208 btrfs_free_path(path
);
7212 static u64
update_block_group_flags(struct btrfs_root
*root
, u64 flags
)
7218 * if restripe for this chunk_type is on pick target profile and
7219 * return, otherwise do the usual balance
7221 stripped
= get_restripe_target(root
->fs_info
, flags
);
7223 return extended_to_chunk(stripped
);
7226 * we add in the count of missing devices because we want
7227 * to make sure that any RAID levels on a degraded FS
7228 * continue to be honored.
7230 num_devices
= root
->fs_info
->fs_devices
->rw_devices
+
7231 root
->fs_info
->fs_devices
->missing_devices
;
7233 stripped
= BTRFS_BLOCK_GROUP_RAID0
|
7234 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
;
7236 if (num_devices
== 1) {
7237 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
7238 stripped
= flags
& ~stripped
;
7240 /* turn raid0 into single device chunks */
7241 if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
7244 /* turn mirroring into duplication */
7245 if (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
7246 BTRFS_BLOCK_GROUP_RAID10
))
7247 return stripped
| BTRFS_BLOCK_GROUP_DUP
;
7249 /* they already had raid on here, just return */
7250 if (flags
& stripped
)
7253 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
7254 stripped
= flags
& ~stripped
;
7256 /* switch duplicated blocks with raid1 */
7257 if (flags
& BTRFS_BLOCK_GROUP_DUP
)
7258 return stripped
| BTRFS_BLOCK_GROUP_RAID1
;
7260 /* this is drive concat, leave it alone */
7266 static int set_block_group_ro(struct btrfs_block_group_cache
*cache
, int force
)
7268 struct btrfs_space_info
*sinfo
= cache
->space_info
;
7270 u64 min_allocable_bytes
;
7275 * We need some metadata space and system metadata space for
7276 * allocating chunks in some corner cases until we force to set
7277 * it to be readonly.
7280 (BTRFS_BLOCK_GROUP_SYSTEM
| BTRFS_BLOCK_GROUP_METADATA
)) &&
7282 min_allocable_bytes
= 1 * 1024 * 1024;
7284 min_allocable_bytes
= 0;
7286 spin_lock(&sinfo
->lock
);
7287 spin_lock(&cache
->lock
);
7294 num_bytes
= cache
->key
.offset
- cache
->reserved
- cache
->pinned
-
7295 cache
->bytes_super
- btrfs_block_group_used(&cache
->item
);
7297 if (sinfo
->bytes_used
+ sinfo
->bytes_reserved
+ sinfo
->bytes_pinned
+
7298 sinfo
->bytes_may_use
+ sinfo
->bytes_readonly
+ num_bytes
+
7299 min_allocable_bytes
<= sinfo
->total_bytes
) {
7300 sinfo
->bytes_readonly
+= num_bytes
;
7305 spin_unlock(&cache
->lock
);
7306 spin_unlock(&sinfo
->lock
);
7310 int btrfs_set_block_group_ro(struct btrfs_root
*root
,
7311 struct btrfs_block_group_cache
*cache
)
7314 struct btrfs_trans_handle
*trans
;
7320 trans
= btrfs_join_transaction(root
);
7322 return PTR_ERR(trans
);
7324 alloc_flags
= update_block_group_flags(root
, cache
->flags
);
7325 if (alloc_flags
!= cache
->flags
) {
7326 ret
= do_chunk_alloc(trans
, root
, alloc_flags
,
7332 ret
= set_block_group_ro(cache
, 0);
7335 alloc_flags
= get_alloc_profile(root
, cache
->space_info
->flags
);
7336 ret
= do_chunk_alloc(trans
, root
, alloc_flags
,
7340 ret
= set_block_group_ro(cache
, 0);
7342 btrfs_end_transaction(trans
, root
);
7346 int btrfs_force_chunk_alloc(struct btrfs_trans_handle
*trans
,
7347 struct btrfs_root
*root
, u64 type
)
7349 u64 alloc_flags
= get_alloc_profile(root
, type
);
7350 return do_chunk_alloc(trans
, root
, alloc_flags
,
7355 * helper to account the unused space of all the readonly block group in the
7356 * list. takes mirrors into account.
7358 static u64
__btrfs_get_ro_block_group_free_space(struct list_head
*groups_list
)
7360 struct btrfs_block_group_cache
*block_group
;
7364 list_for_each_entry(block_group
, groups_list
, list
) {
7365 spin_lock(&block_group
->lock
);
7367 if (!block_group
->ro
) {
7368 spin_unlock(&block_group
->lock
);
7372 if (block_group
->flags
& (BTRFS_BLOCK_GROUP_RAID1
|
7373 BTRFS_BLOCK_GROUP_RAID10
|
7374 BTRFS_BLOCK_GROUP_DUP
))
7379 free_bytes
+= (block_group
->key
.offset
-
7380 btrfs_block_group_used(&block_group
->item
)) *
7383 spin_unlock(&block_group
->lock
);
7390 * helper to account the unused space of all the readonly block group in the
7391 * space_info. takes mirrors into account.
7393 u64
btrfs_account_ro_block_groups_free_space(struct btrfs_space_info
*sinfo
)
7398 spin_lock(&sinfo
->lock
);
7400 for(i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++)
7401 if (!list_empty(&sinfo
->block_groups
[i
]))
7402 free_bytes
+= __btrfs_get_ro_block_group_free_space(
7403 &sinfo
->block_groups
[i
]);
7405 spin_unlock(&sinfo
->lock
);
7410 void btrfs_set_block_group_rw(struct btrfs_root
*root
,
7411 struct btrfs_block_group_cache
*cache
)
7413 struct btrfs_space_info
*sinfo
= cache
->space_info
;
7418 spin_lock(&sinfo
->lock
);
7419 spin_lock(&cache
->lock
);
7420 num_bytes
= cache
->key
.offset
- cache
->reserved
- cache
->pinned
-
7421 cache
->bytes_super
- btrfs_block_group_used(&cache
->item
);
7422 sinfo
->bytes_readonly
-= num_bytes
;
7424 spin_unlock(&cache
->lock
);
7425 spin_unlock(&sinfo
->lock
);
7429 * checks to see if its even possible to relocate this block group.
7431 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7432 * ok to go ahead and try.
7434 int btrfs_can_relocate(struct btrfs_root
*root
, u64 bytenr
)
7436 struct btrfs_block_group_cache
*block_group
;
7437 struct btrfs_space_info
*space_info
;
7438 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
7439 struct btrfs_device
*device
;
7448 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
7450 /* odd, couldn't find the block group, leave it alone */
7454 min_free
= btrfs_block_group_used(&block_group
->item
);
7456 /* no bytes used, we're good */
7460 space_info
= block_group
->space_info
;
7461 spin_lock(&space_info
->lock
);
7463 full
= space_info
->full
;
7466 * if this is the last block group we have in this space, we can't
7467 * relocate it unless we're able to allocate a new chunk below.
7469 * Otherwise, we need to make sure we have room in the space to handle
7470 * all of the extents from this block group. If we can, we're good
7472 if ((space_info
->total_bytes
!= block_group
->key
.offset
) &&
7473 (space_info
->bytes_used
+ space_info
->bytes_reserved
+
7474 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
7475 min_free
< space_info
->total_bytes
)) {
7476 spin_unlock(&space_info
->lock
);
7479 spin_unlock(&space_info
->lock
);
7482 * ok we don't have enough space, but maybe we have free space on our
7483 * devices to allocate new chunks for relocation, so loop through our
7484 * alloc devices and guess if we have enough space. if this block
7485 * group is going to be restriped, run checks against the target
7486 * profile instead of the current one.
7498 target
= get_restripe_target(root
->fs_info
, block_group
->flags
);
7500 index
= __get_raid_index(extended_to_chunk(target
));
7503 * this is just a balance, so if we were marked as full
7504 * we know there is no space for a new chunk
7509 index
= get_block_group_index(block_group
);
7516 } else if (index
== 1) {
7518 } else if (index
== 2) {
7521 } else if (index
== 3) {
7522 dev_min
= fs_devices
->rw_devices
;
7523 do_div(min_free
, dev_min
);
7526 mutex_lock(&root
->fs_info
->chunk_mutex
);
7527 list_for_each_entry(device
, &fs_devices
->alloc_list
, dev_alloc_list
) {
7531 * check to make sure we can actually find a chunk with enough
7532 * space to fit our block group in.
7534 if (device
->total_bytes
> device
->bytes_used
+ min_free
&&
7535 !device
->is_tgtdev_for_dev_replace
) {
7536 ret
= find_free_dev_extent(device
, min_free
,
7541 if (dev_nr
>= dev_min
)
7547 mutex_unlock(&root
->fs_info
->chunk_mutex
);
7549 btrfs_put_block_group(block_group
);
7553 static int find_first_block_group(struct btrfs_root
*root
,
7554 struct btrfs_path
*path
, struct btrfs_key
*key
)
7557 struct btrfs_key found_key
;
7558 struct extent_buffer
*leaf
;
7561 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
7566 slot
= path
->slots
[0];
7567 leaf
= path
->nodes
[0];
7568 if (slot
>= btrfs_header_nritems(leaf
)) {
7569 ret
= btrfs_next_leaf(root
, path
);
7576 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
7578 if (found_key
.objectid
>= key
->objectid
&&
7579 found_key
.type
== BTRFS_BLOCK_GROUP_ITEM_KEY
) {
7589 void btrfs_put_block_group_cache(struct btrfs_fs_info
*info
)
7591 struct btrfs_block_group_cache
*block_group
;
7595 struct inode
*inode
;
7597 block_group
= btrfs_lookup_first_block_group(info
, last
);
7598 while (block_group
) {
7599 spin_lock(&block_group
->lock
);
7600 if (block_group
->iref
)
7602 spin_unlock(&block_group
->lock
);
7603 block_group
= next_block_group(info
->tree_root
,
7613 inode
= block_group
->inode
;
7614 block_group
->iref
= 0;
7615 block_group
->inode
= NULL
;
7616 spin_unlock(&block_group
->lock
);
7618 last
= block_group
->key
.objectid
+ block_group
->key
.offset
;
7619 btrfs_put_block_group(block_group
);
7623 int btrfs_free_block_groups(struct btrfs_fs_info
*info
)
7625 struct btrfs_block_group_cache
*block_group
;
7626 struct btrfs_space_info
*space_info
;
7627 struct btrfs_caching_control
*caching_ctl
;
7630 down_write(&info
->extent_commit_sem
);
7631 while (!list_empty(&info
->caching_block_groups
)) {
7632 caching_ctl
= list_entry(info
->caching_block_groups
.next
,
7633 struct btrfs_caching_control
, list
);
7634 list_del(&caching_ctl
->list
);
7635 put_caching_control(caching_ctl
);
7637 up_write(&info
->extent_commit_sem
);
7639 spin_lock(&info
->block_group_cache_lock
);
7640 while ((n
= rb_last(&info
->block_group_cache_tree
)) != NULL
) {
7641 block_group
= rb_entry(n
, struct btrfs_block_group_cache
,
7643 rb_erase(&block_group
->cache_node
,
7644 &info
->block_group_cache_tree
);
7645 spin_unlock(&info
->block_group_cache_lock
);
7647 down_write(&block_group
->space_info
->groups_sem
);
7648 list_del(&block_group
->list
);
7649 up_write(&block_group
->space_info
->groups_sem
);
7651 if (block_group
->cached
== BTRFS_CACHE_STARTED
)
7652 wait_block_group_cache_done(block_group
);
7655 * We haven't cached this block group, which means we could
7656 * possibly have excluded extents on this block group.
7658 if (block_group
->cached
== BTRFS_CACHE_NO
)
7659 free_excluded_extents(info
->extent_root
, block_group
);
7661 btrfs_remove_free_space_cache(block_group
);
7662 btrfs_put_block_group(block_group
);
7664 spin_lock(&info
->block_group_cache_lock
);
7666 spin_unlock(&info
->block_group_cache_lock
);
7668 /* now that all the block groups are freed, go through and
7669 * free all the space_info structs. This is only called during
7670 * the final stages of unmount, and so we know nobody is
7671 * using them. We call synchronize_rcu() once before we start,
7672 * just to be on the safe side.
7676 release_global_block_rsv(info
);
7678 while(!list_empty(&info
->space_info
)) {
7679 space_info
= list_entry(info
->space_info
.next
,
7680 struct btrfs_space_info
,
7682 if (space_info
->bytes_pinned
> 0 ||
7683 space_info
->bytes_reserved
> 0 ||
7684 space_info
->bytes_may_use
> 0) {
7686 dump_space_info(space_info
, 0, 0);
7688 list_del(&space_info
->list
);
7694 static void __link_block_group(struct btrfs_space_info
*space_info
,
7695 struct btrfs_block_group_cache
*cache
)
7697 int index
= get_block_group_index(cache
);
7699 down_write(&space_info
->groups_sem
);
7700 list_add_tail(&cache
->list
, &space_info
->block_groups
[index
]);
7701 up_write(&space_info
->groups_sem
);
7704 int btrfs_read_block_groups(struct btrfs_root
*root
)
7706 struct btrfs_path
*path
;
7708 struct btrfs_block_group_cache
*cache
;
7709 struct btrfs_fs_info
*info
= root
->fs_info
;
7710 struct btrfs_space_info
*space_info
;
7711 struct btrfs_key key
;
7712 struct btrfs_key found_key
;
7713 struct extent_buffer
*leaf
;
7717 root
= info
->extent_root
;
7720 btrfs_set_key_type(&key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
7721 path
= btrfs_alloc_path();
7726 cache_gen
= btrfs_super_cache_generation(root
->fs_info
->super_copy
);
7727 if (btrfs_test_opt(root
, SPACE_CACHE
) &&
7728 btrfs_super_generation(root
->fs_info
->super_copy
) != cache_gen
)
7730 if (btrfs_test_opt(root
, CLEAR_CACHE
))
7734 ret
= find_first_block_group(root
, path
, &key
);
7739 leaf
= path
->nodes
[0];
7740 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
7741 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
7746 cache
->free_space_ctl
= kzalloc(sizeof(*cache
->free_space_ctl
),
7748 if (!cache
->free_space_ctl
) {
7754 atomic_set(&cache
->count
, 1);
7755 spin_lock_init(&cache
->lock
);
7756 cache
->fs_info
= info
;
7757 INIT_LIST_HEAD(&cache
->list
);
7758 INIT_LIST_HEAD(&cache
->cluster_list
);
7762 * When we mount with old space cache, we need to
7763 * set BTRFS_DC_CLEAR and set dirty flag.
7765 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
7766 * truncate the old free space cache inode and
7768 * b) Setting 'dirty flag' makes sure that we flush
7769 * the new space cache info onto disk.
7771 cache
->disk_cache_state
= BTRFS_DC_CLEAR
;
7772 if (btrfs_test_opt(root
, SPACE_CACHE
))
7776 read_extent_buffer(leaf
, &cache
->item
,
7777 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
7778 sizeof(cache
->item
));
7779 memcpy(&cache
->key
, &found_key
, sizeof(found_key
));
7781 key
.objectid
= found_key
.objectid
+ found_key
.offset
;
7782 btrfs_release_path(path
);
7783 cache
->flags
= btrfs_block_group_flags(&cache
->item
);
7784 cache
->sectorsize
= root
->sectorsize
;
7786 btrfs_init_free_space_ctl(cache
);
7789 * We need to exclude the super stripes now so that the space
7790 * info has super bytes accounted for, otherwise we'll think
7791 * we have more space than we actually do.
7793 exclude_super_stripes(root
, cache
);
7796 * check for two cases, either we are full, and therefore
7797 * don't need to bother with the caching work since we won't
7798 * find any space, or we are empty, and we can just add all
7799 * the space in and be done with it. This saves us _alot_ of
7800 * time, particularly in the full case.
7802 if (found_key
.offset
== btrfs_block_group_used(&cache
->item
)) {
7803 cache
->last_byte_to_unpin
= (u64
)-1;
7804 cache
->cached
= BTRFS_CACHE_FINISHED
;
7805 free_excluded_extents(root
, cache
);
7806 } else if (btrfs_block_group_used(&cache
->item
) == 0) {
7807 cache
->last_byte_to_unpin
= (u64
)-1;
7808 cache
->cached
= BTRFS_CACHE_FINISHED
;
7809 add_new_free_space(cache
, root
->fs_info
,
7811 found_key
.objectid
+
7813 free_excluded_extents(root
, cache
);
7816 ret
= update_space_info(info
, cache
->flags
, found_key
.offset
,
7817 btrfs_block_group_used(&cache
->item
),
7819 BUG_ON(ret
); /* -ENOMEM */
7820 cache
->space_info
= space_info
;
7821 spin_lock(&cache
->space_info
->lock
);
7822 cache
->space_info
->bytes_readonly
+= cache
->bytes_super
;
7823 spin_unlock(&cache
->space_info
->lock
);
7825 __link_block_group(space_info
, cache
);
7827 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
7828 BUG_ON(ret
); /* Logic error */
7830 set_avail_alloc_bits(root
->fs_info
, cache
->flags
);
7831 if (btrfs_chunk_readonly(root
, cache
->key
.objectid
))
7832 set_block_group_ro(cache
, 1);
7835 list_for_each_entry_rcu(space_info
, &root
->fs_info
->space_info
, list
) {
7836 if (!(get_alloc_profile(root
, space_info
->flags
) &
7837 (BTRFS_BLOCK_GROUP_RAID10
|
7838 BTRFS_BLOCK_GROUP_RAID1
|
7839 BTRFS_BLOCK_GROUP_DUP
)))
7842 * avoid allocating from un-mirrored block group if there are
7843 * mirrored block groups.
7845 list_for_each_entry(cache
, &space_info
->block_groups
[3], list
)
7846 set_block_group_ro(cache
, 1);
7847 list_for_each_entry(cache
, &space_info
->block_groups
[4], list
)
7848 set_block_group_ro(cache
, 1);
7851 init_global_block_rsv(info
);
7854 btrfs_free_path(path
);
7858 void btrfs_create_pending_block_groups(struct btrfs_trans_handle
*trans
,
7859 struct btrfs_root
*root
)
7861 struct btrfs_block_group_cache
*block_group
, *tmp
;
7862 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
7863 struct btrfs_block_group_item item
;
7864 struct btrfs_key key
;
7867 list_for_each_entry_safe(block_group
, tmp
, &trans
->new_bgs
,
7869 list_del_init(&block_group
->new_bg_list
);
7874 spin_lock(&block_group
->lock
);
7875 memcpy(&item
, &block_group
->item
, sizeof(item
));
7876 memcpy(&key
, &block_group
->key
, sizeof(key
));
7877 spin_unlock(&block_group
->lock
);
7879 ret
= btrfs_insert_item(trans
, extent_root
, &key
, &item
,
7882 btrfs_abort_transaction(trans
, extent_root
, ret
);
7886 int btrfs_make_block_group(struct btrfs_trans_handle
*trans
,
7887 struct btrfs_root
*root
, u64 bytes_used
,
7888 u64 type
, u64 chunk_objectid
, u64 chunk_offset
,
7892 struct btrfs_root
*extent_root
;
7893 struct btrfs_block_group_cache
*cache
;
7895 extent_root
= root
->fs_info
->extent_root
;
7897 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
7899 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
7902 cache
->free_space_ctl
= kzalloc(sizeof(*cache
->free_space_ctl
),
7904 if (!cache
->free_space_ctl
) {
7909 cache
->key
.objectid
= chunk_offset
;
7910 cache
->key
.offset
= size
;
7911 cache
->key
.type
= BTRFS_BLOCK_GROUP_ITEM_KEY
;
7912 cache
->sectorsize
= root
->sectorsize
;
7913 cache
->fs_info
= root
->fs_info
;
7915 atomic_set(&cache
->count
, 1);
7916 spin_lock_init(&cache
->lock
);
7917 INIT_LIST_HEAD(&cache
->list
);
7918 INIT_LIST_HEAD(&cache
->cluster_list
);
7919 INIT_LIST_HEAD(&cache
->new_bg_list
);
7921 btrfs_init_free_space_ctl(cache
);
7923 btrfs_set_block_group_used(&cache
->item
, bytes_used
);
7924 btrfs_set_block_group_chunk_objectid(&cache
->item
, chunk_objectid
);
7925 cache
->flags
= type
;
7926 btrfs_set_block_group_flags(&cache
->item
, type
);
7928 cache
->last_byte_to_unpin
= (u64
)-1;
7929 cache
->cached
= BTRFS_CACHE_FINISHED
;
7930 exclude_super_stripes(root
, cache
);
7932 add_new_free_space(cache
, root
->fs_info
, chunk_offset
,
7933 chunk_offset
+ size
);
7935 free_excluded_extents(root
, cache
);
7937 ret
= update_space_info(root
->fs_info
, cache
->flags
, size
, bytes_used
,
7938 &cache
->space_info
);
7939 BUG_ON(ret
); /* -ENOMEM */
7940 update_global_block_rsv(root
->fs_info
);
7942 spin_lock(&cache
->space_info
->lock
);
7943 cache
->space_info
->bytes_readonly
+= cache
->bytes_super
;
7944 spin_unlock(&cache
->space_info
->lock
);
7946 __link_block_group(cache
->space_info
, cache
);
7948 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
7949 BUG_ON(ret
); /* Logic error */
7951 list_add_tail(&cache
->new_bg_list
, &trans
->new_bgs
);
7953 set_avail_alloc_bits(extent_root
->fs_info
, type
);
7958 static void clear_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
7960 u64 extra_flags
= chunk_to_extended(flags
) &
7961 BTRFS_EXTENDED_PROFILE_MASK
;
7963 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
7964 fs_info
->avail_data_alloc_bits
&= ~extra_flags
;
7965 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
7966 fs_info
->avail_metadata_alloc_bits
&= ~extra_flags
;
7967 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
7968 fs_info
->avail_system_alloc_bits
&= ~extra_flags
;
7971 int btrfs_remove_block_group(struct btrfs_trans_handle
*trans
,
7972 struct btrfs_root
*root
, u64 group_start
)
7974 struct btrfs_path
*path
;
7975 struct btrfs_block_group_cache
*block_group
;
7976 struct btrfs_free_cluster
*cluster
;
7977 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
7978 struct btrfs_key key
;
7979 struct inode
*inode
;
7984 root
= root
->fs_info
->extent_root
;
7986 block_group
= btrfs_lookup_block_group(root
->fs_info
, group_start
);
7987 BUG_ON(!block_group
);
7988 BUG_ON(!block_group
->ro
);
7991 * Free the reserved super bytes from this block group before
7994 free_excluded_extents(root
, block_group
);
7996 memcpy(&key
, &block_group
->key
, sizeof(key
));
7997 index
= get_block_group_index(block_group
);
7998 if (block_group
->flags
& (BTRFS_BLOCK_GROUP_DUP
|
7999 BTRFS_BLOCK_GROUP_RAID1
|
8000 BTRFS_BLOCK_GROUP_RAID10
))
8005 /* make sure this block group isn't part of an allocation cluster */
8006 cluster
= &root
->fs_info
->data_alloc_cluster
;
8007 spin_lock(&cluster
->refill_lock
);
8008 btrfs_return_cluster_to_free_space(block_group
, cluster
);
8009 spin_unlock(&cluster
->refill_lock
);
8012 * make sure this block group isn't part of a metadata
8013 * allocation cluster
8015 cluster
= &root
->fs_info
->meta_alloc_cluster
;
8016 spin_lock(&cluster
->refill_lock
);
8017 btrfs_return_cluster_to_free_space(block_group
, cluster
);
8018 spin_unlock(&cluster
->refill_lock
);
8020 path
= btrfs_alloc_path();
8026 inode
= lookup_free_space_inode(tree_root
, block_group
, path
);
8027 if (!IS_ERR(inode
)) {
8028 ret
= btrfs_orphan_add(trans
, inode
);
8030 btrfs_add_delayed_iput(inode
);
8034 /* One for the block groups ref */
8035 spin_lock(&block_group
->lock
);
8036 if (block_group
->iref
) {
8037 block_group
->iref
= 0;
8038 block_group
->inode
= NULL
;
8039 spin_unlock(&block_group
->lock
);
8042 spin_unlock(&block_group
->lock
);
8044 /* One for our lookup ref */
8045 btrfs_add_delayed_iput(inode
);
8048 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
8049 key
.offset
= block_group
->key
.objectid
;
8052 ret
= btrfs_search_slot(trans
, tree_root
, &key
, path
, -1, 1);
8056 btrfs_release_path(path
);
8058 ret
= btrfs_del_item(trans
, tree_root
, path
);
8061 btrfs_release_path(path
);
8064 spin_lock(&root
->fs_info
->block_group_cache_lock
);
8065 rb_erase(&block_group
->cache_node
,
8066 &root
->fs_info
->block_group_cache_tree
);
8067 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
8069 down_write(&block_group
->space_info
->groups_sem
);
8071 * we must use list_del_init so people can check to see if they
8072 * are still on the list after taking the semaphore
8074 list_del_init(&block_group
->list
);
8075 if (list_empty(&block_group
->space_info
->block_groups
[index
]))
8076 clear_avail_alloc_bits(root
->fs_info
, block_group
->flags
);
8077 up_write(&block_group
->space_info
->groups_sem
);
8079 if (block_group
->cached
== BTRFS_CACHE_STARTED
)
8080 wait_block_group_cache_done(block_group
);
8082 btrfs_remove_free_space_cache(block_group
);
8084 spin_lock(&block_group
->space_info
->lock
);
8085 block_group
->space_info
->total_bytes
-= block_group
->key
.offset
;
8086 block_group
->space_info
->bytes_readonly
-= block_group
->key
.offset
;
8087 block_group
->space_info
->disk_total
-= block_group
->key
.offset
* factor
;
8088 spin_unlock(&block_group
->space_info
->lock
);
8090 memcpy(&key
, &block_group
->key
, sizeof(key
));
8092 btrfs_clear_space_info_full(root
->fs_info
);
8094 btrfs_put_block_group(block_group
);
8095 btrfs_put_block_group(block_group
);
8097 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
8103 ret
= btrfs_del_item(trans
, root
, path
);
8105 btrfs_free_path(path
);
8109 int btrfs_init_space_info(struct btrfs_fs_info
*fs_info
)
8111 struct btrfs_space_info
*space_info
;
8112 struct btrfs_super_block
*disk_super
;
8118 disk_super
= fs_info
->super_copy
;
8119 if (!btrfs_super_root(disk_super
))
8122 features
= btrfs_super_incompat_flags(disk_super
);
8123 if (features
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
8126 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
8127 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
8132 flags
= BTRFS_BLOCK_GROUP_METADATA
| BTRFS_BLOCK_GROUP_DATA
;
8133 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
8135 flags
= BTRFS_BLOCK_GROUP_METADATA
;
8136 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
8140 flags
= BTRFS_BLOCK_GROUP_DATA
;
8141 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
8147 int btrfs_error_unpin_extent_range(struct btrfs_root
*root
, u64 start
, u64 end
)
8149 return unpin_extent_range(root
, start
, end
);
8152 int btrfs_error_discard_extent(struct btrfs_root
*root
, u64 bytenr
,
8153 u64 num_bytes
, u64
*actual_bytes
)
8155 return btrfs_discard_extent(root
, bytenr
, num_bytes
, actual_bytes
);
8158 int btrfs_trim_fs(struct btrfs_root
*root
, struct fstrim_range
*range
)
8160 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
8161 struct btrfs_block_group_cache
*cache
= NULL
;
8166 u64 total_bytes
= btrfs_super_total_bytes(fs_info
->super_copy
);
8170 * try to trim all FS space, our block group may start from non-zero.
8172 if (range
->len
== total_bytes
)
8173 cache
= btrfs_lookup_first_block_group(fs_info
, range
->start
);
8175 cache
= btrfs_lookup_block_group(fs_info
, range
->start
);
8178 if (cache
->key
.objectid
>= (range
->start
+ range
->len
)) {
8179 btrfs_put_block_group(cache
);
8183 start
= max(range
->start
, cache
->key
.objectid
);
8184 end
= min(range
->start
+ range
->len
,
8185 cache
->key
.objectid
+ cache
->key
.offset
);
8187 if (end
- start
>= range
->minlen
) {
8188 if (!block_group_cache_done(cache
)) {
8189 ret
= cache_block_group(cache
, NULL
, root
, 0);
8191 wait_block_group_cache_done(cache
);
8193 ret
= btrfs_trim_block_group(cache
,
8199 trimmed
+= group_trimmed
;
8201 btrfs_put_block_group(cache
);
8206 cache
= next_block_group(fs_info
->tree_root
, cache
);
8209 range
->len
= trimmed
;