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 /* merging of sequenced refs is not allowed */
111 if (ref1->seq < ref2->seq)
113 if (ref1->seq > ref2->seq)
116 if (ref1->type == BTRFS_TREE_BLOCK_REF_KEY ||
117 ref1->type == BTRFS_SHARED_BLOCK_REF_KEY) {
118 return comp_tree_refs(btrfs_delayed_node_to_tree_ref(ref2),
119 btrfs_delayed_node_to_tree_ref(ref1),
121 } else if (ref1->type == BTRFS_EXTENT_DATA_REF_KEY ||
122 ref1->type == BTRFS_SHARED_DATA_REF_KEY) {
123 return comp_data_refs(btrfs_delayed_node_to_data_ref(ref2),
124 btrfs_delayed_node_to_data_ref(ref1));
131 * insert a new ref into the rbtree. This returns any existing refs
132 * for the same (bytenr,parent) tuple, or NULL if the new node was properly
135 static struct btrfs_delayed_ref_node *tree_insert(struct rb_root *root,
136 struct rb_node *node)
138 struct rb_node **p = &root->rb_node;
139 struct rb_node *parent_node = NULL;
140 struct btrfs_delayed_ref_node *entry;
141 struct btrfs_delayed_ref_node *ins;
144 ins = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
147 entry = rb_entry(parent_node, struct btrfs_delayed_ref_node,
150 cmp = comp_entry(entry, ins, 1);
159 rb_link_node(node, parent_node, p);
160 rb_insert_color(node, root);
164 /* insert a new ref to head ref rbtree */
165 static struct btrfs_delayed_ref_head *htree_insert(struct rb_root *root,
166 struct rb_node *node)
168 struct rb_node **p = &root->rb_node;
169 struct rb_node *parent_node = NULL;
170 struct btrfs_delayed_ref_head *entry;
171 struct btrfs_delayed_ref_head *ins;
174 ins = rb_entry(node, struct btrfs_delayed_ref_head, href_node);
175 bytenr = ins->node.bytenr;
178 entry = rb_entry(parent_node, struct btrfs_delayed_ref_head,
181 if (bytenr < entry->node.bytenr)
183 else if (bytenr > entry->node.bytenr)
189 rb_link_node(node, parent_node, p);
190 rb_insert_color(node, root);
195 * find an head entry based on bytenr. This returns the delayed ref
196 * head if it was able to find one, or NULL if nothing was in that spot.
197 * If return_bigger is given, the next bigger entry is returned if no exact
200 static struct btrfs_delayed_ref_head *
201 find_ref_head(struct rb_root *root, u64 bytenr,
202 struct btrfs_delayed_ref_head **last, int return_bigger)
205 struct btrfs_delayed_ref_head *entry;
211 entry = rb_entry(n, struct btrfs_delayed_ref_head, href_node);
215 if (bytenr < entry->node.bytenr)
217 else if (bytenr > entry->node.bytenr)
229 if (entry && return_bigger) {
231 n = rb_next(&entry->href_node);
234 entry = rb_entry(n, struct btrfs_delayed_ref_head,
245 int btrfs_delayed_ref_lock(struct btrfs_trans_handle *trans,
246 struct btrfs_delayed_ref_head *head)
248 struct btrfs_delayed_ref_root *delayed_refs;
250 delayed_refs = &trans->transaction->delayed_refs;
251 assert_spin_locked(&delayed_refs->lock);
252 if (mutex_trylock(&head->mutex))
255 atomic_inc(&head->node.refs);
256 spin_unlock(&delayed_refs->lock);
258 mutex_lock(&head->mutex);
259 spin_lock(&delayed_refs->lock);
260 if (!head->node.in_tree) {
261 mutex_unlock(&head->mutex);
262 btrfs_put_delayed_ref(&head->node);
265 btrfs_put_delayed_ref(&head->node);
269 static inline void drop_delayed_ref(struct btrfs_trans_handle *trans,
270 struct btrfs_delayed_ref_root *delayed_refs,
271 struct btrfs_delayed_ref_head *head,
272 struct btrfs_delayed_ref_node *ref)
274 if (btrfs_delayed_ref_is_head(ref)) {
275 head = btrfs_delayed_node_to_head(ref);
276 rb_erase(&head->href_node, &delayed_refs->href_root);
278 assert_spin_locked(&head->lock);
279 rb_erase(&ref->rb_node, &head->ref_root);
282 btrfs_put_delayed_ref(ref);
283 atomic_dec(&delayed_refs->num_entries);
284 if (trans->delayed_ref_updates)
285 trans->delayed_ref_updates--;
288 static int merge_ref(struct btrfs_trans_handle *trans,
289 struct btrfs_delayed_ref_root *delayed_refs,
290 struct btrfs_delayed_ref_head *head,
291 struct btrfs_delayed_ref_node *ref, u64 seq)
293 struct rb_node *node;
297 node = rb_next(&ref->rb_node);
298 while (!done && node) {
299 struct btrfs_delayed_ref_node *next;
301 next = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
302 node = rb_next(node);
303 if (seq && next->seq >= seq)
305 if (comp_entry(ref, next, 0))
308 if (ref->action == next->action) {
311 if (ref->ref_mod < next->ref_mod) {
312 struct btrfs_delayed_ref_node *tmp;
319 mod = -next->ref_mod;
322 drop_delayed_ref(trans, delayed_refs, head, next);
324 if (ref->ref_mod == 0) {
325 drop_delayed_ref(trans, delayed_refs, head, ref);
329 * You can't have multiples of the same ref on a tree
332 WARN_ON(ref->type == BTRFS_TREE_BLOCK_REF_KEY ||
333 ref->type == BTRFS_SHARED_BLOCK_REF_KEY);
339 void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
340 struct btrfs_fs_info *fs_info,
341 struct btrfs_delayed_ref_root *delayed_refs,
342 struct btrfs_delayed_ref_head *head)
344 struct rb_node *node;
347 assert_spin_locked(&head->lock);
349 * We don't have too much refs to merge in the case of delayed data
355 spin_lock(&fs_info->tree_mod_seq_lock);
356 if (!list_empty(&fs_info->tree_mod_seq_list)) {
357 struct seq_list *elem;
359 elem = list_first_entry(&fs_info->tree_mod_seq_list,
360 struct seq_list, list);
363 spin_unlock(&fs_info->tree_mod_seq_lock);
365 node = rb_first(&head->ref_root);
367 struct btrfs_delayed_ref_node *ref;
369 ref = rb_entry(node, struct btrfs_delayed_ref_node,
371 /* We can't merge refs that are outside of our seq count */
372 if (seq && ref->seq >= seq)
374 if (merge_ref(trans, delayed_refs, head, ref, seq))
375 node = rb_first(&head->ref_root);
377 node = rb_next(&ref->rb_node);
381 int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info,
382 struct btrfs_delayed_ref_root *delayed_refs,
385 struct seq_list *elem;
388 spin_lock(&fs_info->tree_mod_seq_lock);
389 if (!list_empty(&fs_info->tree_mod_seq_list)) {
390 elem = list_first_entry(&fs_info->tree_mod_seq_list,
391 struct seq_list, list);
392 if (seq >= elem->seq) {
393 pr_debug("holding back delayed_ref %#x.%x, lowest is %#x.%x (%p)\n",
394 (u32)(seq >> 32), (u32)seq,
395 (u32)(elem->seq >> 32), (u32)elem->seq,
401 spin_unlock(&fs_info->tree_mod_seq_lock);
405 struct btrfs_delayed_ref_head *
406 btrfs_select_ref_head(struct btrfs_trans_handle *trans)
408 struct btrfs_delayed_ref_root *delayed_refs;
409 struct btrfs_delayed_ref_head *head;
413 delayed_refs = &trans->transaction->delayed_refs;
416 start = delayed_refs->run_delayed_start;
417 head = find_ref_head(&delayed_refs->href_root, start, NULL, 1);
418 if (!head && !loop) {
419 delayed_refs->run_delayed_start = 0;
422 head = find_ref_head(&delayed_refs->href_root, start, NULL, 1);
425 } else if (!head && loop) {
429 while (head->processing) {
430 struct rb_node *node;
432 node = rb_next(&head->href_node);
436 delayed_refs->run_delayed_start = 0;
441 head = rb_entry(node, struct btrfs_delayed_ref_head,
445 head->processing = 1;
446 WARN_ON(delayed_refs->num_heads_ready == 0);
447 delayed_refs->num_heads_ready--;
448 delayed_refs->run_delayed_start = head->node.bytenr +
449 head->node.num_bytes;
454 * helper function to update an extent delayed ref in the
455 * rbtree. existing and update must both have the same
458 * This may free existing if the update cancels out whatever
459 * operation it was doing.
462 update_existing_ref(struct btrfs_trans_handle *trans,
463 struct btrfs_delayed_ref_root *delayed_refs,
464 struct btrfs_delayed_ref_head *head,
465 struct btrfs_delayed_ref_node *existing,
466 struct btrfs_delayed_ref_node *update)
468 if (update->action != existing->action) {
470 * this is effectively undoing either an add or a
471 * drop. We decrement the ref_mod, and if it goes
472 * down to zero we just delete the entry without
473 * every changing the extent allocation tree.
476 if (existing->ref_mod == 0)
477 drop_delayed_ref(trans, delayed_refs, head, existing);
479 WARN_ON(existing->type == BTRFS_TREE_BLOCK_REF_KEY ||
480 existing->type == BTRFS_SHARED_BLOCK_REF_KEY);
482 WARN_ON(existing->type == BTRFS_TREE_BLOCK_REF_KEY ||
483 existing->type == BTRFS_SHARED_BLOCK_REF_KEY);
485 * the action on the existing ref matches
486 * the action on the ref we're trying to add.
487 * Bump the ref_mod by one so the backref that
488 * is eventually added/removed has the correct
491 existing->ref_mod += update->ref_mod;
496 * helper function to update the accounting in the head ref
497 * existing and update must have the same bytenr
500 update_existing_head_ref(struct btrfs_delayed_ref_node *existing,
501 struct btrfs_delayed_ref_node *update)
503 struct btrfs_delayed_ref_head *existing_ref;
504 struct btrfs_delayed_ref_head *ref;
506 existing_ref = btrfs_delayed_node_to_head(existing);
507 ref = btrfs_delayed_node_to_head(update);
508 BUG_ON(existing_ref->is_data != ref->is_data);
510 if (ref->must_insert_reserved) {
511 /* if the extent was freed and then
512 * reallocated before the delayed ref
513 * entries were processed, we can end up
514 * with an existing head ref without
515 * the must_insert_reserved flag set.
518 existing_ref->must_insert_reserved = ref->must_insert_reserved;
521 * update the num_bytes so we make sure the accounting
524 existing->num_bytes = update->num_bytes;
528 if (ref->extent_op) {
529 if (!existing_ref->extent_op) {
530 existing_ref->extent_op = ref->extent_op;
532 if (ref->extent_op->update_key) {
533 memcpy(&existing_ref->extent_op->key,
534 &ref->extent_op->key,
535 sizeof(ref->extent_op->key));
536 existing_ref->extent_op->update_key = 1;
538 if (ref->extent_op->update_flags) {
539 existing_ref->extent_op->flags_to_set |=
540 ref->extent_op->flags_to_set;
541 existing_ref->extent_op->update_flags = 1;
543 btrfs_free_delayed_extent_op(ref->extent_op);
547 * update the reference mod on the head to reflect this new operation,
548 * only need the lock for this case cause we could be processing it
549 * currently, for refs we just added we know we're a-ok.
551 spin_lock(&existing_ref->lock);
552 existing->ref_mod += update->ref_mod;
553 spin_unlock(&existing_ref->lock);
557 * helper function to actually insert a head node into the rbtree.
558 * this does all the dirty work in terms of maintaining the correct
559 * overall modification count.
561 static noinline struct btrfs_delayed_ref_head *
562 add_delayed_ref_head(struct btrfs_fs_info *fs_info,
563 struct btrfs_trans_handle *trans,
564 struct btrfs_delayed_ref_node *ref, u64 bytenr,
565 u64 num_bytes, int action, int is_data)
567 struct btrfs_delayed_ref_head *existing;
568 struct btrfs_delayed_ref_head *head_ref = NULL;
569 struct btrfs_delayed_ref_root *delayed_refs;
571 int must_insert_reserved = 0;
574 * the head node stores the sum of all the mods, so dropping a ref
575 * should drop the sum in the head node by one.
577 if (action == BTRFS_UPDATE_DELAYED_HEAD)
579 else if (action == BTRFS_DROP_DELAYED_REF)
583 * BTRFS_ADD_DELAYED_EXTENT means that we need to update
584 * the reserved accounting when the extent is finally added, or
585 * if a later modification deletes the delayed ref without ever
586 * inserting the extent into the extent allocation tree.
587 * ref->must_insert_reserved is the flag used to record
588 * that accounting mods are required.
590 * Once we record must_insert_reserved, switch the action to
591 * BTRFS_ADD_DELAYED_REF because other special casing is not required.
593 if (action == BTRFS_ADD_DELAYED_EXTENT)
594 must_insert_reserved = 1;
596 must_insert_reserved = 0;
598 delayed_refs = &trans->transaction->delayed_refs;
600 /* first set the basic ref node struct up */
601 atomic_set(&ref->refs, 1);
602 ref->bytenr = bytenr;
603 ref->num_bytes = num_bytes;
604 ref->ref_mod = count_mod;
611 head_ref = btrfs_delayed_node_to_head(ref);
612 head_ref->must_insert_reserved = must_insert_reserved;
613 head_ref->is_data = is_data;
614 head_ref->ref_root = RB_ROOT;
615 head_ref->processing = 0;
617 spin_lock_init(&head_ref->lock);
618 mutex_init(&head_ref->mutex);
620 trace_add_delayed_ref_head(ref, head_ref, action);
622 existing = htree_insert(&delayed_refs->href_root,
623 &head_ref->href_node);
625 update_existing_head_ref(&existing->node, ref);
627 * we've updated the existing ref, free the newly
630 kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref);
633 delayed_refs->num_heads++;
634 delayed_refs->num_heads_ready++;
635 atomic_inc(&delayed_refs->num_entries);
636 trans->delayed_ref_updates++;
642 * helper to insert a delayed tree ref into the rbtree.
645 add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
646 struct btrfs_trans_handle *trans,
647 struct btrfs_delayed_ref_head *head_ref,
648 struct btrfs_delayed_ref_node *ref, u64 bytenr,
649 u64 num_bytes, u64 parent, u64 ref_root, int level,
650 int action, int for_cow)
652 struct btrfs_delayed_ref_node *existing;
653 struct btrfs_delayed_tree_ref *full_ref;
654 struct btrfs_delayed_ref_root *delayed_refs;
657 if (action == BTRFS_ADD_DELAYED_EXTENT)
658 action = BTRFS_ADD_DELAYED_REF;
660 delayed_refs = &trans->transaction->delayed_refs;
662 /* first set the basic ref node struct up */
663 atomic_set(&ref->refs, 1);
664 ref->bytenr = bytenr;
665 ref->num_bytes = num_bytes;
667 ref->action = action;
671 if (need_ref_seq(for_cow, ref_root))
672 seq = btrfs_get_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
675 full_ref = btrfs_delayed_node_to_tree_ref(ref);
676 full_ref->parent = parent;
677 full_ref->root = ref_root;
679 ref->type = BTRFS_SHARED_BLOCK_REF_KEY;
681 ref->type = BTRFS_TREE_BLOCK_REF_KEY;
682 full_ref->level = level;
684 trace_add_delayed_tree_ref(ref, full_ref, action);
686 spin_lock(&head_ref->lock);
687 existing = tree_insert(&head_ref->ref_root, &ref->rb_node);
689 update_existing_ref(trans, delayed_refs, head_ref, existing,
692 * we've updated the existing ref, free the newly
695 kmem_cache_free(btrfs_delayed_tree_ref_cachep, full_ref);
697 atomic_inc(&delayed_refs->num_entries);
698 trans->delayed_ref_updates++;
700 spin_unlock(&head_ref->lock);
704 * helper to insert a delayed data ref into the rbtree.
707 add_delayed_data_ref(struct btrfs_fs_info *fs_info,
708 struct btrfs_trans_handle *trans,
709 struct btrfs_delayed_ref_head *head_ref,
710 struct btrfs_delayed_ref_node *ref, u64 bytenr,
711 u64 num_bytes, u64 parent, u64 ref_root, u64 owner,
712 u64 offset, int action, int for_cow)
714 struct btrfs_delayed_ref_node *existing;
715 struct btrfs_delayed_data_ref *full_ref;
716 struct btrfs_delayed_ref_root *delayed_refs;
719 if (action == BTRFS_ADD_DELAYED_EXTENT)
720 action = BTRFS_ADD_DELAYED_REF;
722 delayed_refs = &trans->transaction->delayed_refs;
724 /* first set the basic ref node struct up */
725 atomic_set(&ref->refs, 1);
726 ref->bytenr = bytenr;
727 ref->num_bytes = num_bytes;
729 ref->action = action;
733 if (need_ref_seq(for_cow, ref_root))
734 seq = btrfs_get_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
737 full_ref = btrfs_delayed_node_to_data_ref(ref);
738 full_ref->parent = parent;
739 full_ref->root = ref_root;
741 ref->type = BTRFS_SHARED_DATA_REF_KEY;
743 ref->type = BTRFS_EXTENT_DATA_REF_KEY;
745 full_ref->objectid = owner;
746 full_ref->offset = offset;
748 trace_add_delayed_data_ref(ref, full_ref, action);
750 spin_lock(&head_ref->lock);
751 existing = tree_insert(&head_ref->ref_root, &ref->rb_node);
753 update_existing_ref(trans, delayed_refs, head_ref, existing,
756 * we've updated the existing ref, free the newly
759 kmem_cache_free(btrfs_delayed_data_ref_cachep, full_ref);
761 atomic_inc(&delayed_refs->num_entries);
762 trans->delayed_ref_updates++;
764 spin_unlock(&head_ref->lock);
768 * add a delayed tree ref. This does all of the accounting required
769 * to make sure the delayed ref is eventually processed before this
770 * transaction commits.
772 int btrfs_add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
773 struct btrfs_trans_handle *trans,
774 u64 bytenr, u64 num_bytes, u64 parent,
775 u64 ref_root, int level, int action,
776 struct btrfs_delayed_extent_op *extent_op,
779 struct btrfs_delayed_tree_ref *ref;
780 struct btrfs_delayed_ref_head *head_ref;
781 struct btrfs_delayed_ref_root *delayed_refs;
783 BUG_ON(extent_op && extent_op->is_data);
784 ref = kmem_cache_alloc(btrfs_delayed_tree_ref_cachep, GFP_NOFS);
788 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
790 kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
794 head_ref->extent_op = extent_op;
796 delayed_refs = &trans->transaction->delayed_refs;
797 spin_lock(&delayed_refs->lock);
800 * insert both the head node and the new ref without dropping
803 head_ref = add_delayed_ref_head(fs_info, trans, &head_ref->node,
804 bytenr, num_bytes, action, 0);
806 add_delayed_tree_ref(fs_info, trans, head_ref, &ref->node, bytenr,
807 num_bytes, parent, ref_root, level, action,
809 spin_unlock(&delayed_refs->lock);
810 if (need_ref_seq(for_cow, ref_root))
811 btrfs_qgroup_record_ref(trans, &ref->node, extent_op);
817 * add a delayed data ref. it's similar to btrfs_add_delayed_tree_ref.
819 int btrfs_add_delayed_data_ref(struct btrfs_fs_info *fs_info,
820 struct btrfs_trans_handle *trans,
821 u64 bytenr, u64 num_bytes,
822 u64 parent, u64 ref_root,
823 u64 owner, u64 offset, int action,
824 struct btrfs_delayed_extent_op *extent_op,
827 struct btrfs_delayed_data_ref *ref;
828 struct btrfs_delayed_ref_head *head_ref;
829 struct btrfs_delayed_ref_root *delayed_refs;
831 BUG_ON(extent_op && !extent_op->is_data);
832 ref = kmem_cache_alloc(btrfs_delayed_data_ref_cachep, GFP_NOFS);
836 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
838 kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
842 head_ref->extent_op = extent_op;
844 delayed_refs = &trans->transaction->delayed_refs;
845 spin_lock(&delayed_refs->lock);
848 * insert both the head node and the new ref without dropping
851 head_ref = add_delayed_ref_head(fs_info, trans, &head_ref->node,
852 bytenr, num_bytes, action, 1);
854 add_delayed_data_ref(fs_info, trans, head_ref, &ref->node, bytenr,
855 num_bytes, parent, ref_root, owner, offset,
857 spin_unlock(&delayed_refs->lock);
858 if (need_ref_seq(for_cow, ref_root))
859 btrfs_qgroup_record_ref(trans, &ref->node, extent_op);
864 int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info,
865 struct btrfs_trans_handle *trans,
866 u64 bytenr, u64 num_bytes,
867 struct btrfs_delayed_extent_op *extent_op)
869 struct btrfs_delayed_ref_head *head_ref;
870 struct btrfs_delayed_ref_root *delayed_refs;
872 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
876 head_ref->extent_op = extent_op;
878 delayed_refs = &trans->transaction->delayed_refs;
879 spin_lock(&delayed_refs->lock);
881 add_delayed_ref_head(fs_info, trans, &head_ref->node, bytenr,
882 num_bytes, BTRFS_UPDATE_DELAYED_HEAD,
885 spin_unlock(&delayed_refs->lock);
890 * this does a simple search for the head node for a given extent.
891 * It must be called with the delayed ref spinlock held, and it returns
892 * the head node if any where found, or NULL if not.
894 struct btrfs_delayed_ref_head *
895 btrfs_find_delayed_ref_head(struct btrfs_trans_handle *trans, u64 bytenr)
897 struct btrfs_delayed_ref_root *delayed_refs;
899 delayed_refs = &trans->transaction->delayed_refs;
900 return find_ref_head(&delayed_refs->href_root, bytenr, NULL, 0);
903 void btrfs_delayed_ref_exit(void)
905 if (btrfs_delayed_ref_head_cachep)
906 kmem_cache_destroy(btrfs_delayed_ref_head_cachep);
907 if (btrfs_delayed_tree_ref_cachep)
908 kmem_cache_destroy(btrfs_delayed_tree_ref_cachep);
909 if (btrfs_delayed_data_ref_cachep)
910 kmem_cache_destroy(btrfs_delayed_data_ref_cachep);
911 if (btrfs_delayed_extent_op_cachep)
912 kmem_cache_destroy(btrfs_delayed_extent_op_cachep);
915 int btrfs_delayed_ref_init(void)
917 btrfs_delayed_ref_head_cachep = kmem_cache_create(
918 "btrfs_delayed_ref_head",
919 sizeof(struct btrfs_delayed_ref_head), 0,
920 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
921 if (!btrfs_delayed_ref_head_cachep)
924 btrfs_delayed_tree_ref_cachep = kmem_cache_create(
925 "btrfs_delayed_tree_ref",
926 sizeof(struct btrfs_delayed_tree_ref), 0,
927 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
928 if (!btrfs_delayed_tree_ref_cachep)
931 btrfs_delayed_data_ref_cachep = kmem_cache_create(
932 "btrfs_delayed_data_ref",
933 sizeof(struct btrfs_delayed_data_ref), 0,
934 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
935 if (!btrfs_delayed_data_ref_cachep)
938 btrfs_delayed_extent_op_cachep = kmem_cache_create(
939 "btrfs_delayed_extent_op",
940 sizeof(struct btrfs_delayed_extent_op), 0,
941 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
942 if (!btrfs_delayed_extent_op_cachep)
947 btrfs_delayed_ref_exit();