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>
28 #include "print-tree.h"
29 #include "transaction.h"
32 #include "free-space-cache.h"
34 static int update_reserved_extents(struct btrfs_root
*root
,
35 u64 bytenr
, u64 num
, int reserve
);
36 static int update_block_group(struct btrfs_trans_handle
*trans
,
37 struct btrfs_root
*root
,
38 u64 bytenr
, u64 num_bytes
, int alloc
,
40 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
41 struct btrfs_root
*root
,
42 u64 bytenr
, u64 num_bytes
, u64 parent
,
43 u64 root_objectid
, u64 owner_objectid
,
44 u64 owner_offset
, int refs_to_drop
,
45 struct btrfs_delayed_extent_op
*extra_op
);
46 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op
*extent_op
,
47 struct extent_buffer
*leaf
,
48 struct btrfs_extent_item
*ei
);
49 static int alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
50 struct btrfs_root
*root
,
51 u64 parent
, u64 root_objectid
,
52 u64 flags
, u64 owner
, u64 offset
,
53 struct btrfs_key
*ins
, int ref_mod
);
54 static int alloc_reserved_tree_block(struct btrfs_trans_handle
*trans
,
55 struct btrfs_root
*root
,
56 u64 parent
, u64 root_objectid
,
57 u64 flags
, struct btrfs_disk_key
*key
,
58 int level
, struct btrfs_key
*ins
);
60 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
61 struct btrfs_root
*extent_root
, u64 alloc_bytes
,
62 u64 flags
, int force
);
64 static int block_group_bits(struct btrfs_block_group_cache
*cache
, u64 bits
)
66 return (cache
->flags
& bits
) == bits
;
70 * this adds the block group to the fs_info rb tree for the block group
73 static int btrfs_add_block_group_cache(struct btrfs_fs_info
*info
,
74 struct btrfs_block_group_cache
*block_group
)
77 struct rb_node
*parent
= NULL
;
78 struct btrfs_block_group_cache
*cache
;
80 spin_lock(&info
->block_group_cache_lock
);
81 p
= &info
->block_group_cache_tree
.rb_node
;
85 cache
= rb_entry(parent
, struct btrfs_block_group_cache
,
87 if (block_group
->key
.objectid
< cache
->key
.objectid
) {
89 } else if (block_group
->key
.objectid
> cache
->key
.objectid
) {
92 spin_unlock(&info
->block_group_cache_lock
);
97 rb_link_node(&block_group
->cache_node
, parent
, p
);
98 rb_insert_color(&block_group
->cache_node
,
99 &info
->block_group_cache_tree
);
100 spin_unlock(&info
->block_group_cache_lock
);
106 * This will return the block group at or after bytenr if contains is 0, else
107 * it will return the block group that contains the bytenr
109 static struct btrfs_block_group_cache
*
110 block_group_cache_tree_search(struct btrfs_fs_info
*info
, u64 bytenr
,
113 struct btrfs_block_group_cache
*cache
, *ret
= NULL
;
117 spin_lock(&info
->block_group_cache_lock
);
118 n
= info
->block_group_cache_tree
.rb_node
;
121 cache
= rb_entry(n
, struct btrfs_block_group_cache
,
123 end
= cache
->key
.objectid
+ cache
->key
.offset
- 1;
124 start
= cache
->key
.objectid
;
126 if (bytenr
< start
) {
127 if (!contains
&& (!ret
|| start
< ret
->key
.objectid
))
130 } else if (bytenr
> start
) {
131 if (contains
&& bytenr
<= end
) {
142 atomic_inc(&ret
->count
);
143 spin_unlock(&info
->block_group_cache_lock
);
149 * this is only called by cache_block_group, since we could have freed extents
150 * we need to check the pinned_extents for any extents that can't be used yet
151 * since their free space will be released as soon as the transaction commits.
153 static int add_new_free_space(struct btrfs_block_group_cache
*block_group
,
154 struct btrfs_fs_info
*info
, u64 start
, u64 end
)
156 u64 extent_start
, extent_end
, size
;
159 while (start
< end
) {
160 ret
= find_first_extent_bit(&info
->pinned_extents
, start
,
161 &extent_start
, &extent_end
,
166 if (extent_start
== start
) {
167 start
= extent_end
+ 1;
168 } else if (extent_start
> start
&& extent_start
< end
) {
169 size
= extent_start
- start
;
170 ret
= btrfs_add_free_space(block_group
, start
,
173 start
= extent_end
+ 1;
181 ret
= btrfs_add_free_space(block_group
, start
, size
);
188 static int remove_sb_from_cache(struct btrfs_root
*root
,
189 struct btrfs_block_group_cache
*cache
)
196 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
197 bytenr
= btrfs_sb_offset(i
);
198 ret
= btrfs_rmap_block(&root
->fs_info
->mapping_tree
,
199 cache
->key
.objectid
, bytenr
, 0,
200 &logical
, &nr
, &stripe_len
);
203 btrfs_remove_free_space(cache
, logical
[nr
],
211 static int cache_block_group(struct btrfs_root
*root
,
212 struct btrfs_block_group_cache
*block_group
)
214 struct btrfs_path
*path
;
216 struct btrfs_key key
;
217 struct extent_buffer
*leaf
;
224 root
= root
->fs_info
->extent_root
;
226 if (block_group
->cached
)
229 path
= btrfs_alloc_path();
235 * we get into deadlocks with paths held by callers of this function.
236 * since the alloc_mutex is protecting things right now, just
237 * skip the locking here
239 path
->skip_locking
= 1;
240 last
= max_t(u64
, block_group
->key
.objectid
, BTRFS_SUPER_INFO_OFFSET
);
243 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
244 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
249 leaf
= path
->nodes
[0];
250 slot
= path
->slots
[0];
251 if (slot
>= btrfs_header_nritems(leaf
)) {
252 ret
= btrfs_next_leaf(root
, path
);
260 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
261 if (key
.objectid
< block_group
->key
.objectid
)
264 if (key
.objectid
>= block_group
->key
.objectid
+
265 block_group
->key
.offset
)
268 if (btrfs_key_type(&key
) == BTRFS_EXTENT_ITEM_KEY
) {
269 add_new_free_space(block_group
, root
->fs_info
, last
,
272 last
= key
.objectid
+ key
.offset
;
278 add_new_free_space(block_group
, root
->fs_info
, last
,
279 block_group
->key
.objectid
+
280 block_group
->key
.offset
);
282 block_group
->cached
= 1;
283 remove_sb_from_cache(root
, block_group
);
286 btrfs_free_path(path
);
291 * return the block group that starts at or after bytenr
293 static struct btrfs_block_group_cache
*
294 btrfs_lookup_first_block_group(struct btrfs_fs_info
*info
, u64 bytenr
)
296 struct btrfs_block_group_cache
*cache
;
298 cache
= block_group_cache_tree_search(info
, bytenr
, 0);
304 * return the block group that contains the given bytenr
306 struct btrfs_block_group_cache
*btrfs_lookup_block_group(
307 struct btrfs_fs_info
*info
,
310 struct btrfs_block_group_cache
*cache
;
312 cache
= block_group_cache_tree_search(info
, bytenr
, 1);
317 void btrfs_put_block_group(struct btrfs_block_group_cache
*cache
)
319 if (atomic_dec_and_test(&cache
->count
))
323 static struct btrfs_space_info
*__find_space_info(struct btrfs_fs_info
*info
,
326 struct list_head
*head
= &info
->space_info
;
327 struct btrfs_space_info
*found
;
330 list_for_each_entry_rcu(found
, head
, list
) {
331 if (found
->flags
== flags
) {
341 * after adding space to the filesystem, we need to clear the full flags
342 * on all the space infos.
344 void btrfs_clear_space_info_full(struct btrfs_fs_info
*info
)
346 struct list_head
*head
= &info
->space_info
;
347 struct btrfs_space_info
*found
;
350 list_for_each_entry_rcu(found
, head
, list
)
355 static u64
div_factor(u64 num
, int factor
)
364 u64
btrfs_find_block_group(struct btrfs_root
*root
,
365 u64 search_start
, u64 search_hint
, int owner
)
367 struct btrfs_block_group_cache
*cache
;
369 u64 last
= max(search_hint
, search_start
);
376 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
380 spin_lock(&cache
->lock
);
381 last
= cache
->key
.objectid
+ cache
->key
.offset
;
382 used
= btrfs_block_group_used(&cache
->item
);
384 if ((full_search
|| !cache
->ro
) &&
385 block_group_bits(cache
, BTRFS_BLOCK_GROUP_METADATA
)) {
386 if (used
+ cache
->pinned
+ cache
->reserved
<
387 div_factor(cache
->key
.offset
, factor
)) {
388 group_start
= cache
->key
.objectid
;
389 spin_unlock(&cache
->lock
);
390 btrfs_put_block_group(cache
);
394 spin_unlock(&cache
->lock
);
395 btrfs_put_block_group(cache
);
403 if (!full_search
&& factor
< 10) {
413 /* simple helper to search for an existing extent at a given offset */
414 int btrfs_lookup_extent(struct btrfs_root
*root
, u64 start
, u64 len
)
417 struct btrfs_key key
;
418 struct btrfs_path
*path
;
420 path
= btrfs_alloc_path();
422 key
.objectid
= start
;
424 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
425 ret
= btrfs_search_slot(NULL
, root
->fs_info
->extent_root
, &key
, path
,
427 btrfs_free_path(path
);
432 * Back reference rules. Back refs have three main goals:
434 * 1) differentiate between all holders of references to an extent so that
435 * when a reference is dropped we can make sure it was a valid reference
436 * before freeing the extent.
438 * 2) Provide enough information to quickly find the holders of an extent
439 * if we notice a given block is corrupted or bad.
441 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
442 * maintenance. This is actually the same as #2, but with a slightly
443 * different use case.
445 * There are two kinds of back refs. The implicit back refs is optimized
446 * for pointers in non-shared tree blocks. For a given pointer in a block,
447 * back refs of this kind provide information about the block's owner tree
448 * and the pointer's key. These information allow us to find the block by
449 * b-tree searching. The full back refs is for pointers in tree blocks not
450 * referenced by their owner trees. The location of tree block is recorded
451 * in the back refs. Actually the full back refs is generic, and can be
452 * used in all cases the implicit back refs is used. The major shortcoming
453 * of the full back refs is its overhead. Every time a tree block gets
454 * COWed, we have to update back refs entry for all pointers in it.
456 * For a newly allocated tree block, we use implicit back refs for
457 * pointers in it. This means most tree related operations only involve
458 * implicit back refs. For a tree block created in old transaction, the
459 * only way to drop a reference to it is COW it. So we can detect the
460 * event that tree block loses its owner tree's reference and do the
461 * back refs conversion.
463 * When a tree block is COW'd through a tree, there are four cases:
465 * The reference count of the block is one and the tree is the block's
466 * owner tree. Nothing to do in this case.
468 * The reference count of the block is one and the tree is not the
469 * block's owner tree. In this case, full back refs is used for pointers
470 * in the block. Remove these full back refs, add implicit back refs for
471 * every pointers in the new block.
473 * The reference count of the block is greater than one and the tree is
474 * the block's owner tree. In this case, implicit back refs is used for
475 * pointers in the block. Add full back refs for every pointers in the
476 * block, increase lower level extents' reference counts. The original
477 * implicit back refs are entailed to the new block.
479 * The reference count of the block is greater than one and the tree is
480 * not the block's owner tree. Add implicit back refs for every pointer in
481 * the new block, increase lower level extents' reference count.
483 * Back Reference Key composing:
485 * The key objectid corresponds to the first byte in the extent,
486 * The key type is used to differentiate between types of back refs.
487 * There are different meanings of the key offset for different types
490 * File extents can be referenced by:
492 * - multiple snapshots, subvolumes, or different generations in one subvol
493 * - different files inside a single subvolume
494 * - different offsets inside a file (bookend extents in file.c)
496 * The extent ref structure for the implicit back refs has fields for:
498 * - Objectid of the subvolume root
499 * - objectid of the file holding the reference
500 * - original offset in the file
501 * - how many bookend extents
503 * The key offset for the implicit back refs is hash of the first
506 * The extent ref structure for the full back refs has field for:
508 * - number of pointers in the tree leaf
510 * The key offset for the implicit back refs is the first byte of
513 * When a file extent is allocated, The implicit back refs is used.
514 * the fields are filled in:
516 * (root_key.objectid, inode objectid, offset in file, 1)
518 * When a file extent is removed file truncation, we find the
519 * corresponding implicit back refs and check the following fields:
521 * (btrfs_header_owner(leaf), inode objectid, offset in file)
523 * Btree extents can be referenced by:
525 * - Different subvolumes
527 * Both the implicit back refs and the full back refs for tree blocks
528 * only consist of key. The key offset for the implicit back refs is
529 * objectid of block's owner tree. The key offset for the full back refs
530 * is the first byte of parent block.
532 * When implicit back refs is used, information about the lowest key and
533 * level of the tree block are required. These information are stored in
534 * tree block info structure.
537 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
538 static int convert_extent_item_v0(struct btrfs_trans_handle
*trans
,
539 struct btrfs_root
*root
,
540 struct btrfs_path
*path
,
541 u64 owner
, u32 extra_size
)
543 struct btrfs_extent_item
*item
;
544 struct btrfs_extent_item_v0
*ei0
;
545 struct btrfs_extent_ref_v0
*ref0
;
546 struct btrfs_tree_block_info
*bi
;
547 struct extent_buffer
*leaf
;
548 struct btrfs_key key
;
549 struct btrfs_key found_key
;
550 u32 new_size
= sizeof(*item
);
554 leaf
= path
->nodes
[0];
555 BUG_ON(btrfs_item_size_nr(leaf
, path
->slots
[0]) != sizeof(*ei0
));
557 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
558 ei0
= btrfs_item_ptr(leaf
, path
->slots
[0],
559 struct btrfs_extent_item_v0
);
560 refs
= btrfs_extent_refs_v0(leaf
, ei0
);
562 if (owner
== (u64
)-1) {
564 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
565 ret
= btrfs_next_leaf(root
, path
);
569 leaf
= path
->nodes
[0];
571 btrfs_item_key_to_cpu(leaf
, &found_key
,
573 BUG_ON(key
.objectid
!= found_key
.objectid
);
574 if (found_key
.type
!= BTRFS_EXTENT_REF_V0_KEY
) {
578 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
579 struct btrfs_extent_ref_v0
);
580 owner
= btrfs_ref_objectid_v0(leaf
, ref0
);
584 btrfs_release_path(root
, path
);
586 if (owner
< BTRFS_FIRST_FREE_OBJECTID
)
587 new_size
+= sizeof(*bi
);
589 new_size
-= sizeof(*ei0
);
590 ret
= btrfs_search_slot(trans
, root
, &key
, path
,
591 new_size
+ extra_size
, 1);
596 ret
= btrfs_extend_item(trans
, root
, path
, new_size
);
599 leaf
= path
->nodes
[0];
600 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
601 btrfs_set_extent_refs(leaf
, item
, refs
);
602 /* FIXME: get real generation */
603 btrfs_set_extent_generation(leaf
, item
, 0);
604 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
605 btrfs_set_extent_flags(leaf
, item
,
606 BTRFS_EXTENT_FLAG_TREE_BLOCK
|
607 BTRFS_BLOCK_FLAG_FULL_BACKREF
);
608 bi
= (struct btrfs_tree_block_info
*)(item
+ 1);
609 /* FIXME: get first key of the block */
610 memset_extent_buffer(leaf
, 0, (unsigned long)bi
, sizeof(*bi
));
611 btrfs_set_tree_block_level(leaf
, bi
, (int)owner
);
613 btrfs_set_extent_flags(leaf
, item
, BTRFS_EXTENT_FLAG_DATA
);
615 btrfs_mark_buffer_dirty(leaf
);
620 static u64
hash_extent_data_ref(u64 root_objectid
, u64 owner
, u64 offset
)
622 u32 high_crc
= ~(u32
)0;
623 u32 low_crc
= ~(u32
)0;
626 lenum
= cpu_to_le64(root_objectid
);
627 high_crc
= crc32c(high_crc
, &lenum
, sizeof(lenum
));
628 lenum
= cpu_to_le64(owner
);
629 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
630 lenum
= cpu_to_le64(offset
);
631 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
633 return ((u64
)high_crc
<< 31) ^ (u64
)low_crc
;
636 static u64
hash_extent_data_ref_item(struct extent_buffer
*leaf
,
637 struct btrfs_extent_data_ref
*ref
)
639 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf
, ref
),
640 btrfs_extent_data_ref_objectid(leaf
, ref
),
641 btrfs_extent_data_ref_offset(leaf
, ref
));
644 static int match_extent_data_ref(struct extent_buffer
*leaf
,
645 struct btrfs_extent_data_ref
*ref
,
646 u64 root_objectid
, u64 owner
, u64 offset
)
648 if (btrfs_extent_data_ref_root(leaf
, ref
) != root_objectid
||
649 btrfs_extent_data_ref_objectid(leaf
, ref
) != owner
||
650 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
655 static noinline
int lookup_extent_data_ref(struct btrfs_trans_handle
*trans
,
656 struct btrfs_root
*root
,
657 struct btrfs_path
*path
,
658 u64 bytenr
, u64 parent
,
660 u64 owner
, u64 offset
)
662 struct btrfs_key key
;
663 struct btrfs_extent_data_ref
*ref
;
664 struct extent_buffer
*leaf
;
670 key
.objectid
= bytenr
;
672 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
675 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
676 key
.offset
= hash_extent_data_ref(root_objectid
,
681 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
690 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
691 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
692 btrfs_release_path(root
, path
);
693 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
704 leaf
= path
->nodes
[0];
705 nritems
= btrfs_header_nritems(leaf
);
707 if (path
->slots
[0] >= nritems
) {
708 ret
= btrfs_next_leaf(root
, path
);
714 leaf
= path
->nodes
[0];
715 nritems
= btrfs_header_nritems(leaf
);
719 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
720 if (key
.objectid
!= bytenr
||
721 key
.type
!= BTRFS_EXTENT_DATA_REF_KEY
)
724 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
725 struct btrfs_extent_data_ref
);
727 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
730 btrfs_release_path(root
, path
);
742 static noinline
int insert_extent_data_ref(struct btrfs_trans_handle
*trans
,
743 struct btrfs_root
*root
,
744 struct btrfs_path
*path
,
745 u64 bytenr
, u64 parent
,
746 u64 root_objectid
, u64 owner
,
747 u64 offset
, int refs_to_add
)
749 struct btrfs_key key
;
750 struct extent_buffer
*leaf
;
755 key
.objectid
= bytenr
;
757 key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
759 size
= sizeof(struct btrfs_shared_data_ref
);
761 key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
762 key
.offset
= hash_extent_data_ref(root_objectid
,
764 size
= sizeof(struct btrfs_extent_data_ref
);
767 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, size
);
768 if (ret
&& ret
!= -EEXIST
)
771 leaf
= path
->nodes
[0];
773 struct btrfs_shared_data_ref
*ref
;
774 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
775 struct btrfs_shared_data_ref
);
777 btrfs_set_shared_data_ref_count(leaf
, ref
, refs_to_add
);
779 num_refs
= btrfs_shared_data_ref_count(leaf
, ref
);
780 num_refs
+= refs_to_add
;
781 btrfs_set_shared_data_ref_count(leaf
, ref
, num_refs
);
784 struct btrfs_extent_data_ref
*ref
;
785 while (ret
== -EEXIST
) {
786 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
787 struct btrfs_extent_data_ref
);
788 if (match_extent_data_ref(leaf
, ref
, root_objectid
,
791 btrfs_release_path(root
, path
);
793 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
795 if (ret
&& ret
!= -EEXIST
)
798 leaf
= path
->nodes
[0];
800 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
801 struct btrfs_extent_data_ref
);
803 btrfs_set_extent_data_ref_root(leaf
, ref
,
805 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
806 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
807 btrfs_set_extent_data_ref_count(leaf
, ref
, refs_to_add
);
809 num_refs
= btrfs_extent_data_ref_count(leaf
, ref
);
810 num_refs
+= refs_to_add
;
811 btrfs_set_extent_data_ref_count(leaf
, ref
, num_refs
);
814 btrfs_mark_buffer_dirty(leaf
);
817 btrfs_release_path(root
, path
);
821 static noinline
int remove_extent_data_ref(struct btrfs_trans_handle
*trans
,
822 struct btrfs_root
*root
,
823 struct btrfs_path
*path
,
826 struct btrfs_key key
;
827 struct btrfs_extent_data_ref
*ref1
= NULL
;
828 struct btrfs_shared_data_ref
*ref2
= NULL
;
829 struct extent_buffer
*leaf
;
833 leaf
= path
->nodes
[0];
834 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
836 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
837 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
838 struct btrfs_extent_data_ref
);
839 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
840 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
841 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
842 struct btrfs_shared_data_ref
);
843 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
844 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
845 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
846 struct btrfs_extent_ref_v0
*ref0
;
847 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
848 struct btrfs_extent_ref_v0
);
849 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
855 BUG_ON(num_refs
< refs_to_drop
);
856 num_refs
-= refs_to_drop
;
859 ret
= btrfs_del_item(trans
, root
, path
);
861 if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
)
862 btrfs_set_extent_data_ref_count(leaf
, ref1
, num_refs
);
863 else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
)
864 btrfs_set_shared_data_ref_count(leaf
, ref2
, num_refs
);
865 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
867 struct btrfs_extent_ref_v0
*ref0
;
868 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
869 struct btrfs_extent_ref_v0
);
870 btrfs_set_ref_count_v0(leaf
, ref0
, num_refs
);
873 btrfs_mark_buffer_dirty(leaf
);
878 static noinline u32
extent_data_ref_count(struct btrfs_root
*root
,
879 struct btrfs_path
*path
,
880 struct btrfs_extent_inline_ref
*iref
)
882 struct btrfs_key key
;
883 struct extent_buffer
*leaf
;
884 struct btrfs_extent_data_ref
*ref1
;
885 struct btrfs_shared_data_ref
*ref2
;
888 leaf
= path
->nodes
[0];
889 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
891 if (btrfs_extent_inline_ref_type(leaf
, iref
) ==
892 BTRFS_EXTENT_DATA_REF_KEY
) {
893 ref1
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
894 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
896 ref2
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
897 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
899 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
900 ref1
= btrfs_item_ptr(leaf
, path
->slots
[0],
901 struct btrfs_extent_data_ref
);
902 num_refs
= btrfs_extent_data_ref_count(leaf
, ref1
);
903 } else if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
904 ref2
= btrfs_item_ptr(leaf
, path
->slots
[0],
905 struct btrfs_shared_data_ref
);
906 num_refs
= btrfs_shared_data_ref_count(leaf
, ref2
);
907 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
908 } else if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
909 struct btrfs_extent_ref_v0
*ref0
;
910 ref0
= btrfs_item_ptr(leaf
, path
->slots
[0],
911 struct btrfs_extent_ref_v0
);
912 num_refs
= btrfs_ref_count_v0(leaf
, ref0
);
920 static noinline
int lookup_tree_block_ref(struct btrfs_trans_handle
*trans
,
921 struct btrfs_root
*root
,
922 struct btrfs_path
*path
,
923 u64 bytenr
, u64 parent
,
926 struct btrfs_key key
;
929 key
.objectid
= bytenr
;
931 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
934 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
935 key
.offset
= root_objectid
;
938 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
941 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
942 if (ret
== -ENOENT
&& parent
) {
943 btrfs_release_path(root
, path
);
944 key
.type
= BTRFS_EXTENT_REF_V0_KEY
;
945 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
953 static noinline
int insert_tree_block_ref(struct btrfs_trans_handle
*trans
,
954 struct btrfs_root
*root
,
955 struct btrfs_path
*path
,
956 u64 bytenr
, u64 parent
,
959 struct btrfs_key key
;
962 key
.objectid
= bytenr
;
964 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
967 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
968 key
.offset
= root_objectid
;
971 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, 0);
972 btrfs_release_path(root
, path
);
976 static inline int extent_ref_type(u64 parent
, u64 owner
)
979 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
981 type
= BTRFS_SHARED_BLOCK_REF_KEY
;
983 type
= BTRFS_TREE_BLOCK_REF_KEY
;
986 type
= BTRFS_SHARED_DATA_REF_KEY
;
988 type
= BTRFS_EXTENT_DATA_REF_KEY
;
993 static int find_next_key(struct btrfs_path
*path
, struct btrfs_key
*key
)
997 BUG_ON(!path
->keep_locks
);
998 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
999 if (!path
->nodes
[level
])
1001 btrfs_assert_tree_locked(path
->nodes
[level
]);
1002 if (path
->slots
[level
] + 1 >=
1003 btrfs_header_nritems(path
->nodes
[level
]))
1006 btrfs_item_key_to_cpu(path
->nodes
[level
], key
,
1007 path
->slots
[level
] + 1);
1009 btrfs_node_key_to_cpu(path
->nodes
[level
], key
,
1010 path
->slots
[level
] + 1);
1017 * look for inline back ref. if back ref is found, *ref_ret is set
1018 * to the address of inline back ref, and 0 is returned.
1020 * if back ref isn't found, *ref_ret is set to the address where it
1021 * should be inserted, and -ENOENT is returned.
1023 * if insert is true and there are too many inline back refs, the path
1024 * points to the extent item, and -EAGAIN is returned.
1026 * NOTE: inline back refs are ordered in the same way that back ref
1027 * items in the tree are ordered.
1029 static noinline_for_stack
1030 int lookup_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1031 struct btrfs_root
*root
,
1032 struct btrfs_path
*path
,
1033 struct btrfs_extent_inline_ref
**ref_ret
,
1034 u64 bytenr
, u64 num_bytes
,
1035 u64 parent
, u64 root_objectid
,
1036 u64 owner
, u64 offset
, int insert
)
1038 struct btrfs_key key
;
1039 struct extent_buffer
*leaf
;
1040 struct btrfs_extent_item
*ei
;
1041 struct btrfs_extent_inline_ref
*iref
;
1052 key
.objectid
= bytenr
;
1053 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1054 key
.offset
= num_bytes
;
1056 want
= extent_ref_type(parent
, owner
);
1058 extra_size
= btrfs_extent_inline_ref_size(want
);
1059 path
->keep_locks
= 1;
1062 ret
= btrfs_search_slot(trans
, root
, &key
, path
, extra_size
, 1);
1069 leaf
= path
->nodes
[0];
1070 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1071 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1072 if (item_size
< sizeof(*ei
)) {
1077 ret
= convert_extent_item_v0(trans
, root
, path
, owner
,
1083 leaf
= path
->nodes
[0];
1084 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1087 BUG_ON(item_size
< sizeof(*ei
));
1089 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1090 flags
= btrfs_extent_flags(leaf
, ei
);
1092 ptr
= (unsigned long)(ei
+ 1);
1093 end
= (unsigned long)ei
+ item_size
;
1095 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
1096 ptr
+= sizeof(struct btrfs_tree_block_info
);
1099 BUG_ON(!(flags
& BTRFS_EXTENT_FLAG_DATA
));
1108 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1109 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1113 ptr
+= btrfs_extent_inline_ref_size(type
);
1117 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1118 struct btrfs_extent_data_ref
*dref
;
1119 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1120 if (match_extent_data_ref(leaf
, dref
, root_objectid
,
1125 if (hash_extent_data_ref_item(leaf
, dref
) <
1126 hash_extent_data_ref(root_objectid
, owner
, offset
))
1130 ref_offset
= btrfs_extent_inline_ref_offset(leaf
, iref
);
1132 if (parent
== ref_offset
) {
1136 if (ref_offset
< parent
)
1139 if (root_objectid
== ref_offset
) {
1143 if (ref_offset
< root_objectid
)
1147 ptr
+= btrfs_extent_inline_ref_size(type
);
1149 if (err
== -ENOENT
&& insert
) {
1150 if (item_size
+ extra_size
>=
1151 BTRFS_MAX_EXTENT_ITEM_SIZE(root
)) {
1156 * To add new inline back ref, we have to make sure
1157 * there is no corresponding back ref item.
1158 * For simplicity, we just do not add new inline back
1159 * ref if there is any kind of item for this block
1161 if (find_next_key(path
, &key
) == 0 && key
.objectid
== bytenr
&&
1162 key
.type
< BTRFS_BLOCK_GROUP_ITEM_KEY
) {
1167 *ref_ret
= (struct btrfs_extent_inline_ref
*)ptr
;
1170 path
->keep_locks
= 0;
1171 btrfs_unlock_up_safe(path
, 1);
1177 * helper to add new inline back ref
1179 static noinline_for_stack
1180 int setup_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1181 struct btrfs_root
*root
,
1182 struct btrfs_path
*path
,
1183 struct btrfs_extent_inline_ref
*iref
,
1184 u64 parent
, u64 root_objectid
,
1185 u64 owner
, u64 offset
, int refs_to_add
,
1186 struct btrfs_delayed_extent_op
*extent_op
)
1188 struct extent_buffer
*leaf
;
1189 struct btrfs_extent_item
*ei
;
1192 unsigned long item_offset
;
1198 leaf
= path
->nodes
[0];
1199 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1200 item_offset
= (unsigned long)iref
- (unsigned long)ei
;
1202 type
= extent_ref_type(parent
, owner
);
1203 size
= btrfs_extent_inline_ref_size(type
);
1205 ret
= btrfs_extend_item(trans
, root
, path
, size
);
1208 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1209 refs
= btrfs_extent_refs(leaf
, ei
);
1210 refs
+= refs_to_add
;
1211 btrfs_set_extent_refs(leaf
, ei
, refs
);
1213 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1215 ptr
= (unsigned long)ei
+ item_offset
;
1216 end
= (unsigned long)ei
+ btrfs_item_size_nr(leaf
, path
->slots
[0]);
1217 if (ptr
< end
- size
)
1218 memmove_extent_buffer(leaf
, ptr
+ size
, ptr
,
1221 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
1222 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
1223 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1224 struct btrfs_extent_data_ref
*dref
;
1225 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1226 btrfs_set_extent_data_ref_root(leaf
, dref
, root_objectid
);
1227 btrfs_set_extent_data_ref_objectid(leaf
, dref
, owner
);
1228 btrfs_set_extent_data_ref_offset(leaf
, dref
, offset
);
1229 btrfs_set_extent_data_ref_count(leaf
, dref
, refs_to_add
);
1230 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1231 struct btrfs_shared_data_ref
*sref
;
1232 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1233 btrfs_set_shared_data_ref_count(leaf
, sref
, refs_to_add
);
1234 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1235 } else if (type
== BTRFS_SHARED_BLOCK_REF_KEY
) {
1236 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
1238 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
1240 btrfs_mark_buffer_dirty(leaf
);
1244 static int lookup_extent_backref(struct btrfs_trans_handle
*trans
,
1245 struct btrfs_root
*root
,
1246 struct btrfs_path
*path
,
1247 struct btrfs_extent_inline_ref
**ref_ret
,
1248 u64 bytenr
, u64 num_bytes
, u64 parent
,
1249 u64 root_objectid
, u64 owner
, u64 offset
)
1253 ret
= lookup_inline_extent_backref(trans
, root
, path
, ref_ret
,
1254 bytenr
, num_bytes
, parent
,
1255 root_objectid
, owner
, offset
, 0);
1259 btrfs_release_path(root
, path
);
1262 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1263 ret
= lookup_tree_block_ref(trans
, root
, path
, bytenr
, parent
,
1266 ret
= lookup_extent_data_ref(trans
, root
, path
, bytenr
, parent
,
1267 root_objectid
, owner
, offset
);
1273 * helper to update/remove inline back ref
1275 static noinline_for_stack
1276 int update_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1277 struct btrfs_root
*root
,
1278 struct btrfs_path
*path
,
1279 struct btrfs_extent_inline_ref
*iref
,
1281 struct btrfs_delayed_extent_op
*extent_op
)
1283 struct extent_buffer
*leaf
;
1284 struct btrfs_extent_item
*ei
;
1285 struct btrfs_extent_data_ref
*dref
= NULL
;
1286 struct btrfs_shared_data_ref
*sref
= NULL
;
1295 leaf
= path
->nodes
[0];
1296 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1297 refs
= btrfs_extent_refs(leaf
, ei
);
1298 WARN_ON(refs_to_mod
< 0 && refs
+ refs_to_mod
<= 0);
1299 refs
+= refs_to_mod
;
1300 btrfs_set_extent_refs(leaf
, ei
, refs
);
1302 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1304 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1306 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
1307 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1308 refs
= btrfs_extent_data_ref_count(leaf
, dref
);
1309 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
1310 sref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
1311 refs
= btrfs_shared_data_ref_count(leaf
, sref
);
1314 BUG_ON(refs_to_mod
!= -1);
1317 BUG_ON(refs_to_mod
< 0 && refs
< -refs_to_mod
);
1318 refs
+= refs_to_mod
;
1321 if (type
== BTRFS_EXTENT_DATA_REF_KEY
)
1322 btrfs_set_extent_data_ref_count(leaf
, dref
, refs
);
1324 btrfs_set_shared_data_ref_count(leaf
, sref
, refs
);
1326 size
= btrfs_extent_inline_ref_size(type
);
1327 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1328 ptr
= (unsigned long)iref
;
1329 end
= (unsigned long)ei
+ item_size
;
1330 if (ptr
+ size
< end
)
1331 memmove_extent_buffer(leaf
, ptr
, ptr
+ size
,
1334 ret
= btrfs_truncate_item(trans
, root
, path
, item_size
, 1);
1337 btrfs_mark_buffer_dirty(leaf
);
1341 static noinline_for_stack
1342 int insert_inline_extent_backref(struct btrfs_trans_handle
*trans
,
1343 struct btrfs_root
*root
,
1344 struct btrfs_path
*path
,
1345 u64 bytenr
, u64 num_bytes
, u64 parent
,
1346 u64 root_objectid
, u64 owner
,
1347 u64 offset
, int refs_to_add
,
1348 struct btrfs_delayed_extent_op
*extent_op
)
1350 struct btrfs_extent_inline_ref
*iref
;
1353 ret
= lookup_inline_extent_backref(trans
, root
, path
, &iref
,
1354 bytenr
, num_bytes
, parent
,
1355 root_objectid
, owner
, offset
, 1);
1357 BUG_ON(owner
< BTRFS_FIRST_FREE_OBJECTID
);
1358 ret
= update_inline_extent_backref(trans
, root
, path
, iref
,
1359 refs_to_add
, extent_op
);
1360 } else if (ret
== -ENOENT
) {
1361 ret
= setup_inline_extent_backref(trans
, root
, path
, iref
,
1362 parent
, root_objectid
,
1363 owner
, offset
, refs_to_add
,
1369 static int insert_extent_backref(struct btrfs_trans_handle
*trans
,
1370 struct btrfs_root
*root
,
1371 struct btrfs_path
*path
,
1372 u64 bytenr
, u64 parent
, u64 root_objectid
,
1373 u64 owner
, u64 offset
, int refs_to_add
)
1376 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1377 BUG_ON(refs_to_add
!= 1);
1378 ret
= insert_tree_block_ref(trans
, root
, path
, bytenr
,
1379 parent
, root_objectid
);
1381 ret
= insert_extent_data_ref(trans
, root
, path
, bytenr
,
1382 parent
, root_objectid
,
1383 owner
, offset
, refs_to_add
);
1388 static int remove_extent_backref(struct btrfs_trans_handle
*trans
,
1389 struct btrfs_root
*root
,
1390 struct btrfs_path
*path
,
1391 struct btrfs_extent_inline_ref
*iref
,
1392 int refs_to_drop
, int is_data
)
1396 BUG_ON(!is_data
&& refs_to_drop
!= 1);
1398 ret
= update_inline_extent_backref(trans
, root
, path
, iref
,
1399 -refs_to_drop
, NULL
);
1400 } else if (is_data
) {
1401 ret
= remove_extent_data_ref(trans
, root
, path
, refs_to_drop
);
1403 ret
= btrfs_del_item(trans
, root
, path
);
1408 #ifdef BIO_RW_DISCARD
1409 static void btrfs_issue_discard(struct block_device
*bdev
,
1412 blkdev_issue_discard(bdev
, start
>> 9, len
>> 9, GFP_KERNEL
);
1416 static int btrfs_discard_extent(struct btrfs_root
*root
, u64 bytenr
,
1419 #ifdef BIO_RW_DISCARD
1421 u64 map_length
= num_bytes
;
1422 struct btrfs_multi_bio
*multi
= NULL
;
1424 /* Tell the block device(s) that the sectors can be discarded */
1425 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
1426 bytenr
, &map_length
, &multi
, 0);
1428 struct btrfs_bio_stripe
*stripe
= multi
->stripes
;
1431 if (map_length
> num_bytes
)
1432 map_length
= num_bytes
;
1434 for (i
= 0; i
< multi
->num_stripes
; i
++, stripe
++) {
1435 btrfs_issue_discard(stripe
->dev
->bdev
,
1448 int btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
1449 struct btrfs_root
*root
,
1450 u64 bytenr
, u64 num_bytes
, u64 parent
,
1451 u64 root_objectid
, u64 owner
, u64 offset
)
1454 BUG_ON(owner
< BTRFS_FIRST_FREE_OBJECTID
&&
1455 root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
1457 if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
1458 ret
= btrfs_add_delayed_tree_ref(trans
, bytenr
, num_bytes
,
1459 parent
, root_objectid
, (int)owner
,
1460 BTRFS_ADD_DELAYED_REF
, NULL
);
1462 ret
= btrfs_add_delayed_data_ref(trans
, bytenr
, num_bytes
,
1463 parent
, root_objectid
, owner
, offset
,
1464 BTRFS_ADD_DELAYED_REF
, NULL
);
1469 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
1470 struct btrfs_root
*root
,
1471 u64 bytenr
, u64 num_bytes
,
1472 u64 parent
, u64 root_objectid
,
1473 u64 owner
, u64 offset
, int refs_to_add
,
1474 struct btrfs_delayed_extent_op
*extent_op
)
1476 struct btrfs_path
*path
;
1477 struct extent_buffer
*leaf
;
1478 struct btrfs_extent_item
*item
;
1483 path
= btrfs_alloc_path();
1488 path
->leave_spinning
= 1;
1489 /* this will setup the path even if it fails to insert the back ref */
1490 ret
= insert_inline_extent_backref(trans
, root
->fs_info
->extent_root
,
1491 path
, bytenr
, num_bytes
, parent
,
1492 root_objectid
, owner
, offset
,
1493 refs_to_add
, extent_op
);
1497 if (ret
!= -EAGAIN
) {
1502 leaf
= path
->nodes
[0];
1503 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1504 refs
= btrfs_extent_refs(leaf
, item
);
1505 btrfs_set_extent_refs(leaf
, item
, refs
+ refs_to_add
);
1507 __run_delayed_extent_op(extent_op
, leaf
, item
);
1509 btrfs_mark_buffer_dirty(leaf
);
1510 btrfs_release_path(root
->fs_info
->extent_root
, path
);
1513 path
->leave_spinning
= 1;
1515 /* now insert the actual backref */
1516 ret
= insert_extent_backref(trans
, root
->fs_info
->extent_root
,
1517 path
, bytenr
, parent
, root_objectid
,
1518 owner
, offset
, refs_to_add
);
1521 btrfs_free_path(path
);
1525 static int run_delayed_data_ref(struct btrfs_trans_handle
*trans
,
1526 struct btrfs_root
*root
,
1527 struct btrfs_delayed_ref_node
*node
,
1528 struct btrfs_delayed_extent_op
*extent_op
,
1529 int insert_reserved
)
1532 struct btrfs_delayed_data_ref
*ref
;
1533 struct btrfs_key ins
;
1538 ins
.objectid
= node
->bytenr
;
1539 ins
.offset
= node
->num_bytes
;
1540 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1542 ref
= btrfs_delayed_node_to_data_ref(node
);
1543 if (node
->type
== BTRFS_SHARED_DATA_REF_KEY
)
1544 parent
= ref
->parent
;
1546 ref_root
= ref
->root
;
1548 if (node
->action
== BTRFS_ADD_DELAYED_REF
&& insert_reserved
) {
1550 BUG_ON(extent_op
->update_key
);
1551 flags
|= extent_op
->flags_to_set
;
1553 ret
= alloc_reserved_file_extent(trans
, root
,
1554 parent
, ref_root
, flags
,
1555 ref
->objectid
, ref
->offset
,
1556 &ins
, node
->ref_mod
);
1557 update_reserved_extents(root
, ins
.objectid
, ins
.offset
, 0);
1558 } else if (node
->action
== BTRFS_ADD_DELAYED_REF
) {
1559 ret
= __btrfs_inc_extent_ref(trans
, root
, node
->bytenr
,
1560 node
->num_bytes
, parent
,
1561 ref_root
, ref
->objectid
,
1562 ref
->offset
, node
->ref_mod
,
1564 } else if (node
->action
== BTRFS_DROP_DELAYED_REF
) {
1565 ret
= __btrfs_free_extent(trans
, root
, node
->bytenr
,
1566 node
->num_bytes
, parent
,
1567 ref_root
, ref
->objectid
,
1568 ref
->offset
, node
->ref_mod
,
1576 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op
*extent_op
,
1577 struct extent_buffer
*leaf
,
1578 struct btrfs_extent_item
*ei
)
1580 u64 flags
= btrfs_extent_flags(leaf
, ei
);
1581 if (extent_op
->update_flags
) {
1582 flags
|= extent_op
->flags_to_set
;
1583 btrfs_set_extent_flags(leaf
, ei
, flags
);
1586 if (extent_op
->update_key
) {
1587 struct btrfs_tree_block_info
*bi
;
1588 BUG_ON(!(flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
));
1589 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
1590 btrfs_set_tree_block_key(leaf
, bi
, &extent_op
->key
);
1594 static int run_delayed_extent_op(struct btrfs_trans_handle
*trans
,
1595 struct btrfs_root
*root
,
1596 struct btrfs_delayed_ref_node
*node
,
1597 struct btrfs_delayed_extent_op
*extent_op
)
1599 struct btrfs_key key
;
1600 struct btrfs_path
*path
;
1601 struct btrfs_extent_item
*ei
;
1602 struct extent_buffer
*leaf
;
1607 path
= btrfs_alloc_path();
1611 key
.objectid
= node
->bytenr
;
1612 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1613 key
.offset
= node
->num_bytes
;
1616 path
->leave_spinning
= 1;
1617 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
,
1628 leaf
= path
->nodes
[0];
1629 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1630 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1631 if (item_size
< sizeof(*ei
)) {
1632 ret
= convert_extent_item_v0(trans
, root
->fs_info
->extent_root
,
1638 leaf
= path
->nodes
[0];
1639 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1642 BUG_ON(item_size
< sizeof(*ei
));
1643 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
1644 __run_delayed_extent_op(extent_op
, leaf
, ei
);
1646 btrfs_mark_buffer_dirty(leaf
);
1648 btrfs_free_path(path
);
1652 static int run_delayed_tree_ref(struct btrfs_trans_handle
*trans
,
1653 struct btrfs_root
*root
,
1654 struct btrfs_delayed_ref_node
*node
,
1655 struct btrfs_delayed_extent_op
*extent_op
,
1656 int insert_reserved
)
1659 struct btrfs_delayed_tree_ref
*ref
;
1660 struct btrfs_key ins
;
1664 ins
.objectid
= node
->bytenr
;
1665 ins
.offset
= node
->num_bytes
;
1666 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1668 ref
= btrfs_delayed_node_to_tree_ref(node
);
1669 if (node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
)
1670 parent
= ref
->parent
;
1672 ref_root
= ref
->root
;
1674 BUG_ON(node
->ref_mod
!= 1);
1675 if (node
->action
== BTRFS_ADD_DELAYED_REF
&& insert_reserved
) {
1676 BUG_ON(!extent_op
|| !extent_op
->update_flags
||
1677 !extent_op
->update_key
);
1678 ret
= alloc_reserved_tree_block(trans
, root
,
1680 extent_op
->flags_to_set
,
1683 update_reserved_extents(root
, ins
.objectid
, ins
.offset
, 0);
1684 } else if (node
->action
== BTRFS_ADD_DELAYED_REF
) {
1685 ret
= __btrfs_inc_extent_ref(trans
, root
, node
->bytenr
,
1686 node
->num_bytes
, parent
, ref_root
,
1687 ref
->level
, 0, 1, extent_op
);
1688 } else if (node
->action
== BTRFS_DROP_DELAYED_REF
) {
1689 ret
= __btrfs_free_extent(trans
, root
, node
->bytenr
,
1690 node
->num_bytes
, parent
, ref_root
,
1691 ref
->level
, 0, 1, extent_op
);
1699 /* helper function to actually process a single delayed ref entry */
1700 static int run_one_delayed_ref(struct btrfs_trans_handle
*trans
,
1701 struct btrfs_root
*root
,
1702 struct btrfs_delayed_ref_node
*node
,
1703 struct btrfs_delayed_extent_op
*extent_op
,
1704 int insert_reserved
)
1707 if (btrfs_delayed_ref_is_head(node
)) {
1708 struct btrfs_delayed_ref_head
*head
;
1710 * we've hit the end of the chain and we were supposed
1711 * to insert this extent into the tree. But, it got
1712 * deleted before we ever needed to insert it, so all
1713 * we have to do is clean up the accounting
1716 head
= btrfs_delayed_node_to_head(node
);
1717 if (insert_reserved
) {
1718 if (head
->is_data
) {
1719 ret
= btrfs_del_csums(trans
, root
,
1724 btrfs_update_pinned_extents(root
, node
->bytenr
,
1725 node
->num_bytes
, 1);
1726 update_reserved_extents(root
, node
->bytenr
,
1727 node
->num_bytes
, 0);
1729 mutex_unlock(&head
->mutex
);
1733 if (node
->type
== BTRFS_TREE_BLOCK_REF_KEY
||
1734 node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
)
1735 ret
= run_delayed_tree_ref(trans
, root
, node
, extent_op
,
1737 else if (node
->type
== BTRFS_EXTENT_DATA_REF_KEY
||
1738 node
->type
== BTRFS_SHARED_DATA_REF_KEY
)
1739 ret
= run_delayed_data_ref(trans
, root
, node
, extent_op
,
1746 static noinline
struct btrfs_delayed_ref_node
*
1747 select_delayed_ref(struct btrfs_delayed_ref_head
*head
)
1749 struct rb_node
*node
;
1750 struct btrfs_delayed_ref_node
*ref
;
1751 int action
= BTRFS_ADD_DELAYED_REF
;
1754 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
1755 * this prevents ref count from going down to zero when
1756 * there still are pending delayed ref.
1758 node
= rb_prev(&head
->node
.rb_node
);
1762 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
,
1764 if (ref
->bytenr
!= head
->node
.bytenr
)
1766 if (ref
->action
== action
)
1768 node
= rb_prev(node
);
1770 if (action
== BTRFS_ADD_DELAYED_REF
) {
1771 action
= BTRFS_DROP_DELAYED_REF
;
1777 static noinline
int run_clustered_refs(struct btrfs_trans_handle
*trans
,
1778 struct btrfs_root
*root
,
1779 struct list_head
*cluster
)
1781 struct btrfs_delayed_ref_root
*delayed_refs
;
1782 struct btrfs_delayed_ref_node
*ref
;
1783 struct btrfs_delayed_ref_head
*locked_ref
= NULL
;
1784 struct btrfs_delayed_extent_op
*extent_op
;
1787 int must_insert_reserved
= 0;
1789 delayed_refs
= &trans
->transaction
->delayed_refs
;
1792 /* pick a new head ref from the cluster list */
1793 if (list_empty(cluster
))
1796 locked_ref
= list_entry(cluster
->next
,
1797 struct btrfs_delayed_ref_head
, cluster
);
1799 /* grab the lock that says we are going to process
1800 * all the refs for this head */
1801 ret
= btrfs_delayed_ref_lock(trans
, locked_ref
);
1804 * we may have dropped the spin lock to get the head
1805 * mutex lock, and that might have given someone else
1806 * time to free the head. If that's true, it has been
1807 * removed from our list and we can move on.
1809 if (ret
== -EAGAIN
) {
1817 * record the must insert reserved flag before we
1818 * drop the spin lock.
1820 must_insert_reserved
= locked_ref
->must_insert_reserved
;
1821 locked_ref
->must_insert_reserved
= 0;
1823 extent_op
= locked_ref
->extent_op
;
1824 locked_ref
->extent_op
= NULL
;
1827 * locked_ref is the head node, so we have to go one
1828 * node back for any delayed ref updates
1830 ref
= select_delayed_ref(locked_ref
);
1832 /* All delayed refs have been processed, Go ahead
1833 * and send the head node to run_one_delayed_ref,
1834 * so that any accounting fixes can happen
1836 ref
= &locked_ref
->node
;
1838 if (extent_op
&& must_insert_reserved
) {
1844 spin_unlock(&delayed_refs
->lock
);
1846 ret
= run_delayed_extent_op(trans
, root
,
1852 spin_lock(&delayed_refs
->lock
);
1856 list_del_init(&locked_ref
->cluster
);
1861 rb_erase(&ref
->rb_node
, &delayed_refs
->root
);
1862 delayed_refs
->num_entries
--;
1864 spin_unlock(&delayed_refs
->lock
);
1866 ret
= run_one_delayed_ref(trans
, root
, ref
, extent_op
,
1867 must_insert_reserved
);
1870 btrfs_put_delayed_ref(ref
);
1875 spin_lock(&delayed_refs
->lock
);
1881 * this starts processing the delayed reference count updates and
1882 * extent insertions we have queued up so far. count can be
1883 * 0, which means to process everything in the tree at the start
1884 * of the run (but not newly added entries), or it can be some target
1885 * number you'd like to process.
1887 int btrfs_run_delayed_refs(struct btrfs_trans_handle
*trans
,
1888 struct btrfs_root
*root
, unsigned long count
)
1890 struct rb_node
*node
;
1891 struct btrfs_delayed_ref_root
*delayed_refs
;
1892 struct btrfs_delayed_ref_node
*ref
;
1893 struct list_head cluster
;
1895 int run_all
= count
== (unsigned long)-1;
1898 if (root
== root
->fs_info
->extent_root
)
1899 root
= root
->fs_info
->tree_root
;
1901 delayed_refs
= &trans
->transaction
->delayed_refs
;
1902 INIT_LIST_HEAD(&cluster
);
1904 spin_lock(&delayed_refs
->lock
);
1906 count
= delayed_refs
->num_entries
* 2;
1910 if (!(run_all
|| run_most
) &&
1911 delayed_refs
->num_heads_ready
< 64)
1915 * go find something we can process in the rbtree. We start at
1916 * the beginning of the tree, and then build a cluster
1917 * of refs to process starting at the first one we are able to
1920 ret
= btrfs_find_ref_cluster(trans
, &cluster
,
1921 delayed_refs
->run_delayed_start
);
1925 ret
= run_clustered_refs(trans
, root
, &cluster
);
1928 count
-= min_t(unsigned long, ret
, count
);
1935 node
= rb_first(&delayed_refs
->root
);
1938 count
= (unsigned long)-1;
1941 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
,
1943 if (btrfs_delayed_ref_is_head(ref
)) {
1944 struct btrfs_delayed_ref_head
*head
;
1946 head
= btrfs_delayed_node_to_head(ref
);
1947 atomic_inc(&ref
->refs
);
1949 spin_unlock(&delayed_refs
->lock
);
1950 mutex_lock(&head
->mutex
);
1951 mutex_unlock(&head
->mutex
);
1953 btrfs_put_delayed_ref(ref
);
1957 node
= rb_next(node
);
1959 spin_unlock(&delayed_refs
->lock
);
1960 schedule_timeout(1);
1964 spin_unlock(&delayed_refs
->lock
);
1968 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle
*trans
,
1969 struct btrfs_root
*root
,
1970 u64 bytenr
, u64 num_bytes
, u64 flags
,
1973 struct btrfs_delayed_extent_op
*extent_op
;
1976 extent_op
= kmalloc(sizeof(*extent_op
), GFP_NOFS
);
1980 extent_op
->flags_to_set
= flags
;
1981 extent_op
->update_flags
= 1;
1982 extent_op
->update_key
= 0;
1983 extent_op
->is_data
= is_data
? 1 : 0;
1985 ret
= btrfs_add_delayed_extent_op(trans
, bytenr
, num_bytes
, extent_op
);
1991 static noinline
int check_delayed_ref(struct btrfs_trans_handle
*trans
,
1992 struct btrfs_root
*root
,
1993 struct btrfs_path
*path
,
1994 u64 objectid
, u64 offset
, u64 bytenr
)
1996 struct btrfs_delayed_ref_head
*head
;
1997 struct btrfs_delayed_ref_node
*ref
;
1998 struct btrfs_delayed_data_ref
*data_ref
;
1999 struct btrfs_delayed_ref_root
*delayed_refs
;
2000 struct rb_node
*node
;
2004 delayed_refs
= &trans
->transaction
->delayed_refs
;
2005 spin_lock(&delayed_refs
->lock
);
2006 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
2010 if (!mutex_trylock(&head
->mutex
)) {
2011 atomic_inc(&head
->node
.refs
);
2012 spin_unlock(&delayed_refs
->lock
);
2014 btrfs_release_path(root
->fs_info
->extent_root
, path
);
2016 mutex_lock(&head
->mutex
);
2017 mutex_unlock(&head
->mutex
);
2018 btrfs_put_delayed_ref(&head
->node
);
2022 node
= rb_prev(&head
->node
.rb_node
);
2026 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
2028 if (ref
->bytenr
!= bytenr
)
2032 if (ref
->type
!= BTRFS_EXTENT_DATA_REF_KEY
)
2035 data_ref
= btrfs_delayed_node_to_data_ref(ref
);
2037 node
= rb_prev(node
);
2039 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
2040 if (ref
->bytenr
== bytenr
)
2044 if (data_ref
->root
!= root
->root_key
.objectid
||
2045 data_ref
->objectid
!= objectid
|| data_ref
->offset
!= offset
)
2050 mutex_unlock(&head
->mutex
);
2052 spin_unlock(&delayed_refs
->lock
);
2056 static noinline
int check_committed_ref(struct btrfs_trans_handle
*trans
,
2057 struct btrfs_root
*root
,
2058 struct btrfs_path
*path
,
2059 u64 objectid
, u64 offset
, u64 bytenr
)
2061 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
2062 struct extent_buffer
*leaf
;
2063 struct btrfs_extent_data_ref
*ref
;
2064 struct btrfs_extent_inline_ref
*iref
;
2065 struct btrfs_extent_item
*ei
;
2066 struct btrfs_key key
;
2070 key
.objectid
= bytenr
;
2071 key
.offset
= (u64
)-1;
2072 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2074 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
2080 if (path
->slots
[0] == 0)
2084 leaf
= path
->nodes
[0];
2085 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2087 if (key
.objectid
!= bytenr
|| key
.type
!= BTRFS_EXTENT_ITEM_KEY
)
2091 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2092 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2093 if (item_size
< sizeof(*ei
)) {
2094 WARN_ON(item_size
!= sizeof(struct btrfs_extent_item_v0
));
2098 ei
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
2100 if (item_size
!= sizeof(*ei
) +
2101 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY
))
2104 if (btrfs_extent_generation(leaf
, ei
) <=
2105 btrfs_root_last_snapshot(&root
->root_item
))
2108 iref
= (struct btrfs_extent_inline_ref
*)(ei
+ 1);
2109 if (btrfs_extent_inline_ref_type(leaf
, iref
) !=
2110 BTRFS_EXTENT_DATA_REF_KEY
)
2113 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
2114 if (btrfs_extent_refs(leaf
, ei
) !=
2115 btrfs_extent_data_ref_count(leaf
, ref
) ||
2116 btrfs_extent_data_ref_root(leaf
, ref
) !=
2117 root
->root_key
.objectid
||
2118 btrfs_extent_data_ref_objectid(leaf
, ref
) != objectid
||
2119 btrfs_extent_data_ref_offset(leaf
, ref
) != offset
)
2127 int btrfs_cross_ref_exist(struct btrfs_trans_handle
*trans
,
2128 struct btrfs_root
*root
,
2129 u64 objectid
, u64 offset
, u64 bytenr
)
2131 struct btrfs_path
*path
;
2135 path
= btrfs_alloc_path();
2140 ret
= check_committed_ref(trans
, root
, path
, objectid
,
2142 if (ret
&& ret
!= -ENOENT
)
2145 ret2
= check_delayed_ref(trans
, root
, path
, objectid
,
2147 } while (ret2
== -EAGAIN
);
2149 if (ret2
&& ret2
!= -ENOENT
) {
2154 if (ret
!= -ENOENT
|| ret2
!= -ENOENT
)
2157 btrfs_free_path(path
);
2162 int btrfs_cache_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2163 struct extent_buffer
*buf
, u32 nr_extents
)
2165 struct btrfs_key key
;
2166 struct btrfs_file_extent_item
*fi
;
2174 if (!root
->ref_cows
)
2177 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
2179 root_gen
= root
->root_key
.offset
;
2182 root_gen
= trans
->transid
- 1;
2185 level
= btrfs_header_level(buf
);
2186 nritems
= btrfs_header_nritems(buf
);
2189 struct btrfs_leaf_ref
*ref
;
2190 struct btrfs_extent_info
*info
;
2192 ref
= btrfs_alloc_leaf_ref(root
, nr_extents
);
2198 ref
->root_gen
= root_gen
;
2199 ref
->bytenr
= buf
->start
;
2200 ref
->owner
= btrfs_header_owner(buf
);
2201 ref
->generation
= btrfs_header_generation(buf
);
2202 ref
->nritems
= nr_extents
;
2203 info
= ref
->extents
;
2205 for (i
= 0; nr_extents
> 0 && i
< nritems
; i
++) {
2207 btrfs_item_key_to_cpu(buf
, &key
, i
);
2208 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
2210 fi
= btrfs_item_ptr(buf
, i
,
2211 struct btrfs_file_extent_item
);
2212 if (btrfs_file_extent_type(buf
, fi
) ==
2213 BTRFS_FILE_EXTENT_INLINE
)
2215 disk_bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
2216 if (disk_bytenr
== 0)
2219 info
->bytenr
= disk_bytenr
;
2221 btrfs_file_extent_disk_num_bytes(buf
, fi
);
2222 info
->objectid
= key
.objectid
;
2223 info
->offset
= key
.offset
;
2227 ret
= btrfs_add_leaf_ref(root
, ref
, shared
);
2228 if (ret
== -EEXIST
&& shared
) {
2229 struct btrfs_leaf_ref
*old
;
2230 old
= btrfs_lookup_leaf_ref(root
, ref
->bytenr
);
2232 btrfs_remove_leaf_ref(root
, old
);
2233 btrfs_free_leaf_ref(root
, old
);
2234 ret
= btrfs_add_leaf_ref(root
, ref
, shared
);
2237 btrfs_free_leaf_ref(root
, ref
);
2243 /* when a block goes through cow, we update the reference counts of
2244 * everything that block points to. The internal pointers of the block
2245 * can be in just about any order, and it is likely to have clusters of
2246 * things that are close together and clusters of things that are not.
2248 * To help reduce the seeks that come with updating all of these reference
2249 * counts, sort them by byte number before actual updates are done.
2251 * struct refsort is used to match byte number to slot in the btree block.
2252 * we sort based on the byte number and then use the slot to actually
2255 * struct refsort is smaller than strcut btrfs_item and smaller than
2256 * struct btrfs_key_ptr. Since we're currently limited to the page size
2257 * for a btree block, there's no way for a kmalloc of refsorts for a
2258 * single node to be bigger than a page.
2266 * for passing into sort()
2268 static int refsort_cmp(const void *a_void
, const void *b_void
)
2270 const struct refsort
*a
= a_void
;
2271 const struct refsort
*b
= b_void
;
2273 if (a
->bytenr
< b
->bytenr
)
2275 if (a
->bytenr
> b
->bytenr
)
2281 static int __btrfs_mod_ref(struct btrfs_trans_handle
*trans
,
2282 struct btrfs_root
*root
,
2283 struct extent_buffer
*buf
,
2284 int full_backref
, int inc
)
2291 struct btrfs_key key
;
2292 struct btrfs_file_extent_item
*fi
;
2296 int (*process_func
)(struct btrfs_trans_handle
*, struct btrfs_root
*,
2297 u64
, u64
, u64
, u64
, u64
, u64
);
2299 ref_root
= btrfs_header_owner(buf
);
2300 nritems
= btrfs_header_nritems(buf
);
2301 level
= btrfs_header_level(buf
);
2303 if (!root
->ref_cows
&& level
== 0)
2307 process_func
= btrfs_inc_extent_ref
;
2309 process_func
= btrfs_free_extent
;
2312 parent
= buf
->start
;
2316 for (i
= 0; i
< nritems
; i
++) {
2318 btrfs_item_key_to_cpu(buf
, &key
, i
);
2319 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
2321 fi
= btrfs_item_ptr(buf
, i
,
2322 struct btrfs_file_extent_item
);
2323 if (btrfs_file_extent_type(buf
, fi
) ==
2324 BTRFS_FILE_EXTENT_INLINE
)
2326 bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
2330 num_bytes
= btrfs_file_extent_disk_num_bytes(buf
, fi
);
2331 key
.offset
-= btrfs_file_extent_offset(buf
, fi
);
2332 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
2333 parent
, ref_root
, key
.objectid
,
2338 bytenr
= btrfs_node_blockptr(buf
, i
);
2339 num_bytes
= btrfs_level_size(root
, level
- 1);
2340 ret
= process_func(trans
, root
, bytenr
, num_bytes
,
2341 parent
, ref_root
, level
- 1, 0);
2352 int btrfs_inc_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2353 struct extent_buffer
*buf
, int full_backref
)
2355 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 1);
2358 int btrfs_dec_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2359 struct extent_buffer
*buf
, int full_backref
)
2361 return __btrfs_mod_ref(trans
, root
, buf
, full_backref
, 0);
2364 static int write_one_cache_group(struct btrfs_trans_handle
*trans
,
2365 struct btrfs_root
*root
,
2366 struct btrfs_path
*path
,
2367 struct btrfs_block_group_cache
*cache
)
2370 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
2372 struct extent_buffer
*leaf
;
2374 ret
= btrfs_search_slot(trans
, extent_root
, &cache
->key
, path
, 0, 1);
2379 leaf
= path
->nodes
[0];
2380 bi
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
2381 write_extent_buffer(leaf
, &cache
->item
, bi
, sizeof(cache
->item
));
2382 btrfs_mark_buffer_dirty(leaf
);
2383 btrfs_release_path(extent_root
, path
);
2391 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle
*trans
,
2392 struct btrfs_root
*root
)
2394 struct btrfs_block_group_cache
*cache
, *entry
;
2398 struct btrfs_path
*path
;
2401 path
= btrfs_alloc_path();
2407 spin_lock(&root
->fs_info
->block_group_cache_lock
);
2408 for (n
= rb_first(&root
->fs_info
->block_group_cache_tree
);
2409 n
; n
= rb_next(n
)) {
2410 entry
= rb_entry(n
, struct btrfs_block_group_cache
,
2417 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
2423 last
+= cache
->key
.offset
;
2425 err
= write_one_cache_group(trans
, root
,
2428 * if we fail to write the cache group, we want
2429 * to keep it marked dirty in hopes that a later
2437 btrfs_free_path(path
);
2441 int btrfs_extent_readonly(struct btrfs_root
*root
, u64 bytenr
)
2443 struct btrfs_block_group_cache
*block_group
;
2446 block_group
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
2447 if (!block_group
|| block_group
->ro
)
2450 btrfs_put_block_group(block_group
);
2454 static int update_space_info(struct btrfs_fs_info
*info
, u64 flags
,
2455 u64 total_bytes
, u64 bytes_used
,
2456 struct btrfs_space_info
**space_info
)
2458 struct btrfs_space_info
*found
;
2460 found
= __find_space_info(info
, flags
);
2462 spin_lock(&found
->lock
);
2463 found
->total_bytes
+= total_bytes
;
2464 found
->bytes_used
+= bytes_used
;
2466 spin_unlock(&found
->lock
);
2467 *space_info
= found
;
2470 found
= kzalloc(sizeof(*found
), GFP_NOFS
);
2474 INIT_LIST_HEAD(&found
->block_groups
);
2475 init_rwsem(&found
->groups_sem
);
2476 spin_lock_init(&found
->lock
);
2477 found
->flags
= flags
;
2478 found
->total_bytes
= total_bytes
;
2479 found
->bytes_used
= bytes_used
;
2480 found
->bytes_pinned
= 0;
2481 found
->bytes_reserved
= 0;
2482 found
->bytes_readonly
= 0;
2483 found
->bytes_delalloc
= 0;
2485 found
->force_alloc
= 0;
2486 *space_info
= found
;
2487 list_add_rcu(&found
->list
, &info
->space_info
);
2491 static void set_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
2493 u64 extra_flags
= flags
& (BTRFS_BLOCK_GROUP_RAID0
|
2494 BTRFS_BLOCK_GROUP_RAID1
|
2495 BTRFS_BLOCK_GROUP_RAID10
|
2496 BTRFS_BLOCK_GROUP_DUP
);
2498 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
2499 fs_info
->avail_data_alloc_bits
|= extra_flags
;
2500 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
2501 fs_info
->avail_metadata_alloc_bits
|= extra_flags
;
2502 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
2503 fs_info
->avail_system_alloc_bits
|= extra_flags
;
2507 static void set_block_group_readonly(struct btrfs_block_group_cache
*cache
)
2509 spin_lock(&cache
->space_info
->lock
);
2510 spin_lock(&cache
->lock
);
2512 cache
->space_info
->bytes_readonly
+= cache
->key
.offset
-
2513 btrfs_block_group_used(&cache
->item
);
2516 spin_unlock(&cache
->lock
);
2517 spin_unlock(&cache
->space_info
->lock
);
2520 u64
btrfs_reduce_alloc_profile(struct btrfs_root
*root
, u64 flags
)
2522 u64 num_devices
= root
->fs_info
->fs_devices
->rw_devices
;
2524 if (num_devices
== 1)
2525 flags
&= ~(BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID0
);
2526 if (num_devices
< 4)
2527 flags
&= ~BTRFS_BLOCK_GROUP_RAID10
;
2529 if ((flags
& BTRFS_BLOCK_GROUP_DUP
) &&
2530 (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
2531 BTRFS_BLOCK_GROUP_RAID10
))) {
2532 flags
&= ~BTRFS_BLOCK_GROUP_DUP
;
2535 if ((flags
& BTRFS_BLOCK_GROUP_RAID1
) &&
2536 (flags
& BTRFS_BLOCK_GROUP_RAID10
)) {
2537 flags
&= ~BTRFS_BLOCK_GROUP_RAID1
;
2540 if ((flags
& BTRFS_BLOCK_GROUP_RAID0
) &&
2541 ((flags
& BTRFS_BLOCK_GROUP_RAID1
) |
2542 (flags
& BTRFS_BLOCK_GROUP_RAID10
) |
2543 (flags
& BTRFS_BLOCK_GROUP_DUP
)))
2544 flags
&= ~BTRFS_BLOCK_GROUP_RAID0
;
2548 static u64
btrfs_get_alloc_profile(struct btrfs_root
*root
, u64 data
)
2550 struct btrfs_fs_info
*info
= root
->fs_info
;
2554 alloc_profile
= info
->avail_data_alloc_bits
&
2555 info
->data_alloc_profile
;
2556 data
= BTRFS_BLOCK_GROUP_DATA
| alloc_profile
;
2557 } else if (root
== root
->fs_info
->chunk_root
) {
2558 alloc_profile
= info
->avail_system_alloc_bits
&
2559 info
->system_alloc_profile
;
2560 data
= BTRFS_BLOCK_GROUP_SYSTEM
| alloc_profile
;
2562 alloc_profile
= info
->avail_metadata_alloc_bits
&
2563 info
->metadata_alloc_profile
;
2564 data
= BTRFS_BLOCK_GROUP_METADATA
| alloc_profile
;
2567 return btrfs_reduce_alloc_profile(root
, data
);
2570 void btrfs_set_inode_space_info(struct btrfs_root
*root
, struct inode
*inode
)
2574 alloc_target
= btrfs_get_alloc_profile(root
, 1);
2575 BTRFS_I(inode
)->space_info
= __find_space_info(root
->fs_info
,
2580 * for now this just makes sure we have at least 5% of our metadata space free
2583 int btrfs_check_metadata_free_space(struct btrfs_root
*root
)
2585 struct btrfs_fs_info
*info
= root
->fs_info
;
2586 struct btrfs_space_info
*meta_sinfo
;
2587 u64 alloc_target
, thresh
;
2588 int committed
= 0, ret
;
2590 /* get the space info for where the metadata will live */
2591 alloc_target
= btrfs_get_alloc_profile(root
, 0);
2592 meta_sinfo
= __find_space_info(info
, alloc_target
);
2595 spin_lock(&meta_sinfo
->lock
);
2596 if (!meta_sinfo
->full
)
2597 thresh
= meta_sinfo
->total_bytes
* 80;
2599 thresh
= meta_sinfo
->total_bytes
* 95;
2601 do_div(thresh
, 100);
2603 if (meta_sinfo
->bytes_used
+ meta_sinfo
->bytes_reserved
+
2604 meta_sinfo
->bytes_pinned
+ meta_sinfo
->bytes_readonly
> thresh
) {
2605 struct btrfs_trans_handle
*trans
;
2606 if (!meta_sinfo
->full
) {
2607 meta_sinfo
->force_alloc
= 1;
2608 spin_unlock(&meta_sinfo
->lock
);
2610 trans
= btrfs_start_transaction(root
, 1);
2614 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
2615 2 * 1024 * 1024, alloc_target
, 0);
2616 btrfs_end_transaction(trans
, root
);
2619 spin_unlock(&meta_sinfo
->lock
);
2623 trans
= btrfs_join_transaction(root
, 1);
2626 ret
= btrfs_commit_transaction(trans
, root
);
2633 spin_unlock(&meta_sinfo
->lock
);
2639 * This will check the space that the inode allocates from to make sure we have
2640 * enough space for bytes.
2642 int btrfs_check_data_free_space(struct btrfs_root
*root
, struct inode
*inode
,
2645 struct btrfs_space_info
*data_sinfo
;
2646 int ret
= 0, committed
= 0;
2648 /* make sure bytes are sectorsize aligned */
2649 bytes
= (bytes
+ root
->sectorsize
- 1) & ~((u64
)root
->sectorsize
- 1);
2651 data_sinfo
= BTRFS_I(inode
)->space_info
;
2653 /* make sure we have enough space to handle the data first */
2654 spin_lock(&data_sinfo
->lock
);
2655 if (data_sinfo
->total_bytes
- data_sinfo
->bytes_used
-
2656 data_sinfo
->bytes_delalloc
- data_sinfo
->bytes_reserved
-
2657 data_sinfo
->bytes_pinned
- data_sinfo
->bytes_readonly
-
2658 data_sinfo
->bytes_may_use
< bytes
) {
2659 struct btrfs_trans_handle
*trans
;
2662 * if we don't have enough free bytes in this space then we need
2663 * to alloc a new chunk.
2665 if (!data_sinfo
->full
) {
2668 data_sinfo
->force_alloc
= 1;
2669 spin_unlock(&data_sinfo
->lock
);
2671 alloc_target
= btrfs_get_alloc_profile(root
, 1);
2672 trans
= btrfs_start_transaction(root
, 1);
2676 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
2677 bytes
+ 2 * 1024 * 1024,
2679 btrfs_end_transaction(trans
, root
);
2684 spin_unlock(&data_sinfo
->lock
);
2686 /* commit the current transaction and try again */
2689 trans
= btrfs_join_transaction(root
, 1);
2692 ret
= btrfs_commit_transaction(trans
, root
);
2698 printk(KERN_ERR
"no space left, need %llu, %llu delalloc bytes"
2699 ", %llu bytes_used, %llu bytes_reserved, "
2700 "%llu bytes_pinned, %llu bytes_readonly, %llu may use"
2701 "%llu total\n", (unsigned long long)bytes
,
2702 (unsigned long long)data_sinfo
->bytes_delalloc
,
2703 (unsigned long long)data_sinfo
->bytes_used
,
2704 (unsigned long long)data_sinfo
->bytes_reserved
,
2705 (unsigned long long)data_sinfo
->bytes_pinned
,
2706 (unsigned long long)data_sinfo
->bytes_readonly
,
2707 (unsigned long long)data_sinfo
->bytes_may_use
,
2708 (unsigned long long)data_sinfo
->total_bytes
);
2711 data_sinfo
->bytes_may_use
+= bytes
;
2712 BTRFS_I(inode
)->reserved_bytes
+= bytes
;
2713 spin_unlock(&data_sinfo
->lock
);
2715 return btrfs_check_metadata_free_space(root
);
2719 * if there was an error for whatever reason after calling
2720 * btrfs_check_data_free_space, call this so we can cleanup the counters.
2722 void btrfs_free_reserved_data_space(struct btrfs_root
*root
,
2723 struct inode
*inode
, u64 bytes
)
2725 struct btrfs_space_info
*data_sinfo
;
2727 /* make sure bytes are sectorsize aligned */
2728 bytes
= (bytes
+ root
->sectorsize
- 1) & ~((u64
)root
->sectorsize
- 1);
2730 data_sinfo
= BTRFS_I(inode
)->space_info
;
2731 spin_lock(&data_sinfo
->lock
);
2732 data_sinfo
->bytes_may_use
-= bytes
;
2733 BTRFS_I(inode
)->reserved_bytes
-= bytes
;
2734 spin_unlock(&data_sinfo
->lock
);
2737 /* called when we are adding a delalloc extent to the inode's io_tree */
2738 void btrfs_delalloc_reserve_space(struct btrfs_root
*root
, struct inode
*inode
,
2741 struct btrfs_space_info
*data_sinfo
;
2743 /* get the space info for where this inode will be storing its data */
2744 data_sinfo
= BTRFS_I(inode
)->space_info
;
2746 /* make sure we have enough space to handle the data first */
2747 spin_lock(&data_sinfo
->lock
);
2748 data_sinfo
->bytes_delalloc
+= bytes
;
2751 * we are adding a delalloc extent without calling
2752 * btrfs_check_data_free_space first. This happens on a weird
2753 * writepage condition, but shouldn't hurt our accounting
2755 if (unlikely(bytes
> BTRFS_I(inode
)->reserved_bytes
)) {
2756 data_sinfo
->bytes_may_use
-= BTRFS_I(inode
)->reserved_bytes
;
2757 BTRFS_I(inode
)->reserved_bytes
= 0;
2759 data_sinfo
->bytes_may_use
-= bytes
;
2760 BTRFS_I(inode
)->reserved_bytes
-= bytes
;
2763 spin_unlock(&data_sinfo
->lock
);
2766 /* called when we are clearing an delalloc extent from the inode's io_tree */
2767 void btrfs_delalloc_free_space(struct btrfs_root
*root
, struct inode
*inode
,
2770 struct btrfs_space_info
*info
;
2772 info
= BTRFS_I(inode
)->space_info
;
2774 spin_lock(&info
->lock
);
2775 info
->bytes_delalloc
-= bytes
;
2776 spin_unlock(&info
->lock
);
2779 static void force_metadata_allocation(struct btrfs_fs_info
*info
)
2781 struct list_head
*head
= &info
->space_info
;
2782 struct btrfs_space_info
*found
;
2785 list_for_each_entry_rcu(found
, head
, list
) {
2786 if (found
->flags
& BTRFS_BLOCK_GROUP_METADATA
)
2787 found
->force_alloc
= 1;
2792 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
2793 struct btrfs_root
*extent_root
, u64 alloc_bytes
,
2794 u64 flags
, int force
)
2796 struct btrfs_space_info
*space_info
;
2797 struct btrfs_fs_info
*fs_info
= extent_root
->fs_info
;
2801 mutex_lock(&fs_info
->chunk_mutex
);
2803 flags
= btrfs_reduce_alloc_profile(extent_root
, flags
);
2805 space_info
= __find_space_info(extent_root
->fs_info
, flags
);
2807 ret
= update_space_info(extent_root
->fs_info
, flags
,
2811 BUG_ON(!space_info
);
2813 spin_lock(&space_info
->lock
);
2814 if (space_info
->force_alloc
) {
2816 space_info
->force_alloc
= 0;
2818 if (space_info
->full
) {
2819 spin_unlock(&space_info
->lock
);
2823 thresh
= space_info
->total_bytes
- space_info
->bytes_readonly
;
2824 thresh
= div_factor(thresh
, 6);
2826 (space_info
->bytes_used
+ space_info
->bytes_pinned
+
2827 space_info
->bytes_reserved
+ alloc_bytes
) < thresh
) {
2828 spin_unlock(&space_info
->lock
);
2831 spin_unlock(&space_info
->lock
);
2834 * if we're doing a data chunk, go ahead and make sure that
2835 * we keep a reasonable number of metadata chunks allocated in the
2838 if (flags
& BTRFS_BLOCK_GROUP_DATA
) {
2839 fs_info
->data_chunk_allocations
++;
2840 if (!(fs_info
->data_chunk_allocations
%
2841 fs_info
->metadata_ratio
))
2842 force_metadata_allocation(fs_info
);
2845 ret
= btrfs_alloc_chunk(trans
, extent_root
, flags
);
2847 space_info
->full
= 1;
2849 mutex_unlock(&extent_root
->fs_info
->chunk_mutex
);
2853 static int update_block_group(struct btrfs_trans_handle
*trans
,
2854 struct btrfs_root
*root
,
2855 u64 bytenr
, u64 num_bytes
, int alloc
,
2858 struct btrfs_block_group_cache
*cache
;
2859 struct btrfs_fs_info
*info
= root
->fs_info
;
2860 u64 total
= num_bytes
;
2864 /* block accounting for super block */
2865 spin_lock(&info
->delalloc_lock
);
2866 old_val
= btrfs_super_bytes_used(&info
->super_copy
);
2868 old_val
+= num_bytes
;
2870 old_val
-= num_bytes
;
2871 btrfs_set_super_bytes_used(&info
->super_copy
, old_val
);
2873 /* block accounting for root item */
2874 old_val
= btrfs_root_used(&root
->root_item
);
2876 old_val
+= num_bytes
;
2878 old_val
-= num_bytes
;
2879 btrfs_set_root_used(&root
->root_item
, old_val
);
2880 spin_unlock(&info
->delalloc_lock
);
2883 cache
= btrfs_lookup_block_group(info
, bytenr
);
2886 byte_in_group
= bytenr
- cache
->key
.objectid
;
2887 WARN_ON(byte_in_group
> cache
->key
.offset
);
2889 spin_lock(&cache
->space_info
->lock
);
2890 spin_lock(&cache
->lock
);
2892 old_val
= btrfs_block_group_used(&cache
->item
);
2893 num_bytes
= min(total
, cache
->key
.offset
- byte_in_group
);
2895 old_val
+= num_bytes
;
2896 cache
->space_info
->bytes_used
+= num_bytes
;
2898 cache
->space_info
->bytes_readonly
-= num_bytes
;
2899 btrfs_set_block_group_used(&cache
->item
, old_val
);
2900 spin_unlock(&cache
->lock
);
2901 spin_unlock(&cache
->space_info
->lock
);
2903 old_val
-= num_bytes
;
2904 cache
->space_info
->bytes_used
-= num_bytes
;
2906 cache
->space_info
->bytes_readonly
+= num_bytes
;
2907 btrfs_set_block_group_used(&cache
->item
, old_val
);
2908 spin_unlock(&cache
->lock
);
2909 spin_unlock(&cache
->space_info
->lock
);
2913 ret
= btrfs_discard_extent(root
, bytenr
,
2917 ret
= btrfs_add_free_space(cache
, bytenr
,
2922 btrfs_put_block_group(cache
);
2924 bytenr
+= num_bytes
;
2929 static u64
first_logical_byte(struct btrfs_root
*root
, u64 search_start
)
2931 struct btrfs_block_group_cache
*cache
;
2934 cache
= btrfs_lookup_first_block_group(root
->fs_info
, search_start
);
2938 bytenr
= cache
->key
.objectid
;
2939 btrfs_put_block_group(cache
);
2944 int btrfs_update_pinned_extents(struct btrfs_root
*root
,
2945 u64 bytenr
, u64 num
, int pin
)
2948 struct btrfs_block_group_cache
*cache
;
2949 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2952 set_extent_dirty(&fs_info
->pinned_extents
,
2953 bytenr
, bytenr
+ num
- 1, GFP_NOFS
);
2955 clear_extent_dirty(&fs_info
->pinned_extents
,
2956 bytenr
, bytenr
+ num
- 1, GFP_NOFS
);
2960 cache
= btrfs_lookup_block_group(fs_info
, bytenr
);
2962 len
= min(num
, cache
->key
.offset
-
2963 (bytenr
- cache
->key
.objectid
));
2965 spin_lock(&cache
->space_info
->lock
);
2966 spin_lock(&cache
->lock
);
2967 cache
->pinned
+= len
;
2968 cache
->space_info
->bytes_pinned
+= len
;
2969 spin_unlock(&cache
->lock
);
2970 spin_unlock(&cache
->space_info
->lock
);
2971 fs_info
->total_pinned
+= len
;
2973 spin_lock(&cache
->space_info
->lock
);
2974 spin_lock(&cache
->lock
);
2975 cache
->pinned
-= len
;
2976 cache
->space_info
->bytes_pinned
-= len
;
2977 spin_unlock(&cache
->lock
);
2978 spin_unlock(&cache
->space_info
->lock
);
2979 fs_info
->total_pinned
-= len
;
2981 btrfs_add_free_space(cache
, bytenr
, len
);
2983 btrfs_put_block_group(cache
);
2990 static int update_reserved_extents(struct btrfs_root
*root
,
2991 u64 bytenr
, u64 num
, int reserve
)
2994 struct btrfs_block_group_cache
*cache
;
2995 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2998 cache
= btrfs_lookup_block_group(fs_info
, bytenr
);
3000 len
= min(num
, cache
->key
.offset
-
3001 (bytenr
- cache
->key
.objectid
));
3003 spin_lock(&cache
->space_info
->lock
);
3004 spin_lock(&cache
->lock
);
3006 cache
->reserved
+= len
;
3007 cache
->space_info
->bytes_reserved
+= len
;
3009 cache
->reserved
-= len
;
3010 cache
->space_info
->bytes_reserved
-= len
;
3012 spin_unlock(&cache
->lock
);
3013 spin_unlock(&cache
->space_info
->lock
);
3014 btrfs_put_block_group(cache
);
3021 int btrfs_copy_pinned(struct btrfs_root
*root
, struct extent_io_tree
*copy
)
3026 struct extent_io_tree
*pinned_extents
= &root
->fs_info
->pinned_extents
;
3030 ret
= find_first_extent_bit(pinned_extents
, last
,
3031 &start
, &end
, EXTENT_DIRTY
);
3034 set_extent_dirty(copy
, start
, end
, GFP_NOFS
);
3040 int btrfs_finish_extent_commit(struct btrfs_trans_handle
*trans
,
3041 struct btrfs_root
*root
,
3042 struct extent_io_tree
*unpin
)
3049 ret
= find_first_extent_bit(unpin
, 0, &start
, &end
,
3054 ret
= btrfs_discard_extent(root
, start
, end
+ 1 - start
);
3056 /* unlocks the pinned mutex */
3057 btrfs_update_pinned_extents(root
, start
, end
+ 1 - start
, 0);
3058 clear_extent_dirty(unpin
, start
, end
, GFP_NOFS
);
3065 static int pin_down_bytes(struct btrfs_trans_handle
*trans
,
3066 struct btrfs_root
*root
,
3067 struct btrfs_path
*path
,
3068 u64 bytenr
, u64 num_bytes
, int is_data
,
3069 struct extent_buffer
**must_clean
)
3072 struct extent_buffer
*buf
;
3077 buf
= btrfs_find_tree_block(root
, bytenr
, num_bytes
);
3081 /* we can reuse a block if it hasn't been written
3082 * and it is from this transaction. We can't
3083 * reuse anything from the tree log root because
3084 * it has tiny sub-transactions.
3086 if (btrfs_buffer_uptodate(buf
, 0) &&
3087 btrfs_try_tree_lock(buf
)) {
3088 u64 header_owner
= btrfs_header_owner(buf
);
3089 u64 header_transid
= btrfs_header_generation(buf
);
3090 if (header_owner
!= BTRFS_TREE_LOG_OBJECTID
&&
3091 header_transid
== trans
->transid
&&
3092 !btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
)) {
3096 btrfs_tree_unlock(buf
);
3098 free_extent_buffer(buf
);
3100 btrfs_set_path_blocking(path
);
3101 /* unlocks the pinned mutex */
3102 btrfs_update_pinned_extents(root
, bytenr
, num_bytes
, 1);
3109 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
3110 struct btrfs_root
*root
,
3111 u64 bytenr
, u64 num_bytes
, u64 parent
,
3112 u64 root_objectid
, u64 owner_objectid
,
3113 u64 owner_offset
, int refs_to_drop
,
3114 struct btrfs_delayed_extent_op
*extent_op
)
3116 struct btrfs_key key
;
3117 struct btrfs_path
*path
;
3118 struct btrfs_fs_info
*info
= root
->fs_info
;
3119 struct btrfs_root
*extent_root
= info
->extent_root
;
3120 struct extent_buffer
*leaf
;
3121 struct btrfs_extent_item
*ei
;
3122 struct btrfs_extent_inline_ref
*iref
;
3125 int extent_slot
= 0;
3126 int found_extent
= 0;
3131 path
= btrfs_alloc_path();
3136 path
->leave_spinning
= 1;
3138 is_data
= owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
;
3139 BUG_ON(!is_data
&& refs_to_drop
!= 1);
3141 ret
= lookup_extent_backref(trans
, extent_root
, path
, &iref
,
3142 bytenr
, num_bytes
, parent
,
3143 root_objectid
, owner_objectid
,
3146 extent_slot
= path
->slots
[0];
3147 while (extent_slot
>= 0) {
3148 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
3150 if (key
.objectid
!= bytenr
)
3152 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
3153 key
.offset
== num_bytes
) {
3157 if (path
->slots
[0] - extent_slot
> 5)
3161 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3162 item_size
= btrfs_item_size_nr(path
->nodes
[0], extent_slot
);
3163 if (found_extent
&& item_size
< sizeof(*ei
))
3166 if (!found_extent
) {
3168 ret
= remove_extent_backref(trans
, extent_root
, path
,
3172 btrfs_release_path(extent_root
, path
);
3173 path
->leave_spinning
= 1;
3175 key
.objectid
= bytenr
;
3176 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
3177 key
.offset
= num_bytes
;
3179 ret
= btrfs_search_slot(trans
, extent_root
,
3182 printk(KERN_ERR
"umm, got %d back from search"
3183 ", was looking for %llu\n", ret
,
3184 (unsigned long long)bytenr
);
3185 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
3188 extent_slot
= path
->slots
[0];
3191 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
3193 printk(KERN_ERR
"btrfs unable to find ref byte nr %llu "
3194 "parent %llu root %llu owner %llu offset %llu\n",
3195 (unsigned long long)bytenr
,
3196 (unsigned long long)parent
,
3197 (unsigned long long)root_objectid
,
3198 (unsigned long long)owner_objectid
,
3199 (unsigned long long)owner_offset
);
3202 leaf
= path
->nodes
[0];
3203 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
3204 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3205 if (item_size
< sizeof(*ei
)) {
3206 BUG_ON(found_extent
|| extent_slot
!= path
->slots
[0]);
3207 ret
= convert_extent_item_v0(trans
, extent_root
, path
,
3211 btrfs_release_path(extent_root
, path
);
3212 path
->leave_spinning
= 1;
3214 key
.objectid
= bytenr
;
3215 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
3216 key
.offset
= num_bytes
;
3218 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
,
3221 printk(KERN_ERR
"umm, got %d back from search"
3222 ", was looking for %llu\n", ret
,
3223 (unsigned long long)bytenr
);
3224 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
3227 extent_slot
= path
->slots
[0];
3228 leaf
= path
->nodes
[0];
3229 item_size
= btrfs_item_size_nr(leaf
, extent_slot
);
3232 BUG_ON(item_size
< sizeof(*ei
));
3233 ei
= btrfs_item_ptr(leaf
, extent_slot
,
3234 struct btrfs_extent_item
);
3235 if (owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
) {
3236 struct btrfs_tree_block_info
*bi
;
3237 BUG_ON(item_size
< sizeof(*ei
) + sizeof(*bi
));
3238 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
3239 WARN_ON(owner_objectid
!= btrfs_tree_block_level(leaf
, bi
));
3242 refs
= btrfs_extent_refs(leaf
, ei
);
3243 BUG_ON(refs
< refs_to_drop
);
3244 refs
-= refs_to_drop
;
3248 __run_delayed_extent_op(extent_op
, leaf
, ei
);
3250 * In the case of inline back ref, reference count will
3251 * be updated by remove_extent_backref
3254 BUG_ON(!found_extent
);
3256 btrfs_set_extent_refs(leaf
, ei
, refs
);
3257 btrfs_mark_buffer_dirty(leaf
);
3260 ret
= remove_extent_backref(trans
, extent_root
, path
,
3267 struct extent_buffer
*must_clean
= NULL
;
3270 BUG_ON(is_data
&& refs_to_drop
!=
3271 extent_data_ref_count(root
, path
, iref
));
3273 BUG_ON(path
->slots
[0] != extent_slot
);
3275 BUG_ON(path
->slots
[0] != extent_slot
+ 1);
3276 path
->slots
[0] = extent_slot
;
3281 ret
= pin_down_bytes(trans
, root
, path
, bytenr
,
3282 num_bytes
, is_data
, &must_clean
);
3287 * it is going to be very rare for someone to be waiting
3288 * on the block we're freeing. del_items might need to
3289 * schedule, so rather than get fancy, just force it
3293 btrfs_set_lock_blocking(must_clean
);
3295 ret
= btrfs_del_items(trans
, extent_root
, path
, path
->slots
[0],
3298 btrfs_release_path(extent_root
, path
);
3301 clean_tree_block(NULL
, root
, must_clean
);
3302 btrfs_tree_unlock(must_clean
);
3303 free_extent_buffer(must_clean
);
3307 ret
= btrfs_del_csums(trans
, root
, bytenr
, num_bytes
);
3310 invalidate_mapping_pages(info
->btree_inode
->i_mapping
,
3311 bytenr
>> PAGE_CACHE_SHIFT
,
3312 (bytenr
+ num_bytes
- 1) >> PAGE_CACHE_SHIFT
);
3315 ret
= update_block_group(trans
, root
, bytenr
, num_bytes
, 0,
3319 btrfs_free_path(path
);
3324 * when we free an extent, it is possible (and likely) that we free the last
3325 * delayed ref for that extent as well. This searches the delayed ref tree for
3326 * a given extent, and if there are no other delayed refs to be processed, it
3327 * removes it from the tree.
3329 static noinline
int check_ref_cleanup(struct btrfs_trans_handle
*trans
,
3330 struct btrfs_root
*root
, u64 bytenr
)
3332 struct btrfs_delayed_ref_head
*head
;
3333 struct btrfs_delayed_ref_root
*delayed_refs
;
3334 struct btrfs_delayed_ref_node
*ref
;
3335 struct rb_node
*node
;
3338 delayed_refs
= &trans
->transaction
->delayed_refs
;
3339 spin_lock(&delayed_refs
->lock
);
3340 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
3344 node
= rb_prev(&head
->node
.rb_node
);
3348 ref
= rb_entry(node
, struct btrfs_delayed_ref_node
, rb_node
);
3350 /* there are still entries for this ref, we can't drop it */
3351 if (ref
->bytenr
== bytenr
)
3354 if (head
->extent_op
) {
3355 if (!head
->must_insert_reserved
)
3357 kfree(head
->extent_op
);
3358 head
->extent_op
= NULL
;
3362 * waiting for the lock here would deadlock. If someone else has it
3363 * locked they are already in the process of dropping it anyway
3365 if (!mutex_trylock(&head
->mutex
))
3369 * at this point we have a head with no other entries. Go
3370 * ahead and process it.
3372 head
->node
.in_tree
= 0;
3373 rb_erase(&head
->node
.rb_node
, &delayed_refs
->root
);
3375 delayed_refs
->num_entries
--;
3378 * we don't take a ref on the node because we're removing it from the
3379 * tree, so we just steal the ref the tree was holding.
3381 delayed_refs
->num_heads
--;
3382 if (list_empty(&head
->cluster
))
3383 delayed_refs
->num_heads_ready
--;
3385 list_del_init(&head
->cluster
);
3386 spin_unlock(&delayed_refs
->lock
);
3388 ret
= run_one_delayed_ref(trans
, root
->fs_info
->tree_root
,
3389 &head
->node
, head
->extent_op
,
3390 head
->must_insert_reserved
);
3392 btrfs_put_delayed_ref(&head
->node
);
3395 spin_unlock(&delayed_refs
->lock
);
3399 int btrfs_free_extent(struct btrfs_trans_handle
*trans
,
3400 struct btrfs_root
*root
,
3401 u64 bytenr
, u64 num_bytes
, u64 parent
,
3402 u64 root_objectid
, u64 owner
, u64 offset
)
3407 * tree log blocks never actually go into the extent allocation
3408 * tree, just update pinning info and exit early.
3410 if (root_objectid
== BTRFS_TREE_LOG_OBJECTID
) {
3411 WARN_ON(owner
>= BTRFS_FIRST_FREE_OBJECTID
);
3412 /* unlocks the pinned mutex */
3413 btrfs_update_pinned_extents(root
, bytenr
, num_bytes
, 1);
3414 update_reserved_extents(root
, bytenr
, num_bytes
, 0);
3416 } else if (owner
< BTRFS_FIRST_FREE_OBJECTID
) {
3417 ret
= btrfs_add_delayed_tree_ref(trans
, bytenr
, num_bytes
,
3418 parent
, root_objectid
, (int)owner
,
3419 BTRFS_DROP_DELAYED_REF
, NULL
);
3421 ret
= check_ref_cleanup(trans
, root
, bytenr
);
3424 ret
= btrfs_add_delayed_data_ref(trans
, bytenr
, num_bytes
,
3425 parent
, root_objectid
, owner
,
3426 offset
, BTRFS_DROP_DELAYED_REF
, NULL
);
3432 static u64
stripe_align(struct btrfs_root
*root
, u64 val
)
3434 u64 mask
= ((u64
)root
->stripesize
- 1);
3435 u64 ret
= (val
+ mask
) & ~mask
;
3440 * walks the btree of allocated extents and find a hole of a given size.
3441 * The key ins is changed to record the hole:
3442 * ins->objectid == block start
3443 * ins->flags = BTRFS_EXTENT_ITEM_KEY
3444 * ins->offset == number of blocks
3445 * Any available blocks before search_start are skipped.
3447 static noinline
int find_free_extent(struct btrfs_trans_handle
*trans
,
3448 struct btrfs_root
*orig_root
,
3449 u64 num_bytes
, u64 empty_size
,
3450 u64 search_start
, u64 search_end
,
3451 u64 hint_byte
, struct btrfs_key
*ins
,
3452 u64 exclude_start
, u64 exclude_nr
,
3456 struct btrfs_root
*root
= orig_root
->fs_info
->extent_root
;
3457 struct btrfs_free_cluster
*last_ptr
= NULL
;
3458 struct btrfs_block_group_cache
*block_group
= NULL
;
3459 int empty_cluster
= 2 * 1024 * 1024;
3460 int allowed_chunk_alloc
= 0;
3461 struct btrfs_space_info
*space_info
;
3462 int last_ptr_loop
= 0;
3465 WARN_ON(num_bytes
< root
->sectorsize
);
3466 btrfs_set_key_type(ins
, BTRFS_EXTENT_ITEM_KEY
);
3470 space_info
= __find_space_info(root
->fs_info
, data
);
3472 if (orig_root
->ref_cows
|| empty_size
)
3473 allowed_chunk_alloc
= 1;
3475 if (data
& BTRFS_BLOCK_GROUP_METADATA
) {
3476 last_ptr
= &root
->fs_info
->meta_alloc_cluster
;
3477 if (!btrfs_test_opt(root
, SSD
))
3478 empty_cluster
= 64 * 1024;
3481 if ((data
& BTRFS_BLOCK_GROUP_DATA
) && btrfs_test_opt(root
, SSD
)) {
3482 last_ptr
= &root
->fs_info
->data_alloc_cluster
;
3486 spin_lock(&last_ptr
->lock
);
3487 if (last_ptr
->block_group
)
3488 hint_byte
= last_ptr
->window_start
;
3489 spin_unlock(&last_ptr
->lock
);
3492 search_start
= max(search_start
, first_logical_byte(root
, 0));
3493 search_start
= max(search_start
, hint_byte
);
3500 if (search_start
== hint_byte
) {
3501 block_group
= btrfs_lookup_block_group(root
->fs_info
,
3503 if (block_group
&& block_group_bits(block_group
, data
)) {
3504 down_read(&space_info
->groups_sem
);
3505 if (list_empty(&block_group
->list
) ||
3508 * someone is removing this block group,
3509 * we can't jump into the have_block_group
3510 * target because our list pointers are not
3513 btrfs_put_block_group(block_group
);
3514 up_read(&space_info
->groups_sem
);
3516 goto have_block_group
;
3517 } else if (block_group
) {
3518 btrfs_put_block_group(block_group
);
3523 down_read(&space_info
->groups_sem
);
3524 list_for_each_entry(block_group
, &space_info
->block_groups
, list
) {
3527 atomic_inc(&block_group
->count
);
3528 search_start
= block_group
->key
.objectid
;
3531 if (unlikely(!block_group
->cached
)) {
3532 mutex_lock(&block_group
->cache_mutex
);
3533 ret
= cache_block_group(root
, block_group
);
3534 mutex_unlock(&block_group
->cache_mutex
);
3536 btrfs_put_block_group(block_group
);
3541 if (unlikely(block_group
->ro
))
3546 * the refill lock keeps out other
3547 * people trying to start a new cluster
3549 spin_lock(&last_ptr
->refill_lock
);
3550 if (last_ptr
->block_group
&&
3551 (last_ptr
->block_group
->ro
||
3552 !block_group_bits(last_ptr
->block_group
, data
))) {
3554 goto refill_cluster
;
3557 offset
= btrfs_alloc_from_cluster(block_group
, last_ptr
,
3558 num_bytes
, search_start
);
3560 /* we have a block, we're done */
3561 spin_unlock(&last_ptr
->refill_lock
);
3565 spin_lock(&last_ptr
->lock
);
3567 * whoops, this cluster doesn't actually point to
3568 * this block group. Get a ref on the block
3569 * group is does point to and try again
3571 if (!last_ptr_loop
&& last_ptr
->block_group
&&
3572 last_ptr
->block_group
!= block_group
) {
3574 btrfs_put_block_group(block_group
);
3575 block_group
= last_ptr
->block_group
;
3576 atomic_inc(&block_group
->count
);
3577 spin_unlock(&last_ptr
->lock
);
3578 spin_unlock(&last_ptr
->refill_lock
);
3581 search_start
= block_group
->key
.objectid
;
3583 * we know this block group is properly
3584 * in the list because
3585 * btrfs_remove_block_group, drops the
3586 * cluster before it removes the block
3587 * group from the list
3589 goto have_block_group
;
3591 spin_unlock(&last_ptr
->lock
);
3594 * this cluster didn't work out, free it and
3597 btrfs_return_cluster_to_free_space(NULL
, last_ptr
);
3601 /* allocate a cluster in this block group */
3602 ret
= btrfs_find_space_cluster(trans
, root
,
3603 block_group
, last_ptr
,
3605 empty_cluster
+ empty_size
);
3608 * now pull our allocation out of this
3611 offset
= btrfs_alloc_from_cluster(block_group
,
3612 last_ptr
, num_bytes
,
3615 /* we found one, proceed */
3616 spin_unlock(&last_ptr
->refill_lock
);
3621 * at this point we either didn't find a cluster
3622 * or we weren't able to allocate a block from our
3623 * cluster. Free the cluster we've been trying
3624 * to use, and go to the next block group
3627 btrfs_return_cluster_to_free_space(NULL
,
3629 spin_unlock(&last_ptr
->refill_lock
);
3632 spin_unlock(&last_ptr
->refill_lock
);
3635 offset
= btrfs_find_space_for_alloc(block_group
, search_start
,
3636 num_bytes
, empty_size
);
3640 search_start
= stripe_align(root
, offset
);
3642 /* move on to the next group */
3643 if (search_start
+ num_bytes
>= search_end
) {
3644 btrfs_add_free_space(block_group
, offset
, num_bytes
);
3648 /* move on to the next group */
3649 if (search_start
+ num_bytes
>
3650 block_group
->key
.objectid
+ block_group
->key
.offset
) {
3651 btrfs_add_free_space(block_group
, offset
, num_bytes
);
3655 if (exclude_nr
> 0 &&
3656 (search_start
+ num_bytes
> exclude_start
&&
3657 search_start
< exclude_start
+ exclude_nr
)) {
3658 search_start
= exclude_start
+ exclude_nr
;
3660 btrfs_add_free_space(block_group
, offset
, num_bytes
);
3662 * if search_start is still in this block group
3663 * then we just re-search this block group
3665 if (search_start
>= block_group
->key
.objectid
&&
3666 search_start
< (block_group
->key
.objectid
+
3667 block_group
->key
.offset
))
3668 goto have_block_group
;
3672 ins
->objectid
= search_start
;
3673 ins
->offset
= num_bytes
;
3675 if (offset
< search_start
)
3676 btrfs_add_free_space(block_group
, offset
,
3677 search_start
- offset
);
3678 BUG_ON(offset
> search_start
);
3680 /* we are all good, lets return */
3683 btrfs_put_block_group(block_group
);
3685 up_read(&space_info
->groups_sem
);
3687 /* loop == 0, try to find a clustered alloc in every block group
3688 * loop == 1, try again after forcing a chunk allocation
3689 * loop == 2, set empty_size and empty_cluster to 0 and try again
3691 if (!ins
->objectid
&& loop
< 3 &&
3692 (empty_size
|| empty_cluster
|| allowed_chunk_alloc
)) {
3698 if (allowed_chunk_alloc
) {
3699 ret
= do_chunk_alloc(trans
, root
, num_bytes
+
3700 2 * 1024 * 1024, data
, 1);
3701 allowed_chunk_alloc
= 0;
3703 space_info
->force_alloc
= 1;
3711 } else if (!ins
->objectid
) {
3715 /* we found what we needed */
3716 if (ins
->objectid
) {
3717 if (!(data
& BTRFS_BLOCK_GROUP_DATA
))
3718 trans
->block_group
= block_group
->key
.objectid
;
3720 btrfs_put_block_group(block_group
);
3727 static void dump_space_info(struct btrfs_space_info
*info
, u64 bytes
)
3729 struct btrfs_block_group_cache
*cache
;
3731 printk(KERN_INFO
"space_info has %llu free, is %sfull\n",
3732 (unsigned long long)(info
->total_bytes
- info
->bytes_used
-
3733 info
->bytes_pinned
- info
->bytes_reserved
),
3734 (info
->full
) ? "" : "not ");
3735 printk(KERN_INFO
"space_info total=%llu, pinned=%llu, delalloc=%llu,"
3736 " may_use=%llu, used=%llu\n",
3737 (unsigned long long)info
->total_bytes
,
3738 (unsigned long long)info
->bytes_pinned
,
3739 (unsigned long long)info
->bytes_delalloc
,
3740 (unsigned long long)info
->bytes_may_use
,
3741 (unsigned long long)info
->bytes_used
);
3743 down_read(&info
->groups_sem
);
3744 list_for_each_entry(cache
, &info
->block_groups
, list
) {
3745 spin_lock(&cache
->lock
);
3746 printk(KERN_INFO
"block group %llu has %llu bytes, %llu used "
3747 "%llu pinned %llu reserved\n",
3748 (unsigned long long)cache
->key
.objectid
,
3749 (unsigned long long)cache
->key
.offset
,
3750 (unsigned long long)btrfs_block_group_used(&cache
->item
),
3751 (unsigned long long)cache
->pinned
,
3752 (unsigned long long)cache
->reserved
);
3753 btrfs_dump_free_space(cache
, bytes
);
3754 spin_unlock(&cache
->lock
);
3756 up_read(&info
->groups_sem
);
3759 static int __btrfs_reserve_extent(struct btrfs_trans_handle
*trans
,
3760 struct btrfs_root
*root
,
3761 u64 num_bytes
, u64 min_alloc_size
,
3762 u64 empty_size
, u64 hint_byte
,
3763 u64 search_end
, struct btrfs_key
*ins
,
3767 u64 search_start
= 0;
3768 struct btrfs_fs_info
*info
= root
->fs_info
;
3770 data
= btrfs_get_alloc_profile(root
, data
);
3773 * the only place that sets empty_size is btrfs_realloc_node, which
3774 * is not called recursively on allocations
3776 if (empty_size
|| root
->ref_cows
) {
3777 if (!(data
& BTRFS_BLOCK_GROUP_METADATA
)) {
3778 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
3780 BTRFS_BLOCK_GROUP_METADATA
|
3781 (info
->metadata_alloc_profile
&
3782 info
->avail_metadata_alloc_bits
), 0);
3784 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
3785 num_bytes
+ 2 * 1024 * 1024, data
, 0);
3788 WARN_ON(num_bytes
< root
->sectorsize
);
3789 ret
= find_free_extent(trans
, root
, num_bytes
, empty_size
,
3790 search_start
, search_end
, hint_byte
, ins
,
3791 trans
->alloc_exclude_start
,
3792 trans
->alloc_exclude_nr
, data
);
3794 if (ret
== -ENOSPC
&& num_bytes
> min_alloc_size
) {
3795 num_bytes
= num_bytes
>> 1;
3796 num_bytes
= num_bytes
& ~(root
->sectorsize
- 1);
3797 num_bytes
= max(num_bytes
, min_alloc_size
);
3798 do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
3799 num_bytes
, data
, 1);
3803 struct btrfs_space_info
*sinfo
;
3805 sinfo
= __find_space_info(root
->fs_info
, data
);
3806 printk(KERN_ERR
"btrfs allocation failed flags %llu, "
3807 "wanted %llu\n", (unsigned long long)data
,
3808 (unsigned long long)num_bytes
);
3809 dump_space_info(sinfo
, num_bytes
);
3816 int btrfs_free_reserved_extent(struct btrfs_root
*root
, u64 start
, u64 len
)
3818 struct btrfs_block_group_cache
*cache
;
3821 cache
= btrfs_lookup_block_group(root
->fs_info
, start
);
3823 printk(KERN_ERR
"Unable to find block group for %llu\n",
3824 (unsigned long long)start
);
3828 ret
= btrfs_discard_extent(root
, start
, len
);
3830 btrfs_add_free_space(cache
, start
, len
);
3831 btrfs_put_block_group(cache
);
3832 update_reserved_extents(root
, start
, len
, 0);
3837 int btrfs_reserve_extent(struct btrfs_trans_handle
*trans
,
3838 struct btrfs_root
*root
,
3839 u64 num_bytes
, u64 min_alloc_size
,
3840 u64 empty_size
, u64 hint_byte
,
3841 u64 search_end
, struct btrfs_key
*ins
,
3845 ret
= __btrfs_reserve_extent(trans
, root
, num_bytes
, min_alloc_size
,
3846 empty_size
, hint_byte
, search_end
, ins
,
3848 update_reserved_extents(root
, ins
->objectid
, ins
->offset
, 1);
3852 static int alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
3853 struct btrfs_root
*root
,
3854 u64 parent
, u64 root_objectid
,
3855 u64 flags
, u64 owner
, u64 offset
,
3856 struct btrfs_key
*ins
, int ref_mod
)
3859 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3860 struct btrfs_extent_item
*extent_item
;
3861 struct btrfs_extent_inline_ref
*iref
;
3862 struct btrfs_path
*path
;
3863 struct extent_buffer
*leaf
;
3868 type
= BTRFS_SHARED_DATA_REF_KEY
;
3870 type
= BTRFS_EXTENT_DATA_REF_KEY
;
3872 size
= sizeof(*extent_item
) + btrfs_extent_inline_ref_size(type
);
3874 path
= btrfs_alloc_path();
3877 path
->leave_spinning
= 1;
3878 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
3882 leaf
= path
->nodes
[0];
3883 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3884 struct btrfs_extent_item
);
3885 btrfs_set_extent_refs(leaf
, extent_item
, ref_mod
);
3886 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
3887 btrfs_set_extent_flags(leaf
, extent_item
,
3888 flags
| BTRFS_EXTENT_FLAG_DATA
);
3890 iref
= (struct btrfs_extent_inline_ref
*)(extent_item
+ 1);
3891 btrfs_set_extent_inline_ref_type(leaf
, iref
, type
);
3893 struct btrfs_shared_data_ref
*ref
;
3894 ref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
3895 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
3896 btrfs_set_shared_data_ref_count(leaf
, ref
, ref_mod
);
3898 struct btrfs_extent_data_ref
*ref
;
3899 ref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
3900 btrfs_set_extent_data_ref_root(leaf
, ref
, root_objectid
);
3901 btrfs_set_extent_data_ref_objectid(leaf
, ref
, owner
);
3902 btrfs_set_extent_data_ref_offset(leaf
, ref
, offset
);
3903 btrfs_set_extent_data_ref_count(leaf
, ref
, ref_mod
);
3906 btrfs_mark_buffer_dirty(path
->nodes
[0]);
3907 btrfs_free_path(path
);
3909 ret
= update_block_group(trans
, root
, ins
->objectid
, ins
->offset
,
3912 printk(KERN_ERR
"btrfs update block group failed for %llu "
3913 "%llu\n", (unsigned long long)ins
->objectid
,
3914 (unsigned long long)ins
->offset
);
3920 static int alloc_reserved_tree_block(struct btrfs_trans_handle
*trans
,
3921 struct btrfs_root
*root
,
3922 u64 parent
, u64 root_objectid
,
3923 u64 flags
, struct btrfs_disk_key
*key
,
3924 int level
, struct btrfs_key
*ins
)
3927 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3928 struct btrfs_extent_item
*extent_item
;
3929 struct btrfs_tree_block_info
*block_info
;
3930 struct btrfs_extent_inline_ref
*iref
;
3931 struct btrfs_path
*path
;
3932 struct extent_buffer
*leaf
;
3933 u32 size
= sizeof(*extent_item
) + sizeof(*block_info
) + sizeof(*iref
);
3935 path
= btrfs_alloc_path();
3938 path
->leave_spinning
= 1;
3939 ret
= btrfs_insert_empty_item(trans
, fs_info
->extent_root
, path
,
3943 leaf
= path
->nodes
[0];
3944 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3945 struct btrfs_extent_item
);
3946 btrfs_set_extent_refs(leaf
, extent_item
, 1);
3947 btrfs_set_extent_generation(leaf
, extent_item
, trans
->transid
);
3948 btrfs_set_extent_flags(leaf
, extent_item
,
3949 flags
| BTRFS_EXTENT_FLAG_TREE_BLOCK
);
3950 block_info
= (struct btrfs_tree_block_info
*)(extent_item
+ 1);
3952 btrfs_set_tree_block_key(leaf
, block_info
, key
);
3953 btrfs_set_tree_block_level(leaf
, block_info
, level
);
3955 iref
= (struct btrfs_extent_inline_ref
*)(block_info
+ 1);
3957 BUG_ON(!(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
));
3958 btrfs_set_extent_inline_ref_type(leaf
, iref
,
3959 BTRFS_SHARED_BLOCK_REF_KEY
);
3960 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
3962 btrfs_set_extent_inline_ref_type(leaf
, iref
,
3963 BTRFS_TREE_BLOCK_REF_KEY
);
3964 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
3967 btrfs_mark_buffer_dirty(leaf
);
3968 btrfs_free_path(path
);
3970 ret
= update_block_group(trans
, root
, ins
->objectid
, ins
->offset
,
3973 printk(KERN_ERR
"btrfs update block group failed for %llu "
3974 "%llu\n", (unsigned long long)ins
->objectid
,
3975 (unsigned long long)ins
->offset
);
3981 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle
*trans
,
3982 struct btrfs_root
*root
,
3983 u64 root_objectid
, u64 owner
,
3984 u64 offset
, struct btrfs_key
*ins
)
3988 BUG_ON(root_objectid
== BTRFS_TREE_LOG_OBJECTID
);
3990 ret
= btrfs_add_delayed_data_ref(trans
, ins
->objectid
, ins
->offset
,
3991 0, root_objectid
, owner
, offset
,
3992 BTRFS_ADD_DELAYED_EXTENT
, NULL
);
3997 * this is used by the tree logging recovery code. It records that
3998 * an extent has been allocated and makes sure to clear the free
3999 * space cache bits as well
4001 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle
*trans
,
4002 struct btrfs_root
*root
,
4003 u64 root_objectid
, u64 owner
, u64 offset
,
4004 struct btrfs_key
*ins
)
4007 struct btrfs_block_group_cache
*block_group
;
4009 block_group
= btrfs_lookup_block_group(root
->fs_info
, ins
->objectid
);
4010 mutex_lock(&block_group
->cache_mutex
);
4011 cache_block_group(root
, block_group
);
4012 mutex_unlock(&block_group
->cache_mutex
);
4014 ret
= btrfs_remove_free_space(block_group
, ins
->objectid
,
4017 btrfs_put_block_group(block_group
);
4018 ret
= alloc_reserved_file_extent(trans
, root
, 0, root_objectid
,
4019 0, owner
, offset
, ins
, 1);
4024 * finds a free extent and does all the dirty work required for allocation
4025 * returns the key for the extent through ins, and a tree buffer for
4026 * the first block of the extent through buf.
4028 * returns 0 if everything worked, non-zero otherwise.
4030 static int alloc_tree_block(struct btrfs_trans_handle
*trans
,
4031 struct btrfs_root
*root
,
4032 u64 num_bytes
, u64 parent
, u64 root_objectid
,
4033 struct btrfs_disk_key
*key
, int level
,
4034 u64 empty_size
, u64 hint_byte
, u64 search_end
,
4035 struct btrfs_key
*ins
)
4040 ret
= __btrfs_reserve_extent(trans
, root
, num_bytes
, num_bytes
,
4041 empty_size
, hint_byte
, search_end
,
4045 if (root_objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
4047 parent
= ins
->objectid
;
4048 flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
4052 update_reserved_extents(root
, ins
->objectid
, ins
->offset
, 1);
4053 if (root_objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
4054 struct btrfs_delayed_extent_op
*extent_op
;
4055 extent_op
= kmalloc(sizeof(*extent_op
), GFP_NOFS
);
4058 memcpy(&extent_op
->key
, key
, sizeof(extent_op
->key
));
4060 memset(&extent_op
->key
, 0, sizeof(extent_op
->key
));
4061 extent_op
->flags_to_set
= flags
;
4062 extent_op
->update_key
= 1;
4063 extent_op
->update_flags
= 1;
4064 extent_op
->is_data
= 0;
4066 ret
= btrfs_add_delayed_tree_ref(trans
, ins
->objectid
,
4067 ins
->offset
, parent
, root_objectid
,
4068 level
, BTRFS_ADD_DELAYED_EXTENT
,
4075 struct extent_buffer
*btrfs_init_new_buffer(struct btrfs_trans_handle
*trans
,
4076 struct btrfs_root
*root
,
4077 u64 bytenr
, u32 blocksize
,
4080 struct extent_buffer
*buf
;
4082 buf
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
4084 return ERR_PTR(-ENOMEM
);
4085 btrfs_set_header_generation(buf
, trans
->transid
);
4086 btrfs_set_buffer_lockdep_class(buf
, level
);
4087 btrfs_tree_lock(buf
);
4088 clean_tree_block(trans
, root
, buf
);
4090 btrfs_set_lock_blocking(buf
);
4091 btrfs_set_buffer_uptodate(buf
);
4093 if (root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
) {
4094 set_extent_dirty(&root
->dirty_log_pages
, buf
->start
,
4095 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
4097 set_extent_dirty(&trans
->transaction
->dirty_pages
, buf
->start
,
4098 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
4100 trans
->blocks_used
++;
4101 /* this returns a buffer locked for blocking */
4106 * helper function to allocate a block for a given tree
4107 * returns the tree buffer or NULL.
4109 struct extent_buffer
*btrfs_alloc_free_block(struct btrfs_trans_handle
*trans
,
4110 struct btrfs_root
*root
, u32 blocksize
,
4111 u64 parent
, u64 root_objectid
,
4112 struct btrfs_disk_key
*key
, int level
,
4113 u64 hint
, u64 empty_size
)
4115 struct btrfs_key ins
;
4117 struct extent_buffer
*buf
;
4119 ret
= alloc_tree_block(trans
, root
, blocksize
, parent
, root_objectid
,
4120 key
, level
, empty_size
, hint
, (u64
)-1, &ins
);
4123 return ERR_PTR(ret
);
4126 buf
= btrfs_init_new_buffer(trans
, root
, ins
.objectid
,
4131 int btrfs_drop_leaf_ref(struct btrfs_trans_handle
*trans
,
4132 struct btrfs_root
*root
, struct extent_buffer
*leaf
)
4136 struct btrfs_key key
;
4137 struct btrfs_file_extent_item
*fi
;
4142 BUG_ON(!btrfs_is_leaf(leaf
));
4143 nritems
= btrfs_header_nritems(leaf
);
4145 for (i
= 0; i
< nritems
; i
++) {
4147 btrfs_item_key_to_cpu(leaf
, &key
, i
);
4149 /* only extents have references, skip everything else */
4150 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
4153 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
4155 /* inline extents live in the btree, they don't have refs */
4156 if (btrfs_file_extent_type(leaf
, fi
) ==
4157 BTRFS_FILE_EXTENT_INLINE
)
4160 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
4162 /* holes don't have refs */
4163 if (disk_bytenr
== 0)
4166 num_bytes
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
4167 ret
= btrfs_free_extent(trans
, root
, disk_bytenr
, num_bytes
,
4168 leaf
->start
, 0, key
.objectid
, 0);
4176 static noinline
int cache_drop_leaf_ref(struct btrfs_trans_handle
*trans
,
4177 struct btrfs_root
*root
,
4178 struct btrfs_leaf_ref
*ref
)
4182 struct btrfs_extent_info
*info
;
4183 struct refsort
*sorted
;
4185 if (ref
->nritems
== 0)
4188 sorted
= kmalloc(sizeof(*sorted
) * ref
->nritems
, GFP_NOFS
);
4189 for (i
= 0; i
< ref
->nritems
; i
++) {
4190 sorted
[i
].bytenr
= ref
->extents
[i
].bytenr
;
4193 sort(sorted
, ref
->nritems
, sizeof(struct refsort
), refsort_cmp
, NULL
);
4196 * the items in the ref were sorted when the ref was inserted
4197 * into the ref cache, so this is already in order
4199 for (i
= 0; i
< ref
->nritems
; i
++) {
4200 info
= ref
->extents
+ sorted
[i
].slot
;
4201 ret
= btrfs_free_extent(trans
, root
, info
->bytenr
,
4202 info
->num_bytes
, ref
->bytenr
,
4203 ref
->owner
, ref
->generation
,
4206 atomic_inc(&root
->fs_info
->throttle_gen
);
4207 wake_up(&root
->fs_info
->transaction_throttle
);
4219 static int drop_snap_lookup_refcount(struct btrfs_trans_handle
*trans
,
4220 struct btrfs_root
*root
, u64 start
,
4225 ret
= btrfs_lookup_extent_refs(trans
, root
, start
, len
, refs
);
4228 #if 0 /* some debugging code in case we see problems here */
4229 /* if the refs count is one, it won't get increased again. But
4230 * if the ref count is > 1, someone may be decreasing it at
4231 * the same time we are.
4234 struct extent_buffer
*eb
= NULL
;
4235 eb
= btrfs_find_create_tree_block(root
, start
, len
);
4237 btrfs_tree_lock(eb
);
4239 mutex_lock(&root
->fs_info
->alloc_mutex
);
4240 ret
= lookup_extent_ref(NULL
, root
, start
, len
, refs
);
4242 mutex_unlock(&root
->fs_info
->alloc_mutex
);
4245 btrfs_tree_unlock(eb
);
4246 free_extent_buffer(eb
);
4249 printk(KERN_ERR
"btrfs block %llu went down to one "
4250 "during drop_snap\n", (unsigned long long)start
);
4262 * this is used while deleting old snapshots, and it drops the refs
4263 * on a whole subtree starting from a level 1 node.
4265 * The idea is to sort all the leaf pointers, and then drop the
4266 * ref on all the leaves in order. Most of the time the leaves
4267 * will have ref cache entries, so no leaf IOs will be required to
4268 * find the extents they have references on.
4270 * For each leaf, any references it has are also dropped in order
4272 * This ends up dropping the references in something close to optimal
4273 * order for reading and modifying the extent allocation tree.
4275 static noinline
int drop_level_one_refs(struct btrfs_trans_handle
*trans
,
4276 struct btrfs_root
*root
,
4277 struct btrfs_path
*path
)
4282 struct extent_buffer
*eb
= path
->nodes
[1];
4283 struct extent_buffer
*leaf
;
4284 struct btrfs_leaf_ref
*ref
;
4285 struct refsort
*sorted
= NULL
;
4286 int nritems
= btrfs_header_nritems(eb
);
4290 int slot
= path
->slots
[1];
4291 u32 blocksize
= btrfs_level_size(root
, 0);
4297 root_owner
= btrfs_header_owner(eb
);
4298 root_gen
= btrfs_header_generation(eb
);
4299 sorted
= kmalloc(sizeof(*sorted
) * nritems
, GFP_NOFS
);
4302 * step one, sort all the leaf pointers so we don't scribble
4303 * randomly into the extent allocation tree
4305 for (i
= slot
; i
< nritems
; i
++) {
4306 sorted
[refi
].bytenr
= btrfs_node_blockptr(eb
, i
);
4307 sorted
[refi
].slot
= i
;
4312 * nritems won't be zero, but if we're picking up drop_snapshot
4313 * after a crash, slot might be > 0, so double check things
4319 sort(sorted
, refi
, sizeof(struct refsort
), refsort_cmp
, NULL
);
4322 * the first loop frees everything the leaves point to
4324 for (i
= 0; i
< refi
; i
++) {
4327 bytenr
= sorted
[i
].bytenr
;
4330 * check the reference count on this leaf. If it is > 1
4331 * we just decrement it below and don't update any
4332 * of the refs the leaf points to.
4334 ret
= drop_snap_lookup_refcount(trans
, root
, bytenr
,
4340 ptr_gen
= btrfs_node_ptr_generation(eb
, sorted
[i
].slot
);
4343 * the leaf only had one reference, which means the
4344 * only thing pointing to this leaf is the snapshot
4345 * we're deleting. It isn't possible for the reference
4346 * count to increase again later
4348 * The reference cache is checked for the leaf,
4349 * and if found we'll be able to drop any refs held by
4350 * the leaf without needing to read it in.
4352 ref
= btrfs_lookup_leaf_ref(root
, bytenr
);
4353 if (ref
&& ref
->generation
!= ptr_gen
) {
4354 btrfs_free_leaf_ref(root
, ref
);
4358 ret
= cache_drop_leaf_ref(trans
, root
, ref
);
4360 btrfs_remove_leaf_ref(root
, ref
);
4361 btrfs_free_leaf_ref(root
, ref
);
4364 * the leaf wasn't in the reference cache, so
4365 * we have to read it.
4367 leaf
= read_tree_block(root
, bytenr
, blocksize
,
4369 ret
= btrfs_drop_leaf_ref(trans
, root
, leaf
);
4371 free_extent_buffer(leaf
);
4373 atomic_inc(&root
->fs_info
->throttle_gen
);
4374 wake_up(&root
->fs_info
->transaction_throttle
);
4379 * run through the loop again to free the refs on the leaves.
4380 * This is faster than doing it in the loop above because
4381 * the leaves are likely to be clustered together. We end up
4382 * working in nice chunks on the extent allocation tree.
4384 for (i
= 0; i
< refi
; i
++) {
4385 bytenr
= sorted
[i
].bytenr
;
4386 ret
= btrfs_free_extent(trans
, root
, bytenr
,
4387 blocksize
, eb
->start
,
4388 root_owner
, root_gen
, 0, 1);
4391 atomic_inc(&root
->fs_info
->throttle_gen
);
4392 wake_up(&root
->fs_info
->transaction_throttle
);
4399 * update the path to show we've processed the entire level 1
4400 * node. This will get saved into the root's drop_snapshot_progress
4401 * field so these drops are not repeated again if this transaction
4404 path
->slots
[1] = nritems
;
4409 * helper function for drop_snapshot, this walks down the tree dropping ref
4410 * counts as it goes.
4412 static noinline
int walk_down_tree(struct btrfs_trans_handle
*trans
,
4413 struct btrfs_root
*root
,
4414 struct btrfs_path
*path
, int *level
)
4420 struct extent_buffer
*next
;
4421 struct extent_buffer
*cur
;
4422 struct extent_buffer
*parent
;
4427 WARN_ON(*level
< 0);
4428 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
4429 ret
= drop_snap_lookup_refcount(trans
, root
, path
->nodes
[*level
]->start
,
4430 path
->nodes
[*level
]->len
, &refs
);
4436 * walk down to the last node level and free all the leaves
4438 while (*level
>= 0) {
4439 WARN_ON(*level
< 0);
4440 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
4441 cur
= path
->nodes
[*level
];
4443 if (btrfs_header_level(cur
) != *level
)
4446 if (path
->slots
[*level
] >=
4447 btrfs_header_nritems(cur
))
4450 /* the new code goes down to level 1 and does all the
4451 * leaves pointed to that node in bulk. So, this check
4452 * for level 0 will always be false.
4454 * But, the disk format allows the drop_snapshot_progress
4455 * field in the root to leave things in a state where
4456 * a leaf will need cleaning up here. If someone crashes
4457 * with the old code and then boots with the new code,
4458 * we might find a leaf here.
4461 ret
= btrfs_drop_leaf_ref(trans
, root
, cur
);
4467 * once we get to level one, process the whole node
4468 * at once, including everything below it.
4471 ret
= drop_level_one_refs(trans
, root
, path
);
4476 bytenr
= btrfs_node_blockptr(cur
, path
->slots
[*level
]);
4477 ptr_gen
= btrfs_node_ptr_generation(cur
, path
->slots
[*level
]);
4478 blocksize
= btrfs_level_size(root
, *level
- 1);
4480 ret
= drop_snap_lookup_refcount(trans
, root
, bytenr
,
4485 * if there is more than one reference, we don't need
4486 * to read that node to drop any references it has. We
4487 * just drop the ref we hold on that node and move on to the
4488 * next slot in this level.
4491 parent
= path
->nodes
[*level
];
4492 root_owner
= btrfs_header_owner(parent
);
4493 root_gen
= btrfs_header_generation(parent
);
4494 path
->slots
[*level
]++;
4496 ret
= btrfs_free_extent(trans
, root
, bytenr
,
4497 blocksize
, parent
->start
,
4498 root_owner
, root_gen
,
4502 atomic_inc(&root
->fs_info
->throttle_gen
);
4503 wake_up(&root
->fs_info
->transaction_throttle
);
4510 * we need to keep freeing things in the next level down.
4511 * read the block and loop around to process it
4513 next
= read_tree_block(root
, bytenr
, blocksize
, ptr_gen
);
4514 WARN_ON(*level
<= 0);
4515 if (path
->nodes
[*level
-1])
4516 free_extent_buffer(path
->nodes
[*level
-1]);
4517 path
->nodes
[*level
-1] = next
;
4518 *level
= btrfs_header_level(next
);
4519 path
->slots
[*level
] = 0;
4523 WARN_ON(*level
< 0);
4524 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
4526 if (path
->nodes
[*level
] == root
->node
) {
4527 parent
= path
->nodes
[*level
];
4528 bytenr
= path
->nodes
[*level
]->start
;
4530 parent
= path
->nodes
[*level
+ 1];
4531 bytenr
= btrfs_node_blockptr(parent
, path
->slots
[*level
+ 1]);
4534 blocksize
= btrfs_level_size(root
, *level
);
4535 root_owner
= btrfs_header_owner(parent
);
4536 root_gen
= btrfs_header_generation(parent
);
4539 * cleanup and free the reference on the last node
4542 ret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
,
4543 parent
->start
, root_owner
, root_gen
,
4545 free_extent_buffer(path
->nodes
[*level
]);
4546 path
->nodes
[*level
] = NULL
;
4557 * helper function for drop_subtree, this function is similar to
4558 * walk_down_tree. The main difference is that it checks reference
4559 * counts while tree blocks are locked.
4561 static noinline
int walk_down_tree(struct btrfs_trans_handle
*trans
,
4562 struct btrfs_root
*root
,
4563 struct btrfs_path
*path
, int *level
)
4565 struct extent_buffer
*next
;
4566 struct extent_buffer
*cur
;
4567 struct extent_buffer
*parent
;
4575 cur
= path
->nodes
[*level
];
4576 ret
= btrfs_lookup_extent_info(trans
, root
, cur
->start
, cur
->len
,
4582 BUG_ON(!(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
));
4584 while (*level
>= 0) {
4585 cur
= path
->nodes
[*level
];
4587 ret
= btrfs_drop_leaf_ref(trans
, root
, cur
);
4589 clean_tree_block(trans
, root
, cur
);
4592 if (path
->slots
[*level
] >= btrfs_header_nritems(cur
)) {
4593 clean_tree_block(trans
, root
, cur
);
4597 bytenr
= btrfs_node_blockptr(cur
, path
->slots
[*level
]);
4598 blocksize
= btrfs_level_size(root
, *level
- 1);
4599 ptr_gen
= btrfs_node_ptr_generation(cur
, path
->slots
[*level
]);
4601 next
= read_tree_block(root
, bytenr
, blocksize
, ptr_gen
);
4602 btrfs_tree_lock(next
);
4603 btrfs_set_lock_blocking(next
);
4605 ret
= btrfs_lookup_extent_info(trans
, root
, bytenr
, blocksize
,
4609 parent
= path
->nodes
[*level
];
4610 ret
= btrfs_free_extent(trans
, root
, bytenr
,
4611 blocksize
, parent
->start
,
4612 btrfs_header_owner(parent
),
4615 path
->slots
[*level
]++;
4616 btrfs_tree_unlock(next
);
4617 free_extent_buffer(next
);
4621 BUG_ON(!(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
));
4623 *level
= btrfs_header_level(next
);
4624 path
->nodes
[*level
] = next
;
4625 path
->slots
[*level
] = 0;
4626 path
->locks
[*level
] = 1;
4630 if (path
->nodes
[*level
] == root
->node
)
4631 parent
= path
->nodes
[*level
];
4633 parent
= path
->nodes
[*level
+ 1];
4634 bytenr
= path
->nodes
[*level
]->start
;
4635 blocksize
= path
->nodes
[*level
]->len
;
4637 ret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
, parent
->start
,
4638 btrfs_header_owner(parent
), *level
, 0);
4641 if (path
->locks
[*level
]) {
4642 btrfs_tree_unlock(path
->nodes
[*level
]);
4643 path
->locks
[*level
] = 0;
4645 free_extent_buffer(path
->nodes
[*level
]);
4646 path
->nodes
[*level
] = NULL
;
4653 * helper for dropping snapshots. This walks back up the tree in the path
4654 * to find the first node higher up where we haven't yet gone through
4657 static noinline
int walk_up_tree(struct btrfs_trans_handle
*trans
,
4658 struct btrfs_root
*root
,
4659 struct btrfs_path
*path
,
4660 int *level
, int max_level
)
4662 struct btrfs_root_item
*root_item
= &root
->root_item
;
4667 for (i
= *level
; i
< max_level
&& path
->nodes
[i
]; i
++) {
4668 slot
= path
->slots
[i
];
4669 if (slot
+ 1 < btrfs_header_nritems(path
->nodes
[i
])) {
4671 * there is more work to do in this level.
4672 * Update the drop_progress marker to reflect
4673 * the work we've done so far, and then bump
4677 WARN_ON(*level
== 0);
4678 if (max_level
== BTRFS_MAX_LEVEL
) {
4679 btrfs_node_key(path
->nodes
[i
],
4680 &root_item
->drop_progress
,
4682 root_item
->drop_level
= i
;
4687 struct extent_buffer
*parent
;
4690 * this whole node is done, free our reference
4691 * on it and go up one level
4693 if (path
->nodes
[*level
] == root
->node
)
4694 parent
= path
->nodes
[*level
];
4696 parent
= path
->nodes
[*level
+ 1];
4698 clean_tree_block(trans
, root
, path
->nodes
[i
]);
4699 ret
= btrfs_free_extent(trans
, root
,
4700 path
->nodes
[i
]->start
,
4701 path
->nodes
[i
]->len
,
4703 btrfs_header_owner(parent
),
4706 if (path
->locks
[*level
]) {
4707 btrfs_tree_unlock(path
->nodes
[i
]);
4710 free_extent_buffer(path
->nodes
[i
]);
4711 path
->nodes
[i
] = NULL
;
4719 * drop the reference count on the tree rooted at 'snap'. This traverses
4720 * the tree freeing any blocks that have a ref count of zero after being
4723 int btrfs_drop_snapshot(struct btrfs_trans_handle
*trans
, struct btrfs_root
4729 struct btrfs_path
*path
;
4731 struct btrfs_root_item
*root_item
= &root
->root_item
;
4733 path
= btrfs_alloc_path();
4736 level
= btrfs_header_level(root
->node
);
4737 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
4738 path
->nodes
[level
] = btrfs_lock_root_node(root
);
4739 btrfs_set_lock_blocking(path
->nodes
[level
]);
4740 path
->slots
[level
] = 0;
4741 path
->locks
[level
] = 1;
4743 struct btrfs_key key
;
4744 struct btrfs_disk_key found_key
;
4745 struct extent_buffer
*node
;
4747 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
4748 level
= root_item
->drop_level
;
4749 path
->lowest_level
= level
;
4750 wret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4755 node
= path
->nodes
[level
];
4756 btrfs_node_key(node
, &found_key
, path
->slots
[level
]);
4757 WARN_ON(memcmp(&found_key
, &root_item
->drop_progress
,
4758 sizeof(found_key
)));
4760 * unlock our path, this is safe because only this
4761 * function is allowed to delete this snapshot
4763 btrfs_unlock_up_safe(path
, 0);
4766 unsigned long update
;
4767 wret
= walk_down_tree(trans
, root
, path
, &level
);
4773 wret
= walk_up_tree(trans
, root
, path
, &level
,
4779 if (trans
->transaction
->in_commit
||
4780 trans
->transaction
->delayed_refs
.flushing
) {
4784 for (update_count
= 0; update_count
< 16; update_count
++) {
4785 update
= trans
->delayed_ref_updates
;
4786 trans
->delayed_ref_updates
= 0;
4788 btrfs_run_delayed_refs(trans
, root
, update
);
4794 btrfs_free_path(path
);
4798 int btrfs_drop_subtree(struct btrfs_trans_handle
*trans
,
4799 struct btrfs_root
*root
,
4800 struct extent_buffer
*node
,
4801 struct extent_buffer
*parent
)
4803 struct btrfs_path
*path
;
4809 path
= btrfs_alloc_path();
4812 btrfs_assert_tree_locked(parent
);
4813 parent_level
= btrfs_header_level(parent
);
4814 extent_buffer_get(parent
);
4815 path
->nodes
[parent_level
] = parent
;
4816 path
->slots
[parent_level
] = btrfs_header_nritems(parent
);
4818 btrfs_assert_tree_locked(node
);
4819 level
= btrfs_header_level(node
);
4820 extent_buffer_get(node
);
4821 path
->nodes
[level
] = node
;
4822 path
->slots
[level
] = 0;
4825 wret
= walk_down_tree(trans
, root
, path
, &level
);
4831 wret
= walk_up_tree(trans
, root
, path
, &level
, parent_level
);
4838 btrfs_free_path(path
);
4843 static unsigned long calc_ra(unsigned long start
, unsigned long last
,
4846 return min(last
, start
+ nr
- 1);
4849 static noinline
int relocate_inode_pages(struct inode
*inode
, u64 start
,
4854 unsigned long first_index
;
4855 unsigned long last_index
;
4858 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4859 struct file_ra_state
*ra
;
4860 struct btrfs_ordered_extent
*ordered
;
4861 unsigned int total_read
= 0;
4862 unsigned int total_dirty
= 0;
4865 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
4867 mutex_lock(&inode
->i_mutex
);
4868 first_index
= start
>> PAGE_CACHE_SHIFT
;
4869 last_index
= (start
+ len
- 1) >> PAGE_CACHE_SHIFT
;
4871 /* make sure the dirty trick played by the caller work */
4872 ret
= invalidate_inode_pages2_range(inode
->i_mapping
,
4873 first_index
, last_index
);
4877 file_ra_state_init(ra
, inode
->i_mapping
);
4879 for (i
= first_index
; i
<= last_index
; i
++) {
4880 if (total_read
% ra
->ra_pages
== 0) {
4881 btrfs_force_ra(inode
->i_mapping
, ra
, NULL
, i
,
4882 calc_ra(i
, last_index
, ra
->ra_pages
));
4886 if (((u64
)i
<< PAGE_CACHE_SHIFT
) > i_size_read(inode
))
4888 page
= grab_cache_page(inode
->i_mapping
, i
);
4893 if (!PageUptodate(page
)) {
4894 btrfs_readpage(NULL
, page
);
4896 if (!PageUptodate(page
)) {
4898 page_cache_release(page
);
4903 wait_on_page_writeback(page
);
4905 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
4906 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
4907 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
4909 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
4911 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
4913 page_cache_release(page
);
4914 btrfs_start_ordered_extent(inode
, ordered
, 1);
4915 btrfs_put_ordered_extent(ordered
);
4918 set_page_extent_mapped(page
);
4920 if (i
== first_index
)
4921 set_extent_bits(io_tree
, page_start
, page_end
,
4922 EXTENT_BOUNDARY
, GFP_NOFS
);
4923 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
4925 set_page_dirty(page
);
4928 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
4930 page_cache_release(page
);
4935 mutex_unlock(&inode
->i_mutex
);
4936 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, total_dirty
);
4940 static noinline
int relocate_data_extent(struct inode
*reloc_inode
,
4941 struct btrfs_key
*extent_key
,
4944 struct btrfs_root
*root
= BTRFS_I(reloc_inode
)->root
;
4945 struct extent_map_tree
*em_tree
= &BTRFS_I(reloc_inode
)->extent_tree
;
4946 struct extent_map
*em
;
4947 u64 start
= extent_key
->objectid
- offset
;
4948 u64 end
= start
+ extent_key
->offset
- 1;
4950 em
= alloc_extent_map(GFP_NOFS
);
4951 BUG_ON(!em
|| IS_ERR(em
));
4954 em
->len
= extent_key
->offset
;
4955 em
->block_len
= extent_key
->offset
;
4956 em
->block_start
= extent_key
->objectid
;
4957 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4958 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
4960 /* setup extent map to cheat btrfs_readpage */
4961 lock_extent(&BTRFS_I(reloc_inode
)->io_tree
, start
, end
, GFP_NOFS
);
4964 spin_lock(&em_tree
->lock
);
4965 ret
= add_extent_mapping(em_tree
, em
);
4966 spin_unlock(&em_tree
->lock
);
4967 if (ret
!= -EEXIST
) {
4968 free_extent_map(em
);
4971 btrfs_drop_extent_cache(reloc_inode
, start
, end
, 0);
4973 unlock_extent(&BTRFS_I(reloc_inode
)->io_tree
, start
, end
, GFP_NOFS
);
4975 return relocate_inode_pages(reloc_inode
, start
, extent_key
->offset
);
4978 struct btrfs_ref_path
{
4980 u64 nodes
[BTRFS_MAX_LEVEL
];
4982 u64 root_generation
;
4989 struct btrfs_key node_keys
[BTRFS_MAX_LEVEL
];
4990 u64 new_nodes
[BTRFS_MAX_LEVEL
];
4993 struct disk_extent
{
5004 static int is_cowonly_root(u64 root_objectid
)
5006 if (root_objectid
== BTRFS_ROOT_TREE_OBJECTID
||
5007 root_objectid
== BTRFS_EXTENT_TREE_OBJECTID
||
5008 root_objectid
== BTRFS_CHUNK_TREE_OBJECTID
||
5009 root_objectid
== BTRFS_DEV_TREE_OBJECTID
||
5010 root_objectid
== BTRFS_TREE_LOG_OBJECTID
||
5011 root_objectid
== BTRFS_CSUM_TREE_OBJECTID
)
5016 static noinline
int __next_ref_path(struct btrfs_trans_handle
*trans
,
5017 struct btrfs_root
*extent_root
,
5018 struct btrfs_ref_path
*ref_path
,
5021 struct extent_buffer
*leaf
;
5022 struct btrfs_path
*path
;
5023 struct btrfs_extent_ref
*ref
;
5024 struct btrfs_key key
;
5025 struct btrfs_key found_key
;
5031 path
= btrfs_alloc_path();
5036 ref_path
->lowest_level
= -1;
5037 ref_path
->current_level
= -1;
5038 ref_path
->shared_level
= -1;
5042 level
= ref_path
->current_level
- 1;
5043 while (level
>= -1) {
5045 if (level
< ref_path
->lowest_level
)
5049 bytenr
= ref_path
->nodes
[level
];
5051 bytenr
= ref_path
->extent_start
;
5052 BUG_ON(bytenr
== 0);
5054 parent
= ref_path
->nodes
[level
+ 1];
5055 ref_path
->nodes
[level
+ 1] = 0;
5056 ref_path
->current_level
= level
;
5057 BUG_ON(parent
== 0);
5059 key
.objectid
= bytenr
;
5060 key
.offset
= parent
+ 1;
5061 key
.type
= BTRFS_EXTENT_REF_KEY
;
5063 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
, 0, 0);
5068 leaf
= path
->nodes
[0];
5069 nritems
= btrfs_header_nritems(leaf
);
5070 if (path
->slots
[0] >= nritems
) {
5071 ret
= btrfs_next_leaf(extent_root
, path
);
5076 leaf
= path
->nodes
[0];
5079 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5080 if (found_key
.objectid
== bytenr
&&
5081 found_key
.type
== BTRFS_EXTENT_REF_KEY
) {
5082 if (level
< ref_path
->shared_level
)
5083 ref_path
->shared_level
= level
;
5088 btrfs_release_path(extent_root
, path
);
5091 /* reached lowest level */
5095 level
= ref_path
->current_level
;
5096 while (level
< BTRFS_MAX_LEVEL
- 1) {
5100 bytenr
= ref_path
->nodes
[level
];
5102 bytenr
= ref_path
->extent_start
;
5104 BUG_ON(bytenr
== 0);
5106 key
.objectid
= bytenr
;
5108 key
.type
= BTRFS_EXTENT_REF_KEY
;
5110 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
, 0, 0);
5114 leaf
= path
->nodes
[0];
5115 nritems
= btrfs_header_nritems(leaf
);
5116 if (path
->slots
[0] >= nritems
) {
5117 ret
= btrfs_next_leaf(extent_root
, path
);
5121 /* the extent was freed by someone */
5122 if (ref_path
->lowest_level
== level
)
5124 btrfs_release_path(extent_root
, path
);
5127 leaf
= path
->nodes
[0];
5130 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5131 if (found_key
.objectid
!= bytenr
||
5132 found_key
.type
!= BTRFS_EXTENT_REF_KEY
) {
5133 /* the extent was freed by someone */
5134 if (ref_path
->lowest_level
== level
) {
5138 btrfs_release_path(extent_root
, path
);
5142 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
5143 struct btrfs_extent_ref
);
5144 ref_objectid
= btrfs_ref_objectid(leaf
, ref
);
5145 if (ref_objectid
< BTRFS_FIRST_FREE_OBJECTID
) {
5147 level
= (int)ref_objectid
;
5148 BUG_ON(level
>= BTRFS_MAX_LEVEL
);
5149 ref_path
->lowest_level
= level
;
5150 ref_path
->current_level
= level
;
5151 ref_path
->nodes
[level
] = bytenr
;
5153 WARN_ON(ref_objectid
!= level
);
5156 WARN_ON(level
!= -1);
5160 if (ref_path
->lowest_level
== level
) {
5161 ref_path
->owner_objectid
= ref_objectid
;
5162 ref_path
->num_refs
= btrfs_ref_num_refs(leaf
, ref
);
5166 * the block is tree root or the block isn't in reference
5169 if (found_key
.objectid
== found_key
.offset
||
5170 is_cowonly_root(btrfs_ref_root(leaf
, ref
))) {
5171 ref_path
->root_objectid
= btrfs_ref_root(leaf
, ref
);
5172 ref_path
->root_generation
=
5173 btrfs_ref_generation(leaf
, ref
);
5175 /* special reference from the tree log */
5176 ref_path
->nodes
[0] = found_key
.offset
;
5177 ref_path
->current_level
= 0;
5184 BUG_ON(ref_path
->nodes
[level
] != 0);
5185 ref_path
->nodes
[level
] = found_key
.offset
;
5186 ref_path
->current_level
= level
;
5189 * the reference was created in the running transaction,
5190 * no need to continue walking up.
5192 if (btrfs_ref_generation(leaf
, ref
) == trans
->transid
) {
5193 ref_path
->root_objectid
= btrfs_ref_root(leaf
, ref
);
5194 ref_path
->root_generation
=
5195 btrfs_ref_generation(leaf
, ref
);
5200 btrfs_release_path(extent_root
, path
);
5203 /* reached max tree level, but no tree root found. */
5206 btrfs_free_path(path
);
5210 static int btrfs_first_ref_path(struct btrfs_trans_handle
*trans
,
5211 struct btrfs_root
*extent_root
,
5212 struct btrfs_ref_path
*ref_path
,
5215 memset(ref_path
, 0, sizeof(*ref_path
));
5216 ref_path
->extent_start
= extent_start
;
5218 return __next_ref_path(trans
, extent_root
, ref_path
, 1);
5221 static int btrfs_next_ref_path(struct btrfs_trans_handle
*trans
,
5222 struct btrfs_root
*extent_root
,
5223 struct btrfs_ref_path
*ref_path
)
5225 return __next_ref_path(trans
, extent_root
, ref_path
, 0);
5228 static noinline
int get_new_locations(struct inode
*reloc_inode
,
5229 struct btrfs_key
*extent_key
,
5230 u64 offset
, int no_fragment
,
5231 struct disk_extent
**extents
,
5234 struct btrfs_root
*root
= BTRFS_I(reloc_inode
)->root
;
5235 struct btrfs_path
*path
;
5236 struct btrfs_file_extent_item
*fi
;
5237 struct extent_buffer
*leaf
;
5238 struct disk_extent
*exts
= *extents
;
5239 struct btrfs_key found_key
;
5244 int max
= *nr_extents
;
5247 WARN_ON(!no_fragment
&& *extents
);
5250 exts
= kmalloc(sizeof(*exts
) * max
, GFP_NOFS
);
5255 path
= btrfs_alloc_path();
5258 cur_pos
= extent_key
->objectid
- offset
;
5259 last_byte
= extent_key
->objectid
+ extent_key
->offset
;
5260 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, reloc_inode
->i_ino
,
5270 leaf
= path
->nodes
[0];
5271 nritems
= btrfs_header_nritems(leaf
);
5272 if (path
->slots
[0] >= nritems
) {
5273 ret
= btrfs_next_leaf(root
, path
);
5278 leaf
= path
->nodes
[0];
5281 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5282 if (found_key
.offset
!= cur_pos
||
5283 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
||
5284 found_key
.objectid
!= reloc_inode
->i_ino
)
5287 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
5288 struct btrfs_file_extent_item
);
5289 if (btrfs_file_extent_type(leaf
, fi
) !=
5290 BTRFS_FILE_EXTENT_REG
||
5291 btrfs_file_extent_disk_bytenr(leaf
, fi
) == 0)
5295 struct disk_extent
*old
= exts
;
5297 exts
= kzalloc(sizeof(*exts
) * max
, GFP_NOFS
);
5298 memcpy(exts
, old
, sizeof(*exts
) * nr
);
5299 if (old
!= *extents
)
5303 exts
[nr
].disk_bytenr
=
5304 btrfs_file_extent_disk_bytenr(leaf
, fi
);
5305 exts
[nr
].disk_num_bytes
=
5306 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
5307 exts
[nr
].offset
= btrfs_file_extent_offset(leaf
, fi
);
5308 exts
[nr
].num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
5309 exts
[nr
].ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
5310 exts
[nr
].compression
= btrfs_file_extent_compression(leaf
, fi
);
5311 exts
[nr
].encryption
= btrfs_file_extent_encryption(leaf
, fi
);
5312 exts
[nr
].other_encoding
= btrfs_file_extent_other_encoding(leaf
,
5314 BUG_ON(exts
[nr
].offset
> 0);
5315 BUG_ON(exts
[nr
].compression
|| exts
[nr
].encryption
);
5316 BUG_ON(exts
[nr
].num_bytes
!= exts
[nr
].disk_num_bytes
);
5318 cur_pos
+= exts
[nr
].num_bytes
;
5321 if (cur_pos
+ offset
>= last_byte
)
5331 BUG_ON(cur_pos
+ offset
> last_byte
);
5332 if (cur_pos
+ offset
< last_byte
) {
5338 btrfs_free_path(path
);
5340 if (exts
!= *extents
)
5349 static noinline
int replace_one_extent(struct btrfs_trans_handle
*trans
,
5350 struct btrfs_root
*root
,
5351 struct btrfs_path
*path
,
5352 struct btrfs_key
*extent_key
,
5353 struct btrfs_key
*leaf_key
,
5354 struct btrfs_ref_path
*ref_path
,
5355 struct disk_extent
*new_extents
,
5358 struct extent_buffer
*leaf
;
5359 struct btrfs_file_extent_item
*fi
;
5360 struct inode
*inode
= NULL
;
5361 struct btrfs_key key
;
5366 u64 search_end
= (u64
)-1;
5369 int extent_locked
= 0;
5373 memcpy(&key
, leaf_key
, sizeof(key
));
5374 if (ref_path
->owner_objectid
!= BTRFS_MULTIPLE_OBJECTIDS
) {
5375 if (key
.objectid
< ref_path
->owner_objectid
||
5376 (key
.objectid
== ref_path
->owner_objectid
&&
5377 key
.type
< BTRFS_EXTENT_DATA_KEY
)) {
5378 key
.objectid
= ref_path
->owner_objectid
;
5379 key
.type
= BTRFS_EXTENT_DATA_KEY
;
5385 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
5389 leaf
= path
->nodes
[0];
5390 nritems
= btrfs_header_nritems(leaf
);
5392 if (extent_locked
&& ret
> 0) {
5394 * the file extent item was modified by someone
5395 * before the extent got locked.
5397 unlock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
5398 lock_end
, GFP_NOFS
);
5402 if (path
->slots
[0] >= nritems
) {
5403 if (++nr_scaned
> 2)
5406 BUG_ON(extent_locked
);
5407 ret
= btrfs_next_leaf(root
, path
);
5412 leaf
= path
->nodes
[0];
5413 nritems
= btrfs_header_nritems(leaf
);
5416 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
5418 if (ref_path
->owner_objectid
!= BTRFS_MULTIPLE_OBJECTIDS
) {
5419 if ((key
.objectid
> ref_path
->owner_objectid
) ||
5420 (key
.objectid
== ref_path
->owner_objectid
&&
5421 key
.type
> BTRFS_EXTENT_DATA_KEY
) ||
5422 key
.offset
>= search_end
)
5426 if (inode
&& key
.objectid
!= inode
->i_ino
) {
5427 BUG_ON(extent_locked
);
5428 btrfs_release_path(root
, path
);
5429 mutex_unlock(&inode
->i_mutex
);
5435 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
5440 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
5441 struct btrfs_file_extent_item
);
5442 extent_type
= btrfs_file_extent_type(leaf
, fi
);
5443 if ((extent_type
!= BTRFS_FILE_EXTENT_REG
&&
5444 extent_type
!= BTRFS_FILE_EXTENT_PREALLOC
) ||
5445 (btrfs_file_extent_disk_bytenr(leaf
, fi
) !=
5446 extent_key
->objectid
)) {
5452 num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
5453 ext_offset
= btrfs_file_extent_offset(leaf
, fi
);
5455 if (search_end
== (u64
)-1) {
5456 search_end
= key
.offset
- ext_offset
+
5457 btrfs_file_extent_ram_bytes(leaf
, fi
);
5460 if (!extent_locked
) {
5461 lock_start
= key
.offset
;
5462 lock_end
= lock_start
+ num_bytes
- 1;
5464 if (lock_start
> key
.offset
||
5465 lock_end
+ 1 < key
.offset
+ num_bytes
) {
5466 unlock_extent(&BTRFS_I(inode
)->io_tree
,
5467 lock_start
, lock_end
, GFP_NOFS
);
5473 btrfs_release_path(root
, path
);
5475 inode
= btrfs_iget_locked(root
->fs_info
->sb
,
5476 key
.objectid
, root
);
5477 if (inode
->i_state
& I_NEW
) {
5478 BTRFS_I(inode
)->root
= root
;
5479 BTRFS_I(inode
)->location
.objectid
=
5481 BTRFS_I(inode
)->location
.type
=
5482 BTRFS_INODE_ITEM_KEY
;
5483 BTRFS_I(inode
)->location
.offset
= 0;
5484 btrfs_read_locked_inode(inode
);
5485 unlock_new_inode(inode
);
5488 * some code call btrfs_commit_transaction while
5489 * holding the i_mutex, so we can't use mutex_lock
5492 if (is_bad_inode(inode
) ||
5493 !mutex_trylock(&inode
->i_mutex
)) {
5496 key
.offset
= (u64
)-1;
5501 if (!extent_locked
) {
5502 struct btrfs_ordered_extent
*ordered
;
5504 btrfs_release_path(root
, path
);
5506 lock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
5507 lock_end
, GFP_NOFS
);
5508 ordered
= btrfs_lookup_first_ordered_extent(inode
,
5511 ordered
->file_offset
<= lock_end
&&
5512 ordered
->file_offset
+ ordered
->len
> lock_start
) {
5513 unlock_extent(&BTRFS_I(inode
)->io_tree
,
5514 lock_start
, lock_end
, GFP_NOFS
);
5515 btrfs_start_ordered_extent(inode
, ordered
, 1);
5516 btrfs_put_ordered_extent(ordered
);
5517 key
.offset
+= num_bytes
;
5521 btrfs_put_ordered_extent(ordered
);
5527 if (nr_extents
== 1) {
5528 /* update extent pointer in place */
5529 btrfs_set_file_extent_disk_bytenr(leaf
, fi
,
5530 new_extents
[0].disk_bytenr
);
5531 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
,
5532 new_extents
[0].disk_num_bytes
);
5533 btrfs_mark_buffer_dirty(leaf
);
5535 btrfs_drop_extent_cache(inode
, key
.offset
,
5536 key
.offset
+ num_bytes
- 1, 0);
5538 ret
= btrfs_inc_extent_ref(trans
, root
,
5539 new_extents
[0].disk_bytenr
,
5540 new_extents
[0].disk_num_bytes
,
5542 root
->root_key
.objectid
,
5547 ret
= btrfs_free_extent(trans
, root
,
5548 extent_key
->objectid
,
5551 btrfs_header_owner(leaf
),
5552 btrfs_header_generation(leaf
),
5556 btrfs_release_path(root
, path
);
5557 key
.offset
+= num_bytes
;
5565 * drop old extent pointer at first, then insert the
5566 * new pointers one bye one
5568 btrfs_release_path(root
, path
);
5569 ret
= btrfs_drop_extents(trans
, root
, inode
, key
.offset
,
5570 key
.offset
+ num_bytes
,
5571 key
.offset
, &alloc_hint
);
5574 for (i
= 0; i
< nr_extents
; i
++) {
5575 if (ext_offset
>= new_extents
[i
].num_bytes
) {
5576 ext_offset
-= new_extents
[i
].num_bytes
;
5579 extent_len
= min(new_extents
[i
].num_bytes
-
5580 ext_offset
, num_bytes
);
5582 ret
= btrfs_insert_empty_item(trans
, root
,
5587 leaf
= path
->nodes
[0];
5588 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
5589 struct btrfs_file_extent_item
);
5590 btrfs_set_file_extent_generation(leaf
, fi
,
5592 btrfs_set_file_extent_type(leaf
, fi
,
5593 BTRFS_FILE_EXTENT_REG
);
5594 btrfs_set_file_extent_disk_bytenr(leaf
, fi
,
5595 new_extents
[i
].disk_bytenr
);
5596 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
,
5597 new_extents
[i
].disk_num_bytes
);
5598 btrfs_set_file_extent_ram_bytes(leaf
, fi
,
5599 new_extents
[i
].ram_bytes
);
5601 btrfs_set_file_extent_compression(leaf
, fi
,
5602 new_extents
[i
].compression
);
5603 btrfs_set_file_extent_encryption(leaf
, fi
,
5604 new_extents
[i
].encryption
);
5605 btrfs_set_file_extent_other_encoding(leaf
, fi
,
5606 new_extents
[i
].other_encoding
);
5608 btrfs_set_file_extent_num_bytes(leaf
, fi
,
5610 ext_offset
+= new_extents
[i
].offset
;
5611 btrfs_set_file_extent_offset(leaf
, fi
,
5613 btrfs_mark_buffer_dirty(leaf
);
5615 btrfs_drop_extent_cache(inode
, key
.offset
,
5616 key
.offset
+ extent_len
- 1, 0);
5618 ret
= btrfs_inc_extent_ref(trans
, root
,
5619 new_extents
[i
].disk_bytenr
,
5620 new_extents
[i
].disk_num_bytes
,
5622 root
->root_key
.objectid
,
5623 trans
->transid
, key
.objectid
);
5625 btrfs_release_path(root
, path
);
5627 inode_add_bytes(inode
, extent_len
);
5630 num_bytes
-= extent_len
;
5631 key
.offset
+= extent_len
;
5636 BUG_ON(i
>= nr_extents
);
5640 if (extent_locked
) {
5641 unlock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
5642 lock_end
, GFP_NOFS
);
5646 if (ref_path
->owner_objectid
!= BTRFS_MULTIPLE_OBJECTIDS
&&
5647 key
.offset
>= search_end
)
5654 btrfs_release_path(root
, path
);
5656 mutex_unlock(&inode
->i_mutex
);
5657 if (extent_locked
) {
5658 unlock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
5659 lock_end
, GFP_NOFS
);
5666 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle
*trans
,
5667 struct btrfs_root
*root
,
5668 struct extent_buffer
*buf
, u64 orig_start
)
5673 BUG_ON(btrfs_header_generation(buf
) != trans
->transid
);
5674 BUG_ON(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
5676 level
= btrfs_header_level(buf
);
5678 struct btrfs_leaf_ref
*ref
;
5679 struct btrfs_leaf_ref
*orig_ref
;
5681 orig_ref
= btrfs_lookup_leaf_ref(root
, orig_start
);
5685 ref
= btrfs_alloc_leaf_ref(root
, orig_ref
->nritems
);
5687 btrfs_free_leaf_ref(root
, orig_ref
);
5691 ref
->nritems
= orig_ref
->nritems
;
5692 memcpy(ref
->extents
, orig_ref
->extents
,
5693 sizeof(ref
->extents
[0]) * ref
->nritems
);
5695 btrfs_free_leaf_ref(root
, orig_ref
);
5697 ref
->root_gen
= trans
->transid
;
5698 ref
->bytenr
= buf
->start
;
5699 ref
->owner
= btrfs_header_owner(buf
);
5700 ref
->generation
= btrfs_header_generation(buf
);
5702 ret
= btrfs_add_leaf_ref(root
, ref
, 0);
5704 btrfs_free_leaf_ref(root
, ref
);
5709 static noinline
int invalidate_extent_cache(struct btrfs_root
*root
,
5710 struct extent_buffer
*leaf
,
5711 struct btrfs_block_group_cache
*group
,
5712 struct btrfs_root
*target_root
)
5714 struct btrfs_key key
;
5715 struct inode
*inode
= NULL
;
5716 struct btrfs_file_extent_item
*fi
;
5718 u64 skip_objectid
= 0;
5722 nritems
= btrfs_header_nritems(leaf
);
5723 for (i
= 0; i
< nritems
; i
++) {
5724 btrfs_item_key_to_cpu(leaf
, &key
, i
);
5725 if (key
.objectid
== skip_objectid
||
5726 key
.type
!= BTRFS_EXTENT_DATA_KEY
)
5728 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
5729 if (btrfs_file_extent_type(leaf
, fi
) ==
5730 BTRFS_FILE_EXTENT_INLINE
)
5732 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) == 0)
5734 if (!inode
|| inode
->i_ino
!= key
.objectid
) {
5736 inode
= btrfs_ilookup(target_root
->fs_info
->sb
,
5737 key
.objectid
, target_root
, 1);
5740 skip_objectid
= key
.objectid
;
5743 num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
5745 lock_extent(&BTRFS_I(inode
)->io_tree
, key
.offset
,
5746 key
.offset
+ num_bytes
- 1, GFP_NOFS
);
5747 btrfs_drop_extent_cache(inode
, key
.offset
,
5748 key
.offset
+ num_bytes
- 1, 1);
5749 unlock_extent(&BTRFS_I(inode
)->io_tree
, key
.offset
,
5750 key
.offset
+ num_bytes
- 1, GFP_NOFS
);
5757 static noinline
int replace_extents_in_leaf(struct btrfs_trans_handle
*trans
,
5758 struct btrfs_root
*root
,
5759 struct extent_buffer
*leaf
,
5760 struct btrfs_block_group_cache
*group
,
5761 struct inode
*reloc_inode
)
5763 struct btrfs_key key
;
5764 struct btrfs_key extent_key
;
5765 struct btrfs_file_extent_item
*fi
;
5766 struct btrfs_leaf_ref
*ref
;
5767 struct disk_extent
*new_extent
;
5776 new_extent
= kmalloc(sizeof(*new_extent
), GFP_NOFS
);
5777 BUG_ON(!new_extent
);
5779 ref
= btrfs_lookup_leaf_ref(root
, leaf
->start
);
5783 nritems
= btrfs_header_nritems(leaf
);
5784 for (i
= 0; i
< nritems
; i
++) {
5785 btrfs_item_key_to_cpu(leaf
, &key
, i
);
5786 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
5788 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
5789 if (btrfs_file_extent_type(leaf
, fi
) ==
5790 BTRFS_FILE_EXTENT_INLINE
)
5792 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
5793 num_bytes
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
5798 if (bytenr
>= group
->key
.objectid
+ group
->key
.offset
||
5799 bytenr
+ num_bytes
<= group
->key
.objectid
)
5802 extent_key
.objectid
= bytenr
;
5803 extent_key
.offset
= num_bytes
;
5804 extent_key
.type
= BTRFS_EXTENT_ITEM_KEY
;
5806 ret
= get_new_locations(reloc_inode
, &extent_key
,
5807 group
->key
.objectid
, 1,
5808 &new_extent
, &nr_extent
);
5813 BUG_ON(ref
->extents
[ext_index
].bytenr
!= bytenr
);
5814 BUG_ON(ref
->extents
[ext_index
].num_bytes
!= num_bytes
);
5815 ref
->extents
[ext_index
].bytenr
= new_extent
->disk_bytenr
;
5816 ref
->extents
[ext_index
].num_bytes
= new_extent
->disk_num_bytes
;
5818 btrfs_set_file_extent_disk_bytenr(leaf
, fi
,
5819 new_extent
->disk_bytenr
);
5820 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
,
5821 new_extent
->disk_num_bytes
);
5822 btrfs_mark_buffer_dirty(leaf
);
5824 ret
= btrfs_inc_extent_ref(trans
, root
,
5825 new_extent
->disk_bytenr
,
5826 new_extent
->disk_num_bytes
,
5828 root
->root_key
.objectid
,
5829 trans
->transid
, key
.objectid
);
5832 ret
= btrfs_free_extent(trans
, root
,
5833 bytenr
, num_bytes
, leaf
->start
,
5834 btrfs_header_owner(leaf
),
5835 btrfs_header_generation(leaf
),
5841 BUG_ON(ext_index
+ 1 != ref
->nritems
);
5842 btrfs_free_leaf_ref(root
, ref
);
5846 int btrfs_free_reloc_root(struct btrfs_trans_handle
*trans
,
5847 struct btrfs_root
*root
)
5849 struct btrfs_root
*reloc_root
;
5852 if (root
->reloc_root
) {
5853 reloc_root
= root
->reloc_root
;
5854 root
->reloc_root
= NULL
;
5855 list_add(&reloc_root
->dead_list
,
5856 &root
->fs_info
->dead_reloc_roots
);
5858 btrfs_set_root_bytenr(&reloc_root
->root_item
,
5859 reloc_root
->node
->start
);
5860 btrfs_set_root_level(&root
->root_item
,
5861 btrfs_header_level(reloc_root
->node
));
5862 memset(&reloc_root
->root_item
.drop_progress
, 0,
5863 sizeof(struct btrfs_disk_key
));
5864 reloc_root
->root_item
.drop_level
= 0;
5866 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
5867 &reloc_root
->root_key
,
5868 &reloc_root
->root_item
);
5874 int btrfs_drop_dead_reloc_roots(struct btrfs_root
*root
)
5876 struct btrfs_trans_handle
*trans
;
5877 struct btrfs_root
*reloc_root
;
5878 struct btrfs_root
*prev_root
= NULL
;
5879 struct list_head dead_roots
;
5883 INIT_LIST_HEAD(&dead_roots
);
5884 list_splice_init(&root
->fs_info
->dead_reloc_roots
, &dead_roots
);
5886 while (!list_empty(&dead_roots
)) {
5887 reloc_root
= list_entry(dead_roots
.prev
,
5888 struct btrfs_root
, dead_list
);
5889 list_del_init(&reloc_root
->dead_list
);
5891 BUG_ON(reloc_root
->commit_root
!= NULL
);
5893 trans
= btrfs_join_transaction(root
, 1);
5896 mutex_lock(&root
->fs_info
->drop_mutex
);
5897 ret
= btrfs_drop_snapshot(trans
, reloc_root
);
5900 mutex_unlock(&root
->fs_info
->drop_mutex
);
5902 nr
= trans
->blocks_used
;
5903 ret
= btrfs_end_transaction(trans
, root
);
5905 btrfs_btree_balance_dirty(root
, nr
);
5908 free_extent_buffer(reloc_root
->node
);
5910 ret
= btrfs_del_root(trans
, root
->fs_info
->tree_root
,
5911 &reloc_root
->root_key
);
5913 mutex_unlock(&root
->fs_info
->drop_mutex
);
5915 nr
= trans
->blocks_used
;
5916 ret
= btrfs_end_transaction(trans
, root
);
5918 btrfs_btree_balance_dirty(root
, nr
);
5921 prev_root
= reloc_root
;
5924 btrfs_remove_leaf_refs(prev_root
, (u64
)-1, 0);
5930 int btrfs_add_dead_reloc_root(struct btrfs_root
*root
)
5932 list_add(&root
->dead_list
, &root
->fs_info
->dead_reloc_roots
);
5936 int btrfs_cleanup_reloc_trees(struct btrfs_root
*root
)
5938 struct btrfs_root
*reloc_root
;
5939 struct btrfs_trans_handle
*trans
;
5940 struct btrfs_key location
;
5944 mutex_lock(&root
->fs_info
->tree_reloc_mutex
);
5945 ret
= btrfs_find_dead_roots(root
, BTRFS_TREE_RELOC_OBJECTID
, NULL
);
5947 found
= !list_empty(&root
->fs_info
->dead_reloc_roots
);
5948 mutex_unlock(&root
->fs_info
->tree_reloc_mutex
);
5951 trans
= btrfs_start_transaction(root
, 1);
5953 ret
= btrfs_commit_transaction(trans
, root
);
5957 location
.objectid
= BTRFS_DATA_RELOC_TREE_OBJECTID
;
5958 location
.offset
= (u64
)-1;
5959 location
.type
= BTRFS_ROOT_ITEM_KEY
;
5961 reloc_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
5962 BUG_ON(!reloc_root
);
5963 btrfs_orphan_cleanup(reloc_root
);
5967 static noinline
int init_reloc_tree(struct btrfs_trans_handle
*trans
,
5968 struct btrfs_root
*root
)
5970 struct btrfs_root
*reloc_root
;
5971 struct extent_buffer
*eb
;
5972 struct btrfs_root_item
*root_item
;
5973 struct btrfs_key root_key
;
5976 BUG_ON(!root
->ref_cows
);
5977 if (root
->reloc_root
)
5980 root_item
= kmalloc(sizeof(*root_item
), GFP_NOFS
);
5983 ret
= btrfs_copy_root(trans
, root
, root
->commit_root
,
5984 &eb
, BTRFS_TREE_RELOC_OBJECTID
);
5987 root_key
.objectid
= BTRFS_TREE_RELOC_OBJECTID
;
5988 root_key
.offset
= root
->root_key
.objectid
;
5989 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
5991 memcpy(root_item
, &root
->root_item
, sizeof(root_item
));
5992 btrfs_set_root_refs(root_item
, 0);
5993 btrfs_set_root_bytenr(root_item
, eb
->start
);
5994 btrfs_set_root_level(root_item
, btrfs_header_level(eb
));
5995 btrfs_set_root_generation(root_item
, trans
->transid
);
5997 btrfs_tree_unlock(eb
);
5998 free_extent_buffer(eb
);
6000 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
,
6001 &root_key
, root_item
);
6005 reloc_root
= btrfs_read_fs_root_no_radix(root
->fs_info
->tree_root
,
6007 BUG_ON(!reloc_root
);
6008 reloc_root
->last_trans
= trans
->transid
;
6009 reloc_root
->commit_root
= NULL
;
6010 reloc_root
->ref_tree
= &root
->fs_info
->reloc_ref_tree
;
6012 root
->reloc_root
= reloc_root
;
6017 * Core function of space balance.
6019 * The idea is using reloc trees to relocate tree blocks in reference
6020 * counted roots. There is one reloc tree for each subvol, and all
6021 * reloc trees share same root key objectid. Reloc trees are snapshots
6022 * of the latest committed roots of subvols (root->commit_root).
6024 * To relocate a tree block referenced by a subvol, there are two steps.
6025 * COW the block through subvol's reloc tree, then update block pointer
6026 * in the subvol to point to the new block. Since all reloc trees share
6027 * same root key objectid, doing special handing for tree blocks owned
6028 * by them is easy. Once a tree block has been COWed in one reloc tree,
6029 * we can use the resulting new block directly when the same block is
6030 * required to COW again through other reloc trees. By this way, relocated
6031 * tree blocks are shared between reloc trees, so they are also shared
6034 static noinline
int relocate_one_path(struct btrfs_trans_handle
*trans
,
6035 struct btrfs_root
*root
,
6036 struct btrfs_path
*path
,
6037 struct btrfs_key
*first_key
,
6038 struct btrfs_ref_path
*ref_path
,
6039 struct btrfs_block_group_cache
*group
,
6040 struct inode
*reloc_inode
)
6042 struct btrfs_root
*reloc_root
;
6043 struct extent_buffer
*eb
= NULL
;
6044 struct btrfs_key
*keys
;
6048 int lowest_level
= 0;
6051 if (ref_path
->owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
)
6052 lowest_level
= ref_path
->owner_objectid
;
6054 if (!root
->ref_cows
) {
6055 path
->lowest_level
= lowest_level
;
6056 ret
= btrfs_search_slot(trans
, root
, first_key
, path
, 0, 1);
6058 path
->lowest_level
= 0;
6059 btrfs_release_path(root
, path
);
6063 mutex_lock(&root
->fs_info
->tree_reloc_mutex
);
6064 ret
= init_reloc_tree(trans
, root
);
6066 reloc_root
= root
->reloc_root
;
6068 shared_level
= ref_path
->shared_level
;
6069 ref_path
->shared_level
= BTRFS_MAX_LEVEL
- 1;
6071 keys
= ref_path
->node_keys
;
6072 nodes
= ref_path
->new_nodes
;
6073 memset(&keys
[shared_level
+ 1], 0,
6074 sizeof(*keys
) * (BTRFS_MAX_LEVEL
- shared_level
- 1));
6075 memset(&nodes
[shared_level
+ 1], 0,
6076 sizeof(*nodes
) * (BTRFS_MAX_LEVEL
- shared_level
- 1));
6078 if (nodes
[lowest_level
] == 0) {
6079 path
->lowest_level
= lowest_level
;
6080 ret
= btrfs_search_slot(trans
, reloc_root
, first_key
, path
,
6083 for (level
= lowest_level
; level
< BTRFS_MAX_LEVEL
; level
++) {
6084 eb
= path
->nodes
[level
];
6085 if (!eb
|| eb
== reloc_root
->node
)
6087 nodes
[level
] = eb
->start
;
6089 btrfs_item_key_to_cpu(eb
, &keys
[level
], 0);
6091 btrfs_node_key_to_cpu(eb
, &keys
[level
], 0);
6094 ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
6095 eb
= path
->nodes
[0];
6096 ret
= replace_extents_in_leaf(trans
, reloc_root
, eb
,
6097 group
, reloc_inode
);
6100 btrfs_release_path(reloc_root
, path
);
6102 ret
= btrfs_merge_path(trans
, reloc_root
, keys
, nodes
,
6108 * replace tree blocks in the fs tree with tree blocks in
6111 ret
= btrfs_merge_path(trans
, root
, keys
, nodes
, lowest_level
);
6114 if (ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
6115 ret
= btrfs_search_slot(trans
, reloc_root
, first_key
, path
,
6118 extent_buffer_get(path
->nodes
[0]);
6119 eb
= path
->nodes
[0];
6120 btrfs_release_path(reloc_root
, path
);
6121 ret
= invalidate_extent_cache(reloc_root
, eb
, group
, root
);
6123 free_extent_buffer(eb
);
6126 mutex_unlock(&root
->fs_info
->tree_reloc_mutex
);
6127 path
->lowest_level
= 0;
6131 static noinline
int relocate_tree_block(struct btrfs_trans_handle
*trans
,
6132 struct btrfs_root
*root
,
6133 struct btrfs_path
*path
,
6134 struct btrfs_key
*first_key
,
6135 struct btrfs_ref_path
*ref_path
)
6139 ret
= relocate_one_path(trans
, root
, path
, first_key
,
6140 ref_path
, NULL
, NULL
);
6146 static noinline
int del_extent_zero(struct btrfs_trans_handle
*trans
,
6147 struct btrfs_root
*extent_root
,
6148 struct btrfs_path
*path
,
6149 struct btrfs_key
*extent_key
)
6153 ret
= btrfs_search_slot(trans
, extent_root
, extent_key
, path
, -1, 1);
6156 ret
= btrfs_del_item(trans
, extent_root
, path
);
6158 btrfs_release_path(extent_root
, path
);
6162 static noinline
struct btrfs_root
*read_ref_root(struct btrfs_fs_info
*fs_info
,
6163 struct btrfs_ref_path
*ref_path
)
6165 struct btrfs_key root_key
;
6167 root_key
.objectid
= ref_path
->root_objectid
;
6168 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
6169 if (is_cowonly_root(ref_path
->root_objectid
))
6170 root_key
.offset
= 0;
6172 root_key
.offset
= (u64
)-1;
6174 return btrfs_read_fs_root_no_name(fs_info
, &root_key
);
6177 static noinline
int relocate_one_extent(struct btrfs_root
*extent_root
,
6178 struct btrfs_path
*path
,
6179 struct btrfs_key
*extent_key
,
6180 struct btrfs_block_group_cache
*group
,
6181 struct inode
*reloc_inode
, int pass
)
6183 struct btrfs_trans_handle
*trans
;
6184 struct btrfs_root
*found_root
;
6185 struct btrfs_ref_path
*ref_path
= NULL
;
6186 struct disk_extent
*new_extents
= NULL
;
6191 struct btrfs_key first_key
;
6195 trans
= btrfs_start_transaction(extent_root
, 1);
6198 if (extent_key
->objectid
== 0) {
6199 ret
= del_extent_zero(trans
, extent_root
, path
, extent_key
);
6203 ref_path
= kmalloc(sizeof(*ref_path
), GFP_NOFS
);
6209 for (loops
= 0; ; loops
++) {
6211 ret
= btrfs_first_ref_path(trans
, extent_root
, ref_path
,
6212 extent_key
->objectid
);
6214 ret
= btrfs_next_ref_path(trans
, extent_root
, ref_path
);
6221 if (ref_path
->root_objectid
== BTRFS_TREE_LOG_OBJECTID
||
6222 ref_path
->root_objectid
== BTRFS_TREE_RELOC_OBJECTID
)
6225 found_root
= read_ref_root(extent_root
->fs_info
, ref_path
);
6226 BUG_ON(!found_root
);
6228 * for reference counted tree, only process reference paths
6229 * rooted at the latest committed root.
6231 if (found_root
->ref_cows
&&
6232 ref_path
->root_generation
!= found_root
->root_key
.offset
)
6235 if (ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
6238 * copy data extents to new locations
6240 u64 group_start
= group
->key
.objectid
;
6241 ret
= relocate_data_extent(reloc_inode
,
6250 level
= ref_path
->owner_objectid
;
6253 if (prev_block
!= ref_path
->nodes
[level
]) {
6254 struct extent_buffer
*eb
;
6255 u64 block_start
= ref_path
->nodes
[level
];
6256 u64 block_size
= btrfs_level_size(found_root
, level
);
6258 eb
= read_tree_block(found_root
, block_start
,
6260 btrfs_tree_lock(eb
);
6261 BUG_ON(level
!= btrfs_header_level(eb
));
6264 btrfs_item_key_to_cpu(eb
, &first_key
, 0);
6266 btrfs_node_key_to_cpu(eb
, &first_key
, 0);
6268 btrfs_tree_unlock(eb
);
6269 free_extent_buffer(eb
);
6270 prev_block
= block_start
;
6273 mutex_lock(&extent_root
->fs_info
->trans_mutex
);
6274 btrfs_record_root_in_trans(found_root
);
6275 mutex_unlock(&extent_root
->fs_info
->trans_mutex
);
6276 if (ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
6278 * try to update data extent references while
6279 * keeping metadata shared between snapshots.
6282 ret
= relocate_one_path(trans
, found_root
,
6283 path
, &first_key
, ref_path
,
6284 group
, reloc_inode
);
6290 * use fallback method to process the remaining
6294 u64 group_start
= group
->key
.objectid
;
6295 new_extents
= kmalloc(sizeof(*new_extents
),
6298 ret
= get_new_locations(reloc_inode
,
6306 ret
= replace_one_extent(trans
, found_root
,
6308 &first_key
, ref_path
,
6309 new_extents
, nr_extents
);
6311 ret
= relocate_tree_block(trans
, found_root
, path
,
6312 &first_key
, ref_path
);
6319 btrfs_end_transaction(trans
, extent_root
);
6326 static u64
update_block_group_flags(struct btrfs_root
*root
, u64 flags
)
6329 u64 stripped
= BTRFS_BLOCK_GROUP_RAID0
|
6330 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
;
6332 num_devices
= root
->fs_info
->fs_devices
->rw_devices
;
6333 if (num_devices
== 1) {
6334 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
6335 stripped
= flags
& ~stripped
;
6337 /* turn raid0 into single device chunks */
6338 if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
6341 /* turn mirroring into duplication */
6342 if (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
6343 BTRFS_BLOCK_GROUP_RAID10
))
6344 return stripped
| BTRFS_BLOCK_GROUP_DUP
;
6347 /* they already had raid on here, just return */
6348 if (flags
& stripped
)
6351 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
6352 stripped
= flags
& ~stripped
;
6354 /* switch duplicated blocks with raid1 */
6355 if (flags
& BTRFS_BLOCK_GROUP_DUP
)
6356 return stripped
| BTRFS_BLOCK_GROUP_RAID1
;
6358 /* turn single device chunks into raid0 */
6359 return stripped
| BTRFS_BLOCK_GROUP_RAID0
;
6364 static int __alloc_chunk_for_shrink(struct btrfs_root
*root
,
6365 struct btrfs_block_group_cache
*shrink_block_group
,
6368 struct btrfs_trans_handle
*trans
;
6369 u64 new_alloc_flags
;
6372 spin_lock(&shrink_block_group
->lock
);
6373 if (btrfs_block_group_used(&shrink_block_group
->item
) +
6374 shrink_block_group
->reserved
> 0) {
6375 spin_unlock(&shrink_block_group
->lock
);
6377 trans
= btrfs_start_transaction(root
, 1);
6378 spin_lock(&shrink_block_group
->lock
);
6380 new_alloc_flags
= update_block_group_flags(root
,
6381 shrink_block_group
->flags
);
6382 if (new_alloc_flags
!= shrink_block_group
->flags
) {
6384 btrfs_block_group_used(&shrink_block_group
->item
);
6386 calc
= shrink_block_group
->key
.offset
;
6388 spin_unlock(&shrink_block_group
->lock
);
6390 do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
6391 calc
+ 2 * 1024 * 1024, new_alloc_flags
, force
);
6393 btrfs_end_transaction(trans
, root
);
6395 spin_unlock(&shrink_block_group
->lock
);
6400 int btrfs_prepare_block_group_relocation(struct btrfs_root
*root
,
6401 struct btrfs_block_group_cache
*group
)
6404 __alloc_chunk_for_shrink(root
, group
, 1);
6405 set_block_group_readonly(group
);
6410 static int __insert_orphan_inode(struct btrfs_trans_handle
*trans
,
6411 struct btrfs_root
*root
,
6412 u64 objectid
, u64 size
)
6414 struct btrfs_path
*path
;
6415 struct btrfs_inode_item
*item
;
6416 struct extent_buffer
*leaf
;
6419 path
= btrfs_alloc_path();
6423 path
->leave_spinning
= 1;
6424 ret
= btrfs_insert_empty_inode(trans
, root
, path
, objectid
);
6428 leaf
= path
->nodes
[0];
6429 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_inode_item
);
6430 memset_extent_buffer(leaf
, 0, (unsigned long)item
, sizeof(*item
));
6431 btrfs_set_inode_generation(leaf
, item
, 1);
6432 btrfs_set_inode_size(leaf
, item
, size
);
6433 btrfs_set_inode_mode(leaf
, item
, S_IFREG
| 0600);
6434 btrfs_set_inode_flags(leaf
, item
, BTRFS_INODE_NOCOMPRESS
);
6435 btrfs_mark_buffer_dirty(leaf
);
6436 btrfs_release_path(root
, path
);
6438 btrfs_free_path(path
);
6442 static noinline
struct inode
*create_reloc_inode(struct btrfs_fs_info
*fs_info
,
6443 struct btrfs_block_group_cache
*group
)
6445 struct inode
*inode
= NULL
;
6446 struct btrfs_trans_handle
*trans
;
6447 struct btrfs_root
*root
;
6448 struct btrfs_key root_key
;
6449 u64 objectid
= BTRFS_FIRST_FREE_OBJECTID
;
6452 root_key
.objectid
= BTRFS_DATA_RELOC_TREE_OBJECTID
;
6453 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
6454 root_key
.offset
= (u64
)-1;
6455 root
= btrfs_read_fs_root_no_name(fs_info
, &root_key
);
6457 return ERR_CAST(root
);
6459 trans
= btrfs_start_transaction(root
, 1);
6462 err
= btrfs_find_free_objectid(trans
, root
, objectid
, &objectid
);
6466 err
= __insert_orphan_inode(trans
, root
, objectid
, group
->key
.offset
);
6469 err
= btrfs_insert_file_extent(trans
, root
, objectid
, 0, 0, 0,
6470 group
->key
.offset
, 0, group
->key
.offset
,
6474 inode
= btrfs_iget_locked(root
->fs_info
->sb
, objectid
, root
);
6475 if (inode
->i_state
& I_NEW
) {
6476 BTRFS_I(inode
)->root
= root
;
6477 BTRFS_I(inode
)->location
.objectid
= objectid
;
6478 BTRFS_I(inode
)->location
.type
= BTRFS_INODE_ITEM_KEY
;
6479 BTRFS_I(inode
)->location
.offset
= 0;
6480 btrfs_read_locked_inode(inode
);
6481 unlock_new_inode(inode
);
6482 BUG_ON(is_bad_inode(inode
));
6486 BTRFS_I(inode
)->index_cnt
= group
->key
.objectid
;
6488 err
= btrfs_orphan_add(trans
, inode
);
6490 btrfs_end_transaction(trans
, root
);
6494 inode
= ERR_PTR(err
);
6499 int btrfs_reloc_clone_csums(struct inode
*inode
, u64 file_pos
, u64 len
)
6502 struct btrfs_ordered_sum
*sums
;
6503 struct btrfs_sector_sum
*sector_sum
;
6504 struct btrfs_ordered_extent
*ordered
;
6505 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6506 struct list_head list
;
6511 INIT_LIST_HEAD(&list
);
6513 ordered
= btrfs_lookup_ordered_extent(inode
, file_pos
);
6514 BUG_ON(ordered
->file_offset
!= file_pos
|| ordered
->len
!= len
);
6516 disk_bytenr
= file_pos
+ BTRFS_I(inode
)->index_cnt
;
6517 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, disk_bytenr
,
6518 disk_bytenr
+ len
- 1, &list
);
6520 while (!list_empty(&list
)) {
6521 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
6522 list_del_init(&sums
->list
);
6524 sector_sum
= sums
->sums
;
6525 sums
->bytenr
= ordered
->start
;
6528 while (offset
< sums
->len
) {
6529 sector_sum
->bytenr
+= ordered
->start
- disk_bytenr
;
6531 offset
+= root
->sectorsize
;
6534 btrfs_add_ordered_sum(inode
, ordered
, sums
);
6536 btrfs_put_ordered_extent(ordered
);
6540 int btrfs_relocate_block_group(struct btrfs_root
*root
, u64 group_start
)
6542 struct btrfs_trans_handle
*trans
;
6543 struct btrfs_path
*path
;
6544 struct btrfs_fs_info
*info
= root
->fs_info
;
6545 struct extent_buffer
*leaf
;
6546 struct inode
*reloc_inode
;
6547 struct btrfs_block_group_cache
*block_group
;
6548 struct btrfs_key key
;
6557 root
= root
->fs_info
->extent_root
;
6559 block_group
= btrfs_lookup_block_group(info
, group_start
);
6560 BUG_ON(!block_group
);
6562 printk(KERN_INFO
"btrfs relocating block group %llu flags %llu\n",
6563 (unsigned long long)block_group
->key
.objectid
,
6564 (unsigned long long)block_group
->flags
);
6566 path
= btrfs_alloc_path();
6569 reloc_inode
= create_reloc_inode(info
, block_group
);
6570 BUG_ON(IS_ERR(reloc_inode
));
6572 __alloc_chunk_for_shrink(root
, block_group
, 1);
6573 set_block_group_readonly(block_group
);
6575 btrfs_start_delalloc_inodes(info
->tree_root
);
6576 btrfs_wait_ordered_extents(info
->tree_root
, 0);
6581 key
.objectid
= block_group
->key
.objectid
;
6584 cur_byte
= key
.objectid
;
6586 trans
= btrfs_start_transaction(info
->tree_root
, 1);
6587 btrfs_commit_transaction(trans
, info
->tree_root
);
6589 mutex_lock(&root
->fs_info
->cleaner_mutex
);
6590 btrfs_clean_old_snapshots(info
->tree_root
);
6591 btrfs_remove_leaf_refs(info
->tree_root
, (u64
)-1, 1);
6592 mutex_unlock(&root
->fs_info
->cleaner_mutex
);
6594 trans
= btrfs_start_transaction(info
->tree_root
, 1);
6595 btrfs_commit_transaction(trans
, info
->tree_root
);
6598 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
6602 leaf
= path
->nodes
[0];
6603 nritems
= btrfs_header_nritems(leaf
);
6604 if (path
->slots
[0] >= nritems
) {
6605 ret
= btrfs_next_leaf(root
, path
);
6612 leaf
= path
->nodes
[0];
6613 nritems
= btrfs_header_nritems(leaf
);
6616 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
6618 if (key
.objectid
>= block_group
->key
.objectid
+
6619 block_group
->key
.offset
)
6622 if (progress
&& need_resched()) {
6623 btrfs_release_path(root
, path
);
6630 if (btrfs_key_type(&key
) != BTRFS_EXTENT_ITEM_KEY
||
6631 key
.objectid
+ key
.offset
<= cur_byte
) {
6637 cur_byte
= key
.objectid
+ key
.offset
;
6638 btrfs_release_path(root
, path
);
6640 __alloc_chunk_for_shrink(root
, block_group
, 0);
6641 ret
= relocate_one_extent(root
, path
, &key
, block_group
,
6647 key
.objectid
= cur_byte
;
6652 btrfs_release_path(root
, path
);
6655 btrfs_wait_ordered_range(reloc_inode
, 0, (u64
)-1);
6656 invalidate_mapping_pages(reloc_inode
->i_mapping
, 0, -1);
6659 if (total_found
> 0) {
6660 printk(KERN_INFO
"btrfs found %llu extents in pass %d\n",
6661 (unsigned long long)total_found
, pass
);
6663 if (total_found
== skipped
&& pass
> 2) {
6665 reloc_inode
= create_reloc_inode(info
, block_group
);
6671 /* delete reloc_inode */
6674 /* unpin extents in this range */
6675 trans
= btrfs_start_transaction(info
->tree_root
, 1);
6676 btrfs_commit_transaction(trans
, info
->tree_root
);
6678 spin_lock(&block_group
->lock
);
6679 WARN_ON(block_group
->pinned
> 0);
6680 WARN_ON(block_group
->reserved
> 0);
6681 WARN_ON(btrfs_block_group_used(&block_group
->item
) > 0);
6682 spin_unlock(&block_group
->lock
);
6683 btrfs_put_block_group(block_group
);
6686 btrfs_free_path(path
);
6691 static int find_first_block_group(struct btrfs_root
*root
,
6692 struct btrfs_path
*path
, struct btrfs_key
*key
)
6695 struct btrfs_key found_key
;
6696 struct extent_buffer
*leaf
;
6699 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
6704 slot
= path
->slots
[0];
6705 leaf
= path
->nodes
[0];
6706 if (slot
>= btrfs_header_nritems(leaf
)) {
6707 ret
= btrfs_next_leaf(root
, path
);
6714 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
6716 if (found_key
.objectid
>= key
->objectid
&&
6717 found_key
.type
== BTRFS_BLOCK_GROUP_ITEM_KEY
) {
6728 int btrfs_free_block_groups(struct btrfs_fs_info
*info
)
6730 struct btrfs_block_group_cache
*block_group
;
6731 struct btrfs_space_info
*space_info
;
6734 spin_lock(&info
->block_group_cache_lock
);
6735 while ((n
= rb_last(&info
->block_group_cache_tree
)) != NULL
) {
6736 block_group
= rb_entry(n
, struct btrfs_block_group_cache
,
6738 rb_erase(&block_group
->cache_node
,
6739 &info
->block_group_cache_tree
);
6740 spin_unlock(&info
->block_group_cache_lock
);
6742 btrfs_remove_free_space_cache(block_group
);
6743 down_write(&block_group
->space_info
->groups_sem
);
6744 list_del(&block_group
->list
);
6745 up_write(&block_group
->space_info
->groups_sem
);
6747 WARN_ON(atomic_read(&block_group
->count
) != 1);
6750 spin_lock(&info
->block_group_cache_lock
);
6752 spin_unlock(&info
->block_group_cache_lock
);
6754 /* now that all the block groups are freed, go through and
6755 * free all the space_info structs. This is only called during
6756 * the final stages of unmount, and so we know nobody is
6757 * using them. We call synchronize_rcu() once before we start,
6758 * just to be on the safe side.
6762 while(!list_empty(&info
->space_info
)) {
6763 space_info
= list_entry(info
->space_info
.next
,
6764 struct btrfs_space_info
,
6767 list_del(&space_info
->list
);
6773 int btrfs_read_block_groups(struct btrfs_root
*root
)
6775 struct btrfs_path
*path
;
6777 struct btrfs_block_group_cache
*cache
;
6778 struct btrfs_fs_info
*info
= root
->fs_info
;
6779 struct btrfs_space_info
*space_info
;
6780 struct btrfs_key key
;
6781 struct btrfs_key found_key
;
6782 struct extent_buffer
*leaf
;
6784 root
= info
->extent_root
;
6787 btrfs_set_key_type(&key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
6788 path
= btrfs_alloc_path();
6793 ret
= find_first_block_group(root
, path
, &key
);
6801 leaf
= path
->nodes
[0];
6802 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6803 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
6809 atomic_set(&cache
->count
, 1);
6810 spin_lock_init(&cache
->lock
);
6811 spin_lock_init(&cache
->tree_lock
);
6812 mutex_init(&cache
->cache_mutex
);
6813 INIT_LIST_HEAD(&cache
->list
);
6814 INIT_LIST_HEAD(&cache
->cluster_list
);
6815 read_extent_buffer(leaf
, &cache
->item
,
6816 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
6817 sizeof(cache
->item
));
6818 memcpy(&cache
->key
, &found_key
, sizeof(found_key
));
6820 key
.objectid
= found_key
.objectid
+ found_key
.offset
;
6821 btrfs_release_path(root
, path
);
6822 cache
->flags
= btrfs_block_group_flags(&cache
->item
);
6824 ret
= update_space_info(info
, cache
->flags
, found_key
.offset
,
6825 btrfs_block_group_used(&cache
->item
),
6828 cache
->space_info
= space_info
;
6829 down_write(&space_info
->groups_sem
);
6830 list_add_tail(&cache
->list
, &space_info
->block_groups
);
6831 up_write(&space_info
->groups_sem
);
6833 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
6836 set_avail_alloc_bits(root
->fs_info
, cache
->flags
);
6837 if (btrfs_chunk_readonly(root
, cache
->key
.objectid
))
6838 set_block_group_readonly(cache
);
6842 btrfs_free_path(path
);
6846 int btrfs_make_block_group(struct btrfs_trans_handle
*trans
,
6847 struct btrfs_root
*root
, u64 bytes_used
,
6848 u64 type
, u64 chunk_objectid
, u64 chunk_offset
,
6852 struct btrfs_root
*extent_root
;
6853 struct btrfs_block_group_cache
*cache
;
6855 extent_root
= root
->fs_info
->extent_root
;
6857 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
6859 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
6863 cache
->key
.objectid
= chunk_offset
;
6864 cache
->key
.offset
= size
;
6865 cache
->key
.type
= BTRFS_BLOCK_GROUP_ITEM_KEY
;
6866 atomic_set(&cache
->count
, 1);
6867 spin_lock_init(&cache
->lock
);
6868 spin_lock_init(&cache
->tree_lock
);
6869 mutex_init(&cache
->cache_mutex
);
6870 INIT_LIST_HEAD(&cache
->list
);
6871 INIT_LIST_HEAD(&cache
->cluster_list
);
6873 btrfs_set_block_group_used(&cache
->item
, bytes_used
);
6874 btrfs_set_block_group_chunk_objectid(&cache
->item
, chunk_objectid
);
6875 cache
->flags
= type
;
6876 btrfs_set_block_group_flags(&cache
->item
, type
);
6878 ret
= update_space_info(root
->fs_info
, cache
->flags
, size
, bytes_used
,
6879 &cache
->space_info
);
6881 down_write(&cache
->space_info
->groups_sem
);
6882 list_add_tail(&cache
->list
, &cache
->space_info
->block_groups
);
6883 up_write(&cache
->space_info
->groups_sem
);
6885 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
6888 ret
= btrfs_insert_item(trans
, extent_root
, &cache
->key
, &cache
->item
,
6889 sizeof(cache
->item
));
6892 set_avail_alloc_bits(extent_root
->fs_info
, type
);
6897 int btrfs_remove_block_group(struct btrfs_trans_handle
*trans
,
6898 struct btrfs_root
*root
, u64 group_start
)
6900 struct btrfs_path
*path
;
6901 struct btrfs_block_group_cache
*block_group
;
6902 struct btrfs_free_cluster
*cluster
;
6903 struct btrfs_key key
;
6906 root
= root
->fs_info
->extent_root
;
6908 block_group
= btrfs_lookup_block_group(root
->fs_info
, group_start
);
6909 BUG_ON(!block_group
);
6910 BUG_ON(!block_group
->ro
);
6912 memcpy(&key
, &block_group
->key
, sizeof(key
));
6914 /* make sure this block group isn't part of an allocation cluster */
6915 cluster
= &root
->fs_info
->data_alloc_cluster
;
6916 spin_lock(&cluster
->refill_lock
);
6917 btrfs_return_cluster_to_free_space(block_group
, cluster
);
6918 spin_unlock(&cluster
->refill_lock
);
6921 * make sure this block group isn't part of a metadata
6922 * allocation cluster
6924 cluster
= &root
->fs_info
->meta_alloc_cluster
;
6925 spin_lock(&cluster
->refill_lock
);
6926 btrfs_return_cluster_to_free_space(block_group
, cluster
);
6927 spin_unlock(&cluster
->refill_lock
);
6929 path
= btrfs_alloc_path();
6932 spin_lock(&root
->fs_info
->block_group_cache_lock
);
6933 rb_erase(&block_group
->cache_node
,
6934 &root
->fs_info
->block_group_cache_tree
);
6935 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
6936 btrfs_remove_free_space_cache(block_group
);
6937 down_write(&block_group
->space_info
->groups_sem
);
6939 * we must use list_del_init so people can check to see if they
6940 * are still on the list after taking the semaphore
6942 list_del_init(&block_group
->list
);
6943 up_write(&block_group
->space_info
->groups_sem
);
6945 spin_lock(&block_group
->space_info
->lock
);
6946 block_group
->space_info
->total_bytes
-= block_group
->key
.offset
;
6947 block_group
->space_info
->bytes_readonly
-= block_group
->key
.offset
;
6948 spin_unlock(&block_group
->space_info
->lock
);
6949 block_group
->space_info
->full
= 0;
6951 btrfs_put_block_group(block_group
);
6952 btrfs_put_block_group(block_group
);
6954 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
6960 ret
= btrfs_del_item(trans
, root
, path
);
6962 btrfs_free_path(path
);