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)
1025 spin_lock(&delayed_refs
->lock
);
1028 spin_lock(&delayed_refs
->lock
);
1029 node
= rb_first(&delayed_refs
->root
);
1031 spin_unlock(&delayed_refs
->lock
);
1036 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
,
1038 if (btrfs_delayed_ref_is_head(ref
)) {
1039 struct btrfs_delayed_ref_head
*head
;
1041 head
= btrfs_delayed_node_to_head(ref
);
1042 atomic_inc(&ref
->refs
);
1044 spin_unlock(&delayed_refs
->lock
);
1045 mutex_lock(&head
->mutex
);
1046 mutex_unlock(&head
->mutex
);
1048 btrfs_put_delayed_ref(ref
);
1052 node
= rb_next(node
);
1054 spin_unlock(&delayed_refs
->lock
);
1055 count
= (unsigned long)-1;
1056 schedule_timeout(1);
1063 int btrfs_cross_ref_exist(struct btrfs_trans_handle
*trans
,
1064 struct btrfs_root
*root
, u64 objectid
, u64 bytenr
)
1066 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
1067 struct btrfs_path
*path
;
1068 struct extent_buffer
*leaf
;
1069 struct btrfs_extent_ref
*ref_item
;
1070 struct btrfs_key key
;
1071 struct btrfs_key found_key
;
1077 key
.objectid
= bytenr
;
1078 key
.offset
= (u64
)-1;
1079 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1081 path
= btrfs_alloc_path();
1082 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
1088 if (path
->slots
[0] == 0)
1092 leaf
= path
->nodes
[0];
1093 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1095 if (found_key
.objectid
!= bytenr
||
1096 found_key
.type
!= BTRFS_EXTENT_ITEM_KEY
)
1099 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
1101 leaf
= path
->nodes
[0];
1102 nritems
= btrfs_header_nritems(leaf
);
1103 if (path
->slots
[0] >= nritems
) {
1104 ret
= btrfs_next_leaf(extent_root
, path
);
1111 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1112 if (found_key
.objectid
!= bytenr
)
1115 if (found_key
.type
!= BTRFS_EXTENT_REF_KEY
) {
1120 ref_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1121 struct btrfs_extent_ref
);
1122 ref_root
= btrfs_ref_root(leaf
, ref_item
);
1123 if ((ref_root
!= root
->root_key
.objectid
&&
1124 ref_root
!= BTRFS_TREE_LOG_OBJECTID
) ||
1125 objectid
!= btrfs_ref_objectid(leaf
, ref_item
)) {
1129 if (btrfs_ref_generation(leaf
, ref_item
) <= last_snapshot
) {
1138 btrfs_free_path(path
);
1142 int btrfs_cache_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
1143 struct extent_buffer
*buf
, u32 nr_extents
)
1145 struct btrfs_key key
;
1146 struct btrfs_file_extent_item
*fi
;
1154 if (!root
->ref_cows
)
1157 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
1159 root_gen
= root
->root_key
.offset
;
1162 root_gen
= trans
->transid
- 1;
1165 level
= btrfs_header_level(buf
);
1166 nritems
= btrfs_header_nritems(buf
);
1169 struct btrfs_leaf_ref
*ref
;
1170 struct btrfs_extent_info
*info
;
1172 ref
= btrfs_alloc_leaf_ref(root
, nr_extents
);
1178 ref
->root_gen
= root_gen
;
1179 ref
->bytenr
= buf
->start
;
1180 ref
->owner
= btrfs_header_owner(buf
);
1181 ref
->generation
= btrfs_header_generation(buf
);
1182 ref
->nritems
= nr_extents
;
1183 info
= ref
->extents
;
1185 for (i
= 0; nr_extents
> 0 && i
< nritems
; i
++) {
1187 btrfs_item_key_to_cpu(buf
, &key
, i
);
1188 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
1190 fi
= btrfs_item_ptr(buf
, i
,
1191 struct btrfs_file_extent_item
);
1192 if (btrfs_file_extent_type(buf
, fi
) ==
1193 BTRFS_FILE_EXTENT_INLINE
)
1195 disk_bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
1196 if (disk_bytenr
== 0)
1199 info
->bytenr
= disk_bytenr
;
1201 btrfs_file_extent_disk_num_bytes(buf
, fi
);
1202 info
->objectid
= key
.objectid
;
1203 info
->offset
= key
.offset
;
1207 ret
= btrfs_add_leaf_ref(root
, ref
, shared
);
1208 if (ret
== -EEXIST
&& shared
) {
1209 struct btrfs_leaf_ref
*old
;
1210 old
= btrfs_lookup_leaf_ref(root
, ref
->bytenr
);
1212 btrfs_remove_leaf_ref(root
, old
);
1213 btrfs_free_leaf_ref(root
, old
);
1214 ret
= btrfs_add_leaf_ref(root
, ref
, shared
);
1217 btrfs_free_leaf_ref(root
, ref
);
1223 /* when a block goes through cow, we update the reference counts of
1224 * everything that block points to. The internal pointers of the block
1225 * can be in just about any order, and it is likely to have clusters of
1226 * things that are close together and clusters of things that are not.
1228 * To help reduce the seeks that come with updating all of these reference
1229 * counts, sort them by byte number before actual updates are done.
1231 * struct refsort is used to match byte number to slot in the btree block.
1232 * we sort based on the byte number and then use the slot to actually
1235 * struct refsort is smaller than strcut btrfs_item and smaller than
1236 * struct btrfs_key_ptr. Since we're currently limited to the page size
1237 * for a btree block, there's no way for a kmalloc of refsorts for a
1238 * single node to be bigger than a page.
1246 * for passing into sort()
1248 static int refsort_cmp(const void *a_void
, const void *b_void
)
1250 const struct refsort
*a
= a_void
;
1251 const struct refsort
*b
= b_void
;
1253 if (a
->bytenr
< b
->bytenr
)
1255 if (a
->bytenr
> b
->bytenr
)
1261 noinline
int btrfs_inc_ref(struct btrfs_trans_handle
*trans
,
1262 struct btrfs_root
*root
,
1263 struct extent_buffer
*orig_buf
,
1264 struct extent_buffer
*buf
, u32
*nr_extents
)
1270 u64 orig_generation
;
1271 struct refsort
*sorted
;
1273 u32 nr_file_extents
= 0;
1274 struct btrfs_key key
;
1275 struct btrfs_file_extent_item
*fi
;
1282 int (*process_func
)(struct btrfs_trans_handle
*, struct btrfs_root
*,
1283 u64
, u64
, u64
, u64
, u64
, u64
, u64
, u64
, u64
);
1285 ref_root
= btrfs_header_owner(buf
);
1286 ref_generation
= btrfs_header_generation(buf
);
1287 orig_root
= btrfs_header_owner(orig_buf
);
1288 orig_generation
= btrfs_header_generation(orig_buf
);
1290 nritems
= btrfs_header_nritems(buf
);
1291 level
= btrfs_header_level(buf
);
1293 sorted
= kmalloc(sizeof(struct refsort
) * nritems
, GFP_NOFS
);
1296 if (root
->ref_cows
) {
1297 process_func
= __btrfs_inc_extent_ref
;
1300 root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
)
1303 root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
)
1305 process_func
= __btrfs_update_extent_ref
;
1309 * we make two passes through the items. In the first pass we
1310 * only record the byte number and slot. Then we sort based on
1311 * byte number and do the actual work based on the sorted results
1313 for (i
= 0; i
< nritems
; i
++) {
1316 btrfs_item_key_to_cpu(buf
, &key
, i
);
1317 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
1319 fi
= btrfs_item_ptr(buf
, i
,
1320 struct btrfs_file_extent_item
);
1321 if (btrfs_file_extent_type(buf
, fi
) ==
1322 BTRFS_FILE_EXTENT_INLINE
)
1324 bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
1329 sorted
[refi
].bytenr
= bytenr
;
1330 sorted
[refi
].slot
= i
;
1333 bytenr
= btrfs_node_blockptr(buf
, i
);
1334 sorted
[refi
].bytenr
= bytenr
;
1335 sorted
[refi
].slot
= i
;
1340 * if refi == 0, we didn't actually put anything into the sorted
1341 * array and we're done
1346 sort(sorted
, refi
, sizeof(struct refsort
), refsort_cmp
, NULL
);
1348 for (i
= 0; i
< refi
; i
++) {
1350 slot
= sorted
[i
].slot
;
1351 bytenr
= sorted
[i
].bytenr
;
1354 btrfs_item_key_to_cpu(buf
, &key
, slot
);
1355 fi
= btrfs_item_ptr(buf
, slot
,
1356 struct btrfs_file_extent_item
);
1358 bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
1362 ret
= process_func(trans
, root
, bytenr
,
1363 btrfs_file_extent_disk_num_bytes(buf
, fi
),
1364 orig_buf
->start
, buf
->start
,
1365 orig_root
, ref_root
,
1366 orig_generation
, ref_generation
,
1375 ret
= process_func(trans
, root
, bytenr
, buf
->len
,
1376 orig_buf
->start
, buf
->start
,
1377 orig_root
, ref_root
,
1378 orig_generation
, ref_generation
,
1391 *nr_extents
= nr_file_extents
;
1393 *nr_extents
= nritems
;
1402 int btrfs_update_ref(struct btrfs_trans_handle
*trans
,
1403 struct btrfs_root
*root
, struct extent_buffer
*orig_buf
,
1404 struct extent_buffer
*buf
, int start_slot
, int nr
)
1411 u64 orig_generation
;
1412 struct btrfs_key key
;
1413 struct btrfs_file_extent_item
*fi
;
1419 BUG_ON(start_slot
< 0);
1420 BUG_ON(start_slot
+ nr
> btrfs_header_nritems(buf
));
1422 ref_root
= btrfs_header_owner(buf
);
1423 ref_generation
= btrfs_header_generation(buf
);
1424 orig_root
= btrfs_header_owner(orig_buf
);
1425 orig_generation
= btrfs_header_generation(orig_buf
);
1426 level
= btrfs_header_level(buf
);
1428 if (!root
->ref_cows
) {
1430 root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
)
1433 root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
)
1437 for (i
= 0, slot
= start_slot
; i
< nr
; i
++, slot
++) {
1440 btrfs_item_key_to_cpu(buf
, &key
, slot
);
1441 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
1443 fi
= btrfs_item_ptr(buf
, slot
,
1444 struct btrfs_file_extent_item
);
1445 if (btrfs_file_extent_type(buf
, fi
) ==
1446 BTRFS_FILE_EXTENT_INLINE
)
1448 bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
1451 ret
= __btrfs_update_extent_ref(trans
, root
, bytenr
,
1452 btrfs_file_extent_disk_num_bytes(buf
, fi
),
1453 orig_buf
->start
, buf
->start
,
1454 orig_root
, ref_root
, orig_generation
,
1455 ref_generation
, key
.objectid
);
1459 bytenr
= btrfs_node_blockptr(buf
, slot
);
1460 ret
= __btrfs_update_extent_ref(trans
, root
, bytenr
,
1461 buf
->len
, orig_buf
->start
,
1462 buf
->start
, orig_root
, ref_root
,
1463 orig_generation
, ref_generation
,
1475 static int write_one_cache_group(struct btrfs_trans_handle
*trans
,
1476 struct btrfs_root
*root
,
1477 struct btrfs_path
*path
,
1478 struct btrfs_block_group_cache
*cache
)
1481 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
1483 struct extent_buffer
*leaf
;
1485 ret
= btrfs_search_slot(trans
, extent_root
, &cache
->key
, path
, 0, 1);
1490 leaf
= path
->nodes
[0];
1491 bi
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
1492 write_extent_buffer(leaf
, &cache
->item
, bi
, sizeof(cache
->item
));
1493 btrfs_mark_buffer_dirty(leaf
);
1494 btrfs_release_path(extent_root
, path
);
1502 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle
*trans
,
1503 struct btrfs_root
*root
)
1505 struct btrfs_block_group_cache
*cache
, *entry
;
1509 struct btrfs_path
*path
;
1512 path
= btrfs_alloc_path();
1518 spin_lock(&root
->fs_info
->block_group_cache_lock
);
1519 for (n
= rb_first(&root
->fs_info
->block_group_cache_tree
);
1520 n
; n
= rb_next(n
)) {
1521 entry
= rb_entry(n
, struct btrfs_block_group_cache
,
1528 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
1534 last
+= cache
->key
.offset
;
1536 err
= write_one_cache_group(trans
, root
,
1539 * if we fail to write the cache group, we want
1540 * to keep it marked dirty in hopes that a later
1548 btrfs_free_path(path
);
1552 int btrfs_extent_readonly(struct btrfs_root
*root
, u64 bytenr
)
1554 struct btrfs_block_group_cache
*block_group
;
1557 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
1558 if (!block_group
|| block_group
->ro
)
1561 put_block_group(block_group
);
1565 static int update_space_info(struct btrfs_fs_info
*info
, u64 flags
,
1566 u64 total_bytes
, u64 bytes_used
,
1567 struct btrfs_space_info
**space_info
)
1569 struct btrfs_space_info
*found
;
1571 found
= __find_space_info(info
, flags
);
1573 spin_lock(&found
->lock
);
1574 found
->total_bytes
+= total_bytes
;
1575 found
->bytes_used
+= bytes_used
;
1577 spin_unlock(&found
->lock
);
1578 *space_info
= found
;
1581 found
= kzalloc(sizeof(*found
), GFP_NOFS
);
1585 INIT_LIST_HEAD(&found
->block_groups
);
1586 init_rwsem(&found
->groups_sem
);
1587 spin_lock_init(&found
->lock
);
1588 found
->flags
= flags
;
1589 found
->total_bytes
= total_bytes
;
1590 found
->bytes_used
= bytes_used
;
1591 found
->bytes_pinned
= 0;
1592 found
->bytes_reserved
= 0;
1593 found
->bytes_readonly
= 0;
1594 found
->bytes_delalloc
= 0;
1596 found
->force_alloc
= 0;
1597 *space_info
= found
;
1598 list_add_rcu(&found
->list
, &info
->space_info
);
1602 static void set_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
1604 u64 extra_flags
= flags
& (BTRFS_BLOCK_GROUP_RAID0
|
1605 BTRFS_BLOCK_GROUP_RAID1
|
1606 BTRFS_BLOCK_GROUP_RAID10
|
1607 BTRFS_BLOCK_GROUP_DUP
);
1609 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
1610 fs_info
->avail_data_alloc_bits
|= extra_flags
;
1611 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
1612 fs_info
->avail_metadata_alloc_bits
|= extra_flags
;
1613 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
1614 fs_info
->avail_system_alloc_bits
|= extra_flags
;
1618 static void set_block_group_readonly(struct btrfs_block_group_cache
*cache
)
1620 spin_lock(&cache
->space_info
->lock
);
1621 spin_lock(&cache
->lock
);
1623 cache
->space_info
->bytes_readonly
+= cache
->key
.offset
-
1624 btrfs_block_group_used(&cache
->item
);
1627 spin_unlock(&cache
->lock
);
1628 spin_unlock(&cache
->space_info
->lock
);
1631 u64
btrfs_reduce_alloc_profile(struct btrfs_root
*root
, u64 flags
)
1633 u64 num_devices
= root
->fs_info
->fs_devices
->rw_devices
;
1635 if (num_devices
== 1)
1636 flags
&= ~(BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID0
);
1637 if (num_devices
< 4)
1638 flags
&= ~BTRFS_BLOCK_GROUP_RAID10
;
1640 if ((flags
& BTRFS_BLOCK_GROUP_DUP
) &&
1641 (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
1642 BTRFS_BLOCK_GROUP_RAID10
))) {
1643 flags
&= ~BTRFS_BLOCK_GROUP_DUP
;
1646 if ((flags
& BTRFS_BLOCK_GROUP_RAID1
) &&
1647 (flags
& BTRFS_BLOCK_GROUP_RAID10
)) {
1648 flags
&= ~BTRFS_BLOCK_GROUP_RAID1
;
1651 if ((flags
& BTRFS_BLOCK_GROUP_RAID0
) &&
1652 ((flags
& BTRFS_BLOCK_GROUP_RAID1
) |
1653 (flags
& BTRFS_BLOCK_GROUP_RAID10
) |
1654 (flags
& BTRFS_BLOCK_GROUP_DUP
)))
1655 flags
&= ~BTRFS_BLOCK_GROUP_RAID0
;
1659 static u64
btrfs_get_alloc_profile(struct btrfs_root
*root
, u64 data
)
1661 struct btrfs_fs_info
*info
= root
->fs_info
;
1665 alloc_profile
= info
->avail_data_alloc_bits
&
1666 info
->data_alloc_profile
;
1667 data
= BTRFS_BLOCK_GROUP_DATA
| alloc_profile
;
1668 } else if (root
== root
->fs_info
->chunk_root
) {
1669 alloc_profile
= info
->avail_system_alloc_bits
&
1670 info
->system_alloc_profile
;
1671 data
= BTRFS_BLOCK_GROUP_SYSTEM
| alloc_profile
;
1673 alloc_profile
= info
->avail_metadata_alloc_bits
&
1674 info
->metadata_alloc_profile
;
1675 data
= BTRFS_BLOCK_GROUP_METADATA
| alloc_profile
;
1678 return btrfs_reduce_alloc_profile(root
, data
);
1681 void btrfs_set_inode_space_info(struct btrfs_root
*root
, struct inode
*inode
)
1685 alloc_target
= btrfs_get_alloc_profile(root
, 1);
1686 BTRFS_I(inode
)->space_info
= __find_space_info(root
->fs_info
,
1691 * for now this just makes sure we have at least 5% of our metadata space free
1694 int btrfs_check_metadata_free_space(struct btrfs_root
*root
)
1696 struct btrfs_fs_info
*info
= root
->fs_info
;
1697 struct btrfs_space_info
*meta_sinfo
;
1698 u64 alloc_target
, thresh
;
1699 int committed
= 0, ret
;
1701 /* get the space info for where the metadata will live */
1702 alloc_target
= btrfs_get_alloc_profile(root
, 0);
1703 meta_sinfo
= __find_space_info(info
, alloc_target
);
1706 spin_lock(&meta_sinfo
->lock
);
1707 if (!meta_sinfo
->full
)
1708 thresh
= meta_sinfo
->total_bytes
* 80;
1710 thresh
= meta_sinfo
->total_bytes
* 95;
1712 do_div(thresh
, 100);
1714 if (meta_sinfo
->bytes_used
+ meta_sinfo
->bytes_reserved
+
1715 meta_sinfo
->bytes_pinned
+ meta_sinfo
->bytes_readonly
> thresh
) {
1716 struct btrfs_trans_handle
*trans
;
1717 if (!meta_sinfo
->full
) {
1718 meta_sinfo
->force_alloc
= 1;
1719 spin_unlock(&meta_sinfo
->lock
);
1721 trans
= btrfs_start_transaction(root
, 1);
1725 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
1726 2 * 1024 * 1024, alloc_target
, 0);
1727 btrfs_end_transaction(trans
, root
);
1730 spin_unlock(&meta_sinfo
->lock
);
1734 trans
= btrfs_join_transaction(root
, 1);
1737 ret
= btrfs_commit_transaction(trans
, root
);
1744 spin_unlock(&meta_sinfo
->lock
);
1750 * This will check the space that the inode allocates from to make sure we have
1751 * enough space for bytes.
1753 int btrfs_check_data_free_space(struct btrfs_root
*root
, struct inode
*inode
,
1756 struct btrfs_space_info
*data_sinfo
;
1757 int ret
= 0, committed
= 0;
1759 /* make sure bytes are sectorsize aligned */
1760 bytes
= (bytes
+ root
->sectorsize
- 1) & ~((u64
)root
->sectorsize
- 1);
1762 data_sinfo
= BTRFS_I(inode
)->space_info
;
1764 /* make sure we have enough space to handle the data first */
1765 spin_lock(&data_sinfo
->lock
);
1766 if (data_sinfo
->total_bytes
- data_sinfo
->bytes_used
-
1767 data_sinfo
->bytes_delalloc
- data_sinfo
->bytes_reserved
-
1768 data_sinfo
->bytes_pinned
- data_sinfo
->bytes_readonly
-
1769 data_sinfo
->bytes_may_use
< bytes
) {
1770 struct btrfs_trans_handle
*trans
;
1773 * if we don't have enough free bytes in this space then we need
1774 * to alloc a new chunk.
1776 if (!data_sinfo
->full
) {
1779 data_sinfo
->force_alloc
= 1;
1780 spin_unlock(&data_sinfo
->lock
);
1782 alloc_target
= btrfs_get_alloc_profile(root
, 1);
1783 trans
= btrfs_start_transaction(root
, 1);
1787 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
1788 bytes
+ 2 * 1024 * 1024,
1790 btrfs_end_transaction(trans
, root
);
1795 spin_unlock(&data_sinfo
->lock
);
1797 /* commit the current transaction and try again */
1800 trans
= btrfs_join_transaction(root
, 1);
1803 ret
= btrfs_commit_transaction(trans
, root
);
1809 printk(KERN_ERR
"no space left, need %llu, %llu delalloc bytes"
1810 ", %llu bytes_used, %llu bytes_reserved, "
1811 "%llu bytes_pinned, %llu bytes_readonly, %llu may use"
1812 "%llu total\n", bytes
, data_sinfo
->bytes_delalloc
,
1813 data_sinfo
->bytes_used
, data_sinfo
->bytes_reserved
,
1814 data_sinfo
->bytes_pinned
, data_sinfo
->bytes_readonly
,
1815 data_sinfo
->bytes_may_use
, data_sinfo
->total_bytes
);
1818 data_sinfo
->bytes_may_use
+= bytes
;
1819 BTRFS_I(inode
)->reserved_bytes
+= bytes
;
1820 spin_unlock(&data_sinfo
->lock
);
1822 return btrfs_check_metadata_free_space(root
);
1826 * if there was an error for whatever reason after calling
1827 * btrfs_check_data_free_space, call this so we can cleanup the counters.
1829 void btrfs_free_reserved_data_space(struct btrfs_root
*root
,
1830 struct inode
*inode
, u64 bytes
)
1832 struct btrfs_space_info
*data_sinfo
;
1834 /* make sure bytes are sectorsize aligned */
1835 bytes
= (bytes
+ root
->sectorsize
- 1) & ~((u64
)root
->sectorsize
- 1);
1837 data_sinfo
= BTRFS_I(inode
)->space_info
;
1838 spin_lock(&data_sinfo
->lock
);
1839 data_sinfo
->bytes_may_use
-= bytes
;
1840 BTRFS_I(inode
)->reserved_bytes
-= bytes
;
1841 spin_unlock(&data_sinfo
->lock
);
1844 /* called when we are adding a delalloc extent to the inode's io_tree */
1845 void btrfs_delalloc_reserve_space(struct btrfs_root
*root
, struct inode
*inode
,
1848 struct btrfs_space_info
*data_sinfo
;
1850 /* get the space info for where this inode will be storing its data */
1851 data_sinfo
= BTRFS_I(inode
)->space_info
;
1853 /* make sure we have enough space to handle the data first */
1854 spin_lock(&data_sinfo
->lock
);
1855 data_sinfo
->bytes_delalloc
+= bytes
;
1858 * we are adding a delalloc extent without calling
1859 * btrfs_check_data_free_space first. This happens on a weird
1860 * writepage condition, but shouldn't hurt our accounting
1862 if (unlikely(bytes
> BTRFS_I(inode
)->reserved_bytes
)) {
1863 data_sinfo
->bytes_may_use
-= BTRFS_I(inode
)->reserved_bytes
;
1864 BTRFS_I(inode
)->reserved_bytes
= 0;
1866 data_sinfo
->bytes_may_use
-= bytes
;
1867 BTRFS_I(inode
)->reserved_bytes
-= bytes
;
1870 spin_unlock(&data_sinfo
->lock
);
1873 /* called when we are clearing an delalloc extent from the inode's io_tree */
1874 void btrfs_delalloc_free_space(struct btrfs_root
*root
, struct inode
*inode
,
1877 struct btrfs_space_info
*info
;
1879 info
= BTRFS_I(inode
)->space_info
;
1881 spin_lock(&info
->lock
);
1882 info
->bytes_delalloc
-= bytes
;
1883 spin_unlock(&info
->lock
);
1886 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
1887 struct btrfs_root
*extent_root
, u64 alloc_bytes
,
1888 u64 flags
, int force
)
1890 struct btrfs_space_info
*space_info
;
1894 mutex_lock(&extent_root
->fs_info
->chunk_mutex
);
1896 flags
= btrfs_reduce_alloc_profile(extent_root
, flags
);
1898 space_info
= __find_space_info(extent_root
->fs_info
, flags
);
1900 ret
= update_space_info(extent_root
->fs_info
, flags
,
1904 BUG_ON(!space_info
);
1906 spin_lock(&space_info
->lock
);
1907 if (space_info
->force_alloc
) {
1909 space_info
->force_alloc
= 0;
1911 if (space_info
->full
) {
1912 spin_unlock(&space_info
->lock
);
1916 thresh
= space_info
->total_bytes
- space_info
->bytes_readonly
;
1917 thresh
= div_factor(thresh
, 6);
1919 (space_info
->bytes_used
+ space_info
->bytes_pinned
+
1920 space_info
->bytes_reserved
+ alloc_bytes
) < thresh
) {
1921 spin_unlock(&space_info
->lock
);
1924 spin_unlock(&space_info
->lock
);
1926 ret
= btrfs_alloc_chunk(trans
, extent_root
, flags
);
1928 space_info
->full
= 1;
1930 mutex_unlock(&extent_root
->fs_info
->chunk_mutex
);
1934 static int update_block_group(struct btrfs_trans_handle
*trans
,
1935 struct btrfs_root
*root
,
1936 u64 bytenr
, u64 num_bytes
, int alloc
,
1939 struct btrfs_block_group_cache
*cache
;
1940 struct btrfs_fs_info
*info
= root
->fs_info
;
1941 u64 total
= num_bytes
;
1946 cache
= btrfs_lookup_block_group(info
, bytenr
);
1949 byte_in_group
= bytenr
- cache
->key
.objectid
;
1950 WARN_ON(byte_in_group
> cache
->key
.offset
);
1952 spin_lock(&cache
->space_info
->lock
);
1953 spin_lock(&cache
->lock
);
1955 old_val
= btrfs_block_group_used(&cache
->item
);
1956 num_bytes
= min(total
, cache
->key
.offset
- byte_in_group
);
1958 old_val
+= num_bytes
;
1959 cache
->space_info
->bytes_used
+= num_bytes
;
1961 cache
->space_info
->bytes_readonly
-= num_bytes
;
1962 btrfs_set_block_group_used(&cache
->item
, old_val
);
1963 spin_unlock(&cache
->lock
);
1964 spin_unlock(&cache
->space_info
->lock
);
1966 old_val
-= num_bytes
;
1967 cache
->space_info
->bytes_used
-= num_bytes
;
1969 cache
->space_info
->bytes_readonly
+= num_bytes
;
1970 btrfs_set_block_group_used(&cache
->item
, old_val
);
1971 spin_unlock(&cache
->lock
);
1972 spin_unlock(&cache
->space_info
->lock
);
1976 ret
= btrfs_discard_extent(root
, bytenr
,
1980 ret
= btrfs_add_free_space(cache
, bytenr
,
1985 put_block_group(cache
);
1987 bytenr
+= num_bytes
;
1992 static u64
first_logical_byte(struct btrfs_root
*root
, u64 search_start
)
1994 struct btrfs_block_group_cache
*cache
;
1997 cache
= btrfs_lookup_first_block_group(root
->fs_info
, search_start
);
2001 bytenr
= cache
->key
.objectid
;
2002 put_block_group(cache
);
2007 int btrfs_update_pinned_extents(struct btrfs_root
*root
,
2008 u64 bytenr
, u64 num
, int pin
)
2011 struct btrfs_block_group_cache
*cache
;
2012 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2014 WARN_ON(!mutex_is_locked(&root
->fs_info
->pinned_mutex
));
2016 set_extent_dirty(&fs_info
->pinned_extents
,
2017 bytenr
, bytenr
+ num
- 1, GFP_NOFS
);
2019 clear_extent_dirty(&fs_info
->pinned_extents
,
2020 bytenr
, bytenr
+ num
- 1, GFP_NOFS
);
2023 cache
= btrfs_lookup_block_group(fs_info
, bytenr
);
2025 len
= min(num
, cache
->key
.offset
-
2026 (bytenr
- cache
->key
.objectid
));
2028 spin_lock(&cache
->space_info
->lock
);
2029 spin_lock(&cache
->lock
);
2030 cache
->pinned
+= len
;
2031 cache
->space_info
->bytes_pinned
+= len
;
2032 spin_unlock(&cache
->lock
);
2033 spin_unlock(&cache
->space_info
->lock
);
2034 fs_info
->total_pinned
+= len
;
2036 spin_lock(&cache
->space_info
->lock
);
2037 spin_lock(&cache
->lock
);
2038 cache
->pinned
-= len
;
2039 cache
->space_info
->bytes_pinned
-= len
;
2040 spin_unlock(&cache
->lock
);
2041 spin_unlock(&cache
->space_info
->lock
);
2042 fs_info
->total_pinned
-= len
;
2044 btrfs_add_free_space(cache
, bytenr
, len
);
2046 put_block_group(cache
);
2053 static int update_reserved_extents(struct btrfs_root
*root
,
2054 u64 bytenr
, u64 num
, int reserve
)
2057 struct btrfs_block_group_cache
*cache
;
2058 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2061 cache
= btrfs_lookup_block_group(fs_info
, bytenr
);
2063 len
= min(num
, cache
->key
.offset
-
2064 (bytenr
- cache
->key
.objectid
));
2066 spin_lock(&cache
->space_info
->lock
);
2067 spin_lock(&cache
->lock
);
2069 cache
->reserved
+= len
;
2070 cache
->space_info
->bytes_reserved
+= len
;
2072 cache
->reserved
-= len
;
2073 cache
->space_info
->bytes_reserved
-= len
;
2075 spin_unlock(&cache
->lock
);
2076 spin_unlock(&cache
->space_info
->lock
);
2077 put_block_group(cache
);
2084 int btrfs_copy_pinned(struct btrfs_root
*root
, struct extent_io_tree
*copy
)
2089 struct extent_io_tree
*pinned_extents
= &root
->fs_info
->pinned_extents
;
2092 mutex_lock(&root
->fs_info
->pinned_mutex
);
2094 ret
= find_first_extent_bit(pinned_extents
, last
,
2095 &start
, &end
, EXTENT_DIRTY
);
2098 set_extent_dirty(copy
, start
, end
, GFP_NOFS
);
2101 mutex_unlock(&root
->fs_info
->pinned_mutex
);
2105 int btrfs_finish_extent_commit(struct btrfs_trans_handle
*trans
,
2106 struct btrfs_root
*root
,
2107 struct extent_io_tree
*unpin
)
2113 mutex_lock(&root
->fs_info
->pinned_mutex
);
2115 ret
= find_first_extent_bit(unpin
, 0, &start
, &end
,
2120 ret
= btrfs_discard_extent(root
, start
, end
+ 1 - start
);
2122 btrfs_update_pinned_extents(root
, start
, end
+ 1 - start
, 0);
2123 clear_extent_dirty(unpin
, start
, end
, GFP_NOFS
);
2125 if (need_resched()) {
2126 mutex_unlock(&root
->fs_info
->pinned_mutex
);
2128 mutex_lock(&root
->fs_info
->pinned_mutex
);
2131 mutex_unlock(&root
->fs_info
->pinned_mutex
);
2135 static int pin_down_bytes(struct btrfs_trans_handle
*trans
,
2136 struct btrfs_root
*root
,
2137 u64 bytenr
, u64 num_bytes
, int is_data
)
2140 struct extent_buffer
*buf
;
2145 buf
= btrfs_find_tree_block(root
, bytenr
, num_bytes
);
2149 /* we can reuse a block if it hasn't been written
2150 * and it is from this transaction. We can't
2151 * reuse anything from the tree log root because
2152 * it has tiny sub-transactions.
2154 if (btrfs_buffer_uptodate(buf
, 0) &&
2155 btrfs_try_tree_lock(buf
)) {
2156 u64 header_owner
= btrfs_header_owner(buf
);
2157 u64 header_transid
= btrfs_header_generation(buf
);
2158 if (header_owner
!= BTRFS_TREE_LOG_OBJECTID
&&
2159 header_owner
!= BTRFS_TREE_RELOC_OBJECTID
&&
2160 header_owner
!= BTRFS_DATA_RELOC_TREE_OBJECTID
&&
2161 header_transid
== trans
->transid
&&
2162 !btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
)) {
2163 clean_tree_block(NULL
, root
, buf
);
2164 btrfs_tree_unlock(buf
);
2165 free_extent_buffer(buf
);
2168 btrfs_tree_unlock(buf
);
2170 free_extent_buffer(buf
);
2172 btrfs_update_pinned_extents(root
, bytenr
, num_bytes
, 1);
2179 * remove an extent from the root, returns 0 on success
2181 static int __free_extent(struct btrfs_trans_handle
*trans
,
2182 struct btrfs_root
*root
,
2183 u64 bytenr
, u64 num_bytes
, u64 parent
,
2184 u64 root_objectid
, u64 ref_generation
,
2185 u64 owner_objectid
, int pin
, int mark_free
,
2188 struct btrfs_path
*path
;
2189 struct btrfs_key key
;
2190 struct btrfs_fs_info
*info
= root
->fs_info
;
2191 struct btrfs_root
*extent_root
= info
->extent_root
;
2192 struct extent_buffer
*leaf
;
2194 int extent_slot
= 0;
2195 int found_extent
= 0;
2197 struct btrfs_extent_item
*ei
;
2200 key
.objectid
= bytenr
;
2201 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
2202 key
.offset
= num_bytes
;
2203 path
= btrfs_alloc_path();
2208 ret
= lookup_extent_backref(trans
, extent_root
, path
,
2209 bytenr
, parent
, root_objectid
,
2210 ref_generation
, owner_objectid
, 1);
2212 struct btrfs_key found_key
;
2213 extent_slot
= path
->slots
[0];
2214 while (extent_slot
> 0) {
2216 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2218 if (found_key
.objectid
!= bytenr
)
2220 if (found_key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
2221 found_key
.offset
== num_bytes
) {
2225 if (path
->slots
[0] - extent_slot
> 5)
2228 if (!found_extent
) {
2229 ret
= remove_extent_backref(trans
, extent_root
, path
,
2232 btrfs_release_path(extent_root
, path
);
2233 ret
= btrfs_search_slot(trans
, extent_root
,
2236 printk(KERN_ERR
"umm, got %d back from search"
2237 ", was looking for %llu\n", ret
,
2238 (unsigned long long)bytenr
);
2239 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
2242 extent_slot
= path
->slots
[0];
2245 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
2247 printk(KERN_ERR
"btrfs unable to find ref byte nr %llu "
2248 "parent %llu root %llu gen %llu owner %llu\n",
2249 (unsigned long long)bytenr
,
2250 (unsigned long long)parent
,
2251 (unsigned long long)root_objectid
,
2252 (unsigned long long)ref_generation
,
2253 (unsigned long long)owner_objectid
);
2256 leaf
= path
->nodes
[0];
2257 ei
= btrfs_item_ptr(leaf
, extent_slot
,
2258 struct btrfs_extent_item
);
2259 refs
= btrfs_extent_refs(leaf
, ei
);
2262 * we're not allowed to delete the extent item if there
2263 * are other delayed ref updates pending
2266 BUG_ON(refs
< refs_to_drop
);
2267 refs
-= refs_to_drop
;
2268 btrfs_set_extent_refs(leaf
, ei
, refs
);
2269 btrfs_mark_buffer_dirty(leaf
);
2271 if (refs
== 0 && found_extent
&&
2272 path
->slots
[0] == extent_slot
+ 1) {
2273 struct btrfs_extent_ref
*ref
;
2274 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
2275 struct btrfs_extent_ref
);
2276 BUG_ON(btrfs_ref_num_refs(leaf
, ref
) != refs_to_drop
);
2277 /* if the back ref and the extent are next to each other
2278 * they get deleted below in one shot
2280 path
->slots
[0] = extent_slot
;
2282 } else if (found_extent
) {
2283 /* otherwise delete the extent back ref */
2284 ret
= remove_extent_backref(trans
, extent_root
, path
,
2287 /* if refs are 0, we need to setup the path for deletion */
2289 btrfs_release_path(extent_root
, path
);
2290 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
,
2301 mutex_lock(&root
->fs_info
->pinned_mutex
);
2302 ret
= pin_down_bytes(trans
, root
, bytenr
, num_bytes
,
2303 owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
);
2304 mutex_unlock(&root
->fs_info
->pinned_mutex
);
2309 /* block accounting for super block */
2310 spin_lock(&info
->delalloc_lock
);
2311 super_used
= btrfs_super_bytes_used(&info
->super_copy
);
2312 btrfs_set_super_bytes_used(&info
->super_copy
,
2313 super_used
- num_bytes
);
2315 /* block accounting for root item */
2316 root_used
= btrfs_root_used(&root
->root_item
);
2317 btrfs_set_root_used(&root
->root_item
,
2318 root_used
- num_bytes
);
2319 spin_unlock(&info
->delalloc_lock
);
2320 ret
= btrfs_del_items(trans
, extent_root
, path
, path
->slots
[0],
2323 btrfs_release_path(extent_root
, path
);
2325 if (owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
2326 ret
= btrfs_del_csums(trans
, root
, bytenr
, num_bytes
);
2330 ret
= update_block_group(trans
, root
, bytenr
, num_bytes
, 0,
2334 btrfs_free_path(path
);
2339 * remove an extent from the root, returns 0 on success
2341 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
2342 struct btrfs_root
*root
,
2343 u64 bytenr
, u64 num_bytes
, u64 parent
,
2344 u64 root_objectid
, u64 ref_generation
,
2345 u64 owner_objectid
, int pin
,
2348 WARN_ON(num_bytes
< root
->sectorsize
);
2351 * if metadata always pin
2352 * if data pin when any transaction has committed this
2354 if (owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
||
2355 ref_generation
!= trans
->transid
)
2358 if (ref_generation
!= trans
->transid
)
2361 return __free_extent(trans
, root
, bytenr
, num_bytes
, parent
,
2362 root_objectid
, ref_generation
,
2363 owner_objectid
, pin
, pin
== 0, refs_to_drop
);
2367 * when we free an extent, it is possible (and likely) that we free the last
2368 * delayed ref for that extent as well. This searches the delayed ref tree for
2369 * a given extent, and if there are no other delayed refs to be processed, it
2370 * removes it from the tree.
2372 static noinline
int check_ref_cleanup(struct btrfs_trans_handle
*trans
,
2373 struct btrfs_root
*root
, u64 bytenr
)
2375 struct btrfs_delayed_ref_head
*head
;
2376 struct btrfs_delayed_ref_root
*delayed_refs
;
2377 struct btrfs_delayed_ref_node
*ref
;
2378 struct rb_node
*node
;
2381 delayed_refs
= &trans
->transaction
->delayed_refs
;
2382 spin_lock(&delayed_refs
->lock
);
2383 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
2387 node
= rb_prev(&head
->node
.rb_node
);
2391 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
2393 /* there are still entries for this ref, we can't drop it */
2394 if (ref
->bytenr
== bytenr
)
2398 * waiting for the lock here would deadlock. If someone else has it
2399 * locked they are already in the process of dropping it anyway
2401 if (!mutex_trylock(&head
->mutex
))
2405 * at this point we have a head with no other entries. Go
2406 * ahead and process it.
2408 head
->node
.in_tree
= 0;
2409 rb_erase(&head
->node
.rb_node
, &delayed_refs
->root
);
2410 delayed_refs
->num_entries
--;
2413 * we don't take a ref on the node because we're removing it from the
2414 * tree, so we just steal the ref the tree was holding.
2416 spin_unlock(&delayed_refs
->lock
);
2418 ret
= run_one_delayed_ref(trans
, root
->fs_info
->tree_root
,
2419 &head
->node
, head
->must_insert_reserved
);
2421 btrfs_put_delayed_ref(&head
->node
);
2424 spin_unlock(&delayed_refs
->lock
);
2428 int btrfs_free_extent(struct btrfs_trans_handle
*trans
,
2429 struct btrfs_root
*root
,
2430 u64 bytenr
, u64 num_bytes
, u64 parent
,
2431 u64 root_objectid
, u64 ref_generation
,
2432 u64 owner_objectid
, int pin
)
2437 * tree log blocks never actually go into the extent allocation
2438 * tree, just update pinning info and exit early.
2440 * data extents referenced by the tree log do need to have
2441 * their reference counts bumped.
2443 if (root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
&&
2444 owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
) {
2445 mutex_lock(&root
->fs_info
->pinned_mutex
);
2446 btrfs_update_pinned_extents(root
, bytenr
, num_bytes
, 1);
2447 mutex_unlock(&root
->fs_info
->pinned_mutex
);
2448 update_reserved_extents(root
, bytenr
, num_bytes
, 0);
2451 ret
= btrfs_add_delayed_ref(trans
, bytenr
, num_bytes
, parent
,
2452 root_objectid
, ref_generation
,
2454 BTRFS_DROP_DELAYED_REF
, 1);
2456 ret
= check_ref_cleanup(trans
, root
, bytenr
);
2462 static u64
stripe_align(struct btrfs_root
*root
, u64 val
)
2464 u64 mask
= ((u64
)root
->stripesize
- 1);
2465 u64 ret
= (val
+ mask
) & ~mask
;
2470 * walks the btree of allocated extents and find a hole of a given size.
2471 * The key ins is changed to record the hole:
2472 * ins->objectid == block start
2473 * ins->flags = BTRFS_EXTENT_ITEM_KEY
2474 * ins->offset == number of blocks
2475 * Any available blocks before search_start are skipped.
2477 static noinline
int find_free_extent(struct btrfs_trans_handle
*trans
,
2478 struct btrfs_root
*orig_root
,
2479 u64 num_bytes
, u64 empty_size
,
2480 u64 search_start
, u64 search_end
,
2481 u64 hint_byte
, struct btrfs_key
*ins
,
2482 u64 exclude_start
, u64 exclude_nr
,
2486 struct btrfs_root
*root
= orig_root
->fs_info
->extent_root
;
2487 u64 total_needed
= num_bytes
;
2488 u64
*last_ptr
= NULL
;
2489 u64 last_wanted
= 0;
2490 struct btrfs_block_group_cache
*block_group
= NULL
;
2491 int chunk_alloc_done
= 0;
2492 int empty_cluster
= 2 * 1024 * 1024;
2493 int allowed_chunk_alloc
= 0;
2494 struct list_head
*head
= NULL
, *cur
= NULL
;
2497 struct btrfs_space_info
*space_info
;
2499 WARN_ON(num_bytes
< root
->sectorsize
);
2500 btrfs_set_key_type(ins
, BTRFS_EXTENT_ITEM_KEY
);
2504 if (orig_root
->ref_cows
|| empty_size
)
2505 allowed_chunk_alloc
= 1;
2507 if (data
& BTRFS_BLOCK_GROUP_METADATA
) {
2508 last_ptr
= &root
->fs_info
->last_alloc
;
2509 if (!btrfs_test_opt(root
, SSD
))
2510 empty_cluster
= 64 * 1024;
2513 if ((data
& BTRFS_BLOCK_GROUP_DATA
) && btrfs_test_opt(root
, SSD
))
2514 last_ptr
= &root
->fs_info
->last_data_alloc
;
2518 hint_byte
= *last_ptr
;
2519 last_wanted
= *last_ptr
;
2521 empty_size
+= empty_cluster
;
2525 search_start
= max(search_start
, first_logical_byte(root
, 0));
2526 search_start
= max(search_start
, hint_byte
);
2528 if (last_wanted
&& search_start
!= last_wanted
) {
2530 empty_size
+= empty_cluster
;
2533 total_needed
+= empty_size
;
2534 block_group
= btrfs_lookup_block_group(root
->fs_info
, search_start
);
2536 block_group
= btrfs_lookup_first_block_group(root
->fs_info
,
2538 space_info
= __find_space_info(root
->fs_info
, data
);
2540 down_read(&space_info
->groups_sem
);
2542 struct btrfs_free_space
*free_space
;
2544 * the only way this happens if our hint points to a block
2545 * group thats not of the proper type, while looping this
2546 * should never happen
2552 goto new_group_no_lock
;
2554 if (unlikely(!block_group
->cached
)) {
2555 mutex_lock(&block_group
->cache_mutex
);
2556 ret
= cache_block_group(root
, block_group
);
2557 mutex_unlock(&block_group
->cache_mutex
);
2562 mutex_lock(&block_group
->alloc_mutex
);
2563 if (unlikely(!block_group_bits(block_group
, data
)))
2566 if (unlikely(block_group
->ro
))
2569 free_space
= btrfs_find_free_space(block_group
, search_start
,
2572 u64 start
= block_group
->key
.objectid
;
2573 u64 end
= block_group
->key
.objectid
+
2574 block_group
->key
.offset
;
2576 search_start
= stripe_align(root
, free_space
->offset
);
2578 /* move on to the next group */
2579 if (search_start
+ num_bytes
>= search_end
)
2582 /* move on to the next group */
2583 if (search_start
+ num_bytes
> end
)
2586 if (last_wanted
&& search_start
!= last_wanted
) {
2587 total_needed
+= empty_cluster
;
2588 empty_size
+= empty_cluster
;
2591 * if search_start is still in this block group
2592 * then we just re-search this block group
2594 if (search_start
>= start
&&
2595 search_start
< end
) {
2596 mutex_unlock(&block_group
->alloc_mutex
);
2600 /* else we go to the next block group */
2604 if (exclude_nr
> 0 &&
2605 (search_start
+ num_bytes
> exclude_start
&&
2606 search_start
< exclude_start
+ exclude_nr
)) {
2607 search_start
= exclude_start
+ exclude_nr
;
2609 * if search_start is still in this block group
2610 * then we just re-search this block group
2612 if (search_start
>= start
&&
2613 search_start
< end
) {
2614 mutex_unlock(&block_group
->alloc_mutex
);
2619 /* else we go to the next block group */
2623 ins
->objectid
= search_start
;
2624 ins
->offset
= num_bytes
;
2626 btrfs_remove_free_space_lock(block_group
, search_start
,
2628 /* we are all good, lets return */
2629 mutex_unlock(&block_group
->alloc_mutex
);
2633 mutex_unlock(&block_group
->alloc_mutex
);
2634 put_block_group(block_group
);
2637 /* don't try to compare new allocations against the
2638 * last allocation any more
2643 * Here's how this works.
2644 * loop == 0: we were searching a block group via a hint
2645 * and didn't find anything, so we start at
2646 * the head of the block groups and keep searching
2647 * loop == 1: we're searching through all of the block groups
2648 * if we hit the head again we have searched
2649 * all of the block groups for this space and we
2650 * need to try and allocate, if we cant error out.
2651 * loop == 2: we allocated more space and are looping through
2652 * all of the block groups again.
2655 head
= &space_info
->block_groups
;
2658 } else if (loop
== 1 && cur
== head
) {
2661 /* at this point we give up on the empty_size
2662 * allocations and just try to allocate the min
2665 * The extra_loop field was set if an empty_size
2666 * allocation was attempted above, and if this
2667 * is try we need to try the loop again without
2668 * the additional empty_size.
2670 total_needed
-= empty_size
;
2672 keep_going
= extra_loop
;
2675 if (allowed_chunk_alloc
&& !chunk_alloc_done
) {
2676 up_read(&space_info
->groups_sem
);
2677 ret
= do_chunk_alloc(trans
, root
, num_bytes
+
2678 2 * 1024 * 1024, data
, 1);
2679 down_read(&space_info
->groups_sem
);
2682 head
= &space_info
->block_groups
;
2684 * we've allocated a new chunk, keep
2688 chunk_alloc_done
= 1;
2689 } else if (!allowed_chunk_alloc
) {
2690 space_info
->force_alloc
= 1;
2699 } else if (cur
== head
) {
2703 block_group
= list_entry(cur
, struct btrfs_block_group_cache
,
2705 atomic_inc(&block_group
->count
);
2707 search_start
= block_group
->key
.objectid
;
2711 /* we found what we needed */
2712 if (ins
->objectid
) {
2713 if (!(data
& BTRFS_BLOCK_GROUP_DATA
))
2714 trans
->block_group
= block_group
->key
.objectid
;
2717 *last_ptr
= ins
->objectid
+ ins
->offset
;
2720 printk(KERN_ERR
"btrfs searching for %llu bytes, "
2721 "num_bytes %llu, loop %d, allowed_alloc %d\n",
2722 (unsigned long long)total_needed
,
2723 (unsigned long long)num_bytes
,
2724 loop
, allowed_chunk_alloc
);
2728 put_block_group(block_group
);
2730 up_read(&space_info
->groups_sem
);
2734 static void dump_space_info(struct btrfs_space_info
*info
, u64 bytes
)
2736 struct btrfs_block_group_cache
*cache
;
2738 printk(KERN_INFO
"space_info has %llu free, is %sfull\n",
2739 (unsigned long long)(info
->total_bytes
- info
->bytes_used
-
2740 info
->bytes_pinned
- info
->bytes_reserved
),
2741 (info
->full
) ? "" : "not ");
2742 printk(KERN_INFO
"space_info total=%llu, pinned=%llu, delalloc=%llu,"
2743 " may_use=%llu, used=%llu\n", info
->total_bytes
,
2744 info
->bytes_pinned
, info
->bytes_delalloc
, info
->bytes_may_use
,
2747 down_read(&info
->groups_sem
);
2748 list_for_each_entry(cache
, &info
->block_groups
, list
) {
2749 spin_lock(&cache
->lock
);
2750 printk(KERN_INFO
"block group %llu has %llu bytes, %llu used "
2751 "%llu pinned %llu reserved\n",
2752 (unsigned long long)cache
->key
.objectid
,
2753 (unsigned long long)cache
->key
.offset
,
2754 (unsigned long long)btrfs_block_group_used(&cache
->item
),
2755 (unsigned long long)cache
->pinned
,
2756 (unsigned long long)cache
->reserved
);
2757 btrfs_dump_free_space(cache
, bytes
);
2758 spin_unlock(&cache
->lock
);
2760 up_read(&info
->groups_sem
);
2763 static int __btrfs_reserve_extent(struct btrfs_trans_handle
*trans
,
2764 struct btrfs_root
*root
,
2765 u64 num_bytes
, u64 min_alloc_size
,
2766 u64 empty_size
, u64 hint_byte
,
2767 u64 search_end
, struct btrfs_key
*ins
,
2771 u64 search_start
= 0;
2772 struct btrfs_fs_info
*info
= root
->fs_info
;
2774 data
= btrfs_get_alloc_profile(root
, data
);
2777 * the only place that sets empty_size is btrfs_realloc_node, which
2778 * is not called recursively on allocations
2780 if (empty_size
|| root
->ref_cows
) {
2781 if (!(data
& BTRFS_BLOCK_GROUP_METADATA
)) {
2782 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
2784 BTRFS_BLOCK_GROUP_METADATA
|
2785 (info
->metadata_alloc_profile
&
2786 info
->avail_metadata_alloc_bits
), 0);
2788 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
2789 num_bytes
+ 2 * 1024 * 1024, data
, 0);
2792 WARN_ON(num_bytes
< root
->sectorsize
);
2793 ret
= find_free_extent(trans
, root
, num_bytes
, empty_size
,
2794 search_start
, search_end
, hint_byte
, ins
,
2795 trans
->alloc_exclude_start
,
2796 trans
->alloc_exclude_nr
, data
);
2798 if (ret
== -ENOSPC
&& num_bytes
> min_alloc_size
) {
2799 num_bytes
= num_bytes
>> 1;
2800 num_bytes
= num_bytes
& ~(root
->sectorsize
- 1);
2801 num_bytes
= max(num_bytes
, min_alloc_size
);
2802 do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
2803 num_bytes
, data
, 1);
2807 struct btrfs_space_info
*sinfo
;
2809 sinfo
= __find_space_info(root
->fs_info
, data
);
2810 printk(KERN_ERR
"btrfs allocation failed flags %llu, "
2811 "wanted %llu\n", (unsigned long long)data
,
2812 (unsigned long long)num_bytes
);
2813 dump_space_info(sinfo
, num_bytes
);
2820 int btrfs_free_reserved_extent(struct btrfs_root
*root
, u64 start
, u64 len
)
2822 struct btrfs_block_group_cache
*cache
;
2825 cache
= btrfs_lookup_block_group(root
->fs_info
, start
);
2827 printk(KERN_ERR
"Unable to find block group for %llu\n",
2828 (unsigned long long)start
);
2832 ret
= btrfs_discard_extent(root
, start
, len
);
2834 btrfs_add_free_space(cache
, start
, len
);
2835 put_block_group(cache
);
2836 update_reserved_extents(root
, start
, len
, 0);
2841 int btrfs_reserve_extent(struct btrfs_trans_handle
*trans
,
2842 struct btrfs_root
*root
,
2843 u64 num_bytes
, u64 min_alloc_size
,
2844 u64 empty_size
, u64 hint_byte
,
2845 u64 search_end
, struct btrfs_key
*ins
,
2849 ret
= __btrfs_reserve_extent(trans
, root
, num_bytes
, min_alloc_size
,
2850 empty_size
, hint_byte
, search_end
, ins
,
2852 update_reserved_extents(root
, ins
->objectid
, ins
->offset
, 1);
2856 static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle
*trans
,
2857 struct btrfs_root
*root
, u64 parent
,
2858 u64 root_objectid
, u64 ref_generation
,
2859 u64 owner
, struct btrfs_key
*ins
,
2865 u64 num_bytes
= ins
->offset
;
2867 struct btrfs_fs_info
*info
= root
->fs_info
;
2868 struct btrfs_root
*extent_root
= info
->extent_root
;
2869 struct btrfs_extent_item
*extent_item
;
2870 struct btrfs_extent_ref
*ref
;
2871 struct btrfs_path
*path
;
2872 struct btrfs_key keys
[2];
2875 parent
= ins
->objectid
;
2877 /* block accounting for super block */
2878 spin_lock(&info
->delalloc_lock
);
2879 super_used
= btrfs_super_bytes_used(&info
->super_copy
);
2880 btrfs_set_super_bytes_used(&info
->super_copy
, super_used
+ num_bytes
);
2882 /* block accounting for root item */
2883 root_used
= btrfs_root_used(&root
->root_item
);
2884 btrfs_set_root_used(&root
->root_item
, root_used
+ num_bytes
);
2885 spin_unlock(&info
->delalloc_lock
);
2887 memcpy(&keys
[0], ins
, sizeof(*ins
));
2888 keys
[1].objectid
= ins
->objectid
;
2889 keys
[1].type
= BTRFS_EXTENT_REF_KEY
;
2890 keys
[1].offset
= parent
;
2891 sizes
[0] = sizeof(*extent_item
);
2892 sizes
[1] = sizeof(*ref
);
2894 path
= btrfs_alloc_path();
2897 ret
= btrfs_insert_empty_items(trans
, extent_root
, path
, keys
,
2901 extent_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2902 struct btrfs_extent_item
);
2903 btrfs_set_extent_refs(path
->nodes
[0], extent_item
, ref_mod
);
2904 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
2905 struct btrfs_extent_ref
);
2907 btrfs_set_ref_root(path
->nodes
[0], ref
, root_objectid
);
2908 btrfs_set_ref_generation(path
->nodes
[0], ref
, ref_generation
);
2909 btrfs_set_ref_objectid(path
->nodes
[0], ref
, owner
);
2910 btrfs_set_ref_num_refs(path
->nodes
[0], ref
, ref_mod
);
2912 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2914 trans
->alloc_exclude_start
= 0;
2915 trans
->alloc_exclude_nr
= 0;
2916 btrfs_free_path(path
);
2921 ret
= update_block_group(trans
, root
, ins
->objectid
,
2924 printk(KERN_ERR
"btrfs update block group failed for %llu "
2925 "%llu\n", (unsigned long long)ins
->objectid
,
2926 (unsigned long long)ins
->offset
);
2933 int btrfs_alloc_reserved_extent(struct btrfs_trans_handle
*trans
,
2934 struct btrfs_root
*root
, u64 parent
,
2935 u64 root_objectid
, u64 ref_generation
,
2936 u64 owner
, struct btrfs_key
*ins
)
2940 if (root_objectid
== BTRFS_TREE_LOG_OBJECTID
)
2943 ret
= btrfs_add_delayed_ref(trans
, ins
->objectid
,
2944 ins
->offset
, parent
, root_objectid
,
2945 ref_generation
, owner
,
2946 BTRFS_ADD_DELAYED_EXTENT
, 0);
2952 * this is used by the tree logging recovery code. It records that
2953 * an extent has been allocated and makes sure to clear the free
2954 * space cache bits as well
2956 int btrfs_alloc_logged_extent(struct btrfs_trans_handle
*trans
,
2957 struct btrfs_root
*root
, u64 parent
,
2958 u64 root_objectid
, u64 ref_generation
,
2959 u64 owner
, struct btrfs_key
*ins
)
2962 struct btrfs_block_group_cache
*block_group
;
2964 block_group
= btrfs_lookup_block_group(root
->fs_info
, ins
->objectid
);
2965 mutex_lock(&block_group
->cache_mutex
);
2966 cache_block_group(root
, block_group
);
2967 mutex_unlock(&block_group
->cache_mutex
);
2969 ret
= btrfs_remove_free_space(block_group
, ins
->objectid
,
2972 put_block_group(block_group
);
2973 ret
= __btrfs_alloc_reserved_extent(trans
, root
, parent
, root_objectid
,
2974 ref_generation
, owner
, ins
, 1);
2979 * finds a free extent and does all the dirty work required for allocation
2980 * returns the key for the extent through ins, and a tree buffer for
2981 * the first block of the extent through buf.
2983 * returns 0 if everything worked, non-zero otherwise.
2985 int btrfs_alloc_extent(struct btrfs_trans_handle
*trans
,
2986 struct btrfs_root
*root
,
2987 u64 num_bytes
, u64 parent
, u64 min_alloc_size
,
2988 u64 root_objectid
, u64 ref_generation
,
2989 u64 owner_objectid
, u64 empty_size
, u64 hint_byte
,
2990 u64 search_end
, struct btrfs_key
*ins
, u64 data
)
2993 ret
= __btrfs_reserve_extent(trans
, root
, num_bytes
,
2994 min_alloc_size
, empty_size
, hint_byte
,
2995 search_end
, ins
, data
);
2997 if (root_objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
2998 ret
= btrfs_add_delayed_ref(trans
, ins
->objectid
,
2999 ins
->offset
, parent
, root_objectid
,
3000 ref_generation
, owner_objectid
,
3001 BTRFS_ADD_DELAYED_EXTENT
, 0);
3004 update_reserved_extents(root
, ins
->objectid
, ins
->offset
, 1);
3008 struct extent_buffer
*btrfs_init_new_buffer(struct btrfs_trans_handle
*trans
,
3009 struct btrfs_root
*root
,
3010 u64 bytenr
, u32 blocksize
,
3013 struct extent_buffer
*buf
;
3015 buf
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
3017 return ERR_PTR(-ENOMEM
);
3018 btrfs_set_header_generation(buf
, trans
->transid
);
3019 btrfs_set_buffer_lockdep_class(buf
, level
);
3020 btrfs_tree_lock(buf
);
3021 clean_tree_block(trans
, root
, buf
);
3023 btrfs_set_lock_blocking(buf
);
3024 btrfs_set_buffer_uptodate(buf
);
3026 if (root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
) {
3027 set_extent_dirty(&root
->dirty_log_pages
, buf
->start
,
3028 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
3030 set_extent_dirty(&trans
->transaction
->dirty_pages
, buf
->start
,
3031 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
3033 trans
->blocks_used
++;
3034 /* this returns a buffer locked for blocking */
3039 * helper function to allocate a block for a given tree
3040 * returns the tree buffer or NULL.
3042 struct extent_buffer
*btrfs_alloc_free_block(struct btrfs_trans_handle
*trans
,
3043 struct btrfs_root
*root
,
3044 u32 blocksize
, u64 parent
,
3051 struct btrfs_key ins
;
3053 struct extent_buffer
*buf
;
3055 ret
= btrfs_alloc_extent(trans
, root
, blocksize
, parent
, blocksize
,
3056 root_objectid
, ref_generation
, level
,
3057 empty_size
, hint
, (u64
)-1, &ins
, 0);
3060 return ERR_PTR(ret
);
3063 buf
= btrfs_init_new_buffer(trans
, root
, ins
.objectid
,
3068 int btrfs_drop_leaf_ref(struct btrfs_trans_handle
*trans
,
3069 struct btrfs_root
*root
, struct extent_buffer
*leaf
)
3072 u64 leaf_generation
;
3073 struct refsort
*sorted
;
3074 struct btrfs_key key
;
3075 struct btrfs_file_extent_item
*fi
;
3082 BUG_ON(!btrfs_is_leaf(leaf
));
3083 nritems
= btrfs_header_nritems(leaf
);
3084 leaf_owner
= btrfs_header_owner(leaf
);
3085 leaf_generation
= btrfs_header_generation(leaf
);
3087 sorted
= kmalloc(sizeof(*sorted
) * nritems
, GFP_NOFS
);
3088 /* we do this loop twice. The first time we build a list
3089 * of the extents we have a reference on, then we sort the list
3090 * by bytenr. The second time around we actually do the
3093 for (i
= 0; i
< nritems
; i
++) {
3097 btrfs_item_key_to_cpu(leaf
, &key
, i
);
3099 /* only extents have references, skip everything else */
3100 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
3103 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
3105 /* inline extents live in the btree, they don't have refs */
3106 if (btrfs_file_extent_type(leaf
, fi
) ==
3107 BTRFS_FILE_EXTENT_INLINE
)
3110 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
3112 /* holes don't have refs */
3113 if (disk_bytenr
== 0)
3116 sorted
[refi
].bytenr
= disk_bytenr
;
3117 sorted
[refi
].slot
= i
;
3124 sort(sorted
, refi
, sizeof(struct refsort
), refsort_cmp
, NULL
);
3126 for (i
= 0; i
< refi
; i
++) {
3129 disk_bytenr
= sorted
[i
].bytenr
;
3130 slot
= sorted
[i
].slot
;
3134 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
3135 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
3138 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
3140 ret
= btrfs_free_extent(trans
, root
, disk_bytenr
,
3141 btrfs_file_extent_disk_num_bytes(leaf
, fi
),
3142 leaf
->start
, leaf_owner
, leaf_generation
,
3146 atomic_inc(&root
->fs_info
->throttle_gen
);
3147 wake_up(&root
->fs_info
->transaction_throttle
);
3155 static noinline
int cache_drop_leaf_ref(struct btrfs_trans_handle
*trans
,
3156 struct btrfs_root
*root
,
3157 struct btrfs_leaf_ref
*ref
)
3161 struct btrfs_extent_info
*info
;
3162 struct refsort
*sorted
;
3164 if (ref
->nritems
== 0)
3167 sorted
= kmalloc(sizeof(*sorted
) * ref
->nritems
, GFP_NOFS
);
3168 for (i
= 0; i
< ref
->nritems
; i
++) {
3169 sorted
[i
].bytenr
= ref
->extents
[i
].bytenr
;
3172 sort(sorted
, ref
->nritems
, sizeof(struct refsort
), refsort_cmp
, NULL
);
3175 * the items in the ref were sorted when the ref was inserted
3176 * into the ref cache, so this is already in order
3178 for (i
= 0; i
< ref
->nritems
; i
++) {
3179 info
= ref
->extents
+ sorted
[i
].slot
;
3180 ret
= btrfs_free_extent(trans
, root
, info
->bytenr
,
3181 info
->num_bytes
, ref
->bytenr
,
3182 ref
->owner
, ref
->generation
,
3185 atomic_inc(&root
->fs_info
->throttle_gen
);
3186 wake_up(&root
->fs_info
->transaction_throttle
);
3197 static int drop_snap_lookup_refcount(struct btrfs_trans_handle
*trans
,
3198 struct btrfs_root
*root
, u64 start
,
3203 ret
= btrfs_lookup_extent_ref(trans
, root
, start
, len
, refs
);
3206 #if 0 /* some debugging code in case we see problems here */
3207 /* if the refs count is one, it won't get increased again. But
3208 * if the ref count is > 1, someone may be decreasing it at
3209 * the same time we are.
3212 struct extent_buffer
*eb
= NULL
;
3213 eb
= btrfs_find_create_tree_block(root
, start
, len
);
3215 btrfs_tree_lock(eb
);
3217 mutex_lock(&root
->fs_info
->alloc_mutex
);
3218 ret
= lookup_extent_ref(NULL
, root
, start
, len
, refs
);
3220 mutex_unlock(&root
->fs_info
->alloc_mutex
);
3223 btrfs_tree_unlock(eb
);
3224 free_extent_buffer(eb
);
3227 printk(KERN_ERR
"btrfs block %llu went down to one "
3228 "during drop_snap\n", (unsigned long long)start
);
3239 * this is used while deleting old snapshots, and it drops the refs
3240 * on a whole subtree starting from a level 1 node.
3242 * The idea is to sort all the leaf pointers, and then drop the
3243 * ref on all the leaves in order. Most of the time the leaves
3244 * will have ref cache entries, so no leaf IOs will be required to
3245 * find the extents they have references on.
3247 * For each leaf, any references it has are also dropped in order
3249 * This ends up dropping the references in something close to optimal
3250 * order for reading and modifying the extent allocation tree.
3252 static noinline
int drop_level_one_refs(struct btrfs_trans_handle
*trans
,
3253 struct btrfs_root
*root
,
3254 struct btrfs_path
*path
)
3259 struct extent_buffer
*eb
= path
->nodes
[1];
3260 struct extent_buffer
*leaf
;
3261 struct btrfs_leaf_ref
*ref
;
3262 struct refsort
*sorted
= NULL
;
3263 int nritems
= btrfs_header_nritems(eb
);
3267 int slot
= path
->slots
[1];
3268 u32 blocksize
= btrfs_level_size(root
, 0);
3274 root_owner
= btrfs_header_owner(eb
);
3275 root_gen
= btrfs_header_generation(eb
);
3276 sorted
= kmalloc(sizeof(*sorted
) * nritems
, GFP_NOFS
);
3279 * step one, sort all the leaf pointers so we don't scribble
3280 * randomly into the extent allocation tree
3282 for (i
= slot
; i
< nritems
; i
++) {
3283 sorted
[refi
].bytenr
= btrfs_node_blockptr(eb
, i
);
3284 sorted
[refi
].slot
= i
;
3289 * nritems won't be zero, but if we're picking up drop_snapshot
3290 * after a crash, slot might be > 0, so double check things
3296 sort(sorted
, refi
, sizeof(struct refsort
), refsort_cmp
, NULL
);
3299 * the first loop frees everything the leaves point to
3301 for (i
= 0; i
< refi
; i
++) {
3304 bytenr
= sorted
[i
].bytenr
;
3307 * check the reference count on this leaf. If it is > 1
3308 * we just decrement it below and don't update any
3309 * of the refs the leaf points to.
3311 ret
= drop_snap_lookup_refcount(trans
, root
, bytenr
,
3317 ptr_gen
= btrfs_node_ptr_generation(eb
, sorted
[i
].slot
);
3320 * the leaf only had one reference, which means the
3321 * only thing pointing to this leaf is the snapshot
3322 * we're deleting. It isn't possible for the reference
3323 * count to increase again later
3325 * The reference cache is checked for the leaf,
3326 * and if found we'll be able to drop any refs held by
3327 * the leaf without needing to read it in.
3329 ref
= btrfs_lookup_leaf_ref(root
, bytenr
);
3330 if (ref
&& ref
->generation
!= ptr_gen
) {
3331 btrfs_free_leaf_ref(root
, ref
);
3335 ret
= cache_drop_leaf_ref(trans
, root
, ref
);
3337 btrfs_remove_leaf_ref(root
, ref
);
3338 btrfs_free_leaf_ref(root
, ref
);
3341 * the leaf wasn't in the reference cache, so
3342 * we have to read it.
3344 leaf
= read_tree_block(root
, bytenr
, blocksize
,
3346 ret
= btrfs_drop_leaf_ref(trans
, root
, leaf
);
3348 free_extent_buffer(leaf
);
3350 atomic_inc(&root
->fs_info
->throttle_gen
);
3351 wake_up(&root
->fs_info
->transaction_throttle
);
3356 * run through the loop again to free the refs on the leaves.
3357 * This is faster than doing it in the loop above because
3358 * the leaves are likely to be clustered together. We end up
3359 * working in nice chunks on the extent allocation tree.
3361 for (i
= 0; i
< refi
; i
++) {
3362 bytenr
= sorted
[i
].bytenr
;
3363 ret
= btrfs_free_extent(trans
, root
, bytenr
,
3364 blocksize
, eb
->start
,
3365 root_owner
, root_gen
, 0, 1);
3368 atomic_inc(&root
->fs_info
->throttle_gen
);
3369 wake_up(&root
->fs_info
->transaction_throttle
);
3376 * update the path to show we've processed the entire level 1
3377 * node. This will get saved into the root's drop_snapshot_progress
3378 * field so these drops are not repeated again if this transaction
3381 path
->slots
[1] = nritems
;
3386 * helper function for drop_snapshot, this walks down the tree dropping ref
3387 * counts as it goes.
3389 static noinline
int walk_down_tree(struct btrfs_trans_handle
*trans
,
3390 struct btrfs_root
*root
,
3391 struct btrfs_path
*path
, int *level
)
3397 struct extent_buffer
*next
;
3398 struct extent_buffer
*cur
;
3399 struct extent_buffer
*parent
;
3404 WARN_ON(*level
< 0);
3405 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
3406 ret
= drop_snap_lookup_refcount(trans
, root
, path
->nodes
[*level
]->start
,
3407 path
->nodes
[*level
]->len
, &refs
);
3413 * walk down to the last node level and free all the leaves
3415 while (*level
>= 0) {
3416 WARN_ON(*level
< 0);
3417 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
3418 cur
= path
->nodes
[*level
];
3420 if (btrfs_header_level(cur
) != *level
)
3423 if (path
->slots
[*level
] >=
3424 btrfs_header_nritems(cur
))
3427 /* the new code goes down to level 1 and does all the
3428 * leaves pointed to that node in bulk. So, this check
3429 * for level 0 will always be false.
3431 * But, the disk format allows the drop_snapshot_progress
3432 * field in the root to leave things in a state where
3433 * a leaf will need cleaning up here. If someone crashes
3434 * with the old code and then boots with the new code,
3435 * we might find a leaf here.
3438 ret
= btrfs_drop_leaf_ref(trans
, root
, cur
);
3444 * once we get to level one, process the whole node
3445 * at once, including everything below it.
3448 ret
= drop_level_one_refs(trans
, root
, path
);
3453 bytenr
= btrfs_node_blockptr(cur
, path
->slots
[*level
]);
3454 ptr_gen
= btrfs_node_ptr_generation(cur
, path
->slots
[*level
]);
3455 blocksize
= btrfs_level_size(root
, *level
- 1);
3457 ret
= drop_snap_lookup_refcount(trans
, root
, bytenr
,
3462 * if there is more than one reference, we don't need
3463 * to read that node to drop any references it has. We
3464 * just drop the ref we hold on that node and move on to the
3465 * next slot in this level.
3468 parent
= path
->nodes
[*level
];
3469 root_owner
= btrfs_header_owner(parent
);
3470 root_gen
= btrfs_header_generation(parent
);
3471 path
->slots
[*level
]++;
3473 ret
= btrfs_free_extent(trans
, root
, bytenr
,
3474 blocksize
, parent
->start
,
3475 root_owner
, root_gen
,
3479 atomic_inc(&root
->fs_info
->throttle_gen
);
3480 wake_up(&root
->fs_info
->transaction_throttle
);
3487 * we need to keep freeing things in the next level down.
3488 * read the block and loop around to process it
3490 next
= read_tree_block(root
, bytenr
, blocksize
, ptr_gen
);
3491 WARN_ON(*level
<= 0);
3492 if (path
->nodes
[*level
-1])
3493 free_extent_buffer(path
->nodes
[*level
-1]);
3494 path
->nodes
[*level
-1] = next
;
3495 *level
= btrfs_header_level(next
);
3496 path
->slots
[*level
] = 0;
3500 WARN_ON(*level
< 0);
3501 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
3503 if (path
->nodes
[*level
] == root
->node
) {
3504 parent
= path
->nodes
[*level
];
3505 bytenr
= path
->nodes
[*level
]->start
;
3507 parent
= path
->nodes
[*level
+ 1];
3508 bytenr
= btrfs_node_blockptr(parent
, path
->slots
[*level
+ 1]);
3511 blocksize
= btrfs_level_size(root
, *level
);
3512 root_owner
= btrfs_header_owner(parent
);
3513 root_gen
= btrfs_header_generation(parent
);
3516 * cleanup and free the reference on the last node
3519 ret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
,
3520 parent
->start
, root_owner
, root_gen
,
3522 free_extent_buffer(path
->nodes
[*level
]);
3523 path
->nodes
[*level
] = NULL
;
3533 * helper function for drop_subtree, this function is similar to
3534 * walk_down_tree. The main difference is that it checks reference
3535 * counts while tree blocks are locked.
3537 static noinline
int walk_down_subtree(struct btrfs_trans_handle
*trans
,
3538 struct btrfs_root
*root
,
3539 struct btrfs_path
*path
, int *level
)
3541 struct extent_buffer
*next
;
3542 struct extent_buffer
*cur
;
3543 struct extent_buffer
*parent
;
3550 cur
= path
->nodes
[*level
];
3551 ret
= btrfs_lookup_extent_ref(trans
, root
, cur
->start
, cur
->len
,
3557 while (*level
>= 0) {
3558 cur
= path
->nodes
[*level
];
3560 ret
= btrfs_drop_leaf_ref(trans
, root
, cur
);
3562 clean_tree_block(trans
, root
, cur
);
3565 if (path
->slots
[*level
] >= btrfs_header_nritems(cur
)) {
3566 clean_tree_block(trans
, root
, cur
);
3570 bytenr
= btrfs_node_blockptr(cur
, path
->slots
[*level
]);
3571 blocksize
= btrfs_level_size(root
, *level
- 1);
3572 ptr_gen
= btrfs_node_ptr_generation(cur
, path
->slots
[*level
]);
3574 next
= read_tree_block(root
, bytenr
, blocksize
, ptr_gen
);
3575 btrfs_tree_lock(next
);
3576 btrfs_set_lock_blocking(next
);
3578 ret
= btrfs_lookup_extent_ref(trans
, root
, bytenr
, blocksize
,
3582 parent
= path
->nodes
[*level
];
3583 ret
= btrfs_free_extent(trans
, root
, bytenr
,
3584 blocksize
, parent
->start
,
3585 btrfs_header_owner(parent
),
3586 btrfs_header_generation(parent
),
3589 path
->slots
[*level
]++;
3590 btrfs_tree_unlock(next
);
3591 free_extent_buffer(next
);
3595 *level
= btrfs_header_level(next
);
3596 path
->nodes
[*level
] = next
;
3597 path
->slots
[*level
] = 0;
3598 path
->locks
[*level
] = 1;
3602 parent
= path
->nodes
[*level
+ 1];
3603 bytenr
= path
->nodes
[*level
]->start
;
3604 blocksize
= path
->nodes
[*level
]->len
;
3606 ret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
,
3607 parent
->start
, btrfs_header_owner(parent
),
3608 btrfs_header_generation(parent
), *level
, 1);
3611 if (path
->locks
[*level
]) {
3612 btrfs_tree_unlock(path
->nodes
[*level
]);
3613 path
->locks
[*level
] = 0;
3615 free_extent_buffer(path
->nodes
[*level
]);
3616 path
->nodes
[*level
] = NULL
;
3623 * helper for dropping snapshots. This walks back up the tree in the path
3624 * to find the first node higher up where we haven't yet gone through
3627 static noinline
int walk_up_tree(struct btrfs_trans_handle
*trans
,
3628 struct btrfs_root
*root
,
3629 struct btrfs_path
*path
,
3630 int *level
, int max_level
)
3634 struct btrfs_root_item
*root_item
= &root
->root_item
;
3639 for (i
= *level
; i
< max_level
&& path
->nodes
[i
]; i
++) {
3640 slot
= path
->slots
[i
];
3641 if (slot
< btrfs_header_nritems(path
->nodes
[i
]) - 1) {
3642 struct extent_buffer
*node
;
3643 struct btrfs_disk_key disk_key
;
3646 * there is more work to do in this level.
3647 * Update the drop_progress marker to reflect
3648 * the work we've done so far, and then bump
3651 node
= path
->nodes
[i
];
3654 WARN_ON(*level
== 0);
3655 btrfs_node_key(node
, &disk_key
, path
->slots
[i
]);
3656 memcpy(&root_item
->drop_progress
,
3657 &disk_key
, sizeof(disk_key
));
3658 root_item
->drop_level
= i
;
3661 struct extent_buffer
*parent
;
3664 * this whole node is done, free our reference
3665 * on it and go up one level
3667 if (path
->nodes
[*level
] == root
->node
)
3668 parent
= path
->nodes
[*level
];
3670 parent
= path
->nodes
[*level
+ 1];
3672 root_owner
= btrfs_header_owner(parent
);
3673 root_gen
= btrfs_header_generation(parent
);
3675 clean_tree_block(trans
, root
, path
->nodes
[*level
]);
3676 ret
= btrfs_free_extent(trans
, root
,
3677 path
->nodes
[*level
]->start
,
3678 path
->nodes
[*level
]->len
,
3679 parent
->start
, root_owner
,
3680 root_gen
, *level
, 1);
3682 if (path
->locks
[*level
]) {
3683 btrfs_tree_unlock(path
->nodes
[*level
]);
3684 path
->locks
[*level
] = 0;
3686 free_extent_buffer(path
->nodes
[*level
]);
3687 path
->nodes
[*level
] = NULL
;
3695 * drop the reference count on the tree rooted at 'snap'. This traverses
3696 * the tree freeing any blocks that have a ref count of zero after being
3699 int btrfs_drop_snapshot(struct btrfs_trans_handle
*trans
, struct btrfs_root
3705 struct btrfs_path
*path
;
3708 struct btrfs_root_item
*root_item
= &root
->root_item
;
3710 WARN_ON(!mutex_is_locked(&root
->fs_info
->drop_mutex
));
3711 path
= btrfs_alloc_path();
3714 level
= btrfs_header_level(root
->node
);
3716 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
3717 path
->nodes
[level
] = root
->node
;
3718 extent_buffer_get(root
->node
);
3719 path
->slots
[level
] = 0;
3721 struct btrfs_key key
;
3722 struct btrfs_disk_key found_key
;
3723 struct extent_buffer
*node
;
3725 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
3726 level
= root_item
->drop_level
;
3727 path
->lowest_level
= level
;
3728 wret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3733 node
= path
->nodes
[level
];
3734 btrfs_node_key(node
, &found_key
, path
->slots
[level
]);
3735 WARN_ON(memcmp(&found_key
, &root_item
->drop_progress
,
3736 sizeof(found_key
)));
3738 * unlock our path, this is safe because only this
3739 * function is allowed to delete this snapshot
3741 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++) {
3742 if (path
->nodes
[i
] && path
->locks
[i
]) {
3744 btrfs_tree_unlock(path
->nodes
[i
]);
3749 wret
= walk_down_tree(trans
, root
, path
, &level
);
3755 wret
= walk_up_tree(trans
, root
, path
, &level
,
3761 if (trans
->transaction
->in_commit
) {
3765 atomic_inc(&root
->fs_info
->throttle_gen
);
3766 wake_up(&root
->fs_info
->transaction_throttle
);
3768 for (i
= 0; i
<= orig_level
; i
++) {
3769 if (path
->nodes
[i
]) {
3770 free_extent_buffer(path
->nodes
[i
]);
3771 path
->nodes
[i
] = NULL
;
3775 btrfs_free_path(path
);
3779 int btrfs_drop_subtree(struct btrfs_trans_handle
*trans
,
3780 struct btrfs_root
*root
,
3781 struct extent_buffer
*node
,
3782 struct extent_buffer
*parent
)
3784 struct btrfs_path
*path
;
3790 path
= btrfs_alloc_path();
3793 btrfs_assert_tree_locked(parent
);
3794 parent_level
= btrfs_header_level(parent
);
3795 extent_buffer_get(parent
);
3796 path
->nodes
[parent_level
] = parent
;
3797 path
->slots
[parent_level
] = btrfs_header_nritems(parent
);
3799 btrfs_assert_tree_locked(node
);
3800 level
= btrfs_header_level(node
);
3801 extent_buffer_get(node
);
3802 path
->nodes
[level
] = node
;
3803 path
->slots
[level
] = 0;
3806 wret
= walk_down_subtree(trans
, root
, path
, &level
);
3812 wret
= walk_up_tree(trans
, root
, path
, &level
, parent_level
);
3819 btrfs_free_path(path
);
3823 static unsigned long calc_ra(unsigned long start
, unsigned long last
,
3826 return min(last
, start
+ nr
- 1);
3829 static noinline
int relocate_inode_pages(struct inode
*inode
, u64 start
,
3834 unsigned long first_index
;
3835 unsigned long last_index
;
3838 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3839 struct file_ra_state
*ra
;
3840 struct btrfs_ordered_extent
*ordered
;
3841 unsigned int total_read
= 0;
3842 unsigned int total_dirty
= 0;
3845 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
3847 mutex_lock(&inode
->i_mutex
);
3848 first_index
= start
>> PAGE_CACHE_SHIFT
;
3849 last_index
= (start
+ len
- 1) >> PAGE_CACHE_SHIFT
;
3851 /* make sure the dirty trick played by the caller work */
3852 ret
= invalidate_inode_pages2_range(inode
->i_mapping
,
3853 first_index
, last_index
);
3857 file_ra_state_init(ra
, inode
->i_mapping
);
3859 for (i
= first_index
; i
<= last_index
; i
++) {
3860 if (total_read
% ra
->ra_pages
== 0) {
3861 btrfs_force_ra(inode
->i_mapping
, ra
, NULL
, i
,
3862 calc_ra(i
, last_index
, ra
->ra_pages
));
3866 if (((u64
)i
<< PAGE_CACHE_SHIFT
) > i_size_read(inode
))
3868 page
= grab_cache_page(inode
->i_mapping
, i
);
3873 if (!PageUptodate(page
)) {
3874 btrfs_readpage(NULL
, page
);
3876 if (!PageUptodate(page
)) {
3878 page_cache_release(page
);
3883 wait_on_page_writeback(page
);
3885 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
3886 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
3887 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
3889 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
3891 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
3893 page_cache_release(page
);
3894 btrfs_start_ordered_extent(inode
, ordered
, 1);
3895 btrfs_put_ordered_extent(ordered
);
3898 set_page_extent_mapped(page
);
3900 if (i
== first_index
)
3901 set_extent_bits(io_tree
, page_start
, page_end
,
3902 EXTENT_BOUNDARY
, GFP_NOFS
);
3903 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
3905 set_page_dirty(page
);
3908 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
3910 page_cache_release(page
);
3915 mutex_unlock(&inode
->i_mutex
);
3916 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, total_dirty
);
3920 static noinline
int relocate_data_extent(struct inode
*reloc_inode
,
3921 struct btrfs_key
*extent_key
,
3924 struct btrfs_root
*root
= BTRFS_I(reloc_inode
)->root
;
3925 struct extent_map_tree
*em_tree
= &BTRFS_I(reloc_inode
)->extent_tree
;
3926 struct extent_map
*em
;
3927 u64 start
= extent_key
->objectid
- offset
;
3928 u64 end
= start
+ extent_key
->offset
- 1;
3930 em
= alloc_extent_map(GFP_NOFS
);
3931 BUG_ON(!em
|| IS_ERR(em
));
3934 em
->len
= extent_key
->offset
;
3935 em
->block_len
= extent_key
->offset
;
3936 em
->block_start
= extent_key
->objectid
;
3937 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
3938 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
3940 /* setup extent map to cheat btrfs_readpage */
3941 lock_extent(&BTRFS_I(reloc_inode
)->io_tree
, start
, end
, GFP_NOFS
);
3944 spin_lock(&em_tree
->lock
);
3945 ret
= add_extent_mapping(em_tree
, em
);
3946 spin_unlock(&em_tree
->lock
);
3947 if (ret
!= -EEXIST
) {
3948 free_extent_map(em
);
3951 btrfs_drop_extent_cache(reloc_inode
, start
, end
, 0);
3953 unlock_extent(&BTRFS_I(reloc_inode
)->io_tree
, start
, end
, GFP_NOFS
);
3955 return relocate_inode_pages(reloc_inode
, start
, extent_key
->offset
);
3958 struct btrfs_ref_path
{
3960 u64 nodes
[BTRFS_MAX_LEVEL
];
3962 u64 root_generation
;
3969 struct btrfs_key node_keys
[BTRFS_MAX_LEVEL
];
3970 u64 new_nodes
[BTRFS_MAX_LEVEL
];
3973 struct disk_extent
{
3984 static int is_cowonly_root(u64 root_objectid
)
3986 if (root_objectid
== BTRFS_ROOT_TREE_OBJECTID
||
3987 root_objectid
== BTRFS_EXTENT_TREE_OBJECTID
||
3988 root_objectid
== BTRFS_CHUNK_TREE_OBJECTID
||
3989 root_objectid
== BTRFS_DEV_TREE_OBJECTID
||
3990 root_objectid
== BTRFS_TREE_LOG_OBJECTID
||
3991 root_objectid
== BTRFS_CSUM_TREE_OBJECTID
)
3996 static noinline
int __next_ref_path(struct btrfs_trans_handle
*trans
,
3997 struct btrfs_root
*extent_root
,
3998 struct btrfs_ref_path
*ref_path
,
4001 struct extent_buffer
*leaf
;
4002 struct btrfs_path
*path
;
4003 struct btrfs_extent_ref
*ref
;
4004 struct btrfs_key key
;
4005 struct btrfs_key found_key
;
4011 path
= btrfs_alloc_path();
4016 ref_path
->lowest_level
= -1;
4017 ref_path
->current_level
= -1;
4018 ref_path
->shared_level
= -1;
4022 level
= ref_path
->current_level
- 1;
4023 while (level
>= -1) {
4025 if (level
< ref_path
->lowest_level
)
4029 bytenr
= ref_path
->nodes
[level
];
4031 bytenr
= ref_path
->extent_start
;
4032 BUG_ON(bytenr
== 0);
4034 parent
= ref_path
->nodes
[level
+ 1];
4035 ref_path
->nodes
[level
+ 1] = 0;
4036 ref_path
->current_level
= level
;
4037 BUG_ON(parent
== 0);
4039 key
.objectid
= bytenr
;
4040 key
.offset
= parent
+ 1;
4041 key
.type
= BTRFS_EXTENT_REF_KEY
;
4043 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
, 0, 0);
4048 leaf
= path
->nodes
[0];
4049 nritems
= btrfs_header_nritems(leaf
);
4050 if (path
->slots
[0] >= nritems
) {
4051 ret
= btrfs_next_leaf(extent_root
, path
);
4056 leaf
= path
->nodes
[0];
4059 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4060 if (found_key
.objectid
== bytenr
&&
4061 found_key
.type
== BTRFS_EXTENT_REF_KEY
) {
4062 if (level
< ref_path
->shared_level
)
4063 ref_path
->shared_level
= level
;
4068 btrfs_release_path(extent_root
, path
);
4071 /* reached lowest level */
4075 level
= ref_path
->current_level
;
4076 while (level
< BTRFS_MAX_LEVEL
- 1) {
4080 bytenr
= ref_path
->nodes
[level
];
4082 bytenr
= ref_path
->extent_start
;
4084 BUG_ON(bytenr
== 0);
4086 key
.objectid
= bytenr
;
4088 key
.type
= BTRFS_EXTENT_REF_KEY
;
4090 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
, 0, 0);
4094 leaf
= path
->nodes
[0];
4095 nritems
= btrfs_header_nritems(leaf
);
4096 if (path
->slots
[0] >= nritems
) {
4097 ret
= btrfs_next_leaf(extent_root
, path
);
4101 /* the extent was freed by someone */
4102 if (ref_path
->lowest_level
== level
)
4104 btrfs_release_path(extent_root
, path
);
4107 leaf
= path
->nodes
[0];
4110 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4111 if (found_key
.objectid
!= bytenr
||
4112 found_key
.type
!= BTRFS_EXTENT_REF_KEY
) {
4113 /* the extent was freed by someone */
4114 if (ref_path
->lowest_level
== level
) {
4118 btrfs_release_path(extent_root
, path
);
4122 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
4123 struct btrfs_extent_ref
);
4124 ref_objectid
= btrfs_ref_objectid(leaf
, ref
);
4125 if (ref_objectid
< BTRFS_FIRST_FREE_OBJECTID
) {
4127 level
= (int)ref_objectid
;
4128 BUG_ON(level
>= BTRFS_MAX_LEVEL
);
4129 ref_path
->lowest_level
= level
;
4130 ref_path
->current_level
= level
;
4131 ref_path
->nodes
[level
] = bytenr
;
4133 WARN_ON(ref_objectid
!= level
);
4136 WARN_ON(level
!= -1);
4140 if (ref_path
->lowest_level
== level
) {
4141 ref_path
->owner_objectid
= ref_objectid
;
4142 ref_path
->num_refs
= btrfs_ref_num_refs(leaf
, ref
);
4146 * the block is tree root or the block isn't in reference
4149 if (found_key
.objectid
== found_key
.offset
||
4150 is_cowonly_root(btrfs_ref_root(leaf
, ref
))) {
4151 ref_path
->root_objectid
= btrfs_ref_root(leaf
, ref
);
4152 ref_path
->root_generation
=
4153 btrfs_ref_generation(leaf
, ref
);
4155 /* special reference from the tree log */
4156 ref_path
->nodes
[0] = found_key
.offset
;
4157 ref_path
->current_level
= 0;
4164 BUG_ON(ref_path
->nodes
[level
] != 0);
4165 ref_path
->nodes
[level
] = found_key
.offset
;
4166 ref_path
->current_level
= level
;
4169 * the reference was created in the running transaction,
4170 * no need to continue walking up.
4172 if (btrfs_ref_generation(leaf
, ref
) == trans
->transid
) {
4173 ref_path
->root_objectid
= btrfs_ref_root(leaf
, ref
);
4174 ref_path
->root_generation
=
4175 btrfs_ref_generation(leaf
, ref
);
4180 btrfs_release_path(extent_root
, path
);
4183 /* reached max tree level, but no tree root found. */
4186 btrfs_free_path(path
);
4190 static int btrfs_first_ref_path(struct btrfs_trans_handle
*trans
,
4191 struct btrfs_root
*extent_root
,
4192 struct btrfs_ref_path
*ref_path
,
4195 memset(ref_path
, 0, sizeof(*ref_path
));
4196 ref_path
->extent_start
= extent_start
;
4198 return __next_ref_path(trans
, extent_root
, ref_path
, 1);
4201 static int btrfs_next_ref_path(struct btrfs_trans_handle
*trans
,
4202 struct btrfs_root
*extent_root
,
4203 struct btrfs_ref_path
*ref_path
)
4205 return __next_ref_path(trans
, extent_root
, ref_path
, 0);
4208 static noinline
int get_new_locations(struct inode
*reloc_inode
,
4209 struct btrfs_key
*extent_key
,
4210 u64 offset
, int no_fragment
,
4211 struct disk_extent
**extents
,
4214 struct btrfs_root
*root
= BTRFS_I(reloc_inode
)->root
;
4215 struct btrfs_path
*path
;
4216 struct btrfs_file_extent_item
*fi
;
4217 struct extent_buffer
*leaf
;
4218 struct disk_extent
*exts
= *extents
;
4219 struct btrfs_key found_key
;
4224 int max
= *nr_extents
;
4227 WARN_ON(!no_fragment
&& *extents
);
4230 exts
= kmalloc(sizeof(*exts
) * max
, GFP_NOFS
);
4235 path
= btrfs_alloc_path();
4238 cur_pos
= extent_key
->objectid
- offset
;
4239 last_byte
= extent_key
->objectid
+ extent_key
->offset
;
4240 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, reloc_inode
->i_ino
,
4250 leaf
= path
->nodes
[0];
4251 nritems
= btrfs_header_nritems(leaf
);
4252 if (path
->slots
[0] >= nritems
) {
4253 ret
= btrfs_next_leaf(root
, path
);
4258 leaf
= path
->nodes
[0];
4261 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4262 if (found_key
.offset
!= cur_pos
||
4263 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
||
4264 found_key
.objectid
!= reloc_inode
->i_ino
)
4267 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
4268 struct btrfs_file_extent_item
);
4269 if (btrfs_file_extent_type(leaf
, fi
) !=
4270 BTRFS_FILE_EXTENT_REG
||
4271 btrfs_file_extent_disk_bytenr(leaf
, fi
) == 0)
4275 struct disk_extent
*old
= exts
;
4277 exts
= kzalloc(sizeof(*exts
) * max
, GFP_NOFS
);
4278 memcpy(exts
, old
, sizeof(*exts
) * nr
);
4279 if (old
!= *extents
)
4283 exts
[nr
].disk_bytenr
=
4284 btrfs_file_extent_disk_bytenr(leaf
, fi
);
4285 exts
[nr
].disk_num_bytes
=
4286 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
4287 exts
[nr
].offset
= btrfs_file_extent_offset(leaf
, fi
);
4288 exts
[nr
].num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
4289 exts
[nr
].ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
4290 exts
[nr
].compression
= btrfs_file_extent_compression(leaf
, fi
);
4291 exts
[nr
].encryption
= btrfs_file_extent_encryption(leaf
, fi
);
4292 exts
[nr
].other_encoding
= btrfs_file_extent_other_encoding(leaf
,
4294 BUG_ON(exts
[nr
].offset
> 0);
4295 BUG_ON(exts
[nr
].compression
|| exts
[nr
].encryption
);
4296 BUG_ON(exts
[nr
].num_bytes
!= exts
[nr
].disk_num_bytes
);
4298 cur_pos
+= exts
[nr
].num_bytes
;
4301 if (cur_pos
+ offset
>= last_byte
)
4311 BUG_ON(cur_pos
+ offset
> last_byte
);
4312 if (cur_pos
+ offset
< last_byte
) {
4318 btrfs_free_path(path
);
4320 if (exts
!= *extents
)
4329 static noinline
int replace_one_extent(struct btrfs_trans_handle
*trans
,
4330 struct btrfs_root
*root
,
4331 struct btrfs_path
*path
,
4332 struct btrfs_key
*extent_key
,
4333 struct btrfs_key
*leaf_key
,
4334 struct btrfs_ref_path
*ref_path
,
4335 struct disk_extent
*new_extents
,
4338 struct extent_buffer
*leaf
;
4339 struct btrfs_file_extent_item
*fi
;
4340 struct inode
*inode
= NULL
;
4341 struct btrfs_key key
;
4346 u64 search_end
= (u64
)-1;
4349 int extent_locked
= 0;
4353 memcpy(&key
, leaf_key
, sizeof(key
));
4354 if (ref_path
->owner_objectid
!= BTRFS_MULTIPLE_OBJECTIDS
) {
4355 if (key
.objectid
< ref_path
->owner_objectid
||
4356 (key
.objectid
== ref_path
->owner_objectid
&&
4357 key
.type
< BTRFS_EXTENT_DATA_KEY
)) {
4358 key
.objectid
= ref_path
->owner_objectid
;
4359 key
.type
= BTRFS_EXTENT_DATA_KEY
;
4365 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
4369 leaf
= path
->nodes
[0];
4370 nritems
= btrfs_header_nritems(leaf
);
4372 if (extent_locked
&& ret
> 0) {
4374 * the file extent item was modified by someone
4375 * before the extent got locked.
4377 unlock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
4378 lock_end
, GFP_NOFS
);
4382 if (path
->slots
[0] >= nritems
) {
4383 if (++nr_scaned
> 2)
4386 BUG_ON(extent_locked
);
4387 ret
= btrfs_next_leaf(root
, path
);
4392 leaf
= path
->nodes
[0];
4393 nritems
= btrfs_header_nritems(leaf
);
4396 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
4398 if (ref_path
->owner_objectid
!= BTRFS_MULTIPLE_OBJECTIDS
) {
4399 if ((key
.objectid
> ref_path
->owner_objectid
) ||
4400 (key
.objectid
== ref_path
->owner_objectid
&&
4401 key
.type
> BTRFS_EXTENT_DATA_KEY
) ||
4402 key
.offset
>= search_end
)
4406 if (inode
&& key
.objectid
!= inode
->i_ino
) {
4407 BUG_ON(extent_locked
);
4408 btrfs_release_path(root
, path
);
4409 mutex_unlock(&inode
->i_mutex
);
4415 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
4420 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
4421 struct btrfs_file_extent_item
);
4422 extent_type
= btrfs_file_extent_type(leaf
, fi
);
4423 if ((extent_type
!= BTRFS_FILE_EXTENT_REG
&&
4424 extent_type
!= BTRFS_FILE_EXTENT_PREALLOC
) ||
4425 (btrfs_file_extent_disk_bytenr(leaf
, fi
) !=
4426 extent_key
->objectid
)) {
4432 num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
4433 ext_offset
= btrfs_file_extent_offset(leaf
, fi
);
4435 if (search_end
== (u64
)-1) {
4436 search_end
= key
.offset
- ext_offset
+
4437 btrfs_file_extent_ram_bytes(leaf
, fi
);
4440 if (!extent_locked
) {
4441 lock_start
= key
.offset
;
4442 lock_end
= lock_start
+ num_bytes
- 1;
4444 if (lock_start
> key
.offset
||
4445 lock_end
+ 1 < key
.offset
+ num_bytes
) {
4446 unlock_extent(&BTRFS_I(inode
)->io_tree
,
4447 lock_start
, lock_end
, GFP_NOFS
);
4453 btrfs_release_path(root
, path
);
4455 inode
= btrfs_iget_locked(root
->fs_info
->sb
,
4456 key
.objectid
, root
);
4457 if (inode
->i_state
& I_NEW
) {
4458 BTRFS_I(inode
)->root
= root
;
4459 BTRFS_I(inode
)->location
.objectid
=
4461 BTRFS_I(inode
)->location
.type
=
4462 BTRFS_INODE_ITEM_KEY
;
4463 BTRFS_I(inode
)->location
.offset
= 0;
4464 btrfs_read_locked_inode(inode
);
4465 unlock_new_inode(inode
);
4468 * some code call btrfs_commit_transaction while
4469 * holding the i_mutex, so we can't use mutex_lock
4472 if (is_bad_inode(inode
) ||
4473 !mutex_trylock(&inode
->i_mutex
)) {
4476 key
.offset
= (u64
)-1;
4481 if (!extent_locked
) {
4482 struct btrfs_ordered_extent
*ordered
;
4484 btrfs_release_path(root
, path
);
4486 lock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
4487 lock_end
, GFP_NOFS
);
4488 ordered
= btrfs_lookup_first_ordered_extent(inode
,
4491 ordered
->file_offset
<= lock_end
&&
4492 ordered
->file_offset
+ ordered
->len
> lock_start
) {
4493 unlock_extent(&BTRFS_I(inode
)->io_tree
,
4494 lock_start
, lock_end
, GFP_NOFS
);
4495 btrfs_start_ordered_extent(inode
, ordered
, 1);
4496 btrfs_put_ordered_extent(ordered
);
4497 key
.offset
+= num_bytes
;
4501 btrfs_put_ordered_extent(ordered
);
4507 if (nr_extents
== 1) {
4508 /* update extent pointer in place */
4509 btrfs_set_file_extent_disk_bytenr(leaf
, fi
,
4510 new_extents
[0].disk_bytenr
);
4511 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
,
4512 new_extents
[0].disk_num_bytes
);
4513 btrfs_mark_buffer_dirty(leaf
);
4515 btrfs_drop_extent_cache(inode
, key
.offset
,
4516 key
.offset
+ num_bytes
- 1, 0);
4518 ret
= btrfs_inc_extent_ref(trans
, root
,
4519 new_extents
[0].disk_bytenr
,
4520 new_extents
[0].disk_num_bytes
,
4522 root
->root_key
.objectid
,
4527 ret
= btrfs_free_extent(trans
, root
,
4528 extent_key
->objectid
,
4531 btrfs_header_owner(leaf
),
4532 btrfs_header_generation(leaf
),
4536 btrfs_release_path(root
, path
);
4537 key
.offset
+= num_bytes
;
4545 * drop old extent pointer at first, then insert the
4546 * new pointers one bye one
4548 btrfs_release_path(root
, path
);
4549 ret
= btrfs_drop_extents(trans
, root
, inode
, key
.offset
,
4550 key
.offset
+ num_bytes
,
4551 key
.offset
, &alloc_hint
);
4554 for (i
= 0; i
< nr_extents
; i
++) {
4555 if (ext_offset
>= new_extents
[i
].num_bytes
) {
4556 ext_offset
-= new_extents
[i
].num_bytes
;
4559 extent_len
= min(new_extents
[i
].num_bytes
-
4560 ext_offset
, num_bytes
);
4562 ret
= btrfs_insert_empty_item(trans
, root
,
4567 leaf
= path
->nodes
[0];
4568 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
4569 struct btrfs_file_extent_item
);
4570 btrfs_set_file_extent_generation(leaf
, fi
,
4572 btrfs_set_file_extent_type(leaf
, fi
,
4573 BTRFS_FILE_EXTENT_REG
);
4574 btrfs_set_file_extent_disk_bytenr(leaf
, fi
,
4575 new_extents
[i
].disk_bytenr
);
4576 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
,
4577 new_extents
[i
].disk_num_bytes
);
4578 btrfs_set_file_extent_ram_bytes(leaf
, fi
,
4579 new_extents
[i
].ram_bytes
);
4581 btrfs_set_file_extent_compression(leaf
, fi
,
4582 new_extents
[i
].compression
);
4583 btrfs_set_file_extent_encryption(leaf
, fi
,
4584 new_extents
[i
].encryption
);
4585 btrfs_set_file_extent_other_encoding(leaf
, fi
,
4586 new_extents
[i
].other_encoding
);
4588 btrfs_set_file_extent_num_bytes(leaf
, fi
,
4590 ext_offset
+= new_extents
[i
].offset
;
4591 btrfs_set_file_extent_offset(leaf
, fi
,
4593 btrfs_mark_buffer_dirty(leaf
);
4595 btrfs_drop_extent_cache(inode
, key
.offset
,
4596 key
.offset
+ extent_len
- 1, 0);
4598 ret
= btrfs_inc_extent_ref(trans
, root
,
4599 new_extents
[i
].disk_bytenr
,
4600 new_extents
[i
].disk_num_bytes
,
4602 root
->root_key
.objectid
,
4603 trans
->transid
, key
.objectid
);
4605 btrfs_release_path(root
, path
);
4607 inode_add_bytes(inode
, extent_len
);
4610 num_bytes
-= extent_len
;
4611 key
.offset
+= extent_len
;
4616 BUG_ON(i
>= nr_extents
);
4620 if (extent_locked
) {
4621 unlock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
4622 lock_end
, GFP_NOFS
);
4626 if (ref_path
->owner_objectid
!= BTRFS_MULTIPLE_OBJECTIDS
&&
4627 key
.offset
>= search_end
)
4634 btrfs_release_path(root
, path
);
4636 mutex_unlock(&inode
->i_mutex
);
4637 if (extent_locked
) {
4638 unlock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
4639 lock_end
, GFP_NOFS
);
4646 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle
*trans
,
4647 struct btrfs_root
*root
,
4648 struct extent_buffer
*buf
, u64 orig_start
)
4653 BUG_ON(btrfs_header_generation(buf
) != trans
->transid
);
4654 BUG_ON(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
4656 level
= btrfs_header_level(buf
);
4658 struct btrfs_leaf_ref
*ref
;
4659 struct btrfs_leaf_ref
*orig_ref
;
4661 orig_ref
= btrfs_lookup_leaf_ref(root
, orig_start
);
4665 ref
= btrfs_alloc_leaf_ref(root
, orig_ref
->nritems
);
4667 btrfs_free_leaf_ref(root
, orig_ref
);
4671 ref
->nritems
= orig_ref
->nritems
;
4672 memcpy(ref
->extents
, orig_ref
->extents
,
4673 sizeof(ref
->extents
[0]) * ref
->nritems
);
4675 btrfs_free_leaf_ref(root
, orig_ref
);
4677 ref
->root_gen
= trans
->transid
;
4678 ref
->bytenr
= buf
->start
;
4679 ref
->owner
= btrfs_header_owner(buf
);
4680 ref
->generation
= btrfs_header_generation(buf
);
4682 ret
= btrfs_add_leaf_ref(root
, ref
, 0);
4684 btrfs_free_leaf_ref(root
, ref
);
4689 static noinline
int invalidate_extent_cache(struct btrfs_root
*root
,
4690 struct extent_buffer
*leaf
,
4691 struct btrfs_block_group_cache
*group
,
4692 struct btrfs_root
*target_root
)
4694 struct btrfs_key key
;
4695 struct inode
*inode
= NULL
;
4696 struct btrfs_file_extent_item
*fi
;
4698 u64 skip_objectid
= 0;
4702 nritems
= btrfs_header_nritems(leaf
);
4703 for (i
= 0; i
< nritems
; i
++) {
4704 btrfs_item_key_to_cpu(leaf
, &key
, i
);
4705 if (key
.objectid
== skip_objectid
||
4706 key
.type
!= BTRFS_EXTENT_DATA_KEY
)
4708 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
4709 if (btrfs_file_extent_type(leaf
, fi
) ==
4710 BTRFS_FILE_EXTENT_INLINE
)
4712 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) == 0)
4714 if (!inode
|| inode
->i_ino
!= key
.objectid
) {
4716 inode
= btrfs_ilookup(target_root
->fs_info
->sb
,
4717 key
.objectid
, target_root
, 1);
4720 skip_objectid
= key
.objectid
;
4723 num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
4725 lock_extent(&BTRFS_I(inode
)->io_tree
, key
.offset
,
4726 key
.offset
+ num_bytes
- 1, GFP_NOFS
);
4727 btrfs_drop_extent_cache(inode
, key
.offset
,
4728 key
.offset
+ num_bytes
- 1, 1);
4729 unlock_extent(&BTRFS_I(inode
)->io_tree
, key
.offset
,
4730 key
.offset
+ num_bytes
- 1, GFP_NOFS
);
4737 static noinline
int replace_extents_in_leaf(struct btrfs_trans_handle
*trans
,
4738 struct btrfs_root
*root
,
4739 struct extent_buffer
*leaf
,
4740 struct btrfs_block_group_cache
*group
,
4741 struct inode
*reloc_inode
)
4743 struct btrfs_key key
;
4744 struct btrfs_key extent_key
;
4745 struct btrfs_file_extent_item
*fi
;
4746 struct btrfs_leaf_ref
*ref
;
4747 struct disk_extent
*new_extent
;
4756 new_extent
= kmalloc(sizeof(*new_extent
), GFP_NOFS
);
4757 BUG_ON(!new_extent
);
4759 ref
= btrfs_lookup_leaf_ref(root
, leaf
->start
);
4763 nritems
= btrfs_header_nritems(leaf
);
4764 for (i
= 0; i
< nritems
; i
++) {
4765 btrfs_item_key_to_cpu(leaf
, &key
, i
);
4766 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
4768 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
4769 if (btrfs_file_extent_type(leaf
, fi
) ==
4770 BTRFS_FILE_EXTENT_INLINE
)
4772 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
4773 num_bytes
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
4778 if (bytenr
>= group
->key
.objectid
+ group
->key
.offset
||
4779 bytenr
+ num_bytes
<= group
->key
.objectid
)
4782 extent_key
.objectid
= bytenr
;
4783 extent_key
.offset
= num_bytes
;
4784 extent_key
.type
= BTRFS_EXTENT_ITEM_KEY
;
4786 ret
= get_new_locations(reloc_inode
, &extent_key
,
4787 group
->key
.objectid
, 1,
4788 &new_extent
, &nr_extent
);
4793 BUG_ON(ref
->extents
[ext_index
].bytenr
!= bytenr
);
4794 BUG_ON(ref
->extents
[ext_index
].num_bytes
!= num_bytes
);
4795 ref
->extents
[ext_index
].bytenr
= new_extent
->disk_bytenr
;
4796 ref
->extents
[ext_index
].num_bytes
= new_extent
->disk_num_bytes
;
4798 btrfs_set_file_extent_disk_bytenr(leaf
, fi
,
4799 new_extent
->disk_bytenr
);
4800 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
,
4801 new_extent
->disk_num_bytes
);
4802 btrfs_mark_buffer_dirty(leaf
);
4804 ret
= btrfs_inc_extent_ref(trans
, root
,
4805 new_extent
->disk_bytenr
,
4806 new_extent
->disk_num_bytes
,
4808 root
->root_key
.objectid
,
4809 trans
->transid
, key
.objectid
);
4812 ret
= btrfs_free_extent(trans
, root
,
4813 bytenr
, num_bytes
, leaf
->start
,
4814 btrfs_header_owner(leaf
),
4815 btrfs_header_generation(leaf
),
4821 BUG_ON(ext_index
+ 1 != ref
->nritems
);
4822 btrfs_free_leaf_ref(root
, ref
);
4826 int btrfs_free_reloc_root(struct btrfs_trans_handle
*trans
,
4827 struct btrfs_root
*root
)
4829 struct btrfs_root
*reloc_root
;
4832 if (root
->reloc_root
) {
4833 reloc_root
= root
->reloc_root
;
4834 root
->reloc_root
= NULL
;
4835 list_add(&reloc_root
->dead_list
,
4836 &root
->fs_info
->dead_reloc_roots
);
4838 btrfs_set_root_bytenr(&reloc_root
->root_item
,
4839 reloc_root
->node
->start
);
4840 btrfs_set_root_level(&root
->root_item
,
4841 btrfs_header_level(reloc_root
->node
));
4842 memset(&reloc_root
->root_item
.drop_progress
, 0,
4843 sizeof(struct btrfs_disk_key
));
4844 reloc_root
->root_item
.drop_level
= 0;
4846 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
4847 &reloc_root
->root_key
,
4848 &reloc_root
->root_item
);
4854 int btrfs_drop_dead_reloc_roots(struct btrfs_root
*root
)
4856 struct btrfs_trans_handle
*trans
;
4857 struct btrfs_root
*reloc_root
;
4858 struct btrfs_root
*prev_root
= NULL
;
4859 struct list_head dead_roots
;
4863 INIT_LIST_HEAD(&dead_roots
);
4864 list_splice_init(&root
->fs_info
->dead_reloc_roots
, &dead_roots
);
4866 while (!list_empty(&dead_roots
)) {
4867 reloc_root
= list_entry(dead_roots
.prev
,
4868 struct btrfs_root
, dead_list
);
4869 list_del_init(&reloc_root
->dead_list
);
4871 BUG_ON(reloc_root
->commit_root
!= NULL
);
4873 trans
= btrfs_join_transaction(root
, 1);
4876 mutex_lock(&root
->fs_info
->drop_mutex
);
4877 ret
= btrfs_drop_snapshot(trans
, reloc_root
);
4880 mutex_unlock(&root
->fs_info
->drop_mutex
);
4882 nr
= trans
->blocks_used
;
4883 ret
= btrfs_end_transaction(trans
, root
);
4885 btrfs_btree_balance_dirty(root
, nr
);
4888 free_extent_buffer(reloc_root
->node
);
4890 ret
= btrfs_del_root(trans
, root
->fs_info
->tree_root
,
4891 &reloc_root
->root_key
);
4893 mutex_unlock(&root
->fs_info
->drop_mutex
);
4895 nr
= trans
->blocks_used
;
4896 ret
= btrfs_end_transaction(trans
, root
);
4898 btrfs_btree_balance_dirty(root
, nr
);
4901 prev_root
= reloc_root
;
4904 btrfs_remove_leaf_refs(prev_root
, (u64
)-1, 0);
4910 int btrfs_add_dead_reloc_root(struct btrfs_root
*root
)
4912 list_add(&root
->dead_list
, &root
->fs_info
->dead_reloc_roots
);
4916 int btrfs_cleanup_reloc_trees(struct btrfs_root
*root
)
4918 struct btrfs_root
*reloc_root
;
4919 struct btrfs_trans_handle
*trans
;
4920 struct btrfs_key location
;
4924 mutex_lock(&root
->fs_info
->tree_reloc_mutex
);
4925 ret
= btrfs_find_dead_roots(root
, BTRFS_TREE_RELOC_OBJECTID
, NULL
);
4927 found
= !list_empty(&root
->fs_info
->dead_reloc_roots
);
4928 mutex_unlock(&root
->fs_info
->tree_reloc_mutex
);
4931 trans
= btrfs_start_transaction(root
, 1);
4933 ret
= btrfs_commit_transaction(trans
, root
);
4937 location
.objectid
= BTRFS_DATA_RELOC_TREE_OBJECTID
;
4938 location
.offset
= (u64
)-1;
4939 location
.type
= BTRFS_ROOT_ITEM_KEY
;
4941 reloc_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
4942 BUG_ON(!reloc_root
);
4943 btrfs_orphan_cleanup(reloc_root
);
4947 static noinline
int init_reloc_tree(struct btrfs_trans_handle
*trans
,
4948 struct btrfs_root
*root
)
4950 struct btrfs_root
*reloc_root
;
4951 struct extent_buffer
*eb
;
4952 struct btrfs_root_item
*root_item
;
4953 struct btrfs_key root_key
;
4956 BUG_ON(!root
->ref_cows
);
4957 if (root
->reloc_root
)
4960 root_item
= kmalloc(sizeof(*root_item
), GFP_NOFS
);
4963 ret
= btrfs_copy_root(trans
, root
, root
->commit_root
,
4964 &eb
, BTRFS_TREE_RELOC_OBJECTID
);
4967 root_key
.objectid
= BTRFS_TREE_RELOC_OBJECTID
;
4968 root_key
.offset
= root
->root_key
.objectid
;
4969 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
4971 memcpy(root_item
, &root
->root_item
, sizeof(root_item
));
4972 btrfs_set_root_refs(root_item
, 0);
4973 btrfs_set_root_bytenr(root_item
, eb
->start
);
4974 btrfs_set_root_level(root_item
, btrfs_header_level(eb
));
4975 btrfs_set_root_generation(root_item
, trans
->transid
);
4977 btrfs_tree_unlock(eb
);
4978 free_extent_buffer(eb
);
4980 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
,
4981 &root_key
, root_item
);
4985 reloc_root
= btrfs_read_fs_root_no_radix(root
->fs_info
->tree_root
,
4987 BUG_ON(!reloc_root
);
4988 reloc_root
->last_trans
= trans
->transid
;
4989 reloc_root
->commit_root
= NULL
;
4990 reloc_root
->ref_tree
= &root
->fs_info
->reloc_ref_tree
;
4992 root
->reloc_root
= reloc_root
;
4997 * Core function of space balance.
4999 * The idea is using reloc trees to relocate tree blocks in reference
5000 * counted roots. There is one reloc tree for each subvol, and all
5001 * reloc trees share same root key objectid. Reloc trees are snapshots
5002 * of the latest committed roots of subvols (root->commit_root).
5004 * To relocate a tree block referenced by a subvol, there are two steps.
5005 * COW the block through subvol's reloc tree, then update block pointer
5006 * in the subvol to point to the new block. Since all reloc trees share
5007 * same root key objectid, doing special handing for tree blocks owned
5008 * by them is easy. Once a tree block has been COWed in one reloc tree,
5009 * we can use the resulting new block directly when the same block is
5010 * required to COW again through other reloc trees. By this way, relocated
5011 * tree blocks are shared between reloc trees, so they are also shared
5014 static noinline
int relocate_one_path(struct btrfs_trans_handle
*trans
,
5015 struct btrfs_root
*root
,
5016 struct btrfs_path
*path
,
5017 struct btrfs_key
*first_key
,
5018 struct btrfs_ref_path
*ref_path
,
5019 struct btrfs_block_group_cache
*group
,
5020 struct inode
*reloc_inode
)
5022 struct btrfs_root
*reloc_root
;
5023 struct extent_buffer
*eb
= NULL
;
5024 struct btrfs_key
*keys
;
5028 int lowest_level
= 0;
5031 if (ref_path
->owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
)
5032 lowest_level
= ref_path
->owner_objectid
;
5034 if (!root
->ref_cows
) {
5035 path
->lowest_level
= lowest_level
;
5036 ret
= btrfs_search_slot(trans
, root
, first_key
, path
, 0, 1);
5038 path
->lowest_level
= 0;
5039 btrfs_release_path(root
, path
);
5043 mutex_lock(&root
->fs_info
->tree_reloc_mutex
);
5044 ret
= init_reloc_tree(trans
, root
);
5046 reloc_root
= root
->reloc_root
;
5048 shared_level
= ref_path
->shared_level
;
5049 ref_path
->shared_level
= BTRFS_MAX_LEVEL
- 1;
5051 keys
= ref_path
->node_keys
;
5052 nodes
= ref_path
->new_nodes
;
5053 memset(&keys
[shared_level
+ 1], 0,
5054 sizeof(*keys
) * (BTRFS_MAX_LEVEL
- shared_level
- 1));
5055 memset(&nodes
[shared_level
+ 1], 0,
5056 sizeof(*nodes
) * (BTRFS_MAX_LEVEL
- shared_level
- 1));
5058 if (nodes
[lowest_level
] == 0) {
5059 path
->lowest_level
= lowest_level
;
5060 ret
= btrfs_search_slot(trans
, reloc_root
, first_key
, path
,
5063 for (level
= lowest_level
; level
< BTRFS_MAX_LEVEL
; level
++) {
5064 eb
= path
->nodes
[level
];
5065 if (!eb
|| eb
== reloc_root
->node
)
5067 nodes
[level
] = eb
->start
;
5069 btrfs_item_key_to_cpu(eb
, &keys
[level
], 0);
5071 btrfs_node_key_to_cpu(eb
, &keys
[level
], 0);
5074 ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
5075 eb
= path
->nodes
[0];
5076 ret
= replace_extents_in_leaf(trans
, reloc_root
, eb
,
5077 group
, reloc_inode
);
5080 btrfs_release_path(reloc_root
, path
);
5082 ret
= btrfs_merge_path(trans
, reloc_root
, keys
, nodes
,
5088 * replace tree blocks in the fs tree with tree blocks in
5091 ret
= btrfs_merge_path(trans
, root
, keys
, nodes
, lowest_level
);
5094 if (ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
5095 ret
= btrfs_search_slot(trans
, reloc_root
, first_key
, path
,
5098 extent_buffer_get(path
->nodes
[0]);
5099 eb
= path
->nodes
[0];
5100 btrfs_release_path(reloc_root
, path
);
5101 ret
= invalidate_extent_cache(reloc_root
, eb
, group
, root
);
5103 free_extent_buffer(eb
);
5106 mutex_unlock(&root
->fs_info
->tree_reloc_mutex
);
5107 path
->lowest_level
= 0;
5111 static noinline
int relocate_tree_block(struct btrfs_trans_handle
*trans
,
5112 struct btrfs_root
*root
,
5113 struct btrfs_path
*path
,
5114 struct btrfs_key
*first_key
,
5115 struct btrfs_ref_path
*ref_path
)
5119 ret
= relocate_one_path(trans
, root
, path
, first_key
,
5120 ref_path
, NULL
, NULL
);
5126 static noinline
int del_extent_zero(struct btrfs_trans_handle
*trans
,
5127 struct btrfs_root
*extent_root
,
5128 struct btrfs_path
*path
,
5129 struct btrfs_key
*extent_key
)
5133 ret
= btrfs_search_slot(trans
, extent_root
, extent_key
, path
, -1, 1);
5136 ret
= btrfs_del_item(trans
, extent_root
, path
);
5138 btrfs_release_path(extent_root
, path
);
5142 static noinline
struct btrfs_root
*read_ref_root(struct btrfs_fs_info
*fs_info
,
5143 struct btrfs_ref_path
*ref_path
)
5145 struct btrfs_key root_key
;
5147 root_key
.objectid
= ref_path
->root_objectid
;
5148 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
5149 if (is_cowonly_root(ref_path
->root_objectid
))
5150 root_key
.offset
= 0;
5152 root_key
.offset
= (u64
)-1;
5154 return btrfs_read_fs_root_no_name(fs_info
, &root_key
);
5157 static noinline
int relocate_one_extent(struct btrfs_root
*extent_root
,
5158 struct btrfs_path
*path
,
5159 struct btrfs_key
*extent_key
,
5160 struct btrfs_block_group_cache
*group
,
5161 struct inode
*reloc_inode
, int pass
)
5163 struct btrfs_trans_handle
*trans
;
5164 struct btrfs_root
*found_root
;
5165 struct btrfs_ref_path
*ref_path
= NULL
;
5166 struct disk_extent
*new_extents
= NULL
;
5171 struct btrfs_key first_key
;
5175 trans
= btrfs_start_transaction(extent_root
, 1);
5178 if (extent_key
->objectid
== 0) {
5179 ret
= del_extent_zero(trans
, extent_root
, path
, extent_key
);
5183 ref_path
= kmalloc(sizeof(*ref_path
), GFP_NOFS
);
5189 for (loops
= 0; ; loops
++) {
5191 ret
= btrfs_first_ref_path(trans
, extent_root
, ref_path
,
5192 extent_key
->objectid
);
5194 ret
= btrfs_next_ref_path(trans
, extent_root
, ref_path
);
5201 if (ref_path
->root_objectid
== BTRFS_TREE_LOG_OBJECTID
||
5202 ref_path
->root_objectid
== BTRFS_TREE_RELOC_OBJECTID
)
5205 found_root
= read_ref_root(extent_root
->fs_info
, ref_path
);
5206 BUG_ON(!found_root
);
5208 * for reference counted tree, only process reference paths
5209 * rooted at the latest committed root.
5211 if (found_root
->ref_cows
&&
5212 ref_path
->root_generation
!= found_root
->root_key
.offset
)
5215 if (ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
5218 * copy data extents to new locations
5220 u64 group_start
= group
->key
.objectid
;
5221 ret
= relocate_data_extent(reloc_inode
,
5230 level
= ref_path
->owner_objectid
;
5233 if (prev_block
!= ref_path
->nodes
[level
]) {
5234 struct extent_buffer
*eb
;
5235 u64 block_start
= ref_path
->nodes
[level
];
5236 u64 block_size
= btrfs_level_size(found_root
, level
);
5238 eb
= read_tree_block(found_root
, block_start
,
5240 btrfs_tree_lock(eb
);
5241 BUG_ON(level
!= btrfs_header_level(eb
));
5244 btrfs_item_key_to_cpu(eb
, &first_key
, 0);
5246 btrfs_node_key_to_cpu(eb
, &first_key
, 0);
5248 btrfs_tree_unlock(eb
);
5249 free_extent_buffer(eb
);
5250 prev_block
= block_start
;
5253 mutex_lock(&extent_root
->fs_info
->trans_mutex
);
5254 btrfs_record_root_in_trans(found_root
);
5255 mutex_unlock(&extent_root
->fs_info
->trans_mutex
);
5256 if (ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
5258 * try to update data extent references while
5259 * keeping metadata shared between snapshots.
5262 ret
= relocate_one_path(trans
, found_root
,
5263 path
, &first_key
, ref_path
,
5264 group
, reloc_inode
);
5270 * use fallback method to process the remaining
5274 u64 group_start
= group
->key
.objectid
;
5275 new_extents
= kmalloc(sizeof(*new_extents
),
5278 ret
= get_new_locations(reloc_inode
,
5286 ret
= replace_one_extent(trans
, found_root
,
5288 &first_key
, ref_path
,
5289 new_extents
, nr_extents
);
5291 ret
= relocate_tree_block(trans
, found_root
, path
,
5292 &first_key
, ref_path
);
5299 btrfs_end_transaction(trans
, extent_root
);
5305 static u64
update_block_group_flags(struct btrfs_root
*root
, u64 flags
)
5308 u64 stripped
= BTRFS_BLOCK_GROUP_RAID0
|
5309 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
;
5311 num_devices
= root
->fs_info
->fs_devices
->rw_devices
;
5312 if (num_devices
== 1) {
5313 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
5314 stripped
= flags
& ~stripped
;
5316 /* turn raid0 into single device chunks */
5317 if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
5320 /* turn mirroring into duplication */
5321 if (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
5322 BTRFS_BLOCK_GROUP_RAID10
))
5323 return stripped
| BTRFS_BLOCK_GROUP_DUP
;
5326 /* they already had raid on here, just return */
5327 if (flags
& stripped
)
5330 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
5331 stripped
= flags
& ~stripped
;
5333 /* switch duplicated blocks with raid1 */
5334 if (flags
& BTRFS_BLOCK_GROUP_DUP
)
5335 return stripped
| BTRFS_BLOCK_GROUP_RAID1
;
5337 /* turn single device chunks into raid0 */
5338 return stripped
| BTRFS_BLOCK_GROUP_RAID0
;
5343 static int __alloc_chunk_for_shrink(struct btrfs_root
*root
,
5344 struct btrfs_block_group_cache
*shrink_block_group
,
5347 struct btrfs_trans_handle
*trans
;
5348 u64 new_alloc_flags
;
5351 spin_lock(&shrink_block_group
->lock
);
5352 if (btrfs_block_group_used(&shrink_block_group
->item
) > 0) {
5353 spin_unlock(&shrink_block_group
->lock
);
5355 trans
= btrfs_start_transaction(root
, 1);
5356 spin_lock(&shrink_block_group
->lock
);
5358 new_alloc_flags
= update_block_group_flags(root
,
5359 shrink_block_group
->flags
);
5360 if (new_alloc_flags
!= shrink_block_group
->flags
) {
5362 btrfs_block_group_used(&shrink_block_group
->item
);
5364 calc
= shrink_block_group
->key
.offset
;
5366 spin_unlock(&shrink_block_group
->lock
);
5368 do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
5369 calc
+ 2 * 1024 * 1024, new_alloc_flags
, force
);
5371 btrfs_end_transaction(trans
, root
);
5373 spin_unlock(&shrink_block_group
->lock
);
5377 static int __insert_orphan_inode(struct btrfs_trans_handle
*trans
,
5378 struct btrfs_root
*root
,
5379 u64 objectid
, u64 size
)
5381 struct btrfs_path
*path
;
5382 struct btrfs_inode_item
*item
;
5383 struct extent_buffer
*leaf
;
5386 path
= btrfs_alloc_path();
5390 ret
= btrfs_insert_empty_inode(trans
, root
, path
, objectid
);
5394 leaf
= path
->nodes
[0];
5395 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_inode_item
);
5396 memset_extent_buffer(leaf
, 0, (unsigned long)item
, sizeof(*item
));
5397 btrfs_set_inode_generation(leaf
, item
, 1);
5398 btrfs_set_inode_size(leaf
, item
, size
);
5399 btrfs_set_inode_mode(leaf
, item
, S_IFREG
| 0600);
5400 btrfs_set_inode_flags(leaf
, item
, BTRFS_INODE_NOCOMPRESS
);
5401 btrfs_mark_buffer_dirty(leaf
);
5402 btrfs_release_path(root
, path
);
5404 btrfs_free_path(path
);
5408 static noinline
struct inode
*create_reloc_inode(struct btrfs_fs_info
*fs_info
,
5409 struct btrfs_block_group_cache
*group
)
5411 struct inode
*inode
= NULL
;
5412 struct btrfs_trans_handle
*trans
;
5413 struct btrfs_root
*root
;
5414 struct btrfs_key root_key
;
5415 u64 objectid
= BTRFS_FIRST_FREE_OBJECTID
;
5418 root_key
.objectid
= BTRFS_DATA_RELOC_TREE_OBJECTID
;
5419 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
5420 root_key
.offset
= (u64
)-1;
5421 root
= btrfs_read_fs_root_no_name(fs_info
, &root_key
);
5423 return ERR_CAST(root
);
5425 trans
= btrfs_start_transaction(root
, 1);
5428 err
= btrfs_find_free_objectid(trans
, root
, objectid
, &objectid
);
5432 err
= __insert_orphan_inode(trans
, root
, objectid
, group
->key
.offset
);
5435 err
= btrfs_insert_file_extent(trans
, root
, objectid
, 0, 0, 0,
5436 group
->key
.offset
, 0, group
->key
.offset
,
5440 inode
= btrfs_iget_locked(root
->fs_info
->sb
, objectid
, root
);
5441 if (inode
->i_state
& I_NEW
) {
5442 BTRFS_I(inode
)->root
= root
;
5443 BTRFS_I(inode
)->location
.objectid
= objectid
;
5444 BTRFS_I(inode
)->location
.type
= BTRFS_INODE_ITEM_KEY
;
5445 BTRFS_I(inode
)->location
.offset
= 0;
5446 btrfs_read_locked_inode(inode
);
5447 unlock_new_inode(inode
);
5448 BUG_ON(is_bad_inode(inode
));
5452 BTRFS_I(inode
)->index_cnt
= group
->key
.objectid
;
5454 err
= btrfs_orphan_add(trans
, inode
);
5456 btrfs_end_transaction(trans
, root
);
5460 inode
= ERR_PTR(err
);
5465 int btrfs_reloc_clone_csums(struct inode
*inode
, u64 file_pos
, u64 len
)
5468 struct btrfs_ordered_sum
*sums
;
5469 struct btrfs_sector_sum
*sector_sum
;
5470 struct btrfs_ordered_extent
*ordered
;
5471 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5472 struct list_head list
;
5477 INIT_LIST_HEAD(&list
);
5479 ordered
= btrfs_lookup_ordered_extent(inode
, file_pos
);
5480 BUG_ON(ordered
->file_offset
!= file_pos
|| ordered
->len
!= len
);
5482 disk_bytenr
= file_pos
+ BTRFS_I(inode
)->index_cnt
;
5483 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, disk_bytenr
,
5484 disk_bytenr
+ len
- 1, &list
);
5486 while (!list_empty(&list
)) {
5487 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
5488 list_del_init(&sums
->list
);
5490 sector_sum
= sums
->sums
;
5491 sums
->bytenr
= ordered
->start
;
5494 while (offset
< sums
->len
) {
5495 sector_sum
->bytenr
+= ordered
->start
- disk_bytenr
;
5497 offset
+= root
->sectorsize
;
5500 btrfs_add_ordered_sum(inode
, ordered
, sums
);
5502 btrfs_put_ordered_extent(ordered
);
5506 int btrfs_relocate_block_group(struct btrfs_root
*root
, u64 group_start
)
5508 struct btrfs_trans_handle
*trans
;
5509 struct btrfs_path
*path
;
5510 struct btrfs_fs_info
*info
= root
->fs_info
;
5511 struct extent_buffer
*leaf
;
5512 struct inode
*reloc_inode
;
5513 struct btrfs_block_group_cache
*block_group
;
5514 struct btrfs_key key
;
5523 root
= root
->fs_info
->extent_root
;
5525 block_group
= btrfs_lookup_block_group(info
, group_start
);
5526 BUG_ON(!block_group
);
5528 printk(KERN_INFO
"btrfs relocating block group %llu flags %llu\n",
5529 (unsigned long long)block_group
->key
.objectid
,
5530 (unsigned long long)block_group
->flags
);
5532 path
= btrfs_alloc_path();
5535 reloc_inode
= create_reloc_inode(info
, block_group
);
5536 BUG_ON(IS_ERR(reloc_inode
));
5538 __alloc_chunk_for_shrink(root
, block_group
, 1);
5539 set_block_group_readonly(block_group
);
5541 btrfs_start_delalloc_inodes(info
->tree_root
);
5542 btrfs_wait_ordered_extents(info
->tree_root
, 0);
5547 key
.objectid
= block_group
->key
.objectid
;
5550 cur_byte
= key
.objectid
;
5552 trans
= btrfs_start_transaction(info
->tree_root
, 1);
5553 btrfs_commit_transaction(trans
, info
->tree_root
);
5555 mutex_lock(&root
->fs_info
->cleaner_mutex
);
5556 btrfs_clean_old_snapshots(info
->tree_root
);
5557 btrfs_remove_leaf_refs(info
->tree_root
, (u64
)-1, 1);
5558 mutex_unlock(&root
->fs_info
->cleaner_mutex
);
5560 trans
= btrfs_start_transaction(info
->tree_root
, 1);
5561 btrfs_commit_transaction(trans
, info
->tree_root
);
5564 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5568 leaf
= path
->nodes
[0];
5569 nritems
= btrfs_header_nritems(leaf
);
5570 if (path
->slots
[0] >= nritems
) {
5571 ret
= btrfs_next_leaf(root
, path
);
5578 leaf
= path
->nodes
[0];
5579 nritems
= btrfs_header_nritems(leaf
);
5582 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
5584 if (key
.objectid
>= block_group
->key
.objectid
+
5585 block_group
->key
.offset
)
5588 if (progress
&& need_resched()) {
5589 btrfs_release_path(root
, path
);
5596 if (btrfs_key_type(&key
) != BTRFS_EXTENT_ITEM_KEY
||
5597 key
.objectid
+ key
.offset
<= cur_byte
) {
5603 cur_byte
= key
.objectid
+ key
.offset
;
5604 btrfs_release_path(root
, path
);
5606 __alloc_chunk_for_shrink(root
, block_group
, 0);
5607 ret
= relocate_one_extent(root
, path
, &key
, block_group
,
5613 key
.objectid
= cur_byte
;
5618 btrfs_release_path(root
, path
);
5621 btrfs_wait_ordered_range(reloc_inode
, 0, (u64
)-1);
5622 invalidate_mapping_pages(reloc_inode
->i_mapping
, 0, -1);
5625 if (total_found
> 0) {
5626 printk(KERN_INFO
"btrfs found %llu extents in pass %d\n",
5627 (unsigned long long)total_found
, pass
);
5629 if (total_found
== skipped
&& pass
> 2) {
5631 reloc_inode
= create_reloc_inode(info
, block_group
);
5637 /* delete reloc_inode */
5640 /* unpin extents in this range */
5641 trans
= btrfs_start_transaction(info
->tree_root
, 1);
5642 btrfs_commit_transaction(trans
, info
->tree_root
);
5644 spin_lock(&block_group
->lock
);
5645 WARN_ON(block_group
->pinned
> 0);
5646 WARN_ON(block_group
->reserved
> 0);
5647 WARN_ON(btrfs_block_group_used(&block_group
->item
) > 0);
5648 spin_unlock(&block_group
->lock
);
5649 put_block_group(block_group
);
5652 btrfs_free_path(path
);
5656 static int find_first_block_group(struct btrfs_root
*root
,
5657 struct btrfs_path
*path
, struct btrfs_key
*key
)
5660 struct btrfs_key found_key
;
5661 struct extent_buffer
*leaf
;
5664 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
5669 slot
= path
->slots
[0];
5670 leaf
= path
->nodes
[0];
5671 if (slot
>= btrfs_header_nritems(leaf
)) {
5672 ret
= btrfs_next_leaf(root
, path
);
5679 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5681 if (found_key
.objectid
>= key
->objectid
&&
5682 found_key
.type
== BTRFS_BLOCK_GROUP_ITEM_KEY
) {
5693 int btrfs_free_block_groups(struct btrfs_fs_info
*info
)
5695 struct btrfs_block_group_cache
*block_group
;
5696 struct btrfs_space_info
*space_info
;
5699 spin_lock(&info
->block_group_cache_lock
);
5700 while ((n
= rb_last(&info
->block_group_cache_tree
)) != NULL
) {
5701 block_group
= rb_entry(n
, struct btrfs_block_group_cache
,
5703 rb_erase(&block_group
->cache_node
,
5704 &info
->block_group_cache_tree
);
5705 spin_unlock(&info
->block_group_cache_lock
);
5707 btrfs_remove_free_space_cache(block_group
);
5708 down_write(&block_group
->space_info
->groups_sem
);
5709 list_del(&block_group
->list
);
5710 up_write(&block_group
->space_info
->groups_sem
);
5712 WARN_ON(atomic_read(&block_group
->count
) != 1);
5715 spin_lock(&info
->block_group_cache_lock
);
5717 spin_unlock(&info
->block_group_cache_lock
);
5719 /* now that all the block groups are freed, go through and
5720 * free all the space_info structs. This is only called during
5721 * the final stages of unmount, and so we know nobody is
5722 * using them. We call synchronize_rcu() once before we start,
5723 * just to be on the safe side.
5727 while(!list_empty(&info
->space_info
)) {
5728 space_info
= list_entry(info
->space_info
.next
,
5729 struct btrfs_space_info
,
5732 list_del(&space_info
->list
);
5738 int btrfs_read_block_groups(struct btrfs_root
*root
)
5740 struct btrfs_path
*path
;
5742 struct btrfs_block_group_cache
*cache
;
5743 struct btrfs_fs_info
*info
= root
->fs_info
;
5744 struct btrfs_space_info
*space_info
;
5745 struct btrfs_key key
;
5746 struct btrfs_key found_key
;
5747 struct extent_buffer
*leaf
;
5749 root
= info
->extent_root
;
5752 btrfs_set_key_type(&key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
5753 path
= btrfs_alloc_path();
5758 ret
= find_first_block_group(root
, path
, &key
);
5766 leaf
= path
->nodes
[0];
5767 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5768 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
5774 atomic_set(&cache
->count
, 1);
5775 spin_lock_init(&cache
->lock
);
5776 mutex_init(&cache
->alloc_mutex
);
5777 mutex_init(&cache
->cache_mutex
);
5778 INIT_LIST_HEAD(&cache
->list
);
5779 read_extent_buffer(leaf
, &cache
->item
,
5780 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
5781 sizeof(cache
->item
));
5782 memcpy(&cache
->key
, &found_key
, sizeof(found_key
));
5784 key
.objectid
= found_key
.objectid
+ found_key
.offset
;
5785 btrfs_release_path(root
, path
);
5786 cache
->flags
= btrfs_block_group_flags(&cache
->item
);
5788 ret
= update_space_info(info
, cache
->flags
, found_key
.offset
,
5789 btrfs_block_group_used(&cache
->item
),
5792 cache
->space_info
= space_info
;
5793 down_write(&space_info
->groups_sem
);
5794 list_add_tail(&cache
->list
, &space_info
->block_groups
);
5795 up_write(&space_info
->groups_sem
);
5797 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
5800 set_avail_alloc_bits(root
->fs_info
, cache
->flags
);
5801 if (btrfs_chunk_readonly(root
, cache
->key
.objectid
))
5802 set_block_group_readonly(cache
);
5806 btrfs_free_path(path
);
5810 int btrfs_make_block_group(struct btrfs_trans_handle
*trans
,
5811 struct btrfs_root
*root
, u64 bytes_used
,
5812 u64 type
, u64 chunk_objectid
, u64 chunk_offset
,
5816 struct btrfs_root
*extent_root
;
5817 struct btrfs_block_group_cache
*cache
;
5819 extent_root
= root
->fs_info
->extent_root
;
5821 root
->fs_info
->last_trans_new_blockgroup
= trans
->transid
;
5823 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
5827 cache
->key
.objectid
= chunk_offset
;
5828 cache
->key
.offset
= size
;
5829 cache
->key
.type
= BTRFS_BLOCK_GROUP_ITEM_KEY
;
5830 atomic_set(&cache
->count
, 1);
5831 spin_lock_init(&cache
->lock
);
5832 mutex_init(&cache
->alloc_mutex
);
5833 mutex_init(&cache
->cache_mutex
);
5834 INIT_LIST_HEAD(&cache
->list
);
5836 btrfs_set_block_group_used(&cache
->item
, bytes_used
);
5837 btrfs_set_block_group_chunk_objectid(&cache
->item
, chunk_objectid
);
5838 cache
->flags
= type
;
5839 btrfs_set_block_group_flags(&cache
->item
, type
);
5841 ret
= update_space_info(root
->fs_info
, cache
->flags
, size
, bytes_used
,
5842 &cache
->space_info
);
5844 down_write(&cache
->space_info
->groups_sem
);
5845 list_add_tail(&cache
->list
, &cache
->space_info
->block_groups
);
5846 up_write(&cache
->space_info
->groups_sem
);
5848 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
5851 ret
= btrfs_insert_item(trans
, extent_root
, &cache
->key
, &cache
->item
,
5852 sizeof(cache
->item
));
5855 set_avail_alloc_bits(extent_root
->fs_info
, type
);
5860 int btrfs_remove_block_group(struct btrfs_trans_handle
*trans
,
5861 struct btrfs_root
*root
, u64 group_start
)
5863 struct btrfs_path
*path
;
5864 struct btrfs_block_group_cache
*block_group
;
5865 struct btrfs_key key
;
5868 root
= root
->fs_info
->extent_root
;
5870 block_group
= btrfs_lookup_block_group(root
->fs_info
, group_start
);
5871 BUG_ON(!block_group
);
5872 BUG_ON(!block_group
->ro
);
5874 memcpy(&key
, &block_group
->key
, sizeof(key
));
5876 path
= btrfs_alloc_path();
5879 spin_lock(&root
->fs_info
->block_group_cache_lock
);
5880 rb_erase(&block_group
->cache_node
,
5881 &root
->fs_info
->block_group_cache_tree
);
5882 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
5883 btrfs_remove_free_space_cache(block_group
);
5884 down_write(&block_group
->space_info
->groups_sem
);
5885 list_del(&block_group
->list
);
5886 up_write(&block_group
->space_info
->groups_sem
);
5888 spin_lock(&block_group
->space_info
->lock
);
5889 block_group
->space_info
->total_bytes
-= block_group
->key
.offset
;
5890 block_group
->space_info
->bytes_readonly
-= block_group
->key
.offset
;
5891 spin_unlock(&block_group
->space_info
->lock
);
5892 block_group
->space_info
->full
= 0;
5894 put_block_group(block_group
);
5895 put_block_group(block_group
);
5897 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
5903 ret
= btrfs_del_item(trans
, root
, path
);
5905 btrfs_free_path(path
);