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>
29 #include "print-tree.h"
30 #include "transaction.h"
33 #include "free-space-cache.h"
35 static int update_block_group(struct btrfs_trans_handle *trans,
36 struct btrfs_root *root,
37 u64 bytenr, u64 num_bytes, int alloc,
39 static int update_reserved_extents(struct btrfs_block_group_cache *cache,
40 u64 num_bytes, int reserve);
41 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
42 struct btrfs_root *root,
43 u64 bytenr, u64 num_bytes, u64 parent,
44 u64 root_objectid, u64 owner_objectid,
45 u64 owner_offset, int refs_to_drop,
46 struct btrfs_delayed_extent_op *extra_op);
47 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
48 struct extent_buffer *leaf,
49 struct btrfs_extent_item *ei);
50 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
51 struct btrfs_root *root,
52 u64 parent, u64 root_objectid,
53 u64 flags, u64 owner, u64 offset,
54 struct btrfs_key *ins, int ref_mod);
55 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
56 struct btrfs_root *root,
57 u64 parent, u64 root_objectid,
58 u64 flags, struct btrfs_disk_key *key,
59 int level, struct btrfs_key *ins);
60 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
61 struct btrfs_root *extent_root, u64 alloc_bytes,
62 u64 flags, int force);
63 static int pin_down_bytes(struct btrfs_trans_handle *trans,
64 struct btrfs_root *root,
65 struct btrfs_path *path,
66 u64 bytenr, u64 num_bytes,
67 int is_data, int reserved,
68 struct extent_buffer **must_clean);
69 static int find_next_key(struct btrfs_path *path, int level,
70 struct btrfs_key *key);
71 static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
72 int dump_block_groups);
75 block_group_cache_done(struct btrfs_block_group_cache *cache)
78 return cache->cached == BTRFS_CACHE_FINISHED;
81 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
83 return (cache->flags & bits) == bits;
87 * this adds the block group to the fs_info rb tree for the block group
90 static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
91 struct btrfs_block_group_cache *block_group)
94 struct rb_node *parent = NULL;
95 struct btrfs_block_group_cache *cache;
97 spin_lock(&info->block_group_cache_lock);
98 p = &info->block_group_cache_tree.rb_node;
102 cache = rb_entry(parent, struct btrfs_block_group_cache,
104 if (block_group->key.objectid < cache->key.objectid) {
106 } else if (block_group->key.objectid > cache->key.objectid) {
109 spin_unlock(&info->block_group_cache_lock);
114 rb_link_node(&block_group->cache_node, parent, p);
115 rb_insert_color(&block_group->cache_node,
116 &info->block_group_cache_tree);
117 spin_unlock(&info->block_group_cache_lock);
123 * This will return the block group at or after bytenr if contains is 0, else
124 * it will return the block group that contains the bytenr
126 static struct btrfs_block_group_cache *
127 block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
130 struct btrfs_block_group_cache *cache, *ret = NULL;
134 spin_lock(&info->block_group_cache_lock);
135 n = info->block_group_cache_tree.rb_node;
138 cache = rb_entry(n, struct btrfs_block_group_cache,
140 end = cache->key.objectid + cache->key.offset - 1;
141 start = cache->key.objectid;
143 if (bytenr < start) {
144 if (!contains && (!ret || start < ret->key.objectid))
147 } else if (bytenr > start) {
148 if (contains && bytenr <= end) {
159 atomic_inc(&ret->count);
160 spin_unlock(&info->block_group_cache_lock);
165 static int add_excluded_extent(struct btrfs_root *root,
166 u64 start, u64 num_bytes)
168 u64 end = start + num_bytes - 1;
169 set_extent_bits(&root->fs_info->freed_extents[0],
170 start, end, EXTENT_UPTODATE, GFP_NOFS);
171 set_extent_bits(&root->fs_info->freed_extents[1],
172 start, end, EXTENT_UPTODATE, GFP_NOFS);
176 static void free_excluded_extents(struct btrfs_root *root,
177 struct btrfs_block_group_cache *cache)
181 start = cache->key.objectid;
182 end = start + cache->key.offset - 1;
184 clear_extent_bits(&root->fs_info->freed_extents[0],
185 start, end, EXTENT_UPTODATE, GFP_NOFS);
186 clear_extent_bits(&root->fs_info->freed_extents[1],
187 start, end, EXTENT_UPTODATE, GFP_NOFS);
190 static int exclude_super_stripes(struct btrfs_root *root,
191 struct btrfs_block_group_cache *cache)
198 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
199 bytenr = btrfs_sb_offset(i);
200 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
201 cache->key.objectid, bytenr,
202 0, &logical, &nr, &stripe_len);
206 cache->bytes_super += stripe_len;
207 ret = add_excluded_extent(root, logical[nr],
217 static struct btrfs_caching_control *
218 get_caching_control(struct btrfs_block_group_cache *cache)
220 struct btrfs_caching_control *ctl;
222 spin_lock(&cache->lock);
223 if (cache->cached != BTRFS_CACHE_STARTED) {
224 spin_unlock(&cache->lock);
228 ctl = cache->caching_ctl;
229 atomic_inc(&ctl->count);
230 spin_unlock(&cache->lock);
234 static void put_caching_control(struct btrfs_caching_control *ctl)
236 if (atomic_dec_and_test(&ctl->count))
241 * this is only called by cache_block_group, since we could have freed extents
242 * we need to check the pinned_extents for any extents that can't be used yet
243 * since their free space will be released as soon as the transaction commits.
245 static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
246 struct btrfs_fs_info *info, u64 start, u64 end)
248 u64 extent_start, extent_end, size, total_added = 0;
251 while (start < end) {
252 ret = find_first_extent_bit(info->pinned_extents, start,
253 &extent_start, &extent_end,
254 EXTENT_DIRTY | EXTENT_UPTODATE);
258 if (extent_start == start) {
259 start = extent_end + 1;
260 } else if (extent_start > start && extent_start < end) {
261 size = extent_start - start;
263 ret = btrfs_add_free_space(block_group, start,
266 start = extent_end + 1;
275 ret = btrfs_add_free_space(block_group, start, size);
282 static int caching_kthread(void *data)
284 struct btrfs_block_group_cache *block_group = data;
285 struct btrfs_fs_info *fs_info = block_group->fs_info;
286 struct btrfs_caching_control *caching_ctl = block_group->caching_ctl;
287 struct btrfs_root *extent_root = fs_info->extent_root;
288 struct btrfs_path *path;
289 struct extent_buffer *leaf;
290 struct btrfs_key key;
296 path = btrfs_alloc_path();
300 exclude_super_stripes(extent_root, block_group);
301 spin_lock(&block_group->space_info->lock);
302 block_group->space_info->bytes_super += block_group->bytes_super;
303 spin_unlock(&block_group->space_info->lock);
305 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
308 * We don't want to deadlock with somebody trying to allocate a new
309 * extent for the extent root while also trying to search the extent
310 * root to add free space. So we skip locking and search the commit
311 * root, since its read-only
313 path->skip_locking = 1;
314 path->search_commit_root = 1;
319 key.type = BTRFS_EXTENT_ITEM_KEY;
321 mutex_lock(&caching_ctl->mutex);
322 /* need to make sure the commit_root doesn't disappear */
323 down_read(&fs_info->extent_commit_sem);
325 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
329 leaf = path->nodes[0];
330 nritems = btrfs_header_nritems(leaf);
334 if (fs_info->closing > 1) {
339 if (path->slots[0] < nritems) {
340 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
342 ret = find_next_key(path, 0, &key);
346 caching_ctl->progress = last;
347 btrfs_release_path(extent_root, path);
348 up_read(&fs_info->extent_commit_sem);
349 mutex_unlock(&caching_ctl->mutex);
350 if (btrfs_transaction_in_commit(fs_info))
357 if (key.objectid < block_group->key.objectid) {
362 if (key.objectid >= block_group->key.objectid +
363 block_group->key.offset)
366 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
367 total_found += add_new_free_space(block_group,
370 last = key.objectid + key.offset;
372 if (total_found > (1024 * 1024 * 2)) {
374 wake_up(&caching_ctl->wait);
381 total_found += add_new_free_space(block_group, fs_info, last,
382 block_group->key.objectid +
383 block_group->key.offset);
384 caching_ctl->progress = (u64)-1;
386 spin_lock(&block_group->lock);
387 block_group->caching_ctl = NULL;
388 block_group->cached = BTRFS_CACHE_FINISHED;
389 spin_unlock(&block_group->lock);
392 btrfs_free_path(path);
393 up_read(&fs_info->extent_commit_sem);
395 free_excluded_extents(extent_root, block_group);
397 mutex_unlock(&caching_ctl->mutex);
398 wake_up(&caching_ctl->wait);
400 put_caching_control(caching_ctl);
401 atomic_dec(&block_group->space_info->caching_threads);
405 static int cache_block_group(struct btrfs_block_group_cache *cache)
407 struct btrfs_fs_info *fs_info = cache->fs_info;
408 struct btrfs_caching_control *caching_ctl;
409 struct task_struct *tsk;
413 if (cache->cached != BTRFS_CACHE_NO)
416 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL);
417 BUG_ON(!caching_ctl);
419 INIT_LIST_HEAD(&caching_ctl->list);
420 mutex_init(&caching_ctl->mutex);
421 init_waitqueue_head(&caching_ctl->wait);
422 caching_ctl->block_group = cache;
423 caching_ctl->progress = cache->key.objectid;
424 /* one for caching kthread, one for caching block group list */
425 atomic_set(&caching_ctl->count, 2);
427 spin_lock(&cache->lock);
428 if (cache->cached != BTRFS_CACHE_NO) {
429 spin_unlock(&cache->lock);
433 cache->caching_ctl = caching_ctl;
434 cache->cached = BTRFS_CACHE_STARTED;
435 spin_unlock(&cache->lock);
437 down_write(&fs_info->extent_commit_sem);
438 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
439 up_write(&fs_info->extent_commit_sem);
441 atomic_inc(&cache->space_info->caching_threads);
443 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
444 cache->key.objectid);
447 printk(KERN_ERR "error running thread %d\n", ret);
455 * return the block group that starts at or after bytenr
457 static struct btrfs_block_group_cache *
458 btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
460 struct btrfs_block_group_cache *cache;
462 cache = block_group_cache_tree_search(info, bytenr, 0);
468 * return the block group that contains the given bytenr
470 struct btrfs_block_group_cache *btrfs_lookup_block_group(
471 struct btrfs_fs_info *info,
474 struct btrfs_block_group_cache *cache;
476 cache = block_group_cache_tree_search(info, bytenr, 1);
481 void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
483 if (atomic_dec_and_test(&cache->count))
487 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
490 struct list_head *head = &info->space_info;
491 struct btrfs_space_info *found;
494 list_for_each_entry_rcu(found, head, list) {
495 if (found->flags == flags) {
505 * after adding space to the filesystem, we need to clear the full flags
506 * on all the space infos.
508 void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
510 struct list_head *head = &info->space_info;
511 struct btrfs_space_info *found;
514 list_for_each_entry_rcu(found, head, list)
519 static u64 div_factor(u64 num, int factor)
528 u64 btrfs_find_block_group(struct btrfs_root *root,
529 u64 search_start, u64 search_hint, int owner)
531 struct btrfs_block_group_cache *cache;
533 u64 last = max(search_hint, search_start);
540 cache = btrfs_lookup_first_block_group(root->fs_info, last);
544 spin_lock(&cache->lock);
545 last = cache->key.objectid + cache->key.offset;
546 used = btrfs_block_group_used(&cache->item);
548 if ((full_search || !cache->ro) &&
549 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
550 if (used + cache->pinned + cache->reserved <
551 div_factor(cache->key.offset, factor)) {
552 group_start = cache->key.objectid;
553 spin_unlock(&cache->lock);
554 btrfs_put_block_group(cache);
558 spin_unlock(&cache->lock);
559 btrfs_put_block_group(cache);
567 if (!full_search && factor < 10) {
577 /* simple helper to search for an existing extent at a given offset */
578 int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
581 struct btrfs_key key;
582 struct btrfs_path *path;
584 path = btrfs_alloc_path();
586 key.objectid = start;
588 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
589 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
591 btrfs_free_path(path);
596 * Back reference rules. Back refs have three main goals:
598 * 1) differentiate between all holders of references to an extent so that
599 * when a reference is dropped we can make sure it was a valid reference
600 * before freeing the extent.
602 * 2) Provide enough information to quickly find the holders of an extent
603 * if we notice a given block is corrupted or bad.
605 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
606 * maintenance. This is actually the same as #2, but with a slightly
607 * different use case.
609 * There are two kinds of back refs. The implicit back refs is optimized
610 * for pointers in non-shared tree blocks. For a given pointer in a block,
611 * back refs of this kind provide information about the block's owner tree
612 * and the pointer's key. These information allow us to find the block by
613 * b-tree searching. The full back refs is for pointers in tree blocks not
614 * referenced by their owner trees. The location of tree block is recorded
615 * in the back refs. Actually the full back refs is generic, and can be
616 * used in all cases the implicit back refs is used. The major shortcoming
617 * of the full back refs is its overhead. Every time a tree block gets
618 * COWed, we have to update back refs entry for all pointers in it.
620 * For a newly allocated tree block, we use implicit back refs for
621 * pointers in it. This means most tree related operations only involve
622 * implicit back refs. For a tree block created in old transaction, the
623 * only way to drop a reference to it is COW it. So we can detect the
624 * event that tree block loses its owner tree's reference and do the
625 * back refs conversion.
627 * When a tree block is COW'd through a tree, there are four cases:
629 * The reference count of the block is one and the tree is the block's
630 * owner tree. Nothing to do in this case.
632 * The reference count of the block is one and the tree is not the
633 * block's owner tree. In this case, full back refs is used for pointers
634 * in the block. Remove these full back refs, add implicit back refs for
635 * every pointers in the new block.
637 * The reference count of the block is greater than one and the tree is
638 * the block's owner tree. In this case, implicit back refs is used for
639 * pointers in the block. Add full back refs for every pointers in the
640 * block, increase lower level extents' reference counts. The original
641 * implicit back refs are entailed to the new block.
643 * The reference count of the block is greater than one and the tree is
644 * not the block's owner tree. Add implicit back refs for every pointer in
645 * the new block, increase lower level extents' reference count.
647 * Back Reference Key composing:
649 * The key objectid corresponds to the first byte in the extent,
650 * The key type is used to differentiate between types of back refs.
651 * There are different meanings of the key offset for different types
654 * File extents can be referenced by:
656 * - multiple snapshots, subvolumes, or different generations in one subvol
657 * - different files inside a single subvolume
658 * - different offsets inside a file (bookend extents in file.c)
660 * The extent ref structure for the implicit back refs has fields for:
662 * - Objectid of the subvolume root
663 * - objectid of the file holding the reference
664 * - original offset in the file
665 * - how many bookend extents
667 * The key offset for the implicit back refs is hash of the first
670 * The extent ref structure for the full back refs has field for:
672 * - number of pointers in the tree leaf
674 * The key offset for the implicit back refs is the first byte of
677 * When a file extent is allocated, The implicit back refs is used.
678 * the fields are filled in:
680 * (root_key.objectid, inode objectid, offset in file, 1)
682 * When a file extent is removed file truncation, we find the
683 * corresponding implicit back refs and check the following fields:
685 * (btrfs_header_owner(leaf), inode objectid, offset in file)
687 * Btree extents can be referenced by:
689 * - Different subvolumes
691 * Both the implicit back refs and the full back refs for tree blocks
692 * only consist of key. The key offset for the implicit back refs is
693 * objectid of block's owner tree. The key offset for the full back refs
694 * is the first byte of parent block.
696 * When implicit back refs is used, information about the lowest key and
697 * level of the tree block are required. These information are stored in
698 * tree block info structure.
701 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
702 static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
703 struct btrfs_root *root,
704 struct btrfs_path *path,
705 u64 owner, u32 extra_size)
707 struct btrfs_extent_item *item;
708 struct btrfs_extent_item_v0 *ei0;
709 struct btrfs_extent_ref_v0 *ref0;
710 struct btrfs_tree_block_info *bi;
711 struct extent_buffer *leaf;
712 struct btrfs_key key;
713 struct btrfs_key found_key;
714 u32 new_size = sizeof(*item);
718 leaf = path->nodes[0];
719 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
721 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
722 ei0 = btrfs_item_ptr(leaf, path->slots[0],
723 struct btrfs_extent_item_v0);
724 refs = btrfs_extent_refs_v0(leaf, ei0);
726 if (owner == (u64)-1) {
728 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
729 ret = btrfs_next_leaf(root, path);
733 leaf = path->nodes[0];
735 btrfs_item_key_to_cpu(leaf, &found_key,
737 BUG_ON(key.objectid != found_key.objectid);
738 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
742 ref0 = btrfs_item_ptr(leaf, path->slots[0],
743 struct btrfs_extent_ref_v0);
744 owner = btrfs_ref_objectid_v0(leaf, ref0);
748 btrfs_release_path(root, path);
750 if (owner < BTRFS_FIRST_FREE_OBJECTID)
751 new_size += sizeof(*bi);
753 new_size -= sizeof(*ei0);
754 ret = btrfs_search_slot(trans, root, &key, path,
755 new_size + extra_size, 1);
760 ret = btrfs_extend_item(trans, root, path, new_size);
763 leaf = path->nodes[0];
764 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
765 btrfs_set_extent_refs(leaf, item, refs);
766 /* FIXME: get real generation */
767 btrfs_set_extent_generation(leaf, item, 0);
768 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
769 btrfs_set_extent_flags(leaf, item,
770 BTRFS_EXTENT_FLAG_TREE_BLOCK |
771 BTRFS_BLOCK_FLAG_FULL_BACKREF);
772 bi = (struct btrfs_tree_block_info *)(item + 1);
773 /* FIXME: get first key of the block */
774 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
775 btrfs_set_tree_block_level(leaf, bi, (int)owner);
777 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
779 btrfs_mark_buffer_dirty(leaf);
784 static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
786 u32 high_crc = ~(u32)0;
787 u32 low_crc = ~(u32)0;
790 lenum = cpu_to_le64(root_objectid);
791 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
792 lenum = cpu_to_le64(owner);
793 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
794 lenum = cpu_to_le64(offset);
795 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
797 return ((u64)high_crc << 31) ^ (u64)low_crc;
800 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
801 struct btrfs_extent_data_ref *ref)
803 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
804 btrfs_extent_data_ref_objectid(leaf, ref),
805 btrfs_extent_data_ref_offset(leaf, ref));
808 static int match_extent_data_ref(struct extent_buffer *leaf,
809 struct btrfs_extent_data_ref *ref,
810 u64 root_objectid, u64 owner, u64 offset)
812 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
813 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
814 btrfs_extent_data_ref_offset(leaf, ref) != offset)
819 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
820 struct btrfs_root *root,
821 struct btrfs_path *path,
822 u64 bytenr, u64 parent,
824 u64 owner, u64 offset)
826 struct btrfs_key key;
827 struct btrfs_extent_data_ref *ref;
828 struct extent_buffer *leaf;
834 key.objectid = bytenr;
836 key.type = BTRFS_SHARED_DATA_REF_KEY;
839 key.type = BTRFS_EXTENT_DATA_REF_KEY;
840 key.offset = hash_extent_data_ref(root_objectid,
845 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
854 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
855 key.type = BTRFS_EXTENT_REF_V0_KEY;
856 btrfs_release_path(root, path);
857 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
868 leaf = path->nodes[0];
869 nritems = btrfs_header_nritems(leaf);
871 if (path->slots[0] >= nritems) {
872 ret = btrfs_next_leaf(root, path);
878 leaf = path->nodes[0];
879 nritems = btrfs_header_nritems(leaf);
883 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
884 if (key.objectid != bytenr ||
885 key.type != BTRFS_EXTENT_DATA_REF_KEY)
888 ref = btrfs_item_ptr(leaf, path->slots[0],
889 struct btrfs_extent_data_ref);
891 if (match_extent_data_ref(leaf, ref, root_objectid,
894 btrfs_release_path(root, path);
906 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
907 struct btrfs_root *root,
908 struct btrfs_path *path,
909 u64 bytenr, u64 parent,
910 u64 root_objectid, u64 owner,
911 u64 offset, int refs_to_add)
913 struct btrfs_key key;
914 struct extent_buffer *leaf;
919 key.objectid = bytenr;
921 key.type = BTRFS_SHARED_DATA_REF_KEY;
923 size = sizeof(struct btrfs_shared_data_ref);
925 key.type = BTRFS_EXTENT_DATA_REF_KEY;
926 key.offset = hash_extent_data_ref(root_objectid,
928 size = sizeof(struct btrfs_extent_data_ref);
931 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
932 if (ret && ret != -EEXIST)
935 leaf = path->nodes[0];
937 struct btrfs_shared_data_ref *ref;
938 ref = btrfs_item_ptr(leaf, path->slots[0],
939 struct btrfs_shared_data_ref);
941 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
943 num_refs = btrfs_shared_data_ref_count(leaf, ref);
944 num_refs += refs_to_add;
945 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
948 struct btrfs_extent_data_ref *ref;
949 while (ret == -EEXIST) {
950 ref = btrfs_item_ptr(leaf, path->slots[0],
951 struct btrfs_extent_data_ref);
952 if (match_extent_data_ref(leaf, ref, root_objectid,
955 btrfs_release_path(root, path);
957 ret = btrfs_insert_empty_item(trans, root, path, &key,
959 if (ret && ret != -EEXIST)
962 leaf = path->nodes[0];
964 ref = btrfs_item_ptr(leaf, path->slots[0],
965 struct btrfs_extent_data_ref);
967 btrfs_set_extent_data_ref_root(leaf, ref,
969 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
970 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
971 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
973 num_refs = btrfs_extent_data_ref_count(leaf, ref);
974 num_refs += refs_to_add;
975 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
978 btrfs_mark_buffer_dirty(leaf);
981 btrfs_release_path(root, path);
985 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
986 struct btrfs_root *root,
987 struct btrfs_path *path,
990 struct btrfs_key key;
991 struct btrfs_extent_data_ref *ref1 = NULL;
992 struct btrfs_shared_data_ref *ref2 = NULL;
993 struct extent_buffer *leaf;
997 leaf = path->nodes[0];
998 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1000 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1001 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1002 struct btrfs_extent_data_ref);
1003 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1004 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1005 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1006 struct btrfs_shared_data_ref);
1007 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1008 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1009 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1010 struct btrfs_extent_ref_v0 *ref0;
1011 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1012 struct btrfs_extent_ref_v0);
1013 num_refs = btrfs_ref_count_v0(leaf, ref0);
1019 BUG_ON(num_refs < refs_to_drop);
1020 num_refs -= refs_to_drop;
1022 if (num_refs == 0) {
1023 ret = btrfs_del_item(trans, root, path);
1025 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1026 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1027 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1028 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1029 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1031 struct btrfs_extent_ref_v0 *ref0;
1032 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1033 struct btrfs_extent_ref_v0);
1034 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1037 btrfs_mark_buffer_dirty(leaf);
1042 static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1043 struct btrfs_path *path,
1044 struct btrfs_extent_inline_ref *iref)
1046 struct btrfs_key key;
1047 struct extent_buffer *leaf;
1048 struct btrfs_extent_data_ref *ref1;
1049 struct btrfs_shared_data_ref *ref2;
1052 leaf = path->nodes[0];
1053 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1055 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1056 BTRFS_EXTENT_DATA_REF_KEY) {
1057 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1058 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1060 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1061 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1063 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1064 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1065 struct btrfs_extent_data_ref);
1066 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1067 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1068 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1069 struct btrfs_shared_data_ref);
1070 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1071 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1072 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1073 struct btrfs_extent_ref_v0 *ref0;
1074 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1075 struct btrfs_extent_ref_v0);
1076 num_refs = btrfs_ref_count_v0(leaf, ref0);
1084 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1085 struct btrfs_root *root,
1086 struct btrfs_path *path,
1087 u64 bytenr, u64 parent,
1090 struct btrfs_key key;
1093 key.objectid = bytenr;
1095 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1096 key.offset = parent;
1098 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1099 key.offset = root_objectid;
1102 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1105 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1106 if (ret == -ENOENT && parent) {
1107 btrfs_release_path(root, path);
1108 key.type = BTRFS_EXTENT_REF_V0_KEY;
1109 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1117 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1118 struct btrfs_root *root,
1119 struct btrfs_path *path,
1120 u64 bytenr, u64 parent,
1123 struct btrfs_key key;
1126 key.objectid = bytenr;
1128 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1129 key.offset = parent;
1131 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1132 key.offset = root_objectid;
1135 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1136 btrfs_release_path(root, path);
1140 static inline int extent_ref_type(u64 parent, u64 owner)
1143 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1145 type = BTRFS_SHARED_BLOCK_REF_KEY;
1147 type = BTRFS_TREE_BLOCK_REF_KEY;
1150 type = BTRFS_SHARED_DATA_REF_KEY;
1152 type = BTRFS_EXTENT_DATA_REF_KEY;
1157 static int find_next_key(struct btrfs_path *path, int level,
1158 struct btrfs_key *key)
1161 for (; level < BTRFS_MAX_LEVEL; level++) {
1162 if (!path->nodes[level])
1164 if (path->slots[level] + 1 >=
1165 btrfs_header_nritems(path->nodes[level]))
1168 btrfs_item_key_to_cpu(path->nodes[level], key,
1169 path->slots[level] + 1);
1171 btrfs_node_key_to_cpu(path->nodes[level], key,
1172 path->slots[level] + 1);
1179 * look for inline back ref. if back ref is found, *ref_ret is set
1180 * to the address of inline back ref, and 0 is returned.
1182 * if back ref isn't found, *ref_ret is set to the address where it
1183 * should be inserted, and -ENOENT is returned.
1185 * if insert is true and there are too many inline back refs, the path
1186 * points to the extent item, and -EAGAIN is returned.
1188 * NOTE: inline back refs are ordered in the same way that back ref
1189 * items in the tree are ordered.
1191 static noinline_for_stack
1192 int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1193 struct btrfs_root *root,
1194 struct btrfs_path *path,
1195 struct btrfs_extent_inline_ref **ref_ret,
1196 u64 bytenr, u64 num_bytes,
1197 u64 parent, u64 root_objectid,
1198 u64 owner, u64 offset, int insert)
1200 struct btrfs_key key;
1201 struct extent_buffer *leaf;
1202 struct btrfs_extent_item *ei;
1203 struct btrfs_extent_inline_ref *iref;
1214 key.objectid = bytenr;
1215 key.type = BTRFS_EXTENT_ITEM_KEY;
1216 key.offset = num_bytes;
1218 want = extent_ref_type(parent, owner);
1220 extra_size = btrfs_extent_inline_ref_size(want);
1221 path->keep_locks = 1;
1224 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1231 leaf = path->nodes[0];
1232 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1233 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1234 if (item_size < sizeof(*ei)) {
1239 ret = convert_extent_item_v0(trans, root, path, owner,
1245 leaf = path->nodes[0];
1246 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1249 BUG_ON(item_size < sizeof(*ei));
1251 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1252 flags = btrfs_extent_flags(leaf, ei);
1254 ptr = (unsigned long)(ei + 1);
1255 end = (unsigned long)ei + item_size;
1257 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1258 ptr += sizeof(struct btrfs_tree_block_info);
1261 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1270 iref = (struct btrfs_extent_inline_ref *)ptr;
1271 type = btrfs_extent_inline_ref_type(leaf, iref);
1275 ptr += btrfs_extent_inline_ref_size(type);
1279 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1280 struct btrfs_extent_data_ref *dref;
1281 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1282 if (match_extent_data_ref(leaf, dref, root_objectid,
1287 if (hash_extent_data_ref_item(leaf, dref) <
1288 hash_extent_data_ref(root_objectid, owner, offset))
1292 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1294 if (parent == ref_offset) {
1298 if (ref_offset < parent)
1301 if (root_objectid == ref_offset) {
1305 if (ref_offset < root_objectid)
1309 ptr += btrfs_extent_inline_ref_size(type);
1311 if (err == -ENOENT && insert) {
1312 if (item_size + extra_size >=
1313 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1318 * To add new inline back ref, we have to make sure
1319 * there is no corresponding back ref item.
1320 * For simplicity, we just do not add new inline back
1321 * ref if there is any kind of item for this block
1323 if (find_next_key(path, 0, &key) == 0 &&
1324 key.objectid == bytenr &&
1325 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
1330 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1333 path->keep_locks = 0;
1334 btrfs_unlock_up_safe(path, 1);
1340 * helper to add new inline back ref
1342 static noinline_for_stack
1343 int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1344 struct btrfs_root *root,
1345 struct btrfs_path *path,
1346 struct btrfs_extent_inline_ref *iref,
1347 u64 parent, u64 root_objectid,
1348 u64 owner, u64 offset, int refs_to_add,
1349 struct btrfs_delayed_extent_op *extent_op)
1351 struct extent_buffer *leaf;
1352 struct btrfs_extent_item *ei;
1355 unsigned long item_offset;
1361 leaf = path->nodes[0];
1362 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1363 item_offset = (unsigned long)iref - (unsigned long)ei;
1365 type = extent_ref_type(parent, owner);
1366 size = btrfs_extent_inline_ref_size(type);
1368 ret = btrfs_extend_item(trans, root, path, size);
1371 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1372 refs = btrfs_extent_refs(leaf, ei);
1373 refs += refs_to_add;
1374 btrfs_set_extent_refs(leaf, ei, refs);
1376 __run_delayed_extent_op(extent_op, leaf, ei);
1378 ptr = (unsigned long)ei + item_offset;
1379 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1380 if (ptr < end - size)
1381 memmove_extent_buffer(leaf, ptr + size, ptr,
1384 iref = (struct btrfs_extent_inline_ref *)ptr;
1385 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1386 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1387 struct btrfs_extent_data_ref *dref;
1388 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1389 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1390 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1391 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1392 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1393 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1394 struct btrfs_shared_data_ref *sref;
1395 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1396 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1397 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1398 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1399 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1401 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1403 btrfs_mark_buffer_dirty(leaf);
1407 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1408 struct btrfs_root *root,
1409 struct btrfs_path *path,
1410 struct btrfs_extent_inline_ref **ref_ret,
1411 u64 bytenr, u64 num_bytes, u64 parent,
1412 u64 root_objectid, u64 owner, u64 offset)
1416 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1417 bytenr, num_bytes, parent,
1418 root_objectid, owner, offset, 0);
1422 btrfs_release_path(root, path);
1425 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1426 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1429 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1430 root_objectid, owner, offset);
1436 * helper to update/remove inline back ref
1438 static noinline_for_stack
1439 int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1440 struct btrfs_root *root,
1441 struct btrfs_path *path,
1442 struct btrfs_extent_inline_ref *iref,
1444 struct btrfs_delayed_extent_op *extent_op)
1446 struct extent_buffer *leaf;
1447 struct btrfs_extent_item *ei;
1448 struct btrfs_extent_data_ref *dref = NULL;
1449 struct btrfs_shared_data_ref *sref = NULL;
1458 leaf = path->nodes[0];
1459 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1460 refs = btrfs_extent_refs(leaf, ei);
1461 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1462 refs += refs_to_mod;
1463 btrfs_set_extent_refs(leaf, ei, refs);
1465 __run_delayed_extent_op(extent_op, leaf, ei);
1467 type = btrfs_extent_inline_ref_type(leaf, iref);
1469 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1470 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1471 refs = btrfs_extent_data_ref_count(leaf, dref);
1472 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1473 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1474 refs = btrfs_shared_data_ref_count(leaf, sref);
1477 BUG_ON(refs_to_mod != -1);
1480 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1481 refs += refs_to_mod;
1484 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1485 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1487 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1489 size = btrfs_extent_inline_ref_size(type);
1490 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1491 ptr = (unsigned long)iref;
1492 end = (unsigned long)ei + item_size;
1493 if (ptr + size < end)
1494 memmove_extent_buffer(leaf, ptr, ptr + size,
1497 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1500 btrfs_mark_buffer_dirty(leaf);
1504 static noinline_for_stack
1505 int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1506 struct btrfs_root *root,
1507 struct btrfs_path *path,
1508 u64 bytenr, u64 num_bytes, u64 parent,
1509 u64 root_objectid, u64 owner,
1510 u64 offset, int refs_to_add,
1511 struct btrfs_delayed_extent_op *extent_op)
1513 struct btrfs_extent_inline_ref *iref;
1516 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1517 bytenr, num_bytes, parent,
1518 root_objectid, owner, offset, 1);
1520 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1521 ret = update_inline_extent_backref(trans, root, path, iref,
1522 refs_to_add, extent_op);
1523 } else if (ret == -ENOENT) {
1524 ret = setup_inline_extent_backref(trans, root, path, iref,
1525 parent, root_objectid,
1526 owner, offset, refs_to_add,
1532 static int insert_extent_backref(struct btrfs_trans_handle *trans,
1533 struct btrfs_root *root,
1534 struct btrfs_path *path,
1535 u64 bytenr, u64 parent, u64 root_objectid,
1536 u64 owner, u64 offset, int refs_to_add)
1539 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1540 BUG_ON(refs_to_add != 1);
1541 ret = insert_tree_block_ref(trans, root, path, bytenr,
1542 parent, root_objectid);
1544 ret = insert_extent_data_ref(trans, root, path, bytenr,
1545 parent, root_objectid,
1546 owner, offset, refs_to_add);
1551 static int remove_extent_backref(struct btrfs_trans_handle *trans,
1552 struct btrfs_root *root,
1553 struct btrfs_path *path,
1554 struct btrfs_extent_inline_ref *iref,
1555 int refs_to_drop, int is_data)
1559 BUG_ON(!is_data && refs_to_drop != 1);
1561 ret = update_inline_extent_backref(trans, root, path, iref,
1562 -refs_to_drop, NULL);
1563 } else if (is_data) {
1564 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1566 ret = btrfs_del_item(trans, root, path);
1571 static void btrfs_issue_discard(struct block_device *bdev,
1574 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL,
1575 DISCARD_FL_BARRIER);
1578 static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1582 u64 map_length = num_bytes;
1583 struct btrfs_multi_bio *multi = NULL;
1585 if (!btrfs_test_opt(root, DISCARD))
1588 /* Tell the block device(s) that the sectors can be discarded */
1589 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1590 bytenr, &map_length, &multi, 0);
1592 struct btrfs_bio_stripe *stripe = multi->stripes;
1595 if (map_length > num_bytes)
1596 map_length = num_bytes;
1598 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1599 btrfs_issue_discard(stripe->dev->bdev,
1609 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1610 struct btrfs_root *root,
1611 u64 bytenr, u64 num_bytes, u64 parent,
1612 u64 root_objectid, u64 owner, u64 offset)
1615 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1616 root_objectid == BTRFS_TREE_LOG_OBJECTID);
1618 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1619 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1620 parent, root_objectid, (int)owner,
1621 BTRFS_ADD_DELAYED_REF, NULL);
1623 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1624 parent, root_objectid, owner, offset,
1625 BTRFS_ADD_DELAYED_REF, NULL);
1630 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1631 struct btrfs_root *root,
1632 u64 bytenr, u64 num_bytes,
1633 u64 parent, u64 root_objectid,
1634 u64 owner, u64 offset, int refs_to_add,
1635 struct btrfs_delayed_extent_op *extent_op)
1637 struct btrfs_path *path;
1638 struct extent_buffer *leaf;
1639 struct btrfs_extent_item *item;
1644 path = btrfs_alloc_path();
1649 path->leave_spinning = 1;
1650 /* this will setup the path even if it fails to insert the back ref */
1651 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1652 path, bytenr, num_bytes, parent,
1653 root_objectid, owner, offset,
1654 refs_to_add, extent_op);
1658 if (ret != -EAGAIN) {
1663 leaf = path->nodes[0];
1664 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1665 refs = btrfs_extent_refs(leaf, item);
1666 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1668 __run_delayed_extent_op(extent_op, leaf, item);
1670 btrfs_mark_buffer_dirty(leaf);
1671 btrfs_release_path(root->fs_info->extent_root, path);
1674 path->leave_spinning = 1;
1676 /* now insert the actual backref */
1677 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1678 path, bytenr, parent, root_objectid,
1679 owner, offset, refs_to_add);
1682 btrfs_free_path(path);
1686 static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1687 struct btrfs_root *root,
1688 struct btrfs_delayed_ref_node *node,
1689 struct btrfs_delayed_extent_op *extent_op,
1690 int insert_reserved)
1693 struct btrfs_delayed_data_ref *ref;
1694 struct btrfs_key ins;
1699 ins.objectid = node->bytenr;
1700 ins.offset = node->num_bytes;
1701 ins.type = BTRFS_EXTENT_ITEM_KEY;
1703 ref = btrfs_delayed_node_to_data_ref(node);
1704 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1705 parent = ref->parent;
1707 ref_root = ref->root;
1709 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1711 BUG_ON(extent_op->update_key);
1712 flags |= extent_op->flags_to_set;
1714 ret = alloc_reserved_file_extent(trans, root,
1715 parent, ref_root, flags,
1716 ref->objectid, ref->offset,
1717 &ins, node->ref_mod);
1718 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1719 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1720 node->num_bytes, parent,
1721 ref_root, ref->objectid,
1722 ref->offset, node->ref_mod,
1724 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1725 ret = __btrfs_free_extent(trans, root, node->bytenr,
1726 node->num_bytes, parent,
1727 ref_root, ref->objectid,
1728 ref->offset, node->ref_mod,
1736 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1737 struct extent_buffer *leaf,
1738 struct btrfs_extent_item *ei)
1740 u64 flags = btrfs_extent_flags(leaf, ei);
1741 if (extent_op->update_flags) {
1742 flags |= extent_op->flags_to_set;
1743 btrfs_set_extent_flags(leaf, ei, flags);
1746 if (extent_op->update_key) {
1747 struct btrfs_tree_block_info *bi;
1748 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1749 bi = (struct btrfs_tree_block_info *)(ei + 1);
1750 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1754 static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1755 struct btrfs_root *root,
1756 struct btrfs_delayed_ref_node *node,
1757 struct btrfs_delayed_extent_op *extent_op)
1759 struct btrfs_key key;
1760 struct btrfs_path *path;
1761 struct btrfs_extent_item *ei;
1762 struct extent_buffer *leaf;
1767 path = btrfs_alloc_path();
1771 key.objectid = node->bytenr;
1772 key.type = BTRFS_EXTENT_ITEM_KEY;
1773 key.offset = node->num_bytes;
1776 path->leave_spinning = 1;
1777 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1788 leaf = path->nodes[0];
1789 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1790 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1791 if (item_size < sizeof(*ei)) {
1792 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1798 leaf = path->nodes[0];
1799 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1802 BUG_ON(item_size < sizeof(*ei));
1803 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1804 __run_delayed_extent_op(extent_op, leaf, ei);
1806 btrfs_mark_buffer_dirty(leaf);
1808 btrfs_free_path(path);
1812 static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1813 struct btrfs_root *root,
1814 struct btrfs_delayed_ref_node *node,
1815 struct btrfs_delayed_extent_op *extent_op,
1816 int insert_reserved)
1819 struct btrfs_delayed_tree_ref *ref;
1820 struct btrfs_key ins;
1824 ins.objectid = node->bytenr;
1825 ins.offset = node->num_bytes;
1826 ins.type = BTRFS_EXTENT_ITEM_KEY;
1828 ref = btrfs_delayed_node_to_tree_ref(node);
1829 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1830 parent = ref->parent;
1832 ref_root = ref->root;
1834 BUG_ON(node->ref_mod != 1);
1835 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1836 BUG_ON(!extent_op || !extent_op->update_flags ||
1837 !extent_op->update_key);
1838 ret = alloc_reserved_tree_block(trans, root,
1840 extent_op->flags_to_set,
1843 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1844 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1845 node->num_bytes, parent, ref_root,
1846 ref->level, 0, 1, extent_op);
1847 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1848 ret = __btrfs_free_extent(trans, root, node->bytenr,
1849 node->num_bytes, parent, ref_root,
1850 ref->level, 0, 1, extent_op);
1858 /* helper function to actually process a single delayed ref entry */
1859 static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
1860 struct btrfs_root *root,
1861 struct btrfs_delayed_ref_node *node,
1862 struct btrfs_delayed_extent_op *extent_op,
1863 int insert_reserved)
1866 if (btrfs_delayed_ref_is_head(node)) {
1867 struct btrfs_delayed_ref_head *head;
1869 * we've hit the end of the chain and we were supposed
1870 * to insert this extent into the tree. But, it got
1871 * deleted before we ever needed to insert it, so all
1872 * we have to do is clean up the accounting
1875 head = btrfs_delayed_node_to_head(node);
1876 if (insert_reserved) {
1878 struct extent_buffer *must_clean = NULL;
1880 ret = pin_down_bytes(trans, root, NULL,
1881 node->bytenr, node->num_bytes,
1882 head->is_data, 1, &must_clean);
1887 clean_tree_block(NULL, root, must_clean);
1888 btrfs_tree_unlock(must_clean);
1889 free_extent_buffer(must_clean);
1891 if (head->is_data) {
1892 ret = btrfs_del_csums(trans, root,
1898 ret = btrfs_free_reserved_extent(root,
1904 mutex_unlock(&head->mutex);
1908 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1909 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1910 ret = run_delayed_tree_ref(trans, root, node, extent_op,
1912 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1913 node->type == BTRFS_SHARED_DATA_REF_KEY)
1914 ret = run_delayed_data_ref(trans, root, node, extent_op,
1921 static noinline struct btrfs_delayed_ref_node *
1922 select_delayed_ref(struct btrfs_delayed_ref_head *head)
1924 struct rb_node *node;
1925 struct btrfs_delayed_ref_node *ref;
1926 int action = BTRFS_ADD_DELAYED_REF;
1929 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
1930 * this prevents ref count from going down to zero when
1931 * there still are pending delayed ref.
1933 node = rb_prev(&head->node.rb_node);
1937 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1939 if (ref->bytenr != head->node.bytenr)
1941 if (ref->action == action)
1943 node = rb_prev(node);
1945 if (action == BTRFS_ADD_DELAYED_REF) {
1946 action = BTRFS_DROP_DELAYED_REF;
1952 static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
1953 struct btrfs_root *root,
1954 struct list_head *cluster)
1956 struct btrfs_delayed_ref_root *delayed_refs;
1957 struct btrfs_delayed_ref_node *ref;
1958 struct btrfs_delayed_ref_head *locked_ref = NULL;
1959 struct btrfs_delayed_extent_op *extent_op;
1962 int must_insert_reserved = 0;
1964 delayed_refs = &trans->transaction->delayed_refs;
1967 /* pick a new head ref from the cluster list */
1968 if (list_empty(cluster))
1971 locked_ref = list_entry(cluster->next,
1972 struct btrfs_delayed_ref_head, cluster);
1974 /* grab the lock that says we are going to process
1975 * all the refs for this head */
1976 ret = btrfs_delayed_ref_lock(trans, locked_ref);
1979 * we may have dropped the spin lock to get the head
1980 * mutex lock, and that might have given someone else
1981 * time to free the head. If that's true, it has been
1982 * removed from our list and we can move on.
1984 if (ret == -EAGAIN) {
1992 * record the must insert reserved flag before we
1993 * drop the spin lock.
1995 must_insert_reserved = locked_ref->must_insert_reserved;
1996 locked_ref->must_insert_reserved = 0;
1998 extent_op = locked_ref->extent_op;
1999 locked_ref->extent_op = NULL;
2002 * locked_ref is the head node, so we have to go one
2003 * node back for any delayed ref updates
2005 ref = select_delayed_ref(locked_ref);
2007 /* All delayed refs have been processed, Go ahead
2008 * and send the head node to run_one_delayed_ref,
2009 * so that any accounting fixes can happen
2011 ref = &locked_ref->node;
2013 if (extent_op && must_insert_reserved) {
2019 spin_unlock(&delayed_refs->lock);
2021 ret = run_delayed_extent_op(trans, root,
2027 spin_lock(&delayed_refs->lock);
2031 list_del_init(&locked_ref->cluster);
2036 rb_erase(&ref->rb_node, &delayed_refs->root);
2037 delayed_refs->num_entries--;
2039 spin_unlock(&delayed_refs->lock);
2041 ret = run_one_delayed_ref(trans, root, ref, extent_op,
2042 must_insert_reserved);
2045 btrfs_put_delayed_ref(ref);
2050 spin_lock(&delayed_refs->lock);
2056 * this starts processing the delayed reference count updates and
2057 * extent insertions we have queued up so far. count can be
2058 * 0, which means to process everything in the tree at the start
2059 * of the run (but not newly added entries), or it can be some target
2060 * number you'd like to process.
2062 int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2063 struct btrfs_root *root, unsigned long count)
2065 struct rb_node *node;
2066 struct btrfs_delayed_ref_root *delayed_refs;
2067 struct btrfs_delayed_ref_node *ref;
2068 struct list_head cluster;
2070 int run_all = count == (unsigned long)-1;
2073 if (root == root->fs_info->extent_root)
2074 root = root->fs_info->tree_root;
2076 delayed_refs = &trans->transaction->delayed_refs;
2077 INIT_LIST_HEAD(&cluster);
2079 spin_lock(&delayed_refs->lock);
2081 count = delayed_refs->num_entries * 2;
2085 if (!(run_all || run_most) &&
2086 delayed_refs->num_heads_ready < 64)
2090 * go find something we can process in the rbtree. We start at
2091 * the beginning of the tree, and then build a cluster
2092 * of refs to process starting at the first one we are able to
2095 ret = btrfs_find_ref_cluster(trans, &cluster,
2096 delayed_refs->run_delayed_start);
2100 ret = run_clustered_refs(trans, root, &cluster);
2103 count -= min_t(unsigned long, ret, count);
2110 node = rb_first(&delayed_refs->root);
2113 count = (unsigned long)-1;
2116 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2118 if (btrfs_delayed_ref_is_head(ref)) {
2119 struct btrfs_delayed_ref_head *head;
2121 head = btrfs_delayed_node_to_head(ref);
2122 atomic_inc(&ref->refs);
2124 spin_unlock(&delayed_refs->lock);
2125 mutex_lock(&head->mutex);
2126 mutex_unlock(&head->mutex);
2128 btrfs_put_delayed_ref(ref);
2132 node = rb_next(node);
2134 spin_unlock(&delayed_refs->lock);
2135 schedule_timeout(1);
2139 spin_unlock(&delayed_refs->lock);
2143 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2144 struct btrfs_root *root,
2145 u64 bytenr, u64 num_bytes, u64 flags,
2148 struct btrfs_delayed_extent_op *extent_op;
2151 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2155 extent_op->flags_to_set = flags;
2156 extent_op->update_flags = 1;
2157 extent_op->update_key = 0;
2158 extent_op->is_data = is_data ? 1 : 0;
2160 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2166 static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2167 struct btrfs_root *root,
2168 struct btrfs_path *path,
2169 u64 objectid, u64 offset, u64 bytenr)
2171 struct btrfs_delayed_ref_head *head;
2172 struct btrfs_delayed_ref_node *ref;
2173 struct btrfs_delayed_data_ref *data_ref;
2174 struct btrfs_delayed_ref_root *delayed_refs;
2175 struct rb_node *node;
2179 delayed_refs = &trans->transaction->delayed_refs;
2180 spin_lock(&delayed_refs->lock);
2181 head = btrfs_find_delayed_ref_head(trans, bytenr);
2185 if (!mutex_trylock(&head->mutex)) {
2186 atomic_inc(&head->node.refs);
2187 spin_unlock(&delayed_refs->lock);
2189 btrfs_release_path(root->fs_info->extent_root, path);
2191 mutex_lock(&head->mutex);
2192 mutex_unlock(&head->mutex);
2193 btrfs_put_delayed_ref(&head->node);
2197 node = rb_prev(&head->node.rb_node);
2201 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2203 if (ref->bytenr != bytenr)
2207 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2210 data_ref = btrfs_delayed_node_to_data_ref(ref);
2212 node = rb_prev(node);
2214 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2215 if (ref->bytenr == bytenr)
2219 if (data_ref->root != root->root_key.objectid ||
2220 data_ref->objectid != objectid || data_ref->offset != offset)
2225 mutex_unlock(&head->mutex);
2227 spin_unlock(&delayed_refs->lock);
2231 static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2232 struct btrfs_root *root,
2233 struct btrfs_path *path,
2234 u64 objectid, u64 offset, u64 bytenr)
2236 struct btrfs_root *extent_root = root->fs_info->extent_root;
2237 struct extent_buffer *leaf;
2238 struct btrfs_extent_data_ref *ref;
2239 struct btrfs_extent_inline_ref *iref;
2240 struct btrfs_extent_item *ei;
2241 struct btrfs_key key;
2245 key.objectid = bytenr;
2246 key.offset = (u64)-1;
2247 key.type = BTRFS_EXTENT_ITEM_KEY;
2249 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2255 if (path->slots[0] == 0)
2259 leaf = path->nodes[0];
2260 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2262 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
2266 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2267 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2268 if (item_size < sizeof(*ei)) {
2269 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2273 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2275 if (item_size != sizeof(*ei) +
2276 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2279 if (btrfs_extent_generation(leaf, ei) <=
2280 btrfs_root_last_snapshot(&root->root_item))
2283 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2284 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2285 BTRFS_EXTENT_DATA_REF_KEY)
2288 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2289 if (btrfs_extent_refs(leaf, ei) !=
2290 btrfs_extent_data_ref_count(leaf, ref) ||
2291 btrfs_extent_data_ref_root(leaf, ref) !=
2292 root->root_key.objectid ||
2293 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2294 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2302 int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2303 struct btrfs_root *root,
2304 u64 objectid, u64 offset, u64 bytenr)
2306 struct btrfs_path *path;
2310 path = btrfs_alloc_path();
2315 ret = check_committed_ref(trans, root, path, objectid,
2317 if (ret && ret != -ENOENT)
2320 ret2 = check_delayed_ref(trans, root, path, objectid,
2322 } while (ret2 == -EAGAIN);
2324 if (ret2 && ret2 != -ENOENT) {
2329 if (ret != -ENOENT || ret2 != -ENOENT)
2332 btrfs_free_path(path);
2337 int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2338 struct extent_buffer *buf, u32 nr_extents)
2340 struct btrfs_key key;
2341 struct btrfs_file_extent_item *fi;
2349 if (!root->ref_cows)
2352 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2354 root_gen = root->root_key.offset;
2357 root_gen = trans->transid - 1;
2360 level = btrfs_header_level(buf);
2361 nritems = btrfs_header_nritems(buf);
2364 struct btrfs_leaf_ref *ref;
2365 struct btrfs_extent_info *info;
2367 ref = btrfs_alloc_leaf_ref(root, nr_extents);
2373 ref->root_gen = root_gen;
2374 ref->bytenr = buf->start;
2375 ref->owner = btrfs_header_owner(buf);
2376 ref->generation = btrfs_header_generation(buf);
2377 ref->nritems = nr_extents;
2378 info = ref->extents;
2380 for (i = 0; nr_extents > 0 && i < nritems; i++) {
2382 btrfs_item_key_to_cpu(buf, &key, i);
2383 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2385 fi = btrfs_item_ptr(buf, i,
2386 struct btrfs_file_extent_item);
2387 if (btrfs_file_extent_type(buf, fi) ==
2388 BTRFS_FILE_EXTENT_INLINE)
2390 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2391 if (disk_bytenr == 0)
2394 info->bytenr = disk_bytenr;
2396 btrfs_file_extent_disk_num_bytes(buf, fi);
2397 info->objectid = key.objectid;
2398 info->offset = key.offset;
2402 ret = btrfs_add_leaf_ref(root, ref, shared);
2403 if (ret == -EEXIST && shared) {
2404 struct btrfs_leaf_ref *old;
2405 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2407 btrfs_remove_leaf_ref(root, old);
2408 btrfs_free_leaf_ref(root, old);
2409 ret = btrfs_add_leaf_ref(root, ref, shared);
2412 btrfs_free_leaf_ref(root, ref);
2418 /* when a block goes through cow, we update the reference counts of
2419 * everything that block points to. The internal pointers of the block
2420 * can be in just about any order, and it is likely to have clusters of
2421 * things that are close together and clusters of things that are not.
2423 * To help reduce the seeks that come with updating all of these reference
2424 * counts, sort them by byte number before actual updates are done.
2426 * struct refsort is used to match byte number to slot in the btree block.
2427 * we sort based on the byte number and then use the slot to actually
2430 * struct refsort is smaller than strcut btrfs_item and smaller than
2431 * struct btrfs_key_ptr. Since we're currently limited to the page size
2432 * for a btree block, there's no way for a kmalloc of refsorts for a
2433 * single node to be bigger than a page.
2441 * for passing into sort()
2443 static int refsort_cmp(const void *a_void, const void *b_void)
2445 const struct refsort *a = a_void;
2446 const struct refsort *b = b_void;
2448 if (a->bytenr < b->bytenr)
2450 if (a->bytenr > b->bytenr)
2456 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
2457 struct btrfs_root *root,
2458 struct extent_buffer *buf,
2459 int full_backref, int inc)
2466 struct btrfs_key key;
2467 struct btrfs_file_extent_item *fi;
2471 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
2472 u64, u64, u64, u64, u64, u64);
2474 ref_root = btrfs_header_owner(buf);
2475 nritems = btrfs_header_nritems(buf);
2476 level = btrfs_header_level(buf);
2478 if (!root->ref_cows && level == 0)
2482 process_func = btrfs_inc_extent_ref;
2484 process_func = btrfs_free_extent;
2487 parent = buf->start;
2491 for (i = 0; i < nritems; i++) {
2493 btrfs_item_key_to_cpu(buf, &key, i);
2494 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2496 fi = btrfs_item_ptr(buf, i,
2497 struct btrfs_file_extent_item);
2498 if (btrfs_file_extent_type(buf, fi) ==
2499 BTRFS_FILE_EXTENT_INLINE)
2501 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2505 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2506 key.offset -= btrfs_file_extent_offset(buf, fi);
2507 ret = process_func(trans, root, bytenr, num_bytes,
2508 parent, ref_root, key.objectid,
2513 bytenr = btrfs_node_blockptr(buf, i);
2514 num_bytes = btrfs_level_size(root, level - 1);
2515 ret = process_func(trans, root, bytenr, num_bytes,
2516 parent, ref_root, level - 1, 0);
2527 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2528 struct extent_buffer *buf, int full_backref)
2530 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2533 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2534 struct extent_buffer *buf, int full_backref)
2536 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
2539 static int write_one_cache_group(struct btrfs_trans_handle *trans,
2540 struct btrfs_root *root,
2541 struct btrfs_path *path,
2542 struct btrfs_block_group_cache *cache)
2545 struct btrfs_root *extent_root = root->fs_info->extent_root;
2547 struct extent_buffer *leaf;
2549 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
2554 leaf = path->nodes[0];
2555 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2556 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2557 btrfs_mark_buffer_dirty(leaf);
2558 btrfs_release_path(extent_root, path);
2566 static struct btrfs_block_group_cache *
2567 next_block_group(struct btrfs_root *root,
2568 struct btrfs_block_group_cache *cache)
2570 struct rb_node *node;
2571 spin_lock(&root->fs_info->block_group_cache_lock);
2572 node = rb_next(&cache->cache_node);
2573 btrfs_put_block_group(cache);
2575 cache = rb_entry(node, struct btrfs_block_group_cache,
2577 atomic_inc(&cache->count);
2580 spin_unlock(&root->fs_info->block_group_cache_lock);
2584 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2585 struct btrfs_root *root)
2587 struct btrfs_block_group_cache *cache;
2589 struct btrfs_path *path;
2592 path = btrfs_alloc_path();
2598 err = btrfs_run_delayed_refs(trans, root,
2603 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2607 cache = next_block_group(root, cache);
2617 last = cache->key.objectid + cache->key.offset;
2619 err = write_one_cache_group(trans, root, path, cache);
2621 btrfs_put_block_group(cache);
2624 btrfs_free_path(path);
2628 int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2630 struct btrfs_block_group_cache *block_group;
2633 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2634 if (!block_group || block_group->ro)
2637 btrfs_put_block_group(block_group);
2641 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2642 u64 total_bytes, u64 bytes_used,
2643 struct btrfs_space_info **space_info)
2645 struct btrfs_space_info *found;
2647 found = __find_space_info(info, flags);
2649 spin_lock(&found->lock);
2650 found->total_bytes += total_bytes;
2651 found->bytes_used += bytes_used;
2653 spin_unlock(&found->lock);
2654 *space_info = found;
2657 found = kzalloc(sizeof(*found), GFP_NOFS);
2661 INIT_LIST_HEAD(&found->block_groups);
2662 init_rwsem(&found->groups_sem);
2663 spin_lock_init(&found->lock);
2664 found->flags = flags;
2665 found->total_bytes = total_bytes;
2666 found->bytes_used = bytes_used;
2667 found->bytes_pinned = 0;
2668 found->bytes_reserved = 0;
2669 found->bytes_readonly = 0;
2670 found->bytes_delalloc = 0;
2672 found->force_alloc = 0;
2673 *space_info = found;
2674 list_add_rcu(&found->list, &info->space_info);
2675 atomic_set(&found->caching_threads, 0);
2679 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2681 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
2682 BTRFS_BLOCK_GROUP_RAID1 |
2683 BTRFS_BLOCK_GROUP_RAID10 |
2684 BTRFS_BLOCK_GROUP_DUP);
2686 if (flags & BTRFS_BLOCK_GROUP_DATA)
2687 fs_info->avail_data_alloc_bits |= extra_flags;
2688 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2689 fs_info->avail_metadata_alloc_bits |= extra_flags;
2690 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2691 fs_info->avail_system_alloc_bits |= extra_flags;
2695 static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
2697 spin_lock(&cache->space_info->lock);
2698 spin_lock(&cache->lock);
2700 cache->space_info->bytes_readonly += cache->key.offset -
2701 btrfs_block_group_used(&cache->item);
2704 spin_unlock(&cache->lock);
2705 spin_unlock(&cache->space_info->lock);
2708 u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
2710 u64 num_devices = root->fs_info->fs_devices->rw_devices;
2712 if (num_devices == 1)
2713 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2714 if (num_devices < 4)
2715 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2717 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2718 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
2719 BTRFS_BLOCK_GROUP_RAID10))) {
2720 flags &= ~BTRFS_BLOCK_GROUP_DUP;
2723 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
2724 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
2725 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
2728 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2729 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2730 (flags & BTRFS_BLOCK_GROUP_RAID10) |
2731 (flags & BTRFS_BLOCK_GROUP_DUP)))
2732 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2736 static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
2738 struct btrfs_fs_info *info = root->fs_info;
2742 alloc_profile = info->avail_data_alloc_bits &
2743 info->data_alloc_profile;
2744 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2745 } else if (root == root->fs_info->chunk_root) {
2746 alloc_profile = info->avail_system_alloc_bits &
2747 info->system_alloc_profile;
2748 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2750 alloc_profile = info->avail_metadata_alloc_bits &
2751 info->metadata_alloc_profile;
2752 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2755 return btrfs_reduce_alloc_profile(root, data);
2758 void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
2762 alloc_target = btrfs_get_alloc_profile(root, 1);
2763 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
2767 static u64 calculate_bytes_needed(struct btrfs_root *root, int num_items)
2772 level = BTRFS_MAX_LEVEL - 2;
2774 * NOTE: these calculations are absolutely the worst possible case.
2775 * This assumes that _every_ item we insert will require a new leaf, and
2776 * that the tree has grown to its maximum level size.
2780 * for every item we insert we could insert both an extent item and a
2781 * extent ref item. Then for ever item we insert, we will need to cow
2782 * both the original leaf, plus the leaf to the left and right of it.
2784 * Unless we are talking about the extent root, then we just want the
2785 * number of items * 2, since we just need the extent item plus its ref.
2787 if (root == root->fs_info->extent_root)
2788 num_bytes = num_items * 2;
2790 num_bytes = (num_items + (2 * num_items)) * 3;
2793 * num_bytes is total number of leaves we could need times the leaf
2794 * size, and then for every leaf we could end up cow'ing 2 nodes per
2795 * level, down to the leaf level.
2797 num_bytes = (num_bytes * root->leafsize) +
2798 (num_bytes * (level * 2)) * root->nodesize;
2804 * Unreserve metadata space for delalloc. If we have less reserved credits than
2805 * we have extents, this function does nothing.
2807 int btrfs_unreserve_metadata_for_delalloc(struct btrfs_root *root,
2808 struct inode *inode, int num_items)
2810 struct btrfs_fs_info *info = root->fs_info;
2811 struct btrfs_space_info *meta_sinfo;
2816 /* get the space info for where the metadata will live */
2817 alloc_target = btrfs_get_alloc_profile(root, 0);
2818 meta_sinfo = __find_space_info(info, alloc_target);
2820 num_bytes = calculate_bytes_needed(root->fs_info->extent_root,
2823 spin_lock(&meta_sinfo->lock);
2824 spin_lock(&BTRFS_I(inode)->accounting_lock);
2825 if (BTRFS_I(inode)->reserved_extents <=
2826 BTRFS_I(inode)->outstanding_extents) {
2827 spin_unlock(&BTRFS_I(inode)->accounting_lock);
2828 spin_unlock(&meta_sinfo->lock);
2831 spin_unlock(&BTRFS_I(inode)->accounting_lock);
2833 BTRFS_I(inode)->reserved_extents--;
2834 BUG_ON(BTRFS_I(inode)->reserved_extents < 0);
2836 if (meta_sinfo->bytes_delalloc < num_bytes) {
2838 meta_sinfo->bytes_delalloc = 0;
2840 meta_sinfo->bytes_delalloc -= num_bytes;
2842 spin_unlock(&meta_sinfo->lock);
2849 static void check_force_delalloc(struct btrfs_space_info *meta_sinfo)
2853 thresh = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
2854 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
2855 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
2856 meta_sinfo->bytes_may_use;
2858 thresh = meta_sinfo->total_bytes - thresh;
2860 do_div(thresh, 100);
2861 if (thresh <= meta_sinfo->bytes_delalloc)
2862 meta_sinfo->force_delalloc = 1;
2864 meta_sinfo->force_delalloc = 0;
2867 struct async_flush {
2868 struct btrfs_root *root;
2869 struct btrfs_space_info *info;
2870 struct btrfs_work work;
2873 static noinline void flush_delalloc_async(struct btrfs_work *work)
2875 struct async_flush *async;
2876 struct btrfs_root *root;
2877 struct btrfs_space_info *info;
2879 async = container_of(work, struct async_flush, work);
2883 btrfs_start_delalloc_inodes(root, 0);
2884 wake_up(&info->flush_wait);
2885 btrfs_wait_ordered_extents(root, 0, 0);
2887 spin_lock(&info->lock);
2889 spin_unlock(&info->lock);
2890 wake_up(&info->flush_wait);
2895 static void wait_on_flush(struct btrfs_space_info *info)
2901 prepare_to_wait(&info->flush_wait, &wait,
2902 TASK_UNINTERRUPTIBLE);
2903 spin_lock(&info->lock);
2904 if (!info->flushing) {
2905 spin_unlock(&info->lock);
2909 used = info->bytes_used + info->bytes_reserved +
2910 info->bytes_pinned + info->bytes_readonly +
2911 info->bytes_super + info->bytes_root +
2912 info->bytes_may_use + info->bytes_delalloc;
2913 if (used < info->total_bytes) {
2914 spin_unlock(&info->lock);
2917 spin_unlock(&info->lock);
2920 finish_wait(&info->flush_wait, &wait);
2923 static void flush_delalloc(struct btrfs_root *root,
2924 struct btrfs_space_info *info)
2926 struct async_flush *async;
2929 spin_lock(&info->lock);
2931 if (!info->flushing) {
2933 init_waitqueue_head(&info->flush_wait);
2938 spin_unlock(&info->lock);
2941 wait_on_flush(info);
2945 async = kzalloc(sizeof(*async), GFP_NOFS);
2951 async->work.func = flush_delalloc_async;
2953 btrfs_queue_worker(&root->fs_info->enospc_workers,
2955 wait_on_flush(info);
2959 btrfs_start_delalloc_inodes(root, 0);
2960 btrfs_wait_ordered_extents(root, 0, 0);
2962 spin_lock(&info->lock);
2964 spin_unlock(&info->lock);
2965 wake_up(&info->flush_wait);
2968 static int maybe_allocate_chunk(struct btrfs_root *root,
2969 struct btrfs_space_info *info)
2971 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
2972 struct btrfs_trans_handle *trans;
2978 free_space = btrfs_super_total_bytes(disk_super);
2980 * we allow the metadata to grow to a max of either 10gb or 5% of the
2981 * space in the volume.
2983 min_metadata = min((u64)10 * 1024 * 1024 * 1024,
2984 div64_u64(free_space * 5, 100));
2985 if (info->total_bytes >= min_metadata) {
2986 spin_unlock(&info->lock);
2991 spin_unlock(&info->lock);
2995 if (!info->allocating_chunk) {
2996 info->force_alloc = 1;
2997 info->allocating_chunk = 1;
2998 init_waitqueue_head(&info->allocate_wait);
3003 spin_unlock(&info->lock);
3006 wait_event(info->allocate_wait,
3007 !info->allocating_chunk);
3011 trans = btrfs_start_transaction(root, 1);
3017 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3018 4096 + 2 * 1024 * 1024,
3020 btrfs_end_transaction(trans, root);
3024 spin_lock(&info->lock);
3025 info->allocating_chunk = 0;
3026 spin_unlock(&info->lock);
3027 wake_up(&info->allocate_wait);
3035 * Reserve metadata space for delalloc.
3037 int btrfs_reserve_metadata_for_delalloc(struct btrfs_root *root,
3038 struct inode *inode, int num_items)
3040 struct btrfs_fs_info *info = root->fs_info;
3041 struct btrfs_space_info *meta_sinfo;
3048 /* get the space info for where the metadata will live */
3049 alloc_target = btrfs_get_alloc_profile(root, 0);
3050 meta_sinfo = __find_space_info(info, alloc_target);
3052 num_bytes = calculate_bytes_needed(root->fs_info->extent_root,
3055 spin_lock(&meta_sinfo->lock);
3057 force_delalloc = meta_sinfo->force_delalloc;
3059 if (unlikely(!meta_sinfo->bytes_root))
3060 meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
3063 meta_sinfo->bytes_delalloc += num_bytes;
3065 used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
3066 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
3067 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
3068 meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
3070 if (used > meta_sinfo->total_bytes) {
3074 if (maybe_allocate_chunk(root, meta_sinfo))
3078 spin_unlock(&meta_sinfo->lock);
3082 filemap_flush(inode->i_mapping);
3084 } else if (flushed == 3) {
3085 flush_delalloc(root, meta_sinfo);
3088 spin_lock(&meta_sinfo->lock);
3089 meta_sinfo->bytes_delalloc -= num_bytes;
3090 spin_unlock(&meta_sinfo->lock);
3091 printk(KERN_ERR "enospc, has %d, reserved %d\n",
3092 BTRFS_I(inode)->outstanding_extents,
3093 BTRFS_I(inode)->reserved_extents);
3094 dump_space_info(meta_sinfo, 0, 0);
3098 BTRFS_I(inode)->reserved_extents++;
3099 check_force_delalloc(meta_sinfo);
3100 spin_unlock(&meta_sinfo->lock);
3102 if (!flushed && force_delalloc)
3103 filemap_flush(inode->i_mapping);
3109 * unreserve num_items number of items worth of metadata space. This needs to
3110 * be paired with btrfs_reserve_metadata_space.
3112 * NOTE: if you have the option, run this _AFTER_ you do a
3113 * btrfs_end_transaction, since btrfs_end_transaction will run delayed ref
3114 * oprations which will result in more used metadata, so we want to make sure we
3115 * can do that without issue.
3117 int btrfs_unreserve_metadata_space(struct btrfs_root *root, int num_items)
3119 struct btrfs_fs_info *info = root->fs_info;
3120 struct btrfs_space_info *meta_sinfo;
3125 /* get the space info for where the metadata will live */
3126 alloc_target = btrfs_get_alloc_profile(root, 0);
3127 meta_sinfo = __find_space_info(info, alloc_target);
3129 num_bytes = calculate_bytes_needed(root, num_items);
3131 spin_lock(&meta_sinfo->lock);
3132 if (meta_sinfo->bytes_may_use < num_bytes) {
3134 meta_sinfo->bytes_may_use = 0;
3136 meta_sinfo->bytes_may_use -= num_bytes;
3138 spin_unlock(&meta_sinfo->lock);
3146 * Reserve some metadata space for use. We'll calculate the worste case number
3147 * of bytes that would be needed to modify num_items number of items. If we
3148 * have space, fantastic, if not, you get -ENOSPC. Please call
3149 * btrfs_unreserve_metadata_space when you are done for the _SAME_ number of
3150 * items you reserved, since whatever metadata you needed should have already
3153 * This will commit the transaction to make more space if we don't have enough
3154 * metadata space. THe only time we don't do this is if we're reserving space
3155 * inside of a transaction, then we will just return -ENOSPC and it is the
3156 * callers responsibility to handle it properly.
3158 int btrfs_reserve_metadata_space(struct btrfs_root *root, int num_items)
3160 struct btrfs_fs_info *info = root->fs_info;
3161 struct btrfs_space_info *meta_sinfo;
3167 /* get the space info for where the metadata will live */
3168 alloc_target = btrfs_get_alloc_profile(root, 0);
3169 meta_sinfo = __find_space_info(info, alloc_target);
3171 num_bytes = calculate_bytes_needed(root, num_items);
3173 spin_lock(&meta_sinfo->lock);
3175 if (unlikely(!meta_sinfo->bytes_root))
3176 meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
3179 meta_sinfo->bytes_may_use += num_bytes;
3181 used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
3182 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
3183 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
3184 meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
3186 if (used > meta_sinfo->total_bytes) {
3189 if (maybe_allocate_chunk(root, meta_sinfo))
3193 spin_unlock(&meta_sinfo->lock);
3197 flush_delalloc(root, meta_sinfo);
3200 spin_lock(&meta_sinfo->lock);
3201 meta_sinfo->bytes_may_use -= num_bytes;
3202 spin_unlock(&meta_sinfo->lock);
3204 dump_space_info(meta_sinfo, 0, 0);
3208 check_force_delalloc(meta_sinfo);
3209 spin_unlock(&meta_sinfo->lock);
3215 * This will check the space that the inode allocates from to make sure we have
3216 * enough space for bytes.
3218 int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
3221 struct btrfs_space_info *data_sinfo;
3222 int ret = 0, committed = 0;
3224 /* make sure bytes are sectorsize aligned */
3225 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3227 data_sinfo = BTRFS_I(inode)->space_info;
3232 /* make sure we have enough space to handle the data first */
3233 spin_lock(&data_sinfo->lock);
3234 if (data_sinfo->total_bytes - data_sinfo->bytes_used -
3235 data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
3236 data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
3237 data_sinfo->bytes_may_use - data_sinfo->bytes_super < bytes) {
3238 struct btrfs_trans_handle *trans;
3241 * if we don't have enough free bytes in this space then we need
3242 * to alloc a new chunk.
3244 if (!data_sinfo->full) {
3247 data_sinfo->force_alloc = 1;
3248 spin_unlock(&data_sinfo->lock);
3250 alloc_target = btrfs_get_alloc_profile(root, 1);
3251 trans = btrfs_start_transaction(root, 1);
3255 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3256 bytes + 2 * 1024 * 1024,
3258 btrfs_end_transaction(trans, root);
3263 btrfs_set_inode_space_info(root, inode);
3264 data_sinfo = BTRFS_I(inode)->space_info;
3268 spin_unlock(&data_sinfo->lock);
3270 /* commit the current transaction and try again */
3271 if (!committed && !root->fs_info->open_ioctl_trans) {
3273 trans = btrfs_join_transaction(root, 1);
3276 ret = btrfs_commit_transaction(trans, root);
3282 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
3283 ", %llu bytes_used, %llu bytes_reserved, "
3284 "%llu bytes_pinned, %llu bytes_readonly, %llu may use "
3285 "%llu total\n", (unsigned long long)bytes,
3286 (unsigned long long)data_sinfo->bytes_delalloc,
3287 (unsigned long long)data_sinfo->bytes_used,
3288 (unsigned long long)data_sinfo->bytes_reserved,
3289 (unsigned long long)data_sinfo->bytes_pinned,
3290 (unsigned long long)data_sinfo->bytes_readonly,
3291 (unsigned long long)data_sinfo->bytes_may_use,
3292 (unsigned long long)data_sinfo->total_bytes);
3295 data_sinfo->bytes_may_use += bytes;
3296 BTRFS_I(inode)->reserved_bytes += bytes;
3297 spin_unlock(&data_sinfo->lock);
3303 * if there was an error for whatever reason after calling
3304 * btrfs_check_data_free_space, call this so we can cleanup the counters.
3306 void btrfs_free_reserved_data_space(struct btrfs_root *root,
3307 struct inode *inode, u64 bytes)
3309 struct btrfs_space_info *data_sinfo;
3311 /* make sure bytes are sectorsize aligned */
3312 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3314 data_sinfo = BTRFS_I(inode)->space_info;
3315 spin_lock(&data_sinfo->lock);
3316 data_sinfo->bytes_may_use -= bytes;
3317 BTRFS_I(inode)->reserved_bytes -= bytes;
3318 spin_unlock(&data_sinfo->lock);
3321 /* called when we are adding a delalloc extent to the inode's io_tree */
3322 void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
3325 struct btrfs_space_info *data_sinfo;
3327 /* get the space info for where this inode will be storing its data */
3328 data_sinfo = BTRFS_I(inode)->space_info;
3330 /* make sure we have enough space to handle the data first */
3331 spin_lock(&data_sinfo->lock);
3332 data_sinfo->bytes_delalloc += bytes;
3335 * we are adding a delalloc extent without calling
3336 * btrfs_check_data_free_space first. This happens on a weird
3337 * writepage condition, but shouldn't hurt our accounting
3339 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
3340 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
3341 BTRFS_I(inode)->reserved_bytes = 0;
3343 data_sinfo->bytes_may_use -= bytes;
3344 BTRFS_I(inode)->reserved_bytes -= bytes;
3347 spin_unlock(&data_sinfo->lock);
3350 /* called when we are clearing an delalloc extent from the inode's io_tree */
3351 void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
3354 struct btrfs_space_info *info;
3356 info = BTRFS_I(inode)->space_info;
3358 spin_lock(&info->lock);
3359 info->bytes_delalloc -= bytes;
3360 spin_unlock(&info->lock);
3363 static void force_metadata_allocation(struct btrfs_fs_info *info)
3365 struct list_head *head = &info->space_info;
3366 struct btrfs_space_info *found;
3369 list_for_each_entry_rcu(found, head, list) {
3370 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
3371 found->force_alloc = 1;
3376 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3377 struct btrfs_root *extent_root, u64 alloc_bytes,
3378 u64 flags, int force)
3380 struct btrfs_space_info *space_info;
3381 struct btrfs_fs_info *fs_info = extent_root->fs_info;
3385 mutex_lock(&fs_info->chunk_mutex);
3387 flags = btrfs_reduce_alloc_profile(extent_root, flags);
3389 space_info = __find_space_info(extent_root->fs_info, flags);
3391 ret = update_space_info(extent_root->fs_info, flags,
3395 BUG_ON(!space_info);
3397 spin_lock(&space_info->lock);
3398 if (space_info->force_alloc)
3400 if (space_info->full) {
3401 spin_unlock(&space_info->lock);
3405 thresh = space_info->total_bytes - space_info->bytes_readonly;
3406 thresh = div_factor(thresh, 8);
3408 (space_info->bytes_used + space_info->bytes_pinned +
3409 space_info->bytes_reserved + alloc_bytes) < thresh) {
3410 spin_unlock(&space_info->lock);
3413 spin_unlock(&space_info->lock);
3416 * if we're doing a data chunk, go ahead and make sure that
3417 * we keep a reasonable number of metadata chunks allocated in the
3420 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
3421 fs_info->data_chunk_allocations++;
3422 if (!(fs_info->data_chunk_allocations %
3423 fs_info->metadata_ratio))
3424 force_metadata_allocation(fs_info);
3427 ret = btrfs_alloc_chunk(trans, extent_root, flags);
3428 spin_lock(&space_info->lock);
3430 space_info->full = 1;
3431 space_info->force_alloc = 0;
3432 spin_unlock(&space_info->lock);
3434 mutex_unlock(&extent_root->fs_info->chunk_mutex);
3438 static int update_block_group(struct btrfs_trans_handle *trans,
3439 struct btrfs_root *root,
3440 u64 bytenr, u64 num_bytes, int alloc,
3443 struct btrfs_block_group_cache *cache;
3444 struct btrfs_fs_info *info = root->fs_info;
3445 u64 total = num_bytes;
3449 /* block accounting for super block */
3450 spin_lock(&info->delalloc_lock);
3451 old_val = btrfs_super_bytes_used(&info->super_copy);
3453 old_val += num_bytes;
3455 old_val -= num_bytes;
3456 btrfs_set_super_bytes_used(&info->super_copy, old_val);
3458 /* block accounting for root item */
3459 old_val = btrfs_root_used(&root->root_item);
3461 old_val += num_bytes;
3463 old_val -= num_bytes;
3464 btrfs_set_root_used(&root->root_item, old_val);
3465 spin_unlock(&info->delalloc_lock);
3468 cache = btrfs_lookup_block_group(info, bytenr);
3471 byte_in_group = bytenr - cache->key.objectid;
3472 WARN_ON(byte_in_group > cache->key.offset);
3474 spin_lock(&cache->space_info->lock);
3475 spin_lock(&cache->lock);
3477 old_val = btrfs_block_group_used(&cache->item);
3478 num_bytes = min(total, cache->key.offset - byte_in_group);
3480 old_val += num_bytes;
3481 btrfs_set_block_group_used(&cache->item, old_val);
3482 cache->reserved -= num_bytes;
3483 cache->space_info->bytes_used += num_bytes;
3484 cache->space_info->bytes_reserved -= num_bytes;
3486 cache->space_info->bytes_readonly -= num_bytes;
3487 spin_unlock(&cache->lock);
3488 spin_unlock(&cache->space_info->lock);
3490 old_val -= num_bytes;
3491 cache->space_info->bytes_used -= num_bytes;
3493 cache->space_info->bytes_readonly += num_bytes;
3494 btrfs_set_block_group_used(&cache->item, old_val);
3495 spin_unlock(&cache->lock);
3496 spin_unlock(&cache->space_info->lock);
3500 ret = btrfs_discard_extent(root, bytenr,
3504 ret = btrfs_add_free_space(cache, bytenr,
3509 btrfs_put_block_group(cache);
3511 bytenr += num_bytes;
3516 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
3518 struct btrfs_block_group_cache *cache;
3521 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
3525 bytenr = cache->key.objectid;
3526 btrfs_put_block_group(cache);
3532 * this function must be called within transaction
3534 int btrfs_pin_extent(struct btrfs_root *root,
3535 u64 bytenr, u64 num_bytes, int reserved)
3537 struct btrfs_fs_info *fs_info = root->fs_info;
3538 struct btrfs_block_group_cache *cache;
3540 cache = btrfs_lookup_block_group(fs_info, bytenr);
3543 spin_lock(&cache->space_info->lock);
3544 spin_lock(&cache->lock);
3545 cache->pinned += num_bytes;
3546 cache->space_info->bytes_pinned += 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);
3554 btrfs_put_block_group(cache);
3556 set_extent_dirty(fs_info->pinned_extents,
3557 bytenr, bytenr + num_bytes - 1, GFP_NOFS);
3561 static int update_reserved_extents(struct btrfs_block_group_cache *cache,
3562 u64 num_bytes, int reserve)
3564 spin_lock(&cache->space_info->lock);
3565 spin_lock(&cache->lock);
3567 cache->reserved += num_bytes;
3568 cache->space_info->bytes_reserved += num_bytes;
3570 cache->reserved -= num_bytes;
3571 cache->space_info->bytes_reserved -= num_bytes;
3573 spin_unlock(&cache->lock);
3574 spin_unlock(&cache->space_info->lock);
3578 int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
3579 struct btrfs_root *root)
3581 struct btrfs_fs_info *fs_info = root->fs_info;
3582 struct btrfs_caching_control *next;
3583 struct btrfs_caching_control *caching_ctl;
3584 struct btrfs_block_group_cache *cache;
3586 down_write(&fs_info->extent_commit_sem);
3588 list_for_each_entry_safe(caching_ctl, next,
3589 &fs_info->caching_block_groups, list) {
3590 cache = caching_ctl->block_group;
3591 if (block_group_cache_done(cache)) {
3592 cache->last_byte_to_unpin = (u64)-1;
3593 list_del_init(&caching_ctl->list);
3594 put_caching_control(caching_ctl);
3596 cache->last_byte_to_unpin = caching_ctl->progress;
3600 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3601 fs_info->pinned_extents = &fs_info->freed_extents[1];
3603 fs_info->pinned_extents = &fs_info->freed_extents[0];
3605 up_write(&fs_info->extent_commit_sem);
3609 static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
3611 struct btrfs_fs_info *fs_info = root->fs_info;
3612 struct btrfs_block_group_cache *cache = NULL;
3615 while (start <= end) {
3617 start >= cache->key.objectid + cache->key.offset) {
3619 btrfs_put_block_group(cache);
3620 cache = btrfs_lookup_block_group(fs_info, start);
3624 len = cache->key.objectid + cache->key.offset - start;
3625 len = min(len, end + 1 - start);
3627 if (start < cache->last_byte_to_unpin) {
3628 len = min(len, cache->last_byte_to_unpin - start);
3629 btrfs_add_free_space(cache, start, len);
3632 spin_lock(&cache->space_info->lock);
3633 spin_lock(&cache->lock);
3634 cache->pinned -= len;
3635 cache->space_info->bytes_pinned -= len;
3636 spin_unlock(&cache->lock);
3637 spin_unlock(&cache->space_info->lock);
3643 btrfs_put_block_group(cache);
3647 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
3648 struct btrfs_root *root)
3650 struct btrfs_fs_info *fs_info = root->fs_info;
3651 struct extent_io_tree *unpin;
3656 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3657 unpin = &fs_info->freed_extents[1];
3659 unpin = &fs_info->freed_extents[0];
3662 ret = find_first_extent_bit(unpin, 0, &start, &end,
3667 ret = btrfs_discard_extent(root, start, end + 1 - start);
3669 clear_extent_dirty(unpin, start, end, GFP_NOFS);
3670 unpin_extent_range(root, start, end);
3677 static int pin_down_bytes(struct btrfs_trans_handle *trans,
3678 struct btrfs_root *root,
3679 struct btrfs_path *path,
3680 u64 bytenr, u64 num_bytes,
3681 int is_data, int reserved,
3682 struct extent_buffer **must_clean)
3685 struct extent_buffer *buf;
3691 * discard is sloooow, and so triggering discards on
3692 * individual btree blocks isn't a good plan. Just
3693 * pin everything in discard mode.
3695 if (btrfs_test_opt(root, DISCARD))
3698 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
3702 /* we can reuse a block if it hasn't been written
3703 * and it is from this transaction. We can't
3704 * reuse anything from the tree log root because
3705 * it has tiny sub-transactions.
3707 if (btrfs_buffer_uptodate(buf, 0) &&
3708 btrfs_try_tree_lock(buf)) {
3709 u64 header_owner = btrfs_header_owner(buf);
3710 u64 header_transid = btrfs_header_generation(buf);
3711 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
3712 header_transid == trans->transid &&
3713 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
3717 btrfs_tree_unlock(buf);
3719 free_extent_buffer(buf);
3722 btrfs_set_path_blocking(path);
3723 /* unlocks the pinned mutex */
3724 btrfs_pin_extent(root, bytenr, num_bytes, reserved);
3730 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
3731 struct btrfs_root *root,
3732 u64 bytenr, u64 num_bytes, u64 parent,
3733 u64 root_objectid, u64 owner_objectid,
3734 u64 owner_offset, int refs_to_drop,
3735 struct btrfs_delayed_extent_op *extent_op)
3737 struct btrfs_key key;
3738 struct btrfs_path *path;
3739 struct btrfs_fs_info *info = root->fs_info;
3740 struct btrfs_root *extent_root = info->extent_root;
3741 struct extent_buffer *leaf;
3742 struct btrfs_extent_item *ei;
3743 struct btrfs_extent_inline_ref *iref;
3746 int extent_slot = 0;
3747 int found_extent = 0;
3752 path = btrfs_alloc_path();
3757 path->leave_spinning = 1;
3759 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
3760 BUG_ON(!is_data && refs_to_drop != 1);
3762 ret = lookup_extent_backref(trans, extent_root, path, &iref,
3763 bytenr, num_bytes, parent,
3764 root_objectid, owner_objectid,
3767 extent_slot = path->slots[0];
3768 while (extent_slot >= 0) {
3769 btrfs_item_key_to_cpu(path->nodes[0], &key,
3771 if (key.objectid != bytenr)
3773 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3774 key.offset == num_bytes) {
3778 if (path->slots[0] - extent_slot > 5)
3782 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3783 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
3784 if (found_extent && item_size < sizeof(*ei))
3787 if (!found_extent) {
3789 ret = remove_extent_backref(trans, extent_root, path,
3793 btrfs_release_path(extent_root, path);
3794 path->leave_spinning = 1;
3796 key.objectid = bytenr;
3797 key.type = BTRFS_EXTENT_ITEM_KEY;
3798 key.offset = num_bytes;
3800 ret = btrfs_search_slot(trans, extent_root,
3803 printk(KERN_ERR "umm, got %d back from search"
3804 ", was looking for %llu\n", ret,
3805 (unsigned long long)bytenr);
3806 btrfs_print_leaf(extent_root, path->nodes[0]);
3809 extent_slot = path->slots[0];
3812 btrfs_print_leaf(extent_root, path->nodes[0]);
3814 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
3815 "parent %llu root %llu owner %llu offset %llu\n",
3816 (unsigned long long)bytenr,
3817 (unsigned long long)parent,
3818 (unsigned long long)root_objectid,
3819 (unsigned long long)owner_objectid,
3820 (unsigned long long)owner_offset);
3823 leaf = path->nodes[0];
3824 item_size = btrfs_item_size_nr(leaf, extent_slot);
3825 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3826 if (item_size < sizeof(*ei)) {
3827 BUG_ON(found_extent || extent_slot != path->slots[0]);
3828 ret = convert_extent_item_v0(trans, extent_root, path,
3832 btrfs_release_path(extent_root, path);
3833 path->leave_spinning = 1;
3835 key.objectid = bytenr;
3836 key.type = BTRFS_EXTENT_ITEM_KEY;
3837 key.offset = num_bytes;
3839 ret = btrfs_search_slot(trans, extent_root, &key, path,
3842 printk(KERN_ERR "umm, got %d back from search"
3843 ", was looking for %llu\n", ret,
3844 (unsigned long long)bytenr);
3845 btrfs_print_leaf(extent_root, path->nodes[0]);
3848 extent_slot = path->slots[0];
3849 leaf = path->nodes[0];
3850 item_size = btrfs_item_size_nr(leaf, extent_slot);
3853 BUG_ON(item_size < sizeof(*ei));
3854 ei = btrfs_item_ptr(leaf, extent_slot,
3855 struct btrfs_extent_item);
3856 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3857 struct btrfs_tree_block_info *bi;
3858 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
3859 bi = (struct btrfs_tree_block_info *)(ei + 1);
3860 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
3863 refs = btrfs_extent_refs(leaf, ei);
3864 BUG_ON(refs < refs_to_drop);
3865 refs -= refs_to_drop;
3869 __run_delayed_extent_op(extent_op, leaf, ei);
3871 * In the case of inline back ref, reference count will
3872 * be updated by remove_extent_backref
3875 BUG_ON(!found_extent);
3877 btrfs_set_extent_refs(leaf, ei, refs);
3878 btrfs_mark_buffer_dirty(leaf);
3881 ret = remove_extent_backref(trans, extent_root, path,
3888 struct extent_buffer *must_clean = NULL;
3891 BUG_ON(is_data && refs_to_drop !=
3892 extent_data_ref_count(root, path, iref));
3894 BUG_ON(path->slots[0] != extent_slot);
3896 BUG_ON(path->slots[0] != extent_slot + 1);
3897 path->slots[0] = extent_slot;
3902 ret = pin_down_bytes(trans, root, path, bytenr,
3903 num_bytes, is_data, 0, &must_clean);
3908 * it is going to be very rare for someone to be waiting
3909 * on the block we're freeing. del_items might need to
3910 * schedule, so rather than get fancy, just force it
3914 btrfs_set_lock_blocking(must_clean);
3916 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3919 btrfs_release_path(extent_root, path);
3922 clean_tree_block(NULL, root, must_clean);
3923 btrfs_tree_unlock(must_clean);
3924 free_extent_buffer(must_clean);
3928 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
3931 invalidate_mapping_pages(info->btree_inode->i_mapping,
3932 bytenr >> PAGE_CACHE_SHIFT,
3933 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
3936 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
3940 btrfs_free_path(path);
3945 * when we free an extent, it is possible (and likely) that we free the last
3946 * delayed ref for that extent as well. This searches the delayed ref tree for
3947 * a given extent, and if there are no other delayed refs to be processed, it
3948 * removes it from the tree.
3950 static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
3951 struct btrfs_root *root, u64 bytenr)
3953 struct btrfs_delayed_ref_head *head;
3954 struct btrfs_delayed_ref_root *delayed_refs;
3955 struct btrfs_delayed_ref_node *ref;
3956 struct rb_node *node;
3959 delayed_refs = &trans->transaction->delayed_refs;
3960 spin_lock(&delayed_refs->lock);
3961 head = btrfs_find_delayed_ref_head(trans, bytenr);
3965 node = rb_prev(&head->node.rb_node);
3969 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
3971 /* there are still entries for this ref, we can't drop it */
3972 if (ref->bytenr == bytenr)
3975 if (head->extent_op) {
3976 if (!head->must_insert_reserved)
3978 kfree(head->extent_op);
3979 head->extent_op = NULL;
3983 * waiting for the lock here would deadlock. If someone else has it
3984 * locked they are already in the process of dropping it anyway
3986 if (!mutex_trylock(&head->mutex))
3990 * at this point we have a head with no other entries. Go
3991 * ahead and process it.
3993 head->node.in_tree = 0;
3994 rb_erase(&head->node.rb_node, &delayed_refs->root);
3996 delayed_refs->num_entries--;
3999 * we don't take a ref on the node because we're removing it from the
4000 * tree, so we just steal the ref the tree was holding.
4002 delayed_refs->num_heads--;
4003 if (list_empty(&head->cluster))
4004 delayed_refs->num_heads_ready--;
4006 list_del_init(&head->cluster);
4007 spin_unlock(&delayed_refs->lock);
4009 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
4010 &head->node, head->extent_op,
4011 head->must_insert_reserved);
4013 btrfs_put_delayed_ref(&head->node);
4016 spin_unlock(&delayed_refs->lock);
4020 int btrfs_free_extent(struct btrfs_trans_handle *trans,
4021 struct btrfs_root *root,
4022 u64 bytenr, u64 num_bytes, u64 parent,
4023 u64 root_objectid, u64 owner, u64 offset)
4028 * tree log blocks never actually go into the extent allocation
4029 * tree, just update pinning info and exit early.
4031 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4032 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
4033 /* unlocks the pinned mutex */
4034 btrfs_pin_extent(root, bytenr, num_bytes, 1);
4036 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4037 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4038 parent, root_objectid, (int)owner,
4039 BTRFS_DROP_DELAYED_REF, NULL);
4041 ret = check_ref_cleanup(trans, root, bytenr);
4044 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4045 parent, root_objectid, owner,
4046 offset, BTRFS_DROP_DELAYED_REF, NULL);
4052 static u64 stripe_align(struct btrfs_root *root, u64 val)
4054 u64 mask = ((u64)root->stripesize - 1);
4055 u64 ret = (val + mask) & ~mask;
4060 * when we wait for progress in the block group caching, its because
4061 * our allocation attempt failed at least once. So, we must sleep
4062 * and let some progress happen before we try again.
4064 * This function will sleep at least once waiting for new free space to
4065 * show up, and then it will check the block group free space numbers
4066 * for our min num_bytes. Another option is to have it go ahead
4067 * and look in the rbtree for a free extent of a given size, but this
4071 wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4074 struct btrfs_caching_control *caching_ctl;
4077 caching_ctl = get_caching_control(cache);
4081 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
4082 (cache->free_space >= num_bytes));
4084 put_caching_control(caching_ctl);
4089 wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4091 struct btrfs_caching_control *caching_ctl;
4094 caching_ctl = get_caching_control(cache);
4098 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4100 put_caching_control(caching_ctl);
4104 enum btrfs_loop_type {
4105 LOOP_FIND_IDEAL = 0,
4106 LOOP_CACHING_NOWAIT = 1,
4107 LOOP_CACHING_WAIT = 2,
4108 LOOP_ALLOC_CHUNK = 3,
4109 LOOP_NO_EMPTY_SIZE = 4,
4113 * walks the btree of allocated extents and find a hole of a given size.
4114 * The key ins is changed to record the hole:
4115 * ins->objectid == block start
4116 * ins->flags = BTRFS_EXTENT_ITEM_KEY
4117 * ins->offset == number of blocks
4118 * Any available blocks before search_start are skipped.
4120 static noinline int find_free_extent(struct btrfs_trans_handle *trans,
4121 struct btrfs_root *orig_root,
4122 u64 num_bytes, u64 empty_size,
4123 u64 search_start, u64 search_end,
4124 u64 hint_byte, struct btrfs_key *ins,
4125 u64 exclude_start, u64 exclude_nr,
4129 struct btrfs_root *root = orig_root->fs_info->extent_root;
4130 struct btrfs_free_cluster *last_ptr = NULL;
4131 struct btrfs_block_group_cache *block_group = NULL;
4132 int empty_cluster = 2 * 1024 * 1024;
4133 int allowed_chunk_alloc = 0;
4134 int done_chunk_alloc = 0;
4135 struct btrfs_space_info *space_info;
4136 int last_ptr_loop = 0;
4138 bool found_uncached_bg = false;
4139 bool failed_cluster_refill = false;
4140 bool failed_alloc = false;
4141 u64 ideal_cache_percent = 0;
4142 u64 ideal_cache_offset = 0;
4144 WARN_ON(num_bytes < root->sectorsize);
4145 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
4149 space_info = __find_space_info(root->fs_info, data);
4151 if (orig_root->ref_cows || empty_size)
4152 allowed_chunk_alloc = 1;
4154 if (data & BTRFS_BLOCK_GROUP_METADATA) {
4155 last_ptr = &root->fs_info->meta_alloc_cluster;
4156 if (!btrfs_test_opt(root, SSD))
4157 empty_cluster = 64 * 1024;
4160 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
4161 last_ptr = &root->fs_info->data_alloc_cluster;
4165 spin_lock(&last_ptr->lock);
4166 if (last_ptr->block_group)
4167 hint_byte = last_ptr->window_start;
4168 spin_unlock(&last_ptr->lock);
4171 search_start = max(search_start, first_logical_byte(root, 0));
4172 search_start = max(search_start, hint_byte);
4177 if (search_start == hint_byte) {
4179 block_group = btrfs_lookup_block_group(root->fs_info,
4182 * we don't want to use the block group if it doesn't match our
4183 * allocation bits, or if its not cached.
4185 * However if we are re-searching with an ideal block group
4186 * picked out then we don't care that the block group is cached.
4188 if (block_group && block_group_bits(block_group, data) &&
4189 (block_group->cached != BTRFS_CACHE_NO ||
4190 search_start == ideal_cache_offset)) {
4191 down_read(&space_info->groups_sem);
4192 if (list_empty(&block_group->list) ||
4195 * someone is removing this block group,
4196 * we can't jump into the have_block_group
4197 * target because our list pointers are not
4200 btrfs_put_block_group(block_group);
4201 up_read(&space_info->groups_sem);
4203 goto have_block_group;
4205 } else if (block_group) {
4206 btrfs_put_block_group(block_group);
4210 down_read(&space_info->groups_sem);
4211 list_for_each_entry(block_group, &space_info->block_groups, list) {
4215 atomic_inc(&block_group->count);
4216 search_start = block_group->key.objectid;
4219 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
4222 free_percent = btrfs_block_group_used(&block_group->item);
4223 free_percent *= 100;
4224 free_percent = div64_u64(free_percent,
4225 block_group->key.offset);
4226 free_percent = 100 - free_percent;
4227 if (free_percent > ideal_cache_percent &&
4228 likely(!block_group->ro)) {
4229 ideal_cache_offset = block_group->key.objectid;
4230 ideal_cache_percent = free_percent;
4234 * We only want to start kthread caching if we are at
4235 * the point where we will wait for caching to make
4236 * progress, or if our ideal search is over and we've
4237 * found somebody to start caching.
4239 if (loop > LOOP_CACHING_NOWAIT ||
4240 (loop > LOOP_FIND_IDEAL &&
4241 atomic_read(&space_info->caching_threads) < 2)) {
4242 ret = cache_block_group(block_group);
4245 found_uncached_bg = true;
4248 * If loop is set for cached only, try the next block
4251 if (loop == LOOP_FIND_IDEAL)
4255 cached = block_group_cache_done(block_group);
4256 if (unlikely(!cached))
4257 found_uncached_bg = true;
4259 if (unlikely(block_group->ro))
4263 * Ok we want to try and use the cluster allocator, so lets look
4264 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
4265 * have tried the cluster allocator plenty of times at this
4266 * point and not have found anything, so we are likely way too
4267 * fragmented for the clustering stuff to find anything, so lets
4268 * just skip it and let the allocator find whatever block it can
4271 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
4273 * the refill lock keeps out other
4274 * people trying to start a new cluster
4276 spin_lock(&last_ptr->refill_lock);
4277 if (last_ptr->block_group &&
4278 (last_ptr->block_group->ro ||
4279 !block_group_bits(last_ptr->block_group, data))) {
4281 goto refill_cluster;
4284 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
4285 num_bytes, search_start);
4287 /* we have a block, we're done */
4288 spin_unlock(&last_ptr->refill_lock);
4292 spin_lock(&last_ptr->lock);
4294 * whoops, this cluster doesn't actually point to
4295 * this block group. Get a ref on the block
4296 * group is does point to and try again
4298 if (!last_ptr_loop && last_ptr->block_group &&
4299 last_ptr->block_group != block_group) {
4301 btrfs_put_block_group(block_group);
4302 block_group = last_ptr->block_group;
4303 atomic_inc(&block_group->count);
4304 spin_unlock(&last_ptr->lock);
4305 spin_unlock(&last_ptr->refill_lock);
4308 search_start = block_group->key.objectid;
4310 * we know this block group is properly
4311 * in the list because
4312 * btrfs_remove_block_group, drops the
4313 * cluster before it removes the block
4314 * group from the list
4316 goto have_block_group;
4318 spin_unlock(&last_ptr->lock);
4321 * this cluster didn't work out, free it and
4324 btrfs_return_cluster_to_free_space(NULL, last_ptr);
4328 /* allocate a cluster in this block group */
4329 ret = btrfs_find_space_cluster(trans, root,
4330 block_group, last_ptr,
4332 empty_cluster + empty_size);
4335 * now pull our allocation out of this
4338 offset = btrfs_alloc_from_cluster(block_group,
4339 last_ptr, num_bytes,
4342 /* we found one, proceed */
4343 spin_unlock(&last_ptr->refill_lock);
4346 } else if (!cached && loop > LOOP_CACHING_NOWAIT
4347 && !failed_cluster_refill) {
4348 spin_unlock(&last_ptr->refill_lock);
4350 failed_cluster_refill = true;
4351 wait_block_group_cache_progress(block_group,
4352 num_bytes + empty_cluster + empty_size);
4353 goto have_block_group;
4357 * at this point we either didn't find a cluster
4358 * or we weren't able to allocate a block from our
4359 * cluster. Free the cluster we've been trying
4360 * to use, and go to the next block group
4362 btrfs_return_cluster_to_free_space(NULL, last_ptr);
4363 spin_unlock(&last_ptr->refill_lock);
4367 offset = btrfs_find_space_for_alloc(block_group, search_start,
4368 num_bytes, empty_size);
4370 * If we didn't find a chunk, and we haven't failed on this
4371 * block group before, and this block group is in the middle of
4372 * caching and we are ok with waiting, then go ahead and wait
4373 * for progress to be made, and set failed_alloc to true.
4375 * If failed_alloc is true then we've already waited on this
4376 * block group once and should move on to the next block group.
4378 if (!offset && !failed_alloc && !cached &&
4379 loop > LOOP_CACHING_NOWAIT) {
4380 wait_block_group_cache_progress(block_group,
4381 num_bytes + empty_size);
4382 failed_alloc = true;
4383 goto have_block_group;
4384 } else if (!offset) {
4388 search_start = stripe_align(root, offset);
4389 /* move on to the next group */
4390 if (search_start + num_bytes >= search_end) {
4391 btrfs_add_free_space(block_group, offset, num_bytes);
4395 /* move on to the next group */
4396 if (search_start + num_bytes >
4397 block_group->key.objectid + block_group->key.offset) {
4398 btrfs_add_free_space(block_group, offset, num_bytes);
4402 if (exclude_nr > 0 &&
4403 (search_start + num_bytes > exclude_start &&
4404 search_start < exclude_start + exclude_nr)) {
4405 search_start = exclude_start + exclude_nr;
4407 btrfs_add_free_space(block_group, offset, num_bytes);
4409 * if search_start is still in this block group
4410 * then we just re-search this block group
4412 if (search_start >= block_group->key.objectid &&
4413 search_start < (block_group->key.objectid +
4414 block_group->key.offset))
4415 goto have_block_group;
4419 ins->objectid = search_start;
4420 ins->offset = num_bytes;
4422 if (offset < search_start)
4423 btrfs_add_free_space(block_group, offset,
4424 search_start - offset);
4425 BUG_ON(offset > search_start);
4427 update_reserved_extents(block_group, num_bytes, 1);
4429 /* we are all good, lets return */
4432 failed_cluster_refill = false;
4433 failed_alloc = false;
4434 btrfs_put_block_group(block_group);
4436 up_read(&space_info->groups_sem);
4438 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
4439 * for them to make caching progress. Also
4440 * determine the best possible bg to cache
4441 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
4442 * caching kthreads as we move along
4443 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
4444 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
4445 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
4448 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
4449 (found_uncached_bg || empty_size || empty_cluster ||
4450 allowed_chunk_alloc)) {
4451 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
4452 found_uncached_bg = false;
4454 if (!ideal_cache_percent &&
4455 atomic_read(&space_info->caching_threads))
4459 * 1 of the following 2 things have happened so far
4461 * 1) We found an ideal block group for caching that
4462 * is mostly full and will cache quickly, so we might
4463 * as well wait for it.
4465 * 2) We searched for cached only and we didn't find
4466 * anything, and we didn't start any caching kthreads
4467 * either, so chances are we will loop through and
4468 * start a couple caching kthreads, and then come back
4469 * around and just wait for them. This will be slower
4470 * because we will have 2 caching kthreads reading at
4471 * the same time when we could have just started one
4472 * and waited for it to get far enough to give us an
4473 * allocation, so go ahead and go to the wait caching
4476 loop = LOOP_CACHING_WAIT;
4477 search_start = ideal_cache_offset;
4478 ideal_cache_percent = 0;
4480 } else if (loop == LOOP_FIND_IDEAL) {
4482 * Didn't find a uncached bg, wait on anything we find
4485 loop = LOOP_CACHING_WAIT;
4489 if (loop < LOOP_CACHING_WAIT) {
4494 if (loop == LOOP_ALLOC_CHUNK) {
4499 if (allowed_chunk_alloc) {
4500 ret = do_chunk_alloc(trans, root, num_bytes +
4501 2 * 1024 * 1024, data, 1);
4502 allowed_chunk_alloc = 0;
4503 done_chunk_alloc = 1;
4504 } else if (!done_chunk_alloc) {
4505 space_info->force_alloc = 1;
4508 if (loop < LOOP_NO_EMPTY_SIZE) {
4513 } else if (!ins->objectid) {
4517 /* we found what we needed */
4518 if (ins->objectid) {
4519 if (!(data & BTRFS_BLOCK_GROUP_DATA))
4520 trans->block_group = block_group->key.objectid;
4522 btrfs_put_block_group(block_group);
4529 static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
4530 int dump_block_groups)
4532 struct btrfs_block_group_cache *cache;
4534 spin_lock(&info->lock);
4535 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
4536 (unsigned long long)(info->total_bytes - info->bytes_used -
4537 info->bytes_pinned - info->bytes_reserved -
4539 (info->full) ? "" : "not ");
4540 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
4541 " may_use=%llu, used=%llu, root=%llu, super=%llu, reserved=%llu"
4543 (unsigned long long)info->total_bytes,
4544 (unsigned long long)info->bytes_pinned,
4545 (unsigned long long)info->bytes_delalloc,
4546 (unsigned long long)info->bytes_may_use,
4547 (unsigned long long)info->bytes_used,
4548 (unsigned long long)info->bytes_root,
4549 (unsigned long long)info->bytes_super,
4550 (unsigned long long)info->bytes_reserved);
4551 spin_unlock(&info->lock);
4553 if (!dump_block_groups)
4556 down_read(&info->groups_sem);
4557 list_for_each_entry(cache, &info->block_groups, list) {
4558 spin_lock(&cache->lock);
4559 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
4560 "%llu pinned %llu reserved\n",
4561 (unsigned long long)cache->key.objectid,
4562 (unsigned long long)cache->key.offset,
4563 (unsigned long long)btrfs_block_group_used(&cache->item),
4564 (unsigned long long)cache->pinned,
4565 (unsigned long long)cache->reserved);
4566 btrfs_dump_free_space(cache, bytes);
4567 spin_unlock(&cache->lock);
4569 up_read(&info->groups_sem);
4572 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
4573 struct btrfs_root *root,
4574 u64 num_bytes, u64 min_alloc_size,
4575 u64 empty_size, u64 hint_byte,
4576 u64 search_end, struct btrfs_key *ins,
4580 u64 search_start = 0;
4581 struct btrfs_fs_info *info = root->fs_info;
4583 data = btrfs_get_alloc_profile(root, data);
4586 * the only place that sets empty_size is btrfs_realloc_node, which
4587 * is not called recursively on allocations
4589 if (empty_size || root->ref_cows) {
4590 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
4591 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
4593 BTRFS_BLOCK_GROUP_METADATA |
4594 (info->metadata_alloc_profile &
4595 info->avail_metadata_alloc_bits), 0);
4597 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
4598 num_bytes + 2 * 1024 * 1024, data, 0);
4601 WARN_ON(num_bytes < root->sectorsize);
4602 ret = find_free_extent(trans, root, num_bytes, empty_size,
4603 search_start, search_end, hint_byte, ins,
4604 trans->alloc_exclude_start,
4605 trans->alloc_exclude_nr, data);
4607 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
4608 num_bytes = num_bytes >> 1;
4609 num_bytes = num_bytes & ~(root->sectorsize - 1);
4610 num_bytes = max(num_bytes, min_alloc_size);
4611 do_chunk_alloc(trans, root->fs_info->extent_root,
4612 num_bytes, data, 1);
4615 if (ret == -ENOSPC) {
4616 struct btrfs_space_info *sinfo;
4618 sinfo = __find_space_info(root->fs_info, data);
4619 printk(KERN_ERR "btrfs allocation failed flags %llu, "
4620 "wanted %llu\n", (unsigned long long)data,
4621 (unsigned long long)num_bytes);
4622 dump_space_info(sinfo, num_bytes, 1);
4628 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
4630 struct btrfs_block_group_cache *cache;
4633 cache = btrfs_lookup_block_group(root->fs_info, start);
4635 printk(KERN_ERR "Unable to find block group for %llu\n",
4636 (unsigned long long)start);
4640 ret = btrfs_discard_extent(root, start, len);
4642 btrfs_add_free_space(cache, start, len);
4643 update_reserved_extents(cache, len, 0);
4644 btrfs_put_block_group(cache);
4649 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4650 struct btrfs_root *root,
4651 u64 parent, u64 root_objectid,
4652 u64 flags, u64 owner, u64 offset,
4653 struct btrfs_key *ins, int ref_mod)
4656 struct btrfs_fs_info *fs_info = root->fs_info;
4657 struct btrfs_extent_item *extent_item;
4658 struct btrfs_extent_inline_ref *iref;
4659 struct btrfs_path *path;
4660 struct extent_buffer *leaf;
4665 type = BTRFS_SHARED_DATA_REF_KEY;
4667 type = BTRFS_EXTENT_DATA_REF_KEY;
4669 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
4671 path = btrfs_alloc_path();
4674 path->leave_spinning = 1;
4675 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4679 leaf = path->nodes[0];
4680 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4681 struct btrfs_extent_item);
4682 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
4683 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4684 btrfs_set_extent_flags(leaf, extent_item,
4685 flags | BTRFS_EXTENT_FLAG_DATA);
4687 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4688 btrfs_set_extent_inline_ref_type(leaf, iref, type);
4690 struct btrfs_shared_data_ref *ref;
4691 ref = (struct btrfs_shared_data_ref *)(iref + 1);
4692 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4693 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
4695 struct btrfs_extent_data_ref *ref;
4696 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
4697 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
4698 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
4699 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
4700 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
4703 btrfs_mark_buffer_dirty(path->nodes[0]);
4704 btrfs_free_path(path);
4706 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4709 printk(KERN_ERR "btrfs update block group failed for %llu "
4710 "%llu\n", (unsigned long long)ins->objectid,
4711 (unsigned long long)ins->offset);
4717 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
4718 struct btrfs_root *root,
4719 u64 parent, u64 root_objectid,
4720 u64 flags, struct btrfs_disk_key *key,
4721 int level, struct btrfs_key *ins)
4724 struct btrfs_fs_info *fs_info = root->fs_info;
4725 struct btrfs_extent_item *extent_item;
4726 struct btrfs_tree_block_info *block_info;
4727 struct btrfs_extent_inline_ref *iref;
4728 struct btrfs_path *path;
4729 struct extent_buffer *leaf;
4730 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
4732 path = btrfs_alloc_path();
4735 path->leave_spinning = 1;
4736 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4740 leaf = path->nodes[0];
4741 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4742 struct btrfs_extent_item);
4743 btrfs_set_extent_refs(leaf, extent_item, 1);
4744 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4745 btrfs_set_extent_flags(leaf, extent_item,
4746 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
4747 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
4749 btrfs_set_tree_block_key(leaf, block_info, key);
4750 btrfs_set_tree_block_level(leaf, block_info, level);
4752 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
4754 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4755 btrfs_set_extent_inline_ref_type(leaf, iref,
4756 BTRFS_SHARED_BLOCK_REF_KEY);
4757 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4759 btrfs_set_extent_inline_ref_type(leaf, iref,
4760 BTRFS_TREE_BLOCK_REF_KEY);
4761 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
4764 btrfs_mark_buffer_dirty(leaf);
4765 btrfs_free_path(path);
4767 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4770 printk(KERN_ERR "btrfs update block group failed for %llu "
4771 "%llu\n", (unsigned long long)ins->objectid,
4772 (unsigned long long)ins->offset);
4778 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4779 struct btrfs_root *root,
4780 u64 root_objectid, u64 owner,
4781 u64 offset, struct btrfs_key *ins)
4785 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
4787 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
4788 0, root_objectid, owner, offset,
4789 BTRFS_ADD_DELAYED_EXTENT, NULL);
4794 * this is used by the tree logging recovery code. It records that
4795 * an extent has been allocated and makes sure to clear the free
4796 * space cache bits as well
4798 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
4799 struct btrfs_root *root,
4800 u64 root_objectid, u64 owner, u64 offset,
4801 struct btrfs_key *ins)
4804 struct btrfs_block_group_cache *block_group;
4805 struct btrfs_caching_control *caching_ctl;
4806 u64 start = ins->objectid;
4807 u64 num_bytes = ins->offset;
4809 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
4810 cache_block_group(block_group);
4811 caching_ctl = get_caching_control(block_group);
4814 BUG_ON(!block_group_cache_done(block_group));
4815 ret = btrfs_remove_free_space(block_group, start, num_bytes);
4818 mutex_lock(&caching_ctl->mutex);
4820 if (start >= caching_ctl->progress) {
4821 ret = add_excluded_extent(root, start, num_bytes);
4823 } else if (start + num_bytes <= caching_ctl->progress) {
4824 ret = btrfs_remove_free_space(block_group,
4828 num_bytes = caching_ctl->progress - start;
4829 ret = btrfs_remove_free_space(block_group,
4833 start = caching_ctl->progress;
4834 num_bytes = ins->objectid + ins->offset -
4835 caching_ctl->progress;
4836 ret = add_excluded_extent(root, start, num_bytes);
4840 mutex_unlock(&caching_ctl->mutex);
4841 put_caching_control(caching_ctl);
4844 update_reserved_extents(block_group, ins->offset, 1);
4845 btrfs_put_block_group(block_group);
4846 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
4847 0, owner, offset, ins, 1);
4852 * finds a free extent and does all the dirty work required for allocation
4853 * returns the key for the extent through ins, and a tree buffer for
4854 * the first block of the extent through buf.
4856 * returns 0 if everything worked, non-zero otherwise.
4858 static int alloc_tree_block(struct btrfs_trans_handle *trans,
4859 struct btrfs_root *root,
4860 u64 num_bytes, u64 parent, u64 root_objectid,
4861 struct btrfs_disk_key *key, int level,
4862 u64 empty_size, u64 hint_byte, u64 search_end,
4863 struct btrfs_key *ins)
4868 ret = btrfs_reserve_extent(trans, root, num_bytes, num_bytes,
4869 empty_size, hint_byte, search_end,
4874 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4876 parent = ins->objectid;
4877 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4881 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
4882 struct btrfs_delayed_extent_op *extent_op;
4883 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
4886 memcpy(&extent_op->key, key, sizeof(extent_op->key));
4888 memset(&extent_op->key, 0, sizeof(extent_op->key));
4889 extent_op->flags_to_set = flags;
4890 extent_op->update_key = 1;
4891 extent_op->update_flags = 1;
4892 extent_op->is_data = 0;
4894 ret = btrfs_add_delayed_tree_ref(trans, ins->objectid,
4895 ins->offset, parent, root_objectid,
4896 level, BTRFS_ADD_DELAYED_EXTENT,
4903 struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
4904 struct btrfs_root *root,
4905 u64 bytenr, u32 blocksize,
4908 struct extent_buffer *buf;
4910 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
4912 return ERR_PTR(-ENOMEM);
4913 btrfs_set_header_generation(buf, trans->transid);
4914 btrfs_set_buffer_lockdep_class(buf, level);
4915 btrfs_tree_lock(buf);
4916 clean_tree_block(trans, root, buf);
4918 btrfs_set_lock_blocking(buf);
4919 btrfs_set_buffer_uptodate(buf);
4921 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
4923 * we allow two log transactions at a time, use different
4924 * EXENT bit to differentiate dirty pages.
4926 if (root->log_transid % 2 == 0)
4927 set_extent_dirty(&root->dirty_log_pages, buf->start,
4928 buf->start + buf->len - 1, GFP_NOFS);
4930 set_extent_new(&root->dirty_log_pages, buf->start,
4931 buf->start + buf->len - 1, GFP_NOFS);
4933 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
4934 buf->start + buf->len - 1, GFP_NOFS);
4936 trans->blocks_used++;
4937 /* this returns a buffer locked for blocking */
4942 * helper function to allocate a block for a given tree
4943 * returns the tree buffer or NULL.
4945 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
4946 struct btrfs_root *root, u32 blocksize,
4947 u64 parent, u64 root_objectid,
4948 struct btrfs_disk_key *key, int level,
4949 u64 hint, u64 empty_size)
4951 struct btrfs_key ins;
4953 struct extent_buffer *buf;
4955 ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid,
4956 key, level, empty_size, hint, (u64)-1, &ins);
4959 return ERR_PTR(ret);
4962 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
4967 struct walk_control {
4968 u64 refs[BTRFS_MAX_LEVEL];
4969 u64 flags[BTRFS_MAX_LEVEL];
4970 struct btrfs_key update_progress;
4980 #define DROP_REFERENCE 1
4981 #define UPDATE_BACKREF 2
4983 static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
4984 struct btrfs_root *root,
4985 struct walk_control *wc,
4986 struct btrfs_path *path)
4995 struct btrfs_key key;
4996 struct extent_buffer *eb;
5001 if (path->slots[wc->level] < wc->reada_slot) {
5002 wc->reada_count = wc->reada_count * 2 / 3;
5003 wc->reada_count = max(wc->reada_count, 2);
5005 wc->reada_count = wc->reada_count * 3 / 2;
5006 wc->reada_count = min_t(int, wc->reada_count,
5007 BTRFS_NODEPTRS_PER_BLOCK(root));
5010 eb = path->nodes[wc->level];
5011 nritems = btrfs_header_nritems(eb);
5012 blocksize = btrfs_level_size(root, wc->level - 1);
5014 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5015 if (nread >= wc->reada_count)
5019 bytenr = btrfs_node_blockptr(eb, slot);
5020 generation = btrfs_node_ptr_generation(eb, slot);
5022 if (slot == path->slots[wc->level])
5025 if (wc->stage == UPDATE_BACKREF &&
5026 generation <= root->root_key.offset)
5029 /* We don't lock the tree block, it's OK to be racy here */
5030 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5035 if (wc->stage == DROP_REFERENCE) {
5039 if (wc->level == 1 &&
5040 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5042 if (!wc->update_ref ||
5043 generation <= root->root_key.offset)
5045 btrfs_node_key_to_cpu(eb, &key, slot);
5046 ret = btrfs_comp_cpu_keys(&key,
5047 &wc->update_progress);
5051 if (wc->level == 1 &&
5052 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5056 ret = readahead_tree_block(root, bytenr, blocksize,
5060 last = bytenr + blocksize;
5063 wc->reada_slot = slot;
5067 * hepler to process tree block while walking down the tree.
5069 * when wc->stage == UPDATE_BACKREF, this function updates
5070 * back refs for pointers in the block.
5072 * NOTE: return value 1 means we should stop walking down.
5074 static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5075 struct btrfs_root *root,
5076 struct btrfs_path *path,
5077 struct walk_control *wc, int lookup_info)
5079 int level = wc->level;
5080 struct extent_buffer *eb = path->nodes[level];
5081 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5084 if (wc->stage == UPDATE_BACKREF &&
5085 btrfs_header_owner(eb) != root->root_key.objectid)
5089 * when reference count of tree block is 1, it won't increase
5090 * again. once full backref flag is set, we never clear it.
5093 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
5094 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
5095 BUG_ON(!path->locks[level]);
5096 ret = btrfs_lookup_extent_info(trans, root,
5101 BUG_ON(wc->refs[level] == 0);
5104 if (wc->stage == DROP_REFERENCE) {
5105 if (wc->refs[level] > 1)
5108 if (path->locks[level] && !wc->keep_locks) {
5109 btrfs_tree_unlock(eb);
5110 path->locks[level] = 0;
5115 /* wc->stage == UPDATE_BACKREF */
5116 if (!(wc->flags[level] & flag)) {
5117 BUG_ON(!path->locks[level]);
5118 ret = btrfs_inc_ref(trans, root, eb, 1);
5120 ret = btrfs_dec_ref(trans, root, eb, 0);
5122 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
5125 wc->flags[level] |= flag;
5129 * the block is shared by multiple trees, so it's not good to
5130 * keep the tree lock
5132 if (path->locks[level] && level > 0) {
5133 btrfs_tree_unlock(eb);
5134 path->locks[level] = 0;
5140 * hepler to process tree block pointer.
5142 * when wc->stage == DROP_REFERENCE, this function checks
5143 * reference count of the block pointed to. if the block
5144 * is shared and we need update back refs for the subtree
5145 * rooted at the block, this function changes wc->stage to
5146 * UPDATE_BACKREF. if the block is shared and there is no
5147 * need to update back, this function drops the reference
5150 * NOTE: return value 1 means we should stop walking down.
5152 static noinline int do_walk_down(struct btrfs_trans_handle *trans,
5153 struct btrfs_root *root,
5154 struct btrfs_path *path,
5155 struct walk_control *wc, int *lookup_info)
5161 struct btrfs_key key;
5162 struct extent_buffer *next;
5163 int level = wc->level;
5167 generation = btrfs_node_ptr_generation(path->nodes[level],
5168 path->slots[level]);
5170 * if the lower level block was created before the snapshot
5171 * was created, we know there is no need to update back refs
5174 if (wc->stage == UPDATE_BACKREF &&
5175 generation <= root->root_key.offset) {
5180 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
5181 blocksize = btrfs_level_size(root, level - 1);
5183 next = btrfs_find_tree_block(root, bytenr, blocksize);
5185 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
5188 btrfs_tree_lock(next);
5189 btrfs_set_lock_blocking(next);
5191 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5192 &wc->refs[level - 1],
5193 &wc->flags[level - 1]);
5195 BUG_ON(wc->refs[level - 1] == 0);
5198 if (wc->stage == DROP_REFERENCE) {
5199 if (wc->refs[level - 1] > 1) {
5201 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5204 if (!wc->update_ref ||
5205 generation <= root->root_key.offset)
5208 btrfs_node_key_to_cpu(path->nodes[level], &key,
5209 path->slots[level]);
5210 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
5214 wc->stage = UPDATE_BACKREF;
5215 wc->shared_level = level - 1;
5219 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5223 if (!btrfs_buffer_uptodate(next, generation)) {
5224 btrfs_tree_unlock(next);
5225 free_extent_buffer(next);
5231 if (reada && level == 1)
5232 reada_walk_down(trans, root, wc, path);
5233 next = read_tree_block(root, bytenr, blocksize, generation);
5234 btrfs_tree_lock(next);
5235 btrfs_set_lock_blocking(next);
5239 BUG_ON(level != btrfs_header_level(next));
5240 path->nodes[level] = next;
5241 path->slots[level] = 0;
5242 path->locks[level] = 1;
5248 wc->refs[level - 1] = 0;
5249 wc->flags[level - 1] = 0;
5250 if (wc->stage == DROP_REFERENCE) {
5251 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
5252 parent = path->nodes[level]->start;
5254 BUG_ON(root->root_key.objectid !=
5255 btrfs_header_owner(path->nodes[level]));
5259 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
5260 root->root_key.objectid, level - 1, 0);
5263 btrfs_tree_unlock(next);
5264 free_extent_buffer(next);
5270 * hepler to process tree block while walking up the tree.
5272 * when wc->stage == DROP_REFERENCE, this function drops
5273 * reference count on the block.
5275 * when wc->stage == UPDATE_BACKREF, this function changes
5276 * wc->stage back to DROP_REFERENCE if we changed wc->stage
5277 * to UPDATE_BACKREF previously while processing the block.
5279 * NOTE: return value 1 means we should stop walking up.
5281 static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
5282 struct btrfs_root *root,
5283 struct btrfs_path *path,
5284 struct walk_control *wc)
5287 int level = wc->level;
5288 struct extent_buffer *eb = path->nodes[level];
5291 if (wc->stage == UPDATE_BACKREF) {
5292 BUG_ON(wc->shared_level < level);
5293 if (level < wc->shared_level)
5296 ret = find_next_key(path, level + 1, &wc->update_progress);
5300 wc->stage = DROP_REFERENCE;
5301 wc->shared_level = -1;
5302 path->slots[level] = 0;
5305 * check reference count again if the block isn't locked.
5306 * we should start walking down the tree again if reference
5309 if (!path->locks[level]) {
5311 btrfs_tree_lock(eb);
5312 btrfs_set_lock_blocking(eb);
5313 path->locks[level] = 1;
5315 ret = btrfs_lookup_extent_info(trans, root,
5320 BUG_ON(wc->refs[level] == 0);
5321 if (wc->refs[level] == 1) {
5322 btrfs_tree_unlock(eb);
5323 path->locks[level] = 0;
5329 /* wc->stage == DROP_REFERENCE */
5330 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
5332 if (wc->refs[level] == 1) {
5334 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5335 ret = btrfs_dec_ref(trans, root, eb, 1);
5337 ret = btrfs_dec_ref(trans, root, eb, 0);
5340 /* make block locked assertion in clean_tree_block happy */
5341 if (!path->locks[level] &&
5342 btrfs_header_generation(eb) == trans->transid) {
5343 btrfs_tree_lock(eb);
5344 btrfs_set_lock_blocking(eb);
5345 path->locks[level] = 1;
5347 clean_tree_block(trans, root, eb);
5350 if (eb == root->node) {
5351 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5354 BUG_ON(root->root_key.objectid !=
5355 btrfs_header_owner(eb));
5357 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5358 parent = path->nodes[level + 1]->start;
5360 BUG_ON(root->root_key.objectid !=
5361 btrfs_header_owner(path->nodes[level + 1]));
5364 ret = btrfs_free_extent(trans, root, eb->start, eb->len, parent,
5365 root->root_key.objectid, level, 0);
5368 wc->refs[level] = 0;
5369 wc->flags[level] = 0;
5373 static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
5374 struct btrfs_root *root,
5375 struct btrfs_path *path,
5376 struct walk_control *wc)
5378 int level = wc->level;
5379 int lookup_info = 1;
5382 while (level >= 0) {
5383 if (path->slots[level] >=
5384 btrfs_header_nritems(path->nodes[level]))
5387 ret = walk_down_proc(trans, root, path, wc, lookup_info);
5394 ret = do_walk_down(trans, root, path, wc, &lookup_info);
5396 path->slots[level]++;
5404 static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
5405 struct btrfs_root *root,
5406 struct btrfs_path *path,
5407 struct walk_control *wc, int max_level)
5409 int level = wc->level;
5412 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
5413 while (level < max_level && path->nodes[level]) {
5415 if (path->slots[level] + 1 <
5416 btrfs_header_nritems(path->nodes[level])) {
5417 path->slots[level]++;
5420 ret = walk_up_proc(trans, root, path, wc);
5424 if (path->locks[level]) {
5425 btrfs_tree_unlock(path->nodes[level]);
5426 path->locks[level] = 0;
5428 free_extent_buffer(path->nodes[level]);
5429 path->nodes[level] = NULL;
5437 * drop a subvolume tree.
5439 * this function traverses the tree freeing any blocks that only
5440 * referenced by the tree.
5442 * when a shared tree block is found. this function decreases its
5443 * reference count by one. if update_ref is true, this function
5444 * also make sure backrefs for the shared block and all lower level
5445 * blocks are properly updated.
5447 int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref)
5449 struct btrfs_path *path;
5450 struct btrfs_trans_handle *trans;
5451 struct btrfs_root *tree_root = root->fs_info->tree_root;
5452 struct btrfs_root_item *root_item = &root->root_item;
5453 struct walk_control *wc;
5454 struct btrfs_key key;
5459 path = btrfs_alloc_path();
5462 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5465 trans = btrfs_start_transaction(tree_root, 1);
5467 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
5468 level = btrfs_header_level(root->node);
5469 path->nodes[level] = btrfs_lock_root_node(root);
5470 btrfs_set_lock_blocking(path->nodes[level]);
5471 path->slots[level] = 0;
5472 path->locks[level] = 1;
5473 memset(&wc->update_progress, 0,
5474 sizeof(wc->update_progress));
5476 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
5477 memcpy(&wc->update_progress, &key,
5478 sizeof(wc->update_progress));
5480 level = root_item->drop_level;
5482 path->lowest_level = level;
5483 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5484 path->lowest_level = 0;
5492 * unlock our path, this is safe because only this
5493 * function is allowed to delete this snapshot
5495 btrfs_unlock_up_safe(path, 0);
5497 level = btrfs_header_level(root->node);
5499 btrfs_tree_lock(path->nodes[level]);
5500 btrfs_set_lock_blocking(path->nodes[level]);
5502 ret = btrfs_lookup_extent_info(trans, root,
5503 path->nodes[level]->start,
5504 path->nodes[level]->len,
5508 BUG_ON(wc->refs[level] == 0);
5510 if (level == root_item->drop_level)
5513 btrfs_tree_unlock(path->nodes[level]);
5514 WARN_ON(wc->refs[level] != 1);
5520 wc->shared_level = -1;
5521 wc->stage = DROP_REFERENCE;
5522 wc->update_ref = update_ref;
5524 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
5527 ret = walk_down_tree(trans, root, path, wc);
5533 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5540 BUG_ON(wc->stage != DROP_REFERENCE);
5544 if (wc->stage == DROP_REFERENCE) {
5546 btrfs_node_key(path->nodes[level],
5547 &root_item->drop_progress,
5548 path->slots[level]);
5549 root_item->drop_level = level;
5552 BUG_ON(wc->level == 0);
5553 if (trans->transaction->in_commit ||
5554 trans->transaction->delayed_refs.flushing) {
5555 ret = btrfs_update_root(trans, tree_root,
5560 btrfs_end_transaction(trans, tree_root);
5561 trans = btrfs_start_transaction(tree_root, 1);
5563 unsigned long update;
5564 update = trans->delayed_ref_updates;
5565 trans->delayed_ref_updates = 0;
5567 btrfs_run_delayed_refs(trans, tree_root,
5571 btrfs_release_path(root, path);
5574 ret = btrfs_del_root(trans, tree_root, &root->root_key);
5577 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
5578 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
5582 ret = btrfs_del_orphan_item(trans, tree_root,
5583 root->root_key.objectid);
5588 if (root->in_radix) {
5589 btrfs_free_fs_root(tree_root->fs_info, root);
5591 free_extent_buffer(root->node);
5592 free_extent_buffer(root->commit_root);
5596 btrfs_end_transaction(trans, tree_root);
5598 btrfs_free_path(path);
5603 * drop subtree rooted at tree block 'node'.
5605 * NOTE: this function will unlock and release tree block 'node'
5607 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
5608 struct btrfs_root *root,
5609 struct extent_buffer *node,
5610 struct extent_buffer *parent)
5612 struct btrfs_path *path;
5613 struct walk_control *wc;
5619 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5621 path = btrfs_alloc_path();
5624 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5627 btrfs_assert_tree_locked(parent);
5628 parent_level = btrfs_header_level(parent);
5629 extent_buffer_get(parent);
5630 path->nodes[parent_level] = parent;
5631 path->slots[parent_level] = btrfs_header_nritems(parent);
5633 btrfs_assert_tree_locked(node);
5634 level = btrfs_header_level(node);
5635 path->nodes[level] = node;
5636 path->slots[level] = 0;
5637 path->locks[level] = 1;
5639 wc->refs[parent_level] = 1;
5640 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5642 wc->shared_level = -1;
5643 wc->stage = DROP_REFERENCE;
5646 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
5649 wret = walk_down_tree(trans, root, path, wc);
5655 wret = walk_up_tree(trans, root, path, wc, parent_level);
5663 btrfs_free_path(path);
5668 static unsigned long calc_ra(unsigned long start, unsigned long last,
5671 return min(last, start + nr - 1);
5674 static noinline int relocate_inode_pages(struct inode *inode, u64 start,
5679 unsigned long first_index;
5680 unsigned long last_index;
5683 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5684 struct file_ra_state *ra;
5685 struct btrfs_ordered_extent *ordered;
5686 unsigned int total_read = 0;
5687 unsigned int total_dirty = 0;
5690 ra = kzalloc(sizeof(*ra), GFP_NOFS);
5692 mutex_lock(&inode->i_mutex);
5693 first_index = start >> PAGE_CACHE_SHIFT;
5694 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
5696 /* make sure the dirty trick played by the caller work */
5697 ret = invalidate_inode_pages2_range(inode->i_mapping,
5698 first_index, last_index);
5702 file_ra_state_init(ra, inode->i_mapping);
5704 for (i = first_index ; i <= last_index; i++) {
5705 if (total_read % ra->ra_pages == 0) {
5706 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
5707 calc_ra(i, last_index, ra->ra_pages));
5711 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
5713 page = grab_cache_page(inode->i_mapping, i);
5718 if (!PageUptodate(page)) {
5719 btrfs_readpage(NULL, page);
5721 if (!PageUptodate(page)) {
5723 page_cache_release(page);
5728 wait_on_page_writeback(page);
5730 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
5731 page_end = page_start + PAGE_CACHE_SIZE - 1;
5732 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
5734 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5736 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5738 page_cache_release(page);
5739 btrfs_start_ordered_extent(inode, ordered, 1);
5740 btrfs_put_ordered_extent(ordered);
5743 set_page_extent_mapped(page);
5745 if (i == first_index)
5746 set_extent_bits(io_tree, page_start, page_end,
5747 EXTENT_BOUNDARY, GFP_NOFS);
5748 btrfs_set_extent_delalloc(inode, page_start, page_end);
5750 set_page_dirty(page);
5753 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5755 page_cache_release(page);
5760 mutex_unlock(&inode->i_mutex);
5761 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
5765 static noinline int relocate_data_extent(struct inode *reloc_inode,
5766 struct btrfs_key *extent_key,
5769 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5770 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
5771 struct extent_map *em;
5772 u64 start = extent_key->objectid - offset;
5773 u64 end = start + extent_key->offset - 1;
5775 em = alloc_extent_map(GFP_NOFS);
5776 BUG_ON(!em || IS_ERR(em));
5779 em->len = extent_key->offset;
5780 em->block_len = extent_key->offset;
5781 em->block_start = extent_key->objectid;
5782 em->bdev = root->fs_info->fs_devices->latest_bdev;
5783 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5785 /* setup extent map to cheat btrfs_readpage */
5786 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
5789 write_lock(&em_tree->lock);
5790 ret = add_extent_mapping(em_tree, em);
5791 write_unlock(&em_tree->lock);
5792 if (ret != -EEXIST) {
5793 free_extent_map(em);
5796 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
5798 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
5800 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
5803 struct btrfs_ref_path {
5805 u64 nodes[BTRFS_MAX_LEVEL];
5807 u64 root_generation;
5814 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
5815 u64 new_nodes[BTRFS_MAX_LEVEL];
5818 struct disk_extent {
5829 static int is_cowonly_root(u64 root_objectid)
5831 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
5832 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
5833 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
5834 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
5835 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5836 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
5841 static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
5842 struct btrfs_root *extent_root,
5843 struct btrfs_ref_path *ref_path,
5846 struct extent_buffer *leaf;
5847 struct btrfs_path *path;
5848 struct btrfs_extent_ref *ref;
5849 struct btrfs_key key;
5850 struct btrfs_key found_key;
5856 path = btrfs_alloc_path();
5861 ref_path->lowest_level = -1;
5862 ref_path->current_level = -1;
5863 ref_path->shared_level = -1;
5867 level = ref_path->current_level - 1;
5868 while (level >= -1) {
5870 if (level < ref_path->lowest_level)
5874 bytenr = ref_path->nodes[level];
5876 bytenr = ref_path->extent_start;
5877 BUG_ON(bytenr == 0);
5879 parent = ref_path->nodes[level + 1];
5880 ref_path->nodes[level + 1] = 0;
5881 ref_path->current_level = level;
5882 BUG_ON(parent == 0);
5884 key.objectid = bytenr;
5885 key.offset = parent + 1;
5886 key.type = BTRFS_EXTENT_REF_KEY;
5888 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
5893 leaf = path->nodes[0];
5894 nritems = btrfs_header_nritems(leaf);
5895 if (path->slots[0] >= nritems) {
5896 ret = btrfs_next_leaf(extent_root, path);
5901 leaf = path->nodes[0];
5904 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5905 if (found_key.objectid == bytenr &&
5906 found_key.type == BTRFS_EXTENT_REF_KEY) {
5907 if (level < ref_path->shared_level)
5908 ref_path->shared_level = level;
5913 btrfs_release_path(extent_root, path);
5916 /* reached lowest level */
5920 level = ref_path->current_level;
5921 while (level < BTRFS_MAX_LEVEL - 1) {
5925 bytenr = ref_path->nodes[level];
5927 bytenr = ref_path->extent_start;
5929 BUG_ON(bytenr == 0);
5931 key.objectid = bytenr;
5933 key.type = BTRFS_EXTENT_REF_KEY;
5935 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
5939 leaf = path->nodes[0];
5940 nritems = btrfs_header_nritems(leaf);
5941 if (path->slots[0] >= nritems) {
5942 ret = btrfs_next_leaf(extent_root, path);
5946 /* the extent was freed by someone */
5947 if (ref_path->lowest_level == level)
5949 btrfs_release_path(extent_root, path);
5952 leaf = path->nodes[0];
5955 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5956 if (found_key.objectid != bytenr ||
5957 found_key.type != BTRFS_EXTENT_REF_KEY) {
5958 /* the extent was freed by someone */
5959 if (ref_path->lowest_level == level) {
5963 btrfs_release_path(extent_root, path);
5967 ref = btrfs_item_ptr(leaf, path->slots[0],
5968 struct btrfs_extent_ref);
5969 ref_objectid = btrfs_ref_objectid(leaf, ref);
5970 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5972 level = (int)ref_objectid;
5973 BUG_ON(level >= BTRFS_MAX_LEVEL);
5974 ref_path->lowest_level = level;
5975 ref_path->current_level = level;
5976 ref_path->nodes[level] = bytenr;
5978 WARN_ON(ref_objectid != level);
5981 WARN_ON(level != -1);
5985 if (ref_path->lowest_level == level) {
5986 ref_path->owner_objectid = ref_objectid;
5987 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
5991 * the block is tree root or the block isn't in reference
5994 if (found_key.objectid == found_key.offset ||
5995 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
5996 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5997 ref_path->root_generation =
5998 btrfs_ref_generation(leaf, ref);
6000 /* special reference from the tree log */
6001 ref_path->nodes[0] = found_key.offset;
6002 ref_path->current_level = 0;
6009 BUG_ON(ref_path->nodes[level] != 0);
6010 ref_path->nodes[level] = found_key.offset;
6011 ref_path->current_level = level;
6014 * the reference was created in the running transaction,
6015 * no need to continue walking up.
6017 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
6018 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6019 ref_path->root_generation =
6020 btrfs_ref_generation(leaf, ref);
6025 btrfs_release_path(extent_root, path);
6028 /* reached max tree level, but no tree root found. */
6031 btrfs_free_path(path);
6035 static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
6036 struct btrfs_root *extent_root,
6037 struct btrfs_ref_path *ref_path,
6040 memset(ref_path, 0, sizeof(*ref_path));
6041 ref_path->extent_start = extent_start;
6043 return __next_ref_path(trans, extent_root, ref_path, 1);
6046 static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
6047 struct btrfs_root *extent_root,
6048 struct btrfs_ref_path *ref_path)
6050 return __next_ref_path(trans, extent_root, ref_path, 0);
6053 static noinline int get_new_locations(struct inode *reloc_inode,
6054 struct btrfs_key *extent_key,
6055 u64 offset, int no_fragment,
6056 struct disk_extent **extents,
6059 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6060 struct btrfs_path *path;
6061 struct btrfs_file_extent_item *fi;
6062 struct extent_buffer *leaf;
6063 struct disk_extent *exts = *extents;
6064 struct btrfs_key found_key;
6069 int max = *nr_extents;
6072 WARN_ON(!no_fragment && *extents);
6075 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
6080 path = btrfs_alloc_path();
6083 cur_pos = extent_key->objectid - offset;
6084 last_byte = extent_key->objectid + extent_key->offset;
6085 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
6095 leaf = path->nodes[0];
6096 nritems = btrfs_header_nritems(leaf);
6097 if (path->slots[0] >= nritems) {
6098 ret = btrfs_next_leaf(root, path);
6103 leaf = path->nodes[0];
6106 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6107 if (found_key.offset != cur_pos ||
6108 found_key.type != BTRFS_EXTENT_DATA_KEY ||
6109 found_key.objectid != reloc_inode->i_ino)
6112 fi = btrfs_item_ptr(leaf, path->slots[0],
6113 struct btrfs_file_extent_item);
6114 if (btrfs_file_extent_type(leaf, fi) !=
6115 BTRFS_FILE_EXTENT_REG ||
6116 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6120 struct disk_extent *old = exts;
6122 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
6123 memcpy(exts, old, sizeof(*exts) * nr);
6124 if (old != *extents)
6128 exts[nr].disk_bytenr =
6129 btrfs_file_extent_disk_bytenr(leaf, fi);
6130 exts[nr].disk_num_bytes =
6131 btrfs_file_extent_disk_num_bytes(leaf, fi);
6132 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
6133 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6134 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6135 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
6136 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
6137 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
6139 BUG_ON(exts[nr].offset > 0);
6140 BUG_ON(exts[nr].compression || exts[nr].encryption);
6141 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
6143 cur_pos += exts[nr].num_bytes;
6146 if (cur_pos + offset >= last_byte)
6156 BUG_ON(cur_pos + offset > last_byte);
6157 if (cur_pos + offset < last_byte) {
6163 btrfs_free_path(path);
6165 if (exts != *extents)
6174 static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
6175 struct btrfs_root *root,
6176 struct btrfs_path *path,
6177 struct btrfs_key *extent_key,
6178 struct btrfs_key *leaf_key,
6179 struct btrfs_ref_path *ref_path,
6180 struct disk_extent *new_extents,
6183 struct extent_buffer *leaf;
6184 struct btrfs_file_extent_item *fi;
6185 struct inode *inode = NULL;
6186 struct btrfs_key key;
6191 u64 search_end = (u64)-1;
6194 int extent_locked = 0;
6198 memcpy(&key, leaf_key, sizeof(key));
6199 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
6200 if (key.objectid < ref_path->owner_objectid ||
6201 (key.objectid == ref_path->owner_objectid &&
6202 key.type < BTRFS_EXTENT_DATA_KEY)) {
6203 key.objectid = ref_path->owner_objectid;
6204 key.type = BTRFS_EXTENT_DATA_KEY;
6210 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
6214 leaf = path->nodes[0];
6215 nritems = btrfs_header_nritems(leaf);
6217 if (extent_locked && ret > 0) {
6219 * the file extent item was modified by someone
6220 * before the extent got locked.
6222 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6223 lock_end, GFP_NOFS);
6227 if (path->slots[0] >= nritems) {
6228 if (++nr_scaned > 2)
6231 BUG_ON(extent_locked);
6232 ret = btrfs_next_leaf(root, path);
6237 leaf = path->nodes[0];
6238 nritems = btrfs_header_nritems(leaf);
6241 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6243 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
6244 if ((key.objectid > ref_path->owner_objectid) ||
6245 (key.objectid == ref_path->owner_objectid &&
6246 key.type > BTRFS_EXTENT_DATA_KEY) ||
6247 key.offset >= search_end)
6251 if (inode && key.objectid != inode->i_ino) {
6252 BUG_ON(extent_locked);
6253 btrfs_release_path(root, path);
6254 mutex_unlock(&inode->i_mutex);
6260 if (key.type != BTRFS_EXTENT_DATA_KEY) {
6265 fi = btrfs_item_ptr(leaf, path->slots[0],
6266 struct btrfs_file_extent_item);
6267 extent_type = btrfs_file_extent_type(leaf, fi);
6268 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
6269 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
6270 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
6271 extent_key->objectid)) {
6277 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6278 ext_offset = btrfs_file_extent_offset(leaf, fi);
6280 if (search_end == (u64)-1) {
6281 search_end = key.offset - ext_offset +
6282 btrfs_file_extent_ram_bytes(leaf, fi);
6285 if (!extent_locked) {
6286 lock_start = key.offset;
6287 lock_end = lock_start + num_bytes - 1;
6289 if (lock_start > key.offset ||
6290 lock_end + 1 < key.offset + num_bytes) {
6291 unlock_extent(&BTRFS_I(inode)->io_tree,
6292 lock_start, lock_end, GFP_NOFS);
6298 btrfs_release_path(root, path);
6300 inode = btrfs_iget_locked(root->fs_info->sb,
6301 key.objectid, root);
6302 if (inode->i_state & I_NEW) {
6303 BTRFS_I(inode)->root = root;
6304 BTRFS_I(inode)->location.objectid =
6306 BTRFS_I(inode)->location.type =
6307 BTRFS_INODE_ITEM_KEY;
6308 BTRFS_I(inode)->location.offset = 0;
6309 btrfs_read_locked_inode(inode);
6310 unlock_new_inode(inode);
6313 * some code call btrfs_commit_transaction while
6314 * holding the i_mutex, so we can't use mutex_lock
6317 if (is_bad_inode(inode) ||
6318 !mutex_trylock(&inode->i_mutex)) {
6321 key.offset = (u64)-1;
6326 if (!extent_locked) {
6327 struct btrfs_ordered_extent *ordered;
6329 btrfs_release_path(root, path);
6331 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6332 lock_end, GFP_NOFS);
6333 ordered = btrfs_lookup_first_ordered_extent(inode,
6336 ordered->file_offset <= lock_end &&
6337 ordered->file_offset + ordered->len > lock_start) {
6338 unlock_extent(&BTRFS_I(inode)->io_tree,
6339 lock_start, lock_end, GFP_NOFS);
6340 btrfs_start_ordered_extent(inode, ordered, 1);
6341 btrfs_put_ordered_extent(ordered);
6342 key.offset += num_bytes;
6346 btrfs_put_ordered_extent(ordered);
6352 if (nr_extents == 1) {
6353 /* update extent pointer in place */
6354 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6355 new_extents[0].disk_bytenr);
6356 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6357 new_extents[0].disk_num_bytes);
6358 btrfs_mark_buffer_dirty(leaf);
6360 btrfs_drop_extent_cache(inode, key.offset,
6361 key.offset + num_bytes - 1, 0);
6363 ret = btrfs_inc_extent_ref(trans, root,
6364 new_extents[0].disk_bytenr,
6365 new_extents[0].disk_num_bytes,
6367 root->root_key.objectid,
6372 ret = btrfs_free_extent(trans, root,
6373 extent_key->objectid,
6376 btrfs_header_owner(leaf),
6377 btrfs_header_generation(leaf),
6381 btrfs_release_path(root, path);
6382 key.offset += num_bytes;
6390 * drop old extent pointer at first, then insert the
6391 * new pointers one bye one
6393 btrfs_release_path(root, path);
6394 ret = btrfs_drop_extents(trans, root, inode, key.offset,
6395 key.offset + num_bytes,
6396 key.offset, &alloc_hint);
6399 for (i = 0; i < nr_extents; i++) {
6400 if (ext_offset >= new_extents[i].num_bytes) {
6401 ext_offset -= new_extents[i].num_bytes;
6404 extent_len = min(new_extents[i].num_bytes -
6405 ext_offset, num_bytes);
6407 ret = btrfs_insert_empty_item(trans, root,
6412 leaf = path->nodes[0];
6413 fi = btrfs_item_ptr(leaf, path->slots[0],
6414 struct btrfs_file_extent_item);
6415 btrfs_set_file_extent_generation(leaf, fi,
6417 btrfs_set_file_extent_type(leaf, fi,
6418 BTRFS_FILE_EXTENT_REG);
6419 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6420 new_extents[i].disk_bytenr);
6421 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6422 new_extents[i].disk_num_bytes);
6423 btrfs_set_file_extent_ram_bytes(leaf, fi,
6424 new_extents[i].ram_bytes);
6426 btrfs_set_file_extent_compression(leaf, fi,
6427 new_extents[i].compression);
6428 btrfs_set_file_extent_encryption(leaf, fi,
6429 new_extents[i].encryption);
6430 btrfs_set_file_extent_other_encoding(leaf, fi,
6431 new_extents[i].other_encoding);
6433 btrfs_set_file_extent_num_bytes(leaf, fi,
6435 ext_offset += new_extents[i].offset;
6436 btrfs_set_file_extent_offset(leaf, fi,
6438 btrfs_mark_buffer_dirty(leaf);
6440 btrfs_drop_extent_cache(inode, key.offset,
6441 key.offset + extent_len - 1, 0);
6443 ret = btrfs_inc_extent_ref(trans, root,
6444 new_extents[i].disk_bytenr,
6445 new_extents[i].disk_num_bytes,
6447 root->root_key.objectid,
6448 trans->transid, key.objectid);
6450 btrfs_release_path(root, path);
6452 inode_add_bytes(inode, extent_len);
6455 num_bytes -= extent_len;
6456 key.offset += extent_len;
6461 BUG_ON(i >= nr_extents);
6465 if (extent_locked) {
6466 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6467 lock_end, GFP_NOFS);
6471 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
6472 key.offset >= search_end)
6479 btrfs_release_path(root, path);
6481 mutex_unlock(&inode->i_mutex);
6482 if (extent_locked) {
6483 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6484 lock_end, GFP_NOFS);
6491 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
6492 struct btrfs_root *root,
6493 struct extent_buffer *buf, u64 orig_start)
6498 BUG_ON(btrfs_header_generation(buf) != trans->transid);
6499 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6501 level = btrfs_header_level(buf);
6503 struct btrfs_leaf_ref *ref;
6504 struct btrfs_leaf_ref *orig_ref;
6506 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
6510 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
6512 btrfs_free_leaf_ref(root, orig_ref);
6516 ref->nritems = orig_ref->nritems;
6517 memcpy(ref->extents, orig_ref->extents,
6518 sizeof(ref->extents[0]) * ref->nritems);
6520 btrfs_free_leaf_ref(root, orig_ref);
6522 ref->root_gen = trans->transid;
6523 ref->bytenr = buf->start;
6524 ref->owner = btrfs_header_owner(buf);
6525 ref->generation = btrfs_header_generation(buf);
6527 ret = btrfs_add_leaf_ref(root, ref, 0);
6529 btrfs_free_leaf_ref(root, ref);
6534 static noinline int invalidate_extent_cache(struct btrfs_root *root,
6535 struct extent_buffer *leaf,
6536 struct btrfs_block_group_cache *group,
6537 struct btrfs_root *target_root)
6539 struct btrfs_key key;
6540 struct inode *inode = NULL;
6541 struct btrfs_file_extent_item *fi;
6543 u64 skip_objectid = 0;
6547 nritems = btrfs_header_nritems(leaf);
6548 for (i = 0; i < nritems; i++) {
6549 btrfs_item_key_to_cpu(leaf, &key, i);
6550 if (key.objectid == skip_objectid ||
6551 key.type != BTRFS_EXTENT_DATA_KEY)
6553 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6554 if (btrfs_file_extent_type(leaf, fi) ==
6555 BTRFS_FILE_EXTENT_INLINE)
6557 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6559 if (!inode || inode->i_ino != key.objectid) {
6561 inode = btrfs_ilookup(target_root->fs_info->sb,
6562 key.objectid, target_root, 1);
6565 skip_objectid = key.objectid;
6568 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6570 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
6571 key.offset + num_bytes - 1, GFP_NOFS);
6572 btrfs_drop_extent_cache(inode, key.offset,
6573 key.offset + num_bytes - 1, 1);
6574 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
6575 key.offset + num_bytes - 1, GFP_NOFS);
6582 static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
6583 struct btrfs_root *root,
6584 struct extent_buffer *leaf,
6585 struct btrfs_block_group_cache *group,
6586 struct inode *reloc_inode)
6588 struct btrfs_key key;
6589 struct btrfs_key extent_key;
6590 struct btrfs_file_extent_item *fi;
6591 struct btrfs_leaf_ref *ref;
6592 struct disk_extent *new_extent;
6601 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
6602 BUG_ON(!new_extent);
6604 ref = btrfs_lookup_leaf_ref(root, leaf->start);
6608 nritems = btrfs_header_nritems(leaf);
6609 for (i = 0; i < nritems; i++) {
6610 btrfs_item_key_to_cpu(leaf, &key, i);
6611 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6613 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6614 if (btrfs_file_extent_type(leaf, fi) ==
6615 BTRFS_FILE_EXTENT_INLINE)
6617 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6618 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
6623 if (bytenr >= group->key.objectid + group->key.offset ||
6624 bytenr + num_bytes <= group->key.objectid)
6627 extent_key.objectid = bytenr;
6628 extent_key.offset = num_bytes;
6629 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
6631 ret = get_new_locations(reloc_inode, &extent_key,
6632 group->key.objectid, 1,
6633 &new_extent, &nr_extent);
6638 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
6639 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
6640 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
6641 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
6643 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6644 new_extent->disk_bytenr);
6645 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6646 new_extent->disk_num_bytes);
6647 btrfs_mark_buffer_dirty(leaf);
6649 ret = btrfs_inc_extent_ref(trans, root,
6650 new_extent->disk_bytenr,
6651 new_extent->disk_num_bytes,
6653 root->root_key.objectid,
6654 trans->transid, key.objectid);
6657 ret = btrfs_free_extent(trans, root,
6658 bytenr, num_bytes, leaf->start,
6659 btrfs_header_owner(leaf),
6660 btrfs_header_generation(leaf),
6666 BUG_ON(ext_index + 1 != ref->nritems);
6667 btrfs_free_leaf_ref(root, ref);
6671 int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
6672 struct btrfs_root *root)
6674 struct btrfs_root *reloc_root;
6677 if (root->reloc_root) {
6678 reloc_root = root->reloc_root;
6679 root->reloc_root = NULL;
6680 list_add(&reloc_root->dead_list,
6681 &root->fs_info->dead_reloc_roots);
6683 btrfs_set_root_bytenr(&reloc_root->root_item,
6684 reloc_root->node->start);
6685 btrfs_set_root_level(&root->root_item,
6686 btrfs_header_level(reloc_root->node));
6687 memset(&reloc_root->root_item.drop_progress, 0,
6688 sizeof(struct btrfs_disk_key));
6689 reloc_root->root_item.drop_level = 0;
6691 ret = btrfs_update_root(trans, root->fs_info->tree_root,
6692 &reloc_root->root_key,
6693 &reloc_root->root_item);
6699 int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
6701 struct btrfs_trans_handle *trans;
6702 struct btrfs_root *reloc_root;
6703 struct btrfs_root *prev_root = NULL;
6704 struct list_head dead_roots;
6708 INIT_LIST_HEAD(&dead_roots);
6709 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
6711 while (!list_empty(&dead_roots)) {
6712 reloc_root = list_entry(dead_roots.prev,
6713 struct btrfs_root, dead_list);
6714 list_del_init(&reloc_root->dead_list);
6716 BUG_ON(reloc_root->commit_root != NULL);
6718 trans = btrfs_join_transaction(root, 1);
6721 mutex_lock(&root->fs_info->drop_mutex);
6722 ret = btrfs_drop_snapshot(trans, reloc_root);
6725 mutex_unlock(&root->fs_info->drop_mutex);
6727 nr = trans->blocks_used;
6728 ret = btrfs_end_transaction(trans, root);
6730 btrfs_btree_balance_dirty(root, nr);
6733 free_extent_buffer(reloc_root->node);
6735 ret = btrfs_del_root(trans, root->fs_info->tree_root,
6736 &reloc_root->root_key);
6738 mutex_unlock(&root->fs_info->drop_mutex);
6740 nr = trans->blocks_used;
6741 ret = btrfs_end_transaction(trans, root);
6743 btrfs_btree_balance_dirty(root, nr);
6746 prev_root = reloc_root;
6749 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
6755 int btrfs_add_dead_reloc_root(struct btrfs_root *root)
6757 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
6761 int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
6763 struct btrfs_root *reloc_root;
6764 struct btrfs_trans_handle *trans;
6765 struct btrfs_key location;
6769 mutex_lock(&root->fs_info->tree_reloc_mutex);
6770 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
6772 found = !list_empty(&root->fs_info->dead_reloc_roots);
6773 mutex_unlock(&root->fs_info->tree_reloc_mutex);
6776 trans = btrfs_start_transaction(root, 1);
6778 ret = btrfs_commit_transaction(trans, root);
6782 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6783 location.offset = (u64)-1;
6784 location.type = BTRFS_ROOT_ITEM_KEY;
6786 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
6787 BUG_ON(!reloc_root);
6788 btrfs_orphan_cleanup(reloc_root);
6792 static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
6793 struct btrfs_root *root)
6795 struct btrfs_root *reloc_root;
6796 struct extent_buffer *eb;
6797 struct btrfs_root_item *root_item;
6798 struct btrfs_key root_key;
6801 BUG_ON(!root->ref_cows);
6802 if (root->reloc_root)
6805 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
6808 ret = btrfs_copy_root(trans, root, root->commit_root,
6809 &eb, BTRFS_TREE_RELOC_OBJECTID);
6812 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
6813 root_key.offset = root->root_key.objectid;
6814 root_key.type = BTRFS_ROOT_ITEM_KEY;
6816 memcpy(root_item, &root->root_item, sizeof(root_item));
6817 btrfs_set_root_refs(root_item, 0);
6818 btrfs_set_root_bytenr(root_item, eb->start);
6819 btrfs_set_root_level(root_item, btrfs_header_level(eb));
6820 btrfs_set_root_generation(root_item, trans->transid);
6822 btrfs_tree_unlock(eb);
6823 free_extent_buffer(eb);
6825 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
6826 &root_key, root_item);
6830 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
6832 BUG_ON(!reloc_root);
6833 reloc_root->last_trans = trans->transid;
6834 reloc_root->commit_root = NULL;
6835 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
6837 root->reloc_root = reloc_root;
6842 * Core function of space balance.
6844 * The idea is using reloc trees to relocate tree blocks in reference
6845 * counted roots. There is one reloc tree for each subvol, and all
6846 * reloc trees share same root key objectid. Reloc trees are snapshots
6847 * of the latest committed roots of subvols (root->commit_root).
6849 * To relocate a tree block referenced by a subvol, there are two steps.
6850 * COW the block through subvol's reloc tree, then update block pointer
6851 * in the subvol to point to the new block. Since all reloc trees share
6852 * same root key objectid, doing special handing for tree blocks owned
6853 * by them is easy. Once a tree block has been COWed in one reloc tree,
6854 * we can use the resulting new block directly when the same block is
6855 * required to COW again through other reloc trees. By this way, relocated
6856 * tree blocks are shared between reloc trees, so they are also shared
6859 static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
6860 struct btrfs_root *root,
6861 struct btrfs_path *path,
6862 struct btrfs_key *first_key,
6863 struct btrfs_ref_path *ref_path,
6864 struct btrfs_block_group_cache *group,
6865 struct inode *reloc_inode)
6867 struct btrfs_root *reloc_root;
6868 struct extent_buffer *eb = NULL;
6869 struct btrfs_key *keys;
6873 int lowest_level = 0;
6876 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
6877 lowest_level = ref_path->owner_objectid;
6879 if (!root->ref_cows) {
6880 path->lowest_level = lowest_level;
6881 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
6883 path->lowest_level = 0;
6884 btrfs_release_path(root, path);
6888 mutex_lock(&root->fs_info->tree_reloc_mutex);
6889 ret = init_reloc_tree(trans, root);
6891 reloc_root = root->reloc_root;
6893 shared_level = ref_path->shared_level;
6894 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
6896 keys = ref_path->node_keys;
6897 nodes = ref_path->new_nodes;
6898 memset(&keys[shared_level + 1], 0,
6899 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
6900 memset(&nodes[shared_level + 1], 0,
6901 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
6903 if (nodes[lowest_level] == 0) {
6904 path->lowest_level = lowest_level;
6905 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6908 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
6909 eb = path->nodes[level];
6910 if (!eb || eb == reloc_root->node)
6912 nodes[level] = eb->start;
6914 btrfs_item_key_to_cpu(eb, &keys[level], 0);
6916 btrfs_node_key_to_cpu(eb, &keys[level], 0);
6919 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6920 eb = path->nodes[0];
6921 ret = replace_extents_in_leaf(trans, reloc_root, eb,
6922 group, reloc_inode);
6925 btrfs_release_path(reloc_root, path);
6927 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
6933 * replace tree blocks in the fs tree with tree blocks in
6936 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
6939 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6940 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6943 extent_buffer_get(path->nodes[0]);
6944 eb = path->nodes[0];
6945 btrfs_release_path(reloc_root, path);
6946 ret = invalidate_extent_cache(reloc_root, eb, group, root);
6948 free_extent_buffer(eb);
6951 mutex_unlock(&root->fs_info->tree_reloc_mutex);
6952 path->lowest_level = 0;
6956 static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
6957 struct btrfs_root *root,
6958 struct btrfs_path *path,
6959 struct btrfs_key *first_key,
6960 struct btrfs_ref_path *ref_path)
6964 ret = relocate_one_path(trans, root, path, first_key,
6965 ref_path, NULL, NULL);
6971 static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
6972 struct btrfs_root *extent_root,
6973 struct btrfs_path *path,
6974 struct btrfs_key *extent_key)
6978 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
6981 ret = btrfs_del_item(trans, extent_root, path);
6983 btrfs_release_path(extent_root, path);
6987 static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
6988 struct btrfs_ref_path *ref_path)
6990 struct btrfs_key root_key;
6992 root_key.objectid = ref_path->root_objectid;
6993 root_key.type = BTRFS_ROOT_ITEM_KEY;
6994 if (is_cowonly_root(ref_path->root_objectid))
6995 root_key.offset = 0;
6997 root_key.offset = (u64)-1;
6999 return btrfs_read_fs_root_no_name(fs_info, &root_key);
7002 static noinline int relocate_one_extent(struct btrfs_root *extent_root,
7003 struct btrfs_path *path,
7004 struct btrfs_key *extent_key,
7005 struct btrfs_block_group_cache *group,
7006 struct inode *reloc_inode, int pass)
7008 struct btrfs_trans_handle *trans;
7009 struct btrfs_root *found_root;
7010 struct btrfs_ref_path *ref_path = NULL;
7011 struct disk_extent *new_extents = NULL;
7016 struct btrfs_key first_key;
7020 trans = btrfs_start_transaction(extent_root, 1);
7023 if (extent_key->objectid == 0) {
7024 ret = del_extent_zero(trans, extent_root, path, extent_key);
7028 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
7034 for (loops = 0; ; loops++) {
7036 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
7037 extent_key->objectid);
7039 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
7046 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
7047 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
7050 found_root = read_ref_root(extent_root->fs_info, ref_path);
7051 BUG_ON(!found_root);
7053 * for reference counted tree, only process reference paths
7054 * rooted at the latest committed root.
7056 if (found_root->ref_cows &&
7057 ref_path->root_generation != found_root->root_key.offset)
7060 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7063 * copy data extents to new locations
7065 u64 group_start = group->key.objectid;
7066 ret = relocate_data_extent(reloc_inode,
7075 level = ref_path->owner_objectid;
7078 if (prev_block != ref_path->nodes[level]) {
7079 struct extent_buffer *eb;
7080 u64 block_start = ref_path->nodes[level];
7081 u64 block_size = btrfs_level_size(found_root, level);
7083 eb = read_tree_block(found_root, block_start,
7085 btrfs_tree_lock(eb);
7086 BUG_ON(level != btrfs_header_level(eb));
7089 btrfs_item_key_to_cpu(eb, &first_key, 0);
7091 btrfs_node_key_to_cpu(eb, &first_key, 0);
7093 btrfs_tree_unlock(eb);
7094 free_extent_buffer(eb);
7095 prev_block = block_start;
7098 mutex_lock(&extent_root->fs_info->trans_mutex);
7099 btrfs_record_root_in_trans(found_root);
7100 mutex_unlock(&extent_root->fs_info->trans_mutex);
7101 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7103 * try to update data extent references while
7104 * keeping metadata shared between snapshots.
7107 ret = relocate_one_path(trans, found_root,
7108 path, &first_key, ref_path,
7109 group, reloc_inode);
7115 * use fallback method to process the remaining
7119 u64 group_start = group->key.objectid;
7120 new_extents = kmalloc(sizeof(*new_extents),
7123 ret = get_new_locations(reloc_inode,
7131 ret = replace_one_extent(trans, found_root,
7133 &first_key, ref_path,
7134 new_extents, nr_extents);
7136 ret = relocate_tree_block(trans, found_root, path,
7137 &first_key, ref_path);
7144 btrfs_end_transaction(trans, extent_root);
7151 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7154 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
7155 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7157 num_devices = root->fs_info->fs_devices->rw_devices;
7158 if (num_devices == 1) {
7159 stripped |= BTRFS_BLOCK_GROUP_DUP;
7160 stripped = flags & ~stripped;
7162 /* turn raid0 into single device chunks */
7163 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7166 /* turn mirroring into duplication */
7167 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7168 BTRFS_BLOCK_GROUP_RAID10))
7169 return stripped | BTRFS_BLOCK_GROUP_DUP;
7172 /* they already had raid on here, just return */
7173 if (flags & stripped)
7176 stripped |= BTRFS_BLOCK_GROUP_DUP;
7177 stripped = flags & ~stripped;
7179 /* switch duplicated blocks with raid1 */
7180 if (flags & BTRFS_BLOCK_GROUP_DUP)
7181 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7183 /* turn single device chunks into raid0 */
7184 return stripped | BTRFS_BLOCK_GROUP_RAID0;
7189 static int __alloc_chunk_for_shrink(struct btrfs_root *root,
7190 struct btrfs_block_group_cache *shrink_block_group,
7193 struct btrfs_trans_handle *trans;
7194 u64 new_alloc_flags;
7197 spin_lock(&shrink_block_group->lock);
7198 if (btrfs_block_group_used(&shrink_block_group->item) +
7199 shrink_block_group->reserved > 0) {
7200 spin_unlock(&shrink_block_group->lock);
7202 trans = btrfs_start_transaction(root, 1);
7203 spin_lock(&shrink_block_group->lock);
7205 new_alloc_flags = update_block_group_flags(root,
7206 shrink_block_group->flags);
7207 if (new_alloc_flags != shrink_block_group->flags) {
7209 btrfs_block_group_used(&shrink_block_group->item);
7211 calc = shrink_block_group->key.offset;
7213 spin_unlock(&shrink_block_group->lock);
7215 do_chunk_alloc(trans, root->fs_info->extent_root,
7216 calc + 2 * 1024 * 1024, new_alloc_flags, force);
7218 btrfs_end_transaction(trans, root);
7220 spin_unlock(&shrink_block_group->lock);
7225 int btrfs_prepare_block_group_relocation(struct btrfs_root *root,
7226 struct btrfs_block_group_cache *group)
7229 __alloc_chunk_for_shrink(root, group, 1);
7230 set_block_group_readonly(group);
7235 * checks to see if its even possible to relocate this block group.
7237 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7238 * ok to go ahead and try.
7240 int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
7242 struct btrfs_block_group_cache *block_group;
7243 struct btrfs_space_info *space_info;
7244 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7245 struct btrfs_device *device;
7249 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
7251 /* odd, couldn't find the block group, leave it alone */
7255 /* no bytes used, we're good */
7256 if (!btrfs_block_group_used(&block_group->item))
7259 space_info = block_group->space_info;
7260 spin_lock(&space_info->lock);
7262 full = space_info->full;
7265 * if this is the last block group we have in this space, we can't
7266 * relocate it unless we're able to allocate a new chunk below.
7268 * Otherwise, we need to make sure we have room in the space to handle
7269 * all of the extents from this block group. If we can, we're good
7271 if ((space_info->total_bytes != block_group->key.offset) &&
7272 (space_info->bytes_used + space_info->bytes_reserved +
7273 space_info->bytes_pinned + space_info->bytes_readonly +
7274 btrfs_block_group_used(&block_group->item) <
7275 space_info->total_bytes)) {
7276 spin_unlock(&space_info->lock);
7279 spin_unlock(&space_info->lock);
7282 * ok we don't have enough space, but maybe we have free space on our
7283 * devices to allocate new chunks for relocation, so loop through our
7284 * alloc devices and guess if we have enough space. However, if we
7285 * were marked as full, then we know there aren't enough chunks, and we
7292 mutex_lock(&root->fs_info->chunk_mutex);
7293 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
7294 u64 min_free = btrfs_block_group_used(&block_group->item);
7295 u64 dev_offset, max_avail;
7298 * check to make sure we can actually find a chunk with enough
7299 * space to fit our block group in.
7301 if (device->total_bytes > device->bytes_used + min_free) {
7302 ret = find_free_dev_extent(NULL, device, min_free,
7303 &dev_offset, &max_avail);
7309 mutex_unlock(&root->fs_info->chunk_mutex);
7311 btrfs_put_block_group(block_group);
7315 static int find_first_block_group(struct btrfs_root *root,
7316 struct btrfs_path *path, struct btrfs_key *key)
7319 struct btrfs_key found_key;
7320 struct extent_buffer *leaf;
7323 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7328 slot = path->slots[0];
7329 leaf = path->nodes[0];
7330 if (slot >= btrfs_header_nritems(leaf)) {
7331 ret = btrfs_next_leaf(root, path);
7338 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7340 if (found_key.objectid >= key->objectid &&
7341 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7352 int btrfs_free_block_groups(struct btrfs_fs_info *info)
7354 struct btrfs_block_group_cache *block_group;
7355 struct btrfs_space_info *space_info;
7356 struct btrfs_caching_control *caching_ctl;
7359 down_write(&info->extent_commit_sem);
7360 while (!list_empty(&info->caching_block_groups)) {
7361 caching_ctl = list_entry(info->caching_block_groups.next,
7362 struct btrfs_caching_control, list);
7363 list_del(&caching_ctl->list);
7364 put_caching_control(caching_ctl);
7366 up_write(&info->extent_commit_sem);
7368 spin_lock(&info->block_group_cache_lock);
7369 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7370 block_group = rb_entry(n, struct btrfs_block_group_cache,
7372 rb_erase(&block_group->cache_node,
7373 &info->block_group_cache_tree);
7374 spin_unlock(&info->block_group_cache_lock);
7376 down_write(&block_group->space_info->groups_sem);
7377 list_del(&block_group->list);
7378 up_write(&block_group->space_info->groups_sem);
7380 if (block_group->cached == BTRFS_CACHE_STARTED)
7381 wait_block_group_cache_done(block_group);
7383 btrfs_remove_free_space_cache(block_group);
7385 WARN_ON(atomic_read(&block_group->count) != 1);
7388 spin_lock(&info->block_group_cache_lock);
7390 spin_unlock(&info->block_group_cache_lock);
7392 /* now that all the block groups are freed, go through and
7393 * free all the space_info structs. This is only called during
7394 * the final stages of unmount, and so we know nobody is
7395 * using them. We call synchronize_rcu() once before we start,
7396 * just to be on the safe side.
7400 while(!list_empty(&info->space_info)) {
7401 space_info = list_entry(info->space_info.next,
7402 struct btrfs_space_info,
7405 list_del(&space_info->list);
7411 int btrfs_read_block_groups(struct btrfs_root *root)
7413 struct btrfs_path *path;
7415 struct btrfs_block_group_cache *cache;
7416 struct btrfs_fs_info *info = root->fs_info;
7417 struct btrfs_space_info *space_info;
7418 struct btrfs_key key;
7419 struct btrfs_key found_key;
7420 struct extent_buffer *leaf;
7422 root = info->extent_root;
7425 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
7426 path = btrfs_alloc_path();
7431 ret = find_first_block_group(root, path, &key);
7439 leaf = path->nodes[0];
7440 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7441 cache = kzalloc(sizeof(*cache), GFP_NOFS);
7447 atomic_set(&cache->count, 1);
7448 spin_lock_init(&cache->lock);
7449 spin_lock_init(&cache->tree_lock);
7450 cache->fs_info = info;
7451 INIT_LIST_HEAD(&cache->list);
7452 INIT_LIST_HEAD(&cache->cluster_list);
7455 * we only want to have 32k of ram per block group for keeping
7456 * track of free space, and if we pass 1/2 of that we want to
7457 * start converting things over to using bitmaps
7459 cache->extents_thresh = ((1024 * 32) / 2) /
7460 sizeof(struct btrfs_free_space);
7462 read_extent_buffer(leaf, &cache->item,
7463 btrfs_item_ptr_offset(leaf, path->slots[0]),
7464 sizeof(cache->item));
7465 memcpy(&cache->key, &found_key, sizeof(found_key));
7467 key.objectid = found_key.objectid + found_key.offset;
7468 btrfs_release_path(root, path);
7469 cache->flags = btrfs_block_group_flags(&cache->item);
7470 cache->sectorsize = root->sectorsize;
7473 * check for two cases, either we are full, and therefore
7474 * don't need to bother with the caching work since we won't
7475 * find any space, or we are empty, and we can just add all
7476 * the space in and be done with it. This saves us _alot_ of
7477 * time, particularly in the full case.
7479 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
7480 exclude_super_stripes(root, cache);
7481 cache->last_byte_to_unpin = (u64)-1;
7482 cache->cached = BTRFS_CACHE_FINISHED;
7483 free_excluded_extents(root, cache);
7484 } else if (btrfs_block_group_used(&cache->item) == 0) {
7485 exclude_super_stripes(root, cache);
7486 cache->last_byte_to_unpin = (u64)-1;
7487 cache->cached = BTRFS_CACHE_FINISHED;
7488 add_new_free_space(cache, root->fs_info,
7490 found_key.objectid +
7492 free_excluded_extents(root, cache);
7495 ret = update_space_info(info, cache->flags, found_key.offset,
7496 btrfs_block_group_used(&cache->item),
7499 cache->space_info = space_info;
7500 spin_lock(&cache->space_info->lock);
7501 cache->space_info->bytes_super += cache->bytes_super;
7502 spin_unlock(&cache->space_info->lock);
7504 down_write(&space_info->groups_sem);
7505 list_add_tail(&cache->list, &space_info->block_groups);
7506 up_write(&space_info->groups_sem);
7508 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7511 set_avail_alloc_bits(root->fs_info, cache->flags);
7512 if (btrfs_chunk_readonly(root, cache->key.objectid))
7513 set_block_group_readonly(cache);
7517 btrfs_free_path(path);
7521 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7522 struct btrfs_root *root, u64 bytes_used,
7523 u64 type, u64 chunk_objectid, u64 chunk_offset,
7527 struct btrfs_root *extent_root;
7528 struct btrfs_block_group_cache *cache;
7530 extent_root = root->fs_info->extent_root;
7532 root->fs_info->last_trans_log_full_commit = trans->transid;
7534 cache = kzalloc(sizeof(*cache), GFP_NOFS);
7538 cache->key.objectid = chunk_offset;
7539 cache->key.offset = size;
7540 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
7541 cache->sectorsize = root->sectorsize;
7544 * we only want to have 32k of ram per block group for keeping track
7545 * of free space, and if we pass 1/2 of that we want to start
7546 * converting things over to using bitmaps
7548 cache->extents_thresh = ((1024 * 32) / 2) /
7549 sizeof(struct btrfs_free_space);
7550 atomic_set(&cache->count, 1);
7551 spin_lock_init(&cache->lock);
7552 spin_lock_init(&cache->tree_lock);
7553 INIT_LIST_HEAD(&cache->list);
7554 INIT_LIST_HEAD(&cache->cluster_list);
7556 btrfs_set_block_group_used(&cache->item, bytes_used);
7557 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7558 cache->flags = type;
7559 btrfs_set_block_group_flags(&cache->item, type);
7561 cache->last_byte_to_unpin = (u64)-1;
7562 cache->cached = BTRFS_CACHE_FINISHED;
7563 exclude_super_stripes(root, cache);
7565 add_new_free_space(cache, root->fs_info, chunk_offset,
7566 chunk_offset + size);
7568 free_excluded_extents(root, cache);
7570 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7571 &cache->space_info);
7574 spin_lock(&cache->space_info->lock);
7575 cache->space_info->bytes_super += cache->bytes_super;
7576 spin_unlock(&cache->space_info->lock);
7578 down_write(&cache->space_info->groups_sem);
7579 list_add_tail(&cache->list, &cache->space_info->block_groups);
7580 up_write(&cache->space_info->groups_sem);
7582 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7585 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7586 sizeof(cache->item));
7589 set_avail_alloc_bits(extent_root->fs_info, type);
7594 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7595 struct btrfs_root *root, u64 group_start)
7597 struct btrfs_path *path;
7598 struct btrfs_block_group_cache *block_group;
7599 struct btrfs_free_cluster *cluster;
7600 struct btrfs_key key;
7603 root = root->fs_info->extent_root;
7605 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7606 BUG_ON(!block_group);
7607 BUG_ON(!block_group->ro);
7609 memcpy(&key, &block_group->key, sizeof(key));
7611 /* make sure this block group isn't part of an allocation cluster */
7612 cluster = &root->fs_info->data_alloc_cluster;
7613 spin_lock(&cluster->refill_lock);
7614 btrfs_return_cluster_to_free_space(block_group, cluster);
7615 spin_unlock(&cluster->refill_lock);
7618 * make sure this block group isn't part of a metadata
7619 * allocation cluster
7621 cluster = &root->fs_info->meta_alloc_cluster;
7622 spin_lock(&cluster->refill_lock);
7623 btrfs_return_cluster_to_free_space(block_group, cluster);
7624 spin_unlock(&cluster->refill_lock);
7626 path = btrfs_alloc_path();
7629 spin_lock(&root->fs_info->block_group_cache_lock);
7630 rb_erase(&block_group->cache_node,
7631 &root->fs_info->block_group_cache_tree);
7632 spin_unlock(&root->fs_info->block_group_cache_lock);
7634 down_write(&block_group->space_info->groups_sem);
7636 * we must use list_del_init so people can check to see if they
7637 * are still on the list after taking the semaphore
7639 list_del_init(&block_group->list);
7640 up_write(&block_group->space_info->groups_sem);
7642 if (block_group->cached == BTRFS_CACHE_STARTED)
7643 wait_block_group_cache_done(block_group);
7645 btrfs_remove_free_space_cache(block_group);
7647 spin_lock(&block_group->space_info->lock);
7648 block_group->space_info->total_bytes -= block_group->key.offset;
7649 block_group->space_info->bytes_readonly -= block_group->key.offset;
7650 spin_unlock(&block_group->space_info->lock);
7652 btrfs_clear_space_info_full(root->fs_info);
7654 btrfs_put_block_group(block_group);
7655 btrfs_put_block_group(block_group);
7657 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
7663 ret = btrfs_del_item(trans, root, path);
7665 btrfs_free_path(path);