2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
18 #include <linux/sched.h>
19 #include <linux/pagemap.h>
20 #include <linux/writeback.h>
21 #include <linux/blkdev.h>
22 #include <linux/sort.h>
23 #include <linux/rcupdate.h>
24 #include <linux/kthread.h>
29 #include "print-tree.h"
30 #include "transaction.h"
33 #include "free-space-cache.h"
35 static int update_block_group(struct btrfs_trans_handle *trans,
36 struct btrfs_root *root,
37 u64 bytenr, u64 num_bytes, int alloc,
39 static int update_reserved_extents(struct btrfs_block_group_cache *cache,
40 u64 num_bytes, int reserve);
41 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
42 struct btrfs_root *root,
43 u64 bytenr, u64 num_bytes, u64 parent,
44 u64 root_objectid, u64 owner_objectid,
45 u64 owner_offset, int refs_to_drop,
46 struct btrfs_delayed_extent_op *extra_op);
47 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
48 struct extent_buffer *leaf,
49 struct btrfs_extent_item *ei);
50 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
51 struct btrfs_root *root,
52 u64 parent, u64 root_objectid,
53 u64 flags, u64 owner, u64 offset,
54 struct btrfs_key *ins, int ref_mod);
55 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
56 struct btrfs_root *root,
57 u64 parent, u64 root_objectid,
58 u64 flags, struct btrfs_disk_key *key,
59 int level, struct btrfs_key *ins);
60 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
61 struct btrfs_root *extent_root, u64 alloc_bytes,
62 u64 flags, int force);
63 static int pin_down_bytes(struct btrfs_trans_handle *trans,
64 struct btrfs_root *root,
65 struct btrfs_path *path,
66 u64 bytenr, u64 num_bytes,
67 int is_data, int reserved,
68 struct extent_buffer **must_clean);
69 static int find_next_key(struct btrfs_path *path, int level,
70 struct btrfs_key *key);
71 static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
72 int dump_block_groups);
75 block_group_cache_done(struct btrfs_block_group_cache *cache)
78 return cache->cached == BTRFS_CACHE_FINISHED;
81 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
83 return (cache->flags & bits) == bits;
87 * this adds the block group to the fs_info rb tree for the block group
90 static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
91 struct btrfs_block_group_cache *block_group)
94 struct rb_node *parent = NULL;
95 struct btrfs_block_group_cache *cache;
97 spin_lock(&info->block_group_cache_lock);
98 p = &info->block_group_cache_tree.rb_node;
102 cache = rb_entry(parent, struct btrfs_block_group_cache,
104 if (block_group->key.objectid < cache->key.objectid) {
106 } else if (block_group->key.objectid > cache->key.objectid) {
109 spin_unlock(&info->block_group_cache_lock);
114 rb_link_node(&block_group->cache_node, parent, p);
115 rb_insert_color(&block_group->cache_node,
116 &info->block_group_cache_tree);
117 spin_unlock(&info->block_group_cache_lock);
123 * This will return the block group at or after bytenr if contains is 0, else
124 * it will return the block group that contains the bytenr
126 static struct btrfs_block_group_cache *
127 block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
130 struct btrfs_block_group_cache *cache, *ret = NULL;
134 spin_lock(&info->block_group_cache_lock);
135 n = info->block_group_cache_tree.rb_node;
138 cache = rb_entry(n, struct btrfs_block_group_cache,
140 end = cache->key.objectid + cache->key.offset - 1;
141 start = cache->key.objectid;
143 if (bytenr < start) {
144 if (!contains && (!ret || start < ret->key.objectid))
147 } else if (bytenr > start) {
148 if (contains && bytenr <= end) {
159 atomic_inc(&ret->count);
160 spin_unlock(&info->block_group_cache_lock);
165 static int add_excluded_extent(struct btrfs_root *root,
166 u64 start, u64 num_bytes)
168 u64 end = start + num_bytes - 1;
169 set_extent_bits(&root->fs_info->freed_extents[0],
170 start, end, EXTENT_UPTODATE, GFP_NOFS);
171 set_extent_bits(&root->fs_info->freed_extents[1],
172 start, end, EXTENT_UPTODATE, GFP_NOFS);
176 static void free_excluded_extents(struct btrfs_root *root,
177 struct btrfs_block_group_cache *cache)
181 start = cache->key.objectid;
182 end = start + cache->key.offset - 1;
184 clear_extent_bits(&root->fs_info->freed_extents[0],
185 start, end, EXTENT_UPTODATE, GFP_NOFS);
186 clear_extent_bits(&root->fs_info->freed_extents[1],
187 start, end, EXTENT_UPTODATE, GFP_NOFS);
190 static int exclude_super_stripes(struct btrfs_root *root,
191 struct btrfs_block_group_cache *cache)
198 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
199 bytenr = btrfs_sb_offset(i);
200 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
201 cache->key.objectid, bytenr,
202 0, &logical, &nr, &stripe_len);
206 cache->bytes_super += stripe_len;
207 ret = add_excluded_extent(root, logical[nr],
217 static struct btrfs_caching_control *
218 get_caching_control(struct btrfs_block_group_cache *cache)
220 struct btrfs_caching_control *ctl;
222 spin_lock(&cache->lock);
223 if (cache->cached != BTRFS_CACHE_STARTED) {
224 spin_unlock(&cache->lock);
228 ctl = cache->caching_ctl;
229 atomic_inc(&ctl->count);
230 spin_unlock(&cache->lock);
234 static void put_caching_control(struct btrfs_caching_control *ctl)
236 if (atomic_dec_and_test(&ctl->count))
241 * this is only called by cache_block_group, since we could have freed extents
242 * we need to check the pinned_extents for any extents that can't be used yet
243 * since their free space will be released as soon as the transaction commits.
245 static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
246 struct btrfs_fs_info *info, u64 start, u64 end)
248 u64 extent_start, extent_end, size, total_added = 0;
251 while (start < end) {
252 ret = find_first_extent_bit(info->pinned_extents, start,
253 &extent_start, &extent_end,
254 EXTENT_DIRTY | EXTENT_UPTODATE);
258 if (extent_start == start) {
259 start = extent_end + 1;
260 } else if (extent_start > start && extent_start < end) {
261 size = extent_start - start;
263 ret = btrfs_add_free_space(block_group, start,
266 start = extent_end + 1;
275 ret = btrfs_add_free_space(block_group, start, size);
282 static int caching_kthread(void *data)
284 struct btrfs_block_group_cache *block_group = data;
285 struct btrfs_fs_info *fs_info = block_group->fs_info;
286 struct btrfs_caching_control *caching_ctl = block_group->caching_ctl;
287 struct btrfs_root *extent_root = fs_info->extent_root;
288 struct btrfs_path *path;
289 struct extent_buffer *leaf;
290 struct btrfs_key key;
296 path = btrfs_alloc_path();
300 exclude_super_stripes(extent_root, block_group);
301 spin_lock(&block_group->space_info->lock);
302 block_group->space_info->bytes_super += block_group->bytes_super;
303 spin_unlock(&block_group->space_info->lock);
305 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
308 * We don't want to deadlock with somebody trying to allocate a new
309 * extent for the extent root while also trying to search the extent
310 * root to add free space. So we skip locking and search the commit
311 * root, since its read-only
313 path->skip_locking = 1;
314 path->search_commit_root = 1;
319 key.type = BTRFS_EXTENT_ITEM_KEY;
321 mutex_lock(&caching_ctl->mutex);
322 /* need to make sure the commit_root doesn't disappear */
323 down_read(&fs_info->extent_commit_sem);
325 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
329 leaf = path->nodes[0];
330 nritems = btrfs_header_nritems(leaf);
334 if (fs_info->closing > 1) {
339 if (path->slots[0] < nritems) {
340 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
342 ret = find_next_key(path, 0, &key);
346 caching_ctl->progress = last;
347 btrfs_release_path(extent_root, path);
348 up_read(&fs_info->extent_commit_sem);
349 mutex_unlock(&caching_ctl->mutex);
350 if (btrfs_transaction_in_commit(fs_info))
357 if (key.objectid < block_group->key.objectid) {
362 if (key.objectid >= block_group->key.objectid +
363 block_group->key.offset)
366 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
367 total_found += add_new_free_space(block_group,
370 last = key.objectid + key.offset;
372 if (total_found > (1024 * 1024 * 2)) {
374 wake_up(&caching_ctl->wait);
381 total_found += add_new_free_space(block_group, fs_info, last,
382 block_group->key.objectid +
383 block_group->key.offset);
384 caching_ctl->progress = (u64)-1;
386 spin_lock(&block_group->lock);
387 block_group->caching_ctl = NULL;
388 block_group->cached = BTRFS_CACHE_FINISHED;
389 spin_unlock(&block_group->lock);
392 btrfs_free_path(path);
393 up_read(&fs_info->extent_commit_sem);
395 free_excluded_extents(extent_root, block_group);
397 mutex_unlock(&caching_ctl->mutex);
398 wake_up(&caching_ctl->wait);
400 put_caching_control(caching_ctl);
401 atomic_dec(&block_group->space_info->caching_threads);
405 static int cache_block_group(struct btrfs_block_group_cache *cache)
407 struct btrfs_fs_info *fs_info = cache->fs_info;
408 struct btrfs_caching_control *caching_ctl;
409 struct task_struct *tsk;
413 if (cache->cached != BTRFS_CACHE_NO)
416 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL);
417 BUG_ON(!caching_ctl);
419 INIT_LIST_HEAD(&caching_ctl->list);
420 mutex_init(&caching_ctl->mutex);
421 init_waitqueue_head(&caching_ctl->wait);
422 caching_ctl->block_group = cache;
423 caching_ctl->progress = cache->key.objectid;
424 /* one for caching kthread, one for caching block group list */
425 atomic_set(&caching_ctl->count, 2);
427 spin_lock(&cache->lock);
428 if (cache->cached != BTRFS_CACHE_NO) {
429 spin_unlock(&cache->lock);
433 cache->caching_ctl = caching_ctl;
434 cache->cached = BTRFS_CACHE_STARTED;
435 spin_unlock(&cache->lock);
437 down_write(&fs_info->extent_commit_sem);
438 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
439 up_write(&fs_info->extent_commit_sem);
441 atomic_inc(&cache->space_info->caching_threads);
443 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
444 cache->key.objectid);
447 printk(KERN_ERR "error running thread %d\n", ret);
455 * return the block group that starts at or after bytenr
457 static struct btrfs_block_group_cache *
458 btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
460 struct btrfs_block_group_cache *cache;
462 cache = block_group_cache_tree_search(info, bytenr, 0);
468 * return the block group that contains the given bytenr
470 struct btrfs_block_group_cache *btrfs_lookup_block_group(
471 struct btrfs_fs_info *info,
474 struct btrfs_block_group_cache *cache;
476 cache = block_group_cache_tree_search(info, bytenr, 1);
481 void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
483 if (atomic_dec_and_test(&cache->count))
487 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
490 struct list_head *head = &info->space_info;
491 struct btrfs_space_info *found;
494 list_for_each_entry_rcu(found, head, list) {
495 if (found->flags == flags) {
505 * after adding space to the filesystem, we need to clear the full flags
506 * on all the space infos.
508 void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
510 struct list_head *head = &info->space_info;
511 struct btrfs_space_info *found;
514 list_for_each_entry_rcu(found, head, list)
519 static u64 div_factor(u64 num, int factor)
528 u64 btrfs_find_block_group(struct btrfs_root *root,
529 u64 search_start, u64 search_hint, int owner)
531 struct btrfs_block_group_cache *cache;
533 u64 last = max(search_hint, search_start);
540 cache = btrfs_lookup_first_block_group(root->fs_info, last);
544 spin_lock(&cache->lock);
545 last = cache->key.objectid + cache->key.offset;
546 used = btrfs_block_group_used(&cache->item);
548 if ((full_search || !cache->ro) &&
549 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
550 if (used + cache->pinned + cache->reserved <
551 div_factor(cache->key.offset, factor)) {
552 group_start = cache->key.objectid;
553 spin_unlock(&cache->lock);
554 btrfs_put_block_group(cache);
558 spin_unlock(&cache->lock);
559 btrfs_put_block_group(cache);
567 if (!full_search && factor < 10) {
577 /* simple helper to search for an existing extent at a given offset */
578 int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
581 struct btrfs_key key;
582 struct btrfs_path *path;
584 path = btrfs_alloc_path();
586 key.objectid = start;
588 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
589 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
591 btrfs_free_path(path);
596 * Back reference rules. Back refs have three main goals:
598 * 1) differentiate between all holders of references to an extent so that
599 * when a reference is dropped we can make sure it was a valid reference
600 * before freeing the extent.
602 * 2) Provide enough information to quickly find the holders of an extent
603 * if we notice a given block is corrupted or bad.
605 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
606 * maintenance. This is actually the same as #2, but with a slightly
607 * different use case.
609 * There are two kinds of back refs. The implicit back refs is optimized
610 * for pointers in non-shared tree blocks. For a given pointer in a block,
611 * back refs of this kind provide information about the block's owner tree
612 * and the pointer's key. These information allow us to find the block by
613 * b-tree searching. The full back refs is for pointers in tree blocks not
614 * referenced by their owner trees. The location of tree block is recorded
615 * in the back refs. Actually the full back refs is generic, and can be
616 * used in all cases the implicit back refs is used. The major shortcoming
617 * of the full back refs is its overhead. Every time a tree block gets
618 * COWed, we have to update back refs entry for all pointers in it.
620 * For a newly allocated tree block, we use implicit back refs for
621 * pointers in it. This means most tree related operations only involve
622 * implicit back refs. For a tree block created in old transaction, the
623 * only way to drop a reference to it is COW it. So we can detect the
624 * event that tree block loses its owner tree's reference and do the
625 * back refs conversion.
627 * When a tree block is COW'd through a tree, there are four cases:
629 * The reference count of the block is one and the tree is the block's
630 * owner tree. Nothing to do in this case.
632 * The reference count of the block is one and the tree is not the
633 * block's owner tree. In this case, full back refs is used for pointers
634 * in the block. Remove these full back refs, add implicit back refs for
635 * every pointers in the new block.
637 * The reference count of the block is greater than one and the tree is
638 * the block's owner tree. In this case, implicit back refs is used for
639 * pointers in the block. Add full back refs for every pointers in the
640 * block, increase lower level extents' reference counts. The original
641 * implicit back refs are entailed to the new block.
643 * The reference count of the block is greater than one and the tree is
644 * not the block's owner tree. Add implicit back refs for every pointer in
645 * the new block, increase lower level extents' reference count.
647 * Back Reference Key composing:
649 * The key objectid corresponds to the first byte in the extent,
650 * The key type is used to differentiate between types of back refs.
651 * There are different meanings of the key offset for different types
654 * File extents can be referenced by:
656 * - multiple snapshots, subvolumes, or different generations in one subvol
657 * - different files inside a single subvolume
658 * - different offsets inside a file (bookend extents in file.c)
660 * The extent ref structure for the implicit back refs has fields for:
662 * - Objectid of the subvolume root
663 * - objectid of the file holding the reference
664 * - original offset in the file
665 * - how many bookend extents
667 * The key offset for the implicit back refs is hash of the first
670 * The extent ref structure for the full back refs has field for:
672 * - number of pointers in the tree leaf
674 * The key offset for the implicit back refs is the first byte of
677 * When a file extent is allocated, The implicit back refs is used.
678 * the fields are filled in:
680 * (root_key.objectid, inode objectid, offset in file, 1)
682 * When a file extent is removed file truncation, we find the
683 * corresponding implicit back refs and check the following fields:
685 * (btrfs_header_owner(leaf), inode objectid, offset in file)
687 * Btree extents can be referenced by:
689 * - Different subvolumes
691 * Both the implicit back refs and the full back refs for tree blocks
692 * only consist of key. The key offset for the implicit back refs is
693 * objectid of block's owner tree. The key offset for the full back refs
694 * is the first byte of parent block.
696 * When implicit back refs is used, information about the lowest key and
697 * level of the tree block are required. These information are stored in
698 * tree block info structure.
701 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
702 static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
703 struct btrfs_root *root,
704 struct btrfs_path *path,
705 u64 owner, u32 extra_size)
707 struct btrfs_extent_item *item;
708 struct btrfs_extent_item_v0 *ei0;
709 struct btrfs_extent_ref_v0 *ref0;
710 struct btrfs_tree_block_info *bi;
711 struct extent_buffer *leaf;
712 struct btrfs_key key;
713 struct btrfs_key found_key;
714 u32 new_size = sizeof(*item);
718 leaf = path->nodes[0];
719 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
721 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
722 ei0 = btrfs_item_ptr(leaf, path->slots[0],
723 struct btrfs_extent_item_v0);
724 refs = btrfs_extent_refs_v0(leaf, ei0);
726 if (owner == (u64)-1) {
728 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
729 ret = btrfs_next_leaf(root, path);
733 leaf = path->nodes[0];
735 btrfs_item_key_to_cpu(leaf, &found_key,
737 BUG_ON(key.objectid != found_key.objectid);
738 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
742 ref0 = btrfs_item_ptr(leaf, path->slots[0],
743 struct btrfs_extent_ref_v0);
744 owner = btrfs_ref_objectid_v0(leaf, ref0);
748 btrfs_release_path(root, path);
750 if (owner < BTRFS_FIRST_FREE_OBJECTID)
751 new_size += sizeof(*bi);
753 new_size -= sizeof(*ei0);
754 ret = btrfs_search_slot(trans, root, &key, path,
755 new_size + extra_size, 1);
760 ret = btrfs_extend_item(trans, root, path, new_size);
763 leaf = path->nodes[0];
764 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
765 btrfs_set_extent_refs(leaf, item, refs);
766 /* FIXME: get real generation */
767 btrfs_set_extent_generation(leaf, item, 0);
768 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
769 btrfs_set_extent_flags(leaf, item,
770 BTRFS_EXTENT_FLAG_TREE_BLOCK |
771 BTRFS_BLOCK_FLAG_FULL_BACKREF);
772 bi = (struct btrfs_tree_block_info *)(item + 1);
773 /* FIXME: get first key of the block */
774 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
775 btrfs_set_tree_block_level(leaf, bi, (int)owner);
777 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
779 btrfs_mark_buffer_dirty(leaf);
784 static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
786 u32 high_crc = ~(u32)0;
787 u32 low_crc = ~(u32)0;
790 lenum = cpu_to_le64(root_objectid);
791 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
792 lenum = cpu_to_le64(owner);
793 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
794 lenum = cpu_to_le64(offset);
795 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
797 return ((u64)high_crc << 31) ^ (u64)low_crc;
800 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
801 struct btrfs_extent_data_ref *ref)
803 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
804 btrfs_extent_data_ref_objectid(leaf, ref),
805 btrfs_extent_data_ref_offset(leaf, ref));
808 static int match_extent_data_ref(struct extent_buffer *leaf,
809 struct btrfs_extent_data_ref *ref,
810 u64 root_objectid, u64 owner, u64 offset)
812 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
813 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
814 btrfs_extent_data_ref_offset(leaf, ref) != offset)
819 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
820 struct btrfs_root *root,
821 struct btrfs_path *path,
822 u64 bytenr, u64 parent,
824 u64 owner, u64 offset)
826 struct btrfs_key key;
827 struct btrfs_extent_data_ref *ref;
828 struct extent_buffer *leaf;
834 key.objectid = bytenr;
836 key.type = BTRFS_SHARED_DATA_REF_KEY;
839 key.type = BTRFS_EXTENT_DATA_REF_KEY;
840 key.offset = hash_extent_data_ref(root_objectid,
845 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
854 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
855 key.type = BTRFS_EXTENT_REF_V0_KEY;
856 btrfs_release_path(root, path);
857 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
868 leaf = path->nodes[0];
869 nritems = btrfs_header_nritems(leaf);
871 if (path->slots[0] >= nritems) {
872 ret = btrfs_next_leaf(root, path);
878 leaf = path->nodes[0];
879 nritems = btrfs_header_nritems(leaf);
883 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
884 if (key.objectid != bytenr ||
885 key.type != BTRFS_EXTENT_DATA_REF_KEY)
888 ref = btrfs_item_ptr(leaf, path->slots[0],
889 struct btrfs_extent_data_ref);
891 if (match_extent_data_ref(leaf, ref, root_objectid,
894 btrfs_release_path(root, path);
906 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
907 struct btrfs_root *root,
908 struct btrfs_path *path,
909 u64 bytenr, u64 parent,
910 u64 root_objectid, u64 owner,
911 u64 offset, int refs_to_add)
913 struct btrfs_key key;
914 struct extent_buffer *leaf;
919 key.objectid = bytenr;
921 key.type = BTRFS_SHARED_DATA_REF_KEY;
923 size = sizeof(struct btrfs_shared_data_ref);
925 key.type = BTRFS_EXTENT_DATA_REF_KEY;
926 key.offset = hash_extent_data_ref(root_objectid,
928 size = sizeof(struct btrfs_extent_data_ref);
931 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
932 if (ret && ret != -EEXIST)
935 leaf = path->nodes[0];
937 struct btrfs_shared_data_ref *ref;
938 ref = btrfs_item_ptr(leaf, path->slots[0],
939 struct btrfs_shared_data_ref);
941 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
943 num_refs = btrfs_shared_data_ref_count(leaf, ref);
944 num_refs += refs_to_add;
945 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
948 struct btrfs_extent_data_ref *ref;
949 while (ret == -EEXIST) {
950 ref = btrfs_item_ptr(leaf, path->slots[0],
951 struct btrfs_extent_data_ref);
952 if (match_extent_data_ref(leaf, ref, root_objectid,
955 btrfs_release_path(root, path);
957 ret = btrfs_insert_empty_item(trans, root, path, &key,
959 if (ret && ret != -EEXIST)
962 leaf = path->nodes[0];
964 ref = btrfs_item_ptr(leaf, path->slots[0],
965 struct btrfs_extent_data_ref);
967 btrfs_set_extent_data_ref_root(leaf, ref,
969 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
970 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
971 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
973 num_refs = btrfs_extent_data_ref_count(leaf, ref);
974 num_refs += refs_to_add;
975 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
978 btrfs_mark_buffer_dirty(leaf);
981 btrfs_release_path(root, path);
985 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
986 struct btrfs_root *root,
987 struct btrfs_path *path,
990 struct btrfs_key key;
991 struct btrfs_extent_data_ref *ref1 = NULL;
992 struct btrfs_shared_data_ref *ref2 = NULL;
993 struct extent_buffer *leaf;
997 leaf = path->nodes[0];
998 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1000 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1001 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1002 struct btrfs_extent_data_ref);
1003 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1004 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1005 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1006 struct btrfs_shared_data_ref);
1007 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1008 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1009 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1010 struct btrfs_extent_ref_v0 *ref0;
1011 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1012 struct btrfs_extent_ref_v0);
1013 num_refs = btrfs_ref_count_v0(leaf, ref0);
1019 BUG_ON(num_refs < refs_to_drop);
1020 num_refs -= refs_to_drop;
1022 if (num_refs == 0) {
1023 ret = btrfs_del_item(trans, root, path);
1025 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1026 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1027 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1028 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1029 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1031 struct btrfs_extent_ref_v0 *ref0;
1032 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1033 struct btrfs_extent_ref_v0);
1034 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1037 btrfs_mark_buffer_dirty(leaf);
1042 static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1043 struct btrfs_path *path,
1044 struct btrfs_extent_inline_ref *iref)
1046 struct btrfs_key key;
1047 struct extent_buffer *leaf;
1048 struct btrfs_extent_data_ref *ref1;
1049 struct btrfs_shared_data_ref *ref2;
1052 leaf = path->nodes[0];
1053 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1055 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1056 BTRFS_EXTENT_DATA_REF_KEY) {
1057 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1058 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1060 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1061 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1063 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1064 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1065 struct btrfs_extent_data_ref);
1066 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1067 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1068 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1069 struct btrfs_shared_data_ref);
1070 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1071 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1072 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1073 struct btrfs_extent_ref_v0 *ref0;
1074 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1075 struct btrfs_extent_ref_v0);
1076 num_refs = btrfs_ref_count_v0(leaf, ref0);
1084 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1085 struct btrfs_root *root,
1086 struct btrfs_path *path,
1087 u64 bytenr, u64 parent,
1090 struct btrfs_key key;
1093 key.objectid = bytenr;
1095 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1096 key.offset = parent;
1098 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1099 key.offset = root_objectid;
1102 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1105 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1106 if (ret == -ENOENT && parent) {
1107 btrfs_release_path(root, path);
1108 key.type = BTRFS_EXTENT_REF_V0_KEY;
1109 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1117 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1118 struct btrfs_root *root,
1119 struct btrfs_path *path,
1120 u64 bytenr, u64 parent,
1123 struct btrfs_key key;
1126 key.objectid = bytenr;
1128 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1129 key.offset = parent;
1131 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1132 key.offset = root_objectid;
1135 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1136 btrfs_release_path(root, path);
1140 static inline int extent_ref_type(u64 parent, u64 owner)
1143 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1145 type = BTRFS_SHARED_BLOCK_REF_KEY;
1147 type = BTRFS_TREE_BLOCK_REF_KEY;
1150 type = BTRFS_SHARED_DATA_REF_KEY;
1152 type = BTRFS_EXTENT_DATA_REF_KEY;
1157 static int find_next_key(struct btrfs_path *path, int level,
1158 struct btrfs_key *key)
1161 for (; level < BTRFS_MAX_LEVEL; level++) {
1162 if (!path->nodes[level])
1164 if (path->slots[level] + 1 >=
1165 btrfs_header_nritems(path->nodes[level]))
1168 btrfs_item_key_to_cpu(path->nodes[level], key,
1169 path->slots[level] + 1);
1171 btrfs_node_key_to_cpu(path->nodes[level], key,
1172 path->slots[level] + 1);
1179 * look for inline back ref. if back ref is found, *ref_ret is set
1180 * to the address of inline back ref, and 0 is returned.
1182 * if back ref isn't found, *ref_ret is set to the address where it
1183 * should be inserted, and -ENOENT is returned.
1185 * if insert is true and there are too many inline back refs, the path
1186 * points to the extent item, and -EAGAIN is returned.
1188 * NOTE: inline back refs are ordered in the same way that back ref
1189 * items in the tree are ordered.
1191 static noinline_for_stack
1192 int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1193 struct btrfs_root *root,
1194 struct btrfs_path *path,
1195 struct btrfs_extent_inline_ref **ref_ret,
1196 u64 bytenr, u64 num_bytes,
1197 u64 parent, u64 root_objectid,
1198 u64 owner, u64 offset, int insert)
1200 struct btrfs_key key;
1201 struct extent_buffer *leaf;
1202 struct btrfs_extent_item *ei;
1203 struct btrfs_extent_inline_ref *iref;
1214 key.objectid = bytenr;
1215 key.type = BTRFS_EXTENT_ITEM_KEY;
1216 key.offset = num_bytes;
1218 want = extent_ref_type(parent, owner);
1220 extra_size = btrfs_extent_inline_ref_size(want);
1221 path->keep_locks = 1;
1224 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1231 leaf = path->nodes[0];
1232 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1233 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1234 if (item_size < sizeof(*ei)) {
1239 ret = convert_extent_item_v0(trans, root, path, owner,
1245 leaf = path->nodes[0];
1246 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1249 BUG_ON(item_size < sizeof(*ei));
1251 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1252 flags = btrfs_extent_flags(leaf, ei);
1254 ptr = (unsigned long)(ei + 1);
1255 end = (unsigned long)ei + item_size;
1257 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1258 ptr += sizeof(struct btrfs_tree_block_info);
1261 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1270 iref = (struct btrfs_extent_inline_ref *)ptr;
1271 type = btrfs_extent_inline_ref_type(leaf, iref);
1275 ptr += btrfs_extent_inline_ref_size(type);
1279 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1280 struct btrfs_extent_data_ref *dref;
1281 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1282 if (match_extent_data_ref(leaf, dref, root_objectid,
1287 if (hash_extent_data_ref_item(leaf, dref) <
1288 hash_extent_data_ref(root_objectid, owner, offset))
1292 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1294 if (parent == ref_offset) {
1298 if (ref_offset < parent)
1301 if (root_objectid == ref_offset) {
1305 if (ref_offset < root_objectid)
1309 ptr += btrfs_extent_inline_ref_size(type);
1311 if (err == -ENOENT && insert) {
1312 if (item_size + extra_size >=
1313 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1318 * To add new inline back ref, we have to make sure
1319 * there is no corresponding back ref item.
1320 * For simplicity, we just do not add new inline back
1321 * ref if there is any kind of item for this block
1323 if (find_next_key(path, 0, &key) == 0 &&
1324 key.objectid == bytenr &&
1325 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
1330 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1333 path->keep_locks = 0;
1334 btrfs_unlock_up_safe(path, 1);
1340 * helper to add new inline back ref
1342 static noinline_for_stack
1343 int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1344 struct btrfs_root *root,
1345 struct btrfs_path *path,
1346 struct btrfs_extent_inline_ref *iref,
1347 u64 parent, u64 root_objectid,
1348 u64 owner, u64 offset, int refs_to_add,
1349 struct btrfs_delayed_extent_op *extent_op)
1351 struct extent_buffer *leaf;
1352 struct btrfs_extent_item *ei;
1355 unsigned long item_offset;
1361 leaf = path->nodes[0];
1362 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1363 item_offset = (unsigned long)iref - (unsigned long)ei;
1365 type = extent_ref_type(parent, owner);
1366 size = btrfs_extent_inline_ref_size(type);
1368 ret = btrfs_extend_item(trans, root, path, size);
1371 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1372 refs = btrfs_extent_refs(leaf, ei);
1373 refs += refs_to_add;
1374 btrfs_set_extent_refs(leaf, ei, refs);
1376 __run_delayed_extent_op(extent_op, leaf, ei);
1378 ptr = (unsigned long)ei + item_offset;
1379 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1380 if (ptr < end - size)
1381 memmove_extent_buffer(leaf, ptr + size, ptr,
1384 iref = (struct btrfs_extent_inline_ref *)ptr;
1385 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1386 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1387 struct btrfs_extent_data_ref *dref;
1388 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1389 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1390 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1391 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1392 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1393 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1394 struct btrfs_shared_data_ref *sref;
1395 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1396 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1397 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1398 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1399 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1401 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1403 btrfs_mark_buffer_dirty(leaf);
1407 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1408 struct btrfs_root *root,
1409 struct btrfs_path *path,
1410 struct btrfs_extent_inline_ref **ref_ret,
1411 u64 bytenr, u64 num_bytes, u64 parent,
1412 u64 root_objectid, u64 owner, u64 offset)
1416 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1417 bytenr, num_bytes, parent,
1418 root_objectid, owner, offset, 0);
1422 btrfs_release_path(root, path);
1425 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1426 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1429 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1430 root_objectid, owner, offset);
1436 * helper to update/remove inline back ref
1438 static noinline_for_stack
1439 int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1440 struct btrfs_root *root,
1441 struct btrfs_path *path,
1442 struct btrfs_extent_inline_ref *iref,
1444 struct btrfs_delayed_extent_op *extent_op)
1446 struct extent_buffer *leaf;
1447 struct btrfs_extent_item *ei;
1448 struct btrfs_extent_data_ref *dref = NULL;
1449 struct btrfs_shared_data_ref *sref = NULL;
1458 leaf = path->nodes[0];
1459 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1460 refs = btrfs_extent_refs(leaf, ei);
1461 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1462 refs += refs_to_mod;
1463 btrfs_set_extent_refs(leaf, ei, refs);
1465 __run_delayed_extent_op(extent_op, leaf, ei);
1467 type = btrfs_extent_inline_ref_type(leaf, iref);
1469 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1470 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1471 refs = btrfs_extent_data_ref_count(leaf, dref);
1472 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1473 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1474 refs = btrfs_shared_data_ref_count(leaf, sref);
1477 BUG_ON(refs_to_mod != -1);
1480 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1481 refs += refs_to_mod;
1484 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1485 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1487 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1489 size = btrfs_extent_inline_ref_size(type);
1490 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1491 ptr = (unsigned long)iref;
1492 end = (unsigned long)ei + item_size;
1493 if (ptr + size < end)
1494 memmove_extent_buffer(leaf, ptr, ptr + size,
1497 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1500 btrfs_mark_buffer_dirty(leaf);
1504 static noinline_for_stack
1505 int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1506 struct btrfs_root *root,
1507 struct btrfs_path *path,
1508 u64 bytenr, u64 num_bytes, u64 parent,
1509 u64 root_objectid, u64 owner,
1510 u64 offset, int refs_to_add,
1511 struct btrfs_delayed_extent_op *extent_op)
1513 struct btrfs_extent_inline_ref *iref;
1516 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1517 bytenr, num_bytes, parent,
1518 root_objectid, owner, offset, 1);
1520 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1521 ret = update_inline_extent_backref(trans, root, path, iref,
1522 refs_to_add, extent_op);
1523 } else if (ret == -ENOENT) {
1524 ret = setup_inline_extent_backref(trans, root, path, iref,
1525 parent, root_objectid,
1526 owner, offset, refs_to_add,
1532 static int insert_extent_backref(struct btrfs_trans_handle *trans,
1533 struct btrfs_root *root,
1534 struct btrfs_path *path,
1535 u64 bytenr, u64 parent, u64 root_objectid,
1536 u64 owner, u64 offset, int refs_to_add)
1539 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1540 BUG_ON(refs_to_add != 1);
1541 ret = insert_tree_block_ref(trans, root, path, bytenr,
1542 parent, root_objectid);
1544 ret = insert_extent_data_ref(trans, root, path, bytenr,
1545 parent, root_objectid,
1546 owner, offset, refs_to_add);
1551 static int remove_extent_backref(struct btrfs_trans_handle *trans,
1552 struct btrfs_root *root,
1553 struct btrfs_path *path,
1554 struct btrfs_extent_inline_ref *iref,
1555 int refs_to_drop, int is_data)
1559 BUG_ON(!is_data && refs_to_drop != 1);
1561 ret = update_inline_extent_backref(trans, root, path, iref,
1562 -refs_to_drop, NULL);
1563 } else if (is_data) {
1564 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1566 ret = btrfs_del_item(trans, root, path);
1571 #ifdef BIO_RW_DISCARD
1572 static void btrfs_issue_discard(struct block_device *bdev,
1575 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
1579 static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1582 #ifdef BIO_RW_DISCARD
1584 u64 map_length = num_bytes;
1585 struct btrfs_multi_bio *multi = NULL;
1587 /* Tell the block device(s) that the sectors can be discarded */
1588 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1589 bytenr, &map_length, &multi, 0);
1591 struct btrfs_bio_stripe *stripe = multi->stripes;
1594 if (map_length > num_bytes)
1595 map_length = num_bytes;
1597 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1598 btrfs_issue_discard(stripe->dev->bdev,
1611 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1612 struct btrfs_root *root,
1613 u64 bytenr, u64 num_bytes, u64 parent,
1614 u64 root_objectid, u64 owner, u64 offset)
1617 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1618 root_objectid == BTRFS_TREE_LOG_OBJECTID);
1620 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1621 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1622 parent, root_objectid, (int)owner,
1623 BTRFS_ADD_DELAYED_REF, NULL);
1625 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1626 parent, root_objectid, owner, offset,
1627 BTRFS_ADD_DELAYED_REF, NULL);
1632 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1633 struct btrfs_root *root,
1634 u64 bytenr, u64 num_bytes,
1635 u64 parent, u64 root_objectid,
1636 u64 owner, u64 offset, int refs_to_add,
1637 struct btrfs_delayed_extent_op *extent_op)
1639 struct btrfs_path *path;
1640 struct extent_buffer *leaf;
1641 struct btrfs_extent_item *item;
1646 path = btrfs_alloc_path();
1651 path->leave_spinning = 1;
1652 /* this will setup the path even if it fails to insert the back ref */
1653 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1654 path, bytenr, num_bytes, parent,
1655 root_objectid, owner, offset,
1656 refs_to_add, extent_op);
1660 if (ret != -EAGAIN) {
1665 leaf = path->nodes[0];
1666 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1667 refs = btrfs_extent_refs(leaf, item);
1668 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1670 __run_delayed_extent_op(extent_op, leaf, item);
1672 btrfs_mark_buffer_dirty(leaf);
1673 btrfs_release_path(root->fs_info->extent_root, path);
1676 path->leave_spinning = 1;
1678 /* now insert the actual backref */
1679 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1680 path, bytenr, parent, root_objectid,
1681 owner, offset, refs_to_add);
1684 btrfs_free_path(path);
1688 static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1689 struct btrfs_root *root,
1690 struct btrfs_delayed_ref_node *node,
1691 struct btrfs_delayed_extent_op *extent_op,
1692 int insert_reserved)
1695 struct btrfs_delayed_data_ref *ref;
1696 struct btrfs_key ins;
1701 ins.objectid = node->bytenr;
1702 ins.offset = node->num_bytes;
1703 ins.type = BTRFS_EXTENT_ITEM_KEY;
1705 ref = btrfs_delayed_node_to_data_ref(node);
1706 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1707 parent = ref->parent;
1709 ref_root = ref->root;
1711 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1713 BUG_ON(extent_op->update_key);
1714 flags |= extent_op->flags_to_set;
1716 ret = alloc_reserved_file_extent(trans, root,
1717 parent, ref_root, flags,
1718 ref->objectid, ref->offset,
1719 &ins, node->ref_mod);
1720 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1721 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1722 node->num_bytes, parent,
1723 ref_root, ref->objectid,
1724 ref->offset, node->ref_mod,
1726 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1727 ret = __btrfs_free_extent(trans, root, node->bytenr,
1728 node->num_bytes, parent,
1729 ref_root, ref->objectid,
1730 ref->offset, node->ref_mod,
1738 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1739 struct extent_buffer *leaf,
1740 struct btrfs_extent_item *ei)
1742 u64 flags = btrfs_extent_flags(leaf, ei);
1743 if (extent_op->update_flags) {
1744 flags |= extent_op->flags_to_set;
1745 btrfs_set_extent_flags(leaf, ei, flags);
1748 if (extent_op->update_key) {
1749 struct btrfs_tree_block_info *bi;
1750 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1751 bi = (struct btrfs_tree_block_info *)(ei + 1);
1752 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1756 static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1757 struct btrfs_root *root,
1758 struct btrfs_delayed_ref_node *node,
1759 struct btrfs_delayed_extent_op *extent_op)
1761 struct btrfs_key key;
1762 struct btrfs_path *path;
1763 struct btrfs_extent_item *ei;
1764 struct extent_buffer *leaf;
1769 path = btrfs_alloc_path();
1773 key.objectid = node->bytenr;
1774 key.type = BTRFS_EXTENT_ITEM_KEY;
1775 key.offset = node->num_bytes;
1778 path->leave_spinning = 1;
1779 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1790 leaf = path->nodes[0];
1791 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1792 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1793 if (item_size < sizeof(*ei)) {
1794 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1800 leaf = path->nodes[0];
1801 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1804 BUG_ON(item_size < sizeof(*ei));
1805 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1806 __run_delayed_extent_op(extent_op, leaf, ei);
1808 btrfs_mark_buffer_dirty(leaf);
1810 btrfs_free_path(path);
1814 static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1815 struct btrfs_root *root,
1816 struct btrfs_delayed_ref_node *node,
1817 struct btrfs_delayed_extent_op *extent_op,
1818 int insert_reserved)
1821 struct btrfs_delayed_tree_ref *ref;
1822 struct btrfs_key ins;
1826 ins.objectid = node->bytenr;
1827 ins.offset = node->num_bytes;
1828 ins.type = BTRFS_EXTENT_ITEM_KEY;
1830 ref = btrfs_delayed_node_to_tree_ref(node);
1831 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1832 parent = ref->parent;
1834 ref_root = ref->root;
1836 BUG_ON(node->ref_mod != 1);
1837 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1838 BUG_ON(!extent_op || !extent_op->update_flags ||
1839 !extent_op->update_key);
1840 ret = alloc_reserved_tree_block(trans, root,
1842 extent_op->flags_to_set,
1845 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1846 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1847 node->num_bytes, parent, ref_root,
1848 ref->level, 0, 1, extent_op);
1849 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1850 ret = __btrfs_free_extent(trans, root, node->bytenr,
1851 node->num_bytes, parent, ref_root,
1852 ref->level, 0, 1, extent_op);
1860 /* helper function to actually process a single delayed ref entry */
1861 static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
1862 struct btrfs_root *root,
1863 struct btrfs_delayed_ref_node *node,
1864 struct btrfs_delayed_extent_op *extent_op,
1865 int insert_reserved)
1868 if (btrfs_delayed_ref_is_head(node)) {
1869 struct btrfs_delayed_ref_head *head;
1871 * we've hit the end of the chain and we were supposed
1872 * to insert this extent into the tree. But, it got
1873 * deleted before we ever needed to insert it, so all
1874 * we have to do is clean up the accounting
1877 head = btrfs_delayed_node_to_head(node);
1878 if (insert_reserved) {
1880 struct extent_buffer *must_clean = NULL;
1882 ret = pin_down_bytes(trans, root, NULL,
1883 node->bytenr, node->num_bytes,
1884 head->is_data, 1, &must_clean);
1889 clean_tree_block(NULL, root, must_clean);
1890 btrfs_tree_unlock(must_clean);
1891 free_extent_buffer(must_clean);
1893 if (head->is_data) {
1894 ret = btrfs_del_csums(trans, root,
1900 ret = btrfs_free_reserved_extent(root,
1906 mutex_unlock(&head->mutex);
1910 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1911 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1912 ret = run_delayed_tree_ref(trans, root, node, extent_op,
1914 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1915 node->type == BTRFS_SHARED_DATA_REF_KEY)
1916 ret = run_delayed_data_ref(trans, root, node, extent_op,
1923 static noinline struct btrfs_delayed_ref_node *
1924 select_delayed_ref(struct btrfs_delayed_ref_head *head)
1926 struct rb_node *node;
1927 struct btrfs_delayed_ref_node *ref;
1928 int action = BTRFS_ADD_DELAYED_REF;
1931 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
1932 * this prevents ref count from going down to zero when
1933 * there still are pending delayed ref.
1935 node = rb_prev(&head->node.rb_node);
1939 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1941 if (ref->bytenr != head->node.bytenr)
1943 if (ref->action == action)
1945 node = rb_prev(node);
1947 if (action == BTRFS_ADD_DELAYED_REF) {
1948 action = BTRFS_DROP_DELAYED_REF;
1954 static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
1955 struct btrfs_root *root,
1956 struct list_head *cluster)
1958 struct btrfs_delayed_ref_root *delayed_refs;
1959 struct btrfs_delayed_ref_node *ref;
1960 struct btrfs_delayed_ref_head *locked_ref = NULL;
1961 struct btrfs_delayed_extent_op *extent_op;
1964 int must_insert_reserved = 0;
1966 delayed_refs = &trans->transaction->delayed_refs;
1969 /* pick a new head ref from the cluster list */
1970 if (list_empty(cluster))
1973 locked_ref = list_entry(cluster->next,
1974 struct btrfs_delayed_ref_head, cluster);
1976 /* grab the lock that says we are going to process
1977 * all the refs for this head */
1978 ret = btrfs_delayed_ref_lock(trans, locked_ref);
1981 * we may have dropped the spin lock to get the head
1982 * mutex lock, and that might have given someone else
1983 * time to free the head. If that's true, it has been
1984 * removed from our list and we can move on.
1986 if (ret == -EAGAIN) {
1994 * record the must insert reserved flag before we
1995 * drop the spin lock.
1997 must_insert_reserved = locked_ref->must_insert_reserved;
1998 locked_ref->must_insert_reserved = 0;
2000 extent_op = locked_ref->extent_op;
2001 locked_ref->extent_op = NULL;
2004 * locked_ref is the head node, so we have to go one
2005 * node back for any delayed ref updates
2007 ref = select_delayed_ref(locked_ref);
2009 /* All delayed refs have been processed, Go ahead
2010 * and send the head node to run_one_delayed_ref,
2011 * so that any accounting fixes can happen
2013 ref = &locked_ref->node;
2015 if (extent_op && must_insert_reserved) {
2021 spin_unlock(&delayed_refs->lock);
2023 ret = run_delayed_extent_op(trans, root,
2029 spin_lock(&delayed_refs->lock);
2033 list_del_init(&locked_ref->cluster);
2038 rb_erase(&ref->rb_node, &delayed_refs->root);
2039 delayed_refs->num_entries--;
2041 spin_unlock(&delayed_refs->lock);
2043 ret = run_one_delayed_ref(trans, root, ref, extent_op,
2044 must_insert_reserved);
2047 btrfs_put_delayed_ref(ref);
2052 spin_lock(&delayed_refs->lock);
2058 * this starts processing the delayed reference count updates and
2059 * extent insertions we have queued up so far. count can be
2060 * 0, which means to process everything in the tree at the start
2061 * of the run (but not newly added entries), or it can be some target
2062 * number you'd like to process.
2064 int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2065 struct btrfs_root *root, unsigned long count)
2067 struct rb_node *node;
2068 struct btrfs_delayed_ref_root *delayed_refs;
2069 struct btrfs_delayed_ref_node *ref;
2070 struct list_head cluster;
2072 int run_all = count == (unsigned long)-1;
2075 if (root == root->fs_info->extent_root)
2076 root = root->fs_info->tree_root;
2078 delayed_refs = &trans->transaction->delayed_refs;
2079 INIT_LIST_HEAD(&cluster);
2081 spin_lock(&delayed_refs->lock);
2083 count = delayed_refs->num_entries * 2;
2087 if (!(run_all || run_most) &&
2088 delayed_refs->num_heads_ready < 64)
2092 * go find something we can process in the rbtree. We start at
2093 * the beginning of the tree, and then build a cluster
2094 * of refs to process starting at the first one we are able to
2097 ret = btrfs_find_ref_cluster(trans, &cluster,
2098 delayed_refs->run_delayed_start);
2102 ret = run_clustered_refs(trans, root, &cluster);
2105 count -= min_t(unsigned long, ret, count);
2112 node = rb_first(&delayed_refs->root);
2115 count = (unsigned long)-1;
2118 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2120 if (btrfs_delayed_ref_is_head(ref)) {
2121 struct btrfs_delayed_ref_head *head;
2123 head = btrfs_delayed_node_to_head(ref);
2124 atomic_inc(&ref->refs);
2126 spin_unlock(&delayed_refs->lock);
2127 mutex_lock(&head->mutex);
2128 mutex_unlock(&head->mutex);
2130 btrfs_put_delayed_ref(ref);
2134 node = rb_next(node);
2136 spin_unlock(&delayed_refs->lock);
2137 schedule_timeout(1);
2141 spin_unlock(&delayed_refs->lock);
2145 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2146 struct btrfs_root *root,
2147 u64 bytenr, u64 num_bytes, u64 flags,
2150 struct btrfs_delayed_extent_op *extent_op;
2153 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2157 extent_op->flags_to_set = flags;
2158 extent_op->update_flags = 1;
2159 extent_op->update_key = 0;
2160 extent_op->is_data = is_data ? 1 : 0;
2162 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2168 static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2169 struct btrfs_root *root,
2170 struct btrfs_path *path,
2171 u64 objectid, u64 offset, u64 bytenr)
2173 struct btrfs_delayed_ref_head *head;
2174 struct btrfs_delayed_ref_node *ref;
2175 struct btrfs_delayed_data_ref *data_ref;
2176 struct btrfs_delayed_ref_root *delayed_refs;
2177 struct rb_node *node;
2181 delayed_refs = &trans->transaction->delayed_refs;
2182 spin_lock(&delayed_refs->lock);
2183 head = btrfs_find_delayed_ref_head(trans, bytenr);
2187 if (!mutex_trylock(&head->mutex)) {
2188 atomic_inc(&head->node.refs);
2189 spin_unlock(&delayed_refs->lock);
2191 btrfs_release_path(root->fs_info->extent_root, path);
2193 mutex_lock(&head->mutex);
2194 mutex_unlock(&head->mutex);
2195 btrfs_put_delayed_ref(&head->node);
2199 node = rb_prev(&head->node.rb_node);
2203 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2205 if (ref->bytenr != bytenr)
2209 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2212 data_ref = btrfs_delayed_node_to_data_ref(ref);
2214 node = rb_prev(node);
2216 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2217 if (ref->bytenr == bytenr)
2221 if (data_ref->root != root->root_key.objectid ||
2222 data_ref->objectid != objectid || data_ref->offset != offset)
2227 mutex_unlock(&head->mutex);
2229 spin_unlock(&delayed_refs->lock);
2233 static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2234 struct btrfs_root *root,
2235 struct btrfs_path *path,
2236 u64 objectid, u64 offset, u64 bytenr)
2238 struct btrfs_root *extent_root = root->fs_info->extent_root;
2239 struct extent_buffer *leaf;
2240 struct btrfs_extent_data_ref *ref;
2241 struct btrfs_extent_inline_ref *iref;
2242 struct btrfs_extent_item *ei;
2243 struct btrfs_key key;
2247 key.objectid = bytenr;
2248 key.offset = (u64)-1;
2249 key.type = BTRFS_EXTENT_ITEM_KEY;
2251 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2257 if (path->slots[0] == 0)
2261 leaf = path->nodes[0];
2262 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2264 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
2268 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2269 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2270 if (item_size < sizeof(*ei)) {
2271 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2275 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2277 if (item_size != sizeof(*ei) +
2278 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2281 if (btrfs_extent_generation(leaf, ei) <=
2282 btrfs_root_last_snapshot(&root->root_item))
2285 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2286 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2287 BTRFS_EXTENT_DATA_REF_KEY)
2290 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2291 if (btrfs_extent_refs(leaf, ei) !=
2292 btrfs_extent_data_ref_count(leaf, ref) ||
2293 btrfs_extent_data_ref_root(leaf, ref) !=
2294 root->root_key.objectid ||
2295 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2296 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2304 int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2305 struct btrfs_root *root,
2306 u64 objectid, u64 offset, u64 bytenr)
2308 struct btrfs_path *path;
2312 path = btrfs_alloc_path();
2317 ret = check_committed_ref(trans, root, path, objectid,
2319 if (ret && ret != -ENOENT)
2322 ret2 = check_delayed_ref(trans, root, path, objectid,
2324 } while (ret2 == -EAGAIN);
2326 if (ret2 && ret2 != -ENOENT) {
2331 if (ret != -ENOENT || ret2 != -ENOENT)
2334 btrfs_free_path(path);
2339 int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2340 struct extent_buffer *buf, u32 nr_extents)
2342 struct btrfs_key key;
2343 struct btrfs_file_extent_item *fi;
2351 if (!root->ref_cows)
2354 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2356 root_gen = root->root_key.offset;
2359 root_gen = trans->transid - 1;
2362 level = btrfs_header_level(buf);
2363 nritems = btrfs_header_nritems(buf);
2366 struct btrfs_leaf_ref *ref;
2367 struct btrfs_extent_info *info;
2369 ref = btrfs_alloc_leaf_ref(root, nr_extents);
2375 ref->root_gen = root_gen;
2376 ref->bytenr = buf->start;
2377 ref->owner = btrfs_header_owner(buf);
2378 ref->generation = btrfs_header_generation(buf);
2379 ref->nritems = nr_extents;
2380 info = ref->extents;
2382 for (i = 0; nr_extents > 0 && i < nritems; i++) {
2384 btrfs_item_key_to_cpu(buf, &key, i);
2385 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2387 fi = btrfs_item_ptr(buf, i,
2388 struct btrfs_file_extent_item);
2389 if (btrfs_file_extent_type(buf, fi) ==
2390 BTRFS_FILE_EXTENT_INLINE)
2392 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2393 if (disk_bytenr == 0)
2396 info->bytenr = disk_bytenr;
2398 btrfs_file_extent_disk_num_bytes(buf, fi);
2399 info->objectid = key.objectid;
2400 info->offset = key.offset;
2404 ret = btrfs_add_leaf_ref(root, ref, shared);
2405 if (ret == -EEXIST && shared) {
2406 struct btrfs_leaf_ref *old;
2407 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2409 btrfs_remove_leaf_ref(root, old);
2410 btrfs_free_leaf_ref(root, old);
2411 ret = btrfs_add_leaf_ref(root, ref, shared);
2414 btrfs_free_leaf_ref(root, ref);
2420 /* when a block goes through cow, we update the reference counts of
2421 * everything that block points to. The internal pointers of the block
2422 * can be in just about any order, and it is likely to have clusters of
2423 * things that are close together and clusters of things that are not.
2425 * To help reduce the seeks that come with updating all of these reference
2426 * counts, sort them by byte number before actual updates are done.
2428 * struct refsort is used to match byte number to slot in the btree block.
2429 * we sort based on the byte number and then use the slot to actually
2432 * struct refsort is smaller than strcut btrfs_item and smaller than
2433 * struct btrfs_key_ptr. Since we're currently limited to the page size
2434 * for a btree block, there's no way for a kmalloc of refsorts for a
2435 * single node to be bigger than a page.
2443 * for passing into sort()
2445 static int refsort_cmp(const void *a_void, const void *b_void)
2447 const struct refsort *a = a_void;
2448 const struct refsort *b = b_void;
2450 if (a->bytenr < b->bytenr)
2452 if (a->bytenr > b->bytenr)
2458 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
2459 struct btrfs_root *root,
2460 struct extent_buffer *buf,
2461 int full_backref, int inc)
2468 struct btrfs_key key;
2469 struct btrfs_file_extent_item *fi;
2473 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
2474 u64, u64, u64, u64, u64, u64);
2476 ref_root = btrfs_header_owner(buf);
2477 nritems = btrfs_header_nritems(buf);
2478 level = btrfs_header_level(buf);
2480 if (!root->ref_cows && level == 0)
2484 process_func = btrfs_inc_extent_ref;
2486 process_func = btrfs_free_extent;
2489 parent = buf->start;
2493 for (i = 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 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2507 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2508 key.offset -= btrfs_file_extent_offset(buf, fi);
2509 ret = process_func(trans, root, bytenr, num_bytes,
2510 parent, ref_root, key.objectid,
2515 bytenr = btrfs_node_blockptr(buf, i);
2516 num_bytes = btrfs_level_size(root, level - 1);
2517 ret = process_func(trans, root, bytenr, num_bytes,
2518 parent, ref_root, level - 1, 0);
2529 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2530 struct extent_buffer *buf, int full_backref)
2532 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2535 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2536 struct extent_buffer *buf, int full_backref)
2538 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
2541 static int write_one_cache_group(struct btrfs_trans_handle *trans,
2542 struct btrfs_root *root,
2543 struct btrfs_path *path,
2544 struct btrfs_block_group_cache *cache)
2547 struct btrfs_root *extent_root = root->fs_info->extent_root;
2549 struct extent_buffer *leaf;
2551 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
2556 leaf = path->nodes[0];
2557 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2558 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2559 btrfs_mark_buffer_dirty(leaf);
2560 btrfs_release_path(extent_root, path);
2568 static struct btrfs_block_group_cache *
2569 next_block_group(struct btrfs_root *root,
2570 struct btrfs_block_group_cache *cache)
2572 struct rb_node *node;
2573 spin_lock(&root->fs_info->block_group_cache_lock);
2574 node = rb_next(&cache->cache_node);
2575 btrfs_put_block_group(cache);
2577 cache = rb_entry(node, struct btrfs_block_group_cache,
2579 atomic_inc(&cache->count);
2582 spin_unlock(&root->fs_info->block_group_cache_lock);
2586 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2587 struct btrfs_root *root)
2589 struct btrfs_block_group_cache *cache;
2591 struct btrfs_path *path;
2594 path = btrfs_alloc_path();
2600 err = btrfs_run_delayed_refs(trans, root,
2605 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2609 cache = next_block_group(root, cache);
2619 last = cache->key.objectid + cache->key.offset;
2621 err = write_one_cache_group(trans, root, path, cache);
2623 btrfs_put_block_group(cache);
2626 btrfs_free_path(path);
2630 int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2632 struct btrfs_block_group_cache *block_group;
2635 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2636 if (!block_group || block_group->ro)
2639 btrfs_put_block_group(block_group);
2643 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2644 u64 total_bytes, u64 bytes_used,
2645 struct btrfs_space_info **space_info)
2647 struct btrfs_space_info *found;
2649 found = __find_space_info(info, flags);
2651 spin_lock(&found->lock);
2652 found->total_bytes += total_bytes;
2653 found->bytes_used += bytes_used;
2655 spin_unlock(&found->lock);
2656 *space_info = found;
2659 found = kzalloc(sizeof(*found), GFP_NOFS);
2663 INIT_LIST_HEAD(&found->block_groups);
2664 init_rwsem(&found->groups_sem);
2665 spin_lock_init(&found->lock);
2666 found->flags = flags;
2667 found->total_bytes = total_bytes;
2668 found->bytes_used = bytes_used;
2669 found->bytes_pinned = 0;
2670 found->bytes_reserved = 0;
2671 found->bytes_readonly = 0;
2672 found->bytes_delalloc = 0;
2674 found->force_alloc = 0;
2675 *space_info = found;
2676 list_add_rcu(&found->list, &info->space_info);
2677 atomic_set(&found->caching_threads, 0);
2681 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2683 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
2684 BTRFS_BLOCK_GROUP_RAID1 |
2685 BTRFS_BLOCK_GROUP_RAID10 |
2686 BTRFS_BLOCK_GROUP_DUP);
2688 if (flags & BTRFS_BLOCK_GROUP_DATA)
2689 fs_info->avail_data_alloc_bits |= extra_flags;
2690 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2691 fs_info->avail_metadata_alloc_bits |= extra_flags;
2692 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2693 fs_info->avail_system_alloc_bits |= extra_flags;
2697 static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
2699 spin_lock(&cache->space_info->lock);
2700 spin_lock(&cache->lock);
2702 cache->space_info->bytes_readonly += cache->key.offset -
2703 btrfs_block_group_used(&cache->item);
2706 spin_unlock(&cache->lock);
2707 spin_unlock(&cache->space_info->lock);
2710 u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
2712 u64 num_devices = root->fs_info->fs_devices->rw_devices;
2714 if (num_devices == 1)
2715 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2716 if (num_devices < 4)
2717 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2719 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2720 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
2721 BTRFS_BLOCK_GROUP_RAID10))) {
2722 flags &= ~BTRFS_BLOCK_GROUP_DUP;
2725 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
2726 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
2727 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
2730 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2731 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2732 (flags & BTRFS_BLOCK_GROUP_RAID10) |
2733 (flags & BTRFS_BLOCK_GROUP_DUP)))
2734 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2738 static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
2740 struct btrfs_fs_info *info = root->fs_info;
2744 alloc_profile = info->avail_data_alloc_bits &
2745 info->data_alloc_profile;
2746 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2747 } else if (root == root->fs_info->chunk_root) {
2748 alloc_profile = info->avail_system_alloc_bits &
2749 info->system_alloc_profile;
2750 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2752 alloc_profile = info->avail_metadata_alloc_bits &
2753 info->metadata_alloc_profile;
2754 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2757 return btrfs_reduce_alloc_profile(root, data);
2760 void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
2764 alloc_target = btrfs_get_alloc_profile(root, 1);
2765 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
2769 static u64 calculate_bytes_needed(struct btrfs_root *root, int num_items)
2774 level = BTRFS_MAX_LEVEL - 2;
2776 * NOTE: these calculations are absolutely the worst possible case.
2777 * This assumes that _every_ item we insert will require a new leaf, and
2778 * that the tree has grown to its maximum level size.
2782 * for every item we insert we could insert both an extent item and a
2783 * extent ref item. Then for ever item we insert, we will need to cow
2784 * both the original leaf, plus the leaf to the left and right of it.
2786 * Unless we are talking about the extent root, then we just want the
2787 * number of items * 2, since we just need the extent item plus its ref.
2789 if (root == root->fs_info->extent_root)
2790 num_bytes = num_items * 2;
2792 num_bytes = (num_items + (2 * num_items)) * 3;
2795 * num_bytes is total number of leaves we could need times the leaf
2796 * size, and then for every leaf we could end up cow'ing 2 nodes per
2797 * level, down to the leaf level.
2799 num_bytes = (num_bytes * root->leafsize) +
2800 (num_bytes * (level * 2)) * root->nodesize;
2806 * Unreserve metadata space for delalloc. If we have less reserved credits than
2807 * we have extents, this function does nothing.
2809 int btrfs_unreserve_metadata_for_delalloc(struct btrfs_root *root,
2810 struct inode *inode, int num_items)
2812 struct btrfs_fs_info *info = root->fs_info;
2813 struct btrfs_space_info *meta_sinfo;
2818 /* get the space info for where the metadata will live */
2819 alloc_target = btrfs_get_alloc_profile(root, 0);
2820 meta_sinfo = __find_space_info(info, alloc_target);
2822 num_bytes = calculate_bytes_needed(root->fs_info->extent_root,
2825 spin_lock(&meta_sinfo->lock);
2826 spin_lock(&BTRFS_I(inode)->accounting_lock);
2827 if (BTRFS_I(inode)->reserved_extents <=
2828 BTRFS_I(inode)->outstanding_extents) {
2829 spin_unlock(&BTRFS_I(inode)->accounting_lock);
2830 spin_unlock(&meta_sinfo->lock);
2833 spin_unlock(&BTRFS_I(inode)->accounting_lock);
2835 BTRFS_I(inode)->reserved_extents--;
2836 BUG_ON(BTRFS_I(inode)->reserved_extents < 0);
2838 if (meta_sinfo->bytes_delalloc < num_bytes) {
2840 meta_sinfo->bytes_delalloc = 0;
2842 meta_sinfo->bytes_delalloc -= num_bytes;
2844 spin_unlock(&meta_sinfo->lock);
2851 static void check_force_delalloc(struct btrfs_space_info *meta_sinfo)
2855 thresh = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
2856 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
2857 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
2858 meta_sinfo->bytes_may_use;
2860 thresh = meta_sinfo->total_bytes - thresh;
2862 do_div(thresh, 100);
2863 if (thresh <= meta_sinfo->bytes_delalloc)
2864 meta_sinfo->force_delalloc = 1;
2866 meta_sinfo->force_delalloc = 0;
2869 static void flush_delalloc(struct btrfs_root *root,
2870 struct btrfs_space_info *info)
2874 spin_lock(&info->lock);
2876 if (!info->flushing) {
2878 init_waitqueue_head(&info->flush_wait);
2883 spin_unlock(&info->lock);
2886 wait_event(info->flush_wait,
2891 btrfs_start_delalloc_inodes(root);
2892 btrfs_wait_ordered_extents(root, 0);
2894 spin_lock(&info->lock);
2896 spin_unlock(&info->lock);
2897 wake_up(&info->flush_wait);
2900 static int maybe_allocate_chunk(struct btrfs_root *root,
2901 struct btrfs_space_info *info)
2903 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
2904 struct btrfs_trans_handle *trans;
2910 free_space = btrfs_super_total_bytes(disk_super);
2912 * we allow the metadata to grow to a max of either 5gb or 5% of the
2913 * space in the volume.
2915 min_metadata = min((u64)5 * 1024 * 1024 * 1024,
2916 div64_u64(free_space * 5, 100));
2917 if (info->total_bytes >= min_metadata) {
2918 spin_unlock(&info->lock);
2923 spin_unlock(&info->lock);
2927 if (!info->allocating_chunk) {
2928 info->force_alloc = 1;
2929 info->allocating_chunk = 1;
2930 init_waitqueue_head(&info->wait);
2935 spin_unlock(&info->lock);
2938 wait_event(info->wait,
2939 !info->allocating_chunk);
2943 trans = btrfs_start_transaction(root, 1);
2949 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2950 4096 + 2 * 1024 * 1024,
2952 btrfs_end_transaction(trans, root);
2956 spin_lock(&info->lock);
2957 info->allocating_chunk = 0;
2958 spin_unlock(&info->lock);
2959 wake_up(&info->wait);
2967 * Reserve metadata space for delalloc.
2969 int btrfs_reserve_metadata_for_delalloc(struct btrfs_root *root,
2970 struct inode *inode, int num_items)
2972 struct btrfs_fs_info *info = root->fs_info;
2973 struct btrfs_space_info *meta_sinfo;
2980 /* get the space info for where the metadata will live */
2981 alloc_target = btrfs_get_alloc_profile(root, 0);
2982 meta_sinfo = __find_space_info(info, alloc_target);
2984 num_bytes = calculate_bytes_needed(root->fs_info->extent_root,
2987 spin_lock(&meta_sinfo->lock);
2989 force_delalloc = meta_sinfo->force_delalloc;
2991 if (unlikely(!meta_sinfo->bytes_root))
2992 meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
2995 meta_sinfo->bytes_delalloc += num_bytes;
2997 used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
2998 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
2999 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
3000 meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
3002 if (used > meta_sinfo->total_bytes) {
3006 if (maybe_allocate_chunk(root, meta_sinfo))
3010 spin_unlock(&meta_sinfo->lock);
3014 filemap_flush(inode->i_mapping);
3016 } else if (flushed == 3) {
3017 flush_delalloc(root, meta_sinfo);
3020 spin_lock(&meta_sinfo->lock);
3021 meta_sinfo->bytes_delalloc -= num_bytes;
3022 spin_unlock(&meta_sinfo->lock);
3023 printk(KERN_ERR "enospc, has %d, reserved %d\n",
3024 BTRFS_I(inode)->outstanding_extents,
3025 BTRFS_I(inode)->reserved_extents);
3026 dump_space_info(meta_sinfo, 0, 0);
3030 BTRFS_I(inode)->reserved_extents++;
3031 check_force_delalloc(meta_sinfo);
3032 spin_unlock(&meta_sinfo->lock);
3034 if (!flushed && force_delalloc)
3035 filemap_flush(inode->i_mapping);
3041 * unreserve num_items number of items worth of metadata space. This needs to
3042 * be paired with btrfs_reserve_metadata_space.
3044 * NOTE: if you have the option, run this _AFTER_ you do a
3045 * btrfs_end_transaction, since btrfs_end_transaction will run delayed ref
3046 * oprations which will result in more used metadata, so we want to make sure we
3047 * can do that without issue.
3049 int btrfs_unreserve_metadata_space(struct btrfs_root *root, int num_items)
3051 struct btrfs_fs_info *info = root->fs_info;
3052 struct btrfs_space_info *meta_sinfo;
3057 /* get the space info for where the metadata will live */
3058 alloc_target = btrfs_get_alloc_profile(root, 0);
3059 meta_sinfo = __find_space_info(info, alloc_target);
3061 num_bytes = calculate_bytes_needed(root, num_items);
3063 spin_lock(&meta_sinfo->lock);
3064 if (meta_sinfo->bytes_may_use < num_bytes) {
3066 meta_sinfo->bytes_may_use = 0;
3068 meta_sinfo->bytes_may_use -= num_bytes;
3070 spin_unlock(&meta_sinfo->lock);
3078 * Reserve some metadata space for use. We'll calculate the worste case number
3079 * of bytes that would be needed to modify num_items number of items. If we
3080 * have space, fantastic, if not, you get -ENOSPC. Please call
3081 * btrfs_unreserve_metadata_space when you are done for the _SAME_ number of
3082 * items you reserved, since whatever metadata you needed should have already
3085 * This will commit the transaction to make more space if we don't have enough
3086 * metadata space. THe only time we don't do this is if we're reserving space
3087 * inside of a transaction, then we will just return -ENOSPC and it is the
3088 * callers responsibility to handle it properly.
3090 int btrfs_reserve_metadata_space(struct btrfs_root *root, int num_items)
3092 struct btrfs_fs_info *info = root->fs_info;
3093 struct btrfs_space_info *meta_sinfo;
3099 /* get the space info for where the metadata will live */
3100 alloc_target = btrfs_get_alloc_profile(root, 0);
3101 meta_sinfo = __find_space_info(info, alloc_target);
3103 num_bytes = calculate_bytes_needed(root, num_items);
3105 spin_lock(&meta_sinfo->lock);
3107 if (unlikely(!meta_sinfo->bytes_root))
3108 meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
3111 meta_sinfo->bytes_may_use += num_bytes;
3113 used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
3114 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
3115 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
3116 meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
3118 if (used > meta_sinfo->total_bytes) {
3121 if (maybe_allocate_chunk(root, meta_sinfo))
3125 spin_unlock(&meta_sinfo->lock);
3129 flush_delalloc(root, meta_sinfo);
3132 spin_lock(&meta_sinfo->lock);
3133 meta_sinfo->bytes_may_use -= num_bytes;
3134 spin_unlock(&meta_sinfo->lock);
3136 dump_space_info(meta_sinfo, 0, 0);
3140 check_force_delalloc(meta_sinfo);
3141 spin_unlock(&meta_sinfo->lock);
3147 * This will check the space that the inode allocates from to make sure we have
3148 * enough space for bytes.
3150 int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
3153 struct btrfs_space_info *data_sinfo;
3154 int ret = 0, committed = 0;
3156 /* make sure bytes are sectorsize aligned */
3157 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3159 data_sinfo = BTRFS_I(inode)->space_info;
3164 /* make sure we have enough space to handle the data first */
3165 spin_lock(&data_sinfo->lock);
3166 if (data_sinfo->total_bytes - data_sinfo->bytes_used -
3167 data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
3168 data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
3169 data_sinfo->bytes_may_use - data_sinfo->bytes_super < bytes) {
3170 struct btrfs_trans_handle *trans;
3173 * if we don't have enough free bytes in this space then we need
3174 * to alloc a new chunk.
3176 if (!data_sinfo->full) {
3179 data_sinfo->force_alloc = 1;
3180 spin_unlock(&data_sinfo->lock);
3182 alloc_target = btrfs_get_alloc_profile(root, 1);
3183 trans = btrfs_start_transaction(root, 1);
3187 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3188 bytes + 2 * 1024 * 1024,
3190 btrfs_end_transaction(trans, root);
3195 btrfs_set_inode_space_info(root, inode);
3196 data_sinfo = BTRFS_I(inode)->space_info;
3200 spin_unlock(&data_sinfo->lock);
3202 /* commit the current transaction and try again */
3203 if (!committed && !root->fs_info->open_ioctl_trans) {
3205 trans = btrfs_join_transaction(root, 1);
3208 ret = btrfs_commit_transaction(trans, root);
3214 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
3215 ", %llu bytes_used, %llu bytes_reserved, "
3216 "%llu bytes_pinned, %llu bytes_readonly, %llu may use "
3217 "%llu total\n", (unsigned long long)bytes,
3218 (unsigned long long)data_sinfo->bytes_delalloc,
3219 (unsigned long long)data_sinfo->bytes_used,
3220 (unsigned long long)data_sinfo->bytes_reserved,
3221 (unsigned long long)data_sinfo->bytes_pinned,
3222 (unsigned long long)data_sinfo->bytes_readonly,
3223 (unsigned long long)data_sinfo->bytes_may_use,
3224 (unsigned long long)data_sinfo->total_bytes);
3227 data_sinfo->bytes_may_use += bytes;
3228 BTRFS_I(inode)->reserved_bytes += bytes;
3229 spin_unlock(&data_sinfo->lock);
3235 * if there was an error for whatever reason after calling
3236 * btrfs_check_data_free_space, call this so we can cleanup the counters.
3238 void btrfs_free_reserved_data_space(struct btrfs_root *root,
3239 struct inode *inode, u64 bytes)
3241 struct btrfs_space_info *data_sinfo;
3243 /* make sure bytes are sectorsize aligned */
3244 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3246 data_sinfo = BTRFS_I(inode)->space_info;
3247 spin_lock(&data_sinfo->lock);
3248 data_sinfo->bytes_may_use -= bytes;
3249 BTRFS_I(inode)->reserved_bytes -= bytes;
3250 spin_unlock(&data_sinfo->lock);
3253 /* called when we are adding a delalloc extent to the inode's io_tree */
3254 void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
3257 struct btrfs_space_info *data_sinfo;
3259 /* get the space info for where this inode will be storing its data */
3260 data_sinfo = BTRFS_I(inode)->space_info;
3262 /* make sure we have enough space to handle the data first */
3263 spin_lock(&data_sinfo->lock);
3264 data_sinfo->bytes_delalloc += bytes;
3267 * we are adding a delalloc extent without calling
3268 * btrfs_check_data_free_space first. This happens on a weird
3269 * writepage condition, but shouldn't hurt our accounting
3271 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
3272 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
3273 BTRFS_I(inode)->reserved_bytes = 0;
3275 data_sinfo->bytes_may_use -= bytes;
3276 BTRFS_I(inode)->reserved_bytes -= bytes;
3279 spin_unlock(&data_sinfo->lock);
3282 /* called when we are clearing an delalloc extent from the inode's io_tree */
3283 void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
3286 struct btrfs_space_info *info;
3288 info = BTRFS_I(inode)->space_info;
3290 spin_lock(&info->lock);
3291 info->bytes_delalloc -= bytes;
3292 spin_unlock(&info->lock);
3295 static void force_metadata_allocation(struct btrfs_fs_info *info)
3297 struct list_head *head = &info->space_info;
3298 struct btrfs_space_info *found;
3301 list_for_each_entry_rcu(found, head, list) {
3302 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
3303 found->force_alloc = 1;
3308 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3309 struct btrfs_root *extent_root, u64 alloc_bytes,
3310 u64 flags, int force)
3312 struct btrfs_space_info *space_info;
3313 struct btrfs_fs_info *fs_info = extent_root->fs_info;
3317 mutex_lock(&fs_info->chunk_mutex);
3319 flags = btrfs_reduce_alloc_profile(extent_root, flags);
3321 space_info = __find_space_info(extent_root->fs_info, flags);
3323 ret = update_space_info(extent_root->fs_info, flags,
3327 BUG_ON(!space_info);
3329 spin_lock(&space_info->lock);
3330 if (space_info->force_alloc)
3332 if (space_info->full) {
3333 spin_unlock(&space_info->lock);
3337 thresh = space_info->total_bytes - space_info->bytes_readonly;
3338 thresh = div_factor(thresh, 8);
3340 (space_info->bytes_used + space_info->bytes_pinned +
3341 space_info->bytes_reserved + alloc_bytes) < thresh) {
3342 spin_unlock(&space_info->lock);
3345 spin_unlock(&space_info->lock);
3348 * if we're doing a data chunk, go ahead and make sure that
3349 * we keep a reasonable number of metadata chunks allocated in the
3352 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
3353 fs_info->data_chunk_allocations++;
3354 if (!(fs_info->data_chunk_allocations %
3355 fs_info->metadata_ratio))
3356 force_metadata_allocation(fs_info);
3359 ret = btrfs_alloc_chunk(trans, extent_root, flags);
3360 spin_lock(&space_info->lock);
3362 space_info->full = 1;
3363 space_info->force_alloc = 0;
3364 spin_unlock(&space_info->lock);
3366 mutex_unlock(&extent_root->fs_info->chunk_mutex);
3370 static int update_block_group(struct btrfs_trans_handle *trans,
3371 struct btrfs_root *root,
3372 u64 bytenr, u64 num_bytes, int alloc,
3375 struct btrfs_block_group_cache *cache;
3376 struct btrfs_fs_info *info = root->fs_info;
3377 u64 total = num_bytes;
3381 /* block accounting for super block */
3382 spin_lock(&info->delalloc_lock);
3383 old_val = btrfs_super_bytes_used(&info->super_copy);
3385 old_val += num_bytes;
3387 old_val -= num_bytes;
3388 btrfs_set_super_bytes_used(&info->super_copy, old_val);
3390 /* block accounting for root item */
3391 old_val = btrfs_root_used(&root->root_item);
3393 old_val += num_bytes;
3395 old_val -= num_bytes;
3396 btrfs_set_root_used(&root->root_item, old_val);
3397 spin_unlock(&info->delalloc_lock);
3400 cache = btrfs_lookup_block_group(info, bytenr);
3403 byte_in_group = bytenr - cache->key.objectid;
3404 WARN_ON(byte_in_group > cache->key.offset);
3406 spin_lock(&cache->space_info->lock);
3407 spin_lock(&cache->lock);
3409 old_val = btrfs_block_group_used(&cache->item);
3410 num_bytes = min(total, cache->key.offset - byte_in_group);
3412 old_val += num_bytes;
3413 btrfs_set_block_group_used(&cache->item, old_val);
3414 cache->reserved -= num_bytes;
3415 cache->space_info->bytes_used += num_bytes;
3416 cache->space_info->bytes_reserved -= num_bytes;
3418 cache->space_info->bytes_readonly -= num_bytes;
3419 spin_unlock(&cache->lock);
3420 spin_unlock(&cache->space_info->lock);
3422 old_val -= num_bytes;
3423 cache->space_info->bytes_used -= num_bytes;
3425 cache->space_info->bytes_readonly += num_bytes;
3426 btrfs_set_block_group_used(&cache->item, old_val);
3427 spin_unlock(&cache->lock);
3428 spin_unlock(&cache->space_info->lock);
3432 ret = btrfs_discard_extent(root, bytenr,
3436 ret = btrfs_add_free_space(cache, bytenr,
3441 btrfs_put_block_group(cache);
3443 bytenr += num_bytes;
3448 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
3450 struct btrfs_block_group_cache *cache;
3453 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
3457 bytenr = cache->key.objectid;
3458 btrfs_put_block_group(cache);
3464 * this function must be called within transaction
3466 int btrfs_pin_extent(struct btrfs_root *root,
3467 u64 bytenr, u64 num_bytes, int reserved)
3469 struct btrfs_fs_info *fs_info = root->fs_info;
3470 struct btrfs_block_group_cache *cache;
3472 cache = btrfs_lookup_block_group(fs_info, bytenr);
3475 spin_lock(&cache->space_info->lock);
3476 spin_lock(&cache->lock);
3477 cache->pinned += num_bytes;
3478 cache->space_info->bytes_pinned += num_bytes;
3480 cache->reserved -= num_bytes;
3481 cache->space_info->bytes_reserved -= num_bytes;
3483 spin_unlock(&cache->lock);
3484 spin_unlock(&cache->space_info->lock);
3486 btrfs_put_block_group(cache);
3488 set_extent_dirty(fs_info->pinned_extents,
3489 bytenr, bytenr + num_bytes - 1, GFP_NOFS);
3493 static int update_reserved_extents(struct btrfs_block_group_cache *cache,
3494 u64 num_bytes, int reserve)
3496 spin_lock(&cache->space_info->lock);
3497 spin_lock(&cache->lock);
3499 cache->reserved += num_bytes;
3500 cache->space_info->bytes_reserved += num_bytes;
3502 cache->reserved -= num_bytes;
3503 cache->space_info->bytes_reserved -= num_bytes;
3505 spin_unlock(&cache->lock);
3506 spin_unlock(&cache->space_info->lock);
3510 int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
3511 struct btrfs_root *root)
3513 struct btrfs_fs_info *fs_info = root->fs_info;
3514 struct btrfs_caching_control *next;
3515 struct btrfs_caching_control *caching_ctl;
3516 struct btrfs_block_group_cache *cache;
3518 down_write(&fs_info->extent_commit_sem);
3520 list_for_each_entry_safe(caching_ctl, next,
3521 &fs_info->caching_block_groups, list) {
3522 cache = caching_ctl->block_group;
3523 if (block_group_cache_done(cache)) {
3524 cache->last_byte_to_unpin = (u64)-1;
3525 list_del_init(&caching_ctl->list);
3526 put_caching_control(caching_ctl);
3528 cache->last_byte_to_unpin = caching_ctl->progress;
3532 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3533 fs_info->pinned_extents = &fs_info->freed_extents[1];
3535 fs_info->pinned_extents = &fs_info->freed_extents[0];
3537 up_write(&fs_info->extent_commit_sem);
3541 static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
3543 struct btrfs_fs_info *fs_info = root->fs_info;
3544 struct btrfs_block_group_cache *cache = NULL;
3547 while (start <= end) {
3549 start >= cache->key.objectid + cache->key.offset) {
3551 btrfs_put_block_group(cache);
3552 cache = btrfs_lookup_block_group(fs_info, start);
3556 len = cache->key.objectid + cache->key.offset - start;
3557 len = min(len, end + 1 - start);
3559 if (start < cache->last_byte_to_unpin) {
3560 len = min(len, cache->last_byte_to_unpin - start);
3561 btrfs_add_free_space(cache, start, len);
3564 spin_lock(&cache->space_info->lock);
3565 spin_lock(&cache->lock);
3566 cache->pinned -= len;
3567 cache->space_info->bytes_pinned -= len;
3568 spin_unlock(&cache->lock);
3569 spin_unlock(&cache->space_info->lock);
3575 btrfs_put_block_group(cache);
3579 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
3580 struct btrfs_root *root)
3582 struct btrfs_fs_info *fs_info = root->fs_info;
3583 struct extent_io_tree *unpin;
3588 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3589 unpin = &fs_info->freed_extents[1];
3591 unpin = &fs_info->freed_extents[0];
3594 ret = find_first_extent_bit(unpin, 0, &start, &end,
3599 ret = btrfs_discard_extent(root, start, end + 1 - start);
3601 clear_extent_dirty(unpin, start, end, GFP_NOFS);
3602 unpin_extent_range(root, start, end);
3609 static int pin_down_bytes(struct btrfs_trans_handle *trans,
3610 struct btrfs_root *root,
3611 struct btrfs_path *path,
3612 u64 bytenr, u64 num_bytes,
3613 int is_data, int reserved,
3614 struct extent_buffer **must_clean)
3617 struct extent_buffer *buf;
3622 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
3626 /* we can reuse a block if it hasn't been written
3627 * and it is from this transaction. We can't
3628 * reuse anything from the tree log root because
3629 * it has tiny sub-transactions.
3631 if (btrfs_buffer_uptodate(buf, 0) &&
3632 btrfs_try_tree_lock(buf)) {
3633 u64 header_owner = btrfs_header_owner(buf);
3634 u64 header_transid = btrfs_header_generation(buf);
3635 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
3636 header_transid == trans->transid &&
3637 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
3641 btrfs_tree_unlock(buf);
3643 free_extent_buffer(buf);
3646 btrfs_set_path_blocking(path);
3647 /* unlocks the pinned mutex */
3648 btrfs_pin_extent(root, bytenr, num_bytes, reserved);
3654 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
3655 struct btrfs_root *root,
3656 u64 bytenr, u64 num_bytes, u64 parent,
3657 u64 root_objectid, u64 owner_objectid,
3658 u64 owner_offset, int refs_to_drop,
3659 struct btrfs_delayed_extent_op *extent_op)
3661 struct btrfs_key key;
3662 struct btrfs_path *path;
3663 struct btrfs_fs_info *info = root->fs_info;
3664 struct btrfs_root *extent_root = info->extent_root;
3665 struct extent_buffer *leaf;
3666 struct btrfs_extent_item *ei;
3667 struct btrfs_extent_inline_ref *iref;
3670 int extent_slot = 0;
3671 int found_extent = 0;
3676 path = btrfs_alloc_path();
3681 path->leave_spinning = 1;
3683 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
3684 BUG_ON(!is_data && refs_to_drop != 1);
3686 ret = lookup_extent_backref(trans, extent_root, path, &iref,
3687 bytenr, num_bytes, parent,
3688 root_objectid, owner_objectid,
3691 extent_slot = path->slots[0];
3692 while (extent_slot >= 0) {
3693 btrfs_item_key_to_cpu(path->nodes[0], &key,
3695 if (key.objectid != bytenr)
3697 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3698 key.offset == num_bytes) {
3702 if (path->slots[0] - extent_slot > 5)
3706 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3707 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
3708 if (found_extent && item_size < sizeof(*ei))
3711 if (!found_extent) {
3713 ret = remove_extent_backref(trans, extent_root, path,
3717 btrfs_release_path(extent_root, path);
3718 path->leave_spinning = 1;
3720 key.objectid = bytenr;
3721 key.type = BTRFS_EXTENT_ITEM_KEY;
3722 key.offset = num_bytes;
3724 ret = btrfs_search_slot(trans, extent_root,
3727 printk(KERN_ERR "umm, got %d back from search"
3728 ", was looking for %llu\n", ret,
3729 (unsigned long long)bytenr);
3730 btrfs_print_leaf(extent_root, path->nodes[0]);
3733 extent_slot = path->slots[0];
3736 btrfs_print_leaf(extent_root, path->nodes[0]);
3738 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
3739 "parent %llu root %llu owner %llu offset %llu\n",
3740 (unsigned long long)bytenr,
3741 (unsigned long long)parent,
3742 (unsigned long long)root_objectid,
3743 (unsigned long long)owner_objectid,
3744 (unsigned long long)owner_offset);
3747 leaf = path->nodes[0];
3748 item_size = btrfs_item_size_nr(leaf, extent_slot);
3749 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3750 if (item_size < sizeof(*ei)) {
3751 BUG_ON(found_extent || extent_slot != path->slots[0]);
3752 ret = convert_extent_item_v0(trans, extent_root, path,
3756 btrfs_release_path(extent_root, path);
3757 path->leave_spinning = 1;
3759 key.objectid = bytenr;
3760 key.type = BTRFS_EXTENT_ITEM_KEY;
3761 key.offset = num_bytes;
3763 ret = btrfs_search_slot(trans, extent_root, &key, path,
3766 printk(KERN_ERR "umm, got %d back from search"
3767 ", was looking for %llu\n", ret,
3768 (unsigned long long)bytenr);
3769 btrfs_print_leaf(extent_root, path->nodes[0]);
3772 extent_slot = path->slots[0];
3773 leaf = path->nodes[0];
3774 item_size = btrfs_item_size_nr(leaf, extent_slot);
3777 BUG_ON(item_size < sizeof(*ei));
3778 ei = btrfs_item_ptr(leaf, extent_slot,
3779 struct btrfs_extent_item);
3780 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3781 struct btrfs_tree_block_info *bi;
3782 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
3783 bi = (struct btrfs_tree_block_info *)(ei + 1);
3784 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
3787 refs = btrfs_extent_refs(leaf, ei);
3788 BUG_ON(refs < refs_to_drop);
3789 refs -= refs_to_drop;
3793 __run_delayed_extent_op(extent_op, leaf, ei);
3795 * In the case of inline back ref, reference count will
3796 * be updated by remove_extent_backref
3799 BUG_ON(!found_extent);
3801 btrfs_set_extent_refs(leaf, ei, refs);
3802 btrfs_mark_buffer_dirty(leaf);
3805 ret = remove_extent_backref(trans, extent_root, path,
3812 struct extent_buffer *must_clean = NULL;
3815 BUG_ON(is_data && refs_to_drop !=
3816 extent_data_ref_count(root, path, iref));
3818 BUG_ON(path->slots[0] != extent_slot);
3820 BUG_ON(path->slots[0] != extent_slot + 1);
3821 path->slots[0] = extent_slot;
3826 ret = pin_down_bytes(trans, root, path, bytenr,
3827 num_bytes, is_data, 0, &must_clean);
3832 * it is going to be very rare for someone to be waiting
3833 * on the block we're freeing. del_items might need to
3834 * schedule, so rather than get fancy, just force it
3838 btrfs_set_lock_blocking(must_clean);
3840 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3843 btrfs_release_path(extent_root, path);
3846 clean_tree_block(NULL, root, must_clean);
3847 btrfs_tree_unlock(must_clean);
3848 free_extent_buffer(must_clean);
3852 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
3855 invalidate_mapping_pages(info->btree_inode->i_mapping,
3856 bytenr >> PAGE_CACHE_SHIFT,
3857 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
3860 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
3864 btrfs_free_path(path);
3869 * when we free an extent, it is possible (and likely) that we free the last
3870 * delayed ref for that extent as well. This searches the delayed ref tree for
3871 * a given extent, and if there are no other delayed refs to be processed, it
3872 * removes it from the tree.
3874 static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
3875 struct btrfs_root *root, u64 bytenr)
3877 struct btrfs_delayed_ref_head *head;
3878 struct btrfs_delayed_ref_root *delayed_refs;
3879 struct btrfs_delayed_ref_node *ref;
3880 struct rb_node *node;
3883 delayed_refs = &trans->transaction->delayed_refs;
3884 spin_lock(&delayed_refs->lock);
3885 head = btrfs_find_delayed_ref_head(trans, bytenr);
3889 node = rb_prev(&head->node.rb_node);
3893 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
3895 /* there are still entries for this ref, we can't drop it */
3896 if (ref->bytenr == bytenr)
3899 if (head->extent_op) {
3900 if (!head->must_insert_reserved)
3902 kfree(head->extent_op);
3903 head->extent_op = NULL;
3907 * waiting for the lock here would deadlock. If someone else has it
3908 * locked they are already in the process of dropping it anyway
3910 if (!mutex_trylock(&head->mutex))
3914 * at this point we have a head with no other entries. Go
3915 * ahead and process it.
3917 head->node.in_tree = 0;
3918 rb_erase(&head->node.rb_node, &delayed_refs->root);
3920 delayed_refs->num_entries--;
3923 * we don't take a ref on the node because we're removing it from the
3924 * tree, so we just steal the ref the tree was holding.
3926 delayed_refs->num_heads--;
3927 if (list_empty(&head->cluster))
3928 delayed_refs->num_heads_ready--;
3930 list_del_init(&head->cluster);
3931 spin_unlock(&delayed_refs->lock);
3933 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
3934 &head->node, head->extent_op,
3935 head->must_insert_reserved);
3937 btrfs_put_delayed_ref(&head->node);
3940 spin_unlock(&delayed_refs->lock);
3944 int btrfs_free_extent(struct btrfs_trans_handle *trans,
3945 struct btrfs_root *root,
3946 u64 bytenr, u64 num_bytes, u64 parent,
3947 u64 root_objectid, u64 owner, u64 offset)
3952 * tree log blocks never actually go into the extent allocation
3953 * tree, just update pinning info and exit early.
3955 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
3956 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
3957 /* unlocks the pinned mutex */
3958 btrfs_pin_extent(root, bytenr, num_bytes, 1);
3960 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
3961 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
3962 parent, root_objectid, (int)owner,
3963 BTRFS_DROP_DELAYED_REF, NULL);
3965 ret = check_ref_cleanup(trans, root, bytenr);
3968 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
3969 parent, root_objectid, owner,
3970 offset, BTRFS_DROP_DELAYED_REF, NULL);
3976 static u64 stripe_align(struct btrfs_root *root, u64 val)
3978 u64 mask = ((u64)root->stripesize - 1);
3979 u64 ret = (val + mask) & ~mask;
3984 * when we wait for progress in the block group caching, its because
3985 * our allocation attempt failed at least once. So, we must sleep
3986 * and let some progress happen before we try again.
3988 * This function will sleep at least once waiting for new free space to
3989 * show up, and then it will check the block group free space numbers
3990 * for our min num_bytes. Another option is to have it go ahead
3991 * and look in the rbtree for a free extent of a given size, but this
3995 wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
3998 struct btrfs_caching_control *caching_ctl;
4001 caching_ctl = get_caching_control(cache);
4005 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
4006 (cache->free_space >= num_bytes));
4008 put_caching_control(caching_ctl);
4013 wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4015 struct btrfs_caching_control *caching_ctl;
4018 caching_ctl = get_caching_control(cache);
4022 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4024 put_caching_control(caching_ctl);
4028 enum btrfs_loop_type {
4029 LOOP_CACHED_ONLY = 0,
4030 LOOP_CACHING_NOWAIT = 1,
4031 LOOP_CACHING_WAIT = 2,
4032 LOOP_ALLOC_CHUNK = 3,
4033 LOOP_NO_EMPTY_SIZE = 4,
4037 * walks the btree of allocated extents and find a hole of a given size.
4038 * The key ins is changed to record the hole:
4039 * ins->objectid == block start
4040 * ins->flags = BTRFS_EXTENT_ITEM_KEY
4041 * ins->offset == number of blocks
4042 * Any available blocks before search_start are skipped.
4044 static noinline int find_free_extent(struct btrfs_trans_handle *trans,
4045 struct btrfs_root *orig_root,
4046 u64 num_bytes, u64 empty_size,
4047 u64 search_start, u64 search_end,
4048 u64 hint_byte, struct btrfs_key *ins,
4049 u64 exclude_start, u64 exclude_nr,
4053 struct btrfs_root *root = orig_root->fs_info->extent_root;
4054 struct btrfs_free_cluster *last_ptr = NULL;
4055 struct btrfs_block_group_cache *block_group = NULL;
4056 int empty_cluster = 2 * 1024 * 1024;
4057 int allowed_chunk_alloc = 0;
4058 struct btrfs_space_info *space_info;
4059 int last_ptr_loop = 0;
4061 bool found_uncached_bg = false;
4062 bool failed_cluster_refill = false;
4063 bool failed_alloc = false;
4065 WARN_ON(num_bytes < root->sectorsize);
4066 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
4070 space_info = __find_space_info(root->fs_info, data);
4072 if (orig_root->ref_cows || empty_size)
4073 allowed_chunk_alloc = 1;
4075 if (data & BTRFS_BLOCK_GROUP_METADATA) {
4076 last_ptr = &root->fs_info->meta_alloc_cluster;
4077 if (!btrfs_test_opt(root, SSD))
4078 empty_cluster = 64 * 1024;
4081 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
4082 last_ptr = &root->fs_info->data_alloc_cluster;
4086 spin_lock(&last_ptr->lock);
4087 if (last_ptr->block_group)
4088 hint_byte = last_ptr->window_start;
4089 spin_unlock(&last_ptr->lock);
4092 search_start = max(search_start, first_logical_byte(root, 0));
4093 search_start = max(search_start, hint_byte);
4098 if (search_start == hint_byte) {
4099 block_group = btrfs_lookup_block_group(root->fs_info,
4102 * we don't want to use the block group if it doesn't match our
4103 * allocation bits, or if its not cached.
4105 if (block_group && block_group_bits(block_group, data) &&
4106 block_group_cache_done(block_group)) {
4107 down_read(&space_info->groups_sem);
4108 if (list_empty(&block_group->list) ||
4111 * someone is removing this block group,
4112 * we can't jump into the have_block_group
4113 * target because our list pointers are not
4116 btrfs_put_block_group(block_group);
4117 up_read(&space_info->groups_sem);
4119 goto have_block_group;
4120 } else if (block_group) {
4121 btrfs_put_block_group(block_group);
4126 down_read(&space_info->groups_sem);
4127 list_for_each_entry(block_group, &space_info->block_groups, list) {
4131 atomic_inc(&block_group->count);
4132 search_start = block_group->key.objectid;
4135 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
4137 * we want to start caching kthreads, but not too many
4138 * right off the bat so we don't overwhelm the system,
4139 * so only start them if there are less than 2 and we're
4140 * in the initial allocation phase.
4142 if (loop > LOOP_CACHING_NOWAIT ||
4143 atomic_read(&space_info->caching_threads) < 2) {
4144 ret = cache_block_group(block_group);
4149 cached = block_group_cache_done(block_group);
4150 if (unlikely(!cached)) {
4151 found_uncached_bg = true;
4153 /* if we only want cached bgs, loop */
4154 if (loop == LOOP_CACHED_ONLY)
4158 if (unlikely(block_group->ro))
4162 * Ok we want to try and use the cluster allocator, so lets look
4163 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
4164 * have tried the cluster allocator plenty of times at this
4165 * point and not have found anything, so we are likely way too
4166 * fragmented for the clustering stuff to find anything, so lets
4167 * just skip it and let the allocator find whatever block it can
4170 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
4172 * the refill lock keeps out other
4173 * people trying to start a new cluster
4175 spin_lock(&last_ptr->refill_lock);
4176 if (last_ptr->block_group &&
4177 (last_ptr->block_group->ro ||
4178 !block_group_bits(last_ptr->block_group, data))) {
4180 goto refill_cluster;
4183 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
4184 num_bytes, search_start);
4186 /* we have a block, we're done */
4187 spin_unlock(&last_ptr->refill_lock);
4191 spin_lock(&last_ptr->lock);
4193 * whoops, this cluster doesn't actually point to
4194 * this block group. Get a ref on the block
4195 * group is does point to and try again
4197 if (!last_ptr_loop && last_ptr->block_group &&
4198 last_ptr->block_group != block_group) {
4200 btrfs_put_block_group(block_group);
4201 block_group = last_ptr->block_group;
4202 atomic_inc(&block_group->count);
4203 spin_unlock(&last_ptr->lock);
4204 spin_unlock(&last_ptr->refill_lock);
4207 search_start = block_group->key.objectid;
4209 * we know this block group is properly
4210 * in the list because
4211 * btrfs_remove_block_group, drops the
4212 * cluster before it removes the block
4213 * group from the list
4215 goto have_block_group;
4217 spin_unlock(&last_ptr->lock);
4220 * this cluster didn't work out, free it and
4223 btrfs_return_cluster_to_free_space(NULL, last_ptr);
4227 /* allocate a cluster in this block group */
4228 ret = btrfs_find_space_cluster(trans, root,
4229 block_group, last_ptr,
4231 empty_cluster + empty_size);
4234 * now pull our allocation out of this
4237 offset = btrfs_alloc_from_cluster(block_group,
4238 last_ptr, num_bytes,
4241 /* we found one, proceed */
4242 spin_unlock(&last_ptr->refill_lock);
4245 } else if (!cached && loop > LOOP_CACHING_NOWAIT
4246 && !failed_cluster_refill) {
4247 spin_unlock(&last_ptr->refill_lock);
4249 failed_cluster_refill = true;
4250 wait_block_group_cache_progress(block_group,
4251 num_bytes + empty_cluster + empty_size);
4252 goto have_block_group;
4256 * at this point we either didn't find a cluster
4257 * or we weren't able to allocate a block from our
4258 * cluster. Free the cluster we've been trying
4259 * to use, and go to the next block group
4261 btrfs_return_cluster_to_free_space(NULL, last_ptr);
4262 spin_unlock(&last_ptr->refill_lock);
4266 offset = btrfs_find_space_for_alloc(block_group, search_start,
4267 num_bytes, empty_size);
4269 * If we didn't find a chunk, and we haven't failed on this
4270 * block group before, and this block group is in the middle of
4271 * caching and we are ok with waiting, then go ahead and wait
4272 * for progress to be made, and set failed_alloc to true.
4274 * If failed_alloc is true then we've already waited on this
4275 * block group once and should move on to the next block group.
4277 if (!offset && !failed_alloc && !cached &&
4278 loop > LOOP_CACHING_NOWAIT) {
4279 wait_block_group_cache_progress(block_group,
4280 num_bytes + empty_size);
4281 failed_alloc = true;
4282 goto have_block_group;
4283 } else if (!offset) {
4287 search_start = stripe_align(root, offset);
4288 /* move on to the next group */
4289 if (search_start + num_bytes >= search_end) {
4290 btrfs_add_free_space(block_group, offset, num_bytes);
4294 /* move on to the next group */
4295 if (search_start + num_bytes >
4296 block_group->key.objectid + block_group->key.offset) {
4297 btrfs_add_free_space(block_group, offset, num_bytes);
4301 if (exclude_nr > 0 &&
4302 (search_start + num_bytes > exclude_start &&
4303 search_start < exclude_start + exclude_nr)) {
4304 search_start = exclude_start + exclude_nr;
4306 btrfs_add_free_space(block_group, offset, num_bytes);
4308 * if search_start is still in this block group
4309 * then we just re-search this block group
4311 if (search_start >= block_group->key.objectid &&
4312 search_start < (block_group->key.objectid +
4313 block_group->key.offset))
4314 goto have_block_group;
4318 ins->objectid = search_start;
4319 ins->offset = num_bytes;
4321 if (offset < search_start)
4322 btrfs_add_free_space(block_group, offset,
4323 search_start - offset);
4324 BUG_ON(offset > search_start);
4326 update_reserved_extents(block_group, num_bytes, 1);
4328 /* we are all good, lets return */
4331 failed_cluster_refill = false;
4332 failed_alloc = false;
4333 btrfs_put_block_group(block_group);
4335 up_read(&space_info->groups_sem);
4337 /* LOOP_CACHED_ONLY, only search fully cached block groups
4338 * LOOP_CACHING_NOWAIT, search partially cached block groups, but
4339 * dont wait foR them to finish caching
4340 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
4341 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
4342 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
4345 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
4346 (found_uncached_bg || empty_size || empty_cluster ||
4347 allowed_chunk_alloc)) {
4348 if (found_uncached_bg) {
4349 found_uncached_bg = false;
4350 if (loop < LOOP_CACHING_WAIT) {
4356 if (loop == LOOP_ALLOC_CHUNK) {
4361 if (allowed_chunk_alloc) {
4362 ret = do_chunk_alloc(trans, root, num_bytes +
4363 2 * 1024 * 1024, data, 1);
4364 allowed_chunk_alloc = 0;
4366 space_info->force_alloc = 1;
4369 if (loop < LOOP_NO_EMPTY_SIZE) {
4374 } else if (!ins->objectid) {
4378 /* we found what we needed */
4379 if (ins->objectid) {
4380 if (!(data & BTRFS_BLOCK_GROUP_DATA))
4381 trans->block_group = block_group->key.objectid;
4383 btrfs_put_block_group(block_group);
4390 static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
4391 int dump_block_groups)
4393 struct btrfs_block_group_cache *cache;
4395 spin_lock(&info->lock);
4396 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
4397 (unsigned long long)(info->total_bytes - info->bytes_used -
4398 info->bytes_pinned - info->bytes_reserved -
4400 (info->full) ? "" : "not ");
4401 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
4402 " may_use=%llu, used=%llu, root=%llu, super=%llu, reserved=%llu"
4404 (unsigned long long)info->total_bytes,
4405 (unsigned long long)info->bytes_pinned,
4406 (unsigned long long)info->bytes_delalloc,
4407 (unsigned long long)info->bytes_may_use,
4408 (unsigned long long)info->bytes_used,
4409 (unsigned long long)info->bytes_root,
4410 (unsigned long long)info->bytes_super,
4411 (unsigned long long)info->bytes_reserved);
4412 spin_unlock(&info->lock);
4414 if (!dump_block_groups)
4417 down_read(&info->groups_sem);
4418 list_for_each_entry(cache, &info->block_groups, list) {
4419 spin_lock(&cache->lock);
4420 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
4421 "%llu pinned %llu reserved\n",
4422 (unsigned long long)cache->key.objectid,
4423 (unsigned long long)cache->key.offset,
4424 (unsigned long long)btrfs_block_group_used(&cache->item),
4425 (unsigned long long)cache->pinned,
4426 (unsigned long long)cache->reserved);
4427 btrfs_dump_free_space(cache, bytes);
4428 spin_unlock(&cache->lock);
4430 up_read(&info->groups_sem);
4433 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
4434 struct btrfs_root *root,
4435 u64 num_bytes, u64 min_alloc_size,
4436 u64 empty_size, u64 hint_byte,
4437 u64 search_end, struct btrfs_key *ins,
4441 u64 search_start = 0;
4442 struct btrfs_fs_info *info = root->fs_info;
4444 data = btrfs_get_alloc_profile(root, data);
4447 * the only place that sets empty_size is btrfs_realloc_node, which
4448 * is not called recursively on allocations
4450 if (empty_size || root->ref_cows) {
4451 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
4452 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
4454 BTRFS_BLOCK_GROUP_METADATA |
4455 (info->metadata_alloc_profile &
4456 info->avail_metadata_alloc_bits), 0);
4458 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
4459 num_bytes + 2 * 1024 * 1024, data, 0);
4462 WARN_ON(num_bytes < root->sectorsize);
4463 ret = find_free_extent(trans, root, num_bytes, empty_size,
4464 search_start, search_end, hint_byte, ins,
4465 trans->alloc_exclude_start,
4466 trans->alloc_exclude_nr, data);
4468 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
4469 num_bytes = num_bytes >> 1;
4470 num_bytes = num_bytes & ~(root->sectorsize - 1);
4471 num_bytes = max(num_bytes, min_alloc_size);
4472 do_chunk_alloc(trans, root->fs_info->extent_root,
4473 num_bytes, data, 1);
4476 if (ret == -ENOSPC) {
4477 struct btrfs_space_info *sinfo;
4479 sinfo = __find_space_info(root->fs_info, data);
4480 printk(KERN_ERR "btrfs allocation failed flags %llu, "
4481 "wanted %llu\n", (unsigned long long)data,
4482 (unsigned long long)num_bytes);
4483 dump_space_info(sinfo, num_bytes, 1);
4489 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
4491 struct btrfs_block_group_cache *cache;
4494 cache = btrfs_lookup_block_group(root->fs_info, start);
4496 printk(KERN_ERR "Unable to find block group for %llu\n",
4497 (unsigned long long)start);
4501 ret = btrfs_discard_extent(root, start, len);
4503 btrfs_add_free_space(cache, start, len);
4504 update_reserved_extents(cache, len, 0);
4505 btrfs_put_block_group(cache);
4510 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4511 struct btrfs_root *root,
4512 u64 parent, u64 root_objectid,
4513 u64 flags, u64 owner, u64 offset,
4514 struct btrfs_key *ins, int ref_mod)
4517 struct btrfs_fs_info *fs_info = root->fs_info;
4518 struct btrfs_extent_item *extent_item;
4519 struct btrfs_extent_inline_ref *iref;
4520 struct btrfs_path *path;
4521 struct extent_buffer *leaf;
4526 type = BTRFS_SHARED_DATA_REF_KEY;
4528 type = BTRFS_EXTENT_DATA_REF_KEY;
4530 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
4532 path = btrfs_alloc_path();
4535 path->leave_spinning = 1;
4536 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4540 leaf = path->nodes[0];
4541 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4542 struct btrfs_extent_item);
4543 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
4544 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4545 btrfs_set_extent_flags(leaf, extent_item,
4546 flags | BTRFS_EXTENT_FLAG_DATA);
4548 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4549 btrfs_set_extent_inline_ref_type(leaf, iref, type);
4551 struct btrfs_shared_data_ref *ref;
4552 ref = (struct btrfs_shared_data_ref *)(iref + 1);
4553 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4554 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
4556 struct btrfs_extent_data_ref *ref;
4557 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
4558 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
4559 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
4560 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
4561 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
4564 btrfs_mark_buffer_dirty(path->nodes[0]);
4565 btrfs_free_path(path);
4567 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4570 printk(KERN_ERR "btrfs update block group failed for %llu "
4571 "%llu\n", (unsigned long long)ins->objectid,
4572 (unsigned long long)ins->offset);
4578 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
4579 struct btrfs_root *root,
4580 u64 parent, u64 root_objectid,
4581 u64 flags, struct btrfs_disk_key *key,
4582 int level, struct btrfs_key *ins)
4585 struct btrfs_fs_info *fs_info = root->fs_info;
4586 struct btrfs_extent_item *extent_item;
4587 struct btrfs_tree_block_info *block_info;
4588 struct btrfs_extent_inline_ref *iref;
4589 struct btrfs_path *path;
4590 struct extent_buffer *leaf;
4591 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
4593 path = btrfs_alloc_path();
4596 path->leave_spinning = 1;
4597 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4601 leaf = path->nodes[0];
4602 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4603 struct btrfs_extent_item);
4604 btrfs_set_extent_refs(leaf, extent_item, 1);
4605 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4606 btrfs_set_extent_flags(leaf, extent_item,
4607 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
4608 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
4610 btrfs_set_tree_block_key(leaf, block_info, key);
4611 btrfs_set_tree_block_level(leaf, block_info, level);
4613 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
4615 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4616 btrfs_set_extent_inline_ref_type(leaf, iref,
4617 BTRFS_SHARED_BLOCK_REF_KEY);
4618 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4620 btrfs_set_extent_inline_ref_type(leaf, iref,
4621 BTRFS_TREE_BLOCK_REF_KEY);
4622 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
4625 btrfs_mark_buffer_dirty(leaf);
4626 btrfs_free_path(path);
4628 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4631 printk(KERN_ERR "btrfs update block group failed for %llu "
4632 "%llu\n", (unsigned long long)ins->objectid,
4633 (unsigned long long)ins->offset);
4639 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4640 struct btrfs_root *root,
4641 u64 root_objectid, u64 owner,
4642 u64 offset, struct btrfs_key *ins)
4646 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
4648 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
4649 0, root_objectid, owner, offset,
4650 BTRFS_ADD_DELAYED_EXTENT, NULL);
4655 * this is used by the tree logging recovery code. It records that
4656 * an extent has been allocated and makes sure to clear the free
4657 * space cache bits as well
4659 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
4660 struct btrfs_root *root,
4661 u64 root_objectid, u64 owner, u64 offset,
4662 struct btrfs_key *ins)
4665 struct btrfs_block_group_cache *block_group;
4666 struct btrfs_caching_control *caching_ctl;
4667 u64 start = ins->objectid;
4668 u64 num_bytes = ins->offset;
4670 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
4671 cache_block_group(block_group);
4672 caching_ctl = get_caching_control(block_group);
4675 BUG_ON(!block_group_cache_done(block_group));
4676 ret = btrfs_remove_free_space(block_group, start, num_bytes);
4679 mutex_lock(&caching_ctl->mutex);
4681 if (start >= caching_ctl->progress) {
4682 ret = add_excluded_extent(root, start, num_bytes);
4684 } else if (start + num_bytes <= caching_ctl->progress) {
4685 ret = btrfs_remove_free_space(block_group,
4689 num_bytes = caching_ctl->progress - start;
4690 ret = btrfs_remove_free_space(block_group,
4694 start = caching_ctl->progress;
4695 num_bytes = ins->objectid + ins->offset -
4696 caching_ctl->progress;
4697 ret = add_excluded_extent(root, start, num_bytes);
4701 mutex_unlock(&caching_ctl->mutex);
4702 put_caching_control(caching_ctl);
4705 update_reserved_extents(block_group, ins->offset, 1);
4706 btrfs_put_block_group(block_group);
4707 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
4708 0, owner, offset, ins, 1);
4713 * finds a free extent and does all the dirty work required for allocation
4714 * returns the key for the extent through ins, and a tree buffer for
4715 * the first block of the extent through buf.
4717 * returns 0 if everything worked, non-zero otherwise.
4719 static int alloc_tree_block(struct btrfs_trans_handle *trans,
4720 struct btrfs_root *root,
4721 u64 num_bytes, u64 parent, u64 root_objectid,
4722 struct btrfs_disk_key *key, int level,
4723 u64 empty_size, u64 hint_byte, u64 search_end,
4724 struct btrfs_key *ins)
4729 ret = btrfs_reserve_extent(trans, root, num_bytes, num_bytes,
4730 empty_size, hint_byte, search_end,
4735 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4737 parent = ins->objectid;
4738 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4742 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
4743 struct btrfs_delayed_extent_op *extent_op;
4744 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
4747 memcpy(&extent_op->key, key, sizeof(extent_op->key));
4749 memset(&extent_op->key, 0, sizeof(extent_op->key));
4750 extent_op->flags_to_set = flags;
4751 extent_op->update_key = 1;
4752 extent_op->update_flags = 1;
4753 extent_op->is_data = 0;
4755 ret = btrfs_add_delayed_tree_ref(trans, ins->objectid,
4756 ins->offset, parent, root_objectid,
4757 level, BTRFS_ADD_DELAYED_EXTENT,
4764 struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
4765 struct btrfs_root *root,
4766 u64 bytenr, u32 blocksize,
4769 struct extent_buffer *buf;
4771 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
4773 return ERR_PTR(-ENOMEM);
4774 btrfs_set_header_generation(buf, trans->transid);
4775 btrfs_set_buffer_lockdep_class(buf, level);
4776 btrfs_tree_lock(buf);
4777 clean_tree_block(trans, root, buf);
4779 btrfs_set_lock_blocking(buf);
4780 btrfs_set_buffer_uptodate(buf);
4782 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
4783 set_extent_dirty(&root->dirty_log_pages, buf->start,
4784 buf->start + buf->len - 1, GFP_NOFS);
4786 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
4787 buf->start + buf->len - 1, GFP_NOFS);
4789 trans->blocks_used++;
4790 /* this returns a buffer locked for blocking */
4795 * helper function to allocate a block for a given tree
4796 * returns the tree buffer or NULL.
4798 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
4799 struct btrfs_root *root, u32 blocksize,
4800 u64 parent, u64 root_objectid,
4801 struct btrfs_disk_key *key, int level,
4802 u64 hint, u64 empty_size)
4804 struct btrfs_key ins;
4806 struct extent_buffer *buf;
4808 ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid,
4809 key, level, empty_size, hint, (u64)-1, &ins);
4812 return ERR_PTR(ret);
4815 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
4820 struct walk_control {
4821 u64 refs[BTRFS_MAX_LEVEL];
4822 u64 flags[BTRFS_MAX_LEVEL];
4823 struct btrfs_key update_progress;
4833 #define DROP_REFERENCE 1
4834 #define UPDATE_BACKREF 2
4836 static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
4837 struct btrfs_root *root,
4838 struct walk_control *wc,
4839 struct btrfs_path *path)
4847 struct btrfs_key key;
4848 struct extent_buffer *eb;
4853 if (path->slots[wc->level] < wc->reada_slot) {
4854 wc->reada_count = wc->reada_count * 2 / 3;
4855 wc->reada_count = max(wc->reada_count, 2);
4857 wc->reada_count = wc->reada_count * 3 / 2;
4858 wc->reada_count = min_t(int, wc->reada_count,
4859 BTRFS_NODEPTRS_PER_BLOCK(root));
4862 eb = path->nodes[wc->level];
4863 nritems = btrfs_header_nritems(eb);
4864 blocksize = btrfs_level_size(root, wc->level - 1);
4866 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
4867 if (nread >= wc->reada_count)
4871 bytenr = btrfs_node_blockptr(eb, slot);
4872 generation = btrfs_node_ptr_generation(eb, slot);
4874 if (slot == path->slots[wc->level])
4877 if (wc->stage == UPDATE_BACKREF &&
4878 generation <= root->root_key.offset)
4881 if (wc->stage == DROP_REFERENCE) {
4882 ret = btrfs_lookup_extent_info(trans, root,
4890 if (!wc->update_ref ||
4891 generation <= root->root_key.offset)
4893 btrfs_node_key_to_cpu(eb, &key, slot);
4894 ret = btrfs_comp_cpu_keys(&key,
4895 &wc->update_progress);
4900 ret = readahead_tree_block(root, bytenr, blocksize,
4904 last = bytenr + blocksize;
4907 wc->reada_slot = slot;
4911 * hepler to process tree block while walking down the tree.
4913 * when wc->stage == UPDATE_BACKREF, this function updates
4914 * back refs for pointers in the block.
4916 * NOTE: return value 1 means we should stop walking down.
4918 static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
4919 struct btrfs_root *root,
4920 struct btrfs_path *path,
4921 struct walk_control *wc)
4923 int level = wc->level;
4924 struct extent_buffer *eb = path->nodes[level];
4925 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
4928 if (wc->stage == UPDATE_BACKREF &&
4929 btrfs_header_owner(eb) != root->root_key.objectid)
4933 * when reference count of tree block is 1, it won't increase
4934 * again. once full backref flag is set, we never clear it.
4936 if ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
4937 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag))) {
4938 BUG_ON(!path->locks[level]);
4939 ret = btrfs_lookup_extent_info(trans, root,
4944 BUG_ON(wc->refs[level] == 0);
4947 if (wc->stage == DROP_REFERENCE) {
4948 if (wc->refs[level] > 1)
4951 if (path->locks[level] && !wc->keep_locks) {
4952 btrfs_tree_unlock(eb);
4953 path->locks[level] = 0;
4958 /* wc->stage == UPDATE_BACKREF */
4959 if (!(wc->flags[level] & flag)) {
4960 BUG_ON(!path->locks[level]);
4961 ret = btrfs_inc_ref(trans, root, eb, 1);
4963 ret = btrfs_dec_ref(trans, root, eb, 0);
4965 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
4968 wc->flags[level] |= flag;
4972 * the block is shared by multiple trees, so it's not good to
4973 * keep the tree lock
4975 if (path->locks[level] && level > 0) {
4976 btrfs_tree_unlock(eb);
4977 path->locks[level] = 0;
4983 * hepler to process tree block pointer.
4985 * when wc->stage == DROP_REFERENCE, this function checks
4986 * reference count of the block pointed to. if the block
4987 * is shared and we need update back refs for the subtree
4988 * rooted at the block, this function changes wc->stage to
4989 * UPDATE_BACKREF. if the block is shared and there is no
4990 * need to update back, this function drops the reference
4993 * NOTE: return value 1 means we should stop walking down.
4995 static noinline int do_walk_down(struct btrfs_trans_handle *trans,
4996 struct btrfs_root *root,
4997 struct btrfs_path *path,
4998 struct walk_control *wc)
5004 struct btrfs_key key;
5005 struct extent_buffer *next;
5006 int level = wc->level;
5010 generation = btrfs_node_ptr_generation(path->nodes[level],
5011 path->slots[level]);
5013 * if the lower level block was created before the snapshot
5014 * was created, we know there is no need to update back refs
5017 if (wc->stage == UPDATE_BACKREF &&
5018 generation <= root->root_key.offset)
5021 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
5022 blocksize = btrfs_level_size(root, level - 1);
5024 next = btrfs_find_tree_block(root, bytenr, blocksize);
5026 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
5029 btrfs_tree_lock(next);
5030 btrfs_set_lock_blocking(next);
5032 if (wc->stage == DROP_REFERENCE) {
5033 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5034 &wc->refs[level - 1],
5035 &wc->flags[level - 1]);
5037 BUG_ON(wc->refs[level - 1] == 0);
5039 if (wc->refs[level - 1] > 1) {
5040 if (!wc->update_ref ||
5041 generation <= root->root_key.offset)
5044 btrfs_node_key_to_cpu(path->nodes[level], &key,
5045 path->slots[level]);
5046 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
5050 wc->stage = UPDATE_BACKREF;
5051 wc->shared_level = level - 1;
5055 if (!btrfs_buffer_uptodate(next, generation)) {
5056 btrfs_tree_unlock(next);
5057 free_extent_buffer(next);
5062 if (reada && level == 1)
5063 reada_walk_down(trans, root, wc, path);
5064 next = read_tree_block(root, bytenr, blocksize, generation);
5065 btrfs_tree_lock(next);
5066 btrfs_set_lock_blocking(next);
5070 BUG_ON(level != btrfs_header_level(next));
5071 path->nodes[level] = next;
5072 path->slots[level] = 0;
5073 path->locks[level] = 1;
5079 wc->refs[level - 1] = 0;
5080 wc->flags[level - 1] = 0;
5082 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
5083 parent = path->nodes[level]->start;
5085 BUG_ON(root->root_key.objectid !=
5086 btrfs_header_owner(path->nodes[level]));
5090 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
5091 root->root_key.objectid, level - 1, 0);
5094 btrfs_tree_unlock(next);
5095 free_extent_buffer(next);
5100 * hepler to process tree block while walking up the tree.
5102 * when wc->stage == DROP_REFERENCE, this function drops
5103 * reference count on the block.
5105 * when wc->stage == UPDATE_BACKREF, this function changes
5106 * wc->stage back to DROP_REFERENCE if we changed wc->stage
5107 * to UPDATE_BACKREF previously while processing the block.
5109 * NOTE: return value 1 means we should stop walking up.
5111 static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
5112 struct btrfs_root *root,
5113 struct btrfs_path *path,
5114 struct walk_control *wc)
5117 int level = wc->level;
5118 struct extent_buffer *eb = path->nodes[level];
5121 if (wc->stage == UPDATE_BACKREF) {
5122 BUG_ON(wc->shared_level < level);
5123 if (level < wc->shared_level)
5126 ret = find_next_key(path, level + 1, &wc->update_progress);
5130 wc->stage = DROP_REFERENCE;
5131 wc->shared_level = -1;
5132 path->slots[level] = 0;
5135 * check reference count again if the block isn't locked.
5136 * we should start walking down the tree again if reference
5139 if (!path->locks[level]) {
5141 btrfs_tree_lock(eb);
5142 btrfs_set_lock_blocking(eb);
5143 path->locks[level] = 1;
5145 ret = btrfs_lookup_extent_info(trans, root,
5150 BUG_ON(wc->refs[level] == 0);
5151 if (wc->refs[level] == 1) {
5152 btrfs_tree_unlock(eb);
5153 path->locks[level] = 0;
5159 /* wc->stage == DROP_REFERENCE */
5160 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
5162 if (wc->refs[level] == 1) {
5164 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5165 ret = btrfs_dec_ref(trans, root, eb, 1);
5167 ret = btrfs_dec_ref(trans, root, eb, 0);
5170 /* make block locked assertion in clean_tree_block happy */
5171 if (!path->locks[level] &&
5172 btrfs_header_generation(eb) == trans->transid) {
5173 btrfs_tree_lock(eb);
5174 btrfs_set_lock_blocking(eb);
5175 path->locks[level] = 1;
5177 clean_tree_block(trans, root, eb);
5180 if (eb == root->node) {
5181 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5184 BUG_ON(root->root_key.objectid !=
5185 btrfs_header_owner(eb));
5187 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5188 parent = path->nodes[level + 1]->start;
5190 BUG_ON(root->root_key.objectid !=
5191 btrfs_header_owner(path->nodes[level + 1]));
5194 ret = btrfs_free_extent(trans, root, eb->start, eb->len, parent,
5195 root->root_key.objectid, level, 0);
5198 wc->refs[level] = 0;
5199 wc->flags[level] = 0;
5203 static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
5204 struct btrfs_root *root,
5205 struct btrfs_path *path,
5206 struct walk_control *wc)
5208 int level = wc->level;
5211 while (level >= 0) {
5212 if (path->slots[level] >=
5213 btrfs_header_nritems(path->nodes[level]))
5216 ret = walk_down_proc(trans, root, path, wc);
5223 ret = do_walk_down(trans, root, path, wc);
5225 path->slots[level]++;
5233 static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
5234 struct btrfs_root *root,
5235 struct btrfs_path *path,
5236 struct walk_control *wc, int max_level)
5238 int level = wc->level;
5241 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
5242 while (level < max_level && path->nodes[level]) {
5244 if (path->slots[level] + 1 <
5245 btrfs_header_nritems(path->nodes[level])) {
5246 path->slots[level]++;
5249 ret = walk_up_proc(trans, root, path, wc);
5253 if (path->locks[level]) {
5254 btrfs_tree_unlock(path->nodes[level]);
5255 path->locks[level] = 0;
5257 free_extent_buffer(path->nodes[level]);
5258 path->nodes[level] = NULL;
5266 * drop a subvolume tree.
5268 * this function traverses the tree freeing any blocks that only
5269 * referenced by the tree.
5271 * when a shared tree block is found. this function decreases its
5272 * reference count by one. if update_ref is true, this function
5273 * also make sure backrefs for the shared block and all lower level
5274 * blocks are properly updated.
5276 int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref)
5278 struct btrfs_path *path;
5279 struct btrfs_trans_handle *trans;
5280 struct btrfs_root *tree_root = root->fs_info->tree_root;
5281 struct btrfs_root_item *root_item = &root->root_item;
5282 struct walk_control *wc;
5283 struct btrfs_key key;
5288 path = btrfs_alloc_path();
5291 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5294 trans = btrfs_start_transaction(tree_root, 1);
5296 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
5297 level = btrfs_header_level(root->node);
5298 path->nodes[level] = btrfs_lock_root_node(root);
5299 btrfs_set_lock_blocking(path->nodes[level]);
5300 path->slots[level] = 0;
5301 path->locks[level] = 1;
5302 memset(&wc->update_progress, 0,
5303 sizeof(wc->update_progress));
5305 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
5306 memcpy(&wc->update_progress, &key,
5307 sizeof(wc->update_progress));
5309 level = root_item->drop_level;
5311 path->lowest_level = level;
5312 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5313 path->lowest_level = 0;
5321 * unlock our path, this is safe because only this
5322 * function is allowed to delete this snapshot
5324 btrfs_unlock_up_safe(path, 0);
5326 level = btrfs_header_level(root->node);
5328 btrfs_tree_lock(path->nodes[level]);
5329 btrfs_set_lock_blocking(path->nodes[level]);
5331 ret = btrfs_lookup_extent_info(trans, root,
5332 path->nodes[level]->start,
5333 path->nodes[level]->len,
5337 BUG_ON(wc->refs[level] == 0);
5339 if (level == root_item->drop_level)
5342 btrfs_tree_unlock(path->nodes[level]);
5343 WARN_ON(wc->refs[level] != 1);
5349 wc->shared_level = -1;
5350 wc->stage = DROP_REFERENCE;
5351 wc->update_ref = update_ref;
5353 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
5356 ret = walk_down_tree(trans, root, path, wc);
5362 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5369 BUG_ON(wc->stage != DROP_REFERENCE);
5373 if (wc->stage == DROP_REFERENCE) {
5375 btrfs_node_key(path->nodes[level],
5376 &root_item->drop_progress,
5377 path->slots[level]);
5378 root_item->drop_level = level;
5381 BUG_ON(wc->level == 0);
5382 if (trans->transaction->in_commit ||
5383 trans->transaction->delayed_refs.flushing) {
5384 ret = btrfs_update_root(trans, tree_root,
5389 btrfs_end_transaction(trans, tree_root);
5390 trans = btrfs_start_transaction(tree_root, 1);
5392 unsigned long update;
5393 update = trans->delayed_ref_updates;
5394 trans->delayed_ref_updates = 0;
5396 btrfs_run_delayed_refs(trans, tree_root,
5400 btrfs_release_path(root, path);
5403 ret = btrfs_del_root(trans, tree_root, &root->root_key);
5406 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
5407 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
5411 ret = btrfs_del_orphan_item(trans, tree_root,
5412 root->root_key.objectid);
5417 if (root->in_radix) {
5418 btrfs_free_fs_root(tree_root->fs_info, root);
5420 free_extent_buffer(root->node);
5421 free_extent_buffer(root->commit_root);
5425 btrfs_end_transaction(trans, tree_root);
5427 btrfs_free_path(path);
5432 * drop subtree rooted at tree block 'node'.
5434 * NOTE: this function will unlock and release tree block 'node'
5436 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
5437 struct btrfs_root *root,
5438 struct extent_buffer *node,
5439 struct extent_buffer *parent)
5441 struct btrfs_path *path;
5442 struct walk_control *wc;
5448 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5450 path = btrfs_alloc_path();
5453 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5456 btrfs_assert_tree_locked(parent);
5457 parent_level = btrfs_header_level(parent);
5458 extent_buffer_get(parent);
5459 path->nodes[parent_level] = parent;
5460 path->slots[parent_level] = btrfs_header_nritems(parent);
5462 btrfs_assert_tree_locked(node);
5463 level = btrfs_header_level(node);
5464 path->nodes[level] = node;
5465 path->slots[level] = 0;
5466 path->locks[level] = 1;
5468 wc->refs[parent_level] = 1;
5469 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5471 wc->shared_level = -1;
5472 wc->stage = DROP_REFERENCE;
5475 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
5478 wret = walk_down_tree(trans, root, path, wc);
5484 wret = walk_up_tree(trans, root, path, wc, parent_level);
5492 btrfs_free_path(path);
5497 static unsigned long calc_ra(unsigned long start, unsigned long last,
5500 return min(last, start + nr - 1);
5503 static noinline int relocate_inode_pages(struct inode *inode, u64 start,
5508 unsigned long first_index;
5509 unsigned long last_index;
5512 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5513 struct file_ra_state *ra;
5514 struct btrfs_ordered_extent *ordered;
5515 unsigned int total_read = 0;
5516 unsigned int total_dirty = 0;
5519 ra = kzalloc(sizeof(*ra), GFP_NOFS);
5521 mutex_lock(&inode->i_mutex);
5522 first_index = start >> PAGE_CACHE_SHIFT;
5523 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
5525 /* make sure the dirty trick played by the caller work */
5526 ret = invalidate_inode_pages2_range(inode->i_mapping,
5527 first_index, last_index);
5531 file_ra_state_init(ra, inode->i_mapping);
5533 for (i = first_index ; i <= last_index; i++) {
5534 if (total_read % ra->ra_pages == 0) {
5535 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
5536 calc_ra(i, last_index, ra->ra_pages));
5540 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
5542 page = grab_cache_page(inode->i_mapping, i);
5547 if (!PageUptodate(page)) {
5548 btrfs_readpage(NULL, page);
5550 if (!PageUptodate(page)) {
5552 page_cache_release(page);
5557 wait_on_page_writeback(page);
5559 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
5560 page_end = page_start + PAGE_CACHE_SIZE - 1;
5561 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
5563 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5565 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5567 page_cache_release(page);
5568 btrfs_start_ordered_extent(inode, ordered, 1);
5569 btrfs_put_ordered_extent(ordered);
5572 set_page_extent_mapped(page);
5574 if (i == first_index)
5575 set_extent_bits(io_tree, page_start, page_end,
5576 EXTENT_BOUNDARY, GFP_NOFS);
5577 btrfs_set_extent_delalloc(inode, page_start, page_end);
5579 set_page_dirty(page);
5582 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5584 page_cache_release(page);
5589 mutex_unlock(&inode->i_mutex);
5590 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
5594 static noinline int relocate_data_extent(struct inode *reloc_inode,
5595 struct btrfs_key *extent_key,
5598 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5599 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
5600 struct extent_map *em;
5601 u64 start = extent_key->objectid - offset;
5602 u64 end = start + extent_key->offset - 1;
5604 em = alloc_extent_map(GFP_NOFS);
5605 BUG_ON(!em || IS_ERR(em));
5608 em->len = extent_key->offset;
5609 em->block_len = extent_key->offset;
5610 em->block_start = extent_key->objectid;
5611 em->bdev = root->fs_info->fs_devices->latest_bdev;
5612 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5614 /* setup extent map to cheat btrfs_readpage */
5615 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
5618 write_lock(&em_tree->lock);
5619 ret = add_extent_mapping(em_tree, em);
5620 write_unlock(&em_tree->lock);
5621 if (ret != -EEXIST) {
5622 free_extent_map(em);
5625 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
5627 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
5629 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
5632 struct btrfs_ref_path {
5634 u64 nodes[BTRFS_MAX_LEVEL];
5636 u64 root_generation;
5643 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
5644 u64 new_nodes[BTRFS_MAX_LEVEL];
5647 struct disk_extent {
5658 static int is_cowonly_root(u64 root_objectid)
5660 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
5661 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
5662 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
5663 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
5664 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5665 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
5670 static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
5671 struct btrfs_root *extent_root,
5672 struct btrfs_ref_path *ref_path,
5675 struct extent_buffer *leaf;
5676 struct btrfs_path *path;
5677 struct btrfs_extent_ref *ref;
5678 struct btrfs_key key;
5679 struct btrfs_key found_key;
5685 path = btrfs_alloc_path();
5690 ref_path->lowest_level = -1;
5691 ref_path->current_level = -1;
5692 ref_path->shared_level = -1;
5696 level = ref_path->current_level - 1;
5697 while (level >= -1) {
5699 if (level < ref_path->lowest_level)
5703 bytenr = ref_path->nodes[level];
5705 bytenr = ref_path->extent_start;
5706 BUG_ON(bytenr == 0);
5708 parent = ref_path->nodes[level + 1];
5709 ref_path->nodes[level + 1] = 0;
5710 ref_path->current_level = level;
5711 BUG_ON(parent == 0);
5713 key.objectid = bytenr;
5714 key.offset = parent + 1;
5715 key.type = BTRFS_EXTENT_REF_KEY;
5717 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
5722 leaf = path->nodes[0];
5723 nritems = btrfs_header_nritems(leaf);
5724 if (path->slots[0] >= nritems) {
5725 ret = btrfs_next_leaf(extent_root, path);
5730 leaf = path->nodes[0];
5733 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5734 if (found_key.objectid == bytenr &&
5735 found_key.type == BTRFS_EXTENT_REF_KEY) {
5736 if (level < ref_path->shared_level)
5737 ref_path->shared_level = level;
5742 btrfs_release_path(extent_root, path);
5745 /* reached lowest level */
5749 level = ref_path->current_level;
5750 while (level < BTRFS_MAX_LEVEL - 1) {
5754 bytenr = ref_path->nodes[level];
5756 bytenr = ref_path->extent_start;
5758 BUG_ON(bytenr == 0);
5760 key.objectid = bytenr;
5762 key.type = BTRFS_EXTENT_REF_KEY;
5764 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
5768 leaf = path->nodes[0];
5769 nritems = btrfs_header_nritems(leaf);
5770 if (path->slots[0] >= nritems) {
5771 ret = btrfs_next_leaf(extent_root, path);
5775 /* the extent was freed by someone */
5776 if (ref_path->lowest_level == level)
5778 btrfs_release_path(extent_root, path);
5781 leaf = path->nodes[0];
5784 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5785 if (found_key.objectid != bytenr ||
5786 found_key.type != BTRFS_EXTENT_REF_KEY) {
5787 /* the extent was freed by someone */
5788 if (ref_path->lowest_level == level) {
5792 btrfs_release_path(extent_root, path);
5796 ref = btrfs_item_ptr(leaf, path->slots[0],
5797 struct btrfs_extent_ref);
5798 ref_objectid = btrfs_ref_objectid(leaf, ref);
5799 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5801 level = (int)ref_objectid;
5802 BUG_ON(level >= BTRFS_MAX_LEVEL);
5803 ref_path->lowest_level = level;
5804 ref_path->current_level = level;
5805 ref_path->nodes[level] = bytenr;
5807 WARN_ON(ref_objectid != level);
5810 WARN_ON(level != -1);
5814 if (ref_path->lowest_level == level) {
5815 ref_path->owner_objectid = ref_objectid;
5816 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
5820 * the block is tree root or the block isn't in reference
5823 if (found_key.objectid == found_key.offset ||
5824 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
5825 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5826 ref_path->root_generation =
5827 btrfs_ref_generation(leaf, ref);
5829 /* special reference from the tree log */
5830 ref_path->nodes[0] = found_key.offset;
5831 ref_path->current_level = 0;
5838 BUG_ON(ref_path->nodes[level] != 0);
5839 ref_path->nodes[level] = found_key.offset;
5840 ref_path->current_level = level;
5843 * the reference was created in the running transaction,
5844 * no need to continue walking up.
5846 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
5847 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5848 ref_path->root_generation =
5849 btrfs_ref_generation(leaf, ref);
5854 btrfs_release_path(extent_root, path);
5857 /* reached max tree level, but no tree root found. */
5860 btrfs_free_path(path);
5864 static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
5865 struct btrfs_root *extent_root,
5866 struct btrfs_ref_path *ref_path,
5869 memset(ref_path, 0, sizeof(*ref_path));
5870 ref_path->extent_start = extent_start;
5872 return __next_ref_path(trans, extent_root, ref_path, 1);
5875 static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
5876 struct btrfs_root *extent_root,
5877 struct btrfs_ref_path *ref_path)
5879 return __next_ref_path(trans, extent_root, ref_path, 0);
5882 static noinline int get_new_locations(struct inode *reloc_inode,
5883 struct btrfs_key *extent_key,
5884 u64 offset, int no_fragment,
5885 struct disk_extent **extents,
5888 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5889 struct btrfs_path *path;
5890 struct btrfs_file_extent_item *fi;
5891 struct extent_buffer *leaf;
5892 struct disk_extent *exts = *extents;
5893 struct btrfs_key found_key;
5898 int max = *nr_extents;
5901 WARN_ON(!no_fragment && *extents);
5904 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
5909 path = btrfs_alloc_path();
5912 cur_pos = extent_key->objectid - offset;
5913 last_byte = extent_key->objectid + extent_key->offset;
5914 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
5924 leaf = path->nodes[0];
5925 nritems = btrfs_header_nritems(leaf);
5926 if (path->slots[0] >= nritems) {
5927 ret = btrfs_next_leaf(root, path);
5932 leaf = path->nodes[0];
5935 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5936 if (found_key.offset != cur_pos ||
5937 found_key.type != BTRFS_EXTENT_DATA_KEY ||
5938 found_key.objectid != reloc_inode->i_ino)
5941 fi = btrfs_item_ptr(leaf, path->slots[0],
5942 struct btrfs_file_extent_item);
5943 if (btrfs_file_extent_type(leaf, fi) !=
5944 BTRFS_FILE_EXTENT_REG ||
5945 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
5949 struct disk_extent *old = exts;
5951 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
5952 memcpy(exts, old, sizeof(*exts) * nr);
5953 if (old != *extents)
5957 exts[nr].disk_bytenr =
5958 btrfs_file_extent_disk_bytenr(leaf, fi);
5959 exts[nr].disk_num_bytes =
5960 btrfs_file_extent_disk_num_bytes(leaf, fi);
5961 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
5962 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
5963 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
5964 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
5965 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
5966 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
5968 BUG_ON(exts[nr].offset > 0);
5969 BUG_ON(exts[nr].compression || exts[nr].encryption);
5970 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
5972 cur_pos += exts[nr].num_bytes;
5975 if (cur_pos + offset >= last_byte)
5985 BUG_ON(cur_pos + offset > last_byte);
5986 if (cur_pos + offset < last_byte) {
5992 btrfs_free_path(path);
5994 if (exts != *extents)
6003 static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
6004 struct btrfs_root *root,
6005 struct btrfs_path *path,
6006 struct btrfs_key *extent_key,
6007 struct btrfs_key *leaf_key,
6008 struct btrfs_ref_path *ref_path,
6009 struct disk_extent *new_extents,
6012 struct extent_buffer *leaf;
6013 struct btrfs_file_extent_item *fi;
6014 struct inode *inode = NULL;
6015 struct btrfs_key key;
6020 u64 search_end = (u64)-1;
6023 int extent_locked = 0;
6027 memcpy(&key, leaf_key, sizeof(key));
6028 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
6029 if (key.objectid < ref_path->owner_objectid ||
6030 (key.objectid == ref_path->owner_objectid &&
6031 key.type < BTRFS_EXTENT_DATA_KEY)) {
6032 key.objectid = ref_path->owner_objectid;
6033 key.type = BTRFS_EXTENT_DATA_KEY;
6039 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
6043 leaf = path->nodes[0];
6044 nritems = btrfs_header_nritems(leaf);
6046 if (extent_locked && ret > 0) {
6048 * the file extent item was modified by someone
6049 * before the extent got locked.
6051 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6052 lock_end, GFP_NOFS);
6056 if (path->slots[0] >= nritems) {
6057 if (++nr_scaned > 2)
6060 BUG_ON(extent_locked);
6061 ret = btrfs_next_leaf(root, path);
6066 leaf = path->nodes[0];
6067 nritems = btrfs_header_nritems(leaf);
6070 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6072 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
6073 if ((key.objectid > ref_path->owner_objectid) ||
6074 (key.objectid == ref_path->owner_objectid &&
6075 key.type > BTRFS_EXTENT_DATA_KEY) ||
6076 key.offset >= search_end)
6080 if (inode && key.objectid != inode->i_ino) {
6081 BUG_ON(extent_locked);
6082 btrfs_release_path(root, path);
6083 mutex_unlock(&inode->i_mutex);
6089 if (key.type != BTRFS_EXTENT_DATA_KEY) {
6094 fi = btrfs_item_ptr(leaf, path->slots[0],
6095 struct btrfs_file_extent_item);
6096 extent_type = btrfs_file_extent_type(leaf, fi);
6097 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
6098 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
6099 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
6100 extent_key->objectid)) {
6106 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6107 ext_offset = btrfs_file_extent_offset(leaf, fi);
6109 if (search_end == (u64)-1) {
6110 search_end = key.offset - ext_offset +
6111 btrfs_file_extent_ram_bytes(leaf, fi);
6114 if (!extent_locked) {
6115 lock_start = key.offset;
6116 lock_end = lock_start + num_bytes - 1;
6118 if (lock_start > key.offset ||
6119 lock_end + 1 < key.offset + num_bytes) {
6120 unlock_extent(&BTRFS_I(inode)->io_tree,
6121 lock_start, lock_end, GFP_NOFS);
6127 btrfs_release_path(root, path);
6129 inode = btrfs_iget_locked(root->fs_info->sb,
6130 key.objectid, root);
6131 if (inode->i_state & I_NEW) {
6132 BTRFS_I(inode)->root = root;
6133 BTRFS_I(inode)->location.objectid =
6135 BTRFS_I(inode)->location.type =
6136 BTRFS_INODE_ITEM_KEY;
6137 BTRFS_I(inode)->location.offset = 0;
6138 btrfs_read_locked_inode(inode);
6139 unlock_new_inode(inode);
6142 * some code call btrfs_commit_transaction while
6143 * holding the i_mutex, so we can't use mutex_lock
6146 if (is_bad_inode(inode) ||
6147 !mutex_trylock(&inode->i_mutex)) {
6150 key.offset = (u64)-1;
6155 if (!extent_locked) {
6156 struct btrfs_ordered_extent *ordered;
6158 btrfs_release_path(root, path);
6160 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6161 lock_end, GFP_NOFS);
6162 ordered = btrfs_lookup_first_ordered_extent(inode,
6165 ordered->file_offset <= lock_end &&
6166 ordered->file_offset + ordered->len > lock_start) {
6167 unlock_extent(&BTRFS_I(inode)->io_tree,
6168 lock_start, lock_end, GFP_NOFS);
6169 btrfs_start_ordered_extent(inode, ordered, 1);
6170 btrfs_put_ordered_extent(ordered);
6171 key.offset += num_bytes;
6175 btrfs_put_ordered_extent(ordered);
6181 if (nr_extents == 1) {
6182 /* update extent pointer in place */
6183 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6184 new_extents[0].disk_bytenr);
6185 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6186 new_extents[0].disk_num_bytes);
6187 btrfs_mark_buffer_dirty(leaf);
6189 btrfs_drop_extent_cache(inode, key.offset,
6190 key.offset + num_bytes - 1, 0);
6192 ret = btrfs_inc_extent_ref(trans, root,
6193 new_extents[0].disk_bytenr,
6194 new_extents[0].disk_num_bytes,
6196 root->root_key.objectid,
6201 ret = btrfs_free_extent(trans, root,
6202 extent_key->objectid,
6205 btrfs_header_owner(leaf),
6206 btrfs_header_generation(leaf),
6210 btrfs_release_path(root, path);
6211 key.offset += num_bytes;
6219 * drop old extent pointer at first, then insert the
6220 * new pointers one bye one
6222 btrfs_release_path(root, path);
6223 ret = btrfs_drop_extents(trans, root, inode, key.offset,
6224 key.offset + num_bytes,
6225 key.offset, &alloc_hint);
6228 for (i = 0; i < nr_extents; i++) {
6229 if (ext_offset >= new_extents[i].num_bytes) {
6230 ext_offset -= new_extents[i].num_bytes;
6233 extent_len = min(new_extents[i].num_bytes -
6234 ext_offset, num_bytes);
6236 ret = btrfs_insert_empty_item(trans, root,
6241 leaf = path->nodes[0];
6242 fi = btrfs_item_ptr(leaf, path->slots[0],
6243 struct btrfs_file_extent_item);
6244 btrfs_set_file_extent_generation(leaf, fi,
6246 btrfs_set_file_extent_type(leaf, fi,
6247 BTRFS_FILE_EXTENT_REG);
6248 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6249 new_extents[i].disk_bytenr);
6250 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6251 new_extents[i].disk_num_bytes);
6252 btrfs_set_file_extent_ram_bytes(leaf, fi,
6253 new_extents[i].ram_bytes);
6255 btrfs_set_file_extent_compression(leaf, fi,
6256 new_extents[i].compression);
6257 btrfs_set_file_extent_encryption(leaf, fi,
6258 new_extents[i].encryption);
6259 btrfs_set_file_extent_other_encoding(leaf, fi,
6260 new_extents[i].other_encoding);
6262 btrfs_set_file_extent_num_bytes(leaf, fi,
6264 ext_offset += new_extents[i].offset;
6265 btrfs_set_file_extent_offset(leaf, fi,
6267 btrfs_mark_buffer_dirty(leaf);
6269 btrfs_drop_extent_cache(inode, key.offset,
6270 key.offset + extent_len - 1, 0);
6272 ret = btrfs_inc_extent_ref(trans, root,
6273 new_extents[i].disk_bytenr,
6274 new_extents[i].disk_num_bytes,
6276 root->root_key.objectid,
6277 trans->transid, key.objectid);
6279 btrfs_release_path(root, path);
6281 inode_add_bytes(inode, extent_len);
6284 num_bytes -= extent_len;
6285 key.offset += extent_len;
6290 BUG_ON(i >= nr_extents);
6294 if (extent_locked) {
6295 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6296 lock_end, GFP_NOFS);
6300 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
6301 key.offset >= search_end)
6308 btrfs_release_path(root, path);
6310 mutex_unlock(&inode->i_mutex);
6311 if (extent_locked) {
6312 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6313 lock_end, GFP_NOFS);
6320 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
6321 struct btrfs_root *root,
6322 struct extent_buffer *buf, u64 orig_start)
6327 BUG_ON(btrfs_header_generation(buf) != trans->transid);
6328 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6330 level = btrfs_header_level(buf);
6332 struct btrfs_leaf_ref *ref;
6333 struct btrfs_leaf_ref *orig_ref;
6335 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
6339 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
6341 btrfs_free_leaf_ref(root, orig_ref);
6345 ref->nritems = orig_ref->nritems;
6346 memcpy(ref->extents, orig_ref->extents,
6347 sizeof(ref->extents[0]) * ref->nritems);
6349 btrfs_free_leaf_ref(root, orig_ref);
6351 ref->root_gen = trans->transid;
6352 ref->bytenr = buf->start;
6353 ref->owner = btrfs_header_owner(buf);
6354 ref->generation = btrfs_header_generation(buf);
6356 ret = btrfs_add_leaf_ref(root, ref, 0);
6358 btrfs_free_leaf_ref(root, ref);
6363 static noinline int invalidate_extent_cache(struct btrfs_root *root,
6364 struct extent_buffer *leaf,
6365 struct btrfs_block_group_cache *group,
6366 struct btrfs_root *target_root)
6368 struct btrfs_key key;
6369 struct inode *inode = NULL;
6370 struct btrfs_file_extent_item *fi;
6372 u64 skip_objectid = 0;
6376 nritems = btrfs_header_nritems(leaf);
6377 for (i = 0; i < nritems; i++) {
6378 btrfs_item_key_to_cpu(leaf, &key, i);
6379 if (key.objectid == skip_objectid ||
6380 key.type != BTRFS_EXTENT_DATA_KEY)
6382 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6383 if (btrfs_file_extent_type(leaf, fi) ==
6384 BTRFS_FILE_EXTENT_INLINE)
6386 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6388 if (!inode || inode->i_ino != key.objectid) {
6390 inode = btrfs_ilookup(target_root->fs_info->sb,
6391 key.objectid, target_root, 1);
6394 skip_objectid = key.objectid;
6397 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6399 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
6400 key.offset + num_bytes - 1, GFP_NOFS);
6401 btrfs_drop_extent_cache(inode, key.offset,
6402 key.offset + num_bytes - 1, 1);
6403 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
6404 key.offset + num_bytes - 1, GFP_NOFS);
6411 static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
6412 struct btrfs_root *root,
6413 struct extent_buffer *leaf,
6414 struct btrfs_block_group_cache *group,
6415 struct inode *reloc_inode)
6417 struct btrfs_key key;
6418 struct btrfs_key extent_key;
6419 struct btrfs_file_extent_item *fi;
6420 struct btrfs_leaf_ref *ref;
6421 struct disk_extent *new_extent;
6430 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
6431 BUG_ON(!new_extent);
6433 ref = btrfs_lookup_leaf_ref(root, leaf->start);
6437 nritems = btrfs_header_nritems(leaf);
6438 for (i = 0; i < nritems; i++) {
6439 btrfs_item_key_to_cpu(leaf, &key, i);
6440 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6442 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6443 if (btrfs_file_extent_type(leaf, fi) ==
6444 BTRFS_FILE_EXTENT_INLINE)
6446 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6447 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
6452 if (bytenr >= group->key.objectid + group->key.offset ||
6453 bytenr + num_bytes <= group->key.objectid)
6456 extent_key.objectid = bytenr;
6457 extent_key.offset = num_bytes;
6458 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
6460 ret = get_new_locations(reloc_inode, &extent_key,
6461 group->key.objectid, 1,
6462 &new_extent, &nr_extent);
6467 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
6468 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
6469 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
6470 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
6472 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6473 new_extent->disk_bytenr);
6474 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6475 new_extent->disk_num_bytes);
6476 btrfs_mark_buffer_dirty(leaf);
6478 ret = btrfs_inc_extent_ref(trans, root,
6479 new_extent->disk_bytenr,
6480 new_extent->disk_num_bytes,
6482 root->root_key.objectid,
6483 trans->transid, key.objectid);
6486 ret = btrfs_free_extent(trans, root,
6487 bytenr, num_bytes, leaf->start,
6488 btrfs_header_owner(leaf),
6489 btrfs_header_generation(leaf),
6495 BUG_ON(ext_index + 1 != ref->nritems);
6496 btrfs_free_leaf_ref(root, ref);
6500 int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
6501 struct btrfs_root *root)
6503 struct btrfs_root *reloc_root;
6506 if (root->reloc_root) {
6507 reloc_root = root->reloc_root;
6508 root->reloc_root = NULL;
6509 list_add(&reloc_root->dead_list,
6510 &root->fs_info->dead_reloc_roots);
6512 btrfs_set_root_bytenr(&reloc_root->root_item,
6513 reloc_root->node->start);
6514 btrfs_set_root_level(&root->root_item,
6515 btrfs_header_level(reloc_root->node));
6516 memset(&reloc_root->root_item.drop_progress, 0,
6517 sizeof(struct btrfs_disk_key));
6518 reloc_root->root_item.drop_level = 0;
6520 ret = btrfs_update_root(trans, root->fs_info->tree_root,
6521 &reloc_root->root_key,
6522 &reloc_root->root_item);
6528 int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
6530 struct btrfs_trans_handle *trans;
6531 struct btrfs_root *reloc_root;
6532 struct btrfs_root *prev_root = NULL;
6533 struct list_head dead_roots;
6537 INIT_LIST_HEAD(&dead_roots);
6538 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
6540 while (!list_empty(&dead_roots)) {
6541 reloc_root = list_entry(dead_roots.prev,
6542 struct btrfs_root, dead_list);
6543 list_del_init(&reloc_root->dead_list);
6545 BUG_ON(reloc_root->commit_root != NULL);
6547 trans = btrfs_join_transaction(root, 1);
6550 mutex_lock(&root->fs_info->drop_mutex);
6551 ret = btrfs_drop_snapshot(trans, reloc_root);
6554 mutex_unlock(&root->fs_info->drop_mutex);
6556 nr = trans->blocks_used;
6557 ret = btrfs_end_transaction(trans, root);
6559 btrfs_btree_balance_dirty(root, nr);
6562 free_extent_buffer(reloc_root->node);
6564 ret = btrfs_del_root(trans, root->fs_info->tree_root,
6565 &reloc_root->root_key);
6567 mutex_unlock(&root->fs_info->drop_mutex);
6569 nr = trans->blocks_used;
6570 ret = btrfs_end_transaction(trans, root);
6572 btrfs_btree_balance_dirty(root, nr);
6575 prev_root = reloc_root;
6578 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
6584 int btrfs_add_dead_reloc_root(struct btrfs_root *root)
6586 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
6590 int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
6592 struct btrfs_root *reloc_root;
6593 struct btrfs_trans_handle *trans;
6594 struct btrfs_key location;
6598 mutex_lock(&root->fs_info->tree_reloc_mutex);
6599 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
6601 found = !list_empty(&root->fs_info->dead_reloc_roots);
6602 mutex_unlock(&root->fs_info->tree_reloc_mutex);
6605 trans = btrfs_start_transaction(root, 1);
6607 ret = btrfs_commit_transaction(trans, root);
6611 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6612 location.offset = (u64)-1;
6613 location.type = BTRFS_ROOT_ITEM_KEY;
6615 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
6616 BUG_ON(!reloc_root);
6617 btrfs_orphan_cleanup(reloc_root);
6621 static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
6622 struct btrfs_root *root)
6624 struct btrfs_root *reloc_root;
6625 struct extent_buffer *eb;
6626 struct btrfs_root_item *root_item;
6627 struct btrfs_key root_key;
6630 BUG_ON(!root->ref_cows);
6631 if (root->reloc_root)
6634 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
6637 ret = btrfs_copy_root(trans, root, root->commit_root,
6638 &eb, BTRFS_TREE_RELOC_OBJECTID);
6641 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
6642 root_key.offset = root->root_key.objectid;
6643 root_key.type = BTRFS_ROOT_ITEM_KEY;
6645 memcpy(root_item, &root->root_item, sizeof(root_item));
6646 btrfs_set_root_refs(root_item, 0);
6647 btrfs_set_root_bytenr(root_item, eb->start);
6648 btrfs_set_root_level(root_item, btrfs_header_level(eb));
6649 btrfs_set_root_generation(root_item, trans->transid);
6651 btrfs_tree_unlock(eb);
6652 free_extent_buffer(eb);
6654 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
6655 &root_key, root_item);
6659 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
6661 BUG_ON(!reloc_root);
6662 reloc_root->last_trans = trans->transid;
6663 reloc_root->commit_root = NULL;
6664 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
6666 root->reloc_root = reloc_root;
6671 * Core function of space balance.
6673 * The idea is using reloc trees to relocate tree blocks in reference
6674 * counted roots. There is one reloc tree for each subvol, and all
6675 * reloc trees share same root key objectid. Reloc trees are snapshots
6676 * of the latest committed roots of subvols (root->commit_root).
6678 * To relocate a tree block referenced by a subvol, there are two steps.
6679 * COW the block through subvol's reloc tree, then update block pointer
6680 * in the subvol to point to the new block. Since all reloc trees share
6681 * same root key objectid, doing special handing for tree blocks owned
6682 * by them is easy. Once a tree block has been COWed in one reloc tree,
6683 * we can use the resulting new block directly when the same block is
6684 * required to COW again through other reloc trees. By this way, relocated
6685 * tree blocks are shared between reloc trees, so they are also shared
6688 static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
6689 struct btrfs_root *root,
6690 struct btrfs_path *path,
6691 struct btrfs_key *first_key,
6692 struct btrfs_ref_path *ref_path,
6693 struct btrfs_block_group_cache *group,
6694 struct inode *reloc_inode)
6696 struct btrfs_root *reloc_root;
6697 struct extent_buffer *eb = NULL;
6698 struct btrfs_key *keys;
6702 int lowest_level = 0;
6705 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
6706 lowest_level = ref_path->owner_objectid;
6708 if (!root->ref_cows) {
6709 path->lowest_level = lowest_level;
6710 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
6712 path->lowest_level = 0;
6713 btrfs_release_path(root, path);
6717 mutex_lock(&root->fs_info->tree_reloc_mutex);
6718 ret = init_reloc_tree(trans, root);
6720 reloc_root = root->reloc_root;
6722 shared_level = ref_path->shared_level;
6723 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
6725 keys = ref_path->node_keys;
6726 nodes = ref_path->new_nodes;
6727 memset(&keys[shared_level + 1], 0,
6728 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
6729 memset(&nodes[shared_level + 1], 0,
6730 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
6732 if (nodes[lowest_level] == 0) {
6733 path->lowest_level = lowest_level;
6734 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6737 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
6738 eb = path->nodes[level];
6739 if (!eb || eb == reloc_root->node)
6741 nodes[level] = eb->start;
6743 btrfs_item_key_to_cpu(eb, &keys[level], 0);
6745 btrfs_node_key_to_cpu(eb, &keys[level], 0);
6748 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6749 eb = path->nodes[0];
6750 ret = replace_extents_in_leaf(trans, reloc_root, eb,
6751 group, reloc_inode);
6754 btrfs_release_path(reloc_root, path);
6756 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
6762 * replace tree blocks in the fs tree with tree blocks in
6765 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
6768 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6769 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6772 extent_buffer_get(path->nodes[0]);
6773 eb = path->nodes[0];
6774 btrfs_release_path(reloc_root, path);
6775 ret = invalidate_extent_cache(reloc_root, eb, group, root);
6777 free_extent_buffer(eb);
6780 mutex_unlock(&root->fs_info->tree_reloc_mutex);
6781 path->lowest_level = 0;
6785 static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
6786 struct btrfs_root *root,
6787 struct btrfs_path *path,
6788 struct btrfs_key *first_key,
6789 struct btrfs_ref_path *ref_path)
6793 ret = relocate_one_path(trans, root, path, first_key,
6794 ref_path, NULL, NULL);
6800 static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
6801 struct btrfs_root *extent_root,
6802 struct btrfs_path *path,
6803 struct btrfs_key *extent_key)
6807 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
6810 ret = btrfs_del_item(trans, extent_root, path);
6812 btrfs_release_path(extent_root, path);
6816 static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
6817 struct btrfs_ref_path *ref_path)
6819 struct btrfs_key root_key;
6821 root_key.objectid = ref_path->root_objectid;
6822 root_key.type = BTRFS_ROOT_ITEM_KEY;
6823 if (is_cowonly_root(ref_path->root_objectid))
6824 root_key.offset = 0;
6826 root_key.offset = (u64)-1;
6828 return btrfs_read_fs_root_no_name(fs_info, &root_key);
6831 static noinline int relocate_one_extent(struct btrfs_root *extent_root,
6832 struct btrfs_path *path,
6833 struct btrfs_key *extent_key,
6834 struct btrfs_block_group_cache *group,
6835 struct inode *reloc_inode, int pass)
6837 struct btrfs_trans_handle *trans;
6838 struct btrfs_root *found_root;
6839 struct btrfs_ref_path *ref_path = NULL;
6840 struct disk_extent *new_extents = NULL;
6845 struct btrfs_key first_key;
6849 trans = btrfs_start_transaction(extent_root, 1);
6852 if (extent_key->objectid == 0) {
6853 ret = del_extent_zero(trans, extent_root, path, extent_key);
6857 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
6863 for (loops = 0; ; loops++) {
6865 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
6866 extent_key->objectid);
6868 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
6875 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6876 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
6879 found_root = read_ref_root(extent_root->fs_info, ref_path);
6880 BUG_ON(!found_root);
6882 * for reference counted tree, only process reference paths
6883 * rooted at the latest committed root.
6885 if (found_root->ref_cows &&
6886 ref_path->root_generation != found_root->root_key.offset)
6889 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6892 * copy data extents to new locations
6894 u64 group_start = group->key.objectid;
6895 ret = relocate_data_extent(reloc_inode,
6904 level = ref_path->owner_objectid;
6907 if (prev_block != ref_path->nodes[level]) {
6908 struct extent_buffer *eb;
6909 u64 block_start = ref_path->nodes[level];
6910 u64 block_size = btrfs_level_size(found_root, level);
6912 eb = read_tree_block(found_root, block_start,
6914 btrfs_tree_lock(eb);
6915 BUG_ON(level != btrfs_header_level(eb));
6918 btrfs_item_key_to_cpu(eb, &first_key, 0);
6920 btrfs_node_key_to_cpu(eb, &first_key, 0);
6922 btrfs_tree_unlock(eb);
6923 free_extent_buffer(eb);
6924 prev_block = block_start;
6927 mutex_lock(&extent_root->fs_info->trans_mutex);
6928 btrfs_record_root_in_trans(found_root);
6929 mutex_unlock(&extent_root->fs_info->trans_mutex);
6930 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6932 * try to update data extent references while
6933 * keeping metadata shared between snapshots.
6936 ret = relocate_one_path(trans, found_root,
6937 path, &first_key, ref_path,
6938 group, reloc_inode);
6944 * use fallback method to process the remaining
6948 u64 group_start = group->key.objectid;
6949 new_extents = kmalloc(sizeof(*new_extents),
6952 ret = get_new_locations(reloc_inode,
6960 ret = replace_one_extent(trans, found_root,
6962 &first_key, ref_path,
6963 new_extents, nr_extents);
6965 ret = relocate_tree_block(trans, found_root, path,
6966 &first_key, ref_path);
6973 btrfs_end_transaction(trans, extent_root);
6980 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
6983 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
6984 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
6986 num_devices = root->fs_info->fs_devices->rw_devices;
6987 if (num_devices == 1) {
6988 stripped |= BTRFS_BLOCK_GROUP_DUP;
6989 stripped = flags & ~stripped;
6991 /* turn raid0 into single device chunks */
6992 if (flags & BTRFS_BLOCK_GROUP_RAID0)
6995 /* turn mirroring into duplication */
6996 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
6997 BTRFS_BLOCK_GROUP_RAID10))
6998 return stripped | BTRFS_BLOCK_GROUP_DUP;
7001 /* they already had raid on here, just return */
7002 if (flags & stripped)
7005 stripped |= BTRFS_BLOCK_GROUP_DUP;
7006 stripped = flags & ~stripped;
7008 /* switch duplicated blocks with raid1 */
7009 if (flags & BTRFS_BLOCK_GROUP_DUP)
7010 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7012 /* turn single device chunks into raid0 */
7013 return stripped | BTRFS_BLOCK_GROUP_RAID0;
7018 static int __alloc_chunk_for_shrink(struct btrfs_root *root,
7019 struct btrfs_block_group_cache *shrink_block_group,
7022 struct btrfs_trans_handle *trans;
7023 u64 new_alloc_flags;
7026 spin_lock(&shrink_block_group->lock);
7027 if (btrfs_block_group_used(&shrink_block_group->item) +
7028 shrink_block_group->reserved > 0) {
7029 spin_unlock(&shrink_block_group->lock);
7031 trans = btrfs_start_transaction(root, 1);
7032 spin_lock(&shrink_block_group->lock);
7034 new_alloc_flags = update_block_group_flags(root,
7035 shrink_block_group->flags);
7036 if (new_alloc_flags != shrink_block_group->flags) {
7038 btrfs_block_group_used(&shrink_block_group->item);
7040 calc = shrink_block_group->key.offset;
7042 spin_unlock(&shrink_block_group->lock);
7044 do_chunk_alloc(trans, root->fs_info->extent_root,
7045 calc + 2 * 1024 * 1024, new_alloc_flags, force);
7047 btrfs_end_transaction(trans, root);
7049 spin_unlock(&shrink_block_group->lock);
7054 int btrfs_prepare_block_group_relocation(struct btrfs_root *root,
7055 struct btrfs_block_group_cache *group)
7058 __alloc_chunk_for_shrink(root, group, 1);
7059 set_block_group_readonly(group);
7064 * checks to see if its even possible to relocate this block group.
7066 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7067 * ok to go ahead and try.
7069 int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
7071 struct btrfs_block_group_cache *block_group;
7072 struct btrfs_space_info *space_info;
7073 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7074 struct btrfs_device *device;
7078 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
7080 /* odd, couldn't find the block group, leave it alone */
7084 /* no bytes used, we're good */
7085 if (!btrfs_block_group_used(&block_group->item))
7088 space_info = block_group->space_info;
7089 spin_lock(&space_info->lock);
7091 full = space_info->full;
7094 * if this is the last block group we have in this space, we can't
7095 * relocate it unless we're able to allocate a new chunk below.
7097 * Otherwise, we need to make sure we have room in the space to handle
7098 * all of the extents from this block group. If we can, we're good
7100 if ((space_info->total_bytes != block_group->key.offset) &&
7101 (space_info->bytes_used + space_info->bytes_reserved +
7102 space_info->bytes_pinned + space_info->bytes_readonly +
7103 btrfs_block_group_used(&block_group->item) <
7104 space_info->total_bytes)) {
7105 spin_unlock(&space_info->lock);
7108 spin_unlock(&space_info->lock);
7111 * ok we don't have enough space, but maybe we have free space on our
7112 * devices to allocate new chunks for relocation, so loop through our
7113 * alloc devices and guess if we have enough space. However, if we
7114 * were marked as full, then we know there aren't enough chunks, and we
7121 mutex_lock(&root->fs_info->chunk_mutex);
7122 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
7123 u64 min_free = btrfs_block_group_used(&block_group->item);
7124 u64 dev_offset, max_avail;
7127 * check to make sure we can actually find a chunk with enough
7128 * space to fit our block group in.
7130 if (device->total_bytes > device->bytes_used + min_free) {
7131 ret = find_free_dev_extent(NULL, device, min_free,
7132 &dev_offset, &max_avail);
7138 mutex_unlock(&root->fs_info->chunk_mutex);
7140 btrfs_put_block_group(block_group);
7144 static int find_first_block_group(struct btrfs_root *root,
7145 struct btrfs_path *path, struct btrfs_key *key)
7148 struct btrfs_key found_key;
7149 struct extent_buffer *leaf;
7152 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7157 slot = path->slots[0];
7158 leaf = path->nodes[0];
7159 if (slot >= btrfs_header_nritems(leaf)) {
7160 ret = btrfs_next_leaf(root, path);
7167 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7169 if (found_key.objectid >= key->objectid &&
7170 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7181 int btrfs_free_block_groups(struct btrfs_fs_info *info)
7183 struct btrfs_block_group_cache *block_group;
7184 struct btrfs_space_info *space_info;
7185 struct btrfs_caching_control *caching_ctl;
7188 down_write(&info->extent_commit_sem);
7189 while (!list_empty(&info->caching_block_groups)) {
7190 caching_ctl = list_entry(info->caching_block_groups.next,
7191 struct btrfs_caching_control, list);
7192 list_del(&caching_ctl->list);
7193 put_caching_control(caching_ctl);
7195 up_write(&info->extent_commit_sem);
7197 spin_lock(&info->block_group_cache_lock);
7198 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7199 block_group = rb_entry(n, struct btrfs_block_group_cache,
7201 rb_erase(&block_group->cache_node,
7202 &info->block_group_cache_tree);
7203 spin_unlock(&info->block_group_cache_lock);
7205 down_write(&block_group->space_info->groups_sem);
7206 list_del(&block_group->list);
7207 up_write(&block_group->space_info->groups_sem);
7209 if (block_group->cached == BTRFS_CACHE_STARTED)
7210 wait_block_group_cache_done(block_group);
7212 btrfs_remove_free_space_cache(block_group);
7214 WARN_ON(atomic_read(&block_group->count) != 1);
7217 spin_lock(&info->block_group_cache_lock);
7219 spin_unlock(&info->block_group_cache_lock);
7221 /* now that all the block groups are freed, go through and
7222 * free all the space_info structs. This is only called during
7223 * the final stages of unmount, and so we know nobody is
7224 * using them. We call synchronize_rcu() once before we start,
7225 * just to be on the safe side.
7229 while(!list_empty(&info->space_info)) {
7230 space_info = list_entry(info->space_info.next,
7231 struct btrfs_space_info,
7234 list_del(&space_info->list);
7240 int btrfs_read_block_groups(struct btrfs_root *root)
7242 struct btrfs_path *path;
7244 struct btrfs_block_group_cache *cache;
7245 struct btrfs_fs_info *info = root->fs_info;
7246 struct btrfs_space_info *space_info;
7247 struct btrfs_key key;
7248 struct btrfs_key found_key;
7249 struct extent_buffer *leaf;
7251 root = info->extent_root;
7254 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
7255 path = btrfs_alloc_path();
7260 ret = find_first_block_group(root, path, &key);
7268 leaf = path->nodes[0];
7269 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7270 cache = kzalloc(sizeof(*cache), GFP_NOFS);
7276 atomic_set(&cache->count, 1);
7277 spin_lock_init(&cache->lock);
7278 spin_lock_init(&cache->tree_lock);
7279 cache->fs_info = info;
7280 INIT_LIST_HEAD(&cache->list);
7281 INIT_LIST_HEAD(&cache->cluster_list);
7284 * we only want to have 32k of ram per block group for keeping
7285 * track of free space, and if we pass 1/2 of that we want to
7286 * start converting things over to using bitmaps
7288 cache->extents_thresh = ((1024 * 32) / 2) /
7289 sizeof(struct btrfs_free_space);
7291 read_extent_buffer(leaf, &cache->item,
7292 btrfs_item_ptr_offset(leaf, path->slots[0]),
7293 sizeof(cache->item));
7294 memcpy(&cache->key, &found_key, sizeof(found_key));
7296 key.objectid = found_key.objectid + found_key.offset;
7297 btrfs_release_path(root, path);
7298 cache->flags = btrfs_block_group_flags(&cache->item);
7299 cache->sectorsize = root->sectorsize;
7302 * check for two cases, either we are full, and therefore
7303 * don't need to bother with the caching work since we won't
7304 * find any space, or we are empty, and we can just add all
7305 * the space in and be done with it. This saves us _alot_ of
7306 * time, particularly in the full case.
7308 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
7309 exclude_super_stripes(root, cache);
7310 cache->last_byte_to_unpin = (u64)-1;
7311 cache->cached = BTRFS_CACHE_FINISHED;
7312 free_excluded_extents(root, cache);
7313 } else if (btrfs_block_group_used(&cache->item) == 0) {
7314 exclude_super_stripes(root, cache);
7315 cache->last_byte_to_unpin = (u64)-1;
7316 cache->cached = BTRFS_CACHE_FINISHED;
7317 add_new_free_space(cache, root->fs_info,
7319 found_key.objectid +
7321 free_excluded_extents(root, cache);
7324 ret = update_space_info(info, cache->flags, found_key.offset,
7325 btrfs_block_group_used(&cache->item),
7328 cache->space_info = space_info;
7329 spin_lock(&cache->space_info->lock);
7330 cache->space_info->bytes_super += cache->bytes_super;
7331 spin_unlock(&cache->space_info->lock);
7333 down_write(&space_info->groups_sem);
7334 list_add_tail(&cache->list, &space_info->block_groups);
7335 up_write(&space_info->groups_sem);
7337 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7340 set_avail_alloc_bits(root->fs_info, cache->flags);
7341 if (btrfs_chunk_readonly(root, cache->key.objectid))
7342 set_block_group_readonly(cache);
7346 btrfs_free_path(path);
7350 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7351 struct btrfs_root *root, u64 bytes_used,
7352 u64 type, u64 chunk_objectid, u64 chunk_offset,
7356 struct btrfs_root *extent_root;
7357 struct btrfs_block_group_cache *cache;
7359 extent_root = root->fs_info->extent_root;
7361 root->fs_info->last_trans_log_full_commit = trans->transid;
7363 cache = kzalloc(sizeof(*cache), GFP_NOFS);
7367 cache->key.objectid = chunk_offset;
7368 cache->key.offset = size;
7369 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
7370 cache->sectorsize = root->sectorsize;
7373 * we only want to have 32k of ram per block group for keeping track
7374 * of free space, and if we pass 1/2 of that we want to start
7375 * converting things over to using bitmaps
7377 cache->extents_thresh = ((1024 * 32) / 2) /
7378 sizeof(struct btrfs_free_space);
7379 atomic_set(&cache->count, 1);
7380 spin_lock_init(&cache->lock);
7381 spin_lock_init(&cache->tree_lock);
7382 INIT_LIST_HEAD(&cache->list);
7383 INIT_LIST_HEAD(&cache->cluster_list);
7385 btrfs_set_block_group_used(&cache->item, bytes_used);
7386 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7387 cache->flags = type;
7388 btrfs_set_block_group_flags(&cache->item, type);
7390 cache->last_byte_to_unpin = (u64)-1;
7391 cache->cached = BTRFS_CACHE_FINISHED;
7392 exclude_super_stripes(root, cache);
7394 add_new_free_space(cache, root->fs_info, chunk_offset,
7395 chunk_offset + size);
7397 free_excluded_extents(root, cache);
7399 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7400 &cache->space_info);
7403 spin_lock(&cache->space_info->lock);
7404 cache->space_info->bytes_super += cache->bytes_super;
7405 spin_unlock(&cache->space_info->lock);
7407 down_write(&cache->space_info->groups_sem);
7408 list_add_tail(&cache->list, &cache->space_info->block_groups);
7409 up_write(&cache->space_info->groups_sem);
7411 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7414 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7415 sizeof(cache->item));
7418 set_avail_alloc_bits(extent_root->fs_info, type);
7423 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7424 struct btrfs_root *root, u64 group_start)
7426 struct btrfs_path *path;
7427 struct btrfs_block_group_cache *block_group;
7428 struct btrfs_free_cluster *cluster;
7429 struct btrfs_key key;
7432 root = root->fs_info->extent_root;
7434 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7435 BUG_ON(!block_group);
7436 BUG_ON(!block_group->ro);
7438 memcpy(&key, &block_group->key, sizeof(key));
7440 /* make sure this block group isn't part of an allocation cluster */
7441 cluster = &root->fs_info->data_alloc_cluster;
7442 spin_lock(&cluster->refill_lock);
7443 btrfs_return_cluster_to_free_space(block_group, cluster);
7444 spin_unlock(&cluster->refill_lock);
7447 * make sure this block group isn't part of a metadata
7448 * allocation cluster
7450 cluster = &root->fs_info->meta_alloc_cluster;
7451 spin_lock(&cluster->refill_lock);
7452 btrfs_return_cluster_to_free_space(block_group, cluster);
7453 spin_unlock(&cluster->refill_lock);
7455 path = btrfs_alloc_path();
7458 spin_lock(&root->fs_info->block_group_cache_lock);
7459 rb_erase(&block_group->cache_node,
7460 &root->fs_info->block_group_cache_tree);
7461 spin_unlock(&root->fs_info->block_group_cache_lock);
7463 down_write(&block_group->space_info->groups_sem);
7465 * we must use list_del_init so people can check to see if they
7466 * are still on the list after taking the semaphore
7468 list_del_init(&block_group->list);
7469 up_write(&block_group->space_info->groups_sem);
7471 if (block_group->cached == BTRFS_CACHE_STARTED)
7472 wait_block_group_cache_done(block_group);
7474 btrfs_remove_free_space_cache(block_group);
7476 spin_lock(&block_group->space_info->lock);
7477 block_group->space_info->total_bytes -= block_group->key.offset;
7478 block_group->space_info->bytes_readonly -= block_group->key.offset;
7479 spin_unlock(&block_group->space_info->lock);
7481 btrfs_clear_space_info_full(root->fs_info);
7483 btrfs_put_block_group(block_group);
7484 btrfs_put_block_group(block_group);
7486 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
7492 ret = btrfs_del_item(trans, root, path);
7494 btrfs_free_path(path);