2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
18 #include <linux/sched.h>
19 #include <linux/pagemap.h>
20 #include <linux/writeback.h>
21 #include <linux/blkdev.h>
22 #include <linux/sort.h>
23 #include <linux/rcupdate.h>
24 #include <linux/kthread.h>
25 #include <linux/slab.h>
30 #include "print-tree.h"
31 #include "transaction.h"
34 #include "free-space-cache.h"
36 static int update_block_group(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 u64 bytenr, u64 num_bytes, int alloc);
39 static int update_reserved_bytes(struct btrfs_block_group_cache *cache,
40 u64 num_bytes, int reserve, int sinfo);
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 find_next_key(struct btrfs_path *path, int level,
64 struct btrfs_key *key);
65 static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
66 int dump_block_groups);
69 block_group_cache_done(struct btrfs_block_group_cache *cache)
72 return cache->cached == BTRFS_CACHE_FINISHED;
75 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
77 return (cache->flags & bits) == bits;
80 void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
82 atomic_inc(&cache->count);
85 void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
87 if (atomic_dec_and_test(&cache->count)) {
88 WARN_ON(cache->pinned > 0);
89 WARN_ON(cache->reserved > 0);
90 WARN_ON(cache->reserved_pinned > 0);
96 * this adds the block group to the fs_info rb tree for the block group
99 static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
100 struct btrfs_block_group_cache *block_group)
103 struct rb_node *parent = NULL;
104 struct btrfs_block_group_cache *cache;
106 spin_lock(&info->block_group_cache_lock);
107 p = &info->block_group_cache_tree.rb_node;
111 cache = rb_entry(parent, struct btrfs_block_group_cache,
113 if (block_group->key.objectid < cache->key.objectid) {
115 } else if (block_group->key.objectid > cache->key.objectid) {
118 spin_unlock(&info->block_group_cache_lock);
123 rb_link_node(&block_group->cache_node, parent, p);
124 rb_insert_color(&block_group->cache_node,
125 &info->block_group_cache_tree);
126 spin_unlock(&info->block_group_cache_lock);
132 * This will return the block group at or after bytenr if contains is 0, else
133 * it will return the block group that contains the bytenr
135 static struct btrfs_block_group_cache *
136 block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
139 struct btrfs_block_group_cache *cache, *ret = NULL;
143 spin_lock(&info->block_group_cache_lock);
144 n = info->block_group_cache_tree.rb_node;
147 cache = rb_entry(n, struct btrfs_block_group_cache,
149 end = cache->key.objectid + cache->key.offset - 1;
150 start = cache->key.objectid;
152 if (bytenr < start) {
153 if (!contains && (!ret || start < ret->key.objectid))
156 } else if (bytenr > start) {
157 if (contains && bytenr <= end) {
168 btrfs_get_block_group(ret);
169 spin_unlock(&info->block_group_cache_lock);
174 static int add_excluded_extent(struct btrfs_root *root,
175 u64 start, u64 num_bytes)
177 u64 end = start + num_bytes - 1;
178 set_extent_bits(&root->fs_info->freed_extents[0],
179 start, end, EXTENT_UPTODATE, GFP_NOFS);
180 set_extent_bits(&root->fs_info->freed_extents[1],
181 start, end, EXTENT_UPTODATE, GFP_NOFS);
185 static void free_excluded_extents(struct btrfs_root *root,
186 struct btrfs_block_group_cache *cache)
190 start = cache->key.objectid;
191 end = start + cache->key.offset - 1;
193 clear_extent_bits(&root->fs_info->freed_extents[0],
194 start, end, EXTENT_UPTODATE, GFP_NOFS);
195 clear_extent_bits(&root->fs_info->freed_extents[1],
196 start, end, EXTENT_UPTODATE, GFP_NOFS);
199 static int exclude_super_stripes(struct btrfs_root *root,
200 struct btrfs_block_group_cache *cache)
207 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
208 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
209 cache->bytes_super += stripe_len;
210 ret = add_excluded_extent(root, cache->key.objectid,
215 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
216 bytenr = btrfs_sb_offset(i);
217 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
218 cache->key.objectid, bytenr,
219 0, &logical, &nr, &stripe_len);
223 cache->bytes_super += stripe_len;
224 ret = add_excluded_extent(root, logical[nr],
234 static struct btrfs_caching_control *
235 get_caching_control(struct btrfs_block_group_cache *cache)
237 struct btrfs_caching_control *ctl;
239 spin_lock(&cache->lock);
240 if (cache->cached != BTRFS_CACHE_STARTED) {
241 spin_unlock(&cache->lock);
245 /* We're loading it the fast way, so we don't have a caching_ctl. */
246 if (!cache->caching_ctl) {
247 spin_unlock(&cache->lock);
251 ctl = cache->caching_ctl;
252 atomic_inc(&ctl->count);
253 spin_unlock(&cache->lock);
257 static void put_caching_control(struct btrfs_caching_control *ctl)
259 if (atomic_dec_and_test(&ctl->count))
264 * this is only called by cache_block_group, since we could have freed extents
265 * we need to check the pinned_extents for any extents that can't be used yet
266 * since their free space will be released as soon as the transaction commits.
268 static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
269 struct btrfs_fs_info *info, u64 start, u64 end)
271 u64 extent_start, extent_end, size, total_added = 0;
274 while (start < end) {
275 ret = find_first_extent_bit(info->pinned_extents, start,
276 &extent_start, &extent_end,
277 EXTENT_DIRTY | EXTENT_UPTODATE);
281 if (extent_start <= start) {
282 start = extent_end + 1;
283 } else if (extent_start > start && extent_start < end) {
284 size = extent_start - start;
286 ret = btrfs_add_free_space(block_group, start,
289 start = extent_end + 1;
298 ret = btrfs_add_free_space(block_group, start, size);
305 static int caching_kthread(void *data)
307 struct btrfs_block_group_cache *block_group = data;
308 struct btrfs_fs_info *fs_info = block_group->fs_info;
309 struct btrfs_caching_control *caching_ctl = block_group->caching_ctl;
310 struct btrfs_root *extent_root = fs_info->extent_root;
311 struct btrfs_path *path;
312 struct extent_buffer *leaf;
313 struct btrfs_key key;
319 path = btrfs_alloc_path();
323 exclude_super_stripes(extent_root, block_group);
324 spin_lock(&block_group->space_info->lock);
325 block_group->space_info->bytes_readonly += block_group->bytes_super;
326 spin_unlock(&block_group->space_info->lock);
328 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
331 * We don't want to deadlock with somebody trying to allocate a new
332 * extent for the extent root while also trying to search the extent
333 * root to add free space. So we skip locking and search the commit
334 * root, since its read-only
336 path->skip_locking = 1;
337 path->search_commit_root = 1;
342 key.type = BTRFS_EXTENT_ITEM_KEY;
344 mutex_lock(&caching_ctl->mutex);
345 /* need to make sure the commit_root doesn't disappear */
346 down_read(&fs_info->extent_commit_sem);
348 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
352 leaf = path->nodes[0];
353 nritems = btrfs_header_nritems(leaf);
357 if (fs_info->closing > 1) {
362 if (path->slots[0] < nritems) {
363 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
365 ret = find_next_key(path, 0, &key);
369 caching_ctl->progress = last;
370 btrfs_release_path(extent_root, path);
371 up_read(&fs_info->extent_commit_sem);
372 mutex_unlock(&caching_ctl->mutex);
373 if (btrfs_transaction_in_commit(fs_info))
380 if (key.objectid < block_group->key.objectid) {
385 if (key.objectid >= block_group->key.objectid +
386 block_group->key.offset)
389 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
390 total_found += add_new_free_space(block_group,
393 last = key.objectid + key.offset;
395 if (total_found > (1024 * 1024 * 2)) {
397 wake_up(&caching_ctl->wait);
404 total_found += add_new_free_space(block_group, fs_info, last,
405 block_group->key.objectid +
406 block_group->key.offset);
407 caching_ctl->progress = (u64)-1;
409 spin_lock(&block_group->lock);
410 block_group->caching_ctl = NULL;
411 block_group->cached = BTRFS_CACHE_FINISHED;
412 spin_unlock(&block_group->lock);
415 btrfs_free_path(path);
416 up_read(&fs_info->extent_commit_sem);
418 free_excluded_extents(extent_root, block_group);
420 mutex_unlock(&caching_ctl->mutex);
421 wake_up(&caching_ctl->wait);
423 put_caching_control(caching_ctl);
424 atomic_dec(&block_group->space_info->caching_threads);
425 btrfs_put_block_group(block_group);
430 static int cache_block_group(struct btrfs_block_group_cache *cache,
431 struct btrfs_trans_handle *trans,
432 struct btrfs_root *root,
435 struct btrfs_fs_info *fs_info = cache->fs_info;
436 struct btrfs_caching_control *caching_ctl;
437 struct task_struct *tsk;
441 if (cache->cached != BTRFS_CACHE_NO)
445 * We can't do the read from on-disk cache during a commit since we need
446 * to have the normal tree locking. Also if we are currently trying to
447 * allocate blocks for the tree root we can't do the fast caching since
448 * we likely hold important locks.
450 if (!trans->transaction->in_commit &&
451 (root && root != root->fs_info->tree_root)) {
452 spin_lock(&cache->lock);
453 if (cache->cached != BTRFS_CACHE_NO) {
454 spin_unlock(&cache->lock);
457 cache->cached = BTRFS_CACHE_STARTED;
458 spin_unlock(&cache->lock);
460 ret = load_free_space_cache(fs_info, cache);
462 spin_lock(&cache->lock);
464 cache->cached = BTRFS_CACHE_FINISHED;
465 cache->last_byte_to_unpin = (u64)-1;
467 cache->cached = BTRFS_CACHE_NO;
469 spin_unlock(&cache->lock);
477 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL);
478 BUG_ON(!caching_ctl);
480 INIT_LIST_HEAD(&caching_ctl->list);
481 mutex_init(&caching_ctl->mutex);
482 init_waitqueue_head(&caching_ctl->wait);
483 caching_ctl->block_group = cache;
484 caching_ctl->progress = cache->key.objectid;
485 /* one for caching kthread, one for caching block group list */
486 atomic_set(&caching_ctl->count, 2);
488 spin_lock(&cache->lock);
489 if (cache->cached != BTRFS_CACHE_NO) {
490 spin_unlock(&cache->lock);
494 cache->caching_ctl = caching_ctl;
495 cache->cached = BTRFS_CACHE_STARTED;
496 spin_unlock(&cache->lock);
498 down_write(&fs_info->extent_commit_sem);
499 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
500 up_write(&fs_info->extent_commit_sem);
502 atomic_inc(&cache->space_info->caching_threads);
503 btrfs_get_block_group(cache);
505 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
506 cache->key.objectid);
509 printk(KERN_ERR "error running thread %d\n", ret);
517 * return the block group that starts at or after bytenr
519 static struct btrfs_block_group_cache *
520 btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
522 struct btrfs_block_group_cache *cache;
524 cache = block_group_cache_tree_search(info, bytenr, 0);
530 * return the block group that contains the given bytenr
532 struct btrfs_block_group_cache *btrfs_lookup_block_group(
533 struct btrfs_fs_info *info,
536 struct btrfs_block_group_cache *cache;
538 cache = block_group_cache_tree_search(info, bytenr, 1);
543 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
546 struct list_head *head = &info->space_info;
547 struct btrfs_space_info *found;
549 flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM |
550 BTRFS_BLOCK_GROUP_METADATA;
553 list_for_each_entry_rcu(found, head, list) {
554 if (found->flags & flags) {
564 * after adding space to the filesystem, we need to clear the full flags
565 * on all the space infos.
567 void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
569 struct list_head *head = &info->space_info;
570 struct btrfs_space_info *found;
573 list_for_each_entry_rcu(found, head, list)
578 static u64 div_factor(u64 num, int factor)
587 static u64 div_factor_fine(u64 num, int factor)
596 u64 btrfs_find_block_group(struct btrfs_root *root,
597 u64 search_start, u64 search_hint, int owner)
599 struct btrfs_block_group_cache *cache;
601 u64 last = max(search_hint, search_start);
608 cache = btrfs_lookup_first_block_group(root->fs_info, last);
612 spin_lock(&cache->lock);
613 last = cache->key.objectid + cache->key.offset;
614 used = btrfs_block_group_used(&cache->item);
616 if ((full_search || !cache->ro) &&
617 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
618 if (used + cache->pinned + cache->reserved <
619 div_factor(cache->key.offset, factor)) {
620 group_start = cache->key.objectid;
621 spin_unlock(&cache->lock);
622 btrfs_put_block_group(cache);
626 spin_unlock(&cache->lock);
627 btrfs_put_block_group(cache);
635 if (!full_search && factor < 10) {
645 /* simple helper to search for an existing extent at a given offset */
646 int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
649 struct btrfs_key key;
650 struct btrfs_path *path;
652 path = btrfs_alloc_path();
654 key.objectid = start;
656 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
657 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
659 btrfs_free_path(path);
664 * helper function to lookup reference count and flags of extent.
666 * the head node for delayed ref is used to store the sum of all the
667 * reference count modifications queued up in the rbtree. the head
668 * node may also store the extent flags to set. This way you can check
669 * to see what the reference count and extent flags would be if all of
670 * the delayed refs are not processed.
672 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
673 struct btrfs_root *root, u64 bytenr,
674 u64 num_bytes, u64 *refs, u64 *flags)
676 struct btrfs_delayed_ref_head *head;
677 struct btrfs_delayed_ref_root *delayed_refs;
678 struct btrfs_path *path;
679 struct btrfs_extent_item *ei;
680 struct extent_buffer *leaf;
681 struct btrfs_key key;
687 path = btrfs_alloc_path();
691 key.objectid = bytenr;
692 key.type = BTRFS_EXTENT_ITEM_KEY;
693 key.offset = num_bytes;
695 path->skip_locking = 1;
696 path->search_commit_root = 1;
699 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
705 leaf = path->nodes[0];
706 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
707 if (item_size >= sizeof(*ei)) {
708 ei = btrfs_item_ptr(leaf, path->slots[0],
709 struct btrfs_extent_item);
710 num_refs = btrfs_extent_refs(leaf, ei);
711 extent_flags = btrfs_extent_flags(leaf, ei);
713 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
714 struct btrfs_extent_item_v0 *ei0;
715 BUG_ON(item_size != sizeof(*ei0));
716 ei0 = btrfs_item_ptr(leaf, path->slots[0],
717 struct btrfs_extent_item_v0);
718 num_refs = btrfs_extent_refs_v0(leaf, ei0);
719 /* FIXME: this isn't correct for data */
720 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
725 BUG_ON(num_refs == 0);
735 delayed_refs = &trans->transaction->delayed_refs;
736 spin_lock(&delayed_refs->lock);
737 head = btrfs_find_delayed_ref_head(trans, bytenr);
739 if (!mutex_trylock(&head->mutex)) {
740 atomic_inc(&head->node.refs);
741 spin_unlock(&delayed_refs->lock);
743 btrfs_release_path(root->fs_info->extent_root, path);
745 mutex_lock(&head->mutex);
746 mutex_unlock(&head->mutex);
747 btrfs_put_delayed_ref(&head->node);
750 if (head->extent_op && head->extent_op->update_flags)
751 extent_flags |= head->extent_op->flags_to_set;
753 BUG_ON(num_refs == 0);
755 num_refs += head->node.ref_mod;
756 mutex_unlock(&head->mutex);
758 spin_unlock(&delayed_refs->lock);
760 WARN_ON(num_refs == 0);
764 *flags = extent_flags;
766 btrfs_free_path(path);
771 * Back reference rules. Back refs have three main goals:
773 * 1) differentiate between all holders of references to an extent so that
774 * when a reference is dropped we can make sure it was a valid reference
775 * before freeing the extent.
777 * 2) Provide enough information to quickly find the holders of an extent
778 * if we notice a given block is corrupted or bad.
780 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
781 * maintenance. This is actually the same as #2, but with a slightly
782 * different use case.
784 * There are two kinds of back refs. The implicit back refs is optimized
785 * for pointers in non-shared tree blocks. For a given pointer in a block,
786 * back refs of this kind provide information about the block's owner tree
787 * and the pointer's key. These information allow us to find the block by
788 * b-tree searching. The full back refs is for pointers in tree blocks not
789 * referenced by their owner trees. The location of tree block is recorded
790 * in the back refs. Actually the full back refs is generic, and can be
791 * used in all cases the implicit back refs is used. The major shortcoming
792 * of the full back refs is its overhead. Every time a tree block gets
793 * COWed, we have to update back refs entry for all pointers in it.
795 * For a newly allocated tree block, we use implicit back refs for
796 * pointers in it. This means most tree related operations only involve
797 * implicit back refs. For a tree block created in old transaction, the
798 * only way to drop a reference to it is COW it. So we can detect the
799 * event that tree block loses its owner tree's reference and do the
800 * back refs conversion.
802 * When a tree block is COW'd through a tree, there are four cases:
804 * The reference count of the block is one and the tree is the block's
805 * owner tree. Nothing to do in this case.
807 * The reference count of the block is one and the tree is not the
808 * block's owner tree. In this case, full back refs is used for pointers
809 * in the block. Remove these full back refs, add implicit back refs for
810 * every pointers in the new block.
812 * The reference count of the block is greater than one and the tree is
813 * the block's owner tree. In this case, implicit back refs is used for
814 * pointers in the block. Add full back refs for every pointers in the
815 * block, increase lower level extents' reference counts. The original
816 * implicit back refs are entailed to the new block.
818 * The reference count of the block is greater than one and the tree is
819 * not the block's owner tree. Add implicit back refs for every pointer in
820 * the new block, increase lower level extents' reference count.
822 * Back Reference Key composing:
824 * The key objectid corresponds to the first byte in the extent,
825 * The key type is used to differentiate between types of back refs.
826 * There are different meanings of the key offset for different types
829 * File extents can be referenced by:
831 * - multiple snapshots, subvolumes, or different generations in one subvol
832 * - different files inside a single subvolume
833 * - different offsets inside a file (bookend extents in file.c)
835 * The extent ref structure for the implicit back refs has fields for:
837 * - Objectid of the subvolume root
838 * - objectid of the file holding the reference
839 * - original offset in the file
840 * - how many bookend extents
842 * The key offset for the implicit back refs is hash of the first
845 * The extent ref structure for the full back refs has field for:
847 * - number of pointers in the tree leaf
849 * The key offset for the implicit back refs is the first byte of
852 * When a file extent is allocated, The implicit back refs is used.
853 * the fields are filled in:
855 * (root_key.objectid, inode objectid, offset in file, 1)
857 * When a file extent is removed file truncation, we find the
858 * corresponding implicit back refs and check the following fields:
860 * (btrfs_header_owner(leaf), inode objectid, offset in file)
862 * Btree extents can be referenced by:
864 * - Different subvolumes
866 * Both the implicit back refs and the full back refs for tree blocks
867 * only consist of key. The key offset for the implicit back refs is
868 * objectid of block's owner tree. The key offset for the full back refs
869 * is the first byte of parent block.
871 * When implicit back refs is used, information about the lowest key and
872 * level of the tree block are required. These information are stored in
873 * tree block info structure.
876 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
877 static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
878 struct btrfs_root *root,
879 struct btrfs_path *path,
880 u64 owner, u32 extra_size)
882 struct btrfs_extent_item *item;
883 struct btrfs_extent_item_v0 *ei0;
884 struct btrfs_extent_ref_v0 *ref0;
885 struct btrfs_tree_block_info *bi;
886 struct extent_buffer *leaf;
887 struct btrfs_key key;
888 struct btrfs_key found_key;
889 u32 new_size = sizeof(*item);
893 leaf = path->nodes[0];
894 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
896 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
897 ei0 = btrfs_item_ptr(leaf, path->slots[0],
898 struct btrfs_extent_item_v0);
899 refs = btrfs_extent_refs_v0(leaf, ei0);
901 if (owner == (u64)-1) {
903 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
904 ret = btrfs_next_leaf(root, path);
908 leaf = path->nodes[0];
910 btrfs_item_key_to_cpu(leaf, &found_key,
912 BUG_ON(key.objectid != found_key.objectid);
913 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
917 ref0 = btrfs_item_ptr(leaf, path->slots[0],
918 struct btrfs_extent_ref_v0);
919 owner = btrfs_ref_objectid_v0(leaf, ref0);
923 btrfs_release_path(root, path);
925 if (owner < BTRFS_FIRST_FREE_OBJECTID)
926 new_size += sizeof(*bi);
928 new_size -= sizeof(*ei0);
929 ret = btrfs_search_slot(trans, root, &key, path,
930 new_size + extra_size, 1);
935 ret = btrfs_extend_item(trans, root, path, new_size);
938 leaf = path->nodes[0];
939 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
940 btrfs_set_extent_refs(leaf, item, refs);
941 /* FIXME: get real generation */
942 btrfs_set_extent_generation(leaf, item, 0);
943 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
944 btrfs_set_extent_flags(leaf, item,
945 BTRFS_EXTENT_FLAG_TREE_BLOCK |
946 BTRFS_BLOCK_FLAG_FULL_BACKREF);
947 bi = (struct btrfs_tree_block_info *)(item + 1);
948 /* FIXME: get first key of the block */
949 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
950 btrfs_set_tree_block_level(leaf, bi, (int)owner);
952 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
954 btrfs_mark_buffer_dirty(leaf);
959 static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
961 u32 high_crc = ~(u32)0;
962 u32 low_crc = ~(u32)0;
965 lenum = cpu_to_le64(root_objectid);
966 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
967 lenum = cpu_to_le64(owner);
968 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
969 lenum = cpu_to_le64(offset);
970 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
972 return ((u64)high_crc << 31) ^ (u64)low_crc;
975 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
976 struct btrfs_extent_data_ref *ref)
978 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
979 btrfs_extent_data_ref_objectid(leaf, ref),
980 btrfs_extent_data_ref_offset(leaf, ref));
983 static int match_extent_data_ref(struct extent_buffer *leaf,
984 struct btrfs_extent_data_ref *ref,
985 u64 root_objectid, u64 owner, u64 offset)
987 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
988 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
989 btrfs_extent_data_ref_offset(leaf, ref) != offset)
994 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
995 struct btrfs_root *root,
996 struct btrfs_path *path,
997 u64 bytenr, u64 parent,
999 u64 owner, u64 offset)
1001 struct btrfs_key key;
1002 struct btrfs_extent_data_ref *ref;
1003 struct extent_buffer *leaf;
1009 key.objectid = bytenr;
1011 key.type = BTRFS_SHARED_DATA_REF_KEY;
1012 key.offset = parent;
1014 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1015 key.offset = hash_extent_data_ref(root_objectid,
1020 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1029 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1030 key.type = BTRFS_EXTENT_REF_V0_KEY;
1031 btrfs_release_path(root, path);
1032 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1043 leaf = path->nodes[0];
1044 nritems = btrfs_header_nritems(leaf);
1046 if (path->slots[0] >= nritems) {
1047 ret = btrfs_next_leaf(root, path);
1053 leaf = path->nodes[0];
1054 nritems = btrfs_header_nritems(leaf);
1058 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1059 if (key.objectid != bytenr ||
1060 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1063 ref = btrfs_item_ptr(leaf, path->slots[0],
1064 struct btrfs_extent_data_ref);
1066 if (match_extent_data_ref(leaf, ref, root_objectid,
1069 btrfs_release_path(root, path);
1081 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1082 struct btrfs_root *root,
1083 struct btrfs_path *path,
1084 u64 bytenr, u64 parent,
1085 u64 root_objectid, u64 owner,
1086 u64 offset, int refs_to_add)
1088 struct btrfs_key key;
1089 struct extent_buffer *leaf;
1094 key.objectid = bytenr;
1096 key.type = BTRFS_SHARED_DATA_REF_KEY;
1097 key.offset = parent;
1098 size = sizeof(struct btrfs_shared_data_ref);
1100 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1101 key.offset = hash_extent_data_ref(root_objectid,
1103 size = sizeof(struct btrfs_extent_data_ref);
1106 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1107 if (ret && ret != -EEXIST)
1110 leaf = path->nodes[0];
1112 struct btrfs_shared_data_ref *ref;
1113 ref = btrfs_item_ptr(leaf, path->slots[0],
1114 struct btrfs_shared_data_ref);
1116 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1118 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1119 num_refs += refs_to_add;
1120 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1123 struct btrfs_extent_data_ref *ref;
1124 while (ret == -EEXIST) {
1125 ref = btrfs_item_ptr(leaf, path->slots[0],
1126 struct btrfs_extent_data_ref);
1127 if (match_extent_data_ref(leaf, ref, root_objectid,
1130 btrfs_release_path(root, path);
1132 ret = btrfs_insert_empty_item(trans, root, path, &key,
1134 if (ret && ret != -EEXIST)
1137 leaf = path->nodes[0];
1139 ref = btrfs_item_ptr(leaf, path->slots[0],
1140 struct btrfs_extent_data_ref);
1142 btrfs_set_extent_data_ref_root(leaf, ref,
1144 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1145 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1146 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1148 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1149 num_refs += refs_to_add;
1150 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1153 btrfs_mark_buffer_dirty(leaf);
1156 btrfs_release_path(root, path);
1160 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1161 struct btrfs_root *root,
1162 struct btrfs_path *path,
1165 struct btrfs_key key;
1166 struct btrfs_extent_data_ref *ref1 = NULL;
1167 struct btrfs_shared_data_ref *ref2 = NULL;
1168 struct extent_buffer *leaf;
1172 leaf = path->nodes[0];
1173 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1175 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1176 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1177 struct btrfs_extent_data_ref);
1178 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1179 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1180 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1181 struct btrfs_shared_data_ref);
1182 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1183 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1184 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1185 struct btrfs_extent_ref_v0 *ref0;
1186 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1187 struct btrfs_extent_ref_v0);
1188 num_refs = btrfs_ref_count_v0(leaf, ref0);
1194 BUG_ON(num_refs < refs_to_drop);
1195 num_refs -= refs_to_drop;
1197 if (num_refs == 0) {
1198 ret = btrfs_del_item(trans, root, path);
1200 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1201 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1202 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1203 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1204 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1206 struct btrfs_extent_ref_v0 *ref0;
1207 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1208 struct btrfs_extent_ref_v0);
1209 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1212 btrfs_mark_buffer_dirty(leaf);
1217 static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1218 struct btrfs_path *path,
1219 struct btrfs_extent_inline_ref *iref)
1221 struct btrfs_key key;
1222 struct extent_buffer *leaf;
1223 struct btrfs_extent_data_ref *ref1;
1224 struct btrfs_shared_data_ref *ref2;
1227 leaf = path->nodes[0];
1228 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1230 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1231 BTRFS_EXTENT_DATA_REF_KEY) {
1232 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1233 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1235 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1236 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1238 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1239 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1240 struct btrfs_extent_data_ref);
1241 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1242 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1243 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1244 struct btrfs_shared_data_ref);
1245 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1246 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1247 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1248 struct btrfs_extent_ref_v0 *ref0;
1249 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1250 struct btrfs_extent_ref_v0);
1251 num_refs = btrfs_ref_count_v0(leaf, ref0);
1259 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1260 struct btrfs_root *root,
1261 struct btrfs_path *path,
1262 u64 bytenr, u64 parent,
1265 struct btrfs_key key;
1268 key.objectid = bytenr;
1270 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1271 key.offset = parent;
1273 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1274 key.offset = root_objectid;
1277 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1280 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1281 if (ret == -ENOENT && parent) {
1282 btrfs_release_path(root, path);
1283 key.type = BTRFS_EXTENT_REF_V0_KEY;
1284 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1292 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1293 struct btrfs_root *root,
1294 struct btrfs_path *path,
1295 u64 bytenr, u64 parent,
1298 struct btrfs_key key;
1301 key.objectid = bytenr;
1303 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1304 key.offset = parent;
1306 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1307 key.offset = root_objectid;
1310 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1311 btrfs_release_path(root, path);
1315 static inline int extent_ref_type(u64 parent, u64 owner)
1318 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1320 type = BTRFS_SHARED_BLOCK_REF_KEY;
1322 type = BTRFS_TREE_BLOCK_REF_KEY;
1325 type = BTRFS_SHARED_DATA_REF_KEY;
1327 type = BTRFS_EXTENT_DATA_REF_KEY;
1332 static int find_next_key(struct btrfs_path *path, int level,
1333 struct btrfs_key *key)
1336 for (; level < BTRFS_MAX_LEVEL; level++) {
1337 if (!path->nodes[level])
1339 if (path->slots[level] + 1 >=
1340 btrfs_header_nritems(path->nodes[level]))
1343 btrfs_item_key_to_cpu(path->nodes[level], key,
1344 path->slots[level] + 1);
1346 btrfs_node_key_to_cpu(path->nodes[level], key,
1347 path->slots[level] + 1);
1354 * look for inline back ref. if back ref is found, *ref_ret is set
1355 * to the address of inline back ref, and 0 is returned.
1357 * if back ref isn't found, *ref_ret is set to the address where it
1358 * should be inserted, and -ENOENT is returned.
1360 * if insert is true and there are too many inline back refs, the path
1361 * points to the extent item, and -EAGAIN is returned.
1363 * NOTE: inline back refs are ordered in the same way that back ref
1364 * items in the tree are ordered.
1366 static noinline_for_stack
1367 int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1368 struct btrfs_root *root,
1369 struct btrfs_path *path,
1370 struct btrfs_extent_inline_ref **ref_ret,
1371 u64 bytenr, u64 num_bytes,
1372 u64 parent, u64 root_objectid,
1373 u64 owner, u64 offset, int insert)
1375 struct btrfs_key key;
1376 struct extent_buffer *leaf;
1377 struct btrfs_extent_item *ei;
1378 struct btrfs_extent_inline_ref *iref;
1389 key.objectid = bytenr;
1390 key.type = BTRFS_EXTENT_ITEM_KEY;
1391 key.offset = num_bytes;
1393 want = extent_ref_type(parent, owner);
1395 extra_size = btrfs_extent_inline_ref_size(want);
1396 path->keep_locks = 1;
1399 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1406 leaf = path->nodes[0];
1407 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1408 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1409 if (item_size < sizeof(*ei)) {
1414 ret = convert_extent_item_v0(trans, root, path, owner,
1420 leaf = path->nodes[0];
1421 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1424 BUG_ON(item_size < sizeof(*ei));
1426 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1427 flags = btrfs_extent_flags(leaf, ei);
1429 ptr = (unsigned long)(ei + 1);
1430 end = (unsigned long)ei + item_size;
1432 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1433 ptr += sizeof(struct btrfs_tree_block_info);
1436 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1445 iref = (struct btrfs_extent_inline_ref *)ptr;
1446 type = btrfs_extent_inline_ref_type(leaf, iref);
1450 ptr += btrfs_extent_inline_ref_size(type);
1454 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1455 struct btrfs_extent_data_ref *dref;
1456 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1457 if (match_extent_data_ref(leaf, dref, root_objectid,
1462 if (hash_extent_data_ref_item(leaf, dref) <
1463 hash_extent_data_ref(root_objectid, owner, offset))
1467 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1469 if (parent == ref_offset) {
1473 if (ref_offset < parent)
1476 if (root_objectid == ref_offset) {
1480 if (ref_offset < root_objectid)
1484 ptr += btrfs_extent_inline_ref_size(type);
1486 if (err == -ENOENT && insert) {
1487 if (item_size + extra_size >=
1488 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1493 * To add new inline back ref, we have to make sure
1494 * there is no corresponding back ref item.
1495 * For simplicity, we just do not add new inline back
1496 * ref if there is any kind of item for this block
1498 if (find_next_key(path, 0, &key) == 0 &&
1499 key.objectid == bytenr &&
1500 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
1505 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1508 path->keep_locks = 0;
1509 btrfs_unlock_up_safe(path, 1);
1515 * helper to add new inline back ref
1517 static noinline_for_stack
1518 int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1519 struct btrfs_root *root,
1520 struct btrfs_path *path,
1521 struct btrfs_extent_inline_ref *iref,
1522 u64 parent, u64 root_objectid,
1523 u64 owner, u64 offset, int refs_to_add,
1524 struct btrfs_delayed_extent_op *extent_op)
1526 struct extent_buffer *leaf;
1527 struct btrfs_extent_item *ei;
1530 unsigned long item_offset;
1536 leaf = path->nodes[0];
1537 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1538 item_offset = (unsigned long)iref - (unsigned long)ei;
1540 type = extent_ref_type(parent, owner);
1541 size = btrfs_extent_inline_ref_size(type);
1543 ret = btrfs_extend_item(trans, root, path, size);
1546 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1547 refs = btrfs_extent_refs(leaf, ei);
1548 refs += refs_to_add;
1549 btrfs_set_extent_refs(leaf, ei, refs);
1551 __run_delayed_extent_op(extent_op, leaf, ei);
1553 ptr = (unsigned long)ei + item_offset;
1554 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1555 if (ptr < end - size)
1556 memmove_extent_buffer(leaf, ptr + size, ptr,
1559 iref = (struct btrfs_extent_inline_ref *)ptr;
1560 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1561 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1562 struct btrfs_extent_data_ref *dref;
1563 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1564 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1565 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1566 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1567 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1568 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1569 struct btrfs_shared_data_ref *sref;
1570 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1571 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1572 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1573 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1574 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1576 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1578 btrfs_mark_buffer_dirty(leaf);
1582 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1583 struct btrfs_root *root,
1584 struct btrfs_path *path,
1585 struct btrfs_extent_inline_ref **ref_ret,
1586 u64 bytenr, u64 num_bytes, u64 parent,
1587 u64 root_objectid, u64 owner, u64 offset)
1591 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1592 bytenr, num_bytes, parent,
1593 root_objectid, owner, offset, 0);
1597 btrfs_release_path(root, path);
1600 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1601 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1604 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1605 root_objectid, owner, offset);
1611 * helper to update/remove inline back ref
1613 static noinline_for_stack
1614 int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1615 struct btrfs_root *root,
1616 struct btrfs_path *path,
1617 struct btrfs_extent_inline_ref *iref,
1619 struct btrfs_delayed_extent_op *extent_op)
1621 struct extent_buffer *leaf;
1622 struct btrfs_extent_item *ei;
1623 struct btrfs_extent_data_ref *dref = NULL;
1624 struct btrfs_shared_data_ref *sref = NULL;
1633 leaf = path->nodes[0];
1634 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1635 refs = btrfs_extent_refs(leaf, ei);
1636 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1637 refs += refs_to_mod;
1638 btrfs_set_extent_refs(leaf, ei, refs);
1640 __run_delayed_extent_op(extent_op, leaf, ei);
1642 type = btrfs_extent_inline_ref_type(leaf, iref);
1644 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1645 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1646 refs = btrfs_extent_data_ref_count(leaf, dref);
1647 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1648 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1649 refs = btrfs_shared_data_ref_count(leaf, sref);
1652 BUG_ON(refs_to_mod != -1);
1655 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1656 refs += refs_to_mod;
1659 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1660 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1662 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1664 size = btrfs_extent_inline_ref_size(type);
1665 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1666 ptr = (unsigned long)iref;
1667 end = (unsigned long)ei + item_size;
1668 if (ptr + size < end)
1669 memmove_extent_buffer(leaf, ptr, ptr + size,
1672 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1675 btrfs_mark_buffer_dirty(leaf);
1679 static noinline_for_stack
1680 int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1681 struct btrfs_root *root,
1682 struct btrfs_path *path,
1683 u64 bytenr, u64 num_bytes, u64 parent,
1684 u64 root_objectid, u64 owner,
1685 u64 offset, int refs_to_add,
1686 struct btrfs_delayed_extent_op *extent_op)
1688 struct btrfs_extent_inline_ref *iref;
1691 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1692 bytenr, num_bytes, parent,
1693 root_objectid, owner, offset, 1);
1695 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1696 ret = update_inline_extent_backref(trans, root, path, iref,
1697 refs_to_add, extent_op);
1698 } else if (ret == -ENOENT) {
1699 ret = setup_inline_extent_backref(trans, root, path, iref,
1700 parent, root_objectid,
1701 owner, offset, refs_to_add,
1707 static int insert_extent_backref(struct btrfs_trans_handle *trans,
1708 struct btrfs_root *root,
1709 struct btrfs_path *path,
1710 u64 bytenr, u64 parent, u64 root_objectid,
1711 u64 owner, u64 offset, int refs_to_add)
1714 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1715 BUG_ON(refs_to_add != 1);
1716 ret = insert_tree_block_ref(trans, root, path, bytenr,
1717 parent, root_objectid);
1719 ret = insert_extent_data_ref(trans, root, path, bytenr,
1720 parent, root_objectid,
1721 owner, offset, refs_to_add);
1726 static int remove_extent_backref(struct btrfs_trans_handle *trans,
1727 struct btrfs_root *root,
1728 struct btrfs_path *path,
1729 struct btrfs_extent_inline_ref *iref,
1730 int refs_to_drop, int is_data)
1734 BUG_ON(!is_data && refs_to_drop != 1);
1736 ret = update_inline_extent_backref(trans, root, path, iref,
1737 -refs_to_drop, NULL);
1738 } else if (is_data) {
1739 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1741 ret = btrfs_del_item(trans, root, path);
1746 static void btrfs_issue_discard(struct block_device *bdev,
1749 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL, 0);
1752 static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1756 u64 map_length = num_bytes;
1757 struct btrfs_multi_bio *multi = NULL;
1759 if (!btrfs_test_opt(root, DISCARD))
1762 /* Tell the block device(s) that the sectors can be discarded */
1763 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1764 bytenr, &map_length, &multi, 0);
1766 struct btrfs_bio_stripe *stripe = multi->stripes;
1769 if (map_length > num_bytes)
1770 map_length = num_bytes;
1772 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1773 btrfs_issue_discard(stripe->dev->bdev,
1783 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1784 struct btrfs_root *root,
1785 u64 bytenr, u64 num_bytes, u64 parent,
1786 u64 root_objectid, u64 owner, u64 offset)
1789 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1790 root_objectid == BTRFS_TREE_LOG_OBJECTID);
1792 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1793 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1794 parent, root_objectid, (int)owner,
1795 BTRFS_ADD_DELAYED_REF, NULL);
1797 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1798 parent, root_objectid, owner, offset,
1799 BTRFS_ADD_DELAYED_REF, NULL);
1804 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1805 struct btrfs_root *root,
1806 u64 bytenr, u64 num_bytes,
1807 u64 parent, u64 root_objectid,
1808 u64 owner, u64 offset, int refs_to_add,
1809 struct btrfs_delayed_extent_op *extent_op)
1811 struct btrfs_path *path;
1812 struct extent_buffer *leaf;
1813 struct btrfs_extent_item *item;
1818 path = btrfs_alloc_path();
1823 path->leave_spinning = 1;
1824 /* this will setup the path even if it fails to insert the back ref */
1825 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1826 path, bytenr, num_bytes, parent,
1827 root_objectid, owner, offset,
1828 refs_to_add, extent_op);
1832 if (ret != -EAGAIN) {
1837 leaf = path->nodes[0];
1838 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1839 refs = btrfs_extent_refs(leaf, item);
1840 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1842 __run_delayed_extent_op(extent_op, leaf, item);
1844 btrfs_mark_buffer_dirty(leaf);
1845 btrfs_release_path(root->fs_info->extent_root, path);
1848 path->leave_spinning = 1;
1850 /* now insert the actual backref */
1851 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1852 path, bytenr, parent, root_objectid,
1853 owner, offset, refs_to_add);
1856 btrfs_free_path(path);
1860 static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1861 struct btrfs_root *root,
1862 struct btrfs_delayed_ref_node *node,
1863 struct btrfs_delayed_extent_op *extent_op,
1864 int insert_reserved)
1867 struct btrfs_delayed_data_ref *ref;
1868 struct btrfs_key ins;
1873 ins.objectid = node->bytenr;
1874 ins.offset = node->num_bytes;
1875 ins.type = BTRFS_EXTENT_ITEM_KEY;
1877 ref = btrfs_delayed_node_to_data_ref(node);
1878 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1879 parent = ref->parent;
1881 ref_root = ref->root;
1883 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1885 BUG_ON(extent_op->update_key);
1886 flags |= extent_op->flags_to_set;
1888 ret = alloc_reserved_file_extent(trans, root,
1889 parent, ref_root, flags,
1890 ref->objectid, ref->offset,
1891 &ins, node->ref_mod);
1892 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1893 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1894 node->num_bytes, parent,
1895 ref_root, ref->objectid,
1896 ref->offset, node->ref_mod,
1898 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1899 ret = __btrfs_free_extent(trans, root, node->bytenr,
1900 node->num_bytes, parent,
1901 ref_root, ref->objectid,
1902 ref->offset, node->ref_mod,
1910 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1911 struct extent_buffer *leaf,
1912 struct btrfs_extent_item *ei)
1914 u64 flags = btrfs_extent_flags(leaf, ei);
1915 if (extent_op->update_flags) {
1916 flags |= extent_op->flags_to_set;
1917 btrfs_set_extent_flags(leaf, ei, flags);
1920 if (extent_op->update_key) {
1921 struct btrfs_tree_block_info *bi;
1922 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1923 bi = (struct btrfs_tree_block_info *)(ei + 1);
1924 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1928 static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1929 struct btrfs_root *root,
1930 struct btrfs_delayed_ref_node *node,
1931 struct btrfs_delayed_extent_op *extent_op)
1933 struct btrfs_key key;
1934 struct btrfs_path *path;
1935 struct btrfs_extent_item *ei;
1936 struct extent_buffer *leaf;
1941 path = btrfs_alloc_path();
1945 key.objectid = node->bytenr;
1946 key.type = BTRFS_EXTENT_ITEM_KEY;
1947 key.offset = node->num_bytes;
1950 path->leave_spinning = 1;
1951 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1962 leaf = path->nodes[0];
1963 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1964 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1965 if (item_size < sizeof(*ei)) {
1966 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1972 leaf = path->nodes[0];
1973 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1976 BUG_ON(item_size < sizeof(*ei));
1977 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1978 __run_delayed_extent_op(extent_op, leaf, ei);
1980 btrfs_mark_buffer_dirty(leaf);
1982 btrfs_free_path(path);
1986 static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1987 struct btrfs_root *root,
1988 struct btrfs_delayed_ref_node *node,
1989 struct btrfs_delayed_extent_op *extent_op,
1990 int insert_reserved)
1993 struct btrfs_delayed_tree_ref *ref;
1994 struct btrfs_key ins;
1998 ins.objectid = node->bytenr;
1999 ins.offset = node->num_bytes;
2000 ins.type = BTRFS_EXTENT_ITEM_KEY;
2002 ref = btrfs_delayed_node_to_tree_ref(node);
2003 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2004 parent = ref->parent;
2006 ref_root = ref->root;
2008 BUG_ON(node->ref_mod != 1);
2009 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2010 BUG_ON(!extent_op || !extent_op->update_flags ||
2011 !extent_op->update_key);
2012 ret = alloc_reserved_tree_block(trans, root,
2014 extent_op->flags_to_set,
2017 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2018 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2019 node->num_bytes, parent, ref_root,
2020 ref->level, 0, 1, extent_op);
2021 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2022 ret = __btrfs_free_extent(trans, root, node->bytenr,
2023 node->num_bytes, parent, ref_root,
2024 ref->level, 0, 1, extent_op);
2031 /* helper function to actually process a single delayed ref entry */
2032 static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2033 struct btrfs_root *root,
2034 struct btrfs_delayed_ref_node *node,
2035 struct btrfs_delayed_extent_op *extent_op,
2036 int insert_reserved)
2039 if (btrfs_delayed_ref_is_head(node)) {
2040 struct btrfs_delayed_ref_head *head;
2042 * we've hit the end of the chain and we were supposed
2043 * to insert this extent into the tree. But, it got
2044 * deleted before we ever needed to insert it, so all
2045 * we have to do is clean up the accounting
2048 head = btrfs_delayed_node_to_head(node);
2049 if (insert_reserved) {
2050 btrfs_pin_extent(root, node->bytenr,
2051 node->num_bytes, 1);
2052 if (head->is_data) {
2053 ret = btrfs_del_csums(trans, root,
2059 mutex_unlock(&head->mutex);
2063 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2064 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2065 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2067 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2068 node->type == BTRFS_SHARED_DATA_REF_KEY)
2069 ret = run_delayed_data_ref(trans, root, node, extent_op,
2076 static noinline struct btrfs_delayed_ref_node *
2077 select_delayed_ref(struct btrfs_delayed_ref_head *head)
2079 struct rb_node *node;
2080 struct btrfs_delayed_ref_node *ref;
2081 int action = BTRFS_ADD_DELAYED_REF;
2084 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2085 * this prevents ref count from going down to zero when
2086 * there still are pending delayed ref.
2088 node = rb_prev(&head->node.rb_node);
2092 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2094 if (ref->bytenr != head->node.bytenr)
2096 if (ref->action == action)
2098 node = rb_prev(node);
2100 if (action == BTRFS_ADD_DELAYED_REF) {
2101 action = BTRFS_DROP_DELAYED_REF;
2107 static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2108 struct btrfs_root *root,
2109 struct list_head *cluster)
2111 struct btrfs_delayed_ref_root *delayed_refs;
2112 struct btrfs_delayed_ref_node *ref;
2113 struct btrfs_delayed_ref_head *locked_ref = NULL;
2114 struct btrfs_delayed_extent_op *extent_op;
2117 int must_insert_reserved = 0;
2119 delayed_refs = &trans->transaction->delayed_refs;
2122 /* pick a new head ref from the cluster list */
2123 if (list_empty(cluster))
2126 locked_ref = list_entry(cluster->next,
2127 struct btrfs_delayed_ref_head, cluster);
2129 /* grab the lock that says we are going to process
2130 * all the refs for this head */
2131 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2134 * we may have dropped the spin lock to get the head
2135 * mutex lock, and that might have given someone else
2136 * time to free the head. If that's true, it has been
2137 * removed from our list and we can move on.
2139 if (ret == -EAGAIN) {
2147 * record the must insert reserved flag before we
2148 * drop the spin lock.
2150 must_insert_reserved = locked_ref->must_insert_reserved;
2151 locked_ref->must_insert_reserved = 0;
2153 extent_op = locked_ref->extent_op;
2154 locked_ref->extent_op = NULL;
2157 * locked_ref is the head node, so we have to go one
2158 * node back for any delayed ref updates
2160 ref = select_delayed_ref(locked_ref);
2162 /* All delayed refs have been processed, Go ahead
2163 * and send the head node to run_one_delayed_ref,
2164 * so that any accounting fixes can happen
2166 ref = &locked_ref->node;
2168 if (extent_op && must_insert_reserved) {
2174 spin_unlock(&delayed_refs->lock);
2176 ret = run_delayed_extent_op(trans, root,
2182 spin_lock(&delayed_refs->lock);
2186 list_del_init(&locked_ref->cluster);
2191 rb_erase(&ref->rb_node, &delayed_refs->root);
2192 delayed_refs->num_entries--;
2194 spin_unlock(&delayed_refs->lock);
2196 ret = run_one_delayed_ref(trans, root, ref, extent_op,
2197 must_insert_reserved);
2200 btrfs_put_delayed_ref(ref);
2205 spin_lock(&delayed_refs->lock);
2211 * this starts processing the delayed reference count updates and
2212 * extent insertions we have queued up so far. count can be
2213 * 0, which means to process everything in the tree at the start
2214 * of the run (but not newly added entries), or it can be some target
2215 * number you'd like to process.
2217 int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2218 struct btrfs_root *root, unsigned long count)
2220 struct rb_node *node;
2221 struct btrfs_delayed_ref_root *delayed_refs;
2222 struct btrfs_delayed_ref_node *ref;
2223 struct list_head cluster;
2225 int run_all = count == (unsigned long)-1;
2228 if (root == root->fs_info->extent_root)
2229 root = root->fs_info->tree_root;
2231 delayed_refs = &trans->transaction->delayed_refs;
2232 INIT_LIST_HEAD(&cluster);
2234 spin_lock(&delayed_refs->lock);
2236 count = delayed_refs->num_entries * 2;
2240 if (!(run_all || run_most) &&
2241 delayed_refs->num_heads_ready < 64)
2245 * go find something we can process in the rbtree. We start at
2246 * the beginning of the tree, and then build a cluster
2247 * of refs to process starting at the first one we are able to
2250 ret = btrfs_find_ref_cluster(trans, &cluster,
2251 delayed_refs->run_delayed_start);
2255 ret = run_clustered_refs(trans, root, &cluster);
2258 count -= min_t(unsigned long, ret, count);
2265 node = rb_first(&delayed_refs->root);
2268 count = (unsigned long)-1;
2271 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2273 if (btrfs_delayed_ref_is_head(ref)) {
2274 struct btrfs_delayed_ref_head *head;
2276 head = btrfs_delayed_node_to_head(ref);
2277 atomic_inc(&ref->refs);
2279 spin_unlock(&delayed_refs->lock);
2280 mutex_lock(&head->mutex);
2281 mutex_unlock(&head->mutex);
2283 btrfs_put_delayed_ref(ref);
2287 node = rb_next(node);
2289 spin_unlock(&delayed_refs->lock);
2290 schedule_timeout(1);
2294 spin_unlock(&delayed_refs->lock);
2298 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2299 struct btrfs_root *root,
2300 u64 bytenr, u64 num_bytes, u64 flags,
2303 struct btrfs_delayed_extent_op *extent_op;
2306 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2310 extent_op->flags_to_set = flags;
2311 extent_op->update_flags = 1;
2312 extent_op->update_key = 0;
2313 extent_op->is_data = is_data ? 1 : 0;
2315 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2321 static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2322 struct btrfs_root *root,
2323 struct btrfs_path *path,
2324 u64 objectid, u64 offset, u64 bytenr)
2326 struct btrfs_delayed_ref_head *head;
2327 struct btrfs_delayed_ref_node *ref;
2328 struct btrfs_delayed_data_ref *data_ref;
2329 struct btrfs_delayed_ref_root *delayed_refs;
2330 struct rb_node *node;
2334 delayed_refs = &trans->transaction->delayed_refs;
2335 spin_lock(&delayed_refs->lock);
2336 head = btrfs_find_delayed_ref_head(trans, bytenr);
2340 if (!mutex_trylock(&head->mutex)) {
2341 atomic_inc(&head->node.refs);
2342 spin_unlock(&delayed_refs->lock);
2344 btrfs_release_path(root->fs_info->extent_root, path);
2346 mutex_lock(&head->mutex);
2347 mutex_unlock(&head->mutex);
2348 btrfs_put_delayed_ref(&head->node);
2352 node = rb_prev(&head->node.rb_node);
2356 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2358 if (ref->bytenr != bytenr)
2362 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2365 data_ref = btrfs_delayed_node_to_data_ref(ref);
2367 node = rb_prev(node);
2369 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2370 if (ref->bytenr == bytenr)
2374 if (data_ref->root != root->root_key.objectid ||
2375 data_ref->objectid != objectid || data_ref->offset != offset)
2380 mutex_unlock(&head->mutex);
2382 spin_unlock(&delayed_refs->lock);
2386 static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2387 struct btrfs_root *root,
2388 struct btrfs_path *path,
2389 u64 objectid, u64 offset, u64 bytenr)
2391 struct btrfs_root *extent_root = root->fs_info->extent_root;
2392 struct extent_buffer *leaf;
2393 struct btrfs_extent_data_ref *ref;
2394 struct btrfs_extent_inline_ref *iref;
2395 struct btrfs_extent_item *ei;
2396 struct btrfs_key key;
2400 key.objectid = bytenr;
2401 key.offset = (u64)-1;
2402 key.type = BTRFS_EXTENT_ITEM_KEY;
2404 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2410 if (path->slots[0] == 0)
2414 leaf = path->nodes[0];
2415 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2417 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
2421 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2422 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2423 if (item_size < sizeof(*ei)) {
2424 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2428 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2430 if (item_size != sizeof(*ei) +
2431 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2434 if (btrfs_extent_generation(leaf, ei) <=
2435 btrfs_root_last_snapshot(&root->root_item))
2438 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2439 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2440 BTRFS_EXTENT_DATA_REF_KEY)
2443 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2444 if (btrfs_extent_refs(leaf, ei) !=
2445 btrfs_extent_data_ref_count(leaf, ref) ||
2446 btrfs_extent_data_ref_root(leaf, ref) !=
2447 root->root_key.objectid ||
2448 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2449 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2457 int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2458 struct btrfs_root *root,
2459 u64 objectid, u64 offset, u64 bytenr)
2461 struct btrfs_path *path;
2465 path = btrfs_alloc_path();
2470 ret = check_committed_ref(trans, root, path, objectid,
2472 if (ret && ret != -ENOENT)
2475 ret2 = check_delayed_ref(trans, root, path, objectid,
2477 } while (ret2 == -EAGAIN);
2479 if (ret2 && ret2 != -ENOENT) {
2484 if (ret != -ENOENT || ret2 != -ENOENT)
2487 btrfs_free_path(path);
2488 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2494 int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2495 struct extent_buffer *buf, u32 nr_extents)
2497 struct btrfs_key key;
2498 struct btrfs_file_extent_item *fi;
2506 if (!root->ref_cows)
2509 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2511 root_gen = root->root_key.offset;
2514 root_gen = trans->transid - 1;
2517 level = btrfs_header_level(buf);
2518 nritems = btrfs_header_nritems(buf);
2521 struct btrfs_leaf_ref *ref;
2522 struct btrfs_extent_info *info;
2524 ref = btrfs_alloc_leaf_ref(root, nr_extents);
2530 ref->root_gen = root_gen;
2531 ref->bytenr = buf->start;
2532 ref->owner = btrfs_header_owner(buf);
2533 ref->generation = btrfs_header_generation(buf);
2534 ref->nritems = nr_extents;
2535 info = ref->extents;
2537 for (i = 0; nr_extents > 0 && i < nritems; i++) {
2539 btrfs_item_key_to_cpu(buf, &key, i);
2540 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2542 fi = btrfs_item_ptr(buf, i,
2543 struct btrfs_file_extent_item);
2544 if (btrfs_file_extent_type(buf, fi) ==
2545 BTRFS_FILE_EXTENT_INLINE)
2547 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2548 if (disk_bytenr == 0)
2551 info->bytenr = disk_bytenr;
2553 btrfs_file_extent_disk_num_bytes(buf, fi);
2554 info->objectid = key.objectid;
2555 info->offset = key.offset;
2559 ret = btrfs_add_leaf_ref(root, ref, shared);
2560 if (ret == -EEXIST && shared) {
2561 struct btrfs_leaf_ref *old;
2562 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2564 btrfs_remove_leaf_ref(root, old);
2565 btrfs_free_leaf_ref(root, old);
2566 ret = btrfs_add_leaf_ref(root, ref, shared);
2569 btrfs_free_leaf_ref(root, ref);
2575 /* when a block goes through cow, we update the reference counts of
2576 * everything that block points to. The internal pointers of the block
2577 * can be in just about any order, and it is likely to have clusters of
2578 * things that are close together and clusters of things that are not.
2580 * To help reduce the seeks that come with updating all of these reference
2581 * counts, sort them by byte number before actual updates are done.
2583 * struct refsort is used to match byte number to slot in the btree block.
2584 * we sort based on the byte number and then use the slot to actually
2587 * struct refsort is smaller than strcut btrfs_item and smaller than
2588 * struct btrfs_key_ptr. Since we're currently limited to the page size
2589 * for a btree block, there's no way for a kmalloc of refsorts for a
2590 * single node to be bigger than a page.
2598 * for passing into sort()
2600 static int refsort_cmp(const void *a_void, const void *b_void)
2602 const struct refsort *a = a_void;
2603 const struct refsort *b = b_void;
2605 if (a->bytenr < b->bytenr)
2607 if (a->bytenr > b->bytenr)
2613 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
2614 struct btrfs_root *root,
2615 struct extent_buffer *buf,
2616 int full_backref, int inc)
2623 struct btrfs_key key;
2624 struct btrfs_file_extent_item *fi;
2628 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
2629 u64, u64, u64, u64, u64, u64);
2631 ref_root = btrfs_header_owner(buf);
2632 nritems = btrfs_header_nritems(buf);
2633 level = btrfs_header_level(buf);
2635 if (!root->ref_cows && level == 0)
2639 process_func = btrfs_inc_extent_ref;
2641 process_func = btrfs_free_extent;
2644 parent = buf->start;
2648 for (i = 0; i < nritems; i++) {
2650 btrfs_item_key_to_cpu(buf, &key, i);
2651 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2653 fi = btrfs_item_ptr(buf, i,
2654 struct btrfs_file_extent_item);
2655 if (btrfs_file_extent_type(buf, fi) ==
2656 BTRFS_FILE_EXTENT_INLINE)
2658 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2662 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2663 key.offset -= btrfs_file_extent_offset(buf, fi);
2664 ret = process_func(trans, root, bytenr, num_bytes,
2665 parent, ref_root, key.objectid,
2670 bytenr = btrfs_node_blockptr(buf, i);
2671 num_bytes = btrfs_level_size(root, level - 1);
2672 ret = process_func(trans, root, bytenr, num_bytes,
2673 parent, ref_root, level - 1, 0);
2684 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2685 struct extent_buffer *buf, int full_backref)
2687 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2690 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2691 struct extent_buffer *buf, int full_backref)
2693 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
2696 static int write_one_cache_group(struct btrfs_trans_handle *trans,
2697 struct btrfs_root *root,
2698 struct btrfs_path *path,
2699 struct btrfs_block_group_cache *cache)
2702 struct btrfs_root *extent_root = root->fs_info->extent_root;
2704 struct extent_buffer *leaf;
2706 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
2711 leaf = path->nodes[0];
2712 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2713 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2714 btrfs_mark_buffer_dirty(leaf);
2715 btrfs_release_path(extent_root, path);
2723 static struct btrfs_block_group_cache *
2724 next_block_group(struct btrfs_root *root,
2725 struct btrfs_block_group_cache *cache)
2727 struct rb_node *node;
2728 spin_lock(&root->fs_info->block_group_cache_lock);
2729 node = rb_next(&cache->cache_node);
2730 btrfs_put_block_group(cache);
2732 cache = rb_entry(node, struct btrfs_block_group_cache,
2734 btrfs_get_block_group(cache);
2737 spin_unlock(&root->fs_info->block_group_cache_lock);
2741 static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2742 struct btrfs_trans_handle *trans,
2743 struct btrfs_path *path)
2745 struct btrfs_root *root = block_group->fs_info->tree_root;
2746 struct inode *inode = NULL;
2748 int dcs = BTRFS_DC_ERROR;
2754 * If this block group is smaller than 100 megs don't bother caching the
2757 if (block_group->key.offset < (100 * 1024 * 1024)) {
2758 spin_lock(&block_group->lock);
2759 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2760 spin_unlock(&block_group->lock);
2765 inode = lookup_free_space_inode(root, block_group, path);
2766 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2767 ret = PTR_ERR(inode);
2768 btrfs_release_path(root, path);
2772 if (IS_ERR(inode)) {
2776 if (block_group->ro)
2779 ret = create_free_space_inode(root, trans, block_group, path);
2786 * We want to set the generation to 0, that way if anything goes wrong
2787 * from here on out we know not to trust this cache when we load up next
2790 BTRFS_I(inode)->generation = 0;
2791 ret = btrfs_update_inode(trans, root, inode);
2794 if (i_size_read(inode) > 0) {
2795 ret = btrfs_truncate_free_space_cache(root, trans, path,
2801 spin_lock(&block_group->lock);
2802 if (block_group->cached != BTRFS_CACHE_FINISHED) {
2803 /* We're not cached, don't bother trying to write stuff out */
2804 dcs = BTRFS_DC_WRITTEN;
2805 spin_unlock(&block_group->lock);
2808 spin_unlock(&block_group->lock);
2810 num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024);
2815 * Just to make absolutely sure we have enough space, we're going to
2816 * preallocate 12 pages worth of space for each block group. In
2817 * practice we ought to use at most 8, but we need extra space so we can
2818 * add our header and have a terminator between the extents and the
2822 num_pages *= PAGE_CACHE_SIZE;
2824 ret = btrfs_check_data_free_space(inode, num_pages);
2828 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
2829 num_pages, num_pages,
2832 dcs = BTRFS_DC_SETUP;
2833 btrfs_free_reserved_data_space(inode, num_pages);
2837 btrfs_release_path(root, path);
2839 spin_lock(&block_group->lock);
2840 block_group->disk_cache_state = dcs;
2841 spin_unlock(&block_group->lock);
2846 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2847 struct btrfs_root *root)
2849 struct btrfs_block_group_cache *cache;
2851 struct btrfs_path *path;
2854 path = btrfs_alloc_path();
2860 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2862 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
2864 cache = next_block_group(root, cache);
2872 err = cache_save_setup(cache, trans, path);
2873 last = cache->key.objectid + cache->key.offset;
2874 btrfs_put_block_group(cache);
2879 err = btrfs_run_delayed_refs(trans, root,
2884 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2886 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
2887 btrfs_put_block_group(cache);
2893 cache = next_block_group(root, cache);
2902 if (cache->disk_cache_state == BTRFS_DC_SETUP)
2903 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
2905 last = cache->key.objectid + cache->key.offset;
2907 err = write_one_cache_group(trans, root, path, cache);
2909 btrfs_put_block_group(cache);
2914 * I don't think this is needed since we're just marking our
2915 * preallocated extent as written, but just in case it can't
2919 err = btrfs_run_delayed_refs(trans, root,
2924 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2927 * Really this shouldn't happen, but it could if we
2928 * couldn't write the entire preallocated extent and
2929 * splitting the extent resulted in a new block.
2932 btrfs_put_block_group(cache);
2935 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2937 cache = next_block_group(root, cache);
2946 btrfs_write_out_cache(root, trans, cache, path);
2949 * If we didn't have an error then the cache state is still
2950 * NEED_WRITE, so we can set it to WRITTEN.
2952 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2953 cache->disk_cache_state = BTRFS_DC_WRITTEN;
2954 last = cache->key.objectid + cache->key.offset;
2955 btrfs_put_block_group(cache);
2958 btrfs_free_path(path);
2962 int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2964 struct btrfs_block_group_cache *block_group;
2967 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2968 if (!block_group || block_group->ro)
2971 btrfs_put_block_group(block_group);
2975 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2976 u64 total_bytes, u64 bytes_used,
2977 struct btrfs_space_info **space_info)
2979 struct btrfs_space_info *found;
2983 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2984 BTRFS_BLOCK_GROUP_RAID10))
2989 found = __find_space_info(info, flags);
2991 spin_lock(&found->lock);
2992 found->total_bytes += total_bytes;
2993 found->disk_total += total_bytes * factor;
2994 found->bytes_used += bytes_used;
2995 found->disk_used += bytes_used * factor;
2997 spin_unlock(&found->lock);
2998 *space_info = found;
3001 found = kzalloc(sizeof(*found), GFP_NOFS);
3005 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3006 INIT_LIST_HEAD(&found->block_groups[i]);
3007 init_rwsem(&found->groups_sem);
3008 spin_lock_init(&found->lock);
3009 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
3010 BTRFS_BLOCK_GROUP_SYSTEM |
3011 BTRFS_BLOCK_GROUP_METADATA);
3012 found->total_bytes = total_bytes;
3013 found->disk_total = total_bytes * factor;
3014 found->bytes_used = bytes_used;
3015 found->disk_used = bytes_used * factor;
3016 found->bytes_pinned = 0;
3017 found->bytes_reserved = 0;
3018 found->bytes_readonly = 0;
3019 found->bytes_may_use = 0;
3021 found->force_alloc = 0;
3022 *space_info = found;
3023 list_add_rcu(&found->list, &info->space_info);
3024 atomic_set(&found->caching_threads, 0);
3028 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3030 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
3031 BTRFS_BLOCK_GROUP_RAID1 |
3032 BTRFS_BLOCK_GROUP_RAID10 |
3033 BTRFS_BLOCK_GROUP_DUP);
3035 if (flags & BTRFS_BLOCK_GROUP_DATA)
3036 fs_info->avail_data_alloc_bits |= extra_flags;
3037 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3038 fs_info->avail_metadata_alloc_bits |= extra_flags;
3039 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3040 fs_info->avail_system_alloc_bits |= extra_flags;
3044 u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
3047 * we add in the count of missing devices because we want
3048 * to make sure that any RAID levels on a degraded FS
3049 * continue to be honored.
3051 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3052 root->fs_info->fs_devices->missing_devices;
3054 if (num_devices == 1)
3055 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
3056 if (num_devices < 4)
3057 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3059 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
3060 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
3061 BTRFS_BLOCK_GROUP_RAID10))) {
3062 flags &= ~BTRFS_BLOCK_GROUP_DUP;
3065 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
3066 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
3067 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
3070 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
3071 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
3072 (flags & BTRFS_BLOCK_GROUP_RAID10) |
3073 (flags & BTRFS_BLOCK_GROUP_DUP)))
3074 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
3078 static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
3080 if (flags & BTRFS_BLOCK_GROUP_DATA)
3081 flags |= root->fs_info->avail_data_alloc_bits &
3082 root->fs_info->data_alloc_profile;
3083 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3084 flags |= root->fs_info->avail_system_alloc_bits &
3085 root->fs_info->system_alloc_profile;
3086 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3087 flags |= root->fs_info->avail_metadata_alloc_bits &
3088 root->fs_info->metadata_alloc_profile;
3089 return btrfs_reduce_alloc_profile(root, flags);
3092 static u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
3097 flags = BTRFS_BLOCK_GROUP_DATA;
3098 else if (root == root->fs_info->chunk_root)
3099 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3101 flags = BTRFS_BLOCK_GROUP_METADATA;
3103 return get_alloc_profile(root, flags);
3106 void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
3108 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
3109 BTRFS_BLOCK_GROUP_DATA);
3113 * This will check the space that the inode allocates from to make sure we have
3114 * enough space for bytes.
3116 int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
3118 struct btrfs_space_info *data_sinfo;
3119 struct btrfs_root *root = BTRFS_I(inode)->root;
3121 int ret = 0, committed = 0, alloc_chunk = 1;
3123 /* make sure bytes are sectorsize aligned */
3124 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3126 if (root == root->fs_info->tree_root) {
3131 data_sinfo = BTRFS_I(inode)->space_info;
3136 /* make sure we have enough space to handle the data first */
3137 spin_lock(&data_sinfo->lock);
3138 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3139 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3140 data_sinfo->bytes_may_use;
3142 if (used + bytes > data_sinfo->total_bytes) {
3143 struct btrfs_trans_handle *trans;
3146 * if we don't have enough free bytes in this space then we need
3147 * to alloc a new chunk.
3149 if (!data_sinfo->full && alloc_chunk) {
3152 data_sinfo->force_alloc = 1;
3153 spin_unlock(&data_sinfo->lock);
3155 alloc_target = btrfs_get_alloc_profile(root, 1);
3156 trans = btrfs_join_transaction(root, 1);
3158 return PTR_ERR(trans);
3160 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3161 bytes + 2 * 1024 * 1024,
3163 btrfs_end_transaction(trans, root);
3168 btrfs_set_inode_space_info(root, inode);
3169 data_sinfo = BTRFS_I(inode)->space_info;
3173 spin_unlock(&data_sinfo->lock);
3175 /* commit the current transaction and try again */
3176 if (!committed && !root->fs_info->open_ioctl_trans) {
3178 trans = btrfs_join_transaction(root, 1);
3180 return PTR_ERR(trans);
3181 ret = btrfs_commit_transaction(trans, root);
3187 #if 0 /* I hope we never need this code again, just in case */
3188 printk(KERN_ERR "no space left, need %llu, %llu bytes_used, "
3189 "%llu bytes_reserved, " "%llu bytes_pinned, "
3190 "%llu bytes_readonly, %llu may use %llu total\n",
3191 (unsigned long long)bytes,
3192 (unsigned long long)data_sinfo->bytes_used,
3193 (unsigned long long)data_sinfo->bytes_reserved,
3194 (unsigned long long)data_sinfo->bytes_pinned,
3195 (unsigned long long)data_sinfo->bytes_readonly,
3196 (unsigned long long)data_sinfo->bytes_may_use,
3197 (unsigned long long)data_sinfo->total_bytes);
3201 data_sinfo->bytes_may_use += bytes;
3202 BTRFS_I(inode)->reserved_bytes += bytes;
3203 spin_unlock(&data_sinfo->lock);
3209 * called when we are clearing an delalloc extent from the
3210 * inode's io_tree or there was an error for whatever reason
3211 * after calling btrfs_check_data_free_space
3213 void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
3215 struct btrfs_root *root = BTRFS_I(inode)->root;
3216 struct btrfs_space_info *data_sinfo;
3218 /* make sure bytes are sectorsize aligned */
3219 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3221 data_sinfo = BTRFS_I(inode)->space_info;
3222 spin_lock(&data_sinfo->lock);
3223 data_sinfo->bytes_may_use -= bytes;
3224 BTRFS_I(inode)->reserved_bytes -= bytes;
3225 spin_unlock(&data_sinfo->lock);
3228 static void force_metadata_allocation(struct btrfs_fs_info *info)
3230 struct list_head *head = &info->space_info;
3231 struct btrfs_space_info *found;
3234 list_for_each_entry_rcu(found, head, list) {
3235 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
3236 found->force_alloc = 1;
3241 static int should_alloc_chunk(struct btrfs_root *root,
3242 struct btrfs_space_info *sinfo, u64 alloc_bytes)
3244 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
3247 if (sinfo->bytes_used + sinfo->bytes_reserved +
3248 alloc_bytes + 256 * 1024 * 1024 < num_bytes)
3251 if (sinfo->bytes_used + sinfo->bytes_reserved +
3252 alloc_bytes < div_factor(num_bytes, 8))
3255 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
3256 thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5));
3258 if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3))
3264 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3265 struct btrfs_root *extent_root, u64 alloc_bytes,
3266 u64 flags, int force)
3268 struct btrfs_space_info *space_info;
3269 struct btrfs_fs_info *fs_info = extent_root->fs_info;
3272 mutex_lock(&fs_info->chunk_mutex);
3274 flags = btrfs_reduce_alloc_profile(extent_root, flags);
3276 space_info = __find_space_info(extent_root->fs_info, flags);
3278 ret = update_space_info(extent_root->fs_info, flags,
3282 BUG_ON(!space_info);
3284 spin_lock(&space_info->lock);
3285 if (space_info->force_alloc)
3287 if (space_info->full) {
3288 spin_unlock(&space_info->lock);
3292 if (!force && !should_alloc_chunk(extent_root, space_info,
3294 spin_unlock(&space_info->lock);
3297 spin_unlock(&space_info->lock);
3300 * If we have mixed data/metadata chunks we want to make sure we keep
3301 * allocating mixed chunks instead of individual chunks.
3303 if (btrfs_mixed_space_info(space_info))
3304 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3307 * if we're doing a data chunk, go ahead and make sure that
3308 * we keep a reasonable number of metadata chunks allocated in the
3311 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
3312 fs_info->data_chunk_allocations++;
3313 if (!(fs_info->data_chunk_allocations %
3314 fs_info->metadata_ratio))
3315 force_metadata_allocation(fs_info);
3318 ret = btrfs_alloc_chunk(trans, extent_root, flags);
3319 spin_lock(&space_info->lock);
3321 space_info->full = 1;
3324 space_info->force_alloc = 0;
3325 spin_unlock(&space_info->lock);
3327 mutex_unlock(&extent_root->fs_info->chunk_mutex);
3332 * shrink metadata reservation for delalloc
3334 static int shrink_delalloc(struct btrfs_trans_handle *trans,
3335 struct btrfs_root *root, u64 to_reclaim, int sync)
3337 struct btrfs_block_rsv *block_rsv;
3338 struct btrfs_space_info *space_info;
3343 int nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
3345 block_rsv = &root->fs_info->delalloc_block_rsv;
3346 space_info = block_rsv->space_info;
3349 reserved = space_info->bytes_reserved;
3354 max_reclaim = min(reserved, to_reclaim);
3357 /* have the flusher threads jump in and do some IO */
3359 nr_pages = min_t(unsigned long, nr_pages,
3360 root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT);
3361 writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages);
3363 spin_lock(&space_info->lock);
3364 if (reserved > space_info->bytes_reserved)
3365 reclaimed += reserved - space_info->bytes_reserved;
3366 reserved = space_info->bytes_reserved;
3367 spin_unlock(&space_info->lock);
3369 if (reserved == 0 || reclaimed >= max_reclaim)
3372 if (trans && trans->transaction->blocked)
3375 __set_current_state(TASK_INTERRUPTIBLE);
3376 schedule_timeout(pause);
3378 if (pause > HZ / 10)
3382 return reclaimed >= to_reclaim;
3386 * Retries tells us how many times we've called reserve_metadata_bytes. The
3387 * idea is if this is the first call (retries == 0) then we will add to our
3388 * reserved count if we can't make the allocation in order to hold our place
3389 * while we go and try and free up space. That way for retries > 1 we don't try
3390 * and add space, we just check to see if the amount of unused space is >= the
3391 * total space, meaning that our reservation is valid.
3393 * However if we don't intend to retry this reservation, pass -1 as retries so
3394 * that it short circuits this logic.
3396 static int reserve_metadata_bytes(struct btrfs_trans_handle *trans,
3397 struct btrfs_root *root,
3398 struct btrfs_block_rsv *block_rsv,
3399 u64 orig_bytes, int flush)
3401 struct btrfs_space_info *space_info = block_rsv->space_info;
3403 u64 num_bytes = orig_bytes;
3406 bool reserved = false;
3407 bool committed = false;
3414 spin_lock(&space_info->lock);
3415 unused = space_info->bytes_used + space_info->bytes_reserved +
3416 space_info->bytes_pinned + space_info->bytes_readonly +
3417 space_info->bytes_may_use;
3420 * The idea here is that we've not already over-reserved the block group
3421 * then we can go ahead and save our reservation first and then start
3422 * flushing if we need to. Otherwise if we've already overcommitted
3423 * lets start flushing stuff first and then come back and try to make
3426 if (unused <= space_info->total_bytes) {
3427 unused = space_info->total_bytes - unused;
3428 if (unused >= num_bytes) {
3430 space_info->bytes_reserved += orig_bytes;
3434 * Ok set num_bytes to orig_bytes since we aren't
3435 * overocmmitted, this way we only try and reclaim what
3438 num_bytes = orig_bytes;
3442 * Ok we're over committed, set num_bytes to the overcommitted
3443 * amount plus the amount of bytes that we need for this
3446 num_bytes = unused - space_info->total_bytes +
3447 (orig_bytes * (retries + 1));
3451 * Couldn't make our reservation, save our place so while we're trying
3452 * to reclaim space we can actually use it instead of somebody else
3453 * stealing it from us.
3455 if (ret && !reserved) {
3456 space_info->bytes_reserved += orig_bytes;
3460 spin_unlock(&space_info->lock);
3469 * We do synchronous shrinking since we don't actually unreserve
3470 * metadata until after the IO is completed.
3472 ret = shrink_delalloc(trans, root, num_bytes, 1);
3479 * So if we were overcommitted it's possible that somebody else flushed
3480 * out enough space and we simply didn't have enough space to reclaim,
3481 * so go back around and try again.
3488 spin_lock(&space_info->lock);
3490 * Not enough space to be reclaimed, don't bother committing the
3493 if (space_info->bytes_pinned < orig_bytes)
3495 spin_unlock(&space_info->lock);
3500 if (trans || committed)
3504 trans = btrfs_join_transaction(root, 1);
3507 ret = btrfs_commit_transaction(trans, root);
3516 spin_lock(&space_info->lock);
3517 space_info->bytes_reserved -= orig_bytes;
3518 spin_unlock(&space_info->lock);
3524 static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
3525 struct btrfs_root *root)
3527 struct btrfs_block_rsv *block_rsv;
3529 block_rsv = trans->block_rsv;
3531 block_rsv = root->block_rsv;
3534 block_rsv = &root->fs_info->empty_block_rsv;
3539 static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
3543 spin_lock(&block_rsv->lock);
3544 if (block_rsv->reserved >= num_bytes) {
3545 block_rsv->reserved -= num_bytes;
3546 if (block_rsv->reserved < block_rsv->size)
3547 block_rsv->full = 0;
3550 spin_unlock(&block_rsv->lock);
3554 static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
3555 u64 num_bytes, int update_size)
3557 spin_lock(&block_rsv->lock);
3558 block_rsv->reserved += num_bytes;
3560 block_rsv->size += num_bytes;
3561 else if (block_rsv->reserved >= block_rsv->size)
3562 block_rsv->full = 1;
3563 spin_unlock(&block_rsv->lock);
3566 void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv,
3567 struct btrfs_block_rsv *dest, u64 num_bytes)
3569 struct btrfs_space_info *space_info = block_rsv->space_info;
3571 spin_lock(&block_rsv->lock);
3572 if (num_bytes == (u64)-1)
3573 num_bytes = block_rsv->size;
3574 block_rsv->size -= num_bytes;
3575 if (block_rsv->reserved >= block_rsv->size) {
3576 num_bytes = block_rsv->reserved - block_rsv->size;
3577 block_rsv->reserved = block_rsv->size;
3578 block_rsv->full = 1;
3582 spin_unlock(&block_rsv->lock);
3584 if (num_bytes > 0) {
3586 block_rsv_add_bytes(dest, num_bytes, 0);
3588 spin_lock(&space_info->lock);
3589 space_info->bytes_reserved -= num_bytes;
3590 spin_unlock(&space_info->lock);
3595 static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
3596 struct btrfs_block_rsv *dst, u64 num_bytes)
3600 ret = block_rsv_use_bytes(src, num_bytes);
3604 block_rsv_add_bytes(dst, num_bytes, 1);
3608 void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
3610 memset(rsv, 0, sizeof(*rsv));
3611 spin_lock_init(&rsv->lock);
3612 atomic_set(&rsv->usage, 1);
3614 INIT_LIST_HEAD(&rsv->list);
3617 struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
3619 struct btrfs_block_rsv *block_rsv;
3620 struct btrfs_fs_info *fs_info = root->fs_info;
3622 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
3626 btrfs_init_block_rsv(block_rsv);
3627 block_rsv->space_info = __find_space_info(fs_info,
3628 BTRFS_BLOCK_GROUP_METADATA);
3632 void btrfs_free_block_rsv(struct btrfs_root *root,
3633 struct btrfs_block_rsv *rsv)
3635 if (rsv && atomic_dec_and_test(&rsv->usage)) {
3636 btrfs_block_rsv_release(root, rsv, (u64)-1);
3643 * make the block_rsv struct be able to capture freed space.
3644 * the captured space will re-add to the the block_rsv struct
3645 * after transaction commit
3647 void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info,
3648 struct btrfs_block_rsv *block_rsv)
3650 block_rsv->durable = 1;
3651 mutex_lock(&fs_info->durable_block_rsv_mutex);
3652 list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list);
3653 mutex_unlock(&fs_info->durable_block_rsv_mutex);
3656 int btrfs_block_rsv_add(struct btrfs_trans_handle *trans,
3657 struct btrfs_root *root,
3658 struct btrfs_block_rsv *block_rsv,
3666 ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, 1);
3668 block_rsv_add_bytes(block_rsv, num_bytes, 1);
3675 int btrfs_block_rsv_check(struct btrfs_trans_handle *trans,
3676 struct btrfs_root *root,
3677 struct btrfs_block_rsv *block_rsv,
3678 u64 min_reserved, int min_factor)
3681 int commit_trans = 0;
3687 spin_lock(&block_rsv->lock);
3689 num_bytes = div_factor(block_rsv->size, min_factor);
3690 if (min_reserved > num_bytes)
3691 num_bytes = min_reserved;
3693 if (block_rsv->reserved >= num_bytes) {
3696 num_bytes -= block_rsv->reserved;
3697 if (block_rsv->durable &&
3698 block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes)
3701 spin_unlock(&block_rsv->lock);
3705 if (block_rsv->refill_used) {
3706 ret = reserve_metadata_bytes(trans, root, block_rsv,
3709 block_rsv_add_bytes(block_rsv, num_bytes, 0);
3718 trans = btrfs_join_transaction(root, 1);
3719 BUG_ON(IS_ERR(trans));
3720 ret = btrfs_commit_transaction(trans, root);
3725 printk(KERN_INFO"block_rsv size %llu reserved %llu freed %llu %llu\n",
3726 block_rsv->size, block_rsv->reserved,
3727 block_rsv->freed[0], block_rsv->freed[1]);
3732 int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
3733 struct btrfs_block_rsv *dst_rsv,
3736 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3739 void btrfs_block_rsv_release(struct btrfs_root *root,
3740 struct btrfs_block_rsv *block_rsv,
3743 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3744 if (global_rsv->full || global_rsv == block_rsv ||
3745 block_rsv->space_info != global_rsv->space_info)
3747 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
3751 * helper to calculate size of global block reservation.
3752 * the desired value is sum of space used by extent tree,
3753 * checksum tree and root tree
3755 static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
3757 struct btrfs_space_info *sinfo;
3761 int csum_size = btrfs_super_csum_size(&fs_info->super_copy);
3764 * per tree used space accounting can be inaccuracy, so we
3767 spin_lock(&fs_info->extent_root->accounting_lock);
3768 num_bytes = btrfs_root_used(&fs_info->extent_root->root_item);
3769 spin_unlock(&fs_info->extent_root->accounting_lock);
3771 spin_lock(&fs_info->csum_root->accounting_lock);
3772 num_bytes += btrfs_root_used(&fs_info->csum_root->root_item);
3773 spin_unlock(&fs_info->csum_root->accounting_lock);
3775 spin_lock(&fs_info->tree_root->accounting_lock);
3776 num_bytes += btrfs_root_used(&fs_info->tree_root->root_item);
3777 spin_unlock(&fs_info->tree_root->accounting_lock);
3779 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
3780 spin_lock(&sinfo->lock);
3781 data_used = sinfo->bytes_used;
3782 spin_unlock(&sinfo->lock);
3784 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3785 spin_lock(&sinfo->lock);
3786 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
3788 meta_used = sinfo->bytes_used;
3789 spin_unlock(&sinfo->lock);
3791 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
3793 num_bytes += div64_u64(data_used + meta_used, 50);
3795 if (num_bytes * 3 > meta_used)
3796 num_bytes = div64_u64(meta_used, 3);
3798 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
3801 static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
3803 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3804 struct btrfs_space_info *sinfo = block_rsv->space_info;
3807 num_bytes = calc_global_metadata_size(fs_info);
3809 spin_lock(&block_rsv->lock);
3810 spin_lock(&sinfo->lock);
3812 block_rsv->size = num_bytes;
3814 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
3815 sinfo->bytes_reserved + sinfo->bytes_readonly +
3816 sinfo->bytes_may_use;
3818 if (sinfo->total_bytes > num_bytes) {
3819 num_bytes = sinfo->total_bytes - num_bytes;
3820 block_rsv->reserved += num_bytes;
3821 sinfo->bytes_reserved += num_bytes;
3824 if (block_rsv->reserved >= block_rsv->size) {
3825 num_bytes = block_rsv->reserved - block_rsv->size;
3826 sinfo->bytes_reserved -= num_bytes;
3827 block_rsv->reserved = block_rsv->size;
3828 block_rsv->full = 1;
3831 printk(KERN_INFO"global block rsv size %llu reserved %llu\n",
3832 block_rsv->size, block_rsv->reserved);
3834 spin_unlock(&sinfo->lock);
3835 spin_unlock(&block_rsv->lock);
3838 static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
3840 struct btrfs_space_info *space_info;
3842 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3843 fs_info->chunk_block_rsv.space_info = space_info;
3844 fs_info->chunk_block_rsv.priority = 10;
3846 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3847 fs_info->global_block_rsv.space_info = space_info;
3848 fs_info->global_block_rsv.priority = 10;
3849 fs_info->global_block_rsv.refill_used = 1;
3850 fs_info->delalloc_block_rsv.space_info = space_info;
3851 fs_info->trans_block_rsv.space_info = space_info;
3852 fs_info->empty_block_rsv.space_info = space_info;
3853 fs_info->empty_block_rsv.priority = 10;
3855 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
3856 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
3857 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
3858 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
3859 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
3861 btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv);
3863 btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv);
3865 update_global_block_rsv(fs_info);
3868 static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
3870 block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1);
3871 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
3872 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
3873 WARN_ON(fs_info->trans_block_rsv.size > 0);
3874 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
3875 WARN_ON(fs_info->chunk_block_rsv.size > 0);
3876 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
3879 static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items)
3881 return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) *
3885 int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
3886 struct btrfs_root *root,
3892 if (num_items == 0 || root->fs_info->chunk_root == root)
3895 num_bytes = calc_trans_metadata_size(root, num_items);
3896 ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv,
3899 trans->bytes_reserved += num_bytes;
3900 trans->block_rsv = &root->fs_info->trans_block_rsv;
3905 void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
3906 struct btrfs_root *root)
3908 if (!trans->bytes_reserved)
3911 BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
3912 btrfs_block_rsv_release(root, trans->block_rsv,
3913 trans->bytes_reserved);
3914 trans->bytes_reserved = 0;
3917 int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
3918 struct inode *inode)
3920 struct btrfs_root *root = BTRFS_I(inode)->root;
3921 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3922 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
3925 * one for deleting orphan item, one for updating inode and
3926 * two for calling btrfs_truncate_inode_items.
3928 * btrfs_truncate_inode_items is a delete operation, it frees
3929 * more space than it uses in most cases. So two units of
3930 * metadata space should be enough for calling it many times.
3931 * If all of the metadata space is used, we can commit
3932 * transaction and use space it freed.
3934 u64 num_bytes = calc_trans_metadata_size(root, 4);
3935 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3938 void btrfs_orphan_release_metadata(struct inode *inode)
3940 struct btrfs_root *root = BTRFS_I(inode)->root;
3941 u64 num_bytes = calc_trans_metadata_size(root, 4);
3942 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
3945 int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
3946 struct btrfs_pending_snapshot *pending)
3948 struct btrfs_root *root = pending->root;
3949 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3950 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
3952 * two for root back/forward refs, two for directory entries
3953 * and one for root of the snapshot.
3955 u64 num_bytes = calc_trans_metadata_size(root, 5);
3956 dst_rsv->space_info = src_rsv->space_info;
3957 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3960 static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes)
3962 return num_bytes >>= 3;
3965 int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
3967 struct btrfs_root *root = BTRFS_I(inode)->root;
3968 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
3973 if (btrfs_transaction_in_commit(root->fs_info))
3974 schedule_timeout(1);
3976 num_bytes = ALIGN(num_bytes, root->sectorsize);
3978 spin_lock(&BTRFS_I(inode)->accounting_lock);
3979 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1;
3980 if (nr_extents > BTRFS_I(inode)->reserved_extents) {
3981 nr_extents -= BTRFS_I(inode)->reserved_extents;
3982 to_reserve = calc_trans_metadata_size(root, nr_extents);
3987 spin_unlock(&BTRFS_I(inode)->accounting_lock);
3989 to_reserve += calc_csum_metadata_size(inode, num_bytes);
3990 ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1);
3994 spin_lock(&BTRFS_I(inode)->accounting_lock);
3995 BTRFS_I(inode)->reserved_extents += nr_extents;
3996 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
3997 spin_unlock(&BTRFS_I(inode)->accounting_lock);
3999 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4001 if (block_rsv->size > 512 * 1024 * 1024)
4002 shrink_delalloc(NULL, root, to_reserve, 0);
4007 void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4009 struct btrfs_root *root = BTRFS_I(inode)->root;
4013 num_bytes = ALIGN(num_bytes, root->sectorsize);
4014 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
4016 spin_lock(&BTRFS_I(inode)->accounting_lock);
4017 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents);
4018 if (nr_extents < BTRFS_I(inode)->reserved_extents) {
4019 nr_extents = BTRFS_I(inode)->reserved_extents - nr_extents;
4020 BTRFS_I(inode)->reserved_extents -= nr_extents;
4024 spin_unlock(&BTRFS_I(inode)->accounting_lock);
4026 to_free = calc_csum_metadata_size(inode, num_bytes);
4028 to_free += calc_trans_metadata_size(root, nr_extents);
4030 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4034 int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4038 ret = btrfs_check_data_free_space(inode, num_bytes);
4042 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4044 btrfs_free_reserved_data_space(inode, num_bytes);
4051 void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4053 btrfs_delalloc_release_metadata(inode, num_bytes);
4054 btrfs_free_reserved_data_space(inode, num_bytes);
4057 static int update_block_group(struct btrfs_trans_handle *trans,
4058 struct btrfs_root *root,
4059 u64 bytenr, u64 num_bytes, int alloc)
4061 struct btrfs_block_group_cache *cache = NULL;
4062 struct btrfs_fs_info *info = root->fs_info;
4063 u64 total = num_bytes;
4068 /* block accounting for super block */
4069 spin_lock(&info->delalloc_lock);
4070 old_val = btrfs_super_bytes_used(&info->super_copy);
4072 old_val += num_bytes;
4074 old_val -= num_bytes;
4075 btrfs_set_super_bytes_used(&info->super_copy, old_val);
4076 spin_unlock(&info->delalloc_lock);
4079 cache = btrfs_lookup_block_group(info, bytenr);
4082 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4083 BTRFS_BLOCK_GROUP_RAID1 |
4084 BTRFS_BLOCK_GROUP_RAID10))
4089 * If this block group has free space cache written out, we
4090 * need to make sure to load it if we are removing space. This
4091 * is because we need the unpinning stage to actually add the
4092 * space back to the block group, otherwise we will leak space.
4094 if (!alloc && cache->cached == BTRFS_CACHE_NO)
4095 cache_block_group(cache, trans, NULL, 1);
4097 byte_in_group = bytenr - cache->key.objectid;
4098 WARN_ON(byte_in_group > cache->key.offset);
4100 spin_lock(&cache->space_info->lock);
4101 spin_lock(&cache->lock);
4103 if (btrfs_super_cache_generation(&info->super_copy) != 0 &&
4104 cache->disk_cache_state < BTRFS_DC_CLEAR)
4105 cache->disk_cache_state = BTRFS_DC_CLEAR;
4108 old_val = btrfs_block_group_used(&cache->item);
4109 num_bytes = min(total, cache->key.offset - byte_in_group);
4111 old_val += num_bytes;
4112 btrfs_set_block_group_used(&cache->item, old_val);
4113 cache->reserved -= num_bytes;
4114 cache->space_info->bytes_reserved -= num_bytes;
4115 cache->space_info->bytes_used += num_bytes;
4116 cache->space_info->disk_used += num_bytes * factor;
4117 spin_unlock(&cache->lock);
4118 spin_unlock(&cache->space_info->lock);
4120 old_val -= num_bytes;
4121 btrfs_set_block_group_used(&cache->item, old_val);
4122 cache->pinned += num_bytes;
4123 cache->space_info->bytes_pinned += num_bytes;
4124 cache->space_info->bytes_used -= num_bytes;
4125 cache->space_info->disk_used -= num_bytes * factor;
4126 spin_unlock(&cache->lock);
4127 spin_unlock(&cache->space_info->lock);
4129 set_extent_dirty(info->pinned_extents,
4130 bytenr, bytenr + num_bytes - 1,
4131 GFP_NOFS | __GFP_NOFAIL);
4133 btrfs_put_block_group(cache);
4135 bytenr += num_bytes;
4140 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4142 struct btrfs_block_group_cache *cache;
4145 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4149 bytenr = cache->key.objectid;
4150 btrfs_put_block_group(cache);
4155 static int pin_down_extent(struct btrfs_root *root,
4156 struct btrfs_block_group_cache *cache,
4157 u64 bytenr, u64 num_bytes, int reserved)
4159 spin_lock(&cache->space_info->lock);
4160 spin_lock(&cache->lock);
4161 cache->pinned += num_bytes;
4162 cache->space_info->bytes_pinned += num_bytes;
4164 cache->reserved -= num_bytes;
4165 cache->space_info->bytes_reserved -= num_bytes;
4167 spin_unlock(&cache->lock);
4168 spin_unlock(&cache->space_info->lock);
4170 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4171 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
4176 * this function must be called within transaction
4178 int btrfs_pin_extent(struct btrfs_root *root,
4179 u64 bytenr, u64 num_bytes, int reserved)
4181 struct btrfs_block_group_cache *cache;
4183 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
4186 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4188 btrfs_put_block_group(cache);
4193 * update size of reserved extents. this function may return -EAGAIN
4194 * if 'reserve' is true or 'sinfo' is false.
4196 static int update_reserved_bytes(struct btrfs_block_group_cache *cache,
4197 u64 num_bytes, int reserve, int sinfo)
4201 struct btrfs_space_info *space_info = cache->space_info;
4202 spin_lock(&space_info->lock);
4203 spin_lock(&cache->lock);
4208 cache->reserved += num_bytes;
4209 space_info->bytes_reserved += num_bytes;
4213 space_info->bytes_readonly += num_bytes;
4214 cache->reserved -= num_bytes;
4215 space_info->bytes_reserved -= num_bytes;
4217 spin_unlock(&cache->lock);
4218 spin_unlock(&space_info->lock);
4220 spin_lock(&cache->lock);
4225 cache->reserved += num_bytes;
4227 cache->reserved -= num_bytes;
4229 spin_unlock(&cache->lock);
4234 int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
4235 struct btrfs_root *root)
4237 struct btrfs_fs_info *fs_info = root->fs_info;
4238 struct btrfs_caching_control *next;
4239 struct btrfs_caching_control *caching_ctl;
4240 struct btrfs_block_group_cache *cache;
4242 down_write(&fs_info->extent_commit_sem);
4244 list_for_each_entry_safe(caching_ctl, next,
4245 &fs_info->caching_block_groups, list) {
4246 cache = caching_ctl->block_group;
4247 if (block_group_cache_done(cache)) {
4248 cache->last_byte_to_unpin = (u64)-1;
4249 list_del_init(&caching_ctl->list);
4250 put_caching_control(caching_ctl);
4252 cache->last_byte_to_unpin = caching_ctl->progress;
4256 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4257 fs_info->pinned_extents = &fs_info->freed_extents[1];
4259 fs_info->pinned_extents = &fs_info->freed_extents[0];
4261 up_write(&fs_info->extent_commit_sem);
4263 update_global_block_rsv(fs_info);
4267 static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
4269 struct btrfs_fs_info *fs_info = root->fs_info;
4270 struct btrfs_block_group_cache *cache = NULL;
4273 while (start <= end) {
4275 start >= cache->key.objectid + cache->key.offset) {
4277 btrfs_put_block_group(cache);
4278 cache = btrfs_lookup_block_group(fs_info, start);
4282 len = cache->key.objectid + cache->key.offset - start;
4283 len = min(len, end + 1 - start);
4285 if (start < cache->last_byte_to_unpin) {
4286 len = min(len, cache->last_byte_to_unpin - start);
4287 btrfs_add_free_space(cache, start, len);
4292 spin_lock(&cache->space_info->lock);
4293 spin_lock(&cache->lock);
4294 cache->pinned -= len;
4295 cache->space_info->bytes_pinned -= len;
4297 cache->space_info->bytes_readonly += len;
4298 } else if (cache->reserved_pinned > 0) {
4299 len = min(len, cache->reserved_pinned);
4300 cache->reserved_pinned -= len;
4301 cache->space_info->bytes_reserved += len;
4303 spin_unlock(&cache->lock);
4304 spin_unlock(&cache->space_info->lock);
4308 btrfs_put_block_group(cache);
4312 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
4313 struct btrfs_root *root)
4315 struct btrfs_fs_info *fs_info = root->fs_info;
4316 struct extent_io_tree *unpin;
4317 struct btrfs_block_rsv *block_rsv;
4318 struct btrfs_block_rsv *next_rsv;
4324 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4325 unpin = &fs_info->freed_extents[1];
4327 unpin = &fs_info->freed_extents[0];
4330 ret = find_first_extent_bit(unpin, 0, &start, &end,
4335 ret = btrfs_discard_extent(root, start, end + 1 - start);
4337 clear_extent_dirty(unpin, start, end, GFP_NOFS);
4338 unpin_extent_range(root, start, end);
4342 mutex_lock(&fs_info->durable_block_rsv_mutex);
4343 list_for_each_entry_safe(block_rsv, next_rsv,
4344 &fs_info->durable_block_rsv_list, list) {
4346 idx = trans->transid & 0x1;
4347 if (block_rsv->freed[idx] > 0) {
4348 block_rsv_add_bytes(block_rsv,
4349 block_rsv->freed[idx], 0);
4350 block_rsv->freed[idx] = 0;
4352 if (atomic_read(&block_rsv->usage) == 0) {
4353 btrfs_block_rsv_release(root, block_rsv, (u64)-1);
4355 if (block_rsv->freed[0] == 0 &&
4356 block_rsv->freed[1] == 0) {
4357 list_del_init(&block_rsv->list);
4361 btrfs_block_rsv_release(root, block_rsv, 0);
4364 mutex_unlock(&fs_info->durable_block_rsv_mutex);
4369 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4370 struct btrfs_root *root,
4371 u64 bytenr, u64 num_bytes, u64 parent,
4372 u64 root_objectid, u64 owner_objectid,
4373 u64 owner_offset, int refs_to_drop,
4374 struct btrfs_delayed_extent_op *extent_op)
4376 struct btrfs_key key;
4377 struct btrfs_path *path;
4378 struct btrfs_fs_info *info = root->fs_info;
4379 struct btrfs_root *extent_root = info->extent_root;
4380 struct extent_buffer *leaf;
4381 struct btrfs_extent_item *ei;
4382 struct btrfs_extent_inline_ref *iref;
4385 int extent_slot = 0;
4386 int found_extent = 0;
4391 path = btrfs_alloc_path();
4396 path->leave_spinning = 1;
4398 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
4399 BUG_ON(!is_data && refs_to_drop != 1);
4401 ret = lookup_extent_backref(trans, extent_root, path, &iref,
4402 bytenr, num_bytes, parent,
4403 root_objectid, owner_objectid,
4406 extent_slot = path->slots[0];
4407 while (extent_slot >= 0) {
4408 btrfs_item_key_to_cpu(path->nodes[0], &key,
4410 if (key.objectid != bytenr)
4412 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
4413 key.offset == num_bytes) {
4417 if (path->slots[0] - extent_slot > 5)
4421 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4422 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
4423 if (found_extent && item_size < sizeof(*ei))
4426 if (!found_extent) {
4428 ret = remove_extent_backref(trans, extent_root, path,
4432 btrfs_release_path(extent_root, path);
4433 path->leave_spinning = 1;
4435 key.objectid = bytenr;
4436 key.type = BTRFS_EXTENT_ITEM_KEY;
4437 key.offset = num_bytes;
4439 ret = btrfs_search_slot(trans, extent_root,
4442 printk(KERN_ERR "umm, got %d back from search"
4443 ", was looking for %llu\n", ret,
4444 (unsigned long long)bytenr);
4445 btrfs_print_leaf(extent_root, path->nodes[0]);
4448 extent_slot = path->slots[0];
4451 btrfs_print_leaf(extent_root, path->nodes[0]);
4453 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
4454 "parent %llu root %llu owner %llu offset %llu\n",
4455 (unsigned long long)bytenr,
4456 (unsigned long long)parent,
4457 (unsigned long long)root_objectid,
4458 (unsigned long long)owner_objectid,
4459 (unsigned long long)owner_offset);
4462 leaf = path->nodes[0];
4463 item_size = btrfs_item_size_nr(leaf, extent_slot);
4464 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4465 if (item_size < sizeof(*ei)) {
4466 BUG_ON(found_extent || extent_slot != path->slots[0]);
4467 ret = convert_extent_item_v0(trans, extent_root, path,
4471 btrfs_release_path(extent_root, path);
4472 path->leave_spinning = 1;
4474 key.objectid = bytenr;
4475 key.type = BTRFS_EXTENT_ITEM_KEY;
4476 key.offset = num_bytes;
4478 ret = btrfs_search_slot(trans, extent_root, &key, path,
4481 printk(KERN_ERR "umm, got %d back from search"
4482 ", was looking for %llu\n", ret,
4483 (unsigned long long)bytenr);
4484 btrfs_print_leaf(extent_root, path->nodes[0]);
4487 extent_slot = path->slots[0];
4488 leaf = path->nodes[0];
4489 item_size = btrfs_item_size_nr(leaf, extent_slot);
4492 BUG_ON(item_size < sizeof(*ei));
4493 ei = btrfs_item_ptr(leaf, extent_slot,
4494 struct btrfs_extent_item);
4495 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4496 struct btrfs_tree_block_info *bi;
4497 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
4498 bi = (struct btrfs_tree_block_info *)(ei + 1);
4499 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
4502 refs = btrfs_extent_refs(leaf, ei);
4503 BUG_ON(refs < refs_to_drop);
4504 refs -= refs_to_drop;
4508 __run_delayed_extent_op(extent_op, leaf, ei);
4510 * In the case of inline back ref, reference count will
4511 * be updated by remove_extent_backref
4514 BUG_ON(!found_extent);
4516 btrfs_set_extent_refs(leaf, ei, refs);
4517 btrfs_mark_buffer_dirty(leaf);
4520 ret = remove_extent_backref(trans, extent_root, path,
4527 BUG_ON(is_data && refs_to_drop !=
4528 extent_data_ref_count(root, path, iref));
4530 BUG_ON(path->slots[0] != extent_slot);
4532 BUG_ON(path->slots[0] != extent_slot + 1);
4533 path->slots[0] = extent_slot;
4538 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
4541 btrfs_release_path(extent_root, path);
4544 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
4547 invalidate_mapping_pages(info->btree_inode->i_mapping,
4548 bytenr >> PAGE_CACHE_SHIFT,
4549 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
4552 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
4555 btrfs_free_path(path);
4560 * when we free an block, it is possible (and likely) that we free the last
4561 * delayed ref for that extent as well. This searches the delayed ref tree for
4562 * a given extent, and if there are no other delayed refs to be processed, it
4563 * removes it from the tree.
4565 static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
4566 struct btrfs_root *root, u64 bytenr)
4568 struct btrfs_delayed_ref_head *head;
4569 struct btrfs_delayed_ref_root *delayed_refs;
4570 struct btrfs_delayed_ref_node *ref;
4571 struct rb_node *node;
4574 delayed_refs = &trans->transaction->delayed_refs;
4575 spin_lock(&delayed_refs->lock);
4576 head = btrfs_find_delayed_ref_head(trans, bytenr);
4580 node = rb_prev(&head->node.rb_node);
4584 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
4586 /* there are still entries for this ref, we can't drop it */
4587 if (ref->bytenr == bytenr)
4590 if (head->extent_op) {
4591 if (!head->must_insert_reserved)
4593 kfree(head->extent_op);
4594 head->extent_op = NULL;
4598 * waiting for the lock here would deadlock. If someone else has it
4599 * locked they are already in the process of dropping it anyway
4601 if (!mutex_trylock(&head->mutex))
4605 * at this point we have a head with no other entries. Go
4606 * ahead and process it.
4608 head->node.in_tree = 0;
4609 rb_erase(&head->node.rb_node, &delayed_refs->root);
4611 delayed_refs->num_entries--;
4614 * we don't take a ref on the node because we're removing it from the
4615 * tree, so we just steal the ref the tree was holding.
4617 delayed_refs->num_heads--;
4618 if (list_empty(&head->cluster))
4619 delayed_refs->num_heads_ready--;
4621 list_del_init(&head->cluster);
4622 spin_unlock(&delayed_refs->lock);
4624 BUG_ON(head->extent_op);
4625 if (head->must_insert_reserved)
4628 mutex_unlock(&head->mutex);
4629 btrfs_put_delayed_ref(&head->node);
4632 spin_unlock(&delayed_refs->lock);
4636 void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4637 struct btrfs_root *root,
4638 struct extent_buffer *buf,
4639 u64 parent, int last_ref)
4641 struct btrfs_block_rsv *block_rsv;
4642 struct btrfs_block_group_cache *cache = NULL;
4645 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4646 ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len,
4647 parent, root->root_key.objectid,
4648 btrfs_header_level(buf),
4649 BTRFS_DROP_DELAYED_REF, NULL);
4656 block_rsv = get_block_rsv(trans, root);
4657 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
4658 if (block_rsv->space_info != cache->space_info)
4661 if (btrfs_header_generation(buf) == trans->transid) {
4662 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4663 ret = check_ref_cleanup(trans, root, buf->start);
4668 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
4669 pin_down_extent(root, cache, buf->start, buf->len, 1);
4673 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
4675 btrfs_add_free_space(cache, buf->start, buf->len);
4676 ret = update_reserved_bytes(cache, buf->len, 0, 0);
4677 if (ret == -EAGAIN) {
4678 /* block group became read-only */
4679 update_reserved_bytes(cache, buf->len, 0, 1);
4684 spin_lock(&block_rsv->lock);
4685 if (block_rsv->reserved < block_rsv->size) {
4686 block_rsv->reserved += buf->len;
4689 spin_unlock(&block_rsv->lock);
4692 spin_lock(&cache->space_info->lock);
4693 cache->space_info->bytes_reserved -= buf->len;
4694 spin_unlock(&cache->space_info->lock);
4699 if (block_rsv->durable && !cache->ro) {
4701 spin_lock(&cache->lock);
4703 cache->reserved_pinned += buf->len;
4706 spin_unlock(&cache->lock);
4709 spin_lock(&block_rsv->lock);
4710 block_rsv->freed[trans->transid & 0x1] += buf->len;
4711 spin_unlock(&block_rsv->lock);
4715 btrfs_put_block_group(cache);
4718 int btrfs_free_extent(struct btrfs_trans_handle *trans,
4719 struct btrfs_root *root,
4720 u64 bytenr, u64 num_bytes, u64 parent,
4721 u64 root_objectid, u64 owner, u64 offset)
4726 * tree log blocks never actually go into the extent allocation
4727 * tree, just update pinning info and exit early.
4729 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4730 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
4731 /* unlocks the pinned mutex */
4732 btrfs_pin_extent(root, bytenr, num_bytes, 1);
4734 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4735 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4736 parent, root_objectid, (int)owner,
4737 BTRFS_DROP_DELAYED_REF, NULL);
4740 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4741 parent, root_objectid, owner,
4742 offset, BTRFS_DROP_DELAYED_REF, NULL);
4748 static u64 stripe_align(struct btrfs_root *root, u64 val)
4750 u64 mask = ((u64)root->stripesize - 1);
4751 u64 ret = (val + mask) & ~mask;
4756 * when we wait for progress in the block group caching, its because
4757 * our allocation attempt failed at least once. So, we must sleep
4758 * and let some progress happen before we try again.
4760 * This function will sleep at least once waiting for new free space to
4761 * show up, and then it will check the block group free space numbers
4762 * for our min num_bytes. Another option is to have it go ahead
4763 * and look in the rbtree for a free extent of a given size, but this
4767 wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4770 struct btrfs_caching_control *caching_ctl;
4773 caching_ctl = get_caching_control(cache);
4777 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
4778 (cache->free_space >= num_bytes));
4780 put_caching_control(caching_ctl);
4785 wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4787 struct btrfs_caching_control *caching_ctl;
4790 caching_ctl = get_caching_control(cache);
4794 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4796 put_caching_control(caching_ctl);
4800 static int get_block_group_index(struct btrfs_block_group_cache *cache)
4803 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4805 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4807 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4809 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4816 enum btrfs_loop_type {
4817 LOOP_FIND_IDEAL = 0,
4818 LOOP_CACHING_NOWAIT = 1,
4819 LOOP_CACHING_WAIT = 2,
4820 LOOP_ALLOC_CHUNK = 3,
4821 LOOP_NO_EMPTY_SIZE = 4,
4825 * walks the btree of allocated extents and find a hole of a given size.
4826 * The key ins is changed to record the hole:
4827 * ins->objectid == block start
4828 * ins->flags = BTRFS_EXTENT_ITEM_KEY
4829 * ins->offset == number of blocks
4830 * Any available blocks before search_start are skipped.
4832 static noinline int find_free_extent(struct btrfs_trans_handle *trans,
4833 struct btrfs_root *orig_root,
4834 u64 num_bytes, u64 empty_size,
4835 u64 search_start, u64 search_end,
4836 u64 hint_byte, struct btrfs_key *ins,
4840 struct btrfs_root *root = orig_root->fs_info->extent_root;
4841 struct btrfs_free_cluster *last_ptr = NULL;
4842 struct btrfs_block_group_cache *block_group = NULL;
4843 int empty_cluster = 2 * 1024 * 1024;
4844 int allowed_chunk_alloc = 0;
4845 int done_chunk_alloc = 0;
4846 struct btrfs_space_info *space_info;
4847 int last_ptr_loop = 0;
4850 bool found_uncached_bg = false;
4851 bool failed_cluster_refill = false;
4852 bool failed_alloc = false;
4853 bool use_cluster = true;
4854 u64 ideal_cache_percent = 0;
4855 u64 ideal_cache_offset = 0;
4857 WARN_ON(num_bytes < root->sectorsize);
4858 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
4862 space_info = __find_space_info(root->fs_info, data);
4864 printk(KERN_ERR "No space info for %d\n", data);
4869 * If the space info is for both data and metadata it means we have a
4870 * small filesystem and we can't use the clustering stuff.
4872 if (btrfs_mixed_space_info(space_info))
4873 use_cluster = false;
4875 if (orig_root->ref_cows || empty_size)
4876 allowed_chunk_alloc = 1;
4878 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
4879 last_ptr = &root->fs_info->meta_alloc_cluster;
4880 if (!btrfs_test_opt(root, SSD))
4881 empty_cluster = 64 * 1024;
4884 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
4885 btrfs_test_opt(root, SSD)) {
4886 last_ptr = &root->fs_info->data_alloc_cluster;
4890 spin_lock(&last_ptr->lock);
4891 if (last_ptr->block_group)
4892 hint_byte = last_ptr->window_start;
4893 spin_unlock(&last_ptr->lock);
4896 search_start = max(search_start, first_logical_byte(root, 0));
4897 search_start = max(search_start, hint_byte);
4902 if (search_start == hint_byte) {
4904 block_group = btrfs_lookup_block_group(root->fs_info,
4907 * we don't want to use the block group if it doesn't match our
4908 * allocation bits, or if its not cached.
4910 * However if we are re-searching with an ideal block group
4911 * picked out then we don't care that the block group is cached.
4913 if (block_group && block_group_bits(block_group, data) &&
4914 (block_group->cached != BTRFS_CACHE_NO ||
4915 search_start == ideal_cache_offset)) {
4916 down_read(&space_info->groups_sem);
4917 if (list_empty(&block_group->list) ||
4920 * someone is removing this block group,
4921 * we can't jump into the have_block_group
4922 * target because our list pointers are not
4925 btrfs_put_block_group(block_group);
4926 up_read(&space_info->groups_sem);
4928 index = get_block_group_index(block_group);
4929 goto have_block_group;
4931 } else if (block_group) {
4932 btrfs_put_block_group(block_group);
4936 down_read(&space_info->groups_sem);
4937 list_for_each_entry(block_group, &space_info->block_groups[index],
4942 btrfs_get_block_group(block_group);
4943 search_start = block_group->key.objectid;
4946 * this can happen if we end up cycling through all the
4947 * raid types, but we want to make sure we only allocate
4948 * for the proper type.
4950 if (!block_group_bits(block_group, data)) {
4951 u64 extra = BTRFS_BLOCK_GROUP_DUP |
4952 BTRFS_BLOCK_GROUP_RAID1 |
4953 BTRFS_BLOCK_GROUP_RAID10;
4956 * if they asked for extra copies and this block group
4957 * doesn't provide them, bail. This does allow us to
4958 * fill raid0 from raid1.
4960 if ((data & extra) && !(block_group->flags & extra))
4965 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
4968 ret = cache_block_group(block_group, trans,
4970 if (block_group->cached == BTRFS_CACHE_FINISHED)
4971 goto have_block_group;
4973 free_percent = btrfs_block_group_used(&block_group->item);
4974 free_percent *= 100;
4975 free_percent = div64_u64(free_percent,
4976 block_group->key.offset);
4977 free_percent = 100 - free_percent;
4978 if (free_percent > ideal_cache_percent &&
4979 likely(!block_group->ro)) {
4980 ideal_cache_offset = block_group->key.objectid;
4981 ideal_cache_percent = free_percent;
4985 * We only want to start kthread caching if we are at
4986 * the point where we will wait for caching to make
4987 * progress, or if our ideal search is over and we've
4988 * found somebody to start caching.
4990 if (loop > LOOP_CACHING_NOWAIT ||
4991 (loop > LOOP_FIND_IDEAL &&
4992 atomic_read(&space_info->caching_threads) < 2)) {
4993 ret = cache_block_group(block_group, trans,
4997 found_uncached_bg = true;
5000 * If loop is set for cached only, try the next block
5003 if (loop == LOOP_FIND_IDEAL)
5007 cached = block_group_cache_done(block_group);
5008 if (unlikely(!cached))
5009 found_uncached_bg = true;
5011 if (unlikely(block_group->ro))
5015 * Ok we want to try and use the cluster allocator, so lets look
5016 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
5017 * have tried the cluster allocator plenty of times at this
5018 * point and not have found anything, so we are likely way too
5019 * fragmented for the clustering stuff to find anything, so lets
5020 * just skip it and let the allocator find whatever block it can
5023 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
5025 * the refill lock keeps out other
5026 * people trying to start a new cluster
5028 spin_lock(&last_ptr->refill_lock);
5029 if (last_ptr->block_group &&
5030 (last_ptr->block_group->ro ||
5031 !block_group_bits(last_ptr->block_group, data))) {
5033 goto refill_cluster;
5036 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
5037 num_bytes, search_start);
5039 /* we have a block, we're done */
5040 spin_unlock(&last_ptr->refill_lock);
5044 spin_lock(&last_ptr->lock);
5046 * whoops, this cluster doesn't actually point to
5047 * this block group. Get a ref on the block
5048 * group is does point to and try again
5050 if (!last_ptr_loop && last_ptr->block_group &&
5051 last_ptr->block_group != block_group) {
5053 btrfs_put_block_group(block_group);
5054 block_group = last_ptr->block_group;
5055 btrfs_get_block_group(block_group);
5056 spin_unlock(&last_ptr->lock);
5057 spin_unlock(&last_ptr->refill_lock);
5060 search_start = block_group->key.objectid;
5062 * we know this block group is properly
5063 * in the list because
5064 * btrfs_remove_block_group, drops the
5065 * cluster before it removes the block
5066 * group from the list
5068 goto have_block_group;
5070 spin_unlock(&last_ptr->lock);
5073 * this cluster didn't work out, free it and
5076 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5080 /* allocate a cluster in this block group */
5081 ret = btrfs_find_space_cluster(trans, root,
5082 block_group, last_ptr,
5084 empty_cluster + empty_size);
5087 * now pull our allocation out of this
5090 offset = btrfs_alloc_from_cluster(block_group,
5091 last_ptr, num_bytes,
5094 /* we found one, proceed */
5095 spin_unlock(&last_ptr->refill_lock);
5098 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5099 && !failed_cluster_refill) {
5100 spin_unlock(&last_ptr->refill_lock);
5102 failed_cluster_refill = true;
5103 wait_block_group_cache_progress(block_group,
5104 num_bytes + empty_cluster + empty_size);
5105 goto have_block_group;
5109 * at this point we either didn't find a cluster
5110 * or we weren't able to allocate a block from our
5111 * cluster. Free the cluster we've been trying
5112 * to use, and go to the next block group
5114 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5115 spin_unlock(&last_ptr->refill_lock);
5119 offset = btrfs_find_space_for_alloc(block_group, search_start,
5120 num_bytes, empty_size);
5122 * If we didn't find a chunk, and we haven't failed on this
5123 * block group before, and this block group is in the middle of
5124 * caching and we are ok with waiting, then go ahead and wait
5125 * for progress to be made, and set failed_alloc to true.
5127 * If failed_alloc is true then we've already waited on this
5128 * block group once and should move on to the next block group.
5130 if (!offset && !failed_alloc && !cached &&
5131 loop > LOOP_CACHING_NOWAIT) {
5132 wait_block_group_cache_progress(block_group,
5133 num_bytes + empty_size);
5134 failed_alloc = true;
5135 goto have_block_group;
5136 } else if (!offset) {
5140 search_start = stripe_align(root, offset);
5141 /* move on to the next group */
5142 if (search_start + num_bytes >= search_end) {
5143 btrfs_add_free_space(block_group, offset, num_bytes);
5147 /* move on to the next group */
5148 if (search_start + num_bytes >
5149 block_group->key.objectid + block_group->key.offset) {
5150 btrfs_add_free_space(block_group, offset, num_bytes);
5154 ins->objectid = search_start;
5155 ins->offset = num_bytes;
5157 if (offset < search_start)
5158 btrfs_add_free_space(block_group, offset,
5159 search_start - offset);
5160 BUG_ON(offset > search_start);
5162 ret = update_reserved_bytes(block_group, num_bytes, 1,
5163 (data & BTRFS_BLOCK_GROUP_DATA));
5164 if (ret == -EAGAIN) {
5165 btrfs_add_free_space(block_group, offset, num_bytes);
5169 /* we are all good, lets return */
5170 ins->objectid = search_start;
5171 ins->offset = num_bytes;
5173 if (offset < search_start)
5174 btrfs_add_free_space(block_group, offset,
5175 search_start - offset);
5176 BUG_ON(offset > search_start);
5179 failed_cluster_refill = false;
5180 failed_alloc = false;
5181 BUG_ON(index != get_block_group_index(block_group));
5182 btrfs_put_block_group(block_group);
5184 up_read(&space_info->groups_sem);
5186 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5189 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5190 * for them to make caching progress. Also
5191 * determine the best possible bg to cache
5192 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5193 * caching kthreads as we move along
5194 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5195 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5196 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5199 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
5200 (found_uncached_bg || empty_size || empty_cluster ||
5201 allowed_chunk_alloc)) {
5203 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
5204 found_uncached_bg = false;
5206 if (!ideal_cache_percent &&
5207 atomic_read(&space_info->caching_threads))
5211 * 1 of the following 2 things have happened so far
5213 * 1) We found an ideal block group for caching that
5214 * is mostly full and will cache quickly, so we might
5215 * as well wait for it.
5217 * 2) We searched for cached only and we didn't find
5218 * anything, and we didn't start any caching kthreads
5219 * either, so chances are we will loop through and
5220 * start a couple caching kthreads, and then come back
5221 * around and just wait for them. This will be slower
5222 * because we will have 2 caching kthreads reading at
5223 * the same time when we could have just started one
5224 * and waited for it to get far enough to give us an
5225 * allocation, so go ahead and go to the wait caching
5228 loop = LOOP_CACHING_WAIT;
5229 search_start = ideal_cache_offset;
5230 ideal_cache_percent = 0;
5232 } else if (loop == LOOP_FIND_IDEAL) {
5234 * Didn't find a uncached bg, wait on anything we find
5237 loop = LOOP_CACHING_WAIT;
5241 if (loop < LOOP_CACHING_WAIT) {
5246 if (loop == LOOP_ALLOC_CHUNK) {
5251 if (allowed_chunk_alloc) {
5252 ret = do_chunk_alloc(trans, root, num_bytes +
5253 2 * 1024 * 1024, data, 1);
5254 allowed_chunk_alloc = 0;
5255 done_chunk_alloc = 1;
5256 } else if (!done_chunk_alloc) {
5257 space_info->force_alloc = 1;
5260 if (loop < LOOP_NO_EMPTY_SIZE) {
5265 } else if (!ins->objectid) {
5269 /* we found what we needed */
5270 if (ins->objectid) {
5271 if (!(data & BTRFS_BLOCK_GROUP_DATA))
5272 trans->block_group = block_group->key.objectid;
5274 btrfs_put_block_group(block_group);
5281 static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5282 int dump_block_groups)
5284 struct btrfs_block_group_cache *cache;
5287 spin_lock(&info->lock);
5288 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
5289 (unsigned long long)(info->total_bytes - info->bytes_used -
5290 info->bytes_pinned - info->bytes_reserved -
5291 info->bytes_readonly),
5292 (info->full) ? "" : "not ");
5293 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
5294 "reserved=%llu, may_use=%llu, readonly=%llu\n",
5295 (unsigned long long)info->total_bytes,
5296 (unsigned long long)info->bytes_used,
5297 (unsigned long long)info->bytes_pinned,
5298 (unsigned long long)info->bytes_reserved,
5299 (unsigned long long)info->bytes_may_use,
5300 (unsigned long long)info->bytes_readonly);
5301 spin_unlock(&info->lock);
5303 if (!dump_block_groups)
5306 down_read(&info->groups_sem);
5308 list_for_each_entry(cache, &info->block_groups[index], list) {
5309 spin_lock(&cache->lock);
5310 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
5311 "%llu pinned %llu reserved\n",
5312 (unsigned long long)cache->key.objectid,
5313 (unsigned long long)cache->key.offset,
5314 (unsigned long long)btrfs_block_group_used(&cache->item),
5315 (unsigned long long)cache->pinned,
5316 (unsigned long long)cache->reserved);
5317 btrfs_dump_free_space(cache, bytes);
5318 spin_unlock(&cache->lock);
5320 if (++index < BTRFS_NR_RAID_TYPES)
5322 up_read(&info->groups_sem);
5325 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
5326 struct btrfs_root *root,
5327 u64 num_bytes, u64 min_alloc_size,
5328 u64 empty_size, u64 hint_byte,
5329 u64 search_end, struct btrfs_key *ins,
5333 u64 search_start = 0;
5335 data = btrfs_get_alloc_profile(root, data);
5338 * the only place that sets empty_size is btrfs_realloc_node, which
5339 * is not called recursively on allocations
5341 if (empty_size || root->ref_cows)
5342 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
5343 num_bytes + 2 * 1024 * 1024, data, 0);
5345 WARN_ON(num_bytes < root->sectorsize);
5346 ret = find_free_extent(trans, root, num_bytes, empty_size,
5347 search_start, search_end, hint_byte,
5350 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
5351 num_bytes = num_bytes >> 1;
5352 num_bytes = num_bytes & ~(root->sectorsize - 1);
5353 num_bytes = max(num_bytes, min_alloc_size);
5354 do_chunk_alloc(trans, root->fs_info->extent_root,
5355 num_bytes, data, 1);
5358 if (ret == -ENOSPC) {
5359 struct btrfs_space_info *sinfo;
5361 sinfo = __find_space_info(root->fs_info, data);
5362 printk(KERN_ERR "btrfs allocation failed flags %llu, "
5363 "wanted %llu\n", (unsigned long long)data,
5364 (unsigned long long)num_bytes);
5365 dump_space_info(sinfo, num_bytes, 1);
5371 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
5373 struct btrfs_block_group_cache *cache;
5376 cache = btrfs_lookup_block_group(root->fs_info, start);
5378 printk(KERN_ERR "Unable to find block group for %llu\n",
5379 (unsigned long long)start);
5383 ret = btrfs_discard_extent(root, start, len);
5385 btrfs_add_free_space(cache, start, len);
5386 update_reserved_bytes(cache, len, 0, 1);
5387 btrfs_put_block_group(cache);
5392 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5393 struct btrfs_root *root,
5394 u64 parent, u64 root_objectid,
5395 u64 flags, u64 owner, u64 offset,
5396 struct btrfs_key *ins, int ref_mod)
5399 struct btrfs_fs_info *fs_info = root->fs_info;
5400 struct btrfs_extent_item *extent_item;
5401 struct btrfs_extent_inline_ref *iref;
5402 struct btrfs_path *path;
5403 struct extent_buffer *leaf;
5408 type = BTRFS_SHARED_DATA_REF_KEY;
5410 type = BTRFS_EXTENT_DATA_REF_KEY;
5412 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
5414 path = btrfs_alloc_path();
5417 path->leave_spinning = 1;
5418 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5422 leaf = path->nodes[0];
5423 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5424 struct btrfs_extent_item);
5425 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
5426 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5427 btrfs_set_extent_flags(leaf, extent_item,
5428 flags | BTRFS_EXTENT_FLAG_DATA);
5430 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
5431 btrfs_set_extent_inline_ref_type(leaf, iref, type);
5433 struct btrfs_shared_data_ref *ref;
5434 ref = (struct btrfs_shared_data_ref *)(iref + 1);
5435 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5436 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
5438 struct btrfs_extent_data_ref *ref;
5439 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
5440 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
5441 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
5442 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
5443 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
5446 btrfs_mark_buffer_dirty(path->nodes[0]);
5447 btrfs_free_path(path);
5449 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
5451 printk(KERN_ERR "btrfs update block group failed for %llu "
5452 "%llu\n", (unsigned long long)ins->objectid,
5453 (unsigned long long)ins->offset);
5459 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
5460 struct btrfs_root *root,
5461 u64 parent, u64 root_objectid,
5462 u64 flags, struct btrfs_disk_key *key,
5463 int level, struct btrfs_key *ins)
5466 struct btrfs_fs_info *fs_info = root->fs_info;
5467 struct btrfs_extent_item *extent_item;
5468 struct btrfs_tree_block_info *block_info;
5469 struct btrfs_extent_inline_ref *iref;
5470 struct btrfs_path *path;
5471 struct extent_buffer *leaf;
5472 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
5474 path = btrfs_alloc_path();
5477 path->leave_spinning = 1;
5478 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5482 leaf = path->nodes[0];
5483 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5484 struct btrfs_extent_item);
5485 btrfs_set_extent_refs(leaf, extent_item, 1);
5486 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5487 btrfs_set_extent_flags(leaf, extent_item,
5488 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
5489 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
5491 btrfs_set_tree_block_key(leaf, block_info, key);
5492 btrfs_set_tree_block_level(leaf, block_info, level);
5494 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
5496 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
5497 btrfs_set_extent_inline_ref_type(leaf, iref,
5498 BTRFS_SHARED_BLOCK_REF_KEY);
5499 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5501 btrfs_set_extent_inline_ref_type(leaf, iref,
5502 BTRFS_TREE_BLOCK_REF_KEY);
5503 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
5506 btrfs_mark_buffer_dirty(leaf);
5507 btrfs_free_path(path);
5509 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
5511 printk(KERN_ERR "btrfs update block group failed for %llu "
5512 "%llu\n", (unsigned long long)ins->objectid,
5513 (unsigned long long)ins->offset);
5519 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5520 struct btrfs_root *root,
5521 u64 root_objectid, u64 owner,
5522 u64 offset, struct btrfs_key *ins)
5526 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
5528 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
5529 0, root_objectid, owner, offset,
5530 BTRFS_ADD_DELAYED_EXTENT, NULL);
5535 * this is used by the tree logging recovery code. It records that
5536 * an extent has been allocated and makes sure to clear the free
5537 * space cache bits as well
5539 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
5540 struct btrfs_root *root,
5541 u64 root_objectid, u64 owner, u64 offset,
5542 struct btrfs_key *ins)
5545 struct btrfs_block_group_cache *block_group;
5546 struct btrfs_caching_control *caching_ctl;
5547 u64 start = ins->objectid;
5548 u64 num_bytes = ins->offset;
5550 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
5551 cache_block_group(block_group, trans, NULL, 0);
5552 caching_ctl = get_caching_control(block_group);
5555 BUG_ON(!block_group_cache_done(block_group));
5556 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5559 mutex_lock(&caching_ctl->mutex);
5561 if (start >= caching_ctl->progress) {
5562 ret = add_excluded_extent(root, start, num_bytes);
5564 } else if (start + num_bytes <= caching_ctl->progress) {
5565 ret = btrfs_remove_free_space(block_group,
5569 num_bytes = caching_ctl->progress - start;
5570 ret = btrfs_remove_free_space(block_group,
5574 start = caching_ctl->progress;
5575 num_bytes = ins->objectid + ins->offset -
5576 caching_ctl->progress;
5577 ret = add_excluded_extent(root, start, num_bytes);
5581 mutex_unlock(&caching_ctl->mutex);
5582 put_caching_control(caching_ctl);
5585 ret = update_reserved_bytes(block_group, ins->offset, 1, 1);
5587 btrfs_put_block_group(block_group);
5588 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
5589 0, owner, offset, ins, 1);
5593 struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
5594 struct btrfs_root *root,
5595 u64 bytenr, u32 blocksize,
5598 struct extent_buffer *buf;
5600 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5602 return ERR_PTR(-ENOMEM);
5603 btrfs_set_header_generation(buf, trans->transid);
5604 btrfs_set_buffer_lockdep_class(buf, level);
5605 btrfs_tree_lock(buf);
5606 clean_tree_block(trans, root, buf);
5608 btrfs_set_lock_blocking(buf);
5609 btrfs_set_buffer_uptodate(buf);
5611 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
5613 * we allow two log transactions at a time, use different
5614 * EXENT bit to differentiate dirty pages.
5616 if (root->log_transid % 2 == 0)
5617 set_extent_dirty(&root->dirty_log_pages, buf->start,
5618 buf->start + buf->len - 1, GFP_NOFS);
5620 set_extent_new(&root->dirty_log_pages, buf->start,
5621 buf->start + buf->len - 1, GFP_NOFS);
5623 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
5624 buf->start + buf->len - 1, GFP_NOFS);
5626 trans->blocks_used++;
5627 /* this returns a buffer locked for blocking */
5631 static struct btrfs_block_rsv *
5632 use_block_rsv(struct btrfs_trans_handle *trans,
5633 struct btrfs_root *root, u32 blocksize)
5635 struct btrfs_block_rsv *block_rsv;
5638 block_rsv = get_block_rsv(trans, root);
5640 if (block_rsv->size == 0) {
5641 ret = reserve_metadata_bytes(trans, root, block_rsv,
5644 return ERR_PTR(ret);
5648 ret = block_rsv_use_bytes(block_rsv, blocksize);
5652 return ERR_PTR(-ENOSPC);
5655 static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
5657 block_rsv_add_bytes(block_rsv, blocksize, 0);
5658 block_rsv_release_bytes(block_rsv, NULL, 0);
5662 * finds a free extent and does all the dirty work required for allocation
5663 * returns the key for the extent through ins, and a tree buffer for
5664 * the first block of the extent through buf.
5666 * returns the tree buffer or NULL.
5668 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
5669 struct btrfs_root *root, u32 blocksize,
5670 u64 parent, u64 root_objectid,
5671 struct btrfs_disk_key *key, int level,
5672 u64 hint, u64 empty_size)
5674 struct btrfs_key ins;
5675 struct btrfs_block_rsv *block_rsv;
5676 struct extent_buffer *buf;
5681 block_rsv = use_block_rsv(trans, root, blocksize);
5682 if (IS_ERR(block_rsv))
5683 return ERR_CAST(block_rsv);
5685 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
5686 empty_size, hint, (u64)-1, &ins, 0);
5688 unuse_block_rsv(block_rsv, blocksize);
5689 return ERR_PTR(ret);
5692 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
5694 BUG_ON(IS_ERR(buf));
5696 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
5698 parent = ins.objectid;
5699 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5703 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
5704 struct btrfs_delayed_extent_op *extent_op;
5705 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
5708 memcpy(&extent_op->key, key, sizeof(extent_op->key));
5710 memset(&extent_op->key, 0, sizeof(extent_op->key));
5711 extent_op->flags_to_set = flags;
5712 extent_op->update_key = 1;
5713 extent_op->update_flags = 1;
5714 extent_op->is_data = 0;
5716 ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
5717 ins.offset, parent, root_objectid,
5718 level, BTRFS_ADD_DELAYED_EXTENT,
5725 struct walk_control {
5726 u64 refs[BTRFS_MAX_LEVEL];
5727 u64 flags[BTRFS_MAX_LEVEL];
5728 struct btrfs_key update_progress;
5738 #define DROP_REFERENCE 1
5739 #define UPDATE_BACKREF 2
5741 static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5742 struct btrfs_root *root,
5743 struct walk_control *wc,
5744 struct btrfs_path *path)
5752 struct btrfs_key key;
5753 struct extent_buffer *eb;
5758 if (path->slots[wc->level] < wc->reada_slot) {
5759 wc->reada_count = wc->reada_count * 2 / 3;
5760 wc->reada_count = max(wc->reada_count, 2);
5762 wc->reada_count = wc->reada_count * 3 / 2;
5763 wc->reada_count = min_t(int, wc->reada_count,
5764 BTRFS_NODEPTRS_PER_BLOCK(root));
5767 eb = path->nodes[wc->level];
5768 nritems = btrfs_header_nritems(eb);
5769 blocksize = btrfs_level_size(root, wc->level - 1);
5771 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5772 if (nread >= wc->reada_count)
5776 bytenr = btrfs_node_blockptr(eb, slot);
5777 generation = btrfs_node_ptr_generation(eb, slot);
5779 if (slot == path->slots[wc->level])
5782 if (wc->stage == UPDATE_BACKREF &&
5783 generation <= root->root_key.offset)
5786 /* We don't lock the tree block, it's OK to be racy here */
5787 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5792 if (wc->stage == DROP_REFERENCE) {
5796 if (wc->level == 1 &&
5797 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5799 if (!wc->update_ref ||
5800 generation <= root->root_key.offset)
5802 btrfs_node_key_to_cpu(eb, &key, slot);
5803 ret = btrfs_comp_cpu_keys(&key,
5804 &wc->update_progress);
5808 if (wc->level == 1 &&
5809 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5813 ret = readahead_tree_block(root, bytenr, blocksize,
5819 wc->reada_slot = slot;
5823 * hepler to process tree block while walking down the tree.
5825 * when wc->stage == UPDATE_BACKREF, this function updates
5826 * back refs for pointers in the block.
5828 * NOTE: return value 1 means we should stop walking down.
5830 static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5831 struct btrfs_root *root,
5832 struct btrfs_path *path,
5833 struct walk_control *wc, int lookup_info)
5835 int level = wc->level;
5836 struct extent_buffer *eb = path->nodes[level];
5837 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5840 if (wc->stage == UPDATE_BACKREF &&
5841 btrfs_header_owner(eb) != root->root_key.objectid)
5845 * when reference count of tree block is 1, it won't increase
5846 * again. once full backref flag is set, we never clear it.
5849 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
5850 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
5851 BUG_ON(!path->locks[level]);
5852 ret = btrfs_lookup_extent_info(trans, root,
5857 BUG_ON(wc->refs[level] == 0);
5860 if (wc->stage == DROP_REFERENCE) {
5861 if (wc->refs[level] > 1)
5864 if (path->locks[level] && !wc->keep_locks) {
5865 btrfs_tree_unlock(eb);
5866 path->locks[level] = 0;
5871 /* wc->stage == UPDATE_BACKREF */
5872 if (!(wc->flags[level] & flag)) {
5873 BUG_ON(!path->locks[level]);
5874 ret = btrfs_inc_ref(trans, root, eb, 1);
5876 ret = btrfs_dec_ref(trans, root, eb, 0);
5878 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
5881 wc->flags[level] |= flag;
5885 * the block is shared by multiple trees, so it's not good to
5886 * keep the tree lock
5888 if (path->locks[level] && level > 0) {
5889 btrfs_tree_unlock(eb);
5890 path->locks[level] = 0;
5896 * hepler to process tree block pointer.
5898 * when wc->stage == DROP_REFERENCE, this function checks
5899 * reference count of the block pointed to. if the block
5900 * is shared and we need update back refs for the subtree
5901 * rooted at the block, this function changes wc->stage to
5902 * UPDATE_BACKREF. if the block is shared and there is no
5903 * need to update back, this function drops the reference
5906 * NOTE: return value 1 means we should stop walking down.
5908 static noinline int do_walk_down(struct btrfs_trans_handle *trans,
5909 struct btrfs_root *root,
5910 struct btrfs_path *path,
5911 struct walk_control *wc, int *lookup_info)
5917 struct btrfs_key key;
5918 struct extent_buffer *next;
5919 int level = wc->level;
5923 generation = btrfs_node_ptr_generation(path->nodes[level],
5924 path->slots[level]);
5926 * if the lower level block was created before the snapshot
5927 * was created, we know there is no need to update back refs
5930 if (wc->stage == UPDATE_BACKREF &&
5931 generation <= root->root_key.offset) {
5936 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
5937 blocksize = btrfs_level_size(root, level - 1);
5939 next = btrfs_find_tree_block(root, bytenr, blocksize);
5941 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
5946 btrfs_tree_lock(next);
5947 btrfs_set_lock_blocking(next);
5949 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5950 &wc->refs[level - 1],
5951 &wc->flags[level - 1]);
5953 BUG_ON(wc->refs[level - 1] == 0);
5956 if (wc->stage == DROP_REFERENCE) {
5957 if (wc->refs[level - 1] > 1) {
5959 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5962 if (!wc->update_ref ||
5963 generation <= root->root_key.offset)
5966 btrfs_node_key_to_cpu(path->nodes[level], &key,
5967 path->slots[level]);
5968 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
5972 wc->stage = UPDATE_BACKREF;
5973 wc->shared_level = level - 1;
5977 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5981 if (!btrfs_buffer_uptodate(next, generation)) {
5982 btrfs_tree_unlock(next);
5983 free_extent_buffer(next);
5989 if (reada && level == 1)
5990 reada_walk_down(trans, root, wc, path);
5991 next = read_tree_block(root, bytenr, blocksize, generation);
5992 btrfs_tree_lock(next);
5993 btrfs_set_lock_blocking(next);
5997 BUG_ON(level != btrfs_header_level(next));
5998 path->nodes[level] = next;
5999 path->slots[level] = 0;
6000 path->locks[level] = 1;
6006 wc->refs[level - 1] = 0;
6007 wc->flags[level - 1] = 0;
6008 if (wc->stage == DROP_REFERENCE) {
6009 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6010 parent = path->nodes[level]->start;
6012 BUG_ON(root->root_key.objectid !=
6013 btrfs_header_owner(path->nodes[level]));
6017 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
6018 root->root_key.objectid, level - 1, 0);
6021 btrfs_tree_unlock(next);
6022 free_extent_buffer(next);
6028 * hepler to process tree block while walking up the tree.
6030 * when wc->stage == DROP_REFERENCE, this function drops
6031 * reference count on the block.
6033 * when wc->stage == UPDATE_BACKREF, this function changes
6034 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6035 * to UPDATE_BACKREF previously while processing the block.
6037 * NOTE: return value 1 means we should stop walking up.
6039 static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6040 struct btrfs_root *root,
6041 struct btrfs_path *path,
6042 struct walk_control *wc)
6045 int level = wc->level;
6046 struct extent_buffer *eb = path->nodes[level];
6049 if (wc->stage == UPDATE_BACKREF) {
6050 BUG_ON(wc->shared_level < level);
6051 if (level < wc->shared_level)
6054 ret = find_next_key(path, level + 1, &wc->update_progress);
6058 wc->stage = DROP_REFERENCE;
6059 wc->shared_level = -1;
6060 path->slots[level] = 0;
6063 * check reference count again if the block isn't locked.
6064 * we should start walking down the tree again if reference
6067 if (!path->locks[level]) {
6069 btrfs_tree_lock(eb);
6070 btrfs_set_lock_blocking(eb);
6071 path->locks[level] = 1;
6073 ret = btrfs_lookup_extent_info(trans, root,
6078 BUG_ON(wc->refs[level] == 0);
6079 if (wc->refs[level] == 1) {
6080 btrfs_tree_unlock(eb);
6081 path->locks[level] = 0;
6087 /* wc->stage == DROP_REFERENCE */
6088 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
6090 if (wc->refs[level] == 1) {
6092 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6093 ret = btrfs_dec_ref(trans, root, eb, 1);
6095 ret = btrfs_dec_ref(trans, root, eb, 0);
6098 /* make block locked assertion in clean_tree_block happy */
6099 if (!path->locks[level] &&
6100 btrfs_header_generation(eb) == trans->transid) {
6101 btrfs_tree_lock(eb);
6102 btrfs_set_lock_blocking(eb);
6103 path->locks[level] = 1;
6105 clean_tree_block(trans, root, eb);
6108 if (eb == root->node) {
6109 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6112 BUG_ON(root->root_key.objectid !=
6113 btrfs_header_owner(eb));
6115 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6116 parent = path->nodes[level + 1]->start;
6118 BUG_ON(root->root_key.objectid !=
6119 btrfs_header_owner(path->nodes[level + 1]));
6122 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
6124 wc->refs[level] = 0;
6125 wc->flags[level] = 0;
6129 static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6130 struct btrfs_root *root,
6131 struct btrfs_path *path,
6132 struct walk_control *wc)
6134 int level = wc->level;
6135 int lookup_info = 1;
6138 while (level >= 0) {
6139 ret = walk_down_proc(trans, root, path, wc, lookup_info);
6146 if (path->slots[level] >=
6147 btrfs_header_nritems(path->nodes[level]))
6150 ret = do_walk_down(trans, root, path, wc, &lookup_info);
6152 path->slots[level]++;
6161 static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
6162 struct btrfs_root *root,
6163 struct btrfs_path *path,
6164 struct walk_control *wc, int max_level)
6166 int level = wc->level;
6169 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6170 while (level < max_level && path->nodes[level]) {
6172 if (path->slots[level] + 1 <
6173 btrfs_header_nritems(path->nodes[level])) {
6174 path->slots[level]++;
6177 ret = walk_up_proc(trans, root, path, wc);
6181 if (path->locks[level]) {
6182 btrfs_tree_unlock(path->nodes[level]);
6183 path->locks[level] = 0;
6185 free_extent_buffer(path->nodes[level]);
6186 path->nodes[level] = NULL;
6194 * drop a subvolume tree.
6196 * this function traverses the tree freeing any blocks that only
6197 * referenced by the tree.
6199 * when a shared tree block is found. this function decreases its
6200 * reference count by one. if update_ref is true, this function
6201 * also make sure backrefs for the shared block and all lower level
6202 * blocks are properly updated.
6204 int btrfs_drop_snapshot(struct btrfs_root *root,
6205 struct btrfs_block_rsv *block_rsv, int update_ref)
6207 struct btrfs_path *path;
6208 struct btrfs_trans_handle *trans;
6209 struct btrfs_root *tree_root = root->fs_info->tree_root;
6210 struct btrfs_root_item *root_item = &root->root_item;
6211 struct walk_control *wc;
6212 struct btrfs_key key;
6217 path = btrfs_alloc_path();
6220 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6223 trans = btrfs_start_transaction(tree_root, 0);
6225 trans->block_rsv = block_rsv;
6227 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
6228 level = btrfs_header_level(root->node);
6229 path->nodes[level] = btrfs_lock_root_node(root);
6230 btrfs_set_lock_blocking(path->nodes[level]);
6231 path->slots[level] = 0;
6232 path->locks[level] = 1;
6233 memset(&wc->update_progress, 0,
6234 sizeof(wc->update_progress));
6236 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
6237 memcpy(&wc->update_progress, &key,
6238 sizeof(wc->update_progress));
6240 level = root_item->drop_level;
6242 path->lowest_level = level;
6243 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6244 path->lowest_level = 0;
6252 * unlock our path, this is safe because only this
6253 * function is allowed to delete this snapshot
6255 btrfs_unlock_up_safe(path, 0);
6257 level = btrfs_header_level(root->node);
6259 btrfs_tree_lock(path->nodes[level]);
6260 btrfs_set_lock_blocking(path->nodes[level]);
6262 ret = btrfs_lookup_extent_info(trans, root,
6263 path->nodes[level]->start,
6264 path->nodes[level]->len,
6268 BUG_ON(wc->refs[level] == 0);
6270 if (level == root_item->drop_level)
6273 btrfs_tree_unlock(path->nodes[level]);
6274 WARN_ON(wc->refs[level] != 1);
6280 wc->shared_level = -1;
6281 wc->stage = DROP_REFERENCE;
6282 wc->update_ref = update_ref;
6284 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
6287 ret = walk_down_tree(trans, root, path, wc);
6293 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
6300 BUG_ON(wc->stage != DROP_REFERENCE);
6304 if (wc->stage == DROP_REFERENCE) {
6306 btrfs_node_key(path->nodes[level],
6307 &root_item->drop_progress,
6308 path->slots[level]);
6309 root_item->drop_level = level;
6312 BUG_ON(wc->level == 0);
6313 if (btrfs_should_end_transaction(trans, tree_root)) {
6314 ret = btrfs_update_root(trans, tree_root,
6319 btrfs_end_transaction_throttle(trans, tree_root);
6320 trans = btrfs_start_transaction(tree_root, 0);
6322 trans->block_rsv = block_rsv;
6325 btrfs_release_path(root, path);
6328 ret = btrfs_del_root(trans, tree_root, &root->root_key);
6331 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6332 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
6336 /* if we fail to delete the orphan item this time
6337 * around, it'll get picked up the next time.
6339 * The most common failure here is just -ENOENT.
6341 btrfs_del_orphan_item(trans, tree_root,
6342 root->root_key.objectid);
6346 if (root->in_radix) {
6347 btrfs_free_fs_root(tree_root->fs_info, root);
6349 free_extent_buffer(root->node);
6350 free_extent_buffer(root->commit_root);
6354 btrfs_end_transaction_throttle(trans, tree_root);
6356 btrfs_free_path(path);
6361 * drop subtree rooted at tree block 'node'.
6363 * NOTE: this function will unlock and release tree block 'node'
6365 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
6366 struct btrfs_root *root,
6367 struct extent_buffer *node,
6368 struct extent_buffer *parent)
6370 struct btrfs_path *path;
6371 struct walk_control *wc;
6377 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6379 path = btrfs_alloc_path();
6382 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6385 btrfs_assert_tree_locked(parent);
6386 parent_level = btrfs_header_level(parent);
6387 extent_buffer_get(parent);
6388 path->nodes[parent_level] = parent;
6389 path->slots[parent_level] = btrfs_header_nritems(parent);
6391 btrfs_assert_tree_locked(node);
6392 level = btrfs_header_level(node);
6393 path->nodes[level] = node;
6394 path->slots[level] = 0;
6395 path->locks[level] = 1;
6397 wc->refs[parent_level] = 1;
6398 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6400 wc->shared_level = -1;
6401 wc->stage = DROP_REFERENCE;
6404 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
6407 wret = walk_down_tree(trans, root, path, wc);
6413 wret = walk_up_tree(trans, root, path, wc, parent_level);
6421 btrfs_free_path(path);
6426 static unsigned long calc_ra(unsigned long start, unsigned long last,
6429 return min(last, start + nr - 1);
6432 static noinline int relocate_inode_pages(struct inode *inode, u64 start,
6437 unsigned long first_index;
6438 unsigned long last_index;
6441 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6442 struct file_ra_state *ra;
6443 struct btrfs_ordered_extent *ordered;
6444 unsigned int total_read = 0;
6445 unsigned int total_dirty = 0;
6448 ra = kzalloc(sizeof(*ra), GFP_NOFS);
6450 mutex_lock(&inode->i_mutex);
6451 first_index = start >> PAGE_CACHE_SHIFT;
6452 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
6454 /* make sure the dirty trick played by the caller work */
6455 ret = invalidate_inode_pages2_range(inode->i_mapping,
6456 first_index, last_index);
6460 file_ra_state_init(ra, inode->i_mapping);
6462 for (i = first_index ; i <= last_index; i++) {
6463 if (total_read % ra->ra_pages == 0) {
6464 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
6465 calc_ra(i, last_index, ra->ra_pages));
6469 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
6471 page = grab_cache_page(inode->i_mapping, i);
6476 if (!PageUptodate(page)) {
6477 btrfs_readpage(NULL, page);
6479 if (!PageUptodate(page)) {
6481 page_cache_release(page);
6486 wait_on_page_writeback(page);
6488 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
6489 page_end = page_start + PAGE_CACHE_SIZE - 1;
6490 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
6492 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6494 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
6496 page_cache_release(page);
6497 btrfs_start_ordered_extent(inode, ordered, 1);
6498 btrfs_put_ordered_extent(ordered);
6501 set_page_extent_mapped(page);
6503 if (i == first_index)
6504 set_extent_bits(io_tree, page_start, page_end,
6505 EXTENT_BOUNDARY, GFP_NOFS);
6506 btrfs_set_extent_delalloc(inode, page_start, page_end);
6508 set_page_dirty(page);
6511 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
6513 page_cache_release(page);
6518 mutex_unlock(&inode->i_mutex);
6519 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
6523 static noinline int relocate_data_extent(struct inode *reloc_inode,
6524 struct btrfs_key *extent_key,
6527 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6528 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
6529 struct extent_map *em;
6530 u64 start = extent_key->objectid - offset;
6531 u64 end = start + extent_key->offset - 1;
6533 em = alloc_extent_map(GFP_NOFS);
6534 BUG_ON(!em || IS_ERR(em));
6537 em->len = extent_key->offset;
6538 em->block_len = extent_key->offset;
6539 em->block_start = extent_key->objectid;
6540 em->bdev = root->fs_info->fs_devices->latest_bdev;
6541 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6543 /* setup extent map to cheat btrfs_readpage */
6544 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
6547 write_lock(&em_tree->lock);
6548 ret = add_extent_mapping(em_tree, em);
6549 write_unlock(&em_tree->lock);
6550 if (ret != -EEXIST) {
6551 free_extent_map(em);
6554 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
6556 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
6558 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
6561 struct btrfs_ref_path {
6563 u64 nodes[BTRFS_MAX_LEVEL];
6565 u64 root_generation;
6572 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
6573 u64 new_nodes[BTRFS_MAX_LEVEL];
6576 struct disk_extent {
6587 static int is_cowonly_root(u64 root_objectid)
6589 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
6590 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
6591 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
6592 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
6593 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6594 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
6599 static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
6600 struct btrfs_root *extent_root,
6601 struct btrfs_ref_path *ref_path,
6604 struct extent_buffer *leaf;
6605 struct btrfs_path *path;
6606 struct btrfs_extent_ref *ref;
6607 struct btrfs_key key;
6608 struct btrfs_key found_key;
6614 path = btrfs_alloc_path();
6619 ref_path->lowest_level = -1;
6620 ref_path->current_level = -1;
6621 ref_path->shared_level = -1;
6625 level = ref_path->current_level - 1;
6626 while (level >= -1) {
6628 if (level < ref_path->lowest_level)
6632 bytenr = ref_path->nodes[level];
6634 bytenr = ref_path->extent_start;
6635 BUG_ON(bytenr == 0);
6637 parent = ref_path->nodes[level + 1];
6638 ref_path->nodes[level + 1] = 0;
6639 ref_path->current_level = level;
6640 BUG_ON(parent == 0);
6642 key.objectid = bytenr;
6643 key.offset = parent + 1;
6644 key.type = BTRFS_EXTENT_REF_KEY;
6646 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
6651 leaf = path->nodes[0];
6652 nritems = btrfs_header_nritems(leaf);
6653 if (path->slots[0] >= nritems) {
6654 ret = btrfs_next_leaf(extent_root, path);
6659 leaf = path->nodes[0];
6662 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6663 if (found_key.objectid == bytenr &&
6664 found_key.type == BTRFS_EXTENT_REF_KEY) {
6665 if (level < ref_path->shared_level)
6666 ref_path->shared_level = level;
6671 btrfs_release_path(extent_root, path);
6674 /* reached lowest level */
6678 level = ref_path->current_level;
6679 while (level < BTRFS_MAX_LEVEL - 1) {
6683 bytenr = ref_path->nodes[level];
6685 bytenr = ref_path->extent_start;
6687 BUG_ON(bytenr == 0);
6689 key.objectid = bytenr;
6691 key.type = BTRFS_EXTENT_REF_KEY;
6693 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
6697 leaf = path->nodes[0];
6698 nritems = btrfs_header_nritems(leaf);
6699 if (path->slots[0] >= nritems) {
6700 ret = btrfs_next_leaf(extent_root, path);
6704 /* the extent was freed by someone */
6705 if (ref_path->lowest_level == level)
6707 btrfs_release_path(extent_root, path);
6710 leaf = path->nodes[0];
6713 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6714 if (found_key.objectid != bytenr ||
6715 found_key.type != BTRFS_EXTENT_REF_KEY) {
6716 /* the extent was freed by someone */
6717 if (ref_path->lowest_level == level) {
6721 btrfs_release_path(extent_root, path);
6725 ref = btrfs_item_ptr(leaf, path->slots[0],
6726 struct btrfs_extent_ref);
6727 ref_objectid = btrfs_ref_objectid(leaf, ref);
6728 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
6730 level = (int)ref_objectid;
6731 BUG_ON(level >= BTRFS_MAX_LEVEL);
6732 ref_path->lowest_level = level;
6733 ref_path->current_level = level;
6734 ref_path->nodes[level] = bytenr;
6736 WARN_ON(ref_objectid != level);
6739 WARN_ON(level != -1);
6743 if (ref_path->lowest_level == level) {
6744 ref_path->owner_objectid = ref_objectid;
6745 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
6749 * the block is tree root or the block isn't in reference
6752 if (found_key.objectid == found_key.offset ||
6753 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
6754 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6755 ref_path->root_generation =
6756 btrfs_ref_generation(leaf, ref);
6758 /* special reference from the tree log */
6759 ref_path->nodes[0] = found_key.offset;
6760 ref_path->current_level = 0;
6767 BUG_ON(ref_path->nodes[level] != 0);
6768 ref_path->nodes[level] = found_key.offset;
6769 ref_path->current_level = level;
6772 * the reference was created in the running transaction,
6773 * no need to continue walking up.
6775 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
6776 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6777 ref_path->root_generation =
6778 btrfs_ref_generation(leaf, ref);
6783 btrfs_release_path(extent_root, path);
6786 /* reached max tree level, but no tree root found. */
6789 btrfs_free_path(path);
6793 static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
6794 struct btrfs_root *extent_root,
6795 struct btrfs_ref_path *ref_path,
6798 memset(ref_path, 0, sizeof(*ref_path));
6799 ref_path->extent_start = extent_start;
6801 return __next_ref_path(trans, extent_root, ref_path, 1);
6804 static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
6805 struct btrfs_root *extent_root,
6806 struct btrfs_ref_path *ref_path)
6808 return __next_ref_path(trans, extent_root, ref_path, 0);
6811 static noinline int get_new_locations(struct inode *reloc_inode,
6812 struct btrfs_key *extent_key,
6813 u64 offset, int no_fragment,
6814 struct disk_extent **extents,
6817 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6818 struct btrfs_path *path;
6819 struct btrfs_file_extent_item *fi;
6820 struct extent_buffer *leaf;
6821 struct disk_extent *exts = *extents;
6822 struct btrfs_key found_key;
6827 int max = *nr_extents;
6830 WARN_ON(!no_fragment && *extents);
6833 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
6838 path = btrfs_alloc_path();
6841 cur_pos = extent_key->objectid - offset;
6842 last_byte = extent_key->objectid + extent_key->offset;
6843 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
6853 leaf = path->nodes[0];
6854 nritems = btrfs_header_nritems(leaf);
6855 if (path->slots[0] >= nritems) {
6856 ret = btrfs_next_leaf(root, path);
6861 leaf = path->nodes[0];
6864 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6865 if (found_key.offset != cur_pos ||
6866 found_key.type != BTRFS_EXTENT_DATA_KEY ||
6867 found_key.objectid != reloc_inode->i_ino)
6870 fi = btrfs_item_ptr(leaf, path->slots[0],
6871 struct btrfs_file_extent_item);
6872 if (btrfs_file_extent_type(leaf, fi) !=
6873 BTRFS_FILE_EXTENT_REG ||
6874 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6878 struct disk_extent *old = exts;
6880 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
6881 memcpy(exts, old, sizeof(*exts) * nr);
6882 if (old != *extents)
6886 exts[nr].disk_bytenr =
6887 btrfs_file_extent_disk_bytenr(leaf, fi);
6888 exts[nr].disk_num_bytes =
6889 btrfs_file_extent_disk_num_bytes(leaf, fi);
6890 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
6891 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6892 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6893 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
6894 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
6895 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
6897 BUG_ON(exts[nr].offset > 0);
6898 BUG_ON(exts[nr].compression || exts[nr].encryption);
6899 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
6901 cur_pos += exts[nr].num_bytes;
6904 if (cur_pos + offset >= last_byte)
6914 BUG_ON(cur_pos + offset > last_byte);
6915 if (cur_pos + offset < last_byte) {
6921 btrfs_free_path(path);
6923 if (exts != *extents)
6932 static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
6933 struct btrfs_root *root,
6934 struct btrfs_path *path,
6935 struct btrfs_key *extent_key,
6936 struct btrfs_key *leaf_key,
6937 struct btrfs_ref_path *ref_path,
6938 struct disk_extent *new_extents,
6941 struct extent_buffer *leaf;
6942 struct btrfs_file_extent_item *fi;
6943 struct inode *inode = NULL;
6944 struct btrfs_key key;
6949 u64 search_end = (u64)-1;
6952 int extent_locked = 0;
6956 memcpy(&key, leaf_key, sizeof(key));
6957 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
6958 if (key.objectid < ref_path->owner_objectid ||
6959 (key.objectid == ref_path->owner_objectid &&
6960 key.type < BTRFS_EXTENT_DATA_KEY)) {
6961 key.objectid = ref_path->owner_objectid;
6962 key.type = BTRFS_EXTENT_DATA_KEY;
6968 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
6972 leaf = path->nodes[0];
6973 nritems = btrfs_header_nritems(leaf);
6975 if (extent_locked && ret > 0) {
6977 * the file extent item was modified by someone
6978 * before the extent got locked.
6980 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6981 lock_end, GFP_NOFS);
6985 if (path->slots[0] >= nritems) {
6986 if (++nr_scaned > 2)
6989 BUG_ON(extent_locked);
6990 ret = btrfs_next_leaf(root, path);
6995 leaf = path->nodes[0];
6996 nritems = btrfs_header_nritems(leaf);
6999 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
7001 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
7002 if ((key.objectid > ref_path->owner_objectid) ||
7003 (key.objectid == ref_path->owner_objectid &&
7004 key.type > BTRFS_EXTENT_DATA_KEY) ||
7005 key.offset >= search_end)
7009 if (inode && key.objectid != inode->i_ino) {
7010 BUG_ON(extent_locked);
7011 btrfs_release_path(root, path);
7012 mutex_unlock(&inode->i_mutex);
7018 if (key.type != BTRFS_EXTENT_DATA_KEY) {
7023 fi = btrfs_item_ptr(leaf, path->slots[0],
7024 struct btrfs_file_extent_item);
7025 extent_type = btrfs_file_extent_type(leaf, fi);
7026 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
7027 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
7028 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
7029 extent_key->objectid)) {
7035 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7036 ext_offset = btrfs_file_extent_offset(leaf, fi);
7038 if (search_end == (u64)-1) {
7039 search_end = key.offset - ext_offset +
7040 btrfs_file_extent_ram_bytes(leaf, fi);
7043 if (!extent_locked) {
7044 lock_start = key.offset;
7045 lock_end = lock_start + num_bytes - 1;
7047 if (lock_start > key.offset ||
7048 lock_end + 1 < key.offset + num_bytes) {
7049 unlock_extent(&BTRFS_I(inode)->io_tree,
7050 lock_start, lock_end, GFP_NOFS);
7056 btrfs_release_path(root, path);
7058 inode = btrfs_iget_locked(root->fs_info->sb,
7059 key.objectid, root);
7060 if (inode->i_state & I_NEW) {
7061 BTRFS_I(inode)->root = root;
7062 BTRFS_I(inode)->location.objectid =
7064 BTRFS_I(inode)->location.type =
7065 BTRFS_INODE_ITEM_KEY;
7066 BTRFS_I(inode)->location.offset = 0;
7067 btrfs_read_locked_inode(inode);
7068 unlock_new_inode(inode);
7071 * some code call btrfs_commit_transaction while
7072 * holding the i_mutex, so we can't use mutex_lock
7075 if (is_bad_inode(inode) ||
7076 !mutex_trylock(&inode->i_mutex)) {
7079 key.offset = (u64)-1;
7084 if (!extent_locked) {
7085 struct btrfs_ordered_extent *ordered;
7087 btrfs_release_path(root, path);
7089 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7090 lock_end, GFP_NOFS);
7091 ordered = btrfs_lookup_first_ordered_extent(inode,
7094 ordered->file_offset <= lock_end &&
7095 ordered->file_offset + ordered->len > lock_start) {
7096 unlock_extent(&BTRFS_I(inode)->io_tree,
7097 lock_start, lock_end, GFP_NOFS);
7098 btrfs_start_ordered_extent(inode, ordered, 1);
7099 btrfs_put_ordered_extent(ordered);
7100 key.offset += num_bytes;
7104 btrfs_put_ordered_extent(ordered);
7110 if (nr_extents == 1) {
7111 /* update extent pointer in place */
7112 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7113 new_extents[0].disk_bytenr);
7114 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7115 new_extents[0].disk_num_bytes);
7116 btrfs_mark_buffer_dirty(leaf);
7118 btrfs_drop_extent_cache(inode, key.offset,
7119 key.offset + num_bytes - 1, 0);
7121 ret = btrfs_inc_extent_ref(trans, root,
7122 new_extents[0].disk_bytenr,
7123 new_extents[0].disk_num_bytes,
7125 root->root_key.objectid,
7130 ret = btrfs_free_extent(trans, root,
7131 extent_key->objectid,
7134 btrfs_header_owner(leaf),
7135 btrfs_header_generation(leaf),
7139 btrfs_release_path(root, path);
7140 key.offset += num_bytes;
7148 * drop old extent pointer at first, then insert the
7149 * new pointers one bye one
7151 btrfs_release_path(root, path);
7152 ret = btrfs_drop_extents(trans, root, inode, key.offset,
7153 key.offset + num_bytes,
7154 key.offset, &alloc_hint);
7157 for (i = 0; i < nr_extents; i++) {
7158 if (ext_offset >= new_extents[i].num_bytes) {
7159 ext_offset -= new_extents[i].num_bytes;
7162 extent_len = min(new_extents[i].num_bytes -
7163 ext_offset, num_bytes);
7165 ret = btrfs_insert_empty_item(trans, root,
7170 leaf = path->nodes[0];
7171 fi = btrfs_item_ptr(leaf, path->slots[0],
7172 struct btrfs_file_extent_item);
7173 btrfs_set_file_extent_generation(leaf, fi,
7175 btrfs_set_file_extent_type(leaf, fi,
7176 BTRFS_FILE_EXTENT_REG);
7177 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7178 new_extents[i].disk_bytenr);
7179 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7180 new_extents[i].disk_num_bytes);
7181 btrfs_set_file_extent_ram_bytes(leaf, fi,
7182 new_extents[i].ram_bytes);
7184 btrfs_set_file_extent_compression(leaf, fi,
7185 new_extents[i].compression);
7186 btrfs_set_file_extent_encryption(leaf, fi,
7187 new_extents[i].encryption);
7188 btrfs_set_file_extent_other_encoding(leaf, fi,
7189 new_extents[i].other_encoding);
7191 btrfs_set_file_extent_num_bytes(leaf, fi,
7193 ext_offset += new_extents[i].offset;
7194 btrfs_set_file_extent_offset(leaf, fi,
7196 btrfs_mark_buffer_dirty(leaf);
7198 btrfs_drop_extent_cache(inode, key.offset,
7199 key.offset + extent_len - 1, 0);
7201 ret = btrfs_inc_extent_ref(trans, root,
7202 new_extents[i].disk_bytenr,
7203 new_extents[i].disk_num_bytes,
7205 root->root_key.objectid,
7206 trans->transid, key.objectid);
7208 btrfs_release_path(root, path);
7210 inode_add_bytes(inode, extent_len);
7213 num_bytes -= extent_len;
7214 key.offset += extent_len;
7219 BUG_ON(i >= nr_extents);
7223 if (extent_locked) {
7224 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7225 lock_end, GFP_NOFS);
7229 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
7230 key.offset >= search_end)
7237 btrfs_release_path(root, path);
7239 mutex_unlock(&inode->i_mutex);
7240 if (extent_locked) {
7241 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7242 lock_end, GFP_NOFS);
7249 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
7250 struct btrfs_root *root,
7251 struct extent_buffer *buf, u64 orig_start)
7256 BUG_ON(btrfs_header_generation(buf) != trans->transid);
7257 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7259 level = btrfs_header_level(buf);
7261 struct btrfs_leaf_ref *ref;
7262 struct btrfs_leaf_ref *orig_ref;
7264 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
7268 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
7270 btrfs_free_leaf_ref(root, orig_ref);
7274 ref->nritems = orig_ref->nritems;
7275 memcpy(ref->extents, orig_ref->extents,
7276 sizeof(ref->extents[0]) * ref->nritems);
7278 btrfs_free_leaf_ref(root, orig_ref);
7280 ref->root_gen = trans->transid;
7281 ref->bytenr = buf->start;
7282 ref->owner = btrfs_header_owner(buf);
7283 ref->generation = btrfs_header_generation(buf);
7285 ret = btrfs_add_leaf_ref(root, ref, 0);
7287 btrfs_free_leaf_ref(root, ref);
7292 static noinline int invalidate_extent_cache(struct btrfs_root *root,
7293 struct extent_buffer *leaf,
7294 struct btrfs_block_group_cache *group,
7295 struct btrfs_root *target_root)
7297 struct btrfs_key key;
7298 struct inode *inode = NULL;
7299 struct btrfs_file_extent_item *fi;
7300 struct extent_state *cached_state = NULL;
7302 u64 skip_objectid = 0;
7306 nritems = btrfs_header_nritems(leaf);
7307 for (i = 0; i < nritems; i++) {
7308 btrfs_item_key_to_cpu(leaf, &key, i);
7309 if (key.objectid == skip_objectid ||
7310 key.type != BTRFS_EXTENT_DATA_KEY)
7312 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7313 if (btrfs_file_extent_type(leaf, fi) ==
7314 BTRFS_FILE_EXTENT_INLINE)
7316 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
7318 if (!inode || inode->i_ino != key.objectid) {
7320 inode = btrfs_ilookup(target_root->fs_info->sb,
7321 key.objectid, target_root, 1);
7324 skip_objectid = key.objectid;
7327 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7329 lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset,
7330 key.offset + num_bytes - 1, 0, &cached_state,
7332 btrfs_drop_extent_cache(inode, key.offset,
7333 key.offset + num_bytes - 1, 1);
7334 unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset,
7335 key.offset + num_bytes - 1, &cached_state,
7343 static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
7344 struct btrfs_root *root,
7345 struct extent_buffer *leaf,
7346 struct btrfs_block_group_cache *group,
7347 struct inode *reloc_inode)
7349 struct btrfs_key key;
7350 struct btrfs_key extent_key;
7351 struct btrfs_file_extent_item *fi;
7352 struct btrfs_leaf_ref *ref;
7353 struct disk_extent *new_extent;
7362 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
7363 BUG_ON(!new_extent);
7365 ref = btrfs_lookup_leaf_ref(root, leaf->start);
7369 nritems = btrfs_header_nritems(leaf);
7370 for (i = 0; i < nritems; i++) {
7371 btrfs_item_key_to_cpu(leaf, &key, i);
7372 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
7374 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7375 if (btrfs_file_extent_type(leaf, fi) ==
7376 BTRFS_FILE_EXTENT_INLINE)
7378 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
7379 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
7384 if (bytenr >= group->key.objectid + group->key.offset ||
7385 bytenr + num_bytes <= group->key.objectid)
7388 extent_key.objectid = bytenr;
7389 extent_key.offset = num_bytes;
7390 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
7392 ret = get_new_locations(reloc_inode, &extent_key,
7393 group->key.objectid, 1,
7394 &new_extent, &nr_extent);
7399 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
7400 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
7401 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
7402 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
7404 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7405 new_extent->disk_bytenr);
7406 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7407 new_extent->disk_num_bytes);
7408 btrfs_mark_buffer_dirty(leaf);
7410 ret = btrfs_inc_extent_ref(trans, root,
7411 new_extent->disk_bytenr,
7412 new_extent->disk_num_bytes,
7414 root->root_key.objectid,
7415 trans->transid, key.objectid);
7418 ret = btrfs_free_extent(trans, root,
7419 bytenr, num_bytes, leaf->start,
7420 btrfs_header_owner(leaf),
7421 btrfs_header_generation(leaf),
7427 BUG_ON(ext_index + 1 != ref->nritems);
7428 btrfs_free_leaf_ref(root, ref);
7432 int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
7433 struct btrfs_root *root)
7435 struct btrfs_root *reloc_root;
7438 if (root->reloc_root) {
7439 reloc_root = root->reloc_root;
7440 root->reloc_root = NULL;
7441 list_add(&reloc_root->dead_list,
7442 &root->fs_info->dead_reloc_roots);
7444 btrfs_set_root_bytenr(&reloc_root->root_item,
7445 reloc_root->node->start);
7446 btrfs_set_root_level(&root->root_item,
7447 btrfs_header_level(reloc_root->node));
7448 memset(&reloc_root->root_item.drop_progress, 0,
7449 sizeof(struct btrfs_disk_key));
7450 reloc_root->root_item.drop_level = 0;
7452 ret = btrfs_update_root(trans, root->fs_info->tree_root,
7453 &reloc_root->root_key,
7454 &reloc_root->root_item);
7460 int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
7462 struct btrfs_trans_handle *trans;
7463 struct btrfs_root *reloc_root;
7464 struct btrfs_root *prev_root = NULL;
7465 struct list_head dead_roots;
7469 INIT_LIST_HEAD(&dead_roots);
7470 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
7472 while (!list_empty(&dead_roots)) {
7473 reloc_root = list_entry(dead_roots.prev,
7474 struct btrfs_root, dead_list);
7475 list_del_init(&reloc_root->dead_list);
7477 BUG_ON(reloc_root->commit_root != NULL);
7479 trans = btrfs_join_transaction(root, 1);
7482 mutex_lock(&root->fs_info->drop_mutex);
7483 ret = btrfs_drop_snapshot(trans, reloc_root);
7486 mutex_unlock(&root->fs_info->drop_mutex);
7488 nr = trans->blocks_used;
7489 ret = btrfs_end_transaction(trans, root);
7491 btrfs_btree_balance_dirty(root, nr);
7494 free_extent_buffer(reloc_root->node);
7496 ret = btrfs_del_root(trans, root->fs_info->tree_root,
7497 &reloc_root->root_key);
7499 mutex_unlock(&root->fs_info->drop_mutex);
7501 nr = trans->blocks_used;
7502 ret = btrfs_end_transaction(trans, root);
7504 btrfs_btree_balance_dirty(root, nr);
7507 prev_root = reloc_root;
7510 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
7516 int btrfs_add_dead_reloc_root(struct btrfs_root *root)
7518 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
7522 int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
7524 struct btrfs_root *reloc_root;
7525 struct btrfs_trans_handle *trans;
7526 struct btrfs_key location;
7530 mutex_lock(&root->fs_info->tree_reloc_mutex);
7531 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
7533 found = !list_empty(&root->fs_info->dead_reloc_roots);
7534 mutex_unlock(&root->fs_info->tree_reloc_mutex);
7537 trans = btrfs_start_transaction(root, 1);
7539 ret = btrfs_commit_transaction(trans, root);
7543 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
7544 location.offset = (u64)-1;
7545 location.type = BTRFS_ROOT_ITEM_KEY;
7547 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
7548 BUG_ON(!reloc_root);
7549 btrfs_orphan_cleanup(reloc_root);
7553 static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
7554 struct btrfs_root *root)
7556 struct btrfs_root *reloc_root;
7557 struct extent_buffer *eb;
7558 struct btrfs_root_item *root_item;
7559 struct btrfs_key root_key;
7562 BUG_ON(!root->ref_cows);
7563 if (root->reloc_root)
7566 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
7569 ret = btrfs_copy_root(trans, root, root->commit_root,
7570 &eb, BTRFS_TREE_RELOC_OBJECTID);
7573 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
7574 root_key.offset = root->root_key.objectid;
7575 root_key.type = BTRFS_ROOT_ITEM_KEY;
7577 memcpy(root_item, &root->root_item, sizeof(root_item));
7578 btrfs_set_root_refs(root_item, 0);
7579 btrfs_set_root_bytenr(root_item, eb->start);
7580 btrfs_set_root_level(root_item, btrfs_header_level(eb));
7581 btrfs_set_root_generation(root_item, trans->transid);
7583 btrfs_tree_unlock(eb);
7584 free_extent_buffer(eb);
7586 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
7587 &root_key, root_item);
7591 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
7593 BUG_ON(!reloc_root);
7594 reloc_root->last_trans = trans->transid;
7595 reloc_root->commit_root = NULL;
7596 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
7598 root->reloc_root = reloc_root;
7603 * Core function of space balance.
7605 * The idea is using reloc trees to relocate tree blocks in reference
7606 * counted roots. There is one reloc tree for each subvol, and all
7607 * reloc trees share same root key objectid. Reloc trees are snapshots
7608 * of the latest committed roots of subvols (root->commit_root).
7610 * To relocate a tree block referenced by a subvol, there are two steps.
7611 * COW the block through subvol's reloc tree, then update block pointer
7612 * in the subvol to point to the new block. Since all reloc trees share
7613 * same root key objectid, doing special handing for tree blocks owned
7614 * by them is easy. Once a tree block has been COWed in one reloc tree,
7615 * we can use the resulting new block directly when the same block is
7616 * required to COW again through other reloc trees. By this way, relocated
7617 * tree blocks are shared between reloc trees, so they are also shared
7620 static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
7621 struct btrfs_root *root,
7622 struct btrfs_path *path,
7623 struct btrfs_key *first_key,
7624 struct btrfs_ref_path *ref_path,
7625 struct btrfs_block_group_cache *group,
7626 struct inode *reloc_inode)
7628 struct btrfs_root *reloc_root;
7629 struct extent_buffer *eb = NULL;
7630 struct btrfs_key *keys;
7634 int lowest_level = 0;
7637 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
7638 lowest_level = ref_path->owner_objectid;
7640 if (!root->ref_cows) {
7641 path->lowest_level = lowest_level;
7642 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
7644 path->lowest_level = 0;
7645 btrfs_release_path(root, path);
7649 mutex_lock(&root->fs_info->tree_reloc_mutex);
7650 ret = init_reloc_tree(trans, root);
7652 reloc_root = root->reloc_root;
7654 shared_level = ref_path->shared_level;
7655 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
7657 keys = ref_path->node_keys;
7658 nodes = ref_path->new_nodes;
7659 memset(&keys[shared_level + 1], 0,
7660 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
7661 memset(&nodes[shared_level + 1], 0,
7662 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
7664 if (nodes[lowest_level] == 0) {
7665 path->lowest_level = lowest_level;
7666 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7669 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
7670 eb = path->nodes[level];
7671 if (!eb || eb == reloc_root->node)
7673 nodes[level] = eb->start;
7675 btrfs_item_key_to_cpu(eb, &keys[level], 0);
7677 btrfs_node_key_to_cpu(eb, &keys[level], 0);
7680 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7681 eb = path->nodes[0];
7682 ret = replace_extents_in_leaf(trans, reloc_root, eb,
7683 group, reloc_inode);
7686 btrfs_release_path(reloc_root, path);
7688 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
7694 * replace tree blocks in the fs tree with tree blocks in
7697 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
7700 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7701 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7704 extent_buffer_get(path->nodes[0]);
7705 eb = path->nodes[0];
7706 btrfs_release_path(reloc_root, path);
7707 ret = invalidate_extent_cache(reloc_root, eb, group, root);
7709 free_extent_buffer(eb);
7712 mutex_unlock(&root->fs_info->tree_reloc_mutex);
7713 path->lowest_level = 0;
7717 static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
7718 struct btrfs_root *root,
7719 struct btrfs_path *path,
7720 struct btrfs_key *first_key,
7721 struct btrfs_ref_path *ref_path)
7725 ret = relocate_one_path(trans, root, path, first_key,
7726 ref_path, NULL, NULL);
7732 static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
7733 struct btrfs_root *extent_root,
7734 struct btrfs_path *path,
7735 struct btrfs_key *extent_key)
7739 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
7742 ret = btrfs_del_item(trans, extent_root, path);
7744 btrfs_release_path(extent_root, path);
7748 static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
7749 struct btrfs_ref_path *ref_path)
7751 struct btrfs_key root_key;
7753 root_key.objectid = ref_path->root_objectid;
7754 root_key.type = BTRFS_ROOT_ITEM_KEY;
7755 if (is_cowonly_root(ref_path->root_objectid))
7756 root_key.offset = 0;
7758 root_key.offset = (u64)-1;
7760 return btrfs_read_fs_root_no_name(fs_info, &root_key);
7763 static noinline int relocate_one_extent(struct btrfs_root *extent_root,
7764 struct btrfs_path *path,
7765 struct btrfs_key *extent_key,
7766 struct btrfs_block_group_cache *group,
7767 struct inode *reloc_inode, int pass)
7769 struct btrfs_trans_handle *trans;
7770 struct btrfs_root *found_root;
7771 struct btrfs_ref_path *ref_path = NULL;
7772 struct disk_extent *new_extents = NULL;
7777 struct btrfs_key first_key;
7781 trans = btrfs_start_transaction(extent_root, 1);
7784 if (extent_key->objectid == 0) {
7785 ret = del_extent_zero(trans, extent_root, path, extent_key);
7789 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
7795 for (loops = 0; ; loops++) {
7797 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
7798 extent_key->objectid);
7800 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
7807 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
7808 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
7811 found_root = read_ref_root(extent_root->fs_info, ref_path);
7812 BUG_ON(!found_root);
7814 * for reference counted tree, only process reference paths
7815 * rooted at the latest committed root.
7817 if (found_root->ref_cows &&
7818 ref_path->root_generation != found_root->root_key.offset)
7821 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7824 * copy data extents to new locations
7826 u64 group_start = group->key.objectid;
7827 ret = relocate_data_extent(reloc_inode,
7836 level = ref_path->owner_objectid;
7839 if (prev_block != ref_path->nodes[level]) {
7840 struct extent_buffer *eb;
7841 u64 block_start = ref_path->nodes[level];
7842 u64 block_size = btrfs_level_size(found_root, level);
7844 eb = read_tree_block(found_root, block_start,
7846 btrfs_tree_lock(eb);
7847 BUG_ON(level != btrfs_header_level(eb));
7850 btrfs_item_key_to_cpu(eb, &first_key, 0);
7852 btrfs_node_key_to_cpu(eb, &first_key, 0);
7854 btrfs_tree_unlock(eb);
7855 free_extent_buffer(eb);
7856 prev_block = block_start;
7859 mutex_lock(&extent_root->fs_info->trans_mutex);
7860 btrfs_record_root_in_trans(found_root);
7861 mutex_unlock(&extent_root->fs_info->trans_mutex);
7862 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7864 * try to update data extent references while
7865 * keeping metadata shared between snapshots.
7868 ret = relocate_one_path(trans, found_root,
7869 path, &first_key, ref_path,
7870 group, reloc_inode);
7876 * use fallback method to process the remaining
7880 u64 group_start = group->key.objectid;
7881 new_extents = kmalloc(sizeof(*new_extents),
7884 ret = get_new_locations(reloc_inode,
7892 ret = replace_one_extent(trans, found_root,
7894 &first_key, ref_path,
7895 new_extents, nr_extents);
7897 ret = relocate_tree_block(trans, found_root, path,
7898 &first_key, ref_path);
7905 btrfs_end_transaction(trans, extent_root);
7912 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7915 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
7916 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7919 * we add in the count of missing devices because we want
7920 * to make sure that any RAID levels on a degraded FS
7921 * continue to be honored.
7923 num_devices = root->fs_info->fs_devices->rw_devices +
7924 root->fs_info->fs_devices->missing_devices;
7926 if (num_devices == 1) {
7927 stripped |= BTRFS_BLOCK_GROUP_DUP;
7928 stripped = flags & ~stripped;
7930 /* turn raid0 into single device chunks */
7931 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7934 /* turn mirroring into duplication */
7935 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7936 BTRFS_BLOCK_GROUP_RAID10))
7937 return stripped | BTRFS_BLOCK_GROUP_DUP;
7940 /* they already had raid on here, just return */
7941 if (flags & stripped)
7944 stripped |= BTRFS_BLOCK_GROUP_DUP;
7945 stripped = flags & ~stripped;
7947 /* switch duplicated blocks with raid1 */
7948 if (flags & BTRFS_BLOCK_GROUP_DUP)
7949 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7951 /* turn single device chunks into raid0 */
7952 return stripped | BTRFS_BLOCK_GROUP_RAID0;
7957 static int set_block_group_ro(struct btrfs_block_group_cache *cache)
7959 struct btrfs_space_info *sinfo = cache->space_info;
7966 spin_lock(&sinfo->lock);
7967 spin_lock(&cache->lock);
7968 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7969 cache->bytes_super - btrfs_block_group_used(&cache->item);
7971 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
7972 sinfo->bytes_may_use + sinfo->bytes_readonly +
7973 cache->reserved_pinned + num_bytes < sinfo->total_bytes) {
7974 sinfo->bytes_readonly += num_bytes;
7975 sinfo->bytes_reserved += cache->reserved_pinned;
7976 cache->reserved_pinned = 0;
7980 spin_unlock(&cache->lock);
7981 spin_unlock(&sinfo->lock);
7985 int btrfs_set_block_group_ro(struct btrfs_root *root,
7986 struct btrfs_block_group_cache *cache)
7989 struct btrfs_trans_handle *trans;
7995 trans = btrfs_join_transaction(root, 1);
7996 BUG_ON(IS_ERR(trans));
7998 alloc_flags = update_block_group_flags(root, cache->flags);
7999 if (alloc_flags != cache->flags)
8000 do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1);
8002 ret = set_block_group_ro(cache);
8005 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
8006 ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1);
8009 ret = set_block_group_ro(cache);
8011 btrfs_end_transaction(trans, root);
8015 int btrfs_set_block_group_rw(struct btrfs_root *root,
8016 struct btrfs_block_group_cache *cache)
8018 struct btrfs_space_info *sinfo = cache->space_info;
8023 spin_lock(&sinfo->lock);
8024 spin_lock(&cache->lock);
8025 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8026 cache->bytes_super - btrfs_block_group_used(&cache->item);
8027 sinfo->bytes_readonly -= num_bytes;
8029 spin_unlock(&cache->lock);
8030 spin_unlock(&sinfo->lock);
8035 * checks to see if its even possible to relocate this block group.
8037 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
8038 * ok to go ahead and try.
8040 int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
8042 struct btrfs_block_group_cache *block_group;
8043 struct btrfs_space_info *space_info;
8044 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
8045 struct btrfs_device *device;
8049 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
8051 /* odd, couldn't find the block group, leave it alone */
8055 /* no bytes used, we're good */
8056 if (!btrfs_block_group_used(&block_group->item))
8059 space_info = block_group->space_info;
8060 spin_lock(&space_info->lock);
8062 full = space_info->full;
8065 * if this is the last block group we have in this space, we can't
8066 * relocate it unless we're able to allocate a new chunk below.
8068 * Otherwise, we need to make sure we have room in the space to handle
8069 * all of the extents from this block group. If we can, we're good
8071 if ((space_info->total_bytes != block_group->key.offset) &&
8072 (space_info->bytes_used + space_info->bytes_reserved +
8073 space_info->bytes_pinned + space_info->bytes_readonly +
8074 btrfs_block_group_used(&block_group->item) <
8075 space_info->total_bytes)) {
8076 spin_unlock(&space_info->lock);
8079 spin_unlock(&space_info->lock);
8082 * ok we don't have enough space, but maybe we have free space on our
8083 * devices to allocate new chunks for relocation, so loop through our
8084 * alloc devices and guess if we have enough space. However, if we
8085 * were marked as full, then we know there aren't enough chunks, and we
8092 mutex_lock(&root->fs_info->chunk_mutex);
8093 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
8094 u64 min_free = btrfs_block_group_used(&block_group->item);
8095 u64 dev_offset, max_avail;
8098 * check to make sure we can actually find a chunk with enough
8099 * space to fit our block group in.
8101 if (device->total_bytes > device->bytes_used + min_free) {
8102 ret = find_free_dev_extent(NULL, device, min_free,
8103 &dev_offset, &max_avail);
8109 mutex_unlock(&root->fs_info->chunk_mutex);
8111 btrfs_put_block_group(block_group);
8115 static int find_first_block_group(struct btrfs_root *root,
8116 struct btrfs_path *path, struct btrfs_key *key)
8119 struct btrfs_key found_key;
8120 struct extent_buffer *leaf;
8123 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
8128 slot = path->slots[0];
8129 leaf = path->nodes[0];
8130 if (slot >= btrfs_header_nritems(leaf)) {
8131 ret = btrfs_next_leaf(root, path);
8138 btrfs_item_key_to_cpu(leaf, &found_key, slot);
8140 if (found_key.objectid >= key->objectid &&
8141 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
8151 void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
8153 struct btrfs_block_group_cache *block_group;
8157 struct inode *inode;
8159 block_group = btrfs_lookup_first_block_group(info, last);
8160 while (block_group) {
8161 spin_lock(&block_group->lock);
8162 if (block_group->iref)
8164 spin_unlock(&block_group->lock);
8165 block_group = next_block_group(info->tree_root,
8175 inode = block_group->inode;
8176 block_group->iref = 0;
8177 block_group->inode = NULL;
8178 spin_unlock(&block_group->lock);
8180 last = block_group->key.objectid + block_group->key.offset;
8181 btrfs_put_block_group(block_group);
8185 int btrfs_free_block_groups(struct btrfs_fs_info *info)
8187 struct btrfs_block_group_cache *block_group;
8188 struct btrfs_space_info *space_info;
8189 struct btrfs_caching_control *caching_ctl;
8192 down_write(&info->extent_commit_sem);
8193 while (!list_empty(&info->caching_block_groups)) {
8194 caching_ctl = list_entry(info->caching_block_groups.next,
8195 struct btrfs_caching_control, list);
8196 list_del(&caching_ctl->list);
8197 put_caching_control(caching_ctl);
8199 up_write(&info->extent_commit_sem);
8201 spin_lock(&info->block_group_cache_lock);
8202 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
8203 block_group = rb_entry(n, struct btrfs_block_group_cache,
8205 rb_erase(&block_group->cache_node,
8206 &info->block_group_cache_tree);
8207 spin_unlock(&info->block_group_cache_lock);
8209 down_write(&block_group->space_info->groups_sem);
8210 list_del(&block_group->list);
8211 up_write(&block_group->space_info->groups_sem);
8213 if (block_group->cached == BTRFS_CACHE_STARTED)
8214 wait_block_group_cache_done(block_group);
8216 btrfs_remove_free_space_cache(block_group);
8217 btrfs_put_block_group(block_group);
8219 spin_lock(&info->block_group_cache_lock);
8221 spin_unlock(&info->block_group_cache_lock);
8223 /* now that all the block groups are freed, go through and
8224 * free all the space_info structs. This is only called during
8225 * the final stages of unmount, and so we know nobody is
8226 * using them. We call synchronize_rcu() once before we start,
8227 * just to be on the safe side.
8231 release_global_block_rsv(info);
8233 while(!list_empty(&info->space_info)) {
8234 space_info = list_entry(info->space_info.next,
8235 struct btrfs_space_info,
8237 if (space_info->bytes_pinned > 0 ||
8238 space_info->bytes_reserved > 0) {
8240 dump_space_info(space_info, 0, 0);
8242 list_del(&space_info->list);
8248 static void __link_block_group(struct btrfs_space_info *space_info,
8249 struct btrfs_block_group_cache *cache)
8251 int index = get_block_group_index(cache);
8253 down_write(&space_info->groups_sem);
8254 list_add_tail(&cache->list, &space_info->block_groups[index]);
8255 up_write(&space_info->groups_sem);
8258 int btrfs_read_block_groups(struct btrfs_root *root)
8260 struct btrfs_path *path;
8262 struct btrfs_block_group_cache *cache;
8263 struct btrfs_fs_info *info = root->fs_info;
8264 struct btrfs_space_info *space_info;
8265 struct btrfs_key key;
8266 struct btrfs_key found_key;
8267 struct extent_buffer *leaf;
8271 root = info->extent_root;
8274 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
8275 path = btrfs_alloc_path();
8279 cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy);
8280 if (cache_gen != 0 &&
8281 btrfs_super_generation(&root->fs_info->super_copy) != cache_gen)
8283 if (btrfs_test_opt(root, CLEAR_CACHE))
8285 if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen)
8286 printk(KERN_INFO "btrfs: disk space caching is enabled\n");
8289 ret = find_first_block_group(root, path, &key);
8294 leaf = path->nodes[0];
8295 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
8296 cache = kzalloc(sizeof(*cache), GFP_NOFS);
8302 atomic_set(&cache->count, 1);
8303 spin_lock_init(&cache->lock);
8304 spin_lock_init(&cache->tree_lock);
8305 cache->fs_info = info;
8306 INIT_LIST_HEAD(&cache->list);
8307 INIT_LIST_HEAD(&cache->cluster_list);
8310 cache->disk_cache_state = BTRFS_DC_CLEAR;
8313 * we only want to have 32k of ram per block group for keeping
8314 * track of free space, and if we pass 1/2 of that we want to
8315 * start converting things over to using bitmaps
8317 cache->extents_thresh = ((1024 * 32) / 2) /
8318 sizeof(struct btrfs_free_space);
8320 read_extent_buffer(leaf, &cache->item,
8321 btrfs_item_ptr_offset(leaf, path->slots[0]),
8322 sizeof(cache->item));
8323 memcpy(&cache->key, &found_key, sizeof(found_key));
8325 key.objectid = found_key.objectid + found_key.offset;
8326 btrfs_release_path(root, path);
8327 cache->flags = btrfs_block_group_flags(&cache->item);
8328 cache->sectorsize = root->sectorsize;
8331 * check for two cases, either we are full, and therefore
8332 * don't need to bother with the caching work since we won't
8333 * find any space, or we are empty, and we can just add all
8334 * the space in and be done with it. This saves us _alot_ of
8335 * time, particularly in the full case.
8337 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
8338 exclude_super_stripes(root, cache);
8339 cache->last_byte_to_unpin = (u64)-1;
8340 cache->cached = BTRFS_CACHE_FINISHED;
8341 free_excluded_extents(root, cache);
8342 } else if (btrfs_block_group_used(&cache->item) == 0) {
8343 exclude_super_stripes(root, cache);
8344 cache->last_byte_to_unpin = (u64)-1;
8345 cache->cached = BTRFS_CACHE_FINISHED;
8346 add_new_free_space(cache, root->fs_info,
8348 found_key.objectid +
8350 free_excluded_extents(root, cache);
8353 ret = update_space_info(info, cache->flags, found_key.offset,
8354 btrfs_block_group_used(&cache->item),
8357 cache->space_info = space_info;
8358 spin_lock(&cache->space_info->lock);
8359 cache->space_info->bytes_readonly += cache->bytes_super;
8360 spin_unlock(&cache->space_info->lock);
8362 __link_block_group(space_info, cache);
8364 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8367 set_avail_alloc_bits(root->fs_info, cache->flags);
8368 if (btrfs_chunk_readonly(root, cache->key.objectid))
8369 set_block_group_ro(cache);
8372 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
8373 if (!(get_alloc_profile(root, space_info->flags) &
8374 (BTRFS_BLOCK_GROUP_RAID10 |
8375 BTRFS_BLOCK_GROUP_RAID1 |
8376 BTRFS_BLOCK_GROUP_DUP)))
8379 * avoid allocating from un-mirrored block group if there are
8380 * mirrored block groups.
8382 list_for_each_entry(cache, &space_info->block_groups[3], list)
8383 set_block_group_ro(cache);
8384 list_for_each_entry(cache, &space_info->block_groups[4], list)
8385 set_block_group_ro(cache);
8388 init_global_block_rsv(info);
8391 btrfs_free_path(path);
8395 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8396 struct btrfs_root *root, u64 bytes_used,
8397 u64 type, u64 chunk_objectid, u64 chunk_offset,
8401 struct btrfs_root *extent_root;
8402 struct btrfs_block_group_cache *cache;
8404 extent_root = root->fs_info->extent_root;
8406 root->fs_info->last_trans_log_full_commit = trans->transid;
8408 cache = kzalloc(sizeof(*cache), GFP_NOFS);
8412 cache->key.objectid = chunk_offset;
8413 cache->key.offset = size;
8414 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
8415 cache->sectorsize = root->sectorsize;
8416 cache->fs_info = root->fs_info;
8419 * we only want to have 32k of ram per block group for keeping track
8420 * of free space, and if we pass 1/2 of that we want to start
8421 * converting things over to using bitmaps
8423 cache->extents_thresh = ((1024 * 32) / 2) /
8424 sizeof(struct btrfs_free_space);
8425 atomic_set(&cache->count, 1);
8426 spin_lock_init(&cache->lock);
8427 spin_lock_init(&cache->tree_lock);
8428 INIT_LIST_HEAD(&cache->list);
8429 INIT_LIST_HEAD(&cache->cluster_list);
8431 btrfs_set_block_group_used(&cache->item, bytes_used);
8432 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
8433 cache->flags = type;
8434 btrfs_set_block_group_flags(&cache->item, type);
8436 cache->last_byte_to_unpin = (u64)-1;
8437 cache->cached = BTRFS_CACHE_FINISHED;
8438 exclude_super_stripes(root, cache);
8440 add_new_free_space(cache, root->fs_info, chunk_offset,
8441 chunk_offset + size);
8443 free_excluded_extents(root, cache);
8445 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8446 &cache->space_info);
8449 spin_lock(&cache->space_info->lock);
8450 cache->space_info->bytes_readonly += cache->bytes_super;
8451 spin_unlock(&cache->space_info->lock);
8453 __link_block_group(cache->space_info, cache);
8455 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8458 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
8459 sizeof(cache->item));
8462 set_avail_alloc_bits(extent_root->fs_info, type);
8467 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8468 struct btrfs_root *root, u64 group_start)
8470 struct btrfs_path *path;
8471 struct btrfs_block_group_cache *block_group;
8472 struct btrfs_free_cluster *cluster;
8473 struct btrfs_root *tree_root = root->fs_info->tree_root;
8474 struct btrfs_key key;
8475 struct inode *inode;
8479 root = root->fs_info->extent_root;
8481 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8482 BUG_ON(!block_group);
8483 BUG_ON(!block_group->ro);
8485 memcpy(&key, &block_group->key, sizeof(key));
8486 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
8487 BTRFS_BLOCK_GROUP_RAID1 |
8488 BTRFS_BLOCK_GROUP_RAID10))
8493 /* make sure this block group isn't part of an allocation cluster */
8494 cluster = &root->fs_info->data_alloc_cluster;
8495 spin_lock(&cluster->refill_lock);
8496 btrfs_return_cluster_to_free_space(block_group, cluster);
8497 spin_unlock(&cluster->refill_lock);
8500 * make sure this block group isn't part of a metadata
8501 * allocation cluster
8503 cluster = &root->fs_info->meta_alloc_cluster;
8504 spin_lock(&cluster->refill_lock);
8505 btrfs_return_cluster_to_free_space(block_group, cluster);
8506 spin_unlock(&cluster->refill_lock);
8508 path = btrfs_alloc_path();
8511 inode = lookup_free_space_inode(root, block_group, path);
8512 if (!IS_ERR(inode)) {
8513 btrfs_orphan_add(trans, inode);
8515 /* One for the block groups ref */
8516 spin_lock(&block_group->lock);
8517 if (block_group->iref) {
8518 block_group->iref = 0;
8519 block_group->inode = NULL;
8520 spin_unlock(&block_group->lock);
8523 spin_unlock(&block_group->lock);
8525 /* One for our lookup ref */
8529 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8530 key.offset = block_group->key.objectid;
8533 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8537 btrfs_release_path(tree_root, path);
8539 ret = btrfs_del_item(trans, tree_root, path);
8542 btrfs_release_path(tree_root, path);
8545 spin_lock(&root->fs_info->block_group_cache_lock);
8546 rb_erase(&block_group->cache_node,
8547 &root->fs_info->block_group_cache_tree);
8548 spin_unlock(&root->fs_info->block_group_cache_lock);
8550 down_write(&block_group->space_info->groups_sem);
8552 * we must use list_del_init so people can check to see if they
8553 * are still on the list after taking the semaphore
8555 list_del_init(&block_group->list);
8556 up_write(&block_group->space_info->groups_sem);
8558 if (block_group->cached == BTRFS_CACHE_STARTED)
8559 wait_block_group_cache_done(block_group);
8561 btrfs_remove_free_space_cache(block_group);
8563 spin_lock(&block_group->space_info->lock);
8564 block_group->space_info->total_bytes -= block_group->key.offset;
8565 block_group->space_info->bytes_readonly -= block_group->key.offset;
8566 block_group->space_info->disk_total -= block_group->key.offset * factor;
8567 spin_unlock(&block_group->space_info->lock);
8569 memcpy(&key, &block_group->key, sizeof(key));
8571 btrfs_clear_space_info_full(root->fs_info);
8573 btrfs_put_block_group(block_group);
8574 btrfs_put_block_group(block_group);
8576 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8582 ret = btrfs_del_item(trans, root, path);
8584 btrfs_free_path(path);