2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
18 #include <linux/sched.h>
19 #include <linux/pagemap.h>
20 #include <linux/writeback.h>
21 #include <linux/blkdev.h>
22 #include <linux/sort.h>
23 #include <linux/rcupdate.h>
24 #include <linux/kthread.h>
25 #include <linux/slab.h>
30 #include "print-tree.h"
31 #include "transaction.h"
34 #include "free-space-cache.h"
36 static int update_block_group(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 u64 bytenr, u64 num_bytes, int alloc,
40 static int update_reserved_extents(struct btrfs_block_group_cache *cache,
41 u64 num_bytes, int reserve);
42 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
43 struct btrfs_root *root,
44 u64 bytenr, u64 num_bytes, u64 parent,
45 u64 root_objectid, u64 owner_objectid,
46 u64 owner_offset, int refs_to_drop,
47 struct btrfs_delayed_extent_op *extra_op);
48 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
49 struct extent_buffer *leaf,
50 struct btrfs_extent_item *ei);
51 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
52 struct btrfs_root *root,
53 u64 parent, u64 root_objectid,
54 u64 flags, u64 owner, u64 offset,
55 struct btrfs_key *ins, int ref_mod);
56 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
57 struct btrfs_root *root,
58 u64 parent, u64 root_objectid,
59 u64 flags, struct btrfs_disk_key *key,
60 int level, struct btrfs_key *ins);
61 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
62 struct btrfs_root *extent_root, u64 alloc_bytes,
63 u64 flags, int force);
64 static int pin_down_bytes(struct btrfs_trans_handle *trans,
65 struct btrfs_root *root,
66 struct btrfs_path *path,
67 u64 bytenr, u64 num_bytes,
68 int is_data, int reserved,
69 struct extent_buffer **must_clean);
70 static int find_next_key(struct btrfs_path *path, int level,
71 struct btrfs_key *key);
72 static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
73 int dump_block_groups);
74 static int maybe_allocate_chunk(struct btrfs_trans_handle *trans,
75 struct btrfs_root *root,
76 struct btrfs_space_info *sinfo, u64 num_bytes);
77 static int shrink_delalloc(struct btrfs_trans_handle *trans,
78 struct btrfs_root *root,
79 struct btrfs_space_info *sinfo, u64 to_reclaim);
82 block_group_cache_done(struct btrfs_block_group_cache *cache)
85 return cache->cached == BTRFS_CACHE_FINISHED;
88 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
90 return (cache->flags & bits) == bits;
93 void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
95 atomic_inc(&cache->count);
98 void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
100 if (atomic_dec_and_test(&cache->count))
105 * this adds the block group to the fs_info rb tree for the block group
108 static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
109 struct btrfs_block_group_cache *block_group)
112 struct rb_node *parent = NULL;
113 struct btrfs_block_group_cache *cache;
115 spin_lock(&info->block_group_cache_lock);
116 p = &info->block_group_cache_tree.rb_node;
120 cache = rb_entry(parent, struct btrfs_block_group_cache,
122 if (block_group->key.objectid < cache->key.objectid) {
124 } else if (block_group->key.objectid > cache->key.objectid) {
127 spin_unlock(&info->block_group_cache_lock);
132 rb_link_node(&block_group->cache_node, parent, p);
133 rb_insert_color(&block_group->cache_node,
134 &info->block_group_cache_tree);
135 spin_unlock(&info->block_group_cache_lock);
141 * This will return the block group at or after bytenr if contains is 0, else
142 * it will return the block group that contains the bytenr
144 static struct btrfs_block_group_cache *
145 block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
148 struct btrfs_block_group_cache *cache, *ret = NULL;
152 spin_lock(&info->block_group_cache_lock);
153 n = info->block_group_cache_tree.rb_node;
156 cache = rb_entry(n, struct btrfs_block_group_cache,
158 end = cache->key.objectid + cache->key.offset - 1;
159 start = cache->key.objectid;
161 if (bytenr < start) {
162 if (!contains && (!ret || start < ret->key.objectid))
165 } else if (bytenr > start) {
166 if (contains && bytenr <= end) {
177 btrfs_get_block_group(ret);
178 spin_unlock(&info->block_group_cache_lock);
183 static int add_excluded_extent(struct btrfs_root *root,
184 u64 start, u64 num_bytes)
186 u64 end = start + num_bytes - 1;
187 set_extent_bits(&root->fs_info->freed_extents[0],
188 start, end, EXTENT_UPTODATE, GFP_NOFS);
189 set_extent_bits(&root->fs_info->freed_extents[1],
190 start, end, EXTENT_UPTODATE, GFP_NOFS);
194 static void free_excluded_extents(struct btrfs_root *root,
195 struct btrfs_block_group_cache *cache)
199 start = cache->key.objectid;
200 end = start + cache->key.offset - 1;
202 clear_extent_bits(&root->fs_info->freed_extents[0],
203 start, end, EXTENT_UPTODATE, GFP_NOFS);
204 clear_extent_bits(&root->fs_info->freed_extents[1],
205 start, end, EXTENT_UPTODATE, GFP_NOFS);
208 static int exclude_super_stripes(struct btrfs_root *root,
209 struct btrfs_block_group_cache *cache)
216 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
217 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
218 cache->bytes_super += stripe_len;
219 ret = add_excluded_extent(root, cache->key.objectid,
224 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
225 bytenr = btrfs_sb_offset(i);
226 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
227 cache->key.objectid, bytenr,
228 0, &logical, &nr, &stripe_len);
232 cache->bytes_super += stripe_len;
233 ret = add_excluded_extent(root, logical[nr],
243 static struct btrfs_caching_control *
244 get_caching_control(struct btrfs_block_group_cache *cache)
246 struct btrfs_caching_control *ctl;
248 spin_lock(&cache->lock);
249 if (cache->cached != BTRFS_CACHE_STARTED) {
250 spin_unlock(&cache->lock);
254 ctl = cache->caching_ctl;
255 atomic_inc(&ctl->count);
256 spin_unlock(&cache->lock);
260 static void put_caching_control(struct btrfs_caching_control *ctl)
262 if (atomic_dec_and_test(&ctl->count))
267 * this is only called by cache_block_group, since we could have freed extents
268 * we need to check the pinned_extents for any extents that can't be used yet
269 * since their free space will be released as soon as the transaction commits.
271 static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
272 struct btrfs_fs_info *info, u64 start, u64 end)
274 u64 extent_start, extent_end, size, total_added = 0;
277 while (start < end) {
278 ret = find_first_extent_bit(info->pinned_extents, start,
279 &extent_start, &extent_end,
280 EXTENT_DIRTY | EXTENT_UPTODATE);
284 if (extent_start <= start) {
285 start = extent_end + 1;
286 } else if (extent_start > start && extent_start < end) {
287 size = extent_start - start;
289 ret = btrfs_add_free_space(block_group, start,
292 start = extent_end + 1;
301 ret = btrfs_add_free_space(block_group, start, size);
308 static int caching_kthread(void *data)
310 struct btrfs_block_group_cache *block_group = data;
311 struct btrfs_fs_info *fs_info = block_group->fs_info;
312 struct btrfs_caching_control *caching_ctl = block_group->caching_ctl;
313 struct btrfs_root *extent_root = fs_info->extent_root;
314 struct btrfs_path *path;
315 struct extent_buffer *leaf;
316 struct btrfs_key key;
322 path = btrfs_alloc_path();
326 exclude_super_stripes(extent_root, block_group);
327 spin_lock(&block_group->space_info->lock);
328 block_group->space_info->bytes_super += block_group->bytes_super;
329 spin_unlock(&block_group->space_info->lock);
331 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
334 * We don't want to deadlock with somebody trying to allocate a new
335 * extent for the extent root while also trying to search the extent
336 * root to add free space. So we skip locking and search the commit
337 * root, since its read-only
339 path->skip_locking = 1;
340 path->search_commit_root = 1;
345 key.type = BTRFS_EXTENT_ITEM_KEY;
347 mutex_lock(&caching_ctl->mutex);
348 /* need to make sure the commit_root doesn't disappear */
349 down_read(&fs_info->extent_commit_sem);
351 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
355 leaf = path->nodes[0];
356 nritems = btrfs_header_nritems(leaf);
360 if (fs_info->closing > 1) {
365 if (path->slots[0] < nritems) {
366 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
368 ret = find_next_key(path, 0, &key);
372 caching_ctl->progress = last;
373 btrfs_release_path(extent_root, path);
374 up_read(&fs_info->extent_commit_sem);
375 mutex_unlock(&caching_ctl->mutex);
376 if (btrfs_transaction_in_commit(fs_info))
383 if (key.objectid < block_group->key.objectid) {
388 if (key.objectid >= block_group->key.objectid +
389 block_group->key.offset)
392 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
393 total_found += add_new_free_space(block_group,
396 last = key.objectid + key.offset;
398 if (total_found > (1024 * 1024 * 2)) {
400 wake_up(&caching_ctl->wait);
407 total_found += add_new_free_space(block_group, fs_info, last,
408 block_group->key.objectid +
409 block_group->key.offset);
410 caching_ctl->progress = (u64)-1;
412 spin_lock(&block_group->lock);
413 block_group->caching_ctl = NULL;
414 block_group->cached = BTRFS_CACHE_FINISHED;
415 spin_unlock(&block_group->lock);
418 btrfs_free_path(path);
419 up_read(&fs_info->extent_commit_sem);
421 free_excluded_extents(extent_root, block_group);
423 mutex_unlock(&caching_ctl->mutex);
424 wake_up(&caching_ctl->wait);
426 put_caching_control(caching_ctl);
427 atomic_dec(&block_group->space_info->caching_threads);
428 btrfs_put_block_group(block_group);
433 static int cache_block_group(struct btrfs_block_group_cache *cache)
435 struct btrfs_fs_info *fs_info = cache->fs_info;
436 struct btrfs_caching_control *caching_ctl;
437 struct task_struct *tsk;
441 if (cache->cached != BTRFS_CACHE_NO)
444 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL);
445 BUG_ON(!caching_ctl);
447 INIT_LIST_HEAD(&caching_ctl->list);
448 mutex_init(&caching_ctl->mutex);
449 init_waitqueue_head(&caching_ctl->wait);
450 caching_ctl->block_group = cache;
451 caching_ctl->progress = cache->key.objectid;
452 /* one for caching kthread, one for caching block group list */
453 atomic_set(&caching_ctl->count, 2);
455 spin_lock(&cache->lock);
456 if (cache->cached != BTRFS_CACHE_NO) {
457 spin_unlock(&cache->lock);
461 cache->caching_ctl = caching_ctl;
462 cache->cached = BTRFS_CACHE_STARTED;
463 spin_unlock(&cache->lock);
465 down_write(&fs_info->extent_commit_sem);
466 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
467 up_write(&fs_info->extent_commit_sem);
469 atomic_inc(&cache->space_info->caching_threads);
470 btrfs_get_block_group(cache);
472 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
473 cache->key.objectid);
476 printk(KERN_ERR "error running thread %d\n", ret);
484 * return the block group that starts at or after bytenr
486 static struct btrfs_block_group_cache *
487 btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
489 struct btrfs_block_group_cache *cache;
491 cache = block_group_cache_tree_search(info, bytenr, 0);
497 * return the block group that contains the given bytenr
499 struct btrfs_block_group_cache *btrfs_lookup_block_group(
500 struct btrfs_fs_info *info,
503 struct btrfs_block_group_cache *cache;
505 cache = block_group_cache_tree_search(info, bytenr, 1);
510 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
513 struct list_head *head = &info->space_info;
514 struct btrfs_space_info *found;
516 flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM |
517 BTRFS_BLOCK_GROUP_METADATA;
520 list_for_each_entry_rcu(found, head, list) {
521 if (found->flags == flags) {
531 * after adding space to the filesystem, we need to clear the full flags
532 * on all the space infos.
534 void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
536 struct list_head *head = &info->space_info;
537 struct btrfs_space_info *found;
540 list_for_each_entry_rcu(found, head, list)
545 static u64 div_factor(u64 num, int factor)
554 u64 btrfs_find_block_group(struct btrfs_root *root,
555 u64 search_start, u64 search_hint, int owner)
557 struct btrfs_block_group_cache *cache;
559 u64 last = max(search_hint, search_start);
566 cache = btrfs_lookup_first_block_group(root->fs_info, last);
570 spin_lock(&cache->lock);
571 last = cache->key.objectid + cache->key.offset;
572 used = btrfs_block_group_used(&cache->item);
574 if ((full_search || !cache->ro) &&
575 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
576 if (used + cache->pinned + cache->reserved <
577 div_factor(cache->key.offset, factor)) {
578 group_start = cache->key.objectid;
579 spin_unlock(&cache->lock);
580 btrfs_put_block_group(cache);
584 spin_unlock(&cache->lock);
585 btrfs_put_block_group(cache);
593 if (!full_search && factor < 10) {
603 /* simple helper to search for an existing extent at a given offset */
604 int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
607 struct btrfs_key key;
608 struct btrfs_path *path;
610 path = btrfs_alloc_path();
612 key.objectid = start;
614 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
615 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
617 btrfs_free_path(path);
622 * Back reference rules. Back refs have three main goals:
624 * 1) differentiate between all holders of references to an extent so that
625 * when a reference is dropped we can make sure it was a valid reference
626 * before freeing the extent.
628 * 2) Provide enough information to quickly find the holders of an extent
629 * if we notice a given block is corrupted or bad.
631 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
632 * maintenance. This is actually the same as #2, but with a slightly
633 * different use case.
635 * There are two kinds of back refs. The implicit back refs is optimized
636 * for pointers in non-shared tree blocks. For a given pointer in a block,
637 * back refs of this kind provide information about the block's owner tree
638 * and the pointer's key. These information allow us to find the block by
639 * b-tree searching. The full back refs is for pointers in tree blocks not
640 * referenced by their owner trees. The location of tree block is recorded
641 * in the back refs. Actually the full back refs is generic, and can be
642 * used in all cases the implicit back refs is used. The major shortcoming
643 * of the full back refs is its overhead. Every time a tree block gets
644 * COWed, we have to update back refs entry for all pointers in it.
646 * For a newly allocated tree block, we use implicit back refs for
647 * pointers in it. This means most tree related operations only involve
648 * implicit back refs. For a tree block created in old transaction, the
649 * only way to drop a reference to it is COW it. So we can detect the
650 * event that tree block loses its owner tree's reference and do the
651 * back refs conversion.
653 * When a tree block is COW'd through a tree, there are four cases:
655 * The reference count of the block is one and the tree is the block's
656 * owner tree. Nothing to do in this case.
658 * The reference count of the block is one and the tree is not the
659 * block's owner tree. In this case, full back refs is used for pointers
660 * in the block. Remove these full back refs, add implicit back refs for
661 * every pointers in the new block.
663 * The reference count of the block is greater than one and the tree is
664 * the block's owner tree. In this case, implicit back refs is used for
665 * pointers in the block. Add full back refs for every pointers in the
666 * block, increase lower level extents' reference counts. The original
667 * implicit back refs are entailed to the new block.
669 * The reference count of the block is greater than one and the tree is
670 * not the block's owner tree. Add implicit back refs for every pointer in
671 * the new block, increase lower level extents' reference count.
673 * Back Reference Key composing:
675 * The key objectid corresponds to the first byte in the extent,
676 * The key type is used to differentiate between types of back refs.
677 * There are different meanings of the key offset for different types
680 * File extents can be referenced by:
682 * - multiple snapshots, subvolumes, or different generations in one subvol
683 * - different files inside a single subvolume
684 * - different offsets inside a file (bookend extents in file.c)
686 * The extent ref structure for the implicit back refs has fields for:
688 * - Objectid of the subvolume root
689 * - objectid of the file holding the reference
690 * - original offset in the file
691 * - how many bookend extents
693 * The key offset for the implicit back refs is hash of the first
696 * The extent ref structure for the full back refs has field for:
698 * - number of pointers in the tree leaf
700 * The key offset for the implicit back refs is the first byte of
703 * When a file extent is allocated, The implicit back refs is used.
704 * the fields are filled in:
706 * (root_key.objectid, inode objectid, offset in file, 1)
708 * When a file extent is removed file truncation, we find the
709 * corresponding implicit back refs and check the following fields:
711 * (btrfs_header_owner(leaf), inode objectid, offset in file)
713 * Btree extents can be referenced by:
715 * - Different subvolumes
717 * Both the implicit back refs and the full back refs for tree blocks
718 * only consist of key. The key offset for the implicit back refs is
719 * objectid of block's owner tree. The key offset for the full back refs
720 * is the first byte of parent block.
722 * When implicit back refs is used, information about the lowest key and
723 * level of the tree block are required. These information are stored in
724 * tree block info structure.
727 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
728 static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
729 struct btrfs_root *root,
730 struct btrfs_path *path,
731 u64 owner, u32 extra_size)
733 struct btrfs_extent_item *item;
734 struct btrfs_extent_item_v0 *ei0;
735 struct btrfs_extent_ref_v0 *ref0;
736 struct btrfs_tree_block_info *bi;
737 struct extent_buffer *leaf;
738 struct btrfs_key key;
739 struct btrfs_key found_key;
740 u32 new_size = sizeof(*item);
744 leaf = path->nodes[0];
745 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
747 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
748 ei0 = btrfs_item_ptr(leaf, path->slots[0],
749 struct btrfs_extent_item_v0);
750 refs = btrfs_extent_refs_v0(leaf, ei0);
752 if (owner == (u64)-1) {
754 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
755 ret = btrfs_next_leaf(root, path);
759 leaf = path->nodes[0];
761 btrfs_item_key_to_cpu(leaf, &found_key,
763 BUG_ON(key.objectid != found_key.objectid);
764 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
768 ref0 = btrfs_item_ptr(leaf, path->slots[0],
769 struct btrfs_extent_ref_v0);
770 owner = btrfs_ref_objectid_v0(leaf, ref0);
774 btrfs_release_path(root, path);
776 if (owner < BTRFS_FIRST_FREE_OBJECTID)
777 new_size += sizeof(*bi);
779 new_size -= sizeof(*ei0);
780 ret = btrfs_search_slot(trans, root, &key, path,
781 new_size + extra_size, 1);
786 ret = btrfs_extend_item(trans, root, path, new_size);
789 leaf = path->nodes[0];
790 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
791 btrfs_set_extent_refs(leaf, item, refs);
792 /* FIXME: get real generation */
793 btrfs_set_extent_generation(leaf, item, 0);
794 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
795 btrfs_set_extent_flags(leaf, item,
796 BTRFS_EXTENT_FLAG_TREE_BLOCK |
797 BTRFS_BLOCK_FLAG_FULL_BACKREF);
798 bi = (struct btrfs_tree_block_info *)(item + 1);
799 /* FIXME: get first key of the block */
800 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
801 btrfs_set_tree_block_level(leaf, bi, (int)owner);
803 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
805 btrfs_mark_buffer_dirty(leaf);
810 static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
812 u32 high_crc = ~(u32)0;
813 u32 low_crc = ~(u32)0;
816 lenum = cpu_to_le64(root_objectid);
817 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
818 lenum = cpu_to_le64(owner);
819 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
820 lenum = cpu_to_le64(offset);
821 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
823 return ((u64)high_crc << 31) ^ (u64)low_crc;
826 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
827 struct btrfs_extent_data_ref *ref)
829 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
830 btrfs_extent_data_ref_objectid(leaf, ref),
831 btrfs_extent_data_ref_offset(leaf, ref));
834 static int match_extent_data_ref(struct extent_buffer *leaf,
835 struct btrfs_extent_data_ref *ref,
836 u64 root_objectid, u64 owner, u64 offset)
838 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
839 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
840 btrfs_extent_data_ref_offset(leaf, ref) != offset)
845 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
846 struct btrfs_root *root,
847 struct btrfs_path *path,
848 u64 bytenr, u64 parent,
850 u64 owner, u64 offset)
852 struct btrfs_key key;
853 struct btrfs_extent_data_ref *ref;
854 struct extent_buffer *leaf;
860 key.objectid = bytenr;
862 key.type = BTRFS_SHARED_DATA_REF_KEY;
865 key.type = BTRFS_EXTENT_DATA_REF_KEY;
866 key.offset = hash_extent_data_ref(root_objectid,
871 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
880 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
881 key.type = BTRFS_EXTENT_REF_V0_KEY;
882 btrfs_release_path(root, path);
883 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
894 leaf = path->nodes[0];
895 nritems = btrfs_header_nritems(leaf);
897 if (path->slots[0] >= nritems) {
898 ret = btrfs_next_leaf(root, path);
904 leaf = path->nodes[0];
905 nritems = btrfs_header_nritems(leaf);
909 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
910 if (key.objectid != bytenr ||
911 key.type != BTRFS_EXTENT_DATA_REF_KEY)
914 ref = btrfs_item_ptr(leaf, path->slots[0],
915 struct btrfs_extent_data_ref);
917 if (match_extent_data_ref(leaf, ref, root_objectid,
920 btrfs_release_path(root, path);
932 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
933 struct btrfs_root *root,
934 struct btrfs_path *path,
935 u64 bytenr, u64 parent,
936 u64 root_objectid, u64 owner,
937 u64 offset, int refs_to_add)
939 struct btrfs_key key;
940 struct extent_buffer *leaf;
945 key.objectid = bytenr;
947 key.type = BTRFS_SHARED_DATA_REF_KEY;
949 size = sizeof(struct btrfs_shared_data_ref);
951 key.type = BTRFS_EXTENT_DATA_REF_KEY;
952 key.offset = hash_extent_data_ref(root_objectid,
954 size = sizeof(struct btrfs_extent_data_ref);
957 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
958 if (ret && ret != -EEXIST)
961 leaf = path->nodes[0];
963 struct btrfs_shared_data_ref *ref;
964 ref = btrfs_item_ptr(leaf, path->slots[0],
965 struct btrfs_shared_data_ref);
967 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
969 num_refs = btrfs_shared_data_ref_count(leaf, ref);
970 num_refs += refs_to_add;
971 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
974 struct btrfs_extent_data_ref *ref;
975 while (ret == -EEXIST) {
976 ref = btrfs_item_ptr(leaf, path->slots[0],
977 struct btrfs_extent_data_ref);
978 if (match_extent_data_ref(leaf, ref, root_objectid,
981 btrfs_release_path(root, path);
983 ret = btrfs_insert_empty_item(trans, root, path, &key,
985 if (ret && ret != -EEXIST)
988 leaf = path->nodes[0];
990 ref = btrfs_item_ptr(leaf, path->slots[0],
991 struct btrfs_extent_data_ref);
993 btrfs_set_extent_data_ref_root(leaf, ref,
995 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
996 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
997 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
999 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1000 num_refs += refs_to_add;
1001 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1004 btrfs_mark_buffer_dirty(leaf);
1007 btrfs_release_path(root, path);
1011 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1012 struct btrfs_root *root,
1013 struct btrfs_path *path,
1016 struct btrfs_key key;
1017 struct btrfs_extent_data_ref *ref1 = NULL;
1018 struct btrfs_shared_data_ref *ref2 = NULL;
1019 struct extent_buffer *leaf;
1023 leaf = path->nodes[0];
1024 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1026 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1027 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1028 struct btrfs_extent_data_ref);
1029 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1030 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1031 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1032 struct btrfs_shared_data_ref);
1033 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1034 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1035 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1036 struct btrfs_extent_ref_v0 *ref0;
1037 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1038 struct btrfs_extent_ref_v0);
1039 num_refs = btrfs_ref_count_v0(leaf, ref0);
1045 BUG_ON(num_refs < refs_to_drop);
1046 num_refs -= refs_to_drop;
1048 if (num_refs == 0) {
1049 ret = btrfs_del_item(trans, root, path);
1051 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1052 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1053 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1054 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1055 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1057 struct btrfs_extent_ref_v0 *ref0;
1058 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1059 struct btrfs_extent_ref_v0);
1060 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1063 btrfs_mark_buffer_dirty(leaf);
1068 static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1069 struct btrfs_path *path,
1070 struct btrfs_extent_inline_ref *iref)
1072 struct btrfs_key key;
1073 struct extent_buffer *leaf;
1074 struct btrfs_extent_data_ref *ref1;
1075 struct btrfs_shared_data_ref *ref2;
1078 leaf = path->nodes[0];
1079 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1081 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1082 BTRFS_EXTENT_DATA_REF_KEY) {
1083 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1084 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1086 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1087 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1089 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1090 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1091 struct btrfs_extent_data_ref);
1092 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1093 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1094 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1095 struct btrfs_shared_data_ref);
1096 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1097 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1098 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1099 struct btrfs_extent_ref_v0 *ref0;
1100 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1101 struct btrfs_extent_ref_v0);
1102 num_refs = btrfs_ref_count_v0(leaf, ref0);
1110 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1111 struct btrfs_root *root,
1112 struct btrfs_path *path,
1113 u64 bytenr, u64 parent,
1116 struct btrfs_key key;
1119 key.objectid = bytenr;
1121 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1122 key.offset = parent;
1124 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1125 key.offset = root_objectid;
1128 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1131 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1132 if (ret == -ENOENT && parent) {
1133 btrfs_release_path(root, path);
1134 key.type = BTRFS_EXTENT_REF_V0_KEY;
1135 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1143 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1144 struct btrfs_root *root,
1145 struct btrfs_path *path,
1146 u64 bytenr, u64 parent,
1149 struct btrfs_key key;
1152 key.objectid = bytenr;
1154 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1155 key.offset = parent;
1157 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1158 key.offset = root_objectid;
1161 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1162 btrfs_release_path(root, path);
1166 static inline int extent_ref_type(u64 parent, u64 owner)
1169 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1171 type = BTRFS_SHARED_BLOCK_REF_KEY;
1173 type = BTRFS_TREE_BLOCK_REF_KEY;
1176 type = BTRFS_SHARED_DATA_REF_KEY;
1178 type = BTRFS_EXTENT_DATA_REF_KEY;
1183 static int find_next_key(struct btrfs_path *path, int level,
1184 struct btrfs_key *key)
1187 for (; level < BTRFS_MAX_LEVEL; level++) {
1188 if (!path->nodes[level])
1190 if (path->slots[level] + 1 >=
1191 btrfs_header_nritems(path->nodes[level]))
1194 btrfs_item_key_to_cpu(path->nodes[level], key,
1195 path->slots[level] + 1);
1197 btrfs_node_key_to_cpu(path->nodes[level], key,
1198 path->slots[level] + 1);
1205 * look for inline back ref. if back ref is found, *ref_ret is set
1206 * to the address of inline back ref, and 0 is returned.
1208 * if back ref isn't found, *ref_ret is set to the address where it
1209 * should be inserted, and -ENOENT is returned.
1211 * if insert is true and there are too many inline back refs, the path
1212 * points to the extent item, and -EAGAIN is returned.
1214 * NOTE: inline back refs are ordered in the same way that back ref
1215 * items in the tree are ordered.
1217 static noinline_for_stack
1218 int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1219 struct btrfs_root *root,
1220 struct btrfs_path *path,
1221 struct btrfs_extent_inline_ref **ref_ret,
1222 u64 bytenr, u64 num_bytes,
1223 u64 parent, u64 root_objectid,
1224 u64 owner, u64 offset, int insert)
1226 struct btrfs_key key;
1227 struct extent_buffer *leaf;
1228 struct btrfs_extent_item *ei;
1229 struct btrfs_extent_inline_ref *iref;
1240 key.objectid = bytenr;
1241 key.type = BTRFS_EXTENT_ITEM_KEY;
1242 key.offset = num_bytes;
1244 want = extent_ref_type(parent, owner);
1246 extra_size = btrfs_extent_inline_ref_size(want);
1247 path->keep_locks = 1;
1250 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1257 leaf = path->nodes[0];
1258 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1259 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1260 if (item_size < sizeof(*ei)) {
1265 ret = convert_extent_item_v0(trans, root, path, owner,
1271 leaf = path->nodes[0];
1272 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1275 BUG_ON(item_size < sizeof(*ei));
1277 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1278 flags = btrfs_extent_flags(leaf, ei);
1280 ptr = (unsigned long)(ei + 1);
1281 end = (unsigned long)ei + item_size;
1283 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1284 ptr += sizeof(struct btrfs_tree_block_info);
1287 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1296 iref = (struct btrfs_extent_inline_ref *)ptr;
1297 type = btrfs_extent_inline_ref_type(leaf, iref);
1301 ptr += btrfs_extent_inline_ref_size(type);
1305 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1306 struct btrfs_extent_data_ref *dref;
1307 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1308 if (match_extent_data_ref(leaf, dref, root_objectid,
1313 if (hash_extent_data_ref_item(leaf, dref) <
1314 hash_extent_data_ref(root_objectid, owner, offset))
1318 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1320 if (parent == ref_offset) {
1324 if (ref_offset < parent)
1327 if (root_objectid == ref_offset) {
1331 if (ref_offset < root_objectid)
1335 ptr += btrfs_extent_inline_ref_size(type);
1337 if (err == -ENOENT && insert) {
1338 if (item_size + extra_size >=
1339 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1344 * To add new inline back ref, we have to make sure
1345 * there is no corresponding back ref item.
1346 * For simplicity, we just do not add new inline back
1347 * ref if there is any kind of item for this block
1349 if (find_next_key(path, 0, &key) == 0 &&
1350 key.objectid == bytenr &&
1351 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
1356 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1359 path->keep_locks = 0;
1360 btrfs_unlock_up_safe(path, 1);
1366 * helper to add new inline back ref
1368 static noinline_for_stack
1369 int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1370 struct btrfs_root *root,
1371 struct btrfs_path *path,
1372 struct btrfs_extent_inline_ref *iref,
1373 u64 parent, u64 root_objectid,
1374 u64 owner, u64 offset, int refs_to_add,
1375 struct btrfs_delayed_extent_op *extent_op)
1377 struct extent_buffer *leaf;
1378 struct btrfs_extent_item *ei;
1381 unsigned long item_offset;
1387 leaf = path->nodes[0];
1388 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1389 item_offset = (unsigned long)iref - (unsigned long)ei;
1391 type = extent_ref_type(parent, owner);
1392 size = btrfs_extent_inline_ref_size(type);
1394 ret = btrfs_extend_item(trans, root, path, size);
1397 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1398 refs = btrfs_extent_refs(leaf, ei);
1399 refs += refs_to_add;
1400 btrfs_set_extent_refs(leaf, ei, refs);
1402 __run_delayed_extent_op(extent_op, leaf, ei);
1404 ptr = (unsigned long)ei + item_offset;
1405 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1406 if (ptr < end - size)
1407 memmove_extent_buffer(leaf, ptr + size, ptr,
1410 iref = (struct btrfs_extent_inline_ref *)ptr;
1411 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1412 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1413 struct btrfs_extent_data_ref *dref;
1414 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1415 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1416 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1417 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1418 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1419 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1420 struct btrfs_shared_data_ref *sref;
1421 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1422 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1423 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1424 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1425 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1427 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1429 btrfs_mark_buffer_dirty(leaf);
1433 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1434 struct btrfs_root *root,
1435 struct btrfs_path *path,
1436 struct btrfs_extent_inline_ref **ref_ret,
1437 u64 bytenr, u64 num_bytes, u64 parent,
1438 u64 root_objectid, u64 owner, u64 offset)
1442 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1443 bytenr, num_bytes, parent,
1444 root_objectid, owner, offset, 0);
1448 btrfs_release_path(root, path);
1451 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1452 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1455 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1456 root_objectid, owner, offset);
1462 * helper to update/remove inline back ref
1464 static noinline_for_stack
1465 int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1466 struct btrfs_root *root,
1467 struct btrfs_path *path,
1468 struct btrfs_extent_inline_ref *iref,
1470 struct btrfs_delayed_extent_op *extent_op)
1472 struct extent_buffer *leaf;
1473 struct btrfs_extent_item *ei;
1474 struct btrfs_extent_data_ref *dref = NULL;
1475 struct btrfs_shared_data_ref *sref = NULL;
1484 leaf = path->nodes[0];
1485 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1486 refs = btrfs_extent_refs(leaf, ei);
1487 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1488 refs += refs_to_mod;
1489 btrfs_set_extent_refs(leaf, ei, refs);
1491 __run_delayed_extent_op(extent_op, leaf, ei);
1493 type = btrfs_extent_inline_ref_type(leaf, iref);
1495 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1496 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1497 refs = btrfs_extent_data_ref_count(leaf, dref);
1498 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1499 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1500 refs = btrfs_shared_data_ref_count(leaf, sref);
1503 BUG_ON(refs_to_mod != -1);
1506 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1507 refs += refs_to_mod;
1510 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1511 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1513 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1515 size = btrfs_extent_inline_ref_size(type);
1516 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1517 ptr = (unsigned long)iref;
1518 end = (unsigned long)ei + item_size;
1519 if (ptr + size < end)
1520 memmove_extent_buffer(leaf, ptr, ptr + size,
1523 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1526 btrfs_mark_buffer_dirty(leaf);
1530 static noinline_for_stack
1531 int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1532 struct btrfs_root *root,
1533 struct btrfs_path *path,
1534 u64 bytenr, u64 num_bytes, u64 parent,
1535 u64 root_objectid, u64 owner,
1536 u64 offset, int refs_to_add,
1537 struct btrfs_delayed_extent_op *extent_op)
1539 struct btrfs_extent_inline_ref *iref;
1542 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1543 bytenr, num_bytes, parent,
1544 root_objectid, owner, offset, 1);
1546 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1547 ret = update_inline_extent_backref(trans, root, path, iref,
1548 refs_to_add, extent_op);
1549 } else if (ret == -ENOENT) {
1550 ret = setup_inline_extent_backref(trans, root, path, iref,
1551 parent, root_objectid,
1552 owner, offset, refs_to_add,
1558 static int insert_extent_backref(struct btrfs_trans_handle *trans,
1559 struct btrfs_root *root,
1560 struct btrfs_path *path,
1561 u64 bytenr, u64 parent, u64 root_objectid,
1562 u64 owner, u64 offset, int refs_to_add)
1565 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1566 BUG_ON(refs_to_add != 1);
1567 ret = insert_tree_block_ref(trans, root, path, bytenr,
1568 parent, root_objectid);
1570 ret = insert_extent_data_ref(trans, root, path, bytenr,
1571 parent, root_objectid,
1572 owner, offset, refs_to_add);
1577 static int remove_extent_backref(struct btrfs_trans_handle *trans,
1578 struct btrfs_root *root,
1579 struct btrfs_path *path,
1580 struct btrfs_extent_inline_ref *iref,
1581 int refs_to_drop, int is_data)
1585 BUG_ON(!is_data && refs_to_drop != 1);
1587 ret = update_inline_extent_backref(trans, root, path, iref,
1588 -refs_to_drop, NULL);
1589 } else if (is_data) {
1590 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1592 ret = btrfs_del_item(trans, root, path);
1597 static void btrfs_issue_discard(struct block_device *bdev,
1600 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL,
1601 DISCARD_FL_BARRIER);
1604 static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1608 u64 map_length = num_bytes;
1609 struct btrfs_multi_bio *multi = NULL;
1611 if (!btrfs_test_opt(root, DISCARD))
1614 /* Tell the block device(s) that the sectors can be discarded */
1615 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1616 bytenr, &map_length, &multi, 0);
1618 struct btrfs_bio_stripe *stripe = multi->stripes;
1621 if (map_length > num_bytes)
1622 map_length = num_bytes;
1624 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1625 btrfs_issue_discard(stripe->dev->bdev,
1635 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1636 struct btrfs_root *root,
1637 u64 bytenr, u64 num_bytes, u64 parent,
1638 u64 root_objectid, u64 owner, u64 offset)
1641 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1642 root_objectid == BTRFS_TREE_LOG_OBJECTID);
1644 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1645 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1646 parent, root_objectid, (int)owner,
1647 BTRFS_ADD_DELAYED_REF, NULL);
1649 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1650 parent, root_objectid, owner, offset,
1651 BTRFS_ADD_DELAYED_REF, NULL);
1656 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1657 struct btrfs_root *root,
1658 u64 bytenr, u64 num_bytes,
1659 u64 parent, u64 root_objectid,
1660 u64 owner, u64 offset, int refs_to_add,
1661 struct btrfs_delayed_extent_op *extent_op)
1663 struct btrfs_path *path;
1664 struct extent_buffer *leaf;
1665 struct btrfs_extent_item *item;
1670 path = btrfs_alloc_path();
1675 path->leave_spinning = 1;
1676 /* this will setup the path even if it fails to insert the back ref */
1677 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1678 path, bytenr, num_bytes, parent,
1679 root_objectid, owner, offset,
1680 refs_to_add, extent_op);
1684 if (ret != -EAGAIN) {
1689 leaf = path->nodes[0];
1690 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1691 refs = btrfs_extent_refs(leaf, item);
1692 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1694 __run_delayed_extent_op(extent_op, leaf, item);
1696 btrfs_mark_buffer_dirty(leaf);
1697 btrfs_release_path(root->fs_info->extent_root, path);
1700 path->leave_spinning = 1;
1702 /* now insert the actual backref */
1703 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1704 path, bytenr, parent, root_objectid,
1705 owner, offset, refs_to_add);
1708 btrfs_free_path(path);
1712 static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1713 struct btrfs_root *root,
1714 struct btrfs_delayed_ref_node *node,
1715 struct btrfs_delayed_extent_op *extent_op,
1716 int insert_reserved)
1719 struct btrfs_delayed_data_ref *ref;
1720 struct btrfs_key ins;
1725 ins.objectid = node->bytenr;
1726 ins.offset = node->num_bytes;
1727 ins.type = BTRFS_EXTENT_ITEM_KEY;
1729 ref = btrfs_delayed_node_to_data_ref(node);
1730 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1731 parent = ref->parent;
1733 ref_root = ref->root;
1735 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1737 BUG_ON(extent_op->update_key);
1738 flags |= extent_op->flags_to_set;
1740 ret = alloc_reserved_file_extent(trans, root,
1741 parent, ref_root, flags,
1742 ref->objectid, ref->offset,
1743 &ins, node->ref_mod);
1744 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1745 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1746 node->num_bytes, parent,
1747 ref_root, ref->objectid,
1748 ref->offset, node->ref_mod,
1750 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1751 ret = __btrfs_free_extent(trans, root, node->bytenr,
1752 node->num_bytes, parent,
1753 ref_root, ref->objectid,
1754 ref->offset, node->ref_mod,
1762 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1763 struct extent_buffer *leaf,
1764 struct btrfs_extent_item *ei)
1766 u64 flags = btrfs_extent_flags(leaf, ei);
1767 if (extent_op->update_flags) {
1768 flags |= extent_op->flags_to_set;
1769 btrfs_set_extent_flags(leaf, ei, flags);
1772 if (extent_op->update_key) {
1773 struct btrfs_tree_block_info *bi;
1774 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1775 bi = (struct btrfs_tree_block_info *)(ei + 1);
1776 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1780 static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1781 struct btrfs_root *root,
1782 struct btrfs_delayed_ref_node *node,
1783 struct btrfs_delayed_extent_op *extent_op)
1785 struct btrfs_key key;
1786 struct btrfs_path *path;
1787 struct btrfs_extent_item *ei;
1788 struct extent_buffer *leaf;
1793 path = btrfs_alloc_path();
1797 key.objectid = node->bytenr;
1798 key.type = BTRFS_EXTENT_ITEM_KEY;
1799 key.offset = node->num_bytes;
1802 path->leave_spinning = 1;
1803 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1814 leaf = path->nodes[0];
1815 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1816 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1817 if (item_size < sizeof(*ei)) {
1818 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1824 leaf = path->nodes[0];
1825 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1828 BUG_ON(item_size < sizeof(*ei));
1829 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1830 __run_delayed_extent_op(extent_op, leaf, ei);
1832 btrfs_mark_buffer_dirty(leaf);
1834 btrfs_free_path(path);
1838 static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1839 struct btrfs_root *root,
1840 struct btrfs_delayed_ref_node *node,
1841 struct btrfs_delayed_extent_op *extent_op,
1842 int insert_reserved)
1845 struct btrfs_delayed_tree_ref *ref;
1846 struct btrfs_key ins;
1850 ins.objectid = node->bytenr;
1851 ins.offset = node->num_bytes;
1852 ins.type = BTRFS_EXTENT_ITEM_KEY;
1854 ref = btrfs_delayed_node_to_tree_ref(node);
1855 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1856 parent = ref->parent;
1858 ref_root = ref->root;
1860 BUG_ON(node->ref_mod != 1);
1861 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1862 BUG_ON(!extent_op || !extent_op->update_flags ||
1863 !extent_op->update_key);
1864 ret = alloc_reserved_tree_block(trans, root,
1866 extent_op->flags_to_set,
1869 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1870 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1871 node->num_bytes, parent, ref_root,
1872 ref->level, 0, 1, extent_op);
1873 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1874 ret = __btrfs_free_extent(trans, root, node->bytenr,
1875 node->num_bytes, parent, ref_root,
1876 ref->level, 0, 1, extent_op);
1884 /* helper function to actually process a single delayed ref entry */
1885 static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
1886 struct btrfs_root *root,
1887 struct btrfs_delayed_ref_node *node,
1888 struct btrfs_delayed_extent_op *extent_op,
1889 int insert_reserved)
1892 if (btrfs_delayed_ref_is_head(node)) {
1893 struct btrfs_delayed_ref_head *head;
1895 * we've hit the end of the chain and we were supposed
1896 * to insert this extent into the tree. But, it got
1897 * deleted before we ever needed to insert it, so all
1898 * we have to do is clean up the accounting
1901 head = btrfs_delayed_node_to_head(node);
1902 if (insert_reserved) {
1904 struct extent_buffer *must_clean = NULL;
1906 ret = pin_down_bytes(trans, root, NULL,
1907 node->bytenr, node->num_bytes,
1908 head->is_data, 1, &must_clean);
1913 clean_tree_block(NULL, root, must_clean);
1914 btrfs_tree_unlock(must_clean);
1915 free_extent_buffer(must_clean);
1917 if (head->is_data) {
1918 ret = btrfs_del_csums(trans, root,
1924 ret = btrfs_free_reserved_extent(root,
1930 mutex_unlock(&head->mutex);
1934 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1935 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1936 ret = run_delayed_tree_ref(trans, root, node, extent_op,
1938 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1939 node->type == BTRFS_SHARED_DATA_REF_KEY)
1940 ret = run_delayed_data_ref(trans, root, node, extent_op,
1947 static noinline struct btrfs_delayed_ref_node *
1948 select_delayed_ref(struct btrfs_delayed_ref_head *head)
1950 struct rb_node *node;
1951 struct btrfs_delayed_ref_node *ref;
1952 int action = BTRFS_ADD_DELAYED_REF;
1955 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
1956 * this prevents ref count from going down to zero when
1957 * there still are pending delayed ref.
1959 node = rb_prev(&head->node.rb_node);
1963 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1965 if (ref->bytenr != head->node.bytenr)
1967 if (ref->action == action)
1969 node = rb_prev(node);
1971 if (action == BTRFS_ADD_DELAYED_REF) {
1972 action = BTRFS_DROP_DELAYED_REF;
1978 static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
1979 struct btrfs_root *root,
1980 struct list_head *cluster)
1982 struct btrfs_delayed_ref_root *delayed_refs;
1983 struct btrfs_delayed_ref_node *ref;
1984 struct btrfs_delayed_ref_head *locked_ref = NULL;
1985 struct btrfs_delayed_extent_op *extent_op;
1988 int must_insert_reserved = 0;
1990 delayed_refs = &trans->transaction->delayed_refs;
1993 /* pick a new head ref from the cluster list */
1994 if (list_empty(cluster))
1997 locked_ref = list_entry(cluster->next,
1998 struct btrfs_delayed_ref_head, cluster);
2000 /* grab the lock that says we are going to process
2001 * all the refs for this head */
2002 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2005 * we may have dropped the spin lock to get the head
2006 * mutex lock, and that might have given someone else
2007 * time to free the head. If that's true, it has been
2008 * removed from our list and we can move on.
2010 if (ret == -EAGAIN) {
2018 * record the must insert reserved flag before we
2019 * drop the spin lock.
2021 must_insert_reserved = locked_ref->must_insert_reserved;
2022 locked_ref->must_insert_reserved = 0;
2024 extent_op = locked_ref->extent_op;
2025 locked_ref->extent_op = NULL;
2028 * locked_ref is the head node, so we have to go one
2029 * node back for any delayed ref updates
2031 ref = select_delayed_ref(locked_ref);
2033 /* All delayed refs have been processed, Go ahead
2034 * and send the head node to run_one_delayed_ref,
2035 * so that any accounting fixes can happen
2037 ref = &locked_ref->node;
2039 if (extent_op && must_insert_reserved) {
2045 spin_unlock(&delayed_refs->lock);
2047 ret = run_delayed_extent_op(trans, root,
2053 spin_lock(&delayed_refs->lock);
2057 list_del_init(&locked_ref->cluster);
2062 rb_erase(&ref->rb_node, &delayed_refs->root);
2063 delayed_refs->num_entries--;
2065 spin_unlock(&delayed_refs->lock);
2067 ret = run_one_delayed_ref(trans, root, ref, extent_op,
2068 must_insert_reserved);
2071 btrfs_put_delayed_ref(ref);
2076 spin_lock(&delayed_refs->lock);
2082 * this starts processing the delayed reference count updates and
2083 * extent insertions we have queued up so far. count can be
2084 * 0, which means to process everything in the tree at the start
2085 * of the run (but not newly added entries), or it can be some target
2086 * number you'd like to process.
2088 int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2089 struct btrfs_root *root, unsigned long count)
2091 struct rb_node *node;
2092 struct btrfs_delayed_ref_root *delayed_refs;
2093 struct btrfs_delayed_ref_node *ref;
2094 struct list_head cluster;
2096 int run_all = count == (unsigned long)-1;
2099 if (root == root->fs_info->extent_root)
2100 root = root->fs_info->tree_root;
2102 delayed_refs = &trans->transaction->delayed_refs;
2103 INIT_LIST_HEAD(&cluster);
2105 spin_lock(&delayed_refs->lock);
2107 count = delayed_refs->num_entries * 2;
2111 if (!(run_all || run_most) &&
2112 delayed_refs->num_heads_ready < 64)
2116 * go find something we can process in the rbtree. We start at
2117 * the beginning of the tree, and then build a cluster
2118 * of refs to process starting at the first one we are able to
2121 ret = btrfs_find_ref_cluster(trans, &cluster,
2122 delayed_refs->run_delayed_start);
2126 ret = run_clustered_refs(trans, root, &cluster);
2129 count -= min_t(unsigned long, ret, count);
2136 node = rb_first(&delayed_refs->root);
2139 count = (unsigned long)-1;
2142 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2144 if (btrfs_delayed_ref_is_head(ref)) {
2145 struct btrfs_delayed_ref_head *head;
2147 head = btrfs_delayed_node_to_head(ref);
2148 atomic_inc(&ref->refs);
2150 spin_unlock(&delayed_refs->lock);
2151 mutex_lock(&head->mutex);
2152 mutex_unlock(&head->mutex);
2154 btrfs_put_delayed_ref(ref);
2158 node = rb_next(node);
2160 spin_unlock(&delayed_refs->lock);
2161 schedule_timeout(1);
2165 spin_unlock(&delayed_refs->lock);
2169 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2170 struct btrfs_root *root,
2171 u64 bytenr, u64 num_bytes, u64 flags,
2174 struct btrfs_delayed_extent_op *extent_op;
2177 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2181 extent_op->flags_to_set = flags;
2182 extent_op->update_flags = 1;
2183 extent_op->update_key = 0;
2184 extent_op->is_data = is_data ? 1 : 0;
2186 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2192 static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2193 struct btrfs_root *root,
2194 struct btrfs_path *path,
2195 u64 objectid, u64 offset, u64 bytenr)
2197 struct btrfs_delayed_ref_head *head;
2198 struct btrfs_delayed_ref_node *ref;
2199 struct btrfs_delayed_data_ref *data_ref;
2200 struct btrfs_delayed_ref_root *delayed_refs;
2201 struct rb_node *node;
2205 delayed_refs = &trans->transaction->delayed_refs;
2206 spin_lock(&delayed_refs->lock);
2207 head = btrfs_find_delayed_ref_head(trans, bytenr);
2211 if (!mutex_trylock(&head->mutex)) {
2212 atomic_inc(&head->node.refs);
2213 spin_unlock(&delayed_refs->lock);
2215 btrfs_release_path(root->fs_info->extent_root, path);
2217 mutex_lock(&head->mutex);
2218 mutex_unlock(&head->mutex);
2219 btrfs_put_delayed_ref(&head->node);
2223 node = rb_prev(&head->node.rb_node);
2227 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2229 if (ref->bytenr != bytenr)
2233 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2236 data_ref = btrfs_delayed_node_to_data_ref(ref);
2238 node = rb_prev(node);
2240 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2241 if (ref->bytenr == bytenr)
2245 if (data_ref->root != root->root_key.objectid ||
2246 data_ref->objectid != objectid || data_ref->offset != offset)
2251 mutex_unlock(&head->mutex);
2253 spin_unlock(&delayed_refs->lock);
2257 static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2258 struct btrfs_root *root,
2259 struct btrfs_path *path,
2260 u64 objectid, u64 offset, u64 bytenr)
2262 struct btrfs_root *extent_root = root->fs_info->extent_root;
2263 struct extent_buffer *leaf;
2264 struct btrfs_extent_data_ref *ref;
2265 struct btrfs_extent_inline_ref *iref;
2266 struct btrfs_extent_item *ei;
2267 struct btrfs_key key;
2271 key.objectid = bytenr;
2272 key.offset = (u64)-1;
2273 key.type = BTRFS_EXTENT_ITEM_KEY;
2275 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2281 if (path->slots[0] == 0)
2285 leaf = path->nodes[0];
2286 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2288 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
2292 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2293 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2294 if (item_size < sizeof(*ei)) {
2295 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2299 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2301 if (item_size != sizeof(*ei) +
2302 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2305 if (btrfs_extent_generation(leaf, ei) <=
2306 btrfs_root_last_snapshot(&root->root_item))
2309 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2310 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2311 BTRFS_EXTENT_DATA_REF_KEY)
2314 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2315 if (btrfs_extent_refs(leaf, ei) !=
2316 btrfs_extent_data_ref_count(leaf, ref) ||
2317 btrfs_extent_data_ref_root(leaf, ref) !=
2318 root->root_key.objectid ||
2319 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2320 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2328 int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2329 struct btrfs_root *root,
2330 u64 objectid, u64 offset, u64 bytenr)
2332 struct btrfs_path *path;
2336 path = btrfs_alloc_path();
2341 ret = check_committed_ref(trans, root, path, objectid,
2343 if (ret && ret != -ENOENT)
2346 ret2 = check_delayed_ref(trans, root, path, objectid,
2348 } while (ret2 == -EAGAIN);
2350 if (ret2 && ret2 != -ENOENT) {
2355 if (ret != -ENOENT || ret2 != -ENOENT)
2358 btrfs_free_path(path);
2363 int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2364 struct extent_buffer *buf, u32 nr_extents)
2366 struct btrfs_key key;
2367 struct btrfs_file_extent_item *fi;
2375 if (!root->ref_cows)
2378 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2380 root_gen = root->root_key.offset;
2383 root_gen = trans->transid - 1;
2386 level = btrfs_header_level(buf);
2387 nritems = btrfs_header_nritems(buf);
2390 struct btrfs_leaf_ref *ref;
2391 struct btrfs_extent_info *info;
2393 ref = btrfs_alloc_leaf_ref(root, nr_extents);
2399 ref->root_gen = root_gen;
2400 ref->bytenr = buf->start;
2401 ref->owner = btrfs_header_owner(buf);
2402 ref->generation = btrfs_header_generation(buf);
2403 ref->nritems = nr_extents;
2404 info = ref->extents;
2406 for (i = 0; nr_extents > 0 && i < nritems; i++) {
2408 btrfs_item_key_to_cpu(buf, &key, i);
2409 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2411 fi = btrfs_item_ptr(buf, i,
2412 struct btrfs_file_extent_item);
2413 if (btrfs_file_extent_type(buf, fi) ==
2414 BTRFS_FILE_EXTENT_INLINE)
2416 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2417 if (disk_bytenr == 0)
2420 info->bytenr = disk_bytenr;
2422 btrfs_file_extent_disk_num_bytes(buf, fi);
2423 info->objectid = key.objectid;
2424 info->offset = key.offset;
2428 ret = btrfs_add_leaf_ref(root, ref, shared);
2429 if (ret == -EEXIST && shared) {
2430 struct btrfs_leaf_ref *old;
2431 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2433 btrfs_remove_leaf_ref(root, old);
2434 btrfs_free_leaf_ref(root, old);
2435 ret = btrfs_add_leaf_ref(root, ref, shared);
2438 btrfs_free_leaf_ref(root, ref);
2444 /* when a block goes through cow, we update the reference counts of
2445 * everything that block points to. The internal pointers of the block
2446 * can be in just about any order, and it is likely to have clusters of
2447 * things that are close together and clusters of things that are not.
2449 * To help reduce the seeks that come with updating all of these reference
2450 * counts, sort them by byte number before actual updates are done.
2452 * struct refsort is used to match byte number to slot in the btree block.
2453 * we sort based on the byte number and then use the slot to actually
2456 * struct refsort is smaller than strcut btrfs_item and smaller than
2457 * struct btrfs_key_ptr. Since we're currently limited to the page size
2458 * for a btree block, there's no way for a kmalloc of refsorts for a
2459 * single node to be bigger than a page.
2467 * for passing into sort()
2469 static int refsort_cmp(const void *a_void, const void *b_void)
2471 const struct refsort *a = a_void;
2472 const struct refsort *b = b_void;
2474 if (a->bytenr < b->bytenr)
2476 if (a->bytenr > b->bytenr)
2482 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
2483 struct btrfs_root *root,
2484 struct extent_buffer *buf,
2485 int full_backref, int inc)
2492 struct btrfs_key key;
2493 struct btrfs_file_extent_item *fi;
2497 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
2498 u64, u64, u64, u64, u64, u64);
2500 ref_root = btrfs_header_owner(buf);
2501 nritems = btrfs_header_nritems(buf);
2502 level = btrfs_header_level(buf);
2504 if (!root->ref_cows && level == 0)
2508 process_func = btrfs_inc_extent_ref;
2510 process_func = btrfs_free_extent;
2513 parent = buf->start;
2517 for (i = 0; i < nritems; i++) {
2519 btrfs_item_key_to_cpu(buf, &key, i);
2520 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2522 fi = btrfs_item_ptr(buf, i,
2523 struct btrfs_file_extent_item);
2524 if (btrfs_file_extent_type(buf, fi) ==
2525 BTRFS_FILE_EXTENT_INLINE)
2527 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2531 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2532 key.offset -= btrfs_file_extent_offset(buf, fi);
2533 ret = process_func(trans, root, bytenr, num_bytes,
2534 parent, ref_root, key.objectid,
2539 bytenr = btrfs_node_blockptr(buf, i);
2540 num_bytes = btrfs_level_size(root, level - 1);
2541 ret = process_func(trans, root, bytenr, num_bytes,
2542 parent, ref_root, level - 1, 0);
2553 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2554 struct extent_buffer *buf, int full_backref)
2556 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2559 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2560 struct extent_buffer *buf, int full_backref)
2562 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
2565 static int write_one_cache_group(struct btrfs_trans_handle *trans,
2566 struct btrfs_root *root,
2567 struct btrfs_path *path,
2568 struct btrfs_block_group_cache *cache)
2571 struct btrfs_root *extent_root = root->fs_info->extent_root;
2573 struct extent_buffer *leaf;
2575 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
2580 leaf = path->nodes[0];
2581 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2582 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2583 btrfs_mark_buffer_dirty(leaf);
2584 btrfs_release_path(extent_root, path);
2592 static struct btrfs_block_group_cache *
2593 next_block_group(struct btrfs_root *root,
2594 struct btrfs_block_group_cache *cache)
2596 struct rb_node *node;
2597 spin_lock(&root->fs_info->block_group_cache_lock);
2598 node = rb_next(&cache->cache_node);
2599 btrfs_put_block_group(cache);
2601 cache = rb_entry(node, struct btrfs_block_group_cache,
2603 btrfs_get_block_group(cache);
2606 spin_unlock(&root->fs_info->block_group_cache_lock);
2610 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2611 struct btrfs_root *root)
2613 struct btrfs_block_group_cache *cache;
2615 struct btrfs_path *path;
2618 path = btrfs_alloc_path();
2624 err = btrfs_run_delayed_refs(trans, root,
2629 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2633 cache = next_block_group(root, cache);
2643 last = cache->key.objectid + cache->key.offset;
2645 err = write_one_cache_group(trans, root, path, cache);
2647 btrfs_put_block_group(cache);
2650 btrfs_free_path(path);
2654 int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2656 struct btrfs_block_group_cache *block_group;
2659 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2660 if (!block_group || block_group->ro)
2663 btrfs_put_block_group(block_group);
2667 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2668 u64 total_bytes, u64 bytes_used,
2669 struct btrfs_space_info **space_info)
2671 struct btrfs_space_info *found;
2675 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2676 BTRFS_BLOCK_GROUP_RAID10))
2681 found = __find_space_info(info, flags);
2683 spin_lock(&found->lock);
2684 found->total_bytes += total_bytes;
2685 found->bytes_used += bytes_used;
2686 found->disk_used += bytes_used * factor;
2688 spin_unlock(&found->lock);
2689 *space_info = found;
2692 found = kzalloc(sizeof(*found), GFP_NOFS);
2696 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
2697 INIT_LIST_HEAD(&found->block_groups[i]);
2698 init_rwsem(&found->groups_sem);
2699 spin_lock_init(&found->lock);
2700 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
2701 BTRFS_BLOCK_GROUP_SYSTEM |
2702 BTRFS_BLOCK_GROUP_METADATA);
2703 found->total_bytes = total_bytes;
2704 found->bytes_used = bytes_used;
2705 found->disk_used = bytes_used * factor;
2706 found->bytes_pinned = 0;
2707 found->bytes_reserved = 0;
2708 found->bytes_readonly = 0;
2709 found->bytes_delalloc = 0;
2711 found->force_alloc = 0;
2712 *space_info = found;
2713 list_add_rcu(&found->list, &info->space_info);
2714 atomic_set(&found->caching_threads, 0);
2718 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2720 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
2721 BTRFS_BLOCK_GROUP_RAID1 |
2722 BTRFS_BLOCK_GROUP_RAID10 |
2723 BTRFS_BLOCK_GROUP_DUP);
2725 if (flags & BTRFS_BLOCK_GROUP_DATA)
2726 fs_info->avail_data_alloc_bits |= extra_flags;
2727 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2728 fs_info->avail_metadata_alloc_bits |= extra_flags;
2729 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2730 fs_info->avail_system_alloc_bits |= extra_flags;
2734 static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
2736 spin_lock(&cache->space_info->lock);
2737 spin_lock(&cache->lock);
2739 cache->space_info->bytes_readonly += cache->key.offset -
2740 btrfs_block_group_used(&cache->item);
2743 spin_unlock(&cache->lock);
2744 spin_unlock(&cache->space_info->lock);
2747 u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
2749 u64 num_devices = root->fs_info->fs_devices->rw_devices;
2751 if (num_devices == 1)
2752 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2753 if (num_devices < 4)
2754 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2756 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2757 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
2758 BTRFS_BLOCK_GROUP_RAID10))) {
2759 flags &= ~BTRFS_BLOCK_GROUP_DUP;
2762 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
2763 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
2764 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
2767 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2768 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2769 (flags & BTRFS_BLOCK_GROUP_RAID10) |
2770 (flags & BTRFS_BLOCK_GROUP_DUP)))
2771 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2775 static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
2777 if (flags & BTRFS_BLOCK_GROUP_DATA)
2778 flags |= root->fs_info->avail_data_alloc_bits &
2779 root->fs_info->data_alloc_profile;
2780 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2781 flags |= root->fs_info->avail_system_alloc_bits &
2782 root->fs_info->system_alloc_profile;
2783 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
2784 flags |= root->fs_info->avail_metadata_alloc_bits &
2785 root->fs_info->metadata_alloc_profile;
2786 return btrfs_reduce_alloc_profile(root, flags);
2789 static u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
2794 flags = BTRFS_BLOCK_GROUP_DATA;
2795 else if (root == root->fs_info->chunk_root)
2796 flags = BTRFS_BLOCK_GROUP_SYSTEM;
2798 flags = BTRFS_BLOCK_GROUP_METADATA;
2800 return get_alloc_profile(root, flags);
2803 void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
2807 alloc_target = btrfs_get_alloc_profile(root, 1);
2808 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
2812 static u64 calculate_bytes_needed(struct btrfs_root *root, int num_items)
2817 level = BTRFS_MAX_LEVEL - 2;
2819 * NOTE: these calculations are absolutely the worst possible case.
2820 * This assumes that _every_ item we insert will require a new leaf, and
2821 * that the tree has grown to its maximum level size.
2825 * for every item we insert we could insert both an extent item and a
2826 * extent ref item. Then for ever item we insert, we will need to cow
2827 * both the original leaf, plus the leaf to the left and right of it.
2829 * Unless we are talking about the extent root, then we just want the
2830 * number of items * 2, since we just need the extent item plus its ref.
2832 if (root == root->fs_info->extent_root)
2833 num_bytes = num_items * 2;
2835 num_bytes = (num_items + (2 * num_items)) * 3;
2838 * num_bytes is total number of leaves we could need times the leaf
2839 * size, and then for every leaf we could end up cow'ing 2 nodes per
2840 * level, down to the leaf level.
2842 num_bytes = (num_bytes * root->leafsize) +
2843 (num_bytes * (level * 2)) * root->nodesize;
2849 * Unreserve metadata space for delalloc. If we have less reserved credits than
2850 * we have extents, this function does nothing.
2852 int btrfs_unreserve_metadata_for_delalloc(struct btrfs_root *root,
2853 struct inode *inode, int num_items)
2855 struct btrfs_fs_info *info = root->fs_info;
2856 struct btrfs_space_info *meta_sinfo;
2861 /* get the space info for where the metadata will live */
2862 alloc_target = btrfs_get_alloc_profile(root, 0);
2863 meta_sinfo = __find_space_info(info, alloc_target);
2865 num_bytes = calculate_bytes_needed(root->fs_info->extent_root,
2868 spin_lock(&meta_sinfo->lock);
2869 spin_lock(&BTRFS_I(inode)->accounting_lock);
2870 if (BTRFS_I(inode)->reserved_extents <=
2871 BTRFS_I(inode)->outstanding_extents) {
2872 spin_unlock(&BTRFS_I(inode)->accounting_lock);
2873 spin_unlock(&meta_sinfo->lock);
2876 spin_unlock(&BTRFS_I(inode)->accounting_lock);
2878 BTRFS_I(inode)->reserved_extents -= num_items;
2879 BUG_ON(BTRFS_I(inode)->reserved_extents < 0);
2881 if (meta_sinfo->bytes_delalloc < num_bytes) {
2883 meta_sinfo->bytes_delalloc = 0;
2885 meta_sinfo->bytes_delalloc -= num_bytes;
2887 spin_unlock(&meta_sinfo->lock);
2894 static void check_force_delalloc(struct btrfs_space_info *meta_sinfo)
2898 thresh = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
2899 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
2900 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
2901 meta_sinfo->bytes_may_use;
2903 thresh = meta_sinfo->total_bytes - thresh;
2905 do_div(thresh, 100);
2906 if (thresh <= meta_sinfo->bytes_delalloc)
2907 meta_sinfo->force_delalloc = 1;
2909 meta_sinfo->force_delalloc = 0;
2913 * Reserve metadata space for delalloc.
2915 int btrfs_reserve_metadata_for_delalloc(struct btrfs_root *root,
2916 struct inode *inode, int num_items)
2918 struct btrfs_fs_info *info = root->fs_info;
2919 struct btrfs_space_info *meta_sinfo;
2926 /* get the space info for where the metadata will live */
2927 alloc_target = btrfs_get_alloc_profile(root, 0);
2928 meta_sinfo = __find_space_info(info, alloc_target);
2930 num_bytes = calculate_bytes_needed(root->fs_info->extent_root,
2933 spin_lock(&meta_sinfo->lock);
2935 force_delalloc = meta_sinfo->force_delalloc;
2937 if (unlikely(!meta_sinfo->bytes_root))
2938 meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
2941 meta_sinfo->bytes_delalloc += num_bytes;
2943 used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
2944 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
2945 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
2946 meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
2948 if (used > meta_sinfo->total_bytes) {
2952 if (maybe_allocate_chunk(NULL, root, meta_sinfo,
2957 spin_unlock(&meta_sinfo->lock);
2961 filemap_flush(inode->i_mapping);
2963 } else if (flushed == 3) {
2964 shrink_delalloc(NULL, root, meta_sinfo, num_bytes);
2967 spin_lock(&meta_sinfo->lock);
2968 meta_sinfo->bytes_delalloc -= num_bytes;
2969 spin_unlock(&meta_sinfo->lock);
2970 printk(KERN_ERR "enospc, has %d, reserved %d\n",
2971 BTRFS_I(inode)->outstanding_extents,
2972 BTRFS_I(inode)->reserved_extents);
2973 dump_space_info(meta_sinfo, 0, 0);
2977 BTRFS_I(inode)->reserved_extents += num_items;
2978 check_force_delalloc(meta_sinfo);
2979 spin_unlock(&meta_sinfo->lock);
2981 if (!flushed && force_delalloc)
2982 filemap_flush(inode->i_mapping);
2988 * unreserve num_items number of items worth of metadata space. This needs to
2989 * be paired with btrfs_reserve_metadata_space.
2991 * NOTE: if you have the option, run this _AFTER_ you do a
2992 * btrfs_end_transaction, since btrfs_end_transaction will run delayed ref
2993 * oprations which will result in more used metadata, so we want to make sure we
2994 * can do that without issue.
2996 int btrfs_unreserve_metadata_space(struct btrfs_root *root, int num_items)
2998 struct btrfs_fs_info *info = root->fs_info;
2999 struct btrfs_space_info *meta_sinfo;
3004 /* get the space info for where the metadata will live */
3005 alloc_target = btrfs_get_alloc_profile(root, 0);
3006 meta_sinfo = __find_space_info(info, alloc_target);
3008 num_bytes = calculate_bytes_needed(root, num_items);
3010 spin_lock(&meta_sinfo->lock);
3011 if (meta_sinfo->bytes_may_use < num_bytes) {
3013 meta_sinfo->bytes_may_use = 0;
3015 meta_sinfo->bytes_may_use -= num_bytes;
3017 spin_unlock(&meta_sinfo->lock);
3025 * Reserve some metadata space for use. We'll calculate the worste case number
3026 * of bytes that would be needed to modify num_items number of items. If we
3027 * have space, fantastic, if not, you get -ENOSPC. Please call
3028 * btrfs_unreserve_metadata_space when you are done for the _SAME_ number of
3029 * items you reserved, since whatever metadata you needed should have already
3032 * This will commit the transaction to make more space if we don't have enough
3033 * metadata space. THe only time we don't do this is if we're reserving space
3034 * inside of a transaction, then we will just return -ENOSPC and it is the
3035 * callers responsibility to handle it properly.
3037 int btrfs_reserve_metadata_space(struct btrfs_root *root, int num_items)
3039 struct btrfs_fs_info *info = root->fs_info;
3040 struct btrfs_space_info *meta_sinfo;
3046 /* get the space info for where the metadata will live */
3047 alloc_target = btrfs_get_alloc_profile(root, 0);
3048 meta_sinfo = __find_space_info(info, alloc_target);
3050 num_bytes = calculate_bytes_needed(root, num_items);
3052 spin_lock(&meta_sinfo->lock);
3054 if (unlikely(!meta_sinfo->bytes_root))
3055 meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
3058 meta_sinfo->bytes_may_use += num_bytes;
3060 used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
3061 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
3062 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
3063 meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
3065 if (used > meta_sinfo->total_bytes) {
3068 if (maybe_allocate_chunk(NULL, root, meta_sinfo,
3073 spin_unlock(&meta_sinfo->lock);
3077 shrink_delalloc(NULL, root, meta_sinfo, num_bytes);
3080 spin_lock(&meta_sinfo->lock);
3081 meta_sinfo->bytes_may_use -= num_bytes;
3082 spin_unlock(&meta_sinfo->lock);
3084 dump_space_info(meta_sinfo, 0, 0);
3088 check_force_delalloc(meta_sinfo);
3089 spin_unlock(&meta_sinfo->lock);
3095 * This will check the space that the inode allocates from to make sure we have
3096 * enough space for bytes.
3098 int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
3101 struct btrfs_space_info *data_sinfo;
3103 int ret = 0, committed = 0;
3105 /* make sure bytes are sectorsize aligned */
3106 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3108 data_sinfo = BTRFS_I(inode)->space_info;
3113 /* make sure we have enough space to handle the data first */
3114 spin_lock(&data_sinfo->lock);
3115 used = data_sinfo->bytes_used + data_sinfo->bytes_delalloc +
3116 data_sinfo->bytes_reserved + data_sinfo->bytes_pinned +
3117 data_sinfo->bytes_readonly + data_sinfo->bytes_may_use +
3118 data_sinfo->bytes_super;
3120 if (used + bytes > data_sinfo->total_bytes) {
3121 struct btrfs_trans_handle *trans;
3124 * if we don't have enough free bytes in this space then we need
3125 * to alloc a new chunk.
3127 if (!data_sinfo->full) {
3130 data_sinfo->force_alloc = 1;
3131 spin_unlock(&data_sinfo->lock);
3133 alloc_target = btrfs_get_alloc_profile(root, 1);
3134 trans = btrfs_start_transaction(root, 1);
3138 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3139 bytes + 2 * 1024 * 1024,
3141 btrfs_end_transaction(trans, root);
3146 btrfs_set_inode_space_info(root, inode);
3147 data_sinfo = BTRFS_I(inode)->space_info;
3151 spin_unlock(&data_sinfo->lock);
3153 /* commit the current transaction and try again */
3154 if (!committed && !root->fs_info->open_ioctl_trans) {
3156 trans = btrfs_join_transaction(root, 1);
3159 ret = btrfs_commit_transaction(trans, root);
3165 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
3166 ", %llu bytes_used, %llu bytes_reserved, "
3167 "%llu bytes_pinned, %llu bytes_readonly, %llu may use "
3168 "%llu total\n", (unsigned long long)bytes,
3169 (unsigned long long)data_sinfo->bytes_delalloc,
3170 (unsigned long long)data_sinfo->bytes_used,
3171 (unsigned long long)data_sinfo->bytes_reserved,
3172 (unsigned long long)data_sinfo->bytes_pinned,
3173 (unsigned long long)data_sinfo->bytes_readonly,
3174 (unsigned long long)data_sinfo->bytes_may_use,
3175 (unsigned long long)data_sinfo->total_bytes);
3178 data_sinfo->bytes_may_use += bytes;
3179 BTRFS_I(inode)->reserved_bytes += bytes;
3180 spin_unlock(&data_sinfo->lock);
3186 * if there was an error for whatever reason after calling
3187 * btrfs_check_data_free_space, call this so we can cleanup the counters.
3189 void btrfs_free_reserved_data_space(struct btrfs_root *root,
3190 struct inode *inode, u64 bytes)
3192 struct btrfs_space_info *data_sinfo;
3194 /* make sure bytes are sectorsize aligned */
3195 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3197 data_sinfo = BTRFS_I(inode)->space_info;
3198 spin_lock(&data_sinfo->lock);
3199 data_sinfo->bytes_may_use -= bytes;
3200 BTRFS_I(inode)->reserved_bytes -= bytes;
3201 spin_unlock(&data_sinfo->lock);
3204 /* called when we are adding a delalloc extent to the inode's io_tree */
3205 void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
3208 struct btrfs_space_info *data_sinfo;
3210 /* get the space info for where this inode will be storing its data */
3211 data_sinfo = BTRFS_I(inode)->space_info;
3213 /* make sure we have enough space to handle the data first */
3214 spin_lock(&data_sinfo->lock);
3215 data_sinfo->bytes_delalloc += bytes;
3218 * we are adding a delalloc extent without calling
3219 * btrfs_check_data_free_space first. This happens on a weird
3220 * writepage condition, but shouldn't hurt our accounting
3222 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
3223 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
3224 BTRFS_I(inode)->reserved_bytes = 0;
3226 data_sinfo->bytes_may_use -= bytes;
3227 BTRFS_I(inode)->reserved_bytes -= bytes;
3230 spin_unlock(&data_sinfo->lock);
3233 /* called when we are clearing an delalloc extent from the inode's io_tree */
3234 void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
3237 struct btrfs_space_info *info;
3239 info = BTRFS_I(inode)->space_info;
3241 spin_lock(&info->lock);
3242 info->bytes_delalloc -= bytes;
3243 spin_unlock(&info->lock);
3246 static void force_metadata_allocation(struct btrfs_fs_info *info)
3248 struct list_head *head = &info->space_info;
3249 struct btrfs_space_info *found;
3252 list_for_each_entry_rcu(found, head, list) {
3253 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
3254 found->force_alloc = 1;
3259 static int should_alloc_chunk(struct btrfs_space_info *sinfo,
3262 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
3264 if (sinfo->bytes_used + sinfo->bytes_reserved +
3265 alloc_bytes + 256 * 1024 * 1024 < num_bytes)
3268 if (sinfo->bytes_used + sinfo->bytes_reserved +
3269 alloc_bytes < div_factor(num_bytes, 8))
3275 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3276 struct btrfs_root *extent_root, u64 alloc_bytes,
3277 u64 flags, int force)
3279 struct btrfs_space_info *space_info;
3280 struct btrfs_fs_info *fs_info = extent_root->fs_info;
3283 mutex_lock(&fs_info->chunk_mutex);
3285 flags = btrfs_reduce_alloc_profile(extent_root, flags);
3287 space_info = __find_space_info(extent_root->fs_info, flags);
3289 ret = update_space_info(extent_root->fs_info, flags,
3293 BUG_ON(!space_info);
3295 spin_lock(&space_info->lock);
3296 if (space_info->force_alloc)
3298 if (space_info->full) {
3299 spin_unlock(&space_info->lock);
3303 if (!force && !should_alloc_chunk(space_info, alloc_bytes)) {
3304 spin_unlock(&space_info->lock);
3307 spin_unlock(&space_info->lock);
3310 * if we're doing a data chunk, go ahead and make sure that
3311 * we keep a reasonable number of metadata chunks allocated in the
3314 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
3315 fs_info->data_chunk_allocations++;
3316 if (!(fs_info->data_chunk_allocations %
3317 fs_info->metadata_ratio))
3318 force_metadata_allocation(fs_info);
3321 ret = btrfs_alloc_chunk(trans, extent_root, flags);
3322 spin_lock(&space_info->lock);
3324 space_info->full = 1;
3327 space_info->force_alloc = 0;
3328 spin_unlock(&space_info->lock);
3330 mutex_unlock(&extent_root->fs_info->chunk_mutex);
3334 static int maybe_allocate_chunk(struct btrfs_trans_handle *trans,
3335 struct btrfs_root *root,
3336 struct btrfs_space_info *sinfo, u64 num_bytes)
3344 spin_lock(&sinfo->lock);
3345 ret = should_alloc_chunk(sinfo, num_bytes + 2 * 1024 * 1024);
3346 spin_unlock(&sinfo->lock);
3351 trans = btrfs_join_transaction(root, 1);
3352 BUG_ON(IS_ERR(trans));
3356 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3357 num_bytes + 2 * 1024 * 1024,
3358 get_alloc_profile(root, sinfo->flags), 0);
3361 btrfs_end_transaction(trans, root);
3363 return ret == 1 ? 1 : 0;
3367 * shrink metadata reservation for delalloc
3369 static int shrink_delalloc(struct btrfs_trans_handle *trans,
3370 struct btrfs_root *root,
3371 struct btrfs_space_info *sinfo, u64 to_reclaim)
3379 spin_lock(&sinfo->lock);
3380 reserved = sinfo->bytes_delalloc;
3381 spin_unlock(&sinfo->lock);
3386 max_reclaim = min(reserved, to_reclaim);
3389 ret = btrfs_start_one_delalloc_inode(root, trans ? 1 : 0);
3391 __set_current_state(TASK_INTERRUPTIBLE);
3392 schedule_timeout(pause);
3394 if (pause > HZ / 10)
3400 spin_lock(&sinfo->lock);
3401 if (reserved > sinfo->bytes_delalloc)
3402 reclaimed = reserved - sinfo->bytes_delalloc;
3403 reserved = sinfo->bytes_delalloc;
3404 spin_unlock(&sinfo->lock);
3406 if (reserved == 0 || reclaimed >= max_reclaim)
3409 if (trans && trans->transaction->blocked)
3412 return reclaimed >= to_reclaim;
3415 static int update_block_group(struct btrfs_trans_handle *trans,
3416 struct btrfs_root *root,
3417 u64 bytenr, u64 num_bytes, int alloc,
3420 struct btrfs_block_group_cache *cache;
3421 struct btrfs_fs_info *info = root->fs_info;
3423 u64 total = num_bytes;
3427 /* block accounting for super block */
3428 spin_lock(&info->delalloc_lock);
3429 old_val = btrfs_super_bytes_used(&info->super_copy);
3431 old_val += num_bytes;
3433 old_val -= num_bytes;
3434 btrfs_set_super_bytes_used(&info->super_copy, old_val);
3435 spin_unlock(&info->delalloc_lock);
3438 cache = btrfs_lookup_block_group(info, bytenr);
3441 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
3442 BTRFS_BLOCK_GROUP_RAID1 |
3443 BTRFS_BLOCK_GROUP_RAID10))
3447 byte_in_group = bytenr - cache->key.objectid;
3448 WARN_ON(byte_in_group > cache->key.offset);
3450 spin_lock(&cache->space_info->lock);
3451 spin_lock(&cache->lock);
3453 old_val = btrfs_block_group_used(&cache->item);
3454 num_bytes = min(total, cache->key.offset - byte_in_group);
3456 old_val += num_bytes;
3457 btrfs_set_block_group_used(&cache->item, old_val);
3458 cache->reserved -= num_bytes;
3459 cache->space_info->bytes_reserved -= num_bytes;
3460 cache->space_info->bytes_used += num_bytes;
3461 cache->space_info->disk_used += num_bytes * factor;
3463 cache->space_info->bytes_readonly -= num_bytes;
3464 spin_unlock(&cache->lock);
3465 spin_unlock(&cache->space_info->lock);
3467 old_val -= num_bytes;
3468 btrfs_set_block_group_used(&cache->item, old_val);
3469 cache->space_info->bytes_used -= num_bytes;
3470 cache->space_info->disk_used -= num_bytes * factor;
3472 cache->space_info->bytes_readonly += num_bytes;
3473 spin_unlock(&cache->lock);
3474 spin_unlock(&cache->space_info->lock);
3478 ret = btrfs_discard_extent(root, bytenr,
3482 ret = btrfs_add_free_space(cache, bytenr,
3487 btrfs_put_block_group(cache);
3489 bytenr += num_bytes;
3494 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
3496 struct btrfs_block_group_cache *cache;
3499 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
3503 bytenr = cache->key.objectid;
3504 btrfs_put_block_group(cache);
3510 * this function must be called within transaction
3512 int btrfs_pin_extent(struct btrfs_root *root,
3513 u64 bytenr, u64 num_bytes, int reserved)
3515 struct btrfs_fs_info *fs_info = root->fs_info;
3516 struct btrfs_block_group_cache *cache;
3518 cache = btrfs_lookup_block_group(fs_info, bytenr);
3521 spin_lock(&cache->space_info->lock);
3522 spin_lock(&cache->lock);
3523 cache->pinned += num_bytes;
3524 cache->space_info->bytes_pinned += num_bytes;
3526 cache->reserved -= num_bytes;
3527 cache->space_info->bytes_reserved -= num_bytes;
3529 spin_unlock(&cache->lock);
3530 spin_unlock(&cache->space_info->lock);
3532 btrfs_put_block_group(cache);
3534 set_extent_dirty(fs_info->pinned_extents,
3535 bytenr, bytenr + num_bytes - 1, GFP_NOFS);
3539 static int update_reserved_extents(struct btrfs_block_group_cache *cache,
3540 u64 num_bytes, int reserve)
3542 spin_lock(&cache->space_info->lock);
3543 spin_lock(&cache->lock);
3545 cache->reserved += num_bytes;
3546 cache->space_info->bytes_reserved += num_bytes;
3548 cache->reserved -= num_bytes;
3549 cache->space_info->bytes_reserved -= num_bytes;
3551 spin_unlock(&cache->lock);
3552 spin_unlock(&cache->space_info->lock);
3556 int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
3557 struct btrfs_root *root)
3559 struct btrfs_fs_info *fs_info = root->fs_info;
3560 struct btrfs_caching_control *next;
3561 struct btrfs_caching_control *caching_ctl;
3562 struct btrfs_block_group_cache *cache;
3564 down_write(&fs_info->extent_commit_sem);
3566 list_for_each_entry_safe(caching_ctl, next,
3567 &fs_info->caching_block_groups, list) {
3568 cache = caching_ctl->block_group;
3569 if (block_group_cache_done(cache)) {
3570 cache->last_byte_to_unpin = (u64)-1;
3571 list_del_init(&caching_ctl->list);
3572 put_caching_control(caching_ctl);
3574 cache->last_byte_to_unpin = caching_ctl->progress;
3578 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3579 fs_info->pinned_extents = &fs_info->freed_extents[1];
3581 fs_info->pinned_extents = &fs_info->freed_extents[0];
3583 up_write(&fs_info->extent_commit_sem);
3587 static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
3589 struct btrfs_fs_info *fs_info = root->fs_info;
3590 struct btrfs_block_group_cache *cache = NULL;
3593 while (start <= end) {
3595 start >= cache->key.objectid + cache->key.offset) {
3597 btrfs_put_block_group(cache);
3598 cache = btrfs_lookup_block_group(fs_info, start);
3602 len = cache->key.objectid + cache->key.offset - start;
3603 len = min(len, end + 1 - start);
3605 if (start < cache->last_byte_to_unpin) {
3606 len = min(len, cache->last_byte_to_unpin - start);
3607 btrfs_add_free_space(cache, start, len);
3610 spin_lock(&cache->space_info->lock);
3611 spin_lock(&cache->lock);
3612 cache->pinned -= len;
3613 cache->space_info->bytes_pinned -= len;
3614 spin_unlock(&cache->lock);
3615 spin_unlock(&cache->space_info->lock);
3621 btrfs_put_block_group(cache);
3625 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
3626 struct btrfs_root *root)
3628 struct btrfs_fs_info *fs_info = root->fs_info;
3629 struct extent_io_tree *unpin;
3634 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3635 unpin = &fs_info->freed_extents[1];
3637 unpin = &fs_info->freed_extents[0];
3640 ret = find_first_extent_bit(unpin, 0, &start, &end,
3645 ret = btrfs_discard_extent(root, start, end + 1 - start);
3647 clear_extent_dirty(unpin, start, end, GFP_NOFS);
3648 unpin_extent_range(root, start, end);
3655 static int pin_down_bytes(struct btrfs_trans_handle *trans,
3656 struct btrfs_root *root,
3657 struct btrfs_path *path,
3658 u64 bytenr, u64 num_bytes,
3659 int is_data, int reserved,
3660 struct extent_buffer **must_clean)
3663 struct extent_buffer *buf;
3669 * discard is sloooow, and so triggering discards on
3670 * individual btree blocks isn't a good plan. Just
3671 * pin everything in discard mode.
3673 if (btrfs_test_opt(root, DISCARD))
3676 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
3680 /* we can reuse a block if it hasn't been written
3681 * and it is from this transaction. We can't
3682 * reuse anything from the tree log root because
3683 * it has tiny sub-transactions.
3685 if (btrfs_buffer_uptodate(buf, 0) &&
3686 btrfs_try_tree_lock(buf)) {
3687 u64 header_owner = btrfs_header_owner(buf);
3688 u64 header_transid = btrfs_header_generation(buf);
3689 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
3690 header_transid == trans->transid &&
3691 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
3695 btrfs_tree_unlock(buf);
3697 free_extent_buffer(buf);
3700 btrfs_set_path_blocking(path);
3701 /* unlocks the pinned mutex */
3702 btrfs_pin_extent(root, bytenr, num_bytes, reserved);
3708 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
3709 struct btrfs_root *root,
3710 u64 bytenr, u64 num_bytes, u64 parent,
3711 u64 root_objectid, u64 owner_objectid,
3712 u64 owner_offset, int refs_to_drop,
3713 struct btrfs_delayed_extent_op *extent_op)
3715 struct btrfs_key key;
3716 struct btrfs_path *path;
3717 struct btrfs_fs_info *info = root->fs_info;
3718 struct btrfs_root *extent_root = info->extent_root;
3719 struct extent_buffer *leaf;
3720 struct btrfs_extent_item *ei;
3721 struct btrfs_extent_inline_ref *iref;
3724 int extent_slot = 0;
3725 int found_extent = 0;
3730 path = btrfs_alloc_path();
3735 path->leave_spinning = 1;
3737 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
3738 BUG_ON(!is_data && refs_to_drop != 1);
3740 ret = lookup_extent_backref(trans, extent_root, path, &iref,
3741 bytenr, num_bytes, parent,
3742 root_objectid, owner_objectid,
3745 extent_slot = path->slots[0];
3746 while (extent_slot >= 0) {
3747 btrfs_item_key_to_cpu(path->nodes[0], &key,
3749 if (key.objectid != bytenr)
3751 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3752 key.offset == num_bytes) {
3756 if (path->slots[0] - extent_slot > 5)
3760 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3761 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
3762 if (found_extent && item_size < sizeof(*ei))
3765 if (!found_extent) {
3767 ret = remove_extent_backref(trans, extent_root, path,
3771 btrfs_release_path(extent_root, path);
3772 path->leave_spinning = 1;
3774 key.objectid = bytenr;
3775 key.type = BTRFS_EXTENT_ITEM_KEY;
3776 key.offset = num_bytes;
3778 ret = btrfs_search_slot(trans, extent_root,
3781 printk(KERN_ERR "umm, got %d back from search"
3782 ", was looking for %llu\n", ret,
3783 (unsigned long long)bytenr);
3784 btrfs_print_leaf(extent_root, path->nodes[0]);
3787 extent_slot = path->slots[0];
3790 btrfs_print_leaf(extent_root, path->nodes[0]);
3792 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
3793 "parent %llu root %llu owner %llu offset %llu\n",
3794 (unsigned long long)bytenr,
3795 (unsigned long long)parent,
3796 (unsigned long long)root_objectid,
3797 (unsigned long long)owner_objectid,
3798 (unsigned long long)owner_offset);
3801 leaf = path->nodes[0];
3802 item_size = btrfs_item_size_nr(leaf, extent_slot);
3803 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3804 if (item_size < sizeof(*ei)) {
3805 BUG_ON(found_extent || extent_slot != path->slots[0]);
3806 ret = convert_extent_item_v0(trans, extent_root, path,
3810 btrfs_release_path(extent_root, path);
3811 path->leave_spinning = 1;
3813 key.objectid = bytenr;
3814 key.type = BTRFS_EXTENT_ITEM_KEY;
3815 key.offset = num_bytes;
3817 ret = btrfs_search_slot(trans, extent_root, &key, path,
3820 printk(KERN_ERR "umm, got %d back from search"
3821 ", was looking for %llu\n", ret,
3822 (unsigned long long)bytenr);
3823 btrfs_print_leaf(extent_root, path->nodes[0]);
3826 extent_slot = path->slots[0];
3827 leaf = path->nodes[0];
3828 item_size = btrfs_item_size_nr(leaf, extent_slot);
3831 BUG_ON(item_size < sizeof(*ei));
3832 ei = btrfs_item_ptr(leaf, extent_slot,
3833 struct btrfs_extent_item);
3834 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3835 struct btrfs_tree_block_info *bi;
3836 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
3837 bi = (struct btrfs_tree_block_info *)(ei + 1);
3838 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
3841 refs = btrfs_extent_refs(leaf, ei);
3842 BUG_ON(refs < refs_to_drop);
3843 refs -= refs_to_drop;
3847 __run_delayed_extent_op(extent_op, leaf, ei);
3849 * In the case of inline back ref, reference count will
3850 * be updated by remove_extent_backref
3853 BUG_ON(!found_extent);
3855 btrfs_set_extent_refs(leaf, ei, refs);
3856 btrfs_mark_buffer_dirty(leaf);
3859 ret = remove_extent_backref(trans, extent_root, path,
3866 struct extent_buffer *must_clean = NULL;
3869 BUG_ON(is_data && refs_to_drop !=
3870 extent_data_ref_count(root, path, iref));
3872 BUG_ON(path->slots[0] != extent_slot);
3874 BUG_ON(path->slots[0] != extent_slot + 1);
3875 path->slots[0] = extent_slot;
3880 ret = pin_down_bytes(trans, root, path, bytenr,
3881 num_bytes, is_data, 0, &must_clean);
3886 * it is going to be very rare for someone to be waiting
3887 * on the block we're freeing. del_items might need to
3888 * schedule, so rather than get fancy, just force it
3892 btrfs_set_lock_blocking(must_clean);
3894 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3897 btrfs_release_path(extent_root, path);
3900 clean_tree_block(NULL, root, must_clean);
3901 btrfs_tree_unlock(must_clean);
3902 free_extent_buffer(must_clean);
3906 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
3909 invalidate_mapping_pages(info->btree_inode->i_mapping,
3910 bytenr >> PAGE_CACHE_SHIFT,
3911 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
3914 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
3918 btrfs_free_path(path);
3923 * when we free an extent, it is possible (and likely) that we free the last
3924 * delayed ref for that extent as well. This searches the delayed ref tree for
3925 * a given extent, and if there are no other delayed refs to be processed, it
3926 * removes it from the tree.
3928 static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
3929 struct btrfs_root *root, u64 bytenr)
3931 struct btrfs_delayed_ref_head *head;
3932 struct btrfs_delayed_ref_root *delayed_refs;
3933 struct btrfs_delayed_ref_node *ref;
3934 struct rb_node *node;
3937 delayed_refs = &trans->transaction->delayed_refs;
3938 spin_lock(&delayed_refs->lock);
3939 head = btrfs_find_delayed_ref_head(trans, bytenr);
3943 node = rb_prev(&head->node.rb_node);
3947 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
3949 /* there are still entries for this ref, we can't drop it */
3950 if (ref->bytenr == bytenr)
3953 if (head->extent_op) {
3954 if (!head->must_insert_reserved)
3956 kfree(head->extent_op);
3957 head->extent_op = NULL;
3961 * waiting for the lock here would deadlock. If someone else has it
3962 * locked they are already in the process of dropping it anyway
3964 if (!mutex_trylock(&head->mutex))
3968 * at this point we have a head with no other entries. Go
3969 * ahead and process it.
3971 head->node.in_tree = 0;
3972 rb_erase(&head->node.rb_node, &delayed_refs->root);
3974 delayed_refs->num_entries--;
3977 * we don't take a ref on the node because we're removing it from the
3978 * tree, so we just steal the ref the tree was holding.
3980 delayed_refs->num_heads--;
3981 if (list_empty(&head->cluster))
3982 delayed_refs->num_heads_ready--;
3984 list_del_init(&head->cluster);
3985 spin_unlock(&delayed_refs->lock);
3987 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
3988 &head->node, head->extent_op,
3989 head->must_insert_reserved);
3991 btrfs_put_delayed_ref(&head->node);
3994 spin_unlock(&delayed_refs->lock);
3998 int btrfs_free_extent(struct btrfs_trans_handle *trans,
3999 struct btrfs_root *root,
4000 u64 bytenr, u64 num_bytes, u64 parent,
4001 u64 root_objectid, u64 owner, u64 offset)
4006 * tree log blocks never actually go into the extent allocation
4007 * tree, just update pinning info and exit early.
4009 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4010 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
4011 /* unlocks the pinned mutex */
4012 btrfs_pin_extent(root, bytenr, num_bytes, 1);
4014 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4015 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4016 parent, root_objectid, (int)owner,
4017 BTRFS_DROP_DELAYED_REF, NULL);
4019 ret = check_ref_cleanup(trans, root, bytenr);
4022 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4023 parent, root_objectid, owner,
4024 offset, BTRFS_DROP_DELAYED_REF, NULL);
4030 int btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4031 struct btrfs_root *root,
4032 u64 bytenr, u32 blocksize,
4033 u64 parent, u64 root_objectid, int level)
4036 spin_lock(&root->node_lock);
4037 used = btrfs_root_used(&root->root_item) - blocksize;
4038 btrfs_set_root_used(&root->root_item, used);
4039 spin_unlock(&root->node_lock);
4041 return btrfs_free_extent(trans, root, bytenr, blocksize,
4042 parent, root_objectid, level, 0);
4045 static u64 stripe_align(struct btrfs_root *root, u64 val)
4047 u64 mask = ((u64)root->stripesize - 1);
4048 u64 ret = (val + mask) & ~mask;
4053 * when we wait for progress in the block group caching, its because
4054 * our allocation attempt failed at least once. So, we must sleep
4055 * and let some progress happen before we try again.
4057 * This function will sleep at least once waiting for new free space to
4058 * show up, and then it will check the block group free space numbers
4059 * for our min num_bytes. Another option is to have it go ahead
4060 * and look in the rbtree for a free extent of a given size, but this
4064 wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4067 struct btrfs_caching_control *caching_ctl;
4070 caching_ctl = get_caching_control(cache);
4074 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
4075 (cache->free_space >= num_bytes));
4077 put_caching_control(caching_ctl);
4082 wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4084 struct btrfs_caching_control *caching_ctl;
4087 caching_ctl = get_caching_control(cache);
4091 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4093 put_caching_control(caching_ctl);
4097 static int get_block_group_index(struct btrfs_block_group_cache *cache)
4100 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4102 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4104 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4106 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4113 enum btrfs_loop_type {
4114 LOOP_FIND_IDEAL = 0,
4115 LOOP_CACHING_NOWAIT = 1,
4116 LOOP_CACHING_WAIT = 2,
4117 LOOP_ALLOC_CHUNK = 3,
4118 LOOP_NO_EMPTY_SIZE = 4,
4122 * walks the btree of allocated extents and find a hole of a given size.
4123 * The key ins is changed to record the hole:
4124 * ins->objectid == block start
4125 * ins->flags = BTRFS_EXTENT_ITEM_KEY
4126 * ins->offset == number of blocks
4127 * Any available blocks before search_start are skipped.
4129 static noinline int find_free_extent(struct btrfs_trans_handle *trans,
4130 struct btrfs_root *orig_root,
4131 u64 num_bytes, u64 empty_size,
4132 u64 search_start, u64 search_end,
4133 u64 hint_byte, struct btrfs_key *ins,
4134 u64 exclude_start, u64 exclude_nr,
4138 struct btrfs_root *root = orig_root->fs_info->extent_root;
4139 struct btrfs_free_cluster *last_ptr = NULL;
4140 struct btrfs_block_group_cache *block_group = NULL;
4141 int empty_cluster = 2 * 1024 * 1024;
4142 int allowed_chunk_alloc = 0;
4143 int done_chunk_alloc = 0;
4144 struct btrfs_space_info *space_info;
4145 int last_ptr_loop = 0;
4148 bool found_uncached_bg = false;
4149 bool failed_cluster_refill = false;
4150 bool failed_alloc = false;
4151 u64 ideal_cache_percent = 0;
4152 u64 ideal_cache_offset = 0;
4154 WARN_ON(num_bytes < root->sectorsize);
4155 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
4159 space_info = __find_space_info(root->fs_info, data);
4161 printk(KERN_ERR "No space info for %d\n", data);
4165 if (orig_root->ref_cows || empty_size)
4166 allowed_chunk_alloc = 1;
4168 if (data & BTRFS_BLOCK_GROUP_METADATA) {
4169 last_ptr = &root->fs_info->meta_alloc_cluster;
4170 if (!btrfs_test_opt(root, SSD))
4171 empty_cluster = 64 * 1024;
4174 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
4175 last_ptr = &root->fs_info->data_alloc_cluster;
4179 spin_lock(&last_ptr->lock);
4180 if (last_ptr->block_group)
4181 hint_byte = last_ptr->window_start;
4182 spin_unlock(&last_ptr->lock);
4185 search_start = max(search_start, first_logical_byte(root, 0));
4186 search_start = max(search_start, hint_byte);
4191 if (search_start == hint_byte) {
4193 block_group = btrfs_lookup_block_group(root->fs_info,
4196 * we don't want to use the block group if it doesn't match our
4197 * allocation bits, or if its not cached.
4199 * However if we are re-searching with an ideal block group
4200 * picked out then we don't care that the block group is cached.
4202 if (block_group && block_group_bits(block_group, data) &&
4203 (block_group->cached != BTRFS_CACHE_NO ||
4204 search_start == ideal_cache_offset)) {
4205 down_read(&space_info->groups_sem);
4206 if (list_empty(&block_group->list) ||
4209 * someone is removing this block group,
4210 * we can't jump into the have_block_group
4211 * target because our list pointers are not
4214 btrfs_put_block_group(block_group);
4215 up_read(&space_info->groups_sem);
4217 index = get_block_group_index(block_group);
4218 goto have_block_group;
4220 } else if (block_group) {
4221 btrfs_put_block_group(block_group);
4225 down_read(&space_info->groups_sem);
4226 list_for_each_entry(block_group, &space_info->block_groups[index],
4231 btrfs_get_block_group(block_group);
4232 search_start = block_group->key.objectid;
4235 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
4238 free_percent = btrfs_block_group_used(&block_group->item);
4239 free_percent *= 100;
4240 free_percent = div64_u64(free_percent,
4241 block_group->key.offset);
4242 free_percent = 100 - free_percent;
4243 if (free_percent > ideal_cache_percent &&
4244 likely(!block_group->ro)) {
4245 ideal_cache_offset = block_group->key.objectid;
4246 ideal_cache_percent = free_percent;
4250 * We only want to start kthread caching if we are at
4251 * the point where we will wait for caching to make
4252 * progress, or if our ideal search is over and we've
4253 * found somebody to start caching.
4255 if (loop > LOOP_CACHING_NOWAIT ||
4256 (loop > LOOP_FIND_IDEAL &&
4257 atomic_read(&space_info->caching_threads) < 2)) {
4258 ret = cache_block_group(block_group);
4261 found_uncached_bg = true;
4264 * If loop is set for cached only, try the next block
4267 if (loop == LOOP_FIND_IDEAL)
4271 cached = block_group_cache_done(block_group);
4272 if (unlikely(!cached))
4273 found_uncached_bg = true;
4275 if (unlikely(block_group->ro))
4279 * Ok we want to try and use the cluster allocator, so lets look
4280 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
4281 * have tried the cluster allocator plenty of times at this
4282 * point and not have found anything, so we are likely way too
4283 * fragmented for the clustering stuff to find anything, so lets
4284 * just skip it and let the allocator find whatever block it can
4287 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
4289 * the refill lock keeps out other
4290 * people trying to start a new cluster
4292 spin_lock(&last_ptr->refill_lock);
4293 if (last_ptr->block_group &&
4294 (last_ptr->block_group->ro ||
4295 !block_group_bits(last_ptr->block_group, data))) {
4297 goto refill_cluster;
4300 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
4301 num_bytes, search_start);
4303 /* we have a block, we're done */
4304 spin_unlock(&last_ptr->refill_lock);
4308 spin_lock(&last_ptr->lock);
4310 * whoops, this cluster doesn't actually point to
4311 * this block group. Get a ref on the block
4312 * group is does point to and try again
4314 if (!last_ptr_loop && last_ptr->block_group &&
4315 last_ptr->block_group != block_group) {
4317 btrfs_put_block_group(block_group);
4318 block_group = last_ptr->block_group;
4319 btrfs_get_block_group(block_group);
4320 spin_unlock(&last_ptr->lock);
4321 spin_unlock(&last_ptr->refill_lock);
4324 search_start = block_group->key.objectid;
4326 * we know this block group is properly
4327 * in the list because
4328 * btrfs_remove_block_group, drops the
4329 * cluster before it removes the block
4330 * group from the list
4332 goto have_block_group;
4334 spin_unlock(&last_ptr->lock);
4337 * this cluster didn't work out, free it and
4340 btrfs_return_cluster_to_free_space(NULL, last_ptr);
4344 /* allocate a cluster in this block group */
4345 ret = btrfs_find_space_cluster(trans, root,
4346 block_group, last_ptr,
4348 empty_cluster + empty_size);
4351 * now pull our allocation out of this
4354 offset = btrfs_alloc_from_cluster(block_group,
4355 last_ptr, num_bytes,
4358 /* we found one, proceed */
4359 spin_unlock(&last_ptr->refill_lock);
4362 } else if (!cached && loop > LOOP_CACHING_NOWAIT
4363 && !failed_cluster_refill) {
4364 spin_unlock(&last_ptr->refill_lock);
4366 failed_cluster_refill = true;
4367 wait_block_group_cache_progress(block_group,
4368 num_bytes + empty_cluster + empty_size);
4369 goto have_block_group;
4373 * at this point we either didn't find a cluster
4374 * or we weren't able to allocate a block from our
4375 * cluster. Free the cluster we've been trying
4376 * to use, and go to the next block group
4378 btrfs_return_cluster_to_free_space(NULL, last_ptr);
4379 spin_unlock(&last_ptr->refill_lock);
4383 offset = btrfs_find_space_for_alloc(block_group, search_start,
4384 num_bytes, empty_size);
4386 * If we didn't find a chunk, and we haven't failed on this
4387 * block group before, and this block group is in the middle of
4388 * caching and we are ok with waiting, then go ahead and wait
4389 * for progress to be made, and set failed_alloc to true.
4391 * If failed_alloc is true then we've already waited on this
4392 * block group once and should move on to the next block group.
4394 if (!offset && !failed_alloc && !cached &&
4395 loop > LOOP_CACHING_NOWAIT) {
4396 wait_block_group_cache_progress(block_group,
4397 num_bytes + empty_size);
4398 failed_alloc = true;
4399 goto have_block_group;
4400 } else if (!offset) {
4404 search_start = stripe_align(root, offset);
4405 /* move on to the next group */
4406 if (search_start + num_bytes >= search_end) {
4407 btrfs_add_free_space(block_group, offset, num_bytes);
4411 /* move on to the next group */
4412 if (search_start + num_bytes >
4413 block_group->key.objectid + block_group->key.offset) {
4414 btrfs_add_free_space(block_group, offset, num_bytes);
4418 if (exclude_nr > 0 &&
4419 (search_start + num_bytes > exclude_start &&
4420 search_start < exclude_start + exclude_nr)) {
4421 search_start = exclude_start + exclude_nr;
4423 btrfs_add_free_space(block_group, offset, num_bytes);
4425 * if search_start is still in this block group
4426 * then we just re-search this block group
4428 if (search_start >= block_group->key.objectid &&
4429 search_start < (block_group->key.objectid +
4430 block_group->key.offset))
4431 goto have_block_group;
4435 ins->objectid = search_start;
4436 ins->offset = num_bytes;
4438 if (offset < search_start)
4439 btrfs_add_free_space(block_group, offset,
4440 search_start - offset);
4441 BUG_ON(offset > search_start);
4443 update_reserved_extents(block_group, num_bytes, 1);
4445 /* we are all good, lets return */
4448 failed_cluster_refill = false;
4449 failed_alloc = false;
4450 BUG_ON(index != get_block_group_index(block_group));
4451 btrfs_put_block_group(block_group);
4453 up_read(&space_info->groups_sem);
4455 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
4458 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
4459 * for them to make caching progress. Also
4460 * determine the best possible bg to cache
4461 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
4462 * caching kthreads as we move along
4463 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
4464 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
4465 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
4468 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
4469 (found_uncached_bg || empty_size || empty_cluster ||
4470 allowed_chunk_alloc)) {
4472 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
4473 found_uncached_bg = false;
4475 if (!ideal_cache_percent &&
4476 atomic_read(&space_info->caching_threads))
4480 * 1 of the following 2 things have happened so far
4482 * 1) We found an ideal block group for caching that
4483 * is mostly full and will cache quickly, so we might
4484 * as well wait for it.
4486 * 2) We searched for cached only and we didn't find
4487 * anything, and we didn't start any caching kthreads
4488 * either, so chances are we will loop through and
4489 * start a couple caching kthreads, and then come back
4490 * around and just wait for them. This will be slower
4491 * because we will have 2 caching kthreads reading at
4492 * the same time when we could have just started one
4493 * and waited for it to get far enough to give us an
4494 * allocation, so go ahead and go to the wait caching
4497 loop = LOOP_CACHING_WAIT;
4498 search_start = ideal_cache_offset;
4499 ideal_cache_percent = 0;
4501 } else if (loop == LOOP_FIND_IDEAL) {
4503 * Didn't find a uncached bg, wait on anything we find
4506 loop = LOOP_CACHING_WAIT;
4510 if (loop < LOOP_CACHING_WAIT) {
4515 if (loop == LOOP_ALLOC_CHUNK) {
4520 if (allowed_chunk_alloc) {
4521 ret = do_chunk_alloc(trans, root, num_bytes +
4522 2 * 1024 * 1024, data, 1);
4523 allowed_chunk_alloc = 0;
4524 done_chunk_alloc = 1;
4525 } else if (!done_chunk_alloc) {
4526 space_info->force_alloc = 1;
4529 if (loop < LOOP_NO_EMPTY_SIZE) {
4534 } else if (!ins->objectid) {
4538 /* we found what we needed */
4539 if (ins->objectid) {
4540 if (!(data & BTRFS_BLOCK_GROUP_DATA))
4541 trans->block_group = block_group->key.objectid;
4543 btrfs_put_block_group(block_group);
4550 static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
4551 int dump_block_groups)
4553 struct btrfs_block_group_cache *cache;
4556 spin_lock(&info->lock);
4557 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
4558 (unsigned long long)(info->total_bytes - info->bytes_used -
4559 info->bytes_pinned - info->bytes_reserved -
4561 (info->full) ? "" : "not ");
4562 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
4563 " may_use=%llu, used=%llu, root=%llu, super=%llu, reserved=%llu"
4565 (unsigned long long)info->total_bytes,
4566 (unsigned long long)info->bytes_pinned,
4567 (unsigned long long)info->bytes_delalloc,
4568 (unsigned long long)info->bytes_may_use,
4569 (unsigned long long)info->bytes_used,
4570 (unsigned long long)info->bytes_root,
4571 (unsigned long long)info->bytes_super,
4572 (unsigned long long)info->bytes_reserved);
4573 spin_unlock(&info->lock);
4575 if (!dump_block_groups)
4578 down_read(&info->groups_sem);
4580 list_for_each_entry(cache, &info->block_groups[index], list) {
4581 spin_lock(&cache->lock);
4582 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
4583 "%llu pinned %llu reserved\n",
4584 (unsigned long long)cache->key.objectid,
4585 (unsigned long long)cache->key.offset,
4586 (unsigned long long)btrfs_block_group_used(&cache->item),
4587 (unsigned long long)cache->pinned,
4588 (unsigned long long)cache->reserved);
4589 btrfs_dump_free_space(cache, bytes);
4590 spin_unlock(&cache->lock);
4592 if (++index < BTRFS_NR_RAID_TYPES)
4594 up_read(&info->groups_sem);
4597 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
4598 struct btrfs_root *root,
4599 u64 num_bytes, u64 min_alloc_size,
4600 u64 empty_size, u64 hint_byte,
4601 u64 search_end, struct btrfs_key *ins,
4605 u64 search_start = 0;
4607 data = btrfs_get_alloc_profile(root, data);
4610 * the only place that sets empty_size is btrfs_realloc_node, which
4611 * is not called recursively on allocations
4613 if (empty_size || root->ref_cows)
4614 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
4615 num_bytes + 2 * 1024 * 1024, data, 0);
4617 WARN_ON(num_bytes < root->sectorsize);
4618 ret = find_free_extent(trans, root, num_bytes, empty_size,
4619 search_start, search_end, hint_byte, ins,
4620 trans->alloc_exclude_start,
4621 trans->alloc_exclude_nr, data);
4623 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
4624 num_bytes = num_bytes >> 1;
4625 num_bytes = num_bytes & ~(root->sectorsize - 1);
4626 num_bytes = max(num_bytes, min_alloc_size);
4627 do_chunk_alloc(trans, root->fs_info->extent_root,
4628 num_bytes, data, 1);
4631 if (ret == -ENOSPC) {
4632 struct btrfs_space_info *sinfo;
4634 sinfo = __find_space_info(root->fs_info, data);
4635 printk(KERN_ERR "btrfs allocation failed flags %llu, "
4636 "wanted %llu\n", (unsigned long long)data,
4637 (unsigned long long)num_bytes);
4638 dump_space_info(sinfo, num_bytes, 1);
4644 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
4646 struct btrfs_block_group_cache *cache;
4649 cache = btrfs_lookup_block_group(root->fs_info, start);
4651 printk(KERN_ERR "Unable to find block group for %llu\n",
4652 (unsigned long long)start);
4656 ret = btrfs_discard_extent(root, start, len);
4658 btrfs_add_free_space(cache, start, len);
4659 update_reserved_extents(cache, len, 0);
4660 btrfs_put_block_group(cache);
4665 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4666 struct btrfs_root *root,
4667 u64 parent, u64 root_objectid,
4668 u64 flags, u64 owner, u64 offset,
4669 struct btrfs_key *ins, int ref_mod)
4672 struct btrfs_fs_info *fs_info = root->fs_info;
4673 struct btrfs_extent_item *extent_item;
4674 struct btrfs_extent_inline_ref *iref;
4675 struct btrfs_path *path;
4676 struct extent_buffer *leaf;
4681 type = BTRFS_SHARED_DATA_REF_KEY;
4683 type = BTRFS_EXTENT_DATA_REF_KEY;
4685 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
4687 path = btrfs_alloc_path();
4690 path->leave_spinning = 1;
4691 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4695 leaf = path->nodes[0];
4696 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4697 struct btrfs_extent_item);
4698 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
4699 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4700 btrfs_set_extent_flags(leaf, extent_item,
4701 flags | BTRFS_EXTENT_FLAG_DATA);
4703 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4704 btrfs_set_extent_inline_ref_type(leaf, iref, type);
4706 struct btrfs_shared_data_ref *ref;
4707 ref = (struct btrfs_shared_data_ref *)(iref + 1);
4708 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4709 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
4711 struct btrfs_extent_data_ref *ref;
4712 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
4713 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
4714 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
4715 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
4716 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
4719 btrfs_mark_buffer_dirty(path->nodes[0]);
4720 btrfs_free_path(path);
4722 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4725 printk(KERN_ERR "btrfs update block group failed for %llu "
4726 "%llu\n", (unsigned long long)ins->objectid,
4727 (unsigned long long)ins->offset);
4733 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
4734 struct btrfs_root *root,
4735 u64 parent, u64 root_objectid,
4736 u64 flags, struct btrfs_disk_key *key,
4737 int level, struct btrfs_key *ins)
4740 struct btrfs_fs_info *fs_info = root->fs_info;
4741 struct btrfs_extent_item *extent_item;
4742 struct btrfs_tree_block_info *block_info;
4743 struct btrfs_extent_inline_ref *iref;
4744 struct btrfs_path *path;
4745 struct extent_buffer *leaf;
4746 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
4748 path = btrfs_alloc_path();
4751 path->leave_spinning = 1;
4752 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4756 leaf = path->nodes[0];
4757 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4758 struct btrfs_extent_item);
4759 btrfs_set_extent_refs(leaf, extent_item, 1);
4760 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4761 btrfs_set_extent_flags(leaf, extent_item,
4762 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
4763 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
4765 btrfs_set_tree_block_key(leaf, block_info, key);
4766 btrfs_set_tree_block_level(leaf, block_info, level);
4768 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
4770 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4771 btrfs_set_extent_inline_ref_type(leaf, iref,
4772 BTRFS_SHARED_BLOCK_REF_KEY);
4773 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4775 btrfs_set_extent_inline_ref_type(leaf, iref,
4776 BTRFS_TREE_BLOCK_REF_KEY);
4777 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
4780 btrfs_mark_buffer_dirty(leaf);
4781 btrfs_free_path(path);
4783 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4786 printk(KERN_ERR "btrfs update block group failed for %llu "
4787 "%llu\n", (unsigned long long)ins->objectid,
4788 (unsigned long long)ins->offset);
4794 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4795 struct btrfs_root *root,
4796 u64 root_objectid, u64 owner,
4797 u64 offset, struct btrfs_key *ins)
4801 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
4803 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
4804 0, root_objectid, owner, offset,
4805 BTRFS_ADD_DELAYED_EXTENT, NULL);
4810 * this is used by the tree logging recovery code. It records that
4811 * an extent has been allocated and makes sure to clear the free
4812 * space cache bits as well
4814 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
4815 struct btrfs_root *root,
4816 u64 root_objectid, u64 owner, u64 offset,
4817 struct btrfs_key *ins)
4820 struct btrfs_block_group_cache *block_group;
4821 struct btrfs_caching_control *caching_ctl;
4822 u64 start = ins->objectid;
4823 u64 num_bytes = ins->offset;
4825 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
4826 cache_block_group(block_group);
4827 caching_ctl = get_caching_control(block_group);
4830 BUG_ON(!block_group_cache_done(block_group));
4831 ret = btrfs_remove_free_space(block_group, start, num_bytes);
4834 mutex_lock(&caching_ctl->mutex);
4836 if (start >= caching_ctl->progress) {
4837 ret = add_excluded_extent(root, start, num_bytes);
4839 } else if (start + num_bytes <= caching_ctl->progress) {
4840 ret = btrfs_remove_free_space(block_group,
4844 num_bytes = caching_ctl->progress - start;
4845 ret = btrfs_remove_free_space(block_group,
4849 start = caching_ctl->progress;
4850 num_bytes = ins->objectid + ins->offset -
4851 caching_ctl->progress;
4852 ret = add_excluded_extent(root, start, num_bytes);
4856 mutex_unlock(&caching_ctl->mutex);
4857 put_caching_control(caching_ctl);
4860 update_reserved_extents(block_group, ins->offset, 1);
4861 btrfs_put_block_group(block_group);
4862 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
4863 0, owner, offset, ins, 1);
4868 * finds a free extent and does all the dirty work required for allocation
4869 * returns the key for the extent through ins, and a tree buffer for
4870 * the first block of the extent through buf.
4872 * returns 0 if everything worked, non-zero otherwise.
4874 static int alloc_tree_block(struct btrfs_trans_handle *trans,
4875 struct btrfs_root *root,
4876 u64 num_bytes, u64 parent, u64 root_objectid,
4877 struct btrfs_disk_key *key, int level,
4878 u64 empty_size, u64 hint_byte, u64 search_end,
4879 struct btrfs_key *ins)
4884 ret = btrfs_reserve_extent(trans, root, num_bytes, num_bytes,
4885 empty_size, hint_byte, search_end,
4890 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4892 parent = ins->objectid;
4893 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4897 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
4898 struct btrfs_delayed_extent_op *extent_op;
4899 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
4902 memcpy(&extent_op->key, key, sizeof(extent_op->key));
4904 memset(&extent_op->key, 0, sizeof(extent_op->key));
4905 extent_op->flags_to_set = flags;
4906 extent_op->update_key = 1;
4907 extent_op->update_flags = 1;
4908 extent_op->is_data = 0;
4910 ret = btrfs_add_delayed_tree_ref(trans, ins->objectid,
4911 ins->offset, parent, root_objectid,
4912 level, BTRFS_ADD_DELAYED_EXTENT,
4917 if (root_objectid == root->root_key.objectid) {
4919 spin_lock(&root->node_lock);
4920 used = btrfs_root_used(&root->root_item) + num_bytes;
4921 btrfs_set_root_used(&root->root_item, used);
4922 spin_unlock(&root->node_lock);
4927 struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
4928 struct btrfs_root *root,
4929 u64 bytenr, u32 blocksize,
4932 struct extent_buffer *buf;
4934 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
4936 return ERR_PTR(-ENOMEM);
4937 btrfs_set_header_generation(buf, trans->transid);
4938 btrfs_set_buffer_lockdep_class(buf, level);
4939 btrfs_tree_lock(buf);
4940 clean_tree_block(trans, root, buf);
4942 btrfs_set_lock_blocking(buf);
4943 btrfs_set_buffer_uptodate(buf);
4945 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
4947 * we allow two log transactions at a time, use different
4948 * EXENT bit to differentiate dirty pages.
4950 if (root->log_transid % 2 == 0)
4951 set_extent_dirty(&root->dirty_log_pages, buf->start,
4952 buf->start + buf->len - 1, GFP_NOFS);
4954 set_extent_new(&root->dirty_log_pages, buf->start,
4955 buf->start + buf->len - 1, GFP_NOFS);
4957 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
4958 buf->start + buf->len - 1, GFP_NOFS);
4960 trans->blocks_used++;
4961 /* this returns a buffer locked for blocking */
4966 * helper function to allocate a block for a given tree
4967 * returns the tree buffer or NULL.
4969 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
4970 struct btrfs_root *root, u32 blocksize,
4971 u64 parent, u64 root_objectid,
4972 struct btrfs_disk_key *key, int level,
4973 u64 hint, u64 empty_size)
4975 struct btrfs_key ins;
4977 struct extent_buffer *buf;
4979 ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid,
4980 key, level, empty_size, hint, (u64)-1, &ins);
4983 return ERR_PTR(ret);
4986 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
4991 struct walk_control {
4992 u64 refs[BTRFS_MAX_LEVEL];
4993 u64 flags[BTRFS_MAX_LEVEL];
4994 struct btrfs_key update_progress;
5004 #define DROP_REFERENCE 1
5005 #define UPDATE_BACKREF 2
5007 static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5008 struct btrfs_root *root,
5009 struct walk_control *wc,
5010 struct btrfs_path *path)
5019 struct btrfs_key key;
5020 struct extent_buffer *eb;
5025 if (path->slots[wc->level] < wc->reada_slot) {
5026 wc->reada_count = wc->reada_count * 2 / 3;
5027 wc->reada_count = max(wc->reada_count, 2);
5029 wc->reada_count = wc->reada_count * 3 / 2;
5030 wc->reada_count = min_t(int, wc->reada_count,
5031 BTRFS_NODEPTRS_PER_BLOCK(root));
5034 eb = path->nodes[wc->level];
5035 nritems = btrfs_header_nritems(eb);
5036 blocksize = btrfs_level_size(root, wc->level - 1);
5038 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5039 if (nread >= wc->reada_count)
5043 bytenr = btrfs_node_blockptr(eb, slot);
5044 generation = btrfs_node_ptr_generation(eb, slot);
5046 if (slot == path->slots[wc->level])
5049 if (wc->stage == UPDATE_BACKREF &&
5050 generation <= root->root_key.offset)
5053 /* We don't lock the tree block, it's OK to be racy here */
5054 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5059 if (wc->stage == DROP_REFERENCE) {
5063 if (wc->level == 1 &&
5064 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5066 if (!wc->update_ref ||
5067 generation <= root->root_key.offset)
5069 btrfs_node_key_to_cpu(eb, &key, slot);
5070 ret = btrfs_comp_cpu_keys(&key,
5071 &wc->update_progress);
5075 if (wc->level == 1 &&
5076 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5080 ret = readahead_tree_block(root, bytenr, blocksize,
5084 last = bytenr + blocksize;
5087 wc->reada_slot = slot;
5091 * hepler to process tree block while walking down the tree.
5093 * when wc->stage == UPDATE_BACKREF, this function updates
5094 * back refs for pointers in the block.
5096 * NOTE: return value 1 means we should stop walking down.
5098 static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5099 struct btrfs_root *root,
5100 struct btrfs_path *path,
5101 struct walk_control *wc, int lookup_info)
5103 int level = wc->level;
5104 struct extent_buffer *eb = path->nodes[level];
5105 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5108 if (wc->stage == UPDATE_BACKREF &&
5109 btrfs_header_owner(eb) != root->root_key.objectid)
5113 * when reference count of tree block is 1, it won't increase
5114 * again. once full backref flag is set, we never clear it.
5117 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
5118 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
5119 BUG_ON(!path->locks[level]);
5120 ret = btrfs_lookup_extent_info(trans, root,
5125 BUG_ON(wc->refs[level] == 0);
5128 if (wc->stage == DROP_REFERENCE) {
5129 if (wc->refs[level] > 1)
5132 if (path->locks[level] && !wc->keep_locks) {
5133 btrfs_tree_unlock(eb);
5134 path->locks[level] = 0;
5139 /* wc->stage == UPDATE_BACKREF */
5140 if (!(wc->flags[level] & flag)) {
5141 BUG_ON(!path->locks[level]);
5142 ret = btrfs_inc_ref(trans, root, eb, 1);
5144 ret = btrfs_dec_ref(trans, root, eb, 0);
5146 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
5149 wc->flags[level] |= flag;
5153 * the block is shared by multiple trees, so it's not good to
5154 * keep the tree lock
5156 if (path->locks[level] && level > 0) {
5157 btrfs_tree_unlock(eb);
5158 path->locks[level] = 0;
5164 * hepler to process tree block pointer.
5166 * when wc->stage == DROP_REFERENCE, this function checks
5167 * reference count of the block pointed to. if the block
5168 * is shared and we need update back refs for the subtree
5169 * rooted at the block, this function changes wc->stage to
5170 * UPDATE_BACKREF. if the block is shared and there is no
5171 * need to update back, this function drops the reference
5174 * NOTE: return value 1 means we should stop walking down.
5176 static noinline int do_walk_down(struct btrfs_trans_handle *trans,
5177 struct btrfs_root *root,
5178 struct btrfs_path *path,
5179 struct walk_control *wc, int *lookup_info)
5185 struct btrfs_key key;
5186 struct extent_buffer *next;
5187 int level = wc->level;
5191 generation = btrfs_node_ptr_generation(path->nodes[level],
5192 path->slots[level]);
5194 * if the lower level block was created before the snapshot
5195 * was created, we know there is no need to update back refs
5198 if (wc->stage == UPDATE_BACKREF &&
5199 generation <= root->root_key.offset) {
5204 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
5205 blocksize = btrfs_level_size(root, level - 1);
5207 next = btrfs_find_tree_block(root, bytenr, blocksize);
5209 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
5214 btrfs_tree_lock(next);
5215 btrfs_set_lock_blocking(next);
5217 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5218 &wc->refs[level - 1],
5219 &wc->flags[level - 1]);
5221 BUG_ON(wc->refs[level - 1] == 0);
5224 if (wc->stage == DROP_REFERENCE) {
5225 if (wc->refs[level - 1] > 1) {
5227 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5230 if (!wc->update_ref ||
5231 generation <= root->root_key.offset)
5234 btrfs_node_key_to_cpu(path->nodes[level], &key,
5235 path->slots[level]);
5236 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
5240 wc->stage = UPDATE_BACKREF;
5241 wc->shared_level = level - 1;
5245 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5249 if (!btrfs_buffer_uptodate(next, generation)) {
5250 btrfs_tree_unlock(next);
5251 free_extent_buffer(next);
5257 if (reada && level == 1)
5258 reada_walk_down(trans, root, wc, path);
5259 next = read_tree_block(root, bytenr, blocksize, generation);
5260 btrfs_tree_lock(next);
5261 btrfs_set_lock_blocking(next);
5265 BUG_ON(level != btrfs_header_level(next));
5266 path->nodes[level] = next;
5267 path->slots[level] = 0;
5268 path->locks[level] = 1;
5274 wc->refs[level - 1] = 0;
5275 wc->flags[level - 1] = 0;
5276 if (wc->stage == DROP_REFERENCE) {
5277 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
5278 parent = path->nodes[level]->start;
5280 BUG_ON(root->root_key.objectid !=
5281 btrfs_header_owner(path->nodes[level]));
5285 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
5286 root->root_key.objectid, level - 1, 0);
5289 btrfs_tree_unlock(next);
5290 free_extent_buffer(next);
5296 * hepler to process tree block while walking up the tree.
5298 * when wc->stage == DROP_REFERENCE, this function drops
5299 * reference count on the block.
5301 * when wc->stage == UPDATE_BACKREF, this function changes
5302 * wc->stage back to DROP_REFERENCE if we changed wc->stage
5303 * to UPDATE_BACKREF previously while processing the block.
5305 * NOTE: return value 1 means we should stop walking up.
5307 static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
5308 struct btrfs_root *root,
5309 struct btrfs_path *path,
5310 struct walk_control *wc)
5313 int level = wc->level;
5314 struct extent_buffer *eb = path->nodes[level];
5317 if (wc->stage == UPDATE_BACKREF) {
5318 BUG_ON(wc->shared_level < level);
5319 if (level < wc->shared_level)
5322 ret = find_next_key(path, level + 1, &wc->update_progress);
5326 wc->stage = DROP_REFERENCE;
5327 wc->shared_level = -1;
5328 path->slots[level] = 0;
5331 * check reference count again if the block isn't locked.
5332 * we should start walking down the tree again if reference
5335 if (!path->locks[level]) {
5337 btrfs_tree_lock(eb);
5338 btrfs_set_lock_blocking(eb);
5339 path->locks[level] = 1;
5341 ret = btrfs_lookup_extent_info(trans, root,
5346 BUG_ON(wc->refs[level] == 0);
5347 if (wc->refs[level] == 1) {
5348 btrfs_tree_unlock(eb);
5349 path->locks[level] = 0;
5355 /* wc->stage == DROP_REFERENCE */
5356 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
5358 if (wc->refs[level] == 1) {
5360 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5361 ret = btrfs_dec_ref(trans, root, eb, 1);
5363 ret = btrfs_dec_ref(trans, root, eb, 0);
5366 /* make block locked assertion in clean_tree_block happy */
5367 if (!path->locks[level] &&
5368 btrfs_header_generation(eb) == trans->transid) {
5369 btrfs_tree_lock(eb);
5370 btrfs_set_lock_blocking(eb);
5371 path->locks[level] = 1;
5373 clean_tree_block(trans, root, eb);
5376 if (eb == root->node) {
5377 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5380 BUG_ON(root->root_key.objectid !=
5381 btrfs_header_owner(eb));
5383 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5384 parent = path->nodes[level + 1]->start;
5386 BUG_ON(root->root_key.objectid !=
5387 btrfs_header_owner(path->nodes[level + 1]));
5390 ret = btrfs_free_extent(trans, root, eb->start, eb->len, parent,
5391 root->root_key.objectid, level, 0);
5394 wc->refs[level] = 0;
5395 wc->flags[level] = 0;
5399 static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
5400 struct btrfs_root *root,
5401 struct btrfs_path *path,
5402 struct walk_control *wc)
5404 int level = wc->level;
5405 int lookup_info = 1;
5408 while (level >= 0) {
5409 ret = walk_down_proc(trans, root, path, wc, lookup_info);
5416 if (path->slots[level] >=
5417 btrfs_header_nritems(path->nodes[level]))
5420 ret = do_walk_down(trans, root, path, wc, &lookup_info);
5422 path->slots[level]++;
5431 static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
5432 struct btrfs_root *root,
5433 struct btrfs_path *path,
5434 struct walk_control *wc, int max_level)
5436 int level = wc->level;
5439 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
5440 while (level < max_level && path->nodes[level]) {
5442 if (path->slots[level] + 1 <
5443 btrfs_header_nritems(path->nodes[level])) {
5444 path->slots[level]++;
5447 ret = walk_up_proc(trans, root, path, wc);
5451 if (path->locks[level]) {
5452 btrfs_tree_unlock(path->nodes[level]);
5453 path->locks[level] = 0;
5455 free_extent_buffer(path->nodes[level]);
5456 path->nodes[level] = NULL;
5464 * drop a subvolume tree.
5466 * this function traverses the tree freeing any blocks that only
5467 * referenced by the tree.
5469 * when a shared tree block is found. this function decreases its
5470 * reference count by one. if update_ref is true, this function
5471 * also make sure backrefs for the shared block and all lower level
5472 * blocks are properly updated.
5474 int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref)
5476 struct btrfs_path *path;
5477 struct btrfs_trans_handle *trans;
5478 struct btrfs_root *tree_root = root->fs_info->tree_root;
5479 struct btrfs_root_item *root_item = &root->root_item;
5480 struct walk_control *wc;
5481 struct btrfs_key key;
5486 path = btrfs_alloc_path();
5489 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5492 trans = btrfs_start_transaction(tree_root, 1);
5494 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
5495 level = btrfs_header_level(root->node);
5496 path->nodes[level] = btrfs_lock_root_node(root);
5497 btrfs_set_lock_blocking(path->nodes[level]);
5498 path->slots[level] = 0;
5499 path->locks[level] = 1;
5500 memset(&wc->update_progress, 0,
5501 sizeof(wc->update_progress));
5503 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
5504 memcpy(&wc->update_progress, &key,
5505 sizeof(wc->update_progress));
5507 level = root_item->drop_level;
5509 path->lowest_level = level;
5510 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5511 path->lowest_level = 0;
5519 * unlock our path, this is safe because only this
5520 * function is allowed to delete this snapshot
5522 btrfs_unlock_up_safe(path, 0);
5524 level = btrfs_header_level(root->node);
5526 btrfs_tree_lock(path->nodes[level]);
5527 btrfs_set_lock_blocking(path->nodes[level]);
5529 ret = btrfs_lookup_extent_info(trans, root,
5530 path->nodes[level]->start,
5531 path->nodes[level]->len,
5535 BUG_ON(wc->refs[level] == 0);
5537 if (level == root_item->drop_level)
5540 btrfs_tree_unlock(path->nodes[level]);
5541 WARN_ON(wc->refs[level] != 1);
5547 wc->shared_level = -1;
5548 wc->stage = DROP_REFERENCE;
5549 wc->update_ref = update_ref;
5551 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
5554 ret = walk_down_tree(trans, root, path, wc);
5560 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5567 BUG_ON(wc->stage != DROP_REFERENCE);
5571 if (wc->stage == DROP_REFERENCE) {
5573 btrfs_node_key(path->nodes[level],
5574 &root_item->drop_progress,
5575 path->slots[level]);
5576 root_item->drop_level = level;
5579 BUG_ON(wc->level == 0);
5580 if (trans->transaction->in_commit ||
5581 trans->transaction->delayed_refs.flushing) {
5582 ret = btrfs_update_root(trans, tree_root,
5587 btrfs_end_transaction(trans, tree_root);
5588 trans = btrfs_start_transaction(tree_root, 1);
5590 unsigned long update;
5591 update = trans->delayed_ref_updates;
5592 trans->delayed_ref_updates = 0;
5594 btrfs_run_delayed_refs(trans, tree_root,
5598 btrfs_release_path(root, path);
5601 ret = btrfs_del_root(trans, tree_root, &root->root_key);
5604 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
5605 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
5609 ret = btrfs_del_orphan_item(trans, tree_root,
5610 root->root_key.objectid);
5615 if (root->in_radix) {
5616 btrfs_free_fs_root(tree_root->fs_info, root);
5618 free_extent_buffer(root->node);
5619 free_extent_buffer(root->commit_root);
5623 btrfs_end_transaction(trans, tree_root);
5625 btrfs_free_path(path);
5630 * drop subtree rooted at tree block 'node'.
5632 * NOTE: this function will unlock and release tree block 'node'
5634 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
5635 struct btrfs_root *root,
5636 struct extent_buffer *node,
5637 struct extent_buffer *parent)
5639 struct btrfs_path *path;
5640 struct walk_control *wc;
5646 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5648 path = btrfs_alloc_path();
5651 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5654 btrfs_assert_tree_locked(parent);
5655 parent_level = btrfs_header_level(parent);
5656 extent_buffer_get(parent);
5657 path->nodes[parent_level] = parent;
5658 path->slots[parent_level] = btrfs_header_nritems(parent);
5660 btrfs_assert_tree_locked(node);
5661 level = btrfs_header_level(node);
5662 path->nodes[level] = node;
5663 path->slots[level] = 0;
5664 path->locks[level] = 1;
5666 wc->refs[parent_level] = 1;
5667 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5669 wc->shared_level = -1;
5670 wc->stage = DROP_REFERENCE;
5673 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
5676 wret = walk_down_tree(trans, root, path, wc);
5682 wret = walk_up_tree(trans, root, path, wc, parent_level);
5690 btrfs_free_path(path);
5695 static unsigned long calc_ra(unsigned long start, unsigned long last,
5698 return min(last, start + nr - 1);
5701 static noinline int relocate_inode_pages(struct inode *inode, u64 start,
5706 unsigned long first_index;
5707 unsigned long last_index;
5710 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5711 struct file_ra_state *ra;
5712 struct btrfs_ordered_extent *ordered;
5713 unsigned int total_read = 0;
5714 unsigned int total_dirty = 0;
5717 ra = kzalloc(sizeof(*ra), GFP_NOFS);
5719 mutex_lock(&inode->i_mutex);
5720 first_index = start >> PAGE_CACHE_SHIFT;
5721 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
5723 /* make sure the dirty trick played by the caller work */
5724 ret = invalidate_inode_pages2_range(inode->i_mapping,
5725 first_index, last_index);
5729 file_ra_state_init(ra, inode->i_mapping);
5731 for (i = first_index ; i <= last_index; i++) {
5732 if (total_read % ra->ra_pages == 0) {
5733 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
5734 calc_ra(i, last_index, ra->ra_pages));
5738 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
5740 page = grab_cache_page(inode->i_mapping, i);
5745 if (!PageUptodate(page)) {
5746 btrfs_readpage(NULL, page);
5748 if (!PageUptodate(page)) {
5750 page_cache_release(page);
5755 wait_on_page_writeback(page);
5757 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
5758 page_end = page_start + PAGE_CACHE_SIZE - 1;
5759 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
5761 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5763 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5765 page_cache_release(page);
5766 btrfs_start_ordered_extent(inode, ordered, 1);
5767 btrfs_put_ordered_extent(ordered);
5770 set_page_extent_mapped(page);
5772 if (i == first_index)
5773 set_extent_bits(io_tree, page_start, page_end,
5774 EXTENT_BOUNDARY, GFP_NOFS);
5775 btrfs_set_extent_delalloc(inode, page_start, page_end);
5777 set_page_dirty(page);
5780 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5782 page_cache_release(page);
5787 mutex_unlock(&inode->i_mutex);
5788 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
5792 static noinline int relocate_data_extent(struct inode *reloc_inode,
5793 struct btrfs_key *extent_key,
5796 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5797 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
5798 struct extent_map *em;
5799 u64 start = extent_key->objectid - offset;
5800 u64 end = start + extent_key->offset - 1;
5802 em = alloc_extent_map(GFP_NOFS);
5803 BUG_ON(!em || IS_ERR(em));
5806 em->len = extent_key->offset;
5807 em->block_len = extent_key->offset;
5808 em->block_start = extent_key->objectid;
5809 em->bdev = root->fs_info->fs_devices->latest_bdev;
5810 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5812 /* setup extent map to cheat btrfs_readpage */
5813 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
5816 write_lock(&em_tree->lock);
5817 ret = add_extent_mapping(em_tree, em);
5818 write_unlock(&em_tree->lock);
5819 if (ret != -EEXIST) {
5820 free_extent_map(em);
5823 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
5825 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
5827 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
5830 struct btrfs_ref_path {
5832 u64 nodes[BTRFS_MAX_LEVEL];
5834 u64 root_generation;
5841 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
5842 u64 new_nodes[BTRFS_MAX_LEVEL];
5845 struct disk_extent {
5856 static int is_cowonly_root(u64 root_objectid)
5858 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
5859 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
5860 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
5861 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
5862 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5863 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
5868 static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
5869 struct btrfs_root *extent_root,
5870 struct btrfs_ref_path *ref_path,
5873 struct extent_buffer *leaf;
5874 struct btrfs_path *path;
5875 struct btrfs_extent_ref *ref;
5876 struct btrfs_key key;
5877 struct btrfs_key found_key;
5883 path = btrfs_alloc_path();
5888 ref_path->lowest_level = -1;
5889 ref_path->current_level = -1;
5890 ref_path->shared_level = -1;
5894 level = ref_path->current_level - 1;
5895 while (level >= -1) {
5897 if (level < ref_path->lowest_level)
5901 bytenr = ref_path->nodes[level];
5903 bytenr = ref_path->extent_start;
5904 BUG_ON(bytenr == 0);
5906 parent = ref_path->nodes[level + 1];
5907 ref_path->nodes[level + 1] = 0;
5908 ref_path->current_level = level;
5909 BUG_ON(parent == 0);
5911 key.objectid = bytenr;
5912 key.offset = parent + 1;
5913 key.type = BTRFS_EXTENT_REF_KEY;
5915 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
5920 leaf = path->nodes[0];
5921 nritems = btrfs_header_nritems(leaf);
5922 if (path->slots[0] >= nritems) {
5923 ret = btrfs_next_leaf(extent_root, path);
5928 leaf = path->nodes[0];
5931 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5932 if (found_key.objectid == bytenr &&
5933 found_key.type == BTRFS_EXTENT_REF_KEY) {
5934 if (level < ref_path->shared_level)
5935 ref_path->shared_level = level;
5940 btrfs_release_path(extent_root, path);
5943 /* reached lowest level */
5947 level = ref_path->current_level;
5948 while (level < BTRFS_MAX_LEVEL - 1) {
5952 bytenr = ref_path->nodes[level];
5954 bytenr = ref_path->extent_start;
5956 BUG_ON(bytenr == 0);
5958 key.objectid = bytenr;
5960 key.type = BTRFS_EXTENT_REF_KEY;
5962 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
5966 leaf = path->nodes[0];
5967 nritems = btrfs_header_nritems(leaf);
5968 if (path->slots[0] >= nritems) {
5969 ret = btrfs_next_leaf(extent_root, path);
5973 /* the extent was freed by someone */
5974 if (ref_path->lowest_level == level)
5976 btrfs_release_path(extent_root, path);
5979 leaf = path->nodes[0];
5982 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5983 if (found_key.objectid != bytenr ||
5984 found_key.type != BTRFS_EXTENT_REF_KEY) {
5985 /* the extent was freed by someone */
5986 if (ref_path->lowest_level == level) {
5990 btrfs_release_path(extent_root, path);
5994 ref = btrfs_item_ptr(leaf, path->slots[0],
5995 struct btrfs_extent_ref);
5996 ref_objectid = btrfs_ref_objectid(leaf, ref);
5997 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5999 level = (int)ref_objectid;
6000 BUG_ON(level >= BTRFS_MAX_LEVEL);
6001 ref_path->lowest_level = level;
6002 ref_path->current_level = level;
6003 ref_path->nodes[level] = bytenr;
6005 WARN_ON(ref_objectid != level);
6008 WARN_ON(level != -1);
6012 if (ref_path->lowest_level == level) {
6013 ref_path->owner_objectid = ref_objectid;
6014 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
6018 * the block is tree root or the block isn't in reference
6021 if (found_key.objectid == found_key.offset ||
6022 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
6023 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6024 ref_path->root_generation =
6025 btrfs_ref_generation(leaf, ref);
6027 /* special reference from the tree log */
6028 ref_path->nodes[0] = found_key.offset;
6029 ref_path->current_level = 0;
6036 BUG_ON(ref_path->nodes[level] != 0);
6037 ref_path->nodes[level] = found_key.offset;
6038 ref_path->current_level = level;
6041 * the reference was created in the running transaction,
6042 * no need to continue walking up.
6044 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
6045 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6046 ref_path->root_generation =
6047 btrfs_ref_generation(leaf, ref);
6052 btrfs_release_path(extent_root, path);
6055 /* reached max tree level, but no tree root found. */
6058 btrfs_free_path(path);
6062 static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
6063 struct btrfs_root *extent_root,
6064 struct btrfs_ref_path *ref_path,
6067 memset(ref_path, 0, sizeof(*ref_path));
6068 ref_path->extent_start = extent_start;
6070 return __next_ref_path(trans, extent_root, ref_path, 1);
6073 static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
6074 struct btrfs_root *extent_root,
6075 struct btrfs_ref_path *ref_path)
6077 return __next_ref_path(trans, extent_root, ref_path, 0);
6080 static noinline int get_new_locations(struct inode *reloc_inode,
6081 struct btrfs_key *extent_key,
6082 u64 offset, int no_fragment,
6083 struct disk_extent **extents,
6086 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6087 struct btrfs_path *path;
6088 struct btrfs_file_extent_item *fi;
6089 struct extent_buffer *leaf;
6090 struct disk_extent *exts = *extents;
6091 struct btrfs_key found_key;
6096 int max = *nr_extents;
6099 WARN_ON(!no_fragment && *extents);
6102 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
6107 path = btrfs_alloc_path();
6110 cur_pos = extent_key->objectid - offset;
6111 last_byte = extent_key->objectid + extent_key->offset;
6112 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
6122 leaf = path->nodes[0];
6123 nritems = btrfs_header_nritems(leaf);
6124 if (path->slots[0] >= nritems) {
6125 ret = btrfs_next_leaf(root, path);
6130 leaf = path->nodes[0];
6133 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6134 if (found_key.offset != cur_pos ||
6135 found_key.type != BTRFS_EXTENT_DATA_KEY ||
6136 found_key.objectid != reloc_inode->i_ino)
6139 fi = btrfs_item_ptr(leaf, path->slots[0],
6140 struct btrfs_file_extent_item);
6141 if (btrfs_file_extent_type(leaf, fi) !=
6142 BTRFS_FILE_EXTENT_REG ||
6143 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6147 struct disk_extent *old = exts;
6149 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
6150 memcpy(exts, old, sizeof(*exts) * nr);
6151 if (old != *extents)
6155 exts[nr].disk_bytenr =
6156 btrfs_file_extent_disk_bytenr(leaf, fi);
6157 exts[nr].disk_num_bytes =
6158 btrfs_file_extent_disk_num_bytes(leaf, fi);
6159 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
6160 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6161 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6162 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
6163 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
6164 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
6166 BUG_ON(exts[nr].offset > 0);
6167 BUG_ON(exts[nr].compression || exts[nr].encryption);
6168 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
6170 cur_pos += exts[nr].num_bytes;
6173 if (cur_pos + offset >= last_byte)
6183 BUG_ON(cur_pos + offset > last_byte);
6184 if (cur_pos + offset < last_byte) {
6190 btrfs_free_path(path);
6192 if (exts != *extents)
6201 static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
6202 struct btrfs_root *root,
6203 struct btrfs_path *path,
6204 struct btrfs_key *extent_key,
6205 struct btrfs_key *leaf_key,
6206 struct btrfs_ref_path *ref_path,
6207 struct disk_extent *new_extents,
6210 struct extent_buffer *leaf;
6211 struct btrfs_file_extent_item *fi;
6212 struct inode *inode = NULL;
6213 struct btrfs_key key;
6218 u64 search_end = (u64)-1;
6221 int extent_locked = 0;
6225 memcpy(&key, leaf_key, sizeof(key));
6226 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
6227 if (key.objectid < ref_path->owner_objectid ||
6228 (key.objectid == ref_path->owner_objectid &&
6229 key.type < BTRFS_EXTENT_DATA_KEY)) {
6230 key.objectid = ref_path->owner_objectid;
6231 key.type = BTRFS_EXTENT_DATA_KEY;
6237 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
6241 leaf = path->nodes[0];
6242 nritems = btrfs_header_nritems(leaf);
6244 if (extent_locked && ret > 0) {
6246 * the file extent item was modified by someone
6247 * before the extent got locked.
6249 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6250 lock_end, GFP_NOFS);
6254 if (path->slots[0] >= nritems) {
6255 if (++nr_scaned > 2)
6258 BUG_ON(extent_locked);
6259 ret = btrfs_next_leaf(root, path);
6264 leaf = path->nodes[0];
6265 nritems = btrfs_header_nritems(leaf);
6268 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6270 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
6271 if ((key.objectid > ref_path->owner_objectid) ||
6272 (key.objectid == ref_path->owner_objectid &&
6273 key.type > BTRFS_EXTENT_DATA_KEY) ||
6274 key.offset >= search_end)
6278 if (inode && key.objectid != inode->i_ino) {
6279 BUG_ON(extent_locked);
6280 btrfs_release_path(root, path);
6281 mutex_unlock(&inode->i_mutex);
6287 if (key.type != BTRFS_EXTENT_DATA_KEY) {
6292 fi = btrfs_item_ptr(leaf, path->slots[0],
6293 struct btrfs_file_extent_item);
6294 extent_type = btrfs_file_extent_type(leaf, fi);
6295 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
6296 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
6297 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
6298 extent_key->objectid)) {
6304 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6305 ext_offset = btrfs_file_extent_offset(leaf, fi);
6307 if (search_end == (u64)-1) {
6308 search_end = key.offset - ext_offset +
6309 btrfs_file_extent_ram_bytes(leaf, fi);
6312 if (!extent_locked) {
6313 lock_start = key.offset;
6314 lock_end = lock_start + num_bytes - 1;
6316 if (lock_start > key.offset ||
6317 lock_end + 1 < key.offset + num_bytes) {
6318 unlock_extent(&BTRFS_I(inode)->io_tree,
6319 lock_start, lock_end, GFP_NOFS);
6325 btrfs_release_path(root, path);
6327 inode = btrfs_iget_locked(root->fs_info->sb,
6328 key.objectid, root);
6329 if (inode->i_state & I_NEW) {
6330 BTRFS_I(inode)->root = root;
6331 BTRFS_I(inode)->location.objectid =
6333 BTRFS_I(inode)->location.type =
6334 BTRFS_INODE_ITEM_KEY;
6335 BTRFS_I(inode)->location.offset = 0;
6336 btrfs_read_locked_inode(inode);
6337 unlock_new_inode(inode);
6340 * some code call btrfs_commit_transaction while
6341 * holding the i_mutex, so we can't use mutex_lock
6344 if (is_bad_inode(inode) ||
6345 !mutex_trylock(&inode->i_mutex)) {
6348 key.offset = (u64)-1;
6353 if (!extent_locked) {
6354 struct btrfs_ordered_extent *ordered;
6356 btrfs_release_path(root, path);
6358 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6359 lock_end, GFP_NOFS);
6360 ordered = btrfs_lookup_first_ordered_extent(inode,
6363 ordered->file_offset <= lock_end &&
6364 ordered->file_offset + ordered->len > lock_start) {
6365 unlock_extent(&BTRFS_I(inode)->io_tree,
6366 lock_start, lock_end, GFP_NOFS);
6367 btrfs_start_ordered_extent(inode, ordered, 1);
6368 btrfs_put_ordered_extent(ordered);
6369 key.offset += num_bytes;
6373 btrfs_put_ordered_extent(ordered);
6379 if (nr_extents == 1) {
6380 /* update extent pointer in place */
6381 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6382 new_extents[0].disk_bytenr);
6383 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6384 new_extents[0].disk_num_bytes);
6385 btrfs_mark_buffer_dirty(leaf);
6387 btrfs_drop_extent_cache(inode, key.offset,
6388 key.offset + num_bytes - 1, 0);
6390 ret = btrfs_inc_extent_ref(trans, root,
6391 new_extents[0].disk_bytenr,
6392 new_extents[0].disk_num_bytes,
6394 root->root_key.objectid,
6399 ret = btrfs_free_extent(trans, root,
6400 extent_key->objectid,
6403 btrfs_header_owner(leaf),
6404 btrfs_header_generation(leaf),
6408 btrfs_release_path(root, path);
6409 key.offset += num_bytes;
6417 * drop old extent pointer at first, then insert the
6418 * new pointers one bye one
6420 btrfs_release_path(root, path);
6421 ret = btrfs_drop_extents(trans, root, inode, key.offset,
6422 key.offset + num_bytes,
6423 key.offset, &alloc_hint);
6426 for (i = 0; i < nr_extents; i++) {
6427 if (ext_offset >= new_extents[i].num_bytes) {
6428 ext_offset -= new_extents[i].num_bytes;
6431 extent_len = min(new_extents[i].num_bytes -
6432 ext_offset, num_bytes);
6434 ret = btrfs_insert_empty_item(trans, root,
6439 leaf = path->nodes[0];
6440 fi = btrfs_item_ptr(leaf, path->slots[0],
6441 struct btrfs_file_extent_item);
6442 btrfs_set_file_extent_generation(leaf, fi,
6444 btrfs_set_file_extent_type(leaf, fi,
6445 BTRFS_FILE_EXTENT_REG);
6446 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6447 new_extents[i].disk_bytenr);
6448 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6449 new_extents[i].disk_num_bytes);
6450 btrfs_set_file_extent_ram_bytes(leaf, fi,
6451 new_extents[i].ram_bytes);
6453 btrfs_set_file_extent_compression(leaf, fi,
6454 new_extents[i].compression);
6455 btrfs_set_file_extent_encryption(leaf, fi,
6456 new_extents[i].encryption);
6457 btrfs_set_file_extent_other_encoding(leaf, fi,
6458 new_extents[i].other_encoding);
6460 btrfs_set_file_extent_num_bytes(leaf, fi,
6462 ext_offset += new_extents[i].offset;
6463 btrfs_set_file_extent_offset(leaf, fi,
6465 btrfs_mark_buffer_dirty(leaf);
6467 btrfs_drop_extent_cache(inode, key.offset,
6468 key.offset + extent_len - 1, 0);
6470 ret = btrfs_inc_extent_ref(trans, root,
6471 new_extents[i].disk_bytenr,
6472 new_extents[i].disk_num_bytes,
6474 root->root_key.objectid,
6475 trans->transid, key.objectid);
6477 btrfs_release_path(root, path);
6479 inode_add_bytes(inode, extent_len);
6482 num_bytes -= extent_len;
6483 key.offset += extent_len;
6488 BUG_ON(i >= nr_extents);
6492 if (extent_locked) {
6493 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6494 lock_end, GFP_NOFS);
6498 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
6499 key.offset >= search_end)
6506 btrfs_release_path(root, path);
6508 mutex_unlock(&inode->i_mutex);
6509 if (extent_locked) {
6510 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6511 lock_end, GFP_NOFS);
6518 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
6519 struct btrfs_root *root,
6520 struct extent_buffer *buf, u64 orig_start)
6525 BUG_ON(btrfs_header_generation(buf) != trans->transid);
6526 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6528 level = btrfs_header_level(buf);
6530 struct btrfs_leaf_ref *ref;
6531 struct btrfs_leaf_ref *orig_ref;
6533 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
6537 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
6539 btrfs_free_leaf_ref(root, orig_ref);
6543 ref->nritems = orig_ref->nritems;
6544 memcpy(ref->extents, orig_ref->extents,
6545 sizeof(ref->extents[0]) * ref->nritems);
6547 btrfs_free_leaf_ref(root, orig_ref);
6549 ref->root_gen = trans->transid;
6550 ref->bytenr = buf->start;
6551 ref->owner = btrfs_header_owner(buf);
6552 ref->generation = btrfs_header_generation(buf);
6554 ret = btrfs_add_leaf_ref(root, ref, 0);
6556 btrfs_free_leaf_ref(root, ref);
6561 static noinline int invalidate_extent_cache(struct btrfs_root *root,
6562 struct extent_buffer *leaf,
6563 struct btrfs_block_group_cache *group,
6564 struct btrfs_root *target_root)
6566 struct btrfs_key key;
6567 struct inode *inode = NULL;
6568 struct btrfs_file_extent_item *fi;
6569 struct extent_state *cached_state = NULL;
6571 u64 skip_objectid = 0;
6575 nritems = btrfs_header_nritems(leaf);
6576 for (i = 0; i < nritems; i++) {
6577 btrfs_item_key_to_cpu(leaf, &key, i);
6578 if (key.objectid == skip_objectid ||
6579 key.type != BTRFS_EXTENT_DATA_KEY)
6581 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6582 if (btrfs_file_extent_type(leaf, fi) ==
6583 BTRFS_FILE_EXTENT_INLINE)
6585 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6587 if (!inode || inode->i_ino != key.objectid) {
6589 inode = btrfs_ilookup(target_root->fs_info->sb,
6590 key.objectid, target_root, 1);
6593 skip_objectid = key.objectid;
6596 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6598 lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset,
6599 key.offset + num_bytes - 1, 0, &cached_state,
6601 btrfs_drop_extent_cache(inode, key.offset,
6602 key.offset + num_bytes - 1, 1);
6603 unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset,
6604 key.offset + num_bytes - 1, &cached_state,
6612 static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
6613 struct btrfs_root *root,
6614 struct extent_buffer *leaf,
6615 struct btrfs_block_group_cache *group,
6616 struct inode *reloc_inode)
6618 struct btrfs_key key;
6619 struct btrfs_key extent_key;
6620 struct btrfs_file_extent_item *fi;
6621 struct btrfs_leaf_ref *ref;
6622 struct disk_extent *new_extent;
6631 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
6632 BUG_ON(!new_extent);
6634 ref = btrfs_lookup_leaf_ref(root, leaf->start);
6638 nritems = btrfs_header_nritems(leaf);
6639 for (i = 0; i < nritems; i++) {
6640 btrfs_item_key_to_cpu(leaf, &key, i);
6641 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6643 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6644 if (btrfs_file_extent_type(leaf, fi) ==
6645 BTRFS_FILE_EXTENT_INLINE)
6647 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6648 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
6653 if (bytenr >= group->key.objectid + group->key.offset ||
6654 bytenr + num_bytes <= group->key.objectid)
6657 extent_key.objectid = bytenr;
6658 extent_key.offset = num_bytes;
6659 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
6661 ret = get_new_locations(reloc_inode, &extent_key,
6662 group->key.objectid, 1,
6663 &new_extent, &nr_extent);
6668 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
6669 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
6670 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
6671 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
6673 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6674 new_extent->disk_bytenr);
6675 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6676 new_extent->disk_num_bytes);
6677 btrfs_mark_buffer_dirty(leaf);
6679 ret = btrfs_inc_extent_ref(trans, root,
6680 new_extent->disk_bytenr,
6681 new_extent->disk_num_bytes,
6683 root->root_key.objectid,
6684 trans->transid, key.objectid);
6687 ret = btrfs_free_extent(trans, root,
6688 bytenr, num_bytes, leaf->start,
6689 btrfs_header_owner(leaf),
6690 btrfs_header_generation(leaf),
6696 BUG_ON(ext_index + 1 != ref->nritems);
6697 btrfs_free_leaf_ref(root, ref);
6701 int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
6702 struct btrfs_root *root)
6704 struct btrfs_root *reloc_root;
6707 if (root->reloc_root) {
6708 reloc_root = root->reloc_root;
6709 root->reloc_root = NULL;
6710 list_add(&reloc_root->dead_list,
6711 &root->fs_info->dead_reloc_roots);
6713 btrfs_set_root_bytenr(&reloc_root->root_item,
6714 reloc_root->node->start);
6715 btrfs_set_root_level(&root->root_item,
6716 btrfs_header_level(reloc_root->node));
6717 memset(&reloc_root->root_item.drop_progress, 0,
6718 sizeof(struct btrfs_disk_key));
6719 reloc_root->root_item.drop_level = 0;
6721 ret = btrfs_update_root(trans, root->fs_info->tree_root,
6722 &reloc_root->root_key,
6723 &reloc_root->root_item);
6729 int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
6731 struct btrfs_trans_handle *trans;
6732 struct btrfs_root *reloc_root;
6733 struct btrfs_root *prev_root = NULL;
6734 struct list_head dead_roots;
6738 INIT_LIST_HEAD(&dead_roots);
6739 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
6741 while (!list_empty(&dead_roots)) {
6742 reloc_root = list_entry(dead_roots.prev,
6743 struct btrfs_root, dead_list);
6744 list_del_init(&reloc_root->dead_list);
6746 BUG_ON(reloc_root->commit_root != NULL);
6748 trans = btrfs_join_transaction(root, 1);
6751 mutex_lock(&root->fs_info->drop_mutex);
6752 ret = btrfs_drop_snapshot(trans, reloc_root);
6755 mutex_unlock(&root->fs_info->drop_mutex);
6757 nr = trans->blocks_used;
6758 ret = btrfs_end_transaction(trans, root);
6760 btrfs_btree_balance_dirty(root, nr);
6763 free_extent_buffer(reloc_root->node);
6765 ret = btrfs_del_root(trans, root->fs_info->tree_root,
6766 &reloc_root->root_key);
6768 mutex_unlock(&root->fs_info->drop_mutex);
6770 nr = trans->blocks_used;
6771 ret = btrfs_end_transaction(trans, root);
6773 btrfs_btree_balance_dirty(root, nr);
6776 prev_root = reloc_root;
6779 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
6785 int btrfs_add_dead_reloc_root(struct btrfs_root *root)
6787 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
6791 int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
6793 struct btrfs_root *reloc_root;
6794 struct btrfs_trans_handle *trans;
6795 struct btrfs_key location;
6799 mutex_lock(&root->fs_info->tree_reloc_mutex);
6800 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
6802 found = !list_empty(&root->fs_info->dead_reloc_roots);
6803 mutex_unlock(&root->fs_info->tree_reloc_mutex);
6806 trans = btrfs_start_transaction(root, 1);
6808 ret = btrfs_commit_transaction(trans, root);
6812 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6813 location.offset = (u64)-1;
6814 location.type = BTRFS_ROOT_ITEM_KEY;
6816 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
6817 BUG_ON(!reloc_root);
6818 btrfs_orphan_cleanup(reloc_root);
6822 static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
6823 struct btrfs_root *root)
6825 struct btrfs_root *reloc_root;
6826 struct extent_buffer *eb;
6827 struct btrfs_root_item *root_item;
6828 struct btrfs_key root_key;
6831 BUG_ON(!root->ref_cows);
6832 if (root->reloc_root)
6835 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
6838 ret = btrfs_copy_root(trans, root, root->commit_root,
6839 &eb, BTRFS_TREE_RELOC_OBJECTID);
6842 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
6843 root_key.offset = root->root_key.objectid;
6844 root_key.type = BTRFS_ROOT_ITEM_KEY;
6846 memcpy(root_item, &root->root_item, sizeof(root_item));
6847 btrfs_set_root_refs(root_item, 0);
6848 btrfs_set_root_bytenr(root_item, eb->start);
6849 btrfs_set_root_level(root_item, btrfs_header_level(eb));
6850 btrfs_set_root_generation(root_item, trans->transid);
6852 btrfs_tree_unlock(eb);
6853 free_extent_buffer(eb);
6855 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
6856 &root_key, root_item);
6860 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
6862 BUG_ON(!reloc_root);
6863 reloc_root->last_trans = trans->transid;
6864 reloc_root->commit_root = NULL;
6865 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
6867 root->reloc_root = reloc_root;
6872 * Core function of space balance.
6874 * The idea is using reloc trees to relocate tree blocks in reference
6875 * counted roots. There is one reloc tree for each subvol, and all
6876 * reloc trees share same root key objectid. Reloc trees are snapshots
6877 * of the latest committed roots of subvols (root->commit_root).
6879 * To relocate a tree block referenced by a subvol, there are two steps.
6880 * COW the block through subvol's reloc tree, then update block pointer
6881 * in the subvol to point to the new block. Since all reloc trees share
6882 * same root key objectid, doing special handing for tree blocks owned
6883 * by them is easy. Once a tree block has been COWed in one reloc tree,
6884 * we can use the resulting new block directly when the same block is
6885 * required to COW again through other reloc trees. By this way, relocated
6886 * tree blocks are shared between reloc trees, so they are also shared
6889 static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
6890 struct btrfs_root *root,
6891 struct btrfs_path *path,
6892 struct btrfs_key *first_key,
6893 struct btrfs_ref_path *ref_path,
6894 struct btrfs_block_group_cache *group,
6895 struct inode *reloc_inode)
6897 struct btrfs_root *reloc_root;
6898 struct extent_buffer *eb = NULL;
6899 struct btrfs_key *keys;
6903 int lowest_level = 0;
6906 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
6907 lowest_level = ref_path->owner_objectid;
6909 if (!root->ref_cows) {
6910 path->lowest_level = lowest_level;
6911 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
6913 path->lowest_level = 0;
6914 btrfs_release_path(root, path);
6918 mutex_lock(&root->fs_info->tree_reloc_mutex);
6919 ret = init_reloc_tree(trans, root);
6921 reloc_root = root->reloc_root;
6923 shared_level = ref_path->shared_level;
6924 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
6926 keys = ref_path->node_keys;
6927 nodes = ref_path->new_nodes;
6928 memset(&keys[shared_level + 1], 0,
6929 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
6930 memset(&nodes[shared_level + 1], 0,
6931 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
6933 if (nodes[lowest_level] == 0) {
6934 path->lowest_level = lowest_level;
6935 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6938 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
6939 eb = path->nodes[level];
6940 if (!eb || eb == reloc_root->node)
6942 nodes[level] = eb->start;
6944 btrfs_item_key_to_cpu(eb, &keys[level], 0);
6946 btrfs_node_key_to_cpu(eb, &keys[level], 0);
6949 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6950 eb = path->nodes[0];
6951 ret = replace_extents_in_leaf(trans, reloc_root, eb,
6952 group, reloc_inode);
6955 btrfs_release_path(reloc_root, path);
6957 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
6963 * replace tree blocks in the fs tree with tree blocks in
6966 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
6969 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6970 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6973 extent_buffer_get(path->nodes[0]);
6974 eb = path->nodes[0];
6975 btrfs_release_path(reloc_root, path);
6976 ret = invalidate_extent_cache(reloc_root, eb, group, root);
6978 free_extent_buffer(eb);
6981 mutex_unlock(&root->fs_info->tree_reloc_mutex);
6982 path->lowest_level = 0;
6986 static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
6987 struct btrfs_root *root,
6988 struct btrfs_path *path,
6989 struct btrfs_key *first_key,
6990 struct btrfs_ref_path *ref_path)
6994 ret = relocate_one_path(trans, root, path, first_key,
6995 ref_path, NULL, NULL);
7001 static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
7002 struct btrfs_root *extent_root,
7003 struct btrfs_path *path,
7004 struct btrfs_key *extent_key)
7008 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
7011 ret = btrfs_del_item(trans, extent_root, path);
7013 btrfs_release_path(extent_root, path);
7017 static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
7018 struct btrfs_ref_path *ref_path)
7020 struct btrfs_key root_key;
7022 root_key.objectid = ref_path->root_objectid;
7023 root_key.type = BTRFS_ROOT_ITEM_KEY;
7024 if (is_cowonly_root(ref_path->root_objectid))
7025 root_key.offset = 0;
7027 root_key.offset = (u64)-1;
7029 return btrfs_read_fs_root_no_name(fs_info, &root_key);
7032 static noinline int relocate_one_extent(struct btrfs_root *extent_root,
7033 struct btrfs_path *path,
7034 struct btrfs_key *extent_key,
7035 struct btrfs_block_group_cache *group,
7036 struct inode *reloc_inode, int pass)
7038 struct btrfs_trans_handle *trans;
7039 struct btrfs_root *found_root;
7040 struct btrfs_ref_path *ref_path = NULL;
7041 struct disk_extent *new_extents = NULL;
7046 struct btrfs_key first_key;
7050 trans = btrfs_start_transaction(extent_root, 1);
7053 if (extent_key->objectid == 0) {
7054 ret = del_extent_zero(trans, extent_root, path, extent_key);
7058 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
7064 for (loops = 0; ; loops++) {
7066 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
7067 extent_key->objectid);
7069 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
7076 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
7077 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
7080 found_root = read_ref_root(extent_root->fs_info, ref_path);
7081 BUG_ON(!found_root);
7083 * for reference counted tree, only process reference paths
7084 * rooted at the latest committed root.
7086 if (found_root->ref_cows &&
7087 ref_path->root_generation != found_root->root_key.offset)
7090 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7093 * copy data extents to new locations
7095 u64 group_start = group->key.objectid;
7096 ret = relocate_data_extent(reloc_inode,
7105 level = ref_path->owner_objectid;
7108 if (prev_block != ref_path->nodes[level]) {
7109 struct extent_buffer *eb;
7110 u64 block_start = ref_path->nodes[level];
7111 u64 block_size = btrfs_level_size(found_root, level);
7113 eb = read_tree_block(found_root, block_start,
7115 btrfs_tree_lock(eb);
7116 BUG_ON(level != btrfs_header_level(eb));
7119 btrfs_item_key_to_cpu(eb, &first_key, 0);
7121 btrfs_node_key_to_cpu(eb, &first_key, 0);
7123 btrfs_tree_unlock(eb);
7124 free_extent_buffer(eb);
7125 prev_block = block_start;
7128 mutex_lock(&extent_root->fs_info->trans_mutex);
7129 btrfs_record_root_in_trans(found_root);
7130 mutex_unlock(&extent_root->fs_info->trans_mutex);
7131 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7133 * try to update data extent references while
7134 * keeping metadata shared between snapshots.
7137 ret = relocate_one_path(trans, found_root,
7138 path, &first_key, ref_path,
7139 group, reloc_inode);
7145 * use fallback method to process the remaining
7149 u64 group_start = group->key.objectid;
7150 new_extents = kmalloc(sizeof(*new_extents),
7153 ret = get_new_locations(reloc_inode,
7161 ret = replace_one_extent(trans, found_root,
7163 &first_key, ref_path,
7164 new_extents, nr_extents);
7166 ret = relocate_tree_block(trans, found_root, path,
7167 &first_key, ref_path);
7174 btrfs_end_transaction(trans, extent_root);
7181 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7184 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
7185 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7187 num_devices = root->fs_info->fs_devices->rw_devices;
7188 if (num_devices == 1) {
7189 stripped |= BTRFS_BLOCK_GROUP_DUP;
7190 stripped = flags & ~stripped;
7192 /* turn raid0 into single device chunks */
7193 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7196 /* turn mirroring into duplication */
7197 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7198 BTRFS_BLOCK_GROUP_RAID10))
7199 return stripped | BTRFS_BLOCK_GROUP_DUP;
7202 /* they already had raid on here, just return */
7203 if (flags & stripped)
7206 stripped |= BTRFS_BLOCK_GROUP_DUP;
7207 stripped = flags & ~stripped;
7209 /* switch duplicated blocks with raid1 */
7210 if (flags & BTRFS_BLOCK_GROUP_DUP)
7211 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7213 /* turn single device chunks into raid0 */
7214 return stripped | BTRFS_BLOCK_GROUP_RAID0;
7219 static int __alloc_chunk_for_shrink(struct btrfs_root *root,
7220 struct btrfs_block_group_cache *shrink_block_group,
7223 struct btrfs_trans_handle *trans;
7224 u64 new_alloc_flags;
7227 spin_lock(&shrink_block_group->lock);
7228 if (btrfs_block_group_used(&shrink_block_group->item) +
7229 shrink_block_group->reserved > 0) {
7230 spin_unlock(&shrink_block_group->lock);
7232 trans = btrfs_start_transaction(root, 1);
7233 spin_lock(&shrink_block_group->lock);
7235 new_alloc_flags = update_block_group_flags(root,
7236 shrink_block_group->flags);
7237 if (new_alloc_flags != shrink_block_group->flags) {
7239 btrfs_block_group_used(&shrink_block_group->item);
7241 calc = shrink_block_group->key.offset;
7243 spin_unlock(&shrink_block_group->lock);
7245 do_chunk_alloc(trans, root->fs_info->extent_root,
7246 calc + 2 * 1024 * 1024, new_alloc_flags, force);
7248 btrfs_end_transaction(trans, root);
7250 spin_unlock(&shrink_block_group->lock);
7255 int btrfs_prepare_block_group_relocation(struct btrfs_root *root,
7256 struct btrfs_block_group_cache *group)
7259 __alloc_chunk_for_shrink(root, group, 1);
7260 set_block_group_readonly(group);
7265 * checks to see if its even possible to relocate this block group.
7267 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7268 * ok to go ahead and try.
7270 int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
7272 struct btrfs_block_group_cache *block_group;
7273 struct btrfs_space_info *space_info;
7274 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7275 struct btrfs_device *device;
7279 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
7281 /* odd, couldn't find the block group, leave it alone */
7285 /* no bytes used, we're good */
7286 if (!btrfs_block_group_used(&block_group->item))
7289 space_info = block_group->space_info;
7290 spin_lock(&space_info->lock);
7292 full = space_info->full;
7295 * if this is the last block group we have in this space, we can't
7296 * relocate it unless we're able to allocate a new chunk below.
7298 * Otherwise, we need to make sure we have room in the space to handle
7299 * all of the extents from this block group. If we can, we're good
7301 if ((space_info->total_bytes != block_group->key.offset) &&
7302 (space_info->bytes_used + space_info->bytes_reserved +
7303 space_info->bytes_pinned + space_info->bytes_readonly +
7304 btrfs_block_group_used(&block_group->item) <
7305 space_info->total_bytes)) {
7306 spin_unlock(&space_info->lock);
7309 spin_unlock(&space_info->lock);
7312 * ok we don't have enough space, but maybe we have free space on our
7313 * devices to allocate new chunks for relocation, so loop through our
7314 * alloc devices and guess if we have enough space. However, if we
7315 * were marked as full, then we know there aren't enough chunks, and we
7322 mutex_lock(&root->fs_info->chunk_mutex);
7323 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
7324 u64 min_free = btrfs_block_group_used(&block_group->item);
7325 u64 dev_offset, max_avail;
7328 * check to make sure we can actually find a chunk with enough
7329 * space to fit our block group in.
7331 if (device->total_bytes > device->bytes_used + min_free) {
7332 ret = find_free_dev_extent(NULL, device, min_free,
7333 &dev_offset, &max_avail);
7339 mutex_unlock(&root->fs_info->chunk_mutex);
7341 btrfs_put_block_group(block_group);
7345 static int find_first_block_group(struct btrfs_root *root,
7346 struct btrfs_path *path, struct btrfs_key *key)
7349 struct btrfs_key found_key;
7350 struct extent_buffer *leaf;
7353 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7358 slot = path->slots[0];
7359 leaf = path->nodes[0];
7360 if (slot >= btrfs_header_nritems(leaf)) {
7361 ret = btrfs_next_leaf(root, path);
7368 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7370 if (found_key.objectid >= key->objectid &&
7371 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7381 int btrfs_free_block_groups(struct btrfs_fs_info *info)
7383 struct btrfs_block_group_cache *block_group;
7384 struct btrfs_space_info *space_info;
7385 struct btrfs_caching_control *caching_ctl;
7388 down_write(&info->extent_commit_sem);
7389 while (!list_empty(&info->caching_block_groups)) {
7390 caching_ctl = list_entry(info->caching_block_groups.next,
7391 struct btrfs_caching_control, list);
7392 list_del(&caching_ctl->list);
7393 put_caching_control(caching_ctl);
7395 up_write(&info->extent_commit_sem);
7397 spin_lock(&info->block_group_cache_lock);
7398 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7399 block_group = rb_entry(n, struct btrfs_block_group_cache,
7401 rb_erase(&block_group->cache_node,
7402 &info->block_group_cache_tree);
7403 spin_unlock(&info->block_group_cache_lock);
7405 down_write(&block_group->space_info->groups_sem);
7406 list_del(&block_group->list);
7407 up_write(&block_group->space_info->groups_sem);
7409 if (block_group->cached == BTRFS_CACHE_STARTED)
7410 wait_block_group_cache_done(block_group);
7412 btrfs_remove_free_space_cache(block_group);
7413 btrfs_put_block_group(block_group);
7415 spin_lock(&info->block_group_cache_lock);
7417 spin_unlock(&info->block_group_cache_lock);
7419 /* now that all the block groups are freed, go through and
7420 * free all the space_info structs. This is only called during
7421 * the final stages of unmount, and so we know nobody is
7422 * using them. We call synchronize_rcu() once before we start,
7423 * just to be on the safe side.
7427 while(!list_empty(&info->space_info)) {
7428 space_info = list_entry(info->space_info.next,
7429 struct btrfs_space_info,
7432 list_del(&space_info->list);
7438 static void __link_block_group(struct btrfs_space_info *space_info,
7439 struct btrfs_block_group_cache *cache)
7441 int index = get_block_group_index(cache);
7443 down_write(&space_info->groups_sem);
7444 list_add_tail(&cache->list, &space_info->block_groups[index]);
7445 up_write(&space_info->groups_sem);
7448 int btrfs_read_block_groups(struct btrfs_root *root)
7450 struct btrfs_path *path;
7452 struct btrfs_block_group_cache *cache;
7453 struct btrfs_fs_info *info = root->fs_info;
7454 struct btrfs_space_info *space_info;
7455 struct btrfs_key key;
7456 struct btrfs_key found_key;
7457 struct extent_buffer *leaf;
7459 root = info->extent_root;
7462 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
7463 path = btrfs_alloc_path();
7468 ret = find_first_block_group(root, path, &key);
7474 leaf = path->nodes[0];
7475 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7476 cache = kzalloc(sizeof(*cache), GFP_NOFS);
7482 atomic_set(&cache->count, 1);
7483 spin_lock_init(&cache->lock);
7484 spin_lock_init(&cache->tree_lock);
7485 cache->fs_info = info;
7486 INIT_LIST_HEAD(&cache->list);
7487 INIT_LIST_HEAD(&cache->cluster_list);
7490 * we only want to have 32k of ram per block group for keeping
7491 * track of free space, and if we pass 1/2 of that we want to
7492 * start converting things over to using bitmaps
7494 cache->extents_thresh = ((1024 * 32) / 2) /
7495 sizeof(struct btrfs_free_space);
7497 read_extent_buffer(leaf, &cache->item,
7498 btrfs_item_ptr_offset(leaf, path->slots[0]),
7499 sizeof(cache->item));
7500 memcpy(&cache->key, &found_key, sizeof(found_key));
7502 key.objectid = found_key.objectid + found_key.offset;
7503 btrfs_release_path(root, path);
7504 cache->flags = btrfs_block_group_flags(&cache->item);
7505 cache->sectorsize = root->sectorsize;
7508 * check for two cases, either we are full, and therefore
7509 * don't need to bother with the caching work since we won't
7510 * find any space, or we are empty, and we can just add all
7511 * the space in and be done with it. This saves us _alot_ of
7512 * time, particularly in the full case.
7514 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
7515 exclude_super_stripes(root, cache);
7516 cache->last_byte_to_unpin = (u64)-1;
7517 cache->cached = BTRFS_CACHE_FINISHED;
7518 free_excluded_extents(root, cache);
7519 } else if (btrfs_block_group_used(&cache->item) == 0) {
7520 exclude_super_stripes(root, cache);
7521 cache->last_byte_to_unpin = (u64)-1;
7522 cache->cached = BTRFS_CACHE_FINISHED;
7523 add_new_free_space(cache, root->fs_info,
7525 found_key.objectid +
7527 free_excluded_extents(root, cache);
7530 ret = update_space_info(info, cache->flags, found_key.offset,
7531 btrfs_block_group_used(&cache->item),
7534 cache->space_info = space_info;
7535 spin_lock(&cache->space_info->lock);
7536 cache->space_info->bytes_super += cache->bytes_super;
7537 spin_unlock(&cache->space_info->lock);
7539 __link_block_group(space_info, cache);
7541 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7544 set_avail_alloc_bits(root->fs_info, cache->flags);
7545 if (btrfs_chunk_readonly(root, cache->key.objectid))
7546 set_block_group_readonly(cache);
7549 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
7550 if (!(get_alloc_profile(root, space_info->flags) &
7551 (BTRFS_BLOCK_GROUP_RAID10 |
7552 BTRFS_BLOCK_GROUP_RAID1 |
7553 BTRFS_BLOCK_GROUP_DUP)))
7556 * avoid allocating from un-mirrored block group if there are
7557 * mirrored block groups.
7559 list_for_each_entry(cache, &space_info->block_groups[3], list)
7560 set_block_group_readonly(cache);
7561 list_for_each_entry(cache, &space_info->block_groups[4], list)
7562 set_block_group_readonly(cache);
7566 btrfs_free_path(path);
7570 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7571 struct btrfs_root *root, u64 bytes_used,
7572 u64 type, u64 chunk_objectid, u64 chunk_offset,
7576 struct btrfs_root *extent_root;
7577 struct btrfs_block_group_cache *cache;
7579 extent_root = root->fs_info->extent_root;
7581 root->fs_info->last_trans_log_full_commit = trans->transid;
7583 cache = kzalloc(sizeof(*cache), GFP_NOFS);
7587 cache->key.objectid = chunk_offset;
7588 cache->key.offset = size;
7589 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
7590 cache->sectorsize = root->sectorsize;
7593 * we only want to have 32k of ram per block group for keeping track
7594 * of free space, and if we pass 1/2 of that we want to start
7595 * converting things over to using bitmaps
7597 cache->extents_thresh = ((1024 * 32) / 2) /
7598 sizeof(struct btrfs_free_space);
7599 atomic_set(&cache->count, 1);
7600 spin_lock_init(&cache->lock);
7601 spin_lock_init(&cache->tree_lock);
7602 INIT_LIST_HEAD(&cache->list);
7603 INIT_LIST_HEAD(&cache->cluster_list);
7605 btrfs_set_block_group_used(&cache->item, bytes_used);
7606 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7607 cache->flags = type;
7608 btrfs_set_block_group_flags(&cache->item, type);
7610 cache->last_byte_to_unpin = (u64)-1;
7611 cache->cached = BTRFS_CACHE_FINISHED;
7612 exclude_super_stripes(root, cache);
7614 add_new_free_space(cache, root->fs_info, chunk_offset,
7615 chunk_offset + size);
7617 free_excluded_extents(root, cache);
7619 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7620 &cache->space_info);
7623 spin_lock(&cache->space_info->lock);
7624 cache->space_info->bytes_super += cache->bytes_super;
7625 spin_unlock(&cache->space_info->lock);
7627 __link_block_group(cache->space_info, cache);
7629 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7632 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7633 sizeof(cache->item));
7636 set_avail_alloc_bits(extent_root->fs_info, type);
7641 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7642 struct btrfs_root *root, u64 group_start)
7644 struct btrfs_path *path;
7645 struct btrfs_block_group_cache *block_group;
7646 struct btrfs_free_cluster *cluster;
7647 struct btrfs_key key;
7650 root = root->fs_info->extent_root;
7652 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7653 BUG_ON(!block_group);
7654 BUG_ON(!block_group->ro);
7656 memcpy(&key, &block_group->key, sizeof(key));
7658 /* make sure this block group isn't part of an allocation cluster */
7659 cluster = &root->fs_info->data_alloc_cluster;
7660 spin_lock(&cluster->refill_lock);
7661 btrfs_return_cluster_to_free_space(block_group, cluster);
7662 spin_unlock(&cluster->refill_lock);
7665 * make sure this block group isn't part of a metadata
7666 * allocation cluster
7668 cluster = &root->fs_info->meta_alloc_cluster;
7669 spin_lock(&cluster->refill_lock);
7670 btrfs_return_cluster_to_free_space(block_group, cluster);
7671 spin_unlock(&cluster->refill_lock);
7673 path = btrfs_alloc_path();
7676 spin_lock(&root->fs_info->block_group_cache_lock);
7677 rb_erase(&block_group->cache_node,
7678 &root->fs_info->block_group_cache_tree);
7679 spin_unlock(&root->fs_info->block_group_cache_lock);
7681 down_write(&block_group->space_info->groups_sem);
7683 * we must use list_del_init so people can check to see if they
7684 * are still on the list after taking the semaphore
7686 list_del_init(&block_group->list);
7687 up_write(&block_group->space_info->groups_sem);
7689 if (block_group->cached == BTRFS_CACHE_STARTED)
7690 wait_block_group_cache_done(block_group);
7692 btrfs_remove_free_space_cache(block_group);
7694 spin_lock(&block_group->space_info->lock);
7695 block_group->space_info->total_bytes -= block_group->key.offset;
7696 block_group->space_info->bytes_readonly -= block_group->key.offset;
7697 spin_unlock(&block_group->space_info->lock);
7699 btrfs_clear_space_info_full(root->fs_info);
7701 btrfs_put_block_group(block_group);
7702 btrfs_put_block_group(block_group);
7704 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
7710 ret = btrfs_del_item(trans, root, path);
7712 btrfs_free_path(path);