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_reserved_extents(struct btrfs_root *root,
36 u64 bytenr, u64 num, int reserve);
37 static int update_block_group(struct btrfs_trans_handle *trans,
38 struct btrfs_root *root,
39 u64 bytenr, u64 num_bytes, int alloc,
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);
61 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
62 struct btrfs_root *extent_root, u64 alloc_bytes,
63 u64 flags, int force);
66 block_group_cache_done(struct btrfs_block_group_cache *cache)
69 return cache->cached == BTRFS_CACHE_FINISHED;
72 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
74 return (cache->flags & bits) == bits;
78 * this adds the block group to the fs_info rb tree for the block group
81 static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
82 struct btrfs_block_group_cache *block_group)
85 struct rb_node *parent = NULL;
86 struct btrfs_block_group_cache *cache;
88 spin_lock(&info->block_group_cache_lock);
89 p = &info->block_group_cache_tree.rb_node;
93 cache = rb_entry(parent, struct btrfs_block_group_cache,
95 if (block_group->key.objectid < cache->key.objectid) {
97 } else if (block_group->key.objectid > cache->key.objectid) {
100 spin_unlock(&info->block_group_cache_lock);
105 rb_link_node(&block_group->cache_node, parent, p);
106 rb_insert_color(&block_group->cache_node,
107 &info->block_group_cache_tree);
108 spin_unlock(&info->block_group_cache_lock);
114 * This will return the block group at or after bytenr if contains is 0, else
115 * it will return the block group that contains the bytenr
117 static struct btrfs_block_group_cache *
118 block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
121 struct btrfs_block_group_cache *cache, *ret = NULL;
125 spin_lock(&info->block_group_cache_lock);
126 n = info->block_group_cache_tree.rb_node;
129 cache = rb_entry(n, struct btrfs_block_group_cache,
131 end = cache->key.objectid + cache->key.offset - 1;
132 start = cache->key.objectid;
134 if (bytenr < start) {
135 if (!contains && (!ret || start < ret->key.objectid))
138 } else if (bytenr > start) {
139 if (contains && bytenr <= end) {
150 atomic_inc(&ret->count);
151 spin_unlock(&info->block_group_cache_lock);
157 * We always set EXTENT_LOCKED for the super mirror extents so we don't
158 * overwrite them, so those bits need to be unset. Also, if we are unmounting
159 * with pinned extents still sitting there because we had a block group caching,
160 * we need to clear those now, since we are done.
162 void btrfs_free_pinned_extents(struct btrfs_fs_info *info)
164 u64 start, end, last = 0;
168 ret = find_first_extent_bit(&info->pinned_extents, last,
170 EXTENT_LOCKED|EXTENT_DIRTY);
174 clear_extent_bits(&info->pinned_extents, start, end,
175 EXTENT_LOCKED|EXTENT_DIRTY, GFP_NOFS);
180 static int remove_sb_from_cache(struct btrfs_root *root,
181 struct btrfs_block_group_cache *cache)
183 struct btrfs_fs_info *fs_info = root->fs_info;
189 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
190 bytenr = btrfs_sb_offset(i);
191 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
192 cache->key.objectid, bytenr,
193 0, &logical, &nr, &stripe_len);
196 try_lock_extent(&fs_info->pinned_extents,
198 logical[nr] + stripe_len - 1, GFP_NOFS);
207 * this is only called by cache_block_group, since we could have freed extents
208 * we need to check the pinned_extents for any extents that can't be used yet
209 * since their free space will be released as soon as the transaction commits.
211 static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
212 struct btrfs_fs_info *info, u64 start, u64 end)
214 u64 extent_start, extent_end, size, total_added = 0;
217 while (start < end) {
218 ret = find_first_extent_bit(&info->pinned_extents, start,
219 &extent_start, &extent_end,
220 EXTENT_DIRTY|EXTENT_LOCKED);
224 if (extent_start == start) {
225 start = extent_end + 1;
226 } else if (extent_start > start && extent_start < end) {
227 size = extent_start - start;
229 ret = btrfs_add_free_space(block_group, start,
232 start = extent_end + 1;
241 ret = btrfs_add_free_space(block_group, start, size);
248 static int caching_kthread(void *data)
250 struct btrfs_block_group_cache *block_group = data;
251 struct btrfs_fs_info *fs_info = block_group->fs_info;
253 struct btrfs_path *path;
255 struct btrfs_key key;
256 struct extent_buffer *leaf;
262 path = btrfs_alloc_path();
266 atomic_inc(&block_group->space_info->caching_threads);
267 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
269 /* need to make sure the commit_root doesn't disappear */
270 down_read(&fs_info->extent_root->commit_root_sem);
273 * We don't want to deadlock with somebody trying to allocate a new
274 * extent for the extent root while also trying to search the extent
275 * root to add free space. So we skip locking and search the commit
276 * root, since its read-only
278 path->skip_locking = 1;
279 path->search_commit_root = 1;
284 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
285 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
291 if (block_group->fs_info->closing > 1) {
296 leaf = path->nodes[0];
297 slot = path->slots[0];
298 if (slot >= btrfs_header_nritems(leaf)) {
299 ret = btrfs_next_leaf(fs_info->extent_root, path);
305 if (need_resched()) {
306 btrfs_release_path(fs_info->extent_root, path);
307 up_read(&fs_info->extent_root->commit_root_sem);
314 btrfs_item_key_to_cpu(leaf, &key, slot);
315 if (key.objectid < block_group->key.objectid)
318 if (key.objectid >= block_group->key.objectid +
319 block_group->key.offset)
322 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
323 total_found += add_new_free_space(block_group,
326 last = key.objectid + key.offset;
329 if (total_found > (1024 * 1024 * 2)) {
331 wake_up(&block_group->caching_q);
338 total_found += add_new_free_space(block_group, fs_info, last,
339 block_group->key.objectid +
340 block_group->key.offset);
342 spin_lock(&block_group->lock);
343 block_group->cached = BTRFS_CACHE_FINISHED;
344 spin_unlock(&block_group->lock);
347 btrfs_free_path(path);
348 up_read(&fs_info->extent_root->commit_root_sem);
349 atomic_dec(&block_group->space_info->caching_threads);
350 wake_up(&block_group->caching_q);
355 static int cache_block_group(struct btrfs_block_group_cache *cache)
357 struct task_struct *tsk;
360 spin_lock(&cache->lock);
361 if (cache->cached != BTRFS_CACHE_NO) {
362 spin_unlock(&cache->lock);
365 cache->cached = BTRFS_CACHE_STARTED;
366 spin_unlock(&cache->lock);
368 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
369 cache->key.objectid);
372 printk(KERN_ERR "error running thread %d\n", ret);
380 * return the block group that starts at or after bytenr
382 static struct btrfs_block_group_cache *
383 btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
385 struct btrfs_block_group_cache *cache;
387 cache = block_group_cache_tree_search(info, bytenr, 0);
393 * return the block group that contains the given bytenr
395 struct btrfs_block_group_cache *btrfs_lookup_block_group(
396 struct btrfs_fs_info *info,
399 struct btrfs_block_group_cache *cache;
401 cache = block_group_cache_tree_search(info, bytenr, 1);
406 void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
408 if (atomic_dec_and_test(&cache->count))
412 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
415 struct list_head *head = &info->space_info;
416 struct btrfs_space_info *found;
419 list_for_each_entry_rcu(found, head, list) {
420 if (found->flags == flags) {
430 * after adding space to the filesystem, we need to clear the full flags
431 * on all the space infos.
433 void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
435 struct list_head *head = &info->space_info;
436 struct btrfs_space_info *found;
439 list_for_each_entry_rcu(found, head, list)
444 static u64 div_factor(u64 num, int factor)
453 u64 btrfs_find_block_group(struct btrfs_root *root,
454 u64 search_start, u64 search_hint, int owner)
456 struct btrfs_block_group_cache *cache;
458 u64 last = max(search_hint, search_start);
465 cache = btrfs_lookup_first_block_group(root->fs_info, last);
469 spin_lock(&cache->lock);
470 last = cache->key.objectid + cache->key.offset;
471 used = btrfs_block_group_used(&cache->item);
473 if ((full_search || !cache->ro) &&
474 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
475 if (used + cache->pinned + cache->reserved <
476 div_factor(cache->key.offset, factor)) {
477 group_start = cache->key.objectid;
478 spin_unlock(&cache->lock);
479 btrfs_put_block_group(cache);
483 spin_unlock(&cache->lock);
484 btrfs_put_block_group(cache);
492 if (!full_search && factor < 10) {
502 /* simple helper to search for an existing extent at a given offset */
503 int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
506 struct btrfs_key key;
507 struct btrfs_path *path;
509 path = btrfs_alloc_path();
511 key.objectid = start;
513 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
514 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
516 btrfs_free_path(path);
521 * Back reference rules. Back refs have three main goals:
523 * 1) differentiate between all holders of references to an extent so that
524 * when a reference is dropped we can make sure it was a valid reference
525 * before freeing the extent.
527 * 2) Provide enough information to quickly find the holders of an extent
528 * if we notice a given block is corrupted or bad.
530 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
531 * maintenance. This is actually the same as #2, but with a slightly
532 * different use case.
534 * There are two kinds of back refs. The implicit back refs is optimized
535 * for pointers in non-shared tree blocks. For a given pointer in a block,
536 * back refs of this kind provide information about the block's owner tree
537 * and the pointer's key. These information allow us to find the block by
538 * b-tree searching. The full back refs is for pointers in tree blocks not
539 * referenced by their owner trees. The location of tree block is recorded
540 * in the back refs. Actually the full back refs is generic, and can be
541 * used in all cases the implicit back refs is used. The major shortcoming
542 * of the full back refs is its overhead. Every time a tree block gets
543 * COWed, we have to update back refs entry for all pointers in it.
545 * For a newly allocated tree block, we use implicit back refs for
546 * pointers in it. This means most tree related operations only involve
547 * implicit back refs. For a tree block created in old transaction, the
548 * only way to drop a reference to it is COW it. So we can detect the
549 * event that tree block loses its owner tree's reference and do the
550 * back refs conversion.
552 * When a tree block is COW'd through a tree, there are four cases:
554 * The reference count of the block is one and the tree is the block's
555 * owner tree. Nothing to do in this case.
557 * The reference count of the block is one and the tree is not the
558 * block's owner tree. In this case, full back refs is used for pointers
559 * in the block. Remove these full back refs, add implicit back refs for
560 * every pointers in the new block.
562 * The reference count of the block is greater than one and the tree is
563 * the block's owner tree. In this case, implicit back refs is used for
564 * pointers in the block. Add full back refs for every pointers in the
565 * block, increase lower level extents' reference counts. The original
566 * implicit back refs are entailed to the new block.
568 * The reference count of the block is greater than one and the tree is
569 * not the block's owner tree. Add implicit back refs for every pointer in
570 * the new block, increase lower level extents' reference count.
572 * Back Reference Key composing:
574 * The key objectid corresponds to the first byte in the extent,
575 * The key type is used to differentiate between types of back refs.
576 * There are different meanings of the key offset for different types
579 * File extents can be referenced by:
581 * - multiple snapshots, subvolumes, or different generations in one subvol
582 * - different files inside a single subvolume
583 * - different offsets inside a file (bookend extents in file.c)
585 * The extent ref structure for the implicit back refs has fields for:
587 * - Objectid of the subvolume root
588 * - objectid of the file holding the reference
589 * - original offset in the file
590 * - how many bookend extents
592 * The key offset for the implicit back refs is hash of the first
595 * The extent ref structure for the full back refs has field for:
597 * - number of pointers in the tree leaf
599 * The key offset for the implicit back refs is the first byte of
602 * When a file extent is allocated, The implicit back refs is used.
603 * the fields are filled in:
605 * (root_key.objectid, inode objectid, offset in file, 1)
607 * When a file extent is removed file truncation, we find the
608 * corresponding implicit back refs and check the following fields:
610 * (btrfs_header_owner(leaf), inode objectid, offset in file)
612 * Btree extents can be referenced by:
614 * - Different subvolumes
616 * Both the implicit back refs and the full back refs for tree blocks
617 * only consist of key. The key offset for the implicit back refs is
618 * objectid of block's owner tree. The key offset for the full back refs
619 * is the first byte of parent block.
621 * When implicit back refs is used, information about the lowest key and
622 * level of the tree block are required. These information are stored in
623 * tree block info structure.
626 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
627 static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
628 struct btrfs_root *root,
629 struct btrfs_path *path,
630 u64 owner, u32 extra_size)
632 struct btrfs_extent_item *item;
633 struct btrfs_extent_item_v0 *ei0;
634 struct btrfs_extent_ref_v0 *ref0;
635 struct btrfs_tree_block_info *bi;
636 struct extent_buffer *leaf;
637 struct btrfs_key key;
638 struct btrfs_key found_key;
639 u32 new_size = sizeof(*item);
643 leaf = path->nodes[0];
644 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
646 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
647 ei0 = btrfs_item_ptr(leaf, path->slots[0],
648 struct btrfs_extent_item_v0);
649 refs = btrfs_extent_refs_v0(leaf, ei0);
651 if (owner == (u64)-1) {
653 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
654 ret = btrfs_next_leaf(root, path);
658 leaf = path->nodes[0];
660 btrfs_item_key_to_cpu(leaf, &found_key,
662 BUG_ON(key.objectid != found_key.objectid);
663 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
667 ref0 = btrfs_item_ptr(leaf, path->slots[0],
668 struct btrfs_extent_ref_v0);
669 owner = btrfs_ref_objectid_v0(leaf, ref0);
673 btrfs_release_path(root, path);
675 if (owner < BTRFS_FIRST_FREE_OBJECTID)
676 new_size += sizeof(*bi);
678 new_size -= sizeof(*ei0);
679 ret = btrfs_search_slot(trans, root, &key, path,
680 new_size + extra_size, 1);
685 ret = btrfs_extend_item(trans, root, path, new_size);
688 leaf = path->nodes[0];
689 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
690 btrfs_set_extent_refs(leaf, item, refs);
691 /* FIXME: get real generation */
692 btrfs_set_extent_generation(leaf, item, 0);
693 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
694 btrfs_set_extent_flags(leaf, item,
695 BTRFS_EXTENT_FLAG_TREE_BLOCK |
696 BTRFS_BLOCK_FLAG_FULL_BACKREF);
697 bi = (struct btrfs_tree_block_info *)(item + 1);
698 /* FIXME: get first key of the block */
699 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
700 btrfs_set_tree_block_level(leaf, bi, (int)owner);
702 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
704 btrfs_mark_buffer_dirty(leaf);
709 static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
711 u32 high_crc = ~(u32)0;
712 u32 low_crc = ~(u32)0;
715 lenum = cpu_to_le64(root_objectid);
716 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
717 lenum = cpu_to_le64(owner);
718 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
719 lenum = cpu_to_le64(offset);
720 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
722 return ((u64)high_crc << 31) ^ (u64)low_crc;
725 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
726 struct btrfs_extent_data_ref *ref)
728 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
729 btrfs_extent_data_ref_objectid(leaf, ref),
730 btrfs_extent_data_ref_offset(leaf, ref));
733 static int match_extent_data_ref(struct extent_buffer *leaf,
734 struct btrfs_extent_data_ref *ref,
735 u64 root_objectid, u64 owner, u64 offset)
737 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
738 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
739 btrfs_extent_data_ref_offset(leaf, ref) != offset)
744 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
745 struct btrfs_root *root,
746 struct btrfs_path *path,
747 u64 bytenr, u64 parent,
749 u64 owner, u64 offset)
751 struct btrfs_key key;
752 struct btrfs_extent_data_ref *ref;
753 struct extent_buffer *leaf;
759 key.objectid = bytenr;
761 key.type = BTRFS_SHARED_DATA_REF_KEY;
764 key.type = BTRFS_EXTENT_DATA_REF_KEY;
765 key.offset = hash_extent_data_ref(root_objectid,
770 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
779 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
780 key.type = BTRFS_EXTENT_REF_V0_KEY;
781 btrfs_release_path(root, path);
782 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
793 leaf = path->nodes[0];
794 nritems = btrfs_header_nritems(leaf);
796 if (path->slots[0] >= nritems) {
797 ret = btrfs_next_leaf(root, path);
803 leaf = path->nodes[0];
804 nritems = btrfs_header_nritems(leaf);
808 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
809 if (key.objectid != bytenr ||
810 key.type != BTRFS_EXTENT_DATA_REF_KEY)
813 ref = btrfs_item_ptr(leaf, path->slots[0],
814 struct btrfs_extent_data_ref);
816 if (match_extent_data_ref(leaf, ref, root_objectid,
819 btrfs_release_path(root, path);
831 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
832 struct btrfs_root *root,
833 struct btrfs_path *path,
834 u64 bytenr, u64 parent,
835 u64 root_objectid, u64 owner,
836 u64 offset, int refs_to_add)
838 struct btrfs_key key;
839 struct extent_buffer *leaf;
844 key.objectid = bytenr;
846 key.type = BTRFS_SHARED_DATA_REF_KEY;
848 size = sizeof(struct btrfs_shared_data_ref);
850 key.type = BTRFS_EXTENT_DATA_REF_KEY;
851 key.offset = hash_extent_data_ref(root_objectid,
853 size = sizeof(struct btrfs_extent_data_ref);
856 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
857 if (ret && ret != -EEXIST)
860 leaf = path->nodes[0];
862 struct btrfs_shared_data_ref *ref;
863 ref = btrfs_item_ptr(leaf, path->slots[0],
864 struct btrfs_shared_data_ref);
866 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
868 num_refs = btrfs_shared_data_ref_count(leaf, ref);
869 num_refs += refs_to_add;
870 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
873 struct btrfs_extent_data_ref *ref;
874 while (ret == -EEXIST) {
875 ref = btrfs_item_ptr(leaf, path->slots[0],
876 struct btrfs_extent_data_ref);
877 if (match_extent_data_ref(leaf, ref, root_objectid,
880 btrfs_release_path(root, path);
882 ret = btrfs_insert_empty_item(trans, root, path, &key,
884 if (ret && ret != -EEXIST)
887 leaf = path->nodes[0];
889 ref = btrfs_item_ptr(leaf, path->slots[0],
890 struct btrfs_extent_data_ref);
892 btrfs_set_extent_data_ref_root(leaf, ref,
894 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
895 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
896 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
898 num_refs = btrfs_extent_data_ref_count(leaf, ref);
899 num_refs += refs_to_add;
900 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
903 btrfs_mark_buffer_dirty(leaf);
906 btrfs_release_path(root, path);
910 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
911 struct btrfs_root *root,
912 struct btrfs_path *path,
915 struct btrfs_key key;
916 struct btrfs_extent_data_ref *ref1 = NULL;
917 struct btrfs_shared_data_ref *ref2 = NULL;
918 struct extent_buffer *leaf;
922 leaf = path->nodes[0];
923 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
925 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
926 ref1 = btrfs_item_ptr(leaf, path->slots[0],
927 struct btrfs_extent_data_ref);
928 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
929 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
930 ref2 = btrfs_item_ptr(leaf, path->slots[0],
931 struct btrfs_shared_data_ref);
932 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
933 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
934 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
935 struct btrfs_extent_ref_v0 *ref0;
936 ref0 = btrfs_item_ptr(leaf, path->slots[0],
937 struct btrfs_extent_ref_v0);
938 num_refs = btrfs_ref_count_v0(leaf, ref0);
944 BUG_ON(num_refs < refs_to_drop);
945 num_refs -= refs_to_drop;
948 ret = btrfs_del_item(trans, root, path);
950 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
951 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
952 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
953 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
954 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
956 struct btrfs_extent_ref_v0 *ref0;
957 ref0 = btrfs_item_ptr(leaf, path->slots[0],
958 struct btrfs_extent_ref_v0);
959 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
962 btrfs_mark_buffer_dirty(leaf);
967 static noinline u32 extent_data_ref_count(struct btrfs_root *root,
968 struct btrfs_path *path,
969 struct btrfs_extent_inline_ref *iref)
971 struct btrfs_key key;
972 struct extent_buffer *leaf;
973 struct btrfs_extent_data_ref *ref1;
974 struct btrfs_shared_data_ref *ref2;
977 leaf = path->nodes[0];
978 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
980 if (btrfs_extent_inline_ref_type(leaf, iref) ==
981 BTRFS_EXTENT_DATA_REF_KEY) {
982 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
983 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
985 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
986 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
988 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
989 ref1 = btrfs_item_ptr(leaf, path->slots[0],
990 struct btrfs_extent_data_ref);
991 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
992 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
993 ref2 = btrfs_item_ptr(leaf, path->slots[0],
994 struct btrfs_shared_data_ref);
995 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
996 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
997 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
998 struct btrfs_extent_ref_v0 *ref0;
999 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1000 struct btrfs_extent_ref_v0);
1001 num_refs = btrfs_ref_count_v0(leaf, ref0);
1009 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1010 struct btrfs_root *root,
1011 struct btrfs_path *path,
1012 u64 bytenr, u64 parent,
1015 struct btrfs_key key;
1018 key.objectid = bytenr;
1020 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1021 key.offset = parent;
1023 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1024 key.offset = root_objectid;
1027 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1030 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1031 if (ret == -ENOENT && parent) {
1032 btrfs_release_path(root, path);
1033 key.type = BTRFS_EXTENT_REF_V0_KEY;
1034 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1042 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1043 struct btrfs_root *root,
1044 struct btrfs_path *path,
1045 u64 bytenr, u64 parent,
1048 struct btrfs_key key;
1051 key.objectid = bytenr;
1053 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1054 key.offset = parent;
1056 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1057 key.offset = root_objectid;
1060 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1061 btrfs_release_path(root, path);
1065 static inline int extent_ref_type(u64 parent, u64 owner)
1068 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1070 type = BTRFS_SHARED_BLOCK_REF_KEY;
1072 type = BTRFS_TREE_BLOCK_REF_KEY;
1075 type = BTRFS_SHARED_DATA_REF_KEY;
1077 type = BTRFS_EXTENT_DATA_REF_KEY;
1082 static int find_next_key(struct btrfs_path *path, int level,
1083 struct btrfs_key *key)
1086 for (; level < BTRFS_MAX_LEVEL; level++) {
1087 if (!path->nodes[level])
1089 if (path->slots[level] + 1 >=
1090 btrfs_header_nritems(path->nodes[level]))
1093 btrfs_item_key_to_cpu(path->nodes[level], key,
1094 path->slots[level] + 1);
1096 btrfs_node_key_to_cpu(path->nodes[level], key,
1097 path->slots[level] + 1);
1104 * look for inline back ref. if back ref is found, *ref_ret is set
1105 * to the address of inline back ref, and 0 is returned.
1107 * if back ref isn't found, *ref_ret is set to the address where it
1108 * should be inserted, and -ENOENT is returned.
1110 * if insert is true and there are too many inline back refs, the path
1111 * points to the extent item, and -EAGAIN is returned.
1113 * NOTE: inline back refs are ordered in the same way that back ref
1114 * items in the tree are ordered.
1116 static noinline_for_stack
1117 int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1118 struct btrfs_root *root,
1119 struct btrfs_path *path,
1120 struct btrfs_extent_inline_ref **ref_ret,
1121 u64 bytenr, u64 num_bytes,
1122 u64 parent, u64 root_objectid,
1123 u64 owner, u64 offset, int insert)
1125 struct btrfs_key key;
1126 struct extent_buffer *leaf;
1127 struct btrfs_extent_item *ei;
1128 struct btrfs_extent_inline_ref *iref;
1139 key.objectid = bytenr;
1140 key.type = BTRFS_EXTENT_ITEM_KEY;
1141 key.offset = num_bytes;
1143 want = extent_ref_type(parent, owner);
1145 extra_size = btrfs_extent_inline_ref_size(want);
1146 path->keep_locks = 1;
1149 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1156 leaf = path->nodes[0];
1157 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1158 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1159 if (item_size < sizeof(*ei)) {
1164 ret = convert_extent_item_v0(trans, root, path, owner,
1170 leaf = path->nodes[0];
1171 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1174 BUG_ON(item_size < sizeof(*ei));
1176 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1177 flags = btrfs_extent_flags(leaf, ei);
1179 ptr = (unsigned long)(ei + 1);
1180 end = (unsigned long)ei + item_size;
1182 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1183 ptr += sizeof(struct btrfs_tree_block_info);
1186 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1195 iref = (struct btrfs_extent_inline_ref *)ptr;
1196 type = btrfs_extent_inline_ref_type(leaf, iref);
1200 ptr += btrfs_extent_inline_ref_size(type);
1204 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1205 struct btrfs_extent_data_ref *dref;
1206 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1207 if (match_extent_data_ref(leaf, dref, root_objectid,
1212 if (hash_extent_data_ref_item(leaf, dref) <
1213 hash_extent_data_ref(root_objectid, owner, offset))
1217 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1219 if (parent == ref_offset) {
1223 if (ref_offset < parent)
1226 if (root_objectid == ref_offset) {
1230 if (ref_offset < root_objectid)
1234 ptr += btrfs_extent_inline_ref_size(type);
1236 if (err == -ENOENT && insert) {
1237 if (item_size + extra_size >=
1238 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1243 * To add new inline back ref, we have to make sure
1244 * there is no corresponding back ref item.
1245 * For simplicity, we just do not add new inline back
1246 * ref if there is any kind of item for this block
1248 if (find_next_key(path, 0, &key) == 0 &&
1249 key.objectid == bytenr &&
1250 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
1255 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1258 path->keep_locks = 0;
1259 btrfs_unlock_up_safe(path, 1);
1265 * helper to add new inline back ref
1267 static noinline_for_stack
1268 int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1269 struct btrfs_root *root,
1270 struct btrfs_path *path,
1271 struct btrfs_extent_inline_ref *iref,
1272 u64 parent, u64 root_objectid,
1273 u64 owner, u64 offset, int refs_to_add,
1274 struct btrfs_delayed_extent_op *extent_op)
1276 struct extent_buffer *leaf;
1277 struct btrfs_extent_item *ei;
1280 unsigned long item_offset;
1286 leaf = path->nodes[0];
1287 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1288 item_offset = (unsigned long)iref - (unsigned long)ei;
1290 type = extent_ref_type(parent, owner);
1291 size = btrfs_extent_inline_ref_size(type);
1293 ret = btrfs_extend_item(trans, root, path, size);
1296 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1297 refs = btrfs_extent_refs(leaf, ei);
1298 refs += refs_to_add;
1299 btrfs_set_extent_refs(leaf, ei, refs);
1301 __run_delayed_extent_op(extent_op, leaf, ei);
1303 ptr = (unsigned long)ei + item_offset;
1304 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1305 if (ptr < end - size)
1306 memmove_extent_buffer(leaf, ptr + size, ptr,
1309 iref = (struct btrfs_extent_inline_ref *)ptr;
1310 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1311 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1312 struct btrfs_extent_data_ref *dref;
1313 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1314 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1315 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1316 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1317 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1318 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1319 struct btrfs_shared_data_ref *sref;
1320 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1321 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1322 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1323 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1324 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1326 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1328 btrfs_mark_buffer_dirty(leaf);
1332 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1333 struct btrfs_root *root,
1334 struct btrfs_path *path,
1335 struct btrfs_extent_inline_ref **ref_ret,
1336 u64 bytenr, u64 num_bytes, u64 parent,
1337 u64 root_objectid, u64 owner, u64 offset)
1341 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1342 bytenr, num_bytes, parent,
1343 root_objectid, owner, offset, 0);
1347 btrfs_release_path(root, path);
1350 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1351 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1354 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1355 root_objectid, owner, offset);
1361 * helper to update/remove inline back ref
1363 static noinline_for_stack
1364 int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1365 struct btrfs_root *root,
1366 struct btrfs_path *path,
1367 struct btrfs_extent_inline_ref *iref,
1369 struct btrfs_delayed_extent_op *extent_op)
1371 struct extent_buffer *leaf;
1372 struct btrfs_extent_item *ei;
1373 struct btrfs_extent_data_ref *dref = NULL;
1374 struct btrfs_shared_data_ref *sref = NULL;
1383 leaf = path->nodes[0];
1384 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1385 refs = btrfs_extent_refs(leaf, ei);
1386 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1387 refs += refs_to_mod;
1388 btrfs_set_extent_refs(leaf, ei, refs);
1390 __run_delayed_extent_op(extent_op, leaf, ei);
1392 type = btrfs_extent_inline_ref_type(leaf, iref);
1394 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1395 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1396 refs = btrfs_extent_data_ref_count(leaf, dref);
1397 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1398 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1399 refs = btrfs_shared_data_ref_count(leaf, sref);
1402 BUG_ON(refs_to_mod != -1);
1405 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1406 refs += refs_to_mod;
1409 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1410 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1412 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1414 size = btrfs_extent_inline_ref_size(type);
1415 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1416 ptr = (unsigned long)iref;
1417 end = (unsigned long)ei + item_size;
1418 if (ptr + size < end)
1419 memmove_extent_buffer(leaf, ptr, ptr + size,
1422 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1425 btrfs_mark_buffer_dirty(leaf);
1429 static noinline_for_stack
1430 int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1431 struct btrfs_root *root,
1432 struct btrfs_path *path,
1433 u64 bytenr, u64 num_bytes, u64 parent,
1434 u64 root_objectid, u64 owner,
1435 u64 offset, int refs_to_add,
1436 struct btrfs_delayed_extent_op *extent_op)
1438 struct btrfs_extent_inline_ref *iref;
1441 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1442 bytenr, num_bytes, parent,
1443 root_objectid, owner, offset, 1);
1445 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1446 ret = update_inline_extent_backref(trans, root, path, iref,
1447 refs_to_add, extent_op);
1448 } else if (ret == -ENOENT) {
1449 ret = setup_inline_extent_backref(trans, root, path, iref,
1450 parent, root_objectid,
1451 owner, offset, refs_to_add,
1457 static int insert_extent_backref(struct btrfs_trans_handle *trans,
1458 struct btrfs_root *root,
1459 struct btrfs_path *path,
1460 u64 bytenr, u64 parent, u64 root_objectid,
1461 u64 owner, u64 offset, int refs_to_add)
1464 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1465 BUG_ON(refs_to_add != 1);
1466 ret = insert_tree_block_ref(trans, root, path, bytenr,
1467 parent, root_objectid);
1469 ret = insert_extent_data_ref(trans, root, path, bytenr,
1470 parent, root_objectid,
1471 owner, offset, refs_to_add);
1476 static int remove_extent_backref(struct btrfs_trans_handle *trans,
1477 struct btrfs_root *root,
1478 struct btrfs_path *path,
1479 struct btrfs_extent_inline_ref *iref,
1480 int refs_to_drop, int is_data)
1484 BUG_ON(!is_data && refs_to_drop != 1);
1486 ret = update_inline_extent_backref(trans, root, path, iref,
1487 -refs_to_drop, NULL);
1488 } else if (is_data) {
1489 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1491 ret = btrfs_del_item(trans, root, path);
1496 #ifdef BIO_RW_DISCARD
1497 static void btrfs_issue_discard(struct block_device *bdev,
1500 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
1504 static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1507 #ifdef BIO_RW_DISCARD
1509 u64 map_length = num_bytes;
1510 struct btrfs_multi_bio *multi = NULL;
1512 /* Tell the block device(s) that the sectors can be discarded */
1513 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1514 bytenr, &map_length, &multi, 0);
1516 struct btrfs_bio_stripe *stripe = multi->stripes;
1519 if (map_length > num_bytes)
1520 map_length = num_bytes;
1522 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1523 btrfs_issue_discard(stripe->dev->bdev,
1536 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1537 struct btrfs_root *root,
1538 u64 bytenr, u64 num_bytes, u64 parent,
1539 u64 root_objectid, u64 owner, u64 offset)
1542 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1543 root_objectid == BTRFS_TREE_LOG_OBJECTID);
1545 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1546 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1547 parent, root_objectid, (int)owner,
1548 BTRFS_ADD_DELAYED_REF, NULL);
1550 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1551 parent, root_objectid, owner, offset,
1552 BTRFS_ADD_DELAYED_REF, NULL);
1557 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1558 struct btrfs_root *root,
1559 u64 bytenr, u64 num_bytes,
1560 u64 parent, u64 root_objectid,
1561 u64 owner, u64 offset, int refs_to_add,
1562 struct btrfs_delayed_extent_op *extent_op)
1564 struct btrfs_path *path;
1565 struct extent_buffer *leaf;
1566 struct btrfs_extent_item *item;
1571 path = btrfs_alloc_path();
1576 path->leave_spinning = 1;
1577 /* this will setup the path even if it fails to insert the back ref */
1578 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1579 path, bytenr, num_bytes, parent,
1580 root_objectid, owner, offset,
1581 refs_to_add, extent_op);
1585 if (ret != -EAGAIN) {
1590 leaf = path->nodes[0];
1591 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1592 refs = btrfs_extent_refs(leaf, item);
1593 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1595 __run_delayed_extent_op(extent_op, leaf, item);
1597 btrfs_mark_buffer_dirty(leaf);
1598 btrfs_release_path(root->fs_info->extent_root, path);
1601 path->leave_spinning = 1;
1603 /* now insert the actual backref */
1604 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1605 path, bytenr, parent, root_objectid,
1606 owner, offset, refs_to_add);
1609 btrfs_free_path(path);
1613 static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1614 struct btrfs_root *root,
1615 struct btrfs_delayed_ref_node *node,
1616 struct btrfs_delayed_extent_op *extent_op,
1617 int insert_reserved)
1620 struct btrfs_delayed_data_ref *ref;
1621 struct btrfs_key ins;
1626 ins.objectid = node->bytenr;
1627 ins.offset = node->num_bytes;
1628 ins.type = BTRFS_EXTENT_ITEM_KEY;
1630 ref = btrfs_delayed_node_to_data_ref(node);
1631 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1632 parent = ref->parent;
1634 ref_root = ref->root;
1636 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1638 BUG_ON(extent_op->update_key);
1639 flags |= extent_op->flags_to_set;
1641 ret = alloc_reserved_file_extent(trans, root,
1642 parent, ref_root, flags,
1643 ref->objectid, ref->offset,
1644 &ins, node->ref_mod);
1645 update_reserved_extents(root, ins.objectid, ins.offset, 0);
1646 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1647 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1648 node->num_bytes, parent,
1649 ref_root, ref->objectid,
1650 ref->offset, node->ref_mod,
1652 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1653 ret = __btrfs_free_extent(trans, root, node->bytenr,
1654 node->num_bytes, parent,
1655 ref_root, ref->objectid,
1656 ref->offset, node->ref_mod,
1664 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1665 struct extent_buffer *leaf,
1666 struct btrfs_extent_item *ei)
1668 u64 flags = btrfs_extent_flags(leaf, ei);
1669 if (extent_op->update_flags) {
1670 flags |= extent_op->flags_to_set;
1671 btrfs_set_extent_flags(leaf, ei, flags);
1674 if (extent_op->update_key) {
1675 struct btrfs_tree_block_info *bi;
1676 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1677 bi = (struct btrfs_tree_block_info *)(ei + 1);
1678 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1682 static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1683 struct btrfs_root *root,
1684 struct btrfs_delayed_ref_node *node,
1685 struct btrfs_delayed_extent_op *extent_op)
1687 struct btrfs_key key;
1688 struct btrfs_path *path;
1689 struct btrfs_extent_item *ei;
1690 struct extent_buffer *leaf;
1695 path = btrfs_alloc_path();
1699 key.objectid = node->bytenr;
1700 key.type = BTRFS_EXTENT_ITEM_KEY;
1701 key.offset = node->num_bytes;
1704 path->leave_spinning = 1;
1705 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1716 leaf = path->nodes[0];
1717 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1718 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1719 if (item_size < sizeof(*ei)) {
1720 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1726 leaf = path->nodes[0];
1727 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1730 BUG_ON(item_size < sizeof(*ei));
1731 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1732 __run_delayed_extent_op(extent_op, leaf, ei);
1734 btrfs_mark_buffer_dirty(leaf);
1736 btrfs_free_path(path);
1740 static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1741 struct btrfs_root *root,
1742 struct btrfs_delayed_ref_node *node,
1743 struct btrfs_delayed_extent_op *extent_op,
1744 int insert_reserved)
1747 struct btrfs_delayed_tree_ref *ref;
1748 struct btrfs_key ins;
1752 ins.objectid = node->bytenr;
1753 ins.offset = node->num_bytes;
1754 ins.type = BTRFS_EXTENT_ITEM_KEY;
1756 ref = btrfs_delayed_node_to_tree_ref(node);
1757 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1758 parent = ref->parent;
1760 ref_root = ref->root;
1762 BUG_ON(node->ref_mod != 1);
1763 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1764 BUG_ON(!extent_op || !extent_op->update_flags ||
1765 !extent_op->update_key);
1766 ret = alloc_reserved_tree_block(trans, root,
1768 extent_op->flags_to_set,
1771 update_reserved_extents(root, ins.objectid, ins.offset, 0);
1772 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1773 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1774 node->num_bytes, parent, ref_root,
1775 ref->level, 0, 1, extent_op);
1776 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1777 ret = __btrfs_free_extent(trans, root, node->bytenr,
1778 node->num_bytes, parent, ref_root,
1779 ref->level, 0, 1, extent_op);
1787 /* helper function to actually process a single delayed ref entry */
1788 static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
1789 struct btrfs_root *root,
1790 struct btrfs_delayed_ref_node *node,
1791 struct btrfs_delayed_extent_op *extent_op,
1792 int insert_reserved)
1795 if (btrfs_delayed_ref_is_head(node)) {
1796 struct btrfs_delayed_ref_head *head;
1798 * we've hit the end of the chain and we were supposed
1799 * to insert this extent into the tree. But, it got
1800 * deleted before we ever needed to insert it, so all
1801 * we have to do is clean up the accounting
1804 head = btrfs_delayed_node_to_head(node);
1805 if (insert_reserved) {
1806 if (head->is_data) {
1807 ret = btrfs_del_csums(trans, root,
1812 btrfs_update_pinned_extents(root, node->bytenr,
1813 node->num_bytes, 1);
1814 update_reserved_extents(root, node->bytenr,
1815 node->num_bytes, 0);
1817 mutex_unlock(&head->mutex);
1821 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1822 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1823 ret = run_delayed_tree_ref(trans, root, node, extent_op,
1825 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1826 node->type == BTRFS_SHARED_DATA_REF_KEY)
1827 ret = run_delayed_data_ref(trans, root, node, extent_op,
1834 static noinline struct btrfs_delayed_ref_node *
1835 select_delayed_ref(struct btrfs_delayed_ref_head *head)
1837 struct rb_node *node;
1838 struct btrfs_delayed_ref_node *ref;
1839 int action = BTRFS_ADD_DELAYED_REF;
1842 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
1843 * this prevents ref count from going down to zero when
1844 * there still are pending delayed ref.
1846 node = rb_prev(&head->node.rb_node);
1850 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1852 if (ref->bytenr != head->node.bytenr)
1854 if (ref->action == action)
1856 node = rb_prev(node);
1858 if (action == BTRFS_ADD_DELAYED_REF) {
1859 action = BTRFS_DROP_DELAYED_REF;
1865 static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
1866 struct btrfs_root *root,
1867 struct list_head *cluster)
1869 struct btrfs_delayed_ref_root *delayed_refs;
1870 struct btrfs_delayed_ref_node *ref;
1871 struct btrfs_delayed_ref_head *locked_ref = NULL;
1872 struct btrfs_delayed_extent_op *extent_op;
1875 int must_insert_reserved = 0;
1877 delayed_refs = &trans->transaction->delayed_refs;
1880 /* pick a new head ref from the cluster list */
1881 if (list_empty(cluster))
1884 locked_ref = list_entry(cluster->next,
1885 struct btrfs_delayed_ref_head, cluster);
1887 /* grab the lock that says we are going to process
1888 * all the refs for this head */
1889 ret = btrfs_delayed_ref_lock(trans, locked_ref);
1892 * we may have dropped the spin lock to get the head
1893 * mutex lock, and that might have given someone else
1894 * time to free the head. If that's true, it has been
1895 * removed from our list and we can move on.
1897 if (ret == -EAGAIN) {
1905 * record the must insert reserved flag before we
1906 * drop the spin lock.
1908 must_insert_reserved = locked_ref->must_insert_reserved;
1909 locked_ref->must_insert_reserved = 0;
1911 extent_op = locked_ref->extent_op;
1912 locked_ref->extent_op = NULL;
1915 * locked_ref is the head node, so we have to go one
1916 * node back for any delayed ref updates
1918 ref = select_delayed_ref(locked_ref);
1920 /* All delayed refs have been processed, Go ahead
1921 * and send the head node to run_one_delayed_ref,
1922 * so that any accounting fixes can happen
1924 ref = &locked_ref->node;
1926 if (extent_op && must_insert_reserved) {
1932 spin_unlock(&delayed_refs->lock);
1934 ret = run_delayed_extent_op(trans, root,
1940 spin_lock(&delayed_refs->lock);
1944 list_del_init(&locked_ref->cluster);
1949 rb_erase(&ref->rb_node, &delayed_refs->root);
1950 delayed_refs->num_entries--;
1952 spin_unlock(&delayed_refs->lock);
1954 ret = run_one_delayed_ref(trans, root, ref, extent_op,
1955 must_insert_reserved);
1958 btrfs_put_delayed_ref(ref);
1963 spin_lock(&delayed_refs->lock);
1969 * this starts processing the delayed reference count updates and
1970 * extent insertions we have queued up so far. count can be
1971 * 0, which means to process everything in the tree at the start
1972 * of the run (but not newly added entries), or it can be some target
1973 * number you'd like to process.
1975 int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
1976 struct btrfs_root *root, unsigned long count)
1978 struct rb_node *node;
1979 struct btrfs_delayed_ref_root *delayed_refs;
1980 struct btrfs_delayed_ref_node *ref;
1981 struct list_head cluster;
1983 int run_all = count == (unsigned long)-1;
1986 if (root == root->fs_info->extent_root)
1987 root = root->fs_info->tree_root;
1989 delayed_refs = &trans->transaction->delayed_refs;
1990 INIT_LIST_HEAD(&cluster);
1992 spin_lock(&delayed_refs->lock);
1994 count = delayed_refs->num_entries * 2;
1998 if (!(run_all || run_most) &&
1999 delayed_refs->num_heads_ready < 64)
2003 * go find something we can process in the rbtree. We start at
2004 * the beginning of the tree, and then build a cluster
2005 * of refs to process starting at the first one we are able to
2008 ret = btrfs_find_ref_cluster(trans, &cluster,
2009 delayed_refs->run_delayed_start);
2013 ret = run_clustered_refs(trans, root, &cluster);
2016 count -= min_t(unsigned long, ret, count);
2023 node = rb_first(&delayed_refs->root);
2026 count = (unsigned long)-1;
2029 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2031 if (btrfs_delayed_ref_is_head(ref)) {
2032 struct btrfs_delayed_ref_head *head;
2034 head = btrfs_delayed_node_to_head(ref);
2035 atomic_inc(&ref->refs);
2037 spin_unlock(&delayed_refs->lock);
2038 mutex_lock(&head->mutex);
2039 mutex_unlock(&head->mutex);
2041 btrfs_put_delayed_ref(ref);
2045 node = rb_next(node);
2047 spin_unlock(&delayed_refs->lock);
2048 schedule_timeout(1);
2052 spin_unlock(&delayed_refs->lock);
2056 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2057 struct btrfs_root *root,
2058 u64 bytenr, u64 num_bytes, u64 flags,
2061 struct btrfs_delayed_extent_op *extent_op;
2064 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2068 extent_op->flags_to_set = flags;
2069 extent_op->update_flags = 1;
2070 extent_op->update_key = 0;
2071 extent_op->is_data = is_data ? 1 : 0;
2073 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2079 static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2080 struct btrfs_root *root,
2081 struct btrfs_path *path,
2082 u64 objectid, u64 offset, u64 bytenr)
2084 struct btrfs_delayed_ref_head *head;
2085 struct btrfs_delayed_ref_node *ref;
2086 struct btrfs_delayed_data_ref *data_ref;
2087 struct btrfs_delayed_ref_root *delayed_refs;
2088 struct rb_node *node;
2092 delayed_refs = &trans->transaction->delayed_refs;
2093 spin_lock(&delayed_refs->lock);
2094 head = btrfs_find_delayed_ref_head(trans, bytenr);
2098 if (!mutex_trylock(&head->mutex)) {
2099 atomic_inc(&head->node.refs);
2100 spin_unlock(&delayed_refs->lock);
2102 btrfs_release_path(root->fs_info->extent_root, path);
2104 mutex_lock(&head->mutex);
2105 mutex_unlock(&head->mutex);
2106 btrfs_put_delayed_ref(&head->node);
2110 node = rb_prev(&head->node.rb_node);
2114 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2116 if (ref->bytenr != bytenr)
2120 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2123 data_ref = btrfs_delayed_node_to_data_ref(ref);
2125 node = rb_prev(node);
2127 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2128 if (ref->bytenr == bytenr)
2132 if (data_ref->root != root->root_key.objectid ||
2133 data_ref->objectid != objectid || data_ref->offset != offset)
2138 mutex_unlock(&head->mutex);
2140 spin_unlock(&delayed_refs->lock);
2144 static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2145 struct btrfs_root *root,
2146 struct btrfs_path *path,
2147 u64 objectid, u64 offset, u64 bytenr)
2149 struct btrfs_root *extent_root = root->fs_info->extent_root;
2150 struct extent_buffer *leaf;
2151 struct btrfs_extent_data_ref *ref;
2152 struct btrfs_extent_inline_ref *iref;
2153 struct btrfs_extent_item *ei;
2154 struct btrfs_key key;
2158 key.objectid = bytenr;
2159 key.offset = (u64)-1;
2160 key.type = BTRFS_EXTENT_ITEM_KEY;
2162 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2168 if (path->slots[0] == 0)
2172 leaf = path->nodes[0];
2173 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2175 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
2179 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2180 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2181 if (item_size < sizeof(*ei)) {
2182 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2186 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2188 if (item_size != sizeof(*ei) +
2189 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2192 if (btrfs_extent_generation(leaf, ei) <=
2193 btrfs_root_last_snapshot(&root->root_item))
2196 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2197 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2198 BTRFS_EXTENT_DATA_REF_KEY)
2201 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2202 if (btrfs_extent_refs(leaf, ei) !=
2203 btrfs_extent_data_ref_count(leaf, ref) ||
2204 btrfs_extent_data_ref_root(leaf, ref) !=
2205 root->root_key.objectid ||
2206 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2207 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2215 int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2216 struct btrfs_root *root,
2217 u64 objectid, u64 offset, u64 bytenr)
2219 struct btrfs_path *path;
2223 path = btrfs_alloc_path();
2228 ret = check_committed_ref(trans, root, path, objectid,
2230 if (ret && ret != -ENOENT)
2233 ret2 = check_delayed_ref(trans, root, path, objectid,
2235 } while (ret2 == -EAGAIN);
2237 if (ret2 && ret2 != -ENOENT) {
2242 if (ret != -ENOENT || ret2 != -ENOENT)
2245 btrfs_free_path(path);
2250 int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2251 struct extent_buffer *buf, u32 nr_extents)
2253 struct btrfs_key key;
2254 struct btrfs_file_extent_item *fi;
2262 if (!root->ref_cows)
2265 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2267 root_gen = root->root_key.offset;
2270 root_gen = trans->transid - 1;
2273 level = btrfs_header_level(buf);
2274 nritems = btrfs_header_nritems(buf);
2277 struct btrfs_leaf_ref *ref;
2278 struct btrfs_extent_info *info;
2280 ref = btrfs_alloc_leaf_ref(root, nr_extents);
2286 ref->root_gen = root_gen;
2287 ref->bytenr = buf->start;
2288 ref->owner = btrfs_header_owner(buf);
2289 ref->generation = btrfs_header_generation(buf);
2290 ref->nritems = nr_extents;
2291 info = ref->extents;
2293 for (i = 0; nr_extents > 0 && i < nritems; i++) {
2295 btrfs_item_key_to_cpu(buf, &key, i);
2296 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2298 fi = btrfs_item_ptr(buf, i,
2299 struct btrfs_file_extent_item);
2300 if (btrfs_file_extent_type(buf, fi) ==
2301 BTRFS_FILE_EXTENT_INLINE)
2303 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2304 if (disk_bytenr == 0)
2307 info->bytenr = disk_bytenr;
2309 btrfs_file_extent_disk_num_bytes(buf, fi);
2310 info->objectid = key.objectid;
2311 info->offset = key.offset;
2315 ret = btrfs_add_leaf_ref(root, ref, shared);
2316 if (ret == -EEXIST && shared) {
2317 struct btrfs_leaf_ref *old;
2318 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2320 btrfs_remove_leaf_ref(root, old);
2321 btrfs_free_leaf_ref(root, old);
2322 ret = btrfs_add_leaf_ref(root, ref, shared);
2325 btrfs_free_leaf_ref(root, ref);
2331 /* when a block goes through cow, we update the reference counts of
2332 * everything that block points to. The internal pointers of the block
2333 * can be in just about any order, and it is likely to have clusters of
2334 * things that are close together and clusters of things that are not.
2336 * To help reduce the seeks that come with updating all of these reference
2337 * counts, sort them by byte number before actual updates are done.
2339 * struct refsort is used to match byte number to slot in the btree block.
2340 * we sort based on the byte number and then use the slot to actually
2343 * struct refsort is smaller than strcut btrfs_item and smaller than
2344 * struct btrfs_key_ptr. Since we're currently limited to the page size
2345 * for a btree block, there's no way for a kmalloc of refsorts for a
2346 * single node to be bigger than a page.
2354 * for passing into sort()
2356 static int refsort_cmp(const void *a_void, const void *b_void)
2358 const struct refsort *a = a_void;
2359 const struct refsort *b = b_void;
2361 if (a->bytenr < b->bytenr)
2363 if (a->bytenr > b->bytenr)
2369 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
2370 struct btrfs_root *root,
2371 struct extent_buffer *buf,
2372 int full_backref, int inc)
2379 struct btrfs_key key;
2380 struct btrfs_file_extent_item *fi;
2384 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
2385 u64, u64, u64, u64, u64, u64);
2387 ref_root = btrfs_header_owner(buf);
2388 nritems = btrfs_header_nritems(buf);
2389 level = btrfs_header_level(buf);
2391 if (!root->ref_cows && level == 0)
2395 process_func = btrfs_inc_extent_ref;
2397 process_func = btrfs_free_extent;
2400 parent = buf->start;
2404 for (i = 0; i < nritems; i++) {
2406 btrfs_item_key_to_cpu(buf, &key, i);
2407 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2409 fi = btrfs_item_ptr(buf, i,
2410 struct btrfs_file_extent_item);
2411 if (btrfs_file_extent_type(buf, fi) ==
2412 BTRFS_FILE_EXTENT_INLINE)
2414 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2418 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2419 key.offset -= btrfs_file_extent_offset(buf, fi);
2420 ret = process_func(trans, root, bytenr, num_bytes,
2421 parent, ref_root, key.objectid,
2426 bytenr = btrfs_node_blockptr(buf, i);
2427 num_bytes = btrfs_level_size(root, level - 1);
2428 ret = process_func(trans, root, bytenr, num_bytes,
2429 parent, ref_root, level - 1, 0);
2440 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2441 struct extent_buffer *buf, int full_backref)
2443 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2446 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2447 struct extent_buffer *buf, int full_backref)
2449 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
2452 static int write_one_cache_group(struct btrfs_trans_handle *trans,
2453 struct btrfs_root *root,
2454 struct btrfs_path *path,
2455 struct btrfs_block_group_cache *cache)
2458 struct btrfs_root *extent_root = root->fs_info->extent_root;
2460 struct extent_buffer *leaf;
2462 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
2467 leaf = path->nodes[0];
2468 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2469 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2470 btrfs_mark_buffer_dirty(leaf);
2471 btrfs_release_path(extent_root, path);
2479 static struct btrfs_block_group_cache *
2480 next_block_group(struct btrfs_root *root,
2481 struct btrfs_block_group_cache *cache)
2483 struct rb_node *node;
2484 spin_lock(&root->fs_info->block_group_cache_lock);
2485 node = rb_next(&cache->cache_node);
2486 btrfs_put_block_group(cache);
2488 cache = rb_entry(node, struct btrfs_block_group_cache,
2490 atomic_inc(&cache->count);
2493 spin_unlock(&root->fs_info->block_group_cache_lock);
2497 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2498 struct btrfs_root *root)
2500 struct btrfs_block_group_cache *cache;
2502 struct btrfs_path *path;
2505 path = btrfs_alloc_path();
2511 err = btrfs_run_delayed_refs(trans, root,
2516 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2520 cache = next_block_group(root, cache);
2530 last = cache->key.objectid + cache->key.offset;
2532 err = write_one_cache_group(trans, root, path, cache);
2534 btrfs_put_block_group(cache);
2537 btrfs_free_path(path);
2541 int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2543 struct btrfs_block_group_cache *block_group;
2546 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2547 if (!block_group || block_group->ro)
2550 btrfs_put_block_group(block_group);
2554 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2555 u64 total_bytes, u64 bytes_used,
2556 struct btrfs_space_info **space_info)
2558 struct btrfs_space_info *found;
2560 found = __find_space_info(info, flags);
2562 spin_lock(&found->lock);
2563 found->total_bytes += total_bytes;
2564 found->bytes_used += bytes_used;
2566 spin_unlock(&found->lock);
2567 *space_info = found;
2570 found = kzalloc(sizeof(*found), GFP_NOFS);
2574 INIT_LIST_HEAD(&found->block_groups);
2575 init_rwsem(&found->groups_sem);
2576 spin_lock_init(&found->lock);
2577 found->flags = flags;
2578 found->total_bytes = total_bytes;
2579 found->bytes_used = bytes_used;
2580 found->bytes_pinned = 0;
2581 found->bytes_reserved = 0;
2582 found->bytes_readonly = 0;
2583 found->bytes_delalloc = 0;
2585 found->force_alloc = 0;
2586 *space_info = found;
2587 list_add_rcu(&found->list, &info->space_info);
2588 atomic_set(&found->caching_threads, 0);
2592 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2594 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
2595 BTRFS_BLOCK_GROUP_RAID1 |
2596 BTRFS_BLOCK_GROUP_RAID10 |
2597 BTRFS_BLOCK_GROUP_DUP);
2599 if (flags & BTRFS_BLOCK_GROUP_DATA)
2600 fs_info->avail_data_alloc_bits |= extra_flags;
2601 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2602 fs_info->avail_metadata_alloc_bits |= extra_flags;
2603 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2604 fs_info->avail_system_alloc_bits |= extra_flags;
2608 static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
2610 spin_lock(&cache->space_info->lock);
2611 spin_lock(&cache->lock);
2613 cache->space_info->bytes_readonly += cache->key.offset -
2614 btrfs_block_group_used(&cache->item);
2617 spin_unlock(&cache->lock);
2618 spin_unlock(&cache->space_info->lock);
2621 u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
2623 u64 num_devices = root->fs_info->fs_devices->rw_devices;
2625 if (num_devices == 1)
2626 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2627 if (num_devices < 4)
2628 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2630 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2631 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
2632 BTRFS_BLOCK_GROUP_RAID10))) {
2633 flags &= ~BTRFS_BLOCK_GROUP_DUP;
2636 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
2637 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
2638 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
2641 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2642 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2643 (flags & BTRFS_BLOCK_GROUP_RAID10) |
2644 (flags & BTRFS_BLOCK_GROUP_DUP)))
2645 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2649 static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
2651 struct btrfs_fs_info *info = root->fs_info;
2655 alloc_profile = info->avail_data_alloc_bits &
2656 info->data_alloc_profile;
2657 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2658 } else if (root == root->fs_info->chunk_root) {
2659 alloc_profile = info->avail_system_alloc_bits &
2660 info->system_alloc_profile;
2661 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2663 alloc_profile = info->avail_metadata_alloc_bits &
2664 info->metadata_alloc_profile;
2665 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2668 return btrfs_reduce_alloc_profile(root, data);
2671 void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
2675 alloc_target = btrfs_get_alloc_profile(root, 1);
2676 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
2681 * for now this just makes sure we have at least 5% of our metadata space free
2684 int btrfs_check_metadata_free_space(struct btrfs_root *root)
2686 struct btrfs_fs_info *info = root->fs_info;
2687 struct btrfs_space_info *meta_sinfo;
2688 u64 alloc_target, thresh;
2689 int committed = 0, ret;
2691 /* get the space info for where the metadata will live */
2692 alloc_target = btrfs_get_alloc_profile(root, 0);
2693 meta_sinfo = __find_space_info(info, alloc_target);
2696 spin_lock(&meta_sinfo->lock);
2697 if (!meta_sinfo->full)
2698 thresh = meta_sinfo->total_bytes * 80;
2700 thresh = meta_sinfo->total_bytes * 95;
2702 do_div(thresh, 100);
2704 if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
2705 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly > thresh) {
2706 struct btrfs_trans_handle *trans;
2707 if (!meta_sinfo->full) {
2708 meta_sinfo->force_alloc = 1;
2709 spin_unlock(&meta_sinfo->lock);
2711 trans = btrfs_start_transaction(root, 1);
2715 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2716 2 * 1024 * 1024, alloc_target, 0);
2717 btrfs_end_transaction(trans, root);
2720 spin_unlock(&meta_sinfo->lock);
2724 trans = btrfs_join_transaction(root, 1);
2727 ret = btrfs_commit_transaction(trans, root);
2734 spin_unlock(&meta_sinfo->lock);
2740 * This will check the space that the inode allocates from to make sure we have
2741 * enough space for bytes.
2743 int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
2746 struct btrfs_space_info *data_sinfo;
2747 int ret = 0, committed = 0;
2749 /* make sure bytes are sectorsize aligned */
2750 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
2752 data_sinfo = BTRFS_I(inode)->space_info;
2754 /* make sure we have enough space to handle the data first */
2755 spin_lock(&data_sinfo->lock);
2756 if (data_sinfo->total_bytes - data_sinfo->bytes_used -
2757 data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
2758 data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
2759 data_sinfo->bytes_may_use < bytes) {
2760 struct btrfs_trans_handle *trans;
2763 * if we don't have enough free bytes in this space then we need
2764 * to alloc a new chunk.
2766 if (!data_sinfo->full) {
2769 data_sinfo->force_alloc = 1;
2770 spin_unlock(&data_sinfo->lock);
2772 alloc_target = btrfs_get_alloc_profile(root, 1);
2773 trans = btrfs_start_transaction(root, 1);
2777 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2778 bytes + 2 * 1024 * 1024,
2780 btrfs_end_transaction(trans, root);
2785 spin_unlock(&data_sinfo->lock);
2787 /* commit the current transaction and try again */
2790 trans = btrfs_join_transaction(root, 1);
2793 ret = btrfs_commit_transaction(trans, root);
2799 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
2800 ", %llu bytes_used, %llu bytes_reserved, "
2801 "%llu bytes_pinned, %llu bytes_readonly, %llu may use "
2802 "%llu total\n", (unsigned long long)bytes,
2803 (unsigned long long)data_sinfo->bytes_delalloc,
2804 (unsigned long long)data_sinfo->bytes_used,
2805 (unsigned long long)data_sinfo->bytes_reserved,
2806 (unsigned long long)data_sinfo->bytes_pinned,
2807 (unsigned long long)data_sinfo->bytes_readonly,
2808 (unsigned long long)data_sinfo->bytes_may_use,
2809 (unsigned long long)data_sinfo->total_bytes);
2812 data_sinfo->bytes_may_use += bytes;
2813 BTRFS_I(inode)->reserved_bytes += bytes;
2814 spin_unlock(&data_sinfo->lock);
2816 return btrfs_check_metadata_free_space(root);
2820 * if there was an error for whatever reason after calling
2821 * btrfs_check_data_free_space, call this so we can cleanup the counters.
2823 void btrfs_free_reserved_data_space(struct btrfs_root *root,
2824 struct inode *inode, u64 bytes)
2826 struct btrfs_space_info *data_sinfo;
2828 /* make sure bytes are sectorsize aligned */
2829 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
2831 data_sinfo = BTRFS_I(inode)->space_info;
2832 spin_lock(&data_sinfo->lock);
2833 data_sinfo->bytes_may_use -= bytes;
2834 BTRFS_I(inode)->reserved_bytes -= bytes;
2835 spin_unlock(&data_sinfo->lock);
2838 /* called when we are adding a delalloc extent to the inode's io_tree */
2839 void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
2842 struct btrfs_space_info *data_sinfo;
2844 /* get the space info for where this inode will be storing its data */
2845 data_sinfo = BTRFS_I(inode)->space_info;
2847 /* make sure we have enough space to handle the data first */
2848 spin_lock(&data_sinfo->lock);
2849 data_sinfo->bytes_delalloc += bytes;
2852 * we are adding a delalloc extent without calling
2853 * btrfs_check_data_free_space first. This happens on a weird
2854 * writepage condition, but shouldn't hurt our accounting
2856 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
2857 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
2858 BTRFS_I(inode)->reserved_bytes = 0;
2860 data_sinfo->bytes_may_use -= bytes;
2861 BTRFS_I(inode)->reserved_bytes -= bytes;
2864 spin_unlock(&data_sinfo->lock);
2867 /* called when we are clearing an delalloc extent from the inode's io_tree */
2868 void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
2871 struct btrfs_space_info *info;
2873 info = BTRFS_I(inode)->space_info;
2875 spin_lock(&info->lock);
2876 info->bytes_delalloc -= bytes;
2877 spin_unlock(&info->lock);
2880 static void force_metadata_allocation(struct btrfs_fs_info *info)
2882 struct list_head *head = &info->space_info;
2883 struct btrfs_space_info *found;
2886 list_for_each_entry_rcu(found, head, list) {
2887 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
2888 found->force_alloc = 1;
2893 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
2894 struct btrfs_root *extent_root, u64 alloc_bytes,
2895 u64 flags, int force)
2897 struct btrfs_space_info *space_info;
2898 struct btrfs_fs_info *fs_info = extent_root->fs_info;
2902 mutex_lock(&fs_info->chunk_mutex);
2904 flags = btrfs_reduce_alloc_profile(extent_root, flags);
2906 space_info = __find_space_info(extent_root->fs_info, flags);
2908 ret = update_space_info(extent_root->fs_info, flags,
2912 BUG_ON(!space_info);
2914 spin_lock(&space_info->lock);
2915 if (space_info->force_alloc) {
2917 space_info->force_alloc = 0;
2919 if (space_info->full) {
2920 spin_unlock(&space_info->lock);
2924 thresh = space_info->total_bytes - space_info->bytes_readonly;
2925 thresh = div_factor(thresh, 6);
2927 (space_info->bytes_used + space_info->bytes_pinned +
2928 space_info->bytes_reserved + alloc_bytes) < thresh) {
2929 spin_unlock(&space_info->lock);
2932 spin_unlock(&space_info->lock);
2935 * if we're doing a data chunk, go ahead and make sure that
2936 * we keep a reasonable number of metadata chunks allocated in the
2939 if (flags & BTRFS_BLOCK_GROUP_DATA) {
2940 fs_info->data_chunk_allocations++;
2941 if (!(fs_info->data_chunk_allocations %
2942 fs_info->metadata_ratio))
2943 force_metadata_allocation(fs_info);
2946 ret = btrfs_alloc_chunk(trans, extent_root, flags);
2948 space_info->full = 1;
2950 mutex_unlock(&extent_root->fs_info->chunk_mutex);
2954 static int update_block_group(struct btrfs_trans_handle *trans,
2955 struct btrfs_root *root,
2956 u64 bytenr, u64 num_bytes, int alloc,
2959 struct btrfs_block_group_cache *cache;
2960 struct btrfs_fs_info *info = root->fs_info;
2961 u64 total = num_bytes;
2965 /* block accounting for super block */
2966 spin_lock(&info->delalloc_lock);
2967 old_val = btrfs_super_bytes_used(&info->super_copy);
2969 old_val += num_bytes;
2971 old_val -= num_bytes;
2972 btrfs_set_super_bytes_used(&info->super_copy, old_val);
2974 /* block accounting for root item */
2975 old_val = btrfs_root_used(&root->root_item);
2977 old_val += num_bytes;
2979 old_val -= num_bytes;
2980 btrfs_set_root_used(&root->root_item, old_val);
2981 spin_unlock(&info->delalloc_lock);
2984 cache = btrfs_lookup_block_group(info, bytenr);
2987 byte_in_group = bytenr - cache->key.objectid;
2988 WARN_ON(byte_in_group > cache->key.offset);
2990 spin_lock(&cache->space_info->lock);
2991 spin_lock(&cache->lock);
2993 old_val = btrfs_block_group_used(&cache->item);
2994 num_bytes = min(total, cache->key.offset - byte_in_group);
2996 old_val += num_bytes;
2997 cache->space_info->bytes_used += num_bytes;
2999 cache->space_info->bytes_readonly -= num_bytes;
3000 btrfs_set_block_group_used(&cache->item, old_val);
3001 spin_unlock(&cache->lock);
3002 spin_unlock(&cache->space_info->lock);
3004 old_val -= num_bytes;
3005 cache->space_info->bytes_used -= num_bytes;
3007 cache->space_info->bytes_readonly += num_bytes;
3008 btrfs_set_block_group_used(&cache->item, old_val);
3009 spin_unlock(&cache->lock);
3010 spin_unlock(&cache->space_info->lock);
3014 ret = btrfs_discard_extent(root, bytenr,
3018 ret = btrfs_add_free_space(cache, bytenr,
3023 btrfs_put_block_group(cache);
3025 bytenr += num_bytes;
3030 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
3032 struct btrfs_block_group_cache *cache;
3035 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
3039 bytenr = cache->key.objectid;
3040 btrfs_put_block_group(cache);
3045 int btrfs_update_pinned_extents(struct btrfs_root *root,
3046 u64 bytenr, u64 num, int pin)
3049 struct btrfs_block_group_cache *cache;
3050 struct btrfs_fs_info *fs_info = root->fs_info;
3053 set_extent_dirty(&fs_info->pinned_extents,
3054 bytenr, bytenr + num - 1, GFP_NOFS);
3057 cache = btrfs_lookup_block_group(fs_info, bytenr);
3059 len = min(num, cache->key.offset -
3060 (bytenr - cache->key.objectid));
3062 spin_lock(&cache->space_info->lock);
3063 spin_lock(&cache->lock);
3064 cache->pinned += len;
3065 cache->space_info->bytes_pinned += len;
3066 spin_unlock(&cache->lock);
3067 spin_unlock(&cache->space_info->lock);
3068 fs_info->total_pinned += len;
3073 * in order to not race with the block group caching, we
3074 * only want to unpin the extent if we are cached. If
3075 * we aren't cached, we want to start async caching this
3076 * block group so we can free the extent the next time
3079 spin_lock(&cache->space_info->lock);
3080 spin_lock(&cache->lock);
3081 unpin = (cache->cached == BTRFS_CACHE_FINISHED);
3082 if (likely(unpin)) {
3083 cache->pinned -= len;
3084 cache->space_info->bytes_pinned -= len;
3085 fs_info->total_pinned -= len;
3087 spin_unlock(&cache->lock);
3088 spin_unlock(&cache->space_info->lock);
3091 clear_extent_dirty(&fs_info->pinned_extents,
3092 bytenr, bytenr + len -1,
3095 cache_block_group(cache);
3098 btrfs_add_free_space(cache, bytenr, len);
3100 btrfs_put_block_group(cache);
3107 static int update_reserved_extents(struct btrfs_root *root,
3108 u64 bytenr, u64 num, int reserve)
3111 struct btrfs_block_group_cache *cache;
3112 struct btrfs_fs_info *fs_info = root->fs_info;
3115 cache = btrfs_lookup_block_group(fs_info, bytenr);
3117 len = min(num, cache->key.offset -
3118 (bytenr - cache->key.objectid));
3120 spin_lock(&cache->space_info->lock);
3121 spin_lock(&cache->lock);
3123 cache->reserved += len;
3124 cache->space_info->bytes_reserved += len;
3126 cache->reserved -= len;
3127 cache->space_info->bytes_reserved -= len;
3129 spin_unlock(&cache->lock);
3130 spin_unlock(&cache->space_info->lock);
3131 btrfs_put_block_group(cache);
3138 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
3143 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
3147 ret = find_first_extent_bit(pinned_extents, last,
3148 &start, &end, EXTENT_DIRTY);
3152 set_extent_dirty(copy, start, end, GFP_NOFS);
3158 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
3159 struct btrfs_root *root,
3160 struct extent_io_tree *unpin)
3167 ret = find_first_extent_bit(unpin, 0, &start, &end,
3172 ret = btrfs_discard_extent(root, start, end + 1 - start);
3174 /* unlocks the pinned mutex */
3175 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
3176 clear_extent_dirty(unpin, start, end, GFP_NOFS);
3184 static int pin_down_bytes(struct btrfs_trans_handle *trans,
3185 struct btrfs_root *root,
3186 struct btrfs_path *path,
3187 u64 bytenr, u64 num_bytes, int is_data,
3188 struct extent_buffer **must_clean)
3191 struct extent_buffer *buf;
3196 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
3200 /* we can reuse a block if it hasn't been written
3201 * and it is from this transaction. We can't
3202 * reuse anything from the tree log root because
3203 * it has tiny sub-transactions.
3205 if (btrfs_buffer_uptodate(buf, 0) &&
3206 btrfs_try_tree_lock(buf)) {
3207 u64 header_owner = btrfs_header_owner(buf);
3208 u64 header_transid = btrfs_header_generation(buf);
3209 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
3210 header_transid == trans->transid &&
3211 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
3215 btrfs_tree_unlock(buf);
3217 free_extent_buffer(buf);
3219 btrfs_set_path_blocking(path);
3220 /* unlocks the pinned mutex */
3221 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
3228 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
3229 struct btrfs_root *root,
3230 u64 bytenr, u64 num_bytes, u64 parent,
3231 u64 root_objectid, u64 owner_objectid,
3232 u64 owner_offset, int refs_to_drop,
3233 struct btrfs_delayed_extent_op *extent_op)
3235 struct btrfs_key key;
3236 struct btrfs_path *path;
3237 struct btrfs_fs_info *info = root->fs_info;
3238 struct btrfs_root *extent_root = info->extent_root;
3239 struct extent_buffer *leaf;
3240 struct btrfs_extent_item *ei;
3241 struct btrfs_extent_inline_ref *iref;
3244 int extent_slot = 0;
3245 int found_extent = 0;
3250 path = btrfs_alloc_path();
3255 path->leave_spinning = 1;
3257 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
3258 BUG_ON(!is_data && refs_to_drop != 1);
3260 ret = lookup_extent_backref(trans, extent_root, path, &iref,
3261 bytenr, num_bytes, parent,
3262 root_objectid, owner_objectid,
3265 extent_slot = path->slots[0];
3266 while (extent_slot >= 0) {
3267 btrfs_item_key_to_cpu(path->nodes[0], &key,
3269 if (key.objectid != bytenr)
3271 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3272 key.offset == num_bytes) {
3276 if (path->slots[0] - extent_slot > 5)
3280 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3281 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
3282 if (found_extent && item_size < sizeof(*ei))
3285 if (!found_extent) {
3287 ret = remove_extent_backref(trans, extent_root, path,
3291 btrfs_release_path(extent_root, path);
3292 path->leave_spinning = 1;
3294 key.objectid = bytenr;
3295 key.type = BTRFS_EXTENT_ITEM_KEY;
3296 key.offset = num_bytes;
3298 ret = btrfs_search_slot(trans, extent_root,
3301 printk(KERN_ERR "umm, got %d back from search"
3302 ", was looking for %llu\n", ret,
3303 (unsigned long long)bytenr);
3304 btrfs_print_leaf(extent_root, path->nodes[0]);
3307 extent_slot = path->slots[0];
3310 btrfs_print_leaf(extent_root, path->nodes[0]);
3312 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
3313 "parent %llu root %llu owner %llu offset %llu\n",
3314 (unsigned long long)bytenr,
3315 (unsigned long long)parent,
3316 (unsigned long long)root_objectid,
3317 (unsigned long long)owner_objectid,
3318 (unsigned long long)owner_offset);
3321 leaf = path->nodes[0];
3322 item_size = btrfs_item_size_nr(leaf, extent_slot);
3323 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3324 if (item_size < sizeof(*ei)) {
3325 BUG_ON(found_extent || extent_slot != path->slots[0]);
3326 ret = convert_extent_item_v0(trans, extent_root, path,
3330 btrfs_release_path(extent_root, path);
3331 path->leave_spinning = 1;
3333 key.objectid = bytenr;
3334 key.type = BTRFS_EXTENT_ITEM_KEY;
3335 key.offset = num_bytes;
3337 ret = btrfs_search_slot(trans, extent_root, &key, path,
3340 printk(KERN_ERR "umm, got %d back from search"
3341 ", was looking for %llu\n", ret,
3342 (unsigned long long)bytenr);
3343 btrfs_print_leaf(extent_root, path->nodes[0]);
3346 extent_slot = path->slots[0];
3347 leaf = path->nodes[0];
3348 item_size = btrfs_item_size_nr(leaf, extent_slot);
3351 BUG_ON(item_size < sizeof(*ei));
3352 ei = btrfs_item_ptr(leaf, extent_slot,
3353 struct btrfs_extent_item);
3354 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3355 struct btrfs_tree_block_info *bi;
3356 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
3357 bi = (struct btrfs_tree_block_info *)(ei + 1);
3358 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
3361 refs = btrfs_extent_refs(leaf, ei);
3362 BUG_ON(refs < refs_to_drop);
3363 refs -= refs_to_drop;
3367 __run_delayed_extent_op(extent_op, leaf, ei);
3369 * In the case of inline back ref, reference count will
3370 * be updated by remove_extent_backref
3373 BUG_ON(!found_extent);
3375 btrfs_set_extent_refs(leaf, ei, refs);
3376 btrfs_mark_buffer_dirty(leaf);
3379 ret = remove_extent_backref(trans, extent_root, path,
3386 struct extent_buffer *must_clean = NULL;
3389 BUG_ON(is_data && refs_to_drop !=
3390 extent_data_ref_count(root, path, iref));
3392 BUG_ON(path->slots[0] != extent_slot);
3394 BUG_ON(path->slots[0] != extent_slot + 1);
3395 path->slots[0] = extent_slot;
3400 ret = pin_down_bytes(trans, root, path, bytenr,
3401 num_bytes, is_data, &must_clean);
3406 * it is going to be very rare for someone to be waiting
3407 * on the block we're freeing. del_items might need to
3408 * schedule, so rather than get fancy, just force it
3412 btrfs_set_lock_blocking(must_clean);
3414 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3417 btrfs_release_path(extent_root, path);
3420 clean_tree_block(NULL, root, must_clean);
3421 btrfs_tree_unlock(must_clean);
3422 free_extent_buffer(must_clean);
3426 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
3429 invalidate_mapping_pages(info->btree_inode->i_mapping,
3430 bytenr >> PAGE_CACHE_SHIFT,
3431 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
3434 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
3438 btrfs_free_path(path);
3443 * when we free an extent, it is possible (and likely) that we free the last
3444 * delayed ref for that extent as well. This searches the delayed ref tree for
3445 * a given extent, and if there are no other delayed refs to be processed, it
3446 * removes it from the tree.
3448 static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
3449 struct btrfs_root *root, u64 bytenr)
3451 struct btrfs_delayed_ref_head *head;
3452 struct btrfs_delayed_ref_root *delayed_refs;
3453 struct btrfs_delayed_ref_node *ref;
3454 struct rb_node *node;
3457 delayed_refs = &trans->transaction->delayed_refs;
3458 spin_lock(&delayed_refs->lock);
3459 head = btrfs_find_delayed_ref_head(trans, bytenr);
3463 node = rb_prev(&head->node.rb_node);
3467 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
3469 /* there are still entries for this ref, we can't drop it */
3470 if (ref->bytenr == bytenr)
3473 if (head->extent_op) {
3474 if (!head->must_insert_reserved)
3476 kfree(head->extent_op);
3477 head->extent_op = NULL;
3481 * waiting for the lock here would deadlock. If someone else has it
3482 * locked they are already in the process of dropping it anyway
3484 if (!mutex_trylock(&head->mutex))
3488 * at this point we have a head with no other entries. Go
3489 * ahead and process it.
3491 head->node.in_tree = 0;
3492 rb_erase(&head->node.rb_node, &delayed_refs->root);
3494 delayed_refs->num_entries--;
3497 * we don't take a ref on the node because we're removing it from the
3498 * tree, so we just steal the ref the tree was holding.
3500 delayed_refs->num_heads--;
3501 if (list_empty(&head->cluster))
3502 delayed_refs->num_heads_ready--;
3504 list_del_init(&head->cluster);
3505 spin_unlock(&delayed_refs->lock);
3507 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
3508 &head->node, head->extent_op,
3509 head->must_insert_reserved);
3511 btrfs_put_delayed_ref(&head->node);
3514 spin_unlock(&delayed_refs->lock);
3518 int btrfs_free_extent(struct btrfs_trans_handle *trans,
3519 struct btrfs_root *root,
3520 u64 bytenr, u64 num_bytes, u64 parent,
3521 u64 root_objectid, u64 owner, u64 offset)
3526 * tree log blocks never actually go into the extent allocation
3527 * tree, just update pinning info and exit early.
3529 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
3530 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
3531 /* unlocks the pinned mutex */
3532 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
3533 update_reserved_extents(root, bytenr, num_bytes, 0);
3535 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
3536 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
3537 parent, root_objectid, (int)owner,
3538 BTRFS_DROP_DELAYED_REF, NULL);
3540 ret = check_ref_cleanup(trans, root, bytenr);
3543 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
3544 parent, root_objectid, owner,
3545 offset, BTRFS_DROP_DELAYED_REF, NULL);
3551 static u64 stripe_align(struct btrfs_root *root, u64 val)
3553 u64 mask = ((u64)root->stripesize - 1);
3554 u64 ret = (val + mask) & ~mask;
3559 * when we wait for progress in the block group caching, its because
3560 * our allocation attempt failed at least once. So, we must sleep
3561 * and let some progress happen before we try again.
3563 * This function will sleep at least once waiting for new free space to
3564 * show up, and then it will check the block group free space numbers
3565 * for our min num_bytes. Another option is to have it go ahead
3566 * and look in the rbtree for a free extent of a given size, but this
3570 wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
3575 prepare_to_wait(&cache->caching_q, &wait, TASK_UNINTERRUPTIBLE);
3577 if (block_group_cache_done(cache)) {
3578 finish_wait(&cache->caching_q, &wait);
3582 finish_wait(&cache->caching_q, &wait);
3584 wait_event(cache->caching_q, block_group_cache_done(cache) ||
3585 (cache->free_space >= num_bytes));
3589 enum btrfs_loop_type {
3590 LOOP_CACHED_ONLY = 0,
3591 LOOP_CACHING_NOWAIT = 1,
3592 LOOP_CACHING_WAIT = 2,
3593 LOOP_ALLOC_CHUNK = 3,
3594 LOOP_NO_EMPTY_SIZE = 4,
3598 * walks the btree of allocated extents and find a hole of a given size.
3599 * The key ins is changed to record the hole:
3600 * ins->objectid == block start
3601 * ins->flags = BTRFS_EXTENT_ITEM_KEY
3602 * ins->offset == number of blocks
3603 * Any available blocks before search_start are skipped.
3605 static noinline int find_free_extent(struct btrfs_trans_handle *trans,
3606 struct btrfs_root *orig_root,
3607 u64 num_bytes, u64 empty_size,
3608 u64 search_start, u64 search_end,
3609 u64 hint_byte, struct btrfs_key *ins,
3610 u64 exclude_start, u64 exclude_nr,
3614 struct btrfs_root *root = orig_root->fs_info->extent_root;
3615 struct btrfs_free_cluster *last_ptr = NULL;
3616 struct btrfs_block_group_cache *block_group = NULL;
3617 int empty_cluster = 2 * 1024 * 1024;
3618 int allowed_chunk_alloc = 0;
3619 struct btrfs_space_info *space_info;
3620 int last_ptr_loop = 0;
3622 bool found_uncached_bg = false;
3624 WARN_ON(num_bytes < root->sectorsize);
3625 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
3629 space_info = __find_space_info(root->fs_info, data);
3631 if (orig_root->ref_cows || empty_size)
3632 allowed_chunk_alloc = 1;
3634 if (data & BTRFS_BLOCK_GROUP_METADATA) {
3635 last_ptr = &root->fs_info->meta_alloc_cluster;
3636 if (!btrfs_test_opt(root, SSD))
3637 empty_cluster = 64 * 1024;
3640 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
3641 last_ptr = &root->fs_info->data_alloc_cluster;
3645 spin_lock(&last_ptr->lock);
3646 if (last_ptr->block_group)
3647 hint_byte = last_ptr->window_start;
3648 spin_unlock(&last_ptr->lock);
3651 search_start = max(search_start, first_logical_byte(root, 0));
3652 search_start = max(search_start, hint_byte);
3657 if (search_start == hint_byte) {
3658 block_group = btrfs_lookup_block_group(root->fs_info,
3661 * we don't want to use the block group if it doesn't match our
3662 * allocation bits, or if its not cached.
3664 if (block_group && block_group_bits(block_group, data) &&
3665 block_group_cache_done(block_group)) {
3666 down_read(&space_info->groups_sem);
3667 if (list_empty(&block_group->list) ||
3670 * someone is removing this block group,
3671 * we can't jump into the have_block_group
3672 * target because our list pointers are not
3675 btrfs_put_block_group(block_group);
3676 up_read(&space_info->groups_sem);
3678 goto have_block_group;
3679 } else if (block_group) {
3680 btrfs_put_block_group(block_group);
3685 down_read(&space_info->groups_sem);
3686 list_for_each_entry(block_group, &space_info->block_groups, list) {
3690 atomic_inc(&block_group->count);
3691 search_start = block_group->key.objectid;
3694 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
3696 * we want to start caching kthreads, but not too many
3697 * right off the bat so we don't overwhelm the system,
3698 * so only start them if there are less than 2 and we're
3699 * in the initial allocation phase.
3701 if (loop > LOOP_CACHING_NOWAIT ||
3702 atomic_read(&space_info->caching_threads) < 2) {
3703 ret = cache_block_group(block_group);
3708 cached = block_group_cache_done(block_group);
3709 if (unlikely(!cached)) {
3710 found_uncached_bg = true;
3712 /* if we only want cached bgs, loop */
3713 if (loop == LOOP_CACHED_ONLY)
3717 if (unlikely(block_group->ro))
3722 * the refill lock keeps out other
3723 * people trying to start a new cluster
3725 spin_lock(&last_ptr->refill_lock);
3726 if (last_ptr->block_group &&
3727 (last_ptr->block_group->ro ||
3728 !block_group_bits(last_ptr->block_group, data))) {
3730 goto refill_cluster;
3733 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
3734 num_bytes, search_start);
3736 /* we have a block, we're done */
3737 spin_unlock(&last_ptr->refill_lock);
3741 spin_lock(&last_ptr->lock);
3743 * whoops, this cluster doesn't actually point to
3744 * this block group. Get a ref on the block
3745 * group is does point to and try again
3747 if (!last_ptr_loop && last_ptr->block_group &&
3748 last_ptr->block_group != block_group) {
3750 btrfs_put_block_group(block_group);
3751 block_group = last_ptr->block_group;
3752 atomic_inc(&block_group->count);
3753 spin_unlock(&last_ptr->lock);
3754 spin_unlock(&last_ptr->refill_lock);
3757 search_start = block_group->key.objectid;
3759 * we know this block group is properly
3760 * in the list because
3761 * btrfs_remove_block_group, drops the
3762 * cluster before it removes the block
3763 * group from the list
3765 goto have_block_group;
3767 spin_unlock(&last_ptr->lock);
3770 * this cluster didn't work out, free it and
3773 btrfs_return_cluster_to_free_space(NULL, last_ptr);
3777 /* allocate a cluster in this block group */
3778 ret = btrfs_find_space_cluster(trans, root,
3779 block_group, last_ptr,
3781 empty_cluster + empty_size);
3784 * now pull our allocation out of this
3787 offset = btrfs_alloc_from_cluster(block_group,
3788 last_ptr, num_bytes,
3791 /* we found one, proceed */
3792 spin_unlock(&last_ptr->refill_lock);
3795 } else if (!cached && loop > LOOP_CACHING_NOWAIT) {
3796 spin_unlock(&last_ptr->refill_lock);
3798 wait_block_group_cache_progress(block_group,
3799 num_bytes + empty_cluster + empty_size);
3800 goto have_block_group;
3804 * at this point we either didn't find a cluster
3805 * or we weren't able to allocate a block from our
3806 * cluster. Free the cluster we've been trying
3807 * to use, and go to the next block group
3809 if (loop < LOOP_NO_EMPTY_SIZE) {
3810 btrfs_return_cluster_to_free_space(NULL,
3812 spin_unlock(&last_ptr->refill_lock);
3815 spin_unlock(&last_ptr->refill_lock);
3818 offset = btrfs_find_space_for_alloc(block_group, search_start,
3819 num_bytes, empty_size);
3820 if (!offset && (cached || (!cached &&
3821 loop == LOOP_CACHING_NOWAIT))) {
3823 } else if (!offset && (!cached &&
3824 loop > LOOP_CACHING_NOWAIT)) {
3825 wait_block_group_cache_progress(block_group,
3826 num_bytes + empty_size);
3827 goto have_block_group;
3830 search_start = stripe_align(root, offset);
3831 /* move on to the next group */
3832 if (search_start + num_bytes >= search_end) {
3833 btrfs_add_free_space(block_group, offset, num_bytes);
3837 /* move on to the next group */
3838 if (search_start + num_bytes >
3839 block_group->key.objectid + block_group->key.offset) {
3840 btrfs_add_free_space(block_group, offset, num_bytes);
3844 if (exclude_nr > 0 &&
3845 (search_start + num_bytes > exclude_start &&
3846 search_start < exclude_start + exclude_nr)) {
3847 search_start = exclude_start + exclude_nr;
3849 btrfs_add_free_space(block_group, offset, num_bytes);
3851 * if search_start is still in this block group
3852 * then we just re-search this block group
3854 if (search_start >= block_group->key.objectid &&
3855 search_start < (block_group->key.objectid +
3856 block_group->key.offset))
3857 goto have_block_group;
3861 ins->objectid = search_start;
3862 ins->offset = num_bytes;
3864 if (offset < search_start)
3865 btrfs_add_free_space(block_group, offset,
3866 search_start - offset);
3867 BUG_ON(offset > search_start);
3869 /* we are all good, lets return */
3872 btrfs_put_block_group(block_group);
3874 up_read(&space_info->groups_sem);
3876 /* LOOP_CACHED_ONLY, only search fully cached block groups
3877 * LOOP_CACHING_NOWAIT, search partially cached block groups, but
3878 * dont wait foR them to finish caching
3879 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
3880 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
3881 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
3884 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
3885 (found_uncached_bg || empty_size || empty_cluster ||
3886 allowed_chunk_alloc)) {
3887 if (found_uncached_bg) {
3888 found_uncached_bg = false;
3889 if (loop < LOOP_CACHING_WAIT) {
3895 if (loop == LOOP_ALLOC_CHUNK) {
3900 if (allowed_chunk_alloc) {
3901 ret = do_chunk_alloc(trans, root, num_bytes +
3902 2 * 1024 * 1024, data, 1);
3903 allowed_chunk_alloc = 0;
3905 space_info->force_alloc = 1;
3908 if (loop < LOOP_NO_EMPTY_SIZE) {
3913 } else if (!ins->objectid) {
3917 /* we found what we needed */
3918 if (ins->objectid) {
3919 if (!(data & BTRFS_BLOCK_GROUP_DATA))
3920 trans->block_group = block_group->key.objectid;
3922 btrfs_put_block_group(block_group);
3929 static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
3931 struct btrfs_block_group_cache *cache;
3933 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
3934 (unsigned long long)(info->total_bytes - info->bytes_used -
3935 info->bytes_pinned - info->bytes_reserved),
3936 (info->full) ? "" : "not ");
3937 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
3938 " may_use=%llu, used=%llu\n",
3939 (unsigned long long)info->total_bytes,
3940 (unsigned long long)info->bytes_pinned,
3941 (unsigned long long)info->bytes_delalloc,
3942 (unsigned long long)info->bytes_may_use,
3943 (unsigned long long)info->bytes_used);
3945 down_read(&info->groups_sem);
3946 list_for_each_entry(cache, &info->block_groups, list) {
3947 spin_lock(&cache->lock);
3948 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
3949 "%llu pinned %llu reserved\n",
3950 (unsigned long long)cache->key.objectid,
3951 (unsigned long long)cache->key.offset,
3952 (unsigned long long)btrfs_block_group_used(&cache->item),
3953 (unsigned long long)cache->pinned,
3954 (unsigned long long)cache->reserved);
3955 btrfs_dump_free_space(cache, bytes);
3956 spin_unlock(&cache->lock);
3958 up_read(&info->groups_sem);
3961 static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3962 struct btrfs_root *root,
3963 u64 num_bytes, u64 min_alloc_size,
3964 u64 empty_size, u64 hint_byte,
3965 u64 search_end, struct btrfs_key *ins,
3969 u64 search_start = 0;
3970 struct btrfs_fs_info *info = root->fs_info;
3972 data = btrfs_get_alloc_profile(root, data);
3975 * the only place that sets empty_size is btrfs_realloc_node, which
3976 * is not called recursively on allocations
3978 if (empty_size || root->ref_cows) {
3979 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
3980 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3982 BTRFS_BLOCK_GROUP_METADATA |
3983 (info->metadata_alloc_profile &
3984 info->avail_metadata_alloc_bits), 0);
3986 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3987 num_bytes + 2 * 1024 * 1024, data, 0);
3990 WARN_ON(num_bytes < root->sectorsize);
3991 ret = find_free_extent(trans, root, num_bytes, empty_size,
3992 search_start, search_end, hint_byte, ins,
3993 trans->alloc_exclude_start,
3994 trans->alloc_exclude_nr, data);
3996 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
3997 num_bytes = num_bytes >> 1;
3998 num_bytes = num_bytes & ~(root->sectorsize - 1);
3999 num_bytes = max(num_bytes, min_alloc_size);
4000 do_chunk_alloc(trans, root->fs_info->extent_root,
4001 num_bytes, data, 1);
4004 if (ret == -ENOSPC) {
4005 struct btrfs_space_info *sinfo;
4007 sinfo = __find_space_info(root->fs_info, data);
4008 printk(KERN_ERR "btrfs allocation failed flags %llu, "
4009 "wanted %llu\n", (unsigned long long)data,
4010 (unsigned long long)num_bytes);
4011 dump_space_info(sinfo, num_bytes);
4017 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
4019 struct btrfs_block_group_cache *cache;
4022 cache = btrfs_lookup_block_group(root->fs_info, start);
4024 printk(KERN_ERR "Unable to find block group for %llu\n",
4025 (unsigned long long)start);
4029 ret = btrfs_discard_extent(root, start, len);
4031 btrfs_add_free_space(cache, start, len);
4032 btrfs_put_block_group(cache);
4033 update_reserved_extents(root, start, len, 0);
4038 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
4039 struct btrfs_root *root,
4040 u64 num_bytes, u64 min_alloc_size,
4041 u64 empty_size, u64 hint_byte,
4042 u64 search_end, struct btrfs_key *ins,
4046 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
4047 empty_size, hint_byte, search_end, ins,
4050 update_reserved_extents(root, ins->objectid, ins->offset, 1);
4055 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4056 struct btrfs_root *root,
4057 u64 parent, u64 root_objectid,
4058 u64 flags, u64 owner, u64 offset,
4059 struct btrfs_key *ins, int ref_mod)
4062 struct btrfs_fs_info *fs_info = root->fs_info;
4063 struct btrfs_extent_item *extent_item;
4064 struct btrfs_extent_inline_ref *iref;
4065 struct btrfs_path *path;
4066 struct extent_buffer *leaf;
4071 type = BTRFS_SHARED_DATA_REF_KEY;
4073 type = BTRFS_EXTENT_DATA_REF_KEY;
4075 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
4077 path = btrfs_alloc_path();
4080 path->leave_spinning = 1;
4081 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4085 leaf = path->nodes[0];
4086 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4087 struct btrfs_extent_item);
4088 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
4089 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4090 btrfs_set_extent_flags(leaf, extent_item,
4091 flags | BTRFS_EXTENT_FLAG_DATA);
4093 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4094 btrfs_set_extent_inline_ref_type(leaf, iref, type);
4096 struct btrfs_shared_data_ref *ref;
4097 ref = (struct btrfs_shared_data_ref *)(iref + 1);
4098 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4099 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
4101 struct btrfs_extent_data_ref *ref;
4102 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
4103 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
4104 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
4105 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
4106 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
4109 btrfs_mark_buffer_dirty(path->nodes[0]);
4110 btrfs_free_path(path);
4112 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4115 printk(KERN_ERR "btrfs update block group failed for %llu "
4116 "%llu\n", (unsigned long long)ins->objectid,
4117 (unsigned long long)ins->offset);
4123 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
4124 struct btrfs_root *root,
4125 u64 parent, u64 root_objectid,
4126 u64 flags, struct btrfs_disk_key *key,
4127 int level, struct btrfs_key *ins)
4130 struct btrfs_fs_info *fs_info = root->fs_info;
4131 struct btrfs_extent_item *extent_item;
4132 struct btrfs_tree_block_info *block_info;
4133 struct btrfs_extent_inline_ref *iref;
4134 struct btrfs_path *path;
4135 struct extent_buffer *leaf;
4136 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
4138 path = btrfs_alloc_path();
4141 path->leave_spinning = 1;
4142 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4146 leaf = path->nodes[0];
4147 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4148 struct btrfs_extent_item);
4149 btrfs_set_extent_refs(leaf, extent_item, 1);
4150 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4151 btrfs_set_extent_flags(leaf, extent_item,
4152 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
4153 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
4155 btrfs_set_tree_block_key(leaf, block_info, key);
4156 btrfs_set_tree_block_level(leaf, block_info, level);
4158 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
4160 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4161 btrfs_set_extent_inline_ref_type(leaf, iref,
4162 BTRFS_SHARED_BLOCK_REF_KEY);
4163 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4165 btrfs_set_extent_inline_ref_type(leaf, iref,
4166 BTRFS_TREE_BLOCK_REF_KEY);
4167 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
4170 btrfs_mark_buffer_dirty(leaf);
4171 btrfs_free_path(path);
4173 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4176 printk(KERN_ERR "btrfs update block group failed for %llu "
4177 "%llu\n", (unsigned long long)ins->objectid,
4178 (unsigned long long)ins->offset);
4184 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4185 struct btrfs_root *root,
4186 u64 root_objectid, u64 owner,
4187 u64 offset, struct btrfs_key *ins)
4191 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
4193 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
4194 0, root_objectid, owner, offset,
4195 BTRFS_ADD_DELAYED_EXTENT, NULL);
4200 * this is used by the tree logging recovery code. It records that
4201 * an extent has been allocated and makes sure to clear the free
4202 * space cache bits as well
4204 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
4205 struct btrfs_root *root,
4206 u64 root_objectid, u64 owner, u64 offset,
4207 struct btrfs_key *ins)
4210 struct btrfs_block_group_cache *block_group;
4212 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
4213 cache_block_group(block_group);
4214 wait_event(block_group->caching_q,
4215 block_group_cache_done(block_group));
4217 ret = btrfs_remove_free_space(block_group, ins->objectid,
4220 btrfs_put_block_group(block_group);
4221 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
4222 0, owner, offset, ins, 1);
4227 * finds a free extent and does all the dirty work required for allocation
4228 * returns the key for the extent through ins, and a tree buffer for
4229 * the first block of the extent through buf.
4231 * returns 0 if everything worked, non-zero otherwise.
4233 static int alloc_tree_block(struct btrfs_trans_handle *trans,
4234 struct btrfs_root *root,
4235 u64 num_bytes, u64 parent, u64 root_objectid,
4236 struct btrfs_disk_key *key, int level,
4237 u64 empty_size, u64 hint_byte, u64 search_end,
4238 struct btrfs_key *ins)
4243 ret = __btrfs_reserve_extent(trans, root, num_bytes, num_bytes,
4244 empty_size, hint_byte, search_end,
4249 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4251 parent = ins->objectid;
4252 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4256 update_reserved_extents(root, ins->objectid, ins->offset, 1);
4257 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
4258 struct btrfs_delayed_extent_op *extent_op;
4259 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
4262 memcpy(&extent_op->key, key, sizeof(extent_op->key));
4264 memset(&extent_op->key, 0, sizeof(extent_op->key));
4265 extent_op->flags_to_set = flags;
4266 extent_op->update_key = 1;
4267 extent_op->update_flags = 1;
4268 extent_op->is_data = 0;
4270 ret = btrfs_add_delayed_tree_ref(trans, ins->objectid,
4271 ins->offset, parent, root_objectid,
4272 level, BTRFS_ADD_DELAYED_EXTENT,
4279 struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
4280 struct btrfs_root *root,
4281 u64 bytenr, u32 blocksize,
4284 struct extent_buffer *buf;
4286 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
4288 return ERR_PTR(-ENOMEM);
4289 btrfs_set_header_generation(buf, trans->transid);
4290 btrfs_set_buffer_lockdep_class(buf, level);
4291 btrfs_tree_lock(buf);
4292 clean_tree_block(trans, root, buf);
4294 btrfs_set_lock_blocking(buf);
4295 btrfs_set_buffer_uptodate(buf);
4297 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
4298 set_extent_dirty(&root->dirty_log_pages, buf->start,
4299 buf->start + buf->len - 1, GFP_NOFS);
4301 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
4302 buf->start + buf->len - 1, GFP_NOFS);
4304 trans->blocks_used++;
4305 /* this returns a buffer locked for blocking */
4310 * helper function to allocate a block for a given tree
4311 * returns the tree buffer or NULL.
4313 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
4314 struct btrfs_root *root, u32 blocksize,
4315 u64 parent, u64 root_objectid,
4316 struct btrfs_disk_key *key, int level,
4317 u64 hint, u64 empty_size)
4319 struct btrfs_key ins;
4321 struct extent_buffer *buf;
4323 ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid,
4324 key, level, empty_size, hint, (u64)-1, &ins);
4327 return ERR_PTR(ret);
4330 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
4336 int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
4337 struct btrfs_root *root, struct extent_buffer *leaf)
4341 struct btrfs_key key;
4342 struct btrfs_file_extent_item *fi;
4347 BUG_ON(!btrfs_is_leaf(leaf));
4348 nritems = btrfs_header_nritems(leaf);
4350 for (i = 0; i < nritems; i++) {
4352 btrfs_item_key_to_cpu(leaf, &key, i);
4354 /* only extents have references, skip everything else */
4355 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4358 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4360 /* inline extents live in the btree, they don't have refs */
4361 if (btrfs_file_extent_type(leaf, fi) ==
4362 BTRFS_FILE_EXTENT_INLINE)
4365 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4367 /* holes don't have refs */
4368 if (disk_bytenr == 0)
4371 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4372 ret = btrfs_free_extent(trans, root, disk_bytenr, num_bytes,
4373 leaf->start, 0, key.objectid, 0);
4379 static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
4380 struct btrfs_root *root,
4381 struct btrfs_leaf_ref *ref)
4385 struct btrfs_extent_info *info;
4386 struct refsort *sorted;
4388 if (ref->nritems == 0)
4391 sorted = kmalloc(sizeof(*sorted) * ref->nritems, GFP_NOFS);
4392 for (i = 0; i < ref->nritems; i++) {
4393 sorted[i].bytenr = ref->extents[i].bytenr;
4396 sort(sorted, ref->nritems, sizeof(struct refsort), refsort_cmp, NULL);
4399 * the items in the ref were sorted when the ref was inserted
4400 * into the ref cache, so this is already in order
4402 for (i = 0; i < ref->nritems; i++) {
4403 info = ref->extents + sorted[i].slot;
4404 ret = btrfs_free_extent(trans, root, info->bytenr,
4405 info->num_bytes, ref->bytenr,
4406 ref->owner, ref->generation,
4409 atomic_inc(&root->fs_info->throttle_gen);
4410 wake_up(&root->fs_info->transaction_throttle);
4422 static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans,
4423 struct btrfs_root *root, u64 start,
4428 ret = btrfs_lookup_extent_refs(trans, root, start, len, refs);
4431 #if 0 /* some debugging code in case we see problems here */
4432 /* if the refs count is one, it won't get increased again. But
4433 * if the ref count is > 1, someone may be decreasing it at
4434 * the same time we are.
4437 struct extent_buffer *eb = NULL;
4438 eb = btrfs_find_create_tree_block(root, start, len);
4440 btrfs_tree_lock(eb);
4442 mutex_lock(&root->fs_info->alloc_mutex);
4443 ret = lookup_extent_ref(NULL, root, start, len, refs);
4445 mutex_unlock(&root->fs_info->alloc_mutex);
4448 btrfs_tree_unlock(eb);
4449 free_extent_buffer(eb);
4452 printk(KERN_ERR "btrfs block %llu went down to one "
4453 "during drop_snap\n", (unsigned long long)start);
4465 * this is used while deleting old snapshots, and it drops the refs
4466 * on a whole subtree starting from a level 1 node.
4468 * The idea is to sort all the leaf pointers, and then drop the
4469 * ref on all the leaves in order. Most of the time the leaves
4470 * will have ref cache entries, so no leaf IOs will be required to
4471 * find the extents they have references on.
4473 * For each leaf, any references it has are also dropped in order
4475 * This ends up dropping the references in something close to optimal
4476 * order for reading and modifying the extent allocation tree.
4478 static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans,
4479 struct btrfs_root *root,
4480 struct btrfs_path *path)
4485 struct extent_buffer *eb = path->nodes[1];
4486 struct extent_buffer *leaf;
4487 struct btrfs_leaf_ref *ref;
4488 struct refsort *sorted = NULL;
4489 int nritems = btrfs_header_nritems(eb);
4493 int slot = path->slots[1];
4494 u32 blocksize = btrfs_level_size(root, 0);
4500 root_owner = btrfs_header_owner(eb);
4501 root_gen = btrfs_header_generation(eb);
4502 sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
4505 * step one, sort all the leaf pointers so we don't scribble
4506 * randomly into the extent allocation tree
4508 for (i = slot; i < nritems; i++) {
4509 sorted[refi].bytenr = btrfs_node_blockptr(eb, i);
4510 sorted[refi].slot = i;
4515 * nritems won't be zero, but if we're picking up drop_snapshot
4516 * after a crash, slot might be > 0, so double check things
4522 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
4525 * the first loop frees everything the leaves point to
4527 for (i = 0; i < refi; i++) {
4530 bytenr = sorted[i].bytenr;
4533 * check the reference count on this leaf. If it is > 1
4534 * we just decrement it below and don't update any
4535 * of the refs the leaf points to.
4537 ret = drop_snap_lookup_refcount(trans, root, bytenr,
4543 ptr_gen = btrfs_node_ptr_generation(eb, sorted[i].slot);
4546 * the leaf only had one reference, which means the
4547 * only thing pointing to this leaf is the snapshot
4548 * we're deleting. It isn't possible for the reference
4549 * count to increase again later
4551 * The reference cache is checked for the leaf,
4552 * and if found we'll be able to drop any refs held by
4553 * the leaf without needing to read it in.
4555 ref = btrfs_lookup_leaf_ref(root, bytenr);
4556 if (ref && ref->generation != ptr_gen) {
4557 btrfs_free_leaf_ref(root, ref);
4561 ret = cache_drop_leaf_ref(trans, root, ref);
4563 btrfs_remove_leaf_ref(root, ref);
4564 btrfs_free_leaf_ref(root, ref);
4567 * the leaf wasn't in the reference cache, so
4568 * we have to read it.
4570 leaf = read_tree_block(root, bytenr, blocksize,
4572 ret = btrfs_drop_leaf_ref(trans, root, leaf);
4574 free_extent_buffer(leaf);
4576 atomic_inc(&root->fs_info->throttle_gen);
4577 wake_up(&root->fs_info->transaction_throttle);
4582 * run through the loop again to free the refs on the leaves.
4583 * This is faster than doing it in the loop above because
4584 * the leaves are likely to be clustered together. We end up
4585 * working in nice chunks on the extent allocation tree.
4587 for (i = 0; i < refi; i++) {
4588 bytenr = sorted[i].bytenr;
4589 ret = btrfs_free_extent(trans, root, bytenr,
4590 blocksize, eb->start,
4591 root_owner, root_gen, 0, 1);
4594 atomic_inc(&root->fs_info->throttle_gen);
4595 wake_up(&root->fs_info->transaction_throttle);
4602 * update the path to show we've processed the entire level 1
4603 * node. This will get saved into the root's drop_snapshot_progress
4604 * field so these drops are not repeated again if this transaction
4607 path->slots[1] = nritems;
4612 * helper function for drop_snapshot, this walks down the tree dropping ref
4613 * counts as it goes.
4615 static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
4616 struct btrfs_root *root,
4617 struct btrfs_path *path, int *level)
4623 struct extent_buffer *next;
4624 struct extent_buffer *cur;
4625 struct extent_buffer *parent;
4630 WARN_ON(*level < 0);
4631 WARN_ON(*level >= BTRFS_MAX_LEVEL);
4632 ret = drop_snap_lookup_refcount(trans, root, path->nodes[*level]->start,
4633 path->nodes[*level]->len, &refs);
4639 * walk down to the last node level and free all the leaves
4641 while (*level >= 0) {
4642 WARN_ON(*level < 0);
4643 WARN_ON(*level >= BTRFS_MAX_LEVEL);
4644 cur = path->nodes[*level];
4646 if (btrfs_header_level(cur) != *level)
4649 if (path->slots[*level] >=
4650 btrfs_header_nritems(cur))
4653 /* the new code goes down to level 1 and does all the
4654 * leaves pointed to that node in bulk. So, this check
4655 * for level 0 will always be false.
4657 * But, the disk format allows the drop_snapshot_progress
4658 * field in the root to leave things in a state where
4659 * a leaf will need cleaning up here. If someone crashes
4660 * with the old code and then boots with the new code,
4661 * we might find a leaf here.
4664 ret = btrfs_drop_leaf_ref(trans, root, cur);
4670 * once we get to level one, process the whole node
4671 * at once, including everything below it.
4674 ret = drop_level_one_refs(trans, root, path);
4679 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
4680 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
4681 blocksize = btrfs_level_size(root, *level - 1);
4683 ret = drop_snap_lookup_refcount(trans, root, bytenr,
4688 * if there is more than one reference, we don't need
4689 * to read that node to drop any references it has. We
4690 * just drop the ref we hold on that node and move on to the
4691 * next slot in this level.
4694 parent = path->nodes[*level];
4695 root_owner = btrfs_header_owner(parent);
4696 root_gen = btrfs_header_generation(parent);
4697 path->slots[*level]++;
4699 ret = btrfs_free_extent(trans, root, bytenr,
4700 blocksize, parent->start,
4701 root_owner, root_gen,
4705 atomic_inc(&root->fs_info->throttle_gen);
4706 wake_up(&root->fs_info->transaction_throttle);
4713 * we need to keep freeing things in the next level down.
4714 * read the block and loop around to process it
4716 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
4717 WARN_ON(*level <= 0);
4718 if (path->nodes[*level-1])
4719 free_extent_buffer(path->nodes[*level-1]);
4720 path->nodes[*level-1] = next;
4721 *level = btrfs_header_level(next);
4722 path->slots[*level] = 0;
4726 WARN_ON(*level < 0);
4727 WARN_ON(*level >= BTRFS_MAX_LEVEL);
4729 if (path->nodes[*level] == root->node) {
4730 parent = path->nodes[*level];
4731 bytenr = path->nodes[*level]->start;
4733 parent = path->nodes[*level + 1];
4734 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
4737 blocksize = btrfs_level_size(root, *level);
4738 root_owner = btrfs_header_owner(parent);
4739 root_gen = btrfs_header_generation(parent);
4742 * cleanup and free the reference on the last node
4745 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
4746 parent->start, root_owner, root_gen,
4748 free_extent_buffer(path->nodes[*level]);
4749 path->nodes[*level] = NULL;
4759 struct walk_control {
4760 u64 refs[BTRFS_MAX_LEVEL];
4761 u64 flags[BTRFS_MAX_LEVEL];
4762 struct btrfs_key update_progress;
4770 #define DROP_REFERENCE 1
4771 #define UPDATE_BACKREF 2
4774 * hepler to process tree block while walking down the tree.
4776 * when wc->stage == DROP_REFERENCE, this function checks
4777 * reference count of the block. if the block is shared and
4778 * we need update back refs for the subtree rooted at the
4779 * block, this function changes wc->stage to UPDATE_BACKREF
4781 * when wc->stage == UPDATE_BACKREF, this function updates
4782 * back refs for pointers in the block.
4784 * NOTE: return value 1 means we should stop walking down.
4786 static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
4787 struct btrfs_root *root,
4788 struct btrfs_path *path,
4789 struct walk_control *wc)
4791 int level = wc->level;
4792 struct extent_buffer *eb = path->nodes[level];
4793 struct btrfs_key key;
4794 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
4797 if (wc->stage == UPDATE_BACKREF &&
4798 btrfs_header_owner(eb) != root->root_key.objectid)
4802 * when reference count of tree block is 1, it won't increase
4803 * again. once full backref flag is set, we never clear it.
4805 if ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
4806 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag))) {
4807 BUG_ON(!path->locks[level]);
4808 ret = btrfs_lookup_extent_info(trans, root,
4813 BUG_ON(wc->refs[level] == 0);
4816 if (wc->stage == DROP_REFERENCE &&
4817 wc->update_ref && wc->refs[level] > 1) {
4818 BUG_ON(eb == root->node);
4819 BUG_ON(path->slots[level] > 0);
4821 btrfs_item_key_to_cpu(eb, &key, path->slots[level]);
4823 btrfs_node_key_to_cpu(eb, &key, path->slots[level]);
4824 if (btrfs_header_owner(eb) == root->root_key.objectid &&
4825 btrfs_comp_cpu_keys(&key, &wc->update_progress) >= 0) {
4826 wc->stage = UPDATE_BACKREF;
4827 wc->shared_level = level;
4831 if (wc->stage == DROP_REFERENCE) {
4832 if (wc->refs[level] > 1)
4835 if (path->locks[level] && !wc->keep_locks) {
4836 btrfs_tree_unlock(eb);
4837 path->locks[level] = 0;
4842 /* wc->stage == UPDATE_BACKREF */
4843 if (!(wc->flags[level] & flag)) {
4844 BUG_ON(!path->locks[level]);
4845 ret = btrfs_inc_ref(trans, root, eb, 1);
4847 ret = btrfs_dec_ref(trans, root, eb, 0);
4849 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
4852 wc->flags[level] |= flag;
4856 * the block is shared by multiple trees, so it's not good to
4857 * keep the tree lock
4859 if (path->locks[level] && level > 0) {
4860 btrfs_tree_unlock(eb);
4861 path->locks[level] = 0;
4867 * hepler to process tree block while walking up the tree.
4869 * when wc->stage == DROP_REFERENCE, this function drops
4870 * reference count on the block.
4872 * when wc->stage == UPDATE_BACKREF, this function changes
4873 * wc->stage back to DROP_REFERENCE if we changed wc->stage
4874 * to UPDATE_BACKREF previously while processing the block.
4876 * NOTE: return value 1 means we should stop walking up.
4878 static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
4879 struct btrfs_root *root,
4880 struct btrfs_path *path,
4881 struct walk_control *wc)
4884 int level = wc->level;
4885 struct extent_buffer *eb = path->nodes[level];
4888 if (wc->stage == UPDATE_BACKREF) {
4889 BUG_ON(wc->shared_level < level);
4890 if (level < wc->shared_level)
4893 BUG_ON(wc->refs[level] <= 1);
4894 ret = find_next_key(path, level + 1, &wc->update_progress);
4898 wc->stage = DROP_REFERENCE;
4899 wc->shared_level = -1;
4900 path->slots[level] = 0;
4903 * check reference count again if the block isn't locked.
4904 * we should start walking down the tree again if reference
4907 if (!path->locks[level]) {
4909 btrfs_tree_lock(eb);
4910 btrfs_set_lock_blocking(eb);
4911 path->locks[level] = 1;
4913 ret = btrfs_lookup_extent_info(trans, root,
4918 BUG_ON(wc->refs[level] == 0);
4919 if (wc->refs[level] == 1) {
4920 btrfs_tree_unlock(eb);
4921 path->locks[level] = 0;
4929 /* wc->stage == DROP_REFERENCE */
4930 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
4932 if (wc->refs[level] == 1) {
4934 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
4935 ret = btrfs_dec_ref(trans, root, eb, 1);
4937 ret = btrfs_dec_ref(trans, root, eb, 0);
4940 /* make block locked assertion in clean_tree_block happy */
4941 if (!path->locks[level] &&
4942 btrfs_header_generation(eb) == trans->transid) {
4943 btrfs_tree_lock(eb);
4944 btrfs_set_lock_blocking(eb);
4945 path->locks[level] = 1;
4947 clean_tree_block(trans, root, eb);
4950 if (eb == root->node) {
4951 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
4954 BUG_ON(root->root_key.objectid !=
4955 btrfs_header_owner(eb));
4957 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
4958 parent = path->nodes[level + 1]->start;
4960 BUG_ON(root->root_key.objectid !=
4961 btrfs_header_owner(path->nodes[level + 1]));
4964 ret = btrfs_free_extent(trans, root, eb->start, eb->len, parent,
4965 root->root_key.objectid, level, 0);
4968 wc->refs[level] = 0;
4969 wc->flags[level] = 0;
4973 static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
4974 struct btrfs_root *root,
4975 struct btrfs_path *path,
4976 struct walk_control *wc)
4978 struct extent_buffer *next;
4979 struct extent_buffer *cur;
4983 int level = wc->level;
4986 while (level >= 0) {
4987 cur = path->nodes[level];
4988 BUG_ON(path->slots[level] >= btrfs_header_nritems(cur));
4990 ret = walk_down_proc(trans, root, path, wc);
4997 bytenr = btrfs_node_blockptr(cur, path->slots[level]);
4998 blocksize = btrfs_level_size(root, level - 1);
4999 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[level]);
5001 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
5002 btrfs_tree_lock(next);
5003 btrfs_set_lock_blocking(next);
5006 BUG_ON(level != btrfs_header_level(next));
5007 path->nodes[level] = next;
5008 path->slots[level] = 0;
5009 path->locks[level] = 1;
5015 static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
5016 struct btrfs_root *root,
5017 struct btrfs_path *path,
5018 struct walk_control *wc, int max_level)
5020 int level = wc->level;
5023 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
5024 while (level < max_level && path->nodes[level]) {
5026 if (path->slots[level] + 1 <
5027 btrfs_header_nritems(path->nodes[level])) {
5028 path->slots[level]++;
5031 ret = walk_up_proc(trans, root, path, wc);
5035 if (path->locks[level]) {
5036 btrfs_tree_unlock(path->nodes[level]);
5037 path->locks[level] = 0;
5039 free_extent_buffer(path->nodes[level]);
5040 path->nodes[level] = NULL;
5048 * drop a subvolume tree.
5050 * this function traverses the tree freeing any blocks that only
5051 * referenced by the tree.
5053 * when a shared tree block is found. this function decreases its
5054 * reference count by one. if update_ref is true, this function
5055 * also make sure backrefs for the shared block and all lower level
5056 * blocks are properly updated.
5058 int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref)
5060 struct btrfs_path *path;
5061 struct btrfs_trans_handle *trans;
5062 struct btrfs_root *tree_root = root->fs_info->tree_root;
5063 struct btrfs_root_item *root_item = &root->root_item;
5064 struct walk_control *wc;
5065 struct btrfs_key key;
5070 path = btrfs_alloc_path();
5073 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5076 trans = btrfs_start_transaction(tree_root, 1);
5078 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
5079 level = btrfs_header_level(root->node);
5080 path->nodes[level] = btrfs_lock_root_node(root);
5081 btrfs_set_lock_blocking(path->nodes[level]);
5082 path->slots[level] = 0;
5083 path->locks[level] = 1;
5084 memset(&wc->update_progress, 0,
5085 sizeof(wc->update_progress));
5087 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
5088 memcpy(&wc->update_progress, &key,
5089 sizeof(wc->update_progress));
5091 level = root_item->drop_level;
5093 path->lowest_level = level;
5094 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5095 path->lowest_level = 0;
5100 btrfs_node_key_to_cpu(path->nodes[level], &key,
5101 path->slots[level]);
5102 WARN_ON(memcmp(&key, &wc->update_progress, sizeof(key)));
5105 * unlock our path, this is safe because only this
5106 * function is allowed to delete this snapshot
5108 btrfs_unlock_up_safe(path, 0);
5110 level = btrfs_header_level(root->node);
5112 btrfs_tree_lock(path->nodes[level]);
5113 btrfs_set_lock_blocking(path->nodes[level]);
5115 ret = btrfs_lookup_extent_info(trans, root,
5116 path->nodes[level]->start,
5117 path->nodes[level]->len,
5121 BUG_ON(wc->refs[level] == 0);
5123 if (level == root_item->drop_level)
5126 btrfs_tree_unlock(path->nodes[level]);
5127 WARN_ON(wc->refs[level] != 1);
5133 wc->shared_level = -1;
5134 wc->stage = DROP_REFERENCE;
5135 wc->update_ref = update_ref;
5139 ret = walk_down_tree(trans, root, path, wc);
5145 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5152 BUG_ON(wc->stage != DROP_REFERENCE);
5156 if (wc->stage == DROP_REFERENCE) {
5158 btrfs_node_key(path->nodes[level],
5159 &root_item->drop_progress,
5160 path->slots[level]);
5161 root_item->drop_level = level;
5164 BUG_ON(wc->level == 0);
5165 if (trans->transaction->in_commit ||
5166 trans->transaction->delayed_refs.flushing) {
5167 ret = btrfs_update_root(trans, tree_root,
5172 btrfs_end_transaction(trans, tree_root);
5173 trans = btrfs_start_transaction(tree_root, 1);
5175 unsigned long update;
5176 update = trans->delayed_ref_updates;
5177 trans->delayed_ref_updates = 0;
5179 btrfs_run_delayed_refs(trans, tree_root,
5183 btrfs_release_path(root, path);
5186 ret = btrfs_del_root(trans, tree_root, &root->root_key);
5189 free_extent_buffer(root->node);
5190 free_extent_buffer(root->commit_root);
5193 btrfs_end_transaction(trans, tree_root);
5195 btrfs_free_path(path);
5200 * drop subtree rooted at tree block 'node'.
5202 * NOTE: this function will unlock and release tree block 'node'
5204 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
5205 struct btrfs_root *root,
5206 struct extent_buffer *node,
5207 struct extent_buffer *parent)
5209 struct btrfs_path *path;
5210 struct walk_control *wc;
5216 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5218 path = btrfs_alloc_path();
5221 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5224 btrfs_assert_tree_locked(parent);
5225 parent_level = btrfs_header_level(parent);
5226 extent_buffer_get(parent);
5227 path->nodes[parent_level] = parent;
5228 path->slots[parent_level] = btrfs_header_nritems(parent);
5230 btrfs_assert_tree_locked(node);
5231 level = btrfs_header_level(node);
5232 path->nodes[level] = node;
5233 path->slots[level] = 0;
5234 path->locks[level] = 1;
5236 wc->refs[parent_level] = 1;
5237 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5239 wc->shared_level = -1;
5240 wc->stage = DROP_REFERENCE;
5245 wret = walk_down_tree(trans, root, path, wc);
5251 wret = walk_up_tree(trans, root, path, wc, parent_level);
5259 btrfs_free_path(path);
5264 static unsigned long calc_ra(unsigned long start, unsigned long last,
5267 return min(last, start + nr - 1);
5270 static noinline int relocate_inode_pages(struct inode *inode, u64 start,
5275 unsigned long first_index;
5276 unsigned long last_index;
5279 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5280 struct file_ra_state *ra;
5281 struct btrfs_ordered_extent *ordered;
5282 unsigned int total_read = 0;
5283 unsigned int total_dirty = 0;
5286 ra = kzalloc(sizeof(*ra), GFP_NOFS);
5288 mutex_lock(&inode->i_mutex);
5289 first_index = start >> PAGE_CACHE_SHIFT;
5290 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
5292 /* make sure the dirty trick played by the caller work */
5293 ret = invalidate_inode_pages2_range(inode->i_mapping,
5294 first_index, last_index);
5298 file_ra_state_init(ra, inode->i_mapping);
5300 for (i = first_index ; i <= last_index; i++) {
5301 if (total_read % ra->ra_pages == 0) {
5302 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
5303 calc_ra(i, last_index, ra->ra_pages));
5307 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
5309 page = grab_cache_page(inode->i_mapping, i);
5314 if (!PageUptodate(page)) {
5315 btrfs_readpage(NULL, page);
5317 if (!PageUptodate(page)) {
5319 page_cache_release(page);
5324 wait_on_page_writeback(page);
5326 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
5327 page_end = page_start + PAGE_CACHE_SIZE - 1;
5328 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
5330 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5332 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5334 page_cache_release(page);
5335 btrfs_start_ordered_extent(inode, ordered, 1);
5336 btrfs_put_ordered_extent(ordered);
5339 set_page_extent_mapped(page);
5341 if (i == first_index)
5342 set_extent_bits(io_tree, page_start, page_end,
5343 EXTENT_BOUNDARY, GFP_NOFS);
5344 btrfs_set_extent_delalloc(inode, page_start, page_end);
5346 set_page_dirty(page);
5349 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5351 page_cache_release(page);
5356 mutex_unlock(&inode->i_mutex);
5357 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
5361 static noinline int relocate_data_extent(struct inode *reloc_inode,
5362 struct btrfs_key *extent_key,
5365 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5366 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
5367 struct extent_map *em;
5368 u64 start = extent_key->objectid - offset;
5369 u64 end = start + extent_key->offset - 1;
5371 em = alloc_extent_map(GFP_NOFS);
5372 BUG_ON(!em || IS_ERR(em));
5375 em->len = extent_key->offset;
5376 em->block_len = extent_key->offset;
5377 em->block_start = extent_key->objectid;
5378 em->bdev = root->fs_info->fs_devices->latest_bdev;
5379 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5381 /* setup extent map to cheat btrfs_readpage */
5382 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
5385 spin_lock(&em_tree->lock);
5386 ret = add_extent_mapping(em_tree, em);
5387 spin_unlock(&em_tree->lock);
5388 if (ret != -EEXIST) {
5389 free_extent_map(em);
5392 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
5394 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
5396 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
5399 struct btrfs_ref_path {
5401 u64 nodes[BTRFS_MAX_LEVEL];
5403 u64 root_generation;
5410 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
5411 u64 new_nodes[BTRFS_MAX_LEVEL];
5414 struct disk_extent {
5425 static int is_cowonly_root(u64 root_objectid)
5427 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
5428 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
5429 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
5430 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
5431 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5432 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
5437 static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
5438 struct btrfs_root *extent_root,
5439 struct btrfs_ref_path *ref_path,
5442 struct extent_buffer *leaf;
5443 struct btrfs_path *path;
5444 struct btrfs_extent_ref *ref;
5445 struct btrfs_key key;
5446 struct btrfs_key found_key;
5452 path = btrfs_alloc_path();
5457 ref_path->lowest_level = -1;
5458 ref_path->current_level = -1;
5459 ref_path->shared_level = -1;
5463 level = ref_path->current_level - 1;
5464 while (level >= -1) {
5466 if (level < ref_path->lowest_level)
5470 bytenr = ref_path->nodes[level];
5472 bytenr = ref_path->extent_start;
5473 BUG_ON(bytenr == 0);
5475 parent = ref_path->nodes[level + 1];
5476 ref_path->nodes[level + 1] = 0;
5477 ref_path->current_level = level;
5478 BUG_ON(parent == 0);
5480 key.objectid = bytenr;
5481 key.offset = parent + 1;
5482 key.type = BTRFS_EXTENT_REF_KEY;
5484 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
5489 leaf = path->nodes[0];
5490 nritems = btrfs_header_nritems(leaf);
5491 if (path->slots[0] >= nritems) {
5492 ret = btrfs_next_leaf(extent_root, path);
5497 leaf = path->nodes[0];
5500 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5501 if (found_key.objectid == bytenr &&
5502 found_key.type == BTRFS_EXTENT_REF_KEY) {
5503 if (level < ref_path->shared_level)
5504 ref_path->shared_level = level;
5509 btrfs_release_path(extent_root, path);
5512 /* reached lowest level */
5516 level = ref_path->current_level;
5517 while (level < BTRFS_MAX_LEVEL - 1) {
5521 bytenr = ref_path->nodes[level];
5523 bytenr = ref_path->extent_start;
5525 BUG_ON(bytenr == 0);
5527 key.objectid = bytenr;
5529 key.type = BTRFS_EXTENT_REF_KEY;
5531 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
5535 leaf = path->nodes[0];
5536 nritems = btrfs_header_nritems(leaf);
5537 if (path->slots[0] >= nritems) {
5538 ret = btrfs_next_leaf(extent_root, path);
5542 /* the extent was freed by someone */
5543 if (ref_path->lowest_level == level)
5545 btrfs_release_path(extent_root, path);
5548 leaf = path->nodes[0];
5551 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5552 if (found_key.objectid != bytenr ||
5553 found_key.type != BTRFS_EXTENT_REF_KEY) {
5554 /* the extent was freed by someone */
5555 if (ref_path->lowest_level == level) {
5559 btrfs_release_path(extent_root, path);
5563 ref = btrfs_item_ptr(leaf, path->slots[0],
5564 struct btrfs_extent_ref);
5565 ref_objectid = btrfs_ref_objectid(leaf, ref);
5566 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5568 level = (int)ref_objectid;
5569 BUG_ON(level >= BTRFS_MAX_LEVEL);
5570 ref_path->lowest_level = level;
5571 ref_path->current_level = level;
5572 ref_path->nodes[level] = bytenr;
5574 WARN_ON(ref_objectid != level);
5577 WARN_ON(level != -1);
5581 if (ref_path->lowest_level == level) {
5582 ref_path->owner_objectid = ref_objectid;
5583 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
5587 * the block is tree root or the block isn't in reference
5590 if (found_key.objectid == found_key.offset ||
5591 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
5592 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5593 ref_path->root_generation =
5594 btrfs_ref_generation(leaf, ref);
5596 /* special reference from the tree log */
5597 ref_path->nodes[0] = found_key.offset;
5598 ref_path->current_level = 0;
5605 BUG_ON(ref_path->nodes[level] != 0);
5606 ref_path->nodes[level] = found_key.offset;
5607 ref_path->current_level = level;
5610 * the reference was created in the running transaction,
5611 * no need to continue walking up.
5613 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
5614 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5615 ref_path->root_generation =
5616 btrfs_ref_generation(leaf, ref);
5621 btrfs_release_path(extent_root, path);
5624 /* reached max tree level, but no tree root found. */
5627 btrfs_free_path(path);
5631 static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
5632 struct btrfs_root *extent_root,
5633 struct btrfs_ref_path *ref_path,
5636 memset(ref_path, 0, sizeof(*ref_path));
5637 ref_path->extent_start = extent_start;
5639 return __next_ref_path(trans, extent_root, ref_path, 1);
5642 static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
5643 struct btrfs_root *extent_root,
5644 struct btrfs_ref_path *ref_path)
5646 return __next_ref_path(trans, extent_root, ref_path, 0);
5649 static noinline int get_new_locations(struct inode *reloc_inode,
5650 struct btrfs_key *extent_key,
5651 u64 offset, int no_fragment,
5652 struct disk_extent **extents,
5655 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5656 struct btrfs_path *path;
5657 struct btrfs_file_extent_item *fi;
5658 struct extent_buffer *leaf;
5659 struct disk_extent *exts = *extents;
5660 struct btrfs_key found_key;
5665 int max = *nr_extents;
5668 WARN_ON(!no_fragment && *extents);
5671 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
5676 path = btrfs_alloc_path();
5679 cur_pos = extent_key->objectid - offset;
5680 last_byte = extent_key->objectid + extent_key->offset;
5681 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
5691 leaf = path->nodes[0];
5692 nritems = btrfs_header_nritems(leaf);
5693 if (path->slots[0] >= nritems) {
5694 ret = btrfs_next_leaf(root, path);
5699 leaf = path->nodes[0];
5702 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5703 if (found_key.offset != cur_pos ||
5704 found_key.type != BTRFS_EXTENT_DATA_KEY ||
5705 found_key.objectid != reloc_inode->i_ino)
5708 fi = btrfs_item_ptr(leaf, path->slots[0],
5709 struct btrfs_file_extent_item);
5710 if (btrfs_file_extent_type(leaf, fi) !=
5711 BTRFS_FILE_EXTENT_REG ||
5712 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
5716 struct disk_extent *old = exts;
5718 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
5719 memcpy(exts, old, sizeof(*exts) * nr);
5720 if (old != *extents)
5724 exts[nr].disk_bytenr =
5725 btrfs_file_extent_disk_bytenr(leaf, fi);
5726 exts[nr].disk_num_bytes =
5727 btrfs_file_extent_disk_num_bytes(leaf, fi);
5728 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
5729 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
5730 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
5731 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
5732 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
5733 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
5735 BUG_ON(exts[nr].offset > 0);
5736 BUG_ON(exts[nr].compression || exts[nr].encryption);
5737 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
5739 cur_pos += exts[nr].num_bytes;
5742 if (cur_pos + offset >= last_byte)
5752 BUG_ON(cur_pos + offset > last_byte);
5753 if (cur_pos + offset < last_byte) {
5759 btrfs_free_path(path);
5761 if (exts != *extents)
5770 static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
5771 struct btrfs_root *root,
5772 struct btrfs_path *path,
5773 struct btrfs_key *extent_key,
5774 struct btrfs_key *leaf_key,
5775 struct btrfs_ref_path *ref_path,
5776 struct disk_extent *new_extents,
5779 struct extent_buffer *leaf;
5780 struct btrfs_file_extent_item *fi;
5781 struct inode *inode = NULL;
5782 struct btrfs_key key;
5787 u64 search_end = (u64)-1;
5790 int extent_locked = 0;
5794 memcpy(&key, leaf_key, sizeof(key));
5795 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
5796 if (key.objectid < ref_path->owner_objectid ||
5797 (key.objectid == ref_path->owner_objectid &&
5798 key.type < BTRFS_EXTENT_DATA_KEY)) {
5799 key.objectid = ref_path->owner_objectid;
5800 key.type = BTRFS_EXTENT_DATA_KEY;
5806 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
5810 leaf = path->nodes[0];
5811 nritems = btrfs_header_nritems(leaf);
5813 if (extent_locked && ret > 0) {
5815 * the file extent item was modified by someone
5816 * before the extent got locked.
5818 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5819 lock_end, GFP_NOFS);
5823 if (path->slots[0] >= nritems) {
5824 if (++nr_scaned > 2)
5827 BUG_ON(extent_locked);
5828 ret = btrfs_next_leaf(root, path);
5833 leaf = path->nodes[0];
5834 nritems = btrfs_header_nritems(leaf);
5837 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5839 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
5840 if ((key.objectid > ref_path->owner_objectid) ||
5841 (key.objectid == ref_path->owner_objectid &&
5842 key.type > BTRFS_EXTENT_DATA_KEY) ||
5843 key.offset >= search_end)
5847 if (inode && key.objectid != inode->i_ino) {
5848 BUG_ON(extent_locked);
5849 btrfs_release_path(root, path);
5850 mutex_unlock(&inode->i_mutex);
5856 if (key.type != BTRFS_EXTENT_DATA_KEY) {
5861 fi = btrfs_item_ptr(leaf, path->slots[0],
5862 struct btrfs_file_extent_item);
5863 extent_type = btrfs_file_extent_type(leaf, fi);
5864 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
5865 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
5866 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
5867 extent_key->objectid)) {
5873 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
5874 ext_offset = btrfs_file_extent_offset(leaf, fi);
5876 if (search_end == (u64)-1) {
5877 search_end = key.offset - ext_offset +
5878 btrfs_file_extent_ram_bytes(leaf, fi);
5881 if (!extent_locked) {
5882 lock_start = key.offset;
5883 lock_end = lock_start + num_bytes - 1;
5885 if (lock_start > key.offset ||
5886 lock_end + 1 < key.offset + num_bytes) {
5887 unlock_extent(&BTRFS_I(inode)->io_tree,
5888 lock_start, lock_end, GFP_NOFS);
5894 btrfs_release_path(root, path);
5896 inode = btrfs_iget_locked(root->fs_info->sb,
5897 key.objectid, root);
5898 if (inode->i_state & I_NEW) {
5899 BTRFS_I(inode)->root = root;
5900 BTRFS_I(inode)->location.objectid =
5902 BTRFS_I(inode)->location.type =
5903 BTRFS_INODE_ITEM_KEY;
5904 BTRFS_I(inode)->location.offset = 0;
5905 btrfs_read_locked_inode(inode);
5906 unlock_new_inode(inode);
5909 * some code call btrfs_commit_transaction while
5910 * holding the i_mutex, so we can't use mutex_lock
5913 if (is_bad_inode(inode) ||
5914 !mutex_trylock(&inode->i_mutex)) {
5917 key.offset = (u64)-1;
5922 if (!extent_locked) {
5923 struct btrfs_ordered_extent *ordered;
5925 btrfs_release_path(root, path);
5927 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5928 lock_end, GFP_NOFS);
5929 ordered = btrfs_lookup_first_ordered_extent(inode,
5932 ordered->file_offset <= lock_end &&
5933 ordered->file_offset + ordered->len > lock_start) {
5934 unlock_extent(&BTRFS_I(inode)->io_tree,
5935 lock_start, lock_end, GFP_NOFS);
5936 btrfs_start_ordered_extent(inode, ordered, 1);
5937 btrfs_put_ordered_extent(ordered);
5938 key.offset += num_bytes;
5942 btrfs_put_ordered_extent(ordered);
5948 if (nr_extents == 1) {
5949 /* update extent pointer in place */
5950 btrfs_set_file_extent_disk_bytenr(leaf, fi,
5951 new_extents[0].disk_bytenr);
5952 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
5953 new_extents[0].disk_num_bytes);
5954 btrfs_mark_buffer_dirty(leaf);
5956 btrfs_drop_extent_cache(inode, key.offset,
5957 key.offset + num_bytes - 1, 0);
5959 ret = btrfs_inc_extent_ref(trans, root,
5960 new_extents[0].disk_bytenr,
5961 new_extents[0].disk_num_bytes,
5963 root->root_key.objectid,
5968 ret = btrfs_free_extent(trans, root,
5969 extent_key->objectid,
5972 btrfs_header_owner(leaf),
5973 btrfs_header_generation(leaf),
5977 btrfs_release_path(root, path);
5978 key.offset += num_bytes;
5986 * drop old extent pointer at first, then insert the
5987 * new pointers one bye one
5989 btrfs_release_path(root, path);
5990 ret = btrfs_drop_extents(trans, root, inode, key.offset,
5991 key.offset + num_bytes,
5992 key.offset, &alloc_hint);
5995 for (i = 0; i < nr_extents; i++) {
5996 if (ext_offset >= new_extents[i].num_bytes) {
5997 ext_offset -= new_extents[i].num_bytes;
6000 extent_len = min(new_extents[i].num_bytes -
6001 ext_offset, num_bytes);
6003 ret = btrfs_insert_empty_item(trans, root,
6008 leaf = path->nodes[0];
6009 fi = btrfs_item_ptr(leaf, path->slots[0],
6010 struct btrfs_file_extent_item);
6011 btrfs_set_file_extent_generation(leaf, fi,
6013 btrfs_set_file_extent_type(leaf, fi,
6014 BTRFS_FILE_EXTENT_REG);
6015 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6016 new_extents[i].disk_bytenr);
6017 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6018 new_extents[i].disk_num_bytes);
6019 btrfs_set_file_extent_ram_bytes(leaf, fi,
6020 new_extents[i].ram_bytes);
6022 btrfs_set_file_extent_compression(leaf, fi,
6023 new_extents[i].compression);
6024 btrfs_set_file_extent_encryption(leaf, fi,
6025 new_extents[i].encryption);
6026 btrfs_set_file_extent_other_encoding(leaf, fi,
6027 new_extents[i].other_encoding);
6029 btrfs_set_file_extent_num_bytes(leaf, fi,
6031 ext_offset += new_extents[i].offset;
6032 btrfs_set_file_extent_offset(leaf, fi,
6034 btrfs_mark_buffer_dirty(leaf);
6036 btrfs_drop_extent_cache(inode, key.offset,
6037 key.offset + extent_len - 1, 0);
6039 ret = btrfs_inc_extent_ref(trans, root,
6040 new_extents[i].disk_bytenr,
6041 new_extents[i].disk_num_bytes,
6043 root->root_key.objectid,
6044 trans->transid, key.objectid);
6046 btrfs_release_path(root, path);
6048 inode_add_bytes(inode, extent_len);
6051 num_bytes -= extent_len;
6052 key.offset += extent_len;
6057 BUG_ON(i >= nr_extents);
6061 if (extent_locked) {
6062 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6063 lock_end, GFP_NOFS);
6067 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
6068 key.offset >= search_end)
6075 btrfs_release_path(root, path);
6077 mutex_unlock(&inode->i_mutex);
6078 if (extent_locked) {
6079 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6080 lock_end, GFP_NOFS);
6087 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
6088 struct btrfs_root *root,
6089 struct extent_buffer *buf, u64 orig_start)
6094 BUG_ON(btrfs_header_generation(buf) != trans->transid);
6095 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6097 level = btrfs_header_level(buf);
6099 struct btrfs_leaf_ref *ref;
6100 struct btrfs_leaf_ref *orig_ref;
6102 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
6106 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
6108 btrfs_free_leaf_ref(root, orig_ref);
6112 ref->nritems = orig_ref->nritems;
6113 memcpy(ref->extents, orig_ref->extents,
6114 sizeof(ref->extents[0]) * ref->nritems);
6116 btrfs_free_leaf_ref(root, orig_ref);
6118 ref->root_gen = trans->transid;
6119 ref->bytenr = buf->start;
6120 ref->owner = btrfs_header_owner(buf);
6121 ref->generation = btrfs_header_generation(buf);
6123 ret = btrfs_add_leaf_ref(root, ref, 0);
6125 btrfs_free_leaf_ref(root, ref);
6130 static noinline int invalidate_extent_cache(struct btrfs_root *root,
6131 struct extent_buffer *leaf,
6132 struct btrfs_block_group_cache *group,
6133 struct btrfs_root *target_root)
6135 struct btrfs_key key;
6136 struct inode *inode = NULL;
6137 struct btrfs_file_extent_item *fi;
6139 u64 skip_objectid = 0;
6143 nritems = btrfs_header_nritems(leaf);
6144 for (i = 0; i < nritems; i++) {
6145 btrfs_item_key_to_cpu(leaf, &key, i);
6146 if (key.objectid == skip_objectid ||
6147 key.type != BTRFS_EXTENT_DATA_KEY)
6149 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6150 if (btrfs_file_extent_type(leaf, fi) ==
6151 BTRFS_FILE_EXTENT_INLINE)
6153 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6155 if (!inode || inode->i_ino != key.objectid) {
6157 inode = btrfs_ilookup(target_root->fs_info->sb,
6158 key.objectid, target_root, 1);
6161 skip_objectid = key.objectid;
6164 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6166 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
6167 key.offset + num_bytes - 1, GFP_NOFS);
6168 btrfs_drop_extent_cache(inode, key.offset,
6169 key.offset + num_bytes - 1, 1);
6170 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
6171 key.offset + num_bytes - 1, GFP_NOFS);
6178 static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
6179 struct btrfs_root *root,
6180 struct extent_buffer *leaf,
6181 struct btrfs_block_group_cache *group,
6182 struct inode *reloc_inode)
6184 struct btrfs_key key;
6185 struct btrfs_key extent_key;
6186 struct btrfs_file_extent_item *fi;
6187 struct btrfs_leaf_ref *ref;
6188 struct disk_extent *new_extent;
6197 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
6198 BUG_ON(!new_extent);
6200 ref = btrfs_lookup_leaf_ref(root, leaf->start);
6204 nritems = btrfs_header_nritems(leaf);
6205 for (i = 0; i < nritems; i++) {
6206 btrfs_item_key_to_cpu(leaf, &key, i);
6207 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6209 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6210 if (btrfs_file_extent_type(leaf, fi) ==
6211 BTRFS_FILE_EXTENT_INLINE)
6213 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6214 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
6219 if (bytenr >= group->key.objectid + group->key.offset ||
6220 bytenr + num_bytes <= group->key.objectid)
6223 extent_key.objectid = bytenr;
6224 extent_key.offset = num_bytes;
6225 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
6227 ret = get_new_locations(reloc_inode, &extent_key,
6228 group->key.objectid, 1,
6229 &new_extent, &nr_extent);
6234 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
6235 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
6236 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
6237 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
6239 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6240 new_extent->disk_bytenr);
6241 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6242 new_extent->disk_num_bytes);
6243 btrfs_mark_buffer_dirty(leaf);
6245 ret = btrfs_inc_extent_ref(trans, root,
6246 new_extent->disk_bytenr,
6247 new_extent->disk_num_bytes,
6249 root->root_key.objectid,
6250 trans->transid, key.objectid);
6253 ret = btrfs_free_extent(trans, root,
6254 bytenr, num_bytes, leaf->start,
6255 btrfs_header_owner(leaf),
6256 btrfs_header_generation(leaf),
6262 BUG_ON(ext_index + 1 != ref->nritems);
6263 btrfs_free_leaf_ref(root, ref);
6267 int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
6268 struct btrfs_root *root)
6270 struct btrfs_root *reloc_root;
6273 if (root->reloc_root) {
6274 reloc_root = root->reloc_root;
6275 root->reloc_root = NULL;
6276 list_add(&reloc_root->dead_list,
6277 &root->fs_info->dead_reloc_roots);
6279 btrfs_set_root_bytenr(&reloc_root->root_item,
6280 reloc_root->node->start);
6281 btrfs_set_root_level(&root->root_item,
6282 btrfs_header_level(reloc_root->node));
6283 memset(&reloc_root->root_item.drop_progress, 0,
6284 sizeof(struct btrfs_disk_key));
6285 reloc_root->root_item.drop_level = 0;
6287 ret = btrfs_update_root(trans, root->fs_info->tree_root,
6288 &reloc_root->root_key,
6289 &reloc_root->root_item);
6295 int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
6297 struct btrfs_trans_handle *trans;
6298 struct btrfs_root *reloc_root;
6299 struct btrfs_root *prev_root = NULL;
6300 struct list_head dead_roots;
6304 INIT_LIST_HEAD(&dead_roots);
6305 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
6307 while (!list_empty(&dead_roots)) {
6308 reloc_root = list_entry(dead_roots.prev,
6309 struct btrfs_root, dead_list);
6310 list_del_init(&reloc_root->dead_list);
6312 BUG_ON(reloc_root->commit_root != NULL);
6314 trans = btrfs_join_transaction(root, 1);
6317 mutex_lock(&root->fs_info->drop_mutex);
6318 ret = btrfs_drop_snapshot(trans, reloc_root);
6321 mutex_unlock(&root->fs_info->drop_mutex);
6323 nr = trans->blocks_used;
6324 ret = btrfs_end_transaction(trans, root);
6326 btrfs_btree_balance_dirty(root, nr);
6329 free_extent_buffer(reloc_root->node);
6331 ret = btrfs_del_root(trans, root->fs_info->tree_root,
6332 &reloc_root->root_key);
6334 mutex_unlock(&root->fs_info->drop_mutex);
6336 nr = trans->blocks_used;
6337 ret = btrfs_end_transaction(trans, root);
6339 btrfs_btree_balance_dirty(root, nr);
6342 prev_root = reloc_root;
6345 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
6351 int btrfs_add_dead_reloc_root(struct btrfs_root *root)
6353 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
6357 int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
6359 struct btrfs_root *reloc_root;
6360 struct btrfs_trans_handle *trans;
6361 struct btrfs_key location;
6365 mutex_lock(&root->fs_info->tree_reloc_mutex);
6366 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
6368 found = !list_empty(&root->fs_info->dead_reloc_roots);
6369 mutex_unlock(&root->fs_info->tree_reloc_mutex);
6372 trans = btrfs_start_transaction(root, 1);
6374 ret = btrfs_commit_transaction(trans, root);
6378 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6379 location.offset = (u64)-1;
6380 location.type = BTRFS_ROOT_ITEM_KEY;
6382 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
6383 BUG_ON(!reloc_root);
6384 btrfs_orphan_cleanup(reloc_root);
6388 static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
6389 struct btrfs_root *root)
6391 struct btrfs_root *reloc_root;
6392 struct extent_buffer *eb;
6393 struct btrfs_root_item *root_item;
6394 struct btrfs_key root_key;
6397 BUG_ON(!root->ref_cows);
6398 if (root->reloc_root)
6401 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
6404 ret = btrfs_copy_root(trans, root, root->commit_root,
6405 &eb, BTRFS_TREE_RELOC_OBJECTID);
6408 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
6409 root_key.offset = root->root_key.objectid;
6410 root_key.type = BTRFS_ROOT_ITEM_KEY;
6412 memcpy(root_item, &root->root_item, sizeof(root_item));
6413 btrfs_set_root_refs(root_item, 0);
6414 btrfs_set_root_bytenr(root_item, eb->start);
6415 btrfs_set_root_level(root_item, btrfs_header_level(eb));
6416 btrfs_set_root_generation(root_item, trans->transid);
6418 btrfs_tree_unlock(eb);
6419 free_extent_buffer(eb);
6421 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
6422 &root_key, root_item);
6426 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
6428 BUG_ON(!reloc_root);
6429 reloc_root->last_trans = trans->transid;
6430 reloc_root->commit_root = NULL;
6431 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
6433 root->reloc_root = reloc_root;
6438 * Core function of space balance.
6440 * The idea is using reloc trees to relocate tree blocks in reference
6441 * counted roots. There is one reloc tree for each subvol, and all
6442 * reloc trees share same root key objectid. Reloc trees are snapshots
6443 * of the latest committed roots of subvols (root->commit_root).
6445 * To relocate a tree block referenced by a subvol, there are two steps.
6446 * COW the block through subvol's reloc tree, then update block pointer
6447 * in the subvol to point to the new block. Since all reloc trees share
6448 * same root key objectid, doing special handing for tree blocks owned
6449 * by them is easy. Once a tree block has been COWed in one reloc tree,
6450 * we can use the resulting new block directly when the same block is
6451 * required to COW again through other reloc trees. By this way, relocated
6452 * tree blocks are shared between reloc trees, so they are also shared
6455 static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
6456 struct btrfs_root *root,
6457 struct btrfs_path *path,
6458 struct btrfs_key *first_key,
6459 struct btrfs_ref_path *ref_path,
6460 struct btrfs_block_group_cache *group,
6461 struct inode *reloc_inode)
6463 struct btrfs_root *reloc_root;
6464 struct extent_buffer *eb = NULL;
6465 struct btrfs_key *keys;
6469 int lowest_level = 0;
6472 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
6473 lowest_level = ref_path->owner_objectid;
6475 if (!root->ref_cows) {
6476 path->lowest_level = lowest_level;
6477 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
6479 path->lowest_level = 0;
6480 btrfs_release_path(root, path);
6484 mutex_lock(&root->fs_info->tree_reloc_mutex);
6485 ret = init_reloc_tree(trans, root);
6487 reloc_root = root->reloc_root;
6489 shared_level = ref_path->shared_level;
6490 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
6492 keys = ref_path->node_keys;
6493 nodes = ref_path->new_nodes;
6494 memset(&keys[shared_level + 1], 0,
6495 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
6496 memset(&nodes[shared_level + 1], 0,
6497 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
6499 if (nodes[lowest_level] == 0) {
6500 path->lowest_level = lowest_level;
6501 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6504 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
6505 eb = path->nodes[level];
6506 if (!eb || eb == reloc_root->node)
6508 nodes[level] = eb->start;
6510 btrfs_item_key_to_cpu(eb, &keys[level], 0);
6512 btrfs_node_key_to_cpu(eb, &keys[level], 0);
6515 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6516 eb = path->nodes[0];
6517 ret = replace_extents_in_leaf(trans, reloc_root, eb,
6518 group, reloc_inode);
6521 btrfs_release_path(reloc_root, path);
6523 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
6529 * replace tree blocks in the fs tree with tree blocks in
6532 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
6535 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6536 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6539 extent_buffer_get(path->nodes[0]);
6540 eb = path->nodes[0];
6541 btrfs_release_path(reloc_root, path);
6542 ret = invalidate_extent_cache(reloc_root, eb, group, root);
6544 free_extent_buffer(eb);
6547 mutex_unlock(&root->fs_info->tree_reloc_mutex);
6548 path->lowest_level = 0;
6552 static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
6553 struct btrfs_root *root,
6554 struct btrfs_path *path,
6555 struct btrfs_key *first_key,
6556 struct btrfs_ref_path *ref_path)
6560 ret = relocate_one_path(trans, root, path, first_key,
6561 ref_path, NULL, NULL);
6567 static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
6568 struct btrfs_root *extent_root,
6569 struct btrfs_path *path,
6570 struct btrfs_key *extent_key)
6574 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
6577 ret = btrfs_del_item(trans, extent_root, path);
6579 btrfs_release_path(extent_root, path);
6583 static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
6584 struct btrfs_ref_path *ref_path)
6586 struct btrfs_key root_key;
6588 root_key.objectid = ref_path->root_objectid;
6589 root_key.type = BTRFS_ROOT_ITEM_KEY;
6590 if (is_cowonly_root(ref_path->root_objectid))
6591 root_key.offset = 0;
6593 root_key.offset = (u64)-1;
6595 return btrfs_read_fs_root_no_name(fs_info, &root_key);
6598 static noinline int relocate_one_extent(struct btrfs_root *extent_root,
6599 struct btrfs_path *path,
6600 struct btrfs_key *extent_key,
6601 struct btrfs_block_group_cache *group,
6602 struct inode *reloc_inode, int pass)
6604 struct btrfs_trans_handle *trans;
6605 struct btrfs_root *found_root;
6606 struct btrfs_ref_path *ref_path = NULL;
6607 struct disk_extent *new_extents = NULL;
6612 struct btrfs_key first_key;
6616 trans = btrfs_start_transaction(extent_root, 1);
6619 if (extent_key->objectid == 0) {
6620 ret = del_extent_zero(trans, extent_root, path, extent_key);
6624 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
6630 for (loops = 0; ; loops++) {
6632 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
6633 extent_key->objectid);
6635 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
6642 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6643 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
6646 found_root = read_ref_root(extent_root->fs_info, ref_path);
6647 BUG_ON(!found_root);
6649 * for reference counted tree, only process reference paths
6650 * rooted at the latest committed root.
6652 if (found_root->ref_cows &&
6653 ref_path->root_generation != found_root->root_key.offset)
6656 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6659 * copy data extents to new locations
6661 u64 group_start = group->key.objectid;
6662 ret = relocate_data_extent(reloc_inode,
6671 level = ref_path->owner_objectid;
6674 if (prev_block != ref_path->nodes[level]) {
6675 struct extent_buffer *eb;
6676 u64 block_start = ref_path->nodes[level];
6677 u64 block_size = btrfs_level_size(found_root, level);
6679 eb = read_tree_block(found_root, block_start,
6681 btrfs_tree_lock(eb);
6682 BUG_ON(level != btrfs_header_level(eb));
6685 btrfs_item_key_to_cpu(eb, &first_key, 0);
6687 btrfs_node_key_to_cpu(eb, &first_key, 0);
6689 btrfs_tree_unlock(eb);
6690 free_extent_buffer(eb);
6691 prev_block = block_start;
6694 mutex_lock(&extent_root->fs_info->trans_mutex);
6695 btrfs_record_root_in_trans(found_root);
6696 mutex_unlock(&extent_root->fs_info->trans_mutex);
6697 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6699 * try to update data extent references while
6700 * keeping metadata shared between snapshots.
6703 ret = relocate_one_path(trans, found_root,
6704 path, &first_key, ref_path,
6705 group, reloc_inode);
6711 * use fallback method to process the remaining
6715 u64 group_start = group->key.objectid;
6716 new_extents = kmalloc(sizeof(*new_extents),
6719 ret = get_new_locations(reloc_inode,
6727 ret = replace_one_extent(trans, found_root,
6729 &first_key, ref_path,
6730 new_extents, nr_extents);
6732 ret = relocate_tree_block(trans, found_root, path,
6733 &first_key, ref_path);
6740 btrfs_end_transaction(trans, extent_root);
6747 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
6750 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
6751 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
6753 num_devices = root->fs_info->fs_devices->rw_devices;
6754 if (num_devices == 1) {
6755 stripped |= BTRFS_BLOCK_GROUP_DUP;
6756 stripped = flags & ~stripped;
6758 /* turn raid0 into single device chunks */
6759 if (flags & BTRFS_BLOCK_GROUP_RAID0)
6762 /* turn mirroring into duplication */
6763 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
6764 BTRFS_BLOCK_GROUP_RAID10))
6765 return stripped | BTRFS_BLOCK_GROUP_DUP;
6768 /* they already had raid on here, just return */
6769 if (flags & stripped)
6772 stripped |= BTRFS_BLOCK_GROUP_DUP;
6773 stripped = flags & ~stripped;
6775 /* switch duplicated blocks with raid1 */
6776 if (flags & BTRFS_BLOCK_GROUP_DUP)
6777 return stripped | BTRFS_BLOCK_GROUP_RAID1;
6779 /* turn single device chunks into raid0 */
6780 return stripped | BTRFS_BLOCK_GROUP_RAID0;
6785 static int __alloc_chunk_for_shrink(struct btrfs_root *root,
6786 struct btrfs_block_group_cache *shrink_block_group,
6789 struct btrfs_trans_handle *trans;
6790 u64 new_alloc_flags;
6793 spin_lock(&shrink_block_group->lock);
6794 if (btrfs_block_group_used(&shrink_block_group->item) +
6795 shrink_block_group->reserved > 0) {
6796 spin_unlock(&shrink_block_group->lock);
6798 trans = btrfs_start_transaction(root, 1);
6799 spin_lock(&shrink_block_group->lock);
6801 new_alloc_flags = update_block_group_flags(root,
6802 shrink_block_group->flags);
6803 if (new_alloc_flags != shrink_block_group->flags) {
6805 btrfs_block_group_used(&shrink_block_group->item);
6807 calc = shrink_block_group->key.offset;
6809 spin_unlock(&shrink_block_group->lock);
6811 do_chunk_alloc(trans, root->fs_info->extent_root,
6812 calc + 2 * 1024 * 1024, new_alloc_flags, force);
6814 btrfs_end_transaction(trans, root);
6816 spin_unlock(&shrink_block_group->lock);
6821 int btrfs_prepare_block_group_relocation(struct btrfs_root *root,
6822 struct btrfs_block_group_cache *group)
6825 __alloc_chunk_for_shrink(root, group, 1);
6826 set_block_group_readonly(group);
6831 static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
6832 struct btrfs_root *root,
6833 u64 objectid, u64 size)
6835 struct btrfs_path *path;
6836 struct btrfs_inode_item *item;
6837 struct extent_buffer *leaf;
6840 path = btrfs_alloc_path();
6844 path->leave_spinning = 1;
6845 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
6849 leaf = path->nodes[0];
6850 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
6851 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
6852 btrfs_set_inode_generation(leaf, item, 1);
6853 btrfs_set_inode_size(leaf, item, size);
6854 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
6855 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
6856 btrfs_mark_buffer_dirty(leaf);
6857 btrfs_release_path(root, path);
6859 btrfs_free_path(path);
6863 static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
6864 struct btrfs_block_group_cache *group)
6866 struct inode *inode = NULL;
6867 struct btrfs_trans_handle *trans;
6868 struct btrfs_root *root;
6869 struct btrfs_key root_key;
6870 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
6873 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6874 root_key.type = BTRFS_ROOT_ITEM_KEY;
6875 root_key.offset = (u64)-1;
6876 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
6878 return ERR_CAST(root);
6880 trans = btrfs_start_transaction(root, 1);
6883 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
6887 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
6890 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
6891 group->key.offset, 0, group->key.offset,
6895 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
6896 if (inode->i_state & I_NEW) {
6897 BTRFS_I(inode)->root = root;
6898 BTRFS_I(inode)->location.objectid = objectid;
6899 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
6900 BTRFS_I(inode)->location.offset = 0;
6901 btrfs_read_locked_inode(inode);
6902 unlock_new_inode(inode);
6903 BUG_ON(is_bad_inode(inode));
6907 BTRFS_I(inode)->index_cnt = group->key.objectid;
6909 err = btrfs_orphan_add(trans, inode);
6911 btrfs_end_transaction(trans, root);
6915 inode = ERR_PTR(err);
6920 int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
6923 struct btrfs_ordered_sum *sums;
6924 struct btrfs_sector_sum *sector_sum;
6925 struct btrfs_ordered_extent *ordered;
6926 struct btrfs_root *root = BTRFS_I(inode)->root;
6927 struct list_head list;
6932 INIT_LIST_HEAD(&list);
6934 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
6935 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
6937 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
6938 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
6939 disk_bytenr + len - 1, &list);
6941 while (!list_empty(&list)) {
6942 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
6943 list_del_init(&sums->list);
6945 sector_sum = sums->sums;
6946 sums->bytenr = ordered->start;
6949 while (offset < sums->len) {
6950 sector_sum->bytenr += ordered->start - disk_bytenr;
6952 offset += root->sectorsize;
6955 btrfs_add_ordered_sum(inode, ordered, sums);
6957 btrfs_put_ordered_extent(ordered);
6961 int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
6963 struct btrfs_trans_handle *trans;
6964 struct btrfs_path *path;
6965 struct btrfs_fs_info *info = root->fs_info;
6966 struct extent_buffer *leaf;
6967 struct inode *reloc_inode;
6968 struct btrfs_block_group_cache *block_group;
6969 struct btrfs_key key;
6978 root = root->fs_info->extent_root;
6980 block_group = btrfs_lookup_block_group(info, group_start);
6981 BUG_ON(!block_group);
6983 printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n",
6984 (unsigned long long)block_group->key.objectid,
6985 (unsigned long long)block_group->flags);
6987 path = btrfs_alloc_path();
6990 reloc_inode = create_reloc_inode(info, block_group);
6991 BUG_ON(IS_ERR(reloc_inode));
6993 __alloc_chunk_for_shrink(root, block_group, 1);
6994 set_block_group_readonly(block_group);
6996 btrfs_start_delalloc_inodes(info->tree_root);
6997 btrfs_wait_ordered_extents(info->tree_root, 0);
7002 key.objectid = block_group->key.objectid;
7005 cur_byte = key.objectid;
7007 trans = btrfs_start_transaction(info->tree_root, 1);
7008 btrfs_commit_transaction(trans, info->tree_root);
7010 mutex_lock(&root->fs_info->cleaner_mutex);
7011 btrfs_clean_old_snapshots(info->tree_root);
7012 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
7013 mutex_unlock(&root->fs_info->cleaner_mutex);
7015 trans = btrfs_start_transaction(info->tree_root, 1);
7016 btrfs_commit_transaction(trans, info->tree_root);
7019 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7023 leaf = path->nodes[0];
7024 nritems = btrfs_header_nritems(leaf);
7025 if (path->slots[0] >= nritems) {
7026 ret = btrfs_next_leaf(root, path);
7033 leaf = path->nodes[0];
7034 nritems = btrfs_header_nritems(leaf);
7037 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
7039 if (key.objectid >= block_group->key.objectid +
7040 block_group->key.offset)
7043 if (progress && need_resched()) {
7044 btrfs_release_path(root, path);
7051 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
7052 key.objectid + key.offset <= cur_byte) {
7058 cur_byte = key.objectid + key.offset;
7059 btrfs_release_path(root, path);
7061 __alloc_chunk_for_shrink(root, block_group, 0);
7062 ret = relocate_one_extent(root, path, &key, block_group,
7068 key.objectid = cur_byte;
7073 btrfs_release_path(root, path);
7076 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
7077 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
7080 if (total_found > 0) {
7081 printk(KERN_INFO "btrfs found %llu extents in pass %d\n",
7082 (unsigned long long)total_found, pass);
7084 if (total_found == skipped && pass > 2) {
7086 reloc_inode = create_reloc_inode(info, block_group);
7092 /* delete reloc_inode */
7095 /* unpin extents in this range */
7096 trans = btrfs_start_transaction(info->tree_root, 1);
7097 btrfs_commit_transaction(trans, info->tree_root);
7099 spin_lock(&block_group->lock);
7100 WARN_ON(block_group->pinned > 0);
7101 WARN_ON(block_group->reserved > 0);
7102 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
7103 spin_unlock(&block_group->lock);
7104 btrfs_put_block_group(block_group);
7107 btrfs_free_path(path);
7112 static int find_first_block_group(struct btrfs_root *root,
7113 struct btrfs_path *path, struct btrfs_key *key)
7116 struct btrfs_key found_key;
7117 struct extent_buffer *leaf;
7120 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7125 slot = path->slots[0];
7126 leaf = path->nodes[0];
7127 if (slot >= btrfs_header_nritems(leaf)) {
7128 ret = btrfs_next_leaf(root, path);
7135 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7137 if (found_key.objectid >= key->objectid &&
7138 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7149 int btrfs_free_block_groups(struct btrfs_fs_info *info)
7151 struct btrfs_block_group_cache *block_group;
7152 struct btrfs_space_info *space_info;
7155 spin_lock(&info->block_group_cache_lock);
7156 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7157 block_group = rb_entry(n, struct btrfs_block_group_cache,
7159 rb_erase(&block_group->cache_node,
7160 &info->block_group_cache_tree);
7161 spin_unlock(&info->block_group_cache_lock);
7163 down_write(&block_group->space_info->groups_sem);
7164 list_del(&block_group->list);
7165 up_write(&block_group->space_info->groups_sem);
7167 if (block_group->cached == BTRFS_CACHE_STARTED)
7168 wait_event(block_group->caching_q,
7169 block_group_cache_done(block_group));
7171 btrfs_remove_free_space_cache(block_group);
7173 WARN_ON(atomic_read(&block_group->count) != 1);
7176 spin_lock(&info->block_group_cache_lock);
7178 spin_unlock(&info->block_group_cache_lock);
7180 /* now that all the block groups are freed, go through and
7181 * free all the space_info structs. This is only called during
7182 * the final stages of unmount, and so we know nobody is
7183 * using them. We call synchronize_rcu() once before we start,
7184 * just to be on the safe side.
7188 while(!list_empty(&info->space_info)) {
7189 space_info = list_entry(info->space_info.next,
7190 struct btrfs_space_info,
7193 list_del(&space_info->list);
7199 int btrfs_read_block_groups(struct btrfs_root *root)
7201 struct btrfs_path *path;
7203 struct btrfs_block_group_cache *cache;
7204 struct btrfs_fs_info *info = root->fs_info;
7205 struct btrfs_space_info *space_info;
7206 struct btrfs_key key;
7207 struct btrfs_key found_key;
7208 struct extent_buffer *leaf;
7210 root = info->extent_root;
7213 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
7214 path = btrfs_alloc_path();
7219 ret = find_first_block_group(root, path, &key);
7227 leaf = path->nodes[0];
7228 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7229 cache = kzalloc(sizeof(*cache), GFP_NOFS);
7235 atomic_set(&cache->count, 1);
7236 spin_lock_init(&cache->lock);
7237 spin_lock_init(&cache->tree_lock);
7238 cache->fs_info = info;
7239 init_waitqueue_head(&cache->caching_q);
7240 INIT_LIST_HEAD(&cache->list);
7241 INIT_LIST_HEAD(&cache->cluster_list);
7244 * we only want to have 32k of ram per block group for keeping
7245 * track of free space, and if we pass 1/2 of that we want to
7246 * start converting things over to using bitmaps
7248 cache->extents_thresh = ((1024 * 32) / 2) /
7249 sizeof(struct btrfs_free_space);
7251 read_extent_buffer(leaf, &cache->item,
7252 btrfs_item_ptr_offset(leaf, path->slots[0]),
7253 sizeof(cache->item));
7254 memcpy(&cache->key, &found_key, sizeof(found_key));
7256 key.objectid = found_key.objectid + found_key.offset;
7257 btrfs_release_path(root, path);
7258 cache->flags = btrfs_block_group_flags(&cache->item);
7259 cache->sectorsize = root->sectorsize;
7261 remove_sb_from_cache(root, cache);
7264 * check for two cases, either we are full, and therefore
7265 * don't need to bother with the caching work since we won't
7266 * find any space, or we are empty, and we can just add all
7267 * the space in and be done with it. This saves us _alot_ of
7268 * time, particularly in the full case.
7270 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
7271 cache->cached = BTRFS_CACHE_FINISHED;
7272 } else if (btrfs_block_group_used(&cache->item) == 0) {
7273 cache->cached = BTRFS_CACHE_FINISHED;
7274 add_new_free_space(cache, root->fs_info,
7276 found_key.objectid +
7280 ret = update_space_info(info, cache->flags, found_key.offset,
7281 btrfs_block_group_used(&cache->item),
7284 cache->space_info = space_info;
7285 down_write(&space_info->groups_sem);
7286 list_add_tail(&cache->list, &space_info->block_groups);
7287 up_write(&space_info->groups_sem);
7289 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7292 set_avail_alloc_bits(root->fs_info, cache->flags);
7293 if (btrfs_chunk_readonly(root, cache->key.objectid))
7294 set_block_group_readonly(cache);
7298 btrfs_free_path(path);
7302 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7303 struct btrfs_root *root, u64 bytes_used,
7304 u64 type, u64 chunk_objectid, u64 chunk_offset,
7308 struct btrfs_root *extent_root;
7309 struct btrfs_block_group_cache *cache;
7311 extent_root = root->fs_info->extent_root;
7313 root->fs_info->last_trans_log_full_commit = trans->transid;
7315 cache = kzalloc(sizeof(*cache), GFP_NOFS);
7319 cache->key.objectid = chunk_offset;
7320 cache->key.offset = size;
7321 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
7322 cache->sectorsize = root->sectorsize;
7325 * we only want to have 32k of ram per block group for keeping track
7326 * of free space, and if we pass 1/2 of that we want to start
7327 * converting things over to using bitmaps
7329 cache->extents_thresh = ((1024 * 32) / 2) /
7330 sizeof(struct btrfs_free_space);
7331 atomic_set(&cache->count, 1);
7332 spin_lock_init(&cache->lock);
7333 spin_lock_init(&cache->tree_lock);
7334 init_waitqueue_head(&cache->caching_q);
7335 INIT_LIST_HEAD(&cache->list);
7336 INIT_LIST_HEAD(&cache->cluster_list);
7338 btrfs_set_block_group_used(&cache->item, bytes_used);
7339 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7340 cache->flags = type;
7341 btrfs_set_block_group_flags(&cache->item, type);
7343 cache->cached = BTRFS_CACHE_FINISHED;
7344 remove_sb_from_cache(root, cache);
7346 add_new_free_space(cache, root->fs_info, chunk_offset,
7347 chunk_offset + size);
7349 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7350 &cache->space_info);
7352 down_write(&cache->space_info->groups_sem);
7353 list_add_tail(&cache->list, &cache->space_info->block_groups);
7354 up_write(&cache->space_info->groups_sem);
7356 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7359 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7360 sizeof(cache->item));
7363 set_avail_alloc_bits(extent_root->fs_info, type);
7368 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7369 struct btrfs_root *root, u64 group_start)
7371 struct btrfs_path *path;
7372 struct btrfs_block_group_cache *block_group;
7373 struct btrfs_free_cluster *cluster;
7374 struct btrfs_key key;
7377 root = root->fs_info->extent_root;
7379 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7380 BUG_ON(!block_group);
7381 BUG_ON(!block_group->ro);
7383 memcpy(&key, &block_group->key, sizeof(key));
7385 /* make sure this block group isn't part of an allocation cluster */
7386 cluster = &root->fs_info->data_alloc_cluster;
7387 spin_lock(&cluster->refill_lock);
7388 btrfs_return_cluster_to_free_space(block_group, cluster);
7389 spin_unlock(&cluster->refill_lock);
7392 * make sure this block group isn't part of a metadata
7393 * allocation cluster
7395 cluster = &root->fs_info->meta_alloc_cluster;
7396 spin_lock(&cluster->refill_lock);
7397 btrfs_return_cluster_to_free_space(block_group, cluster);
7398 spin_unlock(&cluster->refill_lock);
7400 path = btrfs_alloc_path();
7403 spin_lock(&root->fs_info->block_group_cache_lock);
7404 rb_erase(&block_group->cache_node,
7405 &root->fs_info->block_group_cache_tree);
7406 spin_unlock(&root->fs_info->block_group_cache_lock);
7408 down_write(&block_group->space_info->groups_sem);
7410 * we must use list_del_init so people can check to see if they
7411 * are still on the list after taking the semaphore
7413 list_del_init(&block_group->list);
7414 up_write(&block_group->space_info->groups_sem);
7416 if (block_group->cached == BTRFS_CACHE_STARTED)
7417 wait_event(block_group->caching_q,
7418 block_group_cache_done(block_group));
7420 btrfs_remove_free_space_cache(block_group);
7422 spin_lock(&block_group->space_info->lock);
7423 block_group->space_info->total_bytes -= block_group->key.offset;
7424 block_group->space_info->bytes_readonly -= block_group->key.offset;
7425 spin_unlock(&block_group->space_info->lock);
7427 btrfs_clear_space_info_full(root->fs_info);
7429 btrfs_put_block_group(block_group);
7430 btrfs_put_block_group(block_group);
7432 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
7438 ret = btrfs_del_item(trans, root, path);
7440 btrfs_free_path(path);