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_delayed_ref_node *node, u64 parent,
56 u64 root_objectid, u64 owner_objectid,
57 u64 owner_offset, int refs_to_drop,
58 struct btrfs_delayed_extent_op *extra_op);
59 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
60 struct extent_buffer *leaf,
61 struct btrfs_extent_item *ei);
62 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
63 u64 parent, u64 root_objectid,
64 u64 flags, u64 owner, u64 offset,
65 struct btrfs_key *ins, int ref_mod);
66 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
67 struct btrfs_delayed_ref_node *node,
68 struct btrfs_delayed_extent_op *extent_op);
69 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
70 struct btrfs_fs_info *fs_info, u64 flags,
72 static int find_next_key(struct btrfs_path *path, int level,
73 struct btrfs_key *key);
74 static void dump_space_info(struct btrfs_fs_info *fs_info,
75 struct btrfs_space_info *info, u64 bytes,
76 int dump_block_groups);
77 static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
79 static void space_info_add_new_bytes(struct btrfs_fs_info *fs_info,
80 struct btrfs_space_info *space_info,
82 static void space_info_add_old_bytes(struct btrfs_fs_info *fs_info,
83 struct btrfs_space_info *space_info,
87 block_group_cache_done(struct btrfs_block_group_cache *cache)
90 return cache->cached == BTRFS_CACHE_FINISHED ||
91 cache->cached == BTRFS_CACHE_ERROR;
94 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
96 return (cache->flags & bits) == bits;
99 void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
101 atomic_inc(&cache->count);
104 void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
106 if (atomic_dec_and_test(&cache->count)) {
107 WARN_ON(cache->pinned > 0);
108 WARN_ON(cache->reserved > 0);
111 * If not empty, someone is still holding mutex of
112 * full_stripe_lock, which can only be released by caller.
113 * And it will definitely cause use-after-free when caller
114 * tries to release full stripe lock.
116 * No better way to resolve, but only to warn.
118 WARN_ON(!RB_EMPTY_ROOT(&cache->full_stripe_locks_root.root));
119 kfree(cache->free_space_ctl);
125 * this adds the block group to the fs_info rb tree for the block group
128 static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
129 struct btrfs_block_group_cache *block_group)
132 struct rb_node *parent = NULL;
133 struct btrfs_block_group_cache *cache;
135 spin_lock(&info->block_group_cache_lock);
136 p = &info->block_group_cache_tree.rb_node;
140 cache = rb_entry(parent, struct btrfs_block_group_cache,
142 if (block_group->key.objectid < cache->key.objectid) {
144 } else if (block_group->key.objectid > cache->key.objectid) {
147 spin_unlock(&info->block_group_cache_lock);
152 rb_link_node(&block_group->cache_node, parent, p);
153 rb_insert_color(&block_group->cache_node,
154 &info->block_group_cache_tree);
156 if (info->first_logical_byte > block_group->key.objectid)
157 info->first_logical_byte = block_group->key.objectid;
159 spin_unlock(&info->block_group_cache_lock);
165 * This will return the block group at or after bytenr if contains is 0, else
166 * it will return the block group that contains the bytenr
168 static struct btrfs_block_group_cache *
169 block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
172 struct btrfs_block_group_cache *cache, *ret = NULL;
176 spin_lock(&info->block_group_cache_lock);
177 n = info->block_group_cache_tree.rb_node;
180 cache = rb_entry(n, struct btrfs_block_group_cache,
182 end = cache->key.objectid + cache->key.offset - 1;
183 start = cache->key.objectid;
185 if (bytenr < start) {
186 if (!contains && (!ret || start < ret->key.objectid))
189 } else if (bytenr > start) {
190 if (contains && bytenr <= end) {
201 btrfs_get_block_group(ret);
202 if (bytenr == 0 && info->first_logical_byte > ret->key.objectid)
203 info->first_logical_byte = ret->key.objectid;
205 spin_unlock(&info->block_group_cache_lock);
210 static int add_excluded_extent(struct btrfs_fs_info *fs_info,
211 u64 start, u64 num_bytes)
213 u64 end = start + num_bytes - 1;
214 set_extent_bits(&fs_info->freed_extents[0],
215 start, end, EXTENT_UPTODATE);
216 set_extent_bits(&fs_info->freed_extents[1],
217 start, end, EXTENT_UPTODATE);
221 static void free_excluded_extents(struct btrfs_fs_info *fs_info,
222 struct btrfs_block_group_cache *cache)
226 start = cache->key.objectid;
227 end = start + cache->key.offset - 1;
229 clear_extent_bits(&fs_info->freed_extents[0],
230 start, end, EXTENT_UPTODATE);
231 clear_extent_bits(&fs_info->freed_extents[1],
232 start, end, EXTENT_UPTODATE);
235 static int exclude_super_stripes(struct btrfs_fs_info *fs_info,
236 struct btrfs_block_group_cache *cache)
243 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
244 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
245 cache->bytes_super += stripe_len;
246 ret = add_excluded_extent(fs_info, cache->key.objectid,
252 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
253 bytenr = btrfs_sb_offset(i);
254 ret = btrfs_rmap_block(fs_info, cache->key.objectid,
255 bytenr, &logical, &nr, &stripe_len);
262 if (logical[nr] > cache->key.objectid +
266 if (logical[nr] + stripe_len <= cache->key.objectid)
270 if (start < cache->key.objectid) {
271 start = cache->key.objectid;
272 len = (logical[nr] + stripe_len) - start;
274 len = min_t(u64, stripe_len,
275 cache->key.objectid +
276 cache->key.offset - start);
279 cache->bytes_super += len;
280 ret = add_excluded_extent(fs_info, start, len);
292 static struct btrfs_caching_control *
293 get_caching_control(struct btrfs_block_group_cache *cache)
295 struct btrfs_caching_control *ctl;
297 spin_lock(&cache->lock);
298 if (!cache->caching_ctl) {
299 spin_unlock(&cache->lock);
303 ctl = cache->caching_ctl;
304 refcount_inc(&ctl->count);
305 spin_unlock(&cache->lock);
309 static void put_caching_control(struct btrfs_caching_control *ctl)
311 if (refcount_dec_and_test(&ctl->count))
315 #ifdef CONFIG_BTRFS_DEBUG
316 static void fragment_free_space(struct btrfs_block_group_cache *block_group)
318 struct btrfs_fs_info *fs_info = block_group->fs_info;
319 u64 start = block_group->key.objectid;
320 u64 len = block_group->key.offset;
321 u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ?
322 fs_info->nodesize : fs_info->sectorsize;
323 u64 step = chunk << 1;
325 while (len > chunk) {
326 btrfs_remove_free_space(block_group, start, chunk);
337 * this is only called by cache_block_group, since we could have freed extents
338 * we need to check the pinned_extents for any extents that can't be used yet
339 * since their free space will be released as soon as the transaction commits.
341 u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
344 struct btrfs_fs_info *info = block_group->fs_info;
345 u64 extent_start, extent_end, size, total_added = 0;
348 while (start < end) {
349 ret = find_first_extent_bit(info->pinned_extents, start,
350 &extent_start, &extent_end,
351 EXTENT_DIRTY | EXTENT_UPTODATE,
356 if (extent_start <= start) {
357 start = extent_end + 1;
358 } else if (extent_start > start && extent_start < end) {
359 size = extent_start - start;
361 ret = btrfs_add_free_space(block_group, start,
363 BUG_ON(ret); /* -ENOMEM or logic error */
364 start = extent_end + 1;
373 ret = btrfs_add_free_space(block_group, start, size);
374 BUG_ON(ret); /* -ENOMEM or logic error */
380 static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
382 struct btrfs_block_group_cache *block_group = caching_ctl->block_group;
383 struct btrfs_fs_info *fs_info = block_group->fs_info;
384 struct btrfs_root *extent_root = fs_info->extent_root;
385 struct btrfs_path *path;
386 struct extent_buffer *leaf;
387 struct btrfs_key key;
394 path = btrfs_alloc_path();
398 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
400 #ifdef CONFIG_BTRFS_DEBUG
402 * If we're fragmenting we don't want to make anybody think we can
403 * allocate from this block group until we've had a chance to fragment
406 if (btrfs_should_fragment_free_space(block_group))
410 * We don't want to deadlock with somebody trying to allocate a new
411 * extent for the extent root while also trying to search the extent
412 * root to add free space. So we skip locking and search the commit
413 * root, since its read-only
415 path->skip_locking = 1;
416 path->search_commit_root = 1;
417 path->reada = READA_FORWARD;
421 key.type = BTRFS_EXTENT_ITEM_KEY;
424 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
428 leaf = path->nodes[0];
429 nritems = btrfs_header_nritems(leaf);
432 if (btrfs_fs_closing(fs_info) > 1) {
437 if (path->slots[0] < nritems) {
438 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
440 ret = find_next_key(path, 0, &key);
444 if (need_resched() ||
445 rwsem_is_contended(&fs_info->commit_root_sem)) {
447 caching_ctl->progress = last;
448 btrfs_release_path(path);
449 up_read(&fs_info->commit_root_sem);
450 mutex_unlock(&caching_ctl->mutex);
452 mutex_lock(&caching_ctl->mutex);
453 down_read(&fs_info->commit_root_sem);
457 ret = btrfs_next_leaf(extent_root, path);
462 leaf = path->nodes[0];
463 nritems = btrfs_header_nritems(leaf);
467 if (key.objectid < last) {
470 key.type = BTRFS_EXTENT_ITEM_KEY;
473 caching_ctl->progress = last;
474 btrfs_release_path(path);
478 if (key.objectid < block_group->key.objectid) {
483 if (key.objectid >= block_group->key.objectid +
484 block_group->key.offset)
487 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
488 key.type == BTRFS_METADATA_ITEM_KEY) {
489 total_found += add_new_free_space(block_group, last,
491 if (key.type == BTRFS_METADATA_ITEM_KEY)
492 last = key.objectid +
495 last = key.objectid + key.offset;
497 if (total_found > CACHING_CTL_WAKE_UP) {
500 wake_up(&caching_ctl->wait);
507 total_found += add_new_free_space(block_group, last,
508 block_group->key.objectid +
509 block_group->key.offset);
510 caching_ctl->progress = (u64)-1;
513 btrfs_free_path(path);
517 static noinline void caching_thread(struct btrfs_work *work)
519 struct btrfs_block_group_cache *block_group;
520 struct btrfs_fs_info *fs_info;
521 struct btrfs_caching_control *caching_ctl;
524 caching_ctl = container_of(work, struct btrfs_caching_control, work);
525 block_group = caching_ctl->block_group;
526 fs_info = block_group->fs_info;
528 mutex_lock(&caching_ctl->mutex);
529 down_read(&fs_info->commit_root_sem);
531 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
532 ret = load_free_space_tree(caching_ctl);
534 ret = load_extent_tree_free(caching_ctl);
536 spin_lock(&block_group->lock);
537 block_group->caching_ctl = NULL;
538 block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED;
539 spin_unlock(&block_group->lock);
541 #ifdef CONFIG_BTRFS_DEBUG
542 if (btrfs_should_fragment_free_space(block_group)) {
545 spin_lock(&block_group->space_info->lock);
546 spin_lock(&block_group->lock);
547 bytes_used = block_group->key.offset -
548 btrfs_block_group_used(&block_group->item);
549 block_group->space_info->bytes_used += bytes_used >> 1;
550 spin_unlock(&block_group->lock);
551 spin_unlock(&block_group->space_info->lock);
552 fragment_free_space(block_group);
556 caching_ctl->progress = (u64)-1;
558 up_read(&fs_info->commit_root_sem);
559 free_excluded_extents(fs_info, block_group);
560 mutex_unlock(&caching_ctl->mutex);
562 wake_up(&caching_ctl->wait);
564 put_caching_control(caching_ctl);
565 btrfs_put_block_group(block_group);
568 static int cache_block_group(struct btrfs_block_group_cache *cache,
572 struct btrfs_fs_info *fs_info = cache->fs_info;
573 struct btrfs_caching_control *caching_ctl;
576 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
580 INIT_LIST_HEAD(&caching_ctl->list);
581 mutex_init(&caching_ctl->mutex);
582 init_waitqueue_head(&caching_ctl->wait);
583 caching_ctl->block_group = cache;
584 caching_ctl->progress = cache->key.objectid;
585 refcount_set(&caching_ctl->count, 1);
586 btrfs_init_work(&caching_ctl->work, btrfs_cache_helper,
587 caching_thread, NULL, NULL);
589 spin_lock(&cache->lock);
591 * This should be a rare occasion, but this could happen I think in the
592 * case where one thread starts to load the space cache info, and then
593 * some other thread starts a transaction commit which tries to do an
594 * allocation while the other thread is still loading the space cache
595 * info. The previous loop should have kept us from choosing this block
596 * group, but if we've moved to the state where we will wait on caching
597 * block groups we need to first check if we're doing a fast load here,
598 * so we can wait for it to finish, otherwise we could end up allocating
599 * from a block group who's cache gets evicted for one reason or
602 while (cache->cached == BTRFS_CACHE_FAST) {
603 struct btrfs_caching_control *ctl;
605 ctl = cache->caching_ctl;
606 refcount_inc(&ctl->count);
607 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
608 spin_unlock(&cache->lock);
612 finish_wait(&ctl->wait, &wait);
613 put_caching_control(ctl);
614 spin_lock(&cache->lock);
617 if (cache->cached != BTRFS_CACHE_NO) {
618 spin_unlock(&cache->lock);
622 WARN_ON(cache->caching_ctl);
623 cache->caching_ctl = caching_ctl;
624 cache->cached = BTRFS_CACHE_FAST;
625 spin_unlock(&cache->lock);
627 if (btrfs_test_opt(fs_info, SPACE_CACHE)) {
628 mutex_lock(&caching_ctl->mutex);
629 ret = load_free_space_cache(fs_info, cache);
631 spin_lock(&cache->lock);
633 cache->caching_ctl = NULL;
634 cache->cached = BTRFS_CACHE_FINISHED;
635 cache->last_byte_to_unpin = (u64)-1;
636 caching_ctl->progress = (u64)-1;
638 if (load_cache_only) {
639 cache->caching_ctl = NULL;
640 cache->cached = BTRFS_CACHE_NO;
642 cache->cached = BTRFS_CACHE_STARTED;
643 cache->has_caching_ctl = 1;
646 spin_unlock(&cache->lock);
647 #ifdef CONFIG_BTRFS_DEBUG
649 btrfs_should_fragment_free_space(cache)) {
652 spin_lock(&cache->space_info->lock);
653 spin_lock(&cache->lock);
654 bytes_used = cache->key.offset -
655 btrfs_block_group_used(&cache->item);
656 cache->space_info->bytes_used += bytes_used >> 1;
657 spin_unlock(&cache->lock);
658 spin_unlock(&cache->space_info->lock);
659 fragment_free_space(cache);
662 mutex_unlock(&caching_ctl->mutex);
664 wake_up(&caching_ctl->wait);
666 put_caching_control(caching_ctl);
667 free_excluded_extents(fs_info, cache);
672 * We're either using the free space tree or no caching at all.
673 * Set cached to the appropriate value and wakeup any waiters.
675 spin_lock(&cache->lock);
676 if (load_cache_only) {
677 cache->caching_ctl = NULL;
678 cache->cached = BTRFS_CACHE_NO;
680 cache->cached = BTRFS_CACHE_STARTED;
681 cache->has_caching_ctl = 1;
683 spin_unlock(&cache->lock);
684 wake_up(&caching_ctl->wait);
687 if (load_cache_only) {
688 put_caching_control(caching_ctl);
692 down_write(&fs_info->commit_root_sem);
693 refcount_inc(&caching_ctl->count);
694 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
695 up_write(&fs_info->commit_root_sem);
697 btrfs_get_block_group(cache);
699 btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work);
705 * return the block group that starts at or after bytenr
707 static struct btrfs_block_group_cache *
708 btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
710 return block_group_cache_tree_search(info, bytenr, 0);
714 * return the block group that contains the given bytenr
716 struct btrfs_block_group_cache *btrfs_lookup_block_group(
717 struct btrfs_fs_info *info,
720 return block_group_cache_tree_search(info, bytenr, 1);
723 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
726 struct list_head *head = &info->space_info;
727 struct btrfs_space_info *found;
729 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
732 list_for_each_entry_rcu(found, head, list) {
733 if (found->flags & flags) {
742 static void add_pinned_bytes(struct btrfs_fs_info *fs_info, s64 num_bytes,
743 bool metadata, u64 root_objectid)
745 struct btrfs_space_info *space_info;
749 if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
750 flags = BTRFS_BLOCK_GROUP_SYSTEM;
752 flags = BTRFS_BLOCK_GROUP_METADATA;
754 flags = BTRFS_BLOCK_GROUP_DATA;
757 space_info = __find_space_info(fs_info, flags);
759 percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
763 * after adding space to the filesystem, we need to clear the full flags
764 * on all the space infos.
766 void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
768 struct list_head *head = &info->space_info;
769 struct btrfs_space_info *found;
772 list_for_each_entry_rcu(found, head, list)
777 /* simple helper to search for an existing data extent at a given offset */
778 int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len)
781 struct btrfs_key key;
782 struct btrfs_path *path;
784 path = btrfs_alloc_path();
788 key.objectid = start;
790 key.type = BTRFS_EXTENT_ITEM_KEY;
791 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
792 btrfs_free_path(path);
797 * helper function to lookup reference count and flags of a tree block.
799 * the head node for delayed ref is used to store the sum of all the
800 * reference count modifications queued up in the rbtree. the head
801 * node may also store the extent flags to set. This way you can check
802 * to see what the reference count and extent flags would be if all of
803 * the delayed refs are not processed.
805 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
806 struct btrfs_fs_info *fs_info, u64 bytenr,
807 u64 offset, int metadata, u64 *refs, u64 *flags)
809 struct btrfs_delayed_ref_head *head;
810 struct btrfs_delayed_ref_root *delayed_refs;
811 struct btrfs_path *path;
812 struct btrfs_extent_item *ei;
813 struct extent_buffer *leaf;
814 struct btrfs_key key;
821 * If we don't have skinny metadata, don't bother doing anything
824 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) {
825 offset = fs_info->nodesize;
829 path = btrfs_alloc_path();
834 path->skip_locking = 1;
835 path->search_commit_root = 1;
839 key.objectid = bytenr;
842 key.type = BTRFS_METADATA_ITEM_KEY;
844 key.type = BTRFS_EXTENT_ITEM_KEY;
846 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
850 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
851 if (path->slots[0]) {
853 btrfs_item_key_to_cpu(path->nodes[0], &key,
855 if (key.objectid == bytenr &&
856 key.type == BTRFS_EXTENT_ITEM_KEY &&
857 key.offset == fs_info->nodesize)
863 leaf = path->nodes[0];
864 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
865 if (item_size >= sizeof(*ei)) {
866 ei = btrfs_item_ptr(leaf, path->slots[0],
867 struct btrfs_extent_item);
868 num_refs = btrfs_extent_refs(leaf, ei);
869 extent_flags = btrfs_extent_flags(leaf, ei);
871 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
872 struct btrfs_extent_item_v0 *ei0;
873 BUG_ON(item_size != sizeof(*ei0));
874 ei0 = btrfs_item_ptr(leaf, path->slots[0],
875 struct btrfs_extent_item_v0);
876 num_refs = btrfs_extent_refs_v0(leaf, ei0);
877 /* FIXME: this isn't correct for data */
878 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
883 BUG_ON(num_refs == 0);
893 delayed_refs = &trans->transaction->delayed_refs;
894 spin_lock(&delayed_refs->lock);
895 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
897 if (!mutex_trylock(&head->mutex)) {
898 refcount_inc(&head->refs);
899 spin_unlock(&delayed_refs->lock);
901 btrfs_release_path(path);
904 * Mutex was contended, block until it's released and try
907 mutex_lock(&head->mutex);
908 mutex_unlock(&head->mutex);
909 btrfs_put_delayed_ref_head(head);
912 spin_lock(&head->lock);
913 if (head->extent_op && head->extent_op->update_flags)
914 extent_flags |= head->extent_op->flags_to_set;
916 BUG_ON(num_refs == 0);
918 num_refs += head->ref_mod;
919 spin_unlock(&head->lock);
920 mutex_unlock(&head->mutex);
922 spin_unlock(&delayed_refs->lock);
924 WARN_ON(num_refs == 0);
928 *flags = extent_flags;
930 btrfs_free_path(path);
935 * Back reference rules. Back refs have three main goals:
937 * 1) differentiate between all holders of references to an extent so that
938 * when a reference is dropped we can make sure it was a valid reference
939 * before freeing the extent.
941 * 2) Provide enough information to quickly find the holders of an extent
942 * if we notice a given block is corrupted or bad.
944 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
945 * maintenance. This is actually the same as #2, but with a slightly
946 * different use case.
948 * There are two kinds of back refs. The implicit back refs is optimized
949 * for pointers in non-shared tree blocks. For a given pointer in a block,
950 * back refs of this kind provide information about the block's owner tree
951 * and the pointer's key. These information allow us to find the block by
952 * b-tree searching. The full back refs is for pointers in tree blocks not
953 * referenced by their owner trees. The location of tree block is recorded
954 * in the back refs. Actually the full back refs is generic, and can be
955 * used in all cases the implicit back refs is used. The major shortcoming
956 * of the full back refs is its overhead. Every time a tree block gets
957 * COWed, we have to update back refs entry for all pointers in it.
959 * For a newly allocated tree block, we use implicit back refs for
960 * pointers in it. This means most tree related operations only involve
961 * implicit back refs. For a tree block created in old transaction, the
962 * only way to drop a reference to it is COW it. So we can detect the
963 * event that tree block loses its owner tree's reference and do the
964 * back refs conversion.
966 * When a tree block is COWed through a tree, there are four cases:
968 * The reference count of the block is one and the tree is the block's
969 * owner tree. Nothing to do in this case.
971 * The reference count of the block is one and the tree is not the
972 * block's owner tree. In this case, full back refs is used for pointers
973 * in the block. Remove these full back refs, add implicit back refs for
974 * every pointers in the new block.
976 * The reference count of the block is greater than one and the tree is
977 * the block's owner tree. In this case, implicit back refs is used for
978 * pointers in the block. Add full back refs for every pointers in the
979 * block, increase lower level extents' reference counts. The original
980 * implicit back refs are entailed to the new block.
982 * The reference count of the block is greater than one and the tree is
983 * not the block's owner tree. Add implicit back refs for every pointer in
984 * the new block, increase lower level extents' reference count.
986 * Back Reference Key composing:
988 * The key objectid corresponds to the first byte in the extent,
989 * The key type is used to differentiate between types of back refs.
990 * There are different meanings of the key offset for different types
993 * File extents can be referenced by:
995 * - multiple snapshots, subvolumes, or different generations in one subvol
996 * - different files inside a single subvolume
997 * - different offsets inside a file (bookend extents in file.c)
999 * The extent ref structure for the implicit back refs has fields for:
1001 * - Objectid of the subvolume root
1002 * - objectid of the file holding the reference
1003 * - original offset in the file
1004 * - how many bookend extents
1006 * The key offset for the implicit back refs is hash of the first
1009 * The extent ref structure for the full back refs has field for:
1011 * - number of pointers in the tree leaf
1013 * The key offset for the implicit back refs is the first byte of
1016 * When a file extent is allocated, The implicit back refs is used.
1017 * the fields are filled in:
1019 * (root_key.objectid, inode objectid, offset in file, 1)
1021 * When a file extent is removed file truncation, we find the
1022 * corresponding implicit back refs and check the following fields:
1024 * (btrfs_header_owner(leaf), inode objectid, offset in file)
1026 * Btree extents can be referenced by:
1028 * - Different subvolumes
1030 * Both the implicit back refs and the full back refs for tree blocks
1031 * only consist of key. The key offset for the implicit back refs is
1032 * objectid of block's owner tree. The key offset for the full back refs
1033 * is the first byte of parent block.
1035 * When implicit back refs is used, information about the lowest key and
1036 * level of the tree block are required. These information are stored in
1037 * tree block info structure.
1040 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1041 static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
1042 struct btrfs_fs_info *fs_info,
1043 struct btrfs_path *path,
1044 u64 owner, u32 extra_size)
1046 struct btrfs_root *root = fs_info->extent_root;
1047 struct btrfs_extent_item *item;
1048 struct btrfs_extent_item_v0 *ei0;
1049 struct btrfs_extent_ref_v0 *ref0;
1050 struct btrfs_tree_block_info *bi;
1051 struct extent_buffer *leaf;
1052 struct btrfs_key key;
1053 struct btrfs_key found_key;
1054 u32 new_size = sizeof(*item);
1058 leaf = path->nodes[0];
1059 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
1061 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1062 ei0 = btrfs_item_ptr(leaf, path->slots[0],
1063 struct btrfs_extent_item_v0);
1064 refs = btrfs_extent_refs_v0(leaf, ei0);
1066 if (owner == (u64)-1) {
1068 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1069 ret = btrfs_next_leaf(root, path);
1072 BUG_ON(ret > 0); /* Corruption */
1073 leaf = path->nodes[0];
1075 btrfs_item_key_to_cpu(leaf, &found_key,
1077 BUG_ON(key.objectid != found_key.objectid);
1078 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
1082 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1083 struct btrfs_extent_ref_v0);
1084 owner = btrfs_ref_objectid_v0(leaf, ref0);
1088 btrfs_release_path(path);
1090 if (owner < BTRFS_FIRST_FREE_OBJECTID)
1091 new_size += sizeof(*bi);
1093 new_size -= sizeof(*ei0);
1094 ret = btrfs_search_slot(trans, root, &key, path,
1095 new_size + extra_size, 1);
1098 BUG_ON(ret); /* Corruption */
1100 btrfs_extend_item(fs_info, path, new_size);
1102 leaf = path->nodes[0];
1103 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1104 btrfs_set_extent_refs(leaf, item, refs);
1105 /* FIXME: get real generation */
1106 btrfs_set_extent_generation(leaf, item, 0);
1107 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1108 btrfs_set_extent_flags(leaf, item,
1109 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1110 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1111 bi = (struct btrfs_tree_block_info *)(item + 1);
1112 /* FIXME: get first key of the block */
1113 memzero_extent_buffer(leaf, (unsigned long)bi, sizeof(*bi));
1114 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1116 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1118 btrfs_mark_buffer_dirty(leaf);
1124 * is_data == BTRFS_REF_TYPE_BLOCK, tree block type is required,
1125 * is_data == BTRFS_REF_TYPE_DATA, data type is requried,
1126 * is_data == BTRFS_REF_TYPE_ANY, either type is OK.
1128 int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
1129 struct btrfs_extent_inline_ref *iref,
1130 enum btrfs_inline_ref_type is_data)
1132 int type = btrfs_extent_inline_ref_type(eb, iref);
1133 u64 offset = btrfs_extent_inline_ref_offset(eb, iref);
1135 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
1136 type == BTRFS_SHARED_BLOCK_REF_KEY ||
1137 type == BTRFS_SHARED_DATA_REF_KEY ||
1138 type == BTRFS_EXTENT_DATA_REF_KEY) {
1139 if (is_data == BTRFS_REF_TYPE_BLOCK) {
1140 if (type == BTRFS_TREE_BLOCK_REF_KEY)
1142 if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1143 ASSERT(eb->fs_info);
1145 * Every shared one has parent tree
1146 * block, which must be aligned to
1150 IS_ALIGNED(offset, eb->fs_info->nodesize))
1153 } else if (is_data == BTRFS_REF_TYPE_DATA) {
1154 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1156 if (type == BTRFS_SHARED_DATA_REF_KEY) {
1157 ASSERT(eb->fs_info);
1159 * Every shared one has parent tree
1160 * block, which must be aligned to
1164 IS_ALIGNED(offset, eb->fs_info->nodesize))
1168 ASSERT(is_data == BTRFS_REF_TYPE_ANY);
1173 btrfs_print_leaf((struct extent_buffer *)eb);
1174 btrfs_err(eb->fs_info, "eb %llu invalid extent inline ref type %d",
1178 return BTRFS_REF_TYPE_INVALID;
1181 static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1183 u32 high_crc = ~(u32)0;
1184 u32 low_crc = ~(u32)0;
1187 lenum = cpu_to_le64(root_objectid);
1188 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
1189 lenum = cpu_to_le64(owner);
1190 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
1191 lenum = cpu_to_le64(offset);
1192 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
1194 return ((u64)high_crc << 31) ^ (u64)low_crc;
1197 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1198 struct btrfs_extent_data_ref *ref)
1200 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1201 btrfs_extent_data_ref_objectid(leaf, ref),
1202 btrfs_extent_data_ref_offset(leaf, ref));
1205 static int match_extent_data_ref(struct extent_buffer *leaf,
1206 struct btrfs_extent_data_ref *ref,
1207 u64 root_objectid, u64 owner, u64 offset)
1209 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1210 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1211 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1216 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1217 struct btrfs_path *path,
1218 u64 bytenr, u64 parent,
1220 u64 owner, u64 offset)
1222 struct btrfs_root *root = trans->fs_info->extent_root;
1223 struct btrfs_key key;
1224 struct btrfs_extent_data_ref *ref;
1225 struct extent_buffer *leaf;
1231 key.objectid = bytenr;
1233 key.type = BTRFS_SHARED_DATA_REF_KEY;
1234 key.offset = parent;
1236 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1237 key.offset = hash_extent_data_ref(root_objectid,
1242 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1251 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1252 key.type = BTRFS_EXTENT_REF_V0_KEY;
1253 btrfs_release_path(path);
1254 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1265 leaf = path->nodes[0];
1266 nritems = btrfs_header_nritems(leaf);
1268 if (path->slots[0] >= nritems) {
1269 ret = btrfs_next_leaf(root, path);
1275 leaf = path->nodes[0];
1276 nritems = btrfs_header_nritems(leaf);
1280 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1281 if (key.objectid != bytenr ||
1282 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1285 ref = btrfs_item_ptr(leaf, path->slots[0],
1286 struct btrfs_extent_data_ref);
1288 if (match_extent_data_ref(leaf, ref, root_objectid,
1291 btrfs_release_path(path);
1303 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1304 struct btrfs_path *path,
1305 u64 bytenr, u64 parent,
1306 u64 root_objectid, u64 owner,
1307 u64 offset, int refs_to_add)
1309 struct btrfs_root *root = trans->fs_info->extent_root;
1310 struct btrfs_key key;
1311 struct extent_buffer *leaf;
1316 key.objectid = bytenr;
1318 key.type = BTRFS_SHARED_DATA_REF_KEY;
1319 key.offset = parent;
1320 size = sizeof(struct btrfs_shared_data_ref);
1322 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1323 key.offset = hash_extent_data_ref(root_objectid,
1325 size = sizeof(struct btrfs_extent_data_ref);
1328 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1329 if (ret && ret != -EEXIST)
1332 leaf = path->nodes[0];
1334 struct btrfs_shared_data_ref *ref;
1335 ref = btrfs_item_ptr(leaf, path->slots[0],
1336 struct btrfs_shared_data_ref);
1338 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1340 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1341 num_refs += refs_to_add;
1342 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1345 struct btrfs_extent_data_ref *ref;
1346 while (ret == -EEXIST) {
1347 ref = btrfs_item_ptr(leaf, path->slots[0],
1348 struct btrfs_extent_data_ref);
1349 if (match_extent_data_ref(leaf, ref, root_objectid,
1352 btrfs_release_path(path);
1354 ret = btrfs_insert_empty_item(trans, root, path, &key,
1356 if (ret && ret != -EEXIST)
1359 leaf = path->nodes[0];
1361 ref = btrfs_item_ptr(leaf, path->slots[0],
1362 struct btrfs_extent_data_ref);
1364 btrfs_set_extent_data_ref_root(leaf, ref,
1366 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1367 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1368 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1370 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1371 num_refs += refs_to_add;
1372 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1375 btrfs_mark_buffer_dirty(leaf);
1378 btrfs_release_path(path);
1382 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1383 struct btrfs_path *path,
1384 int refs_to_drop, int *last_ref)
1386 struct btrfs_key key;
1387 struct btrfs_extent_data_ref *ref1 = NULL;
1388 struct btrfs_shared_data_ref *ref2 = NULL;
1389 struct extent_buffer *leaf;
1393 leaf = path->nodes[0];
1394 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1396 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1397 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1398 struct btrfs_extent_data_ref);
1399 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1400 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1401 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1402 struct btrfs_shared_data_ref);
1403 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1404 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1405 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1406 struct btrfs_extent_ref_v0 *ref0;
1407 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1408 struct btrfs_extent_ref_v0);
1409 num_refs = btrfs_ref_count_v0(leaf, ref0);
1415 BUG_ON(num_refs < refs_to_drop);
1416 num_refs -= refs_to_drop;
1418 if (num_refs == 0) {
1419 ret = btrfs_del_item(trans, trans->fs_info->extent_root, path);
1422 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1423 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1424 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1425 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1426 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1428 struct btrfs_extent_ref_v0 *ref0;
1429 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1430 struct btrfs_extent_ref_v0);
1431 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1434 btrfs_mark_buffer_dirty(leaf);
1439 static noinline u32 extent_data_ref_count(struct btrfs_path *path,
1440 struct btrfs_extent_inline_ref *iref)
1442 struct btrfs_key key;
1443 struct extent_buffer *leaf;
1444 struct btrfs_extent_data_ref *ref1;
1445 struct btrfs_shared_data_ref *ref2;
1449 leaf = path->nodes[0];
1450 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1453 * If type is invalid, we should have bailed out earlier than
1456 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
1457 ASSERT(type != BTRFS_REF_TYPE_INVALID);
1458 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1459 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1460 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1462 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1463 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1465 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1466 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1467 struct btrfs_extent_data_ref);
1468 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1469 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1470 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1471 struct btrfs_shared_data_ref);
1472 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1473 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1474 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1475 struct btrfs_extent_ref_v0 *ref0;
1476 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1477 struct btrfs_extent_ref_v0);
1478 num_refs = btrfs_ref_count_v0(leaf, ref0);
1486 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1487 struct btrfs_path *path,
1488 u64 bytenr, u64 parent,
1491 struct btrfs_root *root = trans->fs_info->extent_root;
1492 struct btrfs_key key;
1495 key.objectid = bytenr;
1497 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1498 key.offset = parent;
1500 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1501 key.offset = root_objectid;
1504 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1507 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1508 if (ret == -ENOENT && parent) {
1509 btrfs_release_path(path);
1510 key.type = BTRFS_EXTENT_REF_V0_KEY;
1511 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1519 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1520 struct btrfs_path *path,
1521 u64 bytenr, u64 parent,
1524 struct btrfs_key key;
1527 key.objectid = bytenr;
1529 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1530 key.offset = parent;
1532 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1533 key.offset = root_objectid;
1536 ret = btrfs_insert_empty_item(trans, trans->fs_info->extent_root,
1538 btrfs_release_path(path);
1542 static inline int extent_ref_type(u64 parent, u64 owner)
1545 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1547 type = BTRFS_SHARED_BLOCK_REF_KEY;
1549 type = BTRFS_TREE_BLOCK_REF_KEY;
1552 type = BTRFS_SHARED_DATA_REF_KEY;
1554 type = BTRFS_EXTENT_DATA_REF_KEY;
1559 static int find_next_key(struct btrfs_path *path, int level,
1560 struct btrfs_key *key)
1563 for (; level < BTRFS_MAX_LEVEL; level++) {
1564 if (!path->nodes[level])
1566 if (path->slots[level] + 1 >=
1567 btrfs_header_nritems(path->nodes[level]))
1570 btrfs_item_key_to_cpu(path->nodes[level], key,
1571 path->slots[level] + 1);
1573 btrfs_node_key_to_cpu(path->nodes[level], key,
1574 path->slots[level] + 1);
1581 * look for inline back ref. if back ref is found, *ref_ret is set
1582 * to the address of inline back ref, and 0 is returned.
1584 * if back ref isn't found, *ref_ret is set to the address where it
1585 * should be inserted, and -ENOENT is returned.
1587 * if insert is true and there are too many inline back refs, the path
1588 * points to the extent item, and -EAGAIN is returned.
1590 * NOTE: inline back refs are ordered in the same way that back ref
1591 * items in the tree are ordered.
1593 static noinline_for_stack
1594 int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1595 struct btrfs_path *path,
1596 struct btrfs_extent_inline_ref **ref_ret,
1597 u64 bytenr, u64 num_bytes,
1598 u64 parent, u64 root_objectid,
1599 u64 owner, u64 offset, int insert)
1601 struct btrfs_fs_info *fs_info = trans->fs_info;
1602 struct btrfs_root *root = fs_info->extent_root;
1603 struct btrfs_key key;
1604 struct extent_buffer *leaf;
1605 struct btrfs_extent_item *ei;
1606 struct btrfs_extent_inline_ref *iref;
1616 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
1619 key.objectid = bytenr;
1620 key.type = BTRFS_EXTENT_ITEM_KEY;
1621 key.offset = num_bytes;
1623 want = extent_ref_type(parent, owner);
1625 extra_size = btrfs_extent_inline_ref_size(want);
1626 path->keep_locks = 1;
1631 * Owner is our level, so we can just add one to get the level for the
1632 * block we are interested in.
1634 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
1635 key.type = BTRFS_METADATA_ITEM_KEY;
1640 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1647 * We may be a newly converted file system which still has the old fat
1648 * extent entries for metadata, so try and see if we have one of those.
1650 if (ret > 0 && skinny_metadata) {
1651 skinny_metadata = false;
1652 if (path->slots[0]) {
1654 btrfs_item_key_to_cpu(path->nodes[0], &key,
1656 if (key.objectid == bytenr &&
1657 key.type == BTRFS_EXTENT_ITEM_KEY &&
1658 key.offset == num_bytes)
1662 key.objectid = bytenr;
1663 key.type = BTRFS_EXTENT_ITEM_KEY;
1664 key.offset = num_bytes;
1665 btrfs_release_path(path);
1670 if (ret && !insert) {
1673 } else if (WARN_ON(ret)) {
1678 leaf = path->nodes[0];
1679 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1680 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1681 if (item_size < sizeof(*ei)) {
1686 ret = convert_extent_item_v0(trans, fs_info, path, owner,
1692 leaf = path->nodes[0];
1693 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1696 BUG_ON(item_size < sizeof(*ei));
1698 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1699 flags = btrfs_extent_flags(leaf, ei);
1701 ptr = (unsigned long)(ei + 1);
1702 end = (unsigned long)ei + item_size;
1704 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
1705 ptr += sizeof(struct btrfs_tree_block_info);
1709 if (owner >= BTRFS_FIRST_FREE_OBJECTID)
1710 needed = BTRFS_REF_TYPE_DATA;
1712 needed = BTRFS_REF_TYPE_BLOCK;
1720 iref = (struct btrfs_extent_inline_ref *)ptr;
1721 type = btrfs_get_extent_inline_ref_type(leaf, iref, needed);
1722 if (type == BTRFS_REF_TYPE_INVALID) {
1730 ptr += btrfs_extent_inline_ref_size(type);
1734 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1735 struct btrfs_extent_data_ref *dref;
1736 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1737 if (match_extent_data_ref(leaf, dref, root_objectid,
1742 if (hash_extent_data_ref_item(leaf, dref) <
1743 hash_extent_data_ref(root_objectid, owner, offset))
1747 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1749 if (parent == ref_offset) {
1753 if (ref_offset < parent)
1756 if (root_objectid == ref_offset) {
1760 if (ref_offset < root_objectid)
1764 ptr += btrfs_extent_inline_ref_size(type);
1766 if (err == -ENOENT && insert) {
1767 if (item_size + extra_size >=
1768 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1773 * To add new inline back ref, we have to make sure
1774 * there is no corresponding back ref item.
1775 * For simplicity, we just do not add new inline back
1776 * ref if there is any kind of item for this block
1778 if (find_next_key(path, 0, &key) == 0 &&
1779 key.objectid == bytenr &&
1780 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
1785 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1788 path->keep_locks = 0;
1789 btrfs_unlock_up_safe(path, 1);
1795 * helper to add new inline back ref
1797 static noinline_for_stack
1798 void setup_inline_extent_backref(struct btrfs_fs_info *fs_info,
1799 struct btrfs_path *path,
1800 struct btrfs_extent_inline_ref *iref,
1801 u64 parent, u64 root_objectid,
1802 u64 owner, u64 offset, int refs_to_add,
1803 struct btrfs_delayed_extent_op *extent_op)
1805 struct extent_buffer *leaf;
1806 struct btrfs_extent_item *ei;
1809 unsigned long item_offset;
1814 leaf = path->nodes[0];
1815 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1816 item_offset = (unsigned long)iref - (unsigned long)ei;
1818 type = extent_ref_type(parent, owner);
1819 size = btrfs_extent_inline_ref_size(type);
1821 btrfs_extend_item(fs_info, path, size);
1823 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1824 refs = btrfs_extent_refs(leaf, ei);
1825 refs += refs_to_add;
1826 btrfs_set_extent_refs(leaf, ei, refs);
1828 __run_delayed_extent_op(extent_op, leaf, ei);
1830 ptr = (unsigned long)ei + item_offset;
1831 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1832 if (ptr < end - size)
1833 memmove_extent_buffer(leaf, ptr + size, ptr,
1836 iref = (struct btrfs_extent_inline_ref *)ptr;
1837 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1838 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1839 struct btrfs_extent_data_ref *dref;
1840 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1841 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1842 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1843 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1844 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1845 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1846 struct btrfs_shared_data_ref *sref;
1847 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1848 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1849 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1850 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1851 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1853 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1855 btrfs_mark_buffer_dirty(leaf);
1858 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1859 struct btrfs_path *path,
1860 struct btrfs_extent_inline_ref **ref_ret,
1861 u64 bytenr, u64 num_bytes, u64 parent,
1862 u64 root_objectid, u64 owner, u64 offset)
1866 ret = lookup_inline_extent_backref(trans, path, ref_ret, bytenr,
1867 num_bytes, parent, root_objectid,
1872 btrfs_release_path(path);
1875 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1876 ret = lookup_tree_block_ref(trans, path, bytenr, parent,
1879 ret = lookup_extent_data_ref(trans, path, bytenr, parent,
1880 root_objectid, owner, offset);
1886 * helper to update/remove inline back ref
1888 static noinline_for_stack
1889 void update_inline_extent_backref(struct btrfs_path *path,
1890 struct btrfs_extent_inline_ref *iref,
1892 struct btrfs_delayed_extent_op *extent_op,
1895 struct extent_buffer *leaf = path->nodes[0];
1896 struct btrfs_fs_info *fs_info = leaf->fs_info;
1897 struct btrfs_extent_item *ei;
1898 struct btrfs_extent_data_ref *dref = NULL;
1899 struct btrfs_shared_data_ref *sref = NULL;
1907 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1908 refs = btrfs_extent_refs(leaf, ei);
1909 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1910 refs += refs_to_mod;
1911 btrfs_set_extent_refs(leaf, ei, refs);
1913 __run_delayed_extent_op(extent_op, leaf, ei);
1916 * If type is invalid, we should have bailed out after
1917 * lookup_inline_extent_backref().
1919 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_ANY);
1920 ASSERT(type != BTRFS_REF_TYPE_INVALID);
1922 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1923 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1924 refs = btrfs_extent_data_ref_count(leaf, dref);
1925 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1926 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1927 refs = btrfs_shared_data_ref_count(leaf, sref);
1930 BUG_ON(refs_to_mod != -1);
1933 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1934 refs += refs_to_mod;
1937 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1938 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1940 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1943 size = btrfs_extent_inline_ref_size(type);
1944 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1945 ptr = (unsigned long)iref;
1946 end = (unsigned long)ei + item_size;
1947 if (ptr + size < end)
1948 memmove_extent_buffer(leaf, ptr, ptr + size,
1951 btrfs_truncate_item(fs_info, path, item_size, 1);
1953 btrfs_mark_buffer_dirty(leaf);
1956 static noinline_for_stack
1957 int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1958 struct btrfs_fs_info *fs_info,
1959 struct btrfs_path *path,
1960 u64 bytenr, u64 num_bytes, u64 parent,
1961 u64 root_objectid, u64 owner,
1962 u64 offset, int refs_to_add,
1963 struct btrfs_delayed_extent_op *extent_op)
1965 struct btrfs_extent_inline_ref *iref;
1968 ret = lookup_inline_extent_backref(trans, path, &iref, bytenr,
1969 num_bytes, parent, root_objectid,
1972 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1973 update_inline_extent_backref(path, iref, refs_to_add,
1975 } else if (ret == -ENOENT) {
1976 setup_inline_extent_backref(fs_info, path, iref, parent,
1977 root_objectid, owner, offset,
1978 refs_to_add, extent_op);
1984 static int insert_extent_backref(struct btrfs_trans_handle *trans,
1985 struct btrfs_path *path,
1986 u64 bytenr, u64 parent, u64 root_objectid,
1987 u64 owner, u64 offset, int refs_to_add)
1990 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1991 BUG_ON(refs_to_add != 1);
1992 ret = insert_tree_block_ref(trans, path, bytenr, parent,
1995 ret = insert_extent_data_ref(trans, path, bytenr, parent,
1996 root_objectid, owner, offset,
2002 static int remove_extent_backref(struct btrfs_trans_handle *trans,
2003 struct btrfs_fs_info *fs_info,
2004 struct btrfs_path *path,
2005 struct btrfs_extent_inline_ref *iref,
2006 int refs_to_drop, int is_data, int *last_ref)
2010 BUG_ON(!is_data && refs_to_drop != 1);
2012 update_inline_extent_backref(path, iref, -refs_to_drop, NULL,
2014 } else if (is_data) {
2015 ret = remove_extent_data_ref(trans, path, refs_to_drop,
2019 ret = btrfs_del_item(trans, fs_info->extent_root, path);
2024 #define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
2025 static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
2026 u64 *discarded_bytes)
2029 u64 bytes_left, end;
2030 u64 aligned_start = ALIGN(start, 1 << 9);
2032 if (WARN_ON(start != aligned_start)) {
2033 len -= aligned_start - start;
2034 len = round_down(len, 1 << 9);
2035 start = aligned_start;
2038 *discarded_bytes = 0;
2046 /* Skip any superblocks on this device. */
2047 for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) {
2048 u64 sb_start = btrfs_sb_offset(j);
2049 u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE;
2050 u64 size = sb_start - start;
2052 if (!in_range(sb_start, start, bytes_left) &&
2053 !in_range(sb_end, start, bytes_left) &&
2054 !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE))
2058 * Superblock spans beginning of range. Adjust start and
2061 if (sb_start <= start) {
2062 start += sb_end - start;
2067 bytes_left = end - start;
2072 ret = blkdev_issue_discard(bdev, start >> 9, size >> 9,
2075 *discarded_bytes += size;
2076 else if (ret != -EOPNOTSUPP)
2085 bytes_left = end - start;
2089 ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9,
2092 *discarded_bytes += bytes_left;
2097 int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
2098 u64 num_bytes, u64 *actual_bytes)
2101 u64 discarded_bytes = 0;
2102 struct btrfs_bio *bbio = NULL;
2106 * Avoid races with device replace and make sure our bbio has devices
2107 * associated to its stripes that don't go away while we are discarding.
2109 btrfs_bio_counter_inc_blocked(fs_info);
2110 /* Tell the block device(s) that the sectors can be discarded */
2111 ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, bytenr, &num_bytes,
2113 /* Error condition is -ENOMEM */
2115 struct btrfs_bio_stripe *stripe = bbio->stripes;
2119 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
2121 struct request_queue *req_q;
2123 if (!stripe->dev->bdev) {
2124 ASSERT(btrfs_test_opt(fs_info, DEGRADED));
2127 req_q = bdev_get_queue(stripe->dev->bdev);
2128 if (!blk_queue_discard(req_q))
2131 ret = btrfs_issue_discard(stripe->dev->bdev,
2136 discarded_bytes += bytes;
2137 else if (ret != -EOPNOTSUPP)
2138 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
2141 * Just in case we get back EOPNOTSUPP for some reason,
2142 * just ignore the return value so we don't screw up
2143 * people calling discard_extent.
2147 btrfs_put_bbio(bbio);
2149 btrfs_bio_counter_dec(fs_info);
2152 *actual_bytes = discarded_bytes;
2155 if (ret == -EOPNOTSUPP)
2160 /* Can return -ENOMEM */
2161 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
2162 struct btrfs_root *root,
2163 u64 bytenr, u64 num_bytes, u64 parent,
2164 u64 root_objectid, u64 owner, u64 offset)
2166 struct btrfs_fs_info *fs_info = root->fs_info;
2167 int old_ref_mod, new_ref_mod;
2170 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
2171 root_objectid == BTRFS_TREE_LOG_OBJECTID);
2173 btrfs_ref_tree_mod(root, bytenr, num_bytes, parent, root_objectid,
2174 owner, offset, BTRFS_ADD_DELAYED_REF);
2176 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
2177 ret = btrfs_add_delayed_tree_ref(trans, bytenr,
2179 root_objectid, (int)owner,
2180 BTRFS_ADD_DELAYED_REF, NULL,
2181 &old_ref_mod, &new_ref_mod);
2183 ret = btrfs_add_delayed_data_ref(trans, bytenr,
2185 root_objectid, owner, offset,
2186 0, BTRFS_ADD_DELAYED_REF,
2187 &old_ref_mod, &new_ref_mod);
2190 if (ret == 0 && old_ref_mod < 0 && new_ref_mod >= 0) {
2191 bool metadata = owner < BTRFS_FIRST_FREE_OBJECTID;
2193 add_pinned_bytes(fs_info, -num_bytes, metadata, root_objectid);
2200 * __btrfs_inc_extent_ref - insert backreference for a given extent
2202 * @trans: Handle of transaction
2204 * @node: The delayed ref node used to get the bytenr/length for
2205 * extent whose references are incremented.
2207 * @parent: If this is a shared extent (BTRFS_SHARED_DATA_REF_KEY/
2208 * BTRFS_SHARED_BLOCK_REF_KEY) then it holds the logical
2209 * bytenr of the parent block. Since new extents are always
2210 * created with indirect references, this will only be the case
2211 * when relocating a shared extent. In that case, root_objectid
2212 * will be BTRFS_TREE_RELOC_OBJECTID. Otheriwse, parent must
2215 * @root_objectid: The id of the root where this modification has originated,
2216 * this can be either one of the well-known metadata trees or
2217 * the subvolume id which references this extent.
2219 * @owner: For data extents it is the inode number of the owning file.
2220 * For metadata extents this parameter holds the level in the
2221 * tree of the extent.
2223 * @offset: For metadata extents the offset is ignored and is currently
2224 * always passed as 0. For data extents it is the fileoffset
2225 * this extent belongs to.
2227 * @refs_to_add Number of references to add
2229 * @extent_op Pointer to a structure, holding information necessary when
2230 * updating a tree block's flags
2233 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
2234 struct btrfs_delayed_ref_node *node,
2235 u64 parent, u64 root_objectid,
2236 u64 owner, u64 offset, int refs_to_add,
2237 struct btrfs_delayed_extent_op *extent_op)
2239 struct btrfs_fs_info *fs_info = trans->fs_info;
2240 struct btrfs_path *path;
2241 struct extent_buffer *leaf;
2242 struct btrfs_extent_item *item;
2243 struct btrfs_key key;
2244 u64 bytenr = node->bytenr;
2245 u64 num_bytes = node->num_bytes;
2249 path = btrfs_alloc_path();
2253 path->reada = READA_FORWARD;
2254 path->leave_spinning = 1;
2255 /* this will setup the path even if it fails to insert the back ref */
2256 ret = insert_inline_extent_backref(trans, fs_info, path, bytenr,
2257 num_bytes, parent, root_objectid,
2259 refs_to_add, extent_op);
2260 if ((ret < 0 && ret != -EAGAIN) || !ret)
2264 * Ok we had -EAGAIN which means we didn't have space to insert and
2265 * inline extent ref, so just update the reference count and add a
2268 leaf = path->nodes[0];
2269 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2270 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2271 refs = btrfs_extent_refs(leaf, item);
2272 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
2274 __run_delayed_extent_op(extent_op, leaf, item);
2276 btrfs_mark_buffer_dirty(leaf);
2277 btrfs_release_path(path);
2279 path->reada = READA_FORWARD;
2280 path->leave_spinning = 1;
2281 /* now insert the actual backref */
2282 ret = insert_extent_backref(trans, path, bytenr, parent, root_objectid,
2283 owner, offset, refs_to_add);
2285 btrfs_abort_transaction(trans, ret);
2287 btrfs_free_path(path);
2291 static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
2292 struct btrfs_delayed_ref_node *node,
2293 struct btrfs_delayed_extent_op *extent_op,
2294 int insert_reserved)
2297 struct btrfs_delayed_data_ref *ref;
2298 struct btrfs_key ins;
2303 ins.objectid = node->bytenr;
2304 ins.offset = node->num_bytes;
2305 ins.type = BTRFS_EXTENT_ITEM_KEY;
2307 ref = btrfs_delayed_node_to_data_ref(node);
2308 trace_run_delayed_data_ref(trans->fs_info, node, ref, node->action);
2310 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2311 parent = ref->parent;
2312 ref_root = ref->root;
2314 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2316 flags |= extent_op->flags_to_set;
2317 ret = alloc_reserved_file_extent(trans, parent, ref_root,
2318 flags, ref->objectid,
2321 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2322 ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
2323 ref->objectid, ref->offset,
2324 node->ref_mod, extent_op);
2325 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2326 ret = __btrfs_free_extent(trans, node, parent,
2327 ref_root, ref->objectid,
2328 ref->offset, node->ref_mod,
2336 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2337 struct extent_buffer *leaf,
2338 struct btrfs_extent_item *ei)
2340 u64 flags = btrfs_extent_flags(leaf, ei);
2341 if (extent_op->update_flags) {
2342 flags |= extent_op->flags_to_set;
2343 btrfs_set_extent_flags(leaf, ei, flags);
2346 if (extent_op->update_key) {
2347 struct btrfs_tree_block_info *bi;
2348 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2349 bi = (struct btrfs_tree_block_info *)(ei + 1);
2350 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2354 static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2355 struct btrfs_delayed_ref_head *head,
2356 struct btrfs_delayed_extent_op *extent_op)
2358 struct btrfs_fs_info *fs_info = trans->fs_info;
2359 struct btrfs_key key;
2360 struct btrfs_path *path;
2361 struct btrfs_extent_item *ei;
2362 struct extent_buffer *leaf;
2366 int metadata = !extent_op->is_data;
2371 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2374 path = btrfs_alloc_path();
2378 key.objectid = head->bytenr;
2381 key.type = BTRFS_METADATA_ITEM_KEY;
2382 key.offset = extent_op->level;
2384 key.type = BTRFS_EXTENT_ITEM_KEY;
2385 key.offset = head->num_bytes;
2389 path->reada = READA_FORWARD;
2390 path->leave_spinning = 1;
2391 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 1);
2398 if (path->slots[0] > 0) {
2400 btrfs_item_key_to_cpu(path->nodes[0], &key,
2402 if (key.objectid == head->bytenr &&
2403 key.type == BTRFS_EXTENT_ITEM_KEY &&
2404 key.offset == head->num_bytes)
2408 btrfs_release_path(path);
2411 key.objectid = head->bytenr;
2412 key.offset = head->num_bytes;
2413 key.type = BTRFS_EXTENT_ITEM_KEY;
2422 leaf = path->nodes[0];
2423 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2424 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2425 if (item_size < sizeof(*ei)) {
2426 ret = convert_extent_item_v0(trans, fs_info, path, (u64)-1, 0);
2431 leaf = path->nodes[0];
2432 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2435 BUG_ON(item_size < sizeof(*ei));
2436 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2437 __run_delayed_extent_op(extent_op, leaf, ei);
2439 btrfs_mark_buffer_dirty(leaf);
2441 btrfs_free_path(path);
2445 static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2446 struct btrfs_fs_info *fs_info,
2447 struct btrfs_delayed_ref_node *node,
2448 struct btrfs_delayed_extent_op *extent_op,
2449 int insert_reserved)
2452 struct btrfs_delayed_tree_ref *ref;
2456 ref = btrfs_delayed_node_to_tree_ref(node);
2457 trace_run_delayed_tree_ref(fs_info, node, ref, node->action);
2459 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2460 parent = ref->parent;
2461 ref_root = ref->root;
2463 if (node->ref_mod != 1) {
2465 "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu",
2466 node->bytenr, node->ref_mod, node->action, ref_root,
2470 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2471 BUG_ON(!extent_op || !extent_op->update_flags);
2472 ret = alloc_reserved_tree_block(trans, node, extent_op);
2473 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2474 ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
2475 ref->level, 0, 1, extent_op);
2476 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2477 ret = __btrfs_free_extent(trans, node, parent, ref_root,
2478 ref->level, 0, 1, extent_op);
2485 /* helper function to actually process a single delayed ref entry */
2486 static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2487 struct btrfs_fs_info *fs_info,
2488 struct btrfs_delayed_ref_node *node,
2489 struct btrfs_delayed_extent_op *extent_op,
2490 int insert_reserved)
2494 if (trans->aborted) {
2495 if (insert_reserved)
2496 btrfs_pin_extent(fs_info, node->bytenr,
2497 node->num_bytes, 1);
2501 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2502 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2503 ret = run_delayed_tree_ref(trans, fs_info, node, extent_op,
2505 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2506 node->type == BTRFS_SHARED_DATA_REF_KEY)
2507 ret = run_delayed_data_ref(trans, node, extent_op,
2514 static inline struct btrfs_delayed_ref_node *
2515 select_delayed_ref(struct btrfs_delayed_ref_head *head)
2517 struct btrfs_delayed_ref_node *ref;
2519 if (RB_EMPTY_ROOT(&head->ref_tree))
2523 * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
2524 * This is to prevent a ref count from going down to zero, which deletes
2525 * the extent item from the extent tree, when there still are references
2526 * to add, which would fail because they would not find the extent item.
2528 if (!list_empty(&head->ref_add_list))
2529 return list_first_entry(&head->ref_add_list,
2530 struct btrfs_delayed_ref_node, add_list);
2532 ref = rb_entry(rb_first(&head->ref_tree),
2533 struct btrfs_delayed_ref_node, ref_node);
2534 ASSERT(list_empty(&ref->add_list));
2538 static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
2539 struct btrfs_delayed_ref_head *head)
2541 spin_lock(&delayed_refs->lock);
2542 head->processing = 0;
2543 delayed_refs->num_heads_ready++;
2544 spin_unlock(&delayed_refs->lock);
2545 btrfs_delayed_ref_unlock(head);
2548 static int cleanup_extent_op(struct btrfs_trans_handle *trans,
2549 struct btrfs_fs_info *fs_info,
2550 struct btrfs_delayed_ref_head *head)
2552 struct btrfs_delayed_extent_op *extent_op = head->extent_op;
2557 head->extent_op = NULL;
2558 if (head->must_insert_reserved) {
2559 btrfs_free_delayed_extent_op(extent_op);
2562 spin_unlock(&head->lock);
2563 ret = run_delayed_extent_op(trans, head, extent_op);
2564 btrfs_free_delayed_extent_op(extent_op);
2565 return ret ? ret : 1;
2568 static int cleanup_ref_head(struct btrfs_trans_handle *trans,
2569 struct btrfs_fs_info *fs_info,
2570 struct btrfs_delayed_ref_head *head)
2572 struct btrfs_delayed_ref_root *delayed_refs;
2575 delayed_refs = &trans->transaction->delayed_refs;
2577 ret = cleanup_extent_op(trans, fs_info, head);
2579 unselect_delayed_ref_head(delayed_refs, head);
2580 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
2587 * Need to drop our head ref lock and re-acquire the delayed ref lock
2588 * and then re-check to make sure nobody got added.
2590 spin_unlock(&head->lock);
2591 spin_lock(&delayed_refs->lock);
2592 spin_lock(&head->lock);
2593 if (!RB_EMPTY_ROOT(&head->ref_tree) || head->extent_op) {
2594 spin_unlock(&head->lock);
2595 spin_unlock(&delayed_refs->lock);
2598 delayed_refs->num_heads--;
2599 rb_erase(&head->href_node, &delayed_refs->href_root);
2600 RB_CLEAR_NODE(&head->href_node);
2601 spin_unlock(&head->lock);
2602 spin_unlock(&delayed_refs->lock);
2603 atomic_dec(&delayed_refs->num_entries);
2605 trace_run_delayed_ref_head(fs_info, head, 0);
2607 if (head->total_ref_mod < 0) {
2608 struct btrfs_space_info *space_info;
2612 flags = BTRFS_BLOCK_GROUP_DATA;
2613 else if (head->is_system)
2614 flags = BTRFS_BLOCK_GROUP_SYSTEM;
2616 flags = BTRFS_BLOCK_GROUP_METADATA;
2617 space_info = __find_space_info(fs_info, flags);
2619 percpu_counter_add(&space_info->total_bytes_pinned,
2622 if (head->is_data) {
2623 spin_lock(&delayed_refs->lock);
2624 delayed_refs->pending_csums -= head->num_bytes;
2625 spin_unlock(&delayed_refs->lock);
2629 if (head->must_insert_reserved) {
2630 btrfs_pin_extent(fs_info, head->bytenr,
2631 head->num_bytes, 1);
2632 if (head->is_data) {
2633 ret = btrfs_del_csums(trans, fs_info, head->bytenr,
2638 /* Also free its reserved qgroup space */
2639 btrfs_qgroup_free_delayed_ref(fs_info, head->qgroup_ref_root,
2640 head->qgroup_reserved);
2641 btrfs_delayed_ref_unlock(head);
2642 btrfs_put_delayed_ref_head(head);
2647 * Returns 0 on success or if called with an already aborted transaction.
2648 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2650 static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2653 struct btrfs_fs_info *fs_info = trans->fs_info;
2654 struct btrfs_delayed_ref_root *delayed_refs;
2655 struct btrfs_delayed_ref_node *ref;
2656 struct btrfs_delayed_ref_head *locked_ref = NULL;
2657 struct btrfs_delayed_extent_op *extent_op;
2658 ktime_t start = ktime_get();
2660 unsigned long count = 0;
2661 unsigned long actual_count = 0;
2662 int must_insert_reserved = 0;
2664 delayed_refs = &trans->transaction->delayed_refs;
2670 spin_lock(&delayed_refs->lock);
2671 locked_ref = btrfs_select_ref_head(trans);
2673 spin_unlock(&delayed_refs->lock);
2677 /* grab the lock that says we are going to process
2678 * all the refs for this head */
2679 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2680 spin_unlock(&delayed_refs->lock);
2682 * we may have dropped the spin lock to get the head
2683 * mutex lock, and that might have given someone else
2684 * time to free the head. If that's true, it has been
2685 * removed from our list and we can move on.
2687 if (ret == -EAGAIN) {
2695 * We need to try and merge add/drops of the same ref since we
2696 * can run into issues with relocate dropping the implicit ref
2697 * and then it being added back again before the drop can
2698 * finish. If we merged anything we need to re-loop so we can
2700 * Or we can get node references of the same type that weren't
2701 * merged when created due to bumps in the tree mod seq, and
2702 * we need to merge them to prevent adding an inline extent
2703 * backref before dropping it (triggering a BUG_ON at
2704 * insert_inline_extent_backref()).
2706 spin_lock(&locked_ref->lock);
2707 btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref);
2709 ref = select_delayed_ref(locked_ref);
2711 if (ref && ref->seq &&
2712 btrfs_check_delayed_seq(fs_info, ref->seq)) {
2713 spin_unlock(&locked_ref->lock);
2714 unselect_delayed_ref_head(delayed_refs, locked_ref);
2722 * We're done processing refs in this ref_head, clean everything
2723 * up and move on to the next ref_head.
2726 ret = cleanup_ref_head(trans, fs_info, locked_ref);
2728 /* We dropped our lock, we need to loop. */
2741 rb_erase(&ref->ref_node, &locked_ref->ref_tree);
2742 RB_CLEAR_NODE(&ref->ref_node);
2743 if (!list_empty(&ref->add_list))
2744 list_del(&ref->add_list);
2746 * When we play the delayed ref, also correct the ref_mod on
2749 switch (ref->action) {
2750 case BTRFS_ADD_DELAYED_REF:
2751 case BTRFS_ADD_DELAYED_EXTENT:
2752 locked_ref->ref_mod -= ref->ref_mod;
2754 case BTRFS_DROP_DELAYED_REF:
2755 locked_ref->ref_mod += ref->ref_mod;
2760 atomic_dec(&delayed_refs->num_entries);
2763 * Record the must-insert_reserved flag before we drop the spin
2766 must_insert_reserved = locked_ref->must_insert_reserved;
2767 locked_ref->must_insert_reserved = 0;
2769 extent_op = locked_ref->extent_op;
2770 locked_ref->extent_op = NULL;
2771 spin_unlock(&locked_ref->lock);
2773 ret = run_one_delayed_ref(trans, fs_info, ref, extent_op,
2774 must_insert_reserved);
2776 btrfs_free_delayed_extent_op(extent_op);
2778 unselect_delayed_ref_head(delayed_refs, locked_ref);
2779 btrfs_put_delayed_ref(ref);
2780 btrfs_debug(fs_info, "run_one_delayed_ref returned %d",
2785 btrfs_put_delayed_ref(ref);
2791 * We don't want to include ref heads since we can have empty ref heads
2792 * and those will drastically skew our runtime down since we just do
2793 * accounting, no actual extent tree updates.
2795 if (actual_count > 0) {
2796 u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
2800 * We weigh the current average higher than our current runtime
2801 * to avoid large swings in the average.
2803 spin_lock(&delayed_refs->lock);
2804 avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
2805 fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */
2806 spin_unlock(&delayed_refs->lock);
2811 #ifdef SCRAMBLE_DELAYED_REFS
2813 * Normally delayed refs get processed in ascending bytenr order. This
2814 * correlates in most cases to the order added. To expose dependencies on this
2815 * order, we start to process the tree in the middle instead of the beginning
2817 static u64 find_middle(struct rb_root *root)
2819 struct rb_node *n = root->rb_node;
2820 struct btrfs_delayed_ref_node *entry;
2823 u64 first = 0, last = 0;
2827 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2828 first = entry->bytenr;
2832 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2833 last = entry->bytenr;
2838 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2839 WARN_ON(!entry->in_tree);
2841 middle = entry->bytenr;
2854 static inline u64 heads_to_leaves(struct btrfs_fs_info *fs_info, u64 heads)
2858 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2859 sizeof(struct btrfs_extent_inline_ref));
2860 if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2861 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2864 * We don't ever fill up leaves all the way so multiply by 2 just to be
2865 * closer to what we're really going to want to use.
2867 return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info));
2871 * Takes the number of bytes to be csumm'ed and figures out how many leaves it
2872 * would require to store the csums for that many bytes.
2874 u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes)
2877 u64 num_csums_per_leaf;
2880 csum_size = BTRFS_MAX_ITEM_SIZE(fs_info);
2881 num_csums_per_leaf = div64_u64(csum_size,
2882 (u64)btrfs_super_csum_size(fs_info->super_copy));
2883 num_csums = div64_u64(csum_bytes, fs_info->sectorsize);
2884 num_csums += num_csums_per_leaf - 1;
2885 num_csums = div64_u64(num_csums, num_csums_per_leaf);
2889 int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
2890 struct btrfs_fs_info *fs_info)
2892 struct btrfs_block_rsv *global_rsv;
2893 u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
2894 u64 csum_bytes = trans->transaction->delayed_refs.pending_csums;
2895 unsigned int num_dirty_bgs = trans->transaction->num_dirty_bgs;
2896 u64 num_bytes, num_dirty_bgs_bytes;
2899 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
2900 num_heads = heads_to_leaves(fs_info, num_heads);
2902 num_bytes += (num_heads - 1) * fs_info->nodesize;
2904 num_bytes += btrfs_csum_bytes_to_leaves(fs_info, csum_bytes) *
2906 num_dirty_bgs_bytes = btrfs_calc_trans_metadata_size(fs_info,
2908 global_rsv = &fs_info->global_block_rsv;
2911 * If we can't allocate any more chunks lets make sure we have _lots_ of
2912 * wiggle room since running delayed refs can create more delayed refs.
2914 if (global_rsv->space_info->full) {
2915 num_dirty_bgs_bytes <<= 1;
2919 spin_lock(&global_rsv->lock);
2920 if (global_rsv->reserved <= num_bytes + num_dirty_bgs_bytes)
2922 spin_unlock(&global_rsv->lock);
2926 int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
2927 struct btrfs_fs_info *fs_info)
2930 atomic_read(&trans->transaction->delayed_refs.num_entries);
2935 avg_runtime = fs_info->avg_delayed_ref_runtime;
2936 val = num_entries * avg_runtime;
2937 if (val >= NSEC_PER_SEC)
2939 if (val >= NSEC_PER_SEC / 2)
2942 return btrfs_check_space_for_delayed_refs(trans, fs_info);
2945 struct async_delayed_refs {
2946 struct btrfs_root *root;
2951 struct completion wait;
2952 struct btrfs_work work;
2955 static inline struct async_delayed_refs *
2956 to_async_delayed_refs(struct btrfs_work *work)
2958 return container_of(work, struct async_delayed_refs, work);
2961 static void delayed_ref_async_start(struct btrfs_work *work)
2963 struct async_delayed_refs *async = to_async_delayed_refs(work);
2964 struct btrfs_trans_handle *trans;
2965 struct btrfs_fs_info *fs_info = async->root->fs_info;
2968 /* if the commit is already started, we don't need to wait here */
2969 if (btrfs_transaction_blocked(fs_info))
2972 trans = btrfs_join_transaction(async->root);
2973 if (IS_ERR(trans)) {
2974 async->error = PTR_ERR(trans);
2979 * trans->sync means that when we call end_transaction, we won't
2980 * wait on delayed refs
2984 /* Don't bother flushing if we got into a different transaction */
2985 if (trans->transid > async->transid)
2988 ret = btrfs_run_delayed_refs(trans, async->count);
2992 ret = btrfs_end_transaction(trans);
2993 if (ret && !async->error)
2997 complete(&async->wait);
3002 int btrfs_async_run_delayed_refs(struct btrfs_fs_info *fs_info,
3003 unsigned long count, u64 transid, int wait)
3005 struct async_delayed_refs *async;
3008 async = kmalloc(sizeof(*async), GFP_NOFS);
3012 async->root = fs_info->tree_root;
3013 async->count = count;
3015 async->transid = transid;
3020 init_completion(&async->wait);
3022 btrfs_init_work(&async->work, btrfs_extent_refs_helper,
3023 delayed_ref_async_start, NULL, NULL);
3025 btrfs_queue_work(fs_info->extent_workers, &async->work);
3028 wait_for_completion(&async->wait);
3037 * this starts processing the delayed reference count updates and
3038 * extent insertions we have queued up so far. count can be
3039 * 0, which means to process everything in the tree at the start
3040 * of the run (but not newly added entries), or it can be some target
3041 * number you'd like to process.
3043 * Returns 0 on success or if called with an aborted transaction
3044 * Returns <0 on error and aborts the transaction
3046 int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
3047 unsigned long count)
3049 struct btrfs_fs_info *fs_info = trans->fs_info;
3050 struct rb_node *node;
3051 struct btrfs_delayed_ref_root *delayed_refs;
3052 struct btrfs_delayed_ref_head *head;
3054 int run_all = count == (unsigned long)-1;
3055 bool can_flush_pending_bgs = trans->can_flush_pending_bgs;
3057 /* We'll clean this up in btrfs_cleanup_transaction */
3061 if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags))
3064 delayed_refs = &trans->transaction->delayed_refs;
3066 count = atomic_read(&delayed_refs->num_entries) * 2;
3069 #ifdef SCRAMBLE_DELAYED_REFS
3070 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
3072 trans->can_flush_pending_bgs = false;
3073 ret = __btrfs_run_delayed_refs(trans, count);
3075 btrfs_abort_transaction(trans, ret);
3080 if (!list_empty(&trans->new_bgs))
3081 btrfs_create_pending_block_groups(trans);
3083 spin_lock(&delayed_refs->lock);
3084 node = rb_first(&delayed_refs->href_root);
3086 spin_unlock(&delayed_refs->lock);
3089 head = rb_entry(node, struct btrfs_delayed_ref_head,
3091 refcount_inc(&head->refs);
3092 spin_unlock(&delayed_refs->lock);
3094 /* Mutex was contended, block until it's released and retry. */
3095 mutex_lock(&head->mutex);
3096 mutex_unlock(&head->mutex);
3098 btrfs_put_delayed_ref_head(head);
3103 trans->can_flush_pending_bgs = can_flush_pending_bgs;
3107 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
3108 struct btrfs_fs_info *fs_info,
3109 u64 bytenr, u64 num_bytes, u64 flags,
3110 int level, int is_data)
3112 struct btrfs_delayed_extent_op *extent_op;
3115 extent_op = btrfs_alloc_delayed_extent_op();
3119 extent_op->flags_to_set = flags;
3120 extent_op->update_flags = true;
3121 extent_op->update_key = false;
3122 extent_op->is_data = is_data ? true : false;
3123 extent_op->level = level;
3125 ret = btrfs_add_delayed_extent_op(fs_info, trans, bytenr,
3126 num_bytes, extent_op);
3128 btrfs_free_delayed_extent_op(extent_op);
3132 static noinline int check_delayed_ref(struct btrfs_root *root,
3133 struct btrfs_path *path,
3134 u64 objectid, u64 offset, u64 bytenr)
3136 struct btrfs_delayed_ref_head *head;
3137 struct btrfs_delayed_ref_node *ref;
3138 struct btrfs_delayed_data_ref *data_ref;
3139 struct btrfs_delayed_ref_root *delayed_refs;
3140 struct btrfs_transaction *cur_trans;
3141 struct rb_node *node;
3144 spin_lock(&root->fs_info->trans_lock);
3145 cur_trans = root->fs_info->running_transaction;
3147 refcount_inc(&cur_trans->use_count);
3148 spin_unlock(&root->fs_info->trans_lock);
3152 delayed_refs = &cur_trans->delayed_refs;
3153 spin_lock(&delayed_refs->lock);
3154 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
3156 spin_unlock(&delayed_refs->lock);
3157 btrfs_put_transaction(cur_trans);
3161 if (!mutex_trylock(&head->mutex)) {
3162 refcount_inc(&head->refs);
3163 spin_unlock(&delayed_refs->lock);
3165 btrfs_release_path(path);
3168 * Mutex was contended, block until it's released and let
3171 mutex_lock(&head->mutex);
3172 mutex_unlock(&head->mutex);
3173 btrfs_put_delayed_ref_head(head);
3174 btrfs_put_transaction(cur_trans);
3177 spin_unlock(&delayed_refs->lock);
3179 spin_lock(&head->lock);
3181 * XXX: We should replace this with a proper search function in the
3184 for (node = rb_first(&head->ref_tree); node; node = rb_next(node)) {
3185 ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
3186 /* If it's a shared ref we know a cross reference exists */
3187 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
3192 data_ref = btrfs_delayed_node_to_data_ref(ref);
3195 * If our ref doesn't match the one we're currently looking at
3196 * then we have a cross reference.
3198 if (data_ref->root != root->root_key.objectid ||
3199 data_ref->objectid != objectid ||
3200 data_ref->offset != offset) {
3205 spin_unlock(&head->lock);
3206 mutex_unlock(&head->mutex);
3207 btrfs_put_transaction(cur_trans);
3211 static noinline int check_committed_ref(struct btrfs_root *root,
3212 struct btrfs_path *path,
3213 u64 objectid, u64 offset, u64 bytenr)
3215 struct btrfs_fs_info *fs_info = root->fs_info;
3216 struct btrfs_root *extent_root = fs_info->extent_root;
3217 struct extent_buffer *leaf;
3218 struct btrfs_extent_data_ref *ref;
3219 struct btrfs_extent_inline_ref *iref;
3220 struct btrfs_extent_item *ei;
3221 struct btrfs_key key;
3226 key.objectid = bytenr;
3227 key.offset = (u64)-1;
3228 key.type = BTRFS_EXTENT_ITEM_KEY;
3230 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
3233 BUG_ON(ret == 0); /* Corruption */
3236 if (path->slots[0] == 0)
3240 leaf = path->nodes[0];
3241 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3243 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
3247 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3248 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3249 if (item_size < sizeof(*ei)) {
3250 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
3254 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
3256 if (item_size != sizeof(*ei) +
3257 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
3260 if (btrfs_extent_generation(leaf, ei) <=
3261 btrfs_root_last_snapshot(&root->root_item))
3264 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
3266 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
3267 if (type != BTRFS_EXTENT_DATA_REF_KEY)
3270 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
3271 if (btrfs_extent_refs(leaf, ei) !=
3272 btrfs_extent_data_ref_count(leaf, ref) ||
3273 btrfs_extent_data_ref_root(leaf, ref) !=
3274 root->root_key.objectid ||
3275 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
3276 btrfs_extent_data_ref_offset(leaf, ref) != offset)
3284 int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset,
3287 struct btrfs_path *path;
3291 path = btrfs_alloc_path();
3296 ret = check_committed_ref(root, path, objectid,
3298 if (ret && ret != -ENOENT)
3301 ret2 = check_delayed_ref(root, path, objectid,
3303 } while (ret2 == -EAGAIN);
3305 if (ret2 && ret2 != -ENOENT) {
3310 if (ret != -ENOENT || ret2 != -ENOENT)
3313 btrfs_free_path(path);
3314 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
3319 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
3320 struct btrfs_root *root,
3321 struct extent_buffer *buf,
3322 int full_backref, int inc)
3324 struct btrfs_fs_info *fs_info = root->fs_info;
3330 struct btrfs_key key;
3331 struct btrfs_file_extent_item *fi;
3335 int (*process_func)(struct btrfs_trans_handle *,
3336 struct btrfs_root *,
3337 u64, u64, u64, u64, u64, u64);
3340 if (btrfs_is_testing(fs_info))
3343 ref_root = btrfs_header_owner(buf);
3344 nritems = btrfs_header_nritems(buf);
3345 level = btrfs_header_level(buf);
3347 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
3351 process_func = btrfs_inc_extent_ref;
3353 process_func = btrfs_free_extent;
3356 parent = buf->start;
3360 for (i = 0; i < nritems; i++) {
3362 btrfs_item_key_to_cpu(buf, &key, i);
3363 if (key.type != BTRFS_EXTENT_DATA_KEY)
3365 fi = btrfs_item_ptr(buf, i,
3366 struct btrfs_file_extent_item);
3367 if (btrfs_file_extent_type(buf, fi) ==
3368 BTRFS_FILE_EXTENT_INLINE)
3370 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
3374 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
3375 key.offset -= btrfs_file_extent_offset(buf, fi);
3376 ret = process_func(trans, root, bytenr, num_bytes,
3377 parent, ref_root, key.objectid,
3382 bytenr = btrfs_node_blockptr(buf, i);
3383 num_bytes = fs_info->nodesize;
3384 ret = process_func(trans, root, bytenr, num_bytes,
3385 parent, ref_root, level - 1, 0);
3395 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3396 struct extent_buffer *buf, int full_backref)
3398 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
3401 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3402 struct extent_buffer *buf, int full_backref)
3404 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
3407 static int write_one_cache_group(struct btrfs_trans_handle *trans,
3408 struct btrfs_fs_info *fs_info,
3409 struct btrfs_path *path,
3410 struct btrfs_block_group_cache *cache)
3413 struct btrfs_root *extent_root = fs_info->extent_root;
3415 struct extent_buffer *leaf;
3417 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
3424 leaf = path->nodes[0];
3425 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
3426 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
3427 btrfs_mark_buffer_dirty(leaf);
3429 btrfs_release_path(path);
3434 static struct btrfs_block_group_cache *
3435 next_block_group(struct btrfs_fs_info *fs_info,
3436 struct btrfs_block_group_cache *cache)
3438 struct rb_node *node;
3440 spin_lock(&fs_info->block_group_cache_lock);
3442 /* If our block group was removed, we need a full search. */
3443 if (RB_EMPTY_NODE(&cache->cache_node)) {
3444 const u64 next_bytenr = cache->key.objectid + cache->key.offset;
3446 spin_unlock(&fs_info->block_group_cache_lock);
3447 btrfs_put_block_group(cache);
3448 cache = btrfs_lookup_first_block_group(fs_info, next_bytenr); return cache;
3450 node = rb_next(&cache->cache_node);
3451 btrfs_put_block_group(cache);
3453 cache = rb_entry(node, struct btrfs_block_group_cache,
3455 btrfs_get_block_group(cache);
3458 spin_unlock(&fs_info->block_group_cache_lock);
3462 static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3463 struct btrfs_trans_handle *trans,
3464 struct btrfs_path *path)
3466 struct btrfs_fs_info *fs_info = block_group->fs_info;
3467 struct btrfs_root *root = fs_info->tree_root;
3468 struct inode *inode = NULL;
3469 struct extent_changeset *data_reserved = NULL;
3471 int dcs = BTRFS_DC_ERROR;
3477 * If this block group is smaller than 100 megs don't bother caching the
3480 if (block_group->key.offset < (100 * SZ_1M)) {
3481 spin_lock(&block_group->lock);
3482 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
3483 spin_unlock(&block_group->lock);
3490 inode = lookup_free_space_inode(fs_info, block_group, path);
3491 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3492 ret = PTR_ERR(inode);
3493 btrfs_release_path(path);
3497 if (IS_ERR(inode)) {
3501 if (block_group->ro)
3504 ret = create_free_space_inode(fs_info, trans, block_group,
3512 * We want to set the generation to 0, that way if anything goes wrong
3513 * from here on out we know not to trust this cache when we load up next
3516 BTRFS_I(inode)->generation = 0;
3517 ret = btrfs_update_inode(trans, root, inode);
3520 * So theoretically we could recover from this, simply set the
3521 * super cache generation to 0 so we know to invalidate the
3522 * cache, but then we'd have to keep track of the block groups
3523 * that fail this way so we know we _have_ to reset this cache
3524 * before the next commit or risk reading stale cache. So to
3525 * limit our exposure to horrible edge cases lets just abort the
3526 * transaction, this only happens in really bad situations
3529 btrfs_abort_transaction(trans, ret);
3534 /* We've already setup this transaction, go ahead and exit */
3535 if (block_group->cache_generation == trans->transid &&
3536 i_size_read(inode)) {
3537 dcs = BTRFS_DC_SETUP;
3541 if (i_size_read(inode) > 0) {
3542 ret = btrfs_check_trunc_cache_free_space(fs_info,
3543 &fs_info->global_block_rsv);
3547 ret = btrfs_truncate_free_space_cache(trans, NULL, inode);