1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2009 Oracle.  All rights reserved.
4  */
5 
6 #include <linux/sched.h>
7 #include <linux/slab.h>
8 #include <linux/sort.h>
9 #include "messages.h"
10 #include "ctree.h"
11 #include "delayed-ref.h"
12 #include "extent-tree.h"
13 #include "transaction.h"
14 #include "qgroup.h"
15 #include "space-info.h"
16 #include "tree-mod-log.h"
17 #include "fs.h"
18 
19 struct kmem_cache *btrfs_delayed_ref_head_cachep;
20 struct kmem_cache *btrfs_delayed_ref_node_cachep;
21 struct kmem_cache *btrfs_delayed_extent_op_cachep;
22 /*
23  * delayed back reference update tracking.  For subvolume trees
24  * we queue up extent allocations and backref maintenance for
25  * delayed processing.   This avoids deep call chains where we
26  * add extents in the middle of btrfs_search_slot, and it allows
27  * us to buffer up frequently modified backrefs in an rb tree instead
28  * of hammering updates on the extent allocation tree.
29  */
30 
31 bool btrfs_check_space_for_delayed_refs(struct btrfs_fs_info *fs_info)
32 {
33 	struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv;
34 	struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
35 	bool ret = false;
36 	u64 reserved;
37 
38 	spin_lock(&global_rsv->lock);
39 	reserved = global_rsv->reserved;
40 	spin_unlock(&global_rsv->lock);
41 
42 	/*
43 	 * Since the global reserve is just kind of magic we don't really want
44 	 * to rely on it to save our bacon, so if our size is more than the
45 	 * delayed_refs_rsv and the global rsv then it's time to think about
46 	 * bailing.
47 	 */
48 	spin_lock(&delayed_refs_rsv->lock);
49 	reserved += delayed_refs_rsv->reserved;
50 	if (delayed_refs_rsv->size >= reserved)
51 		ret = true;
52 	spin_unlock(&delayed_refs_rsv->lock);
53 	return ret;
54 }
55 
56 /*
57  * Release a ref head's reservation.
58  *
59  * @fs_info:  the filesystem
60  * @nr_refs:  number of delayed refs to drop
61  * @nr_csums: number of csum items to drop
62  *
63  * Drops the delayed ref head's count from the delayed refs rsv and free any
64  * excess reservation we had.
65  */
66 void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr_refs, int nr_csums)
67 {
68 	struct btrfs_block_rsv *block_rsv = &fs_info->delayed_refs_rsv;
69 	u64 num_bytes;
70 	u64 released;
71 
72 	num_bytes = btrfs_calc_delayed_ref_bytes(fs_info, nr_refs);
73 	num_bytes += btrfs_calc_delayed_ref_csum_bytes(fs_info, nr_csums);
74 
75 	released = btrfs_block_rsv_release(fs_info, block_rsv, num_bytes, NULL);
76 	if (released)
77 		trace_btrfs_space_reservation(fs_info, "delayed_refs_rsv",
78 					      0, released, 0);
79 }
80 
81 /*
82  * Adjust the size of the delayed refs rsv.
83  *
84  * This is to be called anytime we may have adjusted trans->delayed_ref_updates
85  * or trans->delayed_ref_csum_deletions, it'll calculate the additional size and
86  * add it to the delayed_refs_rsv.
87  */
88 void btrfs_update_delayed_refs_rsv(struct btrfs_trans_handle *trans)
89 {
90 	struct btrfs_fs_info *fs_info = trans->fs_info;
91 	struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_refs_rsv;
92 	struct btrfs_block_rsv *local_rsv = &trans->delayed_rsv;
93 	u64 num_bytes;
94 	u64 reserved_bytes;
95 
96 	if (btrfs_is_testing(fs_info))
97 		return;
98 
99 	num_bytes = btrfs_calc_delayed_ref_bytes(fs_info, trans->delayed_ref_updates);
100 	num_bytes += btrfs_calc_delayed_ref_csum_bytes(fs_info,
101 						       trans->delayed_ref_csum_deletions);
102 
103 	if (num_bytes == 0)
104 		return;
105 
106 	/*
107 	 * Try to take num_bytes from the transaction's local delayed reserve.
108 	 * If not possible, try to take as much as it's available. If the local
109 	 * reserve doesn't have enough reserved space, the delayed refs reserve
110 	 * will be refilled next time btrfs_delayed_refs_rsv_refill() is called
111 	 * by someone or if a transaction commit is triggered before that, the
112 	 * global block reserve will be used. We want to minimize using the
113 	 * global block reserve for cases we can account for in advance, to
114 	 * avoid exhausting it and reach -ENOSPC during a transaction commit.
115 	 */
116 	spin_lock(&local_rsv->lock);
117 	reserved_bytes = min(num_bytes, local_rsv->reserved);
118 	local_rsv->reserved -= reserved_bytes;
119 	local_rsv->full = (local_rsv->reserved >= local_rsv->size);
120 	spin_unlock(&local_rsv->lock);
121 
122 	spin_lock(&delayed_rsv->lock);
123 	delayed_rsv->size += num_bytes;
124 	delayed_rsv->reserved += reserved_bytes;
125 	delayed_rsv->full = (delayed_rsv->reserved >= delayed_rsv->size);
126 	spin_unlock(&delayed_rsv->lock);
127 	trans->delayed_ref_updates = 0;
128 	trans->delayed_ref_csum_deletions = 0;
129 }
130 
131 /*
132  * Adjust the size of the delayed refs block reserve for 1 block group item
133  * insertion, used after allocating a block group.
134  */
135 void btrfs_inc_delayed_refs_rsv_bg_inserts(struct btrfs_fs_info *fs_info)
136 {
137 	struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_refs_rsv;
138 
139 	spin_lock(&delayed_rsv->lock);
140 	/*
141 	 * Inserting a block group item does not require changing the free space
142 	 * tree, only the extent tree or the block group tree, so this is all we
143 	 * need.
144 	 */
145 	delayed_rsv->size += btrfs_calc_insert_metadata_size(fs_info, 1);
146 	delayed_rsv->full = false;
147 	spin_unlock(&delayed_rsv->lock);
148 }
149 
150 /*
151  * Adjust the size of the delayed refs block reserve to release space for 1
152  * block group item insertion.
153  */
154 void btrfs_dec_delayed_refs_rsv_bg_inserts(struct btrfs_fs_info *fs_info)
155 {
156 	struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_refs_rsv;
157 	const u64 num_bytes = btrfs_calc_insert_metadata_size(fs_info, 1);
158 	u64 released;
159 
160 	released = btrfs_block_rsv_release(fs_info, delayed_rsv, num_bytes, NULL);
161 	if (released > 0)
162 		trace_btrfs_space_reservation(fs_info, "delayed_refs_rsv",
163 					      0, released, 0);
164 }
165 
166 /*
167  * Adjust the size of the delayed refs block reserve for 1 block group item
168  * update.
169  */
170 void btrfs_inc_delayed_refs_rsv_bg_updates(struct btrfs_fs_info *fs_info)
171 {
172 	struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_refs_rsv;
173 
174 	spin_lock(&delayed_rsv->lock);
175 	/*
176 	 * Updating a block group item does not result in new nodes/leaves and
177 	 * does not require changing the free space tree, only the extent tree
178 	 * or the block group tree, so this is all we need.
179 	 */
180 	delayed_rsv->size += btrfs_calc_metadata_size(fs_info, 1);
181 	delayed_rsv->full = false;
182 	spin_unlock(&delayed_rsv->lock);
183 }
184 
185 /*
186  * Adjust the size of the delayed refs block reserve to release space for 1
187  * block group item update.
188  */
189 void btrfs_dec_delayed_refs_rsv_bg_updates(struct btrfs_fs_info *fs_info)
190 {
191 	struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_refs_rsv;
192 	const u64 num_bytes = btrfs_calc_metadata_size(fs_info, 1);
193 	u64 released;
194 
195 	released = btrfs_block_rsv_release(fs_info, delayed_rsv, num_bytes, NULL);
196 	if (released > 0)
197 		trace_btrfs_space_reservation(fs_info, "delayed_refs_rsv",
198 					      0, released, 0);
199 }
200 
201 /*
202  * Refill based on our delayed refs usage.
203  *
204  * @fs_info: the filesystem
205  * @flush:   control how we can flush for this reservation.
206  *
207  * This will refill the delayed block_rsv up to 1 items size worth of space and
208  * will return -ENOSPC if we can't make the reservation.
209  */
210 int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info,
211 				  enum btrfs_reserve_flush_enum flush)
212 {
213 	struct btrfs_block_rsv *block_rsv = &fs_info->delayed_refs_rsv;
214 	struct btrfs_space_info *space_info = block_rsv->space_info;
215 	u64 limit = btrfs_calc_delayed_ref_bytes(fs_info, 1);
216 	u64 num_bytes = 0;
217 	u64 refilled_bytes;
218 	u64 to_free;
219 	int ret = -ENOSPC;
220 
221 	spin_lock(&block_rsv->lock);
222 	if (block_rsv->reserved < block_rsv->size) {
223 		num_bytes = block_rsv->size - block_rsv->reserved;
224 		num_bytes = min(num_bytes, limit);
225 	}
226 	spin_unlock(&block_rsv->lock);
227 
228 	if (!num_bytes)
229 		return 0;
230 
231 	ret = btrfs_reserve_metadata_bytes(fs_info, space_info, num_bytes, flush);
232 	if (ret)
233 		return ret;
234 
235 	/*
236 	 * We may have raced with someone else, so check again if we the block
237 	 * reserve is still not full and release any excess space.
238 	 */
239 	spin_lock(&block_rsv->lock);
240 	if (block_rsv->reserved < block_rsv->size) {
241 		u64 needed = block_rsv->size - block_rsv->reserved;
242 
243 		if (num_bytes >= needed) {
244 			block_rsv->reserved += needed;
245 			block_rsv->full = true;
246 			to_free = num_bytes - needed;
247 			refilled_bytes = needed;
248 		} else {
249 			block_rsv->reserved += num_bytes;
250 			to_free = 0;
251 			refilled_bytes = num_bytes;
252 		}
253 	} else {
254 		to_free = num_bytes;
255 		refilled_bytes = 0;
256 	}
257 	spin_unlock(&block_rsv->lock);
258 
259 	if (to_free > 0)
260 		btrfs_space_info_free_bytes_may_use(space_info, to_free);
261 
262 	if (refilled_bytes > 0)
263 		trace_btrfs_space_reservation(fs_info, "delayed_refs_rsv", 0,
264 					      refilled_bytes, 1);
265 	return 0;
266 }
267 
268 /*
269  * compare two delayed data backrefs with same bytenr and type
270  */
271 static int comp_data_refs(const struct btrfs_delayed_ref_node *ref1,
272 			  const struct btrfs_delayed_ref_node *ref2)
273 {
274 	if (ref1->data_ref.objectid < ref2->data_ref.objectid)
275 		return -1;
276 	if (ref1->data_ref.objectid > ref2->data_ref.objectid)
277 		return 1;
278 	if (ref1->data_ref.offset < ref2->data_ref.offset)
279 		return -1;
280 	if (ref1->data_ref.offset > ref2->data_ref.offset)
281 		return 1;
282 	return 0;
283 }
284 
285 static int comp_refs(const struct btrfs_delayed_ref_node *ref1,
286 		     const struct btrfs_delayed_ref_node *ref2,
287 		     bool check_seq)
288 {
289 	int ret = 0;
290 
291 	if (ref1->type < ref2->type)
292 		return -1;
293 	if (ref1->type > ref2->type)
294 		return 1;
295 	if (ref1->type == BTRFS_SHARED_BLOCK_REF_KEY ||
296 	    ref1->type == BTRFS_SHARED_DATA_REF_KEY) {
297 		if (ref1->parent < ref2->parent)
298 			return -1;
299 		if (ref1->parent > ref2->parent)
300 			return 1;
301 	} else {
302 		if (ref1->ref_root < ref2->ref_root)
303 			return -1;
304 		if (ref1->ref_root > ref2->ref_root)
305 			return 1;
306 		if (ref1->type == BTRFS_EXTENT_DATA_REF_KEY)
307 			ret = comp_data_refs(ref1, ref2);
308 	}
309 	if (ret)
310 		return ret;
311 	if (check_seq) {
312 		if (ref1->seq < ref2->seq)
313 			return -1;
314 		if (ref1->seq > ref2->seq)
315 			return 1;
316 	}
317 	return 0;
318 }
319 
320 static int cmp_refs_node(const struct rb_node *new, const struct rb_node *exist)
321 {
322 	const struct btrfs_delayed_ref_node *new_node =
323 		rb_entry(new, struct btrfs_delayed_ref_node, ref_node);
324 	const struct btrfs_delayed_ref_node *exist_node =
325 		rb_entry(exist, struct btrfs_delayed_ref_node, ref_node);
326 
327 	return comp_refs(new_node, exist_node, true);
328 }
329 
330 static struct btrfs_delayed_ref_node* tree_insert(struct rb_root_cached *root,
331 		struct btrfs_delayed_ref_node *ins)
332 {
333 	struct rb_node *node = &ins->ref_node;
334 	struct rb_node *exist = rb_find_add_cached(node, root, cmp_refs_node);
335 
336 	return rb_entry_safe(exist, struct btrfs_delayed_ref_node, ref_node);
337 }
338 
339 static struct btrfs_delayed_ref_head *find_first_ref_head(
340 		struct btrfs_delayed_ref_root *dr)
341 {
342 	unsigned long from = 0;
343 
344 	lockdep_assert_held(&dr->lock);
345 
346 	return xa_find(&dr->head_refs, &from, ULONG_MAX, XA_PRESENT);
347 }
348 
349 static bool btrfs_delayed_ref_lock(struct btrfs_delayed_ref_root *delayed_refs,
350 				   struct btrfs_delayed_ref_head *head)
351 {
352 	lockdep_assert_held(&delayed_refs->lock);
353 	if (mutex_trylock(&head->mutex))
354 		return true;
355 
356 	refcount_inc(&head->refs);
357 	spin_unlock(&delayed_refs->lock);
358 
359 	mutex_lock(&head->mutex);
360 	spin_lock(&delayed_refs->lock);
361 	if (!head->tracked) {
362 		mutex_unlock(&head->mutex);
363 		btrfs_put_delayed_ref_head(head);
364 		return false;
365 	}
366 	btrfs_put_delayed_ref_head(head);
367 	return true;
368 }
369 
370 static inline void drop_delayed_ref(struct btrfs_fs_info *fs_info,
371 				    struct btrfs_delayed_ref_root *delayed_refs,
372 				    struct btrfs_delayed_ref_head *head,
373 				    struct btrfs_delayed_ref_node *ref)
374 {
375 	lockdep_assert_held(&head->lock);
376 	rb_erase_cached(&ref->ref_node, &head->ref_tree);
377 	RB_CLEAR_NODE(&ref->ref_node);
378 	if (!list_empty(&ref->add_list))
379 		list_del(&ref->add_list);
380 	btrfs_put_delayed_ref(ref);
381 	btrfs_delayed_refs_rsv_release(fs_info, 1, 0);
382 }
383 
384 static bool merge_ref(struct btrfs_fs_info *fs_info,
385 		      struct btrfs_delayed_ref_root *delayed_refs,
386 		      struct btrfs_delayed_ref_head *head,
387 		      struct btrfs_delayed_ref_node *ref,
388 		      u64 seq)
389 {
390 	struct btrfs_delayed_ref_node *next;
391 	struct rb_node *node = rb_next(&ref->ref_node);
392 	bool done = false;
393 
394 	while (!done && node) {
395 		int mod;
396 
397 		next = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
398 		node = rb_next(node);
399 		if (seq && next->seq >= seq)
400 			break;
401 		if (comp_refs(ref, next, false))
402 			break;
403 
404 		if (ref->action == next->action) {
405 			mod = next->ref_mod;
406 		} else {
407 			if (ref->ref_mod < next->ref_mod) {
408 				swap(ref, next);
409 				done = true;
410 			}
411 			mod = -next->ref_mod;
412 		}
413 
414 		drop_delayed_ref(fs_info, delayed_refs, head, next);
415 		ref->ref_mod += mod;
416 		if (ref->ref_mod == 0) {
417 			drop_delayed_ref(fs_info, delayed_refs, head, ref);
418 			done = true;
419 		} else {
420 			/*
421 			 * Can't have multiples of the same ref on a tree block.
422 			 */
423 			WARN_ON(ref->type == BTRFS_TREE_BLOCK_REF_KEY ||
424 				ref->type == BTRFS_SHARED_BLOCK_REF_KEY);
425 		}
426 	}
427 
428 	return done;
429 }
430 
431 void btrfs_merge_delayed_refs(struct btrfs_fs_info *fs_info,
432 			      struct btrfs_delayed_ref_root *delayed_refs,
433 			      struct btrfs_delayed_ref_head *head)
434 {
435 	struct btrfs_delayed_ref_node *ref;
436 	struct rb_node *node;
437 	u64 seq = 0;
438 
439 	lockdep_assert_held(&head->lock);
440 
441 	if (RB_EMPTY_ROOT(&head->ref_tree.rb_root))
442 		return;
443 
444 	/* We don't have too many refs to merge for data. */
445 	if (head->is_data)
446 		return;
447 
448 	seq = btrfs_tree_mod_log_lowest_seq(fs_info);
449 again:
450 	for (node = rb_first_cached(&head->ref_tree); node;
451 	     node = rb_next(node)) {
452 		ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
453 		if (seq && ref->seq >= seq)
454 			continue;
455 		if (merge_ref(fs_info, delayed_refs, head, ref, seq))
456 			goto again;
457 	}
458 }
459 
460 int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq)
461 {
462 	int ret = 0;
463 	u64 min_seq = btrfs_tree_mod_log_lowest_seq(fs_info);
464 
465 	if (min_seq != 0 && seq >= min_seq) {
466 		btrfs_debug(fs_info,
467 			    "holding back delayed_ref %llu, lowest is %llu",
468 			    seq, min_seq);
469 		ret = 1;
470 	}
471 
472 	return ret;
473 }
474 
475 struct btrfs_delayed_ref_head *btrfs_select_ref_head(
476 		const struct btrfs_fs_info *fs_info,
477 		struct btrfs_delayed_ref_root *delayed_refs)
478 {
479 	struct btrfs_delayed_ref_head *head;
480 	unsigned long start_index;
481 	unsigned long found_index;
482 	bool found_head = false;
483 	bool locked;
484 
485 	spin_lock(&delayed_refs->lock);
486 again:
487 	start_index = (delayed_refs->run_delayed_start >> fs_info->sectorsize_bits);
488 	xa_for_each_start(&delayed_refs->head_refs, found_index, head, start_index) {
489 		if (!head->processing) {
490 			found_head = true;
491 			break;
492 		}
493 	}
494 	if (!found_head) {
495 		if (delayed_refs->run_delayed_start == 0) {
496 			spin_unlock(&delayed_refs->lock);
497 			return NULL;
498 		}
499 		delayed_refs->run_delayed_start = 0;
500 		goto again;
501 	}
502 
503 	head->processing = true;
504 	WARN_ON(delayed_refs->num_heads_ready == 0);
505 	delayed_refs->num_heads_ready--;
506 	delayed_refs->run_delayed_start = head->bytenr +
507 		head->num_bytes;
508 
509 	locked = btrfs_delayed_ref_lock(delayed_refs, head);
510 	spin_unlock(&delayed_refs->lock);
511 
512 	/*
513 	 * We may have dropped the spin lock to get the head mutex lock, and
514 	 * that might have given someone else time to free the head.  If that's
515 	 * true, it has been removed from our list and we can move on.
516 	 */
517 	if (!locked)
518 		return ERR_PTR(-EAGAIN);
519 
520 	return head;
521 }
522 
523 void btrfs_unselect_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
524 			     struct btrfs_delayed_ref_head *head)
525 {
526 	spin_lock(&delayed_refs->lock);
527 	head->processing = false;
528 	delayed_refs->num_heads_ready++;
529 	spin_unlock(&delayed_refs->lock);
530 	btrfs_delayed_ref_unlock(head);
531 }
532 
533 void btrfs_delete_ref_head(const struct btrfs_fs_info *fs_info,
534 			   struct btrfs_delayed_ref_root *delayed_refs,
535 			   struct btrfs_delayed_ref_head *head)
536 {
537 	const unsigned long index = (head->bytenr >> fs_info->sectorsize_bits);
538 
539 	lockdep_assert_held(&delayed_refs->lock);
540 	lockdep_assert_held(&head->lock);
541 
542 	xa_erase(&delayed_refs->head_refs, index);
543 	head->tracked = false;
544 	delayed_refs->num_heads--;
545 	if (!head->processing)
546 		delayed_refs->num_heads_ready--;
547 }
548 
549 struct btrfs_delayed_ref_node *btrfs_select_delayed_ref(struct btrfs_delayed_ref_head *head)
550 {
551 	struct btrfs_delayed_ref_node *ref;
552 
553 	lockdep_assert_held(&head->mutex);
554 	lockdep_assert_held(&head->lock);
555 
556 	if (RB_EMPTY_ROOT(&head->ref_tree.rb_root))
557 		return NULL;
558 
559 	/*
560 	 * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
561 	 * This is to prevent a ref count from going down to zero, which deletes
562 	 * the extent item from the extent tree, when there still are references
563 	 * to add, which would fail because they would not find the extent item.
564 	 */
565 	if (!list_empty(&head->ref_add_list))
566 		return list_first_entry(&head->ref_add_list,
567 					struct btrfs_delayed_ref_node, add_list);
568 
569 	ref = rb_entry(rb_first_cached(&head->ref_tree),
570 		       struct btrfs_delayed_ref_node, ref_node);
571 	ASSERT(list_empty(&ref->add_list));
572 	return ref;
573 }
574 
575 /*
576  * Helper to insert the ref_node to the tail or merge with tail.
577  *
578  * Return false if the ref was inserted.
579  * Return true if the ref was merged into an existing one (and therefore can be
580  * freed by the caller).
581  */
582 static bool insert_delayed_ref(struct btrfs_trans_handle *trans,
583 			       struct btrfs_delayed_ref_head *href,
584 			       struct btrfs_delayed_ref_node *ref)
585 {
586 	struct btrfs_delayed_ref_root *root = &trans->transaction->delayed_refs;
587 	struct btrfs_delayed_ref_node *exist;
588 	int mod;
589 
590 	spin_lock(&href->lock);
591 	exist = tree_insert(&href->ref_tree, ref);
592 	if (!exist) {
593 		if (ref->action == BTRFS_ADD_DELAYED_REF)
594 			list_add_tail(&ref->add_list, &href->ref_add_list);
595 		spin_unlock(&href->lock);
596 		trans->delayed_ref_updates++;
597 		return false;
598 	}
599 
600 	/* Now we are sure we can merge */
601 	if (exist->action == ref->action) {
602 		mod = ref->ref_mod;
603 	} else {
604 		/* Need to change action */
605 		if (exist->ref_mod < ref->ref_mod) {
606 			exist->action = ref->action;
607 			mod = -exist->ref_mod;
608 			exist->ref_mod = ref->ref_mod;
609 			if (ref->action == BTRFS_ADD_DELAYED_REF)
610 				list_add_tail(&exist->add_list,
611 					      &href->ref_add_list);
612 			else if (ref->action == BTRFS_DROP_DELAYED_REF) {
613 				ASSERT(!list_empty(&exist->add_list));
614 				list_del_init(&exist->add_list);
615 			} else {
616 				ASSERT(0);
617 			}
618 		} else
619 			mod = -ref->ref_mod;
620 	}
621 	exist->ref_mod += mod;
622 
623 	/* remove existing tail if its ref_mod is zero */
624 	if (exist->ref_mod == 0)
625 		drop_delayed_ref(trans->fs_info, root, href, exist);
626 	spin_unlock(&href->lock);
627 	return true;
628 }
629 
630 /*
631  * helper function to update the accounting in the head ref
632  * existing and update must have the same bytenr
633  */
634 static noinline void update_existing_head_ref(struct btrfs_trans_handle *trans,
635 			 struct btrfs_delayed_ref_head *existing,
636 			 struct btrfs_delayed_ref_head *update)
637 {
638 	struct btrfs_delayed_ref_root *delayed_refs =
639 		&trans->transaction->delayed_refs;
640 	struct btrfs_fs_info *fs_info = trans->fs_info;
641 	int old_ref_mod;
642 
643 	BUG_ON(existing->is_data != update->is_data);
644 
645 	spin_lock(&existing->lock);
646 
647 	/*
648 	 * When freeing an extent, we may not know the owning root when we
649 	 * first create the head_ref. However, some deref before the last deref
650 	 * will know it, so we just need to update the head_ref accordingly.
651 	 */
652 	if (!existing->owning_root)
653 		existing->owning_root = update->owning_root;
654 
655 	if (update->must_insert_reserved) {
656 		/* if the extent was freed and then
657 		 * reallocated before the delayed ref
658 		 * entries were processed, we can end up
659 		 * with an existing head ref without
660 		 * the must_insert_reserved flag set.
661 		 * Set it again here
662 		 */
663 		existing->must_insert_reserved = update->must_insert_reserved;
664 		existing->owning_root = update->owning_root;
665 
666 		/*
667 		 * update the num_bytes so we make sure the accounting
668 		 * is done correctly
669 		 */
670 		existing->num_bytes = update->num_bytes;
671 
672 	}
673 
674 	if (update->extent_op) {
675 		if (!existing->extent_op) {
676 			existing->extent_op = update->extent_op;
677 		} else {
678 			if (update->extent_op->update_key) {
679 				memcpy(&existing->extent_op->key,
680 				       &update->extent_op->key,
681 				       sizeof(update->extent_op->key));
682 				existing->extent_op->update_key = true;
683 			}
684 			if (update->extent_op->update_flags) {
685 				existing->extent_op->flags_to_set |=
686 					update->extent_op->flags_to_set;
687 				existing->extent_op->update_flags = true;
688 			}
689 			btrfs_free_delayed_extent_op(update->extent_op);
690 		}
691 	}
692 	/*
693 	 * update the reference mod on the head to reflect this new operation,
694 	 * only need the lock for this case cause we could be processing it
695 	 * currently, for refs we just added we know we're a-ok.
696 	 */
697 	old_ref_mod = existing->total_ref_mod;
698 	existing->ref_mod += update->ref_mod;
699 	existing->total_ref_mod += update->ref_mod;
700 
701 	/*
702 	 * If we are going to from a positive ref mod to a negative or vice
703 	 * versa we need to make sure to adjust pending_csums accordingly.
704 	 * We reserve bytes for csum deletion when adding or updating a ref head
705 	 * see add_delayed_ref_head() for more details.
706 	 */
707 	if (existing->is_data) {
708 		u64 csum_leaves =
709 			btrfs_csum_bytes_to_leaves(fs_info,
710 						   existing->num_bytes);
711 
712 		if (existing->total_ref_mod >= 0 && old_ref_mod < 0) {
713 			delayed_refs->pending_csums -= existing->num_bytes;
714 			btrfs_delayed_refs_rsv_release(fs_info, 0, csum_leaves);
715 		}
716 		if (existing->total_ref_mod < 0 && old_ref_mod >= 0) {
717 			delayed_refs->pending_csums += existing->num_bytes;
718 			trans->delayed_ref_csum_deletions += csum_leaves;
719 		}
720 	}
721 
722 	spin_unlock(&existing->lock);
723 }
724 
725 static void init_delayed_ref_head(struct btrfs_delayed_ref_head *head_ref,
726 				  struct btrfs_ref *generic_ref,
727 				  struct btrfs_qgroup_extent_record *qrecord,
728 				  u64 reserved)
729 {
730 	int count_mod = 1;
731 	bool must_insert_reserved = false;
732 
733 	/* If reserved is provided, it must be a data extent. */
734 	BUG_ON(generic_ref->type != BTRFS_REF_DATA && reserved);
735 
736 	switch (generic_ref->action) {
737 	case BTRFS_ADD_DELAYED_REF:
738 		/* count_mod is already set to 1. */
739 		break;
740 	case BTRFS_UPDATE_DELAYED_HEAD:
741 		count_mod = 0;
742 		break;
743 	case BTRFS_DROP_DELAYED_REF:
744 		/*
745 		 * The head node stores the sum of all the mods, so dropping a ref
746 		 * should drop the sum in the head node by one.
747 		 */
748 		count_mod = -1;
749 		break;
750 	case BTRFS_ADD_DELAYED_EXTENT:
751 		/*
752 		 * BTRFS_ADD_DELAYED_EXTENT means that we need to update the
753 		 * reserved accounting when the extent is finally added, or if a
754 		 * later modification deletes the delayed ref without ever
755 		 * inserting the extent into the extent allocation tree.
756 		 * ref->must_insert_reserved is the flag used to record that
757 		 * accounting mods are required.
758 		 *
759 		 * Once we record must_insert_reserved, switch the action to
760 		 * BTRFS_ADD_DELAYED_REF because other special casing is not
761 		 * required.
762 		 */
763 		must_insert_reserved = true;
764 		break;
765 	}
766 
767 	refcount_set(&head_ref->refs, 1);
768 	head_ref->bytenr = generic_ref->bytenr;
769 	head_ref->num_bytes = generic_ref->num_bytes;
770 	head_ref->ref_mod = count_mod;
771 	head_ref->reserved_bytes = reserved;
772 	head_ref->must_insert_reserved = must_insert_reserved;
773 	head_ref->owning_root = generic_ref->owning_root;
774 	head_ref->is_data = (generic_ref->type == BTRFS_REF_DATA);
775 	head_ref->is_system = (generic_ref->ref_root == BTRFS_CHUNK_TREE_OBJECTID);
776 	head_ref->ref_tree = RB_ROOT_CACHED;
777 	INIT_LIST_HEAD(&head_ref->ref_add_list);
778 	head_ref->tracked = false;
779 	head_ref->processing = false;
780 	head_ref->total_ref_mod = count_mod;
781 	spin_lock_init(&head_ref->lock);
782 	mutex_init(&head_ref->mutex);
783 
784 	/* If not metadata set an impossible level to help debugging. */
785 	if (generic_ref->type == BTRFS_REF_METADATA)
786 		head_ref->level = generic_ref->tree_ref.level;
787 	else
788 		head_ref->level = U8_MAX;
789 
790 	if (qrecord) {
791 		if (generic_ref->ref_root && reserved) {
792 			qrecord->data_rsv = reserved;
793 			qrecord->data_rsv_refroot = generic_ref->ref_root;
794 		}
795 		qrecord->num_bytes = generic_ref->num_bytes;
796 		qrecord->old_roots = NULL;
797 	}
798 }
799 
800 /*
801  * helper function to actually insert a head node into the rbtree.
802  * this does all the dirty work in terms of maintaining the correct
803  * overall modification count.
804  *
805  * Returns an error pointer in case of an error.
806  */
807 static noinline struct btrfs_delayed_ref_head *
808 add_delayed_ref_head(struct btrfs_trans_handle *trans,
809 		     struct btrfs_delayed_ref_head *head_ref,
810 		     struct btrfs_qgroup_extent_record *qrecord,
811 		     int action, bool *qrecord_inserted_ret)
812 {
813 	struct btrfs_fs_info *fs_info = trans->fs_info;
814 	struct btrfs_delayed_ref_head *existing;
815 	struct btrfs_delayed_ref_root *delayed_refs;
816 	const unsigned long index = (head_ref->bytenr >> fs_info->sectorsize_bits);
817 	bool qrecord_inserted = false;
818 
819 	delayed_refs = &trans->transaction->delayed_refs;
820 	lockdep_assert_held(&delayed_refs->lock);
821 
822 #if BITS_PER_LONG == 32
823 	if (head_ref->bytenr >= MAX_LFS_FILESIZE) {
824 		if (qrecord)
825 			xa_release(&delayed_refs->dirty_extents, index);
826 		btrfs_err_rl(fs_info,
827 "delayed ref head %llu is beyond 32bit page cache and xarray index limit",
828 			     head_ref->bytenr);
829 		btrfs_err_32bit_limit(fs_info);
830 		return ERR_PTR(-EOVERFLOW);
831 	}
832 #endif
833 
834 	/* Record qgroup extent info if provided */
835 	if (qrecord) {
836 		int ret;
837 
838 		ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, qrecord,
839 						       head_ref->bytenr);
840 		if (ret) {
841 			/* Clean up if insertion fails or item exists. */
842 			xa_release(&delayed_refs->dirty_extents, index);
843 			/* Caller responsible for freeing qrecord on error. */
844 			if (ret < 0)
845 				return ERR_PTR(ret);
846 			kfree(qrecord);
847 		} else {
848 			qrecord_inserted = true;
849 		}
850 	}
851 
852 	trace_add_delayed_ref_head(fs_info, head_ref, action);
853 
854 	existing = xa_load(&delayed_refs->head_refs, index);
855 	if (existing) {
856 		update_existing_head_ref(trans, existing, head_ref);
857 		/*
858 		 * we've updated the existing ref, free the newly
859 		 * allocated ref
860 		 */
861 		kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref);
862 		head_ref = existing;
863 	} else {
864 		existing = xa_store(&delayed_refs->head_refs, index, head_ref, GFP_ATOMIC);
865 		if (xa_is_err(existing)) {
866 			/* Memory was preallocated by the caller. */
867 			ASSERT(xa_err(existing) != -ENOMEM);
868 			return ERR_PTR(xa_err(existing));
869 		} else if (WARN_ON(existing)) {
870 			/*
871 			 * Shouldn't happen we just did a lookup before under
872 			 * delayed_refs->lock.
873 			 */
874 			return ERR_PTR(-EEXIST);
875 		}
876 		head_ref->tracked = true;
877 		/*
878 		 * We reserve the amount of bytes needed to delete csums when
879 		 * adding the ref head and not when adding individual drop refs
880 		 * since the csum items are deleted only after running the last
881 		 * delayed drop ref (the data extent's ref count drops to 0).
882 		 */
883 		if (head_ref->is_data && head_ref->ref_mod < 0) {
884 			delayed_refs->pending_csums += head_ref->num_bytes;
885 			trans->delayed_ref_csum_deletions +=
886 				btrfs_csum_bytes_to_leaves(fs_info, head_ref->num_bytes);
887 		}
888 		delayed_refs->num_heads++;
889 		delayed_refs->num_heads_ready++;
890 	}
891 	if (qrecord_inserted_ret)
892 		*qrecord_inserted_ret = qrecord_inserted;
893 
894 	return head_ref;
895 }
896 
897 /*
898  * Initialize the structure which represents a modification to a an extent.
899  *
900  * @fs_info:    Internal to the mounted filesystem mount structure.
901  *
902  * @ref:	The structure which is going to be initialized.
903  *
904  * @bytenr:	The logical address of the extent for which a modification is
905  *		going to be recorded.
906  *
907  * @num_bytes:  Size of the extent whose modification is being recorded.
908  *
909  * @ref_root:	The id of the root where this modification has originated, this
910  *		can be either one of the well-known metadata trees or the
911  *		subvolume id which references this extent.
912  *
913  * @action:	Can be one of BTRFS_ADD_DELAYED_REF/BTRFS_DROP_DELAYED_REF or
914  *		BTRFS_ADD_DELAYED_EXTENT
915  *
916  * @ref_type:	Holds the type of the extent which is being recorded, can be
917  *		one of BTRFS_SHARED_BLOCK_REF_KEY/BTRFS_TREE_BLOCK_REF_KEY
918  *		when recording a metadata extent or BTRFS_SHARED_DATA_REF_KEY/
919  *		BTRFS_EXTENT_DATA_REF_KEY when recording data extent
920  */
921 static void init_delayed_ref_common(struct btrfs_fs_info *fs_info,
922 				    struct btrfs_delayed_ref_node *ref,
923 				    struct btrfs_ref *generic_ref)
924 {
925 	int action = generic_ref->action;
926 	u64 seq = 0;
927 
928 	if (action == BTRFS_ADD_DELAYED_EXTENT)
929 		action = BTRFS_ADD_DELAYED_REF;
930 
931 	if (is_fstree(generic_ref->ref_root))
932 		seq = atomic64_read(&fs_info->tree_mod_seq);
933 
934 	refcount_set(&ref->refs, 1);
935 	ref->bytenr = generic_ref->bytenr;
936 	ref->num_bytes = generic_ref->num_bytes;
937 	ref->ref_mod = 1;
938 	ref->action = action;
939 	ref->seq = seq;
940 	ref->type = btrfs_ref_type(generic_ref);
941 	ref->ref_root = generic_ref->ref_root;
942 	ref->parent = generic_ref->parent;
943 	RB_CLEAR_NODE(&ref->ref_node);
944 	INIT_LIST_HEAD(&ref->add_list);
945 
946 	if (generic_ref->type == BTRFS_REF_DATA)
947 		ref->data_ref = generic_ref->data_ref;
948 	else
949 		ref->tree_ref = generic_ref->tree_ref;
950 }
951 
952 void btrfs_init_tree_ref(struct btrfs_ref *generic_ref, int level, u64 mod_root,
953 			 bool skip_qgroup)
954 {
955 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
956 	/* If @real_root not set, use @root as fallback */
957 	generic_ref->real_root = mod_root ?: generic_ref->ref_root;
958 #endif
959 	generic_ref->tree_ref.level = level;
960 	generic_ref->type = BTRFS_REF_METADATA;
961 	if (skip_qgroup || !(is_fstree(generic_ref->ref_root) &&
962 			     (!mod_root || is_fstree(mod_root))))
963 		generic_ref->skip_qgroup = true;
964 	else
965 		generic_ref->skip_qgroup = false;
966 
967 }
968 
969 void btrfs_init_data_ref(struct btrfs_ref *generic_ref, u64 ino, u64 offset,
970 			 u64 mod_root, bool skip_qgroup)
971 {
972 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
973 	/* If @real_root not set, use @root as fallback */
974 	generic_ref->real_root = mod_root ?: generic_ref->ref_root;
975 #endif
976 	generic_ref->data_ref.objectid = ino;
977 	generic_ref->data_ref.offset = offset;
978 	generic_ref->type = BTRFS_REF_DATA;
979 	if (skip_qgroup || !(is_fstree(generic_ref->ref_root) &&
980 			     (!mod_root || is_fstree(mod_root))))
981 		generic_ref->skip_qgroup = true;
982 	else
983 		generic_ref->skip_qgroup = false;
984 }
985 
986 static int add_delayed_ref(struct btrfs_trans_handle *trans,
987 			   struct btrfs_ref *generic_ref,
988 			   struct btrfs_delayed_extent_op *extent_op,
989 			   u64 reserved)
990 {
991 	struct btrfs_fs_info *fs_info = trans->fs_info;
992 	struct btrfs_delayed_ref_node *node;
993 	struct btrfs_delayed_ref_head *head_ref;
994 	struct btrfs_delayed_ref_head *new_head_ref;
995 	struct btrfs_delayed_ref_root *delayed_refs;
996 	struct btrfs_qgroup_extent_record *record = NULL;
997 	const unsigned long index = (generic_ref->bytenr >> fs_info->sectorsize_bits);
998 	bool qrecord_reserved = false;
999 	bool qrecord_inserted;
1000 	int action = generic_ref->action;
1001 	bool merged;
1002 	int ret;
1003 
1004 	node = kmem_cache_alloc(btrfs_delayed_ref_node_cachep, GFP_NOFS);
1005 	if (!node)
1006 		return -ENOMEM;
1007 
1008 	head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
1009 	if (!head_ref) {
1010 		ret = -ENOMEM;
1011 		goto free_node;
1012 	}
1013 
1014 	delayed_refs = &trans->transaction->delayed_refs;
1015 
1016 	if (btrfs_qgroup_full_accounting(fs_info) && !generic_ref->skip_qgroup) {
1017 		record = kzalloc(sizeof(*record), GFP_NOFS);
1018 		if (!record) {
1019 			ret = -ENOMEM;
1020 			goto free_head_ref;
1021 		}
1022 		if (xa_reserve(&delayed_refs->dirty_extents, index, GFP_NOFS)) {
1023 			ret = -ENOMEM;
1024 			goto free_record;
1025 		}
1026 		qrecord_reserved = true;
1027 	}
1028 
1029 	ret = xa_reserve(&delayed_refs->head_refs, index, GFP_NOFS);
1030 	if (ret) {
1031 		if (qrecord_reserved)
1032 			xa_release(&delayed_refs->dirty_extents, index);
1033 		goto free_record;
1034 	}
1035 
1036 	init_delayed_ref_common(fs_info, node, generic_ref);
1037 	init_delayed_ref_head(head_ref, generic_ref, record, reserved);
1038 	head_ref->extent_op = extent_op;
1039 
1040 	spin_lock(&delayed_refs->lock);
1041 
1042 	/*
1043 	 * insert both the head node and the new ref without dropping
1044 	 * the spin lock
1045 	 */
1046 	new_head_ref = add_delayed_ref_head(trans, head_ref, record,
1047 					    action, &qrecord_inserted);
1048 	if (IS_ERR(new_head_ref)) {
1049 		xa_release(&delayed_refs->head_refs, index);
1050 		spin_unlock(&delayed_refs->lock);
1051 		ret = PTR_ERR(new_head_ref);
1052 		goto free_record;
1053 	}
1054 	head_ref = new_head_ref;
1055 
1056 	merged = insert_delayed_ref(trans, head_ref, node);
1057 	spin_unlock(&delayed_refs->lock);
1058 
1059 	/*
1060 	 * Need to update the delayed_refs_rsv with any changes we may have
1061 	 * made.
1062 	 */
1063 	btrfs_update_delayed_refs_rsv(trans);
1064 
1065 	if (generic_ref->type == BTRFS_REF_DATA)
1066 		trace_add_delayed_data_ref(trans->fs_info, node);
1067 	else
1068 		trace_add_delayed_tree_ref(trans->fs_info, node);
1069 	if (merged)
1070 		kmem_cache_free(btrfs_delayed_ref_node_cachep, node);
1071 
1072 	if (qrecord_inserted)
1073 		return btrfs_qgroup_trace_extent_post(trans, record, generic_ref->bytenr);
1074 	return 0;
1075 
1076 free_record:
1077 	kfree(record);
1078 free_head_ref:
1079 	kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref);
1080 free_node:
1081 	kmem_cache_free(btrfs_delayed_ref_node_cachep, node);
1082 	return ret;
1083 }
1084 
1085 /*
1086  * Add a delayed tree ref. This does all of the accounting required to make sure
1087  * the delayed ref is eventually processed before this transaction commits.
1088  */
1089 int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans,
1090 			       struct btrfs_ref *generic_ref,
1091 			       struct btrfs_delayed_extent_op *extent_op)
1092 {
1093 	ASSERT(generic_ref->type == BTRFS_REF_METADATA && generic_ref->action);
1094 	return add_delayed_ref(trans, generic_ref, extent_op, 0);
1095 }
1096 
1097 /*
1098  * add a delayed data ref. it's similar to btrfs_add_delayed_tree_ref.
1099  */
1100 int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans,
1101 			       struct btrfs_ref *generic_ref,
1102 			       u64 reserved)
1103 {
1104 	ASSERT(generic_ref->type == BTRFS_REF_DATA && generic_ref->action);
1105 	return add_delayed_ref(trans, generic_ref, NULL, reserved);
1106 }
1107 
1108 int btrfs_add_delayed_extent_op(struct btrfs_trans_handle *trans,
1109 				u64 bytenr, u64 num_bytes, u8 level,
1110 				struct btrfs_delayed_extent_op *extent_op)
1111 {
1112 	const unsigned long index = (bytenr >> trans->fs_info->sectorsize_bits);
1113 	struct btrfs_delayed_ref_head *head_ref;
1114 	struct btrfs_delayed_ref_head *head_ref_ret;
1115 	struct btrfs_delayed_ref_root *delayed_refs;
1116 	struct btrfs_ref generic_ref = {
1117 		.type = BTRFS_REF_METADATA,
1118 		.action = BTRFS_UPDATE_DELAYED_HEAD,
1119 		.bytenr = bytenr,
1120 		.num_bytes = num_bytes,
1121 		.tree_ref.level = level,
1122 	};
1123 	int ret;
1124 
1125 	head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
1126 	if (!head_ref)
1127 		return -ENOMEM;
1128 
1129 	init_delayed_ref_head(head_ref, &generic_ref, NULL, 0);
1130 	head_ref->extent_op = extent_op;
1131 
1132 	delayed_refs = &trans->transaction->delayed_refs;
1133 
1134 	ret = xa_reserve(&delayed_refs->head_refs, index, GFP_NOFS);
1135 	if (ret) {
1136 		kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref);
1137 		return ret;
1138 	}
1139 
1140 	spin_lock(&delayed_refs->lock);
1141 	head_ref_ret = add_delayed_ref_head(trans, head_ref, NULL,
1142 					    BTRFS_UPDATE_DELAYED_HEAD, NULL);
1143 	if (IS_ERR(head_ref_ret)) {
1144 		xa_release(&delayed_refs->head_refs, index);
1145 		spin_unlock(&delayed_refs->lock);
1146 		kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref);
1147 		return PTR_ERR(head_ref_ret);
1148 	}
1149 	spin_unlock(&delayed_refs->lock);
1150 
1151 	/*
1152 	 * Need to update the delayed_refs_rsv with any changes we may have
1153 	 * made.
1154 	 */
1155 	btrfs_update_delayed_refs_rsv(trans);
1156 	return 0;
1157 }
1158 
1159 void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref)
1160 {
1161 	if (refcount_dec_and_test(&ref->refs)) {
1162 		WARN_ON(!RB_EMPTY_NODE(&ref->ref_node));
1163 		kmem_cache_free(btrfs_delayed_ref_node_cachep, ref);
1164 	}
1165 }
1166 
1167 /*
1168  * This does a simple search for the head node for a given extent.  Returns the
1169  * head node if found, or NULL if not.
1170  */
1171 struct btrfs_delayed_ref_head *
1172 btrfs_find_delayed_ref_head(const struct btrfs_fs_info *fs_info,
1173 			    struct btrfs_delayed_ref_root *delayed_refs,
1174 			    u64 bytenr)
1175 {
1176 	const unsigned long index = (bytenr >> fs_info->sectorsize_bits);
1177 
1178 	lockdep_assert_held(&delayed_refs->lock);
1179 
1180 	return xa_load(&delayed_refs->head_refs, index);
1181 }
1182 
1183 static int find_comp(struct btrfs_delayed_ref_node *entry, u64 root, u64 parent)
1184 {
1185 	int type = parent ? BTRFS_SHARED_BLOCK_REF_KEY : BTRFS_TREE_BLOCK_REF_KEY;
1186 
1187 	if (type < entry->type)
1188 		return -1;
1189 	if (type > entry->type)
1190 		return 1;
1191 
1192 	if (type == BTRFS_TREE_BLOCK_REF_KEY) {
1193 		if (root < entry->ref_root)
1194 			return -1;
1195 		if (root > entry->ref_root)
1196 			return 1;
1197 	} else {
1198 		if (parent < entry->parent)
1199 			return -1;
1200 		if (parent > entry->parent)
1201 			return 1;
1202 	}
1203 	return 0;
1204 }
1205 
1206 /*
1207  * Check to see if a given root/parent reference is attached to the head.  This
1208  * only checks for BTRFS_ADD_DELAYED_REF references that match, as that
1209  * indicates the reference exists for the given root or parent.  This is for
1210  * tree blocks only.
1211  *
1212  * @head: the head of the bytenr we're searching.
1213  * @root: the root objectid of the reference if it is a normal reference.
1214  * @parent: the parent if this is a shared backref.
1215  */
1216 bool btrfs_find_delayed_tree_ref(struct btrfs_delayed_ref_head *head,
1217 				 u64 root, u64 parent)
1218 {
1219 	struct rb_node *node;
1220 	bool found = false;
1221 
1222 	lockdep_assert_held(&head->mutex);
1223 
1224 	spin_lock(&head->lock);
1225 	node = head->ref_tree.rb_root.rb_node;
1226 	while (node) {
1227 		struct btrfs_delayed_ref_node *entry;
1228 		int ret;
1229 
1230 		entry = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
1231 		ret = find_comp(entry, root, parent);
1232 		if (ret < 0) {
1233 			node = node->rb_left;
1234 		} else if (ret > 0) {
1235 			node = node->rb_right;
1236 		} else {
1237 			/*
1238 			 * We only want to count ADD actions, as drops mean the
1239 			 * ref doesn't exist.
1240 			 */
1241 			if (entry->action == BTRFS_ADD_DELAYED_REF)
1242 				found = true;
1243 			break;
1244 		}
1245 	}
1246 	spin_unlock(&head->lock);
1247 	return found;
1248 }
1249 
1250 void btrfs_destroy_delayed_refs(struct btrfs_transaction *trans)
1251 {
1252 	struct btrfs_delayed_ref_root *delayed_refs = &trans->delayed_refs;
1253 	struct btrfs_fs_info *fs_info = trans->fs_info;
1254 	bool testing = btrfs_is_testing(fs_info);
1255 
1256 	spin_lock(&delayed_refs->lock);
1257 	while (true) {
1258 		struct btrfs_delayed_ref_head *head;
1259 		struct rb_node *n;
1260 		bool pin_bytes = false;
1261 
1262 		head = find_first_ref_head(delayed_refs);
1263 		if (!head)
1264 			break;
1265 
1266 		if (!btrfs_delayed_ref_lock(delayed_refs, head))
1267 			continue;
1268 
1269 		spin_lock(&head->lock);
1270 		while ((n = rb_first_cached(&head->ref_tree)) != NULL) {
1271 			struct btrfs_delayed_ref_node *ref;
1272 
1273 			ref = rb_entry(n, struct btrfs_delayed_ref_node, ref_node);
1274 			drop_delayed_ref(fs_info, delayed_refs, head, ref);
1275 		}
1276 		if (head->must_insert_reserved)
1277 			pin_bytes = true;
1278 		btrfs_free_delayed_extent_op(head->extent_op);
1279 		btrfs_delete_ref_head(fs_info, delayed_refs, head);
1280 		spin_unlock(&head->lock);
1281 		spin_unlock(&delayed_refs->lock);
1282 		mutex_unlock(&head->mutex);
1283 
1284 		if (!testing && pin_bytes) {
1285 			struct btrfs_block_group *bg;
1286 
1287 			bg = btrfs_lookup_block_group(fs_info, head->bytenr);
1288 			if (WARN_ON_ONCE(bg == NULL)) {
1289 				/*
1290 				 * Unexpected and there's nothing we can do here
1291 				 * because we are in a transaction abort path,
1292 				 * so any errors can only be ignored or reported
1293 				 * while attempting to cleanup all resources.
1294 				 */
1295 				btrfs_err(fs_info,
1296 "block group for delayed ref at %llu was not found while destroying ref head",
1297 					  head->bytenr);
1298 			} else {
1299 				spin_lock(&bg->space_info->lock);
1300 				spin_lock(&bg->lock);
1301 				bg->pinned += head->num_bytes;
1302 				btrfs_space_info_update_bytes_pinned(bg->space_info,
1303 								     head->num_bytes);
1304 				bg->reserved -= head->num_bytes;
1305 				bg->space_info->bytes_reserved -= head->num_bytes;
1306 				spin_unlock(&bg->lock);
1307 				spin_unlock(&bg->space_info->lock);
1308 
1309 				btrfs_put_block_group(bg);
1310 			}
1311 
1312 			btrfs_error_unpin_extent_range(fs_info, head->bytenr,
1313 				head->bytenr + head->num_bytes - 1);
1314 		}
1315 		if (!testing)
1316 			btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head);
1317 		btrfs_put_delayed_ref_head(head);
1318 		cond_resched();
1319 		spin_lock(&delayed_refs->lock);
1320 	}
1321 
1322 	if (!testing)
1323 		btrfs_qgroup_destroy_extent_records(trans);
1324 
1325 	spin_unlock(&delayed_refs->lock);
1326 }
1327 
1328 void __cold btrfs_delayed_ref_exit(void)
1329 {
1330 	kmem_cache_destroy(btrfs_delayed_ref_head_cachep);
1331 	kmem_cache_destroy(btrfs_delayed_ref_node_cachep);
1332 	kmem_cache_destroy(btrfs_delayed_extent_op_cachep);
1333 }
1334 
1335 int __init btrfs_delayed_ref_init(void)
1336 {
1337 	btrfs_delayed_ref_head_cachep = KMEM_CACHE(btrfs_delayed_ref_head, 0);
1338 	if (!btrfs_delayed_ref_head_cachep)
1339 		return -ENOMEM;
1340 
1341 	btrfs_delayed_ref_node_cachep = KMEM_CACHE(btrfs_delayed_ref_node, 0);
1342 	if (!btrfs_delayed_ref_node_cachep)
1343 		goto fail;
1344 
1345 	btrfs_delayed_extent_op_cachep = KMEM_CACHE(btrfs_delayed_extent_op, 0);
1346 	if (!btrfs_delayed_extent_op_cachep)
1347 		goto fail;
1348 
1349 	return 0;
1350 fail:
1351 	btrfs_delayed_ref_exit();
1352 	return -ENOMEM;
1353 }
1354