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/sched/signal.h>
20 #include <linux/pagemap.h>
21 #include <linux/writeback.h>
22 #include <linux/blkdev.h>
23 #include <linux/sort.h>
24 #include <linux/rcupdate.h>
25 #include <linux/kthread.h>
26 #include <linux/slab.h>
27 #include <linux/ratelimit.h>
28 #include <linux/percpu_counter.h>
29 #include <linux/lockdep.h>
33 #include "print-tree.h"
37 #include "free-space-cache.h"
38 #include "free-space-tree.h"
42 #include "ref-verify.h"
44 #undef SCRAMBLE_DELAYED_REFS
47 * control flags for do_chunk_alloc's force field
48 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
49 * if we really need one.
51 * CHUNK_ALLOC_LIMITED means to only try and allocate one
52 * if we have very few chunks already allocated. This is
53 * used as part of the clustering code to help make sure
54 * we have a good pool of storage to cluster in, without
55 * filling the FS with empty chunks
57 * CHUNK_ALLOC_FORCE means it must try to allocate one
61 CHUNK_ALLOC_NO_FORCE = 0,
62 CHUNK_ALLOC_LIMITED = 1,
63 CHUNK_ALLOC_FORCE = 2,
66 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
67 struct btrfs_fs_info *fs_info,
68 struct btrfs_delayed_ref_node *node, u64 parent,
69 u64 root_objectid, u64 owner_objectid,
70 u64 owner_offset, int refs_to_drop,
71 struct btrfs_delayed_extent_op *extra_op);
72 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
73 struct extent_buffer *leaf,
74 struct btrfs_extent_item *ei);
75 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
76 struct btrfs_fs_info *fs_info,
77 u64 parent, u64 root_objectid,
78 u64 flags, u64 owner, u64 offset,
79 struct btrfs_key *ins, int ref_mod);
80 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
81 struct btrfs_fs_info *fs_info,
82 u64 parent, u64 root_objectid,
83 u64 flags, struct btrfs_disk_key *key,
84 int level, struct btrfs_key *ins);
85 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
86 struct btrfs_fs_info *fs_info, u64 flags,
88 static int find_next_key(struct btrfs_path *path, int level,
89 struct btrfs_key *key);
90 static void dump_space_info(struct btrfs_fs_info *fs_info,
91 struct btrfs_space_info *info, u64 bytes,
92 int dump_block_groups);
93 static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
95 static void space_info_add_new_bytes(struct btrfs_fs_info *fs_info,
96 struct btrfs_space_info *space_info,
98 static void space_info_add_old_bytes(struct btrfs_fs_info *fs_info,
99 struct btrfs_space_info *space_info,
103 block_group_cache_done(struct btrfs_block_group_cache *cache)
106 return cache->cached == BTRFS_CACHE_FINISHED ||
107 cache->cached == BTRFS_CACHE_ERROR;
110 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
112 return (cache->flags & bits) == bits;
115 void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
117 atomic_inc(&cache->count);
120 void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
122 if (atomic_dec_and_test(&cache->count)) {
123 WARN_ON(cache->pinned > 0);
124 WARN_ON(cache->reserved > 0);
127 * If not empty, someone is still holding mutex of
128 * full_stripe_lock, which can only be released by caller.
129 * And it will definitely cause use-after-free when caller
130 * tries to release full stripe lock.
132 * No better way to resolve, but only to warn.
134 WARN_ON(!RB_EMPTY_ROOT(&cache->full_stripe_locks_root.root));
135 kfree(cache->free_space_ctl);
141 * this adds the block group to the fs_info rb tree for the block group
144 static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
145 struct btrfs_block_group_cache *block_group)
148 struct rb_node *parent = NULL;
149 struct btrfs_block_group_cache *cache;
151 spin_lock(&info->block_group_cache_lock);
152 p = &info->block_group_cache_tree.rb_node;
156 cache = rb_entry(parent, struct btrfs_block_group_cache,
158 if (block_group->key.objectid < cache->key.objectid) {
160 } else if (block_group->key.objectid > cache->key.objectid) {
163 spin_unlock(&info->block_group_cache_lock);
168 rb_link_node(&block_group->cache_node, parent, p);
169 rb_insert_color(&block_group->cache_node,
170 &info->block_group_cache_tree);
172 if (info->first_logical_byte > block_group->key.objectid)
173 info->first_logical_byte = block_group->key.objectid;
175 spin_unlock(&info->block_group_cache_lock);
181 * This will return the block group at or after bytenr if contains is 0, else
182 * it will return the block group that contains the bytenr
184 static struct btrfs_block_group_cache *
185 block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
188 struct btrfs_block_group_cache *cache, *ret = NULL;
192 spin_lock(&info->block_group_cache_lock);
193 n = info->block_group_cache_tree.rb_node;
196 cache = rb_entry(n, struct btrfs_block_group_cache,
198 end = cache->key.objectid + cache->key.offset - 1;
199 start = cache->key.objectid;
201 if (bytenr < start) {
202 if (!contains && (!ret || start < ret->key.objectid))
205 } else if (bytenr > start) {
206 if (contains && bytenr <= end) {
217 btrfs_get_block_group(ret);
218 if (bytenr == 0 && info->first_logical_byte > ret->key.objectid)
219 info->first_logical_byte = ret->key.objectid;
221 spin_unlock(&info->block_group_cache_lock);
226 static int add_excluded_extent(struct btrfs_fs_info *fs_info,
227 u64 start, u64 num_bytes)
229 u64 end = start + num_bytes - 1;
230 set_extent_bits(&fs_info->freed_extents[0],
231 start, end, EXTENT_UPTODATE);
232 set_extent_bits(&fs_info->freed_extents[1],
233 start, end, EXTENT_UPTODATE);
237 static void free_excluded_extents(struct btrfs_fs_info *fs_info,
238 struct btrfs_block_group_cache *cache)
242 start = cache->key.objectid;
243 end = start + cache->key.offset - 1;
245 clear_extent_bits(&fs_info->freed_extents[0],
246 start, end, EXTENT_UPTODATE);
247 clear_extent_bits(&fs_info->freed_extents[1],
248 start, end, EXTENT_UPTODATE);
251 static int exclude_super_stripes(struct btrfs_fs_info *fs_info,
252 struct btrfs_block_group_cache *cache)
259 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
260 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
261 cache->bytes_super += stripe_len;
262 ret = add_excluded_extent(fs_info, cache->key.objectid,
268 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
269 bytenr = btrfs_sb_offset(i);
270 ret = btrfs_rmap_block(fs_info, cache->key.objectid,
271 bytenr, 0, &logical, &nr, &stripe_len);
278 if (logical[nr] > cache->key.objectid +
282 if (logical[nr] + stripe_len <= cache->key.objectid)
286 if (start < cache->key.objectid) {
287 start = cache->key.objectid;
288 len = (logical[nr] + stripe_len) - start;
290 len = min_t(u64, stripe_len,
291 cache->key.objectid +
292 cache->key.offset - start);
295 cache->bytes_super += len;
296 ret = add_excluded_extent(fs_info, start, len);
308 static struct btrfs_caching_control *
309 get_caching_control(struct btrfs_block_group_cache *cache)
311 struct btrfs_caching_control *ctl;
313 spin_lock(&cache->lock);
314 if (!cache->caching_ctl) {
315 spin_unlock(&cache->lock);
319 ctl = cache->caching_ctl;
320 refcount_inc(&ctl->count);
321 spin_unlock(&cache->lock);
325 static void put_caching_control(struct btrfs_caching_control *ctl)
327 if (refcount_dec_and_test(&ctl->count))
331 #ifdef CONFIG_BTRFS_DEBUG
332 static void fragment_free_space(struct btrfs_block_group_cache *block_group)
334 struct btrfs_fs_info *fs_info = block_group->fs_info;
335 u64 start = block_group->key.objectid;
336 u64 len = block_group->key.offset;
337 u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ?
338 fs_info->nodesize : fs_info->sectorsize;
339 u64 step = chunk << 1;
341 while (len > chunk) {
342 btrfs_remove_free_space(block_group, start, chunk);
353 * this is only called by cache_block_group, since we could have freed extents
354 * we need to check the pinned_extents for any extents that can't be used yet
355 * since their free space will be released as soon as the transaction commits.
357 u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
358 struct btrfs_fs_info *info, u64 start, u64 end)
360 u64 extent_start, extent_end, size, total_added = 0;
363 while (start < end) {
364 ret = find_first_extent_bit(info->pinned_extents, start,
365 &extent_start, &extent_end,
366 EXTENT_DIRTY | EXTENT_UPTODATE,
371 if (extent_start <= start) {
372 start = extent_end + 1;
373 } else if (extent_start > start && extent_start < end) {
374 size = extent_start - start;
376 ret = btrfs_add_free_space(block_group, start,
378 BUG_ON(ret); /* -ENOMEM or logic error */
379 start = extent_end + 1;
388 ret = btrfs_add_free_space(block_group, start, size);
389 BUG_ON(ret); /* -ENOMEM or logic error */
395 static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
397 struct btrfs_block_group_cache *block_group = caching_ctl->block_group;
398 struct btrfs_fs_info *fs_info = block_group->fs_info;
399 struct btrfs_root *extent_root = fs_info->extent_root;
400 struct btrfs_path *path;
401 struct extent_buffer *leaf;
402 struct btrfs_key key;
409 path = btrfs_alloc_path();
413 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
415 #ifdef CONFIG_BTRFS_DEBUG
417 * If we're fragmenting we don't want to make anybody think we can
418 * allocate from this block group until we've had a chance to fragment
421 if (btrfs_should_fragment_free_space(block_group))
425 * We don't want to deadlock with somebody trying to allocate a new
426 * extent for the extent root while also trying to search the extent
427 * root to add free space. So we skip locking and search the commit
428 * root, since its read-only
430 path->skip_locking = 1;
431 path->search_commit_root = 1;
432 path->reada = READA_FORWARD;
436 key.type = BTRFS_EXTENT_ITEM_KEY;
439 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
443 leaf = path->nodes[0];
444 nritems = btrfs_header_nritems(leaf);
447 if (btrfs_fs_closing(fs_info) > 1) {
452 if (path->slots[0] < nritems) {
453 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
455 ret = find_next_key(path, 0, &key);
459 if (need_resched() ||
460 rwsem_is_contended(&fs_info->commit_root_sem)) {
462 caching_ctl->progress = last;
463 btrfs_release_path(path);
464 up_read(&fs_info->commit_root_sem);
465 mutex_unlock(&caching_ctl->mutex);
467 mutex_lock(&caching_ctl->mutex);
468 down_read(&fs_info->commit_root_sem);
472 ret = btrfs_next_leaf(extent_root, path);
477 leaf = path->nodes[0];
478 nritems = btrfs_header_nritems(leaf);
482 if (key.objectid < last) {
485 key.type = BTRFS_EXTENT_ITEM_KEY;
488 caching_ctl->progress = last;
489 btrfs_release_path(path);
493 if (key.objectid < block_group->key.objectid) {
498 if (key.objectid >= block_group->key.objectid +
499 block_group->key.offset)
502 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
503 key.type == BTRFS_METADATA_ITEM_KEY) {
504 total_found += add_new_free_space(block_group,
507 if (key.type == BTRFS_METADATA_ITEM_KEY)
508 last = key.objectid +
511 last = key.objectid + key.offset;
513 if (total_found > CACHING_CTL_WAKE_UP) {
516 wake_up(&caching_ctl->wait);
523 total_found += add_new_free_space(block_group, fs_info, last,
524 block_group->key.objectid +
525 block_group->key.offset);
526 caching_ctl->progress = (u64)-1;
529 btrfs_free_path(path);
533 static noinline void caching_thread(struct btrfs_work *work)
535 struct btrfs_block_group_cache *block_group;
536 struct btrfs_fs_info *fs_info;
537 struct btrfs_caching_control *caching_ctl;
538 struct btrfs_root *extent_root;
541 caching_ctl = container_of(work, struct btrfs_caching_control, work);
542 block_group = caching_ctl->block_group;
543 fs_info = block_group->fs_info;
544 extent_root = fs_info->extent_root;
546 mutex_lock(&caching_ctl->mutex);
547 down_read(&fs_info->commit_root_sem);
549 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
550 ret = load_free_space_tree(caching_ctl);
552 ret = load_extent_tree_free(caching_ctl);
554 spin_lock(&block_group->lock);
555 block_group->caching_ctl = NULL;
556 block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED;
557 spin_unlock(&block_group->lock);
559 #ifdef CONFIG_BTRFS_DEBUG
560 if (btrfs_should_fragment_free_space(block_group)) {
563 spin_lock(&block_group->space_info->lock);
564 spin_lock(&block_group->lock);
565 bytes_used = block_group->key.offset -
566 btrfs_block_group_used(&block_group->item);
567 block_group->space_info->bytes_used += bytes_used >> 1;
568 spin_unlock(&block_group->lock);
569 spin_unlock(&block_group->space_info->lock);
570 fragment_free_space(block_group);
574 caching_ctl->progress = (u64)-1;
576 up_read(&fs_info->commit_root_sem);
577 free_excluded_extents(fs_info, block_group);
578 mutex_unlock(&caching_ctl->mutex);
580 wake_up(&caching_ctl->wait);
582 put_caching_control(caching_ctl);
583 btrfs_put_block_group(block_group);
586 static int cache_block_group(struct btrfs_block_group_cache *cache,
590 struct btrfs_fs_info *fs_info = cache->fs_info;
591 struct btrfs_caching_control *caching_ctl;
594 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
598 INIT_LIST_HEAD(&caching_ctl->list);
599 mutex_init(&caching_ctl->mutex);
600 init_waitqueue_head(&caching_ctl->wait);
601 caching_ctl->block_group = cache;
602 caching_ctl->progress = cache->key.objectid;
603 refcount_set(&caching_ctl->count, 1);
604 btrfs_init_work(&caching_ctl->work, btrfs_cache_helper,
605 caching_thread, NULL, NULL);
607 spin_lock(&cache->lock);
609 * This should be a rare occasion, but this could happen I think in the
610 * case where one thread starts to load the space cache info, and then
611 * some other thread starts a transaction commit which tries to do an
612 * allocation while the other thread is still loading the space cache
613 * info. The previous loop should have kept us from choosing this block
614 * group, but if we've moved to the state where we will wait on caching
615 * block groups we need to first check if we're doing a fast load here,
616 * so we can wait for it to finish, otherwise we could end up allocating
617 * from a block group who's cache gets evicted for one reason or
620 while (cache->cached == BTRFS_CACHE_FAST) {
621 struct btrfs_caching_control *ctl;
623 ctl = cache->caching_ctl;
624 refcount_inc(&ctl->count);
625 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
626 spin_unlock(&cache->lock);
630 finish_wait(&ctl->wait, &wait);
631 put_caching_control(ctl);
632 spin_lock(&cache->lock);
635 if (cache->cached != BTRFS_CACHE_NO) {
636 spin_unlock(&cache->lock);
640 WARN_ON(cache->caching_ctl);
641 cache->caching_ctl = caching_ctl;
642 cache->cached = BTRFS_CACHE_FAST;
643 spin_unlock(&cache->lock);
645 if (btrfs_test_opt(fs_info, SPACE_CACHE)) {
646 mutex_lock(&caching_ctl->mutex);
647 ret = load_free_space_cache(fs_info, cache);
649 spin_lock(&cache->lock);
651 cache->caching_ctl = NULL;
652 cache->cached = BTRFS_CACHE_FINISHED;
653 cache->last_byte_to_unpin = (u64)-1;
654 caching_ctl->progress = (u64)-1;
656 if (load_cache_only) {
657 cache->caching_ctl = NULL;
658 cache->cached = BTRFS_CACHE_NO;
660 cache->cached = BTRFS_CACHE_STARTED;
661 cache->has_caching_ctl = 1;
664 spin_unlock(&cache->lock);
665 #ifdef CONFIG_BTRFS_DEBUG
667 btrfs_should_fragment_free_space(cache)) {
670 spin_lock(&cache->space_info->lock);
671 spin_lock(&cache->lock);
672 bytes_used = cache->key.offset -
673 btrfs_block_group_used(&cache->item);
674 cache->space_info->bytes_used += bytes_used >> 1;
675 spin_unlock(&cache->lock);
676 spin_unlock(&cache->space_info->lock);
677 fragment_free_space(cache);
680 mutex_unlock(&caching_ctl->mutex);
682 wake_up(&caching_ctl->wait);
684 put_caching_control(caching_ctl);
685 free_excluded_extents(fs_info, cache);
690 * We're either using the free space tree or no caching at all.
691 * Set cached to the appropriate value and wakeup any waiters.
693 spin_lock(&cache->lock);
694 if (load_cache_only) {
695 cache->caching_ctl = NULL;
696 cache->cached = BTRFS_CACHE_NO;
698 cache->cached = BTRFS_CACHE_STARTED;
699 cache->has_caching_ctl = 1;
701 spin_unlock(&cache->lock);
702 wake_up(&caching_ctl->wait);
705 if (load_cache_only) {
706 put_caching_control(caching_ctl);
710 down_write(&fs_info->commit_root_sem);
711 refcount_inc(&caching_ctl->count);
712 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
713 up_write(&fs_info->commit_root_sem);
715 btrfs_get_block_group(cache);
717 btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work);
723 * return the block group that starts at or after bytenr
725 static struct btrfs_block_group_cache *
726 btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
728 return block_group_cache_tree_search(info, bytenr, 0);
732 * return the block group that contains the given bytenr
734 struct btrfs_block_group_cache *btrfs_lookup_block_group(
735 struct btrfs_fs_info *info,
738 return block_group_cache_tree_search(info, bytenr, 1);
741 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
744 struct list_head *head = &info->space_info;
745 struct btrfs_space_info *found;
747 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
750 list_for_each_entry_rcu(found, head, list) {
751 if (found->flags & flags) {
760 static void add_pinned_bytes(struct btrfs_fs_info *fs_info, s64 num_bytes,
761 u64 owner, u64 root_objectid)
763 struct btrfs_space_info *space_info;
766 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
767 if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
768 flags = BTRFS_BLOCK_GROUP_SYSTEM;
770 flags = BTRFS_BLOCK_GROUP_METADATA;
772 flags = BTRFS_BLOCK_GROUP_DATA;
775 space_info = __find_space_info(fs_info, flags);
777 percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
781 * after adding space to the filesystem, we need to clear the full flags
782 * on all the space infos.
784 void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
786 struct list_head *head = &info->space_info;
787 struct btrfs_space_info *found;
790 list_for_each_entry_rcu(found, head, list)
795 /* simple helper to search for an existing data extent at a given offset */
796 int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len)
799 struct btrfs_key key;
800 struct btrfs_path *path;
802 path = btrfs_alloc_path();
806 key.objectid = start;
808 key.type = BTRFS_EXTENT_ITEM_KEY;
809 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
810 btrfs_free_path(path);
815 * helper function to lookup reference count and flags of a tree block.
817 * the head node for delayed ref is used to store the sum of all the
818 * reference count modifications queued up in the rbtree. the head
819 * node may also store the extent flags to set. This way you can check
820 * to see what the reference count and extent flags would be if all of
821 * the delayed refs are not processed.
823 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
824 struct btrfs_fs_info *fs_info, u64 bytenr,
825 u64 offset, int metadata, u64 *refs, u64 *flags)
827 struct btrfs_delayed_ref_head *head;
828 struct btrfs_delayed_ref_root *delayed_refs;
829 struct btrfs_path *path;
830 struct btrfs_extent_item *ei;
831 struct extent_buffer *leaf;
832 struct btrfs_key key;
839 * If we don't have skinny metadata, don't bother doing anything
842 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) {
843 offset = fs_info->nodesize;
847 path = btrfs_alloc_path();
852 path->skip_locking = 1;
853 path->search_commit_root = 1;
857 key.objectid = bytenr;
860 key.type = BTRFS_METADATA_ITEM_KEY;
862 key.type = BTRFS_EXTENT_ITEM_KEY;
864 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
868 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
869 if (path->slots[0]) {
871 btrfs_item_key_to_cpu(path->nodes[0], &key,
873 if (key.objectid == bytenr &&
874 key.type == BTRFS_EXTENT_ITEM_KEY &&
875 key.offset == fs_info->nodesize)
881 leaf = path->nodes[0];
882 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
883 if (item_size >= sizeof(*ei)) {
884 ei = btrfs_item_ptr(leaf, path->slots[0],
885 struct btrfs_extent_item);
886 num_refs = btrfs_extent_refs(leaf, ei);
887 extent_flags = btrfs_extent_flags(leaf, ei);
889 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
890 struct btrfs_extent_item_v0 *ei0;
891 BUG_ON(item_size != sizeof(*ei0));
892 ei0 = btrfs_item_ptr(leaf, path->slots[0],
893 struct btrfs_extent_item_v0);
894 num_refs = btrfs_extent_refs_v0(leaf, ei0);
895 /* FIXME: this isn't correct for data */
896 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
901 BUG_ON(num_refs == 0);
911 delayed_refs = &trans->transaction->delayed_refs;
912 spin_lock(&delayed_refs->lock);
913 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
915 if (!mutex_trylock(&head->mutex)) {
916 refcount_inc(&head->refs);
917 spin_unlock(&delayed_refs->lock);
919 btrfs_release_path(path);
922 * Mutex was contended, block until it's released and try
925 mutex_lock(&head->mutex);
926 mutex_unlock(&head->mutex);
927 btrfs_put_delayed_ref_head(head);
930 spin_lock(&head->lock);
931 if (head->extent_op && head->extent_op->update_flags)
932 extent_flags |= head->extent_op->flags_to_set;
934 BUG_ON(num_refs == 0);
936 num_refs += head->ref_mod;
937 spin_unlock(&head->lock);
938 mutex_unlock(&head->mutex);
940 spin_unlock(&delayed_refs->lock);
942 WARN_ON(num_refs == 0);
946 *flags = extent_flags;
948 btrfs_free_path(path);
953 * Back reference rules. Back refs have three main goals:
955 * 1) differentiate between all holders of references to an extent so that
956 * when a reference is dropped we can make sure it was a valid reference
957 * before freeing the extent.
959 * 2) Provide enough information to quickly find the holders of an extent
960 * if we notice a given block is corrupted or bad.
962 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
963 * maintenance. This is actually the same as #2, but with a slightly
964 * different use case.
966 * There are two kinds of back refs. The implicit back refs is optimized
967 * for pointers in non-shared tree blocks. For a given pointer in a block,
968 * back refs of this kind provide information about the block's owner tree
969 * and the pointer's key. These information allow us to find the block by
970 * b-tree searching. The full back refs is for pointers in tree blocks not
971 * referenced by their owner trees. The location of tree block is recorded
972 * in the back refs. Actually the full back refs is generic, and can be
973 * used in all cases the implicit back refs is used. The major shortcoming
974 * of the full back refs is its overhead. Every time a tree block gets
975 * COWed, we have to update back refs entry for all pointers in it.
977 * For a newly allocated tree block, we use implicit back refs for
978 * pointers in it. This means most tree related operations only involve
979 * implicit back refs. For a tree block created in old transaction, the
980 * only way to drop a reference to it is COW it. So we can detect the
981 * event that tree block loses its owner tree's reference and do the
982 * back refs conversion.
984 * When a tree block is COWed through a tree, there are four cases:
986 * The reference count of the block is one and the tree is the block's
987 * owner tree. Nothing to do in this case.
989 * The reference count of the block is one and the tree is not the
990 * block's owner tree. In this case, full back refs is used for pointers
991 * in the block. Remove these full back refs, add implicit back refs for
992 * every pointers in the new block.
994 * The reference count of the block is greater than one and the tree is
995 * the block's owner tree. In this case, implicit back refs is used for
996 * pointers in the block. Add full back refs for every pointers in the
997 * block, increase lower level extents' reference counts. The original
998 * implicit back refs are entailed to the new block.
1000 * The reference count of the block is greater than one and the tree is
1001 * not the block's owner tree. Add implicit back refs for every pointer in
1002 * the new block, increase lower level extents' reference count.
1004 * Back Reference Key composing:
1006 * The key objectid corresponds to the first byte in the extent,
1007 * The key type is used to differentiate between types of back refs.
1008 * There are different meanings of the key offset for different types
1011 * File extents can be referenced by:
1013 * - multiple snapshots, subvolumes, or different generations in one subvol
1014 * - different files inside a single subvolume
1015 * - different offsets inside a file (bookend extents in file.c)
1017 * The extent ref structure for the implicit back refs has fields for:
1019 * - Objectid of the subvolume root
1020 * - objectid of the file holding the reference
1021 * - original offset in the file
1022 * - how many bookend extents
1024 * The key offset for the implicit back refs is hash of the first
1027 * The extent ref structure for the full back refs has field for:
1029 * - number of pointers in the tree leaf
1031 * The key offset for the implicit back refs is the first byte of
1034 * When a file extent is allocated, The implicit back refs is used.
1035 * the fields are filled in:
1037 * (root_key.objectid, inode objectid, offset in file, 1)
1039 * When a file extent is removed file truncation, we find the
1040 * corresponding implicit back refs and check the following fields:
1042 * (btrfs_header_owner(leaf), inode objectid, offset in file)
1044 * Btree extents can be referenced by:
1046 * - Different subvolumes
1048 * Both the implicit back refs and the full back refs for tree blocks
1049 * only consist of key. The key offset for the implicit back refs is
1050 * objectid of block's owner tree. The key offset for the full back refs
1051 * is the first byte of parent block.
1053 * When implicit back refs is used, information about the lowest key and
1054 * level of the tree block are required. These information are stored in
1055 * tree block info structure.
1058 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1059 static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
1060 struct btrfs_fs_info *fs_info,
1061 struct btrfs_path *path,
1062 u64 owner, u32 extra_size)
1064 struct btrfs_root *root = fs_info->extent_root;
1065 struct btrfs_extent_item *item;
1066 struct btrfs_extent_item_v0 *ei0;
1067 struct btrfs_extent_ref_v0 *ref0;
1068 struct btrfs_tree_block_info *bi;
1069 struct extent_buffer *leaf;
1070 struct btrfs_key key;
1071 struct btrfs_key found_key;
1072 u32 new_size = sizeof(*item);
1076 leaf = path->nodes[0];
1077 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
1079 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1080 ei0 = btrfs_item_ptr(leaf, path->slots[0],
1081 struct btrfs_extent_item_v0);
1082 refs = btrfs_extent_refs_v0(leaf, ei0);
1084 if (owner == (u64)-1) {
1086 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1087 ret = btrfs_next_leaf(root, path);
1090 BUG_ON(ret > 0); /* Corruption */
1091 leaf = path->nodes[0];
1093 btrfs_item_key_to_cpu(leaf, &found_key,
1095 BUG_ON(key.objectid != found_key.objectid);
1096 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
1100 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1101 struct btrfs_extent_ref_v0);
1102 owner = btrfs_ref_objectid_v0(leaf, ref0);
1106 btrfs_release_path(path);
1108 if (owner < BTRFS_FIRST_FREE_OBJECTID)
1109 new_size += sizeof(*bi);
1111 new_size -= sizeof(*ei0);
1112 ret = btrfs_search_slot(trans, root, &key, path,
1113 new_size + extra_size, 1);
1116 BUG_ON(ret); /* Corruption */
1118 btrfs_extend_item(fs_info, path, new_size);
1120 leaf = path->nodes[0];
1121 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1122 btrfs_set_extent_refs(leaf, item, refs);
1123 /* FIXME: get real generation */
1124 btrfs_set_extent_generation(leaf, item, 0);
1125 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1126 btrfs_set_extent_flags(leaf, item,
1127 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1128 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1129 bi = (struct btrfs_tree_block_info *)(item + 1);
1130 /* FIXME: get first key of the block */
1131 memzero_extent_buffer(leaf, (unsigned long)bi, sizeof(*bi));
1132 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1134 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1136 btrfs_mark_buffer_dirty(leaf);
1142 * is_data == BTRFS_REF_TYPE_BLOCK, tree block type is required,
1143 * is_data == BTRFS_REF_TYPE_DATA, data type is requried,
1144 * is_data == BTRFS_REF_TYPE_ANY, either type is OK.
1146 int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
1147 struct btrfs_extent_inline_ref *iref,
1148 enum btrfs_inline_ref_type is_data)
1150 int type = btrfs_extent_inline_ref_type(eb, iref);
1151 u64 offset = btrfs_extent_inline_ref_offset(eb, iref);
1153 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
1154 type == BTRFS_SHARED_BLOCK_REF_KEY ||
1155 type == BTRFS_SHARED_DATA_REF_KEY ||
1156 type == BTRFS_EXTENT_DATA_REF_KEY) {
1157 if (is_data == BTRFS_REF_TYPE_BLOCK) {
1158 if (type == BTRFS_TREE_BLOCK_REF_KEY)
1160 if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1161 ASSERT(eb->fs_info);
1163 * Every shared one has parent tree
1164 * block, which must be aligned to
1168 IS_ALIGNED(offset, eb->fs_info->nodesize))
1171 } else if (is_data == BTRFS_REF_TYPE_DATA) {
1172 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1174 if (type == BTRFS_SHARED_DATA_REF_KEY) {
1175 ASSERT(eb->fs_info);
1177 * Every shared one has parent tree
1178 * block, which must be aligned to
1182 IS_ALIGNED(offset, eb->fs_info->nodesize))
1186 ASSERT(is_data == BTRFS_REF_TYPE_ANY);
1191 btrfs_print_leaf((struct extent_buffer *)eb);
1192 btrfs_err(eb->fs_info, "eb %llu invalid extent inline ref type %d",
1196 return BTRFS_REF_TYPE_INVALID;
1199 static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1201 u32 high_crc = ~(u32)0;
1202 u32 low_crc = ~(u32)0;
1205 lenum = cpu_to_le64(root_objectid);
1206 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
1207 lenum = cpu_to_le64(owner);
1208 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
1209 lenum = cpu_to_le64(offset);
1210 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
1212 return ((u64)high_crc << 31) ^ (u64)low_crc;
1215 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1216 struct btrfs_extent_data_ref *ref)
1218 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1219 btrfs_extent_data_ref_objectid(leaf, ref),
1220 btrfs_extent_data_ref_offset(leaf, ref));
1223 static int match_extent_data_ref(struct extent_buffer *leaf,
1224 struct btrfs_extent_data_ref *ref,
1225 u64 root_objectid, u64 owner, u64 offset)
1227 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1228 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1229 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1234 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1235 struct btrfs_fs_info *fs_info,
1236 struct btrfs_path *path,
1237 u64 bytenr, u64 parent,
1239 u64 owner, u64 offset)
1241 struct btrfs_root *root = fs_info->extent_root;
1242 struct btrfs_key key;
1243 struct btrfs_extent_data_ref *ref;
1244 struct extent_buffer *leaf;
1250 key.objectid = bytenr;
1252 key.type = BTRFS_SHARED_DATA_REF_KEY;
1253 key.offset = parent;
1255 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1256 key.offset = hash_extent_data_ref(root_objectid,
1261 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1270 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1271 key.type = BTRFS_EXTENT_REF_V0_KEY;
1272 btrfs_release_path(path);
1273 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1284 leaf = path->nodes[0];
1285 nritems = btrfs_header_nritems(leaf);
1287 if (path->slots[0] >= nritems) {
1288 ret = btrfs_next_leaf(root, path);
1294 leaf = path->nodes[0];
1295 nritems = btrfs_header_nritems(leaf);
1299 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1300 if (key.objectid != bytenr ||
1301 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1304 ref = btrfs_item_ptr(leaf, path->slots[0],
1305 struct btrfs_extent_data_ref);
1307 if (match_extent_data_ref(leaf, ref, root_objectid,
1310 btrfs_release_path(path);
1322 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1323 struct btrfs_fs_info *fs_info,
1324 struct btrfs_path *path,
1325 u64 bytenr, u64 parent,
1326 u64 root_objectid, u64 owner,
1327 u64 offset, int refs_to_add)
1329 struct btrfs_root *root = fs_info->extent_root;
1330 struct btrfs_key key;
1331 struct extent_buffer *leaf;
1336 key.objectid = bytenr;
1338 key.type = BTRFS_SHARED_DATA_REF_KEY;
1339 key.offset = parent;
1340 size = sizeof(struct btrfs_shared_data_ref);
1342 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1343 key.offset = hash_extent_data_ref(root_objectid,
1345 size = sizeof(struct btrfs_extent_data_ref);
1348 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1349 if (ret && ret != -EEXIST)
1352 leaf = path->nodes[0];
1354 struct btrfs_shared_data_ref *ref;
1355 ref = btrfs_item_ptr(leaf, path->slots[0],
1356 struct btrfs_shared_data_ref);
1358 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1360 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1361 num_refs += refs_to_add;
1362 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1365 struct btrfs_extent_data_ref *ref;
1366 while (ret == -EEXIST) {
1367 ref = btrfs_item_ptr(leaf, path->slots[0],
1368 struct btrfs_extent_data_ref);
1369 if (match_extent_data_ref(leaf, ref, root_objectid,
1372 btrfs_release_path(path);
1374 ret = btrfs_insert_empty_item(trans, root, path, &key,
1376 if (ret && ret != -EEXIST)
1379 leaf = path->nodes[0];
1381 ref = btrfs_item_ptr(leaf, path->slots[0],
1382 struct btrfs_extent_data_ref);
1384 btrfs_set_extent_data_ref_root(leaf, ref,
1386 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1387 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1388 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1390 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1391 num_refs += refs_to_add;
1392 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1395 btrfs_mark_buffer_dirty(leaf);
1398 btrfs_release_path(path);
1402 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1403 struct btrfs_fs_info *fs_info,
1404 struct btrfs_path *path,
1405 int refs_to_drop, int *last_ref)
1407 struct btrfs_key key;
1408 struct btrfs_extent_data_ref *ref1 = NULL;
1409 struct btrfs_shared_data_ref *ref2 = NULL;
1410 struct extent_buffer *leaf;
1414 leaf = path->nodes[0];
1415 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1417 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1418 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1419 struct btrfs_extent_data_ref);
1420 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1421 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1422 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1423 struct btrfs_shared_data_ref);
1424 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1425 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1426 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1427 struct btrfs_extent_ref_v0 *ref0;
1428 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1429 struct btrfs_extent_ref_v0);
1430 num_refs = btrfs_ref_count_v0(leaf, ref0);
1436 BUG_ON(num_refs < refs_to_drop);
1437 num_refs -= refs_to_drop;
1439 if (num_refs == 0) {
1440 ret = btrfs_del_item(trans, fs_info->extent_root, path);
1443 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1444 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1445 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1446 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1447 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1449 struct btrfs_extent_ref_v0 *ref0;
1450 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1451 struct btrfs_extent_ref_v0);
1452 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1455 btrfs_mark_buffer_dirty(leaf);
1460 static noinline u32 extent_data_ref_count(struct btrfs_path *path,
1461 struct btrfs_extent_inline_ref *iref)
1463 struct btrfs_key key;
1464 struct extent_buffer *leaf;
1465 struct btrfs_extent_data_ref *ref1;
1466 struct btrfs_shared_data_ref *ref2;
1470 leaf = path->nodes[0];
1471 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1474 * If type is invalid, we should have bailed out earlier than
1477 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
1478 ASSERT(type != BTRFS_REF_TYPE_INVALID);
1479 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1480 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1481 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1483 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1484 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1486 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1487 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1488 struct btrfs_extent_data_ref);
1489 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1490 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1491 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1492 struct btrfs_shared_data_ref);
1493 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1494 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1495 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1496 struct btrfs_extent_ref_v0 *ref0;
1497 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1498 struct btrfs_extent_ref_v0);
1499 num_refs = btrfs_ref_count_v0(leaf, ref0);
1507 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1508 struct btrfs_fs_info *fs_info,
1509 struct btrfs_path *path,
1510 u64 bytenr, u64 parent,
1513 struct btrfs_root *root = fs_info->extent_root;
1514 struct btrfs_key key;
1517 key.objectid = bytenr;
1519 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1520 key.offset = parent;
1522 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1523 key.offset = root_objectid;
1526 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1529 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1530 if (ret == -ENOENT && parent) {
1531 btrfs_release_path(path);
1532 key.type = BTRFS_EXTENT_REF_V0_KEY;
1533 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1541 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1542 struct btrfs_fs_info *fs_info,
1543 struct btrfs_path *path,
1544 u64 bytenr, u64 parent,
1547 struct btrfs_key key;
1550 key.objectid = bytenr;
1552 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1553 key.offset = parent;
1555 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1556 key.offset = root_objectid;
1559 ret = btrfs_insert_empty_item(trans, fs_info->extent_root,
1561 btrfs_release_path(path);
1565 static inline int extent_ref_type(u64 parent, u64 owner)
1568 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1570 type = BTRFS_SHARED_BLOCK_REF_KEY;
1572 type = BTRFS_TREE_BLOCK_REF_KEY;
1575 type = BTRFS_SHARED_DATA_REF_KEY;
1577 type = BTRFS_EXTENT_DATA_REF_KEY;
1582 static int find_next_key(struct btrfs_path *path, int level,
1583 struct btrfs_key *key)
1586 for (; level < BTRFS_MAX_LEVEL; level++) {
1587 if (!path->nodes[level])
1589 if (path->slots[level] + 1 >=
1590 btrfs_header_nritems(path->nodes[level]))
1593 btrfs_item_key_to_cpu(path->nodes[level], key,
1594 path->slots[level] + 1);
1596 btrfs_node_key_to_cpu(path->nodes[level], key,
1597 path->slots[level] + 1);
1604 * look for inline back ref. if back ref is found, *ref_ret is set
1605 * to the address of inline back ref, and 0 is returned.
1607 * if back ref isn't found, *ref_ret is set to the address where it
1608 * should be inserted, and -ENOENT is returned.
1610 * if insert is true and there are too many inline back refs, the path
1611 * points to the extent item, and -EAGAIN is returned.
1613 * NOTE: inline back refs are ordered in the same way that back ref
1614 * items in the tree are ordered.
1616 static noinline_for_stack
1617 int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1618 struct btrfs_fs_info *fs_info,
1619 struct btrfs_path *path,
1620 struct btrfs_extent_inline_ref **ref_ret,
1621 u64 bytenr, u64 num_bytes,
1622 u64 parent, u64 root_objectid,
1623 u64 owner, u64 offset, int insert)
1625 struct btrfs_root *root = fs_info->extent_root;
1626 struct btrfs_key key;
1627 struct extent_buffer *leaf;
1628 struct btrfs_extent_item *ei;
1629 struct btrfs_extent_inline_ref *iref;
1639 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
1642 key.objectid = bytenr;
1643 key.type = BTRFS_EXTENT_ITEM_KEY;
1644 key.offset = num_bytes;
1646 want = extent_ref_type(parent, owner);
1648 extra_size = btrfs_extent_inline_ref_size(want);
1649 path->keep_locks = 1;
1654 * Owner is our parent level, so we can just add one to get the level
1655 * for the block we are interested in.
1657 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
1658 key.type = BTRFS_METADATA_ITEM_KEY;
1663 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1670 * We may be a newly converted file system which still has the old fat
1671 * extent entries for metadata, so try and see if we have one of those.
1673 if (ret > 0 && skinny_metadata) {
1674 skinny_metadata = false;
1675 if (path->slots[0]) {
1677 btrfs_item_key_to_cpu(path->nodes[0], &key,
1679 if (key.objectid == bytenr &&
1680 key.type == BTRFS_EXTENT_ITEM_KEY &&
1681 key.offset == num_bytes)
1685 key.objectid = bytenr;
1686 key.type = BTRFS_EXTENT_ITEM_KEY;
1687 key.offset = num_bytes;
1688 btrfs_release_path(path);
1693 if (ret && !insert) {
1696 } else if (WARN_ON(ret)) {
1701 leaf = path->nodes[0];
1702 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1703 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1704 if (item_size < sizeof(*ei)) {
1709 ret = convert_extent_item_v0(trans, fs_info, path, owner,
1715 leaf = path->nodes[0];
1716 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1719 BUG_ON(item_size < sizeof(*ei));
1721 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1722 flags = btrfs_extent_flags(leaf, ei);
1724 ptr = (unsigned long)(ei + 1);
1725 end = (unsigned long)ei + item_size;
1727 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
1728 ptr += sizeof(struct btrfs_tree_block_info);
1732 if (owner >= BTRFS_FIRST_FREE_OBJECTID)
1733 needed = BTRFS_REF_TYPE_DATA;
1735 needed = BTRFS_REF_TYPE_BLOCK;
1743 iref = (struct btrfs_extent_inline_ref *)ptr;
1744 type = btrfs_get_extent_inline_ref_type(leaf, iref, needed);
1745 if (type == BTRFS_REF_TYPE_INVALID) {
1753 ptr += btrfs_extent_inline_ref_size(type);
1757 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1758 struct btrfs_extent_data_ref *dref;
1759 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1760 if (match_extent_data_ref(leaf, dref, root_objectid,
1765 if (hash_extent_data_ref_item(leaf, dref) <
1766 hash_extent_data_ref(root_objectid, owner, offset))
1770 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1772 if (parent == ref_offset) {
1776 if (ref_offset < parent)
1779 if (root_objectid == ref_offset) {
1783 if (ref_offset < root_objectid)
1787 ptr += btrfs_extent_inline_ref_size(type);
1789 if (err == -ENOENT && insert) {
1790 if (item_size + extra_size >=
1791 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1796 * To add new inline back ref, we have to make sure
1797 * there is no corresponding back ref item.
1798 * For simplicity, we just do not add new inline back
1799 * ref if there is any kind of item for this block
1801 if (find_next_key(path, 0, &key) == 0 &&
1802 key.objectid == bytenr &&
1803 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
1808 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1811 path->keep_locks = 0;
1812 btrfs_unlock_up_safe(path, 1);
1818 * helper to add new inline back ref
1820 static noinline_for_stack
1821 void setup_inline_extent_backref(struct btrfs_fs_info *fs_info,
1822 struct btrfs_path *path,
1823 struct btrfs_extent_inline_ref *iref,
1824 u64 parent, u64 root_objectid,
1825 u64 owner, u64 offset, int refs_to_add,
1826 struct btrfs_delayed_extent_op *extent_op)
1828 struct extent_buffer *leaf;
1829 struct btrfs_extent_item *ei;
1832 unsigned long item_offset;
1837 leaf = path->nodes[0];
1838 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1839 item_offset = (unsigned long)iref - (unsigned long)ei;
1841 type = extent_ref_type(parent, owner);
1842 size = btrfs_extent_inline_ref_size(type);
1844 btrfs_extend_item(fs_info, path, size);
1846 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1847 refs = btrfs_extent_refs(leaf, ei);
1848 refs += refs_to_add;
1849 btrfs_set_extent_refs(leaf, ei, refs);
1851 __run_delayed_extent_op(extent_op, leaf, ei);
1853 ptr = (unsigned long)ei + item_offset;
1854 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1855 if (ptr < end - size)
1856 memmove_extent_buffer(leaf, ptr + size, ptr,
1859 iref = (struct btrfs_extent_inline_ref *)ptr;
1860 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1861 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1862 struct btrfs_extent_data_ref *dref;
1863 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1864 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1865 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1866 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1867 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1868 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1869 struct btrfs_shared_data_ref *sref;
1870 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1871 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1872 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1873 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1874 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1876 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1878 btrfs_mark_buffer_dirty(leaf);
1881 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1882 struct btrfs_fs_info *fs_info,
1883 struct btrfs_path *path,
1884 struct btrfs_extent_inline_ref **ref_ret,
1885 u64 bytenr, u64 num_bytes, u64 parent,
1886 u64 root_objectid, u64 owner, u64 offset)
1890 ret = lookup_inline_extent_backref(trans, fs_info, path, ref_ret,
1891 bytenr, num_bytes, parent,
1892 root_objectid, owner, offset, 0);
1896 btrfs_release_path(path);
1899 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1900 ret = lookup_tree_block_ref(trans, fs_info, path, bytenr,
1901 parent, root_objectid);
1903 ret = lookup_extent_data_ref(trans, fs_info, path, bytenr,
1904 parent, root_objectid, owner,
1911 * helper to update/remove inline back ref
1913 static noinline_for_stack
1914 void update_inline_extent_backref(struct btrfs_fs_info *fs_info,
1915 struct btrfs_path *path,
1916 struct btrfs_extent_inline_ref *iref,
1918 struct btrfs_delayed_extent_op *extent_op,
1921 struct extent_buffer *leaf;
1922 struct btrfs_extent_item *ei;
1923 struct btrfs_extent_data_ref *dref = NULL;
1924 struct btrfs_shared_data_ref *sref = NULL;
1932 leaf = path->nodes[0];
1933 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1934 refs = btrfs_extent_refs(leaf, ei);
1935 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1936 refs += refs_to_mod;
1937 btrfs_set_extent_refs(leaf, ei, refs);
1939 __run_delayed_extent_op(extent_op, leaf, ei);
1942 * If type is invalid, we should have bailed out after
1943 * lookup_inline_extent_backref().
1945 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_ANY);
1946 ASSERT(type != BTRFS_REF_TYPE_INVALID);
1948 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1949 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1950 refs = btrfs_extent_data_ref_count(leaf, dref);
1951 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1952 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1953 refs = btrfs_shared_data_ref_count(leaf, sref);
1956 BUG_ON(refs_to_mod != -1);
1959 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1960 refs += refs_to_mod;
1963 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1964 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1966 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1969 size = btrfs_extent_inline_ref_size(type);
1970 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1971 ptr = (unsigned long)iref;
1972 end = (unsigned long)ei + item_size;
1973 if (ptr + size < end)
1974 memmove_extent_buffer(leaf, ptr, ptr + size,
1977 btrfs_truncate_item(fs_info, path, item_size, 1);
1979 btrfs_mark_buffer_dirty(leaf);
1982 static noinline_for_stack
1983 int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1984 struct btrfs_fs_info *fs_info,
1985 struct btrfs_path *path,
1986 u64 bytenr, u64 num_bytes, u64 parent,
1987 u64 root_objectid, u64 owner,
1988 u64 offset, int refs_to_add,
1989 struct btrfs_delayed_extent_op *extent_op)
1991 struct btrfs_extent_inline_ref *iref;
1994 ret = lookup_inline_extent_backref(trans, fs_info, path, &iref,
1995 bytenr, num_bytes, parent,
1996 root_objectid, owner, offset, 1);
1998 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1999 update_inline_extent_backref(fs_info, path, iref,
2000 refs_to_add, extent_op, NULL);
2001 } else if (ret == -ENOENT) {
2002 setup_inline_extent_backref(fs_info, path, iref, parent,
2003 root_objectid, owner, offset,
2004 refs_to_add, extent_op);
2010 static int insert_extent_backref(struct btrfs_trans_handle *trans,
2011 struct btrfs_fs_info *fs_info,
2012 struct btrfs_path *path,
2013 u64 bytenr, u64 parent, u64 root_objectid,
2014 u64 owner, u64 offset, int refs_to_add)
2017 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
2018 BUG_ON(refs_to_add != 1);
2019 ret = insert_tree_block_ref(trans, fs_info, path, bytenr,
2020 parent, root_objectid);
2022 ret = insert_extent_data_ref(trans, fs_info, path, bytenr,
2023 parent, root_objectid,
2024 owner, offset, refs_to_add);
2029 static int remove_extent_backref(struct btrfs_trans_handle *trans,
2030 struct btrfs_fs_info *fs_info,
2031 struct btrfs_path *path,
2032 struct btrfs_extent_inline_ref *iref,
2033 int refs_to_drop, int is_data, int *last_ref)
2037 BUG_ON(!is_data && refs_to_drop != 1);
2039 update_inline_extent_backref(fs_info, path, iref,
2040 -refs_to_drop, NULL, last_ref);
2041 } else if (is_data) {
2042 ret = remove_extent_data_ref(trans, fs_info, path, refs_to_drop,
2046 ret = btrfs_del_item(trans, fs_info->extent_root, path);
2051 #define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
2052 static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
2053 u64 *discarded_bytes)
2056 u64 bytes_left, end;
2057 u64 aligned_start = ALIGN(start, 1 << 9);
2059 if (WARN_ON(start != aligned_start)) {
2060 len -= aligned_start - start;
2061 len = round_down(len, 1 << 9);
2062 start = aligned_start;
2065 *discarded_bytes = 0;
2073 /* Skip any superblocks on this device. */
2074 for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) {
2075 u64 sb_start = btrfs_sb_offset(j);
2076 u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE;
2077 u64 size = sb_start - start;
2079 if (!in_range(sb_start, start, bytes_left) &&
2080 !in_range(sb_end, start, bytes_left) &&
2081 !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE))
2085 * Superblock spans beginning of range. Adjust start and
2088 if (sb_start <= start) {
2089 start += sb_end - start;
2094 bytes_left = end - start;
2099 ret = blkdev_issue_discard(bdev, start >> 9, size >> 9,
2102 *discarded_bytes += size;
2103 else if (ret != -EOPNOTSUPP)
2112 bytes_left = end - start;
2116 ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9,
2119 *discarded_bytes += bytes_left;
2124 int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
2125 u64 num_bytes, u64 *actual_bytes)
2128 u64 discarded_bytes = 0;
2129 struct btrfs_bio *bbio = NULL;
2133 * Avoid races with device replace and make sure our bbio has devices
2134 * associated to its stripes that don't go away while we are discarding.
2136 btrfs_bio_counter_inc_blocked(fs_info);
2137 /* Tell the block device(s) that the sectors can be discarded */
2138 ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, bytenr, &num_bytes,
2140 /* Error condition is -ENOMEM */
2142 struct btrfs_bio_stripe *stripe = bbio->stripes;
2146 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
2148 if (!stripe->dev->can_discard)
2151 ret = btrfs_issue_discard(stripe->dev->bdev,
2156 discarded_bytes += bytes;
2157 else if (ret != -EOPNOTSUPP)
2158 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
2161 * Just in case we get back EOPNOTSUPP for some reason,
2162 * just ignore the return value so we don't screw up
2163 * people calling discard_extent.
2167 btrfs_put_bbio(bbio);
2169 btrfs_bio_counter_dec(fs_info);
2172 *actual_bytes = discarded_bytes;
2175 if (ret == -EOPNOTSUPP)
2180 /* Can return -ENOMEM */
2181 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
2182 struct btrfs_root *root,
2183 u64 bytenr, u64 num_bytes, u64 parent,
2184 u64 root_objectid, u64 owner, u64 offset)
2186 struct btrfs_fs_info *fs_info = root->fs_info;
2187 int old_ref_mod, new_ref_mod;
2190 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
2191 root_objectid == BTRFS_TREE_LOG_OBJECTID);
2193 btrfs_ref_tree_mod(root, bytenr, num_bytes, parent, root_objectid,
2194 owner, offset, BTRFS_ADD_DELAYED_REF);
2196 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
2197 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
2199 root_objectid, (int)owner,
2200 BTRFS_ADD_DELAYED_REF, NULL,
2201 &old_ref_mod, &new_ref_mod);
2203 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
2205 root_objectid, owner, offset,
2206 0, BTRFS_ADD_DELAYED_REF,
2207 &old_ref_mod, &new_ref_mod);
2210 if (ret == 0 && old_ref_mod < 0 && new_ref_mod >= 0)
2211 add_pinned_bytes(fs_info, -num_bytes, owner, root_objectid);
2216 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
2217 struct btrfs_fs_info *fs_info,
2218 struct btrfs_delayed_ref_node *node,
2219 u64 parent, u64 root_objectid,
2220 u64 owner, u64 offset, int refs_to_add,
2221 struct btrfs_delayed_extent_op *extent_op)
2223 struct btrfs_path *path;
2224 struct extent_buffer *leaf;
2225 struct btrfs_extent_item *item;
2226 struct btrfs_key key;
2227 u64 bytenr = node->bytenr;
2228 u64 num_bytes = node->num_bytes;
2232 path = btrfs_alloc_path();
2236 path->reada = READA_FORWARD;
2237 path->leave_spinning = 1;
2238 /* this will setup the path even if it fails to insert the back ref */
2239 ret = insert_inline_extent_backref(trans, fs_info, path, bytenr,
2240 num_bytes, parent, root_objectid,
2242 refs_to_add, extent_op);
2243 if ((ret < 0 && ret != -EAGAIN) || !ret)
2247 * Ok we had -EAGAIN which means we didn't have space to insert and
2248 * inline extent ref, so just update the reference count and add a
2251 leaf = path->nodes[0];
2252 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2253 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2254 refs = btrfs_extent_refs(leaf, item);
2255 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
2257 __run_delayed_extent_op(extent_op, leaf, item);
2259 btrfs_mark_buffer_dirty(leaf);
2260 btrfs_release_path(path);
2262 path->reada = READA_FORWARD;
2263 path->leave_spinning = 1;
2264 /* now insert the actual backref */
2265 ret = insert_extent_backref(trans, fs_info, path, bytenr, parent,
2266 root_objectid, owner, offset, refs_to_add);
2268 btrfs_abort_transaction(trans, ret);
2270 btrfs_free_path(path);
2274 static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
2275 struct btrfs_fs_info *fs_info,
2276 struct btrfs_delayed_ref_node *node,
2277 struct btrfs_delayed_extent_op *extent_op,
2278 int insert_reserved)
2281 struct btrfs_delayed_data_ref *ref;
2282 struct btrfs_key ins;
2287 ins.objectid = node->bytenr;
2288 ins.offset = node->num_bytes;
2289 ins.type = BTRFS_EXTENT_ITEM_KEY;
2291 ref = btrfs_delayed_node_to_data_ref(node);
2292 trace_run_delayed_data_ref(fs_info, node, ref, node->action);
2294 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2295 parent = ref->parent;
2296 ref_root = ref->root;
2298 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2300 flags |= extent_op->flags_to_set;
2301 ret = alloc_reserved_file_extent(trans, fs_info,
2302 parent, ref_root, flags,
2303 ref->objectid, ref->offset,
2304 &ins, node->ref_mod);
2305 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2306 ret = __btrfs_inc_extent_ref(trans, fs_info, node, parent,
2307 ref_root, ref->objectid,
2308 ref->offset, node->ref_mod,
2310 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2311 ret = __btrfs_free_extent(trans, fs_info, node, parent,
2312 ref_root, ref->objectid,
2313 ref->offset, node->ref_mod,
2321 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2322 struct extent_buffer *leaf,
2323 struct btrfs_extent_item *ei)
2325 u64 flags = btrfs_extent_flags(leaf, ei);
2326 if (extent_op->update_flags) {
2327 flags |= extent_op->flags_to_set;
2328 btrfs_set_extent_flags(leaf, ei, flags);
2331 if (extent_op->update_key) {
2332 struct btrfs_tree_block_info *bi;
2333 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2334 bi = (struct btrfs_tree_block_info *)(ei + 1);
2335 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2339 static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2340 struct btrfs_fs_info *fs_info,
2341 struct btrfs_delayed_ref_head *head,
2342 struct btrfs_delayed_extent_op *extent_op)
2344 struct btrfs_key key;
2345 struct btrfs_path *path;
2346 struct btrfs_extent_item *ei;
2347 struct extent_buffer *leaf;
2351 int metadata = !extent_op->is_data;
2356 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2359 path = btrfs_alloc_path();
2363 key.objectid = head->bytenr;
2366 key.type = BTRFS_METADATA_ITEM_KEY;
2367 key.offset = extent_op->level;
2369 key.type = BTRFS_EXTENT_ITEM_KEY;
2370 key.offset = head->num_bytes;
2374 path->reada = READA_FORWARD;
2375 path->leave_spinning = 1;
2376 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 1);
2383 if (path->slots[0] > 0) {
2385 btrfs_item_key_to_cpu(path->nodes[0], &key,
2387 if (key.objectid == head->bytenr &&
2388 key.type == BTRFS_EXTENT_ITEM_KEY &&
2389 key.offset == head->num_bytes)
2393 btrfs_release_path(path);
2396 key.objectid = head->bytenr;
2397 key.offset = head->num_bytes;
2398 key.type = BTRFS_EXTENT_ITEM_KEY;
2407 leaf = path->nodes[0];
2408 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2409 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2410 if (item_size < sizeof(*ei)) {
2411 ret = convert_extent_item_v0(trans, fs_info, path, (u64)-1, 0);
2416 leaf = path->nodes[0];
2417 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2420 BUG_ON(item_size < sizeof(*ei));
2421 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2422 __run_delayed_extent_op(extent_op, leaf, ei);
2424 btrfs_mark_buffer_dirty(leaf);
2426 btrfs_free_path(path);
2430 static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2431 struct btrfs_fs_info *fs_info,
2432 struct btrfs_delayed_ref_node *node,
2433 struct btrfs_delayed_extent_op *extent_op,
2434 int insert_reserved)
2437 struct btrfs_delayed_tree_ref *ref;
2438 struct btrfs_key ins;
2441 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
2443 ref = btrfs_delayed_node_to_tree_ref(node);
2444 trace_run_delayed_tree_ref(fs_info, node, ref, node->action);
2446 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2447 parent = ref->parent;
2448 ref_root = ref->root;
2450 ins.objectid = node->bytenr;
2451 if (skinny_metadata) {
2452 ins.offset = ref->level;
2453 ins.type = BTRFS_METADATA_ITEM_KEY;
2455 ins.offset = node->num_bytes;
2456 ins.type = BTRFS_EXTENT_ITEM_KEY;
2459 if (node->ref_mod != 1) {
2461 "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu",
2462 node->bytenr, node->ref_mod, node->action, ref_root,
2466 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2467 BUG_ON(!extent_op || !extent_op->update_flags);
2468 ret = alloc_reserved_tree_block(trans, fs_info,
2470 extent_op->flags_to_set,
2473 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2474 ret = __btrfs_inc_extent_ref(trans, fs_info, node,
2478 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2479 ret = __btrfs_free_extent(trans, fs_info, node,
2481 ref->level, 0, 1, extent_op);
2488 /* helper function to actually process a single delayed ref entry */
2489 static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2490 struct btrfs_fs_info *fs_info,
2491 struct btrfs_delayed_ref_node *node,
2492 struct btrfs_delayed_extent_op *extent_op,
2493 int insert_reserved)
2497 if (trans->aborted) {
2498 if (insert_reserved)
2499 btrfs_pin_extent(fs_info, node->bytenr,
2500 node->num_bytes, 1);
2504 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2505 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2506 ret = run_delayed_tree_ref(trans, fs_info, node, extent_op,
2508 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2509 node->type == BTRFS_SHARED_DATA_REF_KEY)
2510 ret = run_delayed_data_ref(trans, fs_info, node, extent_op,
2517 static inline struct btrfs_delayed_ref_node *
2518 select_delayed_ref(struct btrfs_delayed_ref_head *head)
2520 struct btrfs_delayed_ref_node *ref;
2522 if (RB_EMPTY_ROOT(&head->ref_tree))
2526 * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
2527 * This is to prevent a ref count from going down to zero, which deletes
2528 * the extent item from the extent tree, when there still are references
2529 * to add, which would fail because they would not find the extent item.
2531 if (!list_empty(&head->ref_add_list))
2532 return list_first_entry(&head->ref_add_list,
2533 struct btrfs_delayed_ref_node, add_list);
2535 ref = rb_entry(rb_first(&head->ref_tree),
2536 struct btrfs_delayed_ref_node, ref_node);
2537 ASSERT(list_empty(&ref->add_list));
2541 static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
2542 struct btrfs_delayed_ref_head *head)
2544 spin_lock(&delayed_refs->lock);
2545 head->processing = 0;
2546 delayed_refs->num_heads_ready++;
2547 spin_unlock(&delayed_refs->lock);
2548 btrfs_delayed_ref_unlock(head);
2551 static int cleanup_extent_op(struct btrfs_trans_handle *trans,
2552 struct btrfs_fs_info *fs_info,
2553 struct btrfs_delayed_ref_head *head)
2555 struct btrfs_delayed_extent_op *extent_op = head->extent_op;
2560 head->extent_op = NULL;
2561 if (head->must_insert_reserved) {
2562 btrfs_free_delayed_extent_op(extent_op);
2565 spin_unlock(&head->lock);
2566 ret = run_delayed_extent_op(trans, fs_info, head, extent_op);
2567 btrfs_free_delayed_extent_op(extent_op);
2568 return ret ? ret : 1;
2571 static int cleanup_ref_head(struct btrfs_trans_handle *trans,
2572 struct btrfs_fs_info *fs_info,
2573 struct btrfs_delayed_ref_head *head)
2575 struct btrfs_delayed_ref_root *delayed_refs;
2578 delayed_refs = &trans->transaction->delayed_refs;
2580 ret = cleanup_extent_op(trans, fs_info, head);
2582 unselect_delayed_ref_head(delayed_refs, head);
2583 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
2590 * Need to drop our head ref lock and re-acquire the delayed ref lock
2591 * and then re-check to make sure nobody got added.
2593 spin_unlock(&head->lock);
2594 spin_lock(&delayed_refs->lock);
2595 spin_lock(&head->lock);
2596 if (!RB_EMPTY_ROOT(&head->ref_tree) || head->extent_op) {
2597 spin_unlock(&head->lock);
2598 spin_unlock(&delayed_refs->lock);
2601 delayed_refs->num_heads--;
2602 rb_erase(&head->href_node, &delayed_refs->href_root);
2603 RB_CLEAR_NODE(&head->href_node);
2604 spin_unlock(&delayed_refs->lock);
2605 spin_unlock(&head->lock);
2606 atomic_dec(&delayed_refs->num_entries);
2608 trace_run_delayed_ref_head(fs_info, head, 0);
2610 if (head->total_ref_mod < 0) {
2611 struct btrfs_block_group_cache *cache;
2613 cache = btrfs_lookup_block_group(fs_info, head->bytenr);
2615 percpu_counter_add(&cache->space_info->total_bytes_pinned,
2617 btrfs_put_block_group(cache);
2619 if (head->is_data) {
2620 spin_lock(&delayed_refs->lock);
2621 delayed_refs->pending_csums -= head->num_bytes;
2622 spin_unlock(&delayed_refs->lock);
2626 if (head->must_insert_reserved) {
2627 btrfs_pin_extent(fs_info, head->bytenr,
2628 head->num_bytes, 1);
2629 if (head->is_data) {
2630 ret = btrfs_del_csums(trans, fs_info, head->bytenr,
2635 /* Also free its reserved qgroup space */
2636 btrfs_qgroup_free_delayed_ref(fs_info, head->qgroup_ref_root,
2637 head->qgroup_reserved);
2638 btrfs_delayed_ref_unlock(head);
2639 btrfs_put_delayed_ref_head(head);
2644 * Returns 0 on success or if called with an already aborted transaction.
2645 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2647 static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2648 struct btrfs_fs_info *fs_info,
2651 struct btrfs_delayed_ref_root *delayed_refs;
2652 struct btrfs_delayed_ref_node *ref;
2653 struct btrfs_delayed_ref_head *locked_ref = NULL;
2654 struct btrfs_delayed_extent_op *extent_op;
2655 ktime_t start = ktime_get();
2657 unsigned long count = 0;
2658 unsigned long actual_count = 0;
2659 int must_insert_reserved = 0;
2661 delayed_refs = &trans->transaction->delayed_refs;
2667 spin_lock(&delayed_refs->lock);
2668 locked_ref = btrfs_select_ref_head(trans);
2670 spin_unlock(&delayed_refs->lock);
2674 /* grab the lock that says we are going to process
2675 * all the refs for this head */
2676 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2677 spin_unlock(&delayed_refs->lock);
2679 * we may have dropped the spin lock to get the head
2680 * mutex lock, and that might have given someone else
2681 * time to free the head. If that's true, it has been
2682 * removed from our list and we can move on.
2684 if (ret == -EAGAIN) {
2692 * We need to try and merge add/drops of the same ref since we
2693 * can run into issues with relocate dropping the implicit ref
2694 * and then it being added back again before the drop can
2695 * finish. If we merged anything we need to re-loop so we can
2697 * Or we can get node references of the same type that weren't
2698 * merged when created due to bumps in the tree mod seq, and
2699 * we need to merge them to prevent adding an inline extent
2700 * backref before dropping it (triggering a BUG_ON at
2701 * insert_inline_extent_backref()).
2703 spin_lock(&locked_ref->lock);
2704 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2708 * locked_ref is the head node, so we have to go one
2709 * node back for any delayed ref updates
2711 ref = select_delayed_ref(locked_ref);
2713 if (ref && ref->seq &&
2714 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
2715 spin_unlock(&locked_ref->lock);
2716 unselect_delayed_ref_head(delayed_refs, locked_ref);
2724 * We're done processing refs in this ref_head, clean everything
2725 * up and move on to the next ref_head.
2728 ret = cleanup_ref_head(trans, fs_info, locked_ref);
2730 /* We dropped our lock, we need to loop. */
2743 rb_erase(&ref->ref_node, &locked_ref->ref_tree);
2744 RB_CLEAR_NODE(&ref->ref_node);
2745 if (!list_empty(&ref->add_list))
2746 list_del(&ref->add_list);
2748 * When we play the delayed ref, also correct the ref_mod on
2751 switch (ref->action) {
2752 case BTRFS_ADD_DELAYED_REF:
2753 case BTRFS_ADD_DELAYED_EXTENT:
2754 locked_ref->ref_mod -= ref->ref_mod;
2756 case BTRFS_DROP_DELAYED_REF:
2757 locked_ref->ref_mod += ref->ref_mod;
2762 atomic_dec(&delayed_refs->num_entries);
2765 * Record the must-insert_reserved flag before we drop the spin
2768 must_insert_reserved = locked_ref->must_insert_reserved;
2769 locked_ref->must_insert_reserved = 0;
2771 extent_op = locked_ref->extent_op;
2772 locked_ref->extent_op = NULL;
2773 spin_unlock(&locked_ref->lock);
2775 ret = run_one_delayed_ref(trans, fs_info, ref, extent_op,
2776 must_insert_reserved);
2778 btrfs_free_delayed_extent_op(extent_op);
2780 unselect_delayed_ref_head(delayed_refs, locked_ref);
2781 btrfs_put_delayed_ref(ref);
2782 btrfs_debug(fs_info, "run_one_delayed_ref returned %d",
2787 btrfs_put_delayed_ref(ref);
2793 * We don't want to include ref heads since we can have empty ref heads
2794 * and those will drastically skew our runtime down since we just do
2795 * accounting, no actual extent tree updates.
2797 if (actual_count > 0) {
2798 u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
2802 * We weigh the current average higher than our current runtime
2803 * to avoid large swings in the average.
2805 spin_lock(&delayed_refs->lock);
2806 avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
2807 fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */
2808 spin_unlock(&delayed_refs->lock);
2813 #ifdef SCRAMBLE_DELAYED_REFS
2815 * Normally delayed refs get processed in ascending bytenr order. This
2816 * correlates in most cases to the order added. To expose dependencies on this
2817 * order, we start to process the tree in the middle instead of the beginning
2819 static u64 find_middle(struct rb_root *root)
2821 struct rb_node *n = root->rb_node;
2822 struct btrfs_delayed_ref_node *entry;
2825 u64 first = 0, last = 0;
2829 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2830 first = entry->bytenr;
2834 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2835 last = entry->bytenr;
2840 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2841 WARN_ON(!entry->in_tree);
2843 middle = entry->bytenr;
2856 static inline u64 heads_to_leaves(struct btrfs_fs_info *fs_info, u64 heads)
2860 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2861 sizeof(struct btrfs_extent_inline_ref));
2862 if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2863 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2866 * We don't ever fill up leaves all the way so multiply by 2 just to be
2867 * closer to what we're really going to want to use.
2869 return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info));
2873 * Takes the number of bytes to be csumm'ed and figures out how many leaves it
2874 * would require to store the csums for that many bytes.
2876 u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes)
2879 u64 num_csums_per_leaf;
2882 csum_size = BTRFS_MAX_ITEM_SIZE(fs_info);
2883 num_csums_per_leaf = div64_u64(csum_size,
2884 (u64)btrfs_super_csum_size(fs_info->super_copy));
2885 num_csums = div64_u64(csum_bytes, fs_info->sectorsize);
2886 num_csums += num_csums_per_leaf - 1;
2887 num_csums = div64_u64(num_csums, num_csums_per_leaf);
2891 int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
2892 struct btrfs_fs_info *fs_info)
2894 struct btrfs_block_rsv *global_rsv;
2895 u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
2896 u64 csum_bytes = trans->transaction->delayed_refs.pending_csums;
2897 unsigned int num_dirty_bgs = trans->transaction->num_dirty_bgs;
2898 u64 num_bytes, num_dirty_bgs_bytes;
2901 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
2902 num_heads = heads_to_leaves(fs_info, num_heads);
2904 num_bytes += (num_heads - 1) * fs_info->nodesize;
2906 num_bytes += btrfs_csum_bytes_to_leaves(fs_info, csum_bytes) *
2908 num_dirty_bgs_bytes = btrfs_calc_trans_metadata_size(fs_info,
2910 global_rsv = &fs_info->global_block_rsv;
2913 * If we can't allocate any more chunks lets make sure we have _lots_ of
2914 * wiggle room since running delayed refs can create more delayed refs.
2916 if (global_rsv->space_info->full) {
2917 num_dirty_bgs_bytes <<= 1;
2921 spin_lock(&global_rsv->lock);
2922 if (global_rsv->reserved <= num_bytes + num_dirty_bgs_bytes)
2924 spin_unlock(&global_rsv->lock);
2928 int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
2929 struct btrfs_fs_info *fs_info)
2932 atomic_read(&trans->transaction->delayed_refs.num_entries);
2937 avg_runtime = fs_info->avg_delayed_ref_runtime;
2938 val = num_entries * avg_runtime;
2939 if (val >= NSEC_PER_SEC)
2941 if (val >= NSEC_PER_SEC / 2)
2944 return btrfs_check_space_for_delayed_refs(trans, fs_info);
2947 struct async_delayed_refs {
2948 struct btrfs_root *root;
2953 struct completion wait;
2954 struct btrfs_work work;
2957 static inline struct async_delayed_refs *
2958 to_async_delayed_refs(struct btrfs_work *work)
2960 return container_of(work, struct async_delayed_refs, work);
2963 static void delayed_ref_async_start(struct btrfs_work *work)
2965 struct async_delayed_refs *async = to_async_delayed_refs(work);
2966 struct btrfs_trans_handle *trans;
2967 struct btrfs_fs_info *fs_info = async->root->fs_info;
2970 /* if the commit is already started, we don't need to wait here */
2971 if (btrfs_transaction_blocked(fs_info))
2974 trans = btrfs_join_transaction(async->root);
2975 if (IS_ERR(trans)) {
2976 async->error = PTR_ERR(trans);
2981 * trans->sync means that when we call end_transaction, we won't
2982 * wait on delayed refs
2986 /* Don't bother flushing if we got into a different transaction */
2987 if (trans->transid > async->transid)
2990 ret = btrfs_run_delayed_refs(trans, fs_info, async->count);
2994 ret = btrfs_end_transaction(trans);
2995 if (ret && !async->error)
2999 complete(&async->wait);
3004 int btrfs_async_run_delayed_refs(struct btrfs_fs_info *fs_info,
3005 unsigned long count, u64 transid, int wait)
3007 struct async_delayed_refs *async;
3010 async = kmalloc(sizeof(*async), GFP_NOFS);
3014 async->root = fs_info->tree_root;
3015 async->count = count;
3017 async->transid = transid;
3022 init_completion(&async->wait);
3024 btrfs_init_work(&async->work, btrfs_extent_refs_helper,
3025 delayed_ref_async_start, NULL, NULL);
3027 btrfs_queue_work(fs_info->extent_workers, &async->work);
3030 wait_for_completion(&async->wait);
3039 * this starts processing the delayed reference count updates and
3040 * extent insertions we have queued up so far. count can be
3041 * 0, which means to process everything in the tree at the start
3042 * of the run (but not newly added entries), or it can be some target
3043 * number you'd like to process.
3045 * Returns 0 on success or if called with an aborted transaction
3046 * Returns <0 on error and aborts the transaction
3048 int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
3049 struct btrfs_fs_info *fs_info, unsigned long count)
3051 struct rb_node *node;
3052 struct btrfs_delayed_ref_root *delayed_refs;
3053 struct btrfs_delayed_ref_head *head;
3055 int run_all = count == (unsigned long)-1;
3056 bool can_flush_pending_bgs = trans->can_flush_pending_bgs;
3058 /* We'll clean this up in btrfs_cleanup_transaction */