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"
14 struct kmem_cache *btrfs_delayed_ref_head_cachep;
15 struct kmem_cache *btrfs_delayed_tree_ref_cachep;
16 struct kmem_cache *btrfs_delayed_data_ref_cachep;
17 struct kmem_cache *btrfs_delayed_extent_op_cachep;
19 * delayed back reference update tracking. For subvolume trees
20 * we queue up extent allocations and backref maintenance for
21 * delayed processing. This avoids deep call chains where we
22 * add extents in the middle of btrfs_search_slot, and it allows
23 * us to buffer up frequently modified backrefs in an rb tree instead
24 * of hammering updates on the extent allocation tree.
28 * compare two delayed tree backrefs with same bytenr and type
30 static int comp_tree_refs(struct btrfs_delayed_tree_ref *ref1,
31 struct btrfs_delayed_tree_ref *ref2)
33 if (ref1->node.type == BTRFS_TREE_BLOCK_REF_KEY) {
34 if (ref1->root < ref2->root)
36 if (ref1->root > ref2->root)
39 if (ref1->parent < ref2->parent)
41 if (ref1->parent > ref2->parent)
48 * compare two delayed data backrefs with same bytenr and type
50 static int comp_data_refs(struct btrfs_delayed_data_ref *ref1,
51 struct btrfs_delayed_data_ref *ref2)
53 if (ref1->node.type == BTRFS_EXTENT_DATA_REF_KEY) {
54 if (ref1->root < ref2->root)
56 if (ref1->root > ref2->root)
58 if (ref1->objectid < ref2->objectid)
60 if (ref1->objectid > ref2->objectid)
62 if (ref1->offset < ref2->offset)
64 if (ref1->offset > ref2->offset)
67 if (ref1->parent < ref2->parent)
69 if (ref1->parent > ref2->parent)
75 static int comp_refs(struct btrfs_delayed_ref_node *ref1,
76 struct btrfs_delayed_ref_node *ref2,
81 if (ref1->type < ref2->type)
83 if (ref1->type > ref2->type)
85 if (ref1->type == BTRFS_TREE_BLOCK_REF_KEY ||
86 ref1->type == BTRFS_SHARED_BLOCK_REF_KEY)
87 ret = comp_tree_refs(btrfs_delayed_node_to_tree_ref(ref1),
88 btrfs_delayed_node_to_tree_ref(ref2));
90 ret = comp_data_refs(btrfs_delayed_node_to_data_ref(ref1),
91 btrfs_delayed_node_to_data_ref(ref2));
95 if (ref1->seq < ref2->seq)
97 if (ref1->seq > ref2->seq)
103 /* insert a new ref to head ref rbtree */
104 static struct btrfs_delayed_ref_head *htree_insert(struct rb_root_cached *root,
105 struct rb_node *node)
107 struct rb_node **p = &root->rb_root.rb_node;
108 struct rb_node *parent_node = NULL;
109 struct btrfs_delayed_ref_head *entry;
110 struct btrfs_delayed_ref_head *ins;
112 bool leftmost = true;
114 ins = rb_entry(node, struct btrfs_delayed_ref_head, href_node);
115 bytenr = ins->bytenr;
118 entry = rb_entry(parent_node, struct btrfs_delayed_ref_head,
121 if (bytenr < entry->bytenr) {
123 } else if (bytenr > entry->bytenr) {
131 rb_link_node(node, parent_node, p);
132 rb_insert_color_cached(node, root, leftmost);
136 static struct btrfs_delayed_ref_node* tree_insert(struct rb_root_cached *root,
137 struct btrfs_delayed_ref_node *ins)
139 struct rb_node **p = &root->rb_root.rb_node;
140 struct rb_node *node = &ins->ref_node;
141 struct rb_node *parent_node = NULL;
142 struct btrfs_delayed_ref_node *entry;
143 bool leftmost = true;
149 entry = rb_entry(parent_node, struct btrfs_delayed_ref_node,
151 comp = comp_refs(ins, entry, true);
154 } else if (comp > 0) {
162 rb_link_node(node, parent_node, p);
163 rb_insert_color_cached(node, root, leftmost);
168 * find an head entry based on bytenr. This returns the delayed ref
169 * head if it was able to find one, or NULL if nothing was in that spot.
170 * If return_bigger is given, the next bigger entry is returned if no exact
171 * match is found. But if no bigger one is found then the first node of the
172 * ref head tree will be returned.
174 static struct btrfs_delayed_ref_head* find_ref_head(
175 struct btrfs_delayed_ref_root *dr, u64 bytenr,
178 struct rb_root *root = &dr->href_root.rb_root;
180 struct btrfs_delayed_ref_head *entry;
185 entry = rb_entry(n, struct btrfs_delayed_ref_head, href_node);
187 if (bytenr < entry->bytenr)
189 else if (bytenr > entry->bytenr)
194 if (entry && return_bigger) {
195 if (bytenr > entry->bytenr) {
196 n = rb_next(&entry->href_node);
198 n = rb_first_cached(&dr->href_root);
199 entry = rb_entry(n, struct btrfs_delayed_ref_head,
208 int btrfs_delayed_ref_lock(struct btrfs_delayed_ref_root *delayed_refs,
209 struct btrfs_delayed_ref_head *head)
211 lockdep_assert_held(&delayed_refs->lock);
212 if (mutex_trylock(&head->mutex))
215 refcount_inc(&head->refs);
216 spin_unlock(&delayed_refs->lock);
218 mutex_lock(&head->mutex);
219 spin_lock(&delayed_refs->lock);
220 if (RB_EMPTY_NODE(&head->href_node)) {
221 mutex_unlock(&head->mutex);
222 btrfs_put_delayed_ref_head(head);
225 btrfs_put_delayed_ref_head(head);
229 static inline void drop_delayed_ref(struct btrfs_trans_handle *trans,
230 struct btrfs_delayed_ref_root *delayed_refs,
231 struct btrfs_delayed_ref_head *head,
232 struct btrfs_delayed_ref_node *ref)
234 lockdep_assert_held(&head->lock);
235 rb_erase_cached(&ref->ref_node, &head->ref_tree);
236 RB_CLEAR_NODE(&ref->ref_node);
237 if (!list_empty(&ref->add_list))
238 list_del(&ref->add_list);
240 btrfs_put_delayed_ref(ref);
241 atomic_dec(&delayed_refs->num_entries);
242 if (trans->delayed_ref_updates)
243 trans->delayed_ref_updates--;
246 static bool merge_ref(struct btrfs_trans_handle *trans,
247 struct btrfs_delayed_ref_root *delayed_refs,
248 struct btrfs_delayed_ref_head *head,
249 struct btrfs_delayed_ref_node *ref,
252 struct btrfs_delayed_ref_node *next;
253 struct rb_node *node = rb_next(&ref->ref_node);
256 while (!done && node) {
259 next = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
260 node = rb_next(node);
261 if (seq && next->seq >= seq)
263 if (comp_refs(ref, next, false))
266 if (ref->action == next->action) {
269 if (ref->ref_mod < next->ref_mod) {
273 mod = -next->ref_mod;
276 drop_delayed_ref(trans, delayed_refs, head, next);
278 if (ref->ref_mod == 0) {
279 drop_delayed_ref(trans, delayed_refs, head, ref);
283 * Can't have multiples of the same ref on a tree block.
285 WARN_ON(ref->type == BTRFS_TREE_BLOCK_REF_KEY ||
286 ref->type == BTRFS_SHARED_BLOCK_REF_KEY);
293 void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
294 struct btrfs_delayed_ref_root *delayed_refs,
295 struct btrfs_delayed_ref_head *head)
297 struct btrfs_fs_info *fs_info = trans->fs_info;
298 struct btrfs_delayed_ref_node *ref;
299 struct rb_node *node;
302 lockdep_assert_held(&head->lock);
304 if (RB_EMPTY_ROOT(&head->ref_tree.rb_root))
307 /* We don't have too many refs to merge for data. */
311 spin_lock(&fs_info->tree_mod_seq_lock);
312 if (!list_empty(&fs_info->tree_mod_seq_list)) {
313 struct seq_list *elem;
315 elem = list_first_entry(&fs_info->tree_mod_seq_list,
316 struct seq_list, list);
319 spin_unlock(&fs_info->tree_mod_seq_lock);
322 for (node = rb_first_cached(&head->ref_tree); node;
323 node = rb_next(node)) {
324 ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
325 if (seq && ref->seq >= seq)
327 if (merge_ref(trans, delayed_refs, head, ref, seq))
332 int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq)
334 struct seq_list *elem;
337 spin_lock(&fs_info->tree_mod_seq_lock);
338 if (!list_empty(&fs_info->tree_mod_seq_list)) {
339 elem = list_first_entry(&fs_info->tree_mod_seq_list,
340 struct seq_list, list);
341 if (seq >= elem->seq) {
343 "holding back delayed_ref %#x.%x, lowest is %#x.%x",
344 (u32)(seq >> 32), (u32)seq,
345 (u32)(elem->seq >> 32), (u32)elem->seq);
350 spin_unlock(&fs_info->tree_mod_seq_lock);
354 struct btrfs_delayed_ref_head *btrfs_select_ref_head(
355 struct btrfs_delayed_ref_root *delayed_refs)
357 struct btrfs_delayed_ref_head *head;
362 start = delayed_refs->run_delayed_start;
363 head = find_ref_head(delayed_refs, start, true);
364 if (!head && !loop) {
365 delayed_refs->run_delayed_start = 0;
368 head = find_ref_head(delayed_refs, start, true);
371 } else if (!head && loop) {
375 while (head->processing) {
376 struct rb_node *node;
378 node = rb_next(&head->href_node);
382 delayed_refs->run_delayed_start = 0;
387 head = rb_entry(node, struct btrfs_delayed_ref_head,
391 head->processing = 1;
392 WARN_ON(delayed_refs->num_heads_ready == 0);
393 delayed_refs->num_heads_ready--;
394 delayed_refs->run_delayed_start = head->bytenr +
400 * Helper to insert the ref_node to the tail or merge with tail.
402 * Return 0 for insert.
403 * Return >0 for merge.
405 static int insert_delayed_ref(struct btrfs_trans_handle *trans,
406 struct btrfs_delayed_ref_root *root,
407 struct btrfs_delayed_ref_head *href,
408 struct btrfs_delayed_ref_node *ref)
410 struct btrfs_delayed_ref_node *exist;
414 spin_lock(&href->lock);
415 exist = tree_insert(&href->ref_tree, ref);
419 /* Now we are sure we can merge */
421 if (exist->action == ref->action) {
424 /* Need to change action */
425 if (exist->ref_mod < ref->ref_mod) {
426 exist->action = ref->action;
427 mod = -exist->ref_mod;
428 exist->ref_mod = ref->ref_mod;
429 if (ref->action == BTRFS_ADD_DELAYED_REF)
430 list_add_tail(&exist->add_list,
431 &href->ref_add_list);
432 else if (ref->action == BTRFS_DROP_DELAYED_REF) {
433 ASSERT(!list_empty(&exist->add_list));
434 list_del(&exist->add_list);
441 exist->ref_mod += mod;
443 /* remove existing tail if its ref_mod is zero */
444 if (exist->ref_mod == 0)
445 drop_delayed_ref(trans, root, href, exist);
446 spin_unlock(&href->lock);
449 if (ref->action == BTRFS_ADD_DELAYED_REF)
450 list_add_tail(&ref->add_list, &href->ref_add_list);
451 atomic_inc(&root->num_entries);
452 trans->delayed_ref_updates++;
453 spin_unlock(&href->lock);
458 * helper function to update the accounting in the head ref
459 * existing and update must have the same bytenr
462 update_existing_head_ref(struct btrfs_delayed_ref_root *delayed_refs,
463 struct btrfs_delayed_ref_head *existing,
464 struct btrfs_delayed_ref_head *update,
465 int *old_ref_mod_ret)
469 BUG_ON(existing->is_data != update->is_data);
471 spin_lock(&existing->lock);
472 if (update->must_insert_reserved) {
473 /* if the extent was freed and then
474 * reallocated before the delayed ref
475 * entries were processed, we can end up
476 * with an existing head ref without
477 * the must_insert_reserved flag set.
480 existing->must_insert_reserved = update->must_insert_reserved;
483 * update the num_bytes so we make sure the accounting
486 existing->num_bytes = update->num_bytes;
490 if (update->extent_op) {
491 if (!existing->extent_op) {
492 existing->extent_op = update->extent_op;
494 if (update->extent_op->update_key) {
495 memcpy(&existing->extent_op->key,
496 &update->extent_op->key,
497 sizeof(update->extent_op->key));
498 existing->extent_op->update_key = true;
500 if (update->extent_op->update_flags) {
501 existing->extent_op->flags_to_set |=
502 update->extent_op->flags_to_set;
503 existing->extent_op->update_flags = true;
505 btrfs_free_delayed_extent_op(update->extent_op);
509 * update the reference mod on the head to reflect this new operation,
510 * only need the lock for this case cause we could be processing it
511 * currently, for refs we just added we know we're a-ok.
513 old_ref_mod = existing->total_ref_mod;
515 *old_ref_mod_ret = old_ref_mod;
516 existing->ref_mod += update->ref_mod;
517 existing->total_ref_mod += update->ref_mod;
520 * If we are going to from a positive ref mod to a negative or vice
521 * versa we need to make sure to adjust pending_csums accordingly.
523 if (existing->is_data) {
524 if (existing->total_ref_mod >= 0 && old_ref_mod < 0)
525 delayed_refs->pending_csums -= existing->num_bytes;
526 if (existing->total_ref_mod < 0 && old_ref_mod >= 0)
527 delayed_refs->pending_csums += existing->num_bytes;
529 spin_unlock(&existing->lock);
532 static void init_delayed_ref_head(struct btrfs_delayed_ref_head *head_ref,
533 struct btrfs_qgroup_extent_record *qrecord,
534 u64 bytenr, u64 num_bytes, u64 ref_root,
535 u64 reserved, int action, bool is_data,
539 int must_insert_reserved = 0;
541 /* If reserved is provided, it must be a data extent. */
542 BUG_ON(!is_data && reserved);
545 * The head node stores the sum of all the mods, so dropping a ref
546 * should drop the sum in the head node by one.
548 if (action == BTRFS_UPDATE_DELAYED_HEAD)
550 else if (action == BTRFS_DROP_DELAYED_REF)
554 * BTRFS_ADD_DELAYED_EXTENT means that we need to update the reserved
555 * accounting when the extent is finally added, or if a later
556 * modification deletes the delayed ref without ever inserting the
557 * extent into the extent allocation tree. ref->must_insert_reserved
558 * is the flag used to record that accounting mods are required.
560 * Once we record must_insert_reserved, switch the action to
561 * BTRFS_ADD_DELAYED_REF because other special casing is not required.
563 if (action == BTRFS_ADD_DELAYED_EXTENT)
564 must_insert_reserved = 1;
566 must_insert_reserved = 0;
568 refcount_set(&head_ref->refs, 1);
569 head_ref->bytenr = bytenr;
570 head_ref->num_bytes = num_bytes;
571 head_ref->ref_mod = count_mod;
572 head_ref->must_insert_reserved = must_insert_reserved;
573 head_ref->is_data = is_data;
574 head_ref->is_system = is_system;
575 head_ref->ref_tree = RB_ROOT_CACHED;
576 INIT_LIST_HEAD(&head_ref->ref_add_list);
577 RB_CLEAR_NODE(&head_ref->href_node);
578 head_ref->processing = 0;
579 head_ref->total_ref_mod = count_mod;
580 head_ref->qgroup_reserved = 0;
581 head_ref->qgroup_ref_root = 0;
582 spin_lock_init(&head_ref->lock);
583 mutex_init(&head_ref->mutex);
586 if (ref_root && reserved) {
587 head_ref->qgroup_ref_root = ref_root;
588 head_ref->qgroup_reserved = reserved;
591 qrecord->bytenr = bytenr;
592 qrecord->num_bytes = num_bytes;
593 qrecord->old_roots = NULL;
598 * helper function to actually insert a head node into the rbtree.
599 * this does all the dirty work in terms of maintaining the correct
600 * overall modification count.
602 static noinline struct btrfs_delayed_ref_head *
603 add_delayed_ref_head(struct btrfs_trans_handle *trans,
604 struct btrfs_delayed_ref_head *head_ref,
605 struct btrfs_qgroup_extent_record *qrecord,
606 int action, int *qrecord_inserted_ret,
607 int *old_ref_mod, int *new_ref_mod)
609 struct btrfs_delayed_ref_head *existing;
610 struct btrfs_delayed_ref_root *delayed_refs;
611 int qrecord_inserted = 0;
613 delayed_refs = &trans->transaction->delayed_refs;
615 /* Record qgroup extent info if provided */
617 if (btrfs_qgroup_trace_extent_nolock(trans->fs_info,
618 delayed_refs, qrecord))
621 qrecord_inserted = 1;
624 trace_add_delayed_ref_head(trans->fs_info, head_ref, action);
626 existing = htree_insert(&delayed_refs->href_root,
627 &head_ref->href_node);
629 WARN_ON(qrecord && head_ref->qgroup_ref_root
630 && head_ref->qgroup_reserved
631 && existing->qgroup_ref_root
632 && existing->qgroup_reserved);
633 update_existing_head_ref(delayed_refs, existing, head_ref,
636 * we've updated the existing ref, free the newly
639 kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref);
644 if (head_ref->is_data && head_ref->ref_mod < 0)
645 delayed_refs->pending_csums += head_ref->num_bytes;
646 delayed_refs->num_heads++;
647 delayed_refs->num_heads_ready++;
648 atomic_inc(&delayed_refs->num_entries);
649 trans->delayed_ref_updates++;
651 if (qrecord_inserted_ret)
652 *qrecord_inserted_ret = qrecord_inserted;
654 *new_ref_mod = head_ref->total_ref_mod;
660 * init_delayed_ref_common - Initialize the structure which represents a
661 * modification to a an extent.
663 * @fs_info: Internal to the mounted filesystem mount structure.
665 * @ref: The structure which is going to be initialized.
667 * @bytenr: The logical address of the extent for which a modification is
668 * going to be recorded.
670 * @num_bytes: Size of the extent whose modification is being recorded.
672 * @ref_root: The id of the root where this modification has originated, this
673 * can be either one of the well-known metadata trees or the
674 * subvolume id which references this extent.
676 * @action: Can be one of BTRFS_ADD_DELAYED_REF/BTRFS_DROP_DELAYED_REF or
677 * BTRFS_ADD_DELAYED_EXTENT
679 * @ref_type: Holds the type of the extent which is being recorded, can be
680 * one of BTRFS_SHARED_BLOCK_REF_KEY/BTRFS_TREE_BLOCK_REF_KEY
681 * when recording a metadata extent or BTRFS_SHARED_DATA_REF_KEY/
682 * BTRFS_EXTENT_DATA_REF_KEY when recording data extent
684 static void init_delayed_ref_common(struct btrfs_fs_info *fs_info,
685 struct btrfs_delayed_ref_node *ref,
686 u64 bytenr, u64 num_bytes, u64 ref_root,
687 int action, u8 ref_type)
691 if (action == BTRFS_ADD_DELAYED_EXTENT)
692 action = BTRFS_ADD_DELAYED_REF;
694 if (is_fstree(ref_root))
695 seq = atomic64_read(&fs_info->tree_mod_seq);
697 refcount_set(&ref->refs, 1);
698 ref->bytenr = bytenr;
699 ref->num_bytes = num_bytes;
701 ref->action = action;
705 ref->type = ref_type;
706 RB_CLEAR_NODE(&ref->ref_node);
707 INIT_LIST_HEAD(&ref->add_list);
711 * add a delayed tree ref. This does all of the accounting required
712 * to make sure the delayed ref is eventually processed before this
713 * transaction commits.
715 int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans,
716 u64 bytenr, u64 num_bytes, u64 parent,
717 u64 ref_root, int level, int action,
718 struct btrfs_delayed_extent_op *extent_op,
719 int *old_ref_mod, int *new_ref_mod)
721 struct btrfs_fs_info *fs_info = trans->fs_info;
722 struct btrfs_delayed_tree_ref *ref;
723 struct btrfs_delayed_ref_head *head_ref;
724 struct btrfs_delayed_ref_root *delayed_refs;
725 struct btrfs_qgroup_extent_record *record = NULL;
726 int qrecord_inserted;
727 bool is_system = (ref_root == BTRFS_CHUNK_TREE_OBJECTID);
731 BUG_ON(extent_op && extent_op->is_data);
732 ref = kmem_cache_alloc(btrfs_delayed_tree_ref_cachep, GFP_NOFS);
736 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
738 kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
742 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&
743 is_fstree(ref_root)) {
744 record = kmalloc(sizeof(*record), GFP_NOFS);
746 kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
747 kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref);
753 ref_type = BTRFS_SHARED_BLOCK_REF_KEY;
755 ref_type = BTRFS_TREE_BLOCK_REF_KEY;
757 init_delayed_ref_common(fs_info, &ref->node, bytenr, num_bytes,
758 ref_root, action, ref_type);
759 ref->root = ref_root;
760 ref->parent = parent;
763 init_delayed_ref_head(head_ref, record, bytenr, num_bytes,
764 ref_root, 0, action, false, is_system);
765 head_ref->extent_op = extent_op;
767 delayed_refs = &trans->transaction->delayed_refs;
768 spin_lock(&delayed_refs->lock);
771 * insert both the head node and the new ref without dropping
774 head_ref = add_delayed_ref_head(trans, head_ref, record,
775 action, &qrecord_inserted,
776 old_ref_mod, new_ref_mod);
778 ret = insert_delayed_ref(trans, delayed_refs, head_ref, &ref->node);
779 spin_unlock(&delayed_refs->lock);
781 trace_add_delayed_tree_ref(fs_info, &ref->node, ref,
782 action == BTRFS_ADD_DELAYED_EXTENT ?
783 BTRFS_ADD_DELAYED_REF : action);
785 kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
787 if (qrecord_inserted)
788 btrfs_qgroup_trace_extent_post(fs_info, record);
794 * add a delayed data ref. it's similar to btrfs_add_delayed_tree_ref.
796 int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans,
797 u64 bytenr, u64 num_bytes,
798 u64 parent, u64 ref_root,
799 u64 owner, u64 offset, u64 reserved, int action,
800 int *old_ref_mod, int *new_ref_mod)
802 struct btrfs_fs_info *fs_info = trans->fs_info;
803 struct btrfs_delayed_data_ref *ref;
804 struct btrfs_delayed_ref_head *head_ref;
805 struct btrfs_delayed_ref_root *delayed_refs;
806 struct btrfs_qgroup_extent_record *record = NULL;
807 int qrecord_inserted;
811 ref = kmem_cache_alloc(btrfs_delayed_data_ref_cachep, GFP_NOFS);
816 ref_type = BTRFS_SHARED_DATA_REF_KEY;
818 ref_type = BTRFS_EXTENT_DATA_REF_KEY;
819 init_delayed_ref_common(fs_info, &ref->node, bytenr, num_bytes,
820 ref_root, action, ref_type);
821 ref->root = ref_root;
822 ref->parent = parent;
823 ref->objectid = owner;
824 ref->offset = offset;
827 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
829 kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
833 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&
834 is_fstree(ref_root)) {
835 record = kmalloc(sizeof(*record), GFP_NOFS);
837 kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
838 kmem_cache_free(btrfs_delayed_ref_head_cachep,
844 init_delayed_ref_head(head_ref, record, bytenr, num_bytes, ref_root,
845 reserved, action, true, false);
846 head_ref->extent_op = NULL;
848 delayed_refs = &trans->transaction->delayed_refs;
849 spin_lock(&delayed_refs->lock);
852 * insert both the head node and the new ref without dropping
855 head_ref = add_delayed_ref_head(trans, head_ref, record,
856 action, &qrecord_inserted,
857 old_ref_mod, new_ref_mod);
859 ret = insert_delayed_ref(trans, delayed_refs, head_ref, &ref->node);
860 spin_unlock(&delayed_refs->lock);
862 trace_add_delayed_data_ref(trans->fs_info, &ref->node, ref,
863 action == BTRFS_ADD_DELAYED_EXTENT ?
864 BTRFS_ADD_DELAYED_REF : action);
866 kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
869 if (qrecord_inserted)
870 return btrfs_qgroup_trace_extent_post(fs_info, record);
874 int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info,
875 struct btrfs_trans_handle *trans,
876 u64 bytenr, u64 num_bytes,
877 struct btrfs_delayed_extent_op *extent_op)
879 struct btrfs_delayed_ref_head *head_ref;
880 struct btrfs_delayed_ref_root *delayed_refs;
882 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
886 init_delayed_ref_head(head_ref, NULL, bytenr, num_bytes, 0, 0,
887 BTRFS_UPDATE_DELAYED_HEAD, extent_op->is_data,
889 head_ref->extent_op = extent_op;
891 delayed_refs = &trans->transaction->delayed_refs;
892 spin_lock(&delayed_refs->lock);
894 add_delayed_ref_head(trans, head_ref, NULL, BTRFS_UPDATE_DELAYED_HEAD,
897 spin_unlock(&delayed_refs->lock);
902 * this does a simple search for the head node for a given extent.
903 * It must be called with the delayed ref spinlock held, and it returns
904 * the head node if any where found, or NULL if not.
906 struct btrfs_delayed_ref_head *
907 btrfs_find_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs, u64 bytenr)
909 return find_ref_head(delayed_refs, bytenr, false);
912 void __cold btrfs_delayed_ref_exit(void)
914 kmem_cache_destroy(btrfs_delayed_ref_head_cachep);
915 kmem_cache_destroy(btrfs_delayed_tree_ref_cachep);
916 kmem_cache_destroy(btrfs_delayed_data_ref_cachep);
917 kmem_cache_destroy(btrfs_delayed_extent_op_cachep);
920 int __init btrfs_delayed_ref_init(void)
922 btrfs_delayed_ref_head_cachep = kmem_cache_create(
923 "btrfs_delayed_ref_head",
924 sizeof(struct btrfs_delayed_ref_head), 0,
925 SLAB_MEM_SPREAD, NULL);
926 if (!btrfs_delayed_ref_head_cachep)
929 btrfs_delayed_tree_ref_cachep = kmem_cache_create(
930 "btrfs_delayed_tree_ref",
931 sizeof(struct btrfs_delayed_tree_ref), 0,
932 SLAB_MEM_SPREAD, NULL);
933 if (!btrfs_delayed_tree_ref_cachep)
936 btrfs_delayed_data_ref_cachep = kmem_cache_create(
937 "btrfs_delayed_data_ref",
938 sizeof(struct btrfs_delayed_data_ref), 0,
939 SLAB_MEM_SPREAD, NULL);
940 if (!btrfs_delayed_data_ref_cachep)
943 btrfs_delayed_extent_op_cachep = kmem_cache_create(
944 "btrfs_delayed_extent_op",
945 sizeof(struct btrfs_delayed_extent_op), 0,
946 SLAB_MEM_SPREAD, NULL);
947 if (!btrfs_delayed_extent_op_cachep)
952 btrfs_delayed_ref_exit();