2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
18 #include <linux/sched.h>
19 #include <linux/pagemap.h>
20 #include <linux/writeback.h>
21 #include <linux/blkdev.h>
22 #include <linux/sort.h>
23 #include <linux/rcupdate.h>
24 #include <linux/kthread.h>
25 #include <linux/slab.h>
26 #include <linux/ratelimit.h>
31 #include "print-tree.h"
32 #include "transaction.h"
36 #include "free-space-cache.h"
39 #undef SCRAMBLE_DELAYED_REFS
42 * control flags for do_chunk_alloc's force field
43 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
44 * if we really need one.
46 * CHUNK_ALLOC_LIMITED means to only try and allocate one
47 * if we have very few chunks already allocated. This is
48 * used as part of the clustering code to help make sure
49 * we have a good pool of storage to cluster in, without
50 * filling the FS with empty chunks
52 * CHUNK_ALLOC_FORCE means it must try to allocate one
56 CHUNK_ALLOC_NO_FORCE
= 0,
57 CHUNK_ALLOC_LIMITED
= 1,
58 CHUNK_ALLOC_FORCE
= 2,
62 * Control how reservations are dealt with.
64 * RESERVE_FREE - freeing a reservation.
65 * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for
67 * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update
68 * bytes_may_use as the ENOSPC accounting is done elsewhere
73 RESERVE_ALLOC_NO_ACCOUNT
= 2,
76 static int update_block_group(struct btrfs_root
*root
,
77 u64 bytenr
, u64 num_bytes
, int alloc
);
78 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
79 struct btrfs_root
*root
,
80 u64 bytenr
, u64 num_bytes
, u64 parent
,
81 u64 root_objectid
, u64 owner_objectid
,
82 u64 owner_offset
, int refs_to_drop
,
83 struct btrfs_delayed_extent_op
*extra_op
);
84 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op
*extent_op
,
85 struct extent_buffer
*leaf
,
86 struct btrfs_extent_item
*ei
);
87 static int alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
88 struct btrfs_root
*root
,
89 u64 parent
, u64 root_objectid
,
90 u64 flags
, u64 owner
, u64 offset
,
91 struct btrfs_key
*ins
, int ref_mod
);
92 static int alloc_reserved_tree_block(struct btrfs_trans_handle
*trans
,
93 struct btrfs_root
*root
,
94 u64 parent
, u64 root_objectid
,
95 u64 flags
, struct btrfs_disk_key
*key
,
96 int level
, struct btrfs_key
*ins
);
97 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
98 struct btrfs_root
*extent_root
, u64 flags
,
100 static int find_next_key(struct btrfs_path
*path
, int level
,
101 struct btrfs_key
*key
);
102 static void dump_space_info(struct btrfs_space_info
*info
, u64 bytes
,
103 int dump_block_groups
);
104 static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache
*cache
,
105 u64 num_bytes
, int reserve
);
106 static int block_rsv_use_bytes(struct btrfs_block_rsv
*block_rsv
,
110 block_group_cache_done(struct btrfs_block_group_cache
*cache
)
113 return cache
->cached
== BTRFS_CACHE_FINISHED
;
116 static int block_group_bits(struct btrfs_block_group_cache
*cache
, u64 bits
)
118 return (cache
->flags
& bits
) == bits
;
121 static void btrfs_get_block_group(struct btrfs_block_group_cache
*cache
)
123 atomic_inc(&cache
->count
);
126 void btrfs_put_block_group(struct btrfs_block_group_cache
*cache
)
128 if (atomic_dec_and_test(&cache
->count
)) {
129 WARN_ON(cache
->pinned
> 0);
130 WARN_ON(cache
->reserved
> 0);
131 kfree(cache
->free_space_ctl
);
137 * this adds the block group to the fs_info rb tree for the block group
140 static int btrfs_add_block_group_cache(struct btrfs_fs_info
*info
,
141 struct btrfs_block_group_cache
*block_group
)
144 struct rb_node
*parent
= NULL
;
145 struct btrfs_block_group_cache
*cache
;
147 spin_lock(&info
->block_group_cache_lock
);
148 p
= &info
->block_group_cache_tree
.rb_node
;
152 cache
= rb_entry(parent
, struct btrfs_block_group_cache
,
154 if (block_group
->key
.objectid
< cache
->key
.objectid
) {
156 } else if (block_group
->key
.objectid
> cache
->key
.objectid
) {
159 spin_unlock(&info
->block_group_cache_lock
);
164 rb_link_node(&block_group
->cache_node
, parent
, p
);
165 rb_insert_color(&block_group
->cache_node
,
166 &info
->block_group_cache_tree
);
168 if (info
->first_logical_byte
> block_group
->key
.objectid
)
169 info
->first_logical_byte
= block_group
->key
.objectid
;
171 spin_unlock(&info
->block_group_cache_lock
);
177 * This will return the block group at or after bytenr if contains is 0, else
178 * it will return the block group that contains the bytenr
180 static struct btrfs_block_group_cache
*
181 block_group_cache_tree_search(struct btrfs_fs_info
*info
, u64 bytenr
,
184 struct btrfs_block_group_cache
*cache
, *ret
= NULL
;
188 spin_lock(&info
->block_group_cache_lock
);
189 n
= info
->block_group_cache_tree
.rb_node
;
192 cache
= rb_entry(n
, struct btrfs_block_group_cache
,
194 end
= cache
->key
.objectid
+ cache
->key
.offset
- 1;
195 start
= cache
->key
.objectid
;
197 if (bytenr
< start
) {
198 if (!contains
&& (!ret
|| start
< ret
->key
.objectid
))
201 } else if (bytenr
> start
) {
202 if (contains
&& bytenr
<= end
) {
213 btrfs_get_block_group(ret
);
214 if (bytenr
== 0 && info
->first_logical_byte
> ret
->key
.objectid
)
215 info
->first_logical_byte
= ret
->key
.objectid
;
217 spin_unlock(&info
->block_group_cache_lock
);
222 static int add_excluded_extent(struct btrfs_root
*root
,
223 u64 start
, u64 num_bytes
)
225 u64 end
= start
+ num_bytes
- 1;
226 set_extent_bits(&root
->fs_info
->freed_extents
[0],
227 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
228 set_extent_bits(&root
->fs_info
->freed_extents
[1],
229 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
233 static void free_excluded_extents(struct btrfs_root
*root
,
234 struct btrfs_block_group_cache
*cache
)
238 start
= cache
->key
.objectid
;
239 end
= start
+ cache
->key
.offset
- 1;
241 clear_extent_bits(&root
->fs_info
->freed_extents
[0],
242 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
243 clear_extent_bits(&root
->fs_info
->freed_extents
[1],
244 start
, end
, EXTENT_UPTODATE
, GFP_NOFS
);
247 static int exclude_super_stripes(struct btrfs_root
*root
,
248 struct btrfs_block_group_cache
*cache
)
255 if (cache
->key
.objectid
< BTRFS_SUPER_INFO_OFFSET
) {
256 stripe_len
= BTRFS_SUPER_INFO_OFFSET
- cache
->key
.objectid
;
257 cache
->bytes_super
+= stripe_len
;
258 ret
= add_excluded_extent(root
, cache
->key
.objectid
,
260 BUG_ON(ret
); /* -ENOMEM */
263 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
264 bytenr
= btrfs_sb_offset(i
);
265 ret
= btrfs_rmap_block(&root
->fs_info
->mapping_tree
,
266 cache
->key
.objectid
, bytenr
,
267 0, &logical
, &nr
, &stripe_len
);
268 BUG_ON(ret
); /* -ENOMEM */
271 cache
->bytes_super
+= stripe_len
;
272 ret
= add_excluded_extent(root
, logical
[nr
],
274 BUG_ON(ret
); /* -ENOMEM */
282 static struct btrfs_caching_control
*
283 get_caching_control(struct btrfs_block_group_cache
*cache
)
285 struct btrfs_caching_control
*ctl
;
287 spin_lock(&cache
->lock
);
288 if (cache
->cached
!= BTRFS_CACHE_STARTED
) {
289 spin_unlock(&cache
->lock
);
293 /* We're loading it the fast way, so we don't have a caching_ctl. */
294 if (!cache
->caching_ctl
) {
295 spin_unlock(&cache
->lock
);
299 ctl
= cache
->caching_ctl
;
300 atomic_inc(&ctl
->count
);
301 spin_unlock(&cache
->lock
);
305 static void put_caching_control(struct btrfs_caching_control
*ctl
)
307 if (atomic_dec_and_test(&ctl
->count
))
312 * this is only called by cache_block_group, since we could have freed extents
313 * we need to check the pinned_extents for any extents that can't be used yet
314 * since their free space will be released as soon as the transaction commits.
316 static u64
add_new_free_space(struct btrfs_block_group_cache
*block_group
,
317 struct btrfs_fs_info
*info
, u64 start
, u64 end
)
319 u64 extent_start
, extent_end
, size
, total_added
= 0;
322 while (start
< end
) {
323 ret
= find_first_extent_bit(info
->pinned_extents
, start
,
324 &extent_start
, &extent_end
,
325 EXTENT_DIRTY
| EXTENT_UPTODATE
,
330 if (extent_start
<= start
) {
331 start
= extent_end
+ 1;
332 } else if (extent_start
> start
&& extent_start
< end
) {
333 size
= extent_start
- start
;
335 ret
= btrfs_add_free_space(block_group
, start
,
337 BUG_ON(ret
); /* -ENOMEM or logic error */
338 start
= extent_end
+ 1;
347 ret
= btrfs_add_free_space(block_group
, start
, size
);
348 BUG_ON(ret
); /* -ENOMEM or logic error */
354 static noinline
void caching_thread(struct btrfs_work
*work
)
356 struct btrfs_block_group_cache
*block_group
;
357 struct btrfs_fs_info
*fs_info
;
358 struct btrfs_caching_control
*caching_ctl
;
359 struct btrfs_root
*extent_root
;
360 struct btrfs_path
*path
;
361 struct extent_buffer
*leaf
;
362 struct btrfs_key key
;
368 caching_ctl
= container_of(work
, struct btrfs_caching_control
, work
);
369 block_group
= caching_ctl
->block_group
;
370 fs_info
= block_group
->fs_info
;
371 extent_root
= fs_info
->extent_root
;
373 path
= btrfs_alloc_path();
377 last
= max_t(u64
, block_group
->key
.objectid
, BTRFS_SUPER_INFO_OFFSET
);
380 * We don't want to deadlock with somebody trying to allocate a new
381 * extent for the extent root while also trying to search the extent
382 * root to add free space. So we skip locking and search the commit
383 * root, since its read-only
385 path
->skip_locking
= 1;
386 path
->search_commit_root
= 1;
391 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
393 mutex_lock(&caching_ctl
->mutex
);
394 /* need to make sure the commit_root doesn't disappear */
395 down_read(&fs_info
->extent_commit_sem
);
397 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
401 leaf
= path
->nodes
[0];
402 nritems
= btrfs_header_nritems(leaf
);
405 if (btrfs_fs_closing(fs_info
) > 1) {
410 if (path
->slots
[0] < nritems
) {
411 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
413 ret
= find_next_key(path
, 0, &key
);
417 if (need_resched() ||
418 btrfs_next_leaf(extent_root
, path
)) {
419 caching_ctl
->progress
= last
;
420 btrfs_release_path(path
);
421 up_read(&fs_info
->extent_commit_sem
);
422 mutex_unlock(&caching_ctl
->mutex
);
426 leaf
= path
->nodes
[0];
427 nritems
= btrfs_header_nritems(leaf
);
431 if (key
.objectid
< block_group
->key
.objectid
) {
436 if (key
.objectid
>= block_group
->key
.objectid
+
437 block_group
->key
.offset
)
440 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
441 total_found
+= add_new_free_space(block_group
,
444 last
= key
.objectid
+ key
.offset
;
446 if (total_found
> (1024 * 1024 * 2)) {
448 wake_up(&caching_ctl
->wait
);
455 total_found
+= add_new_free_space(block_group
, fs_info
, last
,
456 block_group
->key
.objectid
+
457 block_group
->key
.offset
);
458 caching_ctl
->progress
= (u64
)-1;
460 spin_lock(&block_group
->lock
);
461 block_group
->caching_ctl
= NULL
;
462 block_group
->cached
= BTRFS_CACHE_FINISHED
;
463 spin_unlock(&block_group
->lock
);
466 btrfs_free_path(path
);
467 up_read(&fs_info
->extent_commit_sem
);
469 free_excluded_extents(extent_root
, block_group
);
471 mutex_unlock(&caching_ctl
->mutex
);
473 wake_up(&caching_ctl
->wait
);
475 put_caching_control(caching_ctl
);
476 btrfs_put_block_group(block_group
);
479 static int cache_block_group(struct btrfs_block_group_cache
*cache
,
483 struct btrfs_fs_info
*fs_info
= cache
->fs_info
;
484 struct btrfs_caching_control
*caching_ctl
;
487 caching_ctl
= kzalloc(sizeof(*caching_ctl
), GFP_NOFS
);
491 INIT_LIST_HEAD(&caching_ctl
->list
);
492 mutex_init(&caching_ctl
->mutex
);
493 init_waitqueue_head(&caching_ctl
->wait
);
494 caching_ctl
->block_group
= cache
;
495 caching_ctl
->progress
= cache
->key
.objectid
;
496 atomic_set(&caching_ctl
->count
, 1);
497 caching_ctl
->work
.func
= caching_thread
;
499 spin_lock(&cache
->lock
);
501 * This should be a rare occasion, but this could happen I think in the
502 * case where one thread starts to load the space cache info, and then
503 * some other thread starts a transaction commit which tries to do an
504 * allocation while the other thread is still loading the space cache
505 * info. The previous loop should have kept us from choosing this block
506 * group, but if we've moved to the state where we will wait on caching
507 * block groups we need to first check if we're doing a fast load here,
508 * so we can wait for it to finish, otherwise we could end up allocating
509 * from a block group who's cache gets evicted for one reason or
512 while (cache
->cached
== BTRFS_CACHE_FAST
) {
513 struct btrfs_caching_control
*ctl
;
515 ctl
= cache
->caching_ctl
;
516 atomic_inc(&ctl
->count
);
517 prepare_to_wait(&ctl
->wait
, &wait
, TASK_UNINTERRUPTIBLE
);
518 spin_unlock(&cache
->lock
);
522 finish_wait(&ctl
->wait
, &wait
);
523 put_caching_control(ctl
);
524 spin_lock(&cache
->lock
);
527 if (cache
->cached
!= BTRFS_CACHE_NO
) {
528 spin_unlock(&cache
->lock
);
532 WARN_ON(cache
->caching_ctl
);
533 cache
->caching_ctl
= caching_ctl
;
534 cache
->cached
= BTRFS_CACHE_FAST
;
535 spin_unlock(&cache
->lock
);
537 if (fs_info
->mount_opt
& BTRFS_MOUNT_SPACE_CACHE
) {
538 ret
= load_free_space_cache(fs_info
, cache
);
540 spin_lock(&cache
->lock
);
542 cache
->caching_ctl
= NULL
;
543 cache
->cached
= BTRFS_CACHE_FINISHED
;
544 cache
->last_byte_to_unpin
= (u64
)-1;
546 if (load_cache_only
) {
547 cache
->caching_ctl
= NULL
;
548 cache
->cached
= BTRFS_CACHE_NO
;
550 cache
->cached
= BTRFS_CACHE_STARTED
;
553 spin_unlock(&cache
->lock
);
554 wake_up(&caching_ctl
->wait
);
556 put_caching_control(caching_ctl
);
557 free_excluded_extents(fs_info
->extent_root
, cache
);
562 * We are not going to do the fast caching, set cached to the
563 * appropriate value and wakeup any waiters.
565 spin_lock(&cache
->lock
);
566 if (load_cache_only
) {
567 cache
->caching_ctl
= NULL
;
568 cache
->cached
= BTRFS_CACHE_NO
;
570 cache
->cached
= BTRFS_CACHE_STARTED
;
572 spin_unlock(&cache
->lock
);
573 wake_up(&caching_ctl
->wait
);
576 if (load_cache_only
) {
577 put_caching_control(caching_ctl
);
581 down_write(&fs_info
->extent_commit_sem
);
582 atomic_inc(&caching_ctl
->count
);
583 list_add_tail(&caching_ctl
->list
, &fs_info
->caching_block_groups
);
584 up_write(&fs_info
->extent_commit_sem
);
586 btrfs_get_block_group(cache
);
588 btrfs_queue_worker(&fs_info
->caching_workers
, &caching_ctl
->work
);
594 * return the block group that starts at or after bytenr
596 static struct btrfs_block_group_cache
*
597 btrfs_lookup_first_block_group(struct btrfs_fs_info
*info
, u64 bytenr
)
599 struct btrfs_block_group_cache
*cache
;
601 cache
= block_group_cache_tree_search(info
, bytenr
, 0);
607 * return the block group that contains the given bytenr
609 struct btrfs_block_group_cache
*btrfs_lookup_block_group(
610 struct btrfs_fs_info
*info
,
613 struct btrfs_block_group_cache
*cache
;
615 cache
= block_group_cache_tree_search(info
, bytenr
, 1);
620 static struct btrfs_space_info
*__find_space_info(struct btrfs_fs_info
*info
,
623 struct list_head
*head
= &info
->space_info
;
624 struct btrfs_space_info
*found
;
626 flags
&= BTRFS_BLOCK_GROUP_TYPE_MASK
;
629 list_for_each_entry_rcu(found
, head
, list
) {
630 if (found
->flags
& flags
) {
640 * after adding space to the filesystem, we need to clear the full flags
641 * on all the space infos.
643 void btrfs_clear_space_info_full(struct btrfs_fs_info
*info
)
645 struct list_head
*head
= &info
->space_info
;
646 struct btrfs_space_info
*found
;
649 list_for_each_entry_rcu(found
, head
, list
)
654 u64
btrfs_find_block_group(struct btrfs_root
*root
,
655 u64 search_start
, u64 search_hint
, int owner
)
657 struct btrfs_block_group_cache
*cache
;
659 u64 last
= max(search_hint
, search_start
);
666 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
670 spin_lock(&cache
->lock
);
671 last
= cache
->key
.objectid
+ cache
->key
.offset
;
672 used
= btrfs_block_group_used(&cache
->item
);
674 if ((full_search
|| !cache
->ro
) &&
675 block_group_bits(cache
, BTRFS_BLOCK_GROUP_METADATA
)) {
676 if (used
+ cache
->pinned
+ cache
->reserved
<
677 div_factor(cache
->key
.offset
, factor
)) {
678 group_start
= cache
->key
.objectid
;
679 spin_unlock(&cache
->lock
);
680 btrfs_put_block_group(cache
);
684 spin_unlock(&cache
->lock
);
685 btrfs_put_block_group(cache
);
693 if (!full_search
&& factor
< 10) {
703 /* simple helper to search for an existing extent at a given offset */
704 int btrfs_lookup_extent(struct btrfs_root
*root
, u64 start
, u64 len
)
707 struct btrfs_key key
;
708 struct btrfs_path
*path
;
710 path
= btrfs_alloc_path();
714 key
.objectid
= start
;
716 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
717 ret
= btrfs_search_slot(NULL
, root
->fs_info
->extent_root
, &key
, path
,
719 btrfs_free_path(path
);
724 * helper function to lookup reference count and flags of extent.
726 * the head node for delayed ref is used to store the sum of all the
727 * reference count modifications queued up in the rbtree. the head
728 * node may also store the extent flags to set. This way you can check
729 * to see what the reference count and extent flags would be if all of
730 * the delayed refs are not processed.
732 int btrfs_lookup_extent_info(struct btrfs_trans_handle
*trans
,
733 struct btrfs_root
*root
, u64 bytenr
,
734 u64 num_bytes
, u64
*refs
, u64
*flags
)
736 struct btrfs_delayed_ref_head
*head
;
737 struct btrfs_delayed_ref_root
*delayed_refs
;
738 struct btrfs_path
*path
;
739 struct btrfs_extent_item
*ei
;
740 struct extent_buffer
*leaf
;
741 struct btrfs_key key
;
747 path
= btrfs_alloc_path();
751 key
.objectid
= bytenr
;
752 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
753 key
.offset
= num_bytes
;
755 path
->skip_locking
= 1;
756 path
->search_commit_root
= 1;
759 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
,
765 leaf
= path
->nodes
[0];
766 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
767 if (item_size
>= sizeof(*ei
)) {
768 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
769 struct btrfs_extent_item
);
770 num_refs
= btrfs_extent_refs(leaf
, ei
);
771 extent_flags
= btrfs_extent_flags(leaf
, ei
);
773 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
774 struct btrfs_extent_item_v0
*ei0
;
775 BUG_ON(item_size
!= sizeof(*ei0
));
776 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
777 struct btrfs_extent_item_v0
);
778 num_refs
= btrfs_extent_refs_v0(leaf
, ei0
);
779 /* FIXME: this isn't correct for data */
780 extent_flags
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
785 BUG_ON(num_refs
== 0);
795 delayed_refs
= &trans
->transaction
->delayed_refs
;
796 spin_lock(&delayed_refs
->lock
);
797 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
799 if (!mutex_trylock(&head
->mutex
)) {
800 atomic_inc(&head
->node
.refs
);
801 spin_unlock(&delayed_refs
->lock
);
803 btrfs_release_path(path
);
806 * Mutex was contended, block until it's released and try
809 mutex_lock(&head
->mutex
);
810 mutex_unlock(&head
->mutex
);
811 btrfs_put_delayed_ref(&head
->node
);
814 if (head
->extent_op
&& head
->extent_op
->update_flags
)
815 extent_flags
|= head
->extent_op
->flags_to_set
;
817 BUG_ON(num_refs
== 0);
819 num_refs
+= head
->node
.ref_mod
;
820 mutex_unlock(&head
->mutex
);
822 spin_unlock(&delayed_refs
->lock
);
824 WARN_ON(num_refs
== 0);
828 *flags
= extent_flags
;
830 btrfs_free_path(path
);
835 * Back reference rules. Back refs have three main goals:
837 * 1) differentiate between all holders of references to an extent so that
838 * when a reference is dropped we can make sure it was a valid reference
839 * before freeing the extent.
841 * 2) Provide enough information to quickly find the holders of an extent
842 * if we notice a given block is corrupted or bad.
844 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
845 * maintenance. This is actually the same as #2, but with a slightly
846 * different use case.
848 * There are two kinds of back refs. The implicit back refs is optimized
849 * for pointers in non-shared tree blocks. For a given pointer in a block,
850 * back refs of this kind provide information about the block's owner tree
851 * and the pointer's key. These information allow us to find the block by
852 * b-tree searching. The full back refs is for pointers in tree blocks not
853 * referenced by their owner trees. The location of tree block is recorded
854 * in the back refs. Actually the full back refs is generic, and can be
855 * used in all cases the implicit back refs is used. The major shortcoming
856 * of the full back refs is its overhead. Every time a tree block gets
857 * COWed, we have to update back refs entry for all pointers in it.
859 * For a newly allocated tree block, we use implicit back refs for
860 * pointers in it. This means most tree related operations only involve
861 * implicit back refs. For a tree block created in old transaction, the
862 * only way to drop a reference to it is COW it. So we can detect the
863 * event that tree block loses its owner tree's reference and do the
864 * back refs conversion.
866 * When a tree block is COW'd through a tree, there are four cases:
868 * The reference count of the block is one and the tree is the block's
869 * owner tree. Nothing to do in this case.
871 * The reference count of the block is one and the tree is not the
872 * block's owner tree. In this case, full back refs is used for pointers
873 * in the block. Remove these full back refs, add implicit back refs for
874 * every pointers in the new block.
876 * The reference count of the block is greater than one and the tree is
877 * the block's owner tree. In this case, implicit back refs is used for
878 * pointers in the block. Add full back refs for every pointers in the
879 * block, increase lower level extents' reference counts. The original
880 * implicit back refs are entailed to the new block.
882 * The reference count of the block is greater than one and the tree is
883 * not the block's owner tree. Add implicit back refs for every pointer in
884 * the new block, increase lower level extents' reference count.
886 * Back Reference Key composing:
888 * The key objectid corresponds to the first byte in the extent,
889 * The key type is used to differentiate between types of back refs.
890 * There are different meanings of the key offset for different types
893 * File extents can be referenced by:
895 * - multiple snapshots, subvolumes, or different generations in one subvol
896 * - different files inside a single subvolume
897 * - different offsets inside a file (bookend extents in file.c)
899 * The extent ref structure for the implicit back refs has fields for:
901 * - Objectid of the subvolume root
902 * - objectid of the file holding the reference
903 * - original offset in the file
904 * - how many bookend extents
906 * The key offset for the implicit back refs is hash of the first
909 * The extent ref structure for the full back refs has field for:
911 * - number of pointers in the tree leaf
913 * The key offset for the implicit back refs is the first byte of
916 * When a file extent is allocated, The implicit back refs is used.
917 * the fields are filled in:
919 * (root_key.objectid, inode objectid, offset in file, 1)
921 * When a file extent is removed file truncation, we find the
922 * corresponding implicit back refs and check the following fields:
924 * (btrfs_header_owner(leaf), inode objectid, offset in file)
926 * Btree extents can be referenced by:
928 * - Different subvolumes
930 * Both the implicit back refs and the full back refs for tree blocks
931 * only consist of key. The key offset for the implicit back refs is
932 * objectid of block's owner tree. The key offset for the full back refs
933 * is the first byte of parent block.
935 * When implicit back refs is used, information about the lowest key and
936 * level of the tree block are required. These information are stored in
937 * tree block info structure.
940 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
941 static int convert_extent_item_v0(struct btrfs_trans_handle
*trans
,
942 struct btrfs_root
*root
,
943 struct btrfs_path
*path
,
944 u64 owner
, u32 extra_size
)
946 struct btrfs_extent_item
*item
;
947 struct btrfs_extent_item_v0
*ei0
;
948 struct btrfs_extent_ref_v0
*ref0
;
949 struct btrfs_tree_block_info
*bi
;
950 struct extent_buffer
*leaf
;
951 struct btrfs_key key
;
952 struct btrfs_key found_key
;
953 u32 new_size
= sizeof(*item
);
957 leaf
= path
->nodes
[0];
958 BUG_ON(btrfs_item_size_nr(leaf
, path
->slots
[0]) != sizeof(*ei0
));
960 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
961 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
962 struct btrfs_extent_item_v0
);
963 refs
= btrfs_extent_refs_v0(leaf
, ei0
);
965 if (owner
== (u64
)-1) {
967 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
968 ret
= btrfs_next_leaf(root
, path
);
971 BUG_ON(ret
> 0); /* Corruption */
972 leaf
= path
->nodes
[0];
974 btrfs_item_key_to_cpu(leaf
, &found_key
,
976 BUG_ON(key
.objectid
!= found_key
.objectid
);
977 if (found_key
.type
!= BTRFS_EXTENT_REF_V0_KEY
) {
981 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
982 struct btrfs_extent_ref_v0
);
983 owner
= btrfs_ref_objectid_v0(leaf
, ref0
);
987 btrfs_release_path(path
);
989 if (owner
< BTRFS_FIRST_FREE_OBJECTID
)
990 new_size
+= sizeof(*bi
);
992 new_size
-= sizeof(*ei0
);
993 ret
= btrfs_search_slot(trans
, root
, &key
, path
,
994 new_size
+ extra_size
, 1);
997 BUG_ON(ret
); /* Corruption */
999 btrfs_extend_item(trans
, root
, path
, new_size
);
1001 leaf
= path
->nodes
[0];
1002 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1003 btrfs_set_extent_refs(leaf
, item
, refs
);
1004 /* FIXME: get real generation */
1005 btrfs_set_extent_generation(leaf
, item
, 0);
1006 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1007 btrfs_set_extent_flags(leaf
, item
,
1008 BTRFS_EXTENT_FLAG_TREE_BLOCK
|
1009 BTRFS_BLOCK_FLAG_FULL_BACKREF
);
1010 bi
= (struct btrfs_tree_block_info
*)(item
+ 1);
1011 /* FIXME: get first key of the block */
1012 memset_extent_buffer(leaf
, 0, (unsigned long)bi
, sizeof(*bi
));
1013 btrfs_set_tree_block_level(leaf
, bi
, (int)owner
);
1015 btrfs_set_extent_flags(leaf
, item
, BTRFS_EXTENT_FLAG_DATA
);
1017 btrfs_mark_buffer_dirty(leaf
);
1022 static u64
hash_extent_data_ref(u64 root_objectid
, u64 owner
, u64 offset
)
1024 u32 high_crc
= ~(u32
)0;
1025 u32 low_crc
= ~(u32
)0;
1028 lenum
= cpu_to_le64(root_objectid
);
1029 high_crc
= crc32c(high_crc
, &lenum
, sizeof(lenum
));
1030 lenum
= cpu_to_le64(owner
);
1031 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
1032 lenum
= cpu_to_le64(offset
);
1033 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
1035 return ((u64
)high_crc
<< 31) ^ (u64
)low_crc
;
1038 static u64
hash_extent_data_ref_item(struct extent_buffer
*leaf
,
1039 struct btrfs_extent_data_ref
*ref
)
1041 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf
, ref
),
1042 btrfs_extent_data_ref_objectid(leaf
, ref
),
1043 btrfs_extent_data_ref_offset(leaf
, ref
));
1046 static int match_extent_data_ref(struct extent_buffer
*leaf
,
1047 struct btrfs_extent_data_ref
*ref
,
1048 u64 root_objectid
, u64 owner
, u64 offset
)
1050 if (btrfs_extent_data_ref_root(leaf
, ref
) != root_objectid
||
1051 btrfs_extent_data_ref_objectid(leaf
, ref
) != owner
||
1052 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
1057 static noinline
int lookup_extent_data_ref(struct btrfs_trans_handle
*trans
,
1058 struct btrfs_root
*root
,
1059 struct btrfs_path
*path
,
1060 u64 bytenr
, u64 parent
,
1062 u64 owner
, u64 offset
)
1064 struct btrfs_key key
;
1065 struct btrfs_extent_data_ref
*ref
;
1066 struct extent_buffer
*leaf
;
1072 key
.objectid
= bytenr
;
1074 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
1075 key
.offset
= parent
;
1077 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
1078 key
.offset
= hash_extent_data_ref(root_objectid
,
1083 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1092 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1093 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
1094 btrfs_release_path(path
);
1095 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1106 leaf
= path
->nodes
[0];
1107 nritems
= btrfs_header_nritems(leaf
);
1109 if (path
->slots
[0] >= nritems
) {
1110 ret
= btrfs_next_leaf(root
, path
);
1116 leaf
= path
->nodes
[0];
1117 nritems
= btrfs_header_nritems(leaf
);
1121 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1122 if (key
.objectid
!= bytenr
||
1123 key
.type
!= BTRFS_EXTENT_DATA_REF_KEY
)
1126 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1127 struct btrfs_extent_data_ref
);
1129 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
1132 btrfs_release_path(path
);
1144 static noinline
int insert_extent_data_ref(struct btrfs_trans_handle
*trans
,
1145 struct btrfs_root
*root
,
1146 struct btrfs_path
*path
,
1147 u64 bytenr
, u64 parent
,
1148 u64 root_objectid
, u64 owner
,
1149 u64 offset
, int refs_to_add
)
1151 struct btrfs_key key
;
1152 struct extent_buffer
*leaf
;
1157 key
.objectid
= bytenr
;
1159 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
1160 key
.offset
= parent
;
1161 size
= sizeof(struct btrfs_shared_data_ref
);
1163 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
1164 key
.offset
= hash_extent_data_ref(root_objectid
,
1166 size
= sizeof(struct btrfs_extent_data_ref
);
1169 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, size
);
1170 if (ret
&& ret
!= -EEXIST
)
1173 leaf
= path
->nodes
[0];
1175 struct btrfs_shared_data_ref
*ref
;
1176 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1177 struct btrfs_shared_data_ref
);
1179 btrfs_set_shared_data_ref_count(leaf
, ref
, refs_to_add
);
1181 num_refs
= btrfs_shared_data_ref_count(leaf
, ref
);
1182 num_refs
+= refs_to_add
;
1183 btrfs_set_shared_data_ref_count(leaf
, ref
, num_refs
);
1186 struct btrfs_extent_data_ref
*ref
;
1187 while (ret
== -EEXIST
) {
1188 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1189 struct btrfs_extent_data_ref
);
1190 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
1193 btrfs_release_path(path
);
1195 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1197 if (ret
&& ret
!= -EEXIST
)
1200 leaf
= path
->nodes
[0];
1202 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1203 struct btrfs_extent_data_ref
);
1205 btrfs_set_extent_data_ref_root(leaf
, ref
,
1207 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
1208 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
1209 btrfs_set_extent_data_ref_count(leaf
, ref
, refs_to_add
);
1211 num_refs
= btrfs_extent_data_ref_count(leaf
, ref
);
1212 num_refs
+= refs_to_add
;
1213 btrfs_set_extent_data_ref_count(leaf
, ref
, num_refs
);
1216 btrfs_mark_buffer_dirty(leaf
);
1219 btrfs_release_path(path
);
1223 static noinline
int remove_extent_data_ref(struct btrfs_trans_handle
*trans
,
1224 struct btrfs_root
*root
,
1225 struct btrfs_path
*path
,
1228 struct btrfs_key key
;
1229 struct btrfs_extent_data_ref
*ref1
= NULL
;
1230 struct btrfs_shared_data_ref
*ref2
= NULL
;
1231 struct extent_buffer
*leaf
;
1235 leaf
= path
->nodes
[0];
1236 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1238 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1239 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
1240 struct btrfs_extent_data_ref
);
1241 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1242 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
1243 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
1244 struct btrfs_shared_data_ref
);
1245 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1246 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1247 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
1248 struct btrfs_extent_ref_v0
*ref0
;
1249 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1250 struct btrfs_extent_ref_v0
);
1251 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
1257 BUG_ON(num_refs
< refs_to_drop
);
1258 num_refs
-= refs_to_drop
;
1260 if (num_refs
== 0) {
1261 ret
= btrfs_del_item(trans
, root
, path
);
1263 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
)
1264 btrfs_set_extent_data_ref_count(leaf
, ref1
, num_refs
);
1265 else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
)
1266 btrfs_set_shared_data_ref_count(leaf
, ref2
, num_refs
);
1267 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1269 struct btrfs_extent_ref_v0
*ref0
;
1270 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1271 struct btrfs_extent_ref_v0
);
1272 btrfs_set_ref_count_v0(leaf
, ref0
, num_refs
);
1275 btrfs_mark_buffer_dirty(leaf
);
1280 static noinline u32
extent_data_ref_count(struct btrfs_root
*root
,
1281 struct btrfs_path
*path
,
1282 struct btrfs_extent_inline_ref
*iref
)
1284 struct btrfs_key key
;
1285 struct extent_buffer
*leaf
;
1286 struct btrfs_extent_data_ref
*ref1
;
1287 struct btrfs_shared_data_ref
*ref2
;
1290 leaf
= path
->nodes
[0];
1291 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1293 if (btrfs_extent_inline_ref_type(leaf
, iref
) ==
1294 BTRFS_EXTENT_DATA_REF_KEY
) {
1295 ref1
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1296 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1298 ref2
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1299 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1301 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1302 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
1303 struct btrfs_extent_data_ref
);
1304 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
1305 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
1306 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
1307 struct btrfs_shared_data_ref
);
1308 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
1309 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1310 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
1311 struct btrfs_extent_ref_v0
*ref0
;
1312 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
1313 struct btrfs_extent_ref_v0
);
1314 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
1322 static noinline
int lookup_tree_block_ref(struct btrfs_trans_handle
*trans
,
1323 struct btrfs_root
*root
,
1324 struct btrfs_path
*path
,
1325 u64 bytenr
, u64 parent
,
1328 struct btrfs_key key
;
1331 key
.objectid
= bytenr
;
1333 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1334 key
.offset
= parent
;
1336 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
1337 key
.offset
= root_objectid
;
1340 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1343 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1344 if (ret
== -ENOENT
&& parent
) {
1345 btrfs_release_path(path
);
1346 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
1347 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1355 static noinline
int insert_tree_block_ref(struct btrfs_trans_handle
*trans
,
1356 struct btrfs_root
*root
,
1357 struct btrfs_path
*path
,
1358 u64 bytenr
, u64 parent
,
1361 struct btrfs_key key
;
1364 key
.objectid
= bytenr
;
1366 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1367 key
.offset
= parent
;
1369 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
1370 key
.offset
= root_objectid
;
1373 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, 0);
1374 btrfs_release_path(path
);
1378 static inline int extent_ref_type(u64 parent
, u64 owner
)
1381 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1383 type
= BTRFS_SHARED_BLOCK_REF_KEY
;
1385 type
= BTRFS_TREE_BLOCK_REF_KEY
;
1388 type
= BTRFS_SHARED_DATA_REF_KEY
;
1390 type
= BTRFS_EXTENT_DATA_REF_KEY
;
1395 static int find_next_key(struct btrfs_path
*path
, int level
,
1396 struct btrfs_key
*key
)
1399 for (; level
< BTRFS_MAX_LEVEL
; level
++) {
1400 if (!path
->nodes
[level
])
1402 if (path
->slots
[level
] + 1 >=
1403 btrfs_header_nritems(path
->nodes
[level
]))
1406 btrfs_item_key_to_cpu(path
->nodes
[level
], key
,
1407 path
->slots
[level
] + 1);
1409 btrfs_node_key_to_cpu(path
->nodes
[level
], key
,
1410 path
->slots
[level
] + 1);
1417 * look for inline back ref. if back ref is found, *ref_ret is set
1418 * to the address of inline back ref, and 0 is returned.
1420 * if back ref isn't found, *ref_ret is set to the address where it
1421 * should be inserted, and -ENOENT is returned.
1423 * if insert is true and there are too many inline back refs, the path
1424 * points to the extent item, and -EAGAIN is returned.
1426 * NOTE: inline back refs are ordered in the same way that back ref
1427 * items in the tree are ordered.
1429 static noinline_for_stack
1430 int lookup_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1431 struct btrfs_root
*root
,
1432 struct btrfs_path
*path
,
1433 struct btrfs_extent_inline_ref
**ref_ret
,
1434 u64 bytenr
, u64 num_bytes
,
1435 u64 parent
, u64 root_objectid
,
1436 u64 owner
, u64 offset
, int insert
)
1438 struct btrfs_key key
;
1439 struct extent_buffer
*leaf
;
1440 struct btrfs_extent_item
*ei
;
1441 struct btrfs_extent_inline_ref
*iref
;
1452 key
.objectid
= bytenr
;
1453 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1454 key
.offset
= num_bytes
;
1456 want
= extent_ref_type(parent
, owner
);
1458 extra_size
= btrfs_extent_inline_ref_size(want
);
1459 path
->keep_locks
= 1;
1462 ret
= btrfs_search_slot(trans
, root
, &key
, path
, extra_size
, 1);
1467 if (ret
&& !insert
) {
1471 BUG_ON(ret
); /* Corruption */
1473 leaf
= path
->nodes
[0];
1474 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1475 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1476 if (item_size
< sizeof(*ei
)) {
1481 ret
= convert_extent_item_v0(trans
, root
, path
, owner
,
1487 leaf
= path
->nodes
[0];
1488 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1491 BUG_ON(item_size
< sizeof(*ei
));
1493 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1494 flags
= btrfs_extent_flags(leaf
, ei
);
1496 ptr
= (unsigned long)(ei
+ 1);
1497 end
= (unsigned long)ei
+ item_size
;
1499 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
1500 ptr
+= sizeof(struct btrfs_tree_block_info
);
1503 BUG_ON(!(flags
& BTRFS_EXTENT_FLAG_DATA
));
1512 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1513 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1517 ptr
+= btrfs_extent_inline_ref_size(type
);
1521 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1522 struct btrfs_extent_data_ref
*dref
;
1523 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1524 if (match_extent_data_ref(leaf
, dref
, root_objectid
,
1529 if (hash_extent_data_ref_item(leaf
, dref
) <
1530 hash_extent_data_ref(root_objectid
, owner
, offset
))
1534 ref_offset
= btrfs_extent_inline_ref_offset(leaf
, iref
);
1536 if (parent
== ref_offset
) {
1540 if (ref_offset
< parent
)
1543 if (root_objectid
== ref_offset
) {
1547 if (ref_offset
< root_objectid
)
1551 ptr
+= btrfs_extent_inline_ref_size(type
);
1553 if (err
== -ENOENT
&& insert
) {
1554 if (item_size
+ extra_size
>=
1555 BTRFS_MAX_EXTENT_ITEM_SIZE(root
)) {
1560 * To add new inline back ref, we have to make sure
1561 * there is no corresponding back ref item.
1562 * For simplicity, we just do not add new inline back
1563 * ref if there is any kind of item for this block
1565 if (find_next_key(path
, 0, &key
) == 0 &&
1566 key
.objectid
== bytenr
&&
1567 key
.type
< BTRFS_BLOCK_GROUP_ITEM_KEY
) {
1572 *ref_ret
= (struct btrfs_extent_inline_ref
*)ptr
;
1575 path
->keep_locks
= 0;
1576 btrfs_unlock_up_safe(path
, 1);
1582 * helper to add new inline back ref
1584 static noinline_for_stack
1585 void setup_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1586 struct btrfs_root
*root
,
1587 struct btrfs_path
*path
,
1588 struct btrfs_extent_inline_ref
*iref
,
1589 u64 parent
, u64 root_objectid
,
1590 u64 owner
, u64 offset
, int refs_to_add
,
1591 struct btrfs_delayed_extent_op
*extent_op
)
1593 struct extent_buffer
*leaf
;
1594 struct btrfs_extent_item
*ei
;
1597 unsigned long item_offset
;
1602 leaf
= path
->nodes
[0];
1603 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1604 item_offset
= (unsigned long)iref
- (unsigned long)ei
;
1606 type
= extent_ref_type(parent
, owner
);
1607 size
= btrfs_extent_inline_ref_size(type
);
1609 btrfs_extend_item(trans
, root
, path
, size
);
1611 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1612 refs
= btrfs_extent_refs(leaf
, ei
);
1613 refs
+= refs_to_add
;
1614 btrfs_set_extent_refs(leaf
, ei
, refs
);
1616 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1618 ptr
= (unsigned long)ei
+ item_offset
;
1619 end
= (unsigned long)ei
+ btrfs_item_size_nr(leaf
, path
->slots
[0]);
1620 if (ptr
< end
- size
)
1621 memmove_extent_buffer(leaf
, ptr
+ size
, ptr
,
1624 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1625 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
1626 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1627 struct btrfs_extent_data_ref
*dref
;
1628 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1629 btrfs_set_extent_data_ref_root(leaf
, dref
, root_objectid
);
1630 btrfs_set_extent_data_ref_objectid(leaf
, dref
, owner
);
1631 btrfs_set_extent_data_ref_offset(leaf
, dref
, offset
);
1632 btrfs_set_extent_data_ref_count(leaf
, dref
, refs_to_add
);
1633 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1634 struct btrfs_shared_data_ref
*sref
;
1635 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1636 btrfs_set_shared_data_ref_count(leaf
, sref
, refs_to_add
);
1637 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1638 } else if (type
== BTRFS_SHARED_BLOCK_REF_KEY
) {
1639 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1641 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
1643 btrfs_mark_buffer_dirty(leaf
);
1646 static int lookup_extent_backref(struct btrfs_trans_handle
*trans
,
1647 struct btrfs_root
*root
,
1648 struct btrfs_path
*path
,
1649 struct btrfs_extent_inline_ref
**ref_ret
,
1650 u64 bytenr
, u64 num_bytes
, u64 parent
,
1651 u64 root_objectid
, u64 owner
, u64 offset
)
1655 ret
= lookup_inline_extent_backref(trans
, root
, path
, ref_ret
,
1656 bytenr
, num_bytes
, parent
,
1657 root_objectid
, owner
, offset
, 0);
1661 btrfs_release_path(path
);
1664 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1665 ret
= lookup_tree_block_ref(trans
, root
, path
, bytenr
, parent
,
1668 ret
= lookup_extent_data_ref(trans
, root
, path
, bytenr
, parent
,
1669 root_objectid
, owner
, offset
);
1675 * helper to update/remove inline back ref
1677 static noinline_for_stack
1678 void update_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1679 struct btrfs_root
*root
,
1680 struct btrfs_path
*path
,
1681 struct btrfs_extent_inline_ref
*iref
,
1683 struct btrfs_delayed_extent_op
*extent_op
)
1685 struct extent_buffer
*leaf
;
1686 struct btrfs_extent_item
*ei
;
1687 struct btrfs_extent_data_ref
*dref
= NULL
;
1688 struct btrfs_shared_data_ref
*sref
= NULL
;
1696 leaf
= path
->nodes
[0];
1697 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1698 refs
= btrfs_extent_refs(leaf
, ei
);
1699 WARN_ON(refs_to_mod
< 0 && refs
+ refs_to_mod
<= 0);
1700 refs
+= refs_to_mod
;
1701 btrfs_set_extent_refs(leaf
, ei
, refs
);
1703 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1705 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1707 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1708 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1709 refs
= btrfs_extent_data_ref_count(leaf
, dref
);
1710 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1711 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1712 refs
= btrfs_shared_data_ref_count(leaf
, sref
);
1715 BUG_ON(refs_to_mod
!= -1);
1718 BUG_ON(refs_to_mod
< 0 && refs
< -refs_to_mod
);
1719 refs
+= refs_to_mod
;
1722 if (type
== BTRFS_EXTENT_DATA_REF_KEY
)
1723 btrfs_set_extent_data_ref_count(leaf
, dref
, refs
);
1725 btrfs_set_shared_data_ref_count(leaf
, sref
, refs
);
1727 size
= btrfs_extent_inline_ref_size(type
);
1728 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1729 ptr
= (unsigned long)iref
;
1730 end
= (unsigned long)ei
+ item_size
;
1731 if (ptr
+ size
< end
)
1732 memmove_extent_buffer(leaf
, ptr
, ptr
+ size
,
1735 btrfs_truncate_item(trans
, root
, path
, item_size
, 1);
1737 btrfs_mark_buffer_dirty(leaf
);
1740 static noinline_for_stack
1741 int insert_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1742 struct btrfs_root
*root
,
1743 struct btrfs_path
*path
,
1744 u64 bytenr
, u64 num_bytes
, u64 parent
,
1745 u64 root_objectid
, u64 owner
,
1746 u64 offset
, int refs_to_add
,
1747 struct btrfs_delayed_extent_op
*extent_op
)
1749 struct btrfs_extent_inline_ref
*iref
;
1752 ret
= lookup_inline_extent_backref(trans
, root
, path
, &iref
,
1753 bytenr
, num_bytes
, parent
,
1754 root_objectid
, owner
, offset
, 1);
1756 BUG_ON(owner
< BTRFS_FIRST_FREE_OBJECTID
);
1757 update_inline_extent_backref(trans
, root
, path
, iref
,
1758 refs_to_add
, extent_op
);
1759 } else if (ret
== -ENOENT
) {
1760 setup_inline_extent_backref(trans
, root
, path
, iref
, parent
,
1761 root_objectid
, owner
, offset
,
1762 refs_to_add
, extent_op
);
1768 static int insert_extent_backref(struct btrfs_trans_handle
*trans
,
1769 struct btrfs_root
*root
,
1770 struct btrfs_path
*path
,
1771 u64 bytenr
, u64 parent
, u64 root_objectid
,
1772 u64 owner
, u64 offset
, int refs_to_add
)
1775 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1776 BUG_ON(refs_to_add
!= 1);
1777 ret
= insert_tree_block_ref(trans
, root
, path
, bytenr
,
1778 parent
, root_objectid
);
1780 ret
= insert_extent_data_ref(trans
, root
, path
, bytenr
,
1781 parent
, root_objectid
,
1782 owner
, offset
, refs_to_add
);
1787 static int remove_extent_backref(struct btrfs_trans_handle
*trans
,
1788 struct btrfs_root
*root
,
1789 struct btrfs_path
*path
,
1790 struct btrfs_extent_inline_ref
*iref
,
1791 int refs_to_drop
, int is_data
)
1795 BUG_ON(!is_data
&& refs_to_drop
!= 1);
1797 update_inline_extent_backref(trans
, root
, path
, iref
,
1798 -refs_to_drop
, NULL
);
1799 } else if (is_data
) {
1800 ret
= remove_extent_data_ref(trans
, root
, path
, refs_to_drop
);
1802 ret
= btrfs_del_item(trans
, root
, path
);
1807 static int btrfs_issue_discard(struct block_device
*bdev
,
1810 return blkdev_issue_discard(bdev
, start
>> 9, len
>> 9, GFP_NOFS
, 0);
1813 static int btrfs_discard_extent(struct btrfs_root
*root
, u64 bytenr
,
1814 u64 num_bytes
, u64
*actual_bytes
)
1817 u64 discarded_bytes
= 0;
1818 struct btrfs_bio
*bbio
= NULL
;
1821 /* Tell the block device(s) that the sectors can be discarded */
1822 ret
= btrfs_map_block(root
->fs_info
, REQ_DISCARD
,
1823 bytenr
, &num_bytes
, &bbio
, 0);
1824 /* Error condition is -ENOMEM */
1826 struct btrfs_bio_stripe
*stripe
= bbio
->stripes
;
1830 for (i
= 0; i
< bbio
->num_stripes
; i
++, stripe
++) {
1831 if (!stripe
->dev
->can_discard
)
1834 ret
= btrfs_issue_discard(stripe
->dev
->bdev
,
1838 discarded_bytes
+= stripe
->length
;
1839 else if (ret
!= -EOPNOTSUPP
)
1840 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
1843 * Just in case we get back EOPNOTSUPP for some reason,
1844 * just ignore the return value so we don't screw up
1845 * people calling discard_extent.
1853 *actual_bytes
= discarded_bytes
;
1856 if (ret
== -EOPNOTSUPP
)
1861 /* Can return -ENOMEM */
1862 int btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
1863 struct btrfs_root
*root
,
1864 u64 bytenr
, u64 num_bytes
, u64 parent
,
1865 u64 root_objectid
, u64 owner
, u64 offset
, int for_cow
)
1868 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1870 BUG_ON(owner
< BTRFS_FIRST_FREE_OBJECTID
&&
1871 root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
1873 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1874 ret
= btrfs_add_delayed_tree_ref(fs_info
, trans
, bytenr
,
1876 parent
, root_objectid
, (int)owner
,
1877 BTRFS_ADD_DELAYED_REF
, NULL
, for_cow
);
1879 ret
= btrfs_add_delayed_data_ref(fs_info
, trans
, bytenr
,
1881 parent
, root_objectid
, owner
, offset
,
1882 BTRFS_ADD_DELAYED_REF
, NULL
, for_cow
);
1887 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
1888 struct btrfs_root
*root
,
1889 u64 bytenr
, u64 num_bytes
,
1890 u64 parent
, u64 root_objectid
,
1891 u64 owner
, u64 offset
, int refs_to_add
,
1892 struct btrfs_delayed_extent_op
*extent_op
)
1894 struct btrfs_path
*path
;
1895 struct extent_buffer
*leaf
;
1896 struct btrfs_extent_item
*item
;
1901 path
= btrfs_alloc_path();
1906 path
->leave_spinning
= 1;
1907 /* this will setup the path even if it fails to insert the back ref */
1908 ret
= insert_inline_extent_backref(trans
, root
->fs_info
->extent_root
,
1909 path
, bytenr
, num_bytes
, parent
,
1910 root_objectid
, owner
, offset
,
1911 refs_to_add
, extent_op
);
1915 if (ret
!= -EAGAIN
) {
1920 leaf
= path
->nodes
[0];
1921 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1922 refs
= btrfs_extent_refs(leaf
, item
);
1923 btrfs_set_extent_refs(leaf
, item
, refs
+ refs_to_add
);
1925 __run_delayed_extent_op(extent_op
, leaf
, item
);
1927 btrfs_mark_buffer_dirty(leaf
);
1928 btrfs_release_path(path
);
1931 path
->leave_spinning
= 1;
1933 /* now insert the actual backref */
1934 ret
= insert_extent_backref(trans
, root
->fs_info
->extent_root
,
1935 path
, bytenr
, parent
, root_objectid
,
1936 owner
, offset
, refs_to_add
);
1938 btrfs_abort_transaction(trans
, root
, ret
);
1940 btrfs_free_path(path
);
1944 static int run_delayed_data_ref(struct btrfs_trans_handle
*trans
,
1945 struct btrfs_root
*root
,
1946 struct btrfs_delayed_ref_node
*node
,
1947 struct btrfs_delayed_extent_op
*extent_op
,
1948 int insert_reserved
)
1951 struct btrfs_delayed_data_ref
*ref
;
1952 struct btrfs_key ins
;
1957 ins
.objectid
= node
->bytenr
;
1958 ins
.offset
= node
->num_bytes
;
1959 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1961 ref
= btrfs_delayed_node_to_data_ref(node
);
1962 if (node
->type
== BTRFS_SHARED_DATA_REF_KEY
)
1963 parent
= ref
->parent
;
1965 ref_root
= ref
->root
;
1967 if (node
->action
== BTRFS_ADD_DELAYED_REF
&& insert_reserved
) {
1969 BUG_ON(extent_op
->update_key
);
1970 flags
|= extent_op
->flags_to_set
;
1972 ret
= alloc_reserved_file_extent(trans
, root
,
1973 parent
, ref_root
, flags
,
1974 ref
->objectid
, ref
->offset
,
1975 &ins
, node
->ref_mod
);
1976 } else if (node
->action
== BTRFS_ADD_DELAYED_REF
) {
1977 ret
= __btrfs_inc_extent_ref(trans
, root
, node
->bytenr
,
1978 node
->num_bytes
, parent
,
1979 ref_root
, ref
->objectid
,
1980 ref
->offset
, node
->ref_mod
,
1982 } else if (node
->action
== BTRFS_DROP_DELAYED_REF
) {
1983 ret
= __btrfs_free_extent(trans
, root
, node
->bytenr
,
1984 node
->num_bytes
, parent
,
1985 ref_root
, ref
->objectid
,
1986 ref
->offset
, node
->ref_mod
,
1994 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op
*extent_op
,
1995 struct extent_buffer
*leaf
,
1996 struct btrfs_extent_item
*ei
)
1998 u64 flags
= btrfs_extent_flags(leaf
, ei
);
1999 if (extent_op
->update_flags
) {
2000 flags
|= extent_op
->flags_to_set
;
2001 btrfs_set_extent_flags(leaf
, ei
, flags
);
2004 if (extent_op
->update_key
) {
2005 struct btrfs_tree_block_info
*bi
;
2006 BUG_ON(!(flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
));
2007 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
2008 btrfs_set_tree_block_key(leaf
, bi
, &extent_op
->key
);
2012 static int run_delayed_extent_op(struct btrfs_trans_handle
*trans
,
2013 struct btrfs_root
*root
,
2014 struct btrfs_delayed_ref_node
*node
,
2015 struct btrfs_delayed_extent_op
*extent_op
)
2017 struct btrfs_key key
;
2018 struct btrfs_path
*path
;
2019 struct btrfs_extent_item
*ei
;
2020 struct extent_buffer
*leaf
;
2028 path
= btrfs_alloc_path();
2032 key
.objectid
= node
->bytenr
;
2033 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2034 key
.offset
= node
->num_bytes
;
2037 path
->leave_spinning
= 1;
2038 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
,
2049 leaf
= path
->nodes
[0];
2050 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2051 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2052 if (item_size
< sizeof(*ei
)) {
2053 ret
= convert_extent_item_v0(trans
, root
->fs_info
->extent_root
,
2059 leaf
= path
->nodes
[0];
2060 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2063 BUG_ON(item_size
< sizeof(*ei
));
2064 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
2065 __run_delayed_extent_op(extent_op
, leaf
, ei
);
2067 btrfs_mark_buffer_dirty(leaf
);
2069 btrfs_free_path(path
);
2073 static int run_delayed_tree_ref(struct btrfs_trans_handle
*trans
,
2074 struct btrfs_root
*root
,
2075 struct btrfs_delayed_ref_node
*node
,
2076 struct btrfs_delayed_extent_op
*extent_op
,
2077 int insert_reserved
)
2080 struct btrfs_delayed_tree_ref
*ref
;
2081 struct btrfs_key ins
;
2085 ins
.objectid
= node
->bytenr
;
2086 ins
.offset
= node
->num_bytes
;
2087 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
2089 ref
= btrfs_delayed_node_to_tree_ref(node
);
2090 if (node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
)
2091 parent
= ref
->parent
;
2093 ref_root
= ref
->root
;
2095 BUG_ON(node
->ref_mod
!= 1);
2096 if (node
->action
== BTRFS_ADD_DELAYED_REF
&& insert_reserved
) {
2097 BUG_ON(!extent_op
|| !extent_op
->update_flags
||
2098 !extent_op
->update_key
);
2099 ret
= alloc_reserved_tree_block(trans
, root
,
2101 extent_op
->flags_to_set
,
2104 } else if (node
->action
== BTRFS_ADD_DELAYED_REF
) {
2105 ret
= __btrfs_inc_extent_ref(trans
, root
, node
->bytenr
,
2106 node
->num_bytes
, parent
, ref_root
,
2107 ref
->level
, 0, 1, extent_op
);
2108 } else if (node
->action
== BTRFS_DROP_DELAYED_REF
) {
2109 ret
= __btrfs_free_extent(trans
, root
, node
->bytenr
,
2110 node
->num_bytes
, parent
, ref_root
,
2111 ref
->level
, 0, 1, extent_op
);
2118 /* helper function to actually process a single delayed ref entry */
2119 static int run_one_delayed_ref(struct btrfs_trans_handle
*trans
,
2120 struct btrfs_root
*root
,
2121 struct btrfs_delayed_ref_node
*node
,
2122 struct btrfs_delayed_extent_op
*extent_op
,
2123 int insert_reserved
)
2130 if (btrfs_delayed_ref_is_head(node
)) {
2131 struct btrfs_delayed_ref_head
*head
;
2133 * we've hit the end of the chain and we were supposed
2134 * to insert this extent into the tree. But, it got
2135 * deleted before we ever needed to insert it, so all
2136 * we have to do is clean up the accounting
2139 head
= btrfs_delayed_node_to_head(node
);
2140 if (insert_reserved
) {
2141 btrfs_pin_extent(root
, node
->bytenr
,
2142 node
->num_bytes
, 1);
2143 if (head
->is_data
) {
2144 ret
= btrfs_del_csums(trans
, root
,
2152 if (node
->type
== BTRFS_TREE_BLOCK_REF_KEY
||
2153 node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
)
2154 ret
= run_delayed_tree_ref(trans
, root
, node
, extent_op
,
2156 else if (node
->type
== BTRFS_EXTENT_DATA_REF_KEY
||
2157 node
->type
== BTRFS_SHARED_DATA_REF_KEY
)
2158 ret
= run_delayed_data_ref(trans
, root
, node
, extent_op
,
2165 static noinline
struct btrfs_delayed_ref_node
*
2166 select_delayed_ref(struct btrfs_delayed_ref_head
*head
)
2168 struct rb_node
*node
;
2169 struct btrfs_delayed_ref_node
*ref
;
2170 int action
= BTRFS_ADD_DELAYED_REF
;
2173 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2174 * this prevents ref count from going down to zero when
2175 * there still are pending delayed ref.
2177 node
= rb_prev(&head
->node
.rb_node
);
2181 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
,
2183 if (ref
->bytenr
!= head
->node
.bytenr
)
2185 if (ref
->action
== action
)
2187 node
= rb_prev(node
);
2189 if (action
== BTRFS_ADD_DELAYED_REF
) {
2190 action
= BTRFS_DROP_DELAYED_REF
;
2197 * Returns 0 on success or if called with an already aborted transaction.
2198 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2200 static noinline
int run_clustered_refs(struct btrfs_trans_handle
*trans
,
2201 struct btrfs_root
*root
,
2202 struct list_head
*cluster
)
2204 struct btrfs_delayed_ref_root
*delayed_refs
;
2205 struct btrfs_delayed_ref_node
*ref
;
2206 struct btrfs_delayed_ref_head
*locked_ref
= NULL
;
2207 struct btrfs_delayed_extent_op
*extent_op
;
2208 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2211 int must_insert_reserved
= 0;
2213 delayed_refs
= &trans
->transaction
->delayed_refs
;
2216 /* pick a new head ref from the cluster list */
2217 if (list_empty(cluster
))
2220 locked_ref
= list_entry(cluster
->next
,
2221 struct btrfs_delayed_ref_head
, cluster
);
2223 /* grab the lock that says we are going to process
2224 * all the refs for this head */
2225 ret
= btrfs_delayed_ref_lock(trans
, locked_ref
);
2228 * we may have dropped the spin lock to get the head
2229 * mutex lock, and that might have given someone else
2230 * time to free the head. If that's true, it has been
2231 * removed from our list and we can move on.
2233 if (ret
== -EAGAIN
) {
2241 * We need to try and merge add/drops of the same ref since we
2242 * can run into issues with relocate dropping the implicit ref
2243 * and then it being added back again before the drop can
2244 * finish. If we merged anything we need to re-loop so we can
2247 btrfs_merge_delayed_refs(trans
, fs_info
, delayed_refs
,
2251 * locked_ref is the head node, so we have to go one
2252 * node back for any delayed ref updates
2254 ref
= select_delayed_ref(locked_ref
);
2256 if (ref
&& ref
->seq
&&
2257 btrfs_check_delayed_seq(fs_info
, delayed_refs
, ref
->seq
)) {
2259 * there are still refs with lower seq numbers in the
2260 * process of being added. Don't run this ref yet.
2262 list_del_init(&locked_ref
->cluster
);
2263 btrfs_delayed_ref_unlock(locked_ref
);
2265 delayed_refs
->num_heads_ready
++;
2266 spin_unlock(&delayed_refs
->lock
);
2268 spin_lock(&delayed_refs
->lock
);
2273 * record the must insert reserved flag before we
2274 * drop the spin lock.
2276 must_insert_reserved
= locked_ref
->must_insert_reserved
;
2277 locked_ref
->must_insert_reserved
= 0;
2279 extent_op
= locked_ref
->extent_op
;
2280 locked_ref
->extent_op
= NULL
;
2283 /* All delayed refs have been processed, Go ahead
2284 * and send the head node to run_one_delayed_ref,
2285 * so that any accounting fixes can happen
2287 ref
= &locked_ref
->node
;
2289 if (extent_op
&& must_insert_reserved
) {
2290 btrfs_free_delayed_extent_op(extent_op
);
2295 spin_unlock(&delayed_refs
->lock
);
2297 ret
= run_delayed_extent_op(trans
, root
,
2299 btrfs_free_delayed_extent_op(extent_op
);
2303 "btrfs: run_delayed_extent_op "
2304 "returned %d\n", ret
);
2305 spin_lock(&delayed_refs
->lock
);
2306 btrfs_delayed_ref_unlock(locked_ref
);
2315 rb_erase(&ref
->rb_node
, &delayed_refs
->root
);
2316 delayed_refs
->num_entries
--;
2317 if (!btrfs_delayed_ref_is_head(ref
)) {
2319 * when we play the delayed ref, also correct the
2322 switch (ref
->action
) {
2323 case BTRFS_ADD_DELAYED_REF
:
2324 case BTRFS_ADD_DELAYED_EXTENT
:
2325 locked_ref
->node
.ref_mod
-= ref
->ref_mod
;
2327 case BTRFS_DROP_DELAYED_REF
:
2328 locked_ref
->node
.ref_mod
+= ref
->ref_mod
;
2334 spin_unlock(&delayed_refs
->lock
);
2336 ret
= run_one_delayed_ref(trans
, root
, ref
, extent_op
,
2337 must_insert_reserved
);
2339 btrfs_free_delayed_extent_op(extent_op
);
2341 btrfs_delayed_ref_unlock(locked_ref
);
2342 btrfs_put_delayed_ref(ref
);
2344 "btrfs: run_one_delayed_ref returned %d\n", ret
);
2345 spin_lock(&delayed_refs
->lock
);
2350 * If this node is a head, that means all the refs in this head
2351 * have been dealt with, and we will pick the next head to deal
2352 * with, so we must unlock the head and drop it from the cluster
2353 * list before we release it.
2355 if (btrfs_delayed_ref_is_head(ref
)) {
2356 list_del_init(&locked_ref
->cluster
);
2357 btrfs_delayed_ref_unlock(locked_ref
);
2360 btrfs_put_delayed_ref(ref
);
2364 spin_lock(&delayed_refs
->lock
);
2369 #ifdef SCRAMBLE_DELAYED_REFS
2371 * Normally delayed refs get processed in ascending bytenr order. This
2372 * correlates in most cases to the order added. To expose dependencies on this
2373 * order, we start to process the tree in the middle instead of the beginning
2375 static u64
find_middle(struct rb_root
*root
)
2377 struct rb_node
*n
= root
->rb_node
;
2378 struct btrfs_delayed_ref_node
*entry
;
2381 u64 first
= 0, last
= 0;
2385 entry
= rb_entry(n
, struct btrfs_delayed_ref_node
, rb_node
);
2386 first
= entry
->bytenr
;
2390 entry
= rb_entry(n
, struct btrfs_delayed_ref_node
, rb_node
);
2391 last
= entry
->bytenr
;
2396 entry
= rb_entry(n
, struct btrfs_delayed_ref_node
, rb_node
);
2397 WARN_ON(!entry
->in_tree
);
2399 middle
= entry
->bytenr
;
2412 int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle
*trans
,
2413 struct btrfs_fs_info
*fs_info
)
2415 struct qgroup_update
*qgroup_update
;
2418 if (list_empty(&trans
->qgroup_ref_list
) !=
2419 !trans
->delayed_ref_elem
.seq
) {
2420 /* list without seq or seq without list */
2421 printk(KERN_ERR
"btrfs: qgroup accounting update error, list is%s empty, seq is %llu\n",
2422 list_empty(&trans
->qgroup_ref_list
) ? "" : " not",
2423 trans
->delayed_ref_elem
.seq
);
2427 if (!trans
->delayed_ref_elem
.seq
)
2430 while (!list_empty(&trans
->qgroup_ref_list
)) {
2431 qgroup_update
= list_first_entry(&trans
->qgroup_ref_list
,
2432 struct qgroup_update
, list
);
2433 list_del(&qgroup_update
->list
);
2435 ret
= btrfs_qgroup_account_ref(
2436 trans
, fs_info
, qgroup_update
->node
,
2437 qgroup_update
->extent_op
);
2438 kfree(qgroup_update
);
2441 btrfs_put_tree_mod_seq(fs_info
, &trans
->delayed_ref_elem
);
2446 static int refs_newer(struct btrfs_delayed_ref_root
*delayed_refs
, int seq
,
2449 int val
= atomic_read(&delayed_refs
->ref_seq
);
2451 if (val
< seq
|| val
>= seq
+ count
)
2457 * this starts processing the delayed reference count updates and
2458 * extent insertions we have queued up so far. count can be
2459 * 0, which means to process everything in the tree at the start
2460 * of the run (but not newly added entries), or it can be some target
2461 * number you'd like to process.
2463 * Returns 0 on success or if called with an aborted transaction
2464 * Returns <0 on error and aborts the transaction
2466 int btrfs_run_delayed_refs(struct btrfs_trans_handle
*trans
,
2467 struct btrfs_root
*root
, unsigned long count
)
2469 struct rb_node
*node
;
2470 struct btrfs_delayed_ref_root
*delayed_refs
;
2471 struct btrfs_delayed_ref_node
*ref
;
2472 struct list_head cluster
;
2475 int run_all
= count
== (unsigned long)-1;
2479 /* We'll clean this up in btrfs_cleanup_transaction */
2483 if (root
== root
->fs_info
->extent_root
)
2484 root
= root
->fs_info
->tree_root
;
2486 btrfs_delayed_refs_qgroup_accounting(trans
, root
->fs_info
);
2488 delayed_refs
= &trans
->transaction
->delayed_refs
;
2489 INIT_LIST_HEAD(&cluster
);
2491 count
= delayed_refs
->num_entries
* 2;
2495 if (!run_all
&& !run_most
) {
2497 int seq
= atomic_read(&delayed_refs
->ref_seq
);
2500 old
= atomic_cmpxchg(&delayed_refs
->procs_running_refs
, 0, 1);
2502 DEFINE_WAIT(__wait
);
2503 if (delayed_refs
->num_entries
< 16348)
2506 prepare_to_wait(&delayed_refs
->wait
, &__wait
,
2507 TASK_UNINTERRUPTIBLE
);
2509 old
= atomic_cmpxchg(&delayed_refs
->procs_running_refs
, 0, 1);
2512 finish_wait(&delayed_refs
->wait
, &__wait
);
2514 if (!refs_newer(delayed_refs
, seq
, 256))
2519 finish_wait(&delayed_refs
->wait
, &__wait
);
2525 atomic_inc(&delayed_refs
->procs_running_refs
);
2530 spin_lock(&delayed_refs
->lock
);
2532 #ifdef SCRAMBLE_DELAYED_REFS
2533 delayed_refs
->run_delayed_start
= find_middle(&delayed_refs
->root
);
2537 if (!(run_all
|| run_most
) &&
2538 delayed_refs
->num_heads_ready
< 64)
2542 * go find something we can process in the rbtree. We start at
2543 * the beginning of the tree, and then build a cluster
2544 * of refs to process starting at the first one we are able to
2547 delayed_start
= delayed_refs
->run_delayed_start
;
2548 ret
= btrfs_find_ref_cluster(trans
, &cluster
,
2549 delayed_refs
->run_delayed_start
);
2553 ret
= run_clustered_refs(trans
, root
, &cluster
);
2555 btrfs_release_ref_cluster(&cluster
);
2556 spin_unlock(&delayed_refs
->lock
);
2557 btrfs_abort_transaction(trans
, root
, ret
);
2558 atomic_dec(&delayed_refs
->procs_running_refs
);
2562 atomic_add(ret
, &delayed_refs
->ref_seq
);
2564 count
-= min_t(unsigned long, ret
, count
);
2569 if (delayed_start
>= delayed_refs
->run_delayed_start
) {
2572 * btrfs_find_ref_cluster looped. let's do one
2573 * more cycle. if we don't run any delayed ref
2574 * during that cycle (because we can't because
2575 * all of them are blocked), bail out.
2580 * no runnable refs left, stop trying
2587 /* refs were run, let's reset staleness detection */
2593 if (!list_empty(&trans
->new_bgs
)) {
2594 spin_unlock(&delayed_refs
->lock
);
2595 btrfs_create_pending_block_groups(trans
, root
);
2596 spin_lock(&delayed_refs
->lock
);
2599 node
= rb_first(&delayed_refs
->root
);
2602 count
= (unsigned long)-1;
2605 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
,
2607 if (btrfs_delayed_ref_is_head(ref
)) {
2608 struct btrfs_delayed_ref_head
*head
;
2610 head
= btrfs_delayed_node_to_head(ref
);
2611 atomic_inc(&ref
->refs
);
2613 spin_unlock(&delayed_refs
->lock
);
2615 * Mutex was contended, block until it's
2616 * released and try again
2618 mutex_lock(&head
->mutex
);
2619 mutex_unlock(&head
->mutex
);
2621 btrfs_put_delayed_ref(ref
);
2625 node
= rb_next(node
);
2627 spin_unlock(&delayed_refs
->lock
);
2628 schedule_timeout(1);
2632 atomic_dec(&delayed_refs
->procs_running_refs
);
2634 if (waitqueue_active(&delayed_refs
->wait
))
2635 wake_up(&delayed_refs
->wait
);
2637 spin_unlock(&delayed_refs
->lock
);
2638 assert_qgroups_uptodate(trans
);
2642 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle
*trans
,
2643 struct btrfs_root
*root
,
2644 u64 bytenr
, u64 num_bytes
, u64 flags
,
2647 struct btrfs_delayed_extent_op
*extent_op
;
2650 extent_op
= btrfs_alloc_delayed_extent_op();
2654 extent_op
->flags_to_set
= flags
;
2655 extent_op
->update_flags
= 1;
2656 extent_op
->update_key
= 0;
2657 extent_op
->is_data
= is_data
? 1 : 0;
2659 ret
= btrfs_add_delayed_extent_op(root
->fs_info
, trans
, bytenr
,
2660 num_bytes
, extent_op
);
2662 btrfs_free_delayed_extent_op(extent_op
);
2666 static noinline
int check_delayed_ref(struct btrfs_trans_handle
*trans
,
2667 struct btrfs_root
*root
,
2668 struct btrfs_path
*path
,
2669 u64 objectid
, u64 offset
, u64 bytenr
)
2671 struct btrfs_delayed_ref_head
*head
;
2672 struct btrfs_delayed_ref_node
*ref
;
2673 struct btrfs_delayed_data_ref
*data_ref
;
2674 struct btrfs_delayed_ref_root
*delayed_refs
;
2675 struct rb_node
*node
;
2679 delayed_refs
= &trans
->transaction
->delayed_refs
;
2680 spin_lock(&delayed_refs
->lock
);
2681 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
2685 if (!mutex_trylock(&head
->mutex
)) {
2686 atomic_inc(&head
->node
.refs
);
2687 spin_unlock(&delayed_refs
->lock
);
2689 btrfs_release_path(path
);
2692 * Mutex was contended, block until it's released and let
2695 mutex_lock(&head
->mutex
);
2696 mutex_unlock(&head
->mutex
);
2697 btrfs_put_delayed_ref(&head
->node
);
2701 node
= rb_prev(&head
->node
.rb_node
);
2705 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
2707 if (ref
->bytenr
!= bytenr
)
2711 if (ref
->type
!= BTRFS_EXTENT_DATA_REF_KEY
)
2714 data_ref
= btrfs_delayed_node_to_data_ref(ref
);
2716 node
= rb_prev(node
);
2720 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
2721 if (ref
->bytenr
== bytenr
&& ref
->seq
== seq
)
2725 if (data_ref
->root
!= root
->root_key
.objectid
||
2726 data_ref
->objectid
!= objectid
|| data_ref
->offset
!= offset
)
2731 mutex_unlock(&head
->mutex
);
2733 spin_unlock(&delayed_refs
->lock
);
2737 static noinline
int check_committed_ref(struct btrfs_trans_handle
*trans
,
2738 struct btrfs_root
*root
,
2739 struct btrfs_path
*path
,
2740 u64 objectid
, u64 offset
, u64 bytenr
)
2742 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
2743 struct extent_buffer
*leaf
;
2744 struct btrfs_extent_data_ref
*ref
;
2745 struct btrfs_extent_inline_ref
*iref
;
2746 struct btrfs_extent_item
*ei
;
2747 struct btrfs_key key
;
2751 key
.objectid
= bytenr
;
2752 key
.offset
= (u64
)-1;
2753 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2755 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
2758 BUG_ON(ret
== 0); /* Corruption */
2761 if (path
->slots
[0] == 0)
2765 leaf
= path
->nodes
[0];
2766 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2768 if (key
.objectid
!= bytenr
|| key
.type
!= BTRFS_EXTENT_ITEM_KEY
)
2772 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2773 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2774 if (item_size
< sizeof(*ei
)) {
2775 WARN_ON(item_size
!= sizeof(struct btrfs_extent_item_v0
));
2779 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
2781 if (item_size
!= sizeof(*ei
) +
2782 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY
))
2785 if (btrfs_extent_generation(leaf
, ei
) <=
2786 btrfs_root_last_snapshot(&root
->root_item
))
2789 iref
= (struct btrfs_extent_inline_ref
*)(ei
+ 1);
2790 if (btrfs_extent_inline_ref_type(leaf
, iref
) !=
2791 BTRFS_EXTENT_DATA_REF_KEY
)
2794 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
2795 if (btrfs_extent_refs(leaf
, ei
) !=
2796 btrfs_extent_data_ref_count(leaf
, ref
) ||
2797 btrfs_extent_data_ref_root(leaf
, ref
) !=
2798 root
->root_key
.objectid
||
2799 btrfs_extent_data_ref_objectid(leaf
, ref
) != objectid
||
2800 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
2808 int btrfs_cross_ref_exist(struct btrfs_trans_handle
*trans
,
2809 struct btrfs_root
*root
,
2810 u64 objectid
, u64 offset
, u64 bytenr
)
2812 struct btrfs_path
*path
;
2816 path
= btrfs_alloc_path();
2821 ret
= check_committed_ref(trans
, root
, path
, objectid
,
2823 if (ret
&& ret
!= -ENOENT
)
2826 ret2
= check_delayed_ref(trans
, root
, path
, objectid
,
2828 } while (ret2
== -EAGAIN
);
2830 if (ret2
&& ret2
!= -ENOENT
) {
2835 if (ret
!= -ENOENT
|| ret2
!= -ENOENT
)
2838 btrfs_free_path(path
);
2839 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
2844 static int __btrfs_mod_ref(struct btrfs_trans_handle
*trans
,
2845 struct btrfs_root
*root
,
2846 struct extent_buffer
*buf
,
2847 int full_backref
, int inc
, int for_cow
)
2854 struct btrfs_key key
;
2855 struct btrfs_file_extent_item
*fi
;
2859 int (*process_func
)(struct btrfs_trans_handle
*, struct btrfs_root
*,
2860 u64
, u64
, u64
, u64
, u64
, u64
, int);
2862 ref_root
= btrfs_header_owner(buf
);
2863 nritems
= btrfs_header_nritems(buf
);
2864 level
= btrfs_header_level(buf
);
2866 if (!root
->ref_cows
&& level
== 0)
2870 process_func
= btrfs_inc_extent_ref
;
2872 process_func
= btrfs_free_extent
;
2875 parent
= buf
->start
;
2879 for (i
= 0; i
< nritems
; i
++) {
2881 btrfs_item_key_to_cpu(buf
, &key
, i
);
2882 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
2884 fi
= btrfs_item_ptr(buf
, i
,
2885 struct btrfs_file_extent_item
);
2886 if (btrfs_file_extent_type(buf
, fi
) ==
2887 BTRFS_FILE_EXTENT_INLINE
)
2889 bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
2893 num_bytes
= btrfs_file_extent_disk_num_bytes(buf
, fi
);
2894 key
.offset
-= btrfs_file_extent_offset(buf
, fi
);
2895 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
2896 parent
, ref_root
, key
.objectid
,
2897 key
.offset
, for_cow
);
2901 bytenr
= btrfs_node_blockptr(buf
, i
);
2902 num_bytes
= btrfs_level_size(root
, level
- 1);
2903 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
2904 parent
, ref_root
, level
- 1, 0,
2915 int btrfs_inc_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2916 struct extent_buffer
*buf
, int full_backref
, int for_cow
)
2918 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 1, for_cow
);
2921 int btrfs_dec_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2922 struct extent_buffer
*buf
, int full_backref
, int for_cow
)
2924 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 0, for_cow
);
2927 static int write_one_cache_group(struct btrfs_trans_handle
*trans
,
2928 struct btrfs_root
*root
,
2929 struct btrfs_path
*path
,
2930 struct btrfs_block_group_cache
*cache
)
2933 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
2935 struct extent_buffer
*leaf
;
2937 ret
= btrfs_search_slot(trans
, extent_root
, &cache
->key
, path
, 0, 1);
2940 BUG_ON(ret
); /* Corruption */
2942 leaf
= path
->nodes
[0];
2943 bi
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
2944 write_extent_buffer(leaf
, &cache
->item
, bi
, sizeof(cache
->item
));
2945 btrfs_mark_buffer_dirty(leaf
);
2946 btrfs_release_path(path
);
2949 btrfs_abort_transaction(trans
, root
, ret
);
2956 static struct btrfs_block_group_cache
*
2957 next_block_group(struct btrfs_root
*root
,
2958 struct btrfs_block_group_cache
*cache
)
2960 struct rb_node
*node
;
2961 spin_lock(&root
->fs_info
->block_group_cache_lock
);
2962 node
= rb_next(&cache
->cache_node
);
2963 btrfs_put_block_group(cache
);
2965 cache
= rb_entry(node
, struct btrfs_block_group_cache
,
2967 btrfs_get_block_group(cache
);
2970 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
2974 static int cache_save_setup(struct btrfs_block_group_cache
*block_group
,
2975 struct btrfs_trans_handle
*trans
,
2976 struct btrfs_path
*path
)
2978 struct btrfs_root
*root
= block_group
->fs_info
->tree_root
;
2979 struct inode
*inode
= NULL
;
2981 int dcs
= BTRFS_DC_ERROR
;
2987 * If this block group is smaller than 100 megs don't bother caching the
2990 if (block_group
->key
.offset
< (100 * 1024 * 1024)) {
2991 spin_lock(&block_group
->lock
);
2992 block_group
->disk_cache_state
= BTRFS_DC_WRITTEN
;
2993 spin_unlock(&block_group
->lock
);
2998 inode
= lookup_free_space_inode(root
, block_group
, path
);
2999 if (IS_ERR(inode
) && PTR_ERR(inode
) != -ENOENT
) {
3000 ret
= PTR_ERR(inode
);
3001 btrfs_release_path(path
);
3005 if (IS_ERR(inode
)) {
3009 if (block_group
->ro
)
3012 ret
= create_free_space_inode(root
, trans
, block_group
, path
);
3018 /* We've already setup this transaction, go ahead and exit */
3019 if (block_group
->cache_generation
== trans
->transid
&&
3020 i_size_read(inode
)) {
3021 dcs
= BTRFS_DC_SETUP
;
3026 * We want to set the generation to 0, that way if anything goes wrong
3027 * from here on out we know not to trust this cache when we load up next
3030 BTRFS_I(inode
)->generation
= 0;
3031 ret
= btrfs_update_inode(trans
, root
, inode
);
3034 if (i_size_read(inode
) > 0) {
3035 ret
= btrfs_truncate_free_space_cache(root
, trans
, path
,
3041 spin_lock(&block_group
->lock
);
3042 if (block_group
->cached
!= BTRFS_CACHE_FINISHED
||
3043 !btrfs_test_opt(root
, SPACE_CACHE
)) {
3045 * don't bother trying to write stuff out _if_
3046 * a) we're not cached,
3047 * b) we're with nospace_cache mount option.
3049 dcs
= BTRFS_DC_WRITTEN
;
3050 spin_unlock(&block_group
->lock
);
3053 spin_unlock(&block_group
->lock
);
3056 * Try to preallocate enough space based on how big the block group is.
3057 * Keep in mind this has to include any pinned space which could end up
3058 * taking up quite a bit since it's not folded into the other space
3061 num_pages
= (int)div64_u64(block_group
->key
.offset
, 256 * 1024 * 1024);
3066 num_pages
*= PAGE_CACHE_SIZE
;
3068 ret
= btrfs_check_data_free_space(inode
, num_pages
);
3072 ret
= btrfs_prealloc_file_range_trans(inode
, trans
, 0, 0, num_pages
,
3073 num_pages
, num_pages
,
3076 dcs
= BTRFS_DC_SETUP
;
3077 btrfs_free_reserved_data_space(inode
, num_pages
);
3082 btrfs_release_path(path
);
3084 spin_lock(&block_group
->lock
);
3085 if (!ret
&& dcs
== BTRFS_DC_SETUP
)
3086 block_group
->cache_generation
= trans
->transid
;
3087 block_group
->disk_cache_state
= dcs
;
3088 spin_unlock(&block_group
->lock
);
3093 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle
*trans
,
3094 struct btrfs_root
*root
)
3096 struct btrfs_block_group_cache
*cache
;
3098 struct btrfs_path
*path
;
3101 path
= btrfs_alloc_path();
3107 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
3109 if (cache
->disk_cache_state
== BTRFS_DC_CLEAR
)
3111 cache
= next_block_group(root
, cache
);
3119 err
= cache_save_setup(cache
, trans
, path
);
3120 last
= cache
->key
.objectid
+ cache
->key
.offset
;
3121 btrfs_put_block_group(cache
);
3126 err
= btrfs_run_delayed_refs(trans
, root
,
3128 if (err
) /* File system offline */
3132 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
3134 if (cache
->disk_cache_state
== BTRFS_DC_CLEAR
) {
3135 btrfs_put_block_group(cache
);
3141 cache
= next_block_group(root
, cache
);
3150 if (cache
->disk_cache_state
== BTRFS_DC_SETUP
)
3151 cache
->disk_cache_state
= BTRFS_DC_NEED_WRITE
;
3153 last
= cache
->key
.objectid
+ cache
->key
.offset
;
3155 err
= write_one_cache_group(trans
, root
, path
, cache
);
3156 if (err
) /* File system offline */
3159 btrfs_put_block_group(cache
);
3164 * I don't think this is needed since we're just marking our
3165 * preallocated extent as written, but just in case it can't
3169 err
= btrfs_run_delayed_refs(trans
, root
,
3171 if (err
) /* File system offline */
3175 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
3178 * Really this shouldn't happen, but it could if we
3179 * couldn't write the entire preallocated extent and
3180 * splitting the extent resulted in a new block.
3183 btrfs_put_block_group(cache
);
3186 if (cache
->disk_cache_state
== BTRFS_DC_NEED_WRITE
)
3188 cache
= next_block_group(root
, cache
);
3197 err
= btrfs_write_out_cache(root
, trans
, cache
, path
);
3200 * If we didn't have an error then the cache state is still
3201 * NEED_WRITE, so we can set it to WRITTEN.
3203 if (!err
&& cache
->disk_cache_state
== BTRFS_DC_NEED_WRITE
)
3204 cache
->disk_cache_state
= BTRFS_DC_WRITTEN
;
3205 last
= cache
->key
.objectid
+ cache
->key
.offset
;
3206 btrfs_put_block_group(cache
);
3210 btrfs_free_path(path
);
3214 int btrfs_extent_readonly(struct btrfs_root
*root
, u64 bytenr
)
3216 struct btrfs_block_group_cache
*block_group
;
3219 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
3220 if (!block_group
|| block_group
->ro
)
3223 btrfs_put_block_group(block_group
);
3227 static int update_space_info(struct btrfs_fs_info
*info
, u64 flags
,
3228 u64 total_bytes
, u64 bytes_used
,
3229 struct btrfs_space_info
**space_info
)
3231 struct btrfs_space_info
*found
;
3235 if (flags
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
3236 BTRFS_BLOCK_GROUP_RAID10
))
3241 found
= __find_space_info(info
, flags
);
3243 spin_lock(&found
->lock
);
3244 found
->total_bytes
+= total_bytes
;
3245 found
->disk_total
+= total_bytes
* factor
;
3246 found
->bytes_used
+= bytes_used
;
3247 found
->disk_used
+= bytes_used
* factor
;
3249 spin_unlock(&found
->lock
);
3250 *space_info
= found
;
3253 found
= kzalloc(sizeof(*found
), GFP_NOFS
);
3257 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++)
3258 INIT_LIST_HEAD(&found
->block_groups
[i
]);
3259 init_rwsem(&found
->groups_sem
);
3260 spin_lock_init(&found
->lock
);
3261 found
->flags
= flags
& BTRFS_BLOCK_GROUP_TYPE_MASK
;
3262 found
->total_bytes
= total_bytes
;
3263 found
->disk_total
= total_bytes
* factor
;
3264 found
->bytes_used
= bytes_used
;
3265 found
->disk_used
= bytes_used
* factor
;
3266 found
->bytes_pinned
= 0;
3267 found
->bytes_reserved
= 0;
3268 found
->bytes_readonly
= 0;
3269 found
->bytes_may_use
= 0;
3271 found
->force_alloc
= CHUNK_ALLOC_NO_FORCE
;
3272 found
->chunk_alloc
= 0;
3274 init_waitqueue_head(&found
->wait
);
3275 *space_info
= found
;
3276 list_add_rcu(&found
->list
, &info
->space_info
);
3277 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3278 info
->data_sinfo
= found
;
3282 static void set_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
3284 u64 extra_flags
= chunk_to_extended(flags
) &
3285 BTRFS_EXTENDED_PROFILE_MASK
;
3287 write_seqlock(&fs_info
->profiles_lock
);
3288 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3289 fs_info
->avail_data_alloc_bits
|= extra_flags
;
3290 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
3291 fs_info
->avail_metadata_alloc_bits
|= extra_flags
;
3292 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
3293 fs_info
->avail_system_alloc_bits
|= extra_flags
;
3294 write_sequnlock(&fs_info
->profiles_lock
);
3298 * returns target flags in extended format or 0 if restripe for this
3299 * chunk_type is not in progress
3301 * should be called with either volume_mutex or balance_lock held
3303 static u64
get_restripe_target(struct btrfs_fs_info
*fs_info
, u64 flags
)
3305 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3311 if (flags
& BTRFS_BLOCK_GROUP_DATA
&&
3312 bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3313 target
= BTRFS_BLOCK_GROUP_DATA
| bctl
->data
.target
;
3314 } else if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
&&
3315 bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3316 target
= BTRFS_BLOCK_GROUP_SYSTEM
| bctl
->sys
.target
;
3317 } else if (flags
& BTRFS_BLOCK_GROUP_METADATA
&&
3318 bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3319 target
= BTRFS_BLOCK_GROUP_METADATA
| bctl
->meta
.target
;
3326 * @flags: available profiles in extended format (see ctree.h)
3328 * Returns reduced profile in chunk format. If profile changing is in
3329 * progress (either running or paused) picks the target profile (if it's
3330 * already available), otherwise falls back to plain reducing.
3332 u64
btrfs_reduce_alloc_profile(struct btrfs_root
*root
, u64 flags
)
3335 * we add in the count of missing devices because we want
3336 * to make sure that any RAID levels on a degraded FS
3337 * continue to be honored.
3339 u64 num_devices
= root
->fs_info
->fs_devices
->rw_devices
+
3340 root
->fs_info
->fs_devices
->missing_devices
;
3345 * see if restripe for this chunk_type is in progress, if so
3346 * try to reduce to the target profile
3348 spin_lock(&root
->fs_info
->balance_lock
);
3349 target
= get_restripe_target(root
->fs_info
, flags
);
3351 /* pick target profile only if it's already available */
3352 if ((flags
& target
) & BTRFS_EXTENDED_PROFILE_MASK
) {
3353 spin_unlock(&root
->fs_info
->balance_lock
);
3354 return extended_to_chunk(target
);
3357 spin_unlock(&root
->fs_info
->balance_lock
);
3359 /* First, mask out the RAID levels which aren't possible */
3360 if (num_devices
== 1)
3361 flags
&= ~(BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID0
|
3362 BTRFS_BLOCK_GROUP_RAID5
);
3363 if (num_devices
< 3)
3364 flags
&= ~BTRFS_BLOCK_GROUP_RAID6
;
3365 if (num_devices
< 4)
3366 flags
&= ~BTRFS_BLOCK_GROUP_RAID10
;
3368 tmp
= flags
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID0
|
3369 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID5
|
3370 BTRFS_BLOCK_GROUP_RAID6
| BTRFS_BLOCK_GROUP_RAID10
);
3373 if (tmp
& BTRFS_BLOCK_GROUP_RAID6
)
3374 tmp
= BTRFS_BLOCK_GROUP_RAID6
;
3375 else if (tmp
& BTRFS_BLOCK_GROUP_RAID5
)
3376 tmp
= BTRFS_BLOCK_GROUP_RAID5
;
3377 else if (tmp
& BTRFS_BLOCK_GROUP_RAID10
)
3378 tmp
= BTRFS_BLOCK_GROUP_RAID10
;
3379 else if (tmp
& BTRFS_BLOCK_GROUP_RAID1
)
3380 tmp
= BTRFS_BLOCK_GROUP_RAID1
;
3381 else if (tmp
& BTRFS_BLOCK_GROUP_RAID0
)
3382 tmp
= BTRFS_BLOCK_GROUP_RAID0
;
3384 return extended_to_chunk(flags
| tmp
);
3387 static u64
get_alloc_profile(struct btrfs_root
*root
, u64 flags
)
3392 seq
= read_seqbegin(&root
->fs_info
->profiles_lock
);
3394 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
3395 flags
|= root
->fs_info
->avail_data_alloc_bits
;
3396 else if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
3397 flags
|= root
->fs_info
->avail_system_alloc_bits
;
3398 else if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
3399 flags
|= root
->fs_info
->avail_metadata_alloc_bits
;
3400 } while (read_seqretry(&root
->fs_info
->profiles_lock
, seq
));
3402 return btrfs_reduce_alloc_profile(root
, flags
);
3405 u64
btrfs_get_alloc_profile(struct btrfs_root
*root
, int data
)
3411 flags
= BTRFS_BLOCK_GROUP_DATA
;
3412 else if (root
== root
->fs_info
->chunk_root
)
3413 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
3415 flags
= BTRFS_BLOCK_GROUP_METADATA
;
3417 ret
= get_alloc_profile(root
, flags
);
3422 * This will check the space that the inode allocates from to make sure we have
3423 * enough space for bytes.
3425 int btrfs_check_data_free_space(struct inode
*inode
, u64 bytes
)
3427 struct btrfs_space_info
*data_sinfo
;
3428 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3429 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3431 int ret
= 0, committed
= 0, alloc_chunk
= 1;
3433 /* make sure bytes are sectorsize aligned */
3434 bytes
= (bytes
+ root
->sectorsize
- 1) & ~((u64
)root
->sectorsize
- 1);
3436 if (root
== root
->fs_info
->tree_root
||
3437 BTRFS_I(inode
)->location
.objectid
== BTRFS_FREE_INO_OBJECTID
) {
3442 data_sinfo
= fs_info
->data_sinfo
;
3447 /* make sure we have enough space to handle the data first */
3448 spin_lock(&data_sinfo
->lock
);
3449 used
= data_sinfo
->bytes_used
+ data_sinfo
->bytes_reserved
+
3450 data_sinfo
->bytes_pinned
+ data_sinfo
->bytes_readonly
+
3451 data_sinfo
->bytes_may_use
;
3453 if (used
+ bytes
> data_sinfo
->total_bytes
) {
3454 struct btrfs_trans_handle
*trans
;
3457 * if we don't have enough free bytes in this space then we need
3458 * to alloc a new chunk.
3460 if (!data_sinfo
->full
&& alloc_chunk
) {
3463 data_sinfo
->force_alloc
= CHUNK_ALLOC_FORCE
;
3464 spin_unlock(&data_sinfo
->lock
);
3466 alloc_target
= btrfs_get_alloc_profile(root
, 1);
3467 trans
= btrfs_join_transaction(root
);
3469 return PTR_ERR(trans
);
3471 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
3473 CHUNK_ALLOC_NO_FORCE
);
3474 btrfs_end_transaction(trans
, root
);
3483 data_sinfo
= fs_info
->data_sinfo
;
3489 * If we have less pinned bytes than we want to allocate then
3490 * don't bother committing the transaction, it won't help us.
3492 if (data_sinfo
->bytes_pinned
< bytes
)
3494 spin_unlock(&data_sinfo
->lock
);
3496 /* commit the current transaction and try again */
3499 !atomic_read(&root
->fs_info
->open_ioctl_trans
)) {
3501 trans
= btrfs_join_transaction(root
);
3503 return PTR_ERR(trans
);
3504 ret
= btrfs_commit_transaction(trans
, root
);
3512 data_sinfo
->bytes_may_use
+= bytes
;
3513 trace_btrfs_space_reservation(root
->fs_info
, "space_info",
3514 data_sinfo
->flags
, bytes
, 1);
3515 spin_unlock(&data_sinfo
->lock
);
3521 * Called if we need to clear a data reservation for this inode.
3523 void btrfs_free_reserved_data_space(struct inode
*inode
, u64 bytes
)
3525 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3526 struct btrfs_space_info
*data_sinfo
;
3528 /* make sure bytes are sectorsize aligned */
3529 bytes
= (bytes
+ root
->sectorsize
- 1) & ~((u64
)root
->sectorsize
- 1);
3531 data_sinfo
= root
->fs_info
->data_sinfo
;
3532 spin_lock(&data_sinfo
->lock
);
3533 data_sinfo
->bytes_may_use
-= bytes
;
3534 trace_btrfs_space_reservation(root
->fs_info
, "space_info",
3535 data_sinfo
->flags
, bytes
, 0);
3536 spin_unlock(&data_sinfo
->lock
);
3539 static void force_metadata_allocation(struct btrfs_fs_info
*info
)
3541 struct list_head
*head
= &info
->space_info
;
3542 struct btrfs_space_info
*found
;
3545 list_for_each_entry_rcu(found
, head
, list
) {
3546 if (found
->flags
& BTRFS_BLOCK_GROUP_METADATA
)
3547 found
->force_alloc
= CHUNK_ALLOC_FORCE
;
3552 static int should_alloc_chunk(struct btrfs_root
*root
,
3553 struct btrfs_space_info
*sinfo
, int force
)
3555 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
3556 u64 num_bytes
= sinfo
->total_bytes
- sinfo
->bytes_readonly
;
3557 u64 num_allocated
= sinfo
->bytes_used
+ sinfo
->bytes_reserved
;
3560 if (force
== CHUNK_ALLOC_FORCE
)
3564 * We need to take into account the global rsv because for all intents
3565 * and purposes it's used space. Don't worry about locking the
3566 * global_rsv, it doesn't change except when the transaction commits.
3568 if (sinfo
->flags
& BTRFS_BLOCK_GROUP_METADATA
)
3569 num_allocated
+= global_rsv
->size
;
3572 * in limited mode, we want to have some free space up to
3573 * about 1% of the FS size.
3575 if (force
== CHUNK_ALLOC_LIMITED
) {
3576 thresh
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
3577 thresh
= max_t(u64
, 64 * 1024 * 1024,
3578 div_factor_fine(thresh
, 1));
3580 if (num_bytes
- num_allocated
< thresh
)
3584 if (num_allocated
+ 2 * 1024 * 1024 < div_factor(num_bytes
, 8))
3589 static u64
get_system_chunk_thresh(struct btrfs_root
*root
, u64 type
)
3593 if (type
& (BTRFS_BLOCK_GROUP_RAID10
|
3594 BTRFS_BLOCK_GROUP_RAID0
|
3595 BTRFS_BLOCK_GROUP_RAID5
|
3596 BTRFS_BLOCK_GROUP_RAID6
))
3597 num_dev
= root
->fs_info
->fs_devices
->rw_devices
;
3598 else if (type
& BTRFS_BLOCK_GROUP_RAID1
)
3601 num_dev
= 1; /* DUP or single */
3603 /* metadata for updaing devices and chunk tree */
3604 return btrfs_calc_trans_metadata_size(root
, num_dev
+ 1);
3607 static void check_system_chunk(struct btrfs_trans_handle
*trans
,
3608 struct btrfs_root
*root
, u64 type
)
3610 struct btrfs_space_info
*info
;
3614 info
= __find_space_info(root
->fs_info
, BTRFS_BLOCK_GROUP_SYSTEM
);
3615 spin_lock(&info
->lock
);
3616 left
= info
->total_bytes
- info
->bytes_used
- info
->bytes_pinned
-
3617 info
->bytes_reserved
- info
->bytes_readonly
;
3618 spin_unlock(&info
->lock
);
3620 thresh
= get_system_chunk_thresh(root
, type
);
3621 if (left
< thresh
&& btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
3622 printk(KERN_INFO
"left=%llu, need=%llu, flags=%llu\n",
3623 left
, thresh
, type
);
3624 dump_space_info(info
, 0, 0);
3627 if (left
< thresh
) {
3630 flags
= btrfs_get_alloc_profile(root
->fs_info
->chunk_root
, 0);
3631 btrfs_alloc_chunk(trans
, root
, flags
);
3635 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
3636 struct btrfs_root
*extent_root
, u64 flags
, int force
)
3638 struct btrfs_space_info
*space_info
;
3639 struct btrfs_fs_info
*fs_info
= extent_root
->fs_info
;
3640 int wait_for_alloc
= 0;
3643 /* Don't re-enter if we're already allocating a chunk */
3644 if (trans
->allocating_chunk
)
3647 space_info
= __find_space_info(extent_root
->fs_info
, flags
);
3649 ret
= update_space_info(extent_root
->fs_info
, flags
,
3651 BUG_ON(ret
); /* -ENOMEM */
3653 BUG_ON(!space_info
); /* Logic error */
3656 spin_lock(&space_info
->lock
);
3657 if (force
< space_info
->force_alloc
)
3658 force
= space_info
->force_alloc
;
3659 if (space_info
->full
) {
3660 spin_unlock(&space_info
->lock
);
3664 if (!should_alloc_chunk(extent_root
, space_info
, force
)) {
3665 spin_unlock(&space_info
->lock
);
3667 } else if (space_info
->chunk_alloc
) {
3670 space_info
->chunk_alloc
= 1;
3673 spin_unlock(&space_info
->lock
);
3675 mutex_lock(&fs_info
->chunk_mutex
);
3678 * The chunk_mutex is held throughout the entirety of a chunk
3679 * allocation, so once we've acquired the chunk_mutex we know that the
3680 * other guy is done and we need to recheck and see if we should
3683 if (wait_for_alloc
) {
3684 mutex_unlock(&fs_info
->chunk_mutex
);
3689 trans
->allocating_chunk
= true;
3692 * If we have mixed data/metadata chunks we want to make sure we keep
3693 * allocating mixed chunks instead of individual chunks.
3695 if (btrfs_mixed_space_info(space_info
))
3696 flags
|= (BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
);
3699 * if we're doing a data chunk, go ahead and make sure that
3700 * we keep a reasonable number of metadata chunks allocated in the
3703 if (flags
& BTRFS_BLOCK_GROUP_DATA
&& fs_info
->metadata_ratio
) {
3704 fs_info
->data_chunk_allocations
++;
3705 if (!(fs_info
->data_chunk_allocations
%
3706 fs_info
->metadata_ratio
))
3707 force_metadata_allocation(fs_info
);
3711 * Check if we have enough space in SYSTEM chunk because we may need
3712 * to update devices.
3714 check_system_chunk(trans
, extent_root
, flags
);
3716 ret
= btrfs_alloc_chunk(trans
, extent_root
, flags
);
3717 trans
->allocating_chunk
= false;
3718 if (ret
< 0 && ret
!= -ENOSPC
)
3721 spin_lock(&space_info
->lock
);
3723 space_info
->full
= 1;
3727 space_info
->force_alloc
= CHUNK_ALLOC_NO_FORCE
;
3728 space_info
->chunk_alloc
= 0;
3729 spin_unlock(&space_info
->lock
);
3731 mutex_unlock(&fs_info
->chunk_mutex
);
3735 static int can_overcommit(struct btrfs_root
*root
,
3736 struct btrfs_space_info
*space_info
, u64 bytes
,
3737 enum btrfs_reserve_flush_enum flush
)
3739 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
3740 u64 profile
= btrfs_get_alloc_profile(root
, 0);
3746 used
= space_info
->bytes_used
+ space_info
->bytes_reserved
+
3747 space_info
->bytes_pinned
+ space_info
->bytes_readonly
;
3749 spin_lock(&global_rsv
->lock
);
3750 rsv_size
= global_rsv
->size
;
3751 spin_unlock(&global_rsv
->lock
);
3754 * We only want to allow over committing if we have lots of actual space
3755 * free, but if we don't have enough space to handle the global reserve
3756 * space then we could end up having a real enospc problem when trying
3757 * to allocate a chunk or some other such important allocation.
3760 if (used
+ rsv_size
>= space_info
->total_bytes
)
3763 used
+= space_info
->bytes_may_use
;
3765 spin_lock(&root
->fs_info
->free_chunk_lock
);
3766 avail
= root
->fs_info
->free_chunk_space
;
3767 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3770 * If we have dup, raid1 or raid10 then only half of the free
3771 * space is actually useable. For raid56, the space info used
3772 * doesn't include the parity drive, so we don't have to
3775 if (profile
& (BTRFS_BLOCK_GROUP_DUP
|
3776 BTRFS_BLOCK_GROUP_RAID1
|
3777 BTRFS_BLOCK_GROUP_RAID10
))
3780 to_add
= space_info
->total_bytes
;
3783 * If we aren't flushing all things, let us overcommit up to
3784 * 1/2th of the space. If we can flush, don't let us overcommit
3785 * too much, let it overcommit up to 1/8 of the space.
3787 if (flush
== BTRFS_RESERVE_FLUSH_ALL
)
3793 * Limit the overcommit to the amount of free space we could possibly
3794 * allocate for chunks.
3796 to_add
= min(avail
, to_add
);
3798 if (used
+ bytes
< space_info
->total_bytes
+ to_add
)
3803 static inline int writeback_inodes_sb_nr_if_idle_safe(struct super_block
*sb
,
3804 unsigned long nr_pages
,
3805 enum wb_reason reason
)
3807 /* the flusher is dealing with the dirty inodes now. */
3808 if (writeback_in_progress(sb
->s_bdi
))
3811 if (down_read_trylock(&sb
->s_umount
)) {
3812 writeback_inodes_sb_nr(sb
, nr_pages
, reason
);
3813 up_read(&sb
->s_umount
);
3820 void btrfs_writeback_inodes_sb_nr(struct btrfs_root
*root
,
3821 unsigned long nr_pages
)
3823 struct super_block
*sb
= root
->fs_info
->sb
;
3826 /* If we can not start writeback, just sync all the delalloc file. */
3827 started
= writeback_inodes_sb_nr_if_idle_safe(sb
, nr_pages
,
3828 WB_REASON_FS_FREE_SPACE
);
3831 * We needn't worry the filesystem going from r/w to r/o though
3832 * we don't acquire ->s_umount mutex, because the filesystem
3833 * should guarantee the delalloc inodes list be empty after
3834 * the filesystem is readonly(all dirty pages are written to
3837 btrfs_start_delalloc_inodes(root
, 0);
3838 btrfs_wait_ordered_extents(root
, 0);
3843 * shrink metadata reservation for delalloc
3845 static void shrink_delalloc(struct btrfs_root
*root
, u64 to_reclaim
, u64 orig
,
3848 struct btrfs_block_rsv
*block_rsv
;
3849 struct btrfs_space_info
*space_info
;
3850 struct btrfs_trans_handle
*trans
;
3854 unsigned long nr_pages
= (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT
;
3856 enum btrfs_reserve_flush_enum flush
;
3858 trans
= (struct btrfs_trans_handle
*)current
->journal_info
;
3859 block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
3860 space_info
= block_rsv
->space_info
;
3863 delalloc_bytes
= percpu_counter_sum_positive(
3864 &root
->fs_info
->delalloc_bytes
);
3865 if (delalloc_bytes
== 0) {
3868 btrfs_wait_ordered_extents(root
, 0);
3872 while (delalloc_bytes
&& loops
< 3) {
3873 max_reclaim
= min(delalloc_bytes
, to_reclaim
);
3874 nr_pages
= max_reclaim
>> PAGE_CACHE_SHIFT
;
3875 btrfs_writeback_inodes_sb_nr(root
, nr_pages
);
3877 * We need to wait for the async pages to actually start before
3880 wait_event(root
->fs_info
->async_submit_wait
,
3881 !atomic_read(&root
->fs_info
->async_delalloc_pages
));
3884 flush
= BTRFS_RESERVE_FLUSH_ALL
;
3886 flush
= BTRFS_RESERVE_NO_FLUSH
;
3887 spin_lock(&space_info
->lock
);
3888 if (can_overcommit(root
, space_info
, orig
, flush
)) {
3889 spin_unlock(&space_info
->lock
);
3892 spin_unlock(&space_info
->lock
);
3895 if (wait_ordered
&& !trans
) {
3896 btrfs_wait_ordered_extents(root
, 0);
3898 time_left
= schedule_timeout_killable(1);
3903 delalloc_bytes
= percpu_counter_sum_positive(
3904 &root
->fs_info
->delalloc_bytes
);
3909 * maybe_commit_transaction - possibly commit the transaction if its ok to
3910 * @root - the root we're allocating for
3911 * @bytes - the number of bytes we want to reserve
3912 * @force - force the commit
3914 * This will check to make sure that committing the transaction will actually
3915 * get us somewhere and then commit the transaction if it does. Otherwise it
3916 * will return -ENOSPC.
3918 static int may_commit_transaction(struct btrfs_root
*root
,
3919 struct btrfs_space_info
*space_info
,
3920 u64 bytes
, int force
)
3922 struct btrfs_block_rsv
*delayed_rsv
= &root
->fs_info
->delayed_block_rsv
;
3923 struct btrfs_trans_handle
*trans
;
3925 trans
= (struct btrfs_trans_handle
*)current
->journal_info
;
3932 /* See if there is enough pinned space to make this reservation */
3933 spin_lock(&space_info
->lock
);
3934 if (space_info
->bytes_pinned
>= bytes
) {
3935 spin_unlock(&space_info
->lock
);
3938 spin_unlock(&space_info
->lock
);
3941 * See if there is some space in the delayed insertion reservation for
3944 if (space_info
!= delayed_rsv
->space_info
)
3947 spin_lock(&space_info
->lock
);
3948 spin_lock(&delayed_rsv
->lock
);
3949 if (space_info
->bytes_pinned
+ delayed_rsv
->size
< bytes
) {
3950 spin_unlock(&delayed_rsv
->lock
);
3951 spin_unlock(&space_info
->lock
);
3954 spin_unlock(&delayed_rsv
->lock
);
3955 spin_unlock(&space_info
->lock
);
3958 trans
= btrfs_join_transaction(root
);
3962 return btrfs_commit_transaction(trans
, root
);
3966 FLUSH_DELAYED_ITEMS_NR
= 1,
3967 FLUSH_DELAYED_ITEMS
= 2,
3969 FLUSH_DELALLOC_WAIT
= 4,
3974 static int flush_space(struct btrfs_root
*root
,
3975 struct btrfs_space_info
*space_info
, u64 num_bytes
,
3976 u64 orig_bytes
, int state
)
3978 struct btrfs_trans_handle
*trans
;
3983 case FLUSH_DELAYED_ITEMS_NR
:
3984 case FLUSH_DELAYED_ITEMS
:
3985 if (state
== FLUSH_DELAYED_ITEMS_NR
) {
3986 u64 bytes
= btrfs_calc_trans_metadata_size(root
, 1);
3988 nr
= (int)div64_u64(num_bytes
, bytes
);
3995 trans
= btrfs_join_transaction(root
);
3996 if (IS_ERR(trans
)) {
3997 ret
= PTR_ERR(trans
);
4000 ret
= btrfs_run_delayed_items_nr(trans
, root
, nr
);
4001 btrfs_end_transaction(trans
, root
);
4003 case FLUSH_DELALLOC
:
4004 case FLUSH_DELALLOC_WAIT
:
4005 shrink_delalloc(root
, num_bytes
, orig_bytes
,
4006 state
== FLUSH_DELALLOC_WAIT
);
4009 trans
= btrfs_join_transaction(root
);
4010 if (IS_ERR(trans
)) {
4011 ret
= PTR_ERR(trans
);
4014 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
4015 btrfs_get_alloc_profile(root
, 0),
4016 CHUNK_ALLOC_NO_FORCE
);
4017 btrfs_end_transaction(trans
, root
);
4022 ret
= may_commit_transaction(root
, space_info
, orig_bytes
, 0);
4032 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
4033 * @root - the root we're allocating for
4034 * @block_rsv - the block_rsv we're allocating for
4035 * @orig_bytes - the number of bytes we want
4036 * @flush - whether or not we can flush to make our reservation
4038 * This will reserve orgi_bytes number of bytes from the space info associated
4039 * with the block_rsv. If there is not enough space it will make an attempt to
4040 * flush out space to make room. It will do this by flushing delalloc if
4041 * possible or committing the transaction. If flush is 0 then no attempts to
4042 * regain reservations will be made and this will fail if there is not enough
4045 static int reserve_metadata_bytes(struct btrfs_root
*root
,
4046 struct btrfs_block_rsv
*block_rsv
,
4048 enum btrfs_reserve_flush_enum flush
)
4050 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
4052 u64 num_bytes
= orig_bytes
;
4053 int flush_state
= FLUSH_DELAYED_ITEMS_NR
;
4055 bool flushing
= false;
4059 spin_lock(&space_info
->lock
);
4061 * We only want to wait if somebody other than us is flushing and we
4062 * are actually allowed to flush all things.
4064 while (flush
== BTRFS_RESERVE_FLUSH_ALL
&& !flushing
&&
4065 space_info
->flush
) {
4066 spin_unlock(&space_info
->lock
);
4068 * If we have a trans handle we can't wait because the flusher
4069 * may have to commit the transaction, which would mean we would
4070 * deadlock since we are waiting for the flusher to finish, but
4071 * hold the current transaction open.
4073 if (current
->journal_info
)
4075 ret
= wait_event_killable(space_info
->wait
, !space_info
->flush
);
4076 /* Must have been killed, return */
4080 spin_lock(&space_info
->lock
);
4084 used
= space_info
->bytes_used
+ space_info
->bytes_reserved
+
4085 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
4086 space_info
->bytes_may_use
;
4089 * The idea here is that we've not already over-reserved the block group
4090 * then we can go ahead and save our reservation first and then start
4091 * flushing if we need to. Otherwise if we've already overcommitted
4092 * lets start flushing stuff first and then come back and try to make
4095 if (used
<= space_info
->total_bytes
) {
4096 if (used
+ orig_bytes
<= space_info
->total_bytes
) {
4097 space_info
->bytes_may_use
+= orig_bytes
;
4098 trace_btrfs_space_reservation(root
->fs_info
,
4099 "space_info", space_info
->flags
, orig_bytes
, 1);
4103 * Ok set num_bytes to orig_bytes since we aren't
4104 * overocmmitted, this way we only try and reclaim what
4107 num_bytes
= orig_bytes
;
4111 * Ok we're over committed, set num_bytes to the overcommitted
4112 * amount plus the amount of bytes that we need for this
4115 num_bytes
= used
- space_info
->total_bytes
+
4119 if (ret
&& can_overcommit(root
, space_info
, orig_bytes
, flush
)) {
4120 space_info
->bytes_may_use
+= orig_bytes
;
4121 trace_btrfs_space_reservation(root
->fs_info
, "space_info",
4122 space_info
->flags
, orig_bytes
,
4128 * Couldn't make our reservation, save our place so while we're trying
4129 * to reclaim space we can actually use it instead of somebody else
4130 * stealing it from us.
4132 * We make the other tasks wait for the flush only when we can flush
4135 if (ret
&& flush
!= BTRFS_RESERVE_NO_FLUSH
) {
4137 space_info
->flush
= 1;
4140 spin_unlock(&space_info
->lock
);
4142 if (!ret
|| flush
== BTRFS_RESERVE_NO_FLUSH
)
4145 ret
= flush_space(root
, space_info
, num_bytes
, orig_bytes
,
4150 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4151 * would happen. So skip delalloc flush.
4153 if (flush
== BTRFS_RESERVE_FLUSH_LIMIT
&&
4154 (flush_state
== FLUSH_DELALLOC
||
4155 flush_state
== FLUSH_DELALLOC_WAIT
))
4156 flush_state
= ALLOC_CHUNK
;
4160 else if (flush
== BTRFS_RESERVE_FLUSH_LIMIT
&&
4161 flush_state
< COMMIT_TRANS
)
4163 else if (flush
== BTRFS_RESERVE_FLUSH_ALL
&&
4164 flush_state
<= COMMIT_TRANS
)
4168 if (ret
== -ENOSPC
&&
4169 unlikely(root
->orphan_cleanup_state
== ORPHAN_CLEANUP_STARTED
)) {
4170 struct btrfs_block_rsv
*global_rsv
=
4171 &root
->fs_info
->global_block_rsv
;
4173 if (block_rsv
!= global_rsv
&&
4174 !block_rsv_use_bytes(global_rsv
, orig_bytes
))
4178 spin_lock(&space_info
->lock
);
4179 space_info
->flush
= 0;
4180 wake_up_all(&space_info
->wait
);
4181 spin_unlock(&space_info
->lock
);
4186 static struct btrfs_block_rsv
*get_block_rsv(
4187 const struct btrfs_trans_handle
*trans
,
4188 const struct btrfs_root
*root
)
4190 struct btrfs_block_rsv
*block_rsv
= NULL
;
4193 block_rsv
= trans
->block_rsv
;
4195 if (root
== root
->fs_info
->csum_root
&& trans
->adding_csums
)
4196 block_rsv
= trans
->block_rsv
;
4199 block_rsv
= root
->block_rsv
;
4202 block_rsv
= &root
->fs_info
->empty_block_rsv
;
4207 static int block_rsv_use_bytes(struct btrfs_block_rsv
*block_rsv
,
4211 spin_lock(&block_rsv
->lock
);
4212 if (block_rsv
->reserved
>= num_bytes
) {
4213 block_rsv
->reserved
-= num_bytes
;
4214 if (block_rsv
->reserved
< block_rsv
->size
)
4215 block_rsv
->full
= 0;
4218 spin_unlock(&block_rsv
->lock
);
4222 static void block_rsv_add_bytes(struct btrfs_block_rsv
*block_rsv
,
4223 u64 num_bytes
, int update_size
)
4225 spin_lock(&block_rsv
->lock
);
4226 block_rsv
->reserved
+= num_bytes
;
4228 block_rsv
->size
+= num_bytes
;
4229 else if (block_rsv
->reserved
>= block_rsv
->size
)
4230 block_rsv
->full
= 1;
4231 spin_unlock(&block_rsv
->lock
);
4234 static void block_rsv_release_bytes(struct btrfs_fs_info
*fs_info
,
4235 struct btrfs_block_rsv
*block_rsv
,
4236 struct btrfs_block_rsv
*dest
, u64 num_bytes
)
4238 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
4240 spin_lock(&block_rsv
->lock
);
4241 if (num_bytes
== (u64
)-1)
4242 num_bytes
= block_rsv
->size
;
4243 block_rsv
->size
-= num_bytes
;
4244 if (block_rsv
->reserved
>= block_rsv
->size
) {
4245 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
4246 block_rsv
->reserved
= block_rsv
->size
;
4247 block_rsv
->full
= 1;
4251 spin_unlock(&block_rsv
->lock
);
4253 if (num_bytes
> 0) {
4255 spin_lock(&dest
->lock
);
4259 bytes_to_add
= dest
->size
- dest
->reserved
;
4260 bytes_to_add
= min(num_bytes
, bytes_to_add
);
4261 dest
->reserved
+= bytes_to_add
;
4262 if (dest
->reserved
>= dest
->size
)
4264 num_bytes
-= bytes_to_add
;
4266 spin_unlock(&dest
->lock
);
4269 spin_lock(&space_info
->lock
);
4270 space_info
->bytes_may_use
-= num_bytes
;
4271 trace_btrfs_space_reservation(fs_info
, "space_info",
4272 space_info
->flags
, num_bytes
, 0);
4273 space_info
->reservation_progress
++;
4274 spin_unlock(&space_info
->lock
);
4279 static int block_rsv_migrate_bytes(struct btrfs_block_rsv
*src
,
4280 struct btrfs_block_rsv
*dst
, u64 num_bytes
)
4284 ret
= block_rsv_use_bytes(src
, num_bytes
);
4288 block_rsv_add_bytes(dst
, num_bytes
, 1);
4292 void btrfs_init_block_rsv(struct btrfs_block_rsv
*rsv
, unsigned short type
)
4294 memset(rsv
, 0, sizeof(*rsv
));
4295 spin_lock_init(&rsv
->lock
);
4299 struct btrfs_block_rsv
*btrfs_alloc_block_rsv(struct btrfs_root
*root
,
4300 unsigned short type
)
4302 struct btrfs_block_rsv
*block_rsv
;
4303 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4305 block_rsv
= kmalloc(sizeof(*block_rsv
), GFP_NOFS
);
4309 btrfs_init_block_rsv(block_rsv
, type
);
4310 block_rsv
->space_info
= __find_space_info(fs_info
,
4311 BTRFS_BLOCK_GROUP_METADATA
);
4315 void btrfs_free_block_rsv(struct btrfs_root
*root
,
4316 struct btrfs_block_rsv
*rsv
)
4320 btrfs_block_rsv_release(root
, rsv
, (u64
)-1);
4324 int btrfs_block_rsv_add(struct btrfs_root
*root
,
4325 struct btrfs_block_rsv
*block_rsv
, u64 num_bytes
,
4326 enum btrfs_reserve_flush_enum flush
)
4333 ret
= reserve_metadata_bytes(root
, block_rsv
, num_bytes
, flush
);
4335 block_rsv_add_bytes(block_rsv
, num_bytes
, 1);
4342 int btrfs_block_rsv_check(struct btrfs_root
*root
,
4343 struct btrfs_block_rsv
*block_rsv
, int min_factor
)
4351 spin_lock(&block_rsv
->lock
);
4352 num_bytes
= div_factor(block_rsv
->size
, min_factor
);
4353 if (block_rsv
->reserved
>= num_bytes
)
4355 spin_unlock(&block_rsv
->lock
);
4360 int btrfs_block_rsv_refill(struct btrfs_root
*root
,
4361 struct btrfs_block_rsv
*block_rsv
, u64 min_reserved
,
4362 enum btrfs_reserve_flush_enum flush
)
4370 spin_lock(&block_rsv
->lock
);
4371 num_bytes
= min_reserved
;
4372 if (block_rsv
->reserved
>= num_bytes
)
4375 num_bytes
-= block_rsv
->reserved
;
4376 spin_unlock(&block_rsv
->lock
);
4381 ret
= reserve_metadata_bytes(root
, block_rsv
, num_bytes
, flush
);
4383 block_rsv_add_bytes(block_rsv
, num_bytes
, 0);
4390 int btrfs_block_rsv_migrate(struct btrfs_block_rsv
*src_rsv
,
4391 struct btrfs_block_rsv
*dst_rsv
,
4394 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
4397 void btrfs_block_rsv_release(struct btrfs_root
*root
,
4398 struct btrfs_block_rsv
*block_rsv
,
4401 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
4402 if (global_rsv
->full
|| global_rsv
== block_rsv
||
4403 block_rsv
->space_info
!= global_rsv
->space_info
)
4405 block_rsv_release_bytes(root
->fs_info
, block_rsv
, global_rsv
,
4410 * helper to calculate size of global block reservation.
4411 * the desired value is sum of space used by extent tree,
4412 * checksum tree and root tree
4414 static u64
calc_global_metadata_size(struct btrfs_fs_info
*fs_info
)
4416 struct btrfs_space_info
*sinfo
;
4420 int csum_size
= btrfs_super_csum_size(fs_info
->super_copy
);
4422 sinfo
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_DATA
);
4423 spin_lock(&sinfo
->lock
);
4424 data_used
= sinfo
->bytes_used
;
4425 spin_unlock(&sinfo
->lock
);
4427 sinfo
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
4428 spin_lock(&sinfo
->lock
);
4429 if (sinfo
->flags
& BTRFS_BLOCK_GROUP_DATA
)
4431 meta_used
= sinfo
->bytes_used
;
4432 spin_unlock(&sinfo
->lock
);
4434 num_bytes
= (data_used
>> fs_info
->sb
->s_blocksize_bits
) *
4436 num_bytes
+= div64_u64(data_used
+ meta_used
, 50);
4438 if (num_bytes
* 3 > meta_used
)
4439 num_bytes
= div64_u64(meta_used
, 3);
4441 return ALIGN(num_bytes
, fs_info
->extent_root
->leafsize
<< 10);
4444 static void update_global_block_rsv(struct btrfs_fs_info
*fs_info
)
4446 struct btrfs_block_rsv
*block_rsv
= &fs_info
->global_block_rsv
;
4447 struct btrfs_space_info
*sinfo
= block_rsv
->space_info
;
4450 num_bytes
= calc_global_metadata_size(fs_info
);
4452 spin_lock(&sinfo
->lock
);
4453 spin_lock(&block_rsv
->lock
);
4455 block_rsv
->size
= num_bytes
;
4457 num_bytes
= sinfo
->bytes_used
+ sinfo
->bytes_pinned
+
4458 sinfo
->bytes_reserved
+ sinfo
->bytes_readonly
+
4459 sinfo
->bytes_may_use
;
4461 if (sinfo
->total_bytes
> num_bytes
) {
4462 num_bytes
= sinfo
->total_bytes
- num_bytes
;
4463 block_rsv
->reserved
+= num_bytes
;
4464 sinfo
->bytes_may_use
+= num_bytes
;
4465 trace_btrfs_space_reservation(fs_info
, "space_info",
4466 sinfo
->flags
, num_bytes
, 1);
4469 if (block_rsv
->reserved
>= block_rsv
->size
) {
4470 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
4471 sinfo
->bytes_may_use
-= num_bytes
;
4472 trace_btrfs_space_reservation(fs_info
, "space_info",
4473 sinfo
->flags
, num_bytes
, 0);
4474 sinfo
->reservation_progress
++;
4475 block_rsv
->reserved
= block_rsv
->size
;
4476 block_rsv
->full
= 1;
4479 spin_unlock(&block_rsv
->lock
);
4480 spin_unlock(&sinfo
->lock
);
4483 static void init_global_block_rsv(struct btrfs_fs_info
*fs_info
)
4485 struct btrfs_space_info
*space_info
;
4487 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_SYSTEM
);
4488 fs_info
->chunk_block_rsv
.space_info
= space_info
;
4490 space_info
= __find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
4491 fs_info
->global_block_rsv
.space_info
= space_info
;
4492 fs_info
->delalloc_block_rsv
.space_info
= space_info
;
4493 fs_info
->trans_block_rsv
.space_info
= space_info
;
4494 fs_info
->empty_block_rsv
.space_info
= space_info
;
4495 fs_info
->delayed_block_rsv
.space_info
= space_info
;
4497 fs_info
->extent_root
->block_rsv
= &fs_info
->global_block_rsv
;
4498 fs_info
->csum_root
->block_rsv
= &fs_info
->global_block_rsv
;
4499 fs_info
->dev_root
->block_rsv
= &fs_info
->global_block_rsv
;
4500 fs_info
->tree_root
->block_rsv
= &fs_info
->global_block_rsv
;
4501 fs_info
->chunk_root
->block_rsv
= &fs_info
->chunk_block_rsv
;
4503 update_global_block_rsv(fs_info
);
4506 static void release_global_block_rsv(struct btrfs_fs_info
*fs_info
)
4508 block_rsv_release_bytes(fs_info
, &fs_info
->global_block_rsv
, NULL
,
4510 WARN_ON(fs_info
->delalloc_block_rsv
.size
> 0);
4511 WARN_ON(fs_info
->delalloc_block_rsv
.reserved
> 0);
4512 WARN_ON(fs_info
->trans_block_rsv
.size
> 0);
4513 WARN_ON(fs_info
->trans_block_rsv
.reserved
> 0);
4514 WARN_ON(fs_info
->chunk_block_rsv
.size
> 0);
4515 WARN_ON(fs_info
->chunk_block_rsv
.reserved
> 0);
4516 WARN_ON(fs_info
->delayed_block_rsv
.size
> 0);
4517 WARN_ON(fs_info
->delayed_block_rsv
.reserved
> 0);
4520 void btrfs_trans_release_metadata(struct btrfs_trans_handle
*trans
,
4521 struct btrfs_root
*root
)
4523 if (!trans
->block_rsv
)
4526 if (!trans
->bytes_reserved
)
4529 trace_btrfs_space_reservation(root
->fs_info
, "transaction",
4530 trans
->transid
, trans
->bytes_reserved
, 0);
4531 btrfs_block_rsv_release(root
, trans
->block_rsv
, trans
->bytes_reserved
);
4532 trans
->bytes_reserved
= 0;
4535 /* Can only return 0 or -ENOSPC */
4536 int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle
*trans
,
4537 struct inode
*inode
)
4539 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4540 struct btrfs_block_rsv
*src_rsv
= get_block_rsv(trans
, root
);
4541 struct btrfs_block_rsv
*dst_rsv
= root
->orphan_block_rsv
;
4544 * We need to hold space in order to delete our orphan item once we've
4545 * added it, so this takes the reservation so we can release it later
4546 * when we are truly done with the orphan item.
4548 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 1);
4549 trace_btrfs_space_reservation(root
->fs_info
, "orphan",
4550 btrfs_ino(inode
), num_bytes
, 1);
4551 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
4554 void btrfs_orphan_release_metadata(struct inode
*inode
)
4556 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4557 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 1);
4558 trace_btrfs_space_reservation(root
->fs_info
, "orphan",
4559 btrfs_ino(inode
), num_bytes
, 0);
4560 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
, num_bytes
);
4563 int btrfs_snap_reserve_metadata(struct btrfs_trans_handle
*trans
,
4564 struct btrfs_pending_snapshot
*pending
)
4566 struct btrfs_root
*root
= pending
->root
;
4567 struct btrfs_block_rsv
*src_rsv
= get_block_rsv(trans
, root
);
4568 struct btrfs_block_rsv
*dst_rsv
= &pending
->block_rsv
;
4570 * two for root back/forward refs, two for directory entries,
4571 * one for root of the snapshot and one for parent inode.
4573 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 6);
4574 dst_rsv
->space_info
= src_rsv
->space_info
;
4575 return block_rsv_migrate_bytes(src_rsv
, dst_rsv
, num_bytes
);
4579 * drop_outstanding_extent - drop an outstanding extent
4580 * @inode: the inode we're dropping the extent for
4582 * This is called when we are freeing up an outstanding extent, either called
4583 * after an error or after an extent is written. This will return the number of
4584 * reserved extents that need to be freed. This must be called with
4585 * BTRFS_I(inode)->lock held.
4587 static unsigned drop_outstanding_extent(struct inode
*inode
)
4589 unsigned drop_inode_space
= 0;
4590 unsigned dropped_extents
= 0;
4592 BUG_ON(!BTRFS_I(inode
)->outstanding_extents
);
4593 BTRFS_I(inode
)->outstanding_extents
--;
4595 if (BTRFS_I(inode
)->outstanding_extents
== 0 &&
4596 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED
,
4597 &BTRFS_I(inode
)->runtime_flags
))
4598 drop_inode_space
= 1;
4601 * If we have more or the same amount of outsanding extents than we have
4602 * reserved then we need to leave the reserved extents count alone.
4604 if (BTRFS_I(inode
)->outstanding_extents
>=
4605 BTRFS_I(inode
)->reserved_extents
)
4606 return drop_inode_space
;
4608 dropped_extents
= BTRFS_I(inode
)->reserved_extents
-
4609 BTRFS_I(inode
)->outstanding_extents
;
4610 BTRFS_I(inode
)->reserved_extents
-= dropped_extents
;
4611 return dropped_extents
+ drop_inode_space
;
4615 * calc_csum_metadata_size - return the amount of metada space that must be
4616 * reserved/free'd for the given bytes.
4617 * @inode: the inode we're manipulating
4618 * @num_bytes: the number of bytes in question
4619 * @reserve: 1 if we are reserving space, 0 if we are freeing space
4621 * This adjusts the number of csum_bytes in the inode and then returns the
4622 * correct amount of metadata that must either be reserved or freed. We
4623 * calculate how many checksums we can fit into one leaf and then divide the
4624 * number of bytes that will need to be checksumed by this value to figure out
4625 * how many checksums will be required. If we are adding bytes then the number
4626 * may go up and we will return the number of additional bytes that must be
4627 * reserved. If it is going down we will return the number of bytes that must
4630 * This must be called with BTRFS_I(inode)->lock held.
4632 static u64
calc_csum_metadata_size(struct inode
*inode
, u64 num_bytes
,
4635 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4637 int num_csums_per_leaf
;
4641 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
&&
4642 BTRFS_I(inode
)->csum_bytes
== 0)
4645 old_csums
= (int)div64_u64(BTRFS_I(inode
)->csum_bytes
, root
->sectorsize
);
4647 BTRFS_I(inode
)->csum_bytes
+= num_bytes
;
4649 BTRFS_I(inode
)->csum_bytes
-= num_bytes
;
4650 csum_size
= BTRFS_LEAF_DATA_SIZE(root
) - sizeof(struct btrfs_item
);
4651 num_csums_per_leaf
= (int)div64_u64(csum_size
,
4652 sizeof(struct btrfs_csum_item
) +
4653 sizeof(struct btrfs_disk_key
));
4654 num_csums
= (int)div64_u64(BTRFS_I(inode
)->csum_bytes
, root
->sectorsize
);
4655 num_csums
= num_csums
+ num_csums_per_leaf
- 1;
4656 num_csums
= num_csums
/ num_csums_per_leaf
;
4658 old_csums
= old_csums
+ num_csums_per_leaf
- 1;
4659 old_csums
= old_csums
/ num_csums_per_leaf
;
4661 /* No change, no need to reserve more */
4662 if (old_csums
== num_csums
)
4666 return btrfs_calc_trans_metadata_size(root
,
4667 num_csums
- old_csums
);
4669 return btrfs_calc_trans_metadata_size(root
, old_csums
- num_csums
);
4672 int btrfs_delalloc_reserve_metadata(struct inode
*inode
, u64 num_bytes
)
4674 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4675 struct btrfs_block_rsv
*block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
4678 unsigned nr_extents
= 0;
4679 int extra_reserve
= 0;
4680 enum btrfs_reserve_flush_enum flush
= BTRFS_RESERVE_FLUSH_ALL
;
4682 bool delalloc_lock
= true;
4684 /* If we are a free space inode we need to not flush since we will be in
4685 * the middle of a transaction commit. We also don't need the delalloc
4686 * mutex since we won't race with anybody. We need this mostly to make
4687 * lockdep shut its filthy mouth.
4689 if (btrfs_is_free_space_inode(inode
)) {
4690 flush
= BTRFS_RESERVE_NO_FLUSH
;
4691 delalloc_lock
= false;
4694 if (flush
!= BTRFS_RESERVE_NO_FLUSH
&&
4695 btrfs_transaction_in_commit(root
->fs_info
))
4696 schedule_timeout(1);
4699 mutex_lock(&BTRFS_I(inode
)->delalloc_mutex
);
4701 num_bytes
= ALIGN(num_bytes
, root
->sectorsize
);
4703 spin_lock(&BTRFS_I(inode
)->lock
);
4704 BTRFS_I(inode
)->outstanding_extents
++;
4706 if (BTRFS_I(inode
)->outstanding_extents
>
4707 BTRFS_I(inode
)->reserved_extents
)
4708 nr_extents
= BTRFS_I(inode
)->outstanding_extents
-
4709 BTRFS_I(inode
)->reserved_extents
;
4712 * Add an item to reserve for updating the inode when we complete the
4715 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED
,
4716 &BTRFS_I(inode
)->runtime_flags
)) {
4721 to_reserve
= btrfs_calc_trans_metadata_size(root
, nr_extents
);
4722 to_reserve
+= calc_csum_metadata_size(inode
, num_bytes
, 1);
4723 csum_bytes
= BTRFS_I(inode
)->csum_bytes
;
4724 spin_unlock(&BTRFS_I(inode
)->lock
);
4726 if (root
->fs_info
->quota_enabled
)
4727 ret
= btrfs_qgroup_reserve(root
, num_bytes
+
4728 nr_extents
* root
->leafsize
);
4731 * ret != 0 here means the qgroup reservation failed, we go straight to
4732 * the shared error handling then.
4735 ret
= reserve_metadata_bytes(root
, block_rsv
,
4742 spin_lock(&BTRFS_I(inode
)->lock
);
4743 dropped
= drop_outstanding_extent(inode
);
4745 * If the inodes csum_bytes is the same as the original
4746 * csum_bytes then we know we haven't raced with any free()ers
4747 * so we can just reduce our inodes csum bytes and carry on.
4748 * Otherwise we have to do the normal free thing to account for
4749 * the case that the free side didn't free up its reserve
4750 * because of this outstanding reservation.
4752 if (BTRFS_I(inode
)->csum_bytes
== csum_bytes
)
4753 calc_csum_metadata_size(inode
, num_bytes
, 0);
4755 to_free
= calc_csum_metadata_size(inode
, num_bytes
, 0);
4756 spin_unlock(&BTRFS_I(inode
)->lock
);
4758 to_free
+= btrfs_calc_trans_metadata_size(root
, dropped
);
4761 btrfs_block_rsv_release(root
, block_rsv
, to_free
);
4762 trace_btrfs_space_reservation(root
->fs_info
,
4767 if (root
->fs_info
->quota_enabled
) {
4768 btrfs_qgroup_free(root
, num_bytes
+
4769 nr_extents
* root
->leafsize
);
4772 mutex_unlock(&BTRFS_I(inode
)->delalloc_mutex
);
4776 spin_lock(&BTRFS_I(inode
)->lock
);
4777 if (extra_reserve
) {
4778 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED
,
4779 &BTRFS_I(inode
)->runtime_flags
);
4782 BTRFS_I(inode
)->reserved_extents
+= nr_extents
;
4783 spin_unlock(&BTRFS_I(inode
)->lock
);
4786 mutex_unlock(&BTRFS_I(inode
)->delalloc_mutex
);
4789 trace_btrfs_space_reservation(root
->fs_info
,"delalloc",
4790 btrfs_ino(inode
), to_reserve
, 1);
4791 block_rsv_add_bytes(block_rsv
, to_reserve
, 1);
4797 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
4798 * @inode: the inode to release the reservation for
4799 * @num_bytes: the number of bytes we're releasing
4801 * This will release the metadata reservation for an inode. This can be called
4802 * once we complete IO for a given set of bytes to release their metadata
4805 void btrfs_delalloc_release_metadata(struct inode
*inode
, u64 num_bytes
)
4807 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4811 num_bytes
= ALIGN(num_bytes
, root
->sectorsize
);
4812 spin_lock(&BTRFS_I(inode
)->lock
);
4813 dropped
= drop_outstanding_extent(inode
);
4816 to_free
= calc_csum_metadata_size(inode
, num_bytes
, 0);
4817 spin_unlock(&BTRFS_I(inode
)->lock
);
4819 to_free
+= btrfs_calc_trans_metadata_size(root
, dropped
);
4821 trace_btrfs_space_reservation(root
->fs_info
, "delalloc",
4822 btrfs_ino(inode
), to_free
, 0);
4823 if (root
->fs_info
->quota_enabled
) {
4824 btrfs_qgroup_free(root
, num_bytes
+
4825 dropped
* root
->leafsize
);
4828 btrfs_block_rsv_release(root
, &root
->fs_info
->delalloc_block_rsv
,
4833 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
4834 * @inode: inode we're writing to
4835 * @num_bytes: the number of bytes we want to allocate
4837 * This will do the following things
4839 * o reserve space in the data space info for num_bytes
4840 * o reserve space in the metadata space info based on number of outstanding
4841 * extents and how much csums will be needed
4842 * o add to the inodes ->delalloc_bytes
4843 * o add it to the fs_info's delalloc inodes list.
4845 * This will return 0 for success and -ENOSPC if there is no space left.
4847 int btrfs_delalloc_reserve_space(struct inode
*inode
, u64 num_bytes
)
4851 ret
= btrfs_check_data_free_space(inode
, num_bytes
);
4855 ret
= btrfs_delalloc_reserve_metadata(inode
, num_bytes
);
4857 btrfs_free_reserved_data_space(inode
, num_bytes
);
4865 * btrfs_delalloc_release_space - release data and metadata space for delalloc
4866 * @inode: inode we're releasing space for
4867 * @num_bytes: the number of bytes we want to free up
4869 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
4870 * called in the case that we don't need the metadata AND data reservations
4871 * anymore. So if there is an error or we insert an inline extent.
4873 * This function will release the metadata space that was not used and will
4874 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
4875 * list if there are no delalloc bytes left.
4877 void btrfs_delalloc_release_space(struct inode
*inode
, u64 num_bytes
)
4879 btrfs_delalloc_release_metadata(inode
, num_bytes
);
4880 btrfs_free_reserved_data_space(inode
, num_bytes
);
4883 static int update_block_group(struct btrfs_root
*root
,
4884 u64 bytenr
, u64 num_bytes
, int alloc
)
4886 struct btrfs_block_group_cache
*cache
= NULL
;
4887 struct btrfs_fs_info
*info
= root
->fs_info
;
4888 u64 total
= num_bytes
;
4893 /* block accounting for super block */
4894 spin_lock(&info
->delalloc_lock
);
4895 old_val
= btrfs_super_bytes_used(info
->super_copy
);
4897 old_val
+= num_bytes
;
4899 old_val
-= num_bytes
;
4900 btrfs_set_super_bytes_used(info
->super_copy
, old_val
);
4901 spin_unlock(&info
->delalloc_lock
);
4904 cache
= btrfs_lookup_block_group(info
, bytenr
);
4907 if (cache
->flags
& (BTRFS_BLOCK_GROUP_DUP
|
4908 BTRFS_BLOCK_GROUP_RAID1
|
4909 BTRFS_BLOCK_GROUP_RAID10
))
4914 * If this block group has free space cache written out, we
4915 * need to make sure to load it if we are removing space. This
4916 * is because we need the unpinning stage to actually add the
4917 * space back to the block group, otherwise we will leak space.
4919 if (!alloc
&& cache
->cached
== BTRFS_CACHE_NO
)
4920 cache_block_group(cache
, 1);
4922 byte_in_group
= bytenr
- cache
->key
.objectid
;
4923 WARN_ON(byte_in_group
> cache
->key
.offset
);
4925 spin_lock(&cache
->space_info
->lock
);
4926 spin_lock(&cache
->lock
);
4928 if (btrfs_test_opt(root
, SPACE_CACHE
) &&
4929 cache
->disk_cache_state
< BTRFS_DC_CLEAR
)
4930 cache
->disk_cache_state
= BTRFS_DC_CLEAR
;
4933 old_val
= btrfs_block_group_used(&cache
->item
);
4934 num_bytes
= min(total
, cache
->key
.offset
- byte_in_group
);
4936 old_val
+= num_bytes
;
4937 btrfs_set_block_group_used(&cache
->item
, old_val
);
4938 cache
->reserved
-= num_bytes
;
4939 cache
->space_info
->bytes_reserved
-= num_bytes
;
4940 cache
->space_info
->bytes_used
+= num_bytes
;
4941 cache
->space_info
->disk_used
+= num_bytes
* factor
;
4942 spin_unlock(&cache
->lock
);
4943 spin_unlock(&cache
->space_info
->lock
);
4945 old_val
-= num_bytes
;
4946 btrfs_set_block_group_used(&cache
->item
, old_val
);
4947 cache
->pinned
+= num_bytes
;
4948 cache
->space_info
->bytes_pinned
+= num_bytes
;
4949 cache
->space_info
->bytes_used
-= num_bytes
;
4950 cache
->space_info
->disk_used
-= num_bytes
* factor
;
4951 spin_unlock(&cache
->lock
);
4952 spin_unlock(&cache
->space_info
->lock
);
4954 set_extent_dirty(info
->pinned_extents
,
4955 bytenr
, bytenr
+ num_bytes
- 1,
4956 GFP_NOFS
| __GFP_NOFAIL
);
4958 btrfs_put_block_group(cache
);
4960 bytenr
+= num_bytes
;
4965 static u64
first_logical_byte(struct btrfs_root
*root
, u64 search_start
)
4967 struct btrfs_block_group_cache
*cache
;
4970 spin_lock(&root
->fs_info
->block_group_cache_lock
);
4971 bytenr
= root
->fs_info
->first_logical_byte
;
4972 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
4974 if (bytenr
< (u64
)-1)
4977 cache
= btrfs_lookup_first_block_group(root
->fs_info
, search_start
);
4981 bytenr
= cache
->key
.objectid
;
4982 btrfs_put_block_group(cache
);
4987 static int pin_down_extent(struct btrfs_root
*root
,
4988 struct btrfs_block_group_cache
*cache
,
4989 u64 bytenr
, u64 num_bytes
, int reserved
)
4991 spin_lock(&cache
->space_info
->lock
);
4992 spin_lock(&cache
->lock
);
4993 cache
->pinned
+= num_bytes
;
4994 cache
->space_info
->bytes_pinned
+= num_bytes
;
4996 cache
->reserved
-= num_bytes
;
4997 cache
->space_info
->bytes_reserved
-= num_bytes
;
4999 spin_unlock(&cache
->lock
);
5000 spin_unlock(&cache
->space_info
->lock
);
5002 set_extent_dirty(root
->fs_info
->pinned_extents
, bytenr
,
5003 bytenr
+ num_bytes
- 1, GFP_NOFS
| __GFP_NOFAIL
);
5008 * this function must be called within transaction
5010 int btrfs_pin_extent(struct btrfs_root
*root
,
5011 u64 bytenr
, u64 num_bytes
, int reserved
)
5013 struct btrfs_block_group_cache
*cache
;
5015 cache
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
5016 BUG_ON(!cache
); /* Logic error */
5018 pin_down_extent(root
, cache
, bytenr
, num_bytes
, reserved
);
5020 btrfs_put_block_group(cache
);
5025 * this function must be called within transaction
5027 int btrfs_pin_extent_for_log_replay(struct btrfs_root
*root
,
5028 u64 bytenr
, u64 num_bytes
)
5030 struct btrfs_block_group_cache
*cache
;
5032 cache
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
5033 BUG_ON(!cache
); /* Logic error */
5036 * pull in the free space cache (if any) so that our pin
5037 * removes the free space from the cache. We have load_only set
5038 * to one because the slow code to read in the free extents does check
5039 * the pinned extents.
5041 cache_block_group(cache
, 1);
5043 pin_down_extent(root
, cache
, bytenr
, num_bytes
, 0);
5045 /* remove us from the free space cache (if we're there at all) */
5046 btrfs_remove_free_space(cache
, bytenr
, num_bytes
);
5047 btrfs_put_block_group(cache
);
5052 * btrfs_update_reserved_bytes - update the block_group and space info counters
5053 * @cache: The cache we are manipulating
5054 * @num_bytes: The number of bytes in question
5055 * @reserve: One of the reservation enums
5057 * This is called by the allocator when it reserves space, or by somebody who is
5058 * freeing space that was never actually used on disk. For example if you
5059 * reserve some space for a new leaf in transaction A and before transaction A
5060 * commits you free that leaf, you call this with reserve set to 0 in order to
5061 * clear the reservation.
5063 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
5064 * ENOSPC accounting. For data we handle the reservation through clearing the
5065 * delalloc bits in the io_tree. We have to do this since we could end up
5066 * allocating less disk space for the amount of data we have reserved in the
5067 * case of compression.
5069 * If this is a reservation and the block group has become read only we cannot
5070 * make the reservation and return -EAGAIN, otherwise this function always
5073 static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache
*cache
,
5074 u64 num_bytes
, int reserve
)
5076 struct btrfs_space_info
*space_info
= cache
->space_info
;
5079 spin_lock(&space_info
->lock
);
5080 spin_lock(&cache
->lock
);
5081 if (reserve
!= RESERVE_FREE
) {
5085 cache
->reserved
+= num_bytes
;
5086 space_info
->bytes_reserved
+= num_bytes
;
5087 if (reserve
== RESERVE_ALLOC
) {
5088 trace_btrfs_space_reservation(cache
->fs_info
,
5089 "space_info", space_info
->flags
,
5091 space_info
->bytes_may_use
-= num_bytes
;
5096 space_info
->bytes_readonly
+= num_bytes
;
5097 cache
->reserved
-= num_bytes
;
5098 space_info
->bytes_reserved
-= num_bytes
;
5099 space_info
->reservation_progress
++;
5101 spin_unlock(&cache
->lock
);
5102 spin_unlock(&space_info
->lock
);
5106 void btrfs_prepare_extent_commit(struct btrfs_trans_handle
*trans
,
5107 struct btrfs_root
*root
)
5109 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5110 struct btrfs_caching_control
*next
;
5111 struct btrfs_caching_control
*caching_ctl
;
5112 struct btrfs_block_group_cache
*cache
;
5114 down_write(&fs_info
->extent_commit_sem
);
5116 list_for_each_entry_safe(caching_ctl
, next
,
5117 &fs_info
->caching_block_groups
, list
) {
5118 cache
= caching_ctl
->block_group
;
5119 if (block_group_cache_done(cache
)) {
5120 cache
->last_byte_to_unpin
= (u64
)-1;
5121 list_del_init(&caching_ctl
->list
);
5122 put_caching_control(caching_ctl
);
5124 cache
->last_byte_to_unpin
= caching_ctl
->progress
;
5128 if (fs_info
->pinned_extents
== &fs_info
->freed_extents
[0])
5129 fs_info
->pinned_extents
= &fs_info
->freed_extents
[1];
5131 fs_info
->pinned_extents
= &fs_info
->freed_extents
[0];
5133 up_write(&fs_info
->extent_commit_sem
);
5135 update_global_block_rsv(fs_info
);
5138 static int unpin_extent_range(struct btrfs_root
*root
, u64 start
, u64 end
)
5140 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5141 struct btrfs_block_group_cache
*cache
= NULL
;
5142 struct btrfs_space_info
*space_info
;
5143 struct btrfs_block_rsv
*global_rsv
= &fs_info
->global_block_rsv
;
5147 while (start
<= end
) {
5150 start
>= cache
->key
.objectid
+ cache
->key
.offset
) {
5152 btrfs_put_block_group(cache
);
5153 cache
= btrfs_lookup_block_group(fs_info
, start
);
5154 BUG_ON(!cache
); /* Logic error */
5157 len
= cache
->key
.objectid
+ cache
->key
.offset
- start
;
5158 len
= min(len
, end
+ 1 - start
);
5160 if (start
< cache
->last_byte_to_unpin
) {
5161 len
= min(len
, cache
->last_byte_to_unpin
- start
);
5162 btrfs_add_free_space(cache
, start
, len
);
5166 space_info
= cache
->space_info
;
5168 spin_lock(&space_info
->lock
);
5169 spin_lock(&cache
->lock
);
5170 cache
->pinned
-= len
;
5171 space_info
->bytes_pinned
-= len
;
5173 space_info
->bytes_readonly
+= len
;
5176 spin_unlock(&cache
->lock
);
5177 if (!readonly
&& global_rsv
->space_info
== space_info
) {
5178 spin_lock(&global_rsv
->lock
);
5179 if (!global_rsv
->full
) {
5180 len
= min(len
, global_rsv
->size
-
5181 global_rsv
->reserved
);
5182 global_rsv
->reserved
+= len
;
5183 space_info
->bytes_may_use
+= len
;
5184 if (global_rsv
->reserved
>= global_rsv
->size
)
5185 global_rsv
->full
= 1;
5187 spin_unlock(&global_rsv
->lock
);
5189 spin_unlock(&space_info
->lock
);
5193 btrfs_put_block_group(cache
);
5197 int btrfs_finish_extent_commit(struct btrfs_trans_handle
*trans
,
5198 struct btrfs_root
*root
)
5200 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5201 struct extent_io_tree
*unpin
;
5209 if (fs_info
->pinned_extents
== &fs_info
->freed_extents
[0])
5210 unpin
= &fs_info
->freed_extents
[1];
5212 unpin
= &fs_info
->freed_extents
[0];
5215 ret
= find_first_extent_bit(unpin
, 0, &start
, &end
,
5216 EXTENT_DIRTY
, NULL
);
5220 if (btrfs_test_opt(root
, DISCARD
))
5221 ret
= btrfs_discard_extent(root
, start
,
5222 end
+ 1 - start
, NULL
);
5224 clear_extent_dirty(unpin
, start
, end
, GFP_NOFS
);
5225 unpin_extent_range(root
, start
, end
);
5232 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
5233 struct btrfs_root
*root
,
5234 u64 bytenr
, u64 num_bytes
, u64 parent
,
5235 u64 root_objectid
, u64 owner_objectid
,
5236 u64 owner_offset
, int refs_to_drop
,
5237 struct btrfs_delayed_extent_op
*extent_op
)
5239 struct btrfs_key key
;
5240 struct btrfs_path
*path
;
5241 struct btrfs_fs_info
*info
= root
->fs_info
;
5242 struct btrfs_root
*extent_root
= info
->extent_root
;
5243 struct extent_buffer
*leaf
;
5244 struct btrfs_extent_item
*ei
;
5245 struct btrfs_extent_inline_ref
*iref
;
5248 int extent_slot
= 0;
5249 int found_extent
= 0;
5254 path
= btrfs_alloc_path();
5259 path
->leave_spinning
= 1;
5261 is_data
= owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
;
5262 BUG_ON(!is_data
&& refs_to_drop
!= 1);
5264 ret
= lookup_extent_backref(trans
, extent_root
, path
, &iref
,
5265 bytenr
, num_bytes
, parent
,
5266 root_objectid
, owner_objectid
,
5269 extent_slot
= path
->slots
[0];
5270 while (extent_slot
>= 0) {
5271 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
5273 if (key
.objectid
!= bytenr
)
5275 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
5276 key
.offset
== num_bytes
) {
5280 if (path
->slots
[0] - extent_slot
> 5)
5284 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5285 item_size
= btrfs_item_size_nr(path
->nodes
[0], extent_slot
);
5286 if (found_extent
&& item_size
< sizeof(*ei
))
5289 if (!found_extent
) {
5291 ret
= remove_extent_backref(trans
, extent_root
, path
,
5295 btrfs_abort_transaction(trans
, extent_root
, ret
);
5298 btrfs_release_path(path
);
5299 path
->leave_spinning
= 1;
5301 key
.objectid
= bytenr
;
5302 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
5303 key
.offset
= num_bytes
;
5305 ret
= btrfs_search_slot(trans
, extent_root
,
5308 printk(KERN_ERR
"umm, got %d back from search"
5309 ", was looking for %llu\n", ret
,
5310 (unsigned long long)bytenr
);
5312 btrfs_print_leaf(extent_root
,
5316 btrfs_abort_transaction(trans
, extent_root
, ret
);
5319 extent_slot
= path
->slots
[0];
5321 } else if (ret
== -ENOENT
) {
5322 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
5324 printk(KERN_ERR
"btrfs unable to find ref byte nr %llu "
5325 "parent %llu root %llu owner %llu offset %llu\n",
5326 (unsigned long long)bytenr
,
5327 (unsigned long long)parent
,
5328 (unsigned long long)root_objectid
,
5329 (unsigned long long)owner_objectid
,
5330 (unsigned long long)owner_offset
);
5332 btrfs_abort_transaction(trans
, extent_root
, ret
);
5336 leaf
= path
->nodes
[0];
5337 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
5338 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5339 if (item_size
< sizeof(*ei
)) {
5340 BUG_ON(found_extent
|| extent_slot
!= path
->slots
[0]);
5341 ret
= convert_extent_item_v0(trans
, extent_root
, path
,
5344 btrfs_abort_transaction(trans
, extent_root
, ret
);
5348 btrfs_release_path(path
);
5349 path
->leave_spinning
= 1;
5351 key
.objectid
= bytenr
;
5352 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
5353 key
.offset
= num_bytes
;
5355 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
,
5358 printk(KERN_ERR
"umm, got %d back from search"
5359 ", was looking for %llu\n", ret
,
5360 (unsigned long long)bytenr
);
5361 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
5364 btrfs_abort_transaction(trans
, extent_root
, ret
);
5368 extent_slot
= path
->slots
[0];
5369 leaf
= path
->nodes
[0];
5370 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
5373 BUG_ON(item_size
< sizeof(*ei
));
5374 ei
= btrfs_item_ptr(leaf
, extent_slot
,
5375 struct btrfs_extent_item
);
5376 if (owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
) {
5377 struct btrfs_tree_block_info
*bi
;
5378 BUG_ON(item_size
< sizeof(*ei
) + sizeof(*bi
));
5379 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
5380 WARN_ON(owner_objectid
!= btrfs_tree_block_level(leaf
, bi
));
5383 refs
= btrfs_extent_refs(leaf
, ei
);
5384 BUG_ON(refs
< refs_to_drop
);
5385 refs
-= refs_to_drop
;
5389 __run_delayed_extent_op(extent_op
, leaf
, ei
);
5391 * In the case of inline back ref, reference count will
5392 * be updated by remove_extent_backref
5395 BUG_ON(!found_extent
);
5397 btrfs_set_extent_refs(leaf
, ei
, refs
);
5398 btrfs_mark_buffer_dirty(leaf
);
5401 ret
= remove_extent_backref(trans
, extent_root
, path
,
5405 btrfs_abort_transaction(trans
, extent_root
, ret
);
5411 BUG_ON(is_data
&& refs_to_drop
!=
5412 extent_data_ref_count(root
, path
, iref
));
5414 BUG_ON(path
->slots
[0] != extent_slot
);
5416 BUG_ON(path
->slots
[0] != extent_slot
+ 1);
5417 path
->slots
[0] = extent_slot
;
5422 ret
= btrfs_del_items(trans
, extent_root
, path
, path
->slots
[0],
5425 btrfs_abort_transaction(trans
, extent_root
, ret
);
5428 btrfs_release_path(path
);
5431 ret
= btrfs_del_csums(trans
, root
, bytenr
, num_bytes
);
5433 btrfs_abort_transaction(trans
, extent_root
, ret
);
5438 ret
= update_block_group(root
, bytenr
, num_bytes
, 0);
5440 btrfs_abort_transaction(trans
, extent_root
, ret
);
5445 btrfs_free_path(path
);
5450 * when we free an block, it is possible (and likely) that we free the last
5451 * delayed ref for that extent as well. This searches the delayed ref tree for
5452 * a given extent, and if there are no other delayed refs to be processed, it
5453 * removes it from the tree.
5455 static noinline
int check_ref_cleanup(struct btrfs_trans_handle
*trans
,
5456 struct btrfs_root
*root
, u64 bytenr
)
5458 struct btrfs_delayed_ref_head
*head
;
5459 struct btrfs_delayed_ref_root
*delayed_refs
;
5460 struct btrfs_delayed_ref_node
*ref
;
5461 struct rb_node
*node
;
5464 delayed_refs
= &trans
->transaction
->delayed_refs
;
5465 spin_lock(&delayed_refs
->lock
);
5466 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
5470 node
= rb_prev(&head
->node
.rb_node
);
5474 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
5476 /* there are still entries for this ref, we can't drop it */
5477 if (ref
->bytenr
== bytenr
)
5480 if (head
->extent_op
) {
5481 if (!head
->must_insert_reserved
)
5483 btrfs_free_delayed_extent_op(head
->extent_op
);
5484 head
->extent_op
= NULL
;
5488 * waiting for the lock here would deadlock. If someone else has it
5489 * locked they are already in the process of dropping it anyway
5491 if (!mutex_trylock(&head
->mutex
))
5495 * at this point we have a head with no other entries. Go
5496 * ahead and process it.
5498 head
->node
.in_tree
= 0;
5499 rb_erase(&head
->node
.rb_node
, &delayed_refs
->root
);
5501 delayed_refs
->num_entries
--;
5504 * we don't take a ref on the node because we're removing it from the
5505 * tree, so we just steal the ref the tree was holding.
5507 delayed_refs
->num_heads
--;
5508 if (list_empty(&head
->cluster
))
5509 delayed_refs
->num_heads_ready
--;
5511 list_del_init(&head
->cluster
);
5512 spin_unlock(&delayed_refs
->lock
);
5514 BUG_ON(head
->extent_op
);
5515 if (head
->must_insert_reserved
)
5518 mutex_unlock(&head
->mutex
);
5519 btrfs_put_delayed_ref(&head
->node
);
5522 spin_unlock(&delayed_refs
->lock
);
5526 void btrfs_free_tree_block(struct btrfs_trans_handle
*trans
,
5527 struct btrfs_root
*root
,
5528 struct extent_buffer
*buf
,
5529 u64 parent
, int last_ref
)
5531 struct btrfs_block_group_cache
*cache
= NULL
;
5534 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
5535 ret
= btrfs_add_delayed_tree_ref(root
->fs_info
, trans
,
5536 buf
->start
, buf
->len
,
5537 parent
, root
->root_key
.objectid
,
5538 btrfs_header_level(buf
),
5539 BTRFS_DROP_DELAYED_REF
, NULL
, 0);
5540 BUG_ON(ret
); /* -ENOMEM */
5546 cache
= btrfs_lookup_block_group(root
->fs_info
, buf
->start
);
5548 if (btrfs_header_generation(buf
) == trans
->transid
) {
5549 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
5550 ret
= check_ref_cleanup(trans
, root
, buf
->start
);
5555 if (btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
)) {
5556 pin_down_extent(root
, cache
, buf
->start
, buf
->len
, 1);
5560 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY
, &buf
->bflags
));
5562 btrfs_add_free_space(cache
, buf
->start
, buf
->len
);
5563 btrfs_update_reserved_bytes(cache
, buf
->len
, RESERVE_FREE
);
5567 * Deleting the buffer, clear the corrupt flag since it doesn't matter
5570 clear_bit(EXTENT_BUFFER_CORRUPT
, &buf
->bflags
);
5571 btrfs_put_block_group(cache
);
5574 /* Can return -ENOMEM */
5575 int btrfs_free_extent(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
5576 u64 bytenr
, u64 num_bytes
, u64 parent
, u64 root_objectid
,
5577 u64 owner
, u64 offset
, int for_cow
)
5580 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5583 * tree log blocks never actually go into the extent allocation
5584 * tree, just update pinning info and exit early.
5586 if (root_objectid
== BTRFS_TREE_LOG_OBJECTID
) {
5587 WARN_ON(owner
>= BTRFS_FIRST_FREE_OBJECTID
);
5588 /* unlocks the pinned mutex */
5589 btrfs_pin_extent(root
, bytenr
, num_bytes
, 1);
5591 } else if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
5592 ret
= btrfs_add_delayed_tree_ref(fs_info
, trans
, bytenr
,
5594 parent
, root_objectid
, (int)owner
,
5595 BTRFS_DROP_DELAYED_REF
, NULL
, for_cow
);
5597 ret
= btrfs_add_delayed_data_ref(fs_info
, trans
, bytenr
,
5599 parent
, root_objectid
, owner
,
5600 offset
, BTRFS_DROP_DELAYED_REF
,
5606 static u64
stripe_align(struct btrfs_root
*root
,
5607 struct btrfs_block_group_cache
*cache
,
5608 u64 val
, u64 num_bytes
)
5612 mask
= ((u64
)root
->stripesize
- 1);
5613 ret
= (val
+ mask
) & ~mask
;
5618 * when we wait for progress in the block group caching, its because
5619 * our allocation attempt failed at least once. So, we must sleep
5620 * and let some progress happen before we try again.
5622 * This function will sleep at least once waiting for new free space to
5623 * show up, and then it will check the block group free space numbers
5624 * for our min num_bytes. Another option is to have it go ahead
5625 * and look in the rbtree for a free extent of a given size, but this
5629 wait_block_group_cache_progress(struct btrfs_block_group_cache
*cache
,
5632 struct btrfs_caching_control
*caching_ctl
;
5634 caching_ctl
= get_caching_control(cache
);
5638 wait_event(caching_ctl
->wait
, block_group_cache_done(cache
) ||
5639 (cache
->free_space_ctl
->free_space
>= num_bytes
));
5641 put_caching_control(caching_ctl
);
5646 wait_block_group_cache_done(struct btrfs_block_group_cache
*cache
)
5648 struct btrfs_caching_control
*caching_ctl
;
5650 caching_ctl
= get_caching_control(cache
);
5654 wait_event(caching_ctl
->wait
, block_group_cache_done(cache
));
5656 put_caching_control(caching_ctl
);
5660 int __get_raid_index(u64 flags
)
5662 if (flags
& BTRFS_BLOCK_GROUP_RAID10
)
5663 return BTRFS_RAID_RAID10
;
5664 else if (flags
& BTRFS_BLOCK_GROUP_RAID1
)
5665 return BTRFS_RAID_RAID1
;
5666 else if (flags
& BTRFS_BLOCK_GROUP_DUP
)
5667 return BTRFS_RAID_DUP
;
5668 else if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
5669 return BTRFS_RAID_RAID0
;
5670 else if (flags
& BTRFS_BLOCK_GROUP_RAID5
)
5671 return BTRFS_RAID_RAID5
;
5672 else if (flags
& BTRFS_BLOCK_GROUP_RAID6
)
5673 return BTRFS_RAID_RAID6
;
5675 return BTRFS_RAID_SINGLE
; /* BTRFS_BLOCK_GROUP_SINGLE */
5678 static int get_block_group_index(struct btrfs_block_group_cache
*cache
)
5680 return __get_raid_index(cache
->flags
);
5683 enum btrfs_loop_type
{
5684 LOOP_CACHING_NOWAIT
= 0,
5685 LOOP_CACHING_WAIT
= 1,
5686 LOOP_ALLOC_CHUNK
= 2,
5687 LOOP_NO_EMPTY_SIZE
= 3,
5691 * walks the btree of allocated extents and find a hole of a given size.
5692 * The key ins is changed to record the hole:
5693 * ins->objectid == block start
5694 * ins->flags = BTRFS_EXTENT_ITEM_KEY
5695 * ins->offset == number of blocks
5696 * Any available blocks before search_start are skipped.
5698 static noinline
int find_free_extent(struct btrfs_trans_handle
*trans
,
5699 struct btrfs_root
*orig_root
,
5700 u64 num_bytes
, u64 empty_size
,
5701 u64 hint_byte
, struct btrfs_key
*ins
,
5705 struct btrfs_root
*root
= orig_root
->fs_info
->extent_root
;
5706 struct btrfs_free_cluster
*last_ptr
= NULL
;
5707 struct btrfs_block_group_cache
*block_group
= NULL
;
5708 struct btrfs_block_group_cache
*used_block_group
;
5709 u64 search_start
= 0;
5710 int empty_cluster
= 2 * 1024 * 1024;
5711 struct btrfs_space_info
*space_info
;
5713 int index
= __get_raid_index(data
);
5714 int alloc_type
= (data
& BTRFS_BLOCK_GROUP_DATA
) ?
5715 RESERVE_ALLOC_NO_ACCOUNT
: RESERVE_ALLOC
;
5716 bool found_uncached_bg
= false;
5717 bool failed_cluster_refill
= false;
5718 bool failed_alloc
= false;
5719 bool use_cluster
= true;
5720 bool have_caching_bg
= false;
5722 WARN_ON(num_bytes
< root
->sectorsize
);
5723 btrfs_set_key_type(ins
, BTRFS_EXTENT_ITEM_KEY
);
5727 trace_find_free_extent(orig_root
, num_bytes
, empty_size
, data
);
5729 space_info
= __find_space_info(root
->fs_info
, data
);
5731 printk(KERN_ERR
"No space info for %llu\n", data
);
5736 * If the space info is for both data and metadata it means we have a
5737 * small filesystem and we can't use the clustering stuff.
5739 if (btrfs_mixed_space_info(space_info
))
5740 use_cluster
= false;
5742 if (data
& BTRFS_BLOCK_GROUP_METADATA
&& use_cluster
) {
5743 last_ptr
= &root
->fs_info
->meta_alloc_cluster
;
5744 if (!btrfs_test_opt(root
, SSD
))
5745 empty_cluster
= 64 * 1024;
5748 if ((data
& BTRFS_BLOCK_GROUP_DATA
) && use_cluster
&&
5749 btrfs_test_opt(root
, SSD
)) {
5750 last_ptr
= &root
->fs_info
->data_alloc_cluster
;
5754 spin_lock(&last_ptr
->lock
);
5755 if (last_ptr
->block_group
)
5756 hint_byte
= last_ptr
->window_start
;
5757 spin_unlock(&last_ptr
->lock
);
5760 search_start
= max(search_start
, first_logical_byte(root
, 0));
5761 search_start
= max(search_start
, hint_byte
);
5766 if (search_start
== hint_byte
) {
5767 block_group
= btrfs_lookup_block_group(root
->fs_info
,
5769 used_block_group
= block_group
;
5771 * we don't want to use the block group if it doesn't match our
5772 * allocation bits, or if its not cached.
5774 * However if we are re-searching with an ideal block group
5775 * picked out then we don't care that the block group is cached.
5777 if (block_group
&& block_group_bits(block_group
, data
) &&
5778 block_group
->cached
!= BTRFS_CACHE_NO
) {
5779 down_read(&space_info
->groups_sem
);
5780 if (list_empty(&block_group
->list
) ||
5783 * someone is removing this block group,
5784 * we can't jump into the have_block_group
5785 * target because our list pointers are not
5788 btrfs_put_block_group(block_group
);
5789 up_read(&space_info
->groups_sem
);
5791 index
= get_block_group_index(block_group
);
5792 goto have_block_group
;
5794 } else if (block_group
) {
5795 btrfs_put_block_group(block_group
);
5799 have_caching_bg
= false;
5800 down_read(&space_info
->groups_sem
);
5801 list_for_each_entry(block_group
, &space_info
->block_groups
[index
],
5806 used_block_group
= block_group
;
5807 btrfs_get_block_group(block_group
);
5808 search_start
= block_group
->key
.objectid
;
5811 * this can happen if we end up cycling through all the
5812 * raid types, but we want to make sure we only allocate
5813 * for the proper type.
5815 if (!block_group_bits(block_group
, data
)) {
5816 u64 extra
= BTRFS_BLOCK_GROUP_DUP
|
5817 BTRFS_BLOCK_GROUP_RAID1
|
5818 BTRFS_BLOCK_GROUP_RAID5
|
5819 BTRFS_BLOCK_GROUP_RAID6
|
5820 BTRFS_BLOCK_GROUP_RAID10
;
5823 * if they asked for extra copies and this block group
5824 * doesn't provide them, bail. This does allow us to
5825 * fill raid0 from raid1.
5827 if ((data
& extra
) && !(block_group
->flags
& extra
))
5832 cached
= block_group_cache_done(block_group
);
5833 if (unlikely(!cached
)) {
5834 found_uncached_bg
= true;
5835 ret
= cache_block_group(block_group
, 0);
5840 if (unlikely(block_group
->ro
))
5844 * Ok we want to try and use the cluster allocator, so
5848 unsigned long aligned_cluster
;
5850 * the refill lock keeps out other
5851 * people trying to start a new cluster
5853 spin_lock(&last_ptr
->refill_lock
);
5854 used_block_group
= last_ptr
->block_group
;
5855 if (used_block_group
!= block_group
&&
5856 (!used_block_group
||
5857 used_block_group
->ro
||
5858 !block_group_bits(used_block_group
, data
))) {
5859 used_block_group
= block_group
;
5860 goto refill_cluster
;
5863 if (used_block_group
!= block_group
)
5864 btrfs_get_block_group(used_block_group
);
5866 offset
= btrfs_alloc_from_cluster(used_block_group
,
5867 last_ptr
, num_bytes
, used_block_group
->key
.objectid
);
5869 /* we have a block, we're done */
5870 spin_unlock(&last_ptr
->refill_lock
);
5871 trace_btrfs_reserve_extent_cluster(root
,
5872 block_group
, search_start
, num_bytes
);
5876 WARN_ON(last_ptr
->block_group
!= used_block_group
);
5877 if (used_block_group
!= block_group
) {
5878 btrfs_put_block_group(used_block_group
);
5879 used_block_group
= block_group
;
5882 BUG_ON(used_block_group
!= block_group
);
5883 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
5884 * set up a new clusters, so lets just skip it
5885 * and let the allocator find whatever block
5886 * it can find. If we reach this point, we
5887 * will have tried the cluster allocator
5888 * plenty of times and not have found
5889 * anything, so we are likely way too
5890 * fragmented for the clustering stuff to find
5893 * However, if the cluster is taken from the
5894 * current block group, release the cluster
5895 * first, so that we stand a better chance of
5896 * succeeding in the unclustered
5898 if (loop
>= LOOP_NO_EMPTY_SIZE
&&
5899 last_ptr
->block_group
!= block_group
) {
5900 spin_unlock(&last_ptr
->refill_lock
);
5901 goto unclustered_alloc
;
5905 * this cluster didn't work out, free it and
5908 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
5910 if (loop
>= LOOP_NO_EMPTY_SIZE
) {
5911 spin_unlock(&last_ptr
->refill_lock
);
5912 goto unclustered_alloc
;
5915 aligned_cluster
= max_t(unsigned long,
5916 empty_cluster
+ empty_size
,
5917 block_group
->full_stripe_len
);
5919 /* allocate a cluster in this block group */
5920 ret
= btrfs_find_space_cluster(trans
, root
,
5921 block_group
, last_ptr
,
5922 search_start
, num_bytes
,
5926 * now pull our allocation out of this
5929 offset
= btrfs_alloc_from_cluster(block_group
,
5930 last_ptr
, num_bytes
,
5933 /* we found one, proceed */
5934 spin_unlock(&last_ptr
->refill_lock
);
5935 trace_btrfs_reserve_extent_cluster(root
,
5936 block_group
, search_start
,
5940 } else if (!cached
&& loop
> LOOP_CACHING_NOWAIT
5941 && !failed_cluster_refill
) {
5942 spin_unlock(&last_ptr
->refill_lock
);
5944 failed_cluster_refill
= true;
5945 wait_block_group_cache_progress(block_group
,
5946 num_bytes
+ empty_cluster
+ empty_size
);
5947 goto have_block_group
;
5951 * at this point we either didn't find a cluster
5952 * or we weren't able to allocate a block from our
5953 * cluster. Free the cluster we've been trying
5954 * to use, and go to the next block group
5956 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
5957 spin_unlock(&last_ptr
->refill_lock
);
5962 spin_lock(&block_group
->free_space_ctl
->tree_lock
);
5964 block_group
->free_space_ctl
->free_space
<
5965 num_bytes
+ empty_cluster
+ empty_size
) {
5966 spin_unlock(&block_group
->free_space_ctl
->tree_lock
);
5969 spin_unlock(&block_group
->free_space_ctl
->tree_lock
);
5971 offset
= btrfs_find_space_for_alloc(block_group
, search_start
,
5972 num_bytes
, empty_size
);
5974 * If we didn't find a chunk, and we haven't failed on this
5975 * block group before, and this block group is in the middle of
5976 * caching and we are ok with waiting, then go ahead and wait
5977 * for progress to be made, and set failed_alloc to true.
5979 * If failed_alloc is true then we've already waited on this
5980 * block group once and should move on to the next block group.
5982 if (!offset
&& !failed_alloc
&& !cached
&&
5983 loop
> LOOP_CACHING_NOWAIT
) {
5984 wait_block_group_cache_progress(block_group
,
5985 num_bytes
+ empty_size
);
5986 failed_alloc
= true;
5987 goto have_block_group
;
5988 } else if (!offset
) {
5990 have_caching_bg
= true;
5994 search_start
= stripe_align(root
, used_block_group
,
5997 /* move on to the next group */
5998 if (search_start
+ num_bytes
>
5999 used_block_group
->key
.objectid
+ used_block_group
->key
.offset
) {
6000 btrfs_add_free_space(used_block_group
, offset
, num_bytes
);
6004 if (offset
< search_start
)
6005 btrfs_add_free_space(used_block_group
, offset
,
6006 search_start
- offset
);
6007 BUG_ON(offset
> search_start
);
6009 ret
= btrfs_update_reserved_bytes(used_block_group
, num_bytes
,
6011 if (ret
== -EAGAIN
) {
6012 btrfs_add_free_space(used_block_group
, offset
, num_bytes
);
6016 /* we are all good, lets return */
6017 ins
->objectid
= search_start
;
6018 ins
->offset
= num_bytes
;
6020 trace_btrfs_reserve_extent(orig_root
, block_group
,
6021 search_start
, num_bytes
);
6022 if (used_block_group
!= block_group
)
6023 btrfs_put_block_group(used_block_group
);
6024 btrfs_put_block_group(block_group
);
6027 failed_cluster_refill
= false;
6028 failed_alloc
= false;
6029 BUG_ON(index
!= get_block_group_index(block_group
));
6030 if (used_block_group
!= block_group
)
6031 btrfs_put_block_group(used_block_group
);
6032 btrfs_put_block_group(block_group
);
6034 up_read(&space_info
->groups_sem
);
6036 if (!ins
->objectid
&& loop
>= LOOP_CACHING_WAIT
&& have_caching_bg
)
6039 if (!ins
->objectid
&& ++index
< BTRFS_NR_RAID_TYPES
)
6043 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
6044 * caching kthreads as we move along
6045 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
6046 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
6047 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
6050 if (!ins
->objectid
&& loop
< LOOP_NO_EMPTY_SIZE
) {
6053 if (loop
== LOOP_ALLOC_CHUNK
) {
6054 ret
= do_chunk_alloc(trans
, root
, data
,
6057 * Do not bail out on ENOSPC since we
6058 * can do more things.
6060 if (ret
< 0 && ret
!= -ENOSPC
) {
6061 btrfs_abort_transaction(trans
,
6067 if (loop
== LOOP_NO_EMPTY_SIZE
) {
6073 } else if (!ins
->objectid
) {
6075 } else if (ins
->objectid
) {
6083 static void dump_space_info(struct btrfs_space_info
*info
, u64 bytes
,
6084 int dump_block_groups
)
6086 struct btrfs_block_group_cache
*cache
;
6089 spin_lock(&info
->lock
);
6090 printk(KERN_INFO
"space_info %llu has %llu free, is %sfull\n",
6091 (unsigned long long)info
->flags
,
6092 (unsigned long long)(info
->total_bytes
- info
->bytes_used
-
6093 info
->bytes_pinned
- info
->bytes_reserved
-
6094 info
->bytes_readonly
),
6095 (info
->full
) ? "" : "not ");
6096 printk(KERN_INFO
"space_info total=%llu, used=%llu, pinned=%llu, "
6097 "reserved=%llu, may_use=%llu, readonly=%llu\n",
6098 (unsigned long long)info
->total_bytes
,
6099 (unsigned long long)info
->bytes_used
,
6100 (unsigned long long)info
->bytes_pinned
,
6101 (unsigned long long)info
->bytes_reserved
,
6102 (unsigned long long)info
->bytes_may_use
,
6103 (unsigned long long)info
->bytes_readonly
);
6104 spin_unlock(&info
->lock
);
6106 if (!dump_block_groups
)
6109 down_read(&info
->groups_sem
);
6111 list_for_each_entry(cache
, &info
->block_groups
[index
], list
) {
6112 spin_lock(&cache
->lock
);
6113 printk(KERN_INFO
"block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
6114 (unsigned long long)cache
->key
.objectid
,
6115 (unsigned long long)cache
->key
.offset
,
6116 (unsigned long long)btrfs_block_group_used(&cache
->item
),
6117 (unsigned long long)cache
->pinned
,
6118 (unsigned long long)cache
->reserved
,
6119 cache
->ro
? "[readonly]" : "");
6120 btrfs_dump_free_space(cache
, bytes
);
6121 spin_unlock(&cache
->lock
);
6123 if (++index
< BTRFS_NR_RAID_TYPES
)
6125 up_read(&info
->groups_sem
);
6128 int btrfs_reserve_extent(struct btrfs_trans_handle
*trans
,
6129 struct btrfs_root
*root
,
6130 u64 num_bytes
, u64 min_alloc_size
,
6131 u64 empty_size
, u64 hint_byte
,
6132 struct btrfs_key
*ins
, u64 data
)
6134 bool final_tried
= false;
6137 data
= btrfs_get_alloc_profile(root
, data
);
6139 WARN_ON(num_bytes
< root
->sectorsize
);
6140 ret
= find_free_extent(trans
, root
, num_bytes
, empty_size
,
6141 hint_byte
, ins
, data
);
6143 if (ret
== -ENOSPC
) {
6145 num_bytes
= num_bytes
>> 1;
6146 num_bytes
= round_down(num_bytes
, root
->sectorsize
);
6147 num_bytes
= max(num_bytes
, min_alloc_size
);
6148 if (num_bytes
== min_alloc_size
)
6151 } else if (btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
6152 struct btrfs_space_info
*sinfo
;
6154 sinfo
= __find_space_info(root
->fs_info
, data
);
6155 printk(KERN_ERR
"btrfs allocation failed flags %llu, "
6156 "wanted %llu\n", (unsigned long long)data
,
6157 (unsigned long long)num_bytes
);
6159 dump_space_info(sinfo
, num_bytes
, 1);
6163 trace_btrfs_reserved_extent_alloc(root
, ins
->objectid
, ins
->offset
);
6168 static int __btrfs_free_reserved_extent(struct btrfs_root
*root
,
6169 u64 start
, u64 len
, int pin
)
6171 struct btrfs_block_group_cache
*cache
;
6174 cache
= btrfs_lookup_block_group(root
->fs_info
, start
);
6176 printk(KERN_ERR
"Unable to find block group for %llu\n",
6177 (unsigned long long)start
);
6181 if (btrfs_test_opt(root
, DISCARD
))
6182 ret
= btrfs_discard_extent(root
, start
, len
, NULL
);
6185 pin_down_extent(root
, cache
, start
, len
, 1);
6187 btrfs_add_free_space(cache
, start
, len
);
6188 btrfs_update_reserved_bytes(cache
, len
, RESERVE_FREE
);
6190 btrfs_put_block_group(cache
);
6192 trace_btrfs_reserved_extent_free(root
, start
, len
);
6197 int btrfs_free_reserved_extent(struct btrfs_root
*root
,
6200 return __btrfs_free_reserved_extent(root
, start
, len
, 0);
6203 int btrfs_free_and_pin_reserved_extent(struct btrfs_root
*root
,
6206 return __btrfs_free_reserved_extent(root
, start
, len
, 1);
6209 static int alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
6210 struct btrfs_root
*root
,
6211 u64 parent
, u64 root_objectid
,
6212 u64 flags
, u64 owner
, u64 offset
,
6213 struct btrfs_key
*ins
, int ref_mod
)
6216 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
6217 struct btrfs_extent_item
*extent_item
;
6218 struct btrfs_extent_inline_ref
*iref
;
6219 struct btrfs_path
*path
;
6220 struct extent_buffer
*leaf
;
6225 type
= BTRFS_SHARED_DATA_REF_KEY
;
6227 type
= BTRFS_EXTENT_DATA_REF_KEY
;
6229 size
= sizeof(*extent_item
) + btrfs_extent_inline_ref_size(type
);
6231 path
= btrfs_alloc_path();
6235 path
->leave_spinning
= 1;
6236 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
6239 btrfs_free_path(path
);
6243 leaf
= path
->nodes
[0];
6244 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
6245 struct btrfs_extent_item
);
6246 btrfs_set_extent_refs(leaf
, extent_item
, ref_mod
);
6247 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
6248 btrfs_set_extent_flags(leaf
, extent_item
,
6249 flags
| BTRFS_EXTENT_FLAG_DATA
);
6251 iref
= (struct btrfs_extent_inline_ref
*)(extent_item
+ 1);
6252 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
6254 struct btrfs_shared_data_ref
*ref
;
6255 ref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
6256 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
6257 btrfs_set_shared_data_ref_count(leaf
, ref
, ref_mod
);
6259 struct btrfs_extent_data_ref
*ref
;
6260 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
6261 btrfs_set_extent_data_ref_root(leaf
, ref
, root_objectid
);
6262 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
6263 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
6264 btrfs_set_extent_data_ref_count(leaf
, ref
, ref_mod
);
6267 btrfs_mark_buffer_dirty(path
->nodes
[0]);
6268 btrfs_free_path(path
);
6270 ret
= update_block_group(root
, ins
->objectid
, ins
->offset
, 1);
6271 if (ret
) { /* -ENOENT, logic error */
6272 printk(KERN_ERR
"btrfs update block group failed for %llu "
6273 "%llu\n", (unsigned long long)ins
->objectid
,
6274 (unsigned long long)ins
->offset
);
6280 static int alloc_reserved_tree_block(struct btrfs_trans_handle
*trans
,
6281 struct btrfs_root
*root
,
6282 u64 parent
, u64 root_objectid
,
6283 u64 flags
, struct btrfs_disk_key
*key
,
6284 int level
, struct btrfs_key
*ins
)
6287 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
6288 struct btrfs_extent_item
*extent_item
;
6289 struct btrfs_tree_block_info
*block_info
;
6290 struct btrfs_extent_inline_ref
*iref
;
6291 struct btrfs_path
*path
;
6292 struct extent_buffer
*leaf
;
6293 u32 size
= sizeof(*extent_item
) + sizeof(*block_info
) + sizeof(*iref
);
6295 path
= btrfs_alloc_path();
6299 path
->leave_spinning
= 1;
6300 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
6303 btrfs_free_path(path
);
6307 leaf
= path
->nodes
[0];
6308 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
6309 struct btrfs_extent_item
);
6310 btrfs_set_extent_refs(leaf
, extent_item
, 1);
6311 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
6312 btrfs_set_extent_flags(leaf
, extent_item
,
6313 flags
| BTRFS_EXTENT_FLAG_TREE_BLOCK
);
6314 block_info
= (struct btrfs_tree_block_info
*)(extent_item
+ 1);
6316 btrfs_set_tree_block_key(leaf
, block_info
, key
);
6317 btrfs_set_tree_block_level(leaf
, block_info
, level
);
6319 iref
= (struct btrfs_extent_inline_ref
*)(block_info
+ 1);
6321 BUG_ON(!(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
));
6322 btrfs_set_extent_inline_ref_type(leaf
, iref
,
6323 BTRFS_SHARED_BLOCK_REF_KEY
);
6324 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
6326 btrfs_set_extent_inline_ref_type(leaf
, iref
,
6327 BTRFS_TREE_BLOCK_REF_KEY
);
6328 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
6331 btrfs_mark_buffer_dirty(leaf
);
6332 btrfs_free_path(path
);
6334 ret
= update_block_group(root
, ins
->objectid
, ins
->offset
, 1);
6335 if (ret
) { /* -ENOENT, logic error */
6336 printk(KERN_ERR
"btrfs update block group failed for %llu "
6337 "%llu\n", (unsigned long long)ins
->objectid
,
6338 (unsigned long long)ins
->offset
);
6344 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
6345 struct btrfs_root
*root
,
6346 u64 root_objectid
, u64 owner
,
6347 u64 offset
, struct btrfs_key
*ins
)
6351 BUG_ON(root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
6353 ret
= btrfs_add_delayed_data_ref(root
->fs_info
, trans
, ins
->objectid
,
6355 root_objectid
, owner
, offset
,
6356 BTRFS_ADD_DELAYED_EXTENT
, NULL
, 0);
6361 * this is used by the tree logging recovery code. It records that
6362 * an extent has been allocated and makes sure to clear the free
6363 * space cache bits as well
6365 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle
*trans
,
6366 struct btrfs_root
*root
,
6367 u64 root_objectid
, u64 owner
, u64 offset
,
6368 struct btrfs_key
*ins
)
6371 struct btrfs_block_group_cache
*block_group
;
6372 struct btrfs_caching_control
*caching_ctl
;
6373 u64 start
= ins
->objectid
;
6374 u64 num_bytes
= ins
->offset
;
6376 block_group
= btrfs_lookup_block_group(root
->fs_info
, ins
->objectid
);
6377 cache_block_group(block_group
, 0);
6378 caching_ctl
= get_caching_control(block_group
);
6381 BUG_ON(!block_group_cache_done(block_group
));
6382 ret
= btrfs_remove_free_space(block_group
, start
, num_bytes
);
6383 BUG_ON(ret
); /* -ENOMEM */
6385 mutex_lock(&caching_ctl
->mutex
);
6387 if (start
>= caching_ctl
->progress
) {
6388 ret
= add_excluded_extent(root
, start
, num_bytes
);
6389 BUG_ON(ret
); /* -ENOMEM */
6390 } else if (start
+ num_bytes
<= caching_ctl
->progress
) {
6391 ret
= btrfs_remove_free_space(block_group
,
6393 BUG_ON(ret
); /* -ENOMEM */
6395 num_bytes
= caching_ctl
->progress
- start
;
6396 ret
= btrfs_remove_free_space(block_group
,
6398 BUG_ON(ret
); /* -ENOMEM */
6400 start
= caching_ctl
->progress
;
6401 num_bytes
= ins
->objectid
+ ins
->offset
-
6402 caching_ctl
->progress
;
6403 ret
= add_excluded_extent(root
, start
, num_bytes
);
6404 BUG_ON(ret
); /* -ENOMEM */
6407 mutex_unlock(&caching_ctl
->mutex
);
6408 put_caching_control(caching_ctl
);
6411 ret
= btrfs_update_reserved_bytes(block_group
, ins
->offset
,
6412 RESERVE_ALLOC_NO_ACCOUNT
);
6413 BUG_ON(ret
); /* logic error */
6414 btrfs_put_block_group(block_group
);
6415 ret
= alloc_reserved_file_extent(trans
, root
, 0, root_objectid
,
6416 0, owner
, offset
, ins
, 1);
6420 struct extent_buffer
*btrfs_init_new_buffer(struct btrfs_trans_handle
*trans
,
6421 struct btrfs_root
*root
,
6422 u64 bytenr
, u32 blocksize
,
6425 struct extent_buffer
*buf
;
6427 buf
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
6429 return ERR_PTR(-ENOMEM
);
6430 btrfs_set_header_generation(buf
, trans
->transid
);
6431 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, buf
, level
);
6432 btrfs_tree_lock(buf
);
6433 clean_tree_block(trans
, root
, buf
);
6434 clear_bit(EXTENT_BUFFER_STALE
, &buf
->bflags
);
6436 btrfs_set_lock_blocking(buf
);
6437 btrfs_set_buffer_uptodate(buf
);
6439 if (root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
) {
6441 * we allow two log transactions at a time, use different
6442 * EXENT bit to differentiate dirty pages.
6444 if (root
->log_transid
% 2 == 0)
6445 set_extent_dirty(&root
->dirty_log_pages
, buf
->start
,
6446 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
6448 set_extent_new(&root
->dirty_log_pages
, buf
->start
,
6449 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
6451 set_extent_dirty(&trans
->transaction
->dirty_pages
, buf
->start
,
6452 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
6454 trans
->blocks_used
++;
6455 /* this returns a buffer locked for blocking */
6459 static struct btrfs_block_rsv
*
6460 use_block_rsv(struct btrfs_trans_handle
*trans
,
6461 struct btrfs_root
*root
, u32 blocksize
)
6463 struct btrfs_block_rsv
*block_rsv
;
6464 struct btrfs_block_rsv
*global_rsv
= &root
->fs_info
->global_block_rsv
;
6467 block_rsv
= get_block_rsv(trans
, root
);
6469 if (block_rsv
->size
== 0) {
6470 ret
= reserve_metadata_bytes(root
, block_rsv
, blocksize
,
6471 BTRFS_RESERVE_NO_FLUSH
);
6473 * If we couldn't reserve metadata bytes try and use some from
6474 * the global reserve.
6476 if (ret
&& block_rsv
!= global_rsv
) {
6477 ret
= block_rsv_use_bytes(global_rsv
, blocksize
);
6480 return ERR_PTR(ret
);
6482 return ERR_PTR(ret
);
6487 ret
= block_rsv_use_bytes(block_rsv
, blocksize
);
6490 if (ret
&& !block_rsv
->failfast
) {
6491 if (btrfs_test_opt(root
, ENOSPC_DEBUG
)) {
6492 static DEFINE_RATELIMIT_STATE(_rs
,
6493 DEFAULT_RATELIMIT_INTERVAL
* 10,
6494 /*DEFAULT_RATELIMIT_BURST*/ 1);
6495 if (__ratelimit(&_rs
))
6497 "btrfs: block rsv returned %d\n", ret
);
6499 ret
= reserve_metadata_bytes(root
, block_rsv
, blocksize
,
6500 BTRFS_RESERVE_NO_FLUSH
);
6503 } else if (ret
&& block_rsv
!= global_rsv
) {
6504 ret
= block_rsv_use_bytes(global_rsv
, blocksize
);
6510 return ERR_PTR(-ENOSPC
);
6513 static void unuse_block_rsv(struct btrfs_fs_info
*fs_info
,
6514 struct btrfs_block_rsv
*block_rsv
, u32 blocksize
)
6516 block_rsv_add_bytes(block_rsv
, blocksize
, 0);
6517 block_rsv_release_bytes(fs_info
, block_rsv
, NULL
, 0);
6521 * finds a free extent and does all the dirty work required for allocation
6522 * returns the key for the extent through ins, and a tree buffer for
6523 * the first block of the extent through buf.
6525 * returns the tree buffer or NULL.
6527 struct extent_buffer
*btrfs_alloc_free_block(struct btrfs_trans_handle
*trans
,
6528 struct btrfs_root
*root
, u32 blocksize
,
6529 u64 parent
, u64 root_objectid
,
6530 struct btrfs_disk_key
*key
, int level
,
6531 u64 hint
, u64 empty_size
)
6533 struct btrfs_key ins
;
6534 struct btrfs_block_rsv
*block_rsv
;
6535 struct extent_buffer
*buf
;
6540 block_rsv
= use_block_rsv(trans
, root
, blocksize
);
6541 if (IS_ERR(block_rsv
))
6542 return ERR_CAST(block_rsv
);
6544 ret
= btrfs_reserve_extent(trans
, root
, blocksize
, blocksize
,
6545 empty_size
, hint
, &ins
, 0);
6547 unuse_block_rsv(root
->fs_info
, block_rsv
, blocksize
);
6548 return ERR_PTR(ret
);
6551 buf
= btrfs_init_new_buffer(trans
, root
, ins
.objectid
,
6553 BUG_ON(IS_ERR(buf
)); /* -ENOMEM */
6555 if (root_objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
6557 parent
= ins
.objectid
;
6558 flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
6562 if (root_objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
6563 struct btrfs_delayed_extent_op
*extent_op
;
6564 extent_op
= btrfs_alloc_delayed_extent_op();
6565 BUG_ON(!extent_op
); /* -ENOMEM */
6567 memcpy(&extent_op
->key
, key
, sizeof(extent_op
->key
));
6569 memset(&extent_op
->key
, 0, sizeof(extent_op
->key
));
6570 extent_op
->flags_to_set
= flags
;
6571 extent_op
->update_key
= 1;
6572 extent_op
->update_flags
= 1;
6573 extent_op
->is_data
= 0;
6575 ret
= btrfs_add_delayed_tree_ref(root
->fs_info
, trans
,
6577 ins
.offset
, parent
, root_objectid
,
6578 level
, BTRFS_ADD_DELAYED_EXTENT
,
6580 BUG_ON(ret
); /* -ENOMEM */
6585 struct walk_control
{
6586 u64 refs
[BTRFS_MAX_LEVEL
];
6587 u64 flags
[BTRFS_MAX_LEVEL
];
6588 struct btrfs_key update_progress
;
6599 #define DROP_REFERENCE 1
6600 #define UPDATE_BACKREF 2
6602 static noinline
void reada_walk_down(struct btrfs_trans_handle
*trans
,
6603 struct btrfs_root
*root
,
6604 struct walk_control
*wc
,
6605 struct btrfs_path
*path
)
6613 struct btrfs_key key
;
6614 struct extent_buffer
*eb
;
6619 if (path
->slots
[wc
->level
] < wc
->reada_slot
) {
6620 wc
->reada_count
= wc
->reada_count
* 2 / 3;
6621 wc
->reada_count
= max(wc
->reada_count
, 2);
6623 wc
->reada_count
= wc
->reada_count
* 3 / 2;
6624 wc
->reada_count
= min_t(int, wc
->reada_count
,
6625 BTRFS_NODEPTRS_PER_BLOCK(root
));
6628 eb
= path
->nodes
[wc
->level
];
6629 nritems
= btrfs_header_nritems(eb
);
6630 blocksize
= btrfs_level_size(root
, wc
->level
- 1);
6632 for (slot
= path
->slots
[wc
->level
]; slot
< nritems
; slot
++) {
6633 if (nread
>= wc
->reada_count
)
6637 bytenr
= btrfs_node_blockptr(eb
, slot
);
6638 generation
= btrfs_node_ptr_generation(eb
, slot
);
6640 if (slot
== path
->slots
[wc
->level
])
6643 if (wc
->stage
== UPDATE_BACKREF
&&
6644 generation
<= root
->root_key
.offset
)
6647 /* We don't lock the tree block, it's OK to be racy here */
6648 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
, blocksize
,
6650 /* We don't care about errors in readahead. */
6655 if (wc
->stage
== DROP_REFERENCE
) {
6659 if (wc
->level
== 1 &&
6660 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
6662 if (!wc
->update_ref
||
6663 generation
<= root
->root_key
.offset
)
6665 btrfs_node_key_to_cpu(eb
, &key
, slot
);
6666 ret
= btrfs_comp_cpu_keys(&key
,
6667 &wc
->update_progress
);
6671 if (wc
->level
== 1 &&
6672 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
6676 ret
= readahead_tree_block(root
, bytenr
, blocksize
,
6682 wc
->reada_slot
= slot
;
6686 * hepler to process tree block while walking down the tree.
6688 * when wc->stage == UPDATE_BACKREF, this function updates
6689 * back refs for pointers in the block.
6691 * NOTE: return value 1 means we should stop walking down.
6693 static noinline
int walk_down_proc(struct btrfs_trans_handle
*trans
,
6694 struct btrfs_root
*root
,
6695 struct btrfs_path
*path
,
6696 struct walk_control
*wc
, int lookup_info
)
6698 int level
= wc
->level
;
6699 struct extent_buffer
*eb
= path
->nodes
[level
];
6700 u64 flag
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
6703 if (wc
->stage
== UPDATE_BACKREF
&&
6704 btrfs_header_owner(eb
) != root
->root_key
.objectid
)
6708 * when reference count of tree block is 1, it won't increase
6709 * again. once full backref flag is set, we never clear it.
6712 ((wc
->stage
== DROP_REFERENCE
&& wc
->refs
[level
] != 1) ||
6713 (wc
->stage
== UPDATE_BACKREF
&& !(wc
->flags
[level
] & flag
)))) {
6714 BUG_ON(!path
->locks
[level
]);
6715 ret
= btrfs_lookup_extent_info(trans
, root
,
6719 BUG_ON(ret
== -ENOMEM
);
6722 BUG_ON(wc
->refs
[level
] == 0);
6725 if (wc
->stage
== DROP_REFERENCE
) {
6726 if (wc
->refs
[level
] > 1)
6729 if (path
->locks
[level
] && !wc
->keep_locks
) {
6730 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
6731 path
->locks
[level
] = 0;
6736 /* wc->stage == UPDATE_BACKREF */
6737 if (!(wc
->flags
[level
] & flag
)) {
6738 BUG_ON(!path
->locks
[level
]);
6739 ret
= btrfs_inc_ref(trans
, root
, eb
, 1, wc
->for_reloc
);
6740 BUG_ON(ret
); /* -ENOMEM */
6741 ret
= btrfs_dec_ref(trans
, root
, eb
, 0, wc
->for_reloc
);
6742 BUG_ON(ret
); /* -ENOMEM */
6743 ret
= btrfs_set_disk_extent_flags(trans
, root
, eb
->start
,
6745 BUG_ON(ret
); /* -ENOMEM */
6746 wc
->flags
[level
] |= flag
;
6750 * the block is shared by multiple trees, so it's not good to
6751 * keep the tree lock
6753 if (path
->locks
[level
] && level
> 0) {
6754 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
6755 path
->locks
[level
] = 0;
6761 * hepler to process tree block pointer.
6763 * when wc->stage == DROP_REFERENCE, this function checks
6764 * reference count of the block pointed to. if the block
6765 * is shared and we need update back refs for the subtree
6766 * rooted at the block, this function changes wc->stage to
6767 * UPDATE_BACKREF. if the block is shared and there is no
6768 * need to update back, this function drops the reference
6771 * NOTE: return value 1 means we should stop walking down.
6773 static noinline
int do_walk_down(struct btrfs_trans_handle
*trans
,
6774 struct btrfs_root
*root
,
6775 struct btrfs_path
*path
,
6776 struct walk_control
*wc
, int *lookup_info
)
6782 struct btrfs_key key
;
6783 struct extent_buffer
*next
;
6784 int level
= wc
->level
;
6788 generation
= btrfs_node_ptr_generation(path
->nodes
[level
],
6789 path
->slots
[level
]);
6791 * if the lower level block was created before the snapshot
6792 * was created, we know there is no need to update back refs
6795 if (wc
->stage
== UPDATE_BACKREF
&&
6796 generation
<= root
->root_key
.offset
) {
6801 bytenr
= btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]);
6802 blocksize
= btrfs_level_size(root
, level
- 1);
6804 next
= btrfs_find_tree_block(root
, bytenr
, blocksize
);
6806 next
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
6811 btrfs_tree_lock(next
);
6812 btrfs_set_lock_blocking(next
);
6814 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
, blocksize
,
6815 &wc
->refs
[level
- 1],
6816 &wc
->flags
[level
- 1]);
6818 btrfs_tree_unlock(next
);
6822 BUG_ON(wc
->refs
[level
- 1] == 0);
6825 if (wc
->stage
== DROP_REFERENCE
) {
6826 if (wc
->refs
[level
- 1] > 1) {
6828 (wc
->flags
[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF
))
6831 if (!wc
->update_ref
||
6832 generation
<= root
->root_key
.offset
)
6835 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
6836 path
->slots
[level
]);
6837 ret
= btrfs_comp_cpu_keys(&key
, &wc
->update_progress
);
6841 wc
->stage
= UPDATE_BACKREF
;
6842 wc
->shared_level
= level
- 1;
6846 (wc
->flags
[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF
))
6850 if (!btrfs_buffer_uptodate(next
, generation
, 0)) {
6851 btrfs_tree_unlock(next
);
6852 free_extent_buffer(next
);
6858 if (reada
&& level
== 1)
6859 reada_walk_down(trans
, root
, wc
, path
);
6860 next
= read_tree_block(root
, bytenr
, blocksize
, generation
);
6863 btrfs_tree_lock(next
);
6864 btrfs_set_lock_blocking(next
);
6868 BUG_ON(level
!= btrfs_header_level(next
));
6869 path
->nodes
[level
] = next
;
6870 path
->slots
[level
] = 0;
6871 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
6877 wc
->refs
[level
- 1] = 0;
6878 wc
->flags
[level
- 1] = 0;
6879 if (wc
->stage
== DROP_REFERENCE
) {
6880 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
) {
6881 parent
= path
->nodes
[level
]->start
;
6883 BUG_ON(root
->root_key
.objectid
!=
6884 btrfs_header_owner(path
->nodes
[level
]));
6888 ret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
, parent
,
6889 root
->root_key
.objectid
, level
- 1, 0, 0);
6890 BUG_ON(ret
); /* -ENOMEM */
6892 btrfs_tree_unlock(next
);
6893 free_extent_buffer(next
);
6899 * hepler to process tree block while walking up the tree.
6901 * when wc->stage == DROP_REFERENCE, this function drops
6902 * reference count on the block.
6904 * when wc->stage == UPDATE_BACKREF, this function changes
6905 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6906 * to UPDATE_BACKREF previously while processing the block.
6908 * NOTE: return value 1 means we should stop walking up.
6910 static noinline
int walk_up_proc(struct btrfs_trans_handle
*trans
,
6911 struct btrfs_root
*root
,
6912 struct btrfs_path
*path
,
6913 struct walk_control
*wc
)
6916 int level
= wc
->level
;
6917 struct extent_buffer
*eb
= path
->nodes
[level
];
6920 if (wc
->stage
== UPDATE_BACKREF
) {
6921 BUG_ON(wc
->shared_level
< level
);
6922 if (level
< wc
->shared_level
)
6925 ret
= find_next_key(path
, level
+ 1, &wc
->update_progress
);
6929 wc
->stage
= DROP_REFERENCE
;
6930 wc
->shared_level
= -1;
6931 path
->slots
[level
] = 0;
6934 * check reference count again if the block isn't locked.
6935 * we should start walking down the tree again if reference
6938 if (!path
->locks
[level
]) {
6940 btrfs_tree_lock(eb
);
6941 btrfs_set_lock_blocking(eb
);
6942 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
6944 ret
= btrfs_lookup_extent_info(trans
, root
,
6949 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
6950 path
->locks
[level
] = 0;
6953 BUG_ON(wc
->refs
[level
] == 0);
6954 if (wc
->refs
[level
] == 1) {
6955 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
6956 path
->locks
[level
] = 0;
6962 /* wc->stage == DROP_REFERENCE */
6963 BUG_ON(wc
->refs
[level
] > 1 && !path
->locks
[level
]);
6965 if (wc
->refs
[level
] == 1) {
6967 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
6968 ret
= btrfs_dec_ref(trans
, root
, eb
, 1,
6971 ret
= btrfs_dec_ref(trans
, root
, eb
, 0,
6973 BUG_ON(ret
); /* -ENOMEM */
6975 /* make block locked assertion in clean_tree_block happy */
6976 if (!path
->locks
[level
] &&
6977 btrfs_header_generation(eb
) == trans
->transid
) {
6978 btrfs_tree_lock(eb
);
6979 btrfs_set_lock_blocking(eb
);
6980 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
6982 clean_tree_block(trans
, root
, eb
);
6985 if (eb
== root
->node
) {
6986 if (wc
->flags
[level
] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
6989 BUG_ON(root
->root_key
.objectid
!=
6990 btrfs_header_owner(eb
));
6992 if (wc
->flags
[level
+ 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF
)
6993 parent
= path
->nodes
[level
+ 1]->start
;
6995 BUG_ON(root
->root_key
.objectid
!=
6996 btrfs_header_owner(path
->nodes
[level
+ 1]));
6999 btrfs_free_tree_block(trans
, root
, eb
, parent
, wc
->refs
[level
] == 1);
7001 wc
->refs
[level
] = 0;
7002 wc
->flags
[level
] = 0;
7006 static noinline
int walk_down_tree(struct btrfs_trans_handle
*trans
,
7007 struct btrfs_root
*root
,
7008 struct btrfs_path
*path
,
7009 struct walk_control
*wc
)
7011 int level
= wc
->level
;
7012 int lookup_info
= 1;
7015 while (level
>= 0) {
7016 ret
= walk_down_proc(trans
, root
, path
, wc
, lookup_info
);
7023 if (path
->slots
[level
] >=
7024 btrfs_header_nritems(path
->nodes
[level
]))
7027 ret
= do_walk_down(trans
, root
, path
, wc
, &lookup_info
);
7029 path
->slots
[level
]++;
7038 static noinline
int walk_up_tree(struct btrfs_trans_handle
*trans
,
7039 struct btrfs_root
*root
,
7040 struct btrfs_path
*path
,
7041 struct walk_control
*wc
, int max_level
)
7043 int level
= wc
->level
;
7046 path
->slots
[level
] = btrfs_header_nritems(path
->nodes
[level
]);
7047 while (level
< max_level
&& path
->nodes
[level
]) {
7049 if (path
->slots
[level
] + 1 <
7050 btrfs_header_nritems(path
->nodes
[level
])) {
7051 path
->slots
[level
]++;
7054 ret
= walk_up_proc(trans
, root
, path
, wc
);
7058 if (path
->locks
[level
]) {
7059 btrfs_tree_unlock_rw(path
->nodes
[level
],
7060 path
->locks
[level
]);
7061 path
->locks
[level
] = 0;
7063 free_extent_buffer(path
->nodes
[level
]);
7064 path
->nodes
[level
] = NULL
;
7072 * drop a subvolume tree.
7074 * this function traverses the tree freeing any blocks that only
7075 * referenced by the tree.
7077 * when a shared tree block is found. this function decreases its
7078 * reference count by one. if update_ref is true, this function
7079 * also make sure backrefs for the shared block and all lower level
7080 * blocks are properly updated.
7082 int btrfs_drop_snapshot(struct btrfs_root
*root
,
7083 struct btrfs_block_rsv
*block_rsv
, int update_ref
,
7086 struct btrfs_path
*path
;
7087 struct btrfs_trans_handle
*trans
;
7088 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
7089 struct btrfs_root_item
*root_item
= &root
->root_item
;
7090 struct walk_control
*wc
;
7091 struct btrfs_key key
;
7096 path
= btrfs_alloc_path();
7102 wc
= kzalloc(sizeof(*wc
), GFP_NOFS
);
7104 btrfs_free_path(path
);
7109 trans
= btrfs_start_transaction(tree_root
, 0);
7110 if (IS_ERR(trans
)) {
7111 err
= PTR_ERR(trans
);
7116 trans
->block_rsv
= block_rsv
;
7118 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
7119 level
= btrfs_header_level(root
->node
);
7120 path
->nodes
[level
] = btrfs_lock_root_node(root
);
7121 btrfs_set_lock_blocking(path
->nodes
[level
]);
7122 path
->slots
[level
] = 0;
7123 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
7124 memset(&wc
->update_progress
, 0,
7125 sizeof(wc
->update_progress
));
7127 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
7128 memcpy(&wc
->update_progress
, &key
,
7129 sizeof(wc
->update_progress
));
7131 level
= root_item
->drop_level
;
7133 path
->lowest_level
= level
;
7134 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
7135 path
->lowest_level
= 0;
7143 * unlock our path, this is safe because only this
7144 * function is allowed to delete this snapshot
7146 btrfs_unlock_up_safe(path
, 0);
7148 level
= btrfs_header_level(root
->node
);
7150 btrfs_tree_lock(path
->nodes
[level
]);
7151 btrfs_set_lock_blocking(path
->nodes
[level
]);
7153 ret
= btrfs_lookup_extent_info(trans
, root
,
7154 path
->nodes
[level
]->start
,
7155 path
->nodes
[level
]->len
,
7162 BUG_ON(wc
->refs
[level
] == 0);
7164 if (level
== root_item
->drop_level
)
7167 btrfs_tree_unlock(path
->nodes
[level
]);
7168 WARN_ON(wc
->refs
[level
] != 1);
7174 wc
->shared_level
= -1;
7175 wc
->stage
= DROP_REFERENCE
;
7176 wc
->update_ref
= update_ref
;
7178 wc
->for_reloc
= for_reloc
;
7179 wc
->reada_count
= BTRFS_NODEPTRS_PER_BLOCK(root
);
7182 ret
= walk_down_tree(trans
, root
, path
, wc
);
7188 ret
= walk_up_tree(trans
, root
, path
, wc
, BTRFS_MAX_LEVEL
);
7195 BUG_ON(wc
->stage
!= DROP_REFERENCE
);
7199 if (wc
->stage
== DROP_REFERENCE
) {
7201 btrfs_node_key(path
->nodes
[level
],
7202 &root_item
->drop_progress
,
7203 path
->slots
[level
]);
7204 root_item
->drop_level
= level
;
7207 BUG_ON(wc
->level
== 0);
7208 if (btrfs_should_end_transaction(trans
, tree_root
)) {
7209 ret
= btrfs_update_root(trans
, tree_root
,
7213 btrfs_abort_transaction(trans
, tree_root
, ret
);
7218 btrfs_end_transaction_throttle(trans
, tree_root
);
7219 trans
= btrfs_start_transaction(tree_root
, 0);
7220 if (IS_ERR(trans
)) {
7221 err
= PTR_ERR(trans
);
7225 trans
->block_rsv
= block_rsv
;
7228 btrfs_release_path(path
);
7232 ret
= btrfs_del_root(trans
, tree_root
, &root
->root_key
);
7234 btrfs_abort_transaction(trans
, tree_root
, ret
);
7238 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
7239 ret
= btrfs_find_last_root(tree_root
, root
->root_key
.objectid
,
7242 btrfs_abort_transaction(trans
, tree_root
, ret
);
7245 } else if (ret
> 0) {
7246 /* if we fail to delete the orphan item this time
7247 * around, it'll get picked up the next time.
7249 * The most common failure here is just -ENOENT.
7251 btrfs_del_orphan_item(trans
, tree_root
,
7252 root
->root_key
.objectid
);
7256 if (root
->in_radix
) {
7257 btrfs_free_fs_root(tree_root
->fs_info
, root
);
7259 free_extent_buffer(root
->node
);
7260 free_extent_buffer(root
->commit_root
);
7264 btrfs_end_transaction_throttle(trans
, tree_root
);
7267 btrfs_free_path(path
);
7270 btrfs_std_error(root
->fs_info
, err
);
7275 * drop subtree rooted at tree block 'node'.
7277 * NOTE: this function will unlock and release tree block 'node'
7278 * only used by relocation code
7280 int btrfs_drop_subtree(struct btrfs_trans_handle
*trans
,
7281 struct btrfs_root
*root
,
7282 struct extent_buffer
*node
,
7283 struct extent_buffer
*parent
)
7285 struct btrfs_path
*path
;
7286 struct walk_control
*wc
;
7292 BUG_ON(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
7294 path
= btrfs_alloc_path();
7298 wc
= kzalloc(sizeof(*wc
), GFP_NOFS
);
7300 btrfs_free_path(path
);
7304 btrfs_assert_tree_locked(parent
);
7305 parent_level
= btrfs_header_level(parent
);
7306 extent_buffer_get(parent
);
7307 path
->nodes
[parent_level
] = parent
;
7308 path
->slots
[parent_level
] = btrfs_header_nritems(parent
);
7310 btrfs_assert_tree_locked(node
);
7311 level
= btrfs_header_level(node
);
7312 path
->nodes
[level
] = node
;
7313 path
->slots
[level
] = 0;
7314 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
7316 wc
->refs
[parent_level
] = 1;
7317 wc
->flags
[parent_level
] = BTRFS_BLOCK_FLAG_FULL_BACKREF
;
7319 wc
->shared_level
= -1;
7320 wc
->stage
= DROP_REFERENCE
;
7324 wc
->reada_count
= BTRFS_NODEPTRS_PER_BLOCK(root
);
7327 wret
= walk_down_tree(trans
, root
, path
, wc
);
7333 wret
= walk_up_tree(trans
, root
, path
, wc
, parent_level
);
7341 btrfs_free_path(path
);
7345 static u64
update_block_group_flags(struct btrfs_root
*root
, u64 flags
)
7351 * if restripe for this chunk_type is on pick target profile and
7352 * return, otherwise do the usual balance
7354 stripped
= get_restripe_target(root
->fs_info
, flags
);
7356 return extended_to_chunk(stripped
);
7359 * we add in the count of missing devices because we want
7360 * to make sure that any RAID levels on a degraded FS
7361 * continue to be honored.
7363 num_devices
= root
->fs_info
->fs_devices
->rw_devices
+
7364 root
->fs_info
->fs_devices
->missing_devices
;
7366 stripped
= BTRFS_BLOCK_GROUP_RAID0
|
7367 BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
|
7368 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
;
7370 if (num_devices
== 1) {
7371 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
7372 stripped
= flags
& ~stripped
;
7374 /* turn raid0 into single device chunks */
7375 if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
7378 /* turn mirroring into duplication */
7379 if (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
7380 BTRFS_BLOCK_GROUP_RAID10
))
7381 return stripped
| BTRFS_BLOCK_GROUP_DUP
;
7383 /* they already had raid on here, just return */
7384 if (flags
& stripped
)
7387 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
7388 stripped
= flags
& ~stripped
;
7390 /* switch duplicated blocks with raid1 */
7391 if (flags
& BTRFS_BLOCK_GROUP_DUP
)
7392 return stripped
| BTRFS_BLOCK_GROUP_RAID1
;
7394 /* this is drive concat, leave it alone */
7400 static int set_block_group_ro(struct btrfs_block_group_cache
*cache
, int force
)
7402 struct btrfs_space_info
*sinfo
= cache
->space_info
;
7404 u64 min_allocable_bytes
;
7409 * We need some metadata space and system metadata space for
7410 * allocating chunks in some corner cases until we force to set
7411 * it to be readonly.
7414 (BTRFS_BLOCK_GROUP_SYSTEM
| BTRFS_BLOCK_GROUP_METADATA
)) &&
7416 min_allocable_bytes
= 1 * 1024 * 1024;
7418 min_allocable_bytes
= 0;
7420 spin_lock(&sinfo
->lock
);
7421 spin_lock(&cache
->lock
);
7428 num_bytes
= cache
->key
.offset
- cache
->reserved
- cache
->pinned
-
7429 cache
->bytes_super
- btrfs_block_group_used(&cache
->item
);
7431 if (sinfo
->bytes_used
+ sinfo
->bytes_reserved
+ sinfo
->bytes_pinned
+
7432 sinfo
->bytes_may_use
+ sinfo
->bytes_readonly
+ num_bytes
+
7433 min_allocable_bytes
<= sinfo
->total_bytes
) {
7434 sinfo
->bytes_readonly
+= num_bytes
;
7439 spin_unlock(&cache
->lock
);
7440 spin_unlock(&sinfo
->lock
);
7444 int btrfs_set_block_group_ro(struct btrfs_root
*root
,
7445 struct btrfs_block_group_cache
*cache
)
7448 struct btrfs_trans_handle
*trans
;
7454 trans
= btrfs_join_transaction(root
);
7456 return PTR_ERR(trans
);
7458 alloc_flags
= update_block_group_flags(root
, cache
->flags
);
7459 if (alloc_flags
!= cache
->flags
) {
7460 ret
= do_chunk_alloc(trans
, root
, alloc_flags
,
7466 ret
= set_block_group_ro(cache
, 0);
7469 alloc_flags
= get_alloc_profile(root
, cache
->space_info
->flags
);
7470 ret
= do_chunk_alloc(trans
, root
, alloc_flags
,
7474 ret
= set_block_group_ro(cache
, 0);
7476 btrfs_end_transaction(trans
, root
);
7480 int btrfs_force_chunk_alloc(struct btrfs_trans_handle
*trans
,
7481 struct btrfs_root
*root
, u64 type
)
7483 u64 alloc_flags
= get_alloc_profile(root
, type
);
7484 return do_chunk_alloc(trans
, root
, alloc_flags
,
7489 * helper to account the unused space of all the readonly block group in the
7490 * list. takes mirrors into account.
7492 static u64
__btrfs_get_ro_block_group_free_space(struct list_head
*groups_list
)
7494 struct btrfs_block_group_cache
*block_group
;
7498 list_for_each_entry(block_group
, groups_list
, list
) {
7499 spin_lock(&block_group
->lock
);
7501 if (!block_group
->ro
) {
7502 spin_unlock(&block_group
->lock
);
7506 if (block_group
->flags
& (BTRFS_BLOCK_GROUP_RAID1
|
7507 BTRFS_BLOCK_GROUP_RAID10
|
7508 BTRFS_BLOCK_GROUP_DUP
))
7513 free_bytes
+= (block_group
->key
.offset
-
7514 btrfs_block_group_used(&block_group
->item
)) *
7517 spin_unlock(&block_group
->lock
);
7524 * helper to account the unused space of all the readonly block group in the
7525 * space_info. takes mirrors into account.
7527 u64
btrfs_account_ro_block_groups_free_space(struct btrfs_space_info
*sinfo
)
7532 spin_lock(&sinfo
->lock
);
7534 for(i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++)
7535 if (!list_empty(&sinfo
->block_groups
[i
]))
7536 free_bytes
+= __btrfs_get_ro_block_group_free_space(
7537 &sinfo
->block_groups
[i
]);
7539 spin_unlock(&sinfo
->lock
);
7544 void btrfs_set_block_group_rw(struct btrfs_root
*root
,
7545 struct btrfs_block_group_cache
*cache
)
7547 struct btrfs_space_info
*sinfo
= cache
->space_info
;
7552 spin_lock(&sinfo
->lock
);
7553 spin_lock(&cache
->lock
);
7554 num_bytes
= cache
->key
.offset
- cache
->reserved
- cache
->pinned
-
7555 cache
->bytes_super
- btrfs_block_group_used(&cache
->item
);
7556 sinfo
->bytes_readonly
-= num_bytes
;
7558 spin_unlock(&cache
->lock
);
7559 spin_unlock(&sinfo
->lock
);
7563 * checks to see if its even possible to relocate this block group.
7565 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7566 * ok to go ahead and try.
7568 int btrfs_can_relocate(struct btrfs_root
*root
, u64 bytenr
)
7570 struct btrfs_block_group_cache
*block_group
;
7571 struct btrfs_space_info
*space_info
;
7572 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
7573 struct btrfs_device
*device
;
7582 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
7584 /* odd, couldn't find the block group, leave it alone */
7588 min_free
= btrfs_block_group_used(&block_group
->item
);
7590 /* no bytes used, we're good */
7594 space_info
= block_group
->space_info
;
7595 spin_lock(&space_info
->lock
);
7597 full
= space_info
->full
;
7600 * if this is the last block group we have in this space, we can't
7601 * relocate it unless we're able to allocate a new chunk below.
7603 * Otherwise, we need to make sure we have room in the space to handle
7604 * all of the extents from this block group. If we can, we're good
7606 if ((space_info
->total_bytes
!= block_group
->key
.offset
) &&
7607 (space_info
->bytes_used
+ space_info
->bytes_reserved
+
7608 space_info
->bytes_pinned
+ space_info
->bytes_readonly
+
7609 min_free
< space_info
->total_bytes
)) {
7610 spin_unlock(&space_info
->lock
);
7613 spin_unlock(&space_info
->lock
);
7616 * ok we don't have enough space, but maybe we have free space on our
7617 * devices to allocate new chunks for relocation, so loop through our
7618 * alloc devices and guess if we have enough space. if this block
7619 * group is going to be restriped, run checks against the target
7620 * profile instead of the current one.
7632 target
= get_restripe_target(root
->fs_info
, block_group
->flags
);
7634 index
= __get_raid_index(extended_to_chunk(target
));
7637 * this is just a balance, so if we were marked as full
7638 * we know there is no space for a new chunk
7643 index
= get_block_group_index(block_group
);
7646 if (index
== BTRFS_RAID_RAID10
) {
7650 } else if (index
== BTRFS_RAID_RAID1
) {
7652 } else if (index
== BTRFS_RAID_DUP
) {
7655 } else if (index
== BTRFS_RAID_RAID0
) {
7656 dev_min
= fs_devices
->rw_devices
;
7657 do_div(min_free
, dev_min
);
7660 mutex_lock(&root
->fs_info
->chunk_mutex
);
7661 list_for_each_entry(device
, &fs_devices
->alloc_list
, dev_alloc_list
) {
7665 * check to make sure we can actually find a chunk with enough
7666 * space to fit our block group in.
7668 if (device
->total_bytes
> device
->bytes_used
+ min_free
&&
7669 !device
->is_tgtdev_for_dev_replace
) {
7670 ret
= find_free_dev_extent(device
, min_free
,
7675 if (dev_nr
>= dev_min
)
7681 mutex_unlock(&root
->fs_info
->chunk_mutex
);
7683 btrfs_put_block_group(block_group
);
7687 static int find_first_block_group(struct btrfs_root
*root
,
7688 struct btrfs_path
*path
, struct btrfs_key
*key
)
7691 struct btrfs_key found_key
;
7692 struct extent_buffer
*leaf
;
7695 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
7700 slot
= path
->slots
[0];
7701 leaf
= path
->nodes
[0];
7702 if (slot
>= btrfs_header_nritems(leaf
)) {
7703 ret
= btrfs_next_leaf(root
, path
);
7710 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
7712 if (found_key
.objectid
>= key
->objectid
&&
7713 found_key
.type
== BTRFS_BLOCK_GROUP_ITEM_KEY
) {
7723 void btrfs_put_block_group_cache(struct btrfs_fs_info
*info
)
7725 struct btrfs_block_group_cache
*block_group
;
7729 struct inode
*inode
;
7731 block_group
= btrfs_lookup_first_block_group(info
, last
);
7732 while (block_group
) {
7733 spin_lock(&block_group
->lock
);
7734 if (block_group
->iref
)
7736 spin_unlock(&block_group
->lock
);
7737 block_group
= next_block_group(info
->tree_root
,
7747 inode
= block_group
->inode
;
7748 block_group
->iref
= 0;
7749 block_group
->inode
= NULL
;
7750 spin_unlock(&block_group
->lock
);
7752 last
= block_group
->key
.objectid
+ block_group
->key
.offset
;
7753 btrfs_put_block_group(block_group
);
7757 int btrfs_free_block_groups(struct btrfs_fs_info
*info
)
7759 struct btrfs_block_group_cache
*block_group
;
7760 struct btrfs_space_info
*space_info
;
7761 struct btrfs_caching_control
*caching_ctl
;
7764 down_write(&info
->extent_commit_sem
);
7765 while (!list_empty(&info
->caching_block_groups
)) {
7766 caching_ctl
= list_entry(info
->caching_block_groups
.next
,
7767 struct btrfs_caching_control
, list
);
7768 list_del(&caching_ctl
->list
);
7769 put_caching_control(caching_ctl
);
7771 up_write(&info
->extent_commit_sem
);
7773 spin_lock(&info
->block_group_cache_lock
);
7774 while ((n
= rb_last(&info
->block_group_cache_tree
)) != NULL
) {
7775 block_group
= rb_entry(n
, struct btrfs_block_group_cache
,
7777 rb_erase(&block_group
->cache_node
,
7778 &info
->block_group_cache_tree
);
7779 spin_unlock(&info
->block_group_cache_lock
);
7781 down_write(&block_group
->space_info
->groups_sem
);
7782 list_del(&block_group
->list
);
7783 up_write(&block_group
->space_info
->groups_sem
);
7785 if (block_group
->cached
== BTRFS_CACHE_STARTED
)
7786 wait_block_group_cache_done(block_group
);
7789 * We haven't cached this block group, which means we could
7790 * possibly have excluded extents on this block group.
7792 if (block_group
->cached
== BTRFS_CACHE_NO
)
7793 free_excluded_extents(info
->extent_root
, block_group
);
7795 btrfs_remove_free_space_cache(block_group
);
7796 btrfs_put_block_group(block_group
);
7798 spin_lock(&info
->block_group_cache_lock
);
7800 spin_unlock(&info
->block_group_cache_lock
);
7802 /* now that all the block groups are freed, go through and
7803 * free all the space_info structs. This is only called during
7804 * the final stages of unmount, and so we know nobody is
7805 * using them. We call synchronize_rcu() once before we start,
7806 * just to be on the safe side.
7810 release_global_block_rsv(info
);
7812 while(!list_empty(&info
->space_info
)) {
7813 space_info
= list_entry(info
->space_info
.next
,
7814 struct btrfs_space_info
,
7816 if (btrfs_test_opt(info
->tree_root
, ENOSPC_DEBUG
)) {
7817 if (space_info
->bytes_pinned
> 0 ||
7818 space_info
->bytes_reserved
> 0 ||
7819 space_info
->bytes_may_use
> 0) {
7821 dump_space_info(space_info
, 0, 0);
7824 list_del(&space_info
->list
);
7830 static void __link_block_group(struct btrfs_space_info
*space_info
,
7831 struct btrfs_block_group_cache
*cache
)
7833 int index
= get_block_group_index(cache
);
7835 down_write(&space_info
->groups_sem
);
7836 list_add_tail(&cache
->list
, &space_info
->block_groups
[index
]);
7837 up_write(&space_info
->groups_sem
);
7840 int btrfs_read_block_groups(struct btrfs_root
*root
)
7842 struct btrfs_path
*path
;
7844 struct btrfs_block_group_cache
*cache
;
7845 struct btrfs_fs_info
*info
= root
->fs_info
;
7846 struct btrfs_space_info
*space_info
;
7847 struct btrfs_key key
;
7848 struct btrfs_key found_key
;
7849 struct extent_buffer
*leaf
;
7853 root
= info
->extent_root
;
7856 btrfs_set_key_type(&key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
7857 path
= btrfs_alloc_path();
7862 cache_gen
= btrfs_super_cache_generation(root
->fs_info
->super_copy
);
7863 if (btrfs_test_opt(root
, SPACE_CACHE
) &&
7864 btrfs_super_generation(root
->fs_info
->super_copy
) != cache_gen
)
7866 if (btrfs_test_opt(root
, CLEAR_CACHE
))
7870 ret
= find_first_block_group(root
, path
, &key
);
7875 leaf
= path
->nodes
[0];
7876 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
7877 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
7882 cache
->free_space_ctl
= kzalloc(sizeof(*cache
->free_space_ctl
),
7884 if (!cache
->free_space_ctl
) {
7890 atomic_set(&cache
->count
, 1);
7891 spin_lock_init(&cache
->lock
);
7892 cache
->fs_info
= info
;
7893 INIT_LIST_HEAD(&cache
->list
);
7894 INIT_LIST_HEAD(&cache
->cluster_list
);
7898 * When we mount with old space cache, we need to
7899 * set BTRFS_DC_CLEAR and set dirty flag.
7901 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
7902 * truncate the old free space cache inode and
7904 * b) Setting 'dirty flag' makes sure that we flush
7905 * the new space cache info onto disk.
7907 cache
->disk_cache_state
= BTRFS_DC_CLEAR
;
7908 if (btrfs_test_opt(root
, SPACE_CACHE
))
7912 read_extent_buffer(leaf
, &cache
->item
,
7913 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
7914 sizeof(cache
->item
));
7915 memcpy(&cache
->key
, &found_key
, sizeof(found_key
));
7917 key
.objectid
= found_key
.objectid
+ found_key
.offset
;
7918 btrfs_release_path(path
);
7919 cache
->flags
= btrfs_block_group_flags(&cache
->item
);
7920 cache
->sectorsize
= root
->sectorsize
;
7921 cache
->full_stripe_len
= btrfs_full_stripe_len(root
,
7922 &root
->fs_info
->mapping_tree
,
7923 found_key
.objectid
);
7924 btrfs_init_free_space_ctl(cache
);
7927 * We need to exclude the super stripes now so that the space
7928 * info has super bytes accounted for, otherwise we'll think
7929 * we have more space than we actually do.
7931 exclude_super_stripes(root
, cache
);
7934 * check for two cases, either we are full, and therefore
7935 * don't need to bother with the caching work since we won't
7936 * find any space, or we are empty, and we can just add all
7937 * the space in and be done with it. This saves us _alot_ of
7938 * time, particularly in the full case.
7940 if (found_key
.offset
== btrfs_block_group_used(&cache
->item
)) {
7941 cache
->last_byte_to_unpin
= (u64
)-1;
7942 cache
->cached
= BTRFS_CACHE_FINISHED
;
7943 free_excluded_extents(root
, cache
);
7944 } else if (btrfs_block_group_used(&cache
->item
) == 0) {
7945 cache
->last_byte_to_unpin
= (u64
)-1;
7946 cache
->cached
= BTRFS_CACHE_FINISHED
;
7947 add_new_free_space(cache
, root
->fs_info
,
7949 found_key
.objectid
+
7951 free_excluded_extents(root
, cache
);
7954 ret
= update_space_info(info
, cache
->flags
, found_key
.offset
,
7955 btrfs_block_group_used(&cache
->item
),
7957 BUG_ON(ret
); /* -ENOMEM */
7958 cache
->space_info
= space_info
;
7959 spin_lock(&cache
->space_info
->lock
);
7960 cache
->space_info
->bytes_readonly
+= cache
->bytes_super
;
7961 spin_unlock(&cache
->space_info
->lock
);
7963 __link_block_group(space_info
, cache
);
7965 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
7966 BUG_ON(ret
); /* Logic error */
7968 set_avail_alloc_bits(root
->fs_info
, cache
->flags
);
7969 if (btrfs_chunk_readonly(root
, cache
->key
.objectid
))
7970 set_block_group_ro(cache
, 1);
7973 list_for_each_entry_rcu(space_info
, &root
->fs_info
->space_info
, list
) {
7974 if (!(get_alloc_profile(root
, space_info
->flags
) &
7975 (BTRFS_BLOCK_GROUP_RAID10
|
7976 BTRFS_BLOCK_GROUP_RAID1
|
7977 BTRFS_BLOCK_GROUP_RAID5
|
7978 BTRFS_BLOCK_GROUP_RAID6
|
7979 BTRFS_BLOCK_GROUP_DUP
)))
7982 * avoid allocating from un-mirrored block group if there are
7983 * mirrored block groups.
7985 list_for_each_entry(cache
, &space_info
->block_groups
[3], list
)
7986 set_block_group_ro(cache
, 1);
7987 list_for_each_entry(cache
, &space_info
->block_groups
[4], list
)
7988 set_block_group_ro(cache
, 1);
7991 init_global_block_rsv(info
);
7994 btrfs_free_path(path
);
7998 void btrfs_create_pending_block_groups(struct btrfs_trans_handle
*trans
,
7999 struct btrfs_root
*root
)
8001 struct btrfs_block_group_cache
*block_group
, *tmp
;
8002 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
8003 struct btrfs_block_group_item item
;
8004 struct btrfs_key key
;
8007 list_for_each_entry_safe(block_group
, tmp
, &trans
->new_bgs
,
8009 list_del_init(&block_group
->new_bg_list
);
8014 spin_lock(&block_group
->lock
);
8015 memcpy(&item
, &block_group
->item
, sizeof(item
));
8016 memcpy(&key
, &block_group
->key
, sizeof(key
));
8017 spin_unlock(&block_group
->lock
);
8019 ret
= btrfs_insert_item(trans
, extent_root
, &key
, &item
,
8022 btrfs_abort_transaction(trans
, extent_root
, ret
);
8026 int btrfs_make_block_group(struct btrfs_trans_handle
*trans
,
8027 struct btrfs_root
*root
, u64 bytes_used
,
8028 u64 type
, u64 chunk_objectid
, u64 chunk_offset
,
8032 struct btrfs_root
*extent_root
;
8033 struct btrfs_block_group_cache
*cache
;
8035 extent_root
= root
->fs_info
->extent_root
;
8037 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
8039 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
8042 cache
->free_space_ctl
= kzalloc(sizeof(*cache
->free_space_ctl
),
8044 if (!cache
->free_space_ctl
) {
8049 cache
->key
.objectid
= chunk_offset
;
8050 cache
->key
.offset
= size
;
8051 cache
->key
.type
= BTRFS_BLOCK_GROUP_ITEM_KEY
;
8052 cache
->sectorsize
= root
->sectorsize
;
8053 cache
->fs_info
= root
->fs_info
;
8054 cache
->full_stripe_len
= btrfs_full_stripe_len(root
,
8055 &root
->fs_info
->mapping_tree
,
8058 atomic_set(&cache
->count
, 1);
8059 spin_lock_init(&cache
->lock
);
8060 INIT_LIST_HEAD(&cache
->list
);
8061 INIT_LIST_HEAD(&cache
->cluster_list
);
8062 INIT_LIST_HEAD(&cache
->new_bg_list
);
8064 btrfs_init_free_space_ctl(cache
);
8066 btrfs_set_block_group_used(&cache
->item
, bytes_used
);
8067 btrfs_set_block_group_chunk_objectid(&cache
->item
, chunk_objectid
);
8068 cache
->flags
= type
;
8069 btrfs_set_block_group_flags(&cache
->item
, type
);
8071 cache
->last_byte_to_unpin
= (u64
)-1;
8072 cache
->cached
= BTRFS_CACHE_FINISHED
;
8073 exclude_super_stripes(root
, cache
);
8075 add_new_free_space(cache
, root
->fs_info
, chunk_offset
,
8076 chunk_offset
+ size
);
8078 free_excluded_extents(root
, cache
);
8080 ret
= update_space_info(root
->fs_info
, cache
->flags
, size
, bytes_used
,
8081 &cache
->space_info
);
8082 BUG_ON(ret
); /* -ENOMEM */
8083 update_global_block_rsv(root
->fs_info
);
8085 spin_lock(&cache
->space_info
->lock
);
8086 cache
->space_info
->bytes_readonly
+= cache
->bytes_super
;
8087 spin_unlock(&cache
->space_info
->lock
);
8089 __link_block_group(cache
->space_info
, cache
);
8091 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
8092 BUG_ON(ret
); /* Logic error */
8094 list_add_tail(&cache
->new_bg_list
, &trans
->new_bgs
);
8096 set_avail_alloc_bits(extent_root
->fs_info
, type
);
8101 static void clear_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
8103 u64 extra_flags
= chunk_to_extended(flags
) &
8104 BTRFS_EXTENDED_PROFILE_MASK
;
8106 write_seqlock(&fs_info
->profiles_lock
);
8107 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
8108 fs_info
->avail_data_alloc_bits
&= ~extra_flags
;
8109 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
8110 fs_info
->avail_metadata_alloc_bits
&= ~extra_flags
;
8111 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
8112 fs_info
->avail_system_alloc_bits
&= ~extra_flags
;
8113 write_sequnlock(&fs_info
->profiles_lock
);
8116 int btrfs_remove_block_group(struct btrfs_trans_handle
*trans
,
8117 struct btrfs_root
*root
, u64 group_start
)
8119 struct btrfs_path
*path
;
8120 struct btrfs_block_group_cache
*block_group
;
8121 struct btrfs_free_cluster
*cluster
;
8122 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
8123 struct btrfs_key key
;
8124 struct inode
*inode
;
8129 root
= root
->fs_info
->extent_root
;
8131 block_group
= btrfs_lookup_block_group(root
->fs_info
, group_start
);
8132 BUG_ON(!block_group
);
8133 BUG_ON(!block_group
->ro
);
8136 * Free the reserved super bytes from this block group before
8139 free_excluded_extents(root
, block_group
);
8141 memcpy(&key
, &block_group
->key
, sizeof(key
));
8142 index
= get_block_group_index(block_group
);
8143 if (block_group
->flags
& (BTRFS_BLOCK_GROUP_DUP
|
8144 BTRFS_BLOCK_GROUP_RAID1
|
8145 BTRFS_BLOCK_GROUP_RAID10
))
8150 /* make sure this block group isn't part of an allocation cluster */
8151 cluster
= &root
->fs_info
->data_alloc_cluster
;
8152 spin_lock(&cluster
->refill_lock
);
8153 btrfs_return_cluster_to_free_space(block_group
, cluster
);
8154 spin_unlock(&cluster
->refill_lock
);
8157 * make sure this block group isn't part of a metadata
8158 * allocation cluster
8160 cluster
= &root
->fs_info
->meta_alloc_cluster
;
8161 spin_lock(&cluster
->refill_lock
);
8162 btrfs_return_cluster_to_free_space(block_group
, cluster
);
8163 spin_unlock(&cluster
->refill_lock
);
8165 path
= btrfs_alloc_path();
8171 inode
= lookup_free_space_inode(tree_root
, block_group
, path
);
8172 if (!IS_ERR(inode
)) {
8173 ret
= btrfs_orphan_add(trans
, inode
);
8175 btrfs_add_delayed_iput(inode
);
8179 /* One for the block groups ref */
8180 spin_lock(&block_group
->lock
);
8181 if (block_group
->iref
) {
8182 block_group
->iref
= 0;
8183 block_group
->inode
= NULL
;
8184 spin_unlock(&block_group
->lock
);
8187 spin_unlock(&block_group
->lock
);
8189 /* One for our lookup ref */
8190 btrfs_add_delayed_iput(inode
);
8193 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
8194 key
.offset
= block_group
->key
.objectid
;
8197 ret
= btrfs_search_slot(trans
, tree_root
, &key
, path
, -1, 1);
8201 btrfs_release_path(path
);
8203 ret
= btrfs_del_item(trans
, tree_root
, path
);
8206 btrfs_release_path(path
);
8209 spin_lock(&root
->fs_info
->block_group_cache_lock
);
8210 rb_erase(&block_group
->cache_node
,
8211 &root
->fs_info
->block_group_cache_tree
);
8213 if (root
->fs_info
->first_logical_byte
== block_group
->key
.objectid
)
8214 root
->fs_info
->first_logical_byte
= (u64
)-1;
8215 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
8217 down_write(&block_group
->space_info
->groups_sem
);
8219 * we must use list_del_init so people can check to see if they
8220 * are still on the list after taking the semaphore
8222 list_del_init(&block_group
->list
);
8223 if (list_empty(&block_group
->space_info
->block_groups
[index
]))
8224 clear_avail_alloc_bits(root
->fs_info
, block_group
->flags
);
8225 up_write(&block_group
->space_info
->groups_sem
);
8227 if (block_group
->cached
== BTRFS_CACHE_STARTED
)
8228 wait_block_group_cache_done(block_group
);
8230 btrfs_remove_free_space_cache(block_group
);
8232 spin_lock(&block_group
->space_info
->lock
);
8233 block_group
->space_info
->total_bytes
-= block_group
->key
.offset
;
8234 block_group
->space_info
->bytes_readonly
-= block_group
->key
.offset
;
8235 block_group
->space_info
->disk_total
-= block_group
->key
.offset
* factor
;
8236 spin_unlock(&block_group
->space_info
->lock
);
8238 memcpy(&key
, &block_group
->key
, sizeof(key
));
8240 btrfs_clear_space_info_full(root
->fs_info
);
8242 btrfs_put_block_group(block_group
);
8243 btrfs_put_block_group(block_group
);
8245 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
8251 ret
= btrfs_del_item(trans
, root
, path
);
8253 btrfs_free_path(path
);
8257 int btrfs_init_space_info(struct btrfs_fs_info
*fs_info
)
8259 struct btrfs_space_info
*space_info
;
8260 struct btrfs_super_block
*disk_super
;
8266 disk_super
= fs_info
->super_copy
;
8267 if (!btrfs_super_root(disk_super
))
8270 features
= btrfs_super_incompat_flags(disk_super
);
8271 if (features
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
8274 flags
= BTRFS_BLOCK_GROUP_SYSTEM
;
8275 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
8280 flags
= BTRFS_BLOCK_GROUP_METADATA
| BTRFS_BLOCK_GROUP_DATA
;
8281 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
8283 flags
= BTRFS_BLOCK_GROUP_METADATA
;
8284 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
8288 flags
= BTRFS_BLOCK_GROUP_DATA
;
8289 ret
= update_space_info(fs_info
, flags
, 0, 0, &space_info
);
8295 int btrfs_error_unpin_extent_range(struct btrfs_root
*root
, u64 start
, u64 end
)
8297 return unpin_extent_range(root
, start
, end
);
8300 int btrfs_error_discard_extent(struct btrfs_root
*root
, u64 bytenr
,
8301 u64 num_bytes
, u64
*actual_bytes
)
8303 return btrfs_discard_extent(root
, bytenr
, num_bytes
, actual_bytes
);
8306 int btrfs_trim_fs(struct btrfs_root
*root
, struct fstrim_range
*range
)
8308 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
8309 struct btrfs_block_group_cache
*cache
= NULL
;
8314 u64 total_bytes
= btrfs_super_total_bytes(fs_info
->super_copy
);
8318 * try to trim all FS space, our block group may start from non-zero.
8320 if (range
->len
== total_bytes
)
8321 cache
= btrfs_lookup_first_block_group(fs_info
, range
->start
);
8323 cache
= btrfs_lookup_block_group(fs_info
, range
->start
);
8326 if (cache
->key
.objectid
>= (range
->start
+ range
->len
)) {
8327 btrfs_put_block_group(cache
);
8331 start
= max(range
->start
, cache
->key
.objectid
);
8332 end
= min(range
->start
+ range
->len
,
8333 cache
->key
.objectid
+ cache
->key
.offset
);
8335 if (end
- start
>= range
->minlen
) {
8336 if (!block_group_cache_done(cache
)) {
8337 ret
= cache_block_group(cache
, 0);
8339 wait_block_group_cache_done(cache
);
8341 ret
= btrfs_trim_block_group(cache
,
8347 trimmed
+= group_trimmed
;
8349 btrfs_put_block_group(cache
);
8354 cache
= next_block_group(fs_info
->tree_root
, cache
);
8357 range
->len
= trimmed
;