1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2009 Oracle. All rights reserved.
6 #include <linux/sched.h>
7 #include <linux/slab.h>
8 #include <linux/sort.h>
10 #include "delayed-ref.h"
11 #include "transaction.h"
13 #include "space-info.h"
15 struct kmem_cache *btrfs_delayed_ref_head_cachep;
16 struct kmem_cache *btrfs_delayed_tree_ref_cachep;
17 struct kmem_cache *btrfs_delayed_data_ref_cachep;
18 struct kmem_cache *btrfs_delayed_extent_op_cachep;
20 * delayed back reference update tracking. For subvolume trees
21 * we queue up extent allocations and backref maintenance for
22 * delayed processing. This avoids deep call chains where we
23 * add extents in the middle of btrfs_search_slot, and it allows
24 * us to buffer up frequently modified backrefs in an rb tree instead
25 * of hammering updates on the extent allocation tree.
28 bool btrfs_check_space_for_delayed_refs(struct btrfs_fs_info *fs_info)
30 struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv;
31 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
35 spin_lock(&global_rsv->lock);
36 reserved = global_rsv->reserved;
37 spin_unlock(&global_rsv->lock);
40 * Since the global reserve is just kind of magic we don't really want
41 * to rely on it to save our bacon, so if our size is more than the
42 * delayed_refs_rsv and the global rsv then it's time to think about
45 spin_lock(&delayed_refs_rsv->lock);
46 reserved += delayed_refs_rsv->reserved;
47 if (delayed_refs_rsv->size >= reserved)
49 spin_unlock(&delayed_refs_rsv->lock);
53 int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans)
56 atomic_read(&trans->transaction->delayed_refs.num_entries);
61 avg_runtime = trans->fs_info->avg_delayed_ref_runtime;
62 val = num_entries * avg_runtime;
63 if (val >= NSEC_PER_SEC)
65 if (val >= NSEC_PER_SEC / 2)
68 return btrfs_check_space_for_delayed_refs(trans->fs_info);
72 * Release a ref head's reservation
74 * @fs_info: the filesystem
75 * @nr: number of items to drop
77 * This drops the delayed ref head's count from the delayed refs rsv and frees
78 * any excess reservation we had.
80 void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr)
82 struct btrfs_block_rsv *block_rsv = &fs_info->delayed_refs_rsv;
83 u64 num_bytes = btrfs_calc_insert_metadata_size(fs_info, nr);
86 released = btrfs_block_rsv_release(fs_info, block_rsv, num_bytes, NULL);
88 trace_btrfs_space_reservation(fs_info, "delayed_refs_rsv",
93 * btrfs_update_delayed_refs_rsv - adjust the size of the delayed refs rsv
94 * @trans - the trans that may have generated delayed refs
96 * This is to be called anytime we may have adjusted trans->delayed_ref_updates,
97 * it'll calculate the additional size and add it to the delayed_refs_rsv.
99 void btrfs_update_delayed_refs_rsv(struct btrfs_trans_handle *trans)
101 struct btrfs_fs_info *fs_info = trans->fs_info;
102 struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_refs_rsv;
105 if (!trans->delayed_ref_updates)
108 num_bytes = btrfs_calc_insert_metadata_size(fs_info,
109 trans->delayed_ref_updates);
110 spin_lock(&delayed_rsv->lock);
111 delayed_rsv->size += num_bytes;
112 delayed_rsv->full = 0;
113 spin_unlock(&delayed_rsv->lock);
114 trans->delayed_ref_updates = 0;
118 * Transfer bytes to our delayed refs rsv
120 * @fs_info: the filesystem
121 * @src: source block rsv to transfer from
122 * @num_bytes: number of bytes to transfer
124 * This transfers up to the num_bytes amount from the src rsv to the
125 * delayed_refs_rsv. Any extra bytes are returned to the space info.
127 void btrfs_migrate_to_delayed_refs_rsv(struct btrfs_fs_info *fs_info,
128 struct btrfs_block_rsv *src,
131 struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv;
134 spin_lock(&src->lock);
135 src->reserved -= num_bytes;
136 src->size -= num_bytes;
137 spin_unlock(&src->lock);
139 spin_lock(&delayed_refs_rsv->lock);
140 if (delayed_refs_rsv->size > delayed_refs_rsv->reserved) {
141 u64 delta = delayed_refs_rsv->size -
142 delayed_refs_rsv->reserved;
143 if (num_bytes > delta) {
144 to_free = num_bytes - delta;
153 delayed_refs_rsv->reserved += num_bytes;
154 if (delayed_refs_rsv->reserved >= delayed_refs_rsv->size)
155 delayed_refs_rsv->full = 1;
156 spin_unlock(&delayed_refs_rsv->lock);
159 trace_btrfs_space_reservation(fs_info, "delayed_refs_rsv",
162 btrfs_space_info_free_bytes_may_use(fs_info,
163 delayed_refs_rsv->space_info, to_free);
167 * Refill based on our delayed refs usage
169 * @fs_info: the filesystem
170 * @flush: control how we can flush for this reservation.
172 * This will refill the delayed block_rsv up to 1 items size worth of space and
173 * will return -ENOSPC if we can't make the reservation.
175 int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info,
176 enum btrfs_reserve_flush_enum flush)
178 struct btrfs_block_rsv *block_rsv = &fs_info->delayed_refs_rsv;
179 u64 limit = btrfs_calc_insert_metadata_size(fs_info, 1);
183 spin_lock(&block_rsv->lock);
184 if (block_rsv->reserved < block_rsv->size) {
185 num_bytes = block_rsv->size - block_rsv->reserved;
186 num_bytes = min(num_bytes, limit);
188 spin_unlock(&block_rsv->lock);
193 ret = btrfs_reserve_metadata_bytes(fs_info->extent_root, block_rsv,
197 btrfs_block_rsv_add_bytes(block_rsv, num_bytes, 0);
198 trace_btrfs_space_reservation(fs_info, "delayed_refs_rsv",
204 * compare two delayed tree backrefs with same bytenr and type
206 static int comp_tree_refs(struct btrfs_delayed_tree_ref *ref1,
207 struct btrfs_delayed_tree_ref *ref2)
209 if (ref1->node.type == BTRFS_TREE_BLOCK_REF_KEY) {
210 if (ref1->root < ref2->root)
212 if (ref1->root > ref2->root)
215 if (ref1->parent < ref2->parent)
217 if (ref1->parent > ref2->parent)
224 * compare two delayed data backrefs with same bytenr and type
226 static int comp_data_refs(struct btrfs_delayed_data_ref *ref1,
227 struct btrfs_delayed_data_ref *ref2)
229 if (ref1->node.type == BTRFS_EXTENT_DATA_REF_KEY) {
230 if (ref1->root < ref2->root)
232 if (ref1->root > ref2->root)
234 if (ref1->objectid < ref2->objectid)
236 if (ref1->objectid > ref2->objectid)
238 if (ref1->offset < ref2->offset)
240 if (ref1->offset > ref2->offset)
243 if (ref1->parent < ref2->parent)
245 if (ref1->parent > ref2->parent)
251 static int comp_refs(struct btrfs_delayed_ref_node *ref1,
252 struct btrfs_delayed_ref_node *ref2,
257 if (ref1->type < ref2->type)
259 if (ref1->type > ref2->type)
261 if (ref1->type == BTRFS_TREE_BLOCK_REF_KEY ||
262 ref1->type == BTRFS_SHARED_BLOCK_REF_KEY)
263 ret = comp_tree_refs(btrfs_delayed_node_to_tree_ref(ref1),
264 btrfs_delayed_node_to_tree_ref(ref2));
266 ret = comp_data_refs(btrfs_delayed_node_to_data_ref(ref1),
267 btrfs_delayed_node_to_data_ref(ref2));
271 if (ref1->seq < ref2->seq)
273 if (ref1->seq > ref2->seq)
279 /* insert a new ref to head ref rbtree */
280 static struct btrfs_delayed_ref_head *htree_insert(struct rb_root_cached *root,
281 struct rb_node *node)
283 struct rb_node **p = &root->rb_root.rb_node;
284 struct rb_node *parent_node = NULL;
285 struct btrfs_delayed_ref_head *entry;
286 struct btrfs_delayed_ref_head *ins;
288 bool leftmost = true;
290 ins = rb_entry(node, struct btrfs_delayed_ref_head, href_node);
291 bytenr = ins->bytenr;
294 entry = rb_entry(parent_node, struct btrfs_delayed_ref_head,
297 if (bytenr < entry->bytenr) {
299 } else if (bytenr > entry->bytenr) {
307 rb_link_node(node, parent_node, p);
308 rb_insert_color_cached(node, root, leftmost);
312 static struct btrfs_delayed_ref_node* tree_insert(struct rb_root_cached *root,
313 struct btrfs_delayed_ref_node *ins)
315 struct rb_node **p = &root->rb_root.rb_node;
316 struct rb_node *node = &ins->ref_node;
317 struct rb_node *parent_node = NULL;
318 struct btrfs_delayed_ref_node *entry;
319 bool leftmost = true;
325 entry = rb_entry(parent_node, struct btrfs_delayed_ref_node,
327 comp = comp_refs(ins, entry, true);
330 } else if (comp > 0) {
338 rb_link_node(node, parent_node, p);
339 rb_insert_color_cached(node, root, leftmost);
343 static struct btrfs_delayed_ref_head *find_first_ref_head(
344 struct btrfs_delayed_ref_root *dr)
347 struct btrfs_delayed_ref_head *entry;
349 n = rb_first_cached(&dr->href_root);
353 entry = rb_entry(n, struct btrfs_delayed_ref_head, href_node);
359 * Find a head entry based on bytenr. This returns the delayed ref head if it
360 * was able to find one, or NULL if nothing was in that spot. If return_bigger
361 * is given, the next bigger entry is returned if no exact match is found.
363 static struct btrfs_delayed_ref_head *find_ref_head(
364 struct btrfs_delayed_ref_root *dr, u64 bytenr,
367 struct rb_root *root = &dr->href_root.rb_root;
369 struct btrfs_delayed_ref_head *entry;
374 entry = rb_entry(n, struct btrfs_delayed_ref_head, href_node);
376 if (bytenr < entry->bytenr)
378 else if (bytenr > entry->bytenr)
383 if (entry && return_bigger) {
384 if (bytenr > entry->bytenr) {
385 n = rb_next(&entry->href_node);
388 entry = rb_entry(n, struct btrfs_delayed_ref_head,
396 int btrfs_delayed_ref_lock(struct btrfs_delayed_ref_root *delayed_refs,
397 struct btrfs_delayed_ref_head *head)
399 lockdep_assert_held(&delayed_refs->lock);
400 if (mutex_trylock(&head->mutex))
403 refcount_inc(&head->refs);
404 spin_unlock(&delayed_refs->lock);
406 mutex_lock(&head->mutex);
407 spin_lock(&delayed_refs->lock);
408 if (RB_EMPTY_NODE(&head->href_node)) {
409 mutex_unlock(&head->mutex);
410 btrfs_put_delayed_ref_head(head);
413 btrfs_put_delayed_ref_head(head);
417 static inline void drop_delayed_ref(struct btrfs_trans_handle *trans,
418 struct btrfs_delayed_ref_root *delayed_refs,
419 struct btrfs_delayed_ref_head *head,
420 struct btrfs_delayed_ref_node *ref)
422 lockdep_assert_held(&head->lock);
423 rb_erase_cached(&ref->ref_node, &head->ref_tree);
424 RB_CLEAR_NODE(&ref->ref_node);
425 if (!list_empty(&ref->add_list))
426 list_del(&ref->add_list);
428 btrfs_put_delayed_ref(ref);
429 atomic_dec(&delayed_refs->num_entries);
432 static bool merge_ref(struct btrfs_trans_handle *trans,
433 struct btrfs_delayed_ref_root *delayed_refs,
434 struct btrfs_delayed_ref_head *head,
435 struct btrfs_delayed_ref_node *ref,
438 struct btrfs_delayed_ref_node *next;
439 struct rb_node *node = rb_next(&ref->ref_node);
442 while (!done && node) {
445 next = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
446 node = rb_next(node);
447 if (seq && next->seq >= seq)
449 if (comp_refs(ref, next, false))
452 if (ref->action == next->action) {
455 if (ref->ref_mod < next->ref_mod) {
459 mod = -next->ref_mod;
462 drop_delayed_ref(trans, delayed_refs, head, next);
464 if (ref->ref_mod == 0) {
465 drop_delayed_ref(trans, delayed_refs, head, ref);
469 * Can't have multiples of the same ref on a tree block.
471 WARN_ON(ref->type == BTRFS_TREE_BLOCK_REF_KEY ||
472 ref->type == BTRFS_SHARED_BLOCK_REF_KEY);
479 void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
480 struct btrfs_delayed_ref_root *delayed_refs,
481 struct btrfs_delayed_ref_head *head)
483 struct btrfs_fs_info *fs_info = trans->fs_info;
484 struct btrfs_delayed_ref_node *ref;
485 struct rb_node *node;
488 lockdep_assert_held(&head->lock);
490 if (RB_EMPTY_ROOT(&head->ref_tree.rb_root))
493 /* We don't have too many refs to merge for data. */
497 read_lock(&fs_info->tree_mod_log_lock);
498 if (!list_empty(&fs_info->tree_mod_seq_list)) {
499 struct seq_list *elem;
501 elem = list_first_entry(&fs_info->tree_mod_seq_list,
502 struct seq_list, list);
505 read_unlock(&fs_info->tree_mod_log_lock);
508 for (node = rb_first_cached(&head->ref_tree); node;
509 node = rb_next(node)) {
510 ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
511 if (seq && ref->seq >= seq)
513 if (merge_ref(trans, delayed_refs, head, ref, seq))
518 int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq)
520 struct seq_list *elem;
523 read_lock(&fs_info->tree_mod_log_lock);
524 if (!list_empty(&fs_info->tree_mod_seq_list)) {
525 elem = list_first_entry(&fs_info->tree_mod_seq_list,
526 struct seq_list, list);
527 if (seq >= elem->seq) {
529 "holding back delayed_ref %#x.%x, lowest is %#x.%x",
530 (u32)(seq >> 32), (u32)seq,
531 (u32)(elem->seq >> 32), (u32)elem->seq);
536 read_unlock(&fs_info->tree_mod_log_lock);
540 struct btrfs_delayed_ref_head *btrfs_select_ref_head(
541 struct btrfs_delayed_ref_root *delayed_refs)
543 struct btrfs_delayed_ref_head *head;
546 head = find_ref_head(delayed_refs, delayed_refs->run_delayed_start,
548 if (!head && delayed_refs->run_delayed_start != 0) {
549 delayed_refs->run_delayed_start = 0;
550 head = find_first_ref_head(delayed_refs);
555 while (head->processing) {
556 struct rb_node *node;
558 node = rb_next(&head->href_node);
560 if (delayed_refs->run_delayed_start == 0)
562 delayed_refs->run_delayed_start = 0;
565 head = rb_entry(node, struct btrfs_delayed_ref_head,
569 head->processing = 1;
570 WARN_ON(delayed_refs->num_heads_ready == 0);
571 delayed_refs->num_heads_ready--;
572 delayed_refs->run_delayed_start = head->bytenr +
577 void btrfs_delete_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
578 struct btrfs_delayed_ref_head *head)
580 lockdep_assert_held(&delayed_refs->lock);
581 lockdep_assert_held(&head->lock);
583 rb_erase_cached(&head->href_node, &delayed_refs->href_root);
584 RB_CLEAR_NODE(&head->href_node);
585 atomic_dec(&delayed_refs->num_entries);
586 delayed_refs->num_heads--;
587 if (head->processing == 0)
588 delayed_refs->num_heads_ready--;
592 * Helper to insert the ref_node to the tail or merge with tail.
594 * Return 0 for insert.
595 * Return >0 for merge.
597 static int insert_delayed_ref(struct btrfs_trans_handle *trans,
598 struct btrfs_delayed_ref_root *root,
599 struct btrfs_delayed_ref_head *href,
600 struct btrfs_delayed_ref_node *ref)
602 struct btrfs_delayed_ref_node *exist;
606 spin_lock(&href->lock);
607 exist = tree_insert(&href->ref_tree, ref);
611 /* Now we are sure we can merge */
613 if (exist->action == ref->action) {
616 /* Need to change action */
617 if (exist->ref_mod < ref->ref_mod) {
618 exist->action = ref->action;
619 mod = -exist->ref_mod;
620 exist->ref_mod = ref->ref_mod;
621 if (ref->action == BTRFS_ADD_DELAYED_REF)
622 list_add_tail(&exist->add_list,
623 &href->ref_add_list);
624 else if (ref->action == BTRFS_DROP_DELAYED_REF) {
625 ASSERT(!list_empty(&exist->add_list));
626 list_del(&exist->add_list);
633 exist->ref_mod += mod;
635 /* remove existing tail if its ref_mod is zero */
636 if (exist->ref_mod == 0)
637 drop_delayed_ref(trans, root, href, exist);
638 spin_unlock(&href->lock);
641 if (ref->action == BTRFS_ADD_DELAYED_REF)
642 list_add_tail(&ref->add_list, &href->ref_add_list);
643 atomic_inc(&root->num_entries);
644 spin_unlock(&href->lock);
649 * helper function to update the accounting in the head ref
650 * existing and update must have the same bytenr
652 static noinline void update_existing_head_ref(struct btrfs_trans_handle *trans,
653 struct btrfs_delayed_ref_head *existing,
654 struct btrfs_delayed_ref_head *update)
656 struct btrfs_delayed_ref_root *delayed_refs =
657 &trans->transaction->delayed_refs;
658 struct btrfs_fs_info *fs_info = trans->fs_info;
659 u64 flags = btrfs_ref_head_to_space_flags(existing);
662 BUG_ON(existing->is_data != update->is_data);
664 spin_lock(&existing->lock);
665 if (update->must_insert_reserved) {
666 /* if the extent was freed and then
667 * reallocated before the delayed ref
668 * entries were processed, we can end up
669 * with an existing head ref without
670 * the must_insert_reserved flag set.
673 existing->must_insert_reserved = update->must_insert_reserved;
676 * update the num_bytes so we make sure the accounting
679 existing->num_bytes = update->num_bytes;
683 if (update->extent_op) {
684 if (!existing->extent_op) {
685 existing->extent_op = update->extent_op;
687 if (update->extent_op->update_key) {
688 memcpy(&existing->extent_op->key,
689 &update->extent_op->key,
690 sizeof(update->extent_op->key));
691 existing->extent_op->update_key = true;
693 if (update->extent_op->update_flags) {
694 existing->extent_op->flags_to_set |=
695 update->extent_op->flags_to_set;
696 existing->extent_op->update_flags = true;
698 btrfs_free_delayed_extent_op(update->extent_op);
702 * update the reference mod on the head to reflect this new operation,
703 * only need the lock for this case cause we could be processing it
704 * currently, for refs we just added we know we're a-ok.
706 old_ref_mod = existing->total_ref_mod;
707 existing->ref_mod += update->ref_mod;
708 existing->total_ref_mod += update->ref_mod;
711 * If we are going to from a positive ref mod to a negative or vice
712 * versa we need to make sure to adjust pending_csums accordingly.
714 if (existing->is_data) {
716 btrfs_csum_bytes_to_leaves(fs_info,
717 existing->num_bytes);
719 if (existing->total_ref_mod >= 0 && old_ref_mod < 0) {
720 delayed_refs->pending_csums -= existing->num_bytes;
721 btrfs_delayed_refs_rsv_release(fs_info, csum_leaves);
723 if (existing->total_ref_mod < 0 && old_ref_mod >= 0) {
724 delayed_refs->pending_csums += existing->num_bytes;
725 trans->delayed_ref_updates += csum_leaves;
730 * This handles the following conditions:
732 * 1. We had a ref mod of 0 or more and went negative, indicating that
733 * we may be freeing space, so add our space to the
734 * total_bytes_pinned counter.
735 * 2. We were negative and went to 0 or positive, so no longer can say
736 * that the space would be pinned, decrement our counter from the
737 * total_bytes_pinned counter.
738 * 3. We are now at 0 and have ->must_insert_reserved set, which means
739 * this was a new allocation and then we dropped it, and thus must
740 * add our space to the total_bytes_pinned counter.
742 if (existing->total_ref_mod < 0 && old_ref_mod >= 0)
743 btrfs_mod_total_bytes_pinned(fs_info, flags, existing->num_bytes);
744 else if (existing->total_ref_mod >= 0 && old_ref_mod < 0)
745 btrfs_mod_total_bytes_pinned(fs_info, flags, -existing->num_bytes);
746 else if (existing->total_ref_mod == 0 && existing->must_insert_reserved)
747 btrfs_mod_total_bytes_pinned(fs_info, flags, existing->num_bytes);
749 spin_unlock(&existing->lock);
752 static void init_delayed_ref_head(struct btrfs_delayed_ref_head *head_ref,
753 struct btrfs_qgroup_extent_record *qrecord,
754 u64 bytenr, u64 num_bytes, u64 ref_root,
755 u64 reserved, int action, bool is_data,
759 int must_insert_reserved = 0;
761 /* If reserved is provided, it must be a data extent. */
762 BUG_ON(!is_data && reserved);
765 * The head node stores the sum of all the mods, so dropping a ref
766 * should drop the sum in the head node by one.
768 if (action == BTRFS_UPDATE_DELAYED_HEAD)
770 else if (action == BTRFS_DROP_DELAYED_REF)
774 * BTRFS_ADD_DELAYED_EXTENT means that we need to update the reserved
775 * accounting when the extent is finally added, or if a later
776 * modification deletes the delayed ref without ever inserting the
777 * extent into the extent allocation tree. ref->must_insert_reserved
778 * is the flag used to record that accounting mods are required.
780 * Once we record must_insert_reserved, switch the action to
781 * BTRFS_ADD_DELAYED_REF because other special casing is not required.
783 if (action == BTRFS_ADD_DELAYED_EXTENT)
784 must_insert_reserved = 1;
786 must_insert_reserved = 0;
788 refcount_set(&head_ref->refs, 1);
789 head_ref->bytenr = bytenr;
790 head_ref->num_bytes = num_bytes;
791 head_ref->ref_mod = count_mod;
792 head_ref->must_insert_reserved = must_insert_reserved;
793 head_ref->is_data = is_data;
794 head_ref->is_system = is_system;
795 head_ref->ref_tree = RB_ROOT_CACHED;
796 INIT_LIST_HEAD(&head_ref->ref_add_list);
797 RB_CLEAR_NODE(&head_ref->href_node);
798 head_ref->processing = 0;
799 head_ref->total_ref_mod = count_mod;
800 spin_lock_init(&head_ref->lock);
801 mutex_init(&head_ref->mutex);
804 if (ref_root && reserved) {
805 qrecord->data_rsv = reserved;
806 qrecord->data_rsv_refroot = ref_root;
808 qrecord->bytenr = bytenr;
809 qrecord->num_bytes = num_bytes;
810 qrecord->old_roots = NULL;
815 * helper function to actually insert a head node into the rbtree.
816 * this does all the dirty work in terms of maintaining the correct
817 * overall modification count.
819 static noinline struct btrfs_delayed_ref_head *
820 add_delayed_ref_head(struct btrfs_trans_handle *trans,
821 struct btrfs_delayed_ref_head *head_ref,
822 struct btrfs_qgroup_extent_record *qrecord,
823 int action, int *qrecord_inserted_ret)
825 struct btrfs_delayed_ref_head *existing;
826 struct btrfs_delayed_ref_root *delayed_refs;
827 int qrecord_inserted = 0;
829 delayed_refs = &trans->transaction->delayed_refs;
831 /* Record qgroup extent info if provided */
833 if (btrfs_qgroup_trace_extent_nolock(trans->fs_info,
834 delayed_refs, qrecord))
837 qrecord_inserted = 1;
840 trace_add_delayed_ref_head(trans->fs_info, head_ref, action);
842 existing = htree_insert(&delayed_refs->href_root,
843 &head_ref->href_node);
845 update_existing_head_ref(trans, existing, head_ref);
847 * we've updated the existing ref, free the newly
850 kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref);
853 u64 flags = btrfs_ref_head_to_space_flags(head_ref);
855 if (head_ref->is_data && head_ref->ref_mod < 0) {
856 delayed_refs->pending_csums += head_ref->num_bytes;
857 trans->delayed_ref_updates +=
858 btrfs_csum_bytes_to_leaves(trans->fs_info,
859 head_ref->num_bytes);
861 if (head_ref->ref_mod < 0)
862 btrfs_mod_total_bytes_pinned(trans->fs_info, flags,
863 head_ref->num_bytes);
864 delayed_refs->num_heads++;
865 delayed_refs->num_heads_ready++;
866 atomic_inc(&delayed_refs->num_entries);
867 trans->delayed_ref_updates++;
869 if (qrecord_inserted_ret)
870 *qrecord_inserted_ret = qrecord_inserted;
876 * init_delayed_ref_common - Initialize the structure which represents a
877 * modification to a an extent.
879 * @fs_info: Internal to the mounted filesystem mount structure.
881 * @ref: The structure which is going to be initialized.
883 * @bytenr: The logical address of the extent for which a modification is
884 * going to be recorded.
886 * @num_bytes: Size of the extent whose modification is being recorded.
888 * @ref_root: The id of the root where this modification has originated, this
889 * can be either one of the well-known metadata trees or the
890 * subvolume id which references this extent.
892 * @action: Can be one of BTRFS_ADD_DELAYED_REF/BTRFS_DROP_DELAYED_REF or
893 * BTRFS_ADD_DELAYED_EXTENT
895 * @ref_type: Holds the type of the extent which is being recorded, can be
896 * one of BTRFS_SHARED_BLOCK_REF_KEY/BTRFS_TREE_BLOCK_REF_KEY
897 * when recording a metadata extent or BTRFS_SHARED_DATA_REF_KEY/
898 * BTRFS_EXTENT_DATA_REF_KEY when recording data extent
900 static void init_delayed_ref_common(struct btrfs_fs_info *fs_info,
901 struct btrfs_delayed_ref_node *ref,
902 u64 bytenr, u64 num_bytes, u64 ref_root,
903 int action, u8 ref_type)
907 if (action == BTRFS_ADD_DELAYED_EXTENT)
908 action = BTRFS_ADD_DELAYED_REF;
910 if (is_fstree(ref_root))
911 seq = atomic64_read(&fs_info->tree_mod_seq);
913 refcount_set(&ref->refs, 1);
914 ref->bytenr = bytenr;
915 ref->num_bytes = num_bytes;
917 ref->action = action;
921 ref->type = ref_type;
922 RB_CLEAR_NODE(&ref->ref_node);
923 INIT_LIST_HEAD(&ref->add_list);
927 * add a delayed tree ref. This does all of the accounting required
928 * to make sure the delayed ref is eventually processed before this
929 * transaction commits.
931 int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans,
932 struct btrfs_ref *generic_ref,
933 struct btrfs_delayed_extent_op *extent_op)
935 struct btrfs_fs_info *fs_info = trans->fs_info;
936 struct btrfs_delayed_tree_ref *ref;
937 struct btrfs_delayed_ref_head *head_ref;
938 struct btrfs_delayed_ref_root *delayed_refs;
939 struct btrfs_qgroup_extent_record *record = NULL;
940 int qrecord_inserted;
942 int action = generic_ref->action;
943 int level = generic_ref->tree_ref.level;
945 u64 bytenr = generic_ref->bytenr;
946 u64 num_bytes = generic_ref->len;
947 u64 parent = generic_ref->parent;
950 is_system = (generic_ref->real_root == BTRFS_CHUNK_TREE_OBJECTID);
952 ASSERT(generic_ref->type == BTRFS_REF_METADATA && generic_ref->action);
953 BUG_ON(extent_op && extent_op->is_data);
954 ref = kmem_cache_alloc(btrfs_delayed_tree_ref_cachep, GFP_NOFS);
958 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
960 kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
964 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&
965 is_fstree(generic_ref->real_root) &&
966 is_fstree(generic_ref->tree_ref.root) &&
967 !generic_ref->skip_qgroup) {
968 record = kzalloc(sizeof(*record), GFP_NOFS);
970 kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
971 kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref);
977 ref_type = BTRFS_SHARED_BLOCK_REF_KEY;
979 ref_type = BTRFS_TREE_BLOCK_REF_KEY;
981 init_delayed_ref_common(fs_info, &ref->node, bytenr, num_bytes,
982 generic_ref->tree_ref.root, action, ref_type);
983 ref->root = generic_ref->tree_ref.root;
984 ref->parent = parent;
987 init_delayed_ref_head(head_ref, record, bytenr, num_bytes,
988 generic_ref->tree_ref.root, 0, action, false,
990 head_ref->extent_op = extent_op;
992 delayed_refs = &trans->transaction->delayed_refs;
993 spin_lock(&delayed_refs->lock);
996 * insert both the head node and the new ref without dropping
999 head_ref = add_delayed_ref_head(trans, head_ref, record,
1000 action, &qrecord_inserted);
1002 ret = insert_delayed_ref(trans, delayed_refs, head_ref, &ref->node);
1003 spin_unlock(&delayed_refs->lock);
1006 * Need to update the delayed_refs_rsv with any changes we may have
1009 btrfs_update_delayed_refs_rsv(trans);
1011 trace_add_delayed_tree_ref(fs_info, &ref->node, ref,
1012 action == BTRFS_ADD_DELAYED_EXTENT ?
1013 BTRFS_ADD_DELAYED_REF : action);
1015 kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
1017 if (qrecord_inserted)
1018 btrfs_qgroup_trace_extent_post(fs_info, record);
1024 * add a delayed data ref. it's similar to btrfs_add_delayed_tree_ref.
1026 int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans,
1027 struct btrfs_ref *generic_ref,
1030 struct btrfs_fs_info *fs_info = trans->fs_info;
1031 struct btrfs_delayed_data_ref *ref;
1032 struct btrfs_delayed_ref_head *head_ref;
1033 struct btrfs_delayed_ref_root *delayed_refs;
1034 struct btrfs_qgroup_extent_record *record = NULL;
1035 int qrecord_inserted;
1036 int action = generic_ref->action;
1038 u64 bytenr = generic_ref->bytenr;
1039 u64 num_bytes = generic_ref->len;
1040 u64 parent = generic_ref->parent;
1041 u64 ref_root = generic_ref->data_ref.ref_root;
1042 u64 owner = generic_ref->data_ref.ino;
1043 u64 offset = generic_ref->data_ref.offset;
1046 ASSERT(generic_ref->type == BTRFS_REF_DATA && action);
1047 ref = kmem_cache_alloc(btrfs_delayed_data_ref_cachep, GFP_NOFS);
1052 ref_type = BTRFS_SHARED_DATA_REF_KEY;
1054 ref_type = BTRFS_EXTENT_DATA_REF_KEY;
1055 init_delayed_ref_common(fs_info, &ref->node, bytenr, num_bytes,
1056 ref_root, action, ref_type);
1057 ref->root = ref_root;
1058 ref->parent = parent;
1059 ref->objectid = owner;
1060 ref->offset = offset;
1063 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
1065 kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
1069 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&
1070 is_fstree(ref_root) &&
1071 is_fstree(generic_ref->real_root) &&
1072 !generic_ref->skip_qgroup) {
1073 record = kzalloc(sizeof(*record), GFP_NOFS);
1075 kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
1076 kmem_cache_free(btrfs_delayed_ref_head_cachep,
1082 init_delayed_ref_head(head_ref, record, bytenr, num_bytes, ref_root,
1083 reserved, action, true, false);
1084 head_ref->extent_op = NULL;
1086 delayed_refs = &trans->transaction->delayed_refs;
1087 spin_lock(&delayed_refs->lock);
1090 * insert both the head node and the new ref without dropping
1093 head_ref = add_delayed_ref_head(trans, head_ref, record,
1094 action, &qrecord_inserted);
1096 ret = insert_delayed_ref(trans, delayed_refs, head_ref, &ref->node);
1097 spin_unlock(&delayed_refs->lock);
1100 * Need to update the delayed_refs_rsv with any changes we may have
1103 btrfs_update_delayed_refs_rsv(trans);
1105 trace_add_delayed_data_ref(trans->fs_info, &ref->node, ref,
1106 action == BTRFS_ADD_DELAYED_EXTENT ?
1107 BTRFS_ADD_DELAYED_REF : action);
1109 kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
1112 if (qrecord_inserted)
1113 return btrfs_qgroup_trace_extent_post(fs_info, record);
1117 int btrfs_add_delayed_extent_op(struct btrfs_trans_handle *trans,
1118 u64 bytenr, u64 num_bytes,
1119 struct btrfs_delayed_extent_op *extent_op)
1121 struct btrfs_delayed_ref_head *head_ref;
1122 struct btrfs_delayed_ref_root *delayed_refs;
1124 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
1128 init_delayed_ref_head(head_ref, NULL, bytenr, num_bytes, 0, 0,
1129 BTRFS_UPDATE_DELAYED_HEAD, extent_op->is_data,
1131 head_ref->extent_op = extent_op;
1133 delayed_refs = &trans->transaction->delayed_refs;
1134 spin_lock(&delayed_refs->lock);
1136 add_delayed_ref_head(trans, head_ref, NULL, BTRFS_UPDATE_DELAYED_HEAD,
1139 spin_unlock(&delayed_refs->lock);
1142 * Need to update the delayed_refs_rsv with any changes we may have
1145 btrfs_update_delayed_refs_rsv(trans);
1150 * This does a simple search for the head node for a given extent. Returns the
1151 * head node if found, or NULL if not.
1153 struct btrfs_delayed_ref_head *
1154 btrfs_find_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs, u64 bytenr)
1156 lockdep_assert_held(&delayed_refs->lock);
1158 return find_ref_head(delayed_refs, bytenr, false);
1161 void __cold btrfs_delayed_ref_exit(void)
1163 kmem_cache_destroy(btrfs_delayed_ref_head_cachep);
1164 kmem_cache_destroy(btrfs_delayed_tree_ref_cachep);
1165 kmem_cache_destroy(btrfs_delayed_data_ref_cachep);
1166 kmem_cache_destroy(btrfs_delayed_extent_op_cachep);
1169 int __init btrfs_delayed_ref_init(void)
1171 btrfs_delayed_ref_head_cachep = kmem_cache_create(
1172 "btrfs_delayed_ref_head",
1173 sizeof(struct btrfs_delayed_ref_head), 0,
1174 SLAB_MEM_SPREAD, NULL);
1175 if (!btrfs_delayed_ref_head_cachep)
1178 btrfs_delayed_tree_ref_cachep = kmem_cache_create(
1179 "btrfs_delayed_tree_ref",
1180 sizeof(struct btrfs_delayed_tree_ref), 0,
1181 SLAB_MEM_SPREAD, NULL);
1182 if (!btrfs_delayed_tree_ref_cachep)
1185 btrfs_delayed_data_ref_cachep = kmem_cache_create(
1186 "btrfs_delayed_data_ref",
1187 sizeof(struct btrfs_delayed_data_ref), 0,
1188 SLAB_MEM_SPREAD, NULL);
1189 if (!btrfs_delayed_data_ref_cachep)
1192 btrfs_delayed_extent_op_cachep = kmem_cache_create(
1193 "btrfs_delayed_extent_op",
1194 sizeof(struct btrfs_delayed_extent_op), 0,
1195 SLAB_MEM_SPREAD, NULL);
1196 if (!btrfs_delayed_extent_op_cachep)
1201 btrfs_delayed_ref_exit();