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,
655 int *old_ref_mod_ret)
657 struct btrfs_delayed_ref_root *delayed_refs =
658 &trans->transaction->delayed_refs;
659 struct btrfs_fs_info *fs_info = trans->fs_info;
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;
708 *old_ref_mod_ret = old_ref_mod;
709 existing->ref_mod += update->ref_mod;
710 existing->total_ref_mod += update->ref_mod;
713 * If we are going to from a positive ref mod to a negative or vice
714 * versa we need to make sure to adjust pending_csums accordingly.
716 if (existing->is_data) {
718 btrfs_csum_bytes_to_leaves(fs_info,
719 existing->num_bytes);
721 if (existing->total_ref_mod >= 0 && old_ref_mod < 0) {
722 delayed_refs->pending_csums -= existing->num_bytes;
723 btrfs_delayed_refs_rsv_release(fs_info, csum_leaves);
725 if (existing->total_ref_mod < 0 && old_ref_mod >= 0) {
726 delayed_refs->pending_csums += existing->num_bytes;
727 trans->delayed_ref_updates += csum_leaves;
730 spin_unlock(&existing->lock);
733 static void init_delayed_ref_head(struct btrfs_delayed_ref_head *head_ref,
734 struct btrfs_qgroup_extent_record *qrecord,
735 u64 bytenr, u64 num_bytes, u64 ref_root,
736 u64 reserved, int action, bool is_data,
740 int must_insert_reserved = 0;
742 /* If reserved is provided, it must be a data extent. */
743 BUG_ON(!is_data && reserved);
746 * The head node stores the sum of all the mods, so dropping a ref
747 * should drop the sum in the head node by one.
749 if (action == BTRFS_UPDATE_DELAYED_HEAD)
751 else if (action == BTRFS_DROP_DELAYED_REF)
755 * BTRFS_ADD_DELAYED_EXTENT means that we need to update the reserved
756 * accounting when the extent is finally added, or if a later
757 * modification deletes the delayed ref without ever inserting the
758 * extent into the extent allocation tree. ref->must_insert_reserved
759 * is the flag used to record that accounting mods are required.
761 * Once we record must_insert_reserved, switch the action to
762 * BTRFS_ADD_DELAYED_REF because other special casing is not required.
764 if (action == BTRFS_ADD_DELAYED_EXTENT)
765 must_insert_reserved = 1;
767 must_insert_reserved = 0;
769 refcount_set(&head_ref->refs, 1);
770 head_ref->bytenr = bytenr;
771 head_ref->num_bytes = num_bytes;
772 head_ref->ref_mod = count_mod;
773 head_ref->must_insert_reserved = must_insert_reserved;
774 head_ref->is_data = is_data;
775 head_ref->is_system = is_system;
776 head_ref->ref_tree = RB_ROOT_CACHED;
777 INIT_LIST_HEAD(&head_ref->ref_add_list);
778 RB_CLEAR_NODE(&head_ref->href_node);
779 head_ref->processing = 0;
780 head_ref->total_ref_mod = count_mod;
781 spin_lock_init(&head_ref->lock);
782 mutex_init(&head_ref->mutex);
785 if (ref_root && reserved) {
786 qrecord->data_rsv = reserved;
787 qrecord->data_rsv_refroot = ref_root;
789 qrecord->bytenr = bytenr;
790 qrecord->num_bytes = num_bytes;
791 qrecord->old_roots = NULL;
796 * helper function to actually insert a head node into the rbtree.
797 * this does all the dirty work in terms of maintaining the correct
798 * overall modification count.
800 static noinline struct btrfs_delayed_ref_head *
801 add_delayed_ref_head(struct btrfs_trans_handle *trans,
802 struct btrfs_delayed_ref_head *head_ref,
803 struct btrfs_qgroup_extent_record *qrecord,
804 int action, int *qrecord_inserted_ret,
805 int *old_ref_mod, int *new_ref_mod)
807 struct btrfs_delayed_ref_head *existing;
808 struct btrfs_delayed_ref_root *delayed_refs;
809 int qrecord_inserted = 0;
811 delayed_refs = &trans->transaction->delayed_refs;
813 /* Record qgroup extent info if provided */
815 if (btrfs_qgroup_trace_extent_nolock(trans->fs_info,
816 delayed_refs, qrecord))
819 qrecord_inserted = 1;
822 trace_add_delayed_ref_head(trans->fs_info, head_ref, action);
824 existing = htree_insert(&delayed_refs->href_root,
825 &head_ref->href_node);
827 update_existing_head_ref(trans, existing, head_ref,
830 * we've updated the existing ref, free the newly
833 kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref);
838 if (head_ref->is_data && head_ref->ref_mod < 0) {
839 delayed_refs->pending_csums += head_ref->num_bytes;
840 trans->delayed_ref_updates +=
841 btrfs_csum_bytes_to_leaves(trans->fs_info,
842 head_ref->num_bytes);
844 delayed_refs->num_heads++;
845 delayed_refs->num_heads_ready++;
846 atomic_inc(&delayed_refs->num_entries);
847 trans->delayed_ref_updates++;
849 if (qrecord_inserted_ret)
850 *qrecord_inserted_ret = qrecord_inserted;
852 *new_ref_mod = head_ref->total_ref_mod;
858 * init_delayed_ref_common - Initialize the structure which represents a
859 * modification to a an extent.
861 * @fs_info: Internal to the mounted filesystem mount structure.
863 * @ref: The structure which is going to be initialized.
865 * @bytenr: The logical address of the extent for which a modification is
866 * going to be recorded.
868 * @num_bytes: Size of the extent whose modification is being recorded.
870 * @ref_root: The id of the root where this modification has originated, this
871 * can be either one of the well-known metadata trees or the
872 * subvolume id which references this extent.
874 * @action: Can be one of BTRFS_ADD_DELAYED_REF/BTRFS_DROP_DELAYED_REF or
875 * BTRFS_ADD_DELAYED_EXTENT
877 * @ref_type: Holds the type of the extent which is being recorded, can be
878 * one of BTRFS_SHARED_BLOCK_REF_KEY/BTRFS_TREE_BLOCK_REF_KEY
879 * when recording a metadata extent or BTRFS_SHARED_DATA_REF_KEY/
880 * BTRFS_EXTENT_DATA_REF_KEY when recording data extent
882 static void init_delayed_ref_common(struct btrfs_fs_info *fs_info,
883 struct btrfs_delayed_ref_node *ref,
884 u64 bytenr, u64 num_bytes, u64 ref_root,
885 int action, u8 ref_type)
889 if (action == BTRFS_ADD_DELAYED_EXTENT)
890 action = BTRFS_ADD_DELAYED_REF;
892 if (is_fstree(ref_root))
893 seq = atomic64_read(&fs_info->tree_mod_seq);
895 refcount_set(&ref->refs, 1);
896 ref->bytenr = bytenr;
897 ref->num_bytes = num_bytes;
899 ref->action = action;
903 ref->type = ref_type;
904 RB_CLEAR_NODE(&ref->ref_node);
905 INIT_LIST_HEAD(&ref->add_list);
909 * add a delayed tree ref. This does all of the accounting required
910 * to make sure the delayed ref is eventually processed before this
911 * transaction commits.
913 int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans,
914 struct btrfs_ref *generic_ref,
915 struct btrfs_delayed_extent_op *extent_op,
916 int *old_ref_mod, int *new_ref_mod)
918 struct btrfs_fs_info *fs_info = trans->fs_info;
919 struct btrfs_delayed_tree_ref *ref;
920 struct btrfs_delayed_ref_head *head_ref;
921 struct btrfs_delayed_ref_root *delayed_refs;
922 struct btrfs_qgroup_extent_record *record = NULL;
923 int qrecord_inserted;
925 int action = generic_ref->action;
926 int level = generic_ref->tree_ref.level;
928 u64 bytenr = generic_ref->bytenr;
929 u64 num_bytes = generic_ref->len;
930 u64 parent = generic_ref->parent;
933 is_system = (generic_ref->real_root == BTRFS_CHUNK_TREE_OBJECTID);
935 ASSERT(generic_ref->type == BTRFS_REF_METADATA && generic_ref->action);
936 BUG_ON(extent_op && extent_op->is_data);
937 ref = kmem_cache_alloc(btrfs_delayed_tree_ref_cachep, GFP_NOFS);
941 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
943 kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
947 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&
948 is_fstree(generic_ref->real_root) &&
949 is_fstree(generic_ref->tree_ref.root) &&
950 !generic_ref->skip_qgroup) {
951 record = kzalloc(sizeof(*record), GFP_NOFS);
953 kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
954 kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref);
960 ref_type = BTRFS_SHARED_BLOCK_REF_KEY;
962 ref_type = BTRFS_TREE_BLOCK_REF_KEY;
964 init_delayed_ref_common(fs_info, &ref->node, bytenr, num_bytes,
965 generic_ref->tree_ref.root, action, ref_type);
966 ref->root = generic_ref->tree_ref.root;
967 ref->parent = parent;
970 init_delayed_ref_head(head_ref, record, bytenr, num_bytes,
971 generic_ref->tree_ref.root, 0, action, false,
973 head_ref->extent_op = extent_op;
975 delayed_refs = &trans->transaction->delayed_refs;
976 spin_lock(&delayed_refs->lock);
979 * insert both the head node and the new ref without dropping
982 head_ref = add_delayed_ref_head(trans, head_ref, record,
983 action, &qrecord_inserted,
984 old_ref_mod, new_ref_mod);
986 ret = insert_delayed_ref(trans, delayed_refs, head_ref, &ref->node);
987 spin_unlock(&delayed_refs->lock);
990 * Need to update the delayed_refs_rsv with any changes we may have
993 btrfs_update_delayed_refs_rsv(trans);
995 trace_add_delayed_tree_ref(fs_info, &ref->node, ref,
996 action == BTRFS_ADD_DELAYED_EXTENT ?
997 BTRFS_ADD_DELAYED_REF : action);
999 kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
1001 if (qrecord_inserted)
1002 btrfs_qgroup_trace_extent_post(fs_info, record);
1008 * add a delayed data ref. it's similar to btrfs_add_delayed_tree_ref.
1010 int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans,
1011 struct btrfs_ref *generic_ref,
1012 u64 reserved, int *old_ref_mod,
1015 struct btrfs_fs_info *fs_info = trans->fs_info;
1016 struct btrfs_delayed_data_ref *ref;
1017 struct btrfs_delayed_ref_head *head_ref;
1018 struct btrfs_delayed_ref_root *delayed_refs;
1019 struct btrfs_qgroup_extent_record *record = NULL;
1020 int qrecord_inserted;
1021 int action = generic_ref->action;
1023 u64 bytenr = generic_ref->bytenr;
1024 u64 num_bytes = generic_ref->len;
1025 u64 parent = generic_ref->parent;
1026 u64 ref_root = generic_ref->data_ref.ref_root;
1027 u64 owner = generic_ref->data_ref.ino;
1028 u64 offset = generic_ref->data_ref.offset;
1031 ASSERT(generic_ref->type == BTRFS_REF_DATA && action);
1032 ref = kmem_cache_alloc(btrfs_delayed_data_ref_cachep, GFP_NOFS);
1037 ref_type = BTRFS_SHARED_DATA_REF_KEY;
1039 ref_type = BTRFS_EXTENT_DATA_REF_KEY;
1040 init_delayed_ref_common(fs_info, &ref->node, bytenr, num_bytes,
1041 ref_root, action, ref_type);
1042 ref->root = ref_root;
1043 ref->parent = parent;
1044 ref->objectid = owner;
1045 ref->offset = offset;
1048 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
1050 kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
1054 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&
1055 is_fstree(ref_root) &&
1056 is_fstree(generic_ref->real_root) &&
1057 !generic_ref->skip_qgroup) {
1058 record = kzalloc(sizeof(*record), GFP_NOFS);
1060 kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
1061 kmem_cache_free(btrfs_delayed_ref_head_cachep,
1067 init_delayed_ref_head(head_ref, record, bytenr, num_bytes, ref_root,
1068 reserved, action, true, false);
1069 head_ref->extent_op = NULL;
1071 delayed_refs = &trans->transaction->delayed_refs;
1072 spin_lock(&delayed_refs->lock);
1075 * insert both the head node and the new ref without dropping
1078 head_ref = add_delayed_ref_head(trans, head_ref, record,
1079 action, &qrecord_inserted,
1080 old_ref_mod, new_ref_mod);
1082 ret = insert_delayed_ref(trans, delayed_refs, head_ref, &ref->node);
1083 spin_unlock(&delayed_refs->lock);
1086 * Need to update the delayed_refs_rsv with any changes we may have
1089 btrfs_update_delayed_refs_rsv(trans);
1091 trace_add_delayed_data_ref(trans->fs_info, &ref->node, ref,
1092 action == BTRFS_ADD_DELAYED_EXTENT ?
1093 BTRFS_ADD_DELAYED_REF : action);
1095 kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
1098 if (qrecord_inserted)
1099 return btrfs_qgroup_trace_extent_post(fs_info, record);
1103 int btrfs_add_delayed_extent_op(struct btrfs_trans_handle *trans,
1104 u64 bytenr, u64 num_bytes,
1105 struct btrfs_delayed_extent_op *extent_op)
1107 struct btrfs_delayed_ref_head *head_ref;
1108 struct btrfs_delayed_ref_root *delayed_refs;
1110 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
1114 init_delayed_ref_head(head_ref, NULL, bytenr, num_bytes, 0, 0,
1115 BTRFS_UPDATE_DELAYED_HEAD, extent_op->is_data,
1117 head_ref->extent_op = extent_op;
1119 delayed_refs = &trans->transaction->delayed_refs;
1120 spin_lock(&delayed_refs->lock);
1122 add_delayed_ref_head(trans, head_ref, NULL, BTRFS_UPDATE_DELAYED_HEAD,
1125 spin_unlock(&delayed_refs->lock);
1128 * Need to update the delayed_refs_rsv with any changes we may have
1131 btrfs_update_delayed_refs_rsv(trans);
1136 * This does a simple search for the head node for a given extent. Returns the
1137 * head node if found, or NULL if not.
1139 struct btrfs_delayed_ref_head *
1140 btrfs_find_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs, u64 bytenr)
1142 lockdep_assert_held(&delayed_refs->lock);
1144 return find_ref_head(delayed_refs, bytenr, false);
1147 void __cold btrfs_delayed_ref_exit(void)
1149 kmem_cache_destroy(btrfs_delayed_ref_head_cachep);
1150 kmem_cache_destroy(btrfs_delayed_tree_ref_cachep);
1151 kmem_cache_destroy(btrfs_delayed_data_ref_cachep);
1152 kmem_cache_destroy(btrfs_delayed_extent_op_cachep);
1155 int __init btrfs_delayed_ref_init(void)
1157 btrfs_delayed_ref_head_cachep = kmem_cache_create(
1158 "btrfs_delayed_ref_head",
1159 sizeof(struct btrfs_delayed_ref_head), 0,
1160 SLAB_MEM_SPREAD, NULL);
1161 if (!btrfs_delayed_ref_head_cachep)
1164 btrfs_delayed_tree_ref_cachep = kmem_cache_create(
1165 "btrfs_delayed_tree_ref",
1166 sizeof(struct btrfs_delayed_tree_ref), 0,
1167 SLAB_MEM_SPREAD, NULL);
1168 if (!btrfs_delayed_tree_ref_cachep)
1171 btrfs_delayed_data_ref_cachep = kmem_cache_create(
1172 "btrfs_delayed_data_ref",
1173 sizeof(struct btrfs_delayed_data_ref), 0,
1174 SLAB_MEM_SPREAD, NULL);
1175 if (!btrfs_delayed_data_ref_cachep)
1178 btrfs_delayed_extent_op_cachep = kmem_cache_create(
1179 "btrfs_delayed_extent_op",
1180 sizeof(struct btrfs_delayed_extent_op), 0,
1181 SLAB_MEM_SPREAD, NULL);
1182 if (!btrfs_delayed_extent_op_cachep)
1187 btrfs_delayed_ref_exit();