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>
29 #include "print-tree.h"
30 #include "transaction.h"
33 #include "ref-cache.h"
35 #define PENDING_EXTENT_INSERT 0
36 #define PENDING_EXTENT_DELETE 1
37 #define PENDING_BACKREF_UPDATE 2
39 struct pending_extent_op
{
48 struct list_head list
;
52 static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle
*trans
,
53 struct btrfs_root
*root
, u64 parent
,
54 u64 root_objectid
, u64 ref_generation
,
55 u64 owner
, struct btrfs_key
*ins
,
57 static int update_reserved_extents(struct btrfs_root
*root
,
58 u64 bytenr
, u64 num
, int reserve
);
59 static int pin_down_bytes(struct btrfs_trans_handle
*trans
,
60 struct btrfs_root
*root
,
61 u64 bytenr
, u64 num_bytes
, int is_data
);
62 static int update_block_group(struct btrfs_trans_handle
*trans
,
63 struct btrfs_root
*root
,
64 u64 bytenr
, u64 num_bytes
, int alloc
,
66 static noinline
int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
67 struct btrfs_root
*root
,
68 u64 bytenr
, u64 num_bytes
, u64 parent
,
69 u64 root_objectid
, u64 ref_generation
,
70 u64 owner_objectid
, int pin
,
73 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
74 struct btrfs_root
*extent_root
, u64 alloc_bytes
,
75 u64 flags
, int force
);
77 static int block_group_bits(struct btrfs_block_group_cache
*cache
, u64 bits
)
79 return (cache
->flags
& bits
) == bits
;
83 * this adds the block group to the fs_info rb tree for the block group
86 static int btrfs_add_block_group_cache(struct btrfs_fs_info
*info
,
87 struct btrfs_block_group_cache
*block_group
)
90 struct rb_node
*parent
= NULL
;
91 struct btrfs_block_group_cache
*cache
;
93 spin_lock(&info
->block_group_cache_lock
);
94 p
= &info
->block_group_cache_tree
.rb_node
;
98 cache
= rb_entry(parent
, struct btrfs_block_group_cache
,
100 if (block_group
->key
.objectid
< cache
->key
.objectid
) {
102 } else if (block_group
->key
.objectid
> cache
->key
.objectid
) {
105 spin_unlock(&info
->block_group_cache_lock
);
110 rb_link_node(&block_group
->cache_node
, parent
, p
);
111 rb_insert_color(&block_group
->cache_node
,
112 &info
->block_group_cache_tree
);
113 spin_unlock(&info
->block_group_cache_lock
);
119 * This will return the block group at or after bytenr if contains is 0, else
120 * it will return the block group that contains the bytenr
122 static struct btrfs_block_group_cache
*
123 block_group_cache_tree_search(struct btrfs_fs_info
*info
, u64 bytenr
,
126 struct btrfs_block_group_cache
*cache
, *ret
= NULL
;
130 spin_lock(&info
->block_group_cache_lock
);
131 n
= info
->block_group_cache_tree
.rb_node
;
134 cache
= rb_entry(n
, struct btrfs_block_group_cache
,
136 end
= cache
->key
.objectid
+ cache
->key
.offset
- 1;
137 start
= cache
->key
.objectid
;
139 if (bytenr
< start
) {
140 if (!contains
&& (!ret
|| start
< ret
->key
.objectid
))
143 } else if (bytenr
> start
) {
144 if (contains
&& bytenr
<= end
) {
155 atomic_inc(&ret
->count
);
156 spin_unlock(&info
->block_group_cache_lock
);
162 * this is only called by cache_block_group, since we could have freed extents
163 * we need to check the pinned_extents for any extents that can't be used yet
164 * since their free space will be released as soon as the transaction commits.
166 static int add_new_free_space(struct btrfs_block_group_cache
*block_group
,
167 struct btrfs_fs_info
*info
, u64 start
, u64 end
)
169 u64 extent_start
, extent_end
, size
;
172 mutex_lock(&info
->pinned_mutex
);
173 while (start
< end
) {
174 ret
= find_first_extent_bit(&info
->pinned_extents
, start
,
175 &extent_start
, &extent_end
,
180 if (extent_start
== start
) {
181 start
= extent_end
+ 1;
182 } else if (extent_start
> start
&& extent_start
< end
) {
183 size
= extent_start
- start
;
184 ret
= btrfs_add_free_space(block_group
, start
,
187 start
= extent_end
+ 1;
195 ret
= btrfs_add_free_space(block_group
, start
, size
);
198 mutex_unlock(&info
->pinned_mutex
);
203 static int remove_sb_from_cache(struct btrfs_root
*root
,
204 struct btrfs_block_group_cache
*cache
)
211 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
212 bytenr
= btrfs_sb_offset(i
);
213 ret
= btrfs_rmap_block(&root
->fs_info
->mapping_tree
,
214 cache
->key
.objectid
, bytenr
, 0,
215 &logical
, &nr
, &stripe_len
);
218 btrfs_remove_free_space(cache
, logical
[nr
],
226 static int cache_block_group(struct btrfs_root
*root
,
227 struct btrfs_block_group_cache
*block_group
)
229 struct btrfs_path
*path
;
231 struct btrfs_key key
;
232 struct extent_buffer
*leaf
;
239 root
= root
->fs_info
->extent_root
;
241 if (block_group
->cached
)
244 path
= btrfs_alloc_path();
250 * we get into deadlocks with paths held by callers of this function.
251 * since the alloc_mutex is protecting things right now, just
252 * skip the locking here
254 path
->skip_locking
= 1;
255 last
= max_t(u64
, block_group
->key
.objectid
, BTRFS_SUPER_INFO_OFFSET
);
258 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
259 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
264 leaf
= path
->nodes
[0];
265 slot
= path
->slots
[0];
266 if (slot
>= btrfs_header_nritems(leaf
)) {
267 ret
= btrfs_next_leaf(root
, path
);
275 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
276 if (key
.objectid
< block_group
->key
.objectid
)
279 if (key
.objectid
>= block_group
->key
.objectid
+
280 block_group
->key
.offset
)
283 if (btrfs_key_type(&key
) == BTRFS_EXTENT_ITEM_KEY
) {
284 add_new_free_space(block_group
, root
->fs_info
, last
,
287 last
= key
.objectid
+ key
.offset
;
293 add_new_free_space(block_group
, root
->fs_info
, last
,
294 block_group
->key
.objectid
+
295 block_group
->key
.offset
);
297 remove_sb_from_cache(root
, block_group
);
298 block_group
->cached
= 1;
301 btrfs_free_path(path
);
306 * return the block group that starts at or after bytenr
308 static struct btrfs_block_group_cache
*
309 btrfs_lookup_first_block_group(struct btrfs_fs_info
*info
, u64 bytenr
)
311 struct btrfs_block_group_cache
*cache
;
313 cache
= block_group_cache_tree_search(info
, bytenr
, 0);
319 * return the block group that contains teh given bytenr
321 struct btrfs_block_group_cache
*btrfs_lookup_block_group(
322 struct btrfs_fs_info
*info
,
325 struct btrfs_block_group_cache
*cache
;
327 cache
= block_group_cache_tree_search(info
, bytenr
, 1);
332 static inline void put_block_group(struct btrfs_block_group_cache
*cache
)
334 if (atomic_dec_and_test(&cache
->count
))
338 static struct btrfs_space_info
*__find_space_info(struct btrfs_fs_info
*info
,
341 struct list_head
*head
= &info
->space_info
;
342 struct btrfs_space_info
*found
;
345 list_for_each_entry_rcu(found
, head
, list
) {
346 if (found
->flags
== flags
) {
356 * after adding space to the filesystem, we need to clear the full flags
357 * on all the space infos.
359 void btrfs_clear_space_info_full(struct btrfs_fs_info
*info
)
361 struct list_head
*head
= &info
->space_info
;
362 struct btrfs_space_info
*found
;
365 list_for_each_entry_rcu(found
, head
, list
)
370 static u64
div_factor(u64 num
, int factor
)
379 u64
btrfs_find_block_group(struct btrfs_root
*root
,
380 u64 search_start
, u64 search_hint
, int owner
)
382 struct btrfs_block_group_cache
*cache
;
384 u64 last
= max(search_hint
, search_start
);
391 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
395 spin_lock(&cache
->lock
);
396 last
= cache
->key
.objectid
+ cache
->key
.offset
;
397 used
= btrfs_block_group_used(&cache
->item
);
399 if ((full_search
|| !cache
->ro
) &&
400 block_group_bits(cache
, BTRFS_BLOCK_GROUP_METADATA
)) {
401 if (used
+ cache
->pinned
+ cache
->reserved
<
402 div_factor(cache
->key
.offset
, factor
)) {
403 group_start
= cache
->key
.objectid
;
404 spin_unlock(&cache
->lock
);
405 put_block_group(cache
);
409 spin_unlock(&cache
->lock
);
410 put_block_group(cache
);
418 if (!full_search
&& factor
< 10) {
428 /* simple helper to search for an existing extent at a given offset */
429 int btrfs_lookup_extent(struct btrfs_root
*root
, u64 start
, u64 len
)
432 struct btrfs_key key
;
433 struct btrfs_path
*path
;
435 path
= btrfs_alloc_path();
437 key
.objectid
= start
;
439 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
440 ret
= btrfs_search_slot(NULL
, root
->fs_info
->extent_root
, &key
, path
,
442 btrfs_free_path(path
);
447 * Back reference rules. Back refs have three main goals:
449 * 1) differentiate between all holders of references to an extent so that
450 * when a reference is dropped we can make sure it was a valid reference
451 * before freeing the extent.
453 * 2) Provide enough information to quickly find the holders of an extent
454 * if we notice a given block is corrupted or bad.
456 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
457 * maintenance. This is actually the same as #2, but with a slightly
458 * different use case.
460 * File extents can be referenced by:
462 * - multiple snapshots, subvolumes, or different generations in one subvol
463 * - different files inside a single subvolume
464 * - different offsets inside a file (bookend extents in file.c)
466 * The extent ref structure has fields for:
468 * - Objectid of the subvolume root
469 * - Generation number of the tree holding the reference
470 * - objectid of the file holding the reference
471 * - number of references holding by parent node (alway 1 for tree blocks)
473 * Btree leaf may hold multiple references to a file extent. In most cases,
474 * these references are from same file and the corresponding offsets inside
475 * the file are close together.
477 * When a file extent is allocated the fields are filled in:
478 * (root_key.objectid, trans->transid, inode objectid, 1)
480 * When a leaf is cow'd new references are added for every file extent found
481 * in the leaf. It looks similar to the create case, but trans->transid will
482 * be different when the block is cow'd.
484 * (root_key.objectid, trans->transid, inode objectid,
485 * number of references in the leaf)
487 * When a file extent is removed either during snapshot deletion or
488 * file truncation, we find the corresponding back reference and check
489 * the following fields:
491 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
494 * Btree extents can be referenced by:
496 * - Different subvolumes
497 * - Different generations of the same subvolume
499 * When a tree block is created, back references are inserted:
501 * (root->root_key.objectid, trans->transid, level, 1)
503 * When a tree block is cow'd, new back references are added for all the
504 * blocks it points to. If the tree block isn't in reference counted root,
505 * the old back references are removed. These new back references are of
506 * the form (trans->transid will have increased since creation):
508 * (root->root_key.objectid, trans->transid, level, 1)
510 * When a backref is in deleting, the following fields are checked:
512 * if backref was for a tree root:
513 * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
515 * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
517 * Back Reference Key composing:
519 * The key objectid corresponds to the first byte in the extent, the key
520 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
521 * byte of parent extent. If a extent is tree root, the key offset is set
522 * to the key objectid.
525 static noinline
int lookup_extent_backref(struct btrfs_trans_handle
*trans
,
526 struct btrfs_root
*root
,
527 struct btrfs_path
*path
,
528 u64 bytenr
, u64 parent
,
529 u64 ref_root
, u64 ref_generation
,
530 u64 owner_objectid
, int del
)
532 struct btrfs_key key
;
533 struct btrfs_extent_ref
*ref
;
534 struct extent_buffer
*leaf
;
538 key
.objectid
= bytenr
;
539 key
.type
= BTRFS_EXTENT_REF_KEY
;
542 ret
= btrfs_search_slot(trans
, root
, &key
, path
, del
? -1 : 0, 1);
550 leaf
= path
->nodes
[0];
551 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_ref
);
552 ref_objectid
= btrfs_ref_objectid(leaf
, ref
);
553 if (btrfs_ref_root(leaf
, ref
) != ref_root
||
554 btrfs_ref_generation(leaf
, ref
) != ref_generation
||
555 (ref_objectid
!= owner_objectid
&&
556 ref_objectid
!= BTRFS_MULTIPLE_OBJECTIDS
)) {
566 static noinline
int insert_extent_backref(struct btrfs_trans_handle
*trans
,
567 struct btrfs_root
*root
,
568 struct btrfs_path
*path
,
569 u64 bytenr
, u64 parent
,
570 u64 ref_root
, u64 ref_generation
,
574 struct btrfs_key key
;
575 struct extent_buffer
*leaf
;
576 struct btrfs_extent_ref
*ref
;
580 key
.objectid
= bytenr
;
581 key
.type
= BTRFS_EXTENT_REF_KEY
;
584 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, sizeof(*ref
));
586 leaf
= path
->nodes
[0];
587 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
588 struct btrfs_extent_ref
);
589 btrfs_set_ref_root(leaf
, ref
, ref_root
);
590 btrfs_set_ref_generation(leaf
, ref
, ref_generation
);
591 btrfs_set_ref_objectid(leaf
, ref
, owner_objectid
);
592 btrfs_set_ref_num_refs(leaf
, ref
, refs_to_add
);
593 } else if (ret
== -EEXIST
) {
596 BUG_ON(owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
);
597 leaf
= path
->nodes
[0];
598 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
599 struct btrfs_extent_ref
);
600 if (btrfs_ref_root(leaf
, ref
) != ref_root
||
601 btrfs_ref_generation(leaf
, ref
) != ref_generation
) {
607 num_refs
= btrfs_ref_num_refs(leaf
, ref
);
608 BUG_ON(num_refs
== 0);
609 btrfs_set_ref_num_refs(leaf
, ref
, num_refs
+ refs_to_add
);
611 existing_owner
= btrfs_ref_objectid(leaf
, ref
);
612 if (existing_owner
!= owner_objectid
&&
613 existing_owner
!= BTRFS_MULTIPLE_OBJECTIDS
) {
614 btrfs_set_ref_objectid(leaf
, ref
,
615 BTRFS_MULTIPLE_OBJECTIDS
);
621 btrfs_mark_buffer_dirty(path
->nodes
[0]);
623 btrfs_release_path(root
, path
);
627 static noinline
int remove_extent_backref(struct btrfs_trans_handle
*trans
,
628 struct btrfs_root
*root
,
629 struct btrfs_path
*path
,
632 struct extent_buffer
*leaf
;
633 struct btrfs_extent_ref
*ref
;
637 leaf
= path
->nodes
[0];
638 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_ref
);
639 num_refs
= btrfs_ref_num_refs(leaf
, ref
);
640 BUG_ON(num_refs
< refs_to_drop
);
641 num_refs
-= refs_to_drop
;
643 ret
= btrfs_del_item(trans
, root
, path
);
645 btrfs_set_ref_num_refs(leaf
, ref
, num_refs
);
646 btrfs_mark_buffer_dirty(leaf
);
648 btrfs_release_path(root
, path
);
652 #ifdef BIO_RW_DISCARD
653 static void btrfs_issue_discard(struct block_device
*bdev
,
656 blkdev_issue_discard(bdev
, start
>> 9, len
>> 9, GFP_KERNEL
);
660 static int btrfs_discard_extent(struct btrfs_root
*root
, u64 bytenr
,
663 #ifdef BIO_RW_DISCARD
665 u64 map_length
= num_bytes
;
666 struct btrfs_multi_bio
*multi
= NULL
;
668 /* Tell the block device(s) that the sectors can be discarded */
669 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
670 bytenr
, &map_length
, &multi
, 0);
672 struct btrfs_bio_stripe
*stripe
= multi
->stripes
;
675 if (map_length
> num_bytes
)
676 map_length
= num_bytes
;
678 for (i
= 0; i
< multi
->num_stripes
; i
++, stripe
++) {
679 btrfs_issue_discard(stripe
->dev
->bdev
,
692 static int __btrfs_update_extent_ref(struct btrfs_trans_handle
*trans
,
693 struct btrfs_root
*root
, u64 bytenr
,
695 u64 orig_parent
, u64 parent
,
696 u64 orig_root
, u64 ref_root
,
697 u64 orig_generation
, u64 ref_generation
,
701 int pin
= owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
;
703 ret
= btrfs_update_delayed_ref(trans
, bytenr
, num_bytes
,
704 orig_parent
, parent
, orig_root
,
705 ref_root
, orig_generation
,
706 ref_generation
, owner_objectid
, pin
);
711 int btrfs_update_extent_ref(struct btrfs_trans_handle
*trans
,
712 struct btrfs_root
*root
, u64 bytenr
,
713 u64 num_bytes
, u64 orig_parent
, u64 parent
,
714 u64 ref_root
, u64 ref_generation
,
718 if (ref_root
== BTRFS_TREE_LOG_OBJECTID
&&
719 owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
)
722 ret
= __btrfs_update_extent_ref(trans
, root
, bytenr
, num_bytes
,
723 orig_parent
, parent
, ref_root
,
724 ref_root
, ref_generation
,
725 ref_generation
, owner_objectid
);
728 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
729 struct btrfs_root
*root
, u64 bytenr
,
731 u64 orig_parent
, u64 parent
,
732 u64 orig_root
, u64 ref_root
,
733 u64 orig_generation
, u64 ref_generation
,
738 ret
= btrfs_add_delayed_ref(trans
, bytenr
, num_bytes
, parent
, ref_root
,
739 ref_generation
, owner_objectid
,
740 BTRFS_ADD_DELAYED_REF
, 0);
745 static noinline_for_stack
int add_extent_ref(struct btrfs_trans_handle
*trans
,
746 struct btrfs_root
*root
, u64 bytenr
,
747 u64 num_bytes
, u64 parent
, u64 ref_root
,
748 u64 ref_generation
, u64 owner_objectid
,
751 struct btrfs_path
*path
;
753 struct btrfs_key key
;
754 struct extent_buffer
*l
;
755 struct btrfs_extent_item
*item
;
758 path
= btrfs_alloc_path();
763 key
.objectid
= bytenr
;
764 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
765 key
.offset
= num_bytes
;
767 /* first find the extent item and update its reference count */
768 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
,
775 btrfs_free_path(path
);
780 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
781 if (key
.objectid
!= bytenr
) {
782 btrfs_print_leaf(root
->fs_info
->extent_root
, path
->nodes
[0]);
783 printk(KERN_ERR
"btrfs wanted %llu found %llu\n",
784 (unsigned long long)bytenr
,
785 (unsigned long long)key
.objectid
);
788 BUG_ON(key
.type
!= BTRFS_EXTENT_ITEM_KEY
);
790 item
= btrfs_item_ptr(l
, path
->slots
[0], struct btrfs_extent_item
);
792 refs
= btrfs_extent_refs(l
, item
);
793 btrfs_set_extent_refs(l
, item
, refs
+ refs_to_add
);
794 btrfs_mark_buffer_dirty(path
->nodes
[0]);
796 btrfs_release_path(root
->fs_info
->extent_root
, path
);
799 /* now insert the actual backref */
800 ret
= insert_extent_backref(trans
, root
->fs_info
->extent_root
,
801 path
, bytenr
, parent
,
802 ref_root
, ref_generation
,
803 owner_objectid
, refs_to_add
);
805 btrfs_free_path(path
);
809 int btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
810 struct btrfs_root
*root
,
811 u64 bytenr
, u64 num_bytes
, u64 parent
,
812 u64 ref_root
, u64 ref_generation
,
816 if (ref_root
== BTRFS_TREE_LOG_OBJECTID
&&
817 owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
)
820 ret
= __btrfs_inc_extent_ref(trans
, root
, bytenr
, num_bytes
, 0, parent
,
821 0, ref_root
, 0, ref_generation
,
826 static int drop_delayed_ref(struct btrfs_trans_handle
*trans
,
827 struct btrfs_root
*root
,
828 struct btrfs_delayed_ref_node
*node
)
831 struct btrfs_delayed_ref
*ref
= btrfs_delayed_node_to_ref(node
);
833 BUG_ON(node
->ref_mod
== 0);
834 ret
= __btrfs_free_extent(trans
, root
, node
->bytenr
, node
->num_bytes
,
835 node
->parent
, ref
->root
, ref
->generation
,
836 ref
->owner_objectid
, ref
->pin
, node
->ref_mod
);
841 /* helper function to actually process a single delayed ref entry */
842 static noinline
int run_one_delayed_ref(struct btrfs_trans_handle
*trans
,
843 struct btrfs_root
*root
,
844 struct btrfs_delayed_ref_node
*node
,
848 struct btrfs_delayed_ref
*ref
;
850 if (node
->parent
== (u64
)-1) {
851 struct btrfs_delayed_ref_head
*head
;
853 * we've hit the end of the chain and we were supposed
854 * to insert this extent into the tree. But, it got
855 * deleted before we ever needed to insert it, so all
856 * we have to do is clean up the accounting
858 if (insert_reserved
) {
859 update_reserved_extents(root
, node
->bytenr
,
862 head
= btrfs_delayed_node_to_head(node
);
863 mutex_unlock(&head
->mutex
);
867 ref
= btrfs_delayed_node_to_ref(node
);
868 if (ref
->action
== BTRFS_ADD_DELAYED_REF
) {
869 if (insert_reserved
) {
870 struct btrfs_key ins
;
872 ins
.objectid
= node
->bytenr
;
873 ins
.offset
= node
->num_bytes
;
874 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
876 /* record the full extent allocation */
877 ret
= __btrfs_alloc_reserved_extent(trans
, root
,
878 node
->parent
, ref
->root
,
879 ref
->generation
, ref
->owner_objectid
,
880 &ins
, node
->ref_mod
);
881 update_reserved_extents(root
, node
->bytenr
,
884 /* just add one backref */
885 ret
= add_extent_ref(trans
, root
, node
->bytenr
,
887 node
->parent
, ref
->root
, ref
->generation
,
888 ref
->owner_objectid
, node
->ref_mod
);
891 } else if (ref
->action
== BTRFS_DROP_DELAYED_REF
) {
892 WARN_ON(insert_reserved
);
893 ret
= drop_delayed_ref(trans
, root
, node
);
898 static noinline
struct btrfs_delayed_ref_node
*
899 select_delayed_ref(struct btrfs_delayed_ref_head
*head
)
901 struct rb_node
*node
;
902 struct btrfs_delayed_ref_node
*ref
;
903 int action
= BTRFS_ADD_DELAYED_REF
;
906 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
907 * this prevents ref count from going down to zero when
908 * there still are pending delayed ref.
910 node
= rb_prev(&head
->node
.rb_node
);
914 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
,
916 if (ref
->bytenr
!= head
->node
.bytenr
)
918 if (btrfs_delayed_node_to_ref(ref
)->action
== action
)
920 node
= rb_prev(node
);
922 if (action
== BTRFS_ADD_DELAYED_REF
) {
923 action
= BTRFS_DROP_DELAYED_REF
;
930 * this starts processing the delayed reference count updates and
931 * extent insertions we have queued up so far. count can be
932 * 0, which means to process everything in the tree at the start
933 * of the run (but not newly added entries), or it can be some target
934 * number you'd like to process.
936 int btrfs_run_delayed_refs(struct btrfs_trans_handle
*trans
,
937 struct btrfs_root
*root
, unsigned long count
)
939 struct rb_node
*node
;
940 struct btrfs_delayed_ref_root
*delayed_refs
;
941 struct btrfs_delayed_ref_node
*ref
;
942 struct btrfs_delayed_ref_head
*locked_ref
= NULL
;
944 int must_insert_reserved
= 0;
945 int run_all
= count
== (unsigned long)-1;
947 if (root
== root
->fs_info
->extent_root
)
948 root
= root
->fs_info
->tree_root
;
950 delayed_refs
= &trans
->transaction
->delayed_refs
;
952 spin_lock(&delayed_refs
->lock
);
954 count
= delayed_refs
->num_entries
;
958 * no locked ref, go find something we can
959 * process in the rbtree. We start at
960 * the beginning of the tree, there may be less
961 * lock contention if we do something smarter here.
963 node
= rb_first(&delayed_refs
->root
);
965 spin_unlock(&delayed_refs
->lock
);
969 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
,
971 ret
= btrfs_lock_delayed_ref(trans
, ref
, &locked_ref
);
973 spin_unlock(&delayed_refs
->lock
);
979 * record the must insert reserved flag before we
980 * drop the spin lock.
982 must_insert_reserved
= locked_ref
->must_insert_reserved
;
983 locked_ref
->must_insert_reserved
= 0;
986 * locked_ref is the head node, so we have to go one
987 * node back for any delayed ref updates
990 ref
= select_delayed_ref(locked_ref
);
992 /* All delayed refs have been processed, Go ahead
993 * and send the head node to run_one_delayed_ref,
994 * so that any accounting fixes can happen
996 ref
= &locked_ref
->node
;
1001 rb_erase(&ref
->rb_node
, &delayed_refs
->root
);
1002 delayed_refs
->num_entries
--;
1003 spin_unlock(&delayed_refs
->lock
);
1005 ret
= run_one_delayed_ref(trans
, root
, ref
,
1006 must_insert_reserved
);
1008 btrfs_put_delayed_ref(ref
);
1010 /* once we lock the head ref, we have to process all the
1011 * entries for it. So, we might end up doing more entries
1012 * that count was asking us to do.
1018 * we set locked_ref to null above if we're all done
1021 if (!locked_ref
&& count
== 0)
1024 spin_lock(&delayed_refs
->lock
);
1027 spin_lock(&delayed_refs
->lock
);
1028 node
= rb_first(&delayed_refs
->root
);
1030 spin_unlock(&delayed_refs
->lock
);
1035 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
,
1037 if (btrfs_delayed_ref_is_head(ref
)) {
1038 struct btrfs_delayed_ref_head
*head
;
1040 head
= btrfs_delayed_node_to_head(ref
);
1041 atomic_inc(&ref
->refs
);
1043 spin_unlock(&delayed_refs
->lock
);
1044 mutex_lock(&head
->mutex
);
1045 mutex_unlock(&head
->mutex
);
1047 btrfs_put_delayed_ref(ref
);
1050 node
= rb_next(node
);
1052 spin_unlock(&delayed_refs
->lock
);
1053 count
= (unsigned long)-1;
1054 schedule_timeout(1);
1061 int btrfs_cross_ref_exist(struct btrfs_trans_handle
*trans
,
1062 struct btrfs_root
*root
, u64 objectid
, u64 bytenr
)
1064 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
1065 struct btrfs_path
*path
;
1066 struct extent_buffer
*leaf
;
1067 struct btrfs_extent_ref
*ref_item
;
1068 struct btrfs_key key
;
1069 struct btrfs_key found_key
;
1075 key
.objectid
= bytenr
;
1076 key
.offset
= (u64
)-1;
1077 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1079 path
= btrfs_alloc_path();
1080 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
1086 if (path
->slots
[0] == 0)
1090 leaf
= path
->nodes
[0];
1091 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1093 if (found_key
.objectid
!= bytenr
||
1094 found_key
.type
!= BTRFS_EXTENT_ITEM_KEY
)
1097 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
1099 leaf
= path
->nodes
[0];
1100 nritems
= btrfs_header_nritems(leaf
);
1101 if (path
->slots
[0] >= nritems
) {
1102 ret
= btrfs_next_leaf(extent_root
, path
);
1109 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1110 if (found_key
.objectid
!= bytenr
)
1113 if (found_key
.type
!= BTRFS_EXTENT_REF_KEY
) {
1118 ref_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1119 struct btrfs_extent_ref
);
1120 ref_root
= btrfs_ref_root(leaf
, ref_item
);
1121 if ((ref_root
!= root
->root_key
.objectid
&&
1122 ref_root
!= BTRFS_TREE_LOG_OBJECTID
) ||
1123 objectid
!= btrfs_ref_objectid(leaf
, ref_item
)) {
1127 if (btrfs_ref_generation(leaf
, ref_item
) <= last_snapshot
) {
1136 btrfs_free_path(path
);
1140 int btrfs_cache_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
1141 struct extent_buffer
*buf
, u32 nr_extents
)
1143 struct btrfs_key key
;
1144 struct btrfs_file_extent_item
*fi
;
1152 if (!root
->ref_cows
)
1155 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
1157 root_gen
= root
->root_key
.offset
;
1160 root_gen
= trans
->transid
- 1;
1163 level
= btrfs_header_level(buf
);
1164 nritems
= btrfs_header_nritems(buf
);
1167 struct btrfs_leaf_ref
*ref
;
1168 struct btrfs_extent_info
*info
;
1170 ref
= btrfs_alloc_leaf_ref(root
, nr_extents
);
1176 ref
->root_gen
= root_gen
;
1177 ref
->bytenr
= buf
->start
;
1178 ref
->owner
= btrfs_header_owner(buf
);
1179 ref
->generation
= btrfs_header_generation(buf
);
1180 ref
->nritems
= nr_extents
;
1181 info
= ref
->extents
;
1183 for (i
= 0; nr_extents
> 0 && i
< nritems
; i
++) {
1185 btrfs_item_key_to_cpu(buf
, &key
, i
);
1186 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
1188 fi
= btrfs_item_ptr(buf
, i
,
1189 struct btrfs_file_extent_item
);
1190 if (btrfs_file_extent_type(buf
, fi
) ==
1191 BTRFS_FILE_EXTENT_INLINE
)
1193 disk_bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
1194 if (disk_bytenr
== 0)
1197 info
->bytenr
= disk_bytenr
;
1199 btrfs_file_extent_disk_num_bytes(buf
, fi
);
1200 info
->objectid
= key
.objectid
;
1201 info
->offset
= key
.offset
;
1205 ret
= btrfs_add_leaf_ref(root
, ref
, shared
);
1206 if (ret
== -EEXIST
&& shared
) {
1207 struct btrfs_leaf_ref
*old
;
1208 old
= btrfs_lookup_leaf_ref(root
, ref
->bytenr
);
1210 btrfs_remove_leaf_ref(root
, old
);
1211 btrfs_free_leaf_ref(root
, old
);
1212 ret
= btrfs_add_leaf_ref(root
, ref
, shared
);
1215 btrfs_free_leaf_ref(root
, ref
);
1221 /* when a block goes through cow, we update the reference counts of
1222 * everything that block points to. The internal pointers of the block
1223 * can be in just about any order, and it is likely to have clusters of
1224 * things that are close together and clusters of things that are not.
1226 * To help reduce the seeks that come with updating all of these reference
1227 * counts, sort them by byte number before actual updates are done.
1229 * struct refsort is used to match byte number to slot in the btree block.
1230 * we sort based on the byte number and then use the slot to actually
1233 * struct refsort is smaller than strcut btrfs_item and smaller than
1234 * struct btrfs_key_ptr. Since we're currently limited to the page size
1235 * for a btree block, there's no way for a kmalloc of refsorts for a
1236 * single node to be bigger than a page.
1244 * for passing into sort()
1246 static int refsort_cmp(const void *a_void
, const void *b_void
)
1248 const struct refsort
*a
= a_void
;
1249 const struct refsort
*b
= b_void
;
1251 if (a
->bytenr
< b
->bytenr
)
1253 if (a
->bytenr
> b
->bytenr
)
1259 noinline
int btrfs_inc_ref(struct btrfs_trans_handle
*trans
,
1260 struct btrfs_root
*root
,
1261 struct extent_buffer
*orig_buf
,
1262 struct extent_buffer
*buf
, u32
*nr_extents
)
1268 u64 orig_generation
;
1269 struct refsort
*sorted
;
1271 u32 nr_file_extents
= 0;
1272 struct btrfs_key key
;
1273 struct btrfs_file_extent_item
*fi
;
1280 int (*process_func
)(struct btrfs_trans_handle
*, struct btrfs_root
*,
1281 u64
, u64
, u64
, u64
, u64
, u64
, u64
, u64
, u64
);
1283 ref_root
= btrfs_header_owner(buf
);
1284 ref_generation
= btrfs_header_generation(buf
);
1285 orig_root
= btrfs_header_owner(orig_buf
);
1286 orig_generation
= btrfs_header_generation(orig_buf
);
1288 nritems
= btrfs_header_nritems(buf
);
1289 level
= btrfs_header_level(buf
);
1291 sorted
= kmalloc(sizeof(struct refsort
) * nritems
, GFP_NOFS
);
1294 if (root
->ref_cows
) {
1295 process_func
= __btrfs_inc_extent_ref
;
1298 root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
)
1301 root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
)
1303 process_func
= __btrfs_update_extent_ref
;
1307 * we make two passes through the items. In the first pass we
1308 * only record the byte number and slot. Then we sort based on
1309 * byte number and do the actual work based on the sorted results
1311 for (i
= 0; i
< nritems
; i
++) {
1314 btrfs_item_key_to_cpu(buf
, &key
, i
);
1315 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
1317 fi
= btrfs_item_ptr(buf
, i
,
1318 struct btrfs_file_extent_item
);
1319 if (btrfs_file_extent_type(buf
, fi
) ==
1320 BTRFS_FILE_EXTENT_INLINE
)
1322 bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
1327 sorted
[refi
].bytenr
= bytenr
;
1328 sorted
[refi
].slot
= i
;
1331 bytenr
= btrfs_node_blockptr(buf
, i
);
1332 sorted
[refi
].bytenr
= bytenr
;
1333 sorted
[refi
].slot
= i
;
1338 * if refi == 0, we didn't actually put anything into the sorted
1339 * array and we're done
1344 sort(sorted
, refi
, sizeof(struct refsort
), refsort_cmp
, NULL
);
1346 for (i
= 0; i
< refi
; i
++) {
1348 slot
= sorted
[i
].slot
;
1349 bytenr
= sorted
[i
].bytenr
;
1352 btrfs_item_key_to_cpu(buf
, &key
, slot
);
1353 fi
= btrfs_item_ptr(buf
, slot
,
1354 struct btrfs_file_extent_item
);
1356 bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
1360 ret
= process_func(trans
, root
, bytenr
,
1361 btrfs_file_extent_disk_num_bytes(buf
, fi
),
1362 orig_buf
->start
, buf
->start
,
1363 orig_root
, ref_root
,
1364 orig_generation
, ref_generation
,
1373 ret
= process_func(trans
, root
, bytenr
, buf
->len
,
1374 orig_buf
->start
, buf
->start
,
1375 orig_root
, ref_root
,
1376 orig_generation
, ref_generation
,
1389 *nr_extents
= nr_file_extents
;
1391 *nr_extents
= nritems
;
1400 int btrfs_update_ref(struct btrfs_trans_handle
*trans
,
1401 struct btrfs_root
*root
, struct extent_buffer
*orig_buf
,
1402 struct extent_buffer
*buf
, int start_slot
, int nr
)
1409 u64 orig_generation
;
1410 struct btrfs_key key
;
1411 struct btrfs_file_extent_item
*fi
;
1417 BUG_ON(start_slot
< 0);
1418 BUG_ON(start_slot
+ nr
> btrfs_header_nritems(buf
));
1420 ref_root
= btrfs_header_owner(buf
);
1421 ref_generation
= btrfs_header_generation(buf
);
1422 orig_root
= btrfs_header_owner(orig_buf
);
1423 orig_generation
= btrfs_header_generation(orig_buf
);
1424 level
= btrfs_header_level(buf
);
1426 if (!root
->ref_cows
) {
1428 root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
)
1431 root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
)
1435 for (i
= 0, slot
= start_slot
; i
< nr
; i
++, slot
++) {
1438 btrfs_item_key_to_cpu(buf
, &key
, slot
);
1439 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
1441 fi
= btrfs_item_ptr(buf
, slot
,
1442 struct btrfs_file_extent_item
);
1443 if (btrfs_file_extent_type(buf
, fi
) ==
1444 BTRFS_FILE_EXTENT_INLINE
)
1446 bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
1449 ret
= __btrfs_update_extent_ref(trans
, root
, bytenr
,
1450 btrfs_file_extent_disk_num_bytes(buf
, fi
),
1451 orig_buf
->start
, buf
->start
,
1452 orig_root
, ref_root
, orig_generation
,
1453 ref_generation
, key
.objectid
);
1457 bytenr
= btrfs_node_blockptr(buf
, slot
);
1458 ret
= __btrfs_update_extent_ref(trans
, root
, bytenr
,
1459 buf
->len
, orig_buf
->start
,
1460 buf
->start
, orig_root
, ref_root
,
1461 orig_generation
, ref_generation
,
1473 static int write_one_cache_group(struct btrfs_trans_handle
*trans
,
1474 struct btrfs_root
*root
,
1475 struct btrfs_path
*path
,
1476 struct btrfs_block_group_cache
*cache
)
1479 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
1481 struct extent_buffer
*leaf
;
1483 ret
= btrfs_search_slot(trans
, extent_root
, &cache
->key
, path
, 0, 1);
1488 leaf
= path
->nodes
[0];
1489 bi
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
1490 write_extent_buffer(leaf
, &cache
->item
, bi
, sizeof(cache
->item
));
1491 btrfs_mark_buffer_dirty(leaf
);
1492 btrfs_release_path(extent_root
, path
);
1500 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle
*trans
,
1501 struct btrfs_root
*root
)
1503 struct btrfs_block_group_cache
*cache
, *entry
;
1507 struct btrfs_path
*path
;
1510 path
= btrfs_alloc_path();
1516 spin_lock(&root
->fs_info
->block_group_cache_lock
);
1517 for (n
= rb_first(&root
->fs_info
->block_group_cache_tree
);
1518 n
; n
= rb_next(n
)) {
1519 entry
= rb_entry(n
, struct btrfs_block_group_cache
,
1526 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
1532 last
+= cache
->key
.offset
;
1534 err
= write_one_cache_group(trans
, root
,
1537 * if we fail to write the cache group, we want
1538 * to keep it marked dirty in hopes that a later
1546 btrfs_free_path(path
);
1550 int btrfs_extent_readonly(struct btrfs_root
*root
, u64 bytenr
)
1552 struct btrfs_block_group_cache
*block_group
;
1555 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
1556 if (!block_group
|| block_group
->ro
)
1559 put_block_group(block_group
);
1563 static int update_space_info(struct btrfs_fs_info
*info
, u64 flags
,
1564 u64 total_bytes
, u64 bytes_used
,
1565 struct btrfs_space_info
**space_info
)
1567 struct btrfs_space_info
*found
;
1569 found
= __find_space_info(info
, flags
);
1571 spin_lock(&found
->lock
);
1572 found
->total_bytes
+= total_bytes
;
1573 found
->bytes_used
+= bytes_used
;
1575 spin_unlock(&found
->lock
);
1576 *space_info
= found
;
1579 found
= kzalloc(sizeof(*found
), GFP_NOFS
);
1583 INIT_LIST_HEAD(&found
->block_groups
);
1584 init_rwsem(&found
->groups_sem
);
1585 spin_lock_init(&found
->lock
);
1586 found
->flags
= flags
;
1587 found
->total_bytes
= total_bytes
;
1588 found
->bytes_used
= bytes_used
;
1589 found
->bytes_pinned
= 0;
1590 found
->bytes_reserved
= 0;
1591 found
->bytes_readonly
= 0;
1592 found
->bytes_delalloc
= 0;
1594 found
->force_alloc
= 0;
1595 *space_info
= found
;
1596 list_add_rcu(&found
->list
, &info
->space_info
);
1600 static void set_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
1602 u64 extra_flags
= flags
& (BTRFS_BLOCK_GROUP_RAID0
|
1603 BTRFS_BLOCK_GROUP_RAID1
|
1604 BTRFS_BLOCK_GROUP_RAID10
|
1605 BTRFS_BLOCK_GROUP_DUP
);
1607 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
1608 fs_info
->avail_data_alloc_bits
|= extra_flags
;
1609 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
1610 fs_info
->avail_metadata_alloc_bits
|= extra_flags
;
1611 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
1612 fs_info
->avail_system_alloc_bits
|= extra_flags
;
1616 static void set_block_group_readonly(struct btrfs_block_group_cache
*cache
)
1618 spin_lock(&cache
->space_info
->lock
);
1619 spin_lock(&cache
->lock
);
1621 cache
->space_info
->bytes_readonly
+= cache
->key
.offset
-
1622 btrfs_block_group_used(&cache
->item
);
1625 spin_unlock(&cache
->lock
);
1626 spin_unlock(&cache
->space_info
->lock
);
1629 u64
btrfs_reduce_alloc_profile(struct btrfs_root
*root
, u64 flags
)
1631 u64 num_devices
= root
->fs_info
->fs_devices
->rw_devices
;
1633 if (num_devices
== 1)
1634 flags
&= ~(BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID0
);
1635 if (num_devices
< 4)
1636 flags
&= ~BTRFS_BLOCK_GROUP_RAID10
;
1638 if ((flags
& BTRFS_BLOCK_GROUP_DUP
) &&
1639 (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
1640 BTRFS_BLOCK_GROUP_RAID10
))) {
1641 flags
&= ~BTRFS_BLOCK_GROUP_DUP
;
1644 if ((flags
& BTRFS_BLOCK_GROUP_RAID1
) &&
1645 (flags
& BTRFS_BLOCK_GROUP_RAID10
)) {
1646 flags
&= ~BTRFS_BLOCK_GROUP_RAID1
;
1649 if ((flags
& BTRFS_BLOCK_GROUP_RAID0
) &&
1650 ((flags
& BTRFS_BLOCK_GROUP_RAID1
) |
1651 (flags
& BTRFS_BLOCK_GROUP_RAID10
) |
1652 (flags
& BTRFS_BLOCK_GROUP_DUP
)))
1653 flags
&= ~BTRFS_BLOCK_GROUP_RAID0
;
1657 static u64
btrfs_get_alloc_profile(struct btrfs_root
*root
, u64 data
)
1659 struct btrfs_fs_info
*info
= root
->fs_info
;
1663 alloc_profile
= info
->avail_data_alloc_bits
&
1664 info
->data_alloc_profile
;
1665 data
= BTRFS_BLOCK_GROUP_DATA
| alloc_profile
;
1666 } else if (root
== root
->fs_info
->chunk_root
) {
1667 alloc_profile
= info
->avail_system_alloc_bits
&
1668 info
->system_alloc_profile
;
1669 data
= BTRFS_BLOCK_GROUP_SYSTEM
| alloc_profile
;
1671 alloc_profile
= info
->avail_metadata_alloc_bits
&
1672 info
->metadata_alloc_profile
;
1673 data
= BTRFS_BLOCK_GROUP_METADATA
| alloc_profile
;
1676 return btrfs_reduce_alloc_profile(root
, data
);
1679 void btrfs_set_inode_space_info(struct btrfs_root
*root
, struct inode
*inode
)
1683 alloc_target
= btrfs_get_alloc_profile(root
, 1);
1684 BTRFS_I(inode
)->space_info
= __find_space_info(root
->fs_info
,
1689 * for now this just makes sure we have at least 5% of our metadata space free
1692 int btrfs_check_metadata_free_space(struct btrfs_root
*root
)
1694 struct btrfs_fs_info
*info
= root
->fs_info
;
1695 struct btrfs_space_info
*meta_sinfo
;
1696 u64 alloc_target
, thresh
;
1697 int committed
= 0, ret
;
1699 /* get the space info for where the metadata will live */
1700 alloc_target
= btrfs_get_alloc_profile(root
, 0);
1701 meta_sinfo
= __find_space_info(info
, alloc_target
);
1704 spin_lock(&meta_sinfo
->lock
);
1705 if (!meta_sinfo
->full
)
1706 thresh
= meta_sinfo
->total_bytes
* 80;
1708 thresh
= meta_sinfo
->total_bytes
* 95;
1710 do_div(thresh
, 100);
1712 if (meta_sinfo
->bytes_used
+ meta_sinfo
->bytes_reserved
+
1713 meta_sinfo
->bytes_pinned
+ meta_sinfo
->bytes_readonly
> thresh
) {
1714 struct btrfs_trans_handle
*trans
;
1715 if (!meta_sinfo
->full
) {
1716 meta_sinfo
->force_alloc
= 1;
1717 spin_unlock(&meta_sinfo
->lock
);
1719 trans
= btrfs_start_transaction(root
, 1);
1723 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
1724 2 * 1024 * 1024, alloc_target
, 0);
1725 btrfs_end_transaction(trans
, root
);
1728 spin_unlock(&meta_sinfo
->lock
);
1732 trans
= btrfs_join_transaction(root
, 1);
1735 ret
= btrfs_commit_transaction(trans
, root
);
1742 spin_unlock(&meta_sinfo
->lock
);
1748 * This will check the space that the inode allocates from to make sure we have
1749 * enough space for bytes.
1751 int btrfs_check_data_free_space(struct btrfs_root
*root
, struct inode
*inode
,
1754 struct btrfs_space_info
*data_sinfo
;
1755 int ret
= 0, committed
= 0;
1757 /* make sure bytes are sectorsize aligned */
1758 bytes
= (bytes
+ root
->sectorsize
- 1) & ~((u64
)root
->sectorsize
- 1);
1760 data_sinfo
= BTRFS_I(inode
)->space_info
;
1762 /* make sure we have enough space to handle the data first */
1763 spin_lock(&data_sinfo
->lock
);
1764 if (data_sinfo
->total_bytes
- data_sinfo
->bytes_used
-
1765 data_sinfo
->bytes_delalloc
- data_sinfo
->bytes_reserved
-
1766 data_sinfo
->bytes_pinned
- data_sinfo
->bytes_readonly
-
1767 data_sinfo
->bytes_may_use
< bytes
) {
1768 struct btrfs_trans_handle
*trans
;
1771 * if we don't have enough free bytes in this space then we need
1772 * to alloc a new chunk.
1774 if (!data_sinfo
->full
) {
1777 data_sinfo
->force_alloc
= 1;
1778 spin_unlock(&data_sinfo
->lock
);
1780 alloc_target
= btrfs_get_alloc_profile(root
, 1);
1781 trans
= btrfs_start_transaction(root
, 1);
1785 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
1786 bytes
+ 2 * 1024 * 1024,
1788 btrfs_end_transaction(trans
, root
);
1793 spin_unlock(&data_sinfo
->lock
);
1795 /* commit the current transaction and try again */
1798 trans
= btrfs_join_transaction(root
, 1);
1801 ret
= btrfs_commit_transaction(trans
, root
);
1807 printk(KERN_ERR
"no space left, need %llu, %llu delalloc bytes"
1808 ", %llu bytes_used, %llu bytes_reserved, "
1809 "%llu bytes_pinned, %llu bytes_readonly, %llu may use"
1810 "%llu total\n", bytes
, data_sinfo
->bytes_delalloc
,
1811 data_sinfo
->bytes_used
, data_sinfo
->bytes_reserved
,
1812 data_sinfo
->bytes_pinned
, data_sinfo
->bytes_readonly
,
1813 data_sinfo
->bytes_may_use
, data_sinfo
->total_bytes
);
1816 data_sinfo
->bytes_may_use
+= bytes
;
1817 BTRFS_I(inode
)->reserved_bytes
+= bytes
;
1818 spin_unlock(&data_sinfo
->lock
);
1820 return btrfs_check_metadata_free_space(root
);
1824 * if there was an error for whatever reason after calling
1825 * btrfs_check_data_free_space, call this so we can cleanup the counters.
1827 void btrfs_free_reserved_data_space(struct btrfs_root
*root
,
1828 struct inode
*inode
, u64 bytes
)
1830 struct btrfs_space_info
*data_sinfo
;
1832 /* make sure bytes are sectorsize aligned */
1833 bytes
= (bytes
+ root
->sectorsize
- 1) & ~((u64
)root
->sectorsize
- 1);
1835 data_sinfo
= BTRFS_I(inode
)->space_info
;
1836 spin_lock(&data_sinfo
->lock
);
1837 data_sinfo
->bytes_may_use
-= bytes
;
1838 BTRFS_I(inode
)->reserved_bytes
-= bytes
;
1839 spin_unlock(&data_sinfo
->lock
);
1842 /* called when we are adding a delalloc extent to the inode's io_tree */
1843 void btrfs_delalloc_reserve_space(struct btrfs_root
*root
, struct inode
*inode
,
1846 struct btrfs_space_info
*data_sinfo
;
1848 /* get the space info for where this inode will be storing its data */
1849 data_sinfo
= BTRFS_I(inode
)->space_info
;
1851 /* make sure we have enough space to handle the data first */
1852 spin_lock(&data_sinfo
->lock
);
1853 data_sinfo
->bytes_delalloc
+= bytes
;
1856 * we are adding a delalloc extent without calling
1857 * btrfs_check_data_free_space first. This happens on a weird
1858 * writepage condition, but shouldn't hurt our accounting
1860 if (unlikely(bytes
> BTRFS_I(inode
)->reserved_bytes
)) {
1861 data_sinfo
->bytes_may_use
-= BTRFS_I(inode
)->reserved_bytes
;
1862 BTRFS_I(inode
)->reserved_bytes
= 0;
1864 data_sinfo
->bytes_may_use
-= bytes
;
1865 BTRFS_I(inode
)->reserved_bytes
-= bytes
;
1868 spin_unlock(&data_sinfo
->lock
);
1871 /* called when we are clearing an delalloc extent from the inode's io_tree */
1872 void btrfs_delalloc_free_space(struct btrfs_root
*root
, struct inode
*inode
,
1875 struct btrfs_space_info
*info
;
1877 info
= BTRFS_I(inode
)->space_info
;
1879 spin_lock(&info
->lock
);
1880 info
->bytes_delalloc
-= bytes
;
1881 spin_unlock(&info
->lock
);
1884 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
1885 struct btrfs_root
*extent_root
, u64 alloc_bytes
,
1886 u64 flags
, int force
)
1888 struct btrfs_space_info
*space_info
;
1892 mutex_lock(&extent_root
->fs_info
->chunk_mutex
);
1894 flags
= btrfs_reduce_alloc_profile(extent_root
, flags
);
1896 space_info
= __find_space_info(extent_root
->fs_info
, flags
);
1898 ret
= update_space_info(extent_root
->fs_info
, flags
,
1902 BUG_ON(!space_info
);
1904 spin_lock(&space_info
->lock
);
1905 if (space_info
->force_alloc
) {
1907 space_info
->force_alloc
= 0;
1909 if (space_info
->full
) {
1910 spin_unlock(&space_info
->lock
);
1914 thresh
= space_info
->total_bytes
- space_info
->bytes_readonly
;
1915 thresh
= div_factor(thresh
, 6);
1917 (space_info
->bytes_used
+ space_info
->bytes_pinned
+
1918 space_info
->bytes_reserved
+ alloc_bytes
) < thresh
) {
1919 spin_unlock(&space_info
->lock
);
1922 spin_unlock(&space_info
->lock
);
1924 ret
= btrfs_alloc_chunk(trans
, extent_root
, flags
);
1926 space_info
->full
= 1;
1928 mutex_unlock(&extent_root
->fs_info
->chunk_mutex
);
1932 static int update_block_group(struct btrfs_trans_handle
*trans
,
1933 struct btrfs_root
*root
,
1934 u64 bytenr
, u64 num_bytes
, int alloc
,
1937 struct btrfs_block_group_cache
*cache
;
1938 struct btrfs_fs_info
*info
= root
->fs_info
;
1939 u64 total
= num_bytes
;
1944 cache
= btrfs_lookup_block_group(info
, bytenr
);
1947 byte_in_group
= bytenr
- cache
->key
.objectid
;
1948 WARN_ON(byte_in_group
> cache
->key
.offset
);
1950 spin_lock(&cache
->space_info
->lock
);
1951 spin_lock(&cache
->lock
);
1953 old_val
= btrfs_block_group_used(&cache
->item
);
1954 num_bytes
= min(total
, cache
->key
.offset
- byte_in_group
);
1956 old_val
+= num_bytes
;
1957 cache
->space_info
->bytes_used
+= num_bytes
;
1959 cache
->space_info
->bytes_readonly
-= num_bytes
;
1960 btrfs_set_block_group_used(&cache
->item
, old_val
);
1961 spin_unlock(&cache
->lock
);
1962 spin_unlock(&cache
->space_info
->lock
);
1964 old_val
-= num_bytes
;
1965 cache
->space_info
->bytes_used
-= num_bytes
;
1967 cache
->space_info
->bytes_readonly
+= num_bytes
;
1968 btrfs_set_block_group_used(&cache
->item
, old_val
);
1969 spin_unlock(&cache
->lock
);
1970 spin_unlock(&cache
->space_info
->lock
);
1974 ret
= btrfs_discard_extent(root
, bytenr
,
1978 ret
= btrfs_add_free_space(cache
, bytenr
,
1983 put_block_group(cache
);
1985 bytenr
+= num_bytes
;
1990 static u64
first_logical_byte(struct btrfs_root
*root
, u64 search_start
)
1992 struct btrfs_block_group_cache
*cache
;
1995 cache
= btrfs_lookup_first_block_group(root
->fs_info
, search_start
);
1999 bytenr
= cache
->key
.objectid
;
2000 put_block_group(cache
);
2005 int btrfs_update_pinned_extents(struct btrfs_root
*root
,
2006 u64 bytenr
, u64 num
, int pin
)
2009 struct btrfs_block_group_cache
*cache
;
2010 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2012 WARN_ON(!mutex_is_locked(&root
->fs_info
->pinned_mutex
));
2014 set_extent_dirty(&fs_info
->pinned_extents
,
2015 bytenr
, bytenr
+ num
- 1, GFP_NOFS
);
2017 clear_extent_dirty(&fs_info
->pinned_extents
,
2018 bytenr
, bytenr
+ num
- 1, GFP_NOFS
);
2021 cache
= btrfs_lookup_block_group(fs_info
, bytenr
);
2023 len
= min(num
, cache
->key
.offset
-
2024 (bytenr
- cache
->key
.objectid
));
2026 spin_lock(&cache
->space_info
->lock
);
2027 spin_lock(&cache
->lock
);
2028 cache
->pinned
+= len
;
2029 cache
->space_info
->bytes_pinned
+= len
;
2030 spin_unlock(&cache
->lock
);
2031 spin_unlock(&cache
->space_info
->lock
);
2032 fs_info
->total_pinned
+= len
;
2034 spin_lock(&cache
->space_info
->lock
);
2035 spin_lock(&cache
->lock
);
2036 cache
->pinned
-= len
;
2037 cache
->space_info
->bytes_pinned
-= len
;
2038 spin_unlock(&cache
->lock
);
2039 spin_unlock(&cache
->space_info
->lock
);
2040 fs_info
->total_pinned
-= len
;
2042 btrfs_add_free_space(cache
, bytenr
, len
);
2044 put_block_group(cache
);
2051 static int update_reserved_extents(struct btrfs_root
*root
,
2052 u64 bytenr
, u64 num
, int reserve
)
2055 struct btrfs_block_group_cache
*cache
;
2056 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2059 cache
= btrfs_lookup_block_group(fs_info
, bytenr
);
2061 len
= min(num
, cache
->key
.offset
-
2062 (bytenr
- cache
->key
.objectid
));
2064 spin_lock(&cache
->space_info
->lock
);
2065 spin_lock(&cache
->lock
);
2067 cache
->reserved
+= len
;
2068 cache
->space_info
->bytes_reserved
+= len
;
2070 cache
->reserved
-= len
;
2071 cache
->space_info
->bytes_reserved
-= len
;
2073 spin_unlock(&cache
->lock
);
2074 spin_unlock(&cache
->space_info
->lock
);
2075 put_block_group(cache
);
2082 int btrfs_copy_pinned(struct btrfs_root
*root
, struct extent_io_tree
*copy
)
2087 struct extent_io_tree
*pinned_extents
= &root
->fs_info
->pinned_extents
;
2090 mutex_lock(&root
->fs_info
->pinned_mutex
);
2092 ret
= find_first_extent_bit(pinned_extents
, last
,
2093 &start
, &end
, EXTENT_DIRTY
);
2096 set_extent_dirty(copy
, start
, end
, GFP_NOFS
);
2099 mutex_unlock(&root
->fs_info
->pinned_mutex
);
2103 int btrfs_finish_extent_commit(struct btrfs_trans_handle
*trans
,
2104 struct btrfs_root
*root
,
2105 struct extent_io_tree
*unpin
)
2111 mutex_lock(&root
->fs_info
->pinned_mutex
);
2113 ret
= find_first_extent_bit(unpin
, 0, &start
, &end
,
2118 ret
= btrfs_discard_extent(root
, start
, end
+ 1 - start
);
2120 btrfs_update_pinned_extents(root
, start
, end
+ 1 - start
, 0);
2121 clear_extent_dirty(unpin
, start
, end
, GFP_NOFS
);
2123 if (need_resched()) {
2124 mutex_unlock(&root
->fs_info
->pinned_mutex
);
2126 mutex_lock(&root
->fs_info
->pinned_mutex
);
2129 mutex_unlock(&root
->fs_info
->pinned_mutex
);
2133 static int pin_down_bytes(struct btrfs_trans_handle
*trans
,
2134 struct btrfs_root
*root
,
2135 u64 bytenr
, u64 num_bytes
, int is_data
)
2138 struct extent_buffer
*buf
;
2143 buf
= btrfs_find_tree_block(root
, bytenr
, num_bytes
);
2147 /* we can reuse a block if it hasn't been written
2148 * and it is from this transaction. We can't
2149 * reuse anything from the tree log root because
2150 * it has tiny sub-transactions.
2152 if (btrfs_buffer_uptodate(buf
, 0) &&
2153 btrfs_try_tree_lock(buf
)) {
2154 u64 header_owner
= btrfs_header_owner(buf
);
2155 u64 header_transid
= btrfs_header_generation(buf
);
2156 if (header_owner
!= BTRFS_TREE_LOG_OBJECTID
&&
2157 header_owner
!= BTRFS_TREE_RELOC_OBJECTID
&&
2158 header_owner
!= BTRFS_DATA_RELOC_TREE_OBJECTID
&&
2159 header_transid
== trans
->transid
&&
2160 !btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
)) {
2161 clean_tree_block(NULL
, root
, buf
);
2162 btrfs_tree_unlock(buf
);
2163 free_extent_buffer(buf
);
2166 btrfs_tree_unlock(buf
);
2168 free_extent_buffer(buf
);
2170 btrfs_update_pinned_extents(root
, bytenr
, num_bytes
, 1);
2177 * remove an extent from the root, returns 0 on success
2179 static int __free_extent(struct btrfs_trans_handle
*trans
,
2180 struct btrfs_root
*root
,
2181 u64 bytenr
, u64 num_bytes
, u64 parent
,
2182 u64 root_objectid
, u64 ref_generation
,
2183 u64 owner_objectid
, int pin
, int mark_free
,
2186 struct btrfs_path
*path
;
2187 struct btrfs_key key
;
2188 struct btrfs_fs_info
*info
= root
->fs_info
;
2189 struct btrfs_root
*extent_root
= info
->extent_root
;
2190 struct extent_buffer
*leaf
;
2192 int extent_slot
= 0;
2193 int found_extent
= 0;
2195 struct btrfs_extent_item
*ei
;
2198 key
.objectid
= bytenr
;
2199 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
2200 key
.offset
= num_bytes
;
2201 path
= btrfs_alloc_path();
2206 ret
= lookup_extent_backref(trans
, extent_root
, path
,
2207 bytenr
, parent
, root_objectid
,
2208 ref_generation
, owner_objectid
, 1);
2210 struct btrfs_key found_key
;
2211 extent_slot
= path
->slots
[0];
2212 while (extent_slot
> 0) {
2214 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2216 if (found_key
.objectid
!= bytenr
)
2218 if (found_key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
2219 found_key
.offset
== num_bytes
) {
2223 if (path
->slots
[0] - extent_slot
> 5)
2226 if (!found_extent
) {
2227 ret
= remove_extent_backref(trans
, extent_root
, path
,
2230 btrfs_release_path(extent_root
, path
);
2231 ret
= btrfs_search_slot(trans
, extent_root
,
2234 printk(KERN_ERR
"umm, got %d back from search"
2235 ", was looking for %llu\n", ret
,
2236 (unsigned long long)bytenr
);
2237 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
2240 extent_slot
= path
->slots
[0];
2243 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
2245 printk(KERN_ERR
"btrfs unable to find ref byte nr %llu "
2246 "parent %llu root %llu gen %llu owner %llu\n",
2247 (unsigned long long)bytenr
,
2248 (unsigned long long)parent
,
2249 (unsigned long long)root_objectid
,
2250 (unsigned long long)ref_generation
,
2251 (unsigned long long)owner_objectid
);
2254 leaf
= path
->nodes
[0];
2255 ei
= btrfs_item_ptr(leaf
, extent_slot
,
2256 struct btrfs_extent_item
);
2257 refs
= btrfs_extent_refs(leaf
, ei
);
2260 * we're not allowed to delete the extent item if there
2261 * are other delayed ref updates pending
2264 BUG_ON(refs
< refs_to_drop
);
2265 refs
-= refs_to_drop
;
2266 btrfs_set_extent_refs(leaf
, ei
, refs
);
2267 btrfs_mark_buffer_dirty(leaf
);
2269 if (refs
== 0 && found_extent
&&
2270 path
->slots
[0] == extent_slot
+ 1) {
2271 struct btrfs_extent_ref
*ref
;
2272 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
2273 struct btrfs_extent_ref
);
2274 BUG_ON(btrfs_ref_num_refs(leaf
, ref
) != refs_to_drop
);
2275 /* if the back ref and the extent are next to each other
2276 * they get deleted below in one shot
2278 path
->slots
[0] = extent_slot
;
2280 } else if (found_extent
) {
2281 /* otherwise delete the extent back ref */
2282 ret
= remove_extent_backref(trans
, extent_root
, path
,
2285 /* if refs are 0, we need to setup the path for deletion */
2287 btrfs_release_path(extent_root
, path
);
2288 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
,
2299 mutex_lock(&root
->fs_info
->pinned_mutex
);
2300 ret
= pin_down_bytes(trans
, root
, bytenr
, num_bytes
,
2301 owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
);
2302 mutex_unlock(&root
->fs_info
->pinned_mutex
);
2307 /* block accounting for super block */
2308 spin_lock(&info
->delalloc_lock
);
2309 super_used
= btrfs_super_bytes_used(&info
->super_copy
);
2310 btrfs_set_super_bytes_used(&info
->super_copy
,
2311 super_used
- num_bytes
);
2313 /* block accounting for root item */
2314 root_used
= btrfs_root_used(&root
->root_item
);
2315 btrfs_set_root_used(&root
->root_item
,
2316 root_used
- num_bytes
);
2317 spin_unlock(&info
->delalloc_lock
);
2318 ret
= btrfs_del_items(trans
, extent_root
, path
, path
->slots
[0],
2321 btrfs_release_path(extent_root
, path
);
2323 if (owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
2324 ret
= btrfs_del_csums(trans
, root
, bytenr
, num_bytes
);
2328 ret
= update_block_group(trans
, root
, bytenr
, num_bytes
, 0,
2332 btrfs_free_path(path
);
2337 * remove an extent from the root, returns 0 on success
2339 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
2340 struct btrfs_root
*root
,
2341 u64 bytenr
, u64 num_bytes
, u64 parent
,
2342 u64 root_objectid
, u64 ref_generation
,
2343 u64 owner_objectid
, int pin
,
2346 WARN_ON(num_bytes
< root
->sectorsize
);
2349 * if metadata always pin
2350 * if data pin when any transaction has committed this
2352 if (owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
||
2353 ref_generation
!= trans
->transid
)
2356 if (ref_generation
!= trans
->transid
)
2359 return __free_extent(trans
, root
, bytenr
, num_bytes
, parent
,
2360 root_objectid
, ref_generation
,
2361 owner_objectid
, pin
, pin
== 0, refs_to_drop
);
2364 int btrfs_free_extent(struct btrfs_trans_handle
*trans
,
2365 struct btrfs_root
*root
,
2366 u64 bytenr
, u64 num_bytes
, u64 parent
,
2367 u64 root_objectid
, u64 ref_generation
,
2368 u64 owner_objectid
, int pin
)
2373 * tree log blocks never actually go into the extent allocation
2374 * tree, just update pinning info and exit early.
2376 * data extents referenced by the tree log do need to have
2377 * their reference counts bumped.
2379 if (root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
&&
2380 owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
) {
2381 mutex_lock(&root
->fs_info
->pinned_mutex
);
2382 btrfs_update_pinned_extents(root
, bytenr
, num_bytes
, 1);
2383 mutex_unlock(&root
->fs_info
->pinned_mutex
);
2384 update_reserved_extents(root
, bytenr
, num_bytes
, 0);
2387 ret
= btrfs_add_delayed_ref(trans
, bytenr
, num_bytes
, parent
,
2388 root_objectid
, ref_generation
,
2390 BTRFS_DROP_DELAYED_REF
, 1);
2395 static u64
stripe_align(struct btrfs_root
*root
, u64 val
)
2397 u64 mask
= ((u64
)root
->stripesize
- 1);
2398 u64 ret
= (val
+ mask
) & ~mask
;
2403 * walks the btree of allocated extents and find a hole of a given size.
2404 * The key ins is changed to record the hole:
2405 * ins->objectid == block start
2406 * ins->flags = BTRFS_EXTENT_ITEM_KEY
2407 * ins->offset == number of blocks
2408 * Any available blocks before search_start are skipped.
2410 static noinline
int find_free_extent(struct btrfs_trans_handle
*trans
,
2411 struct btrfs_root
*orig_root
,
2412 u64 num_bytes
, u64 empty_size
,
2413 u64 search_start
, u64 search_end
,
2414 u64 hint_byte
, struct btrfs_key
*ins
,
2415 u64 exclude_start
, u64 exclude_nr
,
2419 struct btrfs_root
*root
= orig_root
->fs_info
->extent_root
;
2420 u64 total_needed
= num_bytes
;
2421 u64
*last_ptr
= NULL
;
2422 u64 last_wanted
= 0;
2423 struct btrfs_block_group_cache
*block_group
= NULL
;
2424 int chunk_alloc_done
= 0;
2425 int empty_cluster
= 2 * 1024 * 1024;
2426 int allowed_chunk_alloc
= 0;
2427 struct list_head
*head
= NULL
, *cur
= NULL
;
2430 struct btrfs_space_info
*space_info
;
2432 WARN_ON(num_bytes
< root
->sectorsize
);
2433 btrfs_set_key_type(ins
, BTRFS_EXTENT_ITEM_KEY
);
2437 if (orig_root
->ref_cows
|| empty_size
)
2438 allowed_chunk_alloc
= 1;
2440 if (data
& BTRFS_BLOCK_GROUP_METADATA
) {
2441 last_ptr
= &root
->fs_info
->last_alloc
;
2442 if (!btrfs_test_opt(root
, SSD
))
2443 empty_cluster
= 64 * 1024;
2446 if ((data
& BTRFS_BLOCK_GROUP_DATA
) && btrfs_test_opt(root
, SSD
))
2447 last_ptr
= &root
->fs_info
->last_data_alloc
;
2451 hint_byte
= *last_ptr
;
2452 last_wanted
= *last_ptr
;
2454 empty_size
+= empty_cluster
;
2458 search_start
= max(search_start
, first_logical_byte(root
, 0));
2459 search_start
= max(search_start
, hint_byte
);
2461 if (last_wanted
&& search_start
!= last_wanted
) {
2463 empty_size
+= empty_cluster
;
2466 total_needed
+= empty_size
;
2467 block_group
= btrfs_lookup_block_group(root
->fs_info
, search_start
);
2469 block_group
= btrfs_lookup_first_block_group(root
->fs_info
,
2471 space_info
= __find_space_info(root
->fs_info
, data
);
2473 down_read(&space_info
->groups_sem
);
2475 struct btrfs_free_space
*free_space
;
2477 * the only way this happens if our hint points to a block
2478 * group thats not of the proper type, while looping this
2479 * should never happen
2485 goto new_group_no_lock
;
2487 if (unlikely(!block_group
->cached
)) {
2488 mutex_lock(&block_group
->cache_mutex
);
2489 ret
= cache_block_group(root
, block_group
);
2490 mutex_unlock(&block_group
->cache_mutex
);
2495 mutex_lock(&block_group
->alloc_mutex
);
2496 if (unlikely(!block_group_bits(block_group
, data
)))
2499 if (unlikely(block_group
->ro
))
2502 free_space
= btrfs_find_free_space(block_group
, search_start
,
2505 u64 start
= block_group
->key
.objectid
;
2506 u64 end
= block_group
->key
.objectid
+
2507 block_group
->key
.offset
;
2509 search_start
= stripe_align(root
, free_space
->offset
);
2511 /* move on to the next group */
2512 if (search_start
+ num_bytes
>= search_end
)
2515 /* move on to the next group */
2516 if (search_start
+ num_bytes
> end
)
2519 if (last_wanted
&& search_start
!= last_wanted
) {
2520 total_needed
+= empty_cluster
;
2521 empty_size
+= empty_cluster
;
2524 * if search_start is still in this block group
2525 * then we just re-search this block group
2527 if (search_start
>= start
&&
2528 search_start
< end
) {
2529 mutex_unlock(&block_group
->alloc_mutex
);
2533 /* else we go to the next block group */
2537 if (exclude_nr
> 0 &&
2538 (search_start
+ num_bytes
> exclude_start
&&
2539 search_start
< exclude_start
+ exclude_nr
)) {
2540 search_start
= exclude_start
+ exclude_nr
;
2542 * if search_start is still in this block group
2543 * then we just re-search this block group
2545 if (search_start
>= start
&&
2546 search_start
< end
) {
2547 mutex_unlock(&block_group
->alloc_mutex
);
2552 /* else we go to the next block group */
2556 ins
->objectid
= search_start
;
2557 ins
->offset
= num_bytes
;
2559 btrfs_remove_free_space_lock(block_group
, search_start
,
2561 /* we are all good, lets return */
2562 mutex_unlock(&block_group
->alloc_mutex
);
2566 mutex_unlock(&block_group
->alloc_mutex
);
2567 put_block_group(block_group
);
2570 /* don't try to compare new allocations against the
2571 * last allocation any more
2576 * Here's how this works.
2577 * loop == 0: we were searching a block group via a hint
2578 * and didn't find anything, so we start at
2579 * the head of the block groups and keep searching
2580 * loop == 1: we're searching through all of the block groups
2581 * if we hit the head again we have searched
2582 * all of the block groups for this space and we
2583 * need to try and allocate, if we cant error out.
2584 * loop == 2: we allocated more space and are looping through
2585 * all of the block groups again.
2588 head
= &space_info
->block_groups
;
2591 } else if (loop
== 1 && cur
== head
) {
2594 /* at this point we give up on the empty_size
2595 * allocations and just try to allocate the min
2598 * The extra_loop field was set if an empty_size
2599 * allocation was attempted above, and if this
2600 * is try we need to try the loop again without
2601 * the additional empty_size.
2603 total_needed
-= empty_size
;
2605 keep_going
= extra_loop
;
2608 if (allowed_chunk_alloc
&& !chunk_alloc_done
) {
2609 up_read(&space_info
->groups_sem
);
2610 ret
= do_chunk_alloc(trans
, root
, num_bytes
+
2611 2 * 1024 * 1024, data
, 1);
2612 down_read(&space_info
->groups_sem
);
2615 head
= &space_info
->block_groups
;
2617 * we've allocated a new chunk, keep
2621 chunk_alloc_done
= 1;
2622 } else if (!allowed_chunk_alloc
) {
2623 space_info
->force_alloc
= 1;
2632 } else if (cur
== head
) {
2636 block_group
= list_entry(cur
, struct btrfs_block_group_cache
,
2638 atomic_inc(&block_group
->count
);
2640 search_start
= block_group
->key
.objectid
;
2644 /* we found what we needed */
2645 if (ins
->objectid
) {
2646 if (!(data
& BTRFS_BLOCK_GROUP_DATA
))
2647 trans
->block_group
= block_group
->key
.objectid
;
2650 *last_ptr
= ins
->objectid
+ ins
->offset
;
2653 printk(KERN_ERR
"btrfs searching for %llu bytes, "
2654 "num_bytes %llu, loop %d, allowed_alloc %d\n",
2655 (unsigned long long)total_needed
,
2656 (unsigned long long)num_bytes
,
2657 loop
, allowed_chunk_alloc
);
2661 put_block_group(block_group
);
2663 up_read(&space_info
->groups_sem
);
2667 static void dump_space_info(struct btrfs_space_info
*info
, u64 bytes
)
2669 struct btrfs_block_group_cache
*cache
;
2671 printk(KERN_INFO
"space_info has %llu free, is %sfull\n",
2672 (unsigned long long)(info
->total_bytes
- info
->bytes_used
-
2673 info
->bytes_pinned
- info
->bytes_reserved
),
2674 (info
->full
) ? "" : "not ");
2675 printk(KERN_INFO
"space_info total=%llu, pinned=%llu, delalloc=%llu,"
2676 " may_use=%llu, used=%llu\n", info
->total_bytes
,
2677 info
->bytes_pinned
, info
->bytes_delalloc
, info
->bytes_may_use
,
2680 down_read(&info
->groups_sem
);
2681 list_for_each_entry(cache
, &info
->block_groups
, list
) {
2682 spin_lock(&cache
->lock
);
2683 printk(KERN_INFO
"block group %llu has %llu bytes, %llu used "
2684 "%llu pinned %llu reserved\n",
2685 (unsigned long long)cache
->key
.objectid
,
2686 (unsigned long long)cache
->key
.offset
,
2687 (unsigned long long)btrfs_block_group_used(&cache
->item
),
2688 (unsigned long long)cache
->pinned
,
2689 (unsigned long long)cache
->reserved
);
2690 btrfs_dump_free_space(cache
, bytes
);
2691 spin_unlock(&cache
->lock
);
2693 up_read(&info
->groups_sem
);
2696 static int __btrfs_reserve_extent(struct btrfs_trans_handle
*trans
,
2697 struct btrfs_root
*root
,
2698 u64 num_bytes
, u64 min_alloc_size
,
2699 u64 empty_size
, u64 hint_byte
,
2700 u64 search_end
, struct btrfs_key
*ins
,
2704 u64 search_start
= 0;
2705 struct btrfs_fs_info
*info
= root
->fs_info
;
2707 data
= btrfs_get_alloc_profile(root
, data
);
2710 * the only place that sets empty_size is btrfs_realloc_node, which
2711 * is not called recursively on allocations
2713 if (empty_size
|| root
->ref_cows
) {
2714 if (!(data
& BTRFS_BLOCK_GROUP_METADATA
)) {
2715 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
2717 BTRFS_BLOCK_GROUP_METADATA
|
2718 (info
->metadata_alloc_profile
&
2719 info
->avail_metadata_alloc_bits
), 0);
2721 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
2722 num_bytes
+ 2 * 1024 * 1024, data
, 0);
2725 WARN_ON(num_bytes
< root
->sectorsize
);
2726 ret
= find_free_extent(trans
, root
, num_bytes
, empty_size
,
2727 search_start
, search_end
, hint_byte
, ins
,
2728 trans
->alloc_exclude_start
,
2729 trans
->alloc_exclude_nr
, data
);
2731 if (ret
== -ENOSPC
&& num_bytes
> min_alloc_size
) {
2732 num_bytes
= num_bytes
>> 1;
2733 num_bytes
= num_bytes
& ~(root
->sectorsize
- 1);
2734 num_bytes
= max(num_bytes
, min_alloc_size
);
2735 do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
2736 num_bytes
, data
, 1);
2740 struct btrfs_space_info
*sinfo
;
2742 sinfo
= __find_space_info(root
->fs_info
, data
);
2743 printk(KERN_ERR
"btrfs allocation failed flags %llu, "
2744 "wanted %llu\n", (unsigned long long)data
,
2745 (unsigned long long)num_bytes
);
2746 dump_space_info(sinfo
, num_bytes
);
2753 int btrfs_free_reserved_extent(struct btrfs_root
*root
, u64 start
, u64 len
)
2755 struct btrfs_block_group_cache
*cache
;
2758 cache
= btrfs_lookup_block_group(root
->fs_info
, start
);
2760 printk(KERN_ERR
"Unable to find block group for %llu\n",
2761 (unsigned long long)start
);
2765 ret
= btrfs_discard_extent(root
, start
, len
);
2767 btrfs_add_free_space(cache
, start
, len
);
2768 put_block_group(cache
);
2769 update_reserved_extents(root
, start
, len
, 0);
2774 int btrfs_reserve_extent(struct btrfs_trans_handle
*trans
,
2775 struct btrfs_root
*root
,
2776 u64 num_bytes
, u64 min_alloc_size
,
2777 u64 empty_size
, u64 hint_byte
,
2778 u64 search_end
, struct btrfs_key
*ins
,
2782 ret
= __btrfs_reserve_extent(trans
, root
, num_bytes
, min_alloc_size
,
2783 empty_size
, hint_byte
, search_end
, ins
,
2785 update_reserved_extents(root
, ins
->objectid
, ins
->offset
, 1);
2789 static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle
*trans
,
2790 struct btrfs_root
*root
, u64 parent
,
2791 u64 root_objectid
, u64 ref_generation
,
2792 u64 owner
, struct btrfs_key
*ins
,
2798 u64 num_bytes
= ins
->offset
;
2800 struct btrfs_fs_info
*info
= root
->fs_info
;
2801 struct btrfs_root
*extent_root
= info
->extent_root
;
2802 struct btrfs_extent_item
*extent_item
;
2803 struct btrfs_extent_ref
*ref
;
2804 struct btrfs_path
*path
;
2805 struct btrfs_key keys
[2];
2808 parent
= ins
->objectid
;
2810 /* block accounting for super block */
2811 spin_lock(&info
->delalloc_lock
);
2812 super_used
= btrfs_super_bytes_used(&info
->super_copy
);
2813 btrfs_set_super_bytes_used(&info
->super_copy
, super_used
+ num_bytes
);
2815 /* block accounting for root item */
2816 root_used
= btrfs_root_used(&root
->root_item
);
2817 btrfs_set_root_used(&root
->root_item
, root_used
+ num_bytes
);
2818 spin_unlock(&info
->delalloc_lock
);
2820 memcpy(&keys
[0], ins
, sizeof(*ins
));
2821 keys
[1].objectid
= ins
->objectid
;
2822 keys
[1].type
= BTRFS_EXTENT_REF_KEY
;
2823 keys
[1].offset
= parent
;
2824 sizes
[0] = sizeof(*extent_item
);
2825 sizes
[1] = sizeof(*ref
);
2827 path
= btrfs_alloc_path();
2830 ret
= btrfs_insert_empty_items(trans
, extent_root
, path
, keys
,
2834 extent_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2835 struct btrfs_extent_item
);
2836 btrfs_set_extent_refs(path
->nodes
[0], extent_item
, ref_mod
);
2837 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
2838 struct btrfs_extent_ref
);
2840 btrfs_set_ref_root(path
->nodes
[0], ref
, root_objectid
);
2841 btrfs_set_ref_generation(path
->nodes
[0], ref
, ref_generation
);
2842 btrfs_set_ref_objectid(path
->nodes
[0], ref
, owner
);
2843 btrfs_set_ref_num_refs(path
->nodes
[0], ref
, ref_mod
);
2845 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2847 trans
->alloc_exclude_start
= 0;
2848 trans
->alloc_exclude_nr
= 0;
2849 btrfs_free_path(path
);
2854 ret
= update_block_group(trans
, root
, ins
->objectid
,
2857 printk(KERN_ERR
"btrfs update block group failed for %llu "
2858 "%llu\n", (unsigned long long)ins
->objectid
,
2859 (unsigned long long)ins
->offset
);
2866 int btrfs_alloc_reserved_extent(struct btrfs_trans_handle
*trans
,
2867 struct btrfs_root
*root
, u64 parent
,
2868 u64 root_objectid
, u64 ref_generation
,
2869 u64 owner
, struct btrfs_key
*ins
)
2873 if (root_objectid
== BTRFS_TREE_LOG_OBJECTID
)
2876 ret
= btrfs_add_delayed_ref(trans
, ins
->objectid
,
2877 ins
->offset
, parent
, root_objectid
,
2878 ref_generation
, owner
,
2879 BTRFS_ADD_DELAYED_EXTENT
, 0);
2885 * this is used by the tree logging recovery code. It records that
2886 * an extent has been allocated and makes sure to clear the free
2887 * space cache bits as well
2889 int btrfs_alloc_logged_extent(struct btrfs_trans_handle
*trans
,
2890 struct btrfs_root
*root
, u64 parent
,
2891 u64 root_objectid
, u64 ref_generation
,
2892 u64 owner
, struct btrfs_key
*ins
)
2895 struct btrfs_block_group_cache
*block_group
;
2897 block_group
= btrfs_lookup_block_group(root
->fs_info
, ins
->objectid
);
2898 mutex_lock(&block_group
->cache_mutex
);
2899 cache_block_group(root
, block_group
);
2900 mutex_unlock(&block_group
->cache_mutex
);
2902 ret
= btrfs_remove_free_space(block_group
, ins
->objectid
,
2905 put_block_group(block_group
);
2906 ret
= __btrfs_alloc_reserved_extent(trans
, root
, parent
, root_objectid
,
2907 ref_generation
, owner
, ins
, 1);
2912 * finds a free extent and does all the dirty work required for allocation
2913 * returns the key for the extent through ins, and a tree buffer for
2914 * the first block of the extent through buf.
2916 * returns 0 if everything worked, non-zero otherwise.
2918 int btrfs_alloc_extent(struct btrfs_trans_handle
*trans
,
2919 struct btrfs_root
*root
,
2920 u64 num_bytes
, u64 parent
, u64 min_alloc_size
,
2921 u64 root_objectid
, u64 ref_generation
,
2922 u64 owner_objectid
, u64 empty_size
, u64 hint_byte
,
2923 u64 search_end
, struct btrfs_key
*ins
, u64 data
)
2926 ret
= __btrfs_reserve_extent(trans
, root
, num_bytes
,
2927 min_alloc_size
, empty_size
, hint_byte
,
2928 search_end
, ins
, data
);
2930 if (root_objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
2931 ret
= btrfs_add_delayed_ref(trans
, ins
->objectid
,
2932 ins
->offset
, parent
, root_objectid
,
2933 ref_generation
, owner_objectid
,
2934 BTRFS_ADD_DELAYED_EXTENT
, 0);
2937 update_reserved_extents(root
, ins
->objectid
, ins
->offset
, 1);
2941 struct extent_buffer
*btrfs_init_new_buffer(struct btrfs_trans_handle
*trans
,
2942 struct btrfs_root
*root
,
2943 u64 bytenr
, u32 blocksize
,
2946 struct extent_buffer
*buf
;
2948 buf
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
2950 return ERR_PTR(-ENOMEM
);
2951 btrfs_set_header_generation(buf
, trans
->transid
);
2952 btrfs_set_buffer_lockdep_class(buf
, level
);
2953 btrfs_tree_lock(buf
);
2954 clean_tree_block(trans
, root
, buf
);
2956 btrfs_set_lock_blocking(buf
);
2957 btrfs_set_buffer_uptodate(buf
);
2959 if (root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
) {
2960 set_extent_dirty(&root
->dirty_log_pages
, buf
->start
,
2961 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
2963 set_extent_dirty(&trans
->transaction
->dirty_pages
, buf
->start
,
2964 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
2966 trans
->blocks_used
++;
2967 /* this returns a buffer locked for blocking */
2972 * helper function to allocate a block for a given tree
2973 * returns the tree buffer or NULL.
2975 struct extent_buffer
*btrfs_alloc_free_block(struct btrfs_trans_handle
*trans
,
2976 struct btrfs_root
*root
,
2977 u32 blocksize
, u64 parent
,
2984 struct btrfs_key ins
;
2986 struct extent_buffer
*buf
;
2988 ret
= btrfs_alloc_extent(trans
, root
, blocksize
, parent
, blocksize
,
2989 root_objectid
, ref_generation
, level
,
2990 empty_size
, hint
, (u64
)-1, &ins
, 0);
2993 return ERR_PTR(ret
);
2996 buf
= btrfs_init_new_buffer(trans
, root
, ins
.objectid
,
3001 int btrfs_drop_leaf_ref(struct btrfs_trans_handle
*trans
,
3002 struct btrfs_root
*root
, struct extent_buffer
*leaf
)
3005 u64 leaf_generation
;
3006 struct refsort
*sorted
;
3007 struct btrfs_key key
;
3008 struct btrfs_file_extent_item
*fi
;
3015 BUG_ON(!btrfs_is_leaf(leaf
));
3016 nritems
= btrfs_header_nritems(leaf
);
3017 leaf_owner
= btrfs_header_owner(leaf
);
3018 leaf_generation
= btrfs_header_generation(leaf
);
3020 sorted
= kmalloc(sizeof(*sorted
) * nritems
, GFP_NOFS
);
3021 /* we do this loop twice. The first time we build a list
3022 * of the extents we have a reference on, then we sort the list
3023 * by bytenr. The second time around we actually do the
3026 for (i
= 0; i
< nritems
; i
++) {
3030 btrfs_item_key_to_cpu(leaf
, &key
, i
);
3032 /* only extents have references, skip everything else */
3033 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
3036 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
3038 /* inline extents live in the btree, they don't have refs */
3039 if (btrfs_file_extent_type(leaf
, fi
) ==
3040 BTRFS_FILE_EXTENT_INLINE
)
3043 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
3045 /* holes don't have refs */
3046 if (disk_bytenr
== 0)
3049 sorted
[refi
].bytenr
= disk_bytenr
;
3050 sorted
[refi
].slot
= i
;
3057 sort(sorted
, refi
, sizeof(struct refsort
), refsort_cmp
, NULL
);
3059 for (i
= 0; i
< refi
; i
++) {
3062 disk_bytenr
= sorted
[i
].bytenr
;
3063 slot
= sorted
[i
].slot
;
3067 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
3068 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
3071 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
3073 ret
= btrfs_free_extent(trans
, root
, disk_bytenr
,
3074 btrfs_file_extent_disk_num_bytes(leaf
, fi
),
3075 leaf
->start
, leaf_owner
, leaf_generation
,
3079 atomic_inc(&root
->fs_info
->throttle_gen
);
3080 wake_up(&root
->fs_info
->transaction_throttle
);
3088 static noinline
int cache_drop_leaf_ref(struct btrfs_trans_handle
*trans
,
3089 struct btrfs_root
*root
,
3090 struct btrfs_leaf_ref
*ref
)
3094 struct btrfs_extent_info
*info
;
3095 struct refsort
*sorted
;
3097 if (ref
->nritems
== 0)
3100 sorted
= kmalloc(sizeof(*sorted
) * ref
->nritems
, GFP_NOFS
);
3101 for (i
= 0; i
< ref
->nritems
; i
++) {
3102 sorted
[i
].bytenr
= ref
->extents
[i
].bytenr
;
3105 sort(sorted
, ref
->nritems
, sizeof(struct refsort
), refsort_cmp
, NULL
);
3108 * the items in the ref were sorted when the ref was inserted
3109 * into the ref cache, so this is already in order
3111 for (i
= 0; i
< ref
->nritems
; i
++) {
3112 info
= ref
->extents
+ sorted
[i
].slot
;
3113 ret
= btrfs_free_extent(trans
, root
, info
->bytenr
,
3114 info
->num_bytes
, ref
->bytenr
,
3115 ref
->owner
, ref
->generation
,
3118 atomic_inc(&root
->fs_info
->throttle_gen
);
3119 wake_up(&root
->fs_info
->transaction_throttle
);
3130 static int drop_snap_lookup_refcount(struct btrfs_trans_handle
*trans
,
3131 struct btrfs_root
*root
, u64 start
,
3136 ret
= btrfs_lookup_extent_ref(trans
, root
, start
, len
, refs
);
3139 #if 0 /* some debugging code in case we see problems here */
3140 /* if the refs count is one, it won't get increased again. But
3141 * if the ref count is > 1, someone may be decreasing it at
3142 * the same time we are.
3145 struct extent_buffer
*eb
= NULL
;
3146 eb
= btrfs_find_create_tree_block(root
, start
, len
);
3148 btrfs_tree_lock(eb
);
3150 mutex_lock(&root
->fs_info
->alloc_mutex
);
3151 ret
= lookup_extent_ref(NULL
, root
, start
, len
, refs
);
3153 mutex_unlock(&root
->fs_info
->alloc_mutex
);
3156 btrfs_tree_unlock(eb
);
3157 free_extent_buffer(eb
);
3160 printk(KERN_ERR
"btrfs block %llu went down to one "
3161 "during drop_snap\n", (unsigned long long)start
);
3172 * this is used while deleting old snapshots, and it drops the refs
3173 * on a whole subtree starting from a level 1 node.
3175 * The idea is to sort all the leaf pointers, and then drop the
3176 * ref on all the leaves in order. Most of the time the leaves
3177 * will have ref cache entries, so no leaf IOs will be required to
3178 * find the extents they have references on.
3180 * For each leaf, any references it has are also dropped in order
3182 * This ends up dropping the references in something close to optimal
3183 * order for reading and modifying the extent allocation tree.
3185 static noinline
int drop_level_one_refs(struct btrfs_trans_handle
*trans
,
3186 struct btrfs_root
*root
,
3187 struct btrfs_path
*path
)
3192 struct extent_buffer
*eb
= path
->nodes
[1];
3193 struct extent_buffer
*leaf
;
3194 struct btrfs_leaf_ref
*ref
;
3195 struct refsort
*sorted
= NULL
;
3196 int nritems
= btrfs_header_nritems(eb
);
3200 int slot
= path
->slots
[1];
3201 u32 blocksize
= btrfs_level_size(root
, 0);
3207 root_owner
= btrfs_header_owner(eb
);
3208 root_gen
= btrfs_header_generation(eb
);
3209 sorted
= kmalloc(sizeof(*sorted
) * nritems
, GFP_NOFS
);
3212 * step one, sort all the leaf pointers so we don't scribble
3213 * randomly into the extent allocation tree
3215 for (i
= slot
; i
< nritems
; i
++) {
3216 sorted
[refi
].bytenr
= btrfs_node_blockptr(eb
, i
);
3217 sorted
[refi
].slot
= i
;
3222 * nritems won't be zero, but if we're picking up drop_snapshot
3223 * after a crash, slot might be > 0, so double check things
3229 sort(sorted
, refi
, sizeof(struct refsort
), refsort_cmp
, NULL
);
3232 * the first loop frees everything the leaves point to
3234 for (i
= 0; i
< refi
; i
++) {
3237 bytenr
= sorted
[i
].bytenr
;
3240 * check the reference count on this leaf. If it is > 1
3241 * we just decrement it below and don't update any
3242 * of the refs the leaf points to.
3244 ret
= drop_snap_lookup_refcount(trans
, root
, bytenr
,
3250 ptr_gen
= btrfs_node_ptr_generation(eb
, sorted
[i
].slot
);
3253 * the leaf only had one reference, which means the
3254 * only thing pointing to this leaf is the snapshot
3255 * we're deleting. It isn't possible for the reference
3256 * count to increase again later
3258 * The reference cache is checked for the leaf,
3259 * and if found we'll be able to drop any refs held by
3260 * the leaf without needing to read it in.
3262 ref
= btrfs_lookup_leaf_ref(root
, bytenr
);
3263 if (ref
&& ref
->generation
!= ptr_gen
) {
3264 btrfs_free_leaf_ref(root
, ref
);
3268 ret
= cache_drop_leaf_ref(trans
, root
, ref
);
3270 btrfs_remove_leaf_ref(root
, ref
);
3271 btrfs_free_leaf_ref(root
, ref
);
3274 * the leaf wasn't in the reference cache, so
3275 * we have to read it.
3277 leaf
= read_tree_block(root
, bytenr
, blocksize
,
3279 ret
= btrfs_drop_leaf_ref(trans
, root
, leaf
);
3281 free_extent_buffer(leaf
);
3283 atomic_inc(&root
->fs_info
->throttle_gen
);
3284 wake_up(&root
->fs_info
->transaction_throttle
);
3289 * run through the loop again to free the refs on the leaves.
3290 * This is faster than doing it in the loop above because
3291 * the leaves are likely to be clustered together. We end up
3292 * working in nice chunks on the extent allocation tree.
3294 for (i
= 0; i
< refi
; i
++) {
3295 bytenr
= sorted
[i
].bytenr
;
3296 ret
= btrfs_free_extent(trans
, root
, bytenr
,
3297 blocksize
, eb
->start
,
3298 root_owner
, root_gen
, 0, 1);
3301 atomic_inc(&root
->fs_info
->throttle_gen
);
3302 wake_up(&root
->fs_info
->transaction_throttle
);
3309 * update the path to show we've processed the entire level 1
3310 * node. This will get saved into the root's drop_snapshot_progress
3311 * field so these drops are not repeated again if this transaction
3314 path
->slots
[1] = nritems
;
3319 * helper function for drop_snapshot, this walks down the tree dropping ref
3320 * counts as it goes.
3322 static noinline
int walk_down_tree(struct btrfs_trans_handle
*trans
,
3323 struct btrfs_root
*root
,
3324 struct btrfs_path
*path
, int *level
)
3330 struct extent_buffer
*next
;
3331 struct extent_buffer
*cur
;
3332 struct extent_buffer
*parent
;
3337 WARN_ON(*level
< 0);
3338 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
3339 ret
= drop_snap_lookup_refcount(trans
, root
, path
->nodes
[*level
]->start
,
3340 path
->nodes
[*level
]->len
, &refs
);
3346 * walk down to the last node level and free all the leaves
3348 while (*level
>= 0) {
3349 WARN_ON(*level
< 0);
3350 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
3351 cur
= path
->nodes
[*level
];
3353 if (btrfs_header_level(cur
) != *level
)
3356 if (path
->slots
[*level
] >=
3357 btrfs_header_nritems(cur
))
3360 /* the new code goes down to level 1 and does all the
3361 * leaves pointed to that node in bulk. So, this check
3362 * for level 0 will always be false.
3364 * But, the disk format allows the drop_snapshot_progress
3365 * field in the root to leave things in a state where
3366 * a leaf will need cleaning up here. If someone crashes
3367 * with the old code and then boots with the new code,
3368 * we might find a leaf here.
3371 ret
= btrfs_drop_leaf_ref(trans
, root
, cur
);
3377 * once we get to level one, process the whole node
3378 * at once, including everything below it.
3381 ret
= drop_level_one_refs(trans
, root
, path
);
3386 bytenr
= btrfs_node_blockptr(cur
, path
->slots
[*level
]);
3387 ptr_gen
= btrfs_node_ptr_generation(cur
, path
->slots
[*level
]);
3388 blocksize
= btrfs_level_size(root
, *level
- 1);
3390 ret
= drop_snap_lookup_refcount(trans
, root
, bytenr
,
3395 * if there is more than one reference, we don't need
3396 * to read that node to drop any references it has. We
3397 * just drop the ref we hold on that node and move on to the
3398 * next slot in this level.
3401 parent
= path
->nodes
[*level
];
3402 root_owner
= btrfs_header_owner(parent
);
3403 root_gen
= btrfs_header_generation(parent
);
3404 path
->slots
[*level
]++;
3406 ret
= btrfs_free_extent(trans
, root
, bytenr
,
3407 blocksize
, parent
->start
,
3408 root_owner
, root_gen
,
3412 atomic_inc(&root
->fs_info
->throttle_gen
);
3413 wake_up(&root
->fs_info
->transaction_throttle
);
3420 * we need to keep freeing things in the next level down.
3421 * read the block and loop around to process it
3423 next
= read_tree_block(root
, bytenr
, blocksize
, ptr_gen
);
3424 WARN_ON(*level
<= 0);
3425 if (path
->nodes
[*level
-1])
3426 free_extent_buffer(path
->nodes
[*level
-1]);
3427 path
->nodes
[*level
-1] = next
;
3428 *level
= btrfs_header_level(next
);
3429 path
->slots
[*level
] = 0;
3433 WARN_ON(*level
< 0);
3434 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
3436 if (path
->nodes
[*level
] == root
->node
) {
3437 parent
= path
->nodes
[*level
];
3438 bytenr
= path
->nodes
[*level
]->start
;
3440 parent
= path
->nodes
[*level
+ 1];
3441 bytenr
= btrfs_node_blockptr(parent
, path
->slots
[*level
+ 1]);
3444 blocksize
= btrfs_level_size(root
, *level
);
3445 root_owner
= btrfs_header_owner(parent
);
3446 root_gen
= btrfs_header_generation(parent
);
3449 * cleanup and free the reference on the last node
3452 ret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
,
3453 parent
->start
, root_owner
, root_gen
,
3455 free_extent_buffer(path
->nodes
[*level
]);
3456 path
->nodes
[*level
] = NULL
;
3466 * helper function for drop_subtree, this function is similar to
3467 * walk_down_tree. The main difference is that it checks reference
3468 * counts while tree blocks are locked.
3470 static noinline
int walk_down_subtree(struct btrfs_trans_handle
*trans
,
3471 struct btrfs_root
*root
,
3472 struct btrfs_path
*path
, int *level
)
3474 struct extent_buffer
*next
;
3475 struct extent_buffer
*cur
;
3476 struct extent_buffer
*parent
;
3483 cur
= path
->nodes
[*level
];
3484 ret
= btrfs_lookup_extent_ref(trans
, root
, cur
->start
, cur
->len
,
3490 while (*level
>= 0) {
3491 cur
= path
->nodes
[*level
];
3493 ret
= btrfs_drop_leaf_ref(trans
, root
, cur
);
3495 clean_tree_block(trans
, root
, cur
);
3498 if (path
->slots
[*level
] >= btrfs_header_nritems(cur
)) {
3499 clean_tree_block(trans
, root
, cur
);
3503 bytenr
= btrfs_node_blockptr(cur
, path
->slots
[*level
]);
3504 blocksize
= btrfs_level_size(root
, *level
- 1);
3505 ptr_gen
= btrfs_node_ptr_generation(cur
, path
->slots
[*level
]);
3507 next
= read_tree_block(root
, bytenr
, blocksize
, ptr_gen
);
3508 btrfs_tree_lock(next
);
3509 btrfs_set_lock_blocking(next
);
3511 ret
= btrfs_lookup_extent_ref(trans
, root
, bytenr
, blocksize
,
3515 parent
= path
->nodes
[*level
];
3516 ret
= btrfs_free_extent(trans
, root
, bytenr
,
3517 blocksize
, parent
->start
,
3518 btrfs_header_owner(parent
),
3519 btrfs_header_generation(parent
),
3522 path
->slots
[*level
]++;
3523 btrfs_tree_unlock(next
);
3524 free_extent_buffer(next
);
3528 *level
= btrfs_header_level(next
);
3529 path
->nodes
[*level
] = next
;
3530 path
->slots
[*level
] = 0;
3531 path
->locks
[*level
] = 1;
3535 parent
= path
->nodes
[*level
+ 1];
3536 bytenr
= path
->nodes
[*level
]->start
;
3537 blocksize
= path
->nodes
[*level
]->len
;
3539 ret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
,
3540 parent
->start
, btrfs_header_owner(parent
),
3541 btrfs_header_generation(parent
), *level
, 1);
3544 if (path
->locks
[*level
]) {
3545 btrfs_tree_unlock(path
->nodes
[*level
]);
3546 path
->locks
[*level
] = 0;
3548 free_extent_buffer(path
->nodes
[*level
]);
3549 path
->nodes
[*level
] = NULL
;
3556 * helper for dropping snapshots. This walks back up the tree in the path
3557 * to find the first node higher up where we haven't yet gone through
3560 static noinline
int walk_up_tree(struct btrfs_trans_handle
*trans
,
3561 struct btrfs_root
*root
,
3562 struct btrfs_path
*path
,
3563 int *level
, int max_level
)
3567 struct btrfs_root_item
*root_item
= &root
->root_item
;
3572 for (i
= *level
; i
< max_level
&& path
->nodes
[i
]; i
++) {
3573 slot
= path
->slots
[i
];
3574 if (slot
< btrfs_header_nritems(path
->nodes
[i
]) - 1) {
3575 struct extent_buffer
*node
;
3576 struct btrfs_disk_key disk_key
;
3579 * there is more work to do in this level.
3580 * Update the drop_progress marker to reflect
3581 * the work we've done so far, and then bump
3584 node
= path
->nodes
[i
];
3587 WARN_ON(*level
== 0);
3588 btrfs_node_key(node
, &disk_key
, path
->slots
[i
]);
3589 memcpy(&root_item
->drop_progress
,
3590 &disk_key
, sizeof(disk_key
));
3591 root_item
->drop_level
= i
;
3594 struct extent_buffer
*parent
;
3597 * this whole node is done, free our reference
3598 * on it and go up one level
3600 if (path
->nodes
[*level
] == root
->node
)
3601 parent
= path
->nodes
[*level
];
3603 parent
= path
->nodes
[*level
+ 1];
3605 root_owner
= btrfs_header_owner(parent
);
3606 root_gen
= btrfs_header_generation(parent
);
3608 clean_tree_block(trans
, root
, path
->nodes
[*level
]);
3609 ret
= btrfs_free_extent(trans
, root
,
3610 path
->nodes
[*level
]->start
,
3611 path
->nodes
[*level
]->len
,
3612 parent
->start
, root_owner
,
3613 root_gen
, *level
, 1);
3615 if (path
->locks
[*level
]) {
3616 btrfs_tree_unlock(path
->nodes
[*level
]);
3617 path
->locks
[*level
] = 0;
3619 free_extent_buffer(path
->nodes
[*level
]);
3620 path
->nodes
[*level
] = NULL
;
3628 * drop the reference count on the tree rooted at 'snap'. This traverses
3629 * the tree freeing any blocks that have a ref count of zero after being
3632 int btrfs_drop_snapshot(struct btrfs_trans_handle
*trans
, struct btrfs_root
3638 struct btrfs_path
*path
;
3641 struct btrfs_root_item
*root_item
= &root
->root_item
;
3643 WARN_ON(!mutex_is_locked(&root
->fs_info
->drop_mutex
));
3644 path
= btrfs_alloc_path();
3647 level
= btrfs_header_level(root
->node
);
3649 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
3650 path
->nodes
[level
] = root
->node
;
3651 extent_buffer_get(root
->node
);
3652 path
->slots
[level
] = 0;
3654 struct btrfs_key key
;
3655 struct btrfs_disk_key found_key
;
3656 struct extent_buffer
*node
;
3658 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
3659 level
= root_item
->drop_level
;
3660 path
->lowest_level
= level
;
3661 wret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3666 node
= path
->nodes
[level
];
3667 btrfs_node_key(node
, &found_key
, path
->slots
[level
]);
3668 WARN_ON(memcmp(&found_key
, &root_item
->drop_progress
,
3669 sizeof(found_key
)));
3671 * unlock our path, this is safe because only this
3672 * function is allowed to delete this snapshot
3674 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++) {
3675 if (path
->nodes
[i
] && path
->locks
[i
]) {
3677 btrfs_tree_unlock(path
->nodes
[i
]);
3682 wret
= walk_down_tree(trans
, root
, path
, &level
);
3688 wret
= walk_up_tree(trans
, root
, path
, &level
,
3694 if (trans
->transaction
->in_commit
) {
3698 atomic_inc(&root
->fs_info
->throttle_gen
);
3699 wake_up(&root
->fs_info
->transaction_throttle
);
3701 for (i
= 0; i
<= orig_level
; i
++) {
3702 if (path
->nodes
[i
]) {
3703 free_extent_buffer(path
->nodes
[i
]);
3704 path
->nodes
[i
] = NULL
;
3708 btrfs_free_path(path
);
3712 int btrfs_drop_subtree(struct btrfs_trans_handle
*trans
,
3713 struct btrfs_root
*root
,
3714 struct extent_buffer
*node
,
3715 struct extent_buffer
*parent
)
3717 struct btrfs_path
*path
;
3723 path
= btrfs_alloc_path();
3726 btrfs_assert_tree_locked(parent
);
3727 parent_level
= btrfs_header_level(parent
);
3728 extent_buffer_get(parent
);
3729 path
->nodes
[parent_level
] = parent
;
3730 path
->slots
[parent_level
] = btrfs_header_nritems(parent
);
3732 btrfs_assert_tree_locked(node
);
3733 level
= btrfs_header_level(node
);
3734 extent_buffer_get(node
);
3735 path
->nodes
[level
] = node
;
3736 path
->slots
[level
] = 0;
3739 wret
= walk_down_subtree(trans
, root
, path
, &level
);
3745 wret
= walk_up_tree(trans
, root
, path
, &level
, parent_level
);
3752 btrfs_free_path(path
);
3756 static unsigned long calc_ra(unsigned long start
, unsigned long last
,
3759 return min(last
, start
+ nr
- 1);
3762 static noinline
int relocate_inode_pages(struct inode
*inode
, u64 start
,
3767 unsigned long first_index
;
3768 unsigned long last_index
;
3771 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3772 struct file_ra_state
*ra
;
3773 struct btrfs_ordered_extent
*ordered
;
3774 unsigned int total_read
= 0;
3775 unsigned int total_dirty
= 0;
3778 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
3780 mutex_lock(&inode
->i_mutex
);
3781 first_index
= start
>> PAGE_CACHE_SHIFT
;
3782 last_index
= (start
+ len
- 1) >> PAGE_CACHE_SHIFT
;
3784 /* make sure the dirty trick played by the caller work */
3785 ret
= invalidate_inode_pages2_range(inode
->i_mapping
,
3786 first_index
, last_index
);
3790 file_ra_state_init(ra
, inode
->i_mapping
);
3792 for (i
= first_index
; i
<= last_index
; i
++) {
3793 if (total_read
% ra
->ra_pages
== 0) {
3794 btrfs_force_ra(inode
->i_mapping
, ra
, NULL
, i
,
3795 calc_ra(i
, last_index
, ra
->ra_pages
));
3799 if (((u64
)i
<< PAGE_CACHE_SHIFT
) > i_size_read(inode
))
3801 page
= grab_cache_page(inode
->i_mapping
, i
);
3806 if (!PageUptodate(page
)) {
3807 btrfs_readpage(NULL
, page
);
3809 if (!PageUptodate(page
)) {
3811 page_cache_release(page
);
3816 wait_on_page_writeback(page
);
3818 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
3819 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
3820 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
3822 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
3824 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
3826 page_cache_release(page
);
3827 btrfs_start_ordered_extent(inode
, ordered
, 1);
3828 btrfs_put_ordered_extent(ordered
);
3831 set_page_extent_mapped(page
);
3833 if (i
== first_index
)
3834 set_extent_bits(io_tree
, page_start
, page_end
,
3835 EXTENT_BOUNDARY
, GFP_NOFS
);
3836 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
3838 set_page_dirty(page
);
3841 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
3843 page_cache_release(page
);
3848 mutex_unlock(&inode
->i_mutex
);
3849 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, total_dirty
);
3853 static noinline
int relocate_data_extent(struct inode
*reloc_inode
,
3854 struct btrfs_key
*extent_key
,
3857 struct btrfs_root
*root
= BTRFS_I(reloc_inode
)->root
;
3858 struct extent_map_tree
*em_tree
= &BTRFS_I(reloc_inode
)->extent_tree
;
3859 struct extent_map
*em
;
3860 u64 start
= extent_key
->objectid
- offset
;
3861 u64 end
= start
+ extent_key
->offset
- 1;
3863 em
= alloc_extent_map(GFP_NOFS
);
3864 BUG_ON(!em
|| IS_ERR(em
));
3867 em
->len
= extent_key
->offset
;
3868 em
->block_len
= extent_key
->offset
;
3869 em
->block_start
= extent_key
->objectid
;
3870 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
3871 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
3873 /* setup extent map to cheat btrfs_readpage */
3874 lock_extent(&BTRFS_I(reloc_inode
)->io_tree
, start
, end
, GFP_NOFS
);
3877 spin_lock(&em_tree
->lock
);
3878 ret
= add_extent_mapping(em_tree
, em
);
3879 spin_unlock(&em_tree
->lock
);
3880 if (ret
!= -EEXIST
) {
3881 free_extent_map(em
);
3884 btrfs_drop_extent_cache(reloc_inode
, start
, end
, 0);
3886 unlock_extent(&BTRFS_I(reloc_inode
)->io_tree
, start
, end
, GFP_NOFS
);
3888 return relocate_inode_pages(reloc_inode
, start
, extent_key
->offset
);
3891 struct btrfs_ref_path
{
3893 u64 nodes
[BTRFS_MAX_LEVEL
];
3895 u64 root_generation
;
3902 struct btrfs_key node_keys
[BTRFS_MAX_LEVEL
];
3903 u64 new_nodes
[BTRFS_MAX_LEVEL
];
3906 struct disk_extent
{
3917 static int is_cowonly_root(u64 root_objectid
)
3919 if (root_objectid
== BTRFS_ROOT_TREE_OBJECTID
||
3920 root_objectid
== BTRFS_EXTENT_TREE_OBJECTID
||
3921 root_objectid
== BTRFS_CHUNK_TREE_OBJECTID
||
3922 root_objectid
== BTRFS_DEV_TREE_OBJECTID
||
3923 root_objectid
== BTRFS_TREE_LOG_OBJECTID
||
3924 root_objectid
== BTRFS_CSUM_TREE_OBJECTID
)
3929 static noinline
int __next_ref_path(struct btrfs_trans_handle
*trans
,
3930 struct btrfs_root
*extent_root
,
3931 struct btrfs_ref_path
*ref_path
,
3934 struct extent_buffer
*leaf
;
3935 struct btrfs_path
*path
;
3936 struct btrfs_extent_ref
*ref
;
3937 struct btrfs_key key
;
3938 struct btrfs_key found_key
;
3944 path
= btrfs_alloc_path();
3949 ref_path
->lowest_level
= -1;
3950 ref_path
->current_level
= -1;
3951 ref_path
->shared_level
= -1;
3955 level
= ref_path
->current_level
- 1;
3956 while (level
>= -1) {
3958 if (level
< ref_path
->lowest_level
)
3962 bytenr
= ref_path
->nodes
[level
];
3964 bytenr
= ref_path
->extent_start
;
3965 BUG_ON(bytenr
== 0);
3967 parent
= ref_path
->nodes
[level
+ 1];
3968 ref_path
->nodes
[level
+ 1] = 0;
3969 ref_path
->current_level
= level
;
3970 BUG_ON(parent
== 0);
3972 key
.objectid
= bytenr
;
3973 key
.offset
= parent
+ 1;
3974 key
.type
= BTRFS_EXTENT_REF_KEY
;
3976 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
, 0, 0);
3981 leaf
= path
->nodes
[0];
3982 nritems
= btrfs_header_nritems(leaf
);
3983 if (path
->slots
[0] >= nritems
) {
3984 ret
= btrfs_next_leaf(extent_root
, path
);
3989 leaf
= path
->nodes
[0];
3992 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3993 if (found_key
.objectid
== bytenr
&&
3994 found_key
.type
== BTRFS_EXTENT_REF_KEY
) {
3995 if (level
< ref_path
->shared_level
)
3996 ref_path
->shared_level
= level
;
4001 btrfs_release_path(extent_root
, path
);
4004 /* reached lowest level */
4008 level
= ref_path
->current_level
;
4009 while (level
< BTRFS_MAX_LEVEL
- 1) {
4013 bytenr
= ref_path
->nodes
[level
];
4015 bytenr
= ref_path
->extent_start
;
4017 BUG_ON(bytenr
== 0);
4019 key
.objectid
= bytenr
;
4021 key
.type
= BTRFS_EXTENT_REF_KEY
;
4023 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
, 0, 0);
4027 leaf
= path
->nodes
[0];
4028 nritems
= btrfs_header_nritems(leaf
);
4029 if (path
->slots
[0] >= nritems
) {
4030 ret
= btrfs_next_leaf(extent_root
, path
);
4034 /* the extent was freed by someone */
4035 if (ref_path
->lowest_level
== level
)
4037 btrfs_release_path(extent_root
, path
);
4040 leaf
= path
->nodes
[0];
4043 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4044 if (found_key
.objectid
!= bytenr
||
4045 found_key
.type
!= BTRFS_EXTENT_REF_KEY
) {
4046 /* the extent was freed by someone */
4047 if (ref_path
->lowest_level
== level
) {
4051 btrfs_release_path(extent_root
, path
);
4055 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
4056 struct btrfs_extent_ref
);
4057 ref_objectid
= btrfs_ref_objectid(leaf
, ref
);
4058 if (ref_objectid
< BTRFS_FIRST_FREE_OBJECTID
) {
4060 level
= (int)ref_objectid
;
4061 BUG_ON(level
>= BTRFS_MAX_LEVEL
);
4062 ref_path
->lowest_level
= level
;
4063 ref_path
->current_level
= level
;
4064 ref_path
->nodes
[level
] = bytenr
;
4066 WARN_ON(ref_objectid
!= level
);
4069 WARN_ON(level
!= -1);
4073 if (ref_path
->lowest_level
== level
) {
4074 ref_path
->owner_objectid
= ref_objectid
;
4075 ref_path
->num_refs
= btrfs_ref_num_refs(leaf
, ref
);
4079 * the block is tree root or the block isn't in reference
4082 if (found_key
.objectid
== found_key
.offset
||
4083 is_cowonly_root(btrfs_ref_root(leaf
, ref
))) {
4084 ref_path
->root_objectid
= btrfs_ref_root(leaf
, ref
);
4085 ref_path
->root_generation
=
4086 btrfs_ref_generation(leaf
, ref
);
4088 /* special reference from the tree log */
4089 ref_path
->nodes
[0] = found_key
.offset
;
4090 ref_path
->current_level
= 0;
4097 BUG_ON(ref_path
->nodes
[level
] != 0);
4098 ref_path
->nodes
[level
] = found_key
.offset
;
4099 ref_path
->current_level
= level
;
4102 * the reference was created in the running transaction,
4103 * no need to continue walking up.
4105 if (btrfs_ref_generation(leaf
, ref
) == trans
->transid
) {
4106 ref_path
->root_objectid
= btrfs_ref_root(leaf
, ref
);
4107 ref_path
->root_generation
=
4108 btrfs_ref_generation(leaf
, ref
);
4113 btrfs_release_path(extent_root
, path
);
4116 /* reached max tree level, but no tree root found. */
4119 btrfs_free_path(path
);
4123 static int btrfs_first_ref_path(struct btrfs_trans_handle
*trans
,
4124 struct btrfs_root
*extent_root
,
4125 struct btrfs_ref_path
*ref_path
,
4128 memset(ref_path
, 0, sizeof(*ref_path
));
4129 ref_path
->extent_start
= extent_start
;
4131 return __next_ref_path(trans
, extent_root
, ref_path
, 1);
4134 static int btrfs_next_ref_path(struct btrfs_trans_handle
*trans
,
4135 struct btrfs_root
*extent_root
,
4136 struct btrfs_ref_path
*ref_path
)
4138 return __next_ref_path(trans
, extent_root
, ref_path
, 0);
4141 static noinline
int get_new_locations(struct inode
*reloc_inode
,
4142 struct btrfs_key
*extent_key
,
4143 u64 offset
, int no_fragment
,
4144 struct disk_extent
**extents
,
4147 struct btrfs_root
*root
= BTRFS_I(reloc_inode
)->root
;
4148 struct btrfs_path
*path
;
4149 struct btrfs_file_extent_item
*fi
;
4150 struct extent_buffer
*leaf
;
4151 struct disk_extent
*exts
= *extents
;
4152 struct btrfs_key found_key
;
4157 int max
= *nr_extents
;
4160 WARN_ON(!no_fragment
&& *extents
);
4163 exts
= kmalloc(sizeof(*exts
) * max
, GFP_NOFS
);
4168 path
= btrfs_alloc_path();
4171 cur_pos
= extent_key
->objectid
- offset
;
4172 last_byte
= extent_key
->objectid
+ extent_key
->offset
;
4173 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, reloc_inode
->i_ino
,
4183 leaf
= path
->nodes
[0];
4184 nritems
= btrfs_header_nritems(leaf
);
4185 if (path
->slots
[0] >= nritems
) {
4186 ret
= btrfs_next_leaf(root
, path
);
4191 leaf
= path
->nodes
[0];
4194 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4195 if (found_key
.offset
!= cur_pos
||
4196 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
||
4197 found_key
.objectid
!= reloc_inode
->i_ino
)
4200 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
4201 struct btrfs_file_extent_item
);
4202 if (btrfs_file_extent_type(leaf
, fi
) !=
4203 BTRFS_FILE_EXTENT_REG
||
4204 btrfs_file_extent_disk_bytenr(leaf
, fi
) == 0)
4208 struct disk_extent
*old
= exts
;
4210 exts
= kzalloc(sizeof(*exts
) * max
, GFP_NOFS
);
4211 memcpy(exts
, old
, sizeof(*exts
) * nr
);
4212 if (old
!= *extents
)
4216 exts
[nr
].disk_bytenr
=
4217 btrfs_file_extent_disk_bytenr(leaf
, fi
);
4218 exts
[nr
].disk_num_bytes
=
4219 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
4220 exts
[nr
].offset
= btrfs_file_extent_offset(leaf
, fi
);
4221 exts
[nr
].num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
4222 exts
[nr
].ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
4223 exts
[nr
].compression
= btrfs_file_extent_compression(leaf
, fi
);
4224 exts
[nr
].encryption
= btrfs_file_extent_encryption(leaf
, fi
);
4225 exts
[nr
].other_encoding
= btrfs_file_extent_other_encoding(leaf
,
4227 BUG_ON(exts
[nr
].offset
> 0);
4228 BUG_ON(exts
[nr
].compression
|| exts
[nr
].encryption
);
4229 BUG_ON(exts
[nr
].num_bytes
!= exts
[nr
].disk_num_bytes
);
4231 cur_pos
+= exts
[nr
].num_bytes
;
4234 if (cur_pos
+ offset
>= last_byte
)
4244 BUG_ON(cur_pos
+ offset
> last_byte
);
4245 if (cur_pos
+ offset
< last_byte
) {
4251 btrfs_free_path(path
);
4253 if (exts
!= *extents
)
4262 static noinline
int replace_one_extent(struct btrfs_trans_handle
*trans
,
4263 struct btrfs_root
*root
,
4264 struct btrfs_path
*path
,
4265 struct btrfs_key
*extent_key
,
4266 struct btrfs_key
*leaf_key
,
4267 struct btrfs_ref_path
*ref_path
,
4268 struct disk_extent
*new_extents
,
4271 struct extent_buffer
*leaf
;
4272 struct btrfs_file_extent_item
*fi
;
4273 struct inode
*inode
= NULL
;
4274 struct btrfs_key key
;
4279 u64 search_end
= (u64
)-1;
4282 int extent_locked
= 0;
4286 memcpy(&key
, leaf_key
, sizeof(key
));
4287 if (ref_path
->owner_objectid
!= BTRFS_MULTIPLE_OBJECTIDS
) {
4288 if (key
.objectid
< ref_path
->owner_objectid
||
4289 (key
.objectid
== ref_path
->owner_objectid
&&
4290 key
.type
< BTRFS_EXTENT_DATA_KEY
)) {
4291 key
.objectid
= ref_path
->owner_objectid
;
4292 key
.type
= BTRFS_EXTENT_DATA_KEY
;
4298 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
4302 leaf
= path
->nodes
[0];
4303 nritems
= btrfs_header_nritems(leaf
);
4305 if (extent_locked
&& ret
> 0) {
4307 * the file extent item was modified by someone
4308 * before the extent got locked.
4310 unlock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
4311 lock_end
, GFP_NOFS
);
4315 if (path
->slots
[0] >= nritems
) {
4316 if (++nr_scaned
> 2)
4319 BUG_ON(extent_locked
);
4320 ret
= btrfs_next_leaf(root
, path
);
4325 leaf
= path
->nodes
[0];
4326 nritems
= btrfs_header_nritems(leaf
);
4329 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
4331 if (ref_path
->owner_objectid
!= BTRFS_MULTIPLE_OBJECTIDS
) {
4332 if ((key
.objectid
> ref_path
->owner_objectid
) ||
4333 (key
.objectid
== ref_path
->owner_objectid
&&
4334 key
.type
> BTRFS_EXTENT_DATA_KEY
) ||
4335 key
.offset
>= search_end
)
4339 if (inode
&& key
.objectid
!= inode
->i_ino
) {
4340 BUG_ON(extent_locked
);
4341 btrfs_release_path(root
, path
);
4342 mutex_unlock(&inode
->i_mutex
);
4348 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
4353 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
4354 struct btrfs_file_extent_item
);
4355 extent_type
= btrfs_file_extent_type(leaf
, fi
);
4356 if ((extent_type
!= BTRFS_FILE_EXTENT_REG
&&
4357 extent_type
!= BTRFS_FILE_EXTENT_PREALLOC
) ||
4358 (btrfs_file_extent_disk_bytenr(leaf
, fi
) !=
4359 extent_key
->objectid
)) {
4365 num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
4366 ext_offset
= btrfs_file_extent_offset(leaf
, fi
);
4368 if (search_end
== (u64
)-1) {
4369 search_end
= key
.offset
- ext_offset
+
4370 btrfs_file_extent_ram_bytes(leaf
, fi
);
4373 if (!extent_locked
) {
4374 lock_start
= key
.offset
;
4375 lock_end
= lock_start
+ num_bytes
- 1;
4377 if (lock_start
> key
.offset
||
4378 lock_end
+ 1 < key
.offset
+ num_bytes
) {
4379 unlock_extent(&BTRFS_I(inode
)->io_tree
,
4380 lock_start
, lock_end
, GFP_NOFS
);
4386 btrfs_release_path(root
, path
);
4388 inode
= btrfs_iget_locked(root
->fs_info
->sb
,
4389 key
.objectid
, root
);
4390 if (inode
->i_state
& I_NEW
) {
4391 BTRFS_I(inode
)->root
= root
;
4392 BTRFS_I(inode
)->location
.objectid
=
4394 BTRFS_I(inode
)->location
.type
=
4395 BTRFS_INODE_ITEM_KEY
;
4396 BTRFS_I(inode
)->location
.offset
= 0;
4397 btrfs_read_locked_inode(inode
);
4398 unlock_new_inode(inode
);
4401 * some code call btrfs_commit_transaction while
4402 * holding the i_mutex, so we can't use mutex_lock
4405 if (is_bad_inode(inode
) ||
4406 !mutex_trylock(&inode
->i_mutex
)) {
4409 key
.offset
= (u64
)-1;
4414 if (!extent_locked
) {
4415 struct btrfs_ordered_extent
*ordered
;
4417 btrfs_release_path(root
, path
);
4419 lock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
4420 lock_end
, GFP_NOFS
);
4421 ordered
= btrfs_lookup_first_ordered_extent(inode
,
4424 ordered
->file_offset
<= lock_end
&&
4425 ordered
->file_offset
+ ordered
->len
> lock_start
) {
4426 unlock_extent(&BTRFS_I(inode
)->io_tree
,
4427 lock_start
, lock_end
, GFP_NOFS
);
4428 btrfs_start_ordered_extent(inode
, ordered
, 1);
4429 btrfs_put_ordered_extent(ordered
);
4430 key
.offset
+= num_bytes
;
4434 btrfs_put_ordered_extent(ordered
);
4440 if (nr_extents
== 1) {
4441 /* update extent pointer in place */
4442 btrfs_set_file_extent_disk_bytenr(leaf
, fi
,
4443 new_extents
[0].disk_bytenr
);
4444 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
,
4445 new_extents
[0].disk_num_bytes
);
4446 btrfs_mark_buffer_dirty(leaf
);
4448 btrfs_drop_extent_cache(inode
, key
.offset
,
4449 key
.offset
+ num_bytes
- 1, 0);
4451 ret
= btrfs_inc_extent_ref(trans
, root
,
4452 new_extents
[0].disk_bytenr
,
4453 new_extents
[0].disk_num_bytes
,
4455 root
->root_key
.objectid
,
4460 ret
= btrfs_free_extent(trans
, root
,
4461 extent_key
->objectid
,
4464 btrfs_header_owner(leaf
),
4465 btrfs_header_generation(leaf
),
4469 btrfs_release_path(root
, path
);
4470 key
.offset
+= num_bytes
;
4478 * drop old extent pointer at first, then insert the
4479 * new pointers one bye one
4481 btrfs_release_path(root
, path
);
4482 ret
= btrfs_drop_extents(trans
, root
, inode
, key
.offset
,
4483 key
.offset
+ num_bytes
,
4484 key
.offset
, &alloc_hint
);
4487 for (i
= 0; i
< nr_extents
; i
++) {
4488 if (ext_offset
>= new_extents
[i
].num_bytes
) {
4489 ext_offset
-= new_extents
[i
].num_bytes
;
4492 extent_len
= min(new_extents
[i
].num_bytes
-
4493 ext_offset
, num_bytes
);
4495 ret
= btrfs_insert_empty_item(trans
, root
,
4500 leaf
= path
->nodes
[0];
4501 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
4502 struct btrfs_file_extent_item
);
4503 btrfs_set_file_extent_generation(leaf
, fi
,
4505 btrfs_set_file_extent_type(leaf
, fi
,
4506 BTRFS_FILE_EXTENT_REG
);
4507 btrfs_set_file_extent_disk_bytenr(leaf
, fi
,
4508 new_extents
[i
].disk_bytenr
);
4509 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
,
4510 new_extents
[i
].disk_num_bytes
);
4511 btrfs_set_file_extent_ram_bytes(leaf
, fi
,
4512 new_extents
[i
].ram_bytes
);
4514 btrfs_set_file_extent_compression(leaf
, fi
,
4515 new_extents
[i
].compression
);
4516 btrfs_set_file_extent_encryption(leaf
, fi
,
4517 new_extents
[i
].encryption
);
4518 btrfs_set_file_extent_other_encoding(leaf
, fi
,
4519 new_extents
[i
].other_encoding
);
4521 btrfs_set_file_extent_num_bytes(leaf
, fi
,
4523 ext_offset
+= new_extents
[i
].offset
;
4524 btrfs_set_file_extent_offset(leaf
, fi
,
4526 btrfs_mark_buffer_dirty(leaf
);
4528 btrfs_drop_extent_cache(inode
, key
.offset
,
4529 key
.offset
+ extent_len
- 1, 0);
4531 ret
= btrfs_inc_extent_ref(trans
, root
,
4532 new_extents
[i
].disk_bytenr
,
4533 new_extents
[i
].disk_num_bytes
,
4535 root
->root_key
.objectid
,
4536 trans
->transid
, key
.objectid
);
4538 btrfs_release_path(root
, path
);
4540 inode_add_bytes(inode
, extent_len
);
4543 num_bytes
-= extent_len
;
4544 key
.offset
+= extent_len
;
4549 BUG_ON(i
>= nr_extents
);
4553 if (extent_locked
) {
4554 unlock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
4555 lock_end
, GFP_NOFS
);
4559 if (ref_path
->owner_objectid
!= BTRFS_MULTIPLE_OBJECTIDS
&&
4560 key
.offset
>= search_end
)
4567 btrfs_release_path(root
, path
);
4569 mutex_unlock(&inode
->i_mutex
);
4570 if (extent_locked
) {
4571 unlock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
4572 lock_end
, GFP_NOFS
);
4579 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle
*trans
,
4580 struct btrfs_root
*root
,
4581 struct extent_buffer
*buf
, u64 orig_start
)
4586 BUG_ON(btrfs_header_generation(buf
) != trans
->transid
);
4587 BUG_ON(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
4589 level
= btrfs_header_level(buf
);
4591 struct btrfs_leaf_ref
*ref
;
4592 struct btrfs_leaf_ref
*orig_ref
;
4594 orig_ref
= btrfs_lookup_leaf_ref(root
, orig_start
);
4598 ref
= btrfs_alloc_leaf_ref(root
, orig_ref
->nritems
);
4600 btrfs_free_leaf_ref(root
, orig_ref
);
4604 ref
->nritems
= orig_ref
->nritems
;
4605 memcpy(ref
->extents
, orig_ref
->extents
,
4606 sizeof(ref
->extents
[0]) * ref
->nritems
);
4608 btrfs_free_leaf_ref(root
, orig_ref
);
4610 ref
->root_gen
= trans
->transid
;
4611 ref
->bytenr
= buf
->start
;
4612 ref
->owner
= btrfs_header_owner(buf
);
4613 ref
->generation
= btrfs_header_generation(buf
);
4615 ret
= btrfs_add_leaf_ref(root
, ref
, 0);
4617 btrfs_free_leaf_ref(root
, ref
);
4622 static noinline
int invalidate_extent_cache(struct btrfs_root
*root
,
4623 struct extent_buffer
*leaf
,
4624 struct btrfs_block_group_cache
*group
,
4625 struct btrfs_root
*target_root
)
4627 struct btrfs_key key
;
4628 struct inode
*inode
= NULL
;
4629 struct btrfs_file_extent_item
*fi
;
4631 u64 skip_objectid
= 0;
4635 nritems
= btrfs_header_nritems(leaf
);
4636 for (i
= 0; i
< nritems
; i
++) {
4637 btrfs_item_key_to_cpu(leaf
, &key
, i
);
4638 if (key
.objectid
== skip_objectid
||
4639 key
.type
!= BTRFS_EXTENT_DATA_KEY
)
4641 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
4642 if (btrfs_file_extent_type(leaf
, fi
) ==
4643 BTRFS_FILE_EXTENT_INLINE
)
4645 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) == 0)
4647 if (!inode
|| inode
->i_ino
!= key
.objectid
) {
4649 inode
= btrfs_ilookup(target_root
->fs_info
->sb
,
4650 key
.objectid
, target_root
, 1);
4653 skip_objectid
= key
.objectid
;
4656 num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
4658 lock_extent(&BTRFS_I(inode
)->io_tree
, key
.offset
,
4659 key
.offset
+ num_bytes
- 1, GFP_NOFS
);
4660 btrfs_drop_extent_cache(inode
, key
.offset
,
4661 key
.offset
+ num_bytes
- 1, 1);
4662 unlock_extent(&BTRFS_I(inode
)->io_tree
, key
.offset
,
4663 key
.offset
+ num_bytes
- 1, GFP_NOFS
);
4670 static noinline
int replace_extents_in_leaf(struct btrfs_trans_handle
*trans
,
4671 struct btrfs_root
*root
,
4672 struct extent_buffer
*leaf
,
4673 struct btrfs_block_group_cache
*group
,
4674 struct inode
*reloc_inode
)
4676 struct btrfs_key key
;
4677 struct btrfs_key extent_key
;
4678 struct btrfs_file_extent_item
*fi
;
4679 struct btrfs_leaf_ref
*ref
;
4680 struct disk_extent
*new_extent
;
4689 new_extent
= kmalloc(sizeof(*new_extent
), GFP_NOFS
);
4690 BUG_ON(!new_extent
);
4692 ref
= btrfs_lookup_leaf_ref(root
, leaf
->start
);
4696 nritems
= btrfs_header_nritems(leaf
);
4697 for (i
= 0; i
< nritems
; i
++) {
4698 btrfs_item_key_to_cpu(leaf
, &key
, i
);
4699 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
4701 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
4702 if (btrfs_file_extent_type(leaf
, fi
) ==
4703 BTRFS_FILE_EXTENT_INLINE
)
4705 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
4706 num_bytes
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
4711 if (bytenr
>= group
->key
.objectid
+ group
->key
.offset
||
4712 bytenr
+ num_bytes
<= group
->key
.objectid
)
4715 extent_key
.objectid
= bytenr
;
4716 extent_key
.offset
= num_bytes
;
4717 extent_key
.type
= BTRFS_EXTENT_ITEM_KEY
;
4719 ret
= get_new_locations(reloc_inode
, &extent_key
,
4720 group
->key
.objectid
, 1,
4721 &new_extent
, &nr_extent
);
4726 BUG_ON(ref
->extents
[ext_index
].bytenr
!= bytenr
);
4727 BUG_ON(ref
->extents
[ext_index
].num_bytes
!= num_bytes
);
4728 ref
->extents
[ext_index
].bytenr
= new_extent
->disk_bytenr
;
4729 ref
->extents
[ext_index
].num_bytes
= new_extent
->disk_num_bytes
;
4731 btrfs_set_file_extent_disk_bytenr(leaf
, fi
,
4732 new_extent
->disk_bytenr
);
4733 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
,
4734 new_extent
->disk_num_bytes
);
4735 btrfs_mark_buffer_dirty(leaf
);
4737 ret
= btrfs_inc_extent_ref(trans
, root
,
4738 new_extent
->disk_bytenr
,
4739 new_extent
->disk_num_bytes
,
4741 root
->root_key
.objectid
,
4742 trans
->transid
, key
.objectid
);
4745 ret
= btrfs_free_extent(trans
, root
,
4746 bytenr
, num_bytes
, leaf
->start
,
4747 btrfs_header_owner(leaf
),
4748 btrfs_header_generation(leaf
),
4754 BUG_ON(ext_index
+ 1 != ref
->nritems
);
4755 btrfs_free_leaf_ref(root
, ref
);
4759 int btrfs_free_reloc_root(struct btrfs_trans_handle
*trans
,
4760 struct btrfs_root
*root
)
4762 struct btrfs_root
*reloc_root
;
4765 if (root
->reloc_root
) {
4766 reloc_root
= root
->reloc_root
;
4767 root
->reloc_root
= NULL
;
4768 list_add(&reloc_root
->dead_list
,
4769 &root
->fs_info
->dead_reloc_roots
);
4771 btrfs_set_root_bytenr(&reloc_root
->root_item
,
4772 reloc_root
->node
->start
);
4773 btrfs_set_root_level(&root
->root_item
,
4774 btrfs_header_level(reloc_root
->node
));
4775 memset(&reloc_root
->root_item
.drop_progress
, 0,
4776 sizeof(struct btrfs_disk_key
));
4777 reloc_root
->root_item
.drop_level
= 0;
4779 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
4780 &reloc_root
->root_key
,
4781 &reloc_root
->root_item
);
4787 int btrfs_drop_dead_reloc_roots(struct btrfs_root
*root
)
4789 struct btrfs_trans_handle
*trans
;
4790 struct btrfs_root
*reloc_root
;
4791 struct btrfs_root
*prev_root
= NULL
;
4792 struct list_head dead_roots
;
4796 INIT_LIST_HEAD(&dead_roots
);
4797 list_splice_init(&root
->fs_info
->dead_reloc_roots
, &dead_roots
);
4799 while (!list_empty(&dead_roots
)) {
4800 reloc_root
= list_entry(dead_roots
.prev
,
4801 struct btrfs_root
, dead_list
);
4802 list_del_init(&reloc_root
->dead_list
);
4804 BUG_ON(reloc_root
->commit_root
!= NULL
);
4806 trans
= btrfs_join_transaction(root
, 1);
4809 mutex_lock(&root
->fs_info
->drop_mutex
);
4810 ret
= btrfs_drop_snapshot(trans
, reloc_root
);
4813 mutex_unlock(&root
->fs_info
->drop_mutex
);
4815 nr
= trans
->blocks_used
;
4816 ret
= btrfs_end_transaction(trans
, root
);
4818 btrfs_btree_balance_dirty(root
, nr
);
4821 free_extent_buffer(reloc_root
->node
);
4823 ret
= btrfs_del_root(trans
, root
->fs_info
->tree_root
,
4824 &reloc_root
->root_key
);
4826 mutex_unlock(&root
->fs_info
->drop_mutex
);
4828 nr
= trans
->blocks_used
;
4829 ret
= btrfs_end_transaction(trans
, root
);
4831 btrfs_btree_balance_dirty(root
, nr
);
4834 prev_root
= reloc_root
;
4837 btrfs_remove_leaf_refs(prev_root
, (u64
)-1, 0);
4843 int btrfs_add_dead_reloc_root(struct btrfs_root
*root
)
4845 list_add(&root
->dead_list
, &root
->fs_info
->dead_reloc_roots
);
4849 int btrfs_cleanup_reloc_trees(struct btrfs_root
*root
)
4851 struct btrfs_root
*reloc_root
;
4852 struct btrfs_trans_handle
*trans
;
4853 struct btrfs_key location
;
4857 mutex_lock(&root
->fs_info
->tree_reloc_mutex
);
4858 ret
= btrfs_find_dead_roots(root
, BTRFS_TREE_RELOC_OBJECTID
, NULL
);
4860 found
= !list_empty(&root
->fs_info
->dead_reloc_roots
);
4861 mutex_unlock(&root
->fs_info
->tree_reloc_mutex
);
4864 trans
= btrfs_start_transaction(root
, 1);
4866 ret
= btrfs_commit_transaction(trans
, root
);
4870 location
.objectid
= BTRFS_DATA_RELOC_TREE_OBJECTID
;
4871 location
.offset
= (u64
)-1;
4872 location
.type
= BTRFS_ROOT_ITEM_KEY
;
4874 reloc_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
4875 BUG_ON(!reloc_root
);
4876 btrfs_orphan_cleanup(reloc_root
);
4880 static noinline
int init_reloc_tree(struct btrfs_trans_handle
*trans
,
4881 struct btrfs_root
*root
)
4883 struct btrfs_root
*reloc_root
;
4884 struct extent_buffer
*eb
;
4885 struct btrfs_root_item
*root_item
;
4886 struct btrfs_key root_key
;
4889 BUG_ON(!root
->ref_cows
);
4890 if (root
->reloc_root
)
4893 root_item
= kmalloc(sizeof(*root_item
), GFP_NOFS
);
4896 ret
= btrfs_copy_root(trans
, root
, root
->commit_root
,
4897 &eb
, BTRFS_TREE_RELOC_OBJECTID
);
4900 root_key
.objectid
= BTRFS_TREE_RELOC_OBJECTID
;
4901 root_key
.offset
= root
->root_key
.objectid
;
4902 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
4904 memcpy(root_item
, &root
->root_item
, sizeof(root_item
));
4905 btrfs_set_root_refs(root_item
, 0);
4906 btrfs_set_root_bytenr(root_item
, eb
->start
);
4907 btrfs_set_root_level(root_item
, btrfs_header_level(eb
));
4908 btrfs_set_root_generation(root_item
, trans
->transid
);
4910 btrfs_tree_unlock(eb
);
4911 free_extent_buffer(eb
);
4913 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
,
4914 &root_key
, root_item
);
4918 reloc_root
= btrfs_read_fs_root_no_radix(root
->fs_info
->tree_root
,
4920 BUG_ON(!reloc_root
);
4921 reloc_root
->last_trans
= trans
->transid
;
4922 reloc_root
->commit_root
= NULL
;
4923 reloc_root
->ref_tree
= &root
->fs_info
->reloc_ref_tree
;
4925 root
->reloc_root
= reloc_root
;
4930 * Core function of space balance.
4932 * The idea is using reloc trees to relocate tree blocks in reference
4933 * counted roots. There is one reloc tree for each subvol, and all
4934 * reloc trees share same root key objectid. Reloc trees are snapshots
4935 * of the latest committed roots of subvols (root->commit_root).
4937 * To relocate a tree block referenced by a subvol, there are two steps.
4938 * COW the block through subvol's reloc tree, then update block pointer
4939 * in the subvol to point to the new block. Since all reloc trees share
4940 * same root key objectid, doing special handing for tree blocks owned
4941 * by them is easy. Once a tree block has been COWed in one reloc tree,
4942 * we can use the resulting new block directly when the same block is
4943 * required to COW again through other reloc trees. By this way, relocated
4944 * tree blocks are shared between reloc trees, so they are also shared
4947 static noinline
int relocate_one_path(struct btrfs_trans_handle
*trans
,
4948 struct btrfs_root
*root
,
4949 struct btrfs_path
*path
,
4950 struct btrfs_key
*first_key
,
4951 struct btrfs_ref_path
*ref_path
,
4952 struct btrfs_block_group_cache
*group
,
4953 struct inode
*reloc_inode
)
4955 struct btrfs_root
*reloc_root
;
4956 struct extent_buffer
*eb
= NULL
;
4957 struct btrfs_key
*keys
;
4961 int lowest_level
= 0;
4964 if (ref_path
->owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
)
4965 lowest_level
= ref_path
->owner_objectid
;
4967 if (!root
->ref_cows
) {
4968 path
->lowest_level
= lowest_level
;
4969 ret
= btrfs_search_slot(trans
, root
, first_key
, path
, 0, 1);
4971 path
->lowest_level
= 0;
4972 btrfs_release_path(root
, path
);
4976 mutex_lock(&root
->fs_info
->tree_reloc_mutex
);
4977 ret
= init_reloc_tree(trans
, root
);
4979 reloc_root
= root
->reloc_root
;
4981 shared_level
= ref_path
->shared_level
;
4982 ref_path
->shared_level
= BTRFS_MAX_LEVEL
- 1;
4984 keys
= ref_path
->node_keys
;
4985 nodes
= ref_path
->new_nodes
;
4986 memset(&keys
[shared_level
+ 1], 0,
4987 sizeof(*keys
) * (BTRFS_MAX_LEVEL
- shared_level
- 1));
4988 memset(&nodes
[shared_level
+ 1], 0,
4989 sizeof(*nodes
) * (BTRFS_MAX_LEVEL
- shared_level
- 1));
4991 if (nodes
[lowest_level
] == 0) {
4992 path
->lowest_level
= lowest_level
;
4993 ret
= btrfs_search_slot(trans
, reloc_root
, first_key
, path
,
4996 for (level
= lowest_level
; level
< BTRFS_MAX_LEVEL
; level
++) {
4997 eb
= path
->nodes
[level
];
4998 if (!eb
|| eb
== reloc_root
->node
)
5000 nodes
[level
] = eb
->start
;
5002 btrfs_item_key_to_cpu(eb
, &keys
[level
], 0);
5004 btrfs_node_key_to_cpu(eb
, &keys
[level
], 0);
5007 ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
5008 eb
= path
->nodes
[0];
5009 ret
= replace_extents_in_leaf(trans
, reloc_root
, eb
,
5010 group
, reloc_inode
);
5013 btrfs_release_path(reloc_root
, path
);
5015 ret
= btrfs_merge_path(trans
, reloc_root
, keys
, nodes
,
5021 * replace tree blocks in the fs tree with tree blocks in
5024 ret
= btrfs_merge_path(trans
, root
, keys
, nodes
, lowest_level
);
5027 if (ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
5028 ret
= btrfs_search_slot(trans
, reloc_root
, first_key
, path
,
5031 extent_buffer_get(path
->nodes
[0]);
5032 eb
= path
->nodes
[0];
5033 btrfs_release_path(reloc_root
, path
);
5034 ret
= invalidate_extent_cache(reloc_root
, eb
, group
, root
);
5036 free_extent_buffer(eb
);
5039 mutex_unlock(&root
->fs_info
->tree_reloc_mutex
);
5040 path
->lowest_level
= 0;
5044 static noinline
int relocate_tree_block(struct btrfs_trans_handle
*trans
,
5045 struct btrfs_root
*root
,
5046 struct btrfs_path
*path
,
5047 struct btrfs_key
*first_key
,
5048 struct btrfs_ref_path
*ref_path
)
5052 ret
= relocate_one_path(trans
, root
, path
, first_key
,
5053 ref_path
, NULL
, NULL
);
5059 static noinline
int del_extent_zero(struct btrfs_trans_handle
*trans
,
5060 struct btrfs_root
*extent_root
,
5061 struct btrfs_path
*path
,
5062 struct btrfs_key
*extent_key
)
5066 ret
= btrfs_search_slot(trans
, extent_root
, extent_key
, path
, -1, 1);
5069 ret
= btrfs_del_item(trans
, extent_root
, path
);
5071 btrfs_release_path(extent_root
, path
);
5075 static noinline
struct btrfs_root
*read_ref_root(struct btrfs_fs_info
*fs_info
,
5076 struct btrfs_ref_path
*ref_path
)
5078 struct btrfs_key root_key
;
5080 root_key
.objectid
= ref_path
->root_objectid
;
5081 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
5082 if (is_cowonly_root(ref_path
->root_objectid
))
5083 root_key
.offset
= 0;
5085 root_key
.offset
= (u64
)-1;
5087 return btrfs_read_fs_root_no_name(fs_info
, &root_key
);
5090 static noinline
int relocate_one_extent(struct btrfs_root
*extent_root
,
5091 struct btrfs_path
*path
,
5092 struct btrfs_key
*extent_key
,
5093 struct btrfs_block_group_cache
*group
,
5094 struct inode
*reloc_inode
, int pass
)
5096 struct btrfs_trans_handle
*trans
;
5097 struct btrfs_root
*found_root
;
5098 struct btrfs_ref_path
*ref_path
= NULL
;
5099 struct disk_extent
*new_extents
= NULL
;
5104 struct btrfs_key first_key
;
5108 trans
= btrfs_start_transaction(extent_root
, 1);
5111 if (extent_key
->objectid
== 0) {
5112 ret
= del_extent_zero(trans
, extent_root
, path
, extent_key
);
5116 ref_path
= kmalloc(sizeof(*ref_path
), GFP_NOFS
);
5122 for (loops
= 0; ; loops
++) {
5124 ret
= btrfs_first_ref_path(trans
, extent_root
, ref_path
,
5125 extent_key
->objectid
);
5127 ret
= btrfs_next_ref_path(trans
, extent_root
, ref_path
);
5134 if (ref_path
->root_objectid
== BTRFS_TREE_LOG_OBJECTID
||
5135 ref_path
->root_objectid
== BTRFS_TREE_RELOC_OBJECTID
)
5138 found_root
= read_ref_root(extent_root
->fs_info
, ref_path
);
5139 BUG_ON(!found_root
);
5141 * for reference counted tree, only process reference paths
5142 * rooted at the latest committed root.
5144 if (found_root
->ref_cows
&&
5145 ref_path
->root_generation
!= found_root
->root_key
.offset
)
5148 if (ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
5151 * copy data extents to new locations
5153 u64 group_start
= group
->key
.objectid
;
5154 ret
= relocate_data_extent(reloc_inode
,
5163 level
= ref_path
->owner_objectid
;
5166 if (prev_block
!= ref_path
->nodes
[level
]) {
5167 struct extent_buffer
*eb
;
5168 u64 block_start
= ref_path
->nodes
[level
];
5169 u64 block_size
= btrfs_level_size(found_root
, level
);
5171 eb
= read_tree_block(found_root
, block_start
,
5173 btrfs_tree_lock(eb
);
5174 BUG_ON(level
!= btrfs_header_level(eb
));
5177 btrfs_item_key_to_cpu(eb
, &first_key
, 0);
5179 btrfs_node_key_to_cpu(eb
, &first_key
, 0);
5181 btrfs_tree_unlock(eb
);
5182 free_extent_buffer(eb
);
5183 prev_block
= block_start
;
5186 mutex_lock(&extent_root
->fs_info
->trans_mutex
);
5187 btrfs_record_root_in_trans(found_root
);
5188 mutex_unlock(&extent_root
->fs_info
->trans_mutex
);
5189 if (ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
5191 * try to update data extent references while
5192 * keeping metadata shared between snapshots.
5195 ret
= relocate_one_path(trans
, found_root
,
5196 path
, &first_key
, ref_path
,
5197 group
, reloc_inode
);
5203 * use fallback method to process the remaining
5207 u64 group_start
= group
->key
.objectid
;
5208 new_extents
= kmalloc(sizeof(*new_extents
),
5211 ret
= get_new_locations(reloc_inode
,
5219 ret
= replace_one_extent(trans
, found_root
,
5221 &first_key
, ref_path
,
5222 new_extents
, nr_extents
);
5224 ret
= relocate_tree_block(trans
, found_root
, path
,
5225 &first_key
, ref_path
);
5232 btrfs_end_transaction(trans
, extent_root
);
5238 static u64
update_block_group_flags(struct btrfs_root
*root
, u64 flags
)
5241 u64 stripped
= BTRFS_BLOCK_GROUP_RAID0
|
5242 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
;
5244 num_devices
= root
->fs_info
->fs_devices
->rw_devices
;
5245 if (num_devices
== 1) {
5246 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
5247 stripped
= flags
& ~stripped
;
5249 /* turn raid0 into single device chunks */
5250 if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
5253 /* turn mirroring into duplication */
5254 if (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
5255 BTRFS_BLOCK_GROUP_RAID10
))
5256 return stripped
| BTRFS_BLOCK_GROUP_DUP
;
5259 /* they already had raid on here, just return */
5260 if (flags
& stripped
)
5263 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
5264 stripped
= flags
& ~stripped
;
5266 /* switch duplicated blocks with raid1 */
5267 if (flags
& BTRFS_BLOCK_GROUP_DUP
)
5268 return stripped
| BTRFS_BLOCK_GROUP_RAID1
;
5270 /* turn single device chunks into raid0 */
5271 return stripped
| BTRFS_BLOCK_GROUP_RAID0
;
5276 static int __alloc_chunk_for_shrink(struct btrfs_root
*root
,
5277 struct btrfs_block_group_cache
*shrink_block_group
,
5280 struct btrfs_trans_handle
*trans
;
5281 u64 new_alloc_flags
;
5284 spin_lock(&shrink_block_group
->lock
);
5285 if (btrfs_block_group_used(&shrink_block_group
->item
) > 0) {
5286 spin_unlock(&shrink_block_group
->lock
);
5288 trans
= btrfs_start_transaction(root
, 1);
5289 spin_lock(&shrink_block_group
->lock
);
5291 new_alloc_flags
= update_block_group_flags(root
,
5292 shrink_block_group
->flags
);
5293 if (new_alloc_flags
!= shrink_block_group
->flags
) {
5295 btrfs_block_group_used(&shrink_block_group
->item
);
5297 calc
= shrink_block_group
->key
.offset
;
5299 spin_unlock(&shrink_block_group
->lock
);
5301 do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
5302 calc
+ 2 * 1024 * 1024, new_alloc_flags
, force
);
5304 btrfs_end_transaction(trans
, root
);
5306 spin_unlock(&shrink_block_group
->lock
);
5310 static int __insert_orphan_inode(struct btrfs_trans_handle
*trans
,
5311 struct btrfs_root
*root
,
5312 u64 objectid
, u64 size
)
5314 struct btrfs_path
*path
;
5315 struct btrfs_inode_item
*item
;
5316 struct extent_buffer
*leaf
;
5319 path
= btrfs_alloc_path();
5323 ret
= btrfs_insert_empty_inode(trans
, root
, path
, objectid
);
5327 leaf
= path
->nodes
[0];
5328 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_inode_item
);
5329 memset_extent_buffer(leaf
, 0, (unsigned long)item
, sizeof(*item
));
5330 btrfs_set_inode_generation(leaf
, item
, 1);
5331 btrfs_set_inode_size(leaf
, item
, size
);
5332 btrfs_set_inode_mode(leaf
, item
, S_IFREG
| 0600);
5333 btrfs_set_inode_flags(leaf
, item
, BTRFS_INODE_NOCOMPRESS
);
5334 btrfs_mark_buffer_dirty(leaf
);
5335 btrfs_release_path(root
, path
);
5337 btrfs_free_path(path
);
5341 static noinline
struct inode
*create_reloc_inode(struct btrfs_fs_info
*fs_info
,
5342 struct btrfs_block_group_cache
*group
)
5344 struct inode
*inode
= NULL
;
5345 struct btrfs_trans_handle
*trans
;
5346 struct btrfs_root
*root
;
5347 struct btrfs_key root_key
;
5348 u64 objectid
= BTRFS_FIRST_FREE_OBJECTID
;
5351 root_key
.objectid
= BTRFS_DATA_RELOC_TREE_OBJECTID
;
5352 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
5353 root_key
.offset
= (u64
)-1;
5354 root
= btrfs_read_fs_root_no_name(fs_info
, &root_key
);
5356 return ERR_CAST(root
);
5358 trans
= btrfs_start_transaction(root
, 1);
5361 err
= btrfs_find_free_objectid(trans
, root
, objectid
, &objectid
);
5365 err
= __insert_orphan_inode(trans
, root
, objectid
, group
->key
.offset
);
5368 err
= btrfs_insert_file_extent(trans
, root
, objectid
, 0, 0, 0,
5369 group
->key
.offset
, 0, group
->key
.offset
,
5373 inode
= btrfs_iget_locked(root
->fs_info
->sb
, objectid
, root
);
5374 if (inode
->i_state
& I_NEW
) {
5375 BTRFS_I(inode
)->root
= root
;
5376 BTRFS_I(inode
)->location
.objectid
= objectid
;
5377 BTRFS_I(inode
)->location
.type
= BTRFS_INODE_ITEM_KEY
;
5378 BTRFS_I(inode
)->location
.offset
= 0;
5379 btrfs_read_locked_inode(inode
);
5380 unlock_new_inode(inode
);
5381 BUG_ON(is_bad_inode(inode
));
5385 BTRFS_I(inode
)->index_cnt
= group
->key
.objectid
;
5387 err
= btrfs_orphan_add(trans
, inode
);
5389 btrfs_end_transaction(trans
, root
);
5393 inode
= ERR_PTR(err
);
5398 int btrfs_reloc_clone_csums(struct inode
*inode
, u64 file_pos
, u64 len
)
5401 struct btrfs_ordered_sum
*sums
;
5402 struct btrfs_sector_sum
*sector_sum
;
5403 struct btrfs_ordered_extent
*ordered
;
5404 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5405 struct list_head list
;
5410 INIT_LIST_HEAD(&list
);
5412 ordered
= btrfs_lookup_ordered_extent(inode
, file_pos
);
5413 BUG_ON(ordered
->file_offset
!= file_pos
|| ordered
->len
!= len
);
5415 disk_bytenr
= file_pos
+ BTRFS_I(inode
)->index_cnt
;
5416 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, disk_bytenr
,
5417 disk_bytenr
+ len
- 1, &list
);
5419 while (!list_empty(&list
)) {
5420 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
5421 list_del_init(&sums
->list
);
5423 sector_sum
= sums
->sums
;
5424 sums
->bytenr
= ordered
->start
;
5427 while (offset
< sums
->len
) {
5428 sector_sum
->bytenr
+= ordered
->start
- disk_bytenr
;
5430 offset
+= root
->sectorsize
;
5433 btrfs_add_ordered_sum(inode
, ordered
, sums
);
5435 btrfs_put_ordered_extent(ordered
);
5439 int btrfs_relocate_block_group(struct btrfs_root
*root
, u64 group_start
)
5441 struct btrfs_trans_handle
*trans
;
5442 struct btrfs_path
*path
;
5443 struct btrfs_fs_info
*info
= root
->fs_info
;
5444 struct extent_buffer
*leaf
;
5445 struct inode
*reloc_inode
;
5446 struct btrfs_block_group_cache
*block_group
;
5447 struct btrfs_key key
;
5456 root
= root
->fs_info
->extent_root
;
5458 block_group
= btrfs_lookup_block_group(info
, group_start
);
5459 BUG_ON(!block_group
);
5461 printk(KERN_INFO
"btrfs relocating block group %llu flags %llu\n",
5462 (unsigned long long)block_group
->key
.objectid
,
5463 (unsigned long long)block_group
->flags
);
5465 path
= btrfs_alloc_path();
5468 reloc_inode
= create_reloc_inode(info
, block_group
);
5469 BUG_ON(IS_ERR(reloc_inode
));
5471 __alloc_chunk_for_shrink(root
, block_group
, 1);
5472 set_block_group_readonly(block_group
);
5474 btrfs_start_delalloc_inodes(info
->tree_root
);
5475 btrfs_wait_ordered_extents(info
->tree_root
, 0);
5480 key
.objectid
= block_group
->key
.objectid
;
5483 cur_byte
= key
.objectid
;
5485 trans
= btrfs_start_transaction(info
->tree_root
, 1);
5486 btrfs_commit_transaction(trans
, info
->tree_root
);
5488 mutex_lock(&root
->fs_info
->cleaner_mutex
);
5489 btrfs_clean_old_snapshots(info
->tree_root
);
5490 btrfs_remove_leaf_refs(info
->tree_root
, (u64
)-1, 1);
5491 mutex_unlock(&root
->fs_info
->cleaner_mutex
);
5493 trans
= btrfs_start_transaction(info
->tree_root
, 1);
5494 btrfs_commit_transaction(trans
, info
->tree_root
);
5497 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5501 leaf
= path
->nodes
[0];
5502 nritems
= btrfs_header_nritems(leaf
);
5503 if (path
->slots
[0] >= nritems
) {
5504 ret
= btrfs_next_leaf(root
, path
);
5511 leaf
= path
->nodes
[0];
5512 nritems
= btrfs_header_nritems(leaf
);
5515 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
5517 if (key
.objectid
>= block_group
->key
.objectid
+
5518 block_group
->key
.offset
)
5521 if (progress
&& need_resched()) {
5522 btrfs_release_path(root
, path
);
5529 if (btrfs_key_type(&key
) != BTRFS_EXTENT_ITEM_KEY
||
5530 key
.objectid
+ key
.offset
<= cur_byte
) {
5536 cur_byte
= key
.objectid
+ key
.offset
;
5537 btrfs_release_path(root
, path
);
5539 __alloc_chunk_for_shrink(root
, block_group
, 0);
5540 ret
= relocate_one_extent(root
, path
, &key
, block_group
,
5546 key
.objectid
= cur_byte
;
5551 btrfs_release_path(root
, path
);
5554 btrfs_wait_ordered_range(reloc_inode
, 0, (u64
)-1);
5555 invalidate_mapping_pages(reloc_inode
->i_mapping
, 0, -1);
5558 if (total_found
> 0) {
5559 printk(KERN_INFO
"btrfs found %llu extents in pass %d\n",
5560 (unsigned long long)total_found
, pass
);
5562 if (total_found
== skipped
&& pass
> 2) {
5564 reloc_inode
= create_reloc_inode(info
, block_group
);
5570 /* delete reloc_inode */
5573 /* unpin extents in this range */
5574 trans
= btrfs_start_transaction(info
->tree_root
, 1);
5575 btrfs_commit_transaction(trans
, info
->tree_root
);
5577 spin_lock(&block_group
->lock
);
5578 WARN_ON(block_group
->pinned
> 0);
5579 WARN_ON(block_group
->reserved
> 0);
5580 WARN_ON(btrfs_block_group_used(&block_group
->item
) > 0);
5581 spin_unlock(&block_group
->lock
);
5582 put_block_group(block_group
);
5585 btrfs_free_path(path
);
5589 static int find_first_block_group(struct btrfs_root
*root
,
5590 struct btrfs_path
*path
, struct btrfs_key
*key
)
5593 struct btrfs_key found_key
;
5594 struct extent_buffer
*leaf
;
5597 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
5602 slot
= path
->slots
[0];
5603 leaf
= path
->nodes
[0];
5604 if (slot
>= btrfs_header_nritems(leaf
)) {
5605 ret
= btrfs_next_leaf(root
, path
);
5612 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5614 if (found_key
.objectid
>= key
->objectid
&&
5615 found_key
.type
== BTRFS_BLOCK_GROUP_ITEM_KEY
) {
5626 int btrfs_free_block_groups(struct btrfs_fs_info
*info
)
5628 struct btrfs_block_group_cache
*block_group
;
5629 struct btrfs_space_info
*space_info
;
5632 spin_lock(&info
->block_group_cache_lock
);
5633 while ((n
= rb_last(&info
->block_group_cache_tree
)) != NULL
) {
5634 block_group
= rb_entry(n
, struct btrfs_block_group_cache
,
5636 rb_erase(&block_group
->cache_node
,
5637 &info
->block_group_cache_tree
);
5638 spin_unlock(&info
->block_group_cache_lock
);
5640 btrfs_remove_free_space_cache(block_group
);
5641 down_write(&block_group
->space_info
->groups_sem
);
5642 list_del(&block_group
->list
);
5643 up_write(&block_group
->space_info
->groups_sem
);
5645 WARN_ON(atomic_read(&block_group
->count
) != 1);
5648 spin_lock(&info
->block_group_cache_lock
);
5650 spin_unlock(&info
->block_group_cache_lock
);
5652 /* now that all the block groups are freed, go through and
5653 * free all the space_info structs. This is only called during
5654 * the final stages of unmount, and so we know nobody is
5655 * using them. We call synchronize_rcu() once before we start,
5656 * just to be on the safe side.
5660 while(!list_empty(&info
->space_info
)) {
5661 space_info
= list_entry(info
->space_info
.next
,
5662 struct btrfs_space_info
,
5665 list_del(&space_info
->list
);
5671 int btrfs_read_block_groups(struct btrfs_root
*root
)
5673 struct btrfs_path
*path
;
5675 struct btrfs_block_group_cache
*cache
;
5676 struct btrfs_fs_info
*info
= root
->fs_info
;
5677 struct btrfs_space_info
*space_info
;
5678 struct btrfs_key key
;
5679 struct btrfs_key found_key
;
5680 struct extent_buffer
*leaf
;
5682 root
= info
->extent_root
;
5685 btrfs_set_key_type(&key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
5686 path
= btrfs_alloc_path();
5691 ret
= find_first_block_group(root
, path
, &key
);
5699 leaf
= path
->nodes
[0];
5700 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5701 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
5707 atomic_set(&cache
->count
, 1);
5708 spin_lock_init(&cache
->lock
);
5709 mutex_init(&cache
->alloc_mutex
);
5710 mutex_init(&cache
->cache_mutex
);
5711 INIT_LIST_HEAD(&cache
->list
);
5712 read_extent_buffer(leaf
, &cache
->item
,
5713 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
5714 sizeof(cache
->item
));
5715 memcpy(&cache
->key
, &found_key
, sizeof(found_key
));
5717 key
.objectid
= found_key
.objectid
+ found_key
.offset
;
5718 btrfs_release_path(root
, path
);
5719 cache
->flags
= btrfs_block_group_flags(&cache
->item
);
5721 ret
= update_space_info(info
, cache
->flags
, found_key
.offset
,
5722 btrfs_block_group_used(&cache
->item
),
5725 cache
->space_info
= space_info
;
5726 down_write(&space_info
->groups_sem
);
5727 list_add_tail(&cache
->list
, &space_info
->block_groups
);
5728 up_write(&space_info
->groups_sem
);
5730 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
5733 set_avail_alloc_bits(root
->fs_info
, cache
->flags
);
5734 if (btrfs_chunk_readonly(root
, cache
->key
.objectid
))
5735 set_block_group_readonly(cache
);
5739 btrfs_free_path(path
);
5743 int btrfs_make_block_group(struct btrfs_trans_handle
*trans
,
5744 struct btrfs_root
*root
, u64 bytes_used
,
5745 u64 type
, u64 chunk_objectid
, u64 chunk_offset
,
5749 struct btrfs_root
*extent_root
;
5750 struct btrfs_block_group_cache
*cache
;
5752 extent_root
= root
->fs_info
->extent_root
;
5754 root
->fs_info
->last_trans_new_blockgroup
= trans
->transid
;
5756 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
5760 cache
->key
.objectid
= chunk_offset
;
5761 cache
->key
.offset
= size
;
5762 cache
->key
.type
= BTRFS_BLOCK_GROUP_ITEM_KEY
;
5763 atomic_set(&cache
->count
, 1);
5764 spin_lock_init(&cache
->lock
);
5765 mutex_init(&cache
->alloc_mutex
);
5766 mutex_init(&cache
->cache_mutex
);
5767 INIT_LIST_HEAD(&cache
->list
);
5769 btrfs_set_block_group_used(&cache
->item
, bytes_used
);
5770 btrfs_set_block_group_chunk_objectid(&cache
->item
, chunk_objectid
);
5771 cache
->flags
= type
;
5772 btrfs_set_block_group_flags(&cache
->item
, type
);
5774 ret
= update_space_info(root
->fs_info
, cache
->flags
, size
, bytes_used
,
5775 &cache
->space_info
);
5777 down_write(&cache
->space_info
->groups_sem
);
5778 list_add_tail(&cache
->list
, &cache
->space_info
->block_groups
);
5779 up_write(&cache
->space_info
->groups_sem
);
5781 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
5784 ret
= btrfs_insert_item(trans
, extent_root
, &cache
->key
, &cache
->item
,
5785 sizeof(cache
->item
));
5788 set_avail_alloc_bits(extent_root
->fs_info
, type
);
5793 int btrfs_remove_block_group(struct btrfs_trans_handle
*trans
,
5794 struct btrfs_root
*root
, u64 group_start
)
5796 struct btrfs_path
*path
;
5797 struct btrfs_block_group_cache
*block_group
;
5798 struct btrfs_key key
;
5801 root
= root
->fs_info
->extent_root
;
5803 block_group
= btrfs_lookup_block_group(root
->fs_info
, group_start
);
5804 BUG_ON(!block_group
);
5805 BUG_ON(!block_group
->ro
);
5807 memcpy(&key
, &block_group
->key
, sizeof(key
));
5809 path
= btrfs_alloc_path();
5812 spin_lock(&root
->fs_info
->block_group_cache_lock
);
5813 rb_erase(&block_group
->cache_node
,
5814 &root
->fs_info
->block_group_cache_tree
);
5815 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
5816 btrfs_remove_free_space_cache(block_group
);
5817 down_write(&block_group
->space_info
->groups_sem
);
5818 list_del(&block_group
->list
);
5819 up_write(&block_group
->space_info
->groups_sem
);
5821 spin_lock(&block_group
->space_info
->lock
);
5822 block_group
->space_info
->total_bytes
-= block_group
->key
.offset
;
5823 block_group
->space_info
->bytes_readonly
-= block_group
->key
.offset
;
5824 spin_unlock(&block_group
->space_info
->lock
);
5825 block_group
->space_info
->full
= 0;
5827 put_block_group(block_group
);
5828 put_block_group(block_group
);
5830 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
5836 ret
= btrfs_del_item(trans
, root
, path
);
5838 btrfs_free_path(path
);