xref: /openbmc/linux/fs/btrfs/delayed-ref.c (revision c23b8e7a)
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 "ctree.h"
10 #include "delayed-ref.h"
11 #include "transaction.h"
12 #include "qgroup.h"
13 
14 struct kmem_cache *btrfs_delayed_ref_head_cachep;
15 struct kmem_cache *btrfs_delayed_tree_ref_cachep;
16 struct kmem_cache *btrfs_delayed_data_ref_cachep;
17 struct kmem_cache *btrfs_delayed_extent_op_cachep;
18 /*
19  * delayed back reference update tracking.  For subvolume trees
20  * we queue up extent allocations and backref maintenance for
21  * delayed processing.   This avoids deep call chains where we
22  * add extents in the middle of btrfs_search_slot, and it allows
23  * us to buffer up frequently modified backrefs in an rb tree instead
24  * of hammering updates on the extent allocation tree.
25  */
26 
27 /*
28  * compare two delayed tree backrefs with same bytenr and type
29  */
30 static int comp_tree_refs(struct btrfs_delayed_tree_ref *ref1,
31 			  struct btrfs_delayed_tree_ref *ref2)
32 {
33 	if (ref1->node.type == BTRFS_TREE_BLOCK_REF_KEY) {
34 		if (ref1->root < ref2->root)
35 			return -1;
36 		if (ref1->root > ref2->root)
37 			return 1;
38 	} else {
39 		if (ref1->parent < ref2->parent)
40 			return -1;
41 		if (ref1->parent > ref2->parent)
42 			return 1;
43 	}
44 	return 0;
45 }
46 
47 /*
48  * compare two delayed data backrefs with same bytenr and type
49  */
50 static int comp_data_refs(struct btrfs_delayed_data_ref *ref1,
51 			  struct btrfs_delayed_data_ref *ref2)
52 {
53 	if (ref1->node.type == BTRFS_EXTENT_DATA_REF_KEY) {
54 		if (ref1->root < ref2->root)
55 			return -1;
56 		if (ref1->root > ref2->root)
57 			return 1;
58 		if (ref1->objectid < ref2->objectid)
59 			return -1;
60 		if (ref1->objectid > ref2->objectid)
61 			return 1;
62 		if (ref1->offset < ref2->offset)
63 			return -1;
64 		if (ref1->offset > ref2->offset)
65 			return 1;
66 	} else {
67 		if (ref1->parent < ref2->parent)
68 			return -1;
69 		if (ref1->parent > ref2->parent)
70 			return 1;
71 	}
72 	return 0;
73 }
74 
75 static int comp_refs(struct btrfs_delayed_ref_node *ref1,
76 		     struct btrfs_delayed_ref_node *ref2,
77 		     bool check_seq)
78 {
79 	int ret = 0;
80 
81 	if (ref1->type < ref2->type)
82 		return -1;
83 	if (ref1->type > ref2->type)
84 		return 1;
85 	if (ref1->type == BTRFS_TREE_BLOCK_REF_KEY ||
86 	    ref1->type == BTRFS_SHARED_BLOCK_REF_KEY)
87 		ret = comp_tree_refs(btrfs_delayed_node_to_tree_ref(ref1),
88 				     btrfs_delayed_node_to_tree_ref(ref2));
89 	else
90 		ret = comp_data_refs(btrfs_delayed_node_to_data_ref(ref1),
91 				     btrfs_delayed_node_to_data_ref(ref2));
92 	if (ret)
93 		return ret;
94 	if (check_seq) {
95 		if (ref1->seq < ref2->seq)
96 			return -1;
97 		if (ref1->seq > ref2->seq)
98 			return 1;
99 	}
100 	return 0;
101 }
102 
103 /* insert a new ref to head ref rbtree */
104 static struct btrfs_delayed_ref_head *htree_insert(struct rb_root_cached *root,
105 						   struct rb_node *node)
106 {
107 	struct rb_node **p = &root->rb_root.rb_node;
108 	struct rb_node *parent_node = NULL;
109 	struct btrfs_delayed_ref_head *entry;
110 	struct btrfs_delayed_ref_head *ins;
111 	u64 bytenr;
112 	bool leftmost = true;
113 
114 	ins = rb_entry(node, struct btrfs_delayed_ref_head, href_node);
115 	bytenr = ins->bytenr;
116 	while (*p) {
117 		parent_node = *p;
118 		entry = rb_entry(parent_node, struct btrfs_delayed_ref_head,
119 				 href_node);
120 
121 		if (bytenr < entry->bytenr) {
122 			p = &(*p)->rb_left;
123 		} else if (bytenr > entry->bytenr) {
124 			p = &(*p)->rb_right;
125 			leftmost = false;
126 		} else {
127 			return entry;
128 		}
129 	}
130 
131 	rb_link_node(node, parent_node, p);
132 	rb_insert_color_cached(node, root, leftmost);
133 	return NULL;
134 }
135 
136 static struct btrfs_delayed_ref_node* tree_insert(struct rb_root_cached *root,
137 		struct btrfs_delayed_ref_node *ins)
138 {
139 	struct rb_node **p = &root->rb_root.rb_node;
140 	struct rb_node *node = &ins->ref_node;
141 	struct rb_node *parent_node = NULL;
142 	struct btrfs_delayed_ref_node *entry;
143 	bool leftmost = true;
144 
145 	while (*p) {
146 		int comp;
147 
148 		parent_node = *p;
149 		entry = rb_entry(parent_node, struct btrfs_delayed_ref_node,
150 				 ref_node);
151 		comp = comp_refs(ins, entry, true);
152 		if (comp < 0) {
153 			p = &(*p)->rb_left;
154 		} else if (comp > 0) {
155 			p = &(*p)->rb_right;
156 			leftmost = false;
157 		} else {
158 			return entry;
159 		}
160 	}
161 
162 	rb_link_node(node, parent_node, p);
163 	rb_insert_color_cached(node, root, leftmost);
164 	return NULL;
165 }
166 
167 static struct btrfs_delayed_ref_head *find_first_ref_head(
168 		struct btrfs_delayed_ref_root *dr)
169 {
170 	struct rb_node *n;
171 	struct btrfs_delayed_ref_head *entry;
172 
173 	n = rb_first_cached(&dr->href_root);
174 	if (!n)
175 		return NULL;
176 
177 	entry = rb_entry(n, struct btrfs_delayed_ref_head, href_node);
178 
179 	return entry;
180 }
181 
182 /*
183  * Find a head entry based on bytenr. This returns the delayed ref head if it
184  * was able to find one, or NULL if nothing was in that spot.  If return_bigger
185  * is given, the next bigger entry is returned if no exact match is found.
186  */
187 static struct btrfs_delayed_ref_head *find_ref_head(
188 		struct btrfs_delayed_ref_root *dr, u64 bytenr,
189 		bool return_bigger)
190 {
191 	struct rb_root *root = &dr->href_root.rb_root;
192 	struct rb_node *n;
193 	struct btrfs_delayed_ref_head *entry;
194 
195 	n = root->rb_node;
196 	entry = NULL;
197 	while (n) {
198 		entry = rb_entry(n, struct btrfs_delayed_ref_head, href_node);
199 
200 		if (bytenr < entry->bytenr)
201 			n = n->rb_left;
202 		else if (bytenr > entry->bytenr)
203 			n = n->rb_right;
204 		else
205 			return entry;
206 	}
207 	if (entry && return_bigger) {
208 		if (bytenr > entry->bytenr) {
209 			n = rb_next(&entry->href_node);
210 			if (!n)
211 				return NULL;
212 			entry = rb_entry(n, struct btrfs_delayed_ref_head,
213 					 href_node);
214 		}
215 		return entry;
216 	}
217 	return NULL;
218 }
219 
220 int btrfs_delayed_ref_lock(struct btrfs_delayed_ref_root *delayed_refs,
221 			   struct btrfs_delayed_ref_head *head)
222 {
223 	lockdep_assert_held(&delayed_refs->lock);
224 	if (mutex_trylock(&head->mutex))
225 		return 0;
226 
227 	refcount_inc(&head->refs);
228 	spin_unlock(&delayed_refs->lock);
229 
230 	mutex_lock(&head->mutex);
231 	spin_lock(&delayed_refs->lock);
232 	if (RB_EMPTY_NODE(&head->href_node)) {
233 		mutex_unlock(&head->mutex);
234 		btrfs_put_delayed_ref_head(head);
235 		return -EAGAIN;
236 	}
237 	btrfs_put_delayed_ref_head(head);
238 	return 0;
239 }
240 
241 static inline void drop_delayed_ref(struct btrfs_trans_handle *trans,
242 				    struct btrfs_delayed_ref_root *delayed_refs,
243 				    struct btrfs_delayed_ref_head *head,
244 				    struct btrfs_delayed_ref_node *ref)
245 {
246 	lockdep_assert_held(&head->lock);
247 	rb_erase_cached(&ref->ref_node, &head->ref_tree);
248 	RB_CLEAR_NODE(&ref->ref_node);
249 	if (!list_empty(&ref->add_list))
250 		list_del(&ref->add_list);
251 	ref->in_tree = 0;
252 	btrfs_put_delayed_ref(ref);
253 	atomic_dec(&delayed_refs->num_entries);
254 	if (trans->delayed_ref_updates)
255 		trans->delayed_ref_updates--;
256 }
257 
258 static bool merge_ref(struct btrfs_trans_handle *trans,
259 		      struct btrfs_delayed_ref_root *delayed_refs,
260 		      struct btrfs_delayed_ref_head *head,
261 		      struct btrfs_delayed_ref_node *ref,
262 		      u64 seq)
263 {
264 	struct btrfs_delayed_ref_node *next;
265 	struct rb_node *node = rb_next(&ref->ref_node);
266 	bool done = false;
267 
268 	while (!done && node) {
269 		int mod;
270 
271 		next = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
272 		node = rb_next(node);
273 		if (seq && next->seq >= seq)
274 			break;
275 		if (comp_refs(ref, next, false))
276 			break;
277 
278 		if (ref->action == next->action) {
279 			mod = next->ref_mod;
280 		} else {
281 			if (ref->ref_mod < next->ref_mod) {
282 				swap(ref, next);
283 				done = true;
284 			}
285 			mod = -next->ref_mod;
286 		}
287 
288 		drop_delayed_ref(trans, delayed_refs, head, next);
289 		ref->ref_mod += mod;
290 		if (ref->ref_mod == 0) {
291 			drop_delayed_ref(trans, delayed_refs, head, ref);
292 			done = true;
293 		} else {
294 			/*
295 			 * Can't have multiples of the same ref on a tree block.
296 			 */
297 			WARN_ON(ref->type == BTRFS_TREE_BLOCK_REF_KEY ||
298 				ref->type == BTRFS_SHARED_BLOCK_REF_KEY);
299 		}
300 	}
301 
302 	return done;
303 }
304 
305 void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
306 			      struct btrfs_delayed_ref_root *delayed_refs,
307 			      struct btrfs_delayed_ref_head *head)
308 {
309 	struct btrfs_fs_info *fs_info = trans->fs_info;
310 	struct btrfs_delayed_ref_node *ref;
311 	struct rb_node *node;
312 	u64 seq = 0;
313 
314 	lockdep_assert_held(&head->lock);
315 
316 	if (RB_EMPTY_ROOT(&head->ref_tree.rb_root))
317 		return;
318 
319 	/* We don't have too many refs to merge for data. */
320 	if (head->is_data)
321 		return;
322 
323 	spin_lock(&fs_info->tree_mod_seq_lock);
324 	if (!list_empty(&fs_info->tree_mod_seq_list)) {
325 		struct seq_list *elem;
326 
327 		elem = list_first_entry(&fs_info->tree_mod_seq_list,
328 					struct seq_list, list);
329 		seq = elem->seq;
330 	}
331 	spin_unlock(&fs_info->tree_mod_seq_lock);
332 
333 again:
334 	for (node = rb_first_cached(&head->ref_tree); node;
335 	     node = rb_next(node)) {
336 		ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
337 		if (seq && ref->seq >= seq)
338 			continue;
339 		if (merge_ref(trans, delayed_refs, head, ref, seq))
340 			goto again;
341 	}
342 }
343 
344 int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq)
345 {
346 	struct seq_list *elem;
347 	int ret = 0;
348 
349 	spin_lock(&fs_info->tree_mod_seq_lock);
350 	if (!list_empty(&fs_info->tree_mod_seq_list)) {
351 		elem = list_first_entry(&fs_info->tree_mod_seq_list,
352 					struct seq_list, list);
353 		if (seq >= elem->seq) {
354 			btrfs_debug(fs_info,
355 				"holding back delayed_ref %#x.%x, lowest is %#x.%x",
356 				(u32)(seq >> 32), (u32)seq,
357 				(u32)(elem->seq >> 32), (u32)elem->seq);
358 			ret = 1;
359 		}
360 	}
361 
362 	spin_unlock(&fs_info->tree_mod_seq_lock);
363 	return ret;
364 }
365 
366 struct btrfs_delayed_ref_head *btrfs_select_ref_head(
367 		struct btrfs_delayed_ref_root *delayed_refs)
368 {
369 	struct btrfs_delayed_ref_head *head;
370 
371 again:
372 	head = find_ref_head(delayed_refs, delayed_refs->run_delayed_start,
373 			     true);
374 	if (!head && delayed_refs->run_delayed_start != 0) {
375 		delayed_refs->run_delayed_start = 0;
376 		head = find_first_ref_head(delayed_refs);
377 	}
378 	if (!head)
379 		return NULL;
380 
381 	while (head->processing) {
382 		struct rb_node *node;
383 
384 		node = rb_next(&head->href_node);
385 		if (!node) {
386 			if (delayed_refs->run_delayed_start == 0)
387 				return NULL;
388 			delayed_refs->run_delayed_start = 0;
389 			goto again;
390 		}
391 		head = rb_entry(node, struct btrfs_delayed_ref_head,
392 				href_node);
393 	}
394 
395 	head->processing = 1;
396 	WARN_ON(delayed_refs->num_heads_ready == 0);
397 	delayed_refs->num_heads_ready--;
398 	delayed_refs->run_delayed_start = head->bytenr +
399 		head->num_bytes;
400 	return head;
401 }
402 
403 /*
404  * Helper to insert the ref_node to the tail or merge with tail.
405  *
406  * Return 0 for insert.
407  * Return >0 for merge.
408  */
409 static int insert_delayed_ref(struct btrfs_trans_handle *trans,
410 			      struct btrfs_delayed_ref_root *root,
411 			      struct btrfs_delayed_ref_head *href,
412 			      struct btrfs_delayed_ref_node *ref)
413 {
414 	struct btrfs_delayed_ref_node *exist;
415 	int mod;
416 	int ret = 0;
417 
418 	spin_lock(&href->lock);
419 	exist = tree_insert(&href->ref_tree, ref);
420 	if (!exist)
421 		goto inserted;
422 
423 	/* Now we are sure we can merge */
424 	ret = 1;
425 	if (exist->action == ref->action) {
426 		mod = ref->ref_mod;
427 	} else {
428 		/* Need to change action */
429 		if (exist->ref_mod < ref->ref_mod) {
430 			exist->action = ref->action;
431 			mod = -exist->ref_mod;
432 			exist->ref_mod = ref->ref_mod;
433 			if (ref->action == BTRFS_ADD_DELAYED_REF)
434 				list_add_tail(&exist->add_list,
435 					      &href->ref_add_list);
436 			else if (ref->action == BTRFS_DROP_DELAYED_REF) {
437 				ASSERT(!list_empty(&exist->add_list));
438 				list_del(&exist->add_list);
439 			} else {
440 				ASSERT(0);
441 			}
442 		} else
443 			mod = -ref->ref_mod;
444 	}
445 	exist->ref_mod += mod;
446 
447 	/* remove existing tail if its ref_mod is zero */
448 	if (exist->ref_mod == 0)
449 		drop_delayed_ref(trans, root, href, exist);
450 	spin_unlock(&href->lock);
451 	return ret;
452 inserted:
453 	if (ref->action == BTRFS_ADD_DELAYED_REF)
454 		list_add_tail(&ref->add_list, &href->ref_add_list);
455 	atomic_inc(&root->num_entries);
456 	trans->delayed_ref_updates++;
457 	spin_unlock(&href->lock);
458 	return ret;
459 }
460 
461 /*
462  * helper function to update the accounting in the head ref
463  * existing and update must have the same bytenr
464  */
465 static noinline void
466 update_existing_head_ref(struct btrfs_delayed_ref_root *delayed_refs,
467 			 struct btrfs_delayed_ref_head *existing,
468 			 struct btrfs_delayed_ref_head *update,
469 			 int *old_ref_mod_ret)
470 {
471 	int old_ref_mod;
472 
473 	BUG_ON(existing->is_data != update->is_data);
474 
475 	spin_lock(&existing->lock);
476 	if (update->must_insert_reserved) {
477 		/* if the extent was freed and then
478 		 * reallocated before the delayed ref
479 		 * entries were processed, we can end up
480 		 * with an existing head ref without
481 		 * the must_insert_reserved flag set.
482 		 * Set it again here
483 		 */
484 		existing->must_insert_reserved = update->must_insert_reserved;
485 
486 		/*
487 		 * update the num_bytes so we make sure the accounting
488 		 * is done correctly
489 		 */
490 		existing->num_bytes = update->num_bytes;
491 
492 	}
493 
494 	if (update->extent_op) {
495 		if (!existing->extent_op) {
496 			existing->extent_op = update->extent_op;
497 		} else {
498 			if (update->extent_op->update_key) {
499 				memcpy(&existing->extent_op->key,
500 				       &update->extent_op->key,
501 				       sizeof(update->extent_op->key));
502 				existing->extent_op->update_key = true;
503 			}
504 			if (update->extent_op->update_flags) {
505 				existing->extent_op->flags_to_set |=
506 					update->extent_op->flags_to_set;
507 				existing->extent_op->update_flags = true;
508 			}
509 			btrfs_free_delayed_extent_op(update->extent_op);
510 		}
511 	}
512 	/*
513 	 * update the reference mod on the head to reflect this new operation,
514 	 * only need the lock for this case cause we could be processing it
515 	 * currently, for refs we just added we know we're a-ok.
516 	 */
517 	old_ref_mod = existing->total_ref_mod;
518 	if (old_ref_mod_ret)
519 		*old_ref_mod_ret = old_ref_mod;
520 	existing->ref_mod += update->ref_mod;
521 	existing->total_ref_mod += update->ref_mod;
522 
523 	/*
524 	 * If we are going to from a positive ref mod to a negative or vice
525 	 * versa we need to make sure to adjust pending_csums accordingly.
526 	 */
527 	if (existing->is_data) {
528 		if (existing->total_ref_mod >= 0 && old_ref_mod < 0)
529 			delayed_refs->pending_csums -= existing->num_bytes;
530 		if (existing->total_ref_mod < 0 && old_ref_mod >= 0)
531 			delayed_refs->pending_csums += existing->num_bytes;
532 	}
533 	spin_unlock(&existing->lock);
534 }
535 
536 static void init_delayed_ref_head(struct btrfs_delayed_ref_head *head_ref,
537 				  struct btrfs_qgroup_extent_record *qrecord,
538 				  u64 bytenr, u64 num_bytes, u64 ref_root,
539 				  u64 reserved, int action, bool is_data,
540 				  bool is_system)
541 {
542 	int count_mod = 1;
543 	int must_insert_reserved = 0;
544 
545 	/* If reserved is provided, it must be a data extent. */
546 	BUG_ON(!is_data && reserved);
547 
548 	/*
549 	 * The head node stores the sum of all the mods, so dropping a ref
550 	 * should drop the sum in the head node by one.
551 	 */
552 	if (action == BTRFS_UPDATE_DELAYED_HEAD)
553 		count_mod = 0;
554 	else if (action == BTRFS_DROP_DELAYED_REF)
555 		count_mod = -1;
556 
557 	/*
558 	 * BTRFS_ADD_DELAYED_EXTENT means that we need to update the reserved
559 	 * accounting when the extent is finally added, or if a later
560 	 * modification deletes the delayed ref without ever inserting the
561 	 * extent into the extent allocation tree.  ref->must_insert_reserved
562 	 * is the flag used to record that accounting mods are required.
563 	 *
564 	 * Once we record must_insert_reserved, switch the action to
565 	 * BTRFS_ADD_DELAYED_REF because other special casing is not required.
566 	 */
567 	if (action == BTRFS_ADD_DELAYED_EXTENT)
568 		must_insert_reserved = 1;
569 	else
570 		must_insert_reserved = 0;
571 
572 	refcount_set(&head_ref->refs, 1);
573 	head_ref->bytenr = bytenr;
574 	head_ref->num_bytes = num_bytes;
575 	head_ref->ref_mod = count_mod;
576 	head_ref->must_insert_reserved = must_insert_reserved;
577 	head_ref->is_data = is_data;
578 	head_ref->is_system = is_system;
579 	head_ref->ref_tree = RB_ROOT_CACHED;
580 	INIT_LIST_HEAD(&head_ref->ref_add_list);
581 	RB_CLEAR_NODE(&head_ref->href_node);
582 	head_ref->processing = 0;
583 	head_ref->total_ref_mod = count_mod;
584 	head_ref->qgroup_reserved = 0;
585 	head_ref->qgroup_ref_root = 0;
586 	spin_lock_init(&head_ref->lock);
587 	mutex_init(&head_ref->mutex);
588 
589 	if (qrecord) {
590 		if (ref_root && reserved) {
591 			head_ref->qgroup_ref_root = ref_root;
592 			head_ref->qgroup_reserved = reserved;
593 		}
594 
595 		qrecord->bytenr = bytenr;
596 		qrecord->num_bytes = num_bytes;
597 		qrecord->old_roots = NULL;
598 	}
599 }
600 
601 /*
602  * helper function to actually insert a head node into the rbtree.
603  * this does all the dirty work in terms of maintaining the correct
604  * overall modification count.
605  */
606 static noinline struct btrfs_delayed_ref_head *
607 add_delayed_ref_head(struct btrfs_trans_handle *trans,
608 		     struct btrfs_delayed_ref_head *head_ref,
609 		     struct btrfs_qgroup_extent_record *qrecord,
610 		     int action, int *qrecord_inserted_ret,
611 		     int *old_ref_mod, int *new_ref_mod)
612 {
613 	struct btrfs_delayed_ref_head *existing;
614 	struct btrfs_delayed_ref_root *delayed_refs;
615 	int qrecord_inserted = 0;
616 
617 	delayed_refs = &trans->transaction->delayed_refs;
618 
619 	/* Record qgroup extent info if provided */
620 	if (qrecord) {
621 		if (btrfs_qgroup_trace_extent_nolock(trans->fs_info,
622 					delayed_refs, qrecord))
623 			kfree(qrecord);
624 		else
625 			qrecord_inserted = 1;
626 	}
627 
628 	trace_add_delayed_ref_head(trans->fs_info, head_ref, action);
629 
630 	existing = htree_insert(&delayed_refs->href_root,
631 				&head_ref->href_node);
632 	if (existing) {
633 		WARN_ON(qrecord && head_ref->qgroup_ref_root
634 			&& head_ref->qgroup_reserved
635 			&& existing->qgroup_ref_root
636 			&& existing->qgroup_reserved);
637 		update_existing_head_ref(delayed_refs, existing, head_ref,
638 					 old_ref_mod);
639 		/*
640 		 * we've updated the existing ref, free the newly
641 		 * allocated ref
642 		 */
643 		kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref);
644 		head_ref = existing;
645 	} else {
646 		if (old_ref_mod)
647 			*old_ref_mod = 0;
648 		if (head_ref->is_data && head_ref->ref_mod < 0)
649 			delayed_refs->pending_csums += head_ref->num_bytes;
650 		delayed_refs->num_heads++;
651 		delayed_refs->num_heads_ready++;
652 		atomic_inc(&delayed_refs->num_entries);
653 		trans->delayed_ref_updates++;
654 	}
655 	if (qrecord_inserted_ret)
656 		*qrecord_inserted_ret = qrecord_inserted;
657 	if (new_ref_mod)
658 		*new_ref_mod = head_ref->total_ref_mod;
659 
660 	return head_ref;
661 }
662 
663 /*
664  * init_delayed_ref_common - Initialize the structure which represents a
665  *			     modification to a an extent.
666  *
667  * @fs_info:    Internal to the mounted filesystem mount structure.
668  *
669  * @ref:	The structure which is going to be initialized.
670  *
671  * @bytenr:	The logical address of the extent for which a modification is
672  *		going to be recorded.
673  *
674  * @num_bytes:  Size of the extent whose modification is being recorded.
675  *
676  * @ref_root:	The id of the root where this modification has originated, this
677  *		can be either one of the well-known metadata trees or the
678  *		subvolume id which references this extent.
679  *
680  * @action:	Can be one of BTRFS_ADD_DELAYED_REF/BTRFS_DROP_DELAYED_REF or
681  *		BTRFS_ADD_DELAYED_EXTENT
682  *
683  * @ref_type:	Holds the type of the extent which is being recorded, can be
684  *		one of BTRFS_SHARED_BLOCK_REF_KEY/BTRFS_TREE_BLOCK_REF_KEY
685  *		when recording a metadata extent or BTRFS_SHARED_DATA_REF_KEY/
686  *		BTRFS_EXTENT_DATA_REF_KEY when recording data extent
687  */
688 static void init_delayed_ref_common(struct btrfs_fs_info *fs_info,
689 				    struct btrfs_delayed_ref_node *ref,
690 				    u64 bytenr, u64 num_bytes, u64 ref_root,
691 				    int action, u8 ref_type)
692 {
693 	u64 seq = 0;
694 
695 	if (action == BTRFS_ADD_DELAYED_EXTENT)
696 		action = BTRFS_ADD_DELAYED_REF;
697 
698 	if (is_fstree(ref_root))
699 		seq = atomic64_read(&fs_info->tree_mod_seq);
700 
701 	refcount_set(&ref->refs, 1);
702 	ref->bytenr = bytenr;
703 	ref->num_bytes = num_bytes;
704 	ref->ref_mod = 1;
705 	ref->action = action;
706 	ref->is_head = 0;
707 	ref->in_tree = 1;
708 	ref->seq = seq;
709 	ref->type = ref_type;
710 	RB_CLEAR_NODE(&ref->ref_node);
711 	INIT_LIST_HEAD(&ref->add_list);
712 }
713 
714 /*
715  * add a delayed tree ref.  This does all of the accounting required
716  * to make sure the delayed ref is eventually processed before this
717  * transaction commits.
718  */
719 int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans,
720 			       u64 bytenr, u64 num_bytes, u64 parent,
721 			       u64 ref_root,  int level, int action,
722 			       struct btrfs_delayed_extent_op *extent_op,
723 			       int *old_ref_mod, int *new_ref_mod)
724 {
725 	struct btrfs_fs_info *fs_info = trans->fs_info;
726 	struct btrfs_delayed_tree_ref *ref;
727 	struct btrfs_delayed_ref_head *head_ref;
728 	struct btrfs_delayed_ref_root *delayed_refs;
729 	struct btrfs_qgroup_extent_record *record = NULL;
730 	int qrecord_inserted;
731 	bool is_system = (ref_root == BTRFS_CHUNK_TREE_OBJECTID);
732 	int ret;
733 	u8 ref_type;
734 
735 	BUG_ON(extent_op && extent_op->is_data);
736 	ref = kmem_cache_alloc(btrfs_delayed_tree_ref_cachep, GFP_NOFS);
737 	if (!ref)
738 		return -ENOMEM;
739 
740 	head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
741 	if (!head_ref) {
742 		kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
743 		return -ENOMEM;
744 	}
745 
746 	if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&
747 	    is_fstree(ref_root)) {
748 		record = kmalloc(sizeof(*record), GFP_NOFS);
749 		if (!record) {
750 			kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
751 			kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref);
752 			return -ENOMEM;
753 		}
754 	}
755 
756 	if (parent)
757 		ref_type = BTRFS_SHARED_BLOCK_REF_KEY;
758 	else
759 		ref_type = BTRFS_TREE_BLOCK_REF_KEY;
760 
761 	init_delayed_ref_common(fs_info, &ref->node, bytenr, num_bytes,
762 				ref_root, action, ref_type);
763 	ref->root = ref_root;
764 	ref->parent = parent;
765 	ref->level = level;
766 
767 	init_delayed_ref_head(head_ref, record, bytenr, num_bytes,
768 			      ref_root, 0, action, false, is_system);
769 	head_ref->extent_op = extent_op;
770 
771 	delayed_refs = &trans->transaction->delayed_refs;
772 	spin_lock(&delayed_refs->lock);
773 
774 	/*
775 	 * insert both the head node and the new ref without dropping
776 	 * the spin lock
777 	 */
778 	head_ref = add_delayed_ref_head(trans, head_ref, record,
779 					action, &qrecord_inserted,
780 					old_ref_mod, new_ref_mod);
781 
782 	ret = insert_delayed_ref(trans, delayed_refs, head_ref, &ref->node);
783 	spin_unlock(&delayed_refs->lock);
784 
785 	trace_add_delayed_tree_ref(fs_info, &ref->node, ref,
786 				   action == BTRFS_ADD_DELAYED_EXTENT ?
787 				   BTRFS_ADD_DELAYED_REF : action);
788 	if (ret > 0)
789 		kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
790 
791 	if (qrecord_inserted)
792 		btrfs_qgroup_trace_extent_post(fs_info, record);
793 
794 	return 0;
795 }
796 
797 /*
798  * add a delayed data ref. it's similar to btrfs_add_delayed_tree_ref.
799  */
800 int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans,
801 			       u64 bytenr, u64 num_bytes,
802 			       u64 parent, u64 ref_root,
803 			       u64 owner, u64 offset, u64 reserved, int action,
804 			       int *old_ref_mod, int *new_ref_mod)
805 {
806 	struct btrfs_fs_info *fs_info = trans->fs_info;
807 	struct btrfs_delayed_data_ref *ref;
808 	struct btrfs_delayed_ref_head *head_ref;
809 	struct btrfs_delayed_ref_root *delayed_refs;
810 	struct btrfs_qgroup_extent_record *record = NULL;
811 	int qrecord_inserted;
812 	int ret;
813 	u8 ref_type;
814 
815 	ref = kmem_cache_alloc(btrfs_delayed_data_ref_cachep, GFP_NOFS);
816 	if (!ref)
817 		return -ENOMEM;
818 
819 	if (parent)
820 	        ref_type = BTRFS_SHARED_DATA_REF_KEY;
821 	else
822 	        ref_type = BTRFS_EXTENT_DATA_REF_KEY;
823 	init_delayed_ref_common(fs_info, &ref->node, bytenr, num_bytes,
824 				ref_root, action, ref_type);
825 	ref->root = ref_root;
826 	ref->parent = parent;
827 	ref->objectid = owner;
828 	ref->offset = offset;
829 
830 
831 	head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
832 	if (!head_ref) {
833 		kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
834 		return -ENOMEM;
835 	}
836 
837 	if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&
838 	    is_fstree(ref_root)) {
839 		record = kmalloc(sizeof(*record), GFP_NOFS);
840 		if (!record) {
841 			kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
842 			kmem_cache_free(btrfs_delayed_ref_head_cachep,
843 					head_ref);
844 			return -ENOMEM;
845 		}
846 	}
847 
848 	init_delayed_ref_head(head_ref, record, bytenr, num_bytes, ref_root,
849 			      reserved, action, true, false);
850 	head_ref->extent_op = NULL;
851 
852 	delayed_refs = &trans->transaction->delayed_refs;
853 	spin_lock(&delayed_refs->lock);
854 
855 	/*
856 	 * insert both the head node and the new ref without dropping
857 	 * the spin lock
858 	 */
859 	head_ref = add_delayed_ref_head(trans, head_ref, record,
860 					action, &qrecord_inserted,
861 					old_ref_mod, new_ref_mod);
862 
863 	ret = insert_delayed_ref(trans, delayed_refs, head_ref, &ref->node);
864 	spin_unlock(&delayed_refs->lock);
865 
866 	trace_add_delayed_data_ref(trans->fs_info, &ref->node, ref,
867 				   action == BTRFS_ADD_DELAYED_EXTENT ?
868 				   BTRFS_ADD_DELAYED_REF : action);
869 	if (ret > 0)
870 		kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
871 
872 
873 	if (qrecord_inserted)
874 		return btrfs_qgroup_trace_extent_post(fs_info, record);
875 	return 0;
876 }
877 
878 int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info,
879 				struct btrfs_trans_handle *trans,
880 				u64 bytenr, u64 num_bytes,
881 				struct btrfs_delayed_extent_op *extent_op)
882 {
883 	struct btrfs_delayed_ref_head *head_ref;
884 	struct btrfs_delayed_ref_root *delayed_refs;
885 
886 	head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
887 	if (!head_ref)
888 		return -ENOMEM;
889 
890 	init_delayed_ref_head(head_ref, NULL, bytenr, num_bytes, 0, 0,
891 			      BTRFS_UPDATE_DELAYED_HEAD, extent_op->is_data,
892 			      false);
893 	head_ref->extent_op = extent_op;
894 
895 	delayed_refs = &trans->transaction->delayed_refs;
896 	spin_lock(&delayed_refs->lock);
897 
898 	add_delayed_ref_head(trans, head_ref, NULL, BTRFS_UPDATE_DELAYED_HEAD,
899 			     NULL, NULL, NULL);
900 
901 	spin_unlock(&delayed_refs->lock);
902 	return 0;
903 }
904 
905 /*
906  * this does a simple search for the head node for a given extent.
907  * It must be called with the delayed ref spinlock held, and it returns
908  * the head node if any where found, or NULL if not.
909  */
910 struct btrfs_delayed_ref_head *
911 btrfs_find_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs, u64 bytenr)
912 {
913 	return find_ref_head(delayed_refs, bytenr, false);
914 }
915 
916 void __cold btrfs_delayed_ref_exit(void)
917 {
918 	kmem_cache_destroy(btrfs_delayed_ref_head_cachep);
919 	kmem_cache_destroy(btrfs_delayed_tree_ref_cachep);
920 	kmem_cache_destroy(btrfs_delayed_data_ref_cachep);
921 	kmem_cache_destroy(btrfs_delayed_extent_op_cachep);
922 }
923 
924 int __init btrfs_delayed_ref_init(void)
925 {
926 	btrfs_delayed_ref_head_cachep = kmem_cache_create(
927 				"btrfs_delayed_ref_head",
928 				sizeof(struct btrfs_delayed_ref_head), 0,
929 				SLAB_MEM_SPREAD, NULL);
930 	if (!btrfs_delayed_ref_head_cachep)
931 		goto fail;
932 
933 	btrfs_delayed_tree_ref_cachep = kmem_cache_create(
934 				"btrfs_delayed_tree_ref",
935 				sizeof(struct btrfs_delayed_tree_ref), 0,
936 				SLAB_MEM_SPREAD, NULL);
937 	if (!btrfs_delayed_tree_ref_cachep)
938 		goto fail;
939 
940 	btrfs_delayed_data_ref_cachep = kmem_cache_create(
941 				"btrfs_delayed_data_ref",
942 				sizeof(struct btrfs_delayed_data_ref), 0,
943 				SLAB_MEM_SPREAD, NULL);
944 	if (!btrfs_delayed_data_ref_cachep)
945 		goto fail;
946 
947 	btrfs_delayed_extent_op_cachep = kmem_cache_create(
948 				"btrfs_delayed_extent_op",
949 				sizeof(struct btrfs_delayed_extent_op), 0,
950 				SLAB_MEM_SPREAD, NULL);
951 	if (!btrfs_delayed_extent_op_cachep)
952 		goto fail;
953 
954 	return 0;
955 fail:
956 	btrfs_delayed_ref_exit();
957 	return -ENOMEM;
958 }
959