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>
26 #include "print-tree.h"
27 #include "transaction.h"
30 #include "ref-cache.h"
32 #define PENDING_EXTENT_INSERT 0
33 #define PENDING_EXTENT_DELETE 1
34 #define PENDING_BACKREF_UPDATE 2
36 struct pending_extent_op
{
47 static int finish_current_insert(struct btrfs_trans_handle
*trans
, struct
48 btrfs_root
*extent_root
, int all
);
49 static int del_pending_extents(struct btrfs_trans_handle
*trans
, struct
50 btrfs_root
*extent_root
, int all
);
51 static struct btrfs_block_group_cache
*
52 __btrfs_find_block_group(struct btrfs_root
*root
,
53 struct btrfs_block_group_cache
*hint
,
54 u64 search_start
, int data
, int owner
);
56 static int block_group_bits(struct btrfs_block_group_cache
*cache
, u64 bits
)
58 return (cache
->flags
& bits
) == bits
;
62 * this adds the block group to the fs_info rb tree for the block group
65 int btrfs_add_block_group_cache(struct btrfs_fs_info
*info
,
66 struct btrfs_block_group_cache
*block_group
)
69 struct rb_node
*parent
= NULL
;
70 struct btrfs_block_group_cache
*cache
;
72 spin_lock(&info
->block_group_cache_lock
);
73 p
= &info
->block_group_cache_tree
.rb_node
;
77 cache
= rb_entry(parent
, struct btrfs_block_group_cache
,
79 if (block_group
->key
.objectid
< cache
->key
.objectid
) {
81 } else if (block_group
->key
.objectid
> cache
->key
.objectid
) {
84 spin_unlock(&info
->block_group_cache_lock
);
89 rb_link_node(&block_group
->cache_node
, parent
, p
);
90 rb_insert_color(&block_group
->cache_node
,
91 &info
->block_group_cache_tree
);
92 spin_unlock(&info
->block_group_cache_lock
);
98 * This will return the block group at or after bytenr if contains is 0, else
99 * it will return the block group that contains the bytenr
101 static struct btrfs_block_group_cache
*
102 block_group_cache_tree_search(struct btrfs_fs_info
*info
, u64 bytenr
,
105 struct btrfs_block_group_cache
*cache
, *ret
= NULL
;
109 spin_lock(&info
->block_group_cache_lock
);
110 n
= info
->block_group_cache_tree
.rb_node
;
113 cache
= rb_entry(n
, struct btrfs_block_group_cache
,
115 end
= cache
->key
.objectid
+ cache
->key
.offset
- 1;
116 start
= cache
->key
.objectid
;
118 if (bytenr
< start
) {
119 if (!contains
&& (!ret
|| start
< ret
->key
.objectid
))
122 } else if (bytenr
> start
) {
123 if (contains
&& bytenr
<= end
) {
133 spin_unlock(&info
->block_group_cache_lock
);
139 * this is only called by cache_block_group, since we could have freed extents
140 * we need to check the pinned_extents for any extents that can't be used yet
141 * since their free space will be released as soon as the transaction commits.
143 static int add_new_free_space(struct btrfs_block_group_cache
*block_group
,
144 struct btrfs_fs_info
*info
, u64 start
, u64 end
)
146 u64 extent_start
, extent_end
, size
;
149 mutex_lock(&info
->pinned_mutex
);
150 while (start
< end
) {
151 ret
= find_first_extent_bit(&info
->pinned_extents
, start
,
152 &extent_start
, &extent_end
,
157 if (extent_start
== start
) {
158 start
= extent_end
+ 1;
159 } else if (extent_start
> start
&& extent_start
< end
) {
160 size
= extent_start
- start
;
161 ret
= btrfs_add_free_space_lock(block_group
, start
,
164 start
= extent_end
+ 1;
172 ret
= btrfs_add_free_space_lock(block_group
, start
, size
);
175 mutex_unlock(&info
->pinned_mutex
);
180 static int cache_block_group(struct btrfs_root
*root
,
181 struct btrfs_block_group_cache
*block_group
)
183 struct btrfs_path
*path
;
185 struct btrfs_key key
;
186 struct extent_buffer
*leaf
;
195 root
= root
->fs_info
->extent_root
;
197 if (block_group
->cached
)
200 path
= btrfs_alloc_path();
206 * we get into deadlocks with paths held by callers of this function.
207 * since the alloc_mutex is protecting things right now, just
208 * skip the locking here
210 path
->skip_locking
= 1;
211 first_free
= max_t(u64
, block_group
->key
.objectid
,
212 BTRFS_SUPER_INFO_OFFSET
+ BTRFS_SUPER_INFO_SIZE
);
213 key
.objectid
= block_group
->key
.objectid
;
215 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
216 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
219 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_EXTENT_ITEM_KEY
);
223 leaf
= path
->nodes
[0];
224 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
225 if (key
.objectid
+ key
.offset
> first_free
)
226 first_free
= key
.objectid
+ key
.offset
;
229 leaf
= path
->nodes
[0];
230 slot
= path
->slots
[0];
231 if (slot
>= btrfs_header_nritems(leaf
)) {
232 ret
= btrfs_next_leaf(root
, path
);
240 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
241 if (key
.objectid
< block_group
->key
.objectid
)
244 if (key
.objectid
>= block_group
->key
.objectid
+
245 block_group
->key
.offset
)
248 if (btrfs_key_type(&key
) == BTRFS_EXTENT_ITEM_KEY
) {
254 add_new_free_space(block_group
, root
->fs_info
, last
,
257 last
= key
.objectid
+ key
.offset
;
266 add_new_free_space(block_group
, root
->fs_info
, last
,
267 block_group
->key
.objectid
+
268 block_group
->key
.offset
);
270 block_group
->cached
= 1;
273 btrfs_free_path(path
);
278 * return the block group that starts at or after bytenr
280 struct btrfs_block_group_cache
*btrfs_lookup_first_block_group(struct
284 struct btrfs_block_group_cache
*cache
;
286 cache
= block_group_cache_tree_search(info
, bytenr
, 0);
292 * return the block group that contains teh given bytenr
294 struct btrfs_block_group_cache
*btrfs_lookup_block_group(struct
298 struct btrfs_block_group_cache
*cache
;
300 cache
= block_group_cache_tree_search(info
, bytenr
, 1);
305 static struct btrfs_space_info
*__find_space_info(struct btrfs_fs_info
*info
,
308 struct list_head
*head
= &info
->space_info
;
309 struct list_head
*cur
;
310 struct btrfs_space_info
*found
;
311 list_for_each(cur
, head
) {
312 found
= list_entry(cur
, struct btrfs_space_info
, list
);
313 if (found
->flags
== flags
)
319 static u64
div_factor(u64 num
, int factor
)
328 static struct btrfs_block_group_cache
*
329 __btrfs_find_block_group(struct btrfs_root
*root
,
330 struct btrfs_block_group_cache
*hint
,
331 u64 search_start
, int data
, int owner
)
333 struct btrfs_block_group_cache
*cache
;
334 struct btrfs_block_group_cache
*found_group
= NULL
;
335 struct btrfs_fs_info
*info
= root
->fs_info
;
343 if (data
& BTRFS_BLOCK_GROUP_METADATA
)
347 struct btrfs_block_group_cache
*shint
;
348 shint
= btrfs_lookup_first_block_group(info
, search_start
);
349 if (shint
&& block_group_bits(shint
, data
) && !shint
->ro
) {
350 spin_lock(&shint
->lock
);
351 used
= btrfs_block_group_used(&shint
->item
);
352 if (used
+ shint
->pinned
+ shint
->reserved
<
353 div_factor(shint
->key
.offset
, factor
)) {
354 spin_unlock(&shint
->lock
);
357 spin_unlock(&shint
->lock
);
360 if (hint
&& !hint
->ro
&& block_group_bits(hint
, data
)) {
361 spin_lock(&hint
->lock
);
362 used
= btrfs_block_group_used(&hint
->item
);
363 if (used
+ hint
->pinned
+ hint
->reserved
<
364 div_factor(hint
->key
.offset
, factor
)) {
365 spin_unlock(&hint
->lock
);
368 spin_unlock(&hint
->lock
);
369 last
= hint
->key
.objectid
+ hint
->key
.offset
;
372 last
= max(hint
->key
.objectid
, search_start
);
378 cache
= btrfs_lookup_first_block_group(root
->fs_info
, last
);
382 spin_lock(&cache
->lock
);
383 last
= cache
->key
.objectid
+ cache
->key
.offset
;
384 used
= btrfs_block_group_used(&cache
->item
);
386 if (!cache
->ro
&& block_group_bits(cache
, data
)) {
387 free_check
= div_factor(cache
->key
.offset
, factor
);
388 if (used
+ cache
->pinned
+ cache
->reserved
<
391 spin_unlock(&cache
->lock
);
395 spin_unlock(&cache
->lock
);
403 if (!full_search
&& factor
< 10) {
413 struct btrfs_block_group_cache
*btrfs_find_block_group(struct btrfs_root
*root
,
414 struct btrfs_block_group_cache
415 *hint
, u64 search_start
,
419 struct btrfs_block_group_cache
*ret
;
420 ret
= __btrfs_find_block_group(root
, hint
, search_start
, data
, owner
);
424 /* simple helper to search for an existing extent at a given offset */
425 int btrfs_lookup_extent(struct btrfs_root
*root
, u64 start
, u64 len
)
428 struct btrfs_key key
;
429 struct btrfs_path
*path
;
431 path
= btrfs_alloc_path();
433 key
.objectid
= start
;
435 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
436 ret
= btrfs_search_slot(NULL
, root
->fs_info
->extent_root
, &key
, path
,
438 btrfs_free_path(path
);
443 * Back reference rules. Back refs have three main goals:
445 * 1) differentiate between all holders of references to an extent so that
446 * when a reference is dropped we can make sure it was a valid reference
447 * before freeing the extent.
449 * 2) Provide enough information to quickly find the holders of an extent
450 * if we notice a given block is corrupted or bad.
452 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
453 * maintenance. This is actually the same as #2, but with a slightly
454 * different use case.
456 * File extents can be referenced by:
458 * - multiple snapshots, subvolumes, or different generations in one subvol
459 * - different files inside a single subvolume
460 * - different offsets inside a file (bookend extents in file.c)
462 * The extent ref structure has fields for:
464 * - Objectid of the subvolume root
465 * - Generation number of the tree holding the reference
466 * - objectid of the file holding the reference
467 * - number of references holding by parent node (alway 1 for tree blocks)
469 * Btree leaf may hold multiple references to a file extent. In most cases,
470 * these references are from same file and the corresponding offsets inside
471 * the file are close together.
473 * When a file extent is allocated the fields are filled in:
474 * (root_key.objectid, trans->transid, inode objectid, 1)
476 * When a leaf is cow'd new references are added for every file extent found
477 * in the leaf. It looks similar to the create case, but trans->transid will
478 * be different when the block is cow'd.
480 * (root_key.objectid, trans->transid, inode objectid,
481 * number of references in the leaf)
483 * When a file extent is removed either during snapshot deletion or
484 * file truncation, we find the corresponding back reference and check
485 * the following fields:
487 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
490 * Btree extents can be referenced by:
492 * - Different subvolumes
493 * - Different generations of the same subvolume
495 * When a tree block is created, back references are inserted:
497 * (root->root_key.objectid, trans->transid, level, 1)
499 * When a tree block is cow'd, new back references are added for all the
500 * blocks it points to. If the tree block isn't in reference counted root,
501 * the old back references are removed. These new back references are of
502 * the form (trans->transid will have increased since creation):
504 * (root->root_key.objectid, trans->transid, level, 1)
506 * When a backref is in deleting, the following fields are checked:
508 * if backref was for a tree root:
509 * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
511 * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
513 * Back Reference Key composing:
515 * The key objectid corresponds to the first byte in the extent, the key
516 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
517 * byte of parent extent. If a extent is tree root, the key offset is set
518 * to the key objectid.
521 static int noinline
lookup_extent_backref(struct btrfs_trans_handle
*trans
,
522 struct btrfs_root
*root
,
523 struct btrfs_path
*path
,
524 u64 bytenr
, u64 parent
,
525 u64 ref_root
, u64 ref_generation
,
526 u64 owner_objectid
, int del
)
528 struct btrfs_key key
;
529 struct btrfs_extent_ref
*ref
;
530 struct extent_buffer
*leaf
;
534 key
.objectid
= bytenr
;
535 key
.type
= BTRFS_EXTENT_REF_KEY
;
538 ret
= btrfs_search_slot(trans
, root
, &key
, path
, del
? -1 : 0, 1);
546 leaf
= path
->nodes
[0];
547 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_ref
);
548 ref_objectid
= btrfs_ref_objectid(leaf
, ref
);
549 if (btrfs_ref_root(leaf
, ref
) != ref_root
||
550 btrfs_ref_generation(leaf
, ref
) != ref_generation
||
551 (ref_objectid
!= owner_objectid
&&
552 ref_objectid
!= BTRFS_MULTIPLE_OBJECTIDS
)) {
562 static int noinline
insert_extent_backref(struct btrfs_trans_handle
*trans
,
563 struct btrfs_root
*root
,
564 struct btrfs_path
*path
,
565 u64 bytenr
, u64 parent
,
566 u64 ref_root
, u64 ref_generation
,
569 struct btrfs_key key
;
570 struct extent_buffer
*leaf
;
571 struct btrfs_extent_ref
*ref
;
575 key
.objectid
= bytenr
;
576 key
.type
= BTRFS_EXTENT_REF_KEY
;
579 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, sizeof(*ref
));
581 leaf
= path
->nodes
[0];
582 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
583 struct btrfs_extent_ref
);
584 btrfs_set_ref_root(leaf
, ref
, ref_root
);
585 btrfs_set_ref_generation(leaf
, ref
, ref_generation
);
586 btrfs_set_ref_objectid(leaf
, ref
, owner_objectid
);
587 btrfs_set_ref_num_refs(leaf
, ref
, 1);
588 } else if (ret
== -EEXIST
) {
590 BUG_ON(owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
);
591 leaf
= path
->nodes
[0];
592 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
593 struct btrfs_extent_ref
);
594 if (btrfs_ref_root(leaf
, ref
) != ref_root
||
595 btrfs_ref_generation(leaf
, ref
) != ref_generation
) {
601 num_refs
= btrfs_ref_num_refs(leaf
, ref
);
602 BUG_ON(num_refs
== 0);
603 btrfs_set_ref_num_refs(leaf
, ref
, num_refs
+ 1);
605 existing_owner
= btrfs_ref_objectid(leaf
, ref
);
606 if (existing_owner
!= owner_objectid
&&
607 existing_owner
!= BTRFS_MULTIPLE_OBJECTIDS
) {
608 btrfs_set_ref_objectid(leaf
, ref
,
609 BTRFS_MULTIPLE_OBJECTIDS
);
615 btrfs_mark_buffer_dirty(path
->nodes
[0]);
617 btrfs_release_path(root
, path
);
621 static int noinline
remove_extent_backref(struct btrfs_trans_handle
*trans
,
622 struct btrfs_root
*root
,
623 struct btrfs_path
*path
)
625 struct extent_buffer
*leaf
;
626 struct btrfs_extent_ref
*ref
;
630 leaf
= path
->nodes
[0];
631 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_ref
);
632 num_refs
= btrfs_ref_num_refs(leaf
, ref
);
633 BUG_ON(num_refs
== 0);
636 ret
= btrfs_del_item(trans
, root
, path
);
638 btrfs_set_ref_num_refs(leaf
, ref
, num_refs
);
639 btrfs_mark_buffer_dirty(leaf
);
641 btrfs_release_path(root
, path
);
645 static int __btrfs_update_extent_ref(struct btrfs_trans_handle
*trans
,
646 struct btrfs_root
*root
, u64 bytenr
,
647 u64 orig_parent
, u64 parent
,
648 u64 orig_root
, u64 ref_root
,
649 u64 orig_generation
, u64 ref_generation
,
653 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
654 struct btrfs_path
*path
;
656 if (root
== root
->fs_info
->extent_root
) {
657 struct pending_extent_op
*extent_op
;
660 BUG_ON(owner_objectid
>= BTRFS_MAX_LEVEL
);
661 num_bytes
= btrfs_level_size(root
, (int)owner_objectid
);
662 mutex_lock(&root
->fs_info
->extent_ins_mutex
);
663 if (test_range_bit(&root
->fs_info
->extent_ins
, bytenr
,
664 bytenr
+ num_bytes
- 1, EXTENT_WRITEBACK
, 0)) {
666 ret
= get_state_private(&root
->fs_info
->extent_ins
,
669 extent_op
= (struct pending_extent_op
*)
671 BUG_ON(extent_op
->parent
!= orig_parent
);
672 BUG_ON(extent_op
->generation
!= orig_generation
);
674 extent_op
->parent
= parent
;
675 extent_op
->generation
= ref_generation
;
677 extent_op
= kmalloc(sizeof(*extent_op
), GFP_NOFS
);
680 extent_op
->type
= PENDING_BACKREF_UPDATE
;
681 extent_op
->bytenr
= bytenr
;
682 extent_op
->num_bytes
= num_bytes
;
683 extent_op
->parent
= parent
;
684 extent_op
->orig_parent
= orig_parent
;
685 extent_op
->generation
= ref_generation
;
686 extent_op
->orig_generation
= orig_generation
;
687 extent_op
->level
= (int)owner_objectid
;
689 set_extent_bits(&root
->fs_info
->extent_ins
,
690 bytenr
, bytenr
+ num_bytes
- 1,
691 EXTENT_WRITEBACK
, GFP_NOFS
);
692 set_state_private(&root
->fs_info
->extent_ins
,
693 bytenr
, (unsigned long)extent_op
);
695 mutex_unlock(&root
->fs_info
->extent_ins_mutex
);
699 path
= btrfs_alloc_path();
702 ret
= lookup_extent_backref(trans
, extent_root
, path
,
703 bytenr
, orig_parent
, orig_root
,
704 orig_generation
, owner_objectid
, 1);
707 ret
= remove_extent_backref(trans
, extent_root
, path
);
710 ret
= insert_extent_backref(trans
, extent_root
, path
, bytenr
,
711 parent
, ref_root
, ref_generation
,
714 finish_current_insert(trans
, extent_root
, 0);
715 del_pending_extents(trans
, extent_root
, 0);
717 btrfs_free_path(path
);
721 int btrfs_update_extent_ref(struct btrfs_trans_handle
*trans
,
722 struct btrfs_root
*root
, u64 bytenr
,
723 u64 orig_parent
, u64 parent
,
724 u64 ref_root
, u64 ref_generation
,
728 if (ref_root
== BTRFS_TREE_LOG_OBJECTID
&&
729 owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
)
731 ret
= __btrfs_update_extent_ref(trans
, root
, bytenr
, orig_parent
,
732 parent
, ref_root
, ref_root
,
733 ref_generation
, ref_generation
,
738 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
739 struct btrfs_root
*root
, u64 bytenr
,
740 u64 orig_parent
, u64 parent
,
741 u64 orig_root
, u64 ref_root
,
742 u64 orig_generation
, u64 ref_generation
,
745 struct btrfs_path
*path
;
747 struct btrfs_key key
;
748 struct extent_buffer
*l
;
749 struct btrfs_extent_item
*item
;
752 path
= btrfs_alloc_path();
757 key
.objectid
= bytenr
;
758 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
759 key
.offset
= (u64
)-1;
761 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
, path
,
765 BUG_ON(ret
== 0 || path
->slots
[0] == 0);
770 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
771 if (key
.objectid
!= bytenr
) {
772 btrfs_print_leaf(root
->fs_info
->extent_root
, path
->nodes
[0]);
773 printk("wanted %Lu found %Lu\n", bytenr
, key
.objectid
);
776 BUG_ON(key
.type
!= BTRFS_EXTENT_ITEM_KEY
);
778 item
= btrfs_item_ptr(l
, path
->slots
[0], struct btrfs_extent_item
);
779 refs
= btrfs_extent_refs(l
, item
);
780 btrfs_set_extent_refs(l
, item
, refs
+ 1);
781 btrfs_mark_buffer_dirty(path
->nodes
[0]);
783 btrfs_release_path(root
->fs_info
->extent_root
, path
);
786 ret
= insert_extent_backref(trans
, root
->fs_info
->extent_root
,
787 path
, bytenr
, parent
,
788 ref_root
, ref_generation
,
791 finish_current_insert(trans
, root
->fs_info
->extent_root
, 0);
792 del_pending_extents(trans
, root
->fs_info
->extent_root
, 0);
794 btrfs_free_path(path
);
798 int btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
799 struct btrfs_root
*root
,
800 u64 bytenr
, u64 num_bytes
, u64 parent
,
801 u64 ref_root
, u64 ref_generation
,
805 if (ref_root
== BTRFS_TREE_LOG_OBJECTID
&&
806 owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
)
808 ret
= __btrfs_inc_extent_ref(trans
, root
, bytenr
, 0, parent
,
809 0, ref_root
, 0, ref_generation
,
814 int btrfs_extent_post_op(struct btrfs_trans_handle
*trans
,
815 struct btrfs_root
*root
)
817 finish_current_insert(trans
, root
->fs_info
->extent_root
, 1);
818 del_pending_extents(trans
, root
->fs_info
->extent_root
, 1);
822 int btrfs_lookup_extent_ref(struct btrfs_trans_handle
*trans
,
823 struct btrfs_root
*root
, u64 bytenr
,
824 u64 num_bytes
, u32
*refs
)
826 struct btrfs_path
*path
;
828 struct btrfs_key key
;
829 struct extent_buffer
*l
;
830 struct btrfs_extent_item
*item
;
832 WARN_ON(num_bytes
< root
->sectorsize
);
833 path
= btrfs_alloc_path();
835 key
.objectid
= bytenr
;
836 key
.offset
= num_bytes
;
837 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
838 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
, path
,
843 btrfs_print_leaf(root
, path
->nodes
[0]);
844 printk("failed to find block number %Lu\n", bytenr
);
848 item
= btrfs_item_ptr(l
, path
->slots
[0], struct btrfs_extent_item
);
849 *refs
= btrfs_extent_refs(l
, item
);
851 btrfs_free_path(path
);
855 int btrfs_cross_ref_exist(struct btrfs_trans_handle
*trans
,
856 struct btrfs_root
*root
, u64 bytenr
)
858 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
859 struct btrfs_path
*path
;
860 struct extent_buffer
*leaf
;
861 struct btrfs_extent_ref
*ref_item
;
862 struct btrfs_key key
;
863 struct btrfs_key found_key
;
869 key
.objectid
= bytenr
;
870 key
.offset
= (u64
)-1;
871 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
873 path
= btrfs_alloc_path();
874 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
880 if (path
->slots
[0] == 0)
884 leaf
= path
->nodes
[0];
885 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
887 if (found_key
.objectid
!= bytenr
||
888 found_key
.type
!= BTRFS_EXTENT_ITEM_KEY
)
891 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
893 leaf
= path
->nodes
[0];
894 nritems
= btrfs_header_nritems(leaf
);
895 if (path
->slots
[0] >= nritems
) {
896 ret
= btrfs_next_leaf(extent_root
, path
);
903 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
904 if (found_key
.objectid
!= bytenr
)
907 if (found_key
.type
!= BTRFS_EXTENT_REF_KEY
) {
912 ref_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
913 struct btrfs_extent_ref
);
914 ref_root
= btrfs_ref_root(leaf
, ref_item
);
915 if (ref_root
!= root
->root_key
.objectid
&&
916 ref_root
!= BTRFS_TREE_LOG_OBJECTID
) {
920 if (btrfs_ref_generation(leaf
, ref_item
) <= last_snapshot
) {
929 btrfs_free_path(path
);
933 int btrfs_cache_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
934 struct extent_buffer
*buf
, u32 nr_extents
)
936 struct btrfs_key key
;
937 struct btrfs_file_extent_item
*fi
;
948 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
950 root_gen
= root
->root_key
.offset
;
953 root_gen
= trans
->transid
- 1;
956 level
= btrfs_header_level(buf
);
957 nritems
= btrfs_header_nritems(buf
);
960 struct btrfs_leaf_ref
*ref
;
961 struct btrfs_extent_info
*info
;
963 ref
= btrfs_alloc_leaf_ref(root
, nr_extents
);
969 ref
->root_gen
= root_gen
;
970 ref
->bytenr
= buf
->start
;
971 ref
->owner
= btrfs_header_owner(buf
);
972 ref
->generation
= btrfs_header_generation(buf
);
973 ref
->nritems
= nr_extents
;
976 for (i
= 0; nr_extents
> 0 && i
< nritems
; i
++) {
978 btrfs_item_key_to_cpu(buf
, &key
, i
);
979 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
981 fi
= btrfs_item_ptr(buf
, i
,
982 struct btrfs_file_extent_item
);
983 if (btrfs_file_extent_type(buf
, fi
) ==
984 BTRFS_FILE_EXTENT_INLINE
)
986 disk_bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
987 if (disk_bytenr
== 0)
990 info
->bytenr
= disk_bytenr
;
992 btrfs_file_extent_disk_num_bytes(buf
, fi
);
993 info
->objectid
= key
.objectid
;
994 info
->offset
= key
.offset
;
998 ret
= btrfs_add_leaf_ref(root
, ref
, shared
);
999 if (ret
== -EEXIST
&& shared
) {
1000 struct btrfs_leaf_ref
*old
;
1001 old
= btrfs_lookup_leaf_ref(root
, ref
->bytenr
);
1003 btrfs_remove_leaf_ref(root
, old
);
1004 btrfs_free_leaf_ref(root
, old
);
1005 ret
= btrfs_add_leaf_ref(root
, ref
, shared
);
1008 btrfs_free_leaf_ref(root
, ref
);
1014 int btrfs_inc_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
1015 struct extent_buffer
*orig_buf
, struct extent_buffer
*buf
,
1022 u64 orig_generation
;
1024 u32 nr_file_extents
= 0;
1025 struct btrfs_key key
;
1026 struct btrfs_file_extent_item
*fi
;
1031 int (*process_func
)(struct btrfs_trans_handle
*, struct btrfs_root
*,
1032 u64
, u64
, u64
, u64
, u64
, u64
, u64
, u64
);
1034 ref_root
= btrfs_header_owner(buf
);
1035 ref_generation
= btrfs_header_generation(buf
);
1036 orig_root
= btrfs_header_owner(orig_buf
);
1037 orig_generation
= btrfs_header_generation(orig_buf
);
1039 nritems
= btrfs_header_nritems(buf
);
1040 level
= btrfs_header_level(buf
);
1042 if (root
->ref_cows
) {
1043 process_func
= __btrfs_inc_extent_ref
;
1046 root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
)
1049 root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
)
1051 process_func
= __btrfs_update_extent_ref
;
1054 for (i
= 0; i
< nritems
; i
++) {
1057 btrfs_item_key_to_cpu(buf
, &key
, i
);
1058 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
1060 fi
= btrfs_item_ptr(buf
, i
,
1061 struct btrfs_file_extent_item
);
1062 if (btrfs_file_extent_type(buf
, fi
) ==
1063 BTRFS_FILE_EXTENT_INLINE
)
1065 bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
1071 ret
= process_func(trans
, root
, bytenr
,
1072 orig_buf
->start
, buf
->start
,
1073 orig_root
, ref_root
,
1074 orig_generation
, ref_generation
,
1083 bytenr
= btrfs_node_blockptr(buf
, i
);
1084 ret
= process_func(trans
, root
, bytenr
,
1085 orig_buf
->start
, buf
->start
,
1086 orig_root
, ref_root
,
1087 orig_generation
, ref_generation
,
1099 *nr_extents
= nr_file_extents
;
1101 *nr_extents
= nritems
;
1109 int btrfs_update_ref(struct btrfs_trans_handle
*trans
,
1110 struct btrfs_root
*root
, struct extent_buffer
*orig_buf
,
1111 struct extent_buffer
*buf
, int start_slot
, int nr
)
1118 u64 orig_generation
;
1119 struct btrfs_key key
;
1120 struct btrfs_file_extent_item
*fi
;
1126 BUG_ON(start_slot
< 0);
1127 BUG_ON(start_slot
+ nr
> btrfs_header_nritems(buf
));
1129 ref_root
= btrfs_header_owner(buf
);
1130 ref_generation
= btrfs_header_generation(buf
);
1131 orig_root
= btrfs_header_owner(orig_buf
);
1132 orig_generation
= btrfs_header_generation(orig_buf
);
1133 level
= btrfs_header_level(buf
);
1135 if (!root
->ref_cows
) {
1137 root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
)
1140 root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
)
1144 for (i
= 0, slot
= start_slot
; i
< nr
; i
++, slot
++) {
1147 btrfs_item_key_to_cpu(buf
, &key
, slot
);
1148 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
1150 fi
= btrfs_item_ptr(buf
, slot
,
1151 struct btrfs_file_extent_item
);
1152 if (btrfs_file_extent_type(buf
, fi
) ==
1153 BTRFS_FILE_EXTENT_INLINE
)
1155 bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
1158 ret
= __btrfs_update_extent_ref(trans
, root
, bytenr
,
1159 orig_buf
->start
, buf
->start
,
1160 orig_root
, ref_root
,
1161 orig_generation
, ref_generation
,
1166 bytenr
= btrfs_node_blockptr(buf
, slot
);
1167 ret
= __btrfs_update_extent_ref(trans
, root
, bytenr
,
1168 orig_buf
->start
, buf
->start
,
1169 orig_root
, ref_root
,
1170 orig_generation
, ref_generation
,
1182 static int write_one_cache_group(struct btrfs_trans_handle
*trans
,
1183 struct btrfs_root
*root
,
1184 struct btrfs_path
*path
,
1185 struct btrfs_block_group_cache
*cache
)
1189 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
1191 struct extent_buffer
*leaf
;
1193 ret
= btrfs_search_slot(trans
, extent_root
, &cache
->key
, path
, 0, 1);
1198 leaf
= path
->nodes
[0];
1199 bi
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
1200 write_extent_buffer(leaf
, &cache
->item
, bi
, sizeof(cache
->item
));
1201 btrfs_mark_buffer_dirty(leaf
);
1202 btrfs_release_path(extent_root
, path
);
1204 finish_current_insert(trans
, extent_root
, 0);
1205 pending_ret
= del_pending_extents(trans
, extent_root
, 0);
1214 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle
*trans
,
1215 struct btrfs_root
*root
)
1217 struct btrfs_block_group_cache
*cache
, *entry
;
1221 struct btrfs_path
*path
;
1224 path
= btrfs_alloc_path();
1230 spin_lock(&root
->fs_info
->block_group_cache_lock
);
1231 for (n
= rb_first(&root
->fs_info
->block_group_cache_tree
);
1232 n
; n
= rb_next(n
)) {
1233 entry
= rb_entry(n
, struct btrfs_block_group_cache
,
1240 spin_unlock(&root
->fs_info
->block_group_cache_lock
);
1246 last
+= cache
->key
.offset
;
1248 err
= write_one_cache_group(trans
, root
,
1251 * if we fail to write the cache group, we want
1252 * to keep it marked dirty in hopes that a later
1260 btrfs_free_path(path
);
1264 static int update_space_info(struct btrfs_fs_info
*info
, u64 flags
,
1265 u64 total_bytes
, u64 bytes_used
,
1266 struct btrfs_space_info
**space_info
)
1268 struct btrfs_space_info
*found
;
1270 found
= __find_space_info(info
, flags
);
1272 spin_lock(&found
->lock
);
1273 found
->total_bytes
+= total_bytes
;
1274 found
->bytes_used
+= bytes_used
;
1276 spin_unlock(&found
->lock
);
1277 *space_info
= found
;
1280 found
= kmalloc(sizeof(*found
), GFP_NOFS
);
1284 list_add(&found
->list
, &info
->space_info
);
1285 INIT_LIST_HEAD(&found
->block_groups
);
1286 init_rwsem(&found
->groups_sem
);
1287 spin_lock_init(&found
->lock
);
1288 found
->flags
= flags
;
1289 found
->total_bytes
= total_bytes
;
1290 found
->bytes_used
= bytes_used
;
1291 found
->bytes_pinned
= 0;
1292 found
->bytes_reserved
= 0;
1294 found
->force_alloc
= 0;
1295 *space_info
= found
;
1299 static void set_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
1301 u64 extra_flags
= flags
& (BTRFS_BLOCK_GROUP_RAID0
|
1302 BTRFS_BLOCK_GROUP_RAID1
|
1303 BTRFS_BLOCK_GROUP_RAID10
|
1304 BTRFS_BLOCK_GROUP_DUP
);
1306 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
1307 fs_info
->avail_data_alloc_bits
|= extra_flags
;
1308 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
1309 fs_info
->avail_metadata_alloc_bits
|= extra_flags
;
1310 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
1311 fs_info
->avail_system_alloc_bits
|= extra_flags
;
1315 static u64
reduce_alloc_profile(struct btrfs_root
*root
, u64 flags
)
1317 u64 num_devices
= root
->fs_info
->fs_devices
->num_devices
;
1319 if (num_devices
== 1)
1320 flags
&= ~(BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID0
);
1321 if (num_devices
< 4)
1322 flags
&= ~BTRFS_BLOCK_GROUP_RAID10
;
1324 if ((flags
& BTRFS_BLOCK_GROUP_DUP
) &&
1325 (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
1326 BTRFS_BLOCK_GROUP_RAID10
))) {
1327 flags
&= ~BTRFS_BLOCK_GROUP_DUP
;
1330 if ((flags
& BTRFS_BLOCK_GROUP_RAID1
) &&
1331 (flags
& BTRFS_BLOCK_GROUP_RAID10
)) {
1332 flags
&= ~BTRFS_BLOCK_GROUP_RAID1
;
1335 if ((flags
& BTRFS_BLOCK_GROUP_RAID0
) &&
1336 ((flags
& BTRFS_BLOCK_GROUP_RAID1
) |
1337 (flags
& BTRFS_BLOCK_GROUP_RAID10
) |
1338 (flags
& BTRFS_BLOCK_GROUP_DUP
)))
1339 flags
&= ~BTRFS_BLOCK_GROUP_RAID0
;
1343 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
1344 struct btrfs_root
*extent_root
, u64 alloc_bytes
,
1345 u64 flags
, int force
)
1347 struct btrfs_space_info
*space_info
;
1351 int ret
= 0, waited
= 0;
1353 flags
= reduce_alloc_profile(extent_root
, flags
);
1355 space_info
= __find_space_info(extent_root
->fs_info
, flags
);
1357 ret
= update_space_info(extent_root
->fs_info
, flags
,
1361 BUG_ON(!space_info
);
1363 spin_lock(&space_info
->lock
);
1364 if (space_info
->force_alloc
) {
1366 space_info
->force_alloc
= 0;
1368 if (space_info
->full
) {
1369 spin_unlock(&space_info
->lock
);
1373 thresh
= div_factor(space_info
->total_bytes
, 6);
1375 (space_info
->bytes_used
+ space_info
->bytes_pinned
+
1376 space_info
->bytes_reserved
+ alloc_bytes
) < thresh
) {
1377 spin_unlock(&space_info
->lock
);
1381 spin_unlock(&space_info
->lock
);
1383 ret
= mutex_trylock(&extent_root
->fs_info
->chunk_mutex
);
1384 if (!ret
&& !force
) {
1387 mutex_lock(&extent_root
->fs_info
->chunk_mutex
);
1392 spin_lock(&space_info
->lock
);
1393 if (space_info
->full
) {
1394 spin_unlock(&space_info
->lock
);
1397 spin_unlock(&space_info
->lock
);
1400 ret
= btrfs_alloc_chunk(trans
, extent_root
, &start
, &num_bytes
, flags
);
1402 printk("space info full %Lu\n", flags
);
1403 space_info
->full
= 1;
1407 ret
= btrfs_make_block_group(trans
, extent_root
, 0, flags
,
1408 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, start
, num_bytes
);
1411 mutex_unlock(&extent_root
->fs_info
->chunk_mutex
);
1416 static int update_block_group(struct btrfs_trans_handle
*trans
,
1417 struct btrfs_root
*root
,
1418 u64 bytenr
, u64 num_bytes
, int alloc
,
1421 struct btrfs_block_group_cache
*cache
;
1422 struct btrfs_fs_info
*info
= root
->fs_info
;
1423 u64 total
= num_bytes
;
1428 cache
= btrfs_lookup_block_group(info
, bytenr
);
1432 byte_in_group
= bytenr
- cache
->key
.objectid
;
1433 WARN_ON(byte_in_group
> cache
->key
.offset
);
1435 spin_lock(&cache
->space_info
->lock
);
1436 spin_lock(&cache
->lock
);
1438 old_val
= btrfs_block_group_used(&cache
->item
);
1439 num_bytes
= min(total
, cache
->key
.offset
- byte_in_group
);
1441 old_val
+= num_bytes
;
1442 cache
->space_info
->bytes_used
+= num_bytes
;
1443 btrfs_set_block_group_used(&cache
->item
, old_val
);
1444 spin_unlock(&cache
->lock
);
1445 spin_unlock(&cache
->space_info
->lock
);
1447 old_val
-= num_bytes
;
1448 cache
->space_info
->bytes_used
-= num_bytes
;
1449 btrfs_set_block_group_used(&cache
->item
, old_val
);
1450 spin_unlock(&cache
->lock
);
1451 spin_unlock(&cache
->space_info
->lock
);
1454 ret
= btrfs_add_free_space(cache
, bytenr
,
1461 bytenr
+= num_bytes
;
1466 static u64
first_logical_byte(struct btrfs_root
*root
, u64 search_start
)
1468 struct btrfs_block_group_cache
*cache
;
1470 cache
= btrfs_lookup_first_block_group(root
->fs_info
, search_start
);
1474 return cache
->key
.objectid
;
1477 int btrfs_update_pinned_extents(struct btrfs_root
*root
,
1478 u64 bytenr
, u64 num
, int pin
)
1481 struct btrfs_block_group_cache
*cache
;
1482 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1484 WARN_ON(!mutex_is_locked(&root
->fs_info
->pinned_mutex
));
1486 set_extent_dirty(&fs_info
->pinned_extents
,
1487 bytenr
, bytenr
+ num
- 1, GFP_NOFS
);
1489 clear_extent_dirty(&fs_info
->pinned_extents
,
1490 bytenr
, bytenr
+ num
- 1, GFP_NOFS
);
1493 cache
= btrfs_lookup_block_group(fs_info
, bytenr
);
1495 len
= min(num
, cache
->key
.offset
-
1496 (bytenr
- cache
->key
.objectid
));
1498 spin_lock(&cache
->space_info
->lock
);
1499 spin_lock(&cache
->lock
);
1500 cache
->pinned
+= len
;
1501 cache
->space_info
->bytes_pinned
+= len
;
1502 spin_unlock(&cache
->lock
);
1503 spin_unlock(&cache
->space_info
->lock
);
1504 fs_info
->total_pinned
+= len
;
1506 spin_lock(&cache
->space_info
->lock
);
1507 spin_lock(&cache
->lock
);
1508 cache
->pinned
-= len
;
1509 cache
->space_info
->bytes_pinned
-= len
;
1510 spin_unlock(&cache
->lock
);
1511 spin_unlock(&cache
->space_info
->lock
);
1512 fs_info
->total_pinned
-= len
;
1520 static int update_reserved_extents(struct btrfs_root
*root
,
1521 u64 bytenr
, u64 num
, int reserve
)
1524 struct btrfs_block_group_cache
*cache
;
1525 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1528 cache
= btrfs_lookup_block_group(fs_info
, bytenr
);
1530 len
= min(num
, cache
->key
.offset
-
1531 (bytenr
- cache
->key
.objectid
));
1533 spin_lock(&cache
->space_info
->lock
);
1534 spin_lock(&cache
->lock
);
1536 cache
->reserved
+= len
;
1537 cache
->space_info
->bytes_reserved
+= len
;
1539 cache
->reserved
-= len
;
1540 cache
->space_info
->bytes_reserved
-= len
;
1542 spin_unlock(&cache
->lock
);
1543 spin_unlock(&cache
->space_info
->lock
);
1550 int btrfs_copy_pinned(struct btrfs_root
*root
, struct extent_io_tree
*copy
)
1555 struct extent_io_tree
*pinned_extents
= &root
->fs_info
->pinned_extents
;
1558 mutex_lock(&root
->fs_info
->pinned_mutex
);
1560 ret
= find_first_extent_bit(pinned_extents
, last
,
1561 &start
, &end
, EXTENT_DIRTY
);
1564 set_extent_dirty(copy
, start
, end
, GFP_NOFS
);
1567 mutex_unlock(&root
->fs_info
->pinned_mutex
);
1571 int btrfs_finish_extent_commit(struct btrfs_trans_handle
*trans
,
1572 struct btrfs_root
*root
,
1573 struct extent_io_tree
*unpin
)
1578 struct btrfs_block_group_cache
*cache
;
1580 mutex_lock(&root
->fs_info
->pinned_mutex
);
1582 ret
= find_first_extent_bit(unpin
, 0, &start
, &end
,
1586 btrfs_update_pinned_extents(root
, start
, end
+ 1 - start
, 0);
1587 clear_extent_dirty(unpin
, start
, end
, GFP_NOFS
);
1588 cache
= btrfs_lookup_block_group(root
->fs_info
, start
);
1590 btrfs_add_free_space(cache
, start
, end
- start
+ 1);
1591 if (need_resched()) {
1592 mutex_unlock(&root
->fs_info
->pinned_mutex
);
1594 mutex_lock(&root
->fs_info
->pinned_mutex
);
1597 mutex_unlock(&root
->fs_info
->pinned_mutex
);
1601 static int finish_current_insert(struct btrfs_trans_handle
*trans
,
1602 struct btrfs_root
*extent_root
, int all
)
1608 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
1609 struct btrfs_path
*path
;
1610 struct btrfs_extent_ref
*ref
;
1611 struct pending_extent_op
*extent_op
;
1612 struct btrfs_key key
;
1613 struct btrfs_extent_item extent_item
;
1617 btrfs_set_stack_extent_refs(&extent_item
, 1);
1618 path
= btrfs_alloc_path();
1621 mutex_lock(&info
->extent_ins_mutex
);
1622 ret
= find_first_extent_bit(&info
->extent_ins
, search
, &start
,
1623 &end
, EXTENT_WRITEBACK
);
1625 mutex_unlock(&info
->extent_ins_mutex
);
1626 if (search
&& all
) {
1633 ret
= try_lock_extent(&info
->extent_ins
, start
, end
, GFP_NOFS
);
1636 mutex_unlock(&info
->extent_ins_mutex
);
1642 ret
= get_state_private(&info
->extent_ins
, start
, &priv
);
1644 extent_op
= (struct pending_extent_op
*)(unsigned long)priv
;
1646 mutex_unlock(&info
->extent_ins_mutex
);
1648 if (extent_op
->type
== PENDING_EXTENT_INSERT
) {
1649 key
.objectid
= start
;
1650 key
.offset
= end
+ 1 - start
;
1651 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1652 err
= btrfs_insert_item(trans
, extent_root
, &key
,
1653 &extent_item
, sizeof(extent_item
));
1656 mutex_lock(&info
->extent_ins_mutex
);
1657 clear_extent_bits(&info
->extent_ins
, start
, end
,
1658 EXTENT_WRITEBACK
, GFP_NOFS
);
1659 mutex_unlock(&info
->extent_ins_mutex
);
1661 err
= insert_extent_backref(trans
, extent_root
, path
,
1662 start
, extent_op
->parent
,
1663 extent_root
->root_key
.objectid
,
1664 extent_op
->generation
,
1667 } else if (extent_op
->type
== PENDING_BACKREF_UPDATE
) {
1668 err
= lookup_extent_backref(trans
, extent_root
, path
,
1669 start
, extent_op
->orig_parent
,
1670 extent_root
->root_key
.objectid
,
1671 extent_op
->orig_generation
,
1672 extent_op
->level
, 0);
1675 mutex_lock(&info
->extent_ins_mutex
);
1676 clear_extent_bits(&info
->extent_ins
, start
, end
,
1677 EXTENT_WRITEBACK
, GFP_NOFS
);
1678 mutex_unlock(&info
->extent_ins_mutex
);
1680 key
.objectid
= start
;
1681 key
.offset
= extent_op
->parent
;
1682 key
.type
= BTRFS_EXTENT_REF_KEY
;
1683 err
= btrfs_set_item_key_safe(trans
, extent_root
, path
,
1686 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1687 struct btrfs_extent_ref
);
1688 btrfs_set_ref_generation(path
->nodes
[0], ref
,
1689 extent_op
->generation
);
1690 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1691 btrfs_release_path(extent_root
, path
);
1696 unlock_extent(&info
->extent_ins
, start
, end
, GFP_NOFS
);
1704 btrfs_free_path(path
);
1708 static int pin_down_bytes(struct btrfs_trans_handle
*trans
,
1709 struct btrfs_root
*root
,
1710 u64 bytenr
, u64 num_bytes
, int is_data
)
1713 struct extent_buffer
*buf
;
1718 buf
= btrfs_find_tree_block(root
, bytenr
, num_bytes
);
1722 /* we can reuse a block if it hasn't been written
1723 * and it is from this transaction. We can't
1724 * reuse anything from the tree log root because
1725 * it has tiny sub-transactions.
1727 if (btrfs_buffer_uptodate(buf
, 0) &&
1728 btrfs_try_tree_lock(buf
)) {
1729 u64 header_owner
= btrfs_header_owner(buf
);
1730 u64 header_transid
= btrfs_header_generation(buf
);
1731 if (header_owner
!= BTRFS_TREE_LOG_OBJECTID
&&
1732 header_owner
!= BTRFS_TREE_RELOC_OBJECTID
&&
1733 header_transid
== trans
->transid
&&
1734 !btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
)) {
1735 clean_tree_block(NULL
, root
, buf
);
1736 btrfs_tree_unlock(buf
);
1737 free_extent_buffer(buf
);
1740 btrfs_tree_unlock(buf
);
1742 free_extent_buffer(buf
);
1744 btrfs_update_pinned_extents(root
, bytenr
, num_bytes
, 1);
1751 * remove an extent from the root, returns 0 on success
1753 static int __free_extent(struct btrfs_trans_handle
*trans
,
1754 struct btrfs_root
*root
,
1755 u64 bytenr
, u64 num_bytes
, u64 parent
,
1756 u64 root_objectid
, u64 ref_generation
,
1757 u64 owner_objectid
, int pin
, int mark_free
)
1759 struct btrfs_path
*path
;
1760 struct btrfs_key key
;
1761 struct btrfs_fs_info
*info
= root
->fs_info
;
1762 struct btrfs_root
*extent_root
= info
->extent_root
;
1763 struct extent_buffer
*leaf
;
1765 int extent_slot
= 0;
1766 int found_extent
= 0;
1768 struct btrfs_extent_item
*ei
;
1771 key
.objectid
= bytenr
;
1772 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
1773 key
.offset
= num_bytes
;
1774 path
= btrfs_alloc_path();
1779 ret
= lookup_extent_backref(trans
, extent_root
, path
,
1780 bytenr
, parent
, root_objectid
,
1781 ref_generation
, owner_objectid
, 1);
1783 struct btrfs_key found_key
;
1784 extent_slot
= path
->slots
[0];
1785 while(extent_slot
> 0) {
1787 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1789 if (found_key
.objectid
!= bytenr
)
1791 if (found_key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
1792 found_key
.offset
== num_bytes
) {
1796 if (path
->slots
[0] - extent_slot
> 5)
1799 if (!found_extent
) {
1800 ret
= remove_extent_backref(trans
, extent_root
, path
);
1802 btrfs_release_path(extent_root
, path
);
1803 ret
= btrfs_search_slot(trans
, extent_root
,
1806 extent_slot
= path
->slots
[0];
1809 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
1811 printk("Unable to find ref byte nr %Lu root %Lu "
1812 "gen %Lu owner %Lu\n", bytenr
,
1813 root_objectid
, ref_generation
, owner_objectid
);
1816 leaf
= path
->nodes
[0];
1817 ei
= btrfs_item_ptr(leaf
, extent_slot
,
1818 struct btrfs_extent_item
);
1819 refs
= btrfs_extent_refs(leaf
, ei
);
1822 btrfs_set_extent_refs(leaf
, ei
, refs
);
1824 btrfs_mark_buffer_dirty(leaf
);
1826 if (refs
== 0 && found_extent
&& path
->slots
[0] == extent_slot
+ 1) {
1827 struct btrfs_extent_ref
*ref
;
1828 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
1829 struct btrfs_extent_ref
);
1830 BUG_ON(btrfs_ref_num_refs(leaf
, ref
) != 1);
1831 /* if the back ref and the extent are next to each other
1832 * they get deleted below in one shot
1834 path
->slots
[0] = extent_slot
;
1836 } else if (found_extent
) {
1837 /* otherwise delete the extent back ref */
1838 ret
= remove_extent_backref(trans
, extent_root
, path
);
1840 /* if refs are 0, we need to setup the path for deletion */
1842 btrfs_release_path(extent_root
, path
);
1843 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
,
1852 #ifdef BIO_RW_DISCARD
1853 u64 map_length
= num_bytes
;
1854 struct btrfs_multi_bio
*multi
= NULL
;
1858 mutex_lock(&root
->fs_info
->pinned_mutex
);
1859 ret
= pin_down_bytes(trans
, root
, bytenr
, num_bytes
,
1860 owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
);
1861 mutex_unlock(&root
->fs_info
->pinned_mutex
);
1867 /* block accounting for super block */
1868 spin_lock_irq(&info
->delalloc_lock
);
1869 super_used
= btrfs_super_bytes_used(&info
->super_copy
);
1870 btrfs_set_super_bytes_used(&info
->super_copy
,
1871 super_used
- num_bytes
);
1872 spin_unlock_irq(&info
->delalloc_lock
);
1874 /* block accounting for root item */
1875 root_used
= btrfs_root_used(&root
->root_item
);
1876 btrfs_set_root_used(&root
->root_item
,
1877 root_used
- num_bytes
);
1878 ret
= btrfs_del_items(trans
, extent_root
, path
, path
->slots
[0],
1881 btrfs_release_path(extent_root
, path
);
1882 ret
= update_block_group(trans
, root
, bytenr
, num_bytes
, 0,
1886 #ifdef BIO_RW_DISCARD
1887 /* Tell the block device(s) that the sectors can be discarded */
1888 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
1889 bytenr
, &map_length
, &multi
, 0);
1891 struct btrfs_bio_stripe
*stripe
= multi
->stripes
;
1894 if (map_length
> num_bytes
)
1895 map_length
= num_bytes
;
1897 for (i
= 0; i
< multi
->num_stripes
; i
++, stripe
++) {
1898 blkdev_issue_discard(stripe
->dev
->bdev
,
1899 stripe
->physical
>> 9,
1906 btrfs_free_path(path
);
1907 finish_current_insert(trans
, extent_root
, 0);
1912 * find all the blocks marked as pending in the radix tree and remove
1913 * them from the extent map
1915 static int del_pending_extents(struct btrfs_trans_handle
*trans
, struct
1916 btrfs_root
*extent_root
, int all
)
1924 struct extent_io_tree
*pending_del
;
1925 struct extent_io_tree
*extent_ins
;
1926 struct pending_extent_op
*extent_op
;
1927 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
1929 extent_ins
= &extent_root
->fs_info
->extent_ins
;
1930 pending_del
= &extent_root
->fs_info
->pending_del
;
1933 mutex_lock(&info
->extent_ins_mutex
);
1934 ret
= find_first_extent_bit(pending_del
, search
, &start
, &end
,
1937 mutex_unlock(&info
->extent_ins_mutex
);
1938 if (all
&& search
) {
1945 ret
= try_lock_extent(extent_ins
, start
, end
, GFP_NOFS
);
1948 mutex_unlock(&info
->extent_ins_mutex
);
1954 ret
= get_state_private(pending_del
, start
, &priv
);
1956 extent_op
= (struct pending_extent_op
*)(unsigned long)priv
;
1958 clear_extent_bits(pending_del
, start
, end
, EXTENT_WRITEBACK
,
1960 if (!test_range_bit(extent_ins
, start
, end
,
1961 EXTENT_WRITEBACK
, 0)) {
1962 mutex_unlock(&info
->extent_ins_mutex
);
1964 ret
= __free_extent(trans
, extent_root
,
1965 start
, end
+ 1 - start
,
1966 extent_op
->orig_parent
,
1967 extent_root
->root_key
.objectid
,
1968 extent_op
->orig_generation
,
1969 extent_op
->level
, 1, 0);
1974 ret
= get_state_private(&info
->extent_ins
, start
,
1977 extent_op
= (struct pending_extent_op
*)
1978 (unsigned long)priv
;
1980 clear_extent_bits(&info
->extent_ins
, start
, end
,
1981 EXTENT_WRITEBACK
, GFP_NOFS
);
1983 mutex_unlock(&info
->extent_ins_mutex
);
1985 if (extent_op
->type
== PENDING_BACKREF_UPDATE
)
1988 mutex_lock(&extent_root
->fs_info
->pinned_mutex
);
1989 ret
= pin_down_bytes(trans
, extent_root
, start
,
1990 end
+ 1 - start
, 0);
1991 mutex_unlock(&extent_root
->fs_info
->pinned_mutex
);
1993 ret
= update_block_group(trans
, extent_root
, start
,
1994 end
+ 1 - start
, 0, ret
> 0);
2001 unlock_extent(extent_ins
, start
, end
, GFP_NOFS
);
2013 * remove an extent from the root, returns 0 on success
2015 static int __btrfs_free_extent(struct btrfs_trans_handle
*trans
,
2016 struct btrfs_root
*root
,
2017 u64 bytenr
, u64 num_bytes
, u64 parent
,
2018 u64 root_objectid
, u64 ref_generation
,
2019 u64 owner_objectid
, int pin
)
2021 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
2025 WARN_ON(num_bytes
< root
->sectorsize
);
2026 if (root
== extent_root
) {
2027 struct pending_extent_op
*extent_op
;
2029 extent_op
= kmalloc(sizeof(*extent_op
), GFP_NOFS
);
2032 extent_op
->type
= PENDING_EXTENT_DELETE
;
2033 extent_op
->bytenr
= bytenr
;
2034 extent_op
->num_bytes
= num_bytes
;
2035 extent_op
->parent
= parent
;
2036 extent_op
->orig_parent
= parent
;
2037 extent_op
->generation
= ref_generation
;
2038 extent_op
->orig_generation
= ref_generation
;
2039 extent_op
->level
= (int)owner_objectid
;
2041 mutex_lock(&root
->fs_info
->extent_ins_mutex
);
2042 set_extent_bits(&root
->fs_info
->pending_del
,
2043 bytenr
, bytenr
+ num_bytes
- 1,
2044 EXTENT_WRITEBACK
, GFP_NOFS
);
2045 set_state_private(&root
->fs_info
->pending_del
,
2046 bytenr
, (unsigned long)extent_op
);
2047 mutex_unlock(&root
->fs_info
->extent_ins_mutex
);
2050 /* if metadata always pin */
2051 if (owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
) {
2052 if (root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
) {
2053 struct btrfs_block_group_cache
*cache
;
2055 /* btrfs_free_reserved_extent */
2056 cache
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
2058 btrfs_add_free_space(cache
, bytenr
, num_bytes
);
2059 update_reserved_extents(root
, bytenr
, num_bytes
, 0);
2065 /* if data pin when any transaction has committed this */
2066 if (ref_generation
!= trans
->transid
)
2069 ret
= __free_extent(trans
, root
, bytenr
, num_bytes
, parent
,
2070 root_objectid
, ref_generation
,
2071 owner_objectid
, pin
, pin
== 0);
2073 finish_current_insert(trans
, root
->fs_info
->extent_root
, 0);
2074 pending_ret
= del_pending_extents(trans
, root
->fs_info
->extent_root
, 0);
2075 return ret
? ret
: pending_ret
;
2078 int btrfs_free_extent(struct btrfs_trans_handle
*trans
,
2079 struct btrfs_root
*root
,
2080 u64 bytenr
, u64 num_bytes
, u64 parent
,
2081 u64 root_objectid
, u64 ref_generation
,
2082 u64 owner_objectid
, int pin
)
2086 ret
= __btrfs_free_extent(trans
, root
, bytenr
, num_bytes
, parent
,
2087 root_objectid
, ref_generation
,
2088 owner_objectid
, pin
);
2092 static u64
stripe_align(struct btrfs_root
*root
, u64 val
)
2094 u64 mask
= ((u64
)root
->stripesize
- 1);
2095 u64 ret
= (val
+ mask
) & ~mask
;
2100 * walks the btree of allocated extents and find a hole of a given size.
2101 * The key ins is changed to record the hole:
2102 * ins->objectid == block start
2103 * ins->flags = BTRFS_EXTENT_ITEM_KEY
2104 * ins->offset == number of blocks
2105 * Any available blocks before search_start are skipped.
2107 static int noinline
find_free_extent(struct btrfs_trans_handle
*trans
,
2108 struct btrfs_root
*orig_root
,
2109 u64 num_bytes
, u64 empty_size
,
2110 u64 search_start
, u64 search_end
,
2111 u64 hint_byte
, struct btrfs_key
*ins
,
2112 u64 exclude_start
, u64 exclude_nr
,
2116 struct btrfs_root
* root
= orig_root
->fs_info
->extent_root
;
2117 u64 total_needed
= num_bytes
;
2118 u64
*last_ptr
= NULL
;
2119 u64 last_wanted
= 0;
2120 struct btrfs_block_group_cache
*block_group
= NULL
;
2121 int chunk_alloc_done
= 0;
2122 int empty_cluster
= 2 * 1024 * 1024;
2123 int allowed_chunk_alloc
= 0;
2124 struct list_head
*head
= NULL
, *cur
= NULL
;
2126 struct btrfs_space_info
*space_info
;
2128 WARN_ON(num_bytes
< root
->sectorsize
);
2129 btrfs_set_key_type(ins
, BTRFS_EXTENT_ITEM_KEY
);
2133 if (orig_root
->ref_cows
|| empty_size
)
2134 allowed_chunk_alloc
= 1;
2136 if (data
& BTRFS_BLOCK_GROUP_METADATA
) {
2137 last_ptr
= &root
->fs_info
->last_alloc
;
2138 empty_cluster
= 64 * 1024;
2141 if ((data
& BTRFS_BLOCK_GROUP_DATA
) && btrfs_test_opt(root
, SSD
))
2142 last_ptr
= &root
->fs_info
->last_data_alloc
;
2146 hint_byte
= *last_ptr
;
2147 last_wanted
= *last_ptr
;
2149 empty_size
+= empty_cluster
;
2153 search_start
= max(search_start
, first_logical_byte(root
, 0));
2154 search_start
= max(search_start
, hint_byte
);
2156 if (last_wanted
&& search_start
!= last_wanted
) {
2158 empty_size
+= empty_cluster
;
2161 total_needed
+= empty_size
;
2162 block_group
= btrfs_lookup_block_group(root
->fs_info
, search_start
);
2164 block_group
= btrfs_lookup_first_block_group(root
->fs_info
,
2166 space_info
= __find_space_info(root
->fs_info
, data
);
2168 down_read(&space_info
->groups_sem
);
2170 struct btrfs_free_space
*free_space
;
2172 * the only way this happens if our hint points to a block
2173 * group thats not of the proper type, while looping this
2174 * should never happen
2177 goto new_group_no_lock
;
2179 mutex_lock(&block_group
->alloc_mutex
);
2180 if (unlikely(!block_group_bits(block_group
, data
)))
2183 ret
= cache_block_group(root
, block_group
);
2185 mutex_unlock(&block_group
->alloc_mutex
);
2189 if (block_group
->ro
)
2192 free_space
= btrfs_find_free_space(block_group
, search_start
,
2195 u64 start
= block_group
->key
.objectid
;
2196 u64 end
= block_group
->key
.objectid
+
2197 block_group
->key
.offset
;
2199 search_start
= stripe_align(root
, free_space
->offset
);
2201 /* move on to the next group */
2202 if (search_start
+ num_bytes
>= search_end
)
2205 /* move on to the next group */
2206 if (search_start
+ num_bytes
> end
)
2209 if (last_wanted
&& search_start
!= last_wanted
) {
2210 total_needed
+= empty_cluster
;
2213 * if search_start is still in this block group
2214 * then we just re-search this block group
2216 if (search_start
>= start
&&
2217 search_start
< end
) {
2218 mutex_unlock(&block_group
->alloc_mutex
);
2222 /* else we go to the next block group */
2226 if (exclude_nr
> 0 &&
2227 (search_start
+ num_bytes
> exclude_start
&&
2228 search_start
< exclude_start
+ exclude_nr
)) {
2229 search_start
= exclude_start
+ exclude_nr
;
2231 * if search_start is still in this block group
2232 * then we just re-search this block group
2234 if (search_start
>= start
&&
2235 search_start
< end
) {
2236 mutex_unlock(&block_group
->alloc_mutex
);
2241 /* else we go to the next block group */
2245 ins
->objectid
= search_start
;
2246 ins
->offset
= num_bytes
;
2248 btrfs_remove_free_space_lock(block_group
, search_start
,
2250 /* we are all good, lets return */
2251 mutex_unlock(&block_group
->alloc_mutex
);
2255 mutex_unlock(&block_group
->alloc_mutex
);
2258 if (!allowed_chunk_alloc
) {
2259 total_needed
-= empty_size
;
2263 * Here's how this works.
2264 * loop == 0: we were searching a block group via a hint
2265 * and didn't find anything, so we start at
2266 * the head of the block groups and keep searching
2267 * loop == 1: we're searching through all of the block groups
2268 * if we hit the head again we have searched
2269 * all of the block groups for this space and we
2270 * need to try and allocate, if we cant error out.
2271 * loop == 2: we allocated more space and are looping through
2272 * all of the block groups again.
2275 head
= &space_info
->block_groups
;
2278 } else if (loop
== 1 && cur
== head
) {
2280 total_needed
-= empty_size
;
2283 if (allowed_chunk_alloc
&& !chunk_alloc_done
) {
2284 up_read(&space_info
->groups_sem
);
2285 ret
= do_chunk_alloc(trans
, root
, num_bytes
+
2286 2 * 1024 * 1024, data
, 1);
2289 down_read(&space_info
->groups_sem
);
2291 head
= &space_info
->block_groups
;
2293 chunk_alloc_done
= 1;
2294 } else if (!allowed_chunk_alloc
) {
2295 space_info
->force_alloc
= 1;
2300 } else if (cur
== head
) {
2304 block_group
= list_entry(cur
, struct btrfs_block_group_cache
,
2306 search_start
= block_group
->key
.objectid
;
2310 /* we found what we needed */
2311 if (ins
->objectid
) {
2312 if (!(data
& BTRFS_BLOCK_GROUP_DATA
))
2313 trans
->block_group
= block_group
;
2316 *last_ptr
= ins
->objectid
+ ins
->offset
;
2322 up_read(&space_info
->groups_sem
);
2326 static void dump_space_info(struct btrfs_space_info
*info
, u64 bytes
)
2328 struct btrfs_block_group_cache
*cache
;
2329 struct list_head
*l
;
2331 printk(KERN_INFO
"space_info has %Lu free, is %sfull\n",
2332 info
->total_bytes
- info
->bytes_used
- info
->bytes_pinned
-
2333 info
->bytes_reserved
, (info
->full
) ? "" : "not ");
2335 down_read(&info
->groups_sem
);
2336 list_for_each(l
, &info
->block_groups
) {
2337 cache
= list_entry(l
, struct btrfs_block_group_cache
, list
);
2338 spin_lock(&cache
->lock
);
2339 printk(KERN_INFO
"block group %Lu has %Lu bytes, %Lu used "
2340 "%Lu pinned %Lu reserved\n",
2341 cache
->key
.objectid
, cache
->key
.offset
,
2342 btrfs_block_group_used(&cache
->item
),
2343 cache
->pinned
, cache
->reserved
);
2344 btrfs_dump_free_space(cache
, bytes
);
2345 spin_unlock(&cache
->lock
);
2347 up_read(&info
->groups_sem
);
2350 static int __btrfs_reserve_extent(struct btrfs_trans_handle
*trans
,
2351 struct btrfs_root
*root
,
2352 u64 num_bytes
, u64 min_alloc_size
,
2353 u64 empty_size
, u64 hint_byte
,
2354 u64 search_end
, struct btrfs_key
*ins
,
2358 u64 search_start
= 0;
2360 struct btrfs_fs_info
*info
= root
->fs_info
;
2363 alloc_profile
= info
->avail_data_alloc_bits
&
2364 info
->data_alloc_profile
;
2365 data
= BTRFS_BLOCK_GROUP_DATA
| alloc_profile
;
2366 } else if (root
== root
->fs_info
->chunk_root
) {
2367 alloc_profile
= info
->avail_system_alloc_bits
&
2368 info
->system_alloc_profile
;
2369 data
= BTRFS_BLOCK_GROUP_SYSTEM
| alloc_profile
;
2371 alloc_profile
= info
->avail_metadata_alloc_bits
&
2372 info
->metadata_alloc_profile
;
2373 data
= BTRFS_BLOCK_GROUP_METADATA
| alloc_profile
;
2376 data
= reduce_alloc_profile(root
, data
);
2378 * the only place that sets empty_size is btrfs_realloc_node, which
2379 * is not called recursively on allocations
2381 if (empty_size
|| root
->ref_cows
) {
2382 if (!(data
& BTRFS_BLOCK_GROUP_METADATA
)) {
2383 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
2385 BTRFS_BLOCK_GROUP_METADATA
|
2386 (info
->metadata_alloc_profile
&
2387 info
->avail_metadata_alloc_bits
), 0);
2389 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
2390 num_bytes
+ 2 * 1024 * 1024, data
, 0);
2393 WARN_ON(num_bytes
< root
->sectorsize
);
2394 ret
= find_free_extent(trans
, root
, num_bytes
, empty_size
,
2395 search_start
, search_end
, hint_byte
, ins
,
2396 trans
->alloc_exclude_start
,
2397 trans
->alloc_exclude_nr
, data
);
2399 if (ret
== -ENOSPC
&& num_bytes
> min_alloc_size
) {
2400 num_bytes
= num_bytes
>> 1;
2401 num_bytes
= num_bytes
& ~(root
->sectorsize
- 1);
2402 num_bytes
= max(num_bytes
, min_alloc_size
);
2403 do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
2404 num_bytes
, data
, 1);
2408 struct btrfs_space_info
*sinfo
;
2410 sinfo
= __find_space_info(root
->fs_info
, data
);
2411 printk("allocation failed flags %Lu, wanted %Lu\n",
2413 dump_space_info(sinfo
, num_bytes
);
2420 int btrfs_free_reserved_extent(struct btrfs_root
*root
, u64 start
, u64 len
)
2422 struct btrfs_block_group_cache
*cache
;
2424 cache
= btrfs_lookup_block_group(root
->fs_info
, start
);
2426 printk(KERN_ERR
"Unable to find block group for %Lu\n", start
);
2429 btrfs_add_free_space(cache
, start
, len
);
2430 update_reserved_extents(root
, start
, len
, 0);
2434 int btrfs_reserve_extent(struct btrfs_trans_handle
*trans
,
2435 struct btrfs_root
*root
,
2436 u64 num_bytes
, u64 min_alloc_size
,
2437 u64 empty_size
, u64 hint_byte
,
2438 u64 search_end
, struct btrfs_key
*ins
,
2442 ret
= __btrfs_reserve_extent(trans
, root
, num_bytes
, min_alloc_size
,
2443 empty_size
, hint_byte
, search_end
, ins
,
2445 update_reserved_extents(root
, ins
->objectid
, ins
->offset
, 1);
2449 static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle
*trans
,
2450 struct btrfs_root
*root
, u64 parent
,
2451 u64 root_objectid
, u64 ref_generation
,
2452 u64 owner
, struct btrfs_key
*ins
)
2458 u64 num_bytes
= ins
->offset
;
2460 struct btrfs_fs_info
*info
= root
->fs_info
;
2461 struct btrfs_root
*extent_root
= info
->extent_root
;
2462 struct btrfs_extent_item
*extent_item
;
2463 struct btrfs_extent_ref
*ref
;
2464 struct btrfs_path
*path
;
2465 struct btrfs_key keys
[2];
2468 parent
= ins
->objectid
;
2470 /* block accounting for super block */
2471 spin_lock_irq(&info
->delalloc_lock
);
2472 super_used
= btrfs_super_bytes_used(&info
->super_copy
);
2473 btrfs_set_super_bytes_used(&info
->super_copy
, super_used
+ num_bytes
);
2474 spin_unlock_irq(&info
->delalloc_lock
);
2476 /* block accounting for root item */
2477 root_used
= btrfs_root_used(&root
->root_item
);
2478 btrfs_set_root_used(&root
->root_item
, root_used
+ num_bytes
);
2480 if (root
== extent_root
) {
2481 struct pending_extent_op
*extent_op
;
2483 extent_op
= kmalloc(sizeof(*extent_op
), GFP_NOFS
);
2486 extent_op
->type
= PENDING_EXTENT_INSERT
;
2487 extent_op
->bytenr
= ins
->objectid
;
2488 extent_op
->num_bytes
= ins
->offset
;
2489 extent_op
->parent
= parent
;
2490 extent_op
->orig_parent
= 0;
2491 extent_op
->generation
= ref_generation
;
2492 extent_op
->orig_generation
= 0;
2493 extent_op
->level
= (int)owner
;
2495 mutex_lock(&root
->fs_info
->extent_ins_mutex
);
2496 set_extent_bits(&root
->fs_info
->extent_ins
, ins
->objectid
,
2497 ins
->objectid
+ ins
->offset
- 1,
2498 EXTENT_WRITEBACK
, GFP_NOFS
);
2499 set_state_private(&root
->fs_info
->extent_ins
,
2500 ins
->objectid
, (unsigned long)extent_op
);
2501 mutex_unlock(&root
->fs_info
->extent_ins_mutex
);
2505 memcpy(&keys
[0], ins
, sizeof(*ins
));
2506 keys
[1].objectid
= ins
->objectid
;
2507 keys
[1].type
= BTRFS_EXTENT_REF_KEY
;
2508 keys
[1].offset
= parent
;
2509 sizes
[0] = sizeof(*extent_item
);
2510 sizes
[1] = sizeof(*ref
);
2512 path
= btrfs_alloc_path();
2515 ret
= btrfs_insert_empty_items(trans
, extent_root
, path
, keys
,
2519 extent_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2520 struct btrfs_extent_item
);
2521 btrfs_set_extent_refs(path
->nodes
[0], extent_item
, 1);
2522 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
2523 struct btrfs_extent_ref
);
2525 btrfs_set_ref_root(path
->nodes
[0], ref
, root_objectid
);
2526 btrfs_set_ref_generation(path
->nodes
[0], ref
, ref_generation
);
2527 btrfs_set_ref_objectid(path
->nodes
[0], ref
, owner
);
2528 btrfs_set_ref_num_refs(path
->nodes
[0], ref
, 1);
2530 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2532 trans
->alloc_exclude_start
= 0;
2533 trans
->alloc_exclude_nr
= 0;
2534 btrfs_free_path(path
);
2535 finish_current_insert(trans
, extent_root
, 0);
2536 pending_ret
= del_pending_extents(trans
, extent_root
, 0);
2546 ret
= update_block_group(trans
, root
, ins
->objectid
, ins
->offset
, 1, 0);
2548 printk("update block group failed for %Lu %Lu\n",
2549 ins
->objectid
, ins
->offset
);
2556 int btrfs_alloc_reserved_extent(struct btrfs_trans_handle
*trans
,
2557 struct btrfs_root
*root
, u64 parent
,
2558 u64 root_objectid
, u64 ref_generation
,
2559 u64 owner
, struct btrfs_key
*ins
)
2563 if (root_objectid
== BTRFS_TREE_LOG_OBJECTID
)
2565 ret
= __btrfs_alloc_reserved_extent(trans
, root
, parent
, root_objectid
,
2566 ref_generation
, owner
, ins
);
2567 update_reserved_extents(root
, ins
->objectid
, ins
->offset
, 0);
2572 * this is used by the tree logging recovery code. It records that
2573 * an extent has been allocated and makes sure to clear the free
2574 * space cache bits as well
2576 int btrfs_alloc_logged_extent(struct btrfs_trans_handle
*trans
,
2577 struct btrfs_root
*root
, u64 parent
,
2578 u64 root_objectid
, u64 ref_generation
,
2579 u64 owner
, struct btrfs_key
*ins
)
2582 struct btrfs_block_group_cache
*block_group
;
2584 block_group
= btrfs_lookup_block_group(root
->fs_info
, ins
->objectid
);
2585 mutex_lock(&block_group
->alloc_mutex
);
2586 cache_block_group(root
, block_group
);
2588 ret
= btrfs_remove_free_space_lock(block_group
, ins
->objectid
,
2590 mutex_unlock(&block_group
->alloc_mutex
);
2592 ret
= __btrfs_alloc_reserved_extent(trans
, root
, parent
, root_objectid
,
2593 ref_generation
, owner
, ins
);
2598 * finds a free extent and does all the dirty work required for allocation
2599 * returns the key for the extent through ins, and a tree buffer for
2600 * the first block of the extent through buf.
2602 * returns 0 if everything worked, non-zero otherwise.
2604 int btrfs_alloc_extent(struct btrfs_trans_handle
*trans
,
2605 struct btrfs_root
*root
,
2606 u64 num_bytes
, u64 parent
, u64 min_alloc_size
,
2607 u64 root_objectid
, u64 ref_generation
,
2608 u64 owner_objectid
, u64 empty_size
, u64 hint_byte
,
2609 u64 search_end
, struct btrfs_key
*ins
, u64 data
)
2613 ret
= __btrfs_reserve_extent(trans
, root
, num_bytes
,
2614 min_alloc_size
, empty_size
, hint_byte
,
2615 search_end
, ins
, data
);
2617 if (root_objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
2618 ret
= __btrfs_alloc_reserved_extent(trans
, root
, parent
,
2619 root_objectid
, ref_generation
,
2620 owner_objectid
, ins
);
2624 update_reserved_extents(root
, ins
->objectid
, ins
->offset
, 1);
2629 struct extent_buffer
*btrfs_init_new_buffer(struct btrfs_trans_handle
*trans
,
2630 struct btrfs_root
*root
,
2631 u64 bytenr
, u32 blocksize
)
2633 struct extent_buffer
*buf
;
2635 buf
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
2637 return ERR_PTR(-ENOMEM
);
2638 btrfs_set_header_generation(buf
, trans
->transid
);
2639 btrfs_tree_lock(buf
);
2640 clean_tree_block(trans
, root
, buf
);
2641 btrfs_set_buffer_uptodate(buf
);
2642 if (root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
) {
2643 set_extent_dirty(&root
->dirty_log_pages
, buf
->start
,
2644 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
2646 set_extent_dirty(&trans
->transaction
->dirty_pages
, buf
->start
,
2647 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
2649 trans
->blocks_used
++;
2654 * helper function to allocate a block for a given tree
2655 * returns the tree buffer or NULL.
2657 struct extent_buffer
*btrfs_alloc_free_block(struct btrfs_trans_handle
*trans
,
2658 struct btrfs_root
*root
,
2659 u32 blocksize
, u64 parent
,
2666 struct btrfs_key ins
;
2668 struct extent_buffer
*buf
;
2670 ret
= btrfs_alloc_extent(trans
, root
, blocksize
, parent
, blocksize
,
2671 root_objectid
, ref_generation
, level
,
2672 empty_size
, hint
, (u64
)-1, &ins
, 0);
2675 return ERR_PTR(ret
);
2678 buf
= btrfs_init_new_buffer(trans
, root
, ins
.objectid
, blocksize
);
2682 int btrfs_drop_leaf_ref(struct btrfs_trans_handle
*trans
,
2683 struct btrfs_root
*root
, struct extent_buffer
*leaf
)
2686 u64 leaf_generation
;
2687 struct btrfs_key key
;
2688 struct btrfs_file_extent_item
*fi
;
2693 BUG_ON(!btrfs_is_leaf(leaf
));
2694 nritems
= btrfs_header_nritems(leaf
);
2695 leaf_owner
= btrfs_header_owner(leaf
);
2696 leaf_generation
= btrfs_header_generation(leaf
);
2698 for (i
= 0; i
< nritems
; i
++) {
2702 btrfs_item_key_to_cpu(leaf
, &key
, i
);
2703 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
2705 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
2706 if (btrfs_file_extent_type(leaf
, fi
) ==
2707 BTRFS_FILE_EXTENT_INLINE
)
2710 * FIXME make sure to insert a trans record that
2711 * repeats the snapshot del on crash
2713 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
2714 if (disk_bytenr
== 0)
2717 ret
= __btrfs_free_extent(trans
, root
, disk_bytenr
,
2718 btrfs_file_extent_disk_num_bytes(leaf
, fi
),
2719 leaf
->start
, leaf_owner
, leaf_generation
,
2723 atomic_inc(&root
->fs_info
->throttle_gen
);
2724 wake_up(&root
->fs_info
->transaction_throttle
);
2730 static int noinline
cache_drop_leaf_ref(struct btrfs_trans_handle
*trans
,
2731 struct btrfs_root
*root
,
2732 struct btrfs_leaf_ref
*ref
)
2736 struct btrfs_extent_info
*info
= ref
->extents
;
2738 for (i
= 0; i
< ref
->nritems
; i
++) {
2739 ret
= __btrfs_free_extent(trans
, root
, info
->bytenr
,
2740 info
->num_bytes
, ref
->bytenr
,
2741 ref
->owner
, ref
->generation
,
2744 atomic_inc(&root
->fs_info
->throttle_gen
);
2745 wake_up(&root
->fs_info
->transaction_throttle
);
2755 int drop_snap_lookup_refcount(struct btrfs_root
*root
, u64 start
, u64 len
,
2760 ret
= btrfs_lookup_extent_ref(NULL
, root
, start
, len
, refs
);
2763 #if 0 // some debugging code in case we see problems here
2764 /* if the refs count is one, it won't get increased again. But
2765 * if the ref count is > 1, someone may be decreasing it at
2766 * the same time we are.
2769 struct extent_buffer
*eb
= NULL
;
2770 eb
= btrfs_find_create_tree_block(root
, start
, len
);
2772 btrfs_tree_lock(eb
);
2774 mutex_lock(&root
->fs_info
->alloc_mutex
);
2775 ret
= lookup_extent_ref(NULL
, root
, start
, len
, refs
);
2777 mutex_unlock(&root
->fs_info
->alloc_mutex
);
2780 btrfs_tree_unlock(eb
);
2781 free_extent_buffer(eb
);
2784 printk("block %llu went down to one during drop_snap\n",
2785 (unsigned long long)start
);
2796 * helper function for drop_snapshot, this walks down the tree dropping ref
2797 * counts as it goes.
2799 static int noinline
walk_down_tree(struct btrfs_trans_handle
*trans
,
2800 struct btrfs_root
*root
,
2801 struct btrfs_path
*path
, int *level
)
2807 struct extent_buffer
*next
;
2808 struct extent_buffer
*cur
;
2809 struct extent_buffer
*parent
;
2810 struct btrfs_leaf_ref
*ref
;
2815 WARN_ON(*level
< 0);
2816 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
2817 ret
= drop_snap_lookup_refcount(root
, path
->nodes
[*level
]->start
,
2818 path
->nodes
[*level
]->len
, &refs
);
2824 * walk down to the last node level and free all the leaves
2826 while(*level
>= 0) {
2827 WARN_ON(*level
< 0);
2828 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
2829 cur
= path
->nodes
[*level
];
2831 if (btrfs_header_level(cur
) != *level
)
2834 if (path
->slots
[*level
] >=
2835 btrfs_header_nritems(cur
))
2838 ret
= btrfs_drop_leaf_ref(trans
, root
, cur
);
2842 bytenr
= btrfs_node_blockptr(cur
, path
->slots
[*level
]);
2843 ptr_gen
= btrfs_node_ptr_generation(cur
, path
->slots
[*level
]);
2844 blocksize
= btrfs_level_size(root
, *level
- 1);
2846 ret
= drop_snap_lookup_refcount(root
, bytenr
, blocksize
, &refs
);
2849 parent
= path
->nodes
[*level
];
2850 root_owner
= btrfs_header_owner(parent
);
2851 root_gen
= btrfs_header_generation(parent
);
2852 path
->slots
[*level
]++;
2854 ret
= __btrfs_free_extent(trans
, root
, bytenr
,
2855 blocksize
, parent
->start
,
2856 root_owner
, root_gen
,
2860 atomic_inc(&root
->fs_info
->throttle_gen
);
2861 wake_up(&root
->fs_info
->transaction_throttle
);
2867 * at this point, we have a single ref, and since the
2868 * only place referencing this extent is a dead root
2869 * the reference count should never go higher.
2870 * So, we don't need to check it again
2873 ref
= btrfs_lookup_leaf_ref(root
, bytenr
);
2874 if (ref
&& ref
->generation
!= ptr_gen
) {
2875 btrfs_free_leaf_ref(root
, ref
);
2879 ret
= cache_drop_leaf_ref(trans
, root
, ref
);
2881 btrfs_remove_leaf_ref(root
, ref
);
2882 btrfs_free_leaf_ref(root
, ref
);
2886 if (printk_ratelimit()) {
2887 printk("leaf ref miss for bytenr %llu\n",
2888 (unsigned long long)bytenr
);
2891 next
= btrfs_find_tree_block(root
, bytenr
, blocksize
);
2892 if (!next
|| !btrfs_buffer_uptodate(next
, ptr_gen
)) {
2893 free_extent_buffer(next
);
2895 next
= read_tree_block(root
, bytenr
, blocksize
,
2900 * this is a debugging check and can go away
2901 * the ref should never go all the way down to 1
2904 ret
= lookup_extent_ref(NULL
, root
, bytenr
, blocksize
,
2910 WARN_ON(*level
<= 0);
2911 if (path
->nodes
[*level
-1])
2912 free_extent_buffer(path
->nodes
[*level
-1]);
2913 path
->nodes
[*level
-1] = next
;
2914 *level
= btrfs_header_level(next
);
2915 path
->slots
[*level
] = 0;
2919 WARN_ON(*level
< 0);
2920 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
2922 if (path
->nodes
[*level
] == root
->node
) {
2923 parent
= path
->nodes
[*level
];
2924 bytenr
= path
->nodes
[*level
]->start
;
2926 parent
= path
->nodes
[*level
+ 1];
2927 bytenr
= btrfs_node_blockptr(parent
, path
->slots
[*level
+ 1]);
2930 blocksize
= btrfs_level_size(root
, *level
);
2931 root_owner
= btrfs_header_owner(parent
);
2932 root_gen
= btrfs_header_generation(parent
);
2934 ret
= __btrfs_free_extent(trans
, root
, bytenr
, blocksize
,
2935 parent
->start
, root_owner
, root_gen
,
2937 free_extent_buffer(path
->nodes
[*level
]);
2938 path
->nodes
[*level
] = NULL
;
2947 * helper function for drop_subtree, this function is similar to
2948 * walk_down_tree. The main difference is that it checks reference
2949 * counts while tree blocks are locked.
2951 static int noinline
walk_down_subtree(struct btrfs_trans_handle
*trans
,
2952 struct btrfs_root
*root
,
2953 struct btrfs_path
*path
, int *level
)
2955 struct extent_buffer
*next
;
2956 struct extent_buffer
*cur
;
2957 struct extent_buffer
*parent
;
2964 cur
= path
->nodes
[*level
];
2965 ret
= btrfs_lookup_extent_ref(trans
, root
, cur
->start
, cur
->len
,
2971 while (*level
>= 0) {
2972 cur
= path
->nodes
[*level
];
2974 ret
= btrfs_drop_leaf_ref(trans
, root
, cur
);
2976 clean_tree_block(trans
, root
, cur
);
2979 if (path
->slots
[*level
] >= btrfs_header_nritems(cur
)) {
2980 clean_tree_block(trans
, root
, cur
);
2984 bytenr
= btrfs_node_blockptr(cur
, path
->slots
[*level
]);
2985 blocksize
= btrfs_level_size(root
, *level
- 1);
2986 ptr_gen
= btrfs_node_ptr_generation(cur
, path
->slots
[*level
]);
2988 next
= read_tree_block(root
, bytenr
, blocksize
, ptr_gen
);
2989 btrfs_tree_lock(next
);
2991 ret
= btrfs_lookup_extent_ref(trans
, root
, bytenr
, blocksize
,
2995 parent
= path
->nodes
[*level
];
2996 ret
= btrfs_free_extent(trans
, root
, bytenr
,
2997 blocksize
, parent
->start
,
2998 btrfs_header_owner(parent
),
2999 btrfs_header_generation(parent
),
3002 path
->slots
[*level
]++;
3003 btrfs_tree_unlock(next
);
3004 free_extent_buffer(next
);
3008 *level
= btrfs_header_level(next
);
3009 path
->nodes
[*level
] = next
;
3010 path
->slots
[*level
] = 0;
3011 path
->locks
[*level
] = 1;
3015 parent
= path
->nodes
[*level
+ 1];
3016 bytenr
= path
->nodes
[*level
]->start
;
3017 blocksize
= path
->nodes
[*level
]->len
;
3019 ret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
,
3020 parent
->start
, btrfs_header_owner(parent
),
3021 btrfs_header_generation(parent
), *level
, 1);
3024 if (path
->locks
[*level
]) {
3025 btrfs_tree_unlock(path
->nodes
[*level
]);
3026 path
->locks
[*level
] = 0;
3028 free_extent_buffer(path
->nodes
[*level
]);
3029 path
->nodes
[*level
] = NULL
;
3036 * helper for dropping snapshots. This walks back up the tree in the path
3037 * to find the first node higher up where we haven't yet gone through
3040 static int noinline
walk_up_tree(struct btrfs_trans_handle
*trans
,
3041 struct btrfs_root
*root
,
3042 struct btrfs_path
*path
,
3043 int *level
, int max_level
)
3047 struct btrfs_root_item
*root_item
= &root
->root_item
;
3052 for (i
= *level
; i
< max_level
&& path
->nodes
[i
]; i
++) {
3053 slot
= path
->slots
[i
];
3054 if (slot
< btrfs_header_nritems(path
->nodes
[i
]) - 1) {
3055 struct extent_buffer
*node
;
3056 struct btrfs_disk_key disk_key
;
3057 node
= path
->nodes
[i
];
3060 WARN_ON(*level
== 0);
3061 btrfs_node_key(node
, &disk_key
, path
->slots
[i
]);
3062 memcpy(&root_item
->drop_progress
,
3063 &disk_key
, sizeof(disk_key
));
3064 root_item
->drop_level
= i
;
3067 struct extent_buffer
*parent
;
3068 if (path
->nodes
[*level
] == root
->node
)
3069 parent
= path
->nodes
[*level
];
3071 parent
= path
->nodes
[*level
+ 1];
3073 root_owner
= btrfs_header_owner(parent
);
3074 root_gen
= btrfs_header_generation(parent
);
3076 clean_tree_block(trans
, root
, path
->nodes
[*level
]);
3077 ret
= btrfs_free_extent(trans
, root
,
3078 path
->nodes
[*level
]->start
,
3079 path
->nodes
[*level
]->len
,
3080 parent
->start
, root_owner
,
3081 root_gen
, *level
, 1);
3083 if (path
->locks
[*level
]) {
3084 btrfs_tree_unlock(path
->nodes
[*level
]);
3085 path
->locks
[*level
] = 0;
3087 free_extent_buffer(path
->nodes
[*level
]);
3088 path
->nodes
[*level
] = NULL
;
3096 * drop the reference count on the tree rooted at 'snap'. This traverses
3097 * the tree freeing any blocks that have a ref count of zero after being
3100 int btrfs_drop_snapshot(struct btrfs_trans_handle
*trans
, struct btrfs_root
3106 struct btrfs_path
*path
;
3109 struct btrfs_root_item
*root_item
= &root
->root_item
;
3111 WARN_ON(!mutex_is_locked(&root
->fs_info
->drop_mutex
));
3112 path
= btrfs_alloc_path();
3115 level
= btrfs_header_level(root
->node
);
3117 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
3118 path
->nodes
[level
] = root
->node
;
3119 extent_buffer_get(root
->node
);
3120 path
->slots
[level
] = 0;
3122 struct btrfs_key key
;
3123 struct btrfs_disk_key found_key
;
3124 struct extent_buffer
*node
;
3126 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
3127 level
= root_item
->drop_level
;
3128 path
->lowest_level
= level
;
3129 wret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3134 node
= path
->nodes
[level
];
3135 btrfs_node_key(node
, &found_key
, path
->slots
[level
]);
3136 WARN_ON(memcmp(&found_key
, &root_item
->drop_progress
,
3137 sizeof(found_key
)));
3139 * unlock our path, this is safe because only this
3140 * function is allowed to delete this snapshot
3142 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++) {
3143 if (path
->nodes
[i
] && path
->locks
[i
]) {
3145 btrfs_tree_unlock(path
->nodes
[i
]);
3150 wret
= walk_down_tree(trans
, root
, path
, &level
);
3156 wret
= walk_up_tree(trans
, root
, path
, &level
,
3162 if (trans
->transaction
->in_commit
) {
3166 atomic_inc(&root
->fs_info
->throttle_gen
);
3167 wake_up(&root
->fs_info
->transaction_throttle
);
3169 for (i
= 0; i
<= orig_level
; i
++) {
3170 if (path
->nodes
[i
]) {
3171 free_extent_buffer(path
->nodes
[i
]);
3172 path
->nodes
[i
] = NULL
;
3176 btrfs_free_path(path
);
3180 int btrfs_drop_subtree(struct btrfs_trans_handle
*trans
,
3181 struct btrfs_root
*root
,
3182 struct extent_buffer
*node
,
3183 struct extent_buffer
*parent
)
3185 struct btrfs_path
*path
;
3191 path
= btrfs_alloc_path();
3194 BUG_ON(!btrfs_tree_locked(parent
));
3195 parent_level
= btrfs_header_level(parent
);
3196 extent_buffer_get(parent
);
3197 path
->nodes
[parent_level
] = parent
;
3198 path
->slots
[parent_level
] = btrfs_header_nritems(parent
);
3200 BUG_ON(!btrfs_tree_locked(node
));
3201 level
= btrfs_header_level(node
);
3202 extent_buffer_get(node
);
3203 path
->nodes
[level
] = node
;
3204 path
->slots
[level
] = 0;
3207 wret
= walk_down_subtree(trans
, root
, path
, &level
);
3213 wret
= walk_up_tree(trans
, root
, path
, &level
, parent_level
);
3220 btrfs_free_path(path
);
3224 static unsigned long calc_ra(unsigned long start
, unsigned long last
,
3227 return min(last
, start
+ nr
- 1);
3230 static int noinline
relocate_inode_pages(struct inode
*inode
, u64 start
,
3235 unsigned long first_index
;
3236 unsigned long last_index
;
3239 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3240 struct file_ra_state
*ra
;
3241 struct btrfs_ordered_extent
*ordered
;
3242 unsigned int total_read
= 0;
3243 unsigned int total_dirty
= 0;
3246 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
3248 mutex_lock(&inode
->i_mutex
);
3249 first_index
= start
>> PAGE_CACHE_SHIFT
;
3250 last_index
= (start
+ len
- 1) >> PAGE_CACHE_SHIFT
;
3252 /* make sure the dirty trick played by the caller work */
3253 ret
= invalidate_inode_pages2_range(inode
->i_mapping
,
3254 first_index
, last_index
);
3258 file_ra_state_init(ra
, inode
->i_mapping
);
3260 for (i
= first_index
; i
<= last_index
; i
++) {
3261 if (total_read
% ra
->ra_pages
== 0) {
3262 btrfs_force_ra(inode
->i_mapping
, ra
, NULL
, i
,
3263 calc_ra(i
, last_index
, ra
->ra_pages
));
3267 if (((u64
)i
<< PAGE_CACHE_SHIFT
) > i_size_read(inode
))
3269 page
= grab_cache_page(inode
->i_mapping
, i
);
3274 if (!PageUptodate(page
)) {
3275 btrfs_readpage(NULL
, page
);
3277 if (!PageUptodate(page
)) {
3279 page_cache_release(page
);
3284 wait_on_page_writeback(page
);
3286 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
3287 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
3288 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
3290 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
3292 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
3294 page_cache_release(page
);
3295 btrfs_start_ordered_extent(inode
, ordered
, 1);
3296 btrfs_put_ordered_extent(ordered
);
3299 set_page_extent_mapped(page
);
3301 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
3302 if (i
== first_index
)
3303 set_extent_bits(io_tree
, page_start
, page_end
,
3304 EXTENT_BOUNDARY
, GFP_NOFS
);
3306 set_page_dirty(page
);
3309 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
3311 page_cache_release(page
);
3316 mutex_unlock(&inode
->i_mutex
);
3317 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, total_dirty
);
3321 static int noinline
relocate_data_extent(struct inode
*reloc_inode
,
3322 struct btrfs_key
*extent_key
,
3325 struct btrfs_root
*root
= BTRFS_I(reloc_inode
)->root
;
3326 struct extent_map_tree
*em_tree
= &BTRFS_I(reloc_inode
)->extent_tree
;
3327 struct extent_map
*em
;
3328 u64 start
= extent_key
->objectid
- offset
;
3329 u64 end
= start
+ extent_key
->offset
- 1;
3331 em
= alloc_extent_map(GFP_NOFS
);
3332 BUG_ON(!em
|| IS_ERR(em
));
3335 em
->len
= extent_key
->offset
;
3336 em
->block_len
= extent_key
->offset
;
3337 em
->block_start
= extent_key
->objectid
;
3338 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
3339 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
3341 /* setup extent map to cheat btrfs_readpage */
3342 lock_extent(&BTRFS_I(reloc_inode
)->io_tree
, start
, end
, GFP_NOFS
);
3345 spin_lock(&em_tree
->lock
);
3346 ret
= add_extent_mapping(em_tree
, em
);
3347 spin_unlock(&em_tree
->lock
);
3348 if (ret
!= -EEXIST
) {
3349 free_extent_map(em
);
3352 btrfs_drop_extent_cache(reloc_inode
, start
, end
, 0);
3354 unlock_extent(&BTRFS_I(reloc_inode
)->io_tree
, start
, end
, GFP_NOFS
);
3356 return relocate_inode_pages(reloc_inode
, start
, extent_key
->offset
);
3359 struct btrfs_ref_path
{
3361 u64 nodes
[BTRFS_MAX_LEVEL
];
3363 u64 root_generation
;
3370 struct btrfs_key node_keys
[BTRFS_MAX_LEVEL
];
3371 u64 new_nodes
[BTRFS_MAX_LEVEL
];
3374 struct disk_extent
{
3385 static int is_cowonly_root(u64 root_objectid
)
3387 if (root_objectid
== BTRFS_ROOT_TREE_OBJECTID
||
3388 root_objectid
== BTRFS_EXTENT_TREE_OBJECTID
||
3389 root_objectid
== BTRFS_CHUNK_TREE_OBJECTID
||
3390 root_objectid
== BTRFS_DEV_TREE_OBJECTID
||
3391 root_objectid
== BTRFS_TREE_LOG_OBJECTID
)
3396 static int noinline
__next_ref_path(struct btrfs_trans_handle
*trans
,
3397 struct btrfs_root
*extent_root
,
3398 struct btrfs_ref_path
*ref_path
,
3401 struct extent_buffer
*leaf
;
3402 struct btrfs_path
*path
;
3403 struct btrfs_extent_ref
*ref
;
3404 struct btrfs_key key
;
3405 struct btrfs_key found_key
;
3411 path
= btrfs_alloc_path();
3416 ref_path
->lowest_level
= -1;
3417 ref_path
->current_level
= -1;
3418 ref_path
->shared_level
= -1;
3422 level
= ref_path
->current_level
- 1;
3423 while (level
>= -1) {
3425 if (level
< ref_path
->lowest_level
)
3429 bytenr
= ref_path
->nodes
[level
];
3431 bytenr
= ref_path
->extent_start
;
3433 BUG_ON(bytenr
== 0);
3435 parent
= ref_path
->nodes
[level
+ 1];
3436 ref_path
->nodes
[level
+ 1] = 0;
3437 ref_path
->current_level
= level
;
3438 BUG_ON(parent
== 0);
3440 key
.objectid
= bytenr
;
3441 key
.offset
= parent
+ 1;
3442 key
.type
= BTRFS_EXTENT_REF_KEY
;
3444 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
, 0, 0);
3449 leaf
= path
->nodes
[0];
3450 nritems
= btrfs_header_nritems(leaf
);
3451 if (path
->slots
[0] >= nritems
) {
3452 ret
= btrfs_next_leaf(extent_root
, path
);
3457 leaf
= path
->nodes
[0];
3460 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3461 if (found_key
.objectid
== bytenr
&&
3462 found_key
.type
== BTRFS_EXTENT_REF_KEY
) {
3463 if (level
< ref_path
->shared_level
)
3464 ref_path
->shared_level
= level
;
3469 btrfs_release_path(extent_root
, path
);
3472 /* reached lowest level */
3476 level
= ref_path
->current_level
;
3477 while (level
< BTRFS_MAX_LEVEL
- 1) {
3480 bytenr
= ref_path
->nodes
[level
];
3482 bytenr
= ref_path
->extent_start
;
3484 BUG_ON(bytenr
== 0);
3486 key
.objectid
= bytenr
;
3488 key
.type
= BTRFS_EXTENT_REF_KEY
;
3490 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
, 0, 0);
3494 leaf
= path
->nodes
[0];
3495 nritems
= btrfs_header_nritems(leaf
);
3496 if (path
->slots
[0] >= nritems
) {
3497 ret
= btrfs_next_leaf(extent_root
, path
);
3501 /* the extent was freed by someone */
3502 if (ref_path
->lowest_level
== level
)
3504 btrfs_release_path(extent_root
, path
);
3507 leaf
= path
->nodes
[0];
3510 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3511 if (found_key
.objectid
!= bytenr
||
3512 found_key
.type
!= BTRFS_EXTENT_REF_KEY
) {
3513 /* the extent was freed by someone */
3514 if (ref_path
->lowest_level
== level
) {
3518 btrfs_release_path(extent_root
, path
);
3522 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
3523 struct btrfs_extent_ref
);
3524 ref_objectid
= btrfs_ref_objectid(leaf
, ref
);
3525 if (ref_objectid
< BTRFS_FIRST_FREE_OBJECTID
) {
3527 level
= (int)ref_objectid
;
3528 BUG_ON(level
>= BTRFS_MAX_LEVEL
);
3529 ref_path
->lowest_level
= level
;
3530 ref_path
->current_level
= level
;
3531 ref_path
->nodes
[level
] = bytenr
;
3533 WARN_ON(ref_objectid
!= level
);
3536 WARN_ON(level
!= -1);
3540 if (ref_path
->lowest_level
== level
) {
3541 ref_path
->owner_objectid
= ref_objectid
;
3542 ref_path
->num_refs
= btrfs_ref_num_refs(leaf
, ref
);
3546 * the block is tree root or the block isn't in reference
3549 if (found_key
.objectid
== found_key
.offset
||
3550 is_cowonly_root(btrfs_ref_root(leaf
, ref
))) {
3551 ref_path
->root_objectid
= btrfs_ref_root(leaf
, ref
);
3552 ref_path
->root_generation
=
3553 btrfs_ref_generation(leaf
, ref
);
3555 /* special reference from the tree log */
3556 ref_path
->nodes
[0] = found_key
.offset
;
3557 ref_path
->current_level
= 0;
3564 BUG_ON(ref_path
->nodes
[level
] != 0);
3565 ref_path
->nodes
[level
] = found_key
.offset
;
3566 ref_path
->current_level
= level
;
3569 * the reference was created in the running transaction,
3570 * no need to continue walking up.
3572 if (btrfs_ref_generation(leaf
, ref
) == trans
->transid
) {
3573 ref_path
->root_objectid
= btrfs_ref_root(leaf
, ref
);
3574 ref_path
->root_generation
=
3575 btrfs_ref_generation(leaf
, ref
);
3580 btrfs_release_path(extent_root
, path
);
3583 /* reached max tree level, but no tree root found. */
3586 btrfs_free_path(path
);
3590 static int btrfs_first_ref_path(struct btrfs_trans_handle
*trans
,
3591 struct btrfs_root
*extent_root
,
3592 struct btrfs_ref_path
*ref_path
,
3595 memset(ref_path
, 0, sizeof(*ref_path
));
3596 ref_path
->extent_start
= extent_start
;
3598 return __next_ref_path(trans
, extent_root
, ref_path
, 1);
3601 static int btrfs_next_ref_path(struct btrfs_trans_handle
*trans
,
3602 struct btrfs_root
*extent_root
,
3603 struct btrfs_ref_path
*ref_path
)
3605 return __next_ref_path(trans
, extent_root
, ref_path
, 0);
3608 static int noinline
get_new_locations(struct inode
*reloc_inode
,
3609 struct btrfs_key
*extent_key
,
3610 u64 offset
, int no_fragment
,
3611 struct disk_extent
**extents
,
3614 struct btrfs_root
*root
= BTRFS_I(reloc_inode
)->root
;
3615 struct btrfs_path
*path
;
3616 struct btrfs_file_extent_item
*fi
;
3617 struct extent_buffer
*leaf
;
3618 struct disk_extent
*exts
= *extents
;
3619 struct btrfs_key found_key
;
3624 int max
= *nr_extents
;
3627 WARN_ON(!no_fragment
&& *extents
);
3630 exts
= kmalloc(sizeof(*exts
) * max
, GFP_NOFS
);
3635 path
= btrfs_alloc_path();
3638 cur_pos
= extent_key
->objectid
- offset
;
3639 last_byte
= extent_key
->objectid
+ extent_key
->offset
;
3640 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, reloc_inode
->i_ino
,
3650 leaf
= path
->nodes
[0];
3651 nritems
= btrfs_header_nritems(leaf
);
3652 if (path
->slots
[0] >= nritems
) {
3653 ret
= btrfs_next_leaf(root
, path
);
3658 leaf
= path
->nodes
[0];
3661 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3662 if (found_key
.offset
!= cur_pos
||
3663 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
||
3664 found_key
.objectid
!= reloc_inode
->i_ino
)
3667 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3668 struct btrfs_file_extent_item
);
3669 if (btrfs_file_extent_type(leaf
, fi
) !=
3670 BTRFS_FILE_EXTENT_REG
||
3671 btrfs_file_extent_disk_bytenr(leaf
, fi
) == 0)
3675 struct disk_extent
*old
= exts
;
3677 exts
= kzalloc(sizeof(*exts
) * max
, GFP_NOFS
);
3678 memcpy(exts
, old
, sizeof(*exts
) * nr
);
3679 if (old
!= *extents
)
3683 exts
[nr
].disk_bytenr
=
3684 btrfs_file_extent_disk_bytenr(leaf
, fi
);
3685 exts
[nr
].disk_num_bytes
=
3686 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
3687 exts
[nr
].offset
= btrfs_file_extent_offset(leaf
, fi
);
3688 exts
[nr
].num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
3689 exts
[nr
].ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
3690 exts
[nr
].compression
= btrfs_file_extent_compression(leaf
, fi
);
3691 exts
[nr
].encryption
= btrfs_file_extent_encryption(leaf
, fi
);
3692 exts
[nr
].other_encoding
= btrfs_file_extent_other_encoding(leaf
,
3694 BUG_ON(exts
[nr
].offset
> 0);
3695 BUG_ON(exts
[nr
].compression
|| exts
[nr
].encryption
);
3696 BUG_ON(exts
[nr
].num_bytes
!= exts
[nr
].disk_num_bytes
);
3698 cur_pos
+= exts
[nr
].num_bytes
;
3701 if (cur_pos
+ offset
>= last_byte
)
3711 WARN_ON(cur_pos
+ offset
> last_byte
);
3712 if (cur_pos
+ offset
< last_byte
) {
3718 btrfs_free_path(path
);
3720 if (exts
!= *extents
)
3729 static int noinline
replace_one_extent(struct btrfs_trans_handle
*trans
,
3730 struct btrfs_root
*root
,
3731 struct btrfs_path
*path
,
3732 struct btrfs_key
*extent_key
,
3733 struct btrfs_key
*leaf_key
,
3734 struct btrfs_ref_path
*ref_path
,
3735 struct disk_extent
*new_extents
,
3738 struct extent_buffer
*leaf
;
3739 struct btrfs_file_extent_item
*fi
;
3740 struct inode
*inode
= NULL
;
3741 struct btrfs_key key
;
3749 int extent_locked
= 0;
3753 memcpy(&key
, leaf_key
, sizeof(key
));
3754 first_pos
= INT_LIMIT(loff_t
) - extent_key
->offset
;
3755 if (ref_path
->owner_objectid
!= BTRFS_MULTIPLE_OBJECTIDS
) {
3756 if (key
.objectid
< ref_path
->owner_objectid
||
3757 (key
.objectid
== ref_path
->owner_objectid
&&
3758 key
.type
< BTRFS_EXTENT_DATA_KEY
)) {
3759 key
.objectid
= ref_path
->owner_objectid
;
3760 key
.type
= BTRFS_EXTENT_DATA_KEY
;
3766 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
3770 leaf
= path
->nodes
[0];
3771 nritems
= btrfs_header_nritems(leaf
);
3773 if (extent_locked
&& ret
> 0) {
3775 * the file extent item was modified by someone
3776 * before the extent got locked.
3778 unlock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
3779 lock_end
, GFP_NOFS
);
3783 if (path
->slots
[0] >= nritems
) {
3784 if (++nr_scaned
> 2)
3787 BUG_ON(extent_locked
);
3788 ret
= btrfs_next_leaf(root
, path
);
3793 leaf
= path
->nodes
[0];
3794 nritems
= btrfs_header_nritems(leaf
);
3797 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3799 if (ref_path
->owner_objectid
!= BTRFS_MULTIPLE_OBJECTIDS
) {
3800 if ((key
.objectid
> ref_path
->owner_objectid
) ||
3801 (key
.objectid
== ref_path
->owner_objectid
&&
3802 key
.type
> BTRFS_EXTENT_DATA_KEY
) ||
3803 (key
.offset
>= first_pos
+ extent_key
->offset
))
3807 if (inode
&& key
.objectid
!= inode
->i_ino
) {
3808 BUG_ON(extent_locked
);
3809 btrfs_release_path(root
, path
);
3810 mutex_unlock(&inode
->i_mutex
);
3816 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
3821 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3822 struct btrfs_file_extent_item
);
3823 extent_type
= btrfs_file_extent_type(leaf
, fi
);
3824 if ((extent_type
!= BTRFS_FILE_EXTENT_REG
&&
3825 extent_type
!= BTRFS_FILE_EXTENT_PREALLOC
) ||
3826 (btrfs_file_extent_disk_bytenr(leaf
, fi
) !=
3827 extent_key
->objectid
)) {
3833 num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
3834 ext_offset
= btrfs_file_extent_offset(leaf
, fi
);
3836 if (first_pos
> key
.offset
- ext_offset
)
3837 first_pos
= key
.offset
- ext_offset
;
3839 if (!extent_locked
) {
3840 lock_start
= key
.offset
;
3841 lock_end
= lock_start
+ num_bytes
- 1;
3843 if (lock_start
> key
.offset
||
3844 lock_end
+ 1 < key
.offset
+ num_bytes
) {
3845 unlock_extent(&BTRFS_I(inode
)->io_tree
,
3846 lock_start
, lock_end
, GFP_NOFS
);
3852 btrfs_release_path(root
, path
);
3854 inode
= btrfs_iget_locked(root
->fs_info
->sb
,
3855 key
.objectid
, root
);
3856 if (inode
->i_state
& I_NEW
) {
3857 BTRFS_I(inode
)->root
= root
;
3858 BTRFS_I(inode
)->location
.objectid
=
3860 BTRFS_I(inode
)->location
.type
=
3861 BTRFS_INODE_ITEM_KEY
;
3862 BTRFS_I(inode
)->location
.offset
= 0;
3863 btrfs_read_locked_inode(inode
);
3864 unlock_new_inode(inode
);
3867 * some code call btrfs_commit_transaction while
3868 * holding the i_mutex, so we can't use mutex_lock
3871 if (is_bad_inode(inode
) ||
3872 !mutex_trylock(&inode
->i_mutex
)) {
3875 key
.offset
= (u64
)-1;
3880 if (!extent_locked
) {
3881 struct btrfs_ordered_extent
*ordered
;
3883 btrfs_release_path(root
, path
);
3885 lock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
3886 lock_end
, GFP_NOFS
);
3887 ordered
= btrfs_lookup_first_ordered_extent(inode
,
3890 ordered
->file_offset
<= lock_end
&&
3891 ordered
->file_offset
+ ordered
->len
> lock_start
) {
3892 unlock_extent(&BTRFS_I(inode
)->io_tree
,
3893 lock_start
, lock_end
, GFP_NOFS
);
3894 btrfs_start_ordered_extent(inode
, ordered
, 1);
3895 btrfs_put_ordered_extent(ordered
);
3896 key
.offset
+= num_bytes
;
3900 btrfs_put_ordered_extent(ordered
);
3906 if (nr_extents
== 1) {
3907 /* update extent pointer in place */
3908 btrfs_set_file_extent_disk_bytenr(leaf
, fi
,
3909 new_extents
[0].disk_bytenr
);
3910 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
,
3911 new_extents
[0].disk_num_bytes
);
3912 btrfs_mark_buffer_dirty(leaf
);
3914 btrfs_drop_extent_cache(inode
, key
.offset
,
3915 key
.offset
+ num_bytes
- 1, 0);
3917 ret
= btrfs_inc_extent_ref(trans
, root
,
3918 new_extents
[0].disk_bytenr
,
3919 new_extents
[0].disk_num_bytes
,
3921 root
->root_key
.objectid
,
3926 ret
= btrfs_free_extent(trans
, root
,
3927 extent_key
->objectid
,
3930 btrfs_header_owner(leaf
),
3931 btrfs_header_generation(leaf
),
3935 btrfs_release_path(root
, path
);
3936 key
.offset
+= num_bytes
;
3944 * drop old extent pointer at first, then insert the
3945 * new pointers one bye one
3947 btrfs_release_path(root
, path
);
3948 ret
= btrfs_drop_extents(trans
, root
, inode
, key
.offset
,
3949 key
.offset
+ num_bytes
,
3950 key
.offset
, &alloc_hint
);
3953 for (i
= 0; i
< nr_extents
; i
++) {
3954 if (ext_offset
>= new_extents
[i
].num_bytes
) {
3955 ext_offset
-= new_extents
[i
].num_bytes
;
3958 extent_len
= min(new_extents
[i
].num_bytes
-
3959 ext_offset
, num_bytes
);
3961 ret
= btrfs_insert_empty_item(trans
, root
,
3966 leaf
= path
->nodes
[0];
3967 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3968 struct btrfs_file_extent_item
);
3969 btrfs_set_file_extent_generation(leaf
, fi
,
3971 btrfs_set_file_extent_type(leaf
, fi
,
3972 BTRFS_FILE_EXTENT_REG
);
3973 btrfs_set_file_extent_disk_bytenr(leaf
, fi
,
3974 new_extents
[i
].disk_bytenr
);
3975 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
,
3976 new_extents
[i
].disk_num_bytes
);
3977 btrfs_set_file_extent_ram_bytes(leaf
, fi
,
3978 new_extents
[i
].ram_bytes
);
3980 btrfs_set_file_extent_compression(leaf
, fi
,
3981 new_extents
[i
].compression
);
3982 btrfs_set_file_extent_encryption(leaf
, fi
,
3983 new_extents
[i
].encryption
);
3984 btrfs_set_file_extent_other_encoding(leaf
, fi
,
3985 new_extents
[i
].other_encoding
);
3987 btrfs_set_file_extent_num_bytes(leaf
, fi
,
3989 ext_offset
+= new_extents
[i
].offset
;
3990 btrfs_set_file_extent_offset(leaf
, fi
,
3992 btrfs_mark_buffer_dirty(leaf
);
3994 btrfs_drop_extent_cache(inode
, key
.offset
,
3995 key
.offset
+ extent_len
- 1, 0);
3997 ret
= btrfs_inc_extent_ref(trans
, root
,
3998 new_extents
[i
].disk_bytenr
,
3999 new_extents
[i
].disk_num_bytes
,
4001 root
->root_key
.objectid
,
4002 trans
->transid
, key
.objectid
);
4004 btrfs_release_path(root
, path
);
4006 inode_add_bytes(inode
, extent_len
);
4009 num_bytes
-= extent_len
;
4010 key
.offset
+= extent_len
;
4015 BUG_ON(i
>= nr_extents
);
4019 if (extent_locked
) {
4020 unlock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
4021 lock_end
, GFP_NOFS
);
4025 if (ref_path
->owner_objectid
!= BTRFS_MULTIPLE_OBJECTIDS
&&
4026 key
.offset
>= first_pos
+ extent_key
->offset
)
4033 btrfs_release_path(root
, path
);
4035 mutex_unlock(&inode
->i_mutex
);
4036 if (extent_locked
) {
4037 unlock_extent(&BTRFS_I(inode
)->io_tree
, lock_start
,
4038 lock_end
, GFP_NOFS
);
4045 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle
*trans
,
4046 struct btrfs_root
*root
,
4047 struct extent_buffer
*buf
, u64 orig_start
)
4052 BUG_ON(btrfs_header_generation(buf
) != trans
->transid
);
4053 BUG_ON(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
4055 level
= btrfs_header_level(buf
);
4057 struct btrfs_leaf_ref
*ref
;
4058 struct btrfs_leaf_ref
*orig_ref
;
4060 orig_ref
= btrfs_lookup_leaf_ref(root
, orig_start
);
4064 ref
= btrfs_alloc_leaf_ref(root
, orig_ref
->nritems
);
4066 btrfs_free_leaf_ref(root
, orig_ref
);
4070 ref
->nritems
= orig_ref
->nritems
;
4071 memcpy(ref
->extents
, orig_ref
->extents
,
4072 sizeof(ref
->extents
[0]) * ref
->nritems
);
4074 btrfs_free_leaf_ref(root
, orig_ref
);
4076 ref
->root_gen
= trans
->transid
;
4077 ref
->bytenr
= buf
->start
;
4078 ref
->owner
= btrfs_header_owner(buf
);
4079 ref
->generation
= btrfs_header_generation(buf
);
4080 ret
= btrfs_add_leaf_ref(root
, ref
, 0);
4082 btrfs_free_leaf_ref(root
, ref
);
4087 static int noinline
invalidate_extent_cache(struct btrfs_root
*root
,
4088 struct extent_buffer
*leaf
,
4089 struct btrfs_block_group_cache
*group
,
4090 struct btrfs_root
*target_root
)
4092 struct btrfs_key key
;
4093 struct inode
*inode
= NULL
;
4094 struct btrfs_file_extent_item
*fi
;
4096 u64 skip_objectid
= 0;
4100 nritems
= btrfs_header_nritems(leaf
);
4101 for (i
= 0; i
< nritems
; i
++) {
4102 btrfs_item_key_to_cpu(leaf
, &key
, i
);
4103 if (key
.objectid
== skip_objectid
||
4104 key
.type
!= BTRFS_EXTENT_DATA_KEY
)
4106 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
4107 if (btrfs_file_extent_type(leaf
, fi
) ==
4108 BTRFS_FILE_EXTENT_INLINE
)
4110 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) == 0)
4112 if (!inode
|| inode
->i_ino
!= key
.objectid
) {
4114 inode
= btrfs_ilookup(target_root
->fs_info
->sb
,
4115 key
.objectid
, target_root
, 1);
4118 skip_objectid
= key
.objectid
;
4121 num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
4123 lock_extent(&BTRFS_I(inode
)->io_tree
, key
.offset
,
4124 key
.offset
+ num_bytes
- 1, GFP_NOFS
);
4125 btrfs_drop_extent_cache(inode
, key
.offset
,
4126 key
.offset
+ num_bytes
- 1, 1);
4127 unlock_extent(&BTRFS_I(inode
)->io_tree
, key
.offset
,
4128 key
.offset
+ num_bytes
- 1, GFP_NOFS
);
4135 static int noinline
replace_extents_in_leaf(struct btrfs_trans_handle
*trans
,
4136 struct btrfs_root
*root
,
4137 struct extent_buffer
*leaf
,
4138 struct btrfs_block_group_cache
*group
,
4139 struct inode
*reloc_inode
)
4141 struct btrfs_key key
;
4142 struct btrfs_key extent_key
;
4143 struct btrfs_file_extent_item
*fi
;
4144 struct btrfs_leaf_ref
*ref
;
4145 struct disk_extent
*new_extent
;
4154 new_extent
= kmalloc(sizeof(*new_extent
), GFP_NOFS
);
4155 BUG_ON(!new_extent
);
4157 ref
= btrfs_lookup_leaf_ref(root
, leaf
->start
);
4161 nritems
= btrfs_header_nritems(leaf
);
4162 for (i
= 0; i
< nritems
; i
++) {
4163 btrfs_item_key_to_cpu(leaf
, &key
, i
);
4164 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
4166 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
4167 if (btrfs_file_extent_type(leaf
, fi
) ==
4168 BTRFS_FILE_EXTENT_INLINE
)
4170 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
4171 num_bytes
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
4176 if (bytenr
>= group
->key
.objectid
+ group
->key
.offset
||
4177 bytenr
+ num_bytes
<= group
->key
.objectid
)
4180 extent_key
.objectid
= bytenr
;
4181 extent_key
.offset
= num_bytes
;
4182 extent_key
.type
= BTRFS_EXTENT_ITEM_KEY
;
4184 ret
= get_new_locations(reloc_inode
, &extent_key
,
4185 group
->key
.objectid
, 1,
4186 &new_extent
, &nr_extent
);
4191 BUG_ON(ref
->extents
[ext_index
].bytenr
!= bytenr
);
4192 BUG_ON(ref
->extents
[ext_index
].num_bytes
!= num_bytes
);
4193 ref
->extents
[ext_index
].bytenr
= new_extent
->disk_bytenr
;
4194 ref
->extents
[ext_index
].num_bytes
= new_extent
->disk_num_bytes
;
4196 btrfs_set_file_extent_disk_bytenr(leaf
, fi
,
4197 new_extent
->disk_bytenr
);
4198 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
,
4199 new_extent
->disk_num_bytes
);
4200 btrfs_mark_buffer_dirty(leaf
);
4202 ret
= btrfs_inc_extent_ref(trans
, root
,
4203 new_extent
->disk_bytenr
,
4204 new_extent
->disk_num_bytes
,
4206 root
->root_key
.objectid
,
4207 trans
->transid
, key
.objectid
);
4209 ret
= btrfs_free_extent(trans
, root
,
4210 bytenr
, num_bytes
, leaf
->start
,
4211 btrfs_header_owner(leaf
),
4212 btrfs_header_generation(leaf
),
4218 BUG_ON(ext_index
+ 1 != ref
->nritems
);
4219 btrfs_free_leaf_ref(root
, ref
);
4223 int btrfs_free_reloc_root(struct btrfs_trans_handle
*trans
,
4224 struct btrfs_root
*root
)
4226 struct btrfs_root
*reloc_root
;
4229 if (root
->reloc_root
) {
4230 reloc_root
= root
->reloc_root
;
4231 root
->reloc_root
= NULL
;
4232 list_add(&reloc_root
->dead_list
,
4233 &root
->fs_info
->dead_reloc_roots
);
4235 btrfs_set_root_bytenr(&reloc_root
->root_item
,
4236 reloc_root
->node
->start
);
4237 btrfs_set_root_level(&root
->root_item
,
4238 btrfs_header_level(reloc_root
->node
));
4239 memset(&reloc_root
->root_item
.drop_progress
, 0,
4240 sizeof(struct btrfs_disk_key
));
4241 reloc_root
->root_item
.drop_level
= 0;
4243 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
4244 &reloc_root
->root_key
,
4245 &reloc_root
->root_item
);
4251 int btrfs_drop_dead_reloc_roots(struct btrfs_root
*root
)
4253 struct btrfs_trans_handle
*trans
;
4254 struct btrfs_root
*reloc_root
;
4255 struct btrfs_root
*prev_root
= NULL
;
4256 struct list_head dead_roots
;
4260 INIT_LIST_HEAD(&dead_roots
);
4261 list_splice_init(&root
->fs_info
->dead_reloc_roots
, &dead_roots
);
4263 while (!list_empty(&dead_roots
)) {
4264 reloc_root
= list_entry(dead_roots
.prev
,
4265 struct btrfs_root
, dead_list
);
4266 list_del_init(&reloc_root
->dead_list
);
4268 BUG_ON(reloc_root
->commit_root
!= NULL
);
4270 trans
= btrfs_join_transaction(root
, 1);
4273 mutex_lock(&root
->fs_info
->drop_mutex
);
4274 ret
= btrfs_drop_snapshot(trans
, reloc_root
);
4277 mutex_unlock(&root
->fs_info
->drop_mutex
);
4279 nr
= trans
->blocks_used
;
4280 ret
= btrfs_end_transaction(trans
, root
);
4282 btrfs_btree_balance_dirty(root
, nr
);
4285 free_extent_buffer(reloc_root
->node
);
4287 ret
= btrfs_del_root(trans
, root
->fs_info
->tree_root
,
4288 &reloc_root
->root_key
);
4290 mutex_unlock(&root
->fs_info
->drop_mutex
);
4292 nr
= trans
->blocks_used
;
4293 ret
= btrfs_end_transaction(trans
, root
);
4295 btrfs_btree_balance_dirty(root
, nr
);
4298 prev_root
= reloc_root
;
4301 btrfs_remove_leaf_refs(prev_root
, (u64
)-1, 0);
4307 int btrfs_add_dead_reloc_root(struct btrfs_root
*root
)
4309 list_add(&root
->dead_list
, &root
->fs_info
->dead_reloc_roots
);
4313 int btrfs_cleanup_reloc_trees(struct btrfs_root
*root
)
4315 struct btrfs_root
*reloc_root
;
4316 struct btrfs_trans_handle
*trans
;
4317 struct btrfs_key location
;
4321 mutex_lock(&root
->fs_info
->tree_reloc_mutex
);
4322 ret
= btrfs_find_dead_roots(root
, BTRFS_TREE_RELOC_OBJECTID
, NULL
);
4324 found
= !list_empty(&root
->fs_info
->dead_reloc_roots
);
4325 mutex_unlock(&root
->fs_info
->tree_reloc_mutex
);
4328 trans
= btrfs_start_transaction(root
, 1);
4330 ret
= btrfs_commit_transaction(trans
, root
);
4334 location
.objectid
= BTRFS_DATA_RELOC_TREE_OBJECTID
;
4335 location
.offset
= (u64
)-1;
4336 location
.type
= BTRFS_ROOT_ITEM_KEY
;
4338 reloc_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
4339 BUG_ON(!reloc_root
);
4340 btrfs_orphan_cleanup(reloc_root
);
4344 static int noinline
init_reloc_tree(struct btrfs_trans_handle
*trans
,
4345 struct btrfs_root
*root
)
4347 struct btrfs_root
*reloc_root
;
4348 struct extent_buffer
*eb
;
4349 struct btrfs_root_item
*root_item
;
4350 struct btrfs_key root_key
;
4353 BUG_ON(!root
->ref_cows
);
4354 if (root
->reloc_root
)
4357 root_item
= kmalloc(sizeof(*root_item
), GFP_NOFS
);
4360 ret
= btrfs_copy_root(trans
, root
, root
->commit_root
,
4361 &eb
, BTRFS_TREE_RELOC_OBJECTID
);
4364 root_key
.objectid
= BTRFS_TREE_RELOC_OBJECTID
;
4365 root_key
.offset
= root
->root_key
.objectid
;
4366 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
4368 memcpy(root_item
, &root
->root_item
, sizeof(root_item
));
4369 btrfs_set_root_refs(root_item
, 0);
4370 btrfs_set_root_bytenr(root_item
, eb
->start
);
4371 btrfs_set_root_level(root_item
, btrfs_header_level(eb
));
4372 btrfs_set_root_generation(root_item
, trans
->transid
);
4374 btrfs_tree_unlock(eb
);
4375 free_extent_buffer(eb
);
4377 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
,
4378 &root_key
, root_item
);
4382 reloc_root
= btrfs_read_fs_root_no_radix(root
->fs_info
->tree_root
,
4384 BUG_ON(!reloc_root
);
4385 reloc_root
->last_trans
= trans
->transid
;
4386 reloc_root
->commit_root
= NULL
;
4387 reloc_root
->ref_tree
= &root
->fs_info
->reloc_ref_tree
;
4389 root
->reloc_root
= reloc_root
;
4394 * Core function of space balance.
4396 * The idea is using reloc trees to relocate tree blocks in reference
4397 * counted roots. There is one reloc tree for each subvol, and all
4398 * reloc trees share same root key objectid. Reloc trees are snapshots
4399 * of the latest committed roots of subvols (root->commit_root).
4401 * To relocate a tree block referenced by a subvol, there are two steps.
4402 * COW the block through subvol's reloc tree, then update block pointer
4403 * in the subvol to point to the new block. Since all reloc trees share
4404 * same root key objectid, doing special handing for tree blocks owned
4405 * by them is easy. Once a tree block has been COWed in one reloc tree,
4406 * we can use the resulting new block directly when the same block is
4407 * required to COW again through other reloc trees. By this way, relocated
4408 * tree blocks are shared between reloc trees, so they are also shared
4411 static int noinline
relocate_one_path(struct btrfs_trans_handle
*trans
,
4412 struct btrfs_root
*root
,
4413 struct btrfs_path
*path
,
4414 struct btrfs_key
*first_key
,
4415 struct btrfs_ref_path
*ref_path
,
4416 struct btrfs_block_group_cache
*group
,
4417 struct inode
*reloc_inode
)
4419 struct btrfs_root
*reloc_root
;
4420 struct extent_buffer
*eb
= NULL
;
4421 struct btrfs_key
*keys
;
4425 int lowest_level
= 0;
4428 if (ref_path
->owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
)
4429 lowest_level
= ref_path
->owner_objectid
;
4431 if (!root
->ref_cows
) {
4432 path
->lowest_level
= lowest_level
;
4433 ret
= btrfs_search_slot(trans
, root
, first_key
, path
, 0, 1);
4435 path
->lowest_level
= 0;
4436 btrfs_release_path(root
, path
);
4440 mutex_lock(&root
->fs_info
->tree_reloc_mutex
);
4441 ret
= init_reloc_tree(trans
, root
);
4443 reloc_root
= root
->reloc_root
;
4445 shared_level
= ref_path
->shared_level
;
4446 ref_path
->shared_level
= BTRFS_MAX_LEVEL
- 1;
4448 keys
= ref_path
->node_keys
;
4449 nodes
= ref_path
->new_nodes
;
4450 memset(&keys
[shared_level
+ 1], 0,
4451 sizeof(*keys
) * (BTRFS_MAX_LEVEL
- shared_level
- 1));
4452 memset(&nodes
[shared_level
+ 1], 0,
4453 sizeof(*nodes
) * (BTRFS_MAX_LEVEL
- shared_level
- 1));
4455 if (nodes
[lowest_level
] == 0) {
4456 path
->lowest_level
= lowest_level
;
4457 ret
= btrfs_search_slot(trans
, reloc_root
, first_key
, path
,
4460 for (level
= lowest_level
; level
< BTRFS_MAX_LEVEL
; level
++) {
4461 eb
= path
->nodes
[level
];
4462 if (!eb
|| eb
== reloc_root
->node
)
4464 nodes
[level
] = eb
->start
;
4466 btrfs_item_key_to_cpu(eb
, &keys
[level
], 0);
4468 btrfs_node_key_to_cpu(eb
, &keys
[level
], 0);
4470 if (ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
4471 eb
= path
->nodes
[0];
4472 ret
= replace_extents_in_leaf(trans
, reloc_root
, eb
,
4473 group
, reloc_inode
);
4476 btrfs_release_path(reloc_root
, path
);
4478 ret
= btrfs_merge_path(trans
, reloc_root
, keys
, nodes
,
4484 * replace tree blocks in the fs tree with tree blocks in
4487 ret
= btrfs_merge_path(trans
, root
, keys
, nodes
, lowest_level
);
4490 if (ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
4491 ret
= btrfs_search_slot(trans
, reloc_root
, first_key
, path
,
4494 extent_buffer_get(path
->nodes
[0]);
4495 eb
= path
->nodes
[0];
4496 btrfs_release_path(reloc_root
, path
);
4497 ret
= invalidate_extent_cache(reloc_root
, eb
, group
, root
);
4499 free_extent_buffer(eb
);
4502 mutex_unlock(&root
->fs_info
->tree_reloc_mutex
);
4503 path
->lowest_level
= 0;
4507 static int noinline
relocate_tree_block(struct btrfs_trans_handle
*trans
,
4508 struct btrfs_root
*root
,
4509 struct btrfs_path
*path
,
4510 struct btrfs_key
*first_key
,
4511 struct btrfs_ref_path
*ref_path
)
4515 ret
= relocate_one_path(trans
, root
, path
, first_key
,
4516 ref_path
, NULL
, NULL
);
4519 if (root
== root
->fs_info
->extent_root
)
4520 btrfs_extent_post_op(trans
, root
);
4525 static int noinline
del_extent_zero(struct btrfs_trans_handle
*trans
,
4526 struct btrfs_root
*extent_root
,
4527 struct btrfs_path
*path
,
4528 struct btrfs_key
*extent_key
)
4532 ret
= btrfs_search_slot(trans
, extent_root
, extent_key
, path
, -1, 1);
4535 ret
= btrfs_del_item(trans
, extent_root
, path
);
4537 btrfs_release_path(extent_root
, path
);
4541 static struct btrfs_root noinline
*read_ref_root(struct btrfs_fs_info
*fs_info
,
4542 struct btrfs_ref_path
*ref_path
)
4544 struct btrfs_key root_key
;
4546 root_key
.objectid
= ref_path
->root_objectid
;
4547 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
4548 if (is_cowonly_root(ref_path
->root_objectid
))
4549 root_key
.offset
= 0;
4551 root_key
.offset
= (u64
)-1;
4553 return btrfs_read_fs_root_no_name(fs_info
, &root_key
);
4556 static int noinline
relocate_one_extent(struct btrfs_root
*extent_root
,
4557 struct btrfs_path
*path
,
4558 struct btrfs_key
*extent_key
,
4559 struct btrfs_block_group_cache
*group
,
4560 struct inode
*reloc_inode
, int pass
)
4562 struct btrfs_trans_handle
*trans
;
4563 struct btrfs_root
*found_root
;
4564 struct btrfs_ref_path
*ref_path
= NULL
;
4565 struct disk_extent
*new_extents
= NULL
;
4570 struct btrfs_key first_key
;
4574 trans
= btrfs_start_transaction(extent_root
, 1);
4577 if (extent_key
->objectid
== 0) {
4578 ret
= del_extent_zero(trans
, extent_root
, path
, extent_key
);
4582 ref_path
= kmalloc(sizeof(*ref_path
), GFP_NOFS
);
4588 for (loops
= 0; ; loops
++) {
4590 ret
= btrfs_first_ref_path(trans
, extent_root
, ref_path
,
4591 extent_key
->objectid
);
4593 ret
= btrfs_next_ref_path(trans
, extent_root
, ref_path
);
4600 if (ref_path
->root_objectid
== BTRFS_TREE_LOG_OBJECTID
||
4601 ref_path
->root_objectid
== BTRFS_TREE_RELOC_OBJECTID
)
4604 found_root
= read_ref_root(extent_root
->fs_info
, ref_path
);
4605 BUG_ON(!found_root
);
4607 * for reference counted tree, only process reference paths
4608 * rooted at the latest committed root.
4610 if (found_root
->ref_cows
&&
4611 ref_path
->root_generation
!= found_root
->root_key
.offset
)
4614 if (ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
4617 * copy data extents to new locations
4619 u64 group_start
= group
->key
.objectid
;
4620 ret
= relocate_data_extent(reloc_inode
,
4629 level
= ref_path
->owner_objectid
;
4632 if (prev_block
!= ref_path
->nodes
[level
]) {
4633 struct extent_buffer
*eb
;
4634 u64 block_start
= ref_path
->nodes
[level
];
4635 u64 block_size
= btrfs_level_size(found_root
, level
);
4637 eb
= read_tree_block(found_root
, block_start
,
4639 btrfs_tree_lock(eb
);
4640 BUG_ON(level
!= btrfs_header_level(eb
));
4643 btrfs_item_key_to_cpu(eb
, &first_key
, 0);
4645 btrfs_node_key_to_cpu(eb
, &first_key
, 0);
4647 btrfs_tree_unlock(eb
);
4648 free_extent_buffer(eb
);
4649 prev_block
= block_start
;
4652 if (ref_path
->owner_objectid
>= BTRFS_FIRST_FREE_OBJECTID
&&
4655 * use fallback method to process the remaining
4659 u64 group_start
= group
->key
.objectid
;
4660 new_extents
= kmalloc(sizeof(*new_extents
),
4663 ret
= get_new_locations(reloc_inode
,
4671 btrfs_record_root_in_trans(found_root
);
4672 ret
= replace_one_extent(trans
, found_root
,
4674 &first_key
, ref_path
,
4675 new_extents
, nr_extents
);
4681 btrfs_record_root_in_trans(found_root
);
4682 if (ref_path
->owner_objectid
< BTRFS_FIRST_FREE_OBJECTID
) {
4683 ret
= relocate_tree_block(trans
, found_root
, path
,
4684 &first_key
, ref_path
);
4687 * try to update data extent references while
4688 * keeping metadata shared between snapshots.
4690 ret
= relocate_one_path(trans
, found_root
, path
,
4691 &first_key
, ref_path
,
4692 group
, reloc_inode
);
4699 btrfs_end_transaction(trans
, extent_root
);
4705 static u64
update_block_group_flags(struct btrfs_root
*root
, u64 flags
)
4708 u64 stripped
= BTRFS_BLOCK_GROUP_RAID0
|
4709 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
;
4711 num_devices
= root
->fs_info
->fs_devices
->num_devices
;
4712 if (num_devices
== 1) {
4713 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
4714 stripped
= flags
& ~stripped
;
4716 /* turn raid0 into single device chunks */
4717 if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
4720 /* turn mirroring into duplication */
4721 if (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
4722 BTRFS_BLOCK_GROUP_RAID10
))
4723 return stripped
| BTRFS_BLOCK_GROUP_DUP
;
4726 /* they already had raid on here, just return */
4727 if (flags
& stripped
)
4730 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
4731 stripped
= flags
& ~stripped
;
4733 /* switch duplicated blocks with raid1 */
4734 if (flags
& BTRFS_BLOCK_GROUP_DUP
)
4735 return stripped
| BTRFS_BLOCK_GROUP_RAID1
;
4737 /* turn single device chunks into raid0 */
4738 return stripped
| BTRFS_BLOCK_GROUP_RAID0
;
4743 int __alloc_chunk_for_shrink(struct btrfs_root
*root
,
4744 struct btrfs_block_group_cache
*shrink_block_group
,
4747 struct btrfs_trans_handle
*trans
;
4748 u64 new_alloc_flags
;
4751 spin_lock(&shrink_block_group
->lock
);
4752 if (btrfs_block_group_used(&shrink_block_group
->item
) > 0) {
4753 spin_unlock(&shrink_block_group
->lock
);
4755 trans
= btrfs_start_transaction(root
, 1);
4756 spin_lock(&shrink_block_group
->lock
);
4758 new_alloc_flags
= update_block_group_flags(root
,
4759 shrink_block_group
->flags
);
4760 if (new_alloc_flags
!= shrink_block_group
->flags
) {
4762 btrfs_block_group_used(&shrink_block_group
->item
);
4764 calc
= shrink_block_group
->key
.offset
;
4766 spin_unlock(&shrink_block_group
->lock
);
4768 do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
4769 calc
+ 2 * 1024 * 1024, new_alloc_flags
, force
);
4771 btrfs_end_transaction(trans
, root
);
4773 spin_unlock(&shrink_block_group
->lock
);
4777 static int __insert_orphan_inode(struct btrfs_trans_handle
*trans
,
4778 struct btrfs_root
*root
,
4779 u64 objectid
, u64 size
)
4781 struct btrfs_path
*path
;
4782 struct btrfs_inode_item
*item
;
4783 struct extent_buffer
*leaf
;
4786 path
= btrfs_alloc_path();
4790 ret
= btrfs_insert_empty_inode(trans
, root
, path
, objectid
);
4794 leaf
= path
->nodes
[0];
4795 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_inode_item
);
4796 memset_extent_buffer(leaf
, 0, (unsigned long)item
, sizeof(*item
));
4797 btrfs_set_inode_generation(leaf
, item
, 1);
4798 btrfs_set_inode_size(leaf
, item
, size
);
4799 btrfs_set_inode_mode(leaf
, item
, S_IFREG
| 0600);
4800 btrfs_set_inode_flags(leaf
, item
, BTRFS_INODE_NODATASUM
|
4801 BTRFS_INODE_NOCOMPRESS
);
4802 btrfs_mark_buffer_dirty(leaf
);
4803 btrfs_release_path(root
, path
);
4805 btrfs_free_path(path
);
4809 static struct inode noinline
*create_reloc_inode(struct btrfs_fs_info
*fs_info
,
4810 struct btrfs_block_group_cache
*group
)
4812 struct inode
*inode
= NULL
;
4813 struct btrfs_trans_handle
*trans
;
4814 struct btrfs_root
*root
;
4815 struct btrfs_key root_key
;
4816 u64 objectid
= BTRFS_FIRST_FREE_OBJECTID
;
4819 root_key
.objectid
= BTRFS_DATA_RELOC_TREE_OBJECTID
;
4820 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
4821 root_key
.offset
= (u64
)-1;
4822 root
= btrfs_read_fs_root_no_name(fs_info
, &root_key
);
4824 return ERR_CAST(root
);
4826 trans
= btrfs_start_transaction(root
, 1);
4829 err
= btrfs_find_free_objectid(trans
, root
, objectid
, &objectid
);
4833 err
= __insert_orphan_inode(trans
, root
, objectid
, group
->key
.offset
);
4836 err
= btrfs_insert_file_extent(trans
, root
, objectid
, 0, 0, 0,
4837 group
->key
.offset
, 0, group
->key
.offset
,
4841 inode
= btrfs_iget_locked(root
->fs_info
->sb
, objectid
, root
);
4842 if (inode
->i_state
& I_NEW
) {
4843 BTRFS_I(inode
)->root
= root
;
4844 BTRFS_I(inode
)->location
.objectid
= objectid
;
4845 BTRFS_I(inode
)->location
.type
= BTRFS_INODE_ITEM_KEY
;
4846 BTRFS_I(inode
)->location
.offset
= 0;
4847 btrfs_read_locked_inode(inode
);
4848 unlock_new_inode(inode
);
4849 BUG_ON(is_bad_inode(inode
));
4854 err
= btrfs_orphan_add(trans
, inode
);
4856 btrfs_end_transaction(trans
, root
);
4860 inode
= ERR_PTR(err
);
4865 int btrfs_relocate_block_group(struct btrfs_root
*root
, u64 group_start
)
4867 struct btrfs_trans_handle
*trans
;
4868 struct btrfs_path
*path
;
4869 struct btrfs_fs_info
*info
= root
->fs_info
;
4870 struct extent_buffer
*leaf
;
4871 struct inode
*reloc_inode
;
4872 struct btrfs_block_group_cache
*block_group
;
4873 struct btrfs_key key
;
4882 root
= root
->fs_info
->extent_root
;
4884 block_group
= btrfs_lookup_block_group(info
, group_start
);
4885 BUG_ON(!block_group
);
4887 printk("btrfs relocating block group %llu flags %llu\n",
4888 (unsigned long long)block_group
->key
.objectid
,
4889 (unsigned long long)block_group
->flags
);
4891 path
= btrfs_alloc_path();
4894 reloc_inode
= create_reloc_inode(info
, block_group
);
4895 BUG_ON(IS_ERR(reloc_inode
));
4897 __alloc_chunk_for_shrink(root
, block_group
, 1);
4898 block_group
->ro
= 1;
4899 block_group
->space_info
->total_bytes
-= block_group
->key
.offset
;
4901 btrfs_start_delalloc_inodes(info
->tree_root
);
4902 btrfs_wait_ordered_extents(info
->tree_root
, 0);
4907 key
.objectid
= block_group
->key
.objectid
;
4910 cur_byte
= key
.objectid
;
4912 trans
= btrfs_start_transaction(info
->tree_root
, 1);
4913 btrfs_commit_transaction(trans
, info
->tree_root
);
4915 mutex_lock(&root
->fs_info
->cleaner_mutex
);
4916 btrfs_clean_old_snapshots(info
->tree_root
);
4917 btrfs_remove_leaf_refs(info
->tree_root
, (u64
)-1, 1);
4918 mutex_unlock(&root
->fs_info
->cleaner_mutex
);
4921 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4925 leaf
= path
->nodes
[0];
4926 nritems
= btrfs_header_nritems(leaf
);
4927 if (path
->slots
[0] >= nritems
) {
4928 ret
= btrfs_next_leaf(root
, path
);
4935 leaf
= path
->nodes
[0];
4936 nritems
= btrfs_header_nritems(leaf
);
4939 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
4941 if (key
.objectid
>= block_group
->key
.objectid
+
4942 block_group
->key
.offset
)
4945 if (progress
&& need_resched()) {
4946 btrfs_release_path(root
, path
);
4953 if (btrfs_key_type(&key
) != BTRFS_EXTENT_ITEM_KEY
||
4954 key
.objectid
+ key
.offset
<= cur_byte
) {
4960 cur_byte
= key
.objectid
+ key
.offset
;
4961 btrfs_release_path(root
, path
);
4963 __alloc_chunk_for_shrink(root
, block_group
, 0);
4964 ret
= relocate_one_extent(root
, path
, &key
, block_group
,
4970 key
.objectid
= cur_byte
;
4975 btrfs_release_path(root
, path
);
4978 btrfs_wait_ordered_range(reloc_inode
, 0, (u64
)-1);
4979 invalidate_mapping_pages(reloc_inode
->i_mapping
, 0, -1);
4980 WARN_ON(reloc_inode
->i_mapping
->nrpages
);
4983 if (total_found
> 0) {
4984 printk("btrfs found %llu extents in pass %d\n",
4985 (unsigned long long)total_found
, pass
);
4987 if (total_found
== skipped
&& pass
> 2) {
4989 reloc_inode
= create_reloc_inode(info
, block_group
);
4995 /* delete reloc_inode */
4998 /* unpin extents in this range */
4999 trans
= btrfs_start_transaction(info
->tree_root
, 1);
5000 btrfs_commit_transaction(trans
, info
->tree_root
);
5002 spin_lock(&block_group
->lock
);
5003 WARN_ON(block_group
->pinned
> 0);
5004 WARN_ON(block_group
->reserved
> 0);
5005 WARN_ON(btrfs_block_group_used(&block_group
->item
) > 0);
5006 spin_unlock(&block_group
->lock
);
5009 btrfs_free_path(path
);
5013 int find_first_block_group(struct btrfs_root
*root
, struct btrfs_path
*path
,
5014 struct btrfs_key
*key
)
5017 struct btrfs_key found_key
;
5018 struct extent_buffer
*leaf
;
5021 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
5026 slot
= path
->slots
[0];
5027 leaf
= path
->nodes
[0];
5028 if (slot
>= btrfs_header_nritems(leaf
)) {
5029 ret
= btrfs_next_leaf(root
, path
);
5036 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5038 if (found_key
.objectid
>= key
->objectid
&&
5039 found_key
.type
== BTRFS_BLOCK_GROUP_ITEM_KEY
) {
5050 int btrfs_free_block_groups(struct btrfs_fs_info
*info
)
5052 struct btrfs_block_group_cache
*block_group
;
5055 spin_lock(&info
->block_group_cache_lock
);
5056 while ((n
= rb_last(&info
->block_group_cache_tree
)) != NULL
) {
5057 block_group
= rb_entry(n
, struct btrfs_block_group_cache
,
5059 rb_erase(&block_group
->cache_node
,
5060 &info
->block_group_cache_tree
);
5061 spin_unlock(&info
->block_group_cache_lock
);
5063 btrfs_remove_free_space_cache(block_group
);
5064 down_write(&block_group
->space_info
->groups_sem
);
5065 list_del(&block_group
->list
);
5066 up_write(&block_group
->space_info
->groups_sem
);
5069 spin_lock(&info
->block_group_cache_lock
);
5071 spin_unlock(&info
->block_group_cache_lock
);
5075 int btrfs_read_block_groups(struct btrfs_root
*root
)
5077 struct btrfs_path
*path
;
5079 struct btrfs_block_group_cache
*cache
;
5080 struct btrfs_fs_info
*info
= root
->fs_info
;
5081 struct btrfs_space_info
*space_info
;
5082 struct btrfs_key key
;
5083 struct btrfs_key found_key
;
5084 struct extent_buffer
*leaf
;
5086 root
= info
->extent_root
;
5089 btrfs_set_key_type(&key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
5090 path
= btrfs_alloc_path();
5095 ret
= find_first_block_group(root
, path
, &key
);
5103 leaf
= path
->nodes
[0];
5104 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5105 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
5111 spin_lock_init(&cache
->lock
);
5112 mutex_init(&cache
->alloc_mutex
);
5113 INIT_LIST_HEAD(&cache
->list
);
5114 read_extent_buffer(leaf
, &cache
->item
,
5115 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
5116 sizeof(cache
->item
));
5117 memcpy(&cache
->key
, &found_key
, sizeof(found_key
));
5119 key
.objectid
= found_key
.objectid
+ found_key
.offset
;
5120 btrfs_release_path(root
, path
);
5121 cache
->flags
= btrfs_block_group_flags(&cache
->item
);
5123 ret
= update_space_info(info
, cache
->flags
, found_key
.offset
,
5124 btrfs_block_group_used(&cache
->item
),
5127 cache
->space_info
= space_info
;
5128 down_write(&space_info
->groups_sem
);
5129 list_add_tail(&cache
->list
, &space_info
->block_groups
);
5130 up_write(&space_info
->groups_sem
);
5132 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
5135 set_avail_alloc_bits(root
->fs_info
, cache
->flags
);
5139 btrfs_free_path(path
);
5143 int btrfs_make_block_group(struct btrfs_trans_handle
*trans
,
5144 struct btrfs_root
*root
, u64 bytes_used
,
5145 u64 type
, u64 chunk_objectid
, u64 chunk_offset
,
5149 struct btrfs_root
*extent_root
;
5150 struct btrfs_block_group_cache
*cache
;
5152 extent_root
= root
->fs_info
->extent_root
;
5154 root
->fs_info
->last_trans_new_blockgroup
= trans
->transid
;
5156 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
5160 cache
->key
.objectid
= chunk_offset
;
5161 cache
->key
.offset
= size
;
5162 spin_lock_init(&cache
->lock
);
5163 mutex_init(&cache
->alloc_mutex
);
5164 INIT_LIST_HEAD(&cache
->list
);
5165 btrfs_set_key_type(&cache
->key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
5167 btrfs_set_block_group_used(&cache
->item
, bytes_used
);
5168 btrfs_set_block_group_chunk_objectid(&cache
->item
, chunk_objectid
);
5169 cache
->flags
= type
;
5170 btrfs_set_block_group_flags(&cache
->item
, type
);
5172 ret
= update_space_info(root
->fs_info
, cache
->flags
, size
, bytes_used
,
5173 &cache
->space_info
);
5175 down_write(&cache
->space_info
->groups_sem
);
5176 list_add_tail(&cache
->list
, &cache
->space_info
->block_groups
);
5177 up_write(&cache
->space_info
->groups_sem
);
5179 ret
= btrfs_add_block_group_cache(root
->fs_info
, cache
);
5182 ret
= btrfs_insert_item(trans
, extent_root
, &cache
->key
, &cache
->item
,
5183 sizeof(cache
->item
));
5186 finish_current_insert(trans
, extent_root
, 0);
5187 ret
= del_pending_extents(trans
, extent_root
, 0);
5189 set_avail_alloc_bits(extent_root
->fs_info
, type
);
5194 int btrfs_remove_block_group(struct btrfs_trans_handle
*trans
,
5195 struct btrfs_root
*root
, u64 group_start
)
5197 struct btrfs_path
*path
;
5198 struct btrfs_block_group_cache
*block_group
;
5199 struct btrfs_key key
;
5202 root
= root
->fs_info
->extent_root
;
5204 block_group
= btrfs_lookup_block_group(root
->fs_info
, group_start
);
5205 BUG_ON(!block_group
);
5207 memcpy(&key
, &block_group
->key
, sizeof(key
));
5209 path
= btrfs_alloc_path();
5212 btrfs_remove_free_space_cache(block_group
);
5213 rb_erase(&block_group
->cache_node
,
5214 &root
->fs_info
->block_group_cache_tree
);
5215 down_write(&block_group
->space_info
->groups_sem
);
5216 list_del(&block_group
->list
);
5217 up_write(&block_group
->space_info
->groups_sem
);
5220 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
5221 kfree(shrink_block_group);
5224 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
5230 ret
= btrfs_del_item(trans
, root
, path
);
5232 btrfs_free_path(path
);