2 * Copyright (C) 2009 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/sort.h>
23 #include "delayed-ref.h"
24 #include "transaction.h"
26 struct kmem_cache *btrfs_delayed_ref_head_cachep;
27 struct kmem_cache *btrfs_delayed_tree_ref_cachep;
28 struct kmem_cache *btrfs_delayed_data_ref_cachep;
29 struct kmem_cache *btrfs_delayed_extent_op_cachep;
31 * delayed back reference update tracking. For subvolume trees
32 * we queue up extent allocations and backref maintenance for
33 * delayed processing. This avoids deep call chains where we
34 * add extents in the middle of btrfs_search_slot, and it allows
35 * us to buffer up frequently modified backrefs in an rb tree instead
36 * of hammering updates on the extent allocation tree.
40 * compare two delayed tree backrefs with same bytenr and type
42 static int comp_tree_refs(struct btrfs_delayed_tree_ref *ref2,
43 struct btrfs_delayed_tree_ref *ref1, int type)
45 if (type == BTRFS_TREE_BLOCK_REF_KEY) {
46 if (ref1->root < ref2->root)
48 if (ref1->root > ref2->root)
51 if (ref1->parent < ref2->parent)
53 if (ref1->parent > ref2->parent)
60 * compare two delayed data backrefs with same bytenr and type
62 static int comp_data_refs(struct btrfs_delayed_data_ref *ref2,
63 struct btrfs_delayed_data_ref *ref1)
65 if (ref1->node.type == BTRFS_EXTENT_DATA_REF_KEY) {
66 if (ref1->root < ref2->root)
68 if (ref1->root > ref2->root)
70 if (ref1->objectid < ref2->objectid)
72 if (ref1->objectid > ref2->objectid)
74 if (ref1->offset < ref2->offset)
76 if (ref1->offset > ref2->offset)
79 if (ref1->parent < ref2->parent)
81 if (ref1->parent > ref2->parent)
88 * entries in the rb tree are ordered by the byte number of the extent,
89 * type of the delayed backrefs and content of delayed backrefs.
91 static int comp_entry(struct btrfs_delayed_ref_node *ref2,
92 struct btrfs_delayed_ref_node *ref1,
95 if (ref1->bytenr < ref2->bytenr)
97 if (ref1->bytenr > ref2->bytenr)
99 if (ref1->is_head && ref2->is_head)
105 if (ref1->type < ref2->type)
107 if (ref1->type > ref2->type)
109 if (ref1->no_quota > ref2->no_quota)
111 if (ref1->no_quota < ref2->no_quota)
113 /* merging of sequenced refs is not allowed */
115 if (ref1->seq < ref2->seq)
117 if (ref1->seq > ref2->seq)
120 if (ref1->type == BTRFS_TREE_BLOCK_REF_KEY ||
121 ref1->type == BTRFS_SHARED_BLOCK_REF_KEY) {
122 return comp_tree_refs(btrfs_delayed_node_to_tree_ref(ref2),
123 btrfs_delayed_node_to_tree_ref(ref1),
125 } else if (ref1->type == BTRFS_EXTENT_DATA_REF_KEY ||
126 ref1->type == BTRFS_SHARED_DATA_REF_KEY) {
127 return comp_data_refs(btrfs_delayed_node_to_data_ref(ref2),
128 btrfs_delayed_node_to_data_ref(ref1));
135 * insert a new ref into the rbtree. This returns any existing refs
136 * for the same (bytenr,parent) tuple, or NULL if the new node was properly
139 static struct btrfs_delayed_ref_node *tree_insert(struct rb_root *root,
140 struct rb_node *node)
142 struct rb_node **p = &root->rb_node;
143 struct rb_node *parent_node = NULL;
144 struct btrfs_delayed_ref_node *entry;
145 struct btrfs_delayed_ref_node *ins;
148 ins = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
151 entry = rb_entry(parent_node, struct btrfs_delayed_ref_node,
154 cmp = comp_entry(entry, ins, 1);
163 rb_link_node(node, parent_node, p);
164 rb_insert_color(node, root);
168 /* insert a new ref to head ref rbtree */
169 static struct btrfs_delayed_ref_head *htree_insert(struct rb_root *root,
170 struct rb_node *node)
172 struct rb_node **p = &root->rb_node;
173 struct rb_node *parent_node = NULL;
174 struct btrfs_delayed_ref_head *entry;
175 struct btrfs_delayed_ref_head *ins;
178 ins = rb_entry(node, struct btrfs_delayed_ref_head, href_node);
179 bytenr = ins->node.bytenr;
182 entry = rb_entry(parent_node, struct btrfs_delayed_ref_head,
185 if (bytenr < entry->node.bytenr)
187 else if (bytenr > entry->node.bytenr)
193 rb_link_node(node, parent_node, p);
194 rb_insert_color(node, root);
199 * find an head entry based on bytenr. This returns the delayed ref
200 * head if it was able to find one, or NULL if nothing was in that spot.
201 * If return_bigger is given, the next bigger entry is returned if no exact
204 static struct btrfs_delayed_ref_head *
205 find_ref_head(struct rb_root *root, u64 bytenr,
209 struct btrfs_delayed_ref_head *entry;
214 entry = rb_entry(n, struct btrfs_delayed_ref_head, href_node);
216 if (bytenr < entry->node.bytenr)
218 else if (bytenr > entry->node.bytenr)
223 if (entry && return_bigger) {
224 if (bytenr > entry->node.bytenr) {
225 n = rb_next(&entry->href_node);
228 entry = rb_entry(n, struct btrfs_delayed_ref_head,
237 int btrfs_delayed_ref_lock(struct btrfs_trans_handle *trans,
238 struct btrfs_delayed_ref_head *head)
240 struct btrfs_delayed_ref_root *delayed_refs;
242 delayed_refs = &trans->transaction->delayed_refs;
243 assert_spin_locked(&delayed_refs->lock);
244 if (mutex_trylock(&head->mutex))
247 atomic_inc(&head->node.refs);
248 spin_unlock(&delayed_refs->lock);
250 mutex_lock(&head->mutex);
251 spin_lock(&delayed_refs->lock);
252 if (!head->node.in_tree) {
253 mutex_unlock(&head->mutex);
254 btrfs_put_delayed_ref(&head->node);
257 btrfs_put_delayed_ref(&head->node);
261 static inline void drop_delayed_ref(struct btrfs_trans_handle *trans,
262 struct btrfs_delayed_ref_root *delayed_refs,
263 struct btrfs_delayed_ref_head *head,
264 struct btrfs_delayed_ref_node *ref)
266 if (btrfs_delayed_ref_is_head(ref)) {
267 head = btrfs_delayed_node_to_head(ref);
268 rb_erase(&head->href_node, &delayed_refs->href_root);
270 assert_spin_locked(&head->lock);
271 rb_erase(&ref->rb_node, &head->ref_root);
274 btrfs_put_delayed_ref(ref);
275 atomic_dec(&delayed_refs->num_entries);
276 if (trans->delayed_ref_updates)
277 trans->delayed_ref_updates--;
280 static int merge_ref(struct btrfs_trans_handle *trans,
281 struct btrfs_delayed_ref_root *delayed_refs,
282 struct btrfs_delayed_ref_head *head,
283 struct btrfs_delayed_ref_node *ref, u64 seq)
285 struct rb_node *node;
289 node = rb_next(&ref->rb_node);
290 while (!done && node) {
291 struct btrfs_delayed_ref_node *next;
293 next = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
294 node = rb_next(node);
295 if (seq && next->seq >= seq)
297 if (comp_entry(ref, next, 0))
300 if (ref->action == next->action) {
303 if (ref->ref_mod < next->ref_mod) {
304 struct btrfs_delayed_ref_node *tmp;
311 mod = -next->ref_mod;
314 drop_delayed_ref(trans, delayed_refs, head, next);
316 if (ref->ref_mod == 0) {
317 drop_delayed_ref(trans, delayed_refs, head, ref);
321 * You can't have multiples of the same ref on a tree
324 WARN_ON(ref->type == BTRFS_TREE_BLOCK_REF_KEY ||
325 ref->type == BTRFS_SHARED_BLOCK_REF_KEY);
331 void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
332 struct btrfs_fs_info *fs_info,
333 struct btrfs_delayed_ref_root *delayed_refs,
334 struct btrfs_delayed_ref_head *head)
336 struct rb_node *node;
339 assert_spin_locked(&head->lock);
341 * We don't have too much refs to merge in the case of delayed data
347 spin_lock(&fs_info->tree_mod_seq_lock);
348 if (!list_empty(&fs_info->tree_mod_seq_list)) {
349 struct seq_list *elem;
351 elem = list_first_entry(&fs_info->tree_mod_seq_list,
352 struct seq_list, list);
355 spin_unlock(&fs_info->tree_mod_seq_lock);
357 node = rb_first(&head->ref_root);
359 struct btrfs_delayed_ref_node *ref;
361 ref = rb_entry(node, struct btrfs_delayed_ref_node,
363 /* We can't merge refs that are outside of our seq count */
364 if (seq && ref->seq >= seq)
366 if (merge_ref(trans, delayed_refs, head, ref, seq))
367 node = rb_first(&head->ref_root);
369 node = rb_next(&ref->rb_node);
373 int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info,
374 struct btrfs_delayed_ref_root *delayed_refs,
377 struct seq_list *elem;
380 spin_lock(&fs_info->tree_mod_seq_lock);
381 if (!list_empty(&fs_info->tree_mod_seq_list)) {
382 elem = list_first_entry(&fs_info->tree_mod_seq_list,
383 struct seq_list, list);
384 if (seq >= elem->seq) {
385 pr_debug("holding back delayed_ref %#x.%x, lowest is %#x.%x (%p)\n",
386 (u32)(seq >> 32), (u32)seq,
387 (u32)(elem->seq >> 32), (u32)elem->seq,
393 spin_unlock(&fs_info->tree_mod_seq_lock);
397 struct btrfs_delayed_ref_head *
398 btrfs_select_ref_head(struct btrfs_trans_handle *trans)
400 struct btrfs_delayed_ref_root *delayed_refs;
401 struct btrfs_delayed_ref_head *head;
405 delayed_refs = &trans->transaction->delayed_refs;
408 start = delayed_refs->run_delayed_start;
409 head = find_ref_head(&delayed_refs->href_root, start, 1);
410 if (!head && !loop) {
411 delayed_refs->run_delayed_start = 0;
414 head = find_ref_head(&delayed_refs->href_root, start, 1);
417 } else if (!head && loop) {
421 while (head->processing) {
422 struct rb_node *node;
424 node = rb_next(&head->href_node);
428 delayed_refs->run_delayed_start = 0;
433 head = rb_entry(node, struct btrfs_delayed_ref_head,
437 head->processing = 1;
438 WARN_ON(delayed_refs->num_heads_ready == 0);
439 delayed_refs->num_heads_ready--;
440 delayed_refs->run_delayed_start = head->node.bytenr +
441 head->node.num_bytes;
446 * helper function to update an extent delayed ref in the
447 * rbtree. existing and update must both have the same
450 * This may free existing if the update cancels out whatever
451 * operation it was doing.
454 update_existing_ref(struct btrfs_trans_handle *trans,
455 struct btrfs_delayed_ref_root *delayed_refs,
456 struct btrfs_delayed_ref_head *head,
457 struct btrfs_delayed_ref_node *existing,
458 struct btrfs_delayed_ref_node *update)
460 if (update->action != existing->action) {
462 * this is effectively undoing either an add or a
463 * drop. We decrement the ref_mod, and if it goes
464 * down to zero we just delete the entry without
465 * every changing the extent allocation tree.
468 if (existing->ref_mod == 0)
469 drop_delayed_ref(trans, delayed_refs, head, existing);
471 WARN_ON(existing->type == BTRFS_TREE_BLOCK_REF_KEY ||
472 existing->type == BTRFS_SHARED_BLOCK_REF_KEY);
474 WARN_ON(existing->type == BTRFS_TREE_BLOCK_REF_KEY ||
475 existing->type == BTRFS_SHARED_BLOCK_REF_KEY);
477 * the action on the existing ref matches
478 * the action on the ref we're trying to add.
479 * Bump the ref_mod by one so the backref that
480 * is eventually added/removed has the correct
483 existing->ref_mod += update->ref_mod;
488 * helper function to update the accounting in the head ref
489 * existing and update must have the same bytenr
492 update_existing_head_ref(struct btrfs_delayed_ref_node *existing,
493 struct btrfs_delayed_ref_node *update)
495 struct btrfs_delayed_ref_head *existing_ref;
496 struct btrfs_delayed_ref_head *ref;
498 existing_ref = btrfs_delayed_node_to_head(existing);
499 ref = btrfs_delayed_node_to_head(update);
500 BUG_ON(existing_ref->is_data != ref->is_data);
502 spin_lock(&existing_ref->lock);
503 if (ref->must_insert_reserved) {
504 /* if the extent was freed and then
505 * reallocated before the delayed ref
506 * entries were processed, we can end up
507 * with an existing head ref without
508 * the must_insert_reserved flag set.
511 existing_ref->must_insert_reserved = ref->must_insert_reserved;
514 * update the num_bytes so we make sure the accounting
517 existing->num_bytes = update->num_bytes;
521 if (ref->extent_op) {
522 if (!existing_ref->extent_op) {
523 existing_ref->extent_op = ref->extent_op;
525 if (ref->extent_op->update_key) {
526 memcpy(&existing_ref->extent_op->key,
527 &ref->extent_op->key,
528 sizeof(ref->extent_op->key));
529 existing_ref->extent_op->update_key = 1;
531 if (ref->extent_op->update_flags) {
532 existing_ref->extent_op->flags_to_set |=
533 ref->extent_op->flags_to_set;
534 existing_ref->extent_op->update_flags = 1;
536 btrfs_free_delayed_extent_op(ref->extent_op);
540 * update the reference mod on the head to reflect this new operation,
541 * only need the lock for this case cause we could be processing it
542 * currently, for refs we just added we know we're a-ok.
544 existing->ref_mod += update->ref_mod;
545 spin_unlock(&existing_ref->lock);
549 * helper function to actually insert a head node into the rbtree.
550 * this does all the dirty work in terms of maintaining the correct
551 * overall modification count.
553 static noinline struct btrfs_delayed_ref_head *
554 add_delayed_ref_head(struct btrfs_fs_info *fs_info,
555 struct btrfs_trans_handle *trans,
556 struct btrfs_delayed_ref_node *ref, u64 bytenr,
557 u64 num_bytes, int action, int is_data)
559 struct btrfs_delayed_ref_head *existing;
560 struct btrfs_delayed_ref_head *head_ref = NULL;
561 struct btrfs_delayed_ref_root *delayed_refs;
563 int must_insert_reserved = 0;
566 * the head node stores the sum of all the mods, so dropping a ref
567 * should drop the sum in the head node by one.
569 if (action == BTRFS_UPDATE_DELAYED_HEAD)
571 else if (action == BTRFS_DROP_DELAYED_REF)
575 * BTRFS_ADD_DELAYED_EXTENT means that we need to update
576 * the reserved accounting when the extent is finally added, or
577 * if a later modification deletes the delayed ref without ever
578 * inserting the extent into the extent allocation tree.
579 * ref->must_insert_reserved is the flag used to record
580 * that accounting mods are required.
582 * Once we record must_insert_reserved, switch the action to
583 * BTRFS_ADD_DELAYED_REF because other special casing is not required.
585 if (action == BTRFS_ADD_DELAYED_EXTENT)
586 must_insert_reserved = 1;
588 must_insert_reserved = 0;
590 delayed_refs = &trans->transaction->delayed_refs;
592 /* first set the basic ref node struct up */
593 atomic_set(&ref->refs, 1);
594 ref->bytenr = bytenr;
595 ref->num_bytes = num_bytes;
596 ref->ref_mod = count_mod;
603 head_ref = btrfs_delayed_node_to_head(ref);
604 head_ref->must_insert_reserved = must_insert_reserved;
605 head_ref->is_data = is_data;
606 head_ref->ref_root = RB_ROOT;
607 head_ref->processing = 0;
609 spin_lock_init(&head_ref->lock);
610 mutex_init(&head_ref->mutex);
612 trace_add_delayed_ref_head(ref, head_ref, action);
614 existing = htree_insert(&delayed_refs->href_root,
615 &head_ref->href_node);
617 update_existing_head_ref(&existing->node, ref);
619 * we've updated the existing ref, free the newly
622 kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref);
625 delayed_refs->num_heads++;
626 delayed_refs->num_heads_ready++;
627 atomic_inc(&delayed_refs->num_entries);
628 trans->delayed_ref_updates++;
634 * helper to insert a delayed tree ref into the rbtree.
637 add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
638 struct btrfs_trans_handle *trans,
639 struct btrfs_delayed_ref_head *head_ref,
640 struct btrfs_delayed_ref_node *ref, u64 bytenr,
641 u64 num_bytes, u64 parent, u64 ref_root, int level,
642 int action, int no_quota)
644 struct btrfs_delayed_ref_node *existing;
645 struct btrfs_delayed_tree_ref *full_ref;
646 struct btrfs_delayed_ref_root *delayed_refs;
649 if (action == BTRFS_ADD_DELAYED_EXTENT)
650 action = BTRFS_ADD_DELAYED_REF;
652 if (is_fstree(ref_root))
653 seq = atomic64_read(&fs_info->tree_mod_seq);
654 delayed_refs = &trans->transaction->delayed_refs;
656 /* first set the basic ref node struct up */
657 atomic_set(&ref->refs, 1);
658 ref->bytenr = bytenr;
659 ref->num_bytes = num_bytes;
661 ref->action = action;
664 ref->no_quota = no_quota;
667 full_ref = btrfs_delayed_node_to_tree_ref(ref);
668 full_ref->parent = parent;
669 full_ref->root = ref_root;
671 ref->type = BTRFS_SHARED_BLOCK_REF_KEY;
673 ref->type = BTRFS_TREE_BLOCK_REF_KEY;
674 full_ref->level = level;
676 trace_add_delayed_tree_ref(ref, full_ref, action);
678 spin_lock(&head_ref->lock);
679 existing = tree_insert(&head_ref->ref_root, &ref->rb_node);
681 update_existing_ref(trans, delayed_refs, head_ref, existing,
684 * we've updated the existing ref, free the newly
687 kmem_cache_free(btrfs_delayed_tree_ref_cachep, full_ref);
689 atomic_inc(&delayed_refs->num_entries);
690 trans->delayed_ref_updates++;
692 spin_unlock(&head_ref->lock);
696 * helper to insert a delayed data ref into the rbtree.
699 add_delayed_data_ref(struct btrfs_fs_info *fs_info,
700 struct btrfs_trans_handle *trans,
701 struct btrfs_delayed_ref_head *head_ref,
702 struct btrfs_delayed_ref_node *ref, u64 bytenr,
703 u64 num_bytes, u64 parent, u64 ref_root, u64 owner,
704 u64 offset, int action, int no_quota)
706 struct btrfs_delayed_ref_node *existing;
707 struct btrfs_delayed_data_ref *full_ref;
708 struct btrfs_delayed_ref_root *delayed_refs;
711 if (action == BTRFS_ADD_DELAYED_EXTENT)
712 action = BTRFS_ADD_DELAYED_REF;
714 delayed_refs = &trans->transaction->delayed_refs;
716 if (is_fstree(ref_root))
717 seq = atomic64_read(&fs_info->tree_mod_seq);
719 /* first set the basic ref node struct up */
720 atomic_set(&ref->refs, 1);
721 ref->bytenr = bytenr;
722 ref->num_bytes = num_bytes;
724 ref->action = action;
727 ref->no_quota = no_quota;
730 full_ref = btrfs_delayed_node_to_data_ref(ref);
731 full_ref->parent = parent;
732 full_ref->root = ref_root;
734 ref->type = BTRFS_SHARED_DATA_REF_KEY;
736 ref->type = BTRFS_EXTENT_DATA_REF_KEY;
738 full_ref->objectid = owner;
739 full_ref->offset = offset;
741 trace_add_delayed_data_ref(ref, full_ref, action);
743 spin_lock(&head_ref->lock);
744 existing = tree_insert(&head_ref->ref_root, &ref->rb_node);
746 update_existing_ref(trans, delayed_refs, head_ref, existing,
749 * we've updated the existing ref, free the newly
752 kmem_cache_free(btrfs_delayed_data_ref_cachep, full_ref);
754 atomic_inc(&delayed_refs->num_entries);
755 trans->delayed_ref_updates++;
757 spin_unlock(&head_ref->lock);
761 * add a delayed tree ref. This does all of the accounting required
762 * to make sure the delayed ref is eventually processed before this
763 * transaction commits.
765 int btrfs_add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
766 struct btrfs_trans_handle *trans,
767 u64 bytenr, u64 num_bytes, u64 parent,
768 u64 ref_root, int level, int action,
769 struct btrfs_delayed_extent_op *extent_op,
772 struct btrfs_delayed_tree_ref *ref;
773 struct btrfs_delayed_ref_head *head_ref;
774 struct btrfs_delayed_ref_root *delayed_refs;
776 if (!is_fstree(ref_root) || !fs_info->quota_enabled)
779 BUG_ON(extent_op && extent_op->is_data);
780 ref = kmem_cache_alloc(btrfs_delayed_tree_ref_cachep, GFP_NOFS);
784 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
786 kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
790 head_ref->extent_op = extent_op;
792 delayed_refs = &trans->transaction->delayed_refs;
793 spin_lock(&delayed_refs->lock);
796 * insert both the head node and the new ref without dropping
799 head_ref = add_delayed_ref_head(fs_info, trans, &head_ref->node,
800 bytenr, num_bytes, action, 0);
802 add_delayed_tree_ref(fs_info, trans, head_ref, &ref->node, bytenr,
803 num_bytes, parent, ref_root, level, action,
805 spin_unlock(&delayed_refs->lock);
811 * add a delayed data ref. it's similar to btrfs_add_delayed_tree_ref.
813 int btrfs_add_delayed_data_ref(struct btrfs_fs_info *fs_info,
814 struct btrfs_trans_handle *trans,
815 u64 bytenr, u64 num_bytes,
816 u64 parent, u64 ref_root,
817 u64 owner, u64 offset, int action,
818 struct btrfs_delayed_extent_op *extent_op,
821 struct btrfs_delayed_data_ref *ref;
822 struct btrfs_delayed_ref_head *head_ref;
823 struct btrfs_delayed_ref_root *delayed_refs;
825 if (!is_fstree(ref_root) || !fs_info->quota_enabled)
828 BUG_ON(extent_op && !extent_op->is_data);
829 ref = kmem_cache_alloc(btrfs_delayed_data_ref_cachep, GFP_NOFS);
833 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
835 kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
839 head_ref->extent_op = extent_op;
841 delayed_refs = &trans->transaction->delayed_refs;
842 spin_lock(&delayed_refs->lock);
845 * insert both the head node and the new ref without dropping
848 head_ref = add_delayed_ref_head(fs_info, trans, &head_ref->node,
849 bytenr, num_bytes, action, 1);
851 add_delayed_data_ref(fs_info, trans, head_ref, &ref->node, bytenr,
852 num_bytes, parent, ref_root, owner, offset,
854 spin_unlock(&delayed_refs->lock);
859 int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info,
860 struct btrfs_trans_handle *trans,
861 u64 bytenr, u64 num_bytes,
862 struct btrfs_delayed_extent_op *extent_op)
864 struct btrfs_delayed_ref_head *head_ref;
865 struct btrfs_delayed_ref_root *delayed_refs;
867 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
871 head_ref->extent_op = extent_op;
873 delayed_refs = &trans->transaction->delayed_refs;
874 spin_lock(&delayed_refs->lock);
876 add_delayed_ref_head(fs_info, trans, &head_ref->node, bytenr,
877 num_bytes, BTRFS_UPDATE_DELAYED_HEAD,
880 spin_unlock(&delayed_refs->lock);
885 * this does a simple search for the head node for a given extent.
886 * It must be called with the delayed ref spinlock held, and it returns
887 * the head node if any where found, or NULL if not.
889 struct btrfs_delayed_ref_head *
890 btrfs_find_delayed_ref_head(struct btrfs_trans_handle *trans, u64 bytenr)
892 struct btrfs_delayed_ref_root *delayed_refs;
894 delayed_refs = &trans->transaction->delayed_refs;
895 return find_ref_head(&delayed_refs->href_root, bytenr, 0);
898 void btrfs_delayed_ref_exit(void)
900 if (btrfs_delayed_ref_head_cachep)
901 kmem_cache_destroy(btrfs_delayed_ref_head_cachep);
902 if (btrfs_delayed_tree_ref_cachep)
903 kmem_cache_destroy(btrfs_delayed_tree_ref_cachep);
904 if (btrfs_delayed_data_ref_cachep)
905 kmem_cache_destroy(btrfs_delayed_data_ref_cachep);
906 if (btrfs_delayed_extent_op_cachep)
907 kmem_cache_destroy(btrfs_delayed_extent_op_cachep);
910 int btrfs_delayed_ref_init(void)
912 btrfs_delayed_ref_head_cachep = kmem_cache_create(
913 "btrfs_delayed_ref_head",
914 sizeof(struct btrfs_delayed_ref_head), 0,
915 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
916 if (!btrfs_delayed_ref_head_cachep)
919 btrfs_delayed_tree_ref_cachep = kmem_cache_create(
920 "btrfs_delayed_tree_ref",
921 sizeof(struct btrfs_delayed_tree_ref), 0,
922 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
923 if (!btrfs_delayed_tree_ref_cachep)
926 btrfs_delayed_data_ref_cachep = kmem_cache_create(
927 "btrfs_delayed_data_ref",
928 sizeof(struct btrfs_delayed_data_ref), 0,
929 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
930 if (!btrfs_delayed_data_ref_cachep)
933 btrfs_delayed_extent_op_cachep = kmem_cache_create(
934 "btrfs_delayed_extent_op",
935 sizeof(struct btrfs_delayed_extent_op), 0,
936 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
937 if (!btrfs_delayed_extent_op_cachep)
942 btrfs_delayed_ref_exit();