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);
67 static int maybe_allocate_chunk(struct btrfs_trans_handle *trans,
68 struct btrfs_root *root,
69 struct btrfs_space_info *sinfo, u64 num_bytes);
70 static int shrink_delalloc(struct btrfs_trans_handle *trans,
71 struct btrfs_root *root,
72 struct btrfs_space_info *sinfo, u64 to_reclaim);
75 block_group_cache_done(struct btrfs_block_group_cache *cache)
78 return cache->cached == BTRFS_CACHE_FINISHED;
81 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
83 return (cache->flags & bits) == bits;
86 void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
88 atomic_inc(&cache->count);
91 void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
93 if (atomic_dec_and_test(&cache->count)) {
94 WARN_ON(cache->pinned > 0);
95 WARN_ON(cache->reserved > 0);
96 WARN_ON(cache->reserved_pinned > 0);
102 * this adds the block group to the fs_info rb tree for the block group
105 static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
106 struct btrfs_block_group_cache *block_group)
109 struct rb_node *parent = NULL;
110 struct btrfs_block_group_cache *cache;
112 spin_lock(&info->block_group_cache_lock);
113 p = &info->block_group_cache_tree.rb_node;
117 cache = rb_entry(parent, struct btrfs_block_group_cache,
119 if (block_group->key.objectid < cache->key.objectid) {
121 } else if (block_group->key.objectid > cache->key.objectid) {
124 spin_unlock(&info->block_group_cache_lock);
129 rb_link_node(&block_group->cache_node, parent, p);
130 rb_insert_color(&block_group->cache_node,
131 &info->block_group_cache_tree);
132 spin_unlock(&info->block_group_cache_lock);
138 * This will return the block group at or after bytenr if contains is 0, else
139 * it will return the block group that contains the bytenr
141 static struct btrfs_block_group_cache *
142 block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
145 struct btrfs_block_group_cache *cache, *ret = NULL;
149 spin_lock(&info->block_group_cache_lock);
150 n = info->block_group_cache_tree.rb_node;
153 cache = rb_entry(n, struct btrfs_block_group_cache,
155 end = cache->key.objectid + cache->key.offset - 1;
156 start = cache->key.objectid;
158 if (bytenr < start) {
159 if (!contains && (!ret || start < ret->key.objectid))
162 } else if (bytenr > start) {
163 if (contains && bytenr <= end) {
174 btrfs_get_block_group(ret);
175 spin_unlock(&info->block_group_cache_lock);
180 static int add_excluded_extent(struct btrfs_root *root,
181 u64 start, u64 num_bytes)
183 u64 end = start + num_bytes - 1;
184 set_extent_bits(&root->fs_info->freed_extents[0],
185 start, end, EXTENT_UPTODATE, GFP_NOFS);
186 set_extent_bits(&root->fs_info->freed_extents[1],
187 start, end, EXTENT_UPTODATE, GFP_NOFS);
191 static void free_excluded_extents(struct btrfs_root *root,
192 struct btrfs_block_group_cache *cache)
196 start = cache->key.objectid;
197 end = start + cache->key.offset - 1;
199 clear_extent_bits(&root->fs_info->freed_extents[0],
200 start, end, EXTENT_UPTODATE, GFP_NOFS);
201 clear_extent_bits(&root->fs_info->freed_extents[1],
202 start, end, EXTENT_UPTODATE, GFP_NOFS);
205 static int exclude_super_stripes(struct btrfs_root *root,
206 struct btrfs_block_group_cache *cache)
213 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
214 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
215 cache->bytes_super += stripe_len;
216 ret = add_excluded_extent(root, cache->key.objectid,
221 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
222 bytenr = btrfs_sb_offset(i);
223 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
224 cache->key.objectid, bytenr,
225 0, &logical, &nr, &stripe_len);
229 cache->bytes_super += stripe_len;
230 ret = add_excluded_extent(root, logical[nr],
240 static struct btrfs_caching_control *
241 get_caching_control(struct btrfs_block_group_cache *cache)
243 struct btrfs_caching_control *ctl;
245 spin_lock(&cache->lock);
246 if (cache->cached != BTRFS_CACHE_STARTED) {
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)
432 struct btrfs_fs_info *fs_info = cache->fs_info;
433 struct btrfs_caching_control *caching_ctl;
434 struct task_struct *tsk;
438 if (cache->cached != BTRFS_CACHE_NO)
441 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL);
442 BUG_ON(!caching_ctl);
444 INIT_LIST_HEAD(&caching_ctl->list);
445 mutex_init(&caching_ctl->mutex);
446 init_waitqueue_head(&caching_ctl->wait);
447 caching_ctl->block_group = cache;
448 caching_ctl->progress = cache->key.objectid;
449 /* one for caching kthread, one for caching block group list */
450 atomic_set(&caching_ctl->count, 2);
452 spin_lock(&cache->lock);
453 if (cache->cached != BTRFS_CACHE_NO) {
454 spin_unlock(&cache->lock);
458 cache->caching_ctl = caching_ctl;
459 cache->cached = BTRFS_CACHE_STARTED;
460 spin_unlock(&cache->lock);
462 down_write(&fs_info->extent_commit_sem);
463 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
464 up_write(&fs_info->extent_commit_sem);
466 atomic_inc(&cache->space_info->caching_threads);
467 btrfs_get_block_group(cache);
469 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
470 cache->key.objectid);
473 printk(KERN_ERR "error running thread %d\n", ret);
481 * return the block group that starts at or after bytenr
483 static struct btrfs_block_group_cache *
484 btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
486 struct btrfs_block_group_cache *cache;
488 cache = block_group_cache_tree_search(info, bytenr, 0);
494 * return the block group that contains the given bytenr
496 struct btrfs_block_group_cache *btrfs_lookup_block_group(
497 struct btrfs_fs_info *info,
500 struct btrfs_block_group_cache *cache;
502 cache = block_group_cache_tree_search(info, bytenr, 1);
507 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
510 struct list_head *head = &info->space_info;
511 struct btrfs_space_info *found;
513 flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM |
514 BTRFS_BLOCK_GROUP_METADATA;
517 list_for_each_entry_rcu(found, head, list) {
518 if (found->flags == flags) {
528 * after adding space to the filesystem, we need to clear the full flags
529 * on all the space infos.
531 void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
533 struct list_head *head = &info->space_info;
534 struct btrfs_space_info *found;
537 list_for_each_entry_rcu(found, head, list)
542 static u64 div_factor(u64 num, int factor)
551 u64 btrfs_find_block_group(struct btrfs_root *root,
552 u64 search_start, u64 search_hint, int owner)
554 struct btrfs_block_group_cache *cache;
556 u64 last = max(search_hint, search_start);
563 cache = btrfs_lookup_first_block_group(root->fs_info, last);
567 spin_lock(&cache->lock);
568 last = cache->key.objectid + cache->key.offset;
569 used = btrfs_block_group_used(&cache->item);
571 if ((full_search || !cache->ro) &&
572 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
573 if (used + cache->pinned + cache->reserved <
574 div_factor(cache->key.offset, factor)) {
575 group_start = cache->key.objectid;
576 spin_unlock(&cache->lock);
577 btrfs_put_block_group(cache);
581 spin_unlock(&cache->lock);
582 btrfs_put_block_group(cache);
590 if (!full_search && factor < 10) {
600 /* simple helper to search for an existing extent at a given offset */
601 int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
604 struct btrfs_key key;
605 struct btrfs_path *path;
607 path = btrfs_alloc_path();
609 key.objectid = start;
611 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
612 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
614 btrfs_free_path(path);
619 * helper function to lookup reference count and flags of extent.
621 * the head node for delayed ref is used to store the sum of all the
622 * reference count modifications queued up in the rbtree. the head
623 * node may also store the extent flags to set. This way you can check
624 * to see what the reference count and extent flags would be if all of
625 * the delayed refs are not processed.
627 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
628 struct btrfs_root *root, u64 bytenr,
629 u64 num_bytes, u64 *refs, u64 *flags)
631 struct btrfs_delayed_ref_head *head;
632 struct btrfs_delayed_ref_root *delayed_refs;
633 struct btrfs_path *path;
634 struct btrfs_extent_item *ei;
635 struct extent_buffer *leaf;
636 struct btrfs_key key;
642 path = btrfs_alloc_path();
646 key.objectid = bytenr;
647 key.type = BTRFS_EXTENT_ITEM_KEY;
648 key.offset = num_bytes;
650 path->skip_locking = 1;
651 path->search_commit_root = 1;
654 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
660 leaf = path->nodes[0];
661 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
662 if (item_size >= sizeof(*ei)) {
663 ei = btrfs_item_ptr(leaf, path->slots[0],
664 struct btrfs_extent_item);
665 num_refs = btrfs_extent_refs(leaf, ei);
666 extent_flags = btrfs_extent_flags(leaf, ei);
668 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
669 struct btrfs_extent_item_v0 *ei0;
670 BUG_ON(item_size != sizeof(*ei0));
671 ei0 = btrfs_item_ptr(leaf, path->slots[0],
672 struct btrfs_extent_item_v0);
673 num_refs = btrfs_extent_refs_v0(leaf, ei0);
674 /* FIXME: this isn't correct for data */
675 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
680 BUG_ON(num_refs == 0);
690 delayed_refs = &trans->transaction->delayed_refs;
691 spin_lock(&delayed_refs->lock);
692 head = btrfs_find_delayed_ref_head(trans, bytenr);
694 if (!mutex_trylock(&head->mutex)) {
695 atomic_inc(&head->node.refs);
696 spin_unlock(&delayed_refs->lock);
698 btrfs_release_path(root->fs_info->extent_root, path);
700 mutex_lock(&head->mutex);
701 mutex_unlock(&head->mutex);
702 btrfs_put_delayed_ref(&head->node);
705 if (head->extent_op && head->extent_op->update_flags)
706 extent_flags |= head->extent_op->flags_to_set;
708 BUG_ON(num_refs == 0);
710 num_refs += head->node.ref_mod;
711 mutex_unlock(&head->mutex);
713 spin_unlock(&delayed_refs->lock);
715 WARN_ON(num_refs == 0);
719 *flags = extent_flags;
721 btrfs_free_path(path);
726 * Back reference rules. Back refs have three main goals:
728 * 1) differentiate between all holders of references to an extent so that
729 * when a reference is dropped we can make sure it was a valid reference
730 * before freeing the extent.
732 * 2) Provide enough information to quickly find the holders of an extent
733 * if we notice a given block is corrupted or bad.
735 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
736 * maintenance. This is actually the same as #2, but with a slightly
737 * different use case.
739 * There are two kinds of back refs. The implicit back refs is optimized
740 * for pointers in non-shared tree blocks. For a given pointer in a block,
741 * back refs of this kind provide information about the block's owner tree
742 * and the pointer's key. These information allow us to find the block by
743 * b-tree searching. The full back refs is for pointers in tree blocks not
744 * referenced by their owner trees. The location of tree block is recorded
745 * in the back refs. Actually the full back refs is generic, and can be
746 * used in all cases the implicit back refs is used. The major shortcoming
747 * of the full back refs is its overhead. Every time a tree block gets
748 * COWed, we have to update back refs entry for all pointers in it.
750 * For a newly allocated tree block, we use implicit back refs for
751 * pointers in it. This means most tree related operations only involve
752 * implicit back refs. For a tree block created in old transaction, the
753 * only way to drop a reference to it is COW it. So we can detect the
754 * event that tree block loses its owner tree's reference and do the
755 * back refs conversion.
757 * When a tree block is COW'd through a tree, there are four cases:
759 * The reference count of the block is one and the tree is the block's
760 * owner tree. Nothing to do in this case.
762 * The reference count of the block is one and the tree is not the
763 * block's owner tree. In this case, full back refs is used for pointers
764 * in the block. Remove these full back refs, add implicit back refs for
765 * every pointers in the new block.
767 * The reference count of the block is greater than one and the tree is
768 * the block's owner tree. In this case, implicit back refs is used for
769 * pointers in the block. Add full back refs for every pointers in the
770 * block, increase lower level extents' reference counts. The original
771 * implicit back refs are entailed to the new block.
773 * The reference count of the block is greater than one and the tree is
774 * not the block's owner tree. Add implicit back refs for every pointer in
775 * the new block, increase lower level extents' reference count.
777 * Back Reference Key composing:
779 * The key objectid corresponds to the first byte in the extent,
780 * The key type is used to differentiate between types of back refs.
781 * There are different meanings of the key offset for different types
784 * File extents can be referenced by:
786 * - multiple snapshots, subvolumes, or different generations in one subvol
787 * - different files inside a single subvolume
788 * - different offsets inside a file (bookend extents in file.c)
790 * The extent ref structure for the implicit back refs has fields for:
792 * - Objectid of the subvolume root
793 * - objectid of the file holding the reference
794 * - original offset in the file
795 * - how many bookend extents
797 * The key offset for the implicit back refs is hash of the first
800 * The extent ref structure for the full back refs has field for:
802 * - number of pointers in the tree leaf
804 * The key offset for the implicit back refs is the first byte of
807 * When a file extent is allocated, The implicit back refs is used.
808 * the fields are filled in:
810 * (root_key.objectid, inode objectid, offset in file, 1)
812 * When a file extent is removed file truncation, we find the
813 * corresponding implicit back refs and check the following fields:
815 * (btrfs_header_owner(leaf), inode objectid, offset in file)
817 * Btree extents can be referenced by:
819 * - Different subvolumes
821 * Both the implicit back refs and the full back refs for tree blocks
822 * only consist of key. The key offset for the implicit back refs is
823 * objectid of block's owner tree. The key offset for the full back refs
824 * is the first byte of parent block.
826 * When implicit back refs is used, information about the lowest key and
827 * level of the tree block are required. These information are stored in
828 * tree block info structure.
831 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
832 static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
833 struct btrfs_root *root,
834 struct btrfs_path *path,
835 u64 owner, u32 extra_size)
837 struct btrfs_extent_item *item;
838 struct btrfs_extent_item_v0 *ei0;
839 struct btrfs_extent_ref_v0 *ref0;
840 struct btrfs_tree_block_info *bi;
841 struct extent_buffer *leaf;
842 struct btrfs_key key;
843 struct btrfs_key found_key;
844 u32 new_size = sizeof(*item);
848 leaf = path->nodes[0];
849 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
851 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
852 ei0 = btrfs_item_ptr(leaf, path->slots[0],
853 struct btrfs_extent_item_v0);
854 refs = btrfs_extent_refs_v0(leaf, ei0);
856 if (owner == (u64)-1) {
858 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
859 ret = btrfs_next_leaf(root, path);
863 leaf = path->nodes[0];
865 btrfs_item_key_to_cpu(leaf, &found_key,
867 BUG_ON(key.objectid != found_key.objectid);
868 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
872 ref0 = btrfs_item_ptr(leaf, path->slots[0],
873 struct btrfs_extent_ref_v0);
874 owner = btrfs_ref_objectid_v0(leaf, ref0);
878 btrfs_release_path(root, path);
880 if (owner < BTRFS_FIRST_FREE_OBJECTID)
881 new_size += sizeof(*bi);
883 new_size -= sizeof(*ei0);
884 ret = btrfs_search_slot(trans, root, &key, path,
885 new_size + extra_size, 1);
890 ret = btrfs_extend_item(trans, root, path, new_size);
893 leaf = path->nodes[0];
894 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
895 btrfs_set_extent_refs(leaf, item, refs);
896 /* FIXME: get real generation */
897 btrfs_set_extent_generation(leaf, item, 0);
898 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
899 btrfs_set_extent_flags(leaf, item,
900 BTRFS_EXTENT_FLAG_TREE_BLOCK |
901 BTRFS_BLOCK_FLAG_FULL_BACKREF);
902 bi = (struct btrfs_tree_block_info *)(item + 1);
903 /* FIXME: get first key of the block */
904 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
905 btrfs_set_tree_block_level(leaf, bi, (int)owner);
907 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
909 btrfs_mark_buffer_dirty(leaf);
914 static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
916 u32 high_crc = ~(u32)0;
917 u32 low_crc = ~(u32)0;
920 lenum = cpu_to_le64(root_objectid);
921 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
922 lenum = cpu_to_le64(owner);
923 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
924 lenum = cpu_to_le64(offset);
925 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
927 return ((u64)high_crc << 31) ^ (u64)low_crc;
930 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
931 struct btrfs_extent_data_ref *ref)
933 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
934 btrfs_extent_data_ref_objectid(leaf, ref),
935 btrfs_extent_data_ref_offset(leaf, ref));
938 static int match_extent_data_ref(struct extent_buffer *leaf,
939 struct btrfs_extent_data_ref *ref,
940 u64 root_objectid, u64 owner, u64 offset)
942 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
943 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
944 btrfs_extent_data_ref_offset(leaf, ref) != offset)
949 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
950 struct btrfs_root *root,
951 struct btrfs_path *path,
952 u64 bytenr, u64 parent,
954 u64 owner, u64 offset)
956 struct btrfs_key key;
957 struct btrfs_extent_data_ref *ref;
958 struct extent_buffer *leaf;
964 key.objectid = bytenr;
966 key.type = BTRFS_SHARED_DATA_REF_KEY;
969 key.type = BTRFS_EXTENT_DATA_REF_KEY;
970 key.offset = hash_extent_data_ref(root_objectid,
975 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
984 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
985 key.type = BTRFS_EXTENT_REF_V0_KEY;
986 btrfs_release_path(root, path);
987 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
998 leaf = path->nodes[0];
999 nritems = btrfs_header_nritems(leaf);
1001 if (path->slots[0] >= nritems) {
1002 ret = btrfs_next_leaf(root, path);
1008 leaf = path->nodes[0];
1009 nritems = btrfs_header_nritems(leaf);
1013 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1014 if (key.objectid != bytenr ||
1015 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1018 ref = btrfs_item_ptr(leaf, path->slots[0],
1019 struct btrfs_extent_data_ref);
1021 if (match_extent_data_ref(leaf, ref, root_objectid,
1024 btrfs_release_path(root, path);
1036 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1037 struct btrfs_root *root,
1038 struct btrfs_path *path,
1039 u64 bytenr, u64 parent,
1040 u64 root_objectid, u64 owner,
1041 u64 offset, int refs_to_add)
1043 struct btrfs_key key;
1044 struct extent_buffer *leaf;
1049 key.objectid = bytenr;
1051 key.type = BTRFS_SHARED_DATA_REF_KEY;
1052 key.offset = parent;
1053 size = sizeof(struct btrfs_shared_data_ref);
1055 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1056 key.offset = hash_extent_data_ref(root_objectid,
1058 size = sizeof(struct btrfs_extent_data_ref);
1061 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1062 if (ret && ret != -EEXIST)
1065 leaf = path->nodes[0];
1067 struct btrfs_shared_data_ref *ref;
1068 ref = btrfs_item_ptr(leaf, path->slots[0],
1069 struct btrfs_shared_data_ref);
1071 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1073 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1074 num_refs += refs_to_add;
1075 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1078 struct btrfs_extent_data_ref *ref;
1079 while (ret == -EEXIST) {
1080 ref = btrfs_item_ptr(leaf, path->slots[0],
1081 struct btrfs_extent_data_ref);
1082 if (match_extent_data_ref(leaf, ref, root_objectid,
1085 btrfs_release_path(root, path);
1087 ret = btrfs_insert_empty_item(trans, root, path, &key,
1089 if (ret && ret != -EEXIST)
1092 leaf = path->nodes[0];
1094 ref = btrfs_item_ptr(leaf, path->slots[0],
1095 struct btrfs_extent_data_ref);
1097 btrfs_set_extent_data_ref_root(leaf, ref,
1099 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1100 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1101 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1103 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1104 num_refs += refs_to_add;
1105 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1108 btrfs_mark_buffer_dirty(leaf);
1111 btrfs_release_path(root, path);
1115 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1116 struct btrfs_root *root,
1117 struct btrfs_path *path,
1120 struct btrfs_key key;
1121 struct btrfs_extent_data_ref *ref1 = NULL;
1122 struct btrfs_shared_data_ref *ref2 = NULL;
1123 struct extent_buffer *leaf;
1127 leaf = path->nodes[0];
1128 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1130 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1131 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1132 struct btrfs_extent_data_ref);
1133 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1134 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1135 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1136 struct btrfs_shared_data_ref);
1137 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1138 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1139 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1140 struct btrfs_extent_ref_v0 *ref0;
1141 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1142 struct btrfs_extent_ref_v0);
1143 num_refs = btrfs_ref_count_v0(leaf, ref0);
1149 BUG_ON(num_refs < refs_to_drop);
1150 num_refs -= refs_to_drop;
1152 if (num_refs == 0) {
1153 ret = btrfs_del_item(trans, root, path);
1155 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1156 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1157 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1158 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1159 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1161 struct btrfs_extent_ref_v0 *ref0;
1162 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1163 struct btrfs_extent_ref_v0);
1164 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1167 btrfs_mark_buffer_dirty(leaf);
1172 static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1173 struct btrfs_path *path,
1174 struct btrfs_extent_inline_ref *iref)
1176 struct btrfs_key key;
1177 struct extent_buffer *leaf;
1178 struct btrfs_extent_data_ref *ref1;
1179 struct btrfs_shared_data_ref *ref2;
1182 leaf = path->nodes[0];
1183 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1185 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1186 BTRFS_EXTENT_DATA_REF_KEY) {
1187 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1188 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1190 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1191 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1193 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1194 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1195 struct btrfs_extent_data_ref);
1196 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1197 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1198 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1199 struct btrfs_shared_data_ref);
1200 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1201 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1202 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1203 struct btrfs_extent_ref_v0 *ref0;
1204 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1205 struct btrfs_extent_ref_v0);
1206 num_refs = btrfs_ref_count_v0(leaf, ref0);
1214 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1215 struct btrfs_root *root,
1216 struct btrfs_path *path,
1217 u64 bytenr, u64 parent,
1220 struct btrfs_key key;
1223 key.objectid = bytenr;
1225 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1226 key.offset = parent;
1228 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1229 key.offset = root_objectid;
1232 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1235 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1236 if (ret == -ENOENT && parent) {
1237 btrfs_release_path(root, path);
1238 key.type = BTRFS_EXTENT_REF_V0_KEY;
1239 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1247 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1248 struct btrfs_root *root,
1249 struct btrfs_path *path,
1250 u64 bytenr, u64 parent,
1253 struct btrfs_key key;
1256 key.objectid = bytenr;
1258 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1259 key.offset = parent;
1261 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1262 key.offset = root_objectid;
1265 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1266 btrfs_release_path(root, path);
1270 static inline int extent_ref_type(u64 parent, u64 owner)
1273 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1275 type = BTRFS_SHARED_BLOCK_REF_KEY;
1277 type = BTRFS_TREE_BLOCK_REF_KEY;
1280 type = BTRFS_SHARED_DATA_REF_KEY;
1282 type = BTRFS_EXTENT_DATA_REF_KEY;
1287 static int find_next_key(struct btrfs_path *path, int level,
1288 struct btrfs_key *key)
1291 for (; level < BTRFS_MAX_LEVEL; level++) {
1292 if (!path->nodes[level])
1294 if (path->slots[level] + 1 >=
1295 btrfs_header_nritems(path->nodes[level]))
1298 btrfs_item_key_to_cpu(path->nodes[level], key,
1299 path->slots[level] + 1);
1301 btrfs_node_key_to_cpu(path->nodes[level], key,
1302 path->slots[level] + 1);
1309 * look for inline back ref. if back ref is found, *ref_ret is set
1310 * to the address of inline back ref, and 0 is returned.
1312 * if back ref isn't found, *ref_ret is set to the address where it
1313 * should be inserted, and -ENOENT is returned.
1315 * if insert is true and there are too many inline back refs, the path
1316 * points to the extent item, and -EAGAIN is returned.
1318 * NOTE: inline back refs are ordered in the same way that back ref
1319 * items in the tree are ordered.
1321 static noinline_for_stack
1322 int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1323 struct btrfs_root *root,
1324 struct btrfs_path *path,
1325 struct btrfs_extent_inline_ref **ref_ret,
1326 u64 bytenr, u64 num_bytes,
1327 u64 parent, u64 root_objectid,
1328 u64 owner, u64 offset, int insert)
1330 struct btrfs_key key;
1331 struct extent_buffer *leaf;
1332 struct btrfs_extent_item *ei;
1333 struct btrfs_extent_inline_ref *iref;
1344 key.objectid = bytenr;
1345 key.type = BTRFS_EXTENT_ITEM_KEY;
1346 key.offset = num_bytes;
1348 want = extent_ref_type(parent, owner);
1350 extra_size = btrfs_extent_inline_ref_size(want);
1351 path->keep_locks = 1;
1354 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1361 leaf = path->nodes[0];
1362 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1363 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1364 if (item_size < sizeof(*ei)) {
1369 ret = convert_extent_item_v0(trans, root, path, owner,
1375 leaf = path->nodes[0];
1376 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1379 BUG_ON(item_size < sizeof(*ei));
1381 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1382 flags = btrfs_extent_flags(leaf, ei);
1384 ptr = (unsigned long)(ei + 1);
1385 end = (unsigned long)ei + item_size;
1387 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1388 ptr += sizeof(struct btrfs_tree_block_info);
1391 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1400 iref = (struct btrfs_extent_inline_ref *)ptr;
1401 type = btrfs_extent_inline_ref_type(leaf, iref);
1405 ptr += btrfs_extent_inline_ref_size(type);
1409 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1410 struct btrfs_extent_data_ref *dref;
1411 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1412 if (match_extent_data_ref(leaf, dref, root_objectid,
1417 if (hash_extent_data_ref_item(leaf, dref) <
1418 hash_extent_data_ref(root_objectid, owner, offset))
1422 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1424 if (parent == ref_offset) {
1428 if (ref_offset < parent)
1431 if (root_objectid == ref_offset) {
1435 if (ref_offset < root_objectid)
1439 ptr += btrfs_extent_inline_ref_size(type);
1441 if (err == -ENOENT && insert) {
1442 if (item_size + extra_size >=
1443 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1448 * To add new inline back ref, we have to make sure
1449 * there is no corresponding back ref item.
1450 * For simplicity, we just do not add new inline back
1451 * ref if there is any kind of item for this block
1453 if (find_next_key(path, 0, &key) == 0 &&
1454 key.objectid == bytenr &&
1455 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
1460 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1463 path->keep_locks = 0;
1464 btrfs_unlock_up_safe(path, 1);
1470 * helper to add new inline back ref
1472 static noinline_for_stack
1473 int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1474 struct btrfs_root *root,
1475 struct btrfs_path *path,
1476 struct btrfs_extent_inline_ref *iref,
1477 u64 parent, u64 root_objectid,
1478 u64 owner, u64 offset, int refs_to_add,
1479 struct btrfs_delayed_extent_op *extent_op)
1481 struct extent_buffer *leaf;
1482 struct btrfs_extent_item *ei;
1485 unsigned long item_offset;
1491 leaf = path->nodes[0];
1492 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1493 item_offset = (unsigned long)iref - (unsigned long)ei;
1495 type = extent_ref_type(parent, owner);
1496 size = btrfs_extent_inline_ref_size(type);
1498 ret = btrfs_extend_item(trans, root, path, size);
1501 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1502 refs = btrfs_extent_refs(leaf, ei);
1503 refs += refs_to_add;
1504 btrfs_set_extent_refs(leaf, ei, refs);
1506 __run_delayed_extent_op(extent_op, leaf, ei);
1508 ptr = (unsigned long)ei + item_offset;
1509 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1510 if (ptr < end - size)
1511 memmove_extent_buffer(leaf, ptr + size, ptr,
1514 iref = (struct btrfs_extent_inline_ref *)ptr;
1515 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1516 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1517 struct btrfs_extent_data_ref *dref;
1518 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1519 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1520 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1521 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1522 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1523 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1524 struct btrfs_shared_data_ref *sref;
1525 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1526 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1527 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1528 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1529 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1531 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1533 btrfs_mark_buffer_dirty(leaf);
1537 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1538 struct btrfs_root *root,
1539 struct btrfs_path *path,
1540 struct btrfs_extent_inline_ref **ref_ret,
1541 u64 bytenr, u64 num_bytes, u64 parent,
1542 u64 root_objectid, u64 owner, u64 offset)
1546 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1547 bytenr, num_bytes, parent,
1548 root_objectid, owner, offset, 0);
1552 btrfs_release_path(root, path);
1555 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1556 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1559 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1560 root_objectid, owner, offset);
1566 * helper to update/remove inline back ref
1568 static noinline_for_stack
1569 int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1570 struct btrfs_root *root,
1571 struct btrfs_path *path,
1572 struct btrfs_extent_inline_ref *iref,
1574 struct btrfs_delayed_extent_op *extent_op)
1576 struct extent_buffer *leaf;
1577 struct btrfs_extent_item *ei;
1578 struct btrfs_extent_data_ref *dref = NULL;
1579 struct btrfs_shared_data_ref *sref = NULL;
1588 leaf = path->nodes[0];
1589 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1590 refs = btrfs_extent_refs(leaf, ei);
1591 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1592 refs += refs_to_mod;
1593 btrfs_set_extent_refs(leaf, ei, refs);
1595 __run_delayed_extent_op(extent_op, leaf, ei);
1597 type = btrfs_extent_inline_ref_type(leaf, iref);
1599 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1600 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1601 refs = btrfs_extent_data_ref_count(leaf, dref);
1602 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1603 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1604 refs = btrfs_shared_data_ref_count(leaf, sref);
1607 BUG_ON(refs_to_mod != -1);
1610 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1611 refs += refs_to_mod;
1614 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1615 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1617 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1619 size = btrfs_extent_inline_ref_size(type);
1620 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1621 ptr = (unsigned long)iref;
1622 end = (unsigned long)ei + item_size;
1623 if (ptr + size < end)
1624 memmove_extent_buffer(leaf, ptr, ptr + size,
1627 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1630 btrfs_mark_buffer_dirty(leaf);
1634 static noinline_for_stack
1635 int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1636 struct btrfs_root *root,
1637 struct btrfs_path *path,
1638 u64 bytenr, u64 num_bytes, u64 parent,
1639 u64 root_objectid, u64 owner,
1640 u64 offset, int refs_to_add,
1641 struct btrfs_delayed_extent_op *extent_op)
1643 struct btrfs_extent_inline_ref *iref;
1646 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1647 bytenr, num_bytes, parent,
1648 root_objectid, owner, offset, 1);
1650 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1651 ret = update_inline_extent_backref(trans, root, path, iref,
1652 refs_to_add, extent_op);
1653 } else if (ret == -ENOENT) {
1654 ret = setup_inline_extent_backref(trans, root, path, iref,
1655 parent, root_objectid,
1656 owner, offset, refs_to_add,
1662 static int insert_extent_backref(struct btrfs_trans_handle *trans,
1663 struct btrfs_root *root,
1664 struct btrfs_path *path,
1665 u64 bytenr, u64 parent, u64 root_objectid,
1666 u64 owner, u64 offset, int refs_to_add)
1669 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1670 BUG_ON(refs_to_add != 1);
1671 ret = insert_tree_block_ref(trans, root, path, bytenr,
1672 parent, root_objectid);
1674 ret = insert_extent_data_ref(trans, root, path, bytenr,
1675 parent, root_objectid,
1676 owner, offset, refs_to_add);
1681 static int remove_extent_backref(struct btrfs_trans_handle *trans,
1682 struct btrfs_root *root,
1683 struct btrfs_path *path,
1684 struct btrfs_extent_inline_ref *iref,
1685 int refs_to_drop, int is_data)
1689 BUG_ON(!is_data && refs_to_drop != 1);
1691 ret = update_inline_extent_backref(trans, root, path, iref,
1692 -refs_to_drop, NULL);
1693 } else if (is_data) {
1694 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1696 ret = btrfs_del_item(trans, root, path);
1701 static void btrfs_issue_discard(struct block_device *bdev,
1704 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL,
1705 DISCARD_FL_BARRIER);
1708 static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1712 u64 map_length = num_bytes;
1713 struct btrfs_multi_bio *multi = NULL;
1715 if (!btrfs_test_opt(root, DISCARD))
1718 /* Tell the block device(s) that the sectors can be discarded */
1719 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1720 bytenr, &map_length, &multi, 0);
1722 struct btrfs_bio_stripe *stripe = multi->stripes;
1725 if (map_length > num_bytes)
1726 map_length = num_bytes;
1728 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1729 btrfs_issue_discard(stripe->dev->bdev,
1739 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1740 struct btrfs_root *root,
1741 u64 bytenr, u64 num_bytes, u64 parent,
1742 u64 root_objectid, u64 owner, u64 offset)
1745 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1746 root_objectid == BTRFS_TREE_LOG_OBJECTID);
1748 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1749 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1750 parent, root_objectid, (int)owner,
1751 BTRFS_ADD_DELAYED_REF, NULL);
1753 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1754 parent, root_objectid, owner, offset,
1755 BTRFS_ADD_DELAYED_REF, NULL);
1760 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1761 struct btrfs_root *root,
1762 u64 bytenr, u64 num_bytes,
1763 u64 parent, u64 root_objectid,
1764 u64 owner, u64 offset, int refs_to_add,
1765 struct btrfs_delayed_extent_op *extent_op)
1767 struct btrfs_path *path;
1768 struct extent_buffer *leaf;
1769 struct btrfs_extent_item *item;
1774 path = btrfs_alloc_path();
1779 path->leave_spinning = 1;
1780 /* this will setup the path even if it fails to insert the back ref */
1781 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1782 path, bytenr, num_bytes, parent,
1783 root_objectid, owner, offset,
1784 refs_to_add, extent_op);
1788 if (ret != -EAGAIN) {
1793 leaf = path->nodes[0];
1794 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1795 refs = btrfs_extent_refs(leaf, item);
1796 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1798 __run_delayed_extent_op(extent_op, leaf, item);
1800 btrfs_mark_buffer_dirty(leaf);
1801 btrfs_release_path(root->fs_info->extent_root, path);
1804 path->leave_spinning = 1;
1806 /* now insert the actual backref */
1807 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1808 path, bytenr, parent, root_objectid,
1809 owner, offset, refs_to_add);
1812 btrfs_free_path(path);
1816 static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1817 struct btrfs_root *root,
1818 struct btrfs_delayed_ref_node *node,
1819 struct btrfs_delayed_extent_op *extent_op,
1820 int insert_reserved)
1823 struct btrfs_delayed_data_ref *ref;
1824 struct btrfs_key ins;
1829 ins.objectid = node->bytenr;
1830 ins.offset = node->num_bytes;
1831 ins.type = BTRFS_EXTENT_ITEM_KEY;
1833 ref = btrfs_delayed_node_to_data_ref(node);
1834 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1835 parent = ref->parent;
1837 ref_root = ref->root;
1839 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1841 BUG_ON(extent_op->update_key);
1842 flags |= extent_op->flags_to_set;
1844 ret = alloc_reserved_file_extent(trans, root,
1845 parent, ref_root, flags,
1846 ref->objectid, ref->offset,
1847 &ins, node->ref_mod);
1848 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1849 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1850 node->num_bytes, parent,
1851 ref_root, ref->objectid,
1852 ref->offset, node->ref_mod,
1854 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1855 ret = __btrfs_free_extent(trans, root, node->bytenr,
1856 node->num_bytes, parent,
1857 ref_root, ref->objectid,
1858 ref->offset, node->ref_mod,
1866 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1867 struct extent_buffer *leaf,
1868 struct btrfs_extent_item *ei)
1870 u64 flags = btrfs_extent_flags(leaf, ei);
1871 if (extent_op->update_flags) {
1872 flags |= extent_op->flags_to_set;
1873 btrfs_set_extent_flags(leaf, ei, flags);
1876 if (extent_op->update_key) {
1877 struct btrfs_tree_block_info *bi;
1878 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1879 bi = (struct btrfs_tree_block_info *)(ei + 1);
1880 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1884 static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1885 struct btrfs_root *root,
1886 struct btrfs_delayed_ref_node *node,
1887 struct btrfs_delayed_extent_op *extent_op)
1889 struct btrfs_key key;
1890 struct btrfs_path *path;
1891 struct btrfs_extent_item *ei;
1892 struct extent_buffer *leaf;
1897 path = btrfs_alloc_path();
1901 key.objectid = node->bytenr;
1902 key.type = BTRFS_EXTENT_ITEM_KEY;
1903 key.offset = node->num_bytes;
1906 path->leave_spinning = 1;
1907 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1918 leaf = path->nodes[0];
1919 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1920 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1921 if (item_size < sizeof(*ei)) {
1922 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1928 leaf = path->nodes[0];
1929 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1932 BUG_ON(item_size < sizeof(*ei));
1933 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1934 __run_delayed_extent_op(extent_op, leaf, ei);
1936 btrfs_mark_buffer_dirty(leaf);
1938 btrfs_free_path(path);
1942 static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1943 struct btrfs_root *root,
1944 struct btrfs_delayed_ref_node *node,
1945 struct btrfs_delayed_extent_op *extent_op,
1946 int insert_reserved)
1949 struct btrfs_delayed_tree_ref *ref;
1950 struct btrfs_key ins;
1954 ins.objectid = node->bytenr;
1955 ins.offset = node->num_bytes;
1956 ins.type = BTRFS_EXTENT_ITEM_KEY;
1958 ref = btrfs_delayed_node_to_tree_ref(node);
1959 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1960 parent = ref->parent;
1962 ref_root = ref->root;
1964 BUG_ON(node->ref_mod != 1);
1965 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1966 BUG_ON(!extent_op || !extent_op->update_flags ||
1967 !extent_op->update_key);
1968 ret = alloc_reserved_tree_block(trans, root,
1970 extent_op->flags_to_set,
1973 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1974 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1975 node->num_bytes, parent, ref_root,
1976 ref->level, 0, 1, extent_op);
1977 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1978 ret = __btrfs_free_extent(trans, root, node->bytenr,
1979 node->num_bytes, parent, ref_root,
1980 ref->level, 0, 1, extent_op);
1987 /* helper function to actually process a single delayed ref entry */
1988 static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
1989 struct btrfs_root *root,
1990 struct btrfs_delayed_ref_node *node,
1991 struct btrfs_delayed_extent_op *extent_op,
1992 int insert_reserved)
1995 if (btrfs_delayed_ref_is_head(node)) {
1996 struct btrfs_delayed_ref_head *head;
1998 * we've hit the end of the chain and we were supposed
1999 * to insert this extent into the tree. But, it got
2000 * deleted before we ever needed to insert it, so all
2001 * we have to do is clean up the accounting
2004 head = btrfs_delayed_node_to_head(node);
2005 if (insert_reserved) {
2006 btrfs_pin_extent(root, node->bytenr,
2007 node->num_bytes, 1);
2008 if (head->is_data) {
2009 ret = btrfs_del_csums(trans, root,
2015 mutex_unlock(&head->mutex);
2019 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2020 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2021 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2023 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2024 node->type == BTRFS_SHARED_DATA_REF_KEY)
2025 ret = run_delayed_data_ref(trans, root, node, extent_op,
2032 static noinline struct btrfs_delayed_ref_node *
2033 select_delayed_ref(struct btrfs_delayed_ref_head *head)
2035 struct rb_node *node;
2036 struct btrfs_delayed_ref_node *ref;
2037 int action = BTRFS_ADD_DELAYED_REF;
2040 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2041 * this prevents ref count from going down to zero when
2042 * there still are pending delayed ref.
2044 node = rb_prev(&head->node.rb_node);
2048 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2050 if (ref->bytenr != head->node.bytenr)
2052 if (ref->action == action)
2054 node = rb_prev(node);
2056 if (action == BTRFS_ADD_DELAYED_REF) {
2057 action = BTRFS_DROP_DELAYED_REF;
2063 static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2064 struct btrfs_root *root,
2065 struct list_head *cluster)
2067 struct btrfs_delayed_ref_root *delayed_refs;
2068 struct btrfs_delayed_ref_node *ref;
2069 struct btrfs_delayed_ref_head *locked_ref = NULL;
2070 struct btrfs_delayed_extent_op *extent_op;
2073 int must_insert_reserved = 0;
2075 delayed_refs = &trans->transaction->delayed_refs;
2078 /* pick a new head ref from the cluster list */
2079 if (list_empty(cluster))
2082 locked_ref = list_entry(cluster->next,
2083 struct btrfs_delayed_ref_head, cluster);
2085 /* grab the lock that says we are going to process
2086 * all the refs for this head */
2087 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2090 * we may have dropped the spin lock to get the head
2091 * mutex lock, and that might have given someone else
2092 * time to free the head. If that's true, it has been
2093 * removed from our list and we can move on.
2095 if (ret == -EAGAIN) {
2103 * record the must insert reserved flag before we
2104 * drop the spin lock.
2106 must_insert_reserved = locked_ref->must_insert_reserved;
2107 locked_ref->must_insert_reserved = 0;
2109 extent_op = locked_ref->extent_op;
2110 locked_ref->extent_op = NULL;
2113 * locked_ref is the head node, so we have to go one
2114 * node back for any delayed ref updates
2116 ref = select_delayed_ref(locked_ref);
2118 /* All delayed refs have been processed, Go ahead
2119 * and send the head node to run_one_delayed_ref,
2120 * so that any accounting fixes can happen
2122 ref = &locked_ref->node;
2124 if (extent_op && must_insert_reserved) {
2130 spin_unlock(&delayed_refs->lock);
2132 ret = run_delayed_extent_op(trans, root,
2138 spin_lock(&delayed_refs->lock);
2142 list_del_init(&locked_ref->cluster);
2147 rb_erase(&ref->rb_node, &delayed_refs->root);
2148 delayed_refs->num_entries--;
2150 spin_unlock(&delayed_refs->lock);
2152 ret = run_one_delayed_ref(trans, root, ref, extent_op,
2153 must_insert_reserved);
2156 btrfs_put_delayed_ref(ref);
2161 spin_lock(&delayed_refs->lock);
2167 * this starts processing the delayed reference count updates and
2168 * extent insertions we have queued up so far. count can be
2169 * 0, which means to process everything in the tree at the start
2170 * of the run (but not newly added entries), or it can be some target
2171 * number you'd like to process.
2173 int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2174 struct btrfs_root *root, unsigned long count)
2176 struct rb_node *node;
2177 struct btrfs_delayed_ref_root *delayed_refs;
2178 struct btrfs_delayed_ref_node *ref;
2179 struct list_head cluster;
2181 int run_all = count == (unsigned long)-1;
2184 if (root == root->fs_info->extent_root)
2185 root = root->fs_info->tree_root;
2187 delayed_refs = &trans->transaction->delayed_refs;
2188 INIT_LIST_HEAD(&cluster);
2190 spin_lock(&delayed_refs->lock);
2192 count = delayed_refs->num_entries * 2;
2196 if (!(run_all || run_most) &&
2197 delayed_refs->num_heads_ready < 64)
2201 * go find something we can process in the rbtree. We start at
2202 * the beginning of the tree, and then build a cluster
2203 * of refs to process starting at the first one we are able to
2206 ret = btrfs_find_ref_cluster(trans, &cluster,
2207 delayed_refs->run_delayed_start);
2211 ret = run_clustered_refs(trans, root, &cluster);
2214 count -= min_t(unsigned long, ret, count);
2221 node = rb_first(&delayed_refs->root);
2224 count = (unsigned long)-1;
2227 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2229 if (btrfs_delayed_ref_is_head(ref)) {
2230 struct btrfs_delayed_ref_head *head;
2232 head = btrfs_delayed_node_to_head(ref);
2233 atomic_inc(&ref->refs);
2235 spin_unlock(&delayed_refs->lock);
2236 mutex_lock(&head->mutex);
2237 mutex_unlock(&head->mutex);
2239 btrfs_put_delayed_ref(ref);
2243 node = rb_next(node);
2245 spin_unlock(&delayed_refs->lock);
2246 schedule_timeout(1);
2250 spin_unlock(&delayed_refs->lock);
2254 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2255 struct btrfs_root *root,
2256 u64 bytenr, u64 num_bytes, u64 flags,
2259 struct btrfs_delayed_extent_op *extent_op;
2262 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2266 extent_op->flags_to_set = flags;
2267 extent_op->update_flags = 1;
2268 extent_op->update_key = 0;
2269 extent_op->is_data = is_data ? 1 : 0;
2271 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2277 static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2278 struct btrfs_root *root,
2279 struct btrfs_path *path,
2280 u64 objectid, u64 offset, u64 bytenr)
2282 struct btrfs_delayed_ref_head *head;
2283 struct btrfs_delayed_ref_node *ref;
2284 struct btrfs_delayed_data_ref *data_ref;
2285 struct btrfs_delayed_ref_root *delayed_refs;
2286 struct rb_node *node;
2290 delayed_refs = &trans->transaction->delayed_refs;
2291 spin_lock(&delayed_refs->lock);
2292 head = btrfs_find_delayed_ref_head(trans, bytenr);
2296 if (!mutex_trylock(&head->mutex)) {
2297 atomic_inc(&head->node.refs);
2298 spin_unlock(&delayed_refs->lock);
2300 btrfs_release_path(root->fs_info->extent_root, path);
2302 mutex_lock(&head->mutex);
2303 mutex_unlock(&head->mutex);
2304 btrfs_put_delayed_ref(&head->node);
2308 node = rb_prev(&head->node.rb_node);
2312 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2314 if (ref->bytenr != bytenr)
2318 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2321 data_ref = btrfs_delayed_node_to_data_ref(ref);
2323 node = rb_prev(node);
2325 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2326 if (ref->bytenr == bytenr)
2330 if (data_ref->root != root->root_key.objectid ||
2331 data_ref->objectid != objectid || data_ref->offset != offset)
2336 mutex_unlock(&head->mutex);
2338 spin_unlock(&delayed_refs->lock);
2342 static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2343 struct btrfs_root *root,
2344 struct btrfs_path *path,
2345 u64 objectid, u64 offset, u64 bytenr)
2347 struct btrfs_root *extent_root = root->fs_info->extent_root;
2348 struct extent_buffer *leaf;
2349 struct btrfs_extent_data_ref *ref;
2350 struct btrfs_extent_inline_ref *iref;
2351 struct btrfs_extent_item *ei;
2352 struct btrfs_key key;
2356 key.objectid = bytenr;
2357 key.offset = (u64)-1;
2358 key.type = BTRFS_EXTENT_ITEM_KEY;
2360 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2366 if (path->slots[0] == 0)
2370 leaf = path->nodes[0];
2371 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2373 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
2377 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2378 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2379 if (item_size < sizeof(*ei)) {
2380 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2384 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2386 if (item_size != sizeof(*ei) +
2387 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2390 if (btrfs_extent_generation(leaf, ei) <=
2391 btrfs_root_last_snapshot(&root->root_item))
2394 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2395 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2396 BTRFS_EXTENT_DATA_REF_KEY)
2399 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2400 if (btrfs_extent_refs(leaf, ei) !=
2401 btrfs_extent_data_ref_count(leaf, ref) ||
2402 btrfs_extent_data_ref_root(leaf, ref) !=
2403 root->root_key.objectid ||
2404 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2405 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2413 int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2414 struct btrfs_root *root,
2415 u64 objectid, u64 offset, u64 bytenr)
2417 struct btrfs_path *path;
2421 path = btrfs_alloc_path();
2426 ret = check_committed_ref(trans, root, path, objectid,
2428 if (ret && ret != -ENOENT)
2431 ret2 = check_delayed_ref(trans, root, path, objectid,
2433 } while (ret2 == -EAGAIN);
2435 if (ret2 && ret2 != -ENOENT) {
2440 if (ret != -ENOENT || ret2 != -ENOENT)
2443 btrfs_free_path(path);
2444 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2450 int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2451 struct extent_buffer *buf, u32 nr_extents)
2453 struct btrfs_key key;
2454 struct btrfs_file_extent_item *fi;
2462 if (!root->ref_cows)
2465 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2467 root_gen = root->root_key.offset;
2470 root_gen = trans->transid - 1;
2473 level = btrfs_header_level(buf);
2474 nritems = btrfs_header_nritems(buf);
2477 struct btrfs_leaf_ref *ref;
2478 struct btrfs_extent_info *info;
2480 ref = btrfs_alloc_leaf_ref(root, nr_extents);
2486 ref->root_gen = root_gen;
2487 ref->bytenr = buf->start;
2488 ref->owner = btrfs_header_owner(buf);
2489 ref->generation = btrfs_header_generation(buf);
2490 ref->nritems = nr_extents;
2491 info = ref->extents;
2493 for (i = 0; nr_extents > 0 && i < nritems; i++) {
2495 btrfs_item_key_to_cpu(buf, &key, i);
2496 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2498 fi = btrfs_item_ptr(buf, i,
2499 struct btrfs_file_extent_item);
2500 if (btrfs_file_extent_type(buf, fi) ==
2501 BTRFS_FILE_EXTENT_INLINE)
2503 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2504 if (disk_bytenr == 0)
2507 info->bytenr = disk_bytenr;
2509 btrfs_file_extent_disk_num_bytes(buf, fi);
2510 info->objectid = key.objectid;
2511 info->offset = key.offset;
2515 ret = btrfs_add_leaf_ref(root, ref, shared);
2516 if (ret == -EEXIST && shared) {
2517 struct btrfs_leaf_ref *old;
2518 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2520 btrfs_remove_leaf_ref(root, old);
2521 btrfs_free_leaf_ref(root, old);
2522 ret = btrfs_add_leaf_ref(root, ref, shared);
2525 btrfs_free_leaf_ref(root, ref);
2531 /* when a block goes through cow, we update the reference counts of
2532 * everything that block points to. The internal pointers of the block
2533 * can be in just about any order, and it is likely to have clusters of
2534 * things that are close together and clusters of things that are not.
2536 * To help reduce the seeks that come with updating all of these reference
2537 * counts, sort them by byte number before actual updates are done.
2539 * struct refsort is used to match byte number to slot in the btree block.
2540 * we sort based on the byte number and then use the slot to actually
2543 * struct refsort is smaller than strcut btrfs_item and smaller than
2544 * struct btrfs_key_ptr. Since we're currently limited to the page size
2545 * for a btree block, there's no way for a kmalloc of refsorts for a
2546 * single node to be bigger than a page.
2554 * for passing into sort()
2556 static int refsort_cmp(const void *a_void, const void *b_void)
2558 const struct refsort *a = a_void;
2559 const struct refsort *b = b_void;
2561 if (a->bytenr < b->bytenr)
2563 if (a->bytenr > b->bytenr)
2569 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
2570 struct btrfs_root *root,
2571 struct extent_buffer *buf,
2572 int full_backref, int inc)
2579 struct btrfs_key key;
2580 struct btrfs_file_extent_item *fi;
2584 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
2585 u64, u64, u64, u64, u64, u64);
2587 ref_root = btrfs_header_owner(buf);
2588 nritems = btrfs_header_nritems(buf);
2589 level = btrfs_header_level(buf);
2591 if (!root->ref_cows && level == 0)
2595 process_func = btrfs_inc_extent_ref;
2597 process_func = btrfs_free_extent;
2600 parent = buf->start;
2604 for (i = 0; i < nritems; i++) {
2606 btrfs_item_key_to_cpu(buf, &key, i);
2607 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2609 fi = btrfs_item_ptr(buf, i,
2610 struct btrfs_file_extent_item);
2611 if (btrfs_file_extent_type(buf, fi) ==
2612 BTRFS_FILE_EXTENT_INLINE)
2614 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2618 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2619 key.offset -= btrfs_file_extent_offset(buf, fi);
2620 ret = process_func(trans, root, bytenr, num_bytes,
2621 parent, ref_root, key.objectid,
2626 bytenr = btrfs_node_blockptr(buf, i);
2627 num_bytes = btrfs_level_size(root, level - 1);
2628 ret = process_func(trans, root, bytenr, num_bytes,
2629 parent, ref_root, level - 1, 0);
2640 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2641 struct extent_buffer *buf, int full_backref)
2643 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2646 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2647 struct extent_buffer *buf, int full_backref)
2649 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
2652 static int write_one_cache_group(struct btrfs_trans_handle *trans,
2653 struct btrfs_root *root,
2654 struct btrfs_path *path,
2655 struct btrfs_block_group_cache *cache)
2658 struct btrfs_root *extent_root = root->fs_info->extent_root;
2660 struct extent_buffer *leaf;
2662 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
2667 leaf = path->nodes[0];
2668 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2669 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2670 btrfs_mark_buffer_dirty(leaf);
2671 btrfs_release_path(extent_root, path);
2679 static struct btrfs_block_group_cache *
2680 next_block_group(struct btrfs_root *root,
2681 struct btrfs_block_group_cache *cache)
2683 struct rb_node *node;
2684 spin_lock(&root->fs_info->block_group_cache_lock);
2685 node = rb_next(&cache->cache_node);
2686 btrfs_put_block_group(cache);
2688 cache = rb_entry(node, struct btrfs_block_group_cache,
2690 btrfs_get_block_group(cache);
2693 spin_unlock(&root->fs_info->block_group_cache_lock);
2697 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2698 struct btrfs_root *root)
2700 struct btrfs_block_group_cache *cache;
2702 struct btrfs_path *path;
2705 path = btrfs_alloc_path();
2711 err = btrfs_run_delayed_refs(trans, root,
2716 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2720 cache = next_block_group(root, cache);
2730 last = cache->key.objectid + cache->key.offset;
2732 err = write_one_cache_group(trans, root, path, cache);
2734 btrfs_put_block_group(cache);
2737 btrfs_free_path(path);
2741 int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2743 struct btrfs_block_group_cache *block_group;
2746 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2747 if (!block_group || block_group->ro)
2750 btrfs_put_block_group(block_group);
2754 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2755 u64 total_bytes, u64 bytes_used,
2756 struct btrfs_space_info **space_info)
2758 struct btrfs_space_info *found;
2762 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2763 BTRFS_BLOCK_GROUP_RAID10))
2768 found = __find_space_info(info, flags);
2770 spin_lock(&found->lock);
2771 found->total_bytes += total_bytes;
2772 found->bytes_used += bytes_used;
2773 found->disk_used += bytes_used * factor;
2775 spin_unlock(&found->lock);
2776 *space_info = found;
2779 found = kzalloc(sizeof(*found), GFP_NOFS);
2783 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
2784 INIT_LIST_HEAD(&found->block_groups[i]);
2785 init_rwsem(&found->groups_sem);
2786 spin_lock_init(&found->lock);
2787 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
2788 BTRFS_BLOCK_GROUP_SYSTEM |
2789 BTRFS_BLOCK_GROUP_METADATA);
2790 found->total_bytes = total_bytes;
2791 found->bytes_used = bytes_used;
2792 found->disk_used = bytes_used * factor;
2793 found->bytes_pinned = 0;
2794 found->bytes_reserved = 0;
2795 found->bytes_readonly = 0;
2796 found->bytes_may_use = 0;
2798 found->force_alloc = 0;
2799 *space_info = found;
2800 list_add_rcu(&found->list, &info->space_info);
2801 atomic_set(&found->caching_threads, 0);
2805 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2807 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
2808 BTRFS_BLOCK_GROUP_RAID1 |
2809 BTRFS_BLOCK_GROUP_RAID10 |
2810 BTRFS_BLOCK_GROUP_DUP);
2812 if (flags & BTRFS_BLOCK_GROUP_DATA)
2813 fs_info->avail_data_alloc_bits |= extra_flags;
2814 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2815 fs_info->avail_metadata_alloc_bits |= extra_flags;
2816 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2817 fs_info->avail_system_alloc_bits |= extra_flags;
2821 u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
2823 u64 num_devices = root->fs_info->fs_devices->rw_devices;
2825 if (num_devices == 1)
2826 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2827 if (num_devices < 4)
2828 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2830 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2831 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
2832 BTRFS_BLOCK_GROUP_RAID10))) {
2833 flags &= ~BTRFS_BLOCK_GROUP_DUP;
2836 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
2837 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
2838 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
2841 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2842 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2843 (flags & BTRFS_BLOCK_GROUP_RAID10) |
2844 (flags & BTRFS_BLOCK_GROUP_DUP)))
2845 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2849 static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
2851 if (flags & BTRFS_BLOCK_GROUP_DATA)
2852 flags |= root->fs_info->avail_data_alloc_bits &
2853 root->fs_info->data_alloc_profile;
2854 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2855 flags |= root->fs_info->avail_system_alloc_bits &
2856 root->fs_info->system_alloc_profile;
2857 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
2858 flags |= root->fs_info->avail_metadata_alloc_bits &
2859 root->fs_info->metadata_alloc_profile;
2860 return btrfs_reduce_alloc_profile(root, flags);
2863 static u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
2868 flags = BTRFS_BLOCK_GROUP_DATA;
2869 else if (root == root->fs_info->chunk_root)
2870 flags = BTRFS_BLOCK_GROUP_SYSTEM;
2872 flags = BTRFS_BLOCK_GROUP_METADATA;
2874 return get_alloc_profile(root, flags);
2877 void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
2879 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
2880 BTRFS_BLOCK_GROUP_DATA);
2883 static u64 calculate_bytes_needed(struct btrfs_root *root, int num_items)
2888 level = BTRFS_MAX_LEVEL - 2;
2890 * NOTE: these calculations are absolutely the worst possible case.
2891 * This assumes that _every_ item we insert will require a new leaf, and
2892 * that the tree has grown to its maximum level size.
2896 * for every item we insert we could insert both an extent item and a
2897 * extent ref item. Then for ever item we insert, we will need to cow
2898 * both the original leaf, plus the leaf to the left and right of it.
2900 * Unless we are talking about the extent root, then we just want the
2901 * number of items * 2, since we just need the extent item plus its ref.
2903 if (root == root->fs_info->extent_root)
2904 num_bytes = num_items * 2;
2906 num_bytes = (num_items + (2 * num_items)) * 3;
2909 * num_bytes is total number of leaves we could need times the leaf
2910 * size, and then for every leaf we could end up cow'ing 2 nodes per
2911 * level, down to the leaf level.
2913 num_bytes = (num_bytes * root->leafsize) +
2914 (num_bytes * (level * 2)) * root->nodesize;
2920 * Unreserve metadata space for delalloc. If we have less reserved credits than
2921 * we have extents, this function does nothing.
2923 int btrfs_unreserve_metadata_for_delalloc(struct btrfs_root *root,
2924 struct inode *inode, int num_items)
2926 struct btrfs_fs_info *info = root->fs_info;
2927 struct btrfs_space_info *meta_sinfo;
2932 /* get the space info for where the metadata will live */
2933 alloc_target = btrfs_get_alloc_profile(root, 0);
2934 meta_sinfo = __find_space_info(info, alloc_target);
2936 num_bytes = calculate_bytes_needed(root->fs_info->extent_root,
2939 spin_lock(&meta_sinfo->lock);
2940 spin_lock(&BTRFS_I(inode)->accounting_lock);
2941 if (BTRFS_I(inode)->reserved_extents <=
2942 BTRFS_I(inode)->outstanding_extents) {
2943 spin_unlock(&BTRFS_I(inode)->accounting_lock);
2944 spin_unlock(&meta_sinfo->lock);
2947 spin_unlock(&BTRFS_I(inode)->accounting_lock);
2949 BTRFS_I(inode)->reserved_extents -= num_items;
2950 BUG_ON(BTRFS_I(inode)->reserved_extents < 0);
2952 if (meta_sinfo->bytes_delalloc < num_bytes) {
2954 meta_sinfo->bytes_delalloc = 0;
2956 meta_sinfo->bytes_delalloc -= num_bytes;
2958 spin_unlock(&meta_sinfo->lock);
2965 static void check_force_delalloc(struct btrfs_space_info *meta_sinfo)
2969 thresh = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
2970 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
2971 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
2972 meta_sinfo->bytes_may_use;
2974 thresh = meta_sinfo->total_bytes - thresh;
2976 do_div(thresh, 100);
2977 if (thresh <= meta_sinfo->bytes_delalloc)
2978 meta_sinfo->force_delalloc = 1;
2980 meta_sinfo->force_delalloc = 0;
2984 * Reserve metadata space for delalloc.
2986 int btrfs_reserve_metadata_for_delalloc(struct btrfs_root *root,
2987 struct inode *inode, int num_items)
2989 struct btrfs_fs_info *info = root->fs_info;
2990 struct btrfs_space_info *meta_sinfo;
2997 /* get the space info for where the metadata will live */
2998 alloc_target = btrfs_get_alloc_profile(root, 0);
2999 meta_sinfo = __find_space_info(info, alloc_target);
3001 num_bytes = calculate_bytes_needed(root->fs_info->extent_root,
3004 spin_lock(&meta_sinfo->lock);
3006 force_delalloc = meta_sinfo->force_delalloc;
3008 if (unlikely(!meta_sinfo->bytes_root))
3009 meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
3012 meta_sinfo->bytes_delalloc += num_bytes;
3014 used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
3015 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
3016 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
3017 meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
3019 if (used > meta_sinfo->total_bytes) {
3023 if (maybe_allocate_chunk(NULL, root, meta_sinfo,
3028 spin_unlock(&meta_sinfo->lock);
3032 filemap_flush(inode->i_mapping);
3034 } else if (flushed == 3) {
3035 shrink_delalloc(NULL, root, meta_sinfo, num_bytes);
3038 spin_lock(&meta_sinfo->lock);
3039 meta_sinfo->bytes_delalloc -= num_bytes;
3040 spin_unlock(&meta_sinfo->lock);
3041 printk(KERN_ERR "enospc, has %d, reserved %d\n",
3042 BTRFS_I(inode)->outstanding_extents,
3043 BTRFS_I(inode)->reserved_extents);
3044 dump_space_info(meta_sinfo, 0, 0);
3048 BTRFS_I(inode)->reserved_extents += num_items;
3049 check_force_delalloc(meta_sinfo);
3050 spin_unlock(&meta_sinfo->lock);
3052 if (!flushed && force_delalloc)
3053 filemap_flush(inode->i_mapping);
3059 * This will check the space that the inode allocates from to make sure we have
3060 * enough space for bytes.
3062 int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
3065 struct btrfs_space_info *data_sinfo;
3067 int ret = 0, committed = 0;
3069 /* make sure bytes are sectorsize aligned */
3070 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3072 data_sinfo = BTRFS_I(inode)->space_info;
3077 /* make sure we have enough space to handle the data first */
3078 spin_lock(&data_sinfo->lock);
3079 used = data_sinfo->bytes_used + data_sinfo->bytes_delalloc +
3080 data_sinfo->bytes_reserved + data_sinfo->bytes_pinned +
3081 data_sinfo->bytes_readonly + data_sinfo->bytes_may_use +
3082 data_sinfo->bytes_super;
3084 if (used + bytes > data_sinfo->total_bytes) {
3085 struct btrfs_trans_handle *trans;
3088 * if we don't have enough free bytes in this space then we need
3089 * to alloc a new chunk.
3091 if (!data_sinfo->full) {
3094 data_sinfo->force_alloc = 1;
3095 spin_unlock(&data_sinfo->lock);
3097 alloc_target = btrfs_get_alloc_profile(root, 1);
3098 trans = btrfs_join_transaction(root, 1);
3100 return PTR_ERR(trans);
3102 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3103 bytes + 2 * 1024 * 1024,
3105 btrfs_end_transaction(trans, root);
3110 btrfs_set_inode_space_info(root, inode);
3111 data_sinfo = BTRFS_I(inode)->space_info;
3115 spin_unlock(&data_sinfo->lock);
3117 /* commit the current transaction and try again */
3118 if (!committed && !root->fs_info->open_ioctl_trans) {
3120 trans = btrfs_join_transaction(root, 1);
3122 return PTR_ERR(trans);
3123 ret = btrfs_commit_transaction(trans, root);
3129 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
3130 ", %llu bytes_used, %llu bytes_reserved, "
3131 "%llu bytes_pinned, %llu bytes_readonly, %llu may use "
3132 "%llu total\n", (unsigned long long)bytes,
3133 (unsigned long long)data_sinfo->bytes_delalloc,
3134 (unsigned long long)data_sinfo->bytes_used,
3135 (unsigned long long)data_sinfo->bytes_reserved,
3136 (unsigned long long)data_sinfo->bytes_pinned,
3137 (unsigned long long)data_sinfo->bytes_readonly,
3138 (unsigned long long)data_sinfo->bytes_may_use,
3139 (unsigned long long)data_sinfo->total_bytes);
3142 data_sinfo->bytes_may_use += bytes;
3143 BTRFS_I(inode)->reserved_bytes += bytes;
3144 spin_unlock(&data_sinfo->lock);
3150 * if there was an error for whatever reason after calling
3151 * btrfs_check_data_free_space, call this so we can cleanup the counters.
3153 void btrfs_free_reserved_data_space(struct btrfs_root *root,
3154 struct inode *inode, u64 bytes)
3156 struct btrfs_space_info *data_sinfo;
3158 /* make sure bytes are sectorsize aligned */
3159 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3161 data_sinfo = BTRFS_I(inode)->space_info;
3162 spin_lock(&data_sinfo->lock);
3163 data_sinfo->bytes_may_use -= bytes;
3164 BTRFS_I(inode)->reserved_bytes -= bytes;
3165 spin_unlock(&data_sinfo->lock);
3168 /* called when we are adding a delalloc extent to the inode's io_tree */
3169 void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
3172 struct btrfs_space_info *data_sinfo;
3174 /* get the space info for where this inode will be storing its data */
3175 data_sinfo = BTRFS_I(inode)->space_info;
3177 /* make sure we have enough space to handle the data first */
3178 spin_lock(&data_sinfo->lock);
3179 data_sinfo->bytes_delalloc += bytes;
3182 * we are adding a delalloc extent without calling
3183 * btrfs_check_data_free_space first. This happens on a weird
3184 * writepage condition, but shouldn't hurt our accounting
3186 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
3187 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
3188 BTRFS_I(inode)->reserved_bytes = 0;
3190 data_sinfo->bytes_may_use -= bytes;
3191 BTRFS_I(inode)->reserved_bytes -= bytes;
3194 spin_unlock(&data_sinfo->lock);
3197 /* called when we are clearing an delalloc extent from the inode's io_tree */
3198 void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
3201 struct btrfs_space_info *info;
3203 info = BTRFS_I(inode)->space_info;
3205 spin_lock(&info->lock);
3206 info->bytes_delalloc -= bytes;
3207 spin_unlock(&info->lock);
3210 static void force_metadata_allocation(struct btrfs_fs_info *info)
3212 struct list_head *head = &info->space_info;
3213 struct btrfs_space_info *found;
3216 list_for_each_entry_rcu(found, head, list) {
3217 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
3218 found->force_alloc = 1;
3223 static int should_alloc_chunk(struct btrfs_space_info *sinfo,
3226 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
3228 if (sinfo->bytes_used + sinfo->bytes_reserved +
3229 alloc_bytes + 256 * 1024 * 1024 < num_bytes)
3232 if (sinfo->bytes_used + sinfo->bytes_reserved +
3233 alloc_bytes < div_factor(num_bytes, 8))
3239 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3240 struct btrfs_root *extent_root, u64 alloc_bytes,
3241 u64 flags, int force)
3243 struct btrfs_space_info *space_info;
3244 struct btrfs_fs_info *fs_info = extent_root->fs_info;
3247 mutex_lock(&fs_info->chunk_mutex);
3249 flags = btrfs_reduce_alloc_profile(extent_root, flags);
3251 space_info = __find_space_info(extent_root->fs_info, flags);
3253 ret = update_space_info(extent_root->fs_info, flags,
3257 BUG_ON(!space_info);
3259 spin_lock(&space_info->lock);
3260 if (space_info->force_alloc)
3262 if (space_info->full) {
3263 spin_unlock(&space_info->lock);
3267 if (!force && !should_alloc_chunk(space_info, alloc_bytes)) {
3268 spin_unlock(&space_info->lock);
3271 spin_unlock(&space_info->lock);
3274 * if we're doing a data chunk, go ahead and make sure that
3275 * we keep a reasonable number of metadata chunks allocated in the
3278 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
3279 fs_info->data_chunk_allocations++;
3280 if (!(fs_info->data_chunk_allocations %
3281 fs_info->metadata_ratio))
3282 force_metadata_allocation(fs_info);
3285 ret = btrfs_alloc_chunk(trans, extent_root, flags);
3286 spin_lock(&space_info->lock);
3288 space_info->full = 1;
3291 space_info->force_alloc = 0;
3292 spin_unlock(&space_info->lock);
3294 mutex_unlock(&extent_root->fs_info->chunk_mutex);
3298 static int maybe_allocate_chunk(struct btrfs_trans_handle *trans,
3299 struct btrfs_root *root,
3300 struct btrfs_space_info *sinfo, u64 num_bytes)
3308 spin_lock(&sinfo->lock);
3309 ret = should_alloc_chunk(sinfo, num_bytes + 2 * 1024 * 1024);
3310 spin_unlock(&sinfo->lock);
3315 trans = btrfs_join_transaction(root, 1);
3316 BUG_ON(IS_ERR(trans));
3320 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3321 num_bytes + 2 * 1024 * 1024,
3322 get_alloc_profile(root, sinfo->flags), 0);
3325 btrfs_end_transaction(trans, root);
3327 return ret == 1 ? 1 : 0;
3331 * shrink metadata reservation for delalloc
3333 static int shrink_delalloc(struct btrfs_trans_handle *trans,
3334 struct btrfs_root *root,
3335 struct btrfs_space_info *sinfo, u64 to_reclaim)
3343 spin_lock(&sinfo->lock);
3344 reserved = sinfo->bytes_delalloc;
3345 spin_unlock(&sinfo->lock);
3350 max_reclaim = min(reserved, to_reclaim);
3353 ret = btrfs_start_one_delalloc_inode(root, trans ? 1 : 0);
3355 __set_current_state(TASK_INTERRUPTIBLE);
3356 schedule_timeout(pause);
3358 if (pause > HZ / 10)
3364 spin_lock(&sinfo->lock);
3365 if (reserved > sinfo->bytes_delalloc)
3366 reclaimed = reserved - sinfo->bytes_delalloc;
3367 reserved = sinfo->bytes_delalloc;
3368 spin_unlock(&sinfo->lock);
3370 if (reserved == 0 || reclaimed >= max_reclaim)
3373 if (trans && trans->transaction->blocked)
3376 return reclaimed >= to_reclaim;
3379 static int should_retry_reserve(struct btrfs_trans_handle *trans,
3380 struct btrfs_root *root,
3381 struct btrfs_block_rsv *block_rsv,
3382 u64 num_bytes, int *retries)
3384 struct btrfs_space_info *space_info = block_rsv->space_info;
3390 ret = maybe_allocate_chunk(trans, root, space_info, num_bytes);
3394 if (trans && trans->transaction->in_commit)
3397 ret = shrink_delalloc(trans, root, space_info, num_bytes);
3401 spin_lock(&space_info->lock);
3402 if (space_info->bytes_pinned < num_bytes)
3404 spin_unlock(&space_info->lock);
3413 trans = btrfs_join_transaction(root, 1);
3414 BUG_ON(IS_ERR(trans));
3415 ret = btrfs_commit_transaction(trans, root);
3421 static int reserve_metadata_bytes(struct btrfs_block_rsv *block_rsv,
3424 struct btrfs_space_info *space_info = block_rsv->space_info;
3428 spin_lock(&space_info->lock);
3429 unused = space_info->bytes_used + space_info->bytes_reserved +
3430 space_info->bytes_pinned + space_info->bytes_readonly;
3432 if (unused < space_info->total_bytes)
3433 unused = space_info->total_bytes - unused;
3437 if (unused >= num_bytes) {
3438 if (block_rsv->priority >= 10) {
3439 space_info->bytes_reserved += num_bytes;
3442 if ((unused + block_rsv->reserved) *
3443 block_rsv->priority >=
3444 (num_bytes + block_rsv->reserved) * 10) {
3445 space_info->bytes_reserved += num_bytes;
3450 spin_unlock(&space_info->lock);
3455 static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
3456 struct btrfs_root *root)
3458 struct btrfs_block_rsv *block_rsv;
3460 block_rsv = trans->block_rsv;
3462 block_rsv = root->block_rsv;
3465 block_rsv = &root->fs_info->empty_block_rsv;
3470 static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
3474 spin_lock(&block_rsv->lock);
3475 if (block_rsv->reserved >= num_bytes) {
3476 block_rsv->reserved -= num_bytes;
3477 if (block_rsv->reserved < block_rsv->size)
3478 block_rsv->full = 0;
3481 spin_unlock(&block_rsv->lock);
3485 static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
3486 u64 num_bytes, int update_size)
3488 spin_lock(&block_rsv->lock);
3489 block_rsv->reserved += num_bytes;
3491 block_rsv->size += num_bytes;
3492 else if (block_rsv->reserved >= block_rsv->size)
3493 block_rsv->full = 1;
3494 spin_unlock(&block_rsv->lock);
3497 void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv,
3498 struct btrfs_block_rsv *dest, u64 num_bytes)
3500 struct btrfs_space_info *space_info = block_rsv->space_info;
3502 spin_lock(&block_rsv->lock);
3503 if (num_bytes == (u64)-1)
3504 num_bytes = block_rsv->size;
3505 block_rsv->size -= num_bytes;
3506 if (block_rsv->reserved >= block_rsv->size) {
3507 num_bytes = block_rsv->reserved - block_rsv->size;
3508 block_rsv->reserved = block_rsv->size;
3509 block_rsv->full = 1;
3513 spin_unlock(&block_rsv->lock);
3515 if (num_bytes > 0) {
3517 block_rsv_add_bytes(dest, num_bytes, 0);
3519 spin_lock(&space_info->lock);
3520 space_info->bytes_reserved -= num_bytes;
3521 spin_unlock(&space_info->lock);
3526 static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
3527 struct btrfs_block_rsv *dst, u64 num_bytes)
3531 ret = block_rsv_use_bytes(src, num_bytes);
3535 block_rsv_add_bytes(dst, num_bytes, 1);
3539 void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
3541 memset(rsv, 0, sizeof(*rsv));
3542 spin_lock_init(&rsv->lock);
3543 atomic_set(&rsv->usage, 1);
3545 INIT_LIST_HEAD(&rsv->list);
3548 struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
3550 struct btrfs_block_rsv *block_rsv;
3551 struct btrfs_fs_info *fs_info = root->fs_info;
3554 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
3558 btrfs_init_block_rsv(block_rsv);
3560 alloc_target = btrfs_get_alloc_profile(root, 0);
3561 block_rsv->space_info = __find_space_info(fs_info,
3562 BTRFS_BLOCK_GROUP_METADATA);
3567 void btrfs_free_block_rsv(struct btrfs_root *root,
3568 struct btrfs_block_rsv *rsv)
3570 if (rsv && atomic_dec_and_test(&rsv->usage)) {
3571 btrfs_block_rsv_release(root, rsv, (u64)-1);
3578 * make the block_rsv struct be able to capture freed space.
3579 * the captured space will re-add to the the block_rsv struct
3580 * after transaction commit
3582 void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info,
3583 struct btrfs_block_rsv *block_rsv)
3585 block_rsv->durable = 1;
3586 mutex_lock(&fs_info->durable_block_rsv_mutex);
3587 list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list);
3588 mutex_unlock(&fs_info->durable_block_rsv_mutex);
3591 int btrfs_block_rsv_add(struct btrfs_trans_handle *trans,
3592 struct btrfs_root *root,
3593 struct btrfs_block_rsv *block_rsv,
3594 u64 num_bytes, int *retries)
3601 ret = reserve_metadata_bytes(block_rsv, num_bytes);
3603 block_rsv_add_bytes(block_rsv, num_bytes, 1);
3607 ret = should_retry_reserve(trans, root, block_rsv, num_bytes, retries);
3614 int btrfs_block_rsv_check(struct btrfs_trans_handle *trans,
3615 struct btrfs_root *root,
3616 struct btrfs_block_rsv *block_rsv,
3617 u64 min_reserved, int min_factor)
3620 int commit_trans = 0;
3626 spin_lock(&block_rsv->lock);
3628 num_bytes = div_factor(block_rsv->size, min_factor);
3629 if (min_reserved > num_bytes)
3630 num_bytes = min_reserved;
3632 if (block_rsv->reserved >= num_bytes) {
3635 num_bytes -= block_rsv->reserved;
3636 if (block_rsv->durable &&
3637 block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes)
3640 spin_unlock(&block_rsv->lock);
3644 if (block_rsv->refill_used) {
3645 ret = reserve_metadata_bytes(block_rsv, num_bytes);
3647 block_rsv_add_bytes(block_rsv, num_bytes, 0);
3656 trans = btrfs_join_transaction(root, 1);
3657 BUG_ON(IS_ERR(trans));
3658 ret = btrfs_commit_transaction(trans, root);
3663 printk(KERN_INFO"block_rsv size %llu reserved %llu freed %llu %llu\n",
3664 block_rsv->size, block_rsv->reserved,
3665 block_rsv->freed[0], block_rsv->freed[1]);
3670 int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
3671 struct btrfs_block_rsv *dst_rsv,
3674 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3677 void btrfs_block_rsv_release(struct btrfs_root *root,
3678 struct btrfs_block_rsv *block_rsv,
3681 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3682 if (global_rsv->full || global_rsv == block_rsv ||
3683 block_rsv->space_info != global_rsv->space_info)
3685 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
3688 static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
3690 struct btrfs_space_info *space_info;
3692 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3693 fs_info->chunk_block_rsv.space_info = space_info;
3694 fs_info->chunk_block_rsv.priority = 10;
3696 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3697 fs_info->trans_block_rsv.space_info = space_info;
3698 fs_info->empty_block_rsv.space_info = space_info;
3699 fs_info->empty_block_rsv.priority = 10;
3701 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
3704 static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items)
3706 return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) *
3710 int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
3711 struct btrfs_root *root,
3712 int num_items, int *retries)
3717 if (num_items == 0 || root->fs_info->chunk_root == root)
3720 num_bytes = calc_trans_metadata_size(root, num_items);
3721 ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv,
3722 num_bytes, retries);
3724 trans->bytes_reserved += num_bytes;
3725 trans->block_rsv = &root->fs_info->trans_block_rsv;
3730 void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
3731 struct btrfs_root *root)
3733 if (!trans->bytes_reserved)
3736 BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
3737 btrfs_block_rsv_release(root, trans->block_rsv,
3738 trans->bytes_reserved);
3739 trans->bytes_reserved = 0;
3742 int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
3743 struct btrfs_pending_snapshot *pending)
3745 struct btrfs_root *root = pending->root;
3746 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3747 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
3749 * two for root back/forward refs, two for directory entries
3750 * and one for root of the snapshot.
3752 u64 num_bytes = calc_trans_metadata_size(root, 5);
3753 dst_rsv->space_info = src_rsv->space_info;
3754 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3757 static int update_block_group(struct btrfs_trans_handle *trans,
3758 struct btrfs_root *root,
3759 u64 bytenr, u64 num_bytes, int alloc)
3761 struct btrfs_block_group_cache *cache;
3762 struct btrfs_fs_info *info = root->fs_info;
3764 u64 total = num_bytes;
3768 /* block accounting for super block */
3769 spin_lock(&info->delalloc_lock);
3770 old_val = btrfs_super_bytes_used(&info->super_copy);
3772 old_val += num_bytes;
3774 old_val -= num_bytes;
3775 btrfs_set_super_bytes_used(&info->super_copy, old_val);
3776 spin_unlock(&info->delalloc_lock);
3779 cache = btrfs_lookup_block_group(info, bytenr);
3782 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
3783 BTRFS_BLOCK_GROUP_RAID1 |
3784 BTRFS_BLOCK_GROUP_RAID10))
3788 byte_in_group = bytenr - cache->key.objectid;
3789 WARN_ON(byte_in_group > cache->key.offset);
3791 spin_lock(&cache->space_info->lock);
3792 spin_lock(&cache->lock);
3794 old_val = btrfs_block_group_used(&cache->item);
3795 num_bytes = min(total, cache->key.offset - byte_in_group);
3797 old_val += num_bytes;
3798 btrfs_set_block_group_used(&cache->item, old_val);
3799 cache->reserved -= num_bytes;
3800 cache->space_info->bytes_reserved -= num_bytes;
3801 cache->space_info->bytes_used += num_bytes;
3802 cache->space_info->disk_used += num_bytes * factor;
3803 spin_unlock(&cache->lock);
3804 spin_unlock(&cache->space_info->lock);
3806 old_val -= num_bytes;
3807 btrfs_set_block_group_used(&cache->item, old_val);
3808 cache->pinned += num_bytes;
3809 cache->space_info->bytes_pinned += num_bytes;
3810 cache->space_info->bytes_used -= num_bytes;
3811 cache->space_info->disk_used -= num_bytes * factor;
3812 spin_unlock(&cache->lock);
3813 spin_unlock(&cache->space_info->lock);
3815 set_extent_dirty(info->pinned_extents,
3816 bytenr, bytenr + num_bytes - 1,
3817 GFP_NOFS | __GFP_NOFAIL);
3819 btrfs_put_block_group(cache);
3821 bytenr += num_bytes;
3826 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
3828 struct btrfs_block_group_cache *cache;
3831 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
3835 bytenr = cache->key.objectid;
3836 btrfs_put_block_group(cache);
3841 static int pin_down_extent(struct btrfs_root *root,
3842 struct btrfs_block_group_cache *cache,
3843 u64 bytenr, u64 num_bytes, int reserved)
3845 spin_lock(&cache->space_info->lock);
3846 spin_lock(&cache->lock);
3847 cache->pinned += num_bytes;
3848 cache->space_info->bytes_pinned += num_bytes;
3850 cache->reserved -= num_bytes;
3851 cache->space_info->bytes_reserved -= num_bytes;
3853 spin_unlock(&cache->lock);
3854 spin_unlock(&cache->space_info->lock);
3856 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
3857 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
3862 * this function must be called within transaction
3864 int btrfs_pin_extent(struct btrfs_root *root,
3865 u64 bytenr, u64 num_bytes, int reserved)
3867 struct btrfs_block_group_cache *cache;
3869 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
3872 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
3874 btrfs_put_block_group(cache);
3879 * update size of reserved extents. this function may return -EAGAIN
3880 * if 'reserve' is true or 'sinfo' is false.
3882 static int update_reserved_bytes(struct btrfs_block_group_cache *cache,
3883 u64 num_bytes, int reserve, int sinfo)
3887 struct btrfs_space_info *space_info = cache->space_info;
3888 spin_lock(&space_info->lock);
3889 spin_lock(&cache->lock);
3894 cache->reserved += num_bytes;
3895 space_info->bytes_reserved += num_bytes;
3899 space_info->bytes_readonly += num_bytes;
3900 cache->reserved -= num_bytes;
3901 space_info->bytes_reserved -= num_bytes;
3903 spin_unlock(&cache->lock);
3904 spin_unlock(&space_info->lock);
3906 spin_lock(&cache->lock);
3911 cache->reserved += num_bytes;
3913 cache->reserved -= num_bytes;
3915 spin_unlock(&cache->lock);
3920 int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
3921 struct btrfs_root *root)
3923 struct btrfs_fs_info *fs_info = root->fs_info;
3924 struct btrfs_caching_control *next;
3925 struct btrfs_caching_control *caching_ctl;
3926 struct btrfs_block_group_cache *cache;
3928 down_write(&fs_info->extent_commit_sem);
3930 list_for_each_entry_safe(caching_ctl, next,
3931 &fs_info->caching_block_groups, list) {
3932 cache = caching_ctl->block_group;
3933 if (block_group_cache_done(cache)) {
3934 cache->last_byte_to_unpin = (u64)-1;
3935 list_del_init(&caching_ctl->list);
3936 put_caching_control(caching_ctl);
3938 cache->last_byte_to_unpin = caching_ctl->progress;
3942 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3943 fs_info->pinned_extents = &fs_info->freed_extents[1];
3945 fs_info->pinned_extents = &fs_info->freed_extents[0];
3947 up_write(&fs_info->extent_commit_sem);
3951 static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
3953 struct btrfs_fs_info *fs_info = root->fs_info;
3954 struct btrfs_block_group_cache *cache = NULL;
3957 while (start <= end) {
3959 start >= cache->key.objectid + cache->key.offset) {
3961 btrfs_put_block_group(cache);
3962 cache = btrfs_lookup_block_group(fs_info, start);
3966 len = cache->key.objectid + cache->key.offset - start;
3967 len = min(len, end + 1 - start);
3969 if (start < cache->last_byte_to_unpin) {
3970 len = min(len, cache->last_byte_to_unpin - start);
3971 btrfs_add_free_space(cache, start, len);
3976 spin_lock(&cache->space_info->lock);
3977 spin_lock(&cache->lock);
3978 cache->pinned -= len;
3979 cache->space_info->bytes_pinned -= len;
3981 cache->space_info->bytes_readonly += len;
3982 } else if (cache->reserved_pinned > 0) {
3983 len = min(len, cache->reserved_pinned);
3984 cache->reserved_pinned -= len;
3985 cache->space_info->bytes_reserved += len;
3987 spin_unlock(&cache->lock);
3988 spin_unlock(&cache->space_info->lock);
3992 btrfs_put_block_group(cache);
3996 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
3997 struct btrfs_root *root)
3999 struct btrfs_fs_info *fs_info = root->fs_info;
4000 struct extent_io_tree *unpin;
4001 struct btrfs_block_rsv *block_rsv;
4002 struct btrfs_block_rsv *next_rsv;
4008 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4009 unpin = &fs_info->freed_extents[1];
4011 unpin = &fs_info->freed_extents[0];
4014 ret = find_first_extent_bit(unpin, 0, &start, &end,
4019 ret = btrfs_discard_extent(root, start, end + 1 - start);
4021 clear_extent_dirty(unpin, start, end, GFP_NOFS);
4022 unpin_extent_range(root, start, end);
4026 mutex_lock(&fs_info->durable_block_rsv_mutex);
4027 list_for_each_entry_safe(block_rsv, next_rsv,
4028 &fs_info->durable_block_rsv_list, list) {
4030 idx = trans->transid & 0x1;
4031 if (block_rsv->freed[idx] > 0) {
4032 block_rsv_add_bytes(block_rsv,
4033 block_rsv->freed[idx], 0);
4034 block_rsv->freed[idx] = 0;
4036 if (atomic_read(&block_rsv->usage) == 0) {
4037 btrfs_block_rsv_release(root, block_rsv, (u64)-1);
4039 if (block_rsv->freed[0] == 0 &&
4040 block_rsv->freed[1] == 0) {
4041 list_del_init(&block_rsv->list);
4045 btrfs_block_rsv_release(root, block_rsv, 0);
4048 mutex_unlock(&fs_info->durable_block_rsv_mutex);
4053 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4054 struct btrfs_root *root,
4055 u64 bytenr, u64 num_bytes, u64 parent,
4056 u64 root_objectid, u64 owner_objectid,
4057 u64 owner_offset, int refs_to_drop,
4058 struct btrfs_delayed_extent_op *extent_op)
4060 struct btrfs_key key;
4061 struct btrfs_path *path;
4062 struct btrfs_fs_info *info = root->fs_info;
4063 struct btrfs_root *extent_root = info->extent_root;
4064 struct extent_buffer *leaf;
4065 struct btrfs_extent_item *ei;
4066 struct btrfs_extent_inline_ref *iref;
4069 int extent_slot = 0;
4070 int found_extent = 0;
4075 path = btrfs_alloc_path();
4080 path->leave_spinning = 1;
4082 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
4083 BUG_ON(!is_data && refs_to_drop != 1);
4085 ret = lookup_extent_backref(trans, extent_root, path, &iref,
4086 bytenr, num_bytes, parent,
4087 root_objectid, owner_objectid,
4090 extent_slot = path->slots[0];
4091 while (extent_slot >= 0) {
4092 btrfs_item_key_to_cpu(path->nodes[0], &key,
4094 if (key.objectid != bytenr)
4096 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
4097 key.offset == num_bytes) {
4101 if (path->slots[0] - extent_slot > 5)
4105 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4106 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
4107 if (found_extent && item_size < sizeof(*ei))
4110 if (!found_extent) {
4112 ret = remove_extent_backref(trans, extent_root, path,
4116 btrfs_release_path(extent_root, path);
4117 path->leave_spinning = 1;
4119 key.objectid = bytenr;
4120 key.type = BTRFS_EXTENT_ITEM_KEY;
4121 key.offset = num_bytes;
4123 ret = btrfs_search_slot(trans, extent_root,
4126 printk(KERN_ERR "umm, got %d back from search"
4127 ", was looking for %llu\n", ret,
4128 (unsigned long long)bytenr);
4129 btrfs_print_leaf(extent_root, path->nodes[0]);
4132 extent_slot = path->slots[0];
4135 btrfs_print_leaf(extent_root, path->nodes[0]);
4137 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
4138 "parent %llu root %llu owner %llu offset %llu\n",
4139 (unsigned long long)bytenr,
4140 (unsigned long long)parent,
4141 (unsigned long long)root_objectid,
4142 (unsigned long long)owner_objectid,
4143 (unsigned long long)owner_offset);
4146 leaf = path->nodes[0];
4147 item_size = btrfs_item_size_nr(leaf, extent_slot);
4148 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4149 if (item_size < sizeof(*ei)) {
4150 BUG_ON(found_extent || extent_slot != path->slots[0]);
4151 ret = convert_extent_item_v0(trans, extent_root, path,
4155 btrfs_release_path(extent_root, path);
4156 path->leave_spinning = 1;
4158 key.objectid = bytenr;
4159 key.type = BTRFS_EXTENT_ITEM_KEY;
4160 key.offset = num_bytes;
4162 ret = btrfs_search_slot(trans, extent_root, &key, path,
4165 printk(KERN_ERR "umm, got %d back from search"
4166 ", was looking for %llu\n", ret,
4167 (unsigned long long)bytenr);
4168 btrfs_print_leaf(extent_root, path->nodes[0]);
4171 extent_slot = path->slots[0];
4172 leaf = path->nodes[0];
4173 item_size = btrfs_item_size_nr(leaf, extent_slot);
4176 BUG_ON(item_size < sizeof(*ei));
4177 ei = btrfs_item_ptr(leaf, extent_slot,
4178 struct btrfs_extent_item);
4179 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4180 struct btrfs_tree_block_info *bi;
4181 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
4182 bi = (struct btrfs_tree_block_info *)(ei + 1);
4183 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
4186 refs = btrfs_extent_refs(leaf, ei);
4187 BUG_ON(refs < refs_to_drop);
4188 refs -= refs_to_drop;
4192 __run_delayed_extent_op(extent_op, leaf, ei);
4194 * In the case of inline back ref, reference count will
4195 * be updated by remove_extent_backref
4198 BUG_ON(!found_extent);
4200 btrfs_set_extent_refs(leaf, ei, refs);
4201 btrfs_mark_buffer_dirty(leaf);
4204 ret = remove_extent_backref(trans, extent_root, path,
4211 BUG_ON(is_data && refs_to_drop !=
4212 extent_data_ref_count(root, path, iref));
4214 BUG_ON(path->slots[0] != extent_slot);
4216 BUG_ON(path->slots[0] != extent_slot + 1);
4217 path->slots[0] = extent_slot;
4222 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
4225 btrfs_release_path(extent_root, path);
4228 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
4231 invalidate_mapping_pages(info->btree_inode->i_mapping,
4232 bytenr >> PAGE_CACHE_SHIFT,
4233 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
4236 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
4239 btrfs_free_path(path);
4244 * when we free an block, it is possible (and likely) that we free the last
4245 * delayed ref for that extent as well. This searches the delayed ref tree for
4246 * a given extent, and if there are no other delayed refs to be processed, it
4247 * removes it from the tree.
4249 static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
4250 struct btrfs_root *root, u64 bytenr)
4252 struct btrfs_delayed_ref_head *head;
4253 struct btrfs_delayed_ref_root *delayed_refs;
4254 struct btrfs_delayed_ref_node *ref;
4255 struct rb_node *node;
4258 delayed_refs = &trans->transaction->delayed_refs;
4259 spin_lock(&delayed_refs->lock);
4260 head = btrfs_find_delayed_ref_head(trans, bytenr);
4264 node = rb_prev(&head->node.rb_node);
4268 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
4270 /* there are still entries for this ref, we can't drop it */
4271 if (ref->bytenr == bytenr)
4274 if (head->extent_op) {
4275 if (!head->must_insert_reserved)
4277 kfree(head->extent_op);
4278 head->extent_op = NULL;
4282 * waiting for the lock here would deadlock. If someone else has it
4283 * locked they are already in the process of dropping it anyway
4285 if (!mutex_trylock(&head->mutex))
4289 * at this point we have a head with no other entries. Go
4290 * ahead and process it.
4292 head->node.in_tree = 0;
4293 rb_erase(&head->node.rb_node, &delayed_refs->root);
4295 delayed_refs->num_entries--;
4298 * we don't take a ref on the node because we're removing it from the
4299 * tree, so we just steal the ref the tree was holding.
4301 delayed_refs->num_heads--;
4302 if (list_empty(&head->cluster))
4303 delayed_refs->num_heads_ready--;
4305 list_del_init(&head->cluster);
4306 spin_unlock(&delayed_refs->lock);
4308 BUG_ON(head->extent_op);
4309 if (head->must_insert_reserved)
4312 mutex_unlock(&head->mutex);
4313 btrfs_put_delayed_ref(&head->node);
4316 spin_unlock(&delayed_refs->lock);
4320 void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4321 struct btrfs_root *root,
4322 struct extent_buffer *buf,
4323 u64 parent, int last_ref)
4325 struct btrfs_block_rsv *block_rsv;
4326 struct btrfs_block_group_cache *cache = NULL;
4329 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4330 ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len,
4331 parent, root->root_key.objectid,
4332 btrfs_header_level(buf),
4333 BTRFS_DROP_DELAYED_REF, NULL);
4340 block_rsv = get_block_rsv(trans, root);
4341 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
4342 BUG_ON(block_rsv->space_info != cache->space_info);
4344 if (btrfs_header_generation(buf) == trans->transid) {
4345 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4346 ret = check_ref_cleanup(trans, root, buf->start);
4351 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
4352 pin_down_extent(root, cache, buf->start, buf->len, 1);
4356 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
4358 btrfs_add_free_space(cache, buf->start, buf->len);
4359 ret = update_reserved_bytes(cache, buf->len, 0, 0);
4360 if (ret == -EAGAIN) {
4361 /* block group became read-only */
4362 update_reserved_bytes(cache, buf->len, 0, 1);
4367 spin_lock(&block_rsv->lock);
4368 if (block_rsv->reserved < block_rsv->size) {
4369 block_rsv->reserved += buf->len;
4372 spin_unlock(&block_rsv->lock);
4375 spin_lock(&cache->space_info->lock);
4376 cache->space_info->bytes_reserved -= buf->len;
4377 spin_unlock(&cache->space_info->lock);
4382 if (block_rsv->durable && !cache->ro) {
4384 spin_lock(&cache->lock);
4386 cache->reserved_pinned += buf->len;
4389 spin_unlock(&cache->lock);
4392 spin_lock(&block_rsv->lock);
4393 block_rsv->freed[trans->transid & 0x1] += buf->len;
4394 spin_unlock(&block_rsv->lock);
4398 btrfs_put_block_group(cache);
4401 int btrfs_free_extent(struct btrfs_trans_handle *trans,
4402 struct btrfs_root *root,
4403 u64 bytenr, u64 num_bytes, u64 parent,
4404 u64 root_objectid, u64 owner, u64 offset)
4409 * tree log blocks never actually go into the extent allocation
4410 * tree, just update pinning info and exit early.
4412 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4413 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
4414 /* unlocks the pinned mutex */
4415 btrfs_pin_extent(root, bytenr, num_bytes, 1);
4417 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4418 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4419 parent, root_objectid, (int)owner,
4420 BTRFS_DROP_DELAYED_REF, NULL);
4423 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4424 parent, root_objectid, owner,
4425 offset, BTRFS_DROP_DELAYED_REF, NULL);
4431 static u64 stripe_align(struct btrfs_root *root, u64 val)
4433 u64 mask = ((u64)root->stripesize - 1);
4434 u64 ret = (val + mask) & ~mask;
4439 * when we wait for progress in the block group caching, its because
4440 * our allocation attempt failed at least once. So, we must sleep
4441 * and let some progress happen before we try again.
4443 * This function will sleep at least once waiting for new free space to
4444 * show up, and then it will check the block group free space numbers
4445 * for our min num_bytes. Another option is to have it go ahead
4446 * and look in the rbtree for a free extent of a given size, but this
4450 wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4453 struct btrfs_caching_control *caching_ctl;
4456 caching_ctl = get_caching_control(cache);
4460 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
4461 (cache->free_space >= num_bytes));
4463 put_caching_control(caching_ctl);
4468 wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4470 struct btrfs_caching_control *caching_ctl;
4473 caching_ctl = get_caching_control(cache);
4477 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4479 put_caching_control(caching_ctl);
4483 static int get_block_group_index(struct btrfs_block_group_cache *cache)
4486 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4488 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4490 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4492 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4499 enum btrfs_loop_type {
4500 LOOP_FIND_IDEAL = 0,
4501 LOOP_CACHING_NOWAIT = 1,
4502 LOOP_CACHING_WAIT = 2,
4503 LOOP_ALLOC_CHUNK = 3,
4504 LOOP_NO_EMPTY_SIZE = 4,
4508 * walks the btree of allocated extents and find a hole of a given size.
4509 * The key ins is changed to record the hole:
4510 * ins->objectid == block start
4511 * ins->flags = BTRFS_EXTENT_ITEM_KEY
4512 * ins->offset == number of blocks
4513 * Any available blocks before search_start are skipped.
4515 static noinline int find_free_extent(struct btrfs_trans_handle *trans,
4516 struct btrfs_root *orig_root,
4517 u64 num_bytes, u64 empty_size,
4518 u64 search_start, u64 search_end,
4519 u64 hint_byte, struct btrfs_key *ins,
4523 struct btrfs_root *root = orig_root->fs_info->extent_root;
4524 struct btrfs_free_cluster *last_ptr = NULL;
4525 struct btrfs_block_group_cache *block_group = NULL;
4526 int empty_cluster = 2 * 1024 * 1024;
4527 int allowed_chunk_alloc = 0;
4528 int done_chunk_alloc = 0;
4529 struct btrfs_space_info *space_info;
4530 int last_ptr_loop = 0;
4533 bool found_uncached_bg = false;
4534 bool failed_cluster_refill = false;
4535 bool failed_alloc = false;
4536 u64 ideal_cache_percent = 0;
4537 u64 ideal_cache_offset = 0;
4539 WARN_ON(num_bytes < root->sectorsize);
4540 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
4544 space_info = __find_space_info(root->fs_info, data);
4546 printk(KERN_ERR "No space info for %d\n", data);
4550 if (orig_root->ref_cows || empty_size)
4551 allowed_chunk_alloc = 1;
4553 if (data & BTRFS_BLOCK_GROUP_METADATA) {
4554 last_ptr = &root->fs_info->meta_alloc_cluster;
4555 if (!btrfs_test_opt(root, SSD))
4556 empty_cluster = 64 * 1024;
4559 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
4560 last_ptr = &root->fs_info->data_alloc_cluster;
4564 spin_lock(&last_ptr->lock);
4565 if (last_ptr->block_group)
4566 hint_byte = last_ptr->window_start;
4567 spin_unlock(&last_ptr->lock);
4570 search_start = max(search_start, first_logical_byte(root, 0));
4571 search_start = max(search_start, hint_byte);
4576 if (search_start == hint_byte) {
4578 block_group = btrfs_lookup_block_group(root->fs_info,
4581 * we don't want to use the block group if it doesn't match our
4582 * allocation bits, or if its not cached.
4584 * However if we are re-searching with an ideal block group
4585 * picked out then we don't care that the block group is cached.
4587 if (block_group && block_group_bits(block_group, data) &&
4588 (block_group->cached != BTRFS_CACHE_NO ||
4589 search_start == ideal_cache_offset)) {
4590 down_read(&space_info->groups_sem);
4591 if (list_empty(&block_group->list) ||
4594 * someone is removing this block group,
4595 * we can't jump into the have_block_group
4596 * target because our list pointers are not
4599 btrfs_put_block_group(block_group);
4600 up_read(&space_info->groups_sem);
4602 index = get_block_group_index(block_group);
4603 goto have_block_group;
4605 } else if (block_group) {
4606 btrfs_put_block_group(block_group);
4610 down_read(&space_info->groups_sem);
4611 list_for_each_entry(block_group, &space_info->block_groups[index],
4616 btrfs_get_block_group(block_group);
4617 search_start = block_group->key.objectid;
4620 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
4623 free_percent = btrfs_block_group_used(&block_group->item);
4624 free_percent *= 100;
4625 free_percent = div64_u64(free_percent,
4626 block_group->key.offset);
4627 free_percent = 100 - free_percent;
4628 if (free_percent > ideal_cache_percent &&
4629 likely(!block_group->ro)) {
4630 ideal_cache_offset = block_group->key.objectid;
4631 ideal_cache_percent = free_percent;
4635 * We only want to start kthread caching if we are at
4636 * the point where we will wait for caching to make
4637 * progress, or if our ideal search is over and we've
4638 * found somebody to start caching.
4640 if (loop > LOOP_CACHING_NOWAIT ||
4641 (loop > LOOP_FIND_IDEAL &&
4642 atomic_read(&space_info->caching_threads) < 2)) {
4643 ret = cache_block_group(block_group);
4646 found_uncached_bg = true;
4649 * If loop is set for cached only, try the next block
4652 if (loop == LOOP_FIND_IDEAL)
4656 cached = block_group_cache_done(block_group);
4657 if (unlikely(!cached))
4658 found_uncached_bg = true;
4660 if (unlikely(block_group->ro))
4664 * Ok we want to try and use the cluster allocator, so lets look
4665 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
4666 * have tried the cluster allocator plenty of times at this
4667 * point and not have found anything, so we are likely way too
4668 * fragmented for the clustering stuff to find anything, so lets
4669 * just skip it and let the allocator find whatever block it can
4672 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
4674 * the refill lock keeps out other
4675 * people trying to start a new cluster
4677 spin_lock(&last_ptr->refill_lock);
4678 if (last_ptr->block_group &&
4679 (last_ptr->block_group->ro ||
4680 !block_group_bits(last_ptr->block_group, data))) {
4682 goto refill_cluster;
4685 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
4686 num_bytes, search_start);
4688 /* we have a block, we're done */
4689 spin_unlock(&last_ptr->refill_lock);
4693 spin_lock(&last_ptr->lock);
4695 * whoops, this cluster doesn't actually point to
4696 * this block group. Get a ref on the block
4697 * group is does point to and try again
4699 if (!last_ptr_loop && last_ptr->block_group &&
4700 last_ptr->block_group != block_group) {
4702 btrfs_put_block_group(block_group);
4703 block_group = last_ptr->block_group;
4704 btrfs_get_block_group(block_group);
4705 spin_unlock(&last_ptr->lock);
4706 spin_unlock(&last_ptr->refill_lock);
4709 search_start = block_group->key.objectid;
4711 * we know this block group is properly
4712 * in the list because
4713 * btrfs_remove_block_group, drops the
4714 * cluster before it removes the block
4715 * group from the list
4717 goto have_block_group;
4719 spin_unlock(&last_ptr->lock);
4722 * this cluster didn't work out, free it and
4725 btrfs_return_cluster_to_free_space(NULL, last_ptr);
4729 /* allocate a cluster in this block group */
4730 ret = btrfs_find_space_cluster(trans, root,
4731 block_group, last_ptr,
4733 empty_cluster + empty_size);
4736 * now pull our allocation out of this
4739 offset = btrfs_alloc_from_cluster(block_group,
4740 last_ptr, num_bytes,
4743 /* we found one, proceed */
4744 spin_unlock(&last_ptr->refill_lock);
4747 } else if (!cached && loop > LOOP_CACHING_NOWAIT
4748 && !failed_cluster_refill) {
4749 spin_unlock(&last_ptr->refill_lock);
4751 failed_cluster_refill = true;
4752 wait_block_group_cache_progress(block_group,
4753 num_bytes + empty_cluster + empty_size);
4754 goto have_block_group;
4758 * at this point we either didn't find a cluster
4759 * or we weren't able to allocate a block from our
4760 * cluster. Free the cluster we've been trying
4761 * to use, and go to the next block group
4763 btrfs_return_cluster_to_free_space(NULL, last_ptr);
4764 spin_unlock(&last_ptr->refill_lock);
4768 offset = btrfs_find_space_for_alloc(block_group, search_start,
4769 num_bytes, empty_size);
4771 * If we didn't find a chunk, and we haven't failed on this
4772 * block group before, and this block group is in the middle of
4773 * caching and we are ok with waiting, then go ahead and wait
4774 * for progress to be made, and set failed_alloc to true.
4776 * If failed_alloc is true then we've already waited on this
4777 * block group once and should move on to the next block group.
4779 if (!offset && !failed_alloc && !cached &&
4780 loop > LOOP_CACHING_NOWAIT) {
4781 wait_block_group_cache_progress(block_group,
4782 num_bytes + empty_size);
4783 failed_alloc = true;
4784 goto have_block_group;
4785 } else if (!offset) {
4789 search_start = stripe_align(root, offset);
4790 /* move on to the next group */
4791 if (search_start + num_bytes >= search_end) {
4792 btrfs_add_free_space(block_group, offset, num_bytes);
4796 /* move on to the next group */
4797 if (search_start + num_bytes >
4798 block_group->key.objectid + block_group->key.offset) {
4799 btrfs_add_free_space(block_group, offset, num_bytes);
4803 ins->objectid = search_start;
4804 ins->offset = num_bytes;
4806 if (offset < search_start)
4807 btrfs_add_free_space(block_group, offset,
4808 search_start - offset);
4809 BUG_ON(offset > search_start);
4811 ret = update_reserved_bytes(block_group, num_bytes, 1,
4812 (data & BTRFS_BLOCK_GROUP_DATA));
4813 if (ret == -EAGAIN) {
4814 btrfs_add_free_space(block_group, offset, num_bytes);
4818 /* we are all good, lets return */
4819 ins->objectid = search_start;
4820 ins->offset = num_bytes;
4822 if (offset < search_start)
4823 btrfs_add_free_space(block_group, offset,
4824 search_start - offset);
4825 BUG_ON(offset > search_start);
4828 failed_cluster_refill = false;
4829 failed_alloc = false;
4830 BUG_ON(index != get_block_group_index(block_group));
4831 btrfs_put_block_group(block_group);
4833 up_read(&space_info->groups_sem);
4835 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
4838 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
4839 * for them to make caching progress. Also
4840 * determine the best possible bg to cache
4841 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
4842 * caching kthreads as we move along
4843 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
4844 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
4845 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
4848 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
4849 (found_uncached_bg || empty_size || empty_cluster ||
4850 allowed_chunk_alloc)) {
4852 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
4853 found_uncached_bg = false;
4855 if (!ideal_cache_percent &&
4856 atomic_read(&space_info->caching_threads))
4860 * 1 of the following 2 things have happened so far
4862 * 1) We found an ideal block group for caching that
4863 * is mostly full and will cache quickly, so we might
4864 * as well wait for it.
4866 * 2) We searched for cached only and we didn't find
4867 * anything, and we didn't start any caching kthreads
4868 * either, so chances are we will loop through and
4869 * start a couple caching kthreads, and then come back
4870 * around and just wait for them. This will be slower
4871 * because we will have 2 caching kthreads reading at
4872 * the same time when we could have just started one
4873 * and waited for it to get far enough to give us an
4874 * allocation, so go ahead and go to the wait caching
4877 loop = LOOP_CACHING_WAIT;
4878 search_start = ideal_cache_offset;
4879 ideal_cache_percent = 0;
4881 } else if (loop == LOOP_FIND_IDEAL) {
4883 * Didn't find a uncached bg, wait on anything we find
4886 loop = LOOP_CACHING_WAIT;
4890 if (loop < LOOP_CACHING_WAIT) {
4895 if (loop == LOOP_ALLOC_CHUNK) {
4900 if (allowed_chunk_alloc) {
4901 ret = do_chunk_alloc(trans, root, num_bytes +
4902 2 * 1024 * 1024, data, 1);
4903 allowed_chunk_alloc = 0;
4904 done_chunk_alloc = 1;
4905 } else if (!done_chunk_alloc) {
4906 space_info->force_alloc = 1;
4909 if (loop < LOOP_NO_EMPTY_SIZE) {
4914 } else if (!ins->objectid) {
4918 /* we found what we needed */
4919 if (ins->objectid) {
4920 if (!(data & BTRFS_BLOCK_GROUP_DATA))
4921 trans->block_group = block_group->key.objectid;
4923 btrfs_put_block_group(block_group);
4930 static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
4931 int dump_block_groups)
4933 struct btrfs_block_group_cache *cache;
4936 spin_lock(&info->lock);
4937 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
4938 (unsigned long long)(info->total_bytes - info->bytes_used -
4939 info->bytes_pinned - info->bytes_reserved -
4941 (info->full) ? "" : "not ");
4942 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
4943 " may_use=%llu, used=%llu, root=%llu, super=%llu, reserved=%llu"
4945 (unsigned long long)info->total_bytes,
4946 (unsigned long long)info->bytes_pinned,
4947 (unsigned long long)info->bytes_delalloc,
4948 (unsigned long long)info->bytes_may_use,
4949 (unsigned long long)info->bytes_used,
4950 (unsigned long long)info->bytes_root,
4951 (unsigned long long)info->bytes_super,
4952 (unsigned long long)info->bytes_reserved);
4953 spin_unlock(&info->lock);
4955 if (!dump_block_groups)
4958 down_read(&info->groups_sem);
4960 list_for_each_entry(cache, &info->block_groups[index], list) {
4961 spin_lock(&cache->lock);
4962 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
4963 "%llu pinned %llu reserved\n",
4964 (unsigned long long)cache->key.objectid,
4965 (unsigned long long)cache->key.offset,
4966 (unsigned long long)btrfs_block_group_used(&cache->item),
4967 (unsigned long long)cache->pinned,
4968 (unsigned long long)cache->reserved);
4969 btrfs_dump_free_space(cache, bytes);
4970 spin_unlock(&cache->lock);
4972 if (++index < BTRFS_NR_RAID_TYPES)
4974 up_read(&info->groups_sem);
4977 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
4978 struct btrfs_root *root,
4979 u64 num_bytes, u64 min_alloc_size,
4980 u64 empty_size, u64 hint_byte,
4981 u64 search_end, struct btrfs_key *ins,
4985 u64 search_start = 0;
4987 data = btrfs_get_alloc_profile(root, data);
4990 * the only place that sets empty_size is btrfs_realloc_node, which
4991 * is not called recursively on allocations
4993 if (empty_size || root->ref_cows)
4994 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
4995 num_bytes + 2 * 1024 * 1024, data, 0);
4997 WARN_ON(num_bytes < root->sectorsize);
4998 ret = find_free_extent(trans, root, num_bytes, empty_size,
4999 search_start, search_end, hint_byte,
5002 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
5003 num_bytes = num_bytes >> 1;
5004 num_bytes = num_bytes & ~(root->sectorsize - 1);
5005 num_bytes = max(num_bytes, min_alloc_size);
5006 do_chunk_alloc(trans, root->fs_info->extent_root,
5007 num_bytes, data, 1);
5010 if (ret == -ENOSPC) {
5011 struct btrfs_space_info *sinfo;
5013 sinfo = __find_space_info(root->fs_info, data);
5014 printk(KERN_ERR "btrfs allocation failed flags %llu, "
5015 "wanted %llu\n", (unsigned long long)data,
5016 (unsigned long long)num_bytes);
5017 dump_space_info(sinfo, num_bytes, 1);
5023 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
5025 struct btrfs_block_group_cache *cache;
5028 cache = btrfs_lookup_block_group(root->fs_info, start);
5030 printk(KERN_ERR "Unable to find block group for %llu\n",
5031 (unsigned long long)start);
5035 ret = btrfs_discard_extent(root, start, len);
5037 btrfs_add_free_space(cache, start, len);
5038 update_reserved_bytes(cache, len, 0, 1);
5039 btrfs_put_block_group(cache);
5044 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5045 struct btrfs_root *root,
5046 u64 parent, u64 root_objectid,
5047 u64 flags, u64 owner, u64 offset,
5048 struct btrfs_key *ins, int ref_mod)
5051 struct btrfs_fs_info *fs_info = root->fs_info;
5052 struct btrfs_extent_item *extent_item;
5053 struct btrfs_extent_inline_ref *iref;
5054 struct btrfs_path *path;
5055 struct extent_buffer *leaf;
5060 type = BTRFS_SHARED_DATA_REF_KEY;
5062 type = BTRFS_EXTENT_DATA_REF_KEY;
5064 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
5066 path = btrfs_alloc_path();
5069 path->leave_spinning = 1;
5070 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5074 leaf = path->nodes[0];
5075 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5076 struct btrfs_extent_item);
5077 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
5078 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5079 btrfs_set_extent_flags(leaf, extent_item,
5080 flags | BTRFS_EXTENT_FLAG_DATA);
5082 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
5083 btrfs_set_extent_inline_ref_type(leaf, iref, type);
5085 struct btrfs_shared_data_ref *ref;
5086 ref = (struct btrfs_shared_data_ref *)(iref + 1);
5087 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5088 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
5090 struct btrfs_extent_data_ref *ref;
5091 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
5092 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
5093 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
5094 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
5095 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
5098 btrfs_mark_buffer_dirty(path->nodes[0]);
5099 btrfs_free_path(path);
5101 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
5103 printk(KERN_ERR "btrfs update block group failed for %llu "
5104 "%llu\n", (unsigned long long)ins->objectid,
5105 (unsigned long long)ins->offset);
5111 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
5112 struct btrfs_root *root,
5113 u64 parent, u64 root_objectid,
5114 u64 flags, struct btrfs_disk_key *key,
5115 int level, struct btrfs_key *ins)
5118 struct btrfs_fs_info *fs_info = root->fs_info;
5119 struct btrfs_extent_item *extent_item;
5120 struct btrfs_tree_block_info *block_info;
5121 struct btrfs_extent_inline_ref *iref;
5122 struct btrfs_path *path;
5123 struct extent_buffer *leaf;
5124 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
5126 path = btrfs_alloc_path();
5129 path->leave_spinning = 1;
5130 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5134 leaf = path->nodes[0];
5135 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5136 struct btrfs_extent_item);
5137 btrfs_set_extent_refs(leaf, extent_item, 1);
5138 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5139 btrfs_set_extent_flags(leaf, extent_item,
5140 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
5141 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
5143 btrfs_set_tree_block_key(leaf, block_info, key);
5144 btrfs_set_tree_block_level(leaf, block_info, level);
5146 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
5148 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
5149 btrfs_set_extent_inline_ref_type(leaf, iref,
5150 BTRFS_SHARED_BLOCK_REF_KEY);
5151 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5153 btrfs_set_extent_inline_ref_type(leaf, iref,
5154 BTRFS_TREE_BLOCK_REF_KEY);
5155 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
5158 btrfs_mark_buffer_dirty(leaf);
5159 btrfs_free_path(path);
5161 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
5163 printk(KERN_ERR "btrfs update block group failed for %llu "
5164 "%llu\n", (unsigned long long)ins->objectid,
5165 (unsigned long long)ins->offset);
5171 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5172 struct btrfs_root *root,
5173 u64 root_objectid, u64 owner,
5174 u64 offset, struct btrfs_key *ins)
5178 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
5180 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
5181 0, root_objectid, owner, offset,
5182 BTRFS_ADD_DELAYED_EXTENT, NULL);
5187 * this is used by the tree logging recovery code. It records that
5188 * an extent has been allocated and makes sure to clear the free
5189 * space cache bits as well
5191 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
5192 struct btrfs_root *root,
5193 u64 root_objectid, u64 owner, u64 offset,
5194 struct btrfs_key *ins)
5197 struct btrfs_block_group_cache *block_group;
5198 struct btrfs_caching_control *caching_ctl;
5199 u64 start = ins->objectid;
5200 u64 num_bytes = ins->offset;
5202 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
5203 cache_block_group(block_group);
5204 caching_ctl = get_caching_control(block_group);
5207 BUG_ON(!block_group_cache_done(block_group));
5208 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5211 mutex_lock(&caching_ctl->mutex);
5213 if (start >= caching_ctl->progress) {
5214 ret = add_excluded_extent(root, start, num_bytes);
5216 } else if (start + num_bytes <= caching_ctl->progress) {
5217 ret = btrfs_remove_free_space(block_group,
5221 num_bytes = caching_ctl->progress - start;
5222 ret = btrfs_remove_free_space(block_group,
5226 start = caching_ctl->progress;
5227 num_bytes = ins->objectid + ins->offset -
5228 caching_ctl->progress;
5229 ret = add_excluded_extent(root, start, num_bytes);
5233 mutex_unlock(&caching_ctl->mutex);
5234 put_caching_control(caching_ctl);
5237 ret = update_reserved_bytes(block_group, ins->offset, 1, 1);
5239 btrfs_put_block_group(block_group);
5240 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
5241 0, owner, offset, ins, 1);
5245 struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
5246 struct btrfs_root *root,
5247 u64 bytenr, u32 blocksize,
5250 struct extent_buffer *buf;
5252 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5254 return ERR_PTR(-ENOMEM);
5255 btrfs_set_header_generation(buf, trans->transid);
5256 btrfs_set_buffer_lockdep_class(buf, level);
5257 btrfs_tree_lock(buf);
5258 clean_tree_block(trans, root, buf);
5260 btrfs_set_lock_blocking(buf);
5261 btrfs_set_buffer_uptodate(buf);
5263 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
5265 * we allow two log transactions at a time, use different
5266 * EXENT bit to differentiate dirty pages.
5268 if (root->log_transid % 2 == 0)
5269 set_extent_dirty(&root->dirty_log_pages, buf->start,
5270 buf->start + buf->len - 1, GFP_NOFS);
5272 set_extent_new(&root->dirty_log_pages, buf->start,
5273 buf->start + buf->len - 1, GFP_NOFS);
5275 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
5276 buf->start + buf->len - 1, GFP_NOFS);
5278 trans->blocks_used++;
5279 /* this returns a buffer locked for blocking */
5283 static struct btrfs_block_rsv *
5284 use_block_rsv(struct btrfs_trans_handle *trans,
5285 struct btrfs_root *root, u32 blocksize)
5287 struct btrfs_block_rsv *block_rsv;
5290 block_rsv = get_block_rsv(trans, root);
5292 if (block_rsv->size == 0) {
5293 ret = reserve_metadata_bytes(block_rsv, blocksize);
5295 return ERR_PTR(ret);
5299 ret = block_rsv_use_bytes(block_rsv, blocksize);
5304 printk(KERN_INFO"block_rsv size %llu reserved %llu freed %llu %llu\n",
5305 block_rsv->size, block_rsv->reserved,
5306 block_rsv->freed[0], block_rsv->freed[1]);
5308 return ERR_PTR(-ENOSPC);
5311 static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
5313 block_rsv_add_bytes(block_rsv, blocksize, 0);
5314 block_rsv_release_bytes(block_rsv, NULL, 0);
5318 * finds a free extent and does all the dirty work required for allocation
5319 * returns the key for the extent through ins, and a tree buffer for
5320 * the first block of the extent through buf.
5322 * returns the tree buffer or NULL.
5324 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
5325 struct btrfs_root *root, u32 blocksize,
5326 u64 parent, u64 root_objectid,
5327 struct btrfs_disk_key *key, int level,
5328 u64 hint, u64 empty_size)
5330 struct btrfs_key ins;
5331 struct btrfs_block_rsv *block_rsv;
5332 struct extent_buffer *buf;
5337 block_rsv = use_block_rsv(trans, root, blocksize);
5338 if (IS_ERR(block_rsv))
5339 return ERR_CAST(block_rsv);
5341 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
5342 empty_size, hint, (u64)-1, &ins, 0);
5344 unuse_block_rsv(block_rsv, blocksize);
5345 return ERR_PTR(ret);
5348 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
5350 BUG_ON(IS_ERR(buf));
5352 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
5354 parent = ins.objectid;
5355 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5359 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
5360 struct btrfs_delayed_extent_op *extent_op;
5361 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
5364 memcpy(&extent_op->key, key, sizeof(extent_op->key));
5366 memset(&extent_op->key, 0, sizeof(extent_op->key));
5367 extent_op->flags_to_set = flags;
5368 extent_op->update_key = 1;
5369 extent_op->update_flags = 1;
5370 extent_op->is_data = 0;
5372 ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
5373 ins.offset, parent, root_objectid,
5374 level, BTRFS_ADD_DELAYED_EXTENT,
5381 struct walk_control {
5382 u64 refs[BTRFS_MAX_LEVEL];
5383 u64 flags[BTRFS_MAX_LEVEL];
5384 struct btrfs_key update_progress;
5394 #define DROP_REFERENCE 1
5395 #define UPDATE_BACKREF 2
5397 static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5398 struct btrfs_root *root,
5399 struct walk_control *wc,
5400 struct btrfs_path *path)
5409 struct btrfs_key key;
5410 struct extent_buffer *eb;
5415 if (path->slots[wc->level] < wc->reada_slot) {
5416 wc->reada_count = wc->reada_count * 2 / 3;
5417 wc->reada_count = max(wc->reada_count, 2);
5419 wc->reada_count = wc->reada_count * 3 / 2;
5420 wc->reada_count = min_t(int, wc->reada_count,
5421 BTRFS_NODEPTRS_PER_BLOCK(root));
5424 eb = path->nodes[wc->level];
5425 nritems = btrfs_header_nritems(eb);
5426 blocksize = btrfs_level_size(root, wc->level - 1);
5428 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5429 if (nread >= wc->reada_count)
5433 bytenr = btrfs_node_blockptr(eb, slot);
5434 generation = btrfs_node_ptr_generation(eb, slot);
5436 if (slot == path->slots[wc->level])
5439 if (wc->stage == UPDATE_BACKREF &&
5440 generation <= root->root_key.offset)
5443 /* We don't lock the tree block, it's OK to be racy here */
5444 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5449 if (wc->stage == DROP_REFERENCE) {
5453 if (wc->level == 1 &&
5454 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5456 if (!wc->update_ref ||
5457 generation <= root->root_key.offset)
5459 btrfs_node_key_to_cpu(eb, &key, slot);
5460 ret = btrfs_comp_cpu_keys(&key,
5461 &wc->update_progress);
5465 if (wc->level == 1 &&
5466 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5470 ret = readahead_tree_block(root, bytenr, blocksize,
5474 last = bytenr + blocksize;
5477 wc->reada_slot = slot;
5481 * hepler to process tree block while walking down the tree.
5483 * when wc->stage == UPDATE_BACKREF, this function updates
5484 * back refs for pointers in the block.
5486 * NOTE: return value 1 means we should stop walking down.
5488 static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5489 struct btrfs_root *root,
5490 struct btrfs_path *path,
5491 struct walk_control *wc, int lookup_info)
5493 int level = wc->level;
5494 struct extent_buffer *eb = path->nodes[level];
5495 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5498 if (wc->stage == UPDATE_BACKREF &&
5499 btrfs_header_owner(eb) != root->root_key.objectid)
5503 * when reference count of tree block is 1, it won't increase
5504 * again. once full backref flag is set, we never clear it.
5507 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
5508 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
5509 BUG_ON(!path->locks[level]);
5510 ret = btrfs_lookup_extent_info(trans, root,
5515 BUG_ON(wc->refs[level] == 0);
5518 if (wc->stage == DROP_REFERENCE) {
5519 if (wc->refs[level] > 1)
5522 if (path->locks[level] && !wc->keep_locks) {
5523 btrfs_tree_unlock(eb);
5524 path->locks[level] = 0;
5529 /* wc->stage == UPDATE_BACKREF */
5530 if (!(wc->flags[level] & flag)) {
5531 BUG_ON(!path->locks[level]);
5532 ret = btrfs_inc_ref(trans, root, eb, 1);
5534 ret = btrfs_dec_ref(trans, root, eb, 0);
5536 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
5539 wc->flags[level] |= flag;
5543 * the block is shared by multiple trees, so it's not good to
5544 * keep the tree lock
5546 if (path->locks[level] && level > 0) {
5547 btrfs_tree_unlock(eb);
5548 path->locks[level] = 0;
5554 * hepler to process tree block pointer.
5556 * when wc->stage == DROP_REFERENCE, this function checks
5557 * reference count of the block pointed to. if the block
5558 * is shared and we need update back refs for the subtree
5559 * rooted at the block, this function changes wc->stage to
5560 * UPDATE_BACKREF. if the block is shared and there is no
5561 * need to update back, this function drops the reference
5564 * NOTE: return value 1 means we should stop walking down.
5566 static noinline int do_walk_down(struct btrfs_trans_handle *trans,
5567 struct btrfs_root *root,
5568 struct btrfs_path *path,
5569 struct walk_control *wc, int *lookup_info)
5575 struct btrfs_key key;
5576 struct extent_buffer *next;
5577 int level = wc->level;
5581 generation = btrfs_node_ptr_generation(path->nodes[level],
5582 path->slots[level]);
5584 * if the lower level block was created before the snapshot
5585 * was created, we know there is no need to update back refs
5588 if (wc->stage == UPDATE_BACKREF &&
5589 generation <= root->root_key.offset) {
5594 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
5595 blocksize = btrfs_level_size(root, level - 1);
5597 next = btrfs_find_tree_block(root, bytenr, blocksize);
5599 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
5604 btrfs_tree_lock(next);
5605 btrfs_set_lock_blocking(next);
5607 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5608 &wc->refs[level - 1],
5609 &wc->flags[level - 1]);
5611 BUG_ON(wc->refs[level - 1] == 0);
5614 if (wc->stage == DROP_REFERENCE) {
5615 if (wc->refs[level - 1] > 1) {
5617 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5620 if (!wc->update_ref ||
5621 generation <= root->root_key.offset)
5624 btrfs_node_key_to_cpu(path->nodes[level], &key,
5625 path->slots[level]);
5626 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
5630 wc->stage = UPDATE_BACKREF;
5631 wc->shared_level = level - 1;
5635 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5639 if (!btrfs_buffer_uptodate(next, generation)) {
5640 btrfs_tree_unlock(next);
5641 free_extent_buffer(next);
5647 if (reada && level == 1)
5648 reada_walk_down(trans, root, wc, path);
5649 next = read_tree_block(root, bytenr, blocksize, generation);
5650 btrfs_tree_lock(next);
5651 btrfs_set_lock_blocking(next);
5655 BUG_ON(level != btrfs_header_level(next));
5656 path->nodes[level] = next;
5657 path->slots[level] = 0;
5658 path->locks[level] = 1;
5664 wc->refs[level - 1] = 0;
5665 wc->flags[level - 1] = 0;
5666 if (wc->stage == DROP_REFERENCE) {
5667 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
5668 parent = path->nodes[level]->start;
5670 BUG_ON(root->root_key.objectid !=
5671 btrfs_header_owner(path->nodes[level]));
5675 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
5676 root->root_key.objectid, level - 1, 0);
5679 btrfs_tree_unlock(next);
5680 free_extent_buffer(next);
5686 * hepler to process tree block while walking up the tree.
5688 * when wc->stage == DROP_REFERENCE, this function drops
5689 * reference count on the block.
5691 * when wc->stage == UPDATE_BACKREF, this function changes
5692 * wc->stage back to DROP_REFERENCE if we changed wc->stage
5693 * to UPDATE_BACKREF previously while processing the block.
5695 * NOTE: return value 1 means we should stop walking up.
5697 static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
5698 struct btrfs_root *root,
5699 struct btrfs_path *path,
5700 struct walk_control *wc)
5703 int level = wc->level;
5704 struct extent_buffer *eb = path->nodes[level];
5707 if (wc->stage == UPDATE_BACKREF) {
5708 BUG_ON(wc->shared_level < level);
5709 if (level < wc->shared_level)
5712 ret = find_next_key(path, level + 1, &wc->update_progress);
5716 wc->stage = DROP_REFERENCE;
5717 wc->shared_level = -1;
5718 path->slots[level] = 0;
5721 * check reference count again if the block isn't locked.
5722 * we should start walking down the tree again if reference
5725 if (!path->locks[level]) {
5727 btrfs_tree_lock(eb);
5728 btrfs_set_lock_blocking(eb);
5729 path->locks[level] = 1;
5731 ret = btrfs_lookup_extent_info(trans, root,
5736 BUG_ON(wc->refs[level] == 0);
5737 if (wc->refs[level] == 1) {
5738 btrfs_tree_unlock(eb);
5739 path->locks[level] = 0;
5745 /* wc->stage == DROP_REFERENCE */
5746 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
5748 if (wc->refs[level] == 1) {
5750 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5751 ret = btrfs_dec_ref(trans, root, eb, 1);
5753 ret = btrfs_dec_ref(trans, root, eb, 0);
5756 /* make block locked assertion in clean_tree_block happy */
5757 if (!path->locks[level] &&
5758 btrfs_header_generation(eb) == trans->transid) {
5759 btrfs_tree_lock(eb);
5760 btrfs_set_lock_blocking(eb);
5761 path->locks[level] = 1;
5763 clean_tree_block(trans, root, eb);
5766 if (eb == root->node) {
5767 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5770 BUG_ON(root->root_key.objectid !=
5771 btrfs_header_owner(eb));
5773 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5774 parent = path->nodes[level + 1]->start;
5776 BUG_ON(root->root_key.objectid !=
5777 btrfs_header_owner(path->nodes[level + 1]));
5780 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
5782 wc->refs[level] = 0;
5783 wc->flags[level] = 0;
5787 static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
5788 struct btrfs_root *root,
5789 struct btrfs_path *path,
5790 struct walk_control *wc)
5792 int level = wc->level;
5793 int lookup_info = 1;
5796 while (level >= 0) {
5797 ret = walk_down_proc(trans, root, path, wc, lookup_info);
5804 if (path->slots[level] >=
5805 btrfs_header_nritems(path->nodes[level]))
5808 ret = do_walk_down(trans, root, path, wc, &lookup_info);
5810 path->slots[level]++;
5819 static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
5820 struct btrfs_root *root,
5821 struct btrfs_path *path,
5822 struct walk_control *wc, int max_level)
5824 int level = wc->level;
5827 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
5828 while (level < max_level && path->nodes[level]) {
5830 if (path->slots[level] + 1 <
5831 btrfs_header_nritems(path->nodes[level])) {
5832 path->slots[level]++;
5835 ret = walk_up_proc(trans, root, path, wc);
5839 if (path->locks[level]) {
5840 btrfs_tree_unlock(path->nodes[level]);
5841 path->locks[level] = 0;
5843 free_extent_buffer(path->nodes[level]);
5844 path->nodes[level] = NULL;
5852 * drop a subvolume tree.
5854 * this function traverses the tree freeing any blocks that only
5855 * referenced by the tree.
5857 * when a shared tree block is found. this function decreases its
5858 * reference count by one. if update_ref is true, this function
5859 * also make sure backrefs for the shared block and all lower level
5860 * blocks are properly updated.
5862 int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref)
5864 struct btrfs_path *path;
5865 struct btrfs_trans_handle *trans;
5866 struct btrfs_root *tree_root = root->fs_info->tree_root;
5867 struct btrfs_root_item *root_item = &root->root_item;
5868 struct walk_control *wc;
5869 struct btrfs_key key;
5874 path = btrfs_alloc_path();
5877 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5880 trans = btrfs_start_transaction(tree_root, 0);
5882 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
5883 level = btrfs_header_level(root->node);
5884 path->nodes[level] = btrfs_lock_root_node(root);
5885 btrfs_set_lock_blocking(path->nodes[level]);
5886 path->slots[level] = 0;
5887 path->locks[level] = 1;
5888 memset(&wc->update_progress, 0,
5889 sizeof(wc->update_progress));
5891 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
5892 memcpy(&wc->update_progress, &key,
5893 sizeof(wc->update_progress));
5895 level = root_item->drop_level;
5897 path->lowest_level = level;
5898 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5899 path->lowest_level = 0;
5907 * unlock our path, this is safe because only this
5908 * function is allowed to delete this snapshot
5910 btrfs_unlock_up_safe(path, 0);
5912 level = btrfs_header_level(root->node);
5914 btrfs_tree_lock(path->nodes[level]);
5915 btrfs_set_lock_blocking(path->nodes[level]);
5917 ret = btrfs_lookup_extent_info(trans, root,
5918 path->nodes[level]->start,
5919 path->nodes[level]->len,
5923 BUG_ON(wc->refs[level] == 0);
5925 if (level == root_item->drop_level)
5928 btrfs_tree_unlock(path->nodes[level]);
5929 WARN_ON(wc->refs[level] != 1);
5935 wc->shared_level = -1;
5936 wc->stage = DROP_REFERENCE;
5937 wc->update_ref = update_ref;
5939 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
5942 ret = walk_down_tree(trans, root, path, wc);
5948 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5955 BUG_ON(wc->stage != DROP_REFERENCE);
5959 if (wc->stage == DROP_REFERENCE) {
5961 btrfs_node_key(path->nodes[level],
5962 &root_item->drop_progress,
5963 path->slots[level]);
5964 root_item->drop_level = level;
5967 BUG_ON(wc->level == 0);
5968 if (trans->transaction->in_commit ||
5969 trans->transaction->delayed_refs.flushing) {
5970 ret = btrfs_update_root(trans, tree_root,
5975 btrfs_end_transaction(trans, tree_root);
5976 trans = btrfs_start_transaction(tree_root, 0);
5978 return PTR_ERR(trans);
5980 unsigned long update;
5981 update = trans->delayed_ref_updates;
5982 trans->delayed_ref_updates = 0;
5984 btrfs_run_delayed_refs(trans, tree_root,
5988 btrfs_release_path(root, path);
5991 ret = btrfs_del_root(trans, tree_root, &root->root_key);
5994 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
5995 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
5999 ret = btrfs_del_orphan_item(trans, tree_root,
6000 root->root_key.objectid);
6005 if (root->in_radix) {
6006 btrfs_free_fs_root(tree_root->fs_info, root);
6008 free_extent_buffer(root->node);
6009 free_extent_buffer(root->commit_root);
6013 btrfs_end_transaction(trans, tree_root);
6015 btrfs_free_path(path);
6020 * drop subtree rooted at tree block 'node'.
6022 * NOTE: this function will unlock and release tree block 'node'
6024 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
6025 struct btrfs_root *root,
6026 struct extent_buffer *node,
6027 struct extent_buffer *parent)
6029 struct btrfs_path *path;
6030 struct walk_control *wc;
6036 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6038 path = btrfs_alloc_path();
6041 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6044 btrfs_assert_tree_locked(parent);
6045 parent_level = btrfs_header_level(parent);
6046 extent_buffer_get(parent);
6047 path->nodes[parent_level] = parent;
6048 path->slots[parent_level] = btrfs_header_nritems(parent);
6050 btrfs_assert_tree_locked(node);
6051 level = btrfs_header_level(node);
6052 path->nodes[level] = node;
6053 path->slots[level] = 0;
6054 path->locks[level] = 1;
6056 wc->refs[parent_level] = 1;
6057 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6059 wc->shared_level = -1;
6060 wc->stage = DROP_REFERENCE;
6063 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
6066 wret = walk_down_tree(trans, root, path, wc);
6072 wret = walk_up_tree(trans, root, path, wc, parent_level);
6080 btrfs_free_path(path);
6085 static unsigned long calc_ra(unsigned long start, unsigned long last,
6088 return min(last, start + nr - 1);
6091 static noinline int relocate_inode_pages(struct inode *inode, u64 start,
6096 unsigned long first_index;
6097 unsigned long last_index;
6100 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6101 struct file_ra_state *ra;
6102 struct btrfs_ordered_extent *ordered;
6103 unsigned int total_read = 0;
6104 unsigned int total_dirty = 0;
6107 ra = kzalloc(sizeof(*ra), GFP_NOFS);
6109 mutex_lock(&inode->i_mutex);
6110 first_index = start >> PAGE_CACHE_SHIFT;
6111 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
6113 /* make sure the dirty trick played by the caller work */
6114 ret = invalidate_inode_pages2_range(inode->i_mapping,
6115 first_index, last_index);
6119 file_ra_state_init(ra, inode->i_mapping);
6121 for (i = first_index ; i <= last_index; i++) {
6122 if (total_read % ra->ra_pages == 0) {
6123 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
6124 calc_ra(i, last_index, ra->ra_pages));
6128 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
6130 page = grab_cache_page(inode->i_mapping, i);
6135 if (!PageUptodate(page)) {
6136 btrfs_readpage(NULL, page);
6138 if (!PageUptodate(page)) {
6140 page_cache_release(page);
6145 wait_on_page_writeback(page);
6147 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
6148 page_end = page_start + PAGE_CACHE_SIZE - 1;
6149 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
6151 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6153 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
6155 page_cache_release(page);
6156 btrfs_start_ordered_extent(inode, ordered, 1);
6157 btrfs_put_ordered_extent(ordered);
6160 set_page_extent_mapped(page);
6162 if (i == first_index)
6163 set_extent_bits(io_tree, page_start, page_end,
6164 EXTENT_BOUNDARY, GFP_NOFS);
6165 btrfs_set_extent_delalloc(inode, page_start, page_end);
6167 set_page_dirty(page);
6170 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
6172 page_cache_release(page);
6177 mutex_unlock(&inode->i_mutex);
6178 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
6182 static noinline int relocate_data_extent(struct inode *reloc_inode,
6183 struct btrfs_key *extent_key,
6186 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6187 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
6188 struct extent_map *em;
6189 u64 start = extent_key->objectid - offset;
6190 u64 end = start + extent_key->offset - 1;
6192 em = alloc_extent_map(GFP_NOFS);
6193 BUG_ON(!em || IS_ERR(em));
6196 em->len = extent_key->offset;
6197 em->block_len = extent_key->offset;
6198 em->block_start = extent_key->objectid;
6199 em->bdev = root->fs_info->fs_devices->latest_bdev;
6200 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6202 /* setup extent map to cheat btrfs_readpage */
6203 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
6206 write_lock(&em_tree->lock);
6207 ret = add_extent_mapping(em_tree, em);
6208 write_unlock(&em_tree->lock);
6209 if (ret != -EEXIST) {
6210 free_extent_map(em);
6213 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
6215 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
6217 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
6220 struct btrfs_ref_path {
6222 u64 nodes[BTRFS_MAX_LEVEL];
6224 u64 root_generation;
6231 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
6232 u64 new_nodes[BTRFS_MAX_LEVEL];
6235 struct disk_extent {
6246 static int is_cowonly_root(u64 root_objectid)
6248 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
6249 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
6250 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
6251 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
6252 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6253 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
6258 static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
6259 struct btrfs_root *extent_root,
6260 struct btrfs_ref_path *ref_path,
6263 struct extent_buffer *leaf;
6264 struct btrfs_path *path;
6265 struct btrfs_extent_ref *ref;
6266 struct btrfs_key key;
6267 struct btrfs_key found_key;
6273 path = btrfs_alloc_path();
6278 ref_path->lowest_level = -1;
6279 ref_path->current_level = -1;
6280 ref_path->shared_level = -1;
6284 level = ref_path->current_level - 1;
6285 while (level >= -1) {
6287 if (level < ref_path->lowest_level)
6291 bytenr = ref_path->nodes[level];
6293 bytenr = ref_path->extent_start;
6294 BUG_ON(bytenr == 0);
6296 parent = ref_path->nodes[level + 1];
6297 ref_path->nodes[level + 1] = 0;
6298 ref_path->current_level = level;
6299 BUG_ON(parent == 0);
6301 key.objectid = bytenr;
6302 key.offset = parent + 1;
6303 key.type = BTRFS_EXTENT_REF_KEY;
6305 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
6310 leaf = path->nodes[0];
6311 nritems = btrfs_header_nritems(leaf);
6312 if (path->slots[0] >= nritems) {
6313 ret = btrfs_next_leaf(extent_root, path);
6318 leaf = path->nodes[0];
6321 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6322 if (found_key.objectid == bytenr &&
6323 found_key.type == BTRFS_EXTENT_REF_KEY) {
6324 if (level < ref_path->shared_level)
6325 ref_path->shared_level = level;
6330 btrfs_release_path(extent_root, path);
6333 /* reached lowest level */
6337 level = ref_path->current_level;
6338 while (level < BTRFS_MAX_LEVEL - 1) {
6342 bytenr = ref_path->nodes[level];
6344 bytenr = ref_path->extent_start;
6346 BUG_ON(bytenr == 0);
6348 key.objectid = bytenr;
6350 key.type = BTRFS_EXTENT_REF_KEY;
6352 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
6356 leaf = path->nodes[0];
6357 nritems = btrfs_header_nritems(leaf);
6358 if (path->slots[0] >= nritems) {
6359 ret = btrfs_next_leaf(extent_root, path);
6363 /* the extent was freed by someone */
6364 if (ref_path->lowest_level == level)
6366 btrfs_release_path(extent_root, path);
6369 leaf = path->nodes[0];
6372 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6373 if (found_key.objectid != bytenr ||
6374 found_key.type != BTRFS_EXTENT_REF_KEY) {
6375 /* the extent was freed by someone */
6376 if (ref_path->lowest_level == level) {
6380 btrfs_release_path(extent_root, path);
6384 ref = btrfs_item_ptr(leaf, path->slots[0],
6385 struct btrfs_extent_ref);
6386 ref_objectid = btrfs_ref_objectid(leaf, ref);
6387 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
6389 level = (int)ref_objectid;
6390 BUG_ON(level >= BTRFS_MAX_LEVEL);
6391 ref_path->lowest_level = level;
6392 ref_path->current_level = level;
6393 ref_path->nodes[level] = bytenr;
6395 WARN_ON(ref_objectid != level);
6398 WARN_ON(level != -1);
6402 if (ref_path->lowest_level == level) {
6403 ref_path->owner_objectid = ref_objectid;
6404 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
6408 * the block is tree root or the block isn't in reference
6411 if (found_key.objectid == found_key.offset ||
6412 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
6413 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6414 ref_path->root_generation =
6415 btrfs_ref_generation(leaf, ref);
6417 /* special reference from the tree log */
6418 ref_path->nodes[0] = found_key.offset;
6419 ref_path->current_level = 0;
6426 BUG_ON(ref_path->nodes[level] != 0);
6427 ref_path->nodes[level] = found_key.offset;
6428 ref_path->current_level = level;
6431 * the reference was created in the running transaction,
6432 * no need to continue walking up.
6434 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
6435 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6436 ref_path->root_generation =
6437 btrfs_ref_generation(leaf, ref);
6442 btrfs_release_path(extent_root, path);
6445 /* reached max tree level, but no tree root found. */
6448 btrfs_free_path(path);
6452 static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
6453 struct btrfs_root *extent_root,
6454 struct btrfs_ref_path *ref_path,
6457 memset(ref_path, 0, sizeof(*ref_path));
6458 ref_path->extent_start = extent_start;
6460 return __next_ref_path(trans, extent_root, ref_path, 1);
6463 static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
6464 struct btrfs_root *extent_root,
6465 struct btrfs_ref_path *ref_path)
6467 return __next_ref_path(trans, extent_root, ref_path, 0);
6470 static noinline int get_new_locations(struct inode *reloc_inode,
6471 struct btrfs_key *extent_key,
6472 u64 offset, int no_fragment,
6473 struct disk_extent **extents,
6476 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6477 struct btrfs_path *path;
6478 struct btrfs_file_extent_item *fi;
6479 struct extent_buffer *leaf;
6480 struct disk_extent *exts = *extents;
6481 struct btrfs_key found_key;
6486 int max = *nr_extents;
6489 WARN_ON(!no_fragment && *extents);
6492 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
6497 path = btrfs_alloc_path();
6500 cur_pos = extent_key->objectid - offset;
6501 last_byte = extent_key->objectid + extent_key->offset;
6502 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
6512 leaf = path->nodes[0];
6513 nritems = btrfs_header_nritems(leaf);
6514 if (path->slots[0] >= nritems) {
6515 ret = btrfs_next_leaf(root, path);
6520 leaf = path->nodes[0];
6523 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6524 if (found_key.offset != cur_pos ||
6525 found_key.type != BTRFS_EXTENT_DATA_KEY ||
6526 found_key.objectid != reloc_inode->i_ino)
6529 fi = btrfs_item_ptr(leaf, path->slots[0],
6530 struct btrfs_file_extent_item);
6531 if (btrfs_file_extent_type(leaf, fi) !=
6532 BTRFS_FILE_EXTENT_REG ||
6533 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6537 struct disk_extent *old = exts;
6539 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
6540 memcpy(exts, old, sizeof(*exts) * nr);
6541 if (old != *extents)
6545 exts[nr].disk_bytenr =
6546 btrfs_file_extent_disk_bytenr(leaf, fi);
6547 exts[nr].disk_num_bytes =
6548 btrfs_file_extent_disk_num_bytes(leaf, fi);
6549 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
6550 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6551 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6552 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
6553 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
6554 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
6556 BUG_ON(exts[nr].offset > 0);
6557 BUG_ON(exts[nr].compression || exts[nr].encryption);
6558 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
6560 cur_pos += exts[nr].num_bytes;
6563 if (cur_pos + offset >= last_byte)
6573 BUG_ON(cur_pos + offset > last_byte);
6574 if (cur_pos + offset < last_byte) {
6580 btrfs_free_path(path);
6582 if (exts != *extents)
6591 static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
6592 struct btrfs_root *root,
6593 struct btrfs_path *path,
6594 struct btrfs_key *extent_key,
6595 struct btrfs_key *leaf_key,
6596 struct btrfs_ref_path *ref_path,
6597 struct disk_extent *new_extents,
6600 struct extent_buffer *leaf;
6601 struct btrfs_file_extent_item *fi;
6602 struct inode *inode = NULL;
6603 struct btrfs_key key;
6608 u64 search_end = (u64)-1;
6611 int extent_locked = 0;
6615 memcpy(&key, leaf_key, sizeof(key));
6616 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
6617 if (key.objectid < ref_path->owner_objectid ||
6618 (key.objectid == ref_path->owner_objectid &&
6619 key.type < BTRFS_EXTENT_DATA_KEY)) {
6620 key.objectid = ref_path->owner_objectid;
6621 key.type = BTRFS_EXTENT_DATA_KEY;
6627 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
6631 leaf = path->nodes[0];
6632 nritems = btrfs_header_nritems(leaf);
6634 if (extent_locked && ret > 0) {
6636 * the file extent item was modified by someone
6637 * before the extent got locked.
6639 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6640 lock_end, GFP_NOFS);
6644 if (path->slots[0] >= nritems) {
6645 if (++nr_scaned > 2)
6648 BUG_ON(extent_locked);
6649 ret = btrfs_next_leaf(root, path);
6654 leaf = path->nodes[0];
6655 nritems = btrfs_header_nritems(leaf);
6658 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6660 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
6661 if ((key.objectid > ref_path->owner_objectid) ||
6662 (key.objectid == ref_path->owner_objectid &&
6663 key.type > BTRFS_EXTENT_DATA_KEY) ||
6664 key.offset >= search_end)
6668 if (inode && key.objectid != inode->i_ino) {
6669 BUG_ON(extent_locked);
6670 btrfs_release_path(root, path);
6671 mutex_unlock(&inode->i_mutex);
6677 if (key.type != BTRFS_EXTENT_DATA_KEY) {
6682 fi = btrfs_item_ptr(leaf, path->slots[0],
6683 struct btrfs_file_extent_item);
6684 extent_type = btrfs_file_extent_type(leaf, fi);
6685 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
6686 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
6687 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
6688 extent_key->objectid)) {
6694 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6695 ext_offset = btrfs_file_extent_offset(leaf, fi);
6697 if (search_end == (u64)-1) {
6698 search_end = key.offset - ext_offset +
6699 btrfs_file_extent_ram_bytes(leaf, fi);
6702 if (!extent_locked) {
6703 lock_start = key.offset;
6704 lock_end = lock_start + num_bytes - 1;
6706 if (lock_start > key.offset ||
6707 lock_end + 1 < key.offset + num_bytes) {
6708 unlock_extent(&BTRFS_I(inode)->io_tree,
6709 lock_start, lock_end, GFP_NOFS);
6715 btrfs_release_path(root, path);
6717 inode = btrfs_iget_locked(root->fs_info->sb,
6718 key.objectid, root);
6719 if (inode->i_state & I_NEW) {
6720 BTRFS_I(inode)->root = root;
6721 BTRFS_I(inode)->location.objectid =
6723 BTRFS_I(inode)->location.type =
6724 BTRFS_INODE_ITEM_KEY;
6725 BTRFS_I(inode)->location.offset = 0;
6726 btrfs_read_locked_inode(inode);
6727 unlock_new_inode(inode);
6730 * some code call btrfs_commit_transaction while
6731 * holding the i_mutex, so we can't use mutex_lock
6734 if (is_bad_inode(inode) ||
6735 !mutex_trylock(&inode->i_mutex)) {
6738 key.offset = (u64)-1;
6743 if (!extent_locked) {
6744 struct btrfs_ordered_extent *ordered;
6746 btrfs_release_path(root, path);
6748 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6749 lock_end, GFP_NOFS);
6750 ordered = btrfs_lookup_first_ordered_extent(inode,
6753 ordered->file_offset <= lock_end &&
6754 ordered->file_offset + ordered->len > lock_start) {
6755 unlock_extent(&BTRFS_I(inode)->io_tree,
6756 lock_start, lock_end, GFP_NOFS);
6757 btrfs_start_ordered_extent(inode, ordered, 1);
6758 btrfs_put_ordered_extent(ordered);
6759 key.offset += num_bytes;
6763 btrfs_put_ordered_extent(ordered);
6769 if (nr_extents == 1) {
6770 /* update extent pointer in place */
6771 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6772 new_extents[0].disk_bytenr);
6773 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6774 new_extents[0].disk_num_bytes);
6775 btrfs_mark_buffer_dirty(leaf);
6777 btrfs_drop_extent_cache(inode, key.offset,
6778 key.offset + num_bytes - 1, 0);
6780 ret = btrfs_inc_extent_ref(trans, root,
6781 new_extents[0].disk_bytenr,
6782 new_extents[0].disk_num_bytes,
6784 root->root_key.objectid,
6789 ret = btrfs_free_extent(trans, root,
6790 extent_key->objectid,
6793 btrfs_header_owner(leaf),
6794 btrfs_header_generation(leaf),
6798 btrfs_release_path(root, path);
6799 key.offset += num_bytes;
6807 * drop old extent pointer at first, then insert the
6808 * new pointers one bye one
6810 btrfs_release_path(root, path);
6811 ret = btrfs_drop_extents(trans, root, inode, key.offset,
6812 key.offset + num_bytes,
6813 key.offset, &alloc_hint);
6816 for (i = 0; i < nr_extents; i++) {
6817 if (ext_offset >= new_extents[i].num_bytes) {
6818 ext_offset -= new_extents[i].num_bytes;
6821 extent_len = min(new_extents[i].num_bytes -
6822 ext_offset, num_bytes);
6824 ret = btrfs_insert_empty_item(trans, root,
6829 leaf = path->nodes[0];
6830 fi = btrfs_item_ptr(leaf, path->slots[0],
6831 struct btrfs_file_extent_item);
6832 btrfs_set_file_extent_generation(leaf, fi,
6834 btrfs_set_file_extent_type(leaf, fi,
6835 BTRFS_FILE_EXTENT_REG);
6836 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6837 new_extents[i].disk_bytenr);
6838 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6839 new_extents[i].disk_num_bytes);
6840 btrfs_set_file_extent_ram_bytes(leaf, fi,
6841 new_extents[i].ram_bytes);
6843 btrfs_set_file_extent_compression(leaf, fi,
6844 new_extents[i].compression);
6845 btrfs_set_file_extent_encryption(leaf, fi,
6846 new_extents[i].encryption);
6847 btrfs_set_file_extent_other_encoding(leaf, fi,
6848 new_extents[i].other_encoding);
6850 btrfs_set_file_extent_num_bytes(leaf, fi,
6852 ext_offset += new_extents[i].offset;
6853 btrfs_set_file_extent_offset(leaf, fi,
6855 btrfs_mark_buffer_dirty(leaf);
6857 btrfs_drop_extent_cache(inode, key.offset,
6858 key.offset + extent_len - 1, 0);
6860 ret = btrfs_inc_extent_ref(trans, root,
6861 new_extents[i].disk_bytenr,
6862 new_extents[i].disk_num_bytes,
6864 root->root_key.objectid,
6865 trans->transid, key.objectid);
6867 btrfs_release_path(root, path);
6869 inode_add_bytes(inode, extent_len);
6872 num_bytes -= extent_len;
6873 key.offset += extent_len;
6878 BUG_ON(i >= nr_extents);
6882 if (extent_locked) {
6883 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6884 lock_end, GFP_NOFS);
6888 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
6889 key.offset >= search_end)
6896 btrfs_release_path(root, path);
6898 mutex_unlock(&inode->i_mutex);
6899 if (extent_locked) {
6900 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6901 lock_end, GFP_NOFS);
6908 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
6909 struct btrfs_root *root,
6910 struct extent_buffer *buf, u64 orig_start)
6915 BUG_ON(btrfs_header_generation(buf) != trans->transid);
6916 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6918 level = btrfs_header_level(buf);
6920 struct btrfs_leaf_ref *ref;
6921 struct btrfs_leaf_ref *orig_ref;
6923 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
6927 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
6929 btrfs_free_leaf_ref(root, orig_ref);
6933 ref->nritems = orig_ref->nritems;
6934 memcpy(ref->extents, orig_ref->extents,
6935 sizeof(ref->extents[0]) * ref->nritems);
6937 btrfs_free_leaf_ref(root, orig_ref);
6939 ref->root_gen = trans->transid;
6940 ref->bytenr = buf->start;
6941 ref->owner = btrfs_header_owner(buf);
6942 ref->generation = btrfs_header_generation(buf);
6944 ret = btrfs_add_leaf_ref(root, ref, 0);
6946 btrfs_free_leaf_ref(root, ref);
6951 static noinline int invalidate_extent_cache(struct btrfs_root *root,
6952 struct extent_buffer *leaf,
6953 struct btrfs_block_group_cache *group,
6954 struct btrfs_root *target_root)
6956 struct btrfs_key key;
6957 struct inode *inode = NULL;
6958 struct btrfs_file_extent_item *fi;
6959 struct extent_state *cached_state = NULL;
6961 u64 skip_objectid = 0;
6965 nritems = btrfs_header_nritems(leaf);
6966 for (i = 0; i < nritems; i++) {
6967 btrfs_item_key_to_cpu(leaf, &key, i);
6968 if (key.objectid == skip_objectid ||
6969 key.type != BTRFS_EXTENT_DATA_KEY)
6971 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6972 if (btrfs_file_extent_type(leaf, fi) ==
6973 BTRFS_FILE_EXTENT_INLINE)
6975 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6977 if (!inode || inode->i_ino != key.objectid) {
6979 inode = btrfs_ilookup(target_root->fs_info->sb,
6980 key.objectid, target_root, 1);
6983 skip_objectid = key.objectid;
6986 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6988 lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset,
6989 key.offset + num_bytes - 1, 0, &cached_state,
6991 btrfs_drop_extent_cache(inode, key.offset,
6992 key.offset + num_bytes - 1, 1);
6993 unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset,
6994 key.offset + num_bytes - 1, &cached_state,
7002 static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
7003 struct btrfs_root *root,
7004 struct extent_buffer *leaf,
7005 struct btrfs_block_group_cache *group,
7006 struct inode *reloc_inode)
7008 struct btrfs_key key;
7009 struct btrfs_key extent_key;
7010 struct btrfs_file_extent_item *fi;
7011 struct btrfs_leaf_ref *ref;
7012 struct disk_extent *new_extent;
7021 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
7022 BUG_ON(!new_extent);
7024 ref = btrfs_lookup_leaf_ref(root, leaf->start);
7028 nritems = btrfs_header_nritems(leaf);
7029 for (i = 0; i < nritems; i++) {
7030 btrfs_item_key_to_cpu(leaf, &key, i);
7031 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
7033 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7034 if (btrfs_file_extent_type(leaf, fi) ==
7035 BTRFS_FILE_EXTENT_INLINE)
7037 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
7038 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
7043 if (bytenr >= group->key.objectid + group->key.offset ||
7044 bytenr + num_bytes <= group->key.objectid)
7047 extent_key.objectid = bytenr;
7048 extent_key.offset = num_bytes;
7049 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
7051 ret = get_new_locations(reloc_inode, &extent_key,
7052 group->key.objectid, 1,
7053 &new_extent, &nr_extent);
7058 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
7059 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
7060 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
7061 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
7063 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7064 new_extent->disk_bytenr);
7065 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7066 new_extent->disk_num_bytes);
7067 btrfs_mark_buffer_dirty(leaf);
7069 ret = btrfs_inc_extent_ref(trans, root,
7070 new_extent->disk_bytenr,
7071 new_extent->disk_num_bytes,
7073 root->root_key.objectid,
7074 trans->transid, key.objectid);
7077 ret = btrfs_free_extent(trans, root,
7078 bytenr, num_bytes, leaf->start,
7079 btrfs_header_owner(leaf),
7080 btrfs_header_generation(leaf),
7086 BUG_ON(ext_index + 1 != ref->nritems);
7087 btrfs_free_leaf_ref(root, ref);
7091 int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
7092 struct btrfs_root *root)
7094 struct btrfs_root *reloc_root;
7097 if (root->reloc_root) {
7098 reloc_root = root->reloc_root;
7099 root->reloc_root = NULL;
7100 list_add(&reloc_root->dead_list,
7101 &root->fs_info->dead_reloc_roots);
7103 btrfs_set_root_bytenr(&reloc_root->root_item,
7104 reloc_root->node->start);
7105 btrfs_set_root_level(&root->root_item,
7106 btrfs_header_level(reloc_root->node));
7107 memset(&reloc_root->root_item.drop_progress, 0,
7108 sizeof(struct btrfs_disk_key));
7109 reloc_root->root_item.drop_level = 0;
7111 ret = btrfs_update_root(trans, root->fs_info->tree_root,
7112 &reloc_root->root_key,
7113 &reloc_root->root_item);
7119 int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
7121 struct btrfs_trans_handle *trans;
7122 struct btrfs_root *reloc_root;
7123 struct btrfs_root *prev_root = NULL;
7124 struct list_head dead_roots;
7128 INIT_LIST_HEAD(&dead_roots);
7129 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
7131 while (!list_empty(&dead_roots)) {
7132 reloc_root = list_entry(dead_roots.prev,
7133 struct btrfs_root, dead_list);
7134 list_del_init(&reloc_root->dead_list);
7136 BUG_ON(reloc_root->commit_root != NULL);
7138 trans = btrfs_join_transaction(root, 1);
7141 mutex_lock(&root->fs_info->drop_mutex);
7142 ret = btrfs_drop_snapshot(trans, reloc_root);
7145 mutex_unlock(&root->fs_info->drop_mutex);
7147 nr = trans->blocks_used;
7148 ret = btrfs_end_transaction(trans, root);
7150 btrfs_btree_balance_dirty(root, nr);
7153 free_extent_buffer(reloc_root->node);
7155 ret = btrfs_del_root(trans, root->fs_info->tree_root,
7156 &reloc_root->root_key);
7158 mutex_unlock(&root->fs_info->drop_mutex);
7160 nr = trans->blocks_used;
7161 ret = btrfs_end_transaction(trans, root);
7163 btrfs_btree_balance_dirty(root, nr);
7166 prev_root = reloc_root;
7169 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
7175 int btrfs_add_dead_reloc_root(struct btrfs_root *root)
7177 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
7181 int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
7183 struct btrfs_root *reloc_root;
7184 struct btrfs_trans_handle *trans;
7185 struct btrfs_key location;
7189 mutex_lock(&root->fs_info->tree_reloc_mutex);
7190 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
7192 found = !list_empty(&root->fs_info->dead_reloc_roots);
7193 mutex_unlock(&root->fs_info->tree_reloc_mutex);
7196 trans = btrfs_start_transaction(root, 1);
7198 ret = btrfs_commit_transaction(trans, root);
7202 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
7203 location.offset = (u64)-1;
7204 location.type = BTRFS_ROOT_ITEM_KEY;
7206 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
7207 BUG_ON(!reloc_root);
7208 btrfs_orphan_cleanup(reloc_root);
7212 static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
7213 struct btrfs_root *root)
7215 struct btrfs_root *reloc_root;
7216 struct extent_buffer *eb;
7217 struct btrfs_root_item *root_item;
7218 struct btrfs_key root_key;
7221 BUG_ON(!root->ref_cows);
7222 if (root->reloc_root)
7225 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
7228 ret = btrfs_copy_root(trans, root, root->commit_root,
7229 &eb, BTRFS_TREE_RELOC_OBJECTID);
7232 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
7233 root_key.offset = root->root_key.objectid;
7234 root_key.type = BTRFS_ROOT_ITEM_KEY;
7236 memcpy(root_item, &root->root_item, sizeof(root_item));
7237 btrfs_set_root_refs(root_item, 0);
7238 btrfs_set_root_bytenr(root_item, eb->start);
7239 btrfs_set_root_level(root_item, btrfs_header_level(eb));
7240 btrfs_set_root_generation(root_item, trans->transid);
7242 btrfs_tree_unlock(eb);
7243 free_extent_buffer(eb);
7245 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
7246 &root_key, root_item);
7250 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
7252 BUG_ON(!reloc_root);
7253 reloc_root->last_trans = trans->transid;
7254 reloc_root->commit_root = NULL;
7255 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
7257 root->reloc_root = reloc_root;
7262 * Core function of space balance.
7264 * The idea is using reloc trees to relocate tree blocks in reference
7265 * counted roots. There is one reloc tree for each subvol, and all
7266 * reloc trees share same root key objectid. Reloc trees are snapshots
7267 * of the latest committed roots of subvols (root->commit_root).
7269 * To relocate a tree block referenced by a subvol, there are two steps.
7270 * COW the block through subvol's reloc tree, then update block pointer
7271 * in the subvol to point to the new block. Since all reloc trees share
7272 * same root key objectid, doing special handing for tree blocks owned
7273 * by them is easy. Once a tree block has been COWed in one reloc tree,
7274 * we can use the resulting new block directly when the same block is
7275 * required to COW again through other reloc trees. By this way, relocated
7276 * tree blocks are shared between reloc trees, so they are also shared
7279 static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
7280 struct btrfs_root *root,
7281 struct btrfs_path *path,
7282 struct btrfs_key *first_key,
7283 struct btrfs_ref_path *ref_path,
7284 struct btrfs_block_group_cache *group,
7285 struct inode *reloc_inode)
7287 struct btrfs_root *reloc_root;
7288 struct extent_buffer *eb = NULL;
7289 struct btrfs_key *keys;
7293 int lowest_level = 0;
7296 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
7297 lowest_level = ref_path->owner_objectid;
7299 if (!root->ref_cows) {
7300 path->lowest_level = lowest_level;
7301 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
7303 path->lowest_level = 0;
7304 btrfs_release_path(root, path);
7308 mutex_lock(&root->fs_info->tree_reloc_mutex);
7309 ret = init_reloc_tree(trans, root);
7311 reloc_root = root->reloc_root;
7313 shared_level = ref_path->shared_level;
7314 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
7316 keys = ref_path->node_keys;
7317 nodes = ref_path->new_nodes;
7318 memset(&keys[shared_level + 1], 0,
7319 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
7320 memset(&nodes[shared_level + 1], 0,
7321 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
7323 if (nodes[lowest_level] == 0) {
7324 path->lowest_level = lowest_level;
7325 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7328 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
7329 eb = path->nodes[level];
7330 if (!eb || eb == reloc_root->node)
7332 nodes[level] = eb->start;
7334 btrfs_item_key_to_cpu(eb, &keys[level], 0);
7336 btrfs_node_key_to_cpu(eb, &keys[level], 0);
7339 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7340 eb = path->nodes[0];
7341 ret = replace_extents_in_leaf(trans, reloc_root, eb,
7342 group, reloc_inode);
7345 btrfs_release_path(reloc_root, path);
7347 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
7353 * replace tree blocks in the fs tree with tree blocks in
7356 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
7359 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7360 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7363 extent_buffer_get(path->nodes[0]);
7364 eb = path->nodes[0];
7365 btrfs_release_path(reloc_root, path);
7366 ret = invalidate_extent_cache(reloc_root, eb, group, root);
7368 free_extent_buffer(eb);
7371 mutex_unlock(&root->fs_info->tree_reloc_mutex);
7372 path->lowest_level = 0;
7376 static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
7377 struct btrfs_root *root,
7378 struct btrfs_path *path,
7379 struct btrfs_key *first_key,
7380 struct btrfs_ref_path *ref_path)
7384 ret = relocate_one_path(trans, root, path, first_key,
7385 ref_path, NULL, NULL);
7391 static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
7392 struct btrfs_root *extent_root,
7393 struct btrfs_path *path,
7394 struct btrfs_key *extent_key)
7398 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
7401 ret = btrfs_del_item(trans, extent_root, path);
7403 btrfs_release_path(extent_root, path);
7407 static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
7408 struct btrfs_ref_path *ref_path)
7410 struct btrfs_key root_key;
7412 root_key.objectid = ref_path->root_objectid;
7413 root_key.type = BTRFS_ROOT_ITEM_KEY;
7414 if (is_cowonly_root(ref_path->root_objectid))
7415 root_key.offset = 0;
7417 root_key.offset = (u64)-1;
7419 return btrfs_read_fs_root_no_name(fs_info, &root_key);
7422 static noinline int relocate_one_extent(struct btrfs_root *extent_root,
7423 struct btrfs_path *path,
7424 struct btrfs_key *extent_key,
7425 struct btrfs_block_group_cache *group,
7426 struct inode *reloc_inode, int pass)
7428 struct btrfs_trans_handle *trans;
7429 struct btrfs_root *found_root;
7430 struct btrfs_ref_path *ref_path = NULL;
7431 struct disk_extent *new_extents = NULL;
7436 struct btrfs_key first_key;
7440 trans = btrfs_start_transaction(extent_root, 1);
7443 if (extent_key->objectid == 0) {
7444 ret = del_extent_zero(trans, extent_root, path, extent_key);
7448 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
7454 for (loops = 0; ; loops++) {
7456 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
7457 extent_key->objectid);
7459 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
7466 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
7467 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
7470 found_root = read_ref_root(extent_root->fs_info, ref_path);
7471 BUG_ON(!found_root);
7473 * for reference counted tree, only process reference paths
7474 * rooted at the latest committed root.
7476 if (found_root->ref_cows &&
7477 ref_path->root_generation != found_root->root_key.offset)
7480 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7483 * copy data extents to new locations
7485 u64 group_start = group->key.objectid;
7486 ret = relocate_data_extent(reloc_inode,
7495 level = ref_path->owner_objectid;
7498 if (prev_block != ref_path->nodes[level]) {
7499 struct extent_buffer *eb;
7500 u64 block_start = ref_path->nodes[level];
7501 u64 block_size = btrfs_level_size(found_root, level);
7503 eb = read_tree_block(found_root, block_start,
7505 btrfs_tree_lock(eb);
7506 BUG_ON(level != btrfs_header_level(eb));
7509 btrfs_item_key_to_cpu(eb, &first_key, 0);
7511 btrfs_node_key_to_cpu(eb, &first_key, 0);
7513 btrfs_tree_unlock(eb);
7514 free_extent_buffer(eb);
7515 prev_block = block_start;
7518 mutex_lock(&extent_root->fs_info->trans_mutex);
7519 btrfs_record_root_in_trans(found_root);
7520 mutex_unlock(&extent_root->fs_info->trans_mutex);
7521 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7523 * try to update data extent references while
7524 * keeping metadata shared between snapshots.
7527 ret = relocate_one_path(trans, found_root,
7528 path, &first_key, ref_path,
7529 group, reloc_inode);
7535 * use fallback method to process the remaining
7539 u64 group_start = group->key.objectid;
7540 new_extents = kmalloc(sizeof(*new_extents),
7543 ret = get_new_locations(reloc_inode,
7551 ret = replace_one_extent(trans, found_root,
7553 &first_key, ref_path,
7554 new_extents, nr_extents);
7556 ret = relocate_tree_block(trans, found_root, path,
7557 &first_key, ref_path);
7564 btrfs_end_transaction(trans, extent_root);
7571 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7574 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
7575 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7577 num_devices = root->fs_info->fs_devices->rw_devices;
7578 if (num_devices == 1) {
7579 stripped |= BTRFS_BLOCK_GROUP_DUP;
7580 stripped = flags & ~stripped;
7582 /* turn raid0 into single device chunks */
7583 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7586 /* turn mirroring into duplication */
7587 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7588 BTRFS_BLOCK_GROUP_RAID10))
7589 return stripped | BTRFS_BLOCK_GROUP_DUP;
7592 /* they already had raid on here, just return */
7593 if (flags & stripped)
7596 stripped |= BTRFS_BLOCK_GROUP_DUP;
7597 stripped = flags & ~stripped;
7599 /* switch duplicated blocks with raid1 */
7600 if (flags & BTRFS_BLOCK_GROUP_DUP)
7601 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7603 /* turn single device chunks into raid0 */
7604 return stripped | BTRFS_BLOCK_GROUP_RAID0;
7609 static int set_block_group_ro(struct btrfs_block_group_cache *cache)
7611 struct btrfs_space_info *sinfo = cache->space_info;
7618 spin_lock(&sinfo->lock);
7619 spin_lock(&cache->lock);
7620 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7621 cache->bytes_super - btrfs_block_group_used(&cache->item);
7623 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
7624 sinfo->bytes_may_use + sinfo->bytes_readonly +
7625 cache->reserved_pinned + num_bytes < sinfo->total_bytes) {
7626 sinfo->bytes_readonly += num_bytes;
7627 sinfo->bytes_reserved += cache->reserved_pinned;
7628 cache->reserved_pinned = 0;
7632 spin_unlock(&cache->lock);
7633 spin_unlock(&sinfo->lock);
7637 int btrfs_set_block_group_ro(struct btrfs_root *root,
7638 struct btrfs_block_group_cache *cache)
7641 struct btrfs_trans_handle *trans;
7647 trans = btrfs_join_transaction(root, 1);
7648 BUG_ON(IS_ERR(trans));
7650 alloc_flags = update_block_group_flags(root, cache->flags);
7651 if (alloc_flags != cache->flags)
7652 do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1);
7654 ret = set_block_group_ro(cache);
7657 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
7658 ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1);
7661 ret = set_block_group_ro(cache);
7663 btrfs_end_transaction(trans, root);
7667 int btrfs_set_block_group_rw(struct btrfs_root *root,
7668 struct btrfs_block_group_cache *cache)
7670 struct btrfs_space_info *sinfo = cache->space_info;
7675 spin_lock(&sinfo->lock);
7676 spin_lock(&cache->lock);
7677 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7678 cache->bytes_super - btrfs_block_group_used(&cache->item);
7679 sinfo->bytes_readonly -= num_bytes;
7681 spin_unlock(&cache->lock);
7682 spin_unlock(&sinfo->lock);
7687 * checks to see if its even possible to relocate this block group.
7689 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7690 * ok to go ahead and try.
7692 int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
7694 struct btrfs_block_group_cache *block_group;
7695 struct btrfs_space_info *space_info;
7696 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7697 struct btrfs_device *device;
7701 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
7703 /* odd, couldn't find the block group, leave it alone */
7707 /* no bytes used, we're good */
7708 if (!btrfs_block_group_used(&block_group->item))
7711 space_info = block_group->space_info;
7712 spin_lock(&space_info->lock);
7714 full = space_info->full;
7717 * if this is the last block group we have in this space, we can't
7718 * relocate it unless we're able to allocate a new chunk below.
7720 * Otherwise, we need to make sure we have room in the space to handle
7721 * all of the extents from this block group. If we can, we're good
7723 if ((space_info->total_bytes != block_group->key.offset) &&
7724 (space_info->bytes_used + space_info->bytes_reserved +
7725 space_info->bytes_pinned + space_info->bytes_readonly +
7726 btrfs_block_group_used(&block_group->item) <
7727 space_info->total_bytes)) {
7728 spin_unlock(&space_info->lock);
7731 spin_unlock(&space_info->lock);
7734 * ok we don't have enough space, but maybe we have free space on our
7735 * devices to allocate new chunks for relocation, so loop through our
7736 * alloc devices and guess if we have enough space. However, if we
7737 * were marked as full, then we know there aren't enough chunks, and we
7744 mutex_lock(&root->fs_info->chunk_mutex);
7745 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
7746 u64 min_free = btrfs_block_group_used(&block_group->item);
7747 u64 dev_offset, max_avail;
7750 * check to make sure we can actually find a chunk with enough
7751 * space to fit our block group in.
7753 if (device->total_bytes > device->bytes_used + min_free) {
7754 ret = find_free_dev_extent(NULL, device, min_free,
7755 &dev_offset, &max_avail);
7761 mutex_unlock(&root->fs_info->chunk_mutex);
7763 btrfs_put_block_group(block_group);
7767 static int find_first_block_group(struct btrfs_root *root,
7768 struct btrfs_path *path, struct btrfs_key *key)
7771 struct btrfs_key found_key;
7772 struct extent_buffer *leaf;
7775 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7780 slot = path->slots[0];
7781 leaf = path->nodes[0];
7782 if (slot >= btrfs_header_nritems(leaf)) {
7783 ret = btrfs_next_leaf(root, path);
7790 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7792 if (found_key.objectid >= key->objectid &&
7793 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7803 int btrfs_free_block_groups(struct btrfs_fs_info *info)
7805 struct btrfs_block_group_cache *block_group;
7806 struct btrfs_space_info *space_info;
7807 struct btrfs_caching_control *caching_ctl;
7810 down_write(&info->extent_commit_sem);
7811 while (!list_empty(&info->caching_block_groups)) {
7812 caching_ctl = list_entry(info->caching_block_groups.next,
7813 struct btrfs_caching_control, list);
7814 list_del(&caching_ctl->list);
7815 put_caching_control(caching_ctl);
7817 up_write(&info->extent_commit_sem);
7819 spin_lock(&info->block_group_cache_lock);
7820 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7821 block_group = rb_entry(n, struct btrfs_block_group_cache,
7823 rb_erase(&block_group->cache_node,
7824 &info->block_group_cache_tree);
7825 spin_unlock(&info->block_group_cache_lock);
7827 down_write(&block_group->space_info->groups_sem);
7828 list_del(&block_group->list);
7829 up_write(&block_group->space_info->groups_sem);
7831 if (block_group->cached == BTRFS_CACHE_STARTED)
7832 wait_block_group_cache_done(block_group);
7834 btrfs_remove_free_space_cache(block_group);
7835 btrfs_put_block_group(block_group);
7837 spin_lock(&info->block_group_cache_lock);
7839 spin_unlock(&info->block_group_cache_lock);
7841 /* now that all the block groups are freed, go through and
7842 * free all the space_info structs. This is only called during
7843 * the final stages of unmount, and so we know nobody is
7844 * using them. We call synchronize_rcu() once before we start,
7845 * just to be on the safe side.
7849 while(!list_empty(&info->space_info)) {
7850 space_info = list_entry(info->space_info.next,
7851 struct btrfs_space_info,
7853 if (space_info->bytes_pinned > 0 ||
7854 space_info->bytes_reserved > 0) {
7856 dump_space_info(space_info, 0, 0);
7858 list_del(&space_info->list);
7864 static void __link_block_group(struct btrfs_space_info *space_info,
7865 struct btrfs_block_group_cache *cache)
7867 int index = get_block_group_index(cache);
7869 down_write(&space_info->groups_sem);
7870 list_add_tail(&cache->list, &space_info->block_groups[index]);
7871 up_write(&space_info->groups_sem);
7874 int btrfs_read_block_groups(struct btrfs_root *root)
7876 struct btrfs_path *path;
7878 struct btrfs_block_group_cache *cache;
7879 struct btrfs_fs_info *info = root->fs_info;
7880 struct btrfs_space_info *space_info;
7881 struct btrfs_key key;
7882 struct btrfs_key found_key;
7883 struct extent_buffer *leaf;
7885 root = info->extent_root;
7888 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
7889 path = btrfs_alloc_path();
7894 ret = find_first_block_group(root, path, &key);
7900 leaf = path->nodes[0];
7901 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7902 cache = kzalloc(sizeof(*cache), GFP_NOFS);
7908 atomic_set(&cache->count, 1);
7909 spin_lock_init(&cache->lock);
7910 spin_lock_init(&cache->tree_lock);
7911 cache->fs_info = info;
7912 INIT_LIST_HEAD(&cache->list);
7913 INIT_LIST_HEAD(&cache->cluster_list);
7916 * we only want to have 32k of ram per block group for keeping
7917 * track of free space, and if we pass 1/2 of that we want to
7918 * start converting things over to using bitmaps
7920 cache->extents_thresh = ((1024 * 32) / 2) /
7921 sizeof(struct btrfs_free_space);
7923 read_extent_buffer(leaf, &cache->item,
7924 btrfs_item_ptr_offset(leaf, path->slots[0]),
7925 sizeof(cache->item));
7926 memcpy(&cache->key, &found_key, sizeof(found_key));
7928 key.objectid = found_key.objectid + found_key.offset;
7929 btrfs_release_path(root, path);
7930 cache->flags = btrfs_block_group_flags(&cache->item);
7931 cache->sectorsize = root->sectorsize;
7934 * check for two cases, either we are full, and therefore
7935 * don't need to bother with the caching work since we won't
7936 * find any space, or we are empty, and we can just add all
7937 * the space in and be done with it. This saves us _alot_ of
7938 * time, particularly in the full case.
7940 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
7941 exclude_super_stripes(root, cache);
7942 cache->last_byte_to_unpin = (u64)-1;
7943 cache->cached = BTRFS_CACHE_FINISHED;
7944 free_excluded_extents(root, cache);
7945 } else if (btrfs_block_group_used(&cache->item) == 0) {
7946 exclude_super_stripes(root, cache);
7947 cache->last_byte_to_unpin = (u64)-1;
7948 cache->cached = BTRFS_CACHE_FINISHED;
7949 add_new_free_space(cache, root->fs_info,
7951 found_key.objectid +
7953 free_excluded_extents(root, cache);
7956 ret = update_space_info(info, cache->flags, found_key.offset,
7957 btrfs_block_group_used(&cache->item),
7960 cache->space_info = space_info;
7961 spin_lock(&cache->space_info->lock);
7962 cache->space_info->bytes_readonly += cache->bytes_super;
7963 spin_unlock(&cache->space_info->lock);
7965 __link_block_group(space_info, cache);
7967 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7970 set_avail_alloc_bits(root->fs_info, cache->flags);
7971 if (btrfs_chunk_readonly(root, cache->key.objectid))
7972 set_block_group_ro(cache);
7975 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
7976 if (!(get_alloc_profile(root, space_info->flags) &
7977 (BTRFS_BLOCK_GROUP_RAID10 |
7978 BTRFS_BLOCK_GROUP_RAID1 |
7979 BTRFS_BLOCK_GROUP_DUP)))
7982 * avoid allocating from un-mirrored block group if there are
7983 * mirrored block groups.
7985 list_for_each_entry(cache, &space_info->block_groups[3], list)
7986 set_block_group_ro(cache);
7987 list_for_each_entry(cache, &space_info->block_groups[4], list)
7988 set_block_group_ro(cache);
7991 init_global_block_rsv(info);
7994 btrfs_free_path(path);
7998 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7999 struct btrfs_root *root, u64 bytes_used,
8000 u64 type, u64 chunk_objectid, u64 chunk_offset,
8004 struct btrfs_root *extent_root;
8005 struct btrfs_block_group_cache *cache;
8007 extent_root = root->fs_info->extent_root;
8009 root->fs_info->last_trans_log_full_commit = trans->transid;
8011 cache = kzalloc(sizeof(*cache), GFP_NOFS);
8015 cache->key.objectid = chunk_offset;
8016 cache->key.offset = size;
8017 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
8018 cache->sectorsize = root->sectorsize;
8021 * we only want to have 32k of ram per block group for keeping track
8022 * of free space, and if we pass 1/2 of that we want to start
8023 * converting things over to using bitmaps
8025 cache->extents_thresh = ((1024 * 32) / 2) /
8026 sizeof(struct btrfs_free_space);
8027 atomic_set(&cache->count, 1);
8028 spin_lock_init(&cache->lock);
8029 spin_lock_init(&cache->tree_lock);
8030 INIT_LIST_HEAD(&cache->list);
8031 INIT_LIST_HEAD(&cache->cluster_list);
8033 btrfs_set_block_group_used(&cache->item, bytes_used);
8034 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
8035 cache->flags = type;
8036 btrfs_set_block_group_flags(&cache->item, type);
8038 cache->last_byte_to_unpin = (u64)-1;
8039 cache->cached = BTRFS_CACHE_FINISHED;
8040 exclude_super_stripes(root, cache);
8042 add_new_free_space(cache, root->fs_info, chunk_offset,
8043 chunk_offset + size);
8045 free_excluded_extents(root, cache);
8047 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8048 &cache->space_info);
8051 spin_lock(&cache->space_info->lock);
8052 cache->space_info->bytes_readonly += cache->bytes_super;
8053 spin_unlock(&cache->space_info->lock);
8055 __link_block_group(cache->space_info, cache);
8057 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8060 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
8061 sizeof(cache->item));
8064 set_avail_alloc_bits(extent_root->fs_info, type);
8069 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8070 struct btrfs_root *root, u64 group_start)
8072 struct btrfs_path *path;
8073 struct btrfs_block_group_cache *block_group;
8074 struct btrfs_free_cluster *cluster;
8075 struct btrfs_key key;
8078 root = root->fs_info->extent_root;
8080 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8081 BUG_ON(!block_group);
8082 BUG_ON(!block_group->ro);
8084 memcpy(&key, &block_group->key, sizeof(key));
8086 /* make sure this block group isn't part of an allocation cluster */
8087 cluster = &root->fs_info->data_alloc_cluster;
8088 spin_lock(&cluster->refill_lock);
8089 btrfs_return_cluster_to_free_space(block_group, cluster);
8090 spin_unlock(&cluster->refill_lock);
8093 * make sure this block group isn't part of a metadata
8094 * allocation cluster
8096 cluster = &root->fs_info->meta_alloc_cluster;
8097 spin_lock(&cluster->refill_lock);
8098 btrfs_return_cluster_to_free_space(block_group, cluster);
8099 spin_unlock(&cluster->refill_lock);
8101 path = btrfs_alloc_path();
8104 spin_lock(&root->fs_info->block_group_cache_lock);
8105 rb_erase(&block_group->cache_node,
8106 &root->fs_info->block_group_cache_tree);
8107 spin_unlock(&root->fs_info->block_group_cache_lock);
8109 down_write(&block_group->space_info->groups_sem);
8111 * we must use list_del_init so people can check to see if they
8112 * are still on the list after taking the semaphore
8114 list_del_init(&block_group->list);
8115 up_write(&block_group->space_info->groups_sem);
8117 if (block_group->cached == BTRFS_CACHE_STARTED)
8118 wait_block_group_cache_done(block_group);
8120 btrfs_remove_free_space_cache(block_group);
8122 spin_lock(&block_group->space_info->lock);
8123 block_group->space_info->total_bytes -= block_group->key.offset;
8124 block_group->space_info->bytes_readonly -= block_group->key.offset;
8125 spin_unlock(&block_group->space_info->lock);
8127 btrfs_clear_space_info_full(root->fs_info);
8129 btrfs_put_block_group(block_group);
8130 btrfs_put_block_group(block_group);
8132 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8138 ret = btrfs_del_item(trans, root, path);
8140 btrfs_free_path(path);