1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
6 #include <linux/sched.h>
7 #include <linux/sched/signal.h>
8 #include <linux/pagemap.h>
9 #include <linux/writeback.h>
10 #include <linux/blkdev.h>
11 #include <linux/sort.h>
12 #include <linux/rcupdate.h>
13 #include <linux/kthread.h>
14 #include <linux/slab.h>
15 #include <linux/ratelimit.h>
16 #include <linux/percpu_counter.h>
17 #include <linux/lockdep.h>
18 #include <linux/crc32c.h>
21 #include "print-tree.h"
25 #include "free-space-cache.h"
26 #include "free-space-tree.h"
30 #include "ref-verify.h"
32 #undef SCRAMBLE_DELAYED_REFS
35 * control flags for do_chunk_alloc's force field
36 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
37 * if we really need one.
39 * CHUNK_ALLOC_LIMITED means to only try and allocate one
40 * if we have very few chunks already allocated. This is
41 * used as part of the clustering code to help make sure
42 * we have a good pool of storage to cluster in, without
43 * filling the FS with empty chunks
45 * CHUNK_ALLOC_FORCE means it must try to allocate one
49 CHUNK_ALLOC_NO_FORCE = 0,
50 CHUNK_ALLOC_LIMITED = 1,
51 CHUNK_ALLOC_FORCE = 2,
54 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
55 struct btrfs_fs_info *fs_info,
56 struct btrfs_delayed_ref_node *node, u64 parent,
57 u64 root_objectid, u64 owner_objectid,
58 u64 owner_offset, int refs_to_drop,
59 struct btrfs_delayed_extent_op *extra_op);
60 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
61 struct extent_buffer *leaf,
62 struct btrfs_extent_item *ei);
63 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
64 struct btrfs_fs_info *fs_info,
65 u64 parent, u64 root_objectid,
66 u64 flags, u64 owner, u64 offset,
67 struct btrfs_key *ins, int ref_mod);
68 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
69 struct btrfs_delayed_ref_node *node,
70 struct btrfs_delayed_extent_op *extent_op);
71 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
72 struct btrfs_fs_info *fs_info, u64 flags,
74 static int find_next_key(struct btrfs_path *path, int level,
75 struct btrfs_key *key);
76 static void dump_space_info(struct btrfs_fs_info *fs_info,
77 struct btrfs_space_info *info, u64 bytes,
78 int dump_block_groups);
79 static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
81 static void space_info_add_new_bytes(struct btrfs_fs_info *fs_info,
82 struct btrfs_space_info *space_info,
84 static void space_info_add_old_bytes(struct btrfs_fs_info *fs_info,
85 struct btrfs_space_info *space_info,
89 block_group_cache_done(struct btrfs_block_group_cache *cache)
92 return cache->cached == BTRFS_CACHE_FINISHED ||
93 cache->cached == BTRFS_CACHE_ERROR;
96 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
98 return (cache->flags & bits) == bits;
101 void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
103 atomic_inc(&cache->count);
106 void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
108 if (atomic_dec_and_test(&cache->count)) {
109 WARN_ON(cache->pinned > 0);
110 WARN_ON(cache->reserved > 0);
113 * If not empty, someone is still holding mutex of
114 * full_stripe_lock, which can only be released by caller.
115 * And it will definitely cause use-after-free when caller
116 * tries to release full stripe lock.
118 * No better way to resolve, but only to warn.
120 WARN_ON(!RB_EMPTY_ROOT(&cache->full_stripe_locks_root.root));
121 kfree(cache->free_space_ctl);
127 * this adds the block group to the fs_info rb tree for the block group
130 static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
131 struct btrfs_block_group_cache *block_group)
134 struct rb_node *parent = NULL;
135 struct btrfs_block_group_cache *cache;
137 spin_lock(&info->block_group_cache_lock);
138 p = &info->block_group_cache_tree.rb_node;
142 cache = rb_entry(parent, struct btrfs_block_group_cache,
144 if (block_group->key.objectid < cache->key.objectid) {
146 } else if (block_group->key.objectid > cache->key.objectid) {
149 spin_unlock(&info->block_group_cache_lock);
154 rb_link_node(&block_group->cache_node, parent, p);
155 rb_insert_color(&block_group->cache_node,
156 &info->block_group_cache_tree);
158 if (info->first_logical_byte > block_group->key.objectid)
159 info->first_logical_byte = block_group->key.objectid;
161 spin_unlock(&info->block_group_cache_lock);
167 * This will return the block group at or after bytenr if contains is 0, else
168 * it will return the block group that contains the bytenr
170 static struct btrfs_block_group_cache *
171 block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
174 struct btrfs_block_group_cache *cache, *ret = NULL;
178 spin_lock(&info->block_group_cache_lock);
179 n = info->block_group_cache_tree.rb_node;
182 cache = rb_entry(n, struct btrfs_block_group_cache,
184 end = cache->key.objectid + cache->key.offset - 1;
185 start = cache->key.objectid;
187 if (bytenr < start) {
188 if (!contains && (!ret || start < ret->key.objectid))
191 } else if (bytenr > start) {
192 if (contains && bytenr <= end) {
203 btrfs_get_block_group(ret);
204 if (bytenr == 0 && info->first_logical_byte > ret->key.objectid)
205 info->first_logical_byte = ret->key.objectid;
207 spin_unlock(&info->block_group_cache_lock);
212 static int add_excluded_extent(struct btrfs_fs_info *fs_info,
213 u64 start, u64 num_bytes)
215 u64 end = start + num_bytes - 1;
216 set_extent_bits(&fs_info->freed_extents[0],
217 start, end, EXTENT_UPTODATE);
218 set_extent_bits(&fs_info->freed_extents[1],
219 start, end, EXTENT_UPTODATE);
223 static void free_excluded_extents(struct btrfs_fs_info *fs_info,
224 struct btrfs_block_group_cache *cache)
228 start = cache->key.objectid;
229 end = start + cache->key.offset - 1;
231 clear_extent_bits(&fs_info->freed_extents[0],
232 start, end, EXTENT_UPTODATE);
233 clear_extent_bits(&fs_info->freed_extents[1],
234 start, end, EXTENT_UPTODATE);
237 static int exclude_super_stripes(struct btrfs_fs_info *fs_info,
238 struct btrfs_block_group_cache *cache)
245 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
246 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
247 cache->bytes_super += stripe_len;
248 ret = add_excluded_extent(fs_info, cache->key.objectid,
254 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
255 bytenr = btrfs_sb_offset(i);
256 ret = btrfs_rmap_block(fs_info, cache->key.objectid,
257 bytenr, &logical, &nr, &stripe_len);
264 if (logical[nr] > cache->key.objectid +
268 if (logical[nr] + stripe_len <= cache->key.objectid)
272 if (start < cache->key.objectid) {
273 start = cache->key.objectid;
274 len = (logical[nr] + stripe_len) - start;
276 len = min_t(u64, stripe_len,
277 cache->key.objectid +
278 cache->key.offset - start);
281 cache->bytes_super += len;
282 ret = add_excluded_extent(fs_info, start, len);
294 static struct btrfs_caching_control *
295 get_caching_control(struct btrfs_block_group_cache *cache)
297 struct btrfs_caching_control *ctl;
299 spin_lock(&cache->lock);
300 if (!cache->caching_ctl) {
301 spin_unlock(&cache->lock);
305 ctl = cache->caching_ctl;
306 refcount_inc(&ctl->count);
307 spin_unlock(&cache->lock);
311 static void put_caching_control(struct btrfs_caching_control *ctl)
313 if (refcount_dec_and_test(&ctl->count))
317 #ifdef CONFIG_BTRFS_DEBUG
318 static void fragment_free_space(struct btrfs_block_group_cache *block_group)
320 struct btrfs_fs_info *fs_info = block_group->fs_info;
321 u64 start = block_group->key.objectid;
322 u64 len = block_group->key.offset;
323 u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ?
324 fs_info->nodesize : fs_info->sectorsize;
325 u64 step = chunk << 1;
327 while (len > chunk) {
328 btrfs_remove_free_space(block_group, start, chunk);
339 * this is only called by cache_block_group, since we could have freed extents
340 * we need to check the pinned_extents for any extents that can't be used yet
341 * since their free space will be released as soon as the transaction commits.
343 u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
346 struct btrfs_fs_info *info = block_group->fs_info;
347 u64 extent_start, extent_end, size, total_added = 0;
350 while (start < end) {
351 ret = find_first_extent_bit(info->pinned_extents, start,
352 &extent_start, &extent_end,
353 EXTENT_DIRTY | EXTENT_UPTODATE,
358 if (extent_start <= start) {
359 start = extent_end + 1;
360 } else if (extent_start > start && extent_start < end) {
361 size = extent_start - start;
363 ret = btrfs_add_free_space(block_group, start,
365 BUG_ON(ret); /* -ENOMEM or logic error */
366 start = extent_end + 1;
375 ret = btrfs_add_free_space(block_group, start, size);
376 BUG_ON(ret); /* -ENOMEM or logic error */
382 static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
384 struct btrfs_block_group_cache *block_group = caching_ctl->block_group;
385 struct btrfs_fs_info *fs_info = block_group->fs_info;
386 struct btrfs_root *extent_root = fs_info->extent_root;
387 struct btrfs_path *path;
388 struct extent_buffer *leaf;
389 struct btrfs_key key;
396 path = btrfs_alloc_path();
400 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
402 #ifdef CONFIG_BTRFS_DEBUG
404 * If we're fragmenting we don't want to make anybody think we can
405 * allocate from this block group until we've had a chance to fragment
408 if (btrfs_should_fragment_free_space(block_group))
412 * We don't want to deadlock with somebody trying to allocate a new
413 * extent for the extent root while also trying to search the extent
414 * root to add free space. So we skip locking and search the commit
415 * root, since its read-only
417 path->skip_locking = 1;
418 path->search_commit_root = 1;
419 path->reada = READA_FORWARD;
423 key.type = BTRFS_EXTENT_ITEM_KEY;
426 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
430 leaf = path->nodes[0];
431 nritems = btrfs_header_nritems(leaf);
434 if (btrfs_fs_closing(fs_info) > 1) {
439 if (path->slots[0] < nritems) {
440 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
442 ret = find_next_key(path, 0, &key);
446 if (need_resched() ||
447 rwsem_is_contended(&fs_info->commit_root_sem)) {
449 caching_ctl->progress = last;
450 btrfs_release_path(path);
451 up_read(&fs_info->commit_root_sem);
452 mutex_unlock(&caching_ctl->mutex);
454 mutex_lock(&caching_ctl->mutex);
455 down_read(&fs_info->commit_root_sem);
459 ret = btrfs_next_leaf(extent_root, path);
464 leaf = path->nodes[0];
465 nritems = btrfs_header_nritems(leaf);
469 if (key.objectid < last) {
472 key.type = BTRFS_EXTENT_ITEM_KEY;
475 caching_ctl->progress = last;
476 btrfs_release_path(path);
480 if (key.objectid < block_group->key.objectid) {
485 if (key.objectid >= block_group->key.objectid +
486 block_group->key.offset)
489 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
490 key.type == BTRFS_METADATA_ITEM_KEY) {
491 total_found += add_new_free_space(block_group, last,
493 if (key.type == BTRFS_METADATA_ITEM_KEY)
494 last = key.objectid +
497 last = key.objectid + key.offset;
499 if (total_found > CACHING_CTL_WAKE_UP) {
502 wake_up(&caching_ctl->wait);
509 total_found += add_new_free_space(block_group, last,
510 block_group->key.objectid +
511 block_group->key.offset);
512 caching_ctl->progress = (u64)-1;
515 btrfs_free_path(path);
519 static noinline void caching_thread(struct btrfs_work *work)
521 struct btrfs_block_group_cache *block_group;
522 struct btrfs_fs_info *fs_info;
523 struct btrfs_caching_control *caching_ctl;
526 caching_ctl = container_of(work, struct btrfs_caching_control, work);
527 block_group = caching_ctl->block_group;
528 fs_info = block_group->fs_info;
530 mutex_lock(&caching_ctl->mutex);
531 down_read(&fs_info->commit_root_sem);
533 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
534 ret = load_free_space_tree(caching_ctl);
536 ret = load_extent_tree_free(caching_ctl);
538 spin_lock(&block_group->lock);
539 block_group->caching_ctl = NULL;
540 block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED;
541 spin_unlock(&block_group->lock);
543 #ifdef CONFIG_BTRFS_DEBUG
544 if (btrfs_should_fragment_free_space(block_group)) {
547 spin_lock(&block_group->space_info->lock);
548 spin_lock(&block_group->lock);
549 bytes_used = block_group->key.offset -
550 btrfs_block_group_used(&block_group->item);
551 block_group->space_info->bytes_used += bytes_used >> 1;
552 spin_unlock(&block_group->lock);
553 spin_unlock(&block_group->space_info->lock);
554 fragment_free_space(block_group);
558 caching_ctl->progress = (u64)-1;
560 up_read(&fs_info->commit_root_sem);
561 free_excluded_extents(fs_info, block_group);
562 mutex_unlock(&caching_ctl->mutex);
564 wake_up(&caching_ctl->wait);
566 put_caching_control(caching_ctl);
567 btrfs_put_block_group(block_group);
570 static int cache_block_group(struct btrfs_block_group_cache *cache,
574 struct btrfs_fs_info *fs_info = cache->fs_info;
575 struct btrfs_caching_control *caching_ctl;
578 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
582 INIT_LIST_HEAD(&caching_ctl->list);
583 mutex_init(&caching_ctl->mutex);
584 init_waitqueue_head(&caching_ctl->wait);
585 caching_ctl->block_group = cache;
586 caching_ctl->progress = cache->key.objectid;
587 refcount_set(&caching_ctl->count, 1);
588 btrfs_init_work(&caching_ctl->work, btrfs_cache_helper,
589 caching_thread, NULL, NULL);
591 spin_lock(&cache->lock);
593 * This should be a rare occasion, but this could happen I think in the
594 * case where one thread starts to load the space cache info, and then
595 * some other thread starts a transaction commit which tries to do an
596 * allocation while the other thread is still loading the space cache
597 * info. The previous loop should have kept us from choosing this block
598 * group, but if we've moved to the state where we will wait on caching
599 * block groups we need to first check if we're doing a fast load here,
600 * so we can wait for it to finish, otherwise we could end up allocating
601 * from a block group who's cache gets evicted for one reason or
604 while (cache->cached == BTRFS_CACHE_FAST) {
605 struct btrfs_caching_control *ctl;
607 ctl = cache->caching_ctl;
608 refcount_inc(&ctl->count);
609 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
610 spin_unlock(&cache->lock);
614 finish_wait(&ctl->wait, &wait);
615 put_caching_control(ctl);
616 spin_lock(&cache->lock);
619 if (cache->cached != BTRFS_CACHE_NO) {
620 spin_unlock(&cache->lock);
624 WARN_ON(cache->caching_ctl);
625 cache->caching_ctl = caching_ctl;
626 cache->cached = BTRFS_CACHE_FAST;
627 spin_unlock(&cache->lock);
629 if (btrfs_test_opt(fs_info, SPACE_CACHE)) {
630 mutex_lock(&caching_ctl->mutex);
631 ret = load_free_space_cache(fs_info, cache);
633 spin_lock(&cache->lock);
635 cache->caching_ctl = NULL;
636 cache->cached = BTRFS_CACHE_FINISHED;
637 cache->last_byte_to_unpin = (u64)-1;
638 caching_ctl->progress = (u64)-1;
640 if (load_cache_only) {
641 cache->caching_ctl = NULL;
642 cache->cached = BTRFS_CACHE_NO;
644 cache->cached = BTRFS_CACHE_STARTED;
645 cache->has_caching_ctl = 1;
648 spin_unlock(&cache->lock);
649 #ifdef CONFIG_BTRFS_DEBUG
651 btrfs_should_fragment_free_space(cache)) {
654 spin_lock(&cache->space_info->lock);
655 spin_lock(&cache->lock);
656 bytes_used = cache->key.offset -
657 btrfs_block_group_used(&cache->item);
658 cache->space_info->bytes_used += bytes_used >> 1;
659 spin_unlock(&cache->lock);
660 spin_unlock(&cache->space_info->lock);
661 fragment_free_space(cache);
664 mutex_unlock(&caching_ctl->mutex);
666 wake_up(&caching_ctl->wait);
668 put_caching_control(caching_ctl);
669 free_excluded_extents(fs_info, cache);
674 * We're either using the free space tree or no caching at all.
675 * Set cached to the appropriate value and wakeup any waiters.
677 spin_lock(&cache->lock);
678 if (load_cache_only) {
679 cache->caching_ctl = NULL;
680 cache->cached = BTRFS_CACHE_NO;
682 cache->cached = BTRFS_CACHE_STARTED;
683 cache->has_caching_ctl = 1;
685 spin_unlock(&cache->lock);
686 wake_up(&caching_ctl->wait);
689 if (load_cache_only) {
690 put_caching_control(caching_ctl);
694 down_write(&fs_info->commit_root_sem);
695 refcount_inc(&caching_ctl->count);
696 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
697 up_write(&fs_info->commit_root_sem);
699 btrfs_get_block_group(cache);
701 btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work);
707 * return the block group that starts at or after bytenr
709 static struct btrfs_block_group_cache *
710 btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
712 return block_group_cache_tree_search(info, bytenr, 0);
716 * return the block group that contains the given bytenr
718 struct btrfs_block_group_cache *btrfs_lookup_block_group(
719 struct btrfs_fs_info *info,
722 return block_group_cache_tree_search(info, bytenr, 1);
725 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
728 struct list_head *head = &info->space_info;
729 struct btrfs_space_info *found;
731 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
734 list_for_each_entry_rcu(found, head, list) {
735 if (found->flags & flags) {
744 static void add_pinned_bytes(struct btrfs_fs_info *fs_info, s64 num_bytes,
745 bool metadata, u64 root_objectid)
747 struct btrfs_space_info *space_info;
751 if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
752 flags = BTRFS_BLOCK_GROUP_SYSTEM;
754 flags = BTRFS_BLOCK_GROUP_METADATA;
756 flags = BTRFS_BLOCK_GROUP_DATA;
759 space_info = __find_space_info(fs_info, flags);
761 percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
765 * after adding space to the filesystem, we need to clear the full flags
766 * on all the space infos.
768 void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
770 struct list_head *head = &info->space_info;
771 struct btrfs_space_info *found;
774 list_for_each_entry_rcu(found, head, list)
779 /* simple helper to search for an existing data extent at a given offset */
780 int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len)
783 struct btrfs_key key;
784 struct btrfs_path *path;
786 path = btrfs_alloc_path();
790 key.objectid = start;
792 key.type = BTRFS_EXTENT_ITEM_KEY;
793 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
794 btrfs_free_path(path);
799 * helper function to lookup reference count and flags of a tree block.
801 * the head node for delayed ref is used to store the sum of all the
802 * reference count modifications queued up in the rbtree. the head
803 * node may also store the extent flags to set. This way you can check
804 * to see what the reference count and extent flags would be if all of
805 * the delayed refs are not processed.
807 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
808 struct btrfs_fs_info *fs_info, u64 bytenr,
809 u64 offset, int metadata, u64 *refs, u64 *flags)
811 struct btrfs_delayed_ref_head *head;
812 struct btrfs_delayed_ref_root *delayed_refs;
813 struct btrfs_path *path;
814 struct btrfs_extent_item *ei;
815 struct extent_buffer *leaf;
816 struct btrfs_key key;
823 * If we don't have skinny metadata, don't bother doing anything
826 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) {
827 offset = fs_info->nodesize;
831 path = btrfs_alloc_path();
836 path->skip_locking = 1;
837 path->search_commit_root = 1;
841 key.objectid = bytenr;
844 key.type = BTRFS_METADATA_ITEM_KEY;
846 key.type = BTRFS_EXTENT_ITEM_KEY;
848 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
852 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
853 if (path->slots[0]) {
855 btrfs_item_key_to_cpu(path->nodes[0], &key,
857 if (key.objectid == bytenr &&
858 key.type == BTRFS_EXTENT_ITEM_KEY &&
859 key.offset == fs_info->nodesize)
865 leaf = path->nodes[0];
866 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
867 if (item_size >= sizeof(*ei)) {
868 ei = btrfs_item_ptr(leaf, path->slots[0],
869 struct btrfs_extent_item);
870 num_refs = btrfs_extent_refs(leaf, ei);
871 extent_flags = btrfs_extent_flags(leaf, ei);
873 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
874 struct btrfs_extent_item_v0 *ei0;
875 BUG_ON(item_size != sizeof(*ei0));
876 ei0 = btrfs_item_ptr(leaf, path->slots[0],
877 struct btrfs_extent_item_v0);
878 num_refs = btrfs_extent_refs_v0(leaf, ei0);
879 /* FIXME: this isn't correct for data */
880 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
885 BUG_ON(num_refs == 0);
895 delayed_refs = &trans->transaction->delayed_refs;
896 spin_lock(&delayed_refs->lock);
897 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
899 if (!mutex_trylock(&head->mutex)) {
900 refcount_inc(&head->refs);
901 spin_unlock(&delayed_refs->lock);
903 btrfs_release_path(path);
906 * Mutex was contended, block until it's released and try
909 mutex_lock(&head->mutex);
910 mutex_unlock(&head->mutex);
911 btrfs_put_delayed_ref_head(head);
914 spin_lock(&head->lock);
915 if (head->extent_op && head->extent_op->update_flags)
916 extent_flags |= head->extent_op->flags_to_set;
918 BUG_ON(num_refs == 0);
920 num_refs += head->ref_mod;
921 spin_unlock(&head->lock);
922 mutex_unlock(&head->mutex);
924 spin_unlock(&delayed_refs->lock);
926 WARN_ON(num_refs == 0);
930 *flags = extent_flags;
932 btrfs_free_path(path);
937 * Back reference rules. Back refs have three main goals:
939 * 1) differentiate between all holders of references to an extent so that
940 * when a reference is dropped we can make sure it was a valid reference
941 * before freeing the extent.
943 * 2) Provide enough information to quickly find the holders of an extent
944 * if we notice a given block is corrupted or bad.
946 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
947 * maintenance. This is actually the same as #2, but with a slightly
948 * different use case.
950 * There are two kinds of back refs. The implicit back refs is optimized
951 * for pointers in non-shared tree blocks. For a given pointer in a block,
952 * back refs of this kind provide information about the block's owner tree
953 * and the pointer's key. These information allow us to find the block by
954 * b-tree searching. The full back refs is for pointers in tree blocks not
955 * referenced by their owner trees. The location of tree block is recorded
956 * in the back refs. Actually the full back refs is generic, and can be
957 * used in all cases the implicit back refs is used. The major shortcoming
958 * of the full back refs is its overhead. Every time a tree block gets
959 * COWed, we have to update back refs entry for all pointers in it.
961 * For a newly allocated tree block, we use implicit back refs for
962 * pointers in it. This means most tree related operations only involve
963 * implicit back refs. For a tree block created in old transaction, the
964 * only way to drop a reference to it is COW it. So we can detect the
965 * event that tree block loses its owner tree's reference and do the
966 * back refs conversion.
968 * When a tree block is COWed through a tree, there are four cases:
970 * The reference count of the block is one and the tree is the block's
971 * owner tree. Nothing to do in this case.
973 * The reference count of the block is one and the tree is not the
974 * block's owner tree. In this case, full back refs is used for pointers
975 * in the block. Remove these full back refs, add implicit back refs for
976 * every pointers in the new block.
978 * The reference count of the block is greater than one and the tree is
979 * the block's owner tree. In this case, implicit back refs is used for
980 * pointers in the block. Add full back refs for every pointers in the
981 * block, increase lower level extents' reference counts. The original
982 * implicit back refs are entailed to the new block.
984 * The reference count of the block is greater than one and the tree is
985 * not the block's owner tree. Add implicit back refs for every pointer in
986 * the new block, increase lower level extents' reference count.
988 * Back Reference Key composing:
990 * The key objectid corresponds to the first byte in the extent,
991 * The key type is used to differentiate between types of back refs.
992 * There are different meanings of the key offset for different types
995 * File extents can be referenced by:
997 * - multiple snapshots, subvolumes, or different generations in one subvol
998 * - different files inside a single subvolume
999 * - different offsets inside a file (bookend extents in file.c)
1001 * The extent ref structure for the implicit back refs has fields for:
1003 * - Objectid of the subvolume root
1004 * - objectid of the file holding the reference
1005 * - original offset in the file
1006 * - how many bookend extents
1008 * The key offset for the implicit back refs is hash of the first
1011 * The extent ref structure for the full back refs has field for:
1013 * - number of pointers in the tree leaf
1015 * The key offset for the implicit back refs is the first byte of
1018 * When a file extent is allocated, The implicit back refs is used.
1019 * the fields are filled in:
1021 * (root_key.objectid, inode objectid, offset in file, 1)
1023 * When a file extent is removed file truncation, we find the
1024 * corresponding implicit back refs and check the following fields:
1026 * (btrfs_header_owner(leaf), inode objectid, offset in file)
1028 * Btree extents can be referenced by:
1030 * - Different subvolumes
1032 * Both the implicit back refs and the full back refs for tree blocks
1033 * only consist of key. The key offset for the implicit back refs is
1034 * objectid of block's owner tree. The key offset for the full back refs
1035 * is the first byte of parent block.
1037 * When implicit back refs is used, information about the lowest key and
1038 * level of the tree block are required. These information are stored in
1039 * tree block info structure.
1042 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1043 static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
1044 struct btrfs_fs_info *fs_info,
1045 struct btrfs_path *path,
1046 u64 owner, u32 extra_size)
1048 struct btrfs_root *root = fs_info->extent_root;
1049 struct btrfs_extent_item *item;
1050 struct btrfs_extent_item_v0 *ei0;
1051 struct btrfs_extent_ref_v0 *ref0;
1052 struct btrfs_tree_block_info *bi;
1053 struct extent_buffer *leaf;
1054 struct btrfs_key key;
1055 struct btrfs_key found_key;
1056 u32 new_size = sizeof(*item);
1060 leaf = path->nodes[0];
1061 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
1063 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1064 ei0 = btrfs_item_ptr(leaf, path->slots[0],
1065 struct btrfs_extent_item_v0);
1066 refs = btrfs_extent_refs_v0(leaf, ei0);
1068 if (owner == (u64)-1) {
1070 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1071 ret = btrfs_next_leaf(root, path);
1074 BUG_ON(ret > 0); /* Corruption */
1075 leaf = path->nodes[0];
1077 btrfs_item_key_to_cpu(leaf, &found_key,
1079 BUG_ON(key.objectid != found_key.objectid);
1080 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
1084 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1085 struct btrfs_extent_ref_v0);
1086 owner = btrfs_ref_objectid_v0(leaf, ref0);
1090 btrfs_release_path(path);
1092 if (owner < BTRFS_FIRST_FREE_OBJECTID)
1093 new_size += sizeof(*bi);
1095 new_size -= sizeof(*ei0);
1096 ret = btrfs_search_slot(trans, root, &key, path,
1097 new_size + extra_size, 1);
1100 BUG_ON(ret); /* Corruption */
1102 btrfs_extend_item(fs_info, path, new_size);
1104 leaf = path->nodes[0];
1105 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1106 btrfs_set_extent_refs(leaf, item, refs);
1107 /* FIXME: get real generation */
1108 btrfs_set_extent_generation(leaf, item, 0);
1109 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1110 btrfs_set_extent_flags(leaf, item,
1111 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1112 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1113 bi = (struct btrfs_tree_block_info *)(item + 1);
1114 /* FIXME: get first key of the block */
1115 memzero_extent_buffer(leaf, (unsigned long)bi, sizeof(*bi));
1116 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1118 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1120 btrfs_mark_buffer_dirty(leaf);
1126 * is_data == BTRFS_REF_TYPE_BLOCK, tree block type is required,
1127 * is_data == BTRFS_REF_TYPE_DATA, data type is requried,
1128 * is_data == BTRFS_REF_TYPE_ANY, either type is OK.
1130 int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
1131 struct btrfs_extent_inline_ref *iref,
1132 enum btrfs_inline_ref_type is_data)
1134 int type = btrfs_extent_inline_ref_type(eb, iref);
1135 u64 offset = btrfs_extent_inline_ref_offset(eb, iref);
1137 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
1138 type == BTRFS_SHARED_BLOCK_REF_KEY ||
1139 type == BTRFS_SHARED_DATA_REF_KEY ||
1140 type == BTRFS_EXTENT_DATA_REF_KEY) {
1141 if (is_data == BTRFS_REF_TYPE_BLOCK) {
1142 if (type == BTRFS_TREE_BLOCK_REF_KEY)
1144 if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1145 ASSERT(eb->fs_info);
1147 * Every shared one has parent tree
1148 * block, which must be aligned to
1152 IS_ALIGNED(offset, eb->fs_info->nodesize))
1155 } else if (is_data == BTRFS_REF_TYPE_DATA) {
1156 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1158 if (type == BTRFS_SHARED_DATA_REF_KEY) {
1159 ASSERT(eb->fs_info);
1161 * Every shared one has parent tree
1162 * block, which must be aligned to
1166 IS_ALIGNED(offset, eb->fs_info->nodesize))
1170 ASSERT(is_data == BTRFS_REF_TYPE_ANY);
1175 btrfs_print_leaf((struct extent_buffer *)eb);
1176 btrfs_err(eb->fs_info, "eb %llu invalid extent inline ref type %d",
1180 return BTRFS_REF_TYPE_INVALID;
1183 static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1185 u32 high_crc = ~(u32)0;
1186 u32 low_crc = ~(u32)0;
1189 lenum = cpu_to_le64(root_objectid);
1190 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
1191 lenum = cpu_to_le64(owner);
1192 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
1193 lenum = cpu_to_le64(offset);
1194 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
1196 return ((u64)high_crc << 31) ^ (u64)low_crc;
1199 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1200 struct btrfs_extent_data_ref *ref)
1202 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1203 btrfs_extent_data_ref_objectid(leaf, ref),
1204 btrfs_extent_data_ref_offset(leaf, ref));
1207 static int match_extent_data_ref(struct extent_buffer *leaf,
1208 struct btrfs_extent_data_ref *ref,
1209 u64 root_objectid, u64 owner, u64 offset)
1211 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1212 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1213 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1218 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1219 struct btrfs_fs_info *fs_info,
1220 struct btrfs_path *path,
1221 u64 bytenr, u64 parent,
1223 u64 owner, u64 offset)
1225 struct btrfs_root *root = fs_info->extent_root;
1226 struct btrfs_key key;
1227 struct btrfs_extent_data_ref *ref;
1228 struct extent_buffer *leaf;
1234 key.objectid = bytenr;
1236 key.type = BTRFS_SHARED_DATA_REF_KEY;
1237 key.offset = parent;
1239 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1240 key.offset = hash_extent_data_ref(root_objectid,
1245 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1254 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1255 key.type = BTRFS_EXTENT_REF_V0_KEY;
1256 btrfs_release_path(path);
1257 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1268 leaf = path->nodes[0];
1269 nritems = btrfs_header_nritems(leaf);
1271 if (path->slots[0] >= nritems) {
1272 ret = btrfs_next_leaf(root, path);
1278 leaf = path->nodes[0];
1279 nritems = btrfs_header_nritems(leaf);
1283 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1284 if (key.objectid != bytenr ||
1285 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1288 ref = btrfs_item_ptr(leaf, path->slots[0],
1289 struct btrfs_extent_data_ref);
1291 if (match_extent_data_ref(leaf, ref, root_objectid,
1294 btrfs_release_path(path);
1306 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1307 struct btrfs_fs_info *fs_info,
1308 struct btrfs_path *path,
1309 u64 bytenr, u64 parent,
1310 u64 root_objectid, u64 owner,
1311 u64 offset, int refs_to_add)
1313 struct btrfs_root *root = fs_info->extent_root;
1314 struct btrfs_key key;
1315 struct extent_buffer *leaf;
1320 key.objectid = bytenr;
1322 key.type = BTRFS_SHARED_DATA_REF_KEY;
1323 key.offset = parent;
1324 size = sizeof(struct btrfs_shared_data_ref);
1326 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1327 key.offset = hash_extent_data_ref(root_objectid,
1329 size = sizeof(struct btrfs_extent_data_ref);
1332 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1333 if (ret && ret != -EEXIST)
1336 leaf = path->nodes[0];
1338 struct btrfs_shared_data_ref *ref;
1339 ref = btrfs_item_ptr(leaf, path->slots[0],
1340 struct btrfs_shared_data_ref);
1342 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1344 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1345 num_refs += refs_to_add;
1346 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1349 struct btrfs_extent_data_ref *ref;
1350 while (ret == -EEXIST) {
1351 ref = btrfs_item_ptr(leaf, path->slots[0],
1352 struct btrfs_extent_data_ref);
1353 if (match_extent_data_ref(leaf, ref, root_objectid,
1356 btrfs_release_path(path);
1358 ret = btrfs_insert_empty_item(trans, root, path, &key,
1360 if (ret && ret != -EEXIST)
1363 leaf = path->nodes[0];
1365 ref = btrfs_item_ptr(leaf, path->slots[0],
1366 struct btrfs_extent_data_ref);
1368 btrfs_set_extent_data_ref_root(leaf, ref,
1370 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1371 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1372 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1374 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1375 num_refs += refs_to_add;
1376 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1379 btrfs_mark_buffer_dirty(leaf);
1382 btrfs_release_path(path);
1386 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1387 struct btrfs_fs_info *fs_info,
1388 struct btrfs_path *path,
1389 int refs_to_drop, int *last_ref)
1391 struct btrfs_key key;
1392 struct btrfs_extent_data_ref *ref1 = NULL;
1393 struct btrfs_shared_data_ref *ref2 = NULL;
1394 struct extent_buffer *leaf;
1398 leaf = path->nodes[0];
1399 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1401 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1402 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1403 struct btrfs_extent_data_ref);
1404 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1405 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1406 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1407 struct btrfs_shared_data_ref);
1408 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1409 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1410 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1411 struct btrfs_extent_ref_v0 *ref0;
1412 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1413 struct btrfs_extent_ref_v0);
1414 num_refs = btrfs_ref_count_v0(leaf, ref0);
1420 BUG_ON(num_refs < refs_to_drop);
1421 num_refs -= refs_to_drop;
1423 if (num_refs == 0) {
1424 ret = btrfs_del_item(trans, fs_info->extent_root, path);
1427 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1428 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1429 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1430 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1431 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1433 struct btrfs_extent_ref_v0 *ref0;
1434 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1435 struct btrfs_extent_ref_v0);
1436 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1439 btrfs_mark_buffer_dirty(leaf);
1444 static noinline u32 extent_data_ref_count(struct btrfs_path *path,
1445 struct btrfs_extent_inline_ref *iref)
1447 struct btrfs_key key;
1448 struct extent_buffer *leaf;
1449 struct btrfs_extent_data_ref *ref1;
1450 struct btrfs_shared_data_ref *ref2;
1454 leaf = path->nodes[0];
1455 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1458 * If type is invalid, we should have bailed out earlier than
1461 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
1462 ASSERT(type != BTRFS_REF_TYPE_INVALID);
1463 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1464 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1465 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1467 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1468 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1470 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1471 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1472 struct btrfs_extent_data_ref);
1473 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1474 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1475 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1476 struct btrfs_shared_data_ref);
1477 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1478 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1479 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1480 struct btrfs_extent_ref_v0 *ref0;
1481 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1482 struct btrfs_extent_ref_v0);
1483 num_refs = btrfs_ref_count_v0(leaf, ref0);
1491 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1492 struct btrfs_fs_info *fs_info,
1493 struct btrfs_path *path,
1494 u64 bytenr, u64 parent,
1497 struct btrfs_root *root = fs_info->extent_root;
1498 struct btrfs_key key;
1501 key.objectid = bytenr;
1503 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1504 key.offset = parent;
1506 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1507 key.offset = root_objectid;
1510 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1513 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1514 if (ret == -ENOENT && parent) {
1515 btrfs_release_path(path);
1516 key.type = BTRFS_EXTENT_REF_V0_KEY;
1517 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1525 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1526 struct btrfs_fs_info *fs_info,
1527 struct btrfs_path *path,
1528 u64 bytenr, u64 parent,
1531 struct btrfs_key key;
1534 key.objectid = bytenr;
1536 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1537 key.offset = parent;
1539 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1540 key.offset = root_objectid;
1543 ret = btrfs_insert_empty_item(trans, fs_info->extent_root,
1545 btrfs_release_path(path);
1549 static inline int extent_ref_type(u64 parent, u64 owner)
1552 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1554 type = BTRFS_SHARED_BLOCK_REF_KEY;
1556 type = BTRFS_TREE_BLOCK_REF_KEY;
1559 type = BTRFS_SHARED_DATA_REF_KEY;
1561 type = BTRFS_EXTENT_DATA_REF_KEY;
1566 static int find_next_key(struct btrfs_path *path, int level,
1567 struct btrfs_key *key)
1570 for (; level < BTRFS_MAX_LEVEL; level++) {
1571 if (!path->nodes[level])
1573 if (path->slots[level] + 1 >=
1574 btrfs_header_nritems(path->nodes[level]))
1577 btrfs_item_key_to_cpu(path->nodes[level], key,
1578 path->slots[level] + 1);
1580 btrfs_node_key_to_cpu(path->nodes[level], key,
1581 path->slots[level] + 1);
1588 * look for inline back ref. if back ref is found, *ref_ret is set
1589 * to the address of inline back ref, and 0 is returned.
1591 * if back ref isn't found, *ref_ret is set to the address where it
1592 * should be inserted, and -ENOENT is returned.
1594 * if insert is true and there are too many inline back refs, the path
1595 * points to the extent item, and -EAGAIN is returned.
1597 * NOTE: inline back refs are ordered in the same way that back ref
1598 * items in the tree are ordered.
1600 static noinline_for_stack
1601 int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1602 struct btrfs_fs_info *fs_info,
1603 struct btrfs_path *path,
1604 struct btrfs_extent_inline_ref **ref_ret,
1605 u64 bytenr, u64 num_bytes,
1606 u64 parent, u64 root_objectid,
1607 u64 owner, u64 offset, int insert)
1609 struct btrfs_root *root = fs_info->extent_root;
1610 struct btrfs_key key;
1611 struct extent_buffer *leaf;
1612 struct btrfs_extent_item *ei;
1613 struct btrfs_extent_inline_ref *iref;
1623 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
1626 key.objectid = bytenr;
1627 key.type = BTRFS_EXTENT_ITEM_KEY;
1628 key.offset = num_bytes;
1630 want = extent_ref_type(parent, owner);
1632 extra_size = btrfs_extent_inline_ref_size(want);
1633 path->keep_locks = 1;
1638 * Owner is our parent level, so we can just add one to get the level
1639 * for the block we are interested in.
1641 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
1642 key.type = BTRFS_METADATA_ITEM_KEY;
1647 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1654 * We may be a newly converted file system which still has the old fat
1655 * extent entries for metadata, so try and see if we have one of those.
1657 if (ret > 0 && skinny_metadata) {
1658 skinny_metadata = false;
1659 if (path->slots[0]) {
1661 btrfs_item_key_to_cpu(path->nodes[0], &key,
1663 if (key.objectid == bytenr &&
1664 key.type == BTRFS_EXTENT_ITEM_KEY &&
1665 key.offset == num_bytes)
1669 key.objectid = bytenr;
1670 key.type = BTRFS_EXTENT_ITEM_KEY;
1671 key.offset = num_bytes;
1672 btrfs_release_path(path);
1677 if (ret && !insert) {
1680 } else if (WARN_ON(ret)) {
1685 leaf = path->nodes[0];
1686 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1687 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1688 if (item_size < sizeof(*ei)) {
1693 ret = convert_extent_item_v0(trans, fs_info, path, owner,
1699 leaf = path->nodes[0];
1700 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1703 BUG_ON(item_size < sizeof(*ei));
1705 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1706 flags = btrfs_extent_flags(leaf, ei);
1708 ptr = (unsigned long)(ei + 1);
1709 end = (unsigned long)ei + item_size;
1711 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
1712 ptr += sizeof(struct btrfs_tree_block_info);
1716 if (owner >= BTRFS_FIRST_FREE_OBJECTID)
1717 needed = BTRFS_REF_TYPE_DATA;
1719 needed = BTRFS_REF_TYPE_BLOCK;
1727 iref = (struct btrfs_extent_inline_ref *)ptr;
1728 type = btrfs_get_extent_inline_ref_type(leaf, iref, needed);
1729 if (type == BTRFS_REF_TYPE_INVALID) {
1737 ptr += btrfs_extent_inline_ref_size(type);
1741 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1742 struct btrfs_extent_data_ref *dref;
1743 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1744 if (match_extent_data_ref(leaf, dref, root_objectid,
1749 if (hash_extent_data_ref_item(leaf, dref) <
1750 hash_extent_data_ref(root_objectid, owner, offset))
1754 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1756 if (parent == ref_offset) {
1760 if (ref_offset < parent)
1763 if (root_objectid == ref_offset) {
1767 if (ref_offset < root_objectid)
1771 ptr += btrfs_extent_inline_ref_size(type);
1773 if (err == -ENOENT && insert) {
1774 if (item_size + extra_size >=
1775 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1780 * To add new inline back ref, we have to make sure
1781 * there is no corresponding back ref item.
1782 * For simplicity, we just do not add new inline back
1783 * ref if there is any kind of item for this block
1785 if (find_next_key(path, 0, &key) == 0 &&
1786 key.objectid == bytenr &&
1787 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
1792 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1795 path->keep_locks = 0;
1796 btrfs_unlock_up_safe(path, 1);
1802 * helper to add new inline back ref
1804 static noinline_for_stack
1805 void setup_inline_extent_backref(struct btrfs_fs_info *fs_info,
1806 struct btrfs_path *path,
1807 struct btrfs_extent_inline_ref *iref,
1808 u64 parent, u64 root_objectid,
1809 u64 owner, u64 offset, int refs_to_add,
1810 struct btrfs_delayed_extent_op *extent_op)
1812 struct extent_buffer *leaf;
1813 struct btrfs_extent_item *ei;
1816 unsigned long item_offset;
1821 leaf = path->nodes[0];
1822 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1823 item_offset = (unsigned long)iref - (unsigned long)ei;
1825 type = extent_ref_type(parent, owner);
1826 size = btrfs_extent_inline_ref_size(type);
1828 btrfs_extend_item(fs_info, path, size);
1830 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1831 refs = btrfs_extent_refs(leaf, ei);
1832 refs += refs_to_add;
1833 btrfs_set_extent_refs(leaf, ei, refs);
1835 __run_delayed_extent_op(extent_op, leaf, ei);
1837 ptr = (unsigned long)ei + item_offset;
1838 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1839 if (ptr < end - size)
1840 memmove_extent_buffer(leaf, ptr + size, ptr,
1843 iref = (struct btrfs_extent_inline_ref *)ptr;
1844 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1845 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1846 struct btrfs_extent_data_ref *dref;
1847 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1848 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1849 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1850 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1851 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1852 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1853 struct btrfs_shared_data_ref *sref;
1854 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1855 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1856 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1857 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1858 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1860 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1862 btrfs_mark_buffer_dirty(leaf);
1865 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1866 struct btrfs_fs_info *fs_info,
1867 struct btrfs_path *path,
1868 struct btrfs_extent_inline_ref **ref_ret,
1869 u64 bytenr, u64 num_bytes, u64 parent,
1870 u64 root_objectid, u64 owner, u64 offset)
1874 ret = lookup_inline_extent_backref(trans, fs_info, path, ref_ret,
1875 bytenr, num_bytes, parent,
1876 root_objectid, owner, offset, 0);
1880 btrfs_release_path(path);
1883 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1884 ret = lookup_tree_block_ref(trans, fs_info, path, bytenr,
1885 parent, root_objectid);
1887 ret = lookup_extent_data_ref(trans, fs_info, path, bytenr,
1888 parent, root_objectid, owner,
1895 * helper to update/remove inline back ref
1897 static noinline_for_stack
1898 void update_inline_extent_backref(struct btrfs_fs_info *fs_info,
1899 struct btrfs_path *path,
1900 struct btrfs_extent_inline_ref *iref,
1902 struct btrfs_delayed_extent_op *extent_op,
1905 struct extent_buffer *leaf;
1906 struct btrfs_extent_item *ei;
1907 struct btrfs_extent_data_ref *dref = NULL;
1908 struct btrfs_shared_data_ref *sref = NULL;
1916 leaf = path->nodes[0];
1917 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1918 refs = btrfs_extent_refs(leaf, ei);
1919 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1920 refs += refs_to_mod;
1921 btrfs_set_extent_refs(leaf, ei, refs);
1923 __run_delayed_extent_op(extent_op, leaf, ei);
1926 * If type is invalid, we should have bailed out after
1927 * lookup_inline_extent_backref().
1929 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_ANY);
1930 ASSERT(type != BTRFS_REF_TYPE_INVALID);
1932 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1933 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1934 refs = btrfs_extent_data_ref_count(leaf, dref);
1935 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1936 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1937 refs = btrfs_shared_data_ref_count(leaf, sref);
1940 BUG_ON(refs_to_mod != -1);
1943 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1944 refs += refs_to_mod;
1947 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1948 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1950 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1953 size = btrfs_extent_inline_ref_size(type);
1954 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1955 ptr = (unsigned long)iref;
1956 end = (unsigned long)ei + item_size;
1957 if (ptr + size < end)
1958 memmove_extent_buffer(leaf, ptr, ptr + size,
1961 btrfs_truncate_item(fs_info, path, item_size, 1);
1963 btrfs_mark_buffer_dirty(leaf);
1966 static noinline_for_stack
1967 int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1968 struct btrfs_fs_info *fs_info,
1969 struct btrfs_path *path,
1970 u64 bytenr, u64 num_bytes, u64 parent,
1971 u64 root_objectid, u64 owner,
1972 u64 offset, int refs_to_add,
1973 struct btrfs_delayed_extent_op *extent_op)
1975 struct btrfs_extent_inline_ref *iref;
1978 ret = lookup_inline_extent_backref(trans, fs_info, path, &iref,
1979 bytenr, num_bytes, parent,
1980 root_objectid, owner, offset, 1);
1982 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1983 update_inline_extent_backref(fs_info, path, iref,
1984 refs_to_add, extent_op, NULL);
1985 } else if (ret == -ENOENT) {
1986 setup_inline_extent_backref(fs_info, path, iref, parent,
1987 root_objectid, owner, offset,
1988 refs_to_add, extent_op);
1994 static int insert_extent_backref(struct btrfs_trans_handle *trans,
1995 struct btrfs_fs_info *fs_info,
1996 struct btrfs_path *path,
1997 u64 bytenr, u64 parent, u64 root_objectid,
1998 u64 owner, u64 offset, int refs_to_add)
2001 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
2002 BUG_ON(refs_to_add != 1);
2003 ret = insert_tree_block_ref(trans, fs_info, path, bytenr,
2004 parent, root_objectid);
2006 ret = insert_extent_data_ref(trans, fs_info, path, bytenr,
2007 parent, root_objectid,
2008 owner, offset, refs_to_add);
2013 static int remove_extent_backref(struct btrfs_trans_handle *trans,
2014 struct btrfs_fs_info *fs_info,
2015 struct btrfs_path *path,
2016 struct btrfs_extent_inline_ref *iref,
2017 int refs_to_drop, int is_data, int *last_ref)
2021 BUG_ON(!is_data && refs_to_drop != 1);
2023 update_inline_extent_backref(fs_info, path, iref,
2024 -refs_to_drop, NULL, last_ref);
2025 } else if (is_data) {
2026 ret = remove_extent_data_ref(trans, fs_info, path, refs_to_drop,
2030 ret = btrfs_del_item(trans, fs_info->extent_root, path);
2035 #define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
2036 static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
2037 u64 *discarded_bytes)
2040 u64 bytes_left, end;
2041 u64 aligned_start = ALIGN(start, 1 << 9);
2043 if (WARN_ON(start != aligned_start)) {
2044 len -= aligned_start - start;
2045 len = round_down(len, 1 << 9);
2046 start = aligned_start;
2049 *discarded_bytes = 0;
2057 /* Skip any superblocks on this device. */
2058 for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) {
2059 u64 sb_start = btrfs_sb_offset(j);
2060 u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE;
2061 u64 size = sb_start - start;
2063 if (!in_range(sb_start, start, bytes_left) &&
2064 !in_range(sb_end, start, bytes_left) &&
2065 !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE))
2069 * Superblock spans beginning of range. Adjust start and
2072 if (sb_start <= start) {
2073 start += sb_end - start;
2078 bytes_left = end - start;
2083 ret = blkdev_issue_discard(bdev, start >> 9, size >> 9,
2086 *discarded_bytes += size;
2087 else if (ret != -EOPNOTSUPP)
2096 bytes_left = end - start;
2100 ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9,
2103 *discarded_bytes += bytes_left;
2108 int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
2109 u64 num_bytes, u64 *actual_bytes)
2112 u64 discarded_bytes = 0;
2113 struct btrfs_bio *bbio = NULL;
2117 * Avoid races with device replace and make sure our bbio has devices
2118 * associated to its stripes that don't go away while we are discarding.
2120 btrfs_bio_counter_inc_blocked(fs_info);
2121 /* Tell the block device(s) that the sectors can be discarded */
2122 ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, bytenr, &num_bytes,
2124 /* Error condition is -ENOMEM */
2126 struct btrfs_bio_stripe *stripe = bbio->stripes;
2130 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
2132 struct request_queue *req_q;
2134 if (!stripe->dev->bdev) {
2135 ASSERT(btrfs_test_opt(fs_info, DEGRADED));
2138 req_q = bdev_get_queue(stripe->dev->bdev);
2139 if (!blk_queue_discard(req_q))
2142 ret = btrfs_issue_discard(stripe->dev->bdev,
2147 discarded_bytes += bytes;
2148 else if (ret != -EOPNOTSUPP)
2149 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
2152 * Just in case we get back EOPNOTSUPP for some reason,
2153 * just ignore the return value so we don't screw up
2154 * people calling discard_extent.
2158 btrfs_put_bbio(bbio);
2160 btrfs_bio_counter_dec(fs_info);
2163 *actual_bytes = discarded_bytes;
2166 if (ret == -EOPNOTSUPP)
2171 /* Can return -ENOMEM */
2172 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
2173 struct btrfs_root *root,
2174 u64 bytenr, u64 num_bytes, u64 parent,
2175 u64 root_objectid, u64 owner, u64 offset)
2177 struct btrfs_fs_info *fs_info = root->fs_info;
2178 int old_ref_mod, new_ref_mod;
2181 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
2182 root_objectid == BTRFS_TREE_LOG_OBJECTID);
2184 btrfs_ref_tree_mod(root, bytenr, num_bytes, parent, root_objectid,
2185 owner, offset, BTRFS_ADD_DELAYED_REF);
2187 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
2188 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
2190 root_objectid, (int)owner,
2191 BTRFS_ADD_DELAYED_REF, NULL,
2192 &old_ref_mod, &new_ref_mod);
2194 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
2196 root_objectid, owner, offset,
2197 0, BTRFS_ADD_DELAYED_REF,
2198 &old_ref_mod, &new_ref_mod);
2201 if (ret == 0 && old_ref_mod < 0 && new_ref_mod >= 0) {
2202 bool metadata = owner < BTRFS_FIRST_FREE_OBJECTID;
2204 add_pinned_bytes(fs_info, -num_bytes, metadata, root_objectid);
2211 * __btrfs_inc_extent_ref - insert backreference for a given extent
2213 * @trans: Handle of transaction
2215 * @node: The delayed ref node used to get the bytenr/length for
2216 * extent whose references are incremented.
2218 * @parent: If this is a shared extent (BTRFS_SHARED_DATA_REF_KEY/
2219 * BTRFS_SHARED_BLOCK_REF_KEY) then it holds the logical
2220 * bytenr of the parent block. Since new extents are always
2221 * created with indirect references, this will only be the case
2222 * when relocating a shared extent. In that case, root_objectid
2223 * will be BTRFS_TREE_RELOC_OBJECTID. Otheriwse, parent must
2226 * @root_objectid: The id of the root where this modification has originated,
2227 * this can be either one of the well-known metadata trees or
2228 * the subvolume id which references this extent.
2230 * @owner: For data extents it is the inode number of the owning file.
2231 * For metadata extents this parameter holds the level in the
2232 * tree of the extent.
2234 * @offset: For metadata extents the offset is ignored and is currently
2235 * always passed as 0. For data extents it is the fileoffset
2236 * this extent belongs to.
2238 * @refs_to_add Number of references to add
2240 * @extent_op Pointer to a structure, holding information necessary when
2241 * updating a tree block's flags
2244 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
2245 struct btrfs_fs_info *fs_info,
2246 struct btrfs_delayed_ref_node *node,
2247 u64 parent, u64 root_objectid,
2248 u64 owner, u64 offset, int refs_to_add,
2249 struct btrfs_delayed_extent_op *extent_op)
2251 struct btrfs_path *path;
2252 struct extent_buffer *leaf;
2253 struct btrfs_extent_item *item;
2254 struct btrfs_key key;
2255 u64 bytenr = node->bytenr;
2256 u64 num_bytes = node->num_bytes;
2260 path = btrfs_alloc_path();
2264 path->reada = READA_FORWARD;
2265 path->leave_spinning = 1;
2266 /* this will setup the path even if it fails to insert the back ref */
2267 ret = insert_inline_extent_backref(trans, fs_info, path, bytenr,
2268 num_bytes, parent, root_objectid,
2270 refs_to_add, extent_op);
2271 if ((ret < 0 && ret != -EAGAIN) || !ret)
2275 * Ok we had -EAGAIN which means we didn't have space to insert and
2276 * inline extent ref, so just update the reference count and add a
2279 leaf = path->nodes[0];
2280 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2281 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2282 refs = btrfs_extent_refs(leaf, item);
2283 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
2285 __run_delayed_extent_op(extent_op, leaf, item);
2287 btrfs_mark_buffer_dirty(leaf);
2288 btrfs_release_path(path);
2290 path->reada = READA_FORWARD;
2291 path->leave_spinning = 1;
2292 /* now insert the actual backref */
2293 ret = insert_extent_backref(trans, fs_info, path, bytenr, parent,
2294 root_objectid, owner, offset, refs_to_add);
2296 btrfs_abort_transaction(trans, ret);
2298 btrfs_free_path(path);
2302 static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
2303 struct btrfs_fs_info *fs_info,
2304 struct btrfs_delayed_ref_node *node,
2305 struct btrfs_delayed_extent_op *extent_op,
2306 int insert_reserved)
2309 struct btrfs_delayed_data_ref *ref;
2310 struct btrfs_key ins;
2315 ins.objectid = node->bytenr;
2316 ins.offset = node->num_bytes;
2317 ins.type = BTRFS_EXTENT_ITEM_KEY;
2319 ref = btrfs_delayed_node_to_data_ref(node);
2320 trace_run_delayed_data_ref(fs_info, node, ref, node->action);
2322 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2323 parent = ref->parent;
2324 ref_root = ref->root;
2326 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2328 flags |= extent_op->flags_to_set;
2329 ret = alloc_reserved_file_extent(trans, fs_info,
2330 parent, ref_root, flags,
2331 ref->objectid, ref->offset,
2332 &ins, node->ref_mod);
2333 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2334 ret = __btrfs_inc_extent_ref(trans, fs_info, node, parent,
2335 ref_root, ref->objectid,
2336 ref->offset, node->ref_mod,
2338 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2339 ret = __btrfs_free_extent(trans, fs_info, node, parent,
2340 ref_root, ref->objectid,
2341 ref->offset, node->ref_mod,
2349 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2350 struct extent_buffer *leaf,
2351 struct btrfs_extent_item *ei)
2353 u64 flags = btrfs_extent_flags(leaf, ei);
2354 if (extent_op->update_flags) {
2355 flags |= extent_op->flags_to_set;
2356 btrfs_set_extent_flags(leaf, ei, flags);
2359 if (extent_op->update_key) {
2360 struct btrfs_tree_block_info *bi;
2361 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2362 bi = (struct btrfs_tree_block_info *)(ei + 1);
2363 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2367 static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2368 struct btrfs_fs_info *fs_info,
2369 struct btrfs_delayed_ref_head *head,
2370 struct btrfs_delayed_extent_op *extent_op)
2372 struct btrfs_key key;
2373 struct btrfs_path *path;
2374 struct btrfs_extent_item *ei;
2375 struct extent_buffer *leaf;
2379 int metadata = !extent_op->is_data;
2384 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2387 path = btrfs_alloc_path();
2391 key.objectid = head->bytenr;
2394 key.type = BTRFS_METADATA_ITEM_KEY;
2395 key.offset = extent_op->level;
2397 key.type = BTRFS_EXTENT_ITEM_KEY;
2398 key.offset = head->num_bytes;
2402 path->reada = READA_FORWARD;
2403 path->leave_spinning = 1;
2404 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 1);
2411 if (path->slots[0] > 0) {
2413 btrfs_item_key_to_cpu(path->nodes[0], &key,
2415 if (key.objectid == head->bytenr &&
2416 key.type == BTRFS_EXTENT_ITEM_KEY &&
2417 key.offset == head->num_bytes)
2421 btrfs_release_path(path);
2424 key.objectid = head->bytenr;
2425 key.offset = head->num_bytes;
2426 key.type = BTRFS_EXTENT_ITEM_KEY;
2435 leaf = path->nodes[0];
2436 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2437 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2438 if (item_size < sizeof(*ei)) {
2439 ret = convert_extent_item_v0(trans, fs_info, path, (u64)-1, 0);
2444 leaf = path->nodes[0];
2445 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2448 BUG_ON(item_size < sizeof(*ei));
2449 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2450 __run_delayed_extent_op(extent_op, leaf, ei);
2452 btrfs_mark_buffer_dirty(leaf);
2454 btrfs_free_path(path);
2458 static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2459 struct btrfs_fs_info *fs_info,
2460 struct btrfs_delayed_ref_node *node,
2461 struct btrfs_delayed_extent_op *extent_op,
2462 int insert_reserved)
2465 struct btrfs_delayed_tree_ref *ref;
2469 ref = btrfs_delayed_node_to_tree_ref(node);
2470 trace_run_delayed_tree_ref(fs_info, node, ref, node->action);
2472 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2473 parent = ref->parent;
2474 ref_root = ref->root;
2476 if (node->ref_mod != 1) {
2478 "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu",
2479 node->bytenr, node->ref_mod, node->action, ref_root,
2483 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2484 BUG_ON(!extent_op || !extent_op->update_flags);
2485 ret = alloc_reserved_tree_block(trans, node, extent_op);
2486 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2487 ret = __btrfs_inc_extent_ref(trans, fs_info, node,
2491 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2492 ret = __btrfs_free_extent(trans, fs_info, node,
2494 ref->level, 0, 1, extent_op);
2501 /* helper function to actually process a single delayed ref entry */
2502 static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2503 struct btrfs_fs_info *fs_info,
2504 struct btrfs_delayed_ref_node *node,
2505 struct btrfs_delayed_extent_op *extent_op,
2506 int insert_reserved)
2510 if (trans->aborted) {
2511 if (insert_reserved)
2512 btrfs_pin_extent(fs_info, node->bytenr,
2513 node->num_bytes, 1);
2517 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2518 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2519 ret = run_delayed_tree_ref(trans, fs_info, node, extent_op,
2521 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2522 node->type == BTRFS_SHARED_DATA_REF_KEY)
2523 ret = run_delayed_data_ref(trans, fs_info, node, extent_op,
2530 static inline struct btrfs_delayed_ref_node *
2531 select_delayed_ref(struct btrfs_delayed_ref_head *head)
2533 struct btrfs_delayed_ref_node *ref;
2535 if (RB_EMPTY_ROOT(&head->ref_tree))
2539 * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
2540 * This is to prevent a ref count from going down to zero, which deletes
2541 * the extent item from the extent tree, when there still are references
2542 * to add, which would fail because they would not find the extent item.
2544 if (!list_empty(&head->ref_add_list))
2545 return list_first_entry(&head->ref_add_list,
2546 struct btrfs_delayed_ref_node, add_list);
2548 ref = rb_entry(rb_first(&head->ref_tree),
2549 struct btrfs_delayed_ref_node, ref_node);
2550 ASSERT(list_empty(&ref->add_list));
2554 static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
2555 struct btrfs_delayed_ref_head *head)
2557 spin_lock(&delayed_refs->lock);
2558 head->processing = 0;
2559 delayed_refs->num_heads_ready++;
2560 spin_unlock(&delayed_refs->lock);
2561 btrfs_delayed_ref_unlock(head);
2564 static int cleanup_extent_op(struct btrfs_trans_handle *trans,
2565 struct btrfs_fs_info *fs_info,
2566 struct btrfs_delayed_ref_head *head)
2568 struct btrfs_delayed_extent_op *extent_op = head->extent_op;
2573 head->extent_op = NULL;
2574 if (head->must_insert_reserved) {
2575 btrfs_free_delayed_extent_op(extent_op);
2578 spin_unlock(&head->lock);
2579 ret = run_delayed_extent_op(trans, fs_info, head, extent_op);
2580 btrfs_free_delayed_extent_op(extent_op);
2581 return ret ? ret : 1;
2584 static int cleanup_ref_head(struct btrfs_trans_handle *trans,
2585 struct btrfs_fs_info *fs_info,
2586 struct btrfs_delayed_ref_head *head)
2588 struct btrfs_delayed_ref_root *delayed_refs;
2591 delayed_refs = &trans->transaction->delayed_refs;
2593 ret = cleanup_extent_op(trans, fs_info, head);
2595 unselect_delayed_ref_head(delayed_refs, head);
2596 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
2603 * Need to drop our head ref lock and re-acquire the delayed ref lock
2604 * and then re-check to make sure nobody got added.
2606 spin_unlock(&head->lock);
2607 spin_lock(&delayed_refs->lock);
2608 spin_lock(&head->lock);
2609 if (!RB_EMPTY_ROOT(&head->ref_tree) || head->extent_op) {
2610 spin_unlock(&head->lock);
2611 spin_unlock(&delayed_refs->lock);
2614 delayed_refs->num_heads--;
2615 rb_erase(&head->href_node, &delayed_refs->href_root);
2616 RB_CLEAR_NODE(&head->href_node);
2617 spin_unlock(&head->lock);
2618 spin_unlock(&delayed_refs->lock);
2619 atomic_dec(&delayed_refs->num_entries);
2621 trace_run_delayed_ref_head(fs_info, head, 0);
2623 if (head->total_ref_mod < 0) {
2624 struct btrfs_space_info *space_info;
2628 flags = BTRFS_BLOCK_GROUP_DATA;
2629 else if (head->is_system)
2630 flags = BTRFS_BLOCK_GROUP_SYSTEM;
2632 flags = BTRFS_BLOCK_GROUP_METADATA;
2633 space_info = __find_space_info(fs_info, flags);
2635 percpu_counter_add(&space_info->total_bytes_pinned,
2638 if (head->is_data) {
2639 spin_lock(&delayed_refs->lock);
2640 delayed_refs->pending_csums -= head->num_bytes;
2641 spin_unlock(&delayed_refs->lock);
2645 if (head->must_insert_reserved) {
2646 btrfs_pin_extent(fs_info, head->bytenr,
2647 head->num_bytes, 1);
2648 if (head->is_data) {
2649 ret = btrfs_del_csums(trans, fs_info, head->bytenr,
2654 /* Also free its reserved qgroup space */
2655 btrfs_qgroup_free_delayed_ref(fs_info, head->qgroup_ref_root,
2656 head->qgroup_reserved);
2657 btrfs_delayed_ref_unlock(head);
2658 btrfs_put_delayed_ref_head(head);
2663 * Returns 0 on success or if called with an already aborted transaction.
2664 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2666 static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2669 struct btrfs_fs_info *fs_info = trans->fs_info;
2670 struct btrfs_delayed_ref_root *delayed_refs;
2671 struct btrfs_delayed_ref_node *ref;
2672 struct btrfs_delayed_ref_head *locked_ref = NULL;
2673 struct btrfs_delayed_extent_op *extent_op;
2674 ktime_t start = ktime_get();
2676 unsigned long count = 0;
2677 unsigned long actual_count = 0;
2678 int must_insert_reserved = 0;
2680 delayed_refs = &trans->transaction->delayed_refs;
2686 spin_lock(&delayed_refs->lock);
2687 locked_ref = btrfs_select_ref_head(trans);
2689 spin_unlock(&delayed_refs->lock);
2693 /* grab the lock that says we are going to process
2694 * all the refs for this head */
2695 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2696 spin_unlock(&delayed_refs->lock);
2698 * we may have dropped the spin lock to get the head
2699 * mutex lock, and that might have given someone else
2700 * time to free the head. If that's true, it has been
2701 * removed from our list and we can move on.
2703 if (ret == -EAGAIN) {
2711 * We need to try and merge add/drops of the same ref since we
2712 * can run into issues with relocate dropping the implicit ref
2713 * and then it being added back again before the drop can
2714 * finish. If we merged anything we need to re-loop so we can
2716 * Or we can get node references of the same type that weren't
2717 * merged when created due to bumps in the tree mod seq, and
2718 * we need to merge them to prevent adding an inline extent
2719 * backref before dropping it (triggering a BUG_ON at
2720 * insert_inline_extent_backref()).
2722 spin_lock(&locked_ref->lock);
2723 btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref);
2725 ref = select_delayed_ref(locked_ref);
2727 if (ref && ref->seq &&
2728 btrfs_check_delayed_seq(fs_info, ref->seq)) {
2729 spin_unlock(&locked_ref->lock);
2730 unselect_delayed_ref_head(delayed_refs, locked_ref);
2738 * We're done processing refs in this ref_head, clean everything
2739 * up and move on to the next ref_head.
2742 ret = cleanup_ref_head(trans, fs_info, locked_ref);
2744 /* We dropped our lock, we need to loop. */
2757 rb_erase(&ref->ref_node, &locked_ref->ref_tree);
2758 RB_CLEAR_NODE(&ref->ref_node);
2759 if (!list_empty(&ref->add_list))
2760 list_del(&ref->add_list);
2762 * When we play the delayed ref, also correct the ref_mod on
2765 switch (ref->action) {
2766 case BTRFS_ADD_DELAYED_REF:
2767 case BTRFS_ADD_DELAYED_EXTENT:
2768 locked_ref->ref_mod -= ref->ref_mod;
2770 case BTRFS_DROP_DELAYED_REF:
2771 locked_ref->ref_mod += ref->ref_mod;
2776 atomic_dec(&delayed_refs->num_entries);
2779 * Record the must-insert_reserved flag before we drop the spin
2782 must_insert_reserved = locked_ref->must_insert_reserved;
2783 locked_ref->must_insert_reserved = 0;
2785 extent_op = locked_ref->extent_op;
2786 locked_ref->extent_op = NULL;
2787 spin_unlock(&locked_ref->lock);
2789 ret = run_one_delayed_ref(trans, fs_info, ref, extent_op,
2790 must_insert_reserved);
2792 btrfs_free_delayed_extent_op(extent_op);
2794 unselect_delayed_ref_head(delayed_refs, locked_ref);
2795 btrfs_put_delayed_ref(ref);
2796 btrfs_debug(fs_info, "run_one_delayed_ref returned %d",
2801 btrfs_put_delayed_ref(ref);
2807 * We don't want to include ref heads since we can have empty ref heads
2808 * and those will drastically skew our runtime down since we just do
2809 * accounting, no actual extent tree updates.
2811 if (actual_count > 0) {
2812 u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
2816 * We weigh the current average higher than our current runtime
2817 * to avoid large swings in the average.
2819 spin_lock(&delayed_refs->lock);
2820 avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
2821 fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */
2822 spin_unlock(&delayed_refs->lock);
2827 #ifdef SCRAMBLE_DELAYED_REFS
2829 * Normally delayed refs get processed in ascending bytenr order. This
2830 * correlates in most cases to the order added. To expose dependencies on this
2831 * order, we start to process the tree in the middle instead of the beginning
2833 static u64 find_middle(struct rb_root *root)
2835 struct rb_node *n = root->rb_node;
2836 struct btrfs_delayed_ref_node *entry;
2839 u64 first = 0, last = 0;
2843 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2844 first = entry->bytenr;
2848 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2849 last = entry->bytenr;
2854 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2855 WARN_ON(!entry->in_tree);
2857 middle = entry->bytenr;
2870 static inline u64 heads_to_leaves(struct btrfs_fs_info *fs_info, u64 heads)
2874 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2875 sizeof(struct btrfs_extent_inline_ref));
2876 if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2877 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2880 * We don't ever fill up leaves all the way so multiply by 2 just to be
2881 * closer to what we're really going to want to use.
2883 return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info));
2887 * Takes the number of bytes to be csumm'ed and figures out how many leaves it
2888 * would require to store the csums for that many bytes.
2890 u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes)
2893 u64 num_csums_per_leaf;
2896 csum_size = BTRFS_MAX_ITEM_SIZE(fs_info);
2897 num_csums_per_leaf = div64_u64(csum_size,
2898 (u64)btrfs_super_csum_size(fs_info->super_copy));
2899 num_csums = div64_u64(csum_bytes, fs_info->sectorsize);
2900 num_csums += num_csums_per_leaf - 1;
2901 num_csums = div64_u64(num_csums, num_csums_per_leaf);
2905 int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
2906 struct btrfs_fs_info *fs_info)
2908 struct btrfs_block_rsv *global_rsv;
2909 u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
2910 u64 csum_bytes = trans->transaction->delayed_refs.pending_csums;
2911 unsigned int num_dirty_bgs = trans->transaction->num_dirty_bgs;
2912 u64 num_bytes, num_dirty_bgs_bytes;
2915 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
2916 num_heads = heads_to_leaves(fs_info, num_heads);
2918 num_bytes += (num_heads - 1) * fs_info->nodesize;
2920 num_bytes += btrfs_csum_bytes_to_leaves(fs_info, csum_bytes) *
2922 num_dirty_bgs_bytes = btrfs_calc_trans_metadata_size(fs_info,
2924 global_rsv = &fs_info->global_block_rsv;
2927 * If we can't allocate any more chunks lets make sure we have _lots_ of
2928 * wiggle room since running delayed refs can create more delayed refs.
2930 if (global_rsv->space_info->full) {
2931 num_dirty_bgs_bytes <<= 1;
2935 spin_lock(&global_rsv->lock);
2936 if (global_rsv->reserved <= num_bytes + num_dirty_bgs_bytes)
2938 spin_unlock(&global_rsv->lock);
2942 int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
2943 struct btrfs_fs_info *fs_info)
2946 atomic_read(&trans->transaction->delayed_refs.num_entries);
2951 avg_runtime = fs_info->avg_delayed_ref_runtime;
2952 val = num_entries * avg_runtime;
2953 if (val >= NSEC_PER_SEC)
2955 if (val >= NSEC_PER_SEC / 2)
2958 return btrfs_check_space_for_delayed_refs(trans, fs_info);
2961 struct async_delayed_refs {
2962 struct btrfs_root *root;
2967 struct completion wait;
2968 struct btrfs_work work;
2971 static inline struct async_delayed_refs *
2972 to_async_delayed_refs(struct btrfs_work *work)
2974 return container_of(work, struct async_delayed_refs, work);
2977 static void delayed_ref_async_start(struct btrfs_work *work)
2979 struct async_delayed_refs *async = to_async_delayed_refs(work);
2980 struct btrfs_trans_handle *trans;
2981 struct btrfs_fs_info *fs_info = async->root->fs_info;
2984 /* if the commit is already started, we don't need to wait here */
2985 if (btrfs_transaction_blocked(fs_info))
2988 trans = btrfs_join_transaction(async->root);
2989 if (IS_ERR(trans)) {
2990 async->error = PTR_ERR(trans);
2995 * trans->sync means that when we call end_transaction, we won't
2996 * wait on delayed refs
3000 /* Don't bother flushing if we got into a different transaction */
3001 if (trans->transid > async->transid)
3004 ret = btrfs_run_delayed_refs(trans, async->count);
3008 ret = btrfs_end_transaction(trans);
3009 if (ret && !async->error)
3013 complete(&async->wait);
3018 int btrfs_async_run_delayed_refs(struct btrfs_fs_info *fs_info,
3019 unsigned long count, u64 transid, int wait)
3021 struct async_delayed_refs *async;
3024 async = kmalloc(sizeof(*async), GFP_NOFS);
3028 async->root = fs_info->tree_root;
3029 async->count = count;
3031 async->transid = transid;
3036 init_completion(&async->wait);
3038 btrfs_init_work(&async->work, btrfs_extent_refs_helper,
3039 delayed_ref_async_start, NULL, NULL);
3041 btrfs_queue_work(fs_info->extent_workers, &async->work);
3044 wait_for_completion(&async->wait);
3053 * this starts processing the delayed reference count updates and
3054 * extent insertions we have queued up so far. count can be
3055 * 0, which means to process everything in the tree at the start
3056 * of the run (but not newly added entries), or it can be some target
3057 * number you'd like to process.
3059 * Returns 0 on success or if called with an aborted transaction
3060 * Returns <0 on error and aborts the transaction
3062 int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
3063 unsigned long count)
3065 struct btrfs_fs_info *fs_info = trans->fs_info;
3066 struct rb_node *node;
3067 struct btrfs_delayed_ref_root *delayed_refs;
3068 struct btrfs_delayed_ref_head *head;
3070 int run_all = count == (unsigned long)-1;
3071 bool can_flush_pending_bgs = trans->can_flush_pending_bgs;
3073 /* We'll clean this up in btrfs_cleanup_transaction */
3077 if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags))
3080 delayed_refs = &trans->transaction->delayed_refs;
3082 count = atomic_read(&delayed_refs->num_entries) * 2;
3085 #ifdef SCRAMBLE_DELAYED_REFS
3086 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
3088 trans->can_flush_pending_bgs = false;
3089 ret = __btrfs_run_delayed_refs(trans, count);
3091 btrfs_abort_transaction(trans, ret);
3096 if (!list_empty(&trans->new_bgs))
3097 btrfs_create_pending_block_groups(trans);
3099 spin_lock(&delayed_refs->lock);
3100 node = rb_first(&delayed_refs->href_root);
3102 spin_unlock(&delayed_refs->lock);
3105 head = rb_entry(node, struct btrfs_delayed_ref_head,
3107 refcount_inc(&head->refs);
3108 spin_unlock(&delayed_refs->lock);
3110 /* Mutex was contended, block until it's released and retry. */
3111 mutex_lock(&head->mutex);
3112 mutex_unlock(&head->mutex);
3114 btrfs_put_delayed_ref_head(head);
3119 trans->can_flush_pending_bgs = can_flush_pending_bgs;
3123 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
3124 struct btrfs_fs_info *fs_info,
3125 u64 bytenr, u64 num_bytes, u64 flags,
3126 int level, int is_data)
3128 struct btrfs_delayed_extent_op *extent_op;
3131 extent_op = btrfs_alloc_delayed_extent_op();
3135 extent_op->flags_to_set = flags;
3136 extent_op->update_flags = true;
3137 extent_op->update_key = false;
3138 extent_op->is_data = is_data ? true : false;
3139 extent_op->level = level;
3141 ret = btrfs_add_delayed_extent_op(fs_info, trans, bytenr,
3142 num_bytes, extent_op);
3144 btrfs_free_delayed_extent_op(extent_op);
3148 static noinline int check_delayed_ref(struct btrfs_root *root,
3149 struct btrfs_path *path,
3150 u64 objectid, u64 offset, u64 bytenr)
3152 struct btrfs_delayed_ref_head *head;
3153 struct btrfs_delayed_ref_node *ref;
3154 struct btrfs_delayed_data_ref *data_ref;
3155 struct btrfs_delayed_ref_root *delayed_refs;
3156 struct btrfs_transaction *cur_trans;
3157 struct rb_node *node;
3160 spin_lock(&root->fs_info->trans_lock);
3161 cur_trans = root->fs_info->running_transaction;
3163 refcount_inc(&cur_trans->use_count);
3164 spin_unlock(&root->fs_info->trans_lock);
3168 delayed_refs = &cur_trans->delayed_refs;
3169 spin_lock(&delayed_refs->lock);
3170 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
3172 spin_unlock(&delayed_refs->lock);
3173 btrfs_put_transaction(cur_trans);
3177 if (!mutex_trylock(&head->mutex)) {
3178 refcount_inc(&head->refs);
3179 spin_unlock(&delayed_refs->lock);
3181 btrfs_release_path(path);
3184 * Mutex was contended, block until it's released and let
3187 mutex_lock(&head->mutex);
3188 mutex_unlock(&head->mutex);
3189 btrfs_put_delayed_ref_head(head);
3190 btrfs_put_transaction(cur_trans);
3193 spin_unlock(&delayed_refs->lock);
3195 spin_lock(&head->lock);
3197 * XXX: We should replace this with a proper search function in the
3200 for (node = rb_first(&head->ref_tree); node; node = rb_next(node)) {
3201 ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
3202 /* If it's a shared ref we know a cross reference exists */
3203 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
3208 data_ref = btrfs_delayed_node_to_data_ref(ref);
3211 * If our ref doesn't match the one we're currently looking at
3212 * then we have a cross reference.
3214 if (data_ref->root != root->root_key.objectid ||
3215 data_ref->objectid != objectid ||
3216 data_ref->offset != offset) {
3221 spin_unlock(&head->lock);
3222 mutex_unlock(&head->mutex);
3223 btrfs_put_transaction(cur_trans);
3227 static noinline int check_committed_ref(struct btrfs_root *root,
3228 struct btrfs_path *path,
3229 u64 objectid, u64 offset, u64 bytenr)
3231 struct btrfs_fs_info *fs_info = root->fs_info;
3232 struct btrfs_root *extent_root = fs_info->extent_root;
3233 struct extent_buffer *leaf;
3234 struct btrfs_extent_data_ref *ref;
3235 struct btrfs_extent_inline_ref *iref;
3236 struct btrfs_extent_item *ei;
3237 struct btrfs_key key;
3242 key.objectid = bytenr;
3243 key.offset = (u64)-1;
3244 key.type = BTRFS_EXTENT_ITEM_KEY;
3246 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
3249 BUG_ON(ret == 0); /* Corruption */
3252 if (path->slots[0] == 0)
3256 leaf = path->nodes[0];
3257 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3259 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
3263 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3264 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3265 if (item_size < sizeof(*ei)) {
3266 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
3270 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
3272 if (item_size != sizeof(*ei) +
3273 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
3276 if (btrfs_extent_generation(leaf, ei) <=
3277 btrfs_root_last_snapshot(&root->root_item))
3280 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
3282 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
3283 if (type != BTRFS_EXTENT_DATA_REF_KEY)
3286 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
3287 if (btrfs_extent_refs(leaf, ei) !=
3288 btrfs_extent_data_ref_count(leaf, ref) ||
3289 btrfs_extent_data_ref_root(leaf, ref) !=
3290 root->root_key.objectid ||
3291 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
3292 btrfs_extent_data_ref_offset(leaf, ref) != offset)
3300 int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset,
3303 struct btrfs_path *path;
3307 path = btrfs_alloc_path();
3312 ret = check_committed_ref(root, path, objectid,
3314 if (ret && ret != -ENOENT)
3317 ret2 = check_delayed_ref(root, path, objectid,
3319 } while (ret2 == -EAGAIN);
3321 if (ret2 && ret2 != -ENOENT) {
3326 if (ret != -ENOENT || ret2 != -ENOENT)
3329 btrfs_free_path(path);
3330 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
3335 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
3336 struct btrfs_root *root,
3337 struct extent_buffer *buf,
3338 int full_backref, int inc)
3340 struct btrfs_fs_info *fs_info = root->fs_info;
3346 struct btrfs_key key;
3347 struct btrfs_file_extent_item *fi;
3351 int (*process_func)(struct btrfs_trans_handle *,
3352 struct btrfs_root *,
3353 u64, u64, u64, u64, u64, u64);
3356 if (btrfs_is_testing(fs_info))
3359 ref_root = btrfs_header_owner(buf);
3360 nritems = btrfs_header_nritems(buf);
3361 level = btrfs_header_level(buf);
3363 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
3367 process_func = btrfs_inc_extent_ref;
3369 process_func = btrfs_free_extent;
3372 parent = buf->start;
3376 for (i = 0; i < nritems; i++) {
3378 btrfs_item_key_to_cpu(buf, &key, i);
3379 if (key.type != BTRFS_EXTENT_DATA_KEY)
3381 fi = btrfs_item_ptr(buf, i,
3382 struct btrfs_file_extent_item);
3383 if (btrfs_file_extent_type(buf, fi) ==
3384 BTRFS_FILE_EXTENT_INLINE)
3386 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
3390 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
3391 key.offset -= btrfs_file_extent_offset(buf, fi);
3392 ret = process_func(trans, root, bytenr, num_bytes,
3393 parent, ref_root, key.objectid,
3398 bytenr = btrfs_node_blockptr(buf, i);
3399 num_bytes = fs_info->nodesize;
3400 ret = process_func(trans, root, bytenr, num_bytes,
3401 parent, ref_root, level - 1, 0);
3411 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3412 struct extent_buffer *buf, int full_backref)
3414 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
3417 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3418 struct extent_buffer *buf, int full_backref)
3420 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
3423 static int write_one_cache_group(struct btrfs_trans_handle *trans,
3424 struct btrfs_fs_info *fs_info,
3425 struct btrfs_path *path,
3426 struct btrfs_block_group_cache *cache)
3429 struct btrfs_root *extent_root = fs_info->extent_root;
3431 struct extent_buffer *leaf;
3433 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
3440 leaf = path->nodes[0];
3441 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
3442 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
3443 btrfs_mark_buffer_dirty(leaf);
3445 btrfs_release_path(path);
3450 static struct btrfs_block_group_cache *
3451 next_block_group(struct btrfs_fs_info *fs_info,
3452 struct btrfs_block_group_cache *cache)
3454 struct rb_node *node;
3456 spin_lock(&fs_info->block_group_cache_lock);
3458 /* If our block group was removed, we need a full search. */
3459 if (RB_EMPTY_NODE(&cache->cache_node)) {
3460 const u64 next_bytenr = cache->key.objectid + cache->key.offset;
3462 spin_unlock(&fs_info->block_group_cache_lock);
3463 btrfs_put_block_group(cache);
3464 cache = btrfs_lookup_first_block_group(fs_info, next_bytenr); return cache;
3466 node = rb_next(&cache->cache_node);
3467 btrfs_put_block_group(cache);
3469 cache = rb_entry(node, struct btrfs_block_group_cache,
3471 btrfs_get_block_group(cache);
3474 spin_unlock(&fs_info->block_group_cache_lock);
3478 static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3479 struct btrfs_trans_handle *trans,
3480 struct btrfs_path *path)
3482 struct btrfs_fs_info *fs_info = block_group->fs_info;
3483 struct btrfs_root *root = fs_info->tree_root;
3484 struct inode *inode = NULL;
3485 struct extent_changeset *data_reserved = NULL;
3487 int dcs = BTRFS_DC_ERROR;
3493 * If this block group is smaller than 100 megs don't bother caching the
3496 if (block_group->key.offset < (100 * SZ_1M)) {
3497 spin_lock(&block_group->lock);
3498 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
3499 spin_unlock(&block_group->lock);
3506 inode = lookup_free_space_inode(fs_info, block_group, path);
3507 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3508 ret = PTR_ERR(inode);
3509 btrfs_release_path(path);
3513 if (IS_ERR(inode)) {
3517 if (block_group->ro)
3520 ret = create_free_space_inode(fs_info, trans, block_group,
3528 * We want to set the generation to 0, that way if anything goes wrong
3529 * from here on out we know not to trust this cache when we load up next
3532 BTRFS_I(inode)->generation = 0;
3533 ret = btrfs_update_inode(trans, root, inode);
3536 * So theoretically we could recover from this, simply set the
3537 * super cache generation to 0 so we know to invalidate the
3538 * cache, but then we'd have to keep track of the block groups
3539 * that fail this way so we know we _have_ to reset this cache
3540 * before the next commit or risk reading stale cache. So to