xref: /openbmc/linux/fs/btrfs/delayed-inode.c (revision 63705da3)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2011 Fujitsu.  All rights reserved.
4  * Written by Miao Xie <miaox@cn.fujitsu.com>
5  */
6 
7 #include <linux/slab.h>
8 #include <linux/iversion.h>
9 #include "misc.h"
10 #include "delayed-inode.h"
11 #include "disk-io.h"
12 #include "transaction.h"
13 #include "ctree.h"
14 #include "qgroup.h"
15 #include "locking.h"
16 
17 #define BTRFS_DELAYED_WRITEBACK		512
18 #define BTRFS_DELAYED_BACKGROUND	128
19 #define BTRFS_DELAYED_BATCH		16
20 
21 static struct kmem_cache *delayed_node_cache;
22 
23 int __init btrfs_delayed_inode_init(void)
24 {
25 	delayed_node_cache = kmem_cache_create("btrfs_delayed_node",
26 					sizeof(struct btrfs_delayed_node),
27 					0,
28 					SLAB_MEM_SPREAD,
29 					NULL);
30 	if (!delayed_node_cache)
31 		return -ENOMEM;
32 	return 0;
33 }
34 
35 void __cold btrfs_delayed_inode_exit(void)
36 {
37 	kmem_cache_destroy(delayed_node_cache);
38 }
39 
40 static inline void btrfs_init_delayed_node(
41 				struct btrfs_delayed_node *delayed_node,
42 				struct btrfs_root *root, u64 inode_id)
43 {
44 	delayed_node->root = root;
45 	delayed_node->inode_id = inode_id;
46 	refcount_set(&delayed_node->refs, 0);
47 	delayed_node->ins_root = RB_ROOT_CACHED;
48 	delayed_node->del_root = RB_ROOT_CACHED;
49 	mutex_init(&delayed_node->mutex);
50 	INIT_LIST_HEAD(&delayed_node->n_list);
51 	INIT_LIST_HEAD(&delayed_node->p_list);
52 }
53 
54 static inline int btrfs_is_continuous_delayed_item(
55 					struct btrfs_delayed_item *item1,
56 					struct btrfs_delayed_item *item2)
57 {
58 	if (item1->key.type == BTRFS_DIR_INDEX_KEY &&
59 	    item1->key.objectid == item2->key.objectid &&
60 	    item1->key.type == item2->key.type &&
61 	    item1->key.offset + 1 == item2->key.offset)
62 		return 1;
63 	return 0;
64 }
65 
66 static struct btrfs_delayed_node *btrfs_get_delayed_node(
67 		struct btrfs_inode *btrfs_inode)
68 {
69 	struct btrfs_root *root = btrfs_inode->root;
70 	u64 ino = btrfs_ino(btrfs_inode);
71 	struct btrfs_delayed_node *node;
72 
73 	node = READ_ONCE(btrfs_inode->delayed_node);
74 	if (node) {
75 		refcount_inc(&node->refs);
76 		return node;
77 	}
78 
79 	spin_lock(&root->inode_lock);
80 	node = radix_tree_lookup(&root->delayed_nodes_tree, ino);
81 
82 	if (node) {
83 		if (btrfs_inode->delayed_node) {
84 			refcount_inc(&node->refs);	/* can be accessed */
85 			BUG_ON(btrfs_inode->delayed_node != node);
86 			spin_unlock(&root->inode_lock);
87 			return node;
88 		}
89 
90 		/*
91 		 * It's possible that we're racing into the middle of removing
92 		 * this node from the radix tree.  In this case, the refcount
93 		 * was zero and it should never go back to one.  Just return
94 		 * NULL like it was never in the radix at all; our release
95 		 * function is in the process of removing it.
96 		 *
97 		 * Some implementations of refcount_inc refuse to bump the
98 		 * refcount once it has hit zero.  If we don't do this dance
99 		 * here, refcount_inc() may decide to just WARN_ONCE() instead
100 		 * of actually bumping the refcount.
101 		 *
102 		 * If this node is properly in the radix, we want to bump the
103 		 * refcount twice, once for the inode and once for this get
104 		 * operation.
105 		 */
106 		if (refcount_inc_not_zero(&node->refs)) {
107 			refcount_inc(&node->refs);
108 			btrfs_inode->delayed_node = node;
109 		} else {
110 			node = NULL;
111 		}
112 
113 		spin_unlock(&root->inode_lock);
114 		return node;
115 	}
116 	spin_unlock(&root->inode_lock);
117 
118 	return NULL;
119 }
120 
121 /* Will return either the node or PTR_ERR(-ENOMEM) */
122 static struct btrfs_delayed_node *btrfs_get_or_create_delayed_node(
123 		struct btrfs_inode *btrfs_inode)
124 {
125 	struct btrfs_delayed_node *node;
126 	struct btrfs_root *root = btrfs_inode->root;
127 	u64 ino = btrfs_ino(btrfs_inode);
128 	int ret;
129 
130 again:
131 	node = btrfs_get_delayed_node(btrfs_inode);
132 	if (node)
133 		return node;
134 
135 	node = kmem_cache_zalloc(delayed_node_cache, GFP_NOFS);
136 	if (!node)
137 		return ERR_PTR(-ENOMEM);
138 	btrfs_init_delayed_node(node, root, ino);
139 
140 	/* cached in the btrfs inode and can be accessed */
141 	refcount_set(&node->refs, 2);
142 
143 	ret = radix_tree_preload(GFP_NOFS);
144 	if (ret) {
145 		kmem_cache_free(delayed_node_cache, node);
146 		return ERR_PTR(ret);
147 	}
148 
149 	spin_lock(&root->inode_lock);
150 	ret = radix_tree_insert(&root->delayed_nodes_tree, ino, node);
151 	if (ret == -EEXIST) {
152 		spin_unlock(&root->inode_lock);
153 		kmem_cache_free(delayed_node_cache, node);
154 		radix_tree_preload_end();
155 		goto again;
156 	}
157 	btrfs_inode->delayed_node = node;
158 	spin_unlock(&root->inode_lock);
159 	radix_tree_preload_end();
160 
161 	return node;
162 }
163 
164 /*
165  * Call it when holding delayed_node->mutex
166  *
167  * If mod = 1, add this node into the prepared list.
168  */
169 static void btrfs_queue_delayed_node(struct btrfs_delayed_root *root,
170 				     struct btrfs_delayed_node *node,
171 				     int mod)
172 {
173 	spin_lock(&root->lock);
174 	if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
175 		if (!list_empty(&node->p_list))
176 			list_move_tail(&node->p_list, &root->prepare_list);
177 		else if (mod)
178 			list_add_tail(&node->p_list, &root->prepare_list);
179 	} else {
180 		list_add_tail(&node->n_list, &root->node_list);
181 		list_add_tail(&node->p_list, &root->prepare_list);
182 		refcount_inc(&node->refs);	/* inserted into list */
183 		root->nodes++;
184 		set_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags);
185 	}
186 	spin_unlock(&root->lock);
187 }
188 
189 /* Call it when holding delayed_node->mutex */
190 static void btrfs_dequeue_delayed_node(struct btrfs_delayed_root *root,
191 				       struct btrfs_delayed_node *node)
192 {
193 	spin_lock(&root->lock);
194 	if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
195 		root->nodes--;
196 		refcount_dec(&node->refs);	/* not in the list */
197 		list_del_init(&node->n_list);
198 		if (!list_empty(&node->p_list))
199 			list_del_init(&node->p_list);
200 		clear_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags);
201 	}
202 	spin_unlock(&root->lock);
203 }
204 
205 static struct btrfs_delayed_node *btrfs_first_delayed_node(
206 			struct btrfs_delayed_root *delayed_root)
207 {
208 	struct list_head *p;
209 	struct btrfs_delayed_node *node = NULL;
210 
211 	spin_lock(&delayed_root->lock);
212 	if (list_empty(&delayed_root->node_list))
213 		goto out;
214 
215 	p = delayed_root->node_list.next;
216 	node = list_entry(p, struct btrfs_delayed_node, n_list);
217 	refcount_inc(&node->refs);
218 out:
219 	spin_unlock(&delayed_root->lock);
220 
221 	return node;
222 }
223 
224 static struct btrfs_delayed_node *btrfs_next_delayed_node(
225 						struct btrfs_delayed_node *node)
226 {
227 	struct btrfs_delayed_root *delayed_root;
228 	struct list_head *p;
229 	struct btrfs_delayed_node *next = NULL;
230 
231 	delayed_root = node->root->fs_info->delayed_root;
232 	spin_lock(&delayed_root->lock);
233 	if (!test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
234 		/* not in the list */
235 		if (list_empty(&delayed_root->node_list))
236 			goto out;
237 		p = delayed_root->node_list.next;
238 	} else if (list_is_last(&node->n_list, &delayed_root->node_list))
239 		goto out;
240 	else
241 		p = node->n_list.next;
242 
243 	next = list_entry(p, struct btrfs_delayed_node, n_list);
244 	refcount_inc(&next->refs);
245 out:
246 	spin_unlock(&delayed_root->lock);
247 
248 	return next;
249 }
250 
251 static void __btrfs_release_delayed_node(
252 				struct btrfs_delayed_node *delayed_node,
253 				int mod)
254 {
255 	struct btrfs_delayed_root *delayed_root;
256 
257 	if (!delayed_node)
258 		return;
259 
260 	delayed_root = delayed_node->root->fs_info->delayed_root;
261 
262 	mutex_lock(&delayed_node->mutex);
263 	if (delayed_node->count)
264 		btrfs_queue_delayed_node(delayed_root, delayed_node, mod);
265 	else
266 		btrfs_dequeue_delayed_node(delayed_root, delayed_node);
267 	mutex_unlock(&delayed_node->mutex);
268 
269 	if (refcount_dec_and_test(&delayed_node->refs)) {
270 		struct btrfs_root *root = delayed_node->root;
271 
272 		spin_lock(&root->inode_lock);
273 		/*
274 		 * Once our refcount goes to zero, nobody is allowed to bump it
275 		 * back up.  We can delete it now.
276 		 */
277 		ASSERT(refcount_read(&delayed_node->refs) == 0);
278 		radix_tree_delete(&root->delayed_nodes_tree,
279 				  delayed_node->inode_id);
280 		spin_unlock(&root->inode_lock);
281 		kmem_cache_free(delayed_node_cache, delayed_node);
282 	}
283 }
284 
285 static inline void btrfs_release_delayed_node(struct btrfs_delayed_node *node)
286 {
287 	__btrfs_release_delayed_node(node, 0);
288 }
289 
290 static struct btrfs_delayed_node *btrfs_first_prepared_delayed_node(
291 					struct btrfs_delayed_root *delayed_root)
292 {
293 	struct list_head *p;
294 	struct btrfs_delayed_node *node = NULL;
295 
296 	spin_lock(&delayed_root->lock);
297 	if (list_empty(&delayed_root->prepare_list))
298 		goto out;
299 
300 	p = delayed_root->prepare_list.next;
301 	list_del_init(p);
302 	node = list_entry(p, struct btrfs_delayed_node, p_list);
303 	refcount_inc(&node->refs);
304 out:
305 	spin_unlock(&delayed_root->lock);
306 
307 	return node;
308 }
309 
310 static inline void btrfs_release_prepared_delayed_node(
311 					struct btrfs_delayed_node *node)
312 {
313 	__btrfs_release_delayed_node(node, 1);
314 }
315 
316 static struct btrfs_delayed_item *btrfs_alloc_delayed_item(u32 data_len)
317 {
318 	struct btrfs_delayed_item *item;
319 	item = kmalloc(sizeof(*item) + data_len, GFP_NOFS);
320 	if (item) {
321 		item->data_len = data_len;
322 		item->ins_or_del = 0;
323 		item->bytes_reserved = 0;
324 		item->delayed_node = NULL;
325 		refcount_set(&item->refs, 1);
326 	}
327 	return item;
328 }
329 
330 /*
331  * __btrfs_lookup_delayed_item - look up the delayed item by key
332  * @delayed_node: pointer to the delayed node
333  * @key:	  the key to look up
334  * @prev:	  used to store the prev item if the right item isn't found
335  * @next:	  used to store the next item if the right item isn't found
336  *
337  * Note: if we don't find the right item, we will return the prev item and
338  * the next item.
339  */
340 static struct btrfs_delayed_item *__btrfs_lookup_delayed_item(
341 				struct rb_root *root,
342 				struct btrfs_key *key,
343 				struct btrfs_delayed_item **prev,
344 				struct btrfs_delayed_item **next)
345 {
346 	struct rb_node *node, *prev_node = NULL;
347 	struct btrfs_delayed_item *delayed_item = NULL;
348 	int ret = 0;
349 
350 	node = root->rb_node;
351 
352 	while (node) {
353 		delayed_item = rb_entry(node, struct btrfs_delayed_item,
354 					rb_node);
355 		prev_node = node;
356 		ret = btrfs_comp_cpu_keys(&delayed_item->key, key);
357 		if (ret < 0)
358 			node = node->rb_right;
359 		else if (ret > 0)
360 			node = node->rb_left;
361 		else
362 			return delayed_item;
363 	}
364 
365 	if (prev) {
366 		if (!prev_node)
367 			*prev = NULL;
368 		else if (ret < 0)
369 			*prev = delayed_item;
370 		else if ((node = rb_prev(prev_node)) != NULL) {
371 			*prev = rb_entry(node, struct btrfs_delayed_item,
372 					 rb_node);
373 		} else
374 			*prev = NULL;
375 	}
376 
377 	if (next) {
378 		if (!prev_node)
379 			*next = NULL;
380 		else if (ret > 0)
381 			*next = delayed_item;
382 		else if ((node = rb_next(prev_node)) != NULL) {
383 			*next = rb_entry(node, struct btrfs_delayed_item,
384 					 rb_node);
385 		} else
386 			*next = NULL;
387 	}
388 	return NULL;
389 }
390 
391 static struct btrfs_delayed_item *__btrfs_lookup_delayed_insertion_item(
392 					struct btrfs_delayed_node *delayed_node,
393 					struct btrfs_key *key)
394 {
395 	return __btrfs_lookup_delayed_item(&delayed_node->ins_root.rb_root, key,
396 					   NULL, NULL);
397 }
398 
399 static int __btrfs_add_delayed_item(struct btrfs_delayed_node *delayed_node,
400 				    struct btrfs_delayed_item *ins,
401 				    int action)
402 {
403 	struct rb_node **p, *node;
404 	struct rb_node *parent_node = NULL;
405 	struct rb_root_cached *root;
406 	struct btrfs_delayed_item *item;
407 	int cmp;
408 	bool leftmost = true;
409 
410 	if (action == BTRFS_DELAYED_INSERTION_ITEM)
411 		root = &delayed_node->ins_root;
412 	else if (action == BTRFS_DELAYED_DELETION_ITEM)
413 		root = &delayed_node->del_root;
414 	else
415 		BUG();
416 	p = &root->rb_root.rb_node;
417 	node = &ins->rb_node;
418 
419 	while (*p) {
420 		parent_node = *p;
421 		item = rb_entry(parent_node, struct btrfs_delayed_item,
422 				 rb_node);
423 
424 		cmp = btrfs_comp_cpu_keys(&item->key, &ins->key);
425 		if (cmp < 0) {
426 			p = &(*p)->rb_right;
427 			leftmost = false;
428 		} else if (cmp > 0) {
429 			p = &(*p)->rb_left;
430 		} else {
431 			return -EEXIST;
432 		}
433 	}
434 
435 	rb_link_node(node, parent_node, p);
436 	rb_insert_color_cached(node, root, leftmost);
437 	ins->delayed_node = delayed_node;
438 	ins->ins_or_del = action;
439 
440 	if (ins->key.type == BTRFS_DIR_INDEX_KEY &&
441 	    action == BTRFS_DELAYED_INSERTION_ITEM &&
442 	    ins->key.offset >= delayed_node->index_cnt)
443 			delayed_node->index_cnt = ins->key.offset + 1;
444 
445 	delayed_node->count++;
446 	atomic_inc(&delayed_node->root->fs_info->delayed_root->items);
447 	return 0;
448 }
449 
450 static int __btrfs_add_delayed_insertion_item(struct btrfs_delayed_node *node,
451 					      struct btrfs_delayed_item *item)
452 {
453 	return __btrfs_add_delayed_item(node, item,
454 					BTRFS_DELAYED_INSERTION_ITEM);
455 }
456 
457 static int __btrfs_add_delayed_deletion_item(struct btrfs_delayed_node *node,
458 					     struct btrfs_delayed_item *item)
459 {
460 	return __btrfs_add_delayed_item(node, item,
461 					BTRFS_DELAYED_DELETION_ITEM);
462 }
463 
464 static void finish_one_item(struct btrfs_delayed_root *delayed_root)
465 {
466 	int seq = atomic_inc_return(&delayed_root->items_seq);
467 
468 	/* atomic_dec_return implies a barrier */
469 	if ((atomic_dec_return(&delayed_root->items) <
470 	    BTRFS_DELAYED_BACKGROUND || seq % BTRFS_DELAYED_BATCH == 0))
471 		cond_wake_up_nomb(&delayed_root->wait);
472 }
473 
474 static void __btrfs_remove_delayed_item(struct btrfs_delayed_item *delayed_item)
475 {
476 	struct rb_root_cached *root;
477 	struct btrfs_delayed_root *delayed_root;
478 
479 	/* Not associated with any delayed_node */
480 	if (!delayed_item->delayed_node)
481 		return;
482 	delayed_root = delayed_item->delayed_node->root->fs_info->delayed_root;
483 
484 	BUG_ON(!delayed_root);
485 	BUG_ON(delayed_item->ins_or_del != BTRFS_DELAYED_DELETION_ITEM &&
486 	       delayed_item->ins_or_del != BTRFS_DELAYED_INSERTION_ITEM);
487 
488 	if (delayed_item->ins_or_del == BTRFS_DELAYED_INSERTION_ITEM)
489 		root = &delayed_item->delayed_node->ins_root;
490 	else
491 		root = &delayed_item->delayed_node->del_root;
492 
493 	rb_erase_cached(&delayed_item->rb_node, root);
494 	delayed_item->delayed_node->count--;
495 
496 	finish_one_item(delayed_root);
497 }
498 
499 static void btrfs_release_delayed_item(struct btrfs_delayed_item *item)
500 {
501 	if (item) {
502 		__btrfs_remove_delayed_item(item);
503 		if (refcount_dec_and_test(&item->refs))
504 			kfree(item);
505 	}
506 }
507 
508 static struct btrfs_delayed_item *__btrfs_first_delayed_insertion_item(
509 					struct btrfs_delayed_node *delayed_node)
510 {
511 	struct rb_node *p;
512 	struct btrfs_delayed_item *item = NULL;
513 
514 	p = rb_first_cached(&delayed_node->ins_root);
515 	if (p)
516 		item = rb_entry(p, struct btrfs_delayed_item, rb_node);
517 
518 	return item;
519 }
520 
521 static struct btrfs_delayed_item *__btrfs_first_delayed_deletion_item(
522 					struct btrfs_delayed_node *delayed_node)
523 {
524 	struct rb_node *p;
525 	struct btrfs_delayed_item *item = NULL;
526 
527 	p = rb_first_cached(&delayed_node->del_root);
528 	if (p)
529 		item = rb_entry(p, struct btrfs_delayed_item, rb_node);
530 
531 	return item;
532 }
533 
534 static struct btrfs_delayed_item *__btrfs_next_delayed_item(
535 						struct btrfs_delayed_item *item)
536 {
537 	struct rb_node *p;
538 	struct btrfs_delayed_item *next = NULL;
539 
540 	p = rb_next(&item->rb_node);
541 	if (p)
542 		next = rb_entry(p, struct btrfs_delayed_item, rb_node);
543 
544 	return next;
545 }
546 
547 static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle *trans,
548 					       struct btrfs_root *root,
549 					       struct btrfs_delayed_item *item)
550 {
551 	struct btrfs_block_rsv *src_rsv;
552 	struct btrfs_block_rsv *dst_rsv;
553 	struct btrfs_fs_info *fs_info = root->fs_info;
554 	u64 num_bytes;
555 	int ret;
556 
557 	if (!trans->bytes_reserved)
558 		return 0;
559 
560 	src_rsv = trans->block_rsv;
561 	dst_rsv = &fs_info->delayed_block_rsv;
562 
563 	num_bytes = btrfs_calc_insert_metadata_size(fs_info, 1);
564 
565 	/*
566 	 * Here we migrate space rsv from transaction rsv, since have already
567 	 * reserved space when starting a transaction.  So no need to reserve
568 	 * qgroup space here.
569 	 */
570 	ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, true);
571 	if (!ret) {
572 		trace_btrfs_space_reservation(fs_info, "delayed_item",
573 					      item->key.objectid,
574 					      num_bytes, 1);
575 		item->bytes_reserved = num_bytes;
576 	}
577 
578 	return ret;
579 }
580 
581 static void btrfs_delayed_item_release_metadata(struct btrfs_root *root,
582 						struct btrfs_delayed_item *item)
583 {
584 	struct btrfs_block_rsv *rsv;
585 	struct btrfs_fs_info *fs_info = root->fs_info;
586 
587 	if (!item->bytes_reserved)
588 		return;
589 
590 	rsv = &fs_info->delayed_block_rsv;
591 	/*
592 	 * Check btrfs_delayed_item_reserve_metadata() to see why we don't need
593 	 * to release/reserve qgroup space.
594 	 */
595 	trace_btrfs_space_reservation(fs_info, "delayed_item",
596 				      item->key.objectid, item->bytes_reserved,
597 				      0);
598 	btrfs_block_rsv_release(fs_info, rsv, item->bytes_reserved, NULL);
599 }
600 
601 static int btrfs_delayed_inode_reserve_metadata(
602 					struct btrfs_trans_handle *trans,
603 					struct btrfs_root *root,
604 					struct btrfs_delayed_node *node)
605 {
606 	struct btrfs_fs_info *fs_info = root->fs_info;
607 	struct btrfs_block_rsv *src_rsv;
608 	struct btrfs_block_rsv *dst_rsv;
609 	u64 num_bytes;
610 	int ret;
611 
612 	src_rsv = trans->block_rsv;
613 	dst_rsv = &fs_info->delayed_block_rsv;
614 
615 	num_bytes = btrfs_calc_metadata_size(fs_info, 1);
616 
617 	/*
618 	 * btrfs_dirty_inode will update the inode under btrfs_join_transaction
619 	 * which doesn't reserve space for speed.  This is a problem since we
620 	 * still need to reserve space for this update, so try to reserve the
621 	 * space.
622 	 *
623 	 * Now if src_rsv == delalloc_block_rsv we'll let it just steal since
624 	 * we always reserve enough to update the inode item.
625 	 */
626 	if (!src_rsv || (!trans->bytes_reserved &&
627 			 src_rsv->type != BTRFS_BLOCK_RSV_DELALLOC)) {
628 		ret = btrfs_qgroup_reserve_meta(root, num_bytes,
629 					  BTRFS_QGROUP_RSV_META_PREALLOC, true);
630 		if (ret < 0)
631 			return ret;
632 		ret = btrfs_block_rsv_add(root, dst_rsv, num_bytes,
633 					  BTRFS_RESERVE_NO_FLUSH);
634 		/* NO_FLUSH could only fail with -ENOSPC */
635 		ASSERT(ret == 0 || ret == -ENOSPC);
636 		if (ret)
637 			btrfs_qgroup_free_meta_prealloc(root, num_bytes);
638 	} else {
639 		ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, true);
640 	}
641 
642 	if (!ret) {
643 		trace_btrfs_space_reservation(fs_info, "delayed_inode",
644 					      node->inode_id, num_bytes, 1);
645 		node->bytes_reserved = num_bytes;
646 	}
647 
648 	return ret;
649 }
650 
651 static void btrfs_delayed_inode_release_metadata(struct btrfs_fs_info *fs_info,
652 						struct btrfs_delayed_node *node,
653 						bool qgroup_free)
654 {
655 	struct btrfs_block_rsv *rsv;
656 
657 	if (!node->bytes_reserved)
658 		return;
659 
660 	rsv = &fs_info->delayed_block_rsv;
661 	trace_btrfs_space_reservation(fs_info, "delayed_inode",
662 				      node->inode_id, node->bytes_reserved, 0);
663 	btrfs_block_rsv_release(fs_info, rsv, node->bytes_reserved, NULL);
664 	if (qgroup_free)
665 		btrfs_qgroup_free_meta_prealloc(node->root,
666 				node->bytes_reserved);
667 	else
668 		btrfs_qgroup_convert_reserved_meta(node->root,
669 				node->bytes_reserved);
670 	node->bytes_reserved = 0;
671 }
672 
673 /*
674  * Insert a single delayed item or a batch of delayed items that have consecutive
675  * keys if they exist.
676  */
677 static int btrfs_insert_delayed_item(struct btrfs_trans_handle *trans,
678 				     struct btrfs_root *root,
679 				     struct btrfs_path *path,
680 				     struct btrfs_delayed_item *first_item)
681 {
682 	LIST_HEAD(item_list);
683 	struct btrfs_delayed_item *curr;
684 	struct btrfs_delayed_item *next;
685 	const int max_size = BTRFS_LEAF_DATA_SIZE(root->fs_info);
686 	struct btrfs_item_batch batch;
687 	int total_size;
688 	char *ins_data = NULL;
689 	int ret;
690 
691 	list_add_tail(&first_item->tree_list, &item_list);
692 	batch.total_data_size = first_item->data_len;
693 	batch.nr = 1;
694 	total_size = first_item->data_len + sizeof(struct btrfs_item);
695 	curr = first_item;
696 
697 	while (true) {
698 		int next_size;
699 
700 		next = __btrfs_next_delayed_item(curr);
701 		if (!next || !btrfs_is_continuous_delayed_item(curr, next))
702 			break;
703 
704 		next_size = next->data_len + sizeof(struct btrfs_item);
705 		if (total_size + next_size > max_size)
706 			break;
707 
708 		list_add_tail(&next->tree_list, &item_list);
709 		batch.nr++;
710 		total_size += next_size;
711 		batch.total_data_size += next->data_len;
712 		curr = next;
713 	}
714 
715 	if (batch.nr == 1) {
716 		batch.keys = &first_item->key;
717 		batch.data_sizes = &first_item->data_len;
718 	} else {
719 		struct btrfs_key *ins_keys;
720 		u32 *ins_sizes;
721 		int i = 0;
722 
723 		ins_data = kmalloc(batch.nr * sizeof(u32) +
724 				   batch.nr * sizeof(struct btrfs_key), GFP_NOFS);
725 		if (!ins_data) {
726 			ret = -ENOMEM;
727 			goto out;
728 		}
729 		ins_sizes = (u32 *)ins_data;
730 		ins_keys = (struct btrfs_key *)(ins_data + batch.nr * sizeof(u32));
731 		batch.keys = ins_keys;
732 		batch.data_sizes = ins_sizes;
733 		list_for_each_entry(curr, &item_list, tree_list) {
734 			ins_keys[i] = curr->key;
735 			ins_sizes[i] = curr->data_len;
736 			i++;
737 		}
738 	}
739 
740 	ret = btrfs_insert_empty_items(trans, root, path, &batch);
741 	if (ret)
742 		goto out;
743 
744 	list_for_each_entry(curr, &item_list, tree_list) {
745 		char *data_ptr;
746 
747 		data_ptr = btrfs_item_ptr(path->nodes[0], path->slots[0], char);
748 		write_extent_buffer(path->nodes[0], &curr->data,
749 				    (unsigned long)data_ptr, curr->data_len);
750 		path->slots[0]++;
751 	}
752 
753 	/*
754 	 * Now release our path before releasing the delayed items and their
755 	 * metadata reservations, so that we don't block other tasks for more
756 	 * time than needed.
757 	 */
758 	btrfs_release_path(path);
759 
760 	list_for_each_entry_safe(curr, next, &item_list, tree_list) {
761 		list_del(&curr->tree_list);
762 		btrfs_delayed_item_release_metadata(root, curr);
763 		btrfs_release_delayed_item(curr);
764 	}
765 out:
766 	kfree(ins_data);
767 	return ret;
768 }
769 
770 static int btrfs_insert_delayed_items(struct btrfs_trans_handle *trans,
771 				      struct btrfs_path *path,
772 				      struct btrfs_root *root,
773 				      struct btrfs_delayed_node *node)
774 {
775 	int ret = 0;
776 
777 	while (ret == 0) {
778 		struct btrfs_delayed_item *curr;
779 
780 		mutex_lock(&node->mutex);
781 		curr = __btrfs_first_delayed_insertion_item(node);
782 		if (!curr) {
783 			mutex_unlock(&node->mutex);
784 			break;
785 		}
786 		ret = btrfs_insert_delayed_item(trans, root, path, curr);
787 		mutex_unlock(&node->mutex);
788 	}
789 
790 	return ret;
791 }
792 
793 static int btrfs_batch_delete_items(struct btrfs_trans_handle *trans,
794 				    struct btrfs_root *root,
795 				    struct btrfs_path *path,
796 				    struct btrfs_delayed_item *item)
797 {
798 	struct btrfs_delayed_item *curr, *next;
799 	struct extent_buffer *leaf;
800 	struct btrfs_key key;
801 	struct list_head head;
802 	int nitems, i, last_item;
803 	int ret = 0;
804 
805 	BUG_ON(!path->nodes[0]);
806 
807 	leaf = path->nodes[0];
808 
809 	i = path->slots[0];
810 	last_item = btrfs_header_nritems(leaf) - 1;
811 	if (i > last_item)
812 		return -ENOENT;	/* FIXME: Is errno suitable? */
813 
814 	next = item;
815 	INIT_LIST_HEAD(&head);
816 	btrfs_item_key_to_cpu(leaf, &key, i);
817 	nitems = 0;
818 	/*
819 	 * count the number of the dir index items that we can delete in batch
820 	 */
821 	while (btrfs_comp_cpu_keys(&next->key, &key) == 0) {
822 		list_add_tail(&next->tree_list, &head);
823 		nitems++;
824 
825 		curr = next;
826 		next = __btrfs_next_delayed_item(curr);
827 		if (!next)
828 			break;
829 
830 		if (!btrfs_is_continuous_delayed_item(curr, next))
831 			break;
832 
833 		i++;
834 		if (i > last_item)
835 			break;
836 		btrfs_item_key_to_cpu(leaf, &key, i);
837 	}
838 
839 	if (!nitems)
840 		return 0;
841 
842 	ret = btrfs_del_items(trans, root, path, path->slots[0], nitems);
843 	if (ret)
844 		goto out;
845 
846 	list_for_each_entry_safe(curr, next, &head, tree_list) {
847 		btrfs_delayed_item_release_metadata(root, curr);
848 		list_del(&curr->tree_list);
849 		btrfs_release_delayed_item(curr);
850 	}
851 
852 out:
853 	return ret;
854 }
855 
856 static int btrfs_delete_delayed_items(struct btrfs_trans_handle *trans,
857 				      struct btrfs_path *path,
858 				      struct btrfs_root *root,
859 				      struct btrfs_delayed_node *node)
860 {
861 	struct btrfs_delayed_item *curr, *prev;
862 	int ret = 0;
863 
864 do_again:
865 	mutex_lock(&node->mutex);
866 	curr = __btrfs_first_delayed_deletion_item(node);
867 	if (!curr)
868 		goto delete_fail;
869 
870 	ret = btrfs_search_slot(trans, root, &curr->key, path, -1, 1);
871 	if (ret < 0)
872 		goto delete_fail;
873 	else if (ret > 0) {
874 		/*
875 		 * can't find the item which the node points to, so this node
876 		 * is invalid, just drop it.
877 		 */
878 		prev = curr;
879 		curr = __btrfs_next_delayed_item(prev);
880 		btrfs_release_delayed_item(prev);
881 		ret = 0;
882 		btrfs_release_path(path);
883 		if (curr) {
884 			mutex_unlock(&node->mutex);
885 			goto do_again;
886 		} else
887 			goto delete_fail;
888 	}
889 
890 	btrfs_batch_delete_items(trans, root, path, curr);
891 	btrfs_release_path(path);
892 	mutex_unlock(&node->mutex);
893 	goto do_again;
894 
895 delete_fail:
896 	btrfs_release_path(path);
897 	mutex_unlock(&node->mutex);
898 	return ret;
899 }
900 
901 static void btrfs_release_delayed_inode(struct btrfs_delayed_node *delayed_node)
902 {
903 	struct btrfs_delayed_root *delayed_root;
904 
905 	if (delayed_node &&
906 	    test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
907 		BUG_ON(!delayed_node->root);
908 		clear_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags);
909 		delayed_node->count--;
910 
911 		delayed_root = delayed_node->root->fs_info->delayed_root;
912 		finish_one_item(delayed_root);
913 	}
914 }
915 
916 static void btrfs_release_delayed_iref(struct btrfs_delayed_node *delayed_node)
917 {
918 
919 	if (test_and_clear_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags)) {
920 		struct btrfs_delayed_root *delayed_root;
921 
922 		ASSERT(delayed_node->root);
923 		delayed_node->count--;
924 
925 		delayed_root = delayed_node->root->fs_info->delayed_root;
926 		finish_one_item(delayed_root);
927 	}
928 }
929 
930 static int __btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
931 					struct btrfs_root *root,
932 					struct btrfs_path *path,
933 					struct btrfs_delayed_node *node)
934 {
935 	struct btrfs_fs_info *fs_info = root->fs_info;
936 	struct btrfs_key key;
937 	struct btrfs_inode_item *inode_item;
938 	struct extent_buffer *leaf;
939 	int mod;
940 	int ret;
941 
942 	key.objectid = node->inode_id;
943 	key.type = BTRFS_INODE_ITEM_KEY;
944 	key.offset = 0;
945 
946 	if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags))
947 		mod = -1;
948 	else
949 		mod = 1;
950 
951 	ret = btrfs_lookup_inode(trans, root, path, &key, mod);
952 	if (ret > 0)
953 		ret = -ENOENT;
954 	if (ret < 0)
955 		goto out;
956 
957 	leaf = path->nodes[0];
958 	inode_item = btrfs_item_ptr(leaf, path->slots[0],
959 				    struct btrfs_inode_item);
960 	write_extent_buffer(leaf, &node->inode_item, (unsigned long)inode_item,
961 			    sizeof(struct btrfs_inode_item));
962 	btrfs_mark_buffer_dirty(leaf);
963 
964 	if (!test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags))
965 		goto out;
966 
967 	path->slots[0]++;
968 	if (path->slots[0] >= btrfs_header_nritems(leaf))
969 		goto search;
970 again:
971 	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
972 	if (key.objectid != node->inode_id)
973 		goto out;
974 
975 	if (key.type != BTRFS_INODE_REF_KEY &&
976 	    key.type != BTRFS_INODE_EXTREF_KEY)
977 		goto out;
978 
979 	/*
980 	 * Delayed iref deletion is for the inode who has only one link,
981 	 * so there is only one iref. The case that several irefs are
982 	 * in the same item doesn't exist.
983 	 */
984 	btrfs_del_item(trans, root, path);
985 out:
986 	btrfs_release_delayed_iref(node);
987 	btrfs_release_path(path);
988 err_out:
989 	btrfs_delayed_inode_release_metadata(fs_info, node, (ret < 0));
990 	btrfs_release_delayed_inode(node);
991 
992 	/*
993 	 * If we fail to update the delayed inode we need to abort the
994 	 * transaction, because we could leave the inode with the improper
995 	 * counts behind.
996 	 */
997 	if (ret && ret != -ENOENT)
998 		btrfs_abort_transaction(trans, ret);
999 
1000 	return ret;
1001 
1002 search:
1003 	btrfs_release_path(path);
1004 
1005 	key.type = BTRFS_INODE_EXTREF_KEY;
1006 	key.offset = -1;
1007 
1008 	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1009 	if (ret < 0)
1010 		goto err_out;
1011 	ASSERT(ret);
1012 
1013 	ret = 0;
1014 	leaf = path->nodes[0];
1015 	path->slots[0]--;
1016 	goto again;
1017 }
1018 
1019 static inline int btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
1020 					     struct btrfs_root *root,
1021 					     struct btrfs_path *path,
1022 					     struct btrfs_delayed_node *node)
1023 {
1024 	int ret;
1025 
1026 	mutex_lock(&node->mutex);
1027 	if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &node->flags)) {
1028 		mutex_unlock(&node->mutex);
1029 		return 0;
1030 	}
1031 
1032 	ret = __btrfs_update_delayed_inode(trans, root, path, node);
1033 	mutex_unlock(&node->mutex);
1034 	return ret;
1035 }
1036 
1037 static inline int
1038 __btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
1039 				   struct btrfs_path *path,
1040 				   struct btrfs_delayed_node *node)
1041 {
1042 	int ret;
1043 
1044 	ret = btrfs_insert_delayed_items(trans, path, node->root, node);
1045 	if (ret)
1046 		return ret;
1047 
1048 	ret = btrfs_delete_delayed_items(trans, path, node->root, node);
1049 	if (ret)
1050 		return ret;
1051 
1052 	ret = btrfs_update_delayed_inode(trans, node->root, path, node);
1053 	return ret;
1054 }
1055 
1056 /*
1057  * Called when committing the transaction.
1058  * Returns 0 on success.
1059  * Returns < 0 on error and returns with an aborted transaction with any
1060  * outstanding delayed items cleaned up.
1061  */
1062 static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans, int nr)
1063 {
1064 	struct btrfs_fs_info *fs_info = trans->fs_info;
1065 	struct btrfs_delayed_root *delayed_root;
1066 	struct btrfs_delayed_node *curr_node, *prev_node;
1067 	struct btrfs_path *path;
1068 	struct btrfs_block_rsv *block_rsv;
1069 	int ret = 0;
1070 	bool count = (nr > 0);
1071 
1072 	if (TRANS_ABORTED(trans))
1073 		return -EIO;
1074 
1075 	path = btrfs_alloc_path();
1076 	if (!path)
1077 		return -ENOMEM;
1078 
1079 	block_rsv = trans->block_rsv;
1080 	trans->block_rsv = &fs_info->delayed_block_rsv;
1081 
1082 	delayed_root = fs_info->delayed_root;
1083 
1084 	curr_node = btrfs_first_delayed_node(delayed_root);
1085 	while (curr_node && (!count || nr--)) {
1086 		ret = __btrfs_commit_inode_delayed_items(trans, path,
1087 							 curr_node);
1088 		if (ret) {
1089 			btrfs_release_delayed_node(curr_node);
1090 			curr_node = NULL;
1091 			btrfs_abort_transaction(trans, ret);
1092 			break;
1093 		}
1094 
1095 		prev_node = curr_node;
1096 		curr_node = btrfs_next_delayed_node(curr_node);
1097 		btrfs_release_delayed_node(prev_node);
1098 	}
1099 
1100 	if (curr_node)
1101 		btrfs_release_delayed_node(curr_node);
1102 	btrfs_free_path(path);
1103 	trans->block_rsv = block_rsv;
1104 
1105 	return ret;
1106 }
1107 
1108 int btrfs_run_delayed_items(struct btrfs_trans_handle *trans)
1109 {
1110 	return __btrfs_run_delayed_items(trans, -1);
1111 }
1112 
1113 int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans, int nr)
1114 {
1115 	return __btrfs_run_delayed_items(trans, nr);
1116 }
1117 
1118 int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
1119 				     struct btrfs_inode *inode)
1120 {
1121 	struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
1122 	struct btrfs_path *path;
1123 	struct btrfs_block_rsv *block_rsv;
1124 	int ret;
1125 
1126 	if (!delayed_node)
1127 		return 0;
1128 
1129 	mutex_lock(&delayed_node->mutex);
1130 	if (!delayed_node->count) {
1131 		mutex_unlock(&delayed_node->mutex);
1132 		btrfs_release_delayed_node(delayed_node);
1133 		return 0;
1134 	}
1135 	mutex_unlock(&delayed_node->mutex);
1136 
1137 	path = btrfs_alloc_path();
1138 	if (!path) {
1139 		btrfs_release_delayed_node(delayed_node);
1140 		return -ENOMEM;
1141 	}
1142 
1143 	block_rsv = trans->block_rsv;
1144 	trans->block_rsv = &delayed_node->root->fs_info->delayed_block_rsv;
1145 
1146 	ret = __btrfs_commit_inode_delayed_items(trans, path, delayed_node);
1147 
1148 	btrfs_release_delayed_node(delayed_node);
1149 	btrfs_free_path(path);
1150 	trans->block_rsv = block_rsv;
1151 
1152 	return ret;
1153 }
1154 
1155 int btrfs_commit_inode_delayed_inode(struct btrfs_inode *inode)
1156 {
1157 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
1158 	struct btrfs_trans_handle *trans;
1159 	struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
1160 	struct btrfs_path *path;
1161 	struct btrfs_block_rsv *block_rsv;
1162 	int ret;
1163 
1164 	if (!delayed_node)
1165 		return 0;
1166 
1167 	mutex_lock(&delayed_node->mutex);
1168 	if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
1169 		mutex_unlock(&delayed_node->mutex);
1170 		btrfs_release_delayed_node(delayed_node);
1171 		return 0;
1172 	}
1173 	mutex_unlock(&delayed_node->mutex);
1174 
1175 	trans = btrfs_join_transaction(delayed_node->root);
1176 	if (IS_ERR(trans)) {
1177 		ret = PTR_ERR(trans);
1178 		goto out;
1179 	}
1180 
1181 	path = btrfs_alloc_path();
1182 	if (!path) {
1183 		ret = -ENOMEM;
1184 		goto trans_out;
1185 	}
1186 
1187 	block_rsv = trans->block_rsv;
1188 	trans->block_rsv = &fs_info->delayed_block_rsv;
1189 
1190 	mutex_lock(&delayed_node->mutex);
1191 	if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags))
1192 		ret = __btrfs_update_delayed_inode(trans, delayed_node->root,
1193 						   path, delayed_node);
1194 	else
1195 		ret = 0;
1196 	mutex_unlock(&delayed_node->mutex);
1197 
1198 	btrfs_free_path(path);
1199 	trans->block_rsv = block_rsv;
1200 trans_out:
1201 	btrfs_end_transaction(trans);
1202 	btrfs_btree_balance_dirty(fs_info);
1203 out:
1204 	btrfs_release_delayed_node(delayed_node);
1205 
1206 	return ret;
1207 }
1208 
1209 void btrfs_remove_delayed_node(struct btrfs_inode *inode)
1210 {
1211 	struct btrfs_delayed_node *delayed_node;
1212 
1213 	delayed_node = READ_ONCE(inode->delayed_node);
1214 	if (!delayed_node)
1215 		return;
1216 
1217 	inode->delayed_node = NULL;
1218 	btrfs_release_delayed_node(delayed_node);
1219 }
1220 
1221 struct btrfs_async_delayed_work {
1222 	struct btrfs_delayed_root *delayed_root;
1223 	int nr;
1224 	struct btrfs_work work;
1225 };
1226 
1227 static void btrfs_async_run_delayed_root(struct btrfs_work *work)
1228 {
1229 	struct btrfs_async_delayed_work *async_work;
1230 	struct btrfs_delayed_root *delayed_root;
1231 	struct btrfs_trans_handle *trans;
1232 	struct btrfs_path *path;
1233 	struct btrfs_delayed_node *delayed_node = NULL;
1234 	struct btrfs_root *root;
1235 	struct btrfs_block_rsv *block_rsv;
1236 	int total_done = 0;
1237 
1238 	async_work = container_of(work, struct btrfs_async_delayed_work, work);
1239 	delayed_root = async_work->delayed_root;
1240 
1241 	path = btrfs_alloc_path();
1242 	if (!path)
1243 		goto out;
1244 
1245 	do {
1246 		if (atomic_read(&delayed_root->items) <
1247 		    BTRFS_DELAYED_BACKGROUND / 2)
1248 			break;
1249 
1250 		delayed_node = btrfs_first_prepared_delayed_node(delayed_root);
1251 		if (!delayed_node)
1252 			break;
1253 
1254 		root = delayed_node->root;
1255 
1256 		trans = btrfs_join_transaction(root);
1257 		if (IS_ERR(trans)) {
1258 			btrfs_release_path(path);
1259 			btrfs_release_prepared_delayed_node(delayed_node);
1260 			total_done++;
1261 			continue;
1262 		}
1263 
1264 		block_rsv = trans->block_rsv;
1265 		trans->block_rsv = &root->fs_info->delayed_block_rsv;
1266 
1267 		__btrfs_commit_inode_delayed_items(trans, path, delayed_node);
1268 
1269 		trans->block_rsv = block_rsv;
1270 		btrfs_end_transaction(trans);
1271 		btrfs_btree_balance_dirty_nodelay(root->fs_info);
1272 
1273 		btrfs_release_path(path);
1274 		btrfs_release_prepared_delayed_node(delayed_node);
1275 		total_done++;
1276 
1277 	} while ((async_work->nr == 0 && total_done < BTRFS_DELAYED_WRITEBACK)
1278 		 || total_done < async_work->nr);
1279 
1280 	btrfs_free_path(path);
1281 out:
1282 	wake_up(&delayed_root->wait);
1283 	kfree(async_work);
1284 }
1285 
1286 
1287 static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root,
1288 				     struct btrfs_fs_info *fs_info, int nr)
1289 {
1290 	struct btrfs_async_delayed_work *async_work;
1291 
1292 	async_work = kmalloc(sizeof(*async_work), GFP_NOFS);
1293 	if (!async_work)
1294 		return -ENOMEM;
1295 
1296 	async_work->delayed_root = delayed_root;
1297 	btrfs_init_work(&async_work->work, btrfs_async_run_delayed_root, NULL,
1298 			NULL);
1299 	async_work->nr = nr;
1300 
1301 	btrfs_queue_work(fs_info->delayed_workers, &async_work->work);
1302 	return 0;
1303 }
1304 
1305 void btrfs_assert_delayed_root_empty(struct btrfs_fs_info *fs_info)
1306 {
1307 	WARN_ON(btrfs_first_delayed_node(fs_info->delayed_root));
1308 }
1309 
1310 static int could_end_wait(struct btrfs_delayed_root *delayed_root, int seq)
1311 {
1312 	int val = atomic_read(&delayed_root->items_seq);
1313 
1314 	if (val < seq || val >= seq + BTRFS_DELAYED_BATCH)
1315 		return 1;
1316 
1317 	if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
1318 		return 1;
1319 
1320 	return 0;
1321 }
1322 
1323 void btrfs_balance_delayed_items(struct btrfs_fs_info *fs_info)
1324 {
1325 	struct btrfs_delayed_root *delayed_root = fs_info->delayed_root;
1326 
1327 	if ((atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND) ||
1328 		btrfs_workqueue_normal_congested(fs_info->delayed_workers))
1329 		return;
1330 
1331 	if (atomic_read(&delayed_root->items) >= BTRFS_DELAYED_WRITEBACK) {
1332 		int seq;
1333 		int ret;
1334 
1335 		seq = atomic_read(&delayed_root->items_seq);
1336 
1337 		ret = btrfs_wq_run_delayed_node(delayed_root, fs_info, 0);
1338 		if (ret)
1339 			return;
1340 
1341 		wait_event_interruptible(delayed_root->wait,
1342 					 could_end_wait(delayed_root, seq));
1343 		return;
1344 	}
1345 
1346 	btrfs_wq_run_delayed_node(delayed_root, fs_info, BTRFS_DELAYED_BATCH);
1347 }
1348 
1349 /* Will return 0 or -ENOMEM */
1350 int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
1351 				   const char *name, int name_len,
1352 				   struct btrfs_inode *dir,
1353 				   struct btrfs_disk_key *disk_key, u8 type,
1354 				   u64 index)
1355 {
1356 	struct btrfs_delayed_node *delayed_node;
1357 	struct btrfs_delayed_item *delayed_item;
1358 	struct btrfs_dir_item *dir_item;
1359 	int ret;
1360 
1361 	delayed_node = btrfs_get_or_create_delayed_node(dir);
1362 	if (IS_ERR(delayed_node))
1363 		return PTR_ERR(delayed_node);
1364 
1365 	delayed_item = btrfs_alloc_delayed_item(sizeof(*dir_item) + name_len);
1366 	if (!delayed_item) {
1367 		ret = -ENOMEM;
1368 		goto release_node;
1369 	}
1370 
1371 	delayed_item->key.objectid = btrfs_ino(dir);
1372 	delayed_item->key.type = BTRFS_DIR_INDEX_KEY;
1373 	delayed_item->key.offset = index;
1374 
1375 	dir_item = (struct btrfs_dir_item *)delayed_item->data;
1376 	dir_item->location = *disk_key;
1377 	btrfs_set_stack_dir_transid(dir_item, trans->transid);
1378 	btrfs_set_stack_dir_data_len(dir_item, 0);
1379 	btrfs_set_stack_dir_name_len(dir_item, name_len);
1380 	btrfs_set_stack_dir_type(dir_item, type);
1381 	memcpy((char *)(dir_item + 1), name, name_len);
1382 
1383 	ret = btrfs_delayed_item_reserve_metadata(trans, dir->root, delayed_item);
1384 	/*
1385 	 * we have reserved enough space when we start a new transaction,
1386 	 * so reserving metadata failure is impossible
1387 	 */
1388 	BUG_ON(ret);
1389 
1390 	mutex_lock(&delayed_node->mutex);
1391 	ret = __btrfs_add_delayed_insertion_item(delayed_node, delayed_item);
1392 	if (unlikely(ret)) {
1393 		btrfs_err(trans->fs_info,
1394 			  "err add delayed dir index item(name: %.*s) into the insertion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)",
1395 			  name_len, name, delayed_node->root->root_key.objectid,
1396 			  delayed_node->inode_id, ret);
1397 		BUG();
1398 	}
1399 	mutex_unlock(&delayed_node->mutex);
1400 
1401 release_node:
1402 	btrfs_release_delayed_node(delayed_node);
1403 	return ret;
1404 }
1405 
1406 static int btrfs_delete_delayed_insertion_item(struct btrfs_fs_info *fs_info,
1407 					       struct btrfs_delayed_node *node,
1408 					       struct btrfs_key *key)
1409 {
1410 	struct btrfs_delayed_item *item;
1411 
1412 	mutex_lock(&node->mutex);
1413 	item = __btrfs_lookup_delayed_insertion_item(node, key);
1414 	if (!item) {
1415 		mutex_unlock(&node->mutex);
1416 		return 1;
1417 	}
1418 
1419 	btrfs_delayed_item_release_metadata(node->root, item);
1420 	btrfs_release_delayed_item(item);
1421 	mutex_unlock(&node->mutex);
1422 	return 0;
1423 }
1424 
1425 int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
1426 				   struct btrfs_inode *dir, u64 index)
1427 {
1428 	struct btrfs_delayed_node *node;
1429 	struct btrfs_delayed_item *item;
1430 	struct btrfs_key item_key;
1431 	int ret;
1432 
1433 	node = btrfs_get_or_create_delayed_node(dir);
1434 	if (IS_ERR(node))
1435 		return PTR_ERR(node);
1436 
1437 	item_key.objectid = btrfs_ino(dir);
1438 	item_key.type = BTRFS_DIR_INDEX_KEY;
1439 	item_key.offset = index;
1440 
1441 	ret = btrfs_delete_delayed_insertion_item(trans->fs_info, node,
1442 						  &item_key);
1443 	if (!ret)
1444 		goto end;
1445 
1446 	item = btrfs_alloc_delayed_item(0);
1447 	if (!item) {
1448 		ret = -ENOMEM;
1449 		goto end;
1450 	}
1451 
1452 	item->key = item_key;
1453 
1454 	ret = btrfs_delayed_item_reserve_metadata(trans, dir->root, item);
1455 	/*
1456 	 * we have reserved enough space when we start a new transaction,
1457 	 * so reserving metadata failure is impossible.
1458 	 */
1459 	if (ret < 0) {
1460 		btrfs_err(trans->fs_info,
1461 "metadata reservation failed for delayed dir item deltiona, should have been reserved");
1462 		btrfs_release_delayed_item(item);
1463 		goto end;
1464 	}
1465 
1466 	mutex_lock(&node->mutex);
1467 	ret = __btrfs_add_delayed_deletion_item(node, item);
1468 	if (unlikely(ret)) {
1469 		btrfs_err(trans->fs_info,
1470 			  "err add delayed dir index item(index: %llu) into the deletion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)",
1471 			  index, node->root->root_key.objectid,
1472 			  node->inode_id, ret);
1473 		btrfs_delayed_item_release_metadata(dir->root, item);
1474 		btrfs_release_delayed_item(item);
1475 	}
1476 	mutex_unlock(&node->mutex);
1477 end:
1478 	btrfs_release_delayed_node(node);
1479 	return ret;
1480 }
1481 
1482 int btrfs_inode_delayed_dir_index_count(struct btrfs_inode *inode)
1483 {
1484 	struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
1485 
1486 	if (!delayed_node)
1487 		return -ENOENT;
1488 
1489 	/*
1490 	 * Since we have held i_mutex of this directory, it is impossible that
1491 	 * a new directory index is added into the delayed node and index_cnt
1492 	 * is updated now. So we needn't lock the delayed node.
1493 	 */
1494 	if (!delayed_node->index_cnt) {
1495 		btrfs_release_delayed_node(delayed_node);
1496 		return -EINVAL;
1497 	}
1498 
1499 	inode->index_cnt = delayed_node->index_cnt;
1500 	btrfs_release_delayed_node(delayed_node);
1501 	return 0;
1502 }
1503 
1504 bool btrfs_readdir_get_delayed_items(struct inode *inode,
1505 				     struct list_head *ins_list,
1506 				     struct list_head *del_list)
1507 {
1508 	struct btrfs_delayed_node *delayed_node;
1509 	struct btrfs_delayed_item *item;
1510 
1511 	delayed_node = btrfs_get_delayed_node(BTRFS_I(inode));
1512 	if (!delayed_node)
1513 		return false;
1514 
1515 	/*
1516 	 * We can only do one readdir with delayed items at a time because of
1517 	 * item->readdir_list.
1518 	 */
1519 	btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
1520 	btrfs_inode_lock(inode, 0);
1521 
1522 	mutex_lock(&delayed_node->mutex);
1523 	item = __btrfs_first_delayed_insertion_item(delayed_node);
1524 	while (item) {
1525 		refcount_inc(&item->refs);
1526 		list_add_tail(&item->readdir_list, ins_list);
1527 		item = __btrfs_next_delayed_item(item);
1528 	}
1529 
1530 	item = __btrfs_first_delayed_deletion_item(delayed_node);
1531 	while (item) {
1532 		refcount_inc(&item->refs);
1533 		list_add_tail(&item->readdir_list, del_list);
1534 		item = __btrfs_next_delayed_item(item);
1535 	}
1536 	mutex_unlock(&delayed_node->mutex);
1537 	/*
1538 	 * This delayed node is still cached in the btrfs inode, so refs
1539 	 * must be > 1 now, and we needn't check it is going to be freed
1540 	 * or not.
1541 	 *
1542 	 * Besides that, this function is used to read dir, we do not
1543 	 * insert/delete delayed items in this period. So we also needn't
1544 	 * requeue or dequeue this delayed node.
1545 	 */
1546 	refcount_dec(&delayed_node->refs);
1547 
1548 	return true;
1549 }
1550 
1551 void btrfs_readdir_put_delayed_items(struct inode *inode,
1552 				     struct list_head *ins_list,
1553 				     struct list_head *del_list)
1554 {
1555 	struct btrfs_delayed_item *curr, *next;
1556 
1557 	list_for_each_entry_safe(curr, next, ins_list, readdir_list) {
1558 		list_del(&curr->readdir_list);
1559 		if (refcount_dec_and_test(&curr->refs))
1560 			kfree(curr);
1561 	}
1562 
1563 	list_for_each_entry_safe(curr, next, del_list, readdir_list) {
1564 		list_del(&curr->readdir_list);
1565 		if (refcount_dec_and_test(&curr->refs))
1566 			kfree(curr);
1567 	}
1568 
1569 	/*
1570 	 * The VFS is going to do up_read(), so we need to downgrade back to a
1571 	 * read lock.
1572 	 */
1573 	downgrade_write(&inode->i_rwsem);
1574 }
1575 
1576 int btrfs_should_delete_dir_index(struct list_head *del_list,
1577 				  u64 index)
1578 {
1579 	struct btrfs_delayed_item *curr;
1580 	int ret = 0;
1581 
1582 	list_for_each_entry(curr, del_list, readdir_list) {
1583 		if (curr->key.offset > index)
1584 			break;
1585 		if (curr->key.offset == index) {
1586 			ret = 1;
1587 			break;
1588 		}
1589 	}
1590 	return ret;
1591 }
1592 
1593 /*
1594  * btrfs_readdir_delayed_dir_index - read dir info stored in the delayed tree
1595  *
1596  */
1597 int btrfs_readdir_delayed_dir_index(struct dir_context *ctx,
1598 				    struct list_head *ins_list)
1599 {
1600 	struct btrfs_dir_item *di;
1601 	struct btrfs_delayed_item *curr, *next;
1602 	struct btrfs_key location;
1603 	char *name;
1604 	int name_len;
1605 	int over = 0;
1606 	unsigned char d_type;
1607 
1608 	if (list_empty(ins_list))
1609 		return 0;
1610 
1611 	/*
1612 	 * Changing the data of the delayed item is impossible. So
1613 	 * we needn't lock them. And we have held i_mutex of the
1614 	 * directory, nobody can delete any directory indexes now.
1615 	 */
1616 	list_for_each_entry_safe(curr, next, ins_list, readdir_list) {
1617 		list_del(&curr->readdir_list);
1618 
1619 		if (curr->key.offset < ctx->pos) {
1620 			if (refcount_dec_and_test(&curr->refs))
1621 				kfree(curr);
1622 			continue;
1623 		}
1624 
1625 		ctx->pos = curr->key.offset;
1626 
1627 		di = (struct btrfs_dir_item *)curr->data;
1628 		name = (char *)(di + 1);
1629 		name_len = btrfs_stack_dir_name_len(di);
1630 
1631 		d_type = fs_ftype_to_dtype(di->type);
1632 		btrfs_disk_key_to_cpu(&location, &di->location);
1633 
1634 		over = !dir_emit(ctx, name, name_len,
1635 			       location.objectid, d_type);
1636 
1637 		if (refcount_dec_and_test(&curr->refs))
1638 			kfree(curr);
1639 
1640 		if (over)
1641 			return 1;
1642 		ctx->pos++;
1643 	}
1644 	return 0;
1645 }
1646 
1647 static void fill_stack_inode_item(struct btrfs_trans_handle *trans,
1648 				  struct btrfs_inode_item *inode_item,
1649 				  struct inode *inode)
1650 {
1651 	u64 flags;
1652 
1653 	btrfs_set_stack_inode_uid(inode_item, i_uid_read(inode));
1654 	btrfs_set_stack_inode_gid(inode_item, i_gid_read(inode));
1655 	btrfs_set_stack_inode_size(inode_item, BTRFS_I(inode)->disk_i_size);
1656 	btrfs_set_stack_inode_mode(inode_item, inode->i_mode);
1657 	btrfs_set_stack_inode_nlink(inode_item, inode->i_nlink);
1658 	btrfs_set_stack_inode_nbytes(inode_item, inode_get_bytes(inode));
1659 	btrfs_set_stack_inode_generation(inode_item,
1660 					 BTRFS_I(inode)->generation);
1661 	btrfs_set_stack_inode_sequence(inode_item,
1662 				       inode_peek_iversion(inode));
1663 	btrfs_set_stack_inode_transid(inode_item, trans->transid);
1664 	btrfs_set_stack_inode_rdev(inode_item, inode->i_rdev);
1665 	flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags,
1666 					  BTRFS_I(inode)->ro_flags);
1667 	btrfs_set_stack_inode_flags(inode_item, flags);
1668 	btrfs_set_stack_inode_block_group(inode_item, 0);
1669 
1670 	btrfs_set_stack_timespec_sec(&inode_item->atime,
1671 				     inode->i_atime.tv_sec);
1672 	btrfs_set_stack_timespec_nsec(&inode_item->atime,
1673 				      inode->i_atime.tv_nsec);
1674 
1675 	btrfs_set_stack_timespec_sec(&inode_item->mtime,
1676 				     inode->i_mtime.tv_sec);
1677 	btrfs_set_stack_timespec_nsec(&inode_item->mtime,
1678 				      inode->i_mtime.tv_nsec);
1679 
1680 	btrfs_set_stack_timespec_sec(&inode_item->ctime,
1681 				     inode->i_ctime.tv_sec);
1682 	btrfs_set_stack_timespec_nsec(&inode_item->ctime,
1683 				      inode->i_ctime.tv_nsec);
1684 
1685 	btrfs_set_stack_timespec_sec(&inode_item->otime,
1686 				     BTRFS_I(inode)->i_otime.tv_sec);
1687 	btrfs_set_stack_timespec_nsec(&inode_item->otime,
1688 				     BTRFS_I(inode)->i_otime.tv_nsec);
1689 }
1690 
1691 int btrfs_fill_inode(struct inode *inode, u32 *rdev)
1692 {
1693 	struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
1694 	struct btrfs_delayed_node *delayed_node;
1695 	struct btrfs_inode_item *inode_item;
1696 
1697 	delayed_node = btrfs_get_delayed_node(BTRFS_I(inode));
1698 	if (!delayed_node)
1699 		return -ENOENT;
1700 
1701 	mutex_lock(&delayed_node->mutex);
1702 	if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
1703 		mutex_unlock(&delayed_node->mutex);
1704 		btrfs_release_delayed_node(delayed_node);
1705 		return -ENOENT;
1706 	}
1707 
1708 	inode_item = &delayed_node->inode_item;
1709 
1710 	i_uid_write(inode, btrfs_stack_inode_uid(inode_item));
1711 	i_gid_write(inode, btrfs_stack_inode_gid(inode_item));
1712 	btrfs_i_size_write(BTRFS_I(inode), btrfs_stack_inode_size(inode_item));
1713 	btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
1714 			round_up(i_size_read(inode), fs_info->sectorsize));
1715 	inode->i_mode = btrfs_stack_inode_mode(inode_item);
1716 	set_nlink(inode, btrfs_stack_inode_nlink(inode_item));
1717 	inode_set_bytes(inode, btrfs_stack_inode_nbytes(inode_item));
1718 	BTRFS_I(inode)->generation = btrfs_stack_inode_generation(inode_item);
1719         BTRFS_I(inode)->last_trans = btrfs_stack_inode_transid(inode_item);
1720 
1721 	inode_set_iversion_queried(inode,
1722 				   btrfs_stack_inode_sequence(inode_item));
1723 	inode->i_rdev = 0;
1724 	*rdev = btrfs_stack_inode_rdev(inode_item);
1725 	btrfs_inode_split_flags(btrfs_stack_inode_flags(inode_item),
1726 				&BTRFS_I(inode)->flags, &BTRFS_I(inode)->ro_flags);
1727 
1728 	inode->i_atime.tv_sec = btrfs_stack_timespec_sec(&inode_item->atime);
1729 	inode->i_atime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->atime);
1730 
1731 	inode->i_mtime.tv_sec = btrfs_stack_timespec_sec(&inode_item->mtime);
1732 	inode->i_mtime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->mtime);
1733 
1734 	inode->i_ctime.tv_sec = btrfs_stack_timespec_sec(&inode_item->ctime);
1735 	inode->i_ctime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->ctime);
1736 
1737 	BTRFS_I(inode)->i_otime.tv_sec =
1738 		btrfs_stack_timespec_sec(&inode_item->otime);
1739 	BTRFS_I(inode)->i_otime.tv_nsec =
1740 		btrfs_stack_timespec_nsec(&inode_item->otime);
1741 
1742 	inode->i_generation = BTRFS_I(inode)->generation;
1743 	BTRFS_I(inode)->index_cnt = (u64)-1;
1744 
1745 	mutex_unlock(&delayed_node->mutex);
1746 	btrfs_release_delayed_node(delayed_node);
1747 	return 0;
1748 }
1749 
1750 int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,
1751 			       struct btrfs_root *root,
1752 			       struct btrfs_inode *inode)
1753 {
1754 	struct btrfs_delayed_node *delayed_node;
1755 	int ret = 0;
1756 
1757 	delayed_node = btrfs_get_or_create_delayed_node(inode);
1758 	if (IS_ERR(delayed_node))
1759 		return PTR_ERR(delayed_node);
1760 
1761 	mutex_lock(&delayed_node->mutex);
1762 	if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
1763 		fill_stack_inode_item(trans, &delayed_node->inode_item,
1764 				      &inode->vfs_inode);
1765 		goto release_node;
1766 	}
1767 
1768 	ret = btrfs_delayed_inode_reserve_metadata(trans, root, delayed_node);
1769 	if (ret)
1770 		goto release_node;
1771 
1772 	fill_stack_inode_item(trans, &delayed_node->inode_item, &inode->vfs_inode);
1773 	set_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags);
1774 	delayed_node->count++;
1775 	atomic_inc(&root->fs_info->delayed_root->items);
1776 release_node:
1777 	mutex_unlock(&delayed_node->mutex);
1778 	btrfs_release_delayed_node(delayed_node);
1779 	return ret;
1780 }
1781 
1782 int btrfs_delayed_delete_inode_ref(struct btrfs_inode *inode)
1783 {
1784 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
1785 	struct btrfs_delayed_node *delayed_node;
1786 
1787 	/*
1788 	 * we don't do delayed inode updates during log recovery because it
1789 	 * leads to enospc problems.  This means we also can't do
1790 	 * delayed inode refs
1791 	 */
1792 	if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
1793 		return -EAGAIN;
1794 
1795 	delayed_node = btrfs_get_or_create_delayed_node(inode);
1796 	if (IS_ERR(delayed_node))
1797 		return PTR_ERR(delayed_node);
1798 
1799 	/*
1800 	 * We don't reserve space for inode ref deletion is because:
1801 	 * - We ONLY do async inode ref deletion for the inode who has only
1802 	 *   one link(i_nlink == 1), it means there is only one inode ref.
1803 	 *   And in most case, the inode ref and the inode item are in the
1804 	 *   same leaf, and we will deal with them at the same time.
1805 	 *   Since we are sure we will reserve the space for the inode item,
1806 	 *   it is unnecessary to reserve space for inode ref deletion.
1807 	 * - If the inode ref and the inode item are not in the same leaf,
1808 	 *   We also needn't worry about enospc problem, because we reserve
1809 	 *   much more space for the inode update than it needs.
1810 	 * - At the worst, we can steal some space from the global reservation.
1811 	 *   It is very rare.
1812 	 */
1813 	mutex_lock(&delayed_node->mutex);
1814 	if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags))
1815 		goto release_node;
1816 
1817 	set_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags);
1818 	delayed_node->count++;
1819 	atomic_inc(&fs_info->delayed_root->items);
1820 release_node:
1821 	mutex_unlock(&delayed_node->mutex);
1822 	btrfs_release_delayed_node(delayed_node);
1823 	return 0;
1824 }
1825 
1826 static void __btrfs_kill_delayed_node(struct btrfs_delayed_node *delayed_node)
1827 {
1828 	struct btrfs_root *root = delayed_node->root;
1829 	struct btrfs_fs_info *fs_info = root->fs_info;
1830 	struct btrfs_delayed_item *curr_item, *prev_item;
1831 
1832 	mutex_lock(&delayed_node->mutex);
1833 	curr_item = __btrfs_first_delayed_insertion_item(delayed_node);
1834 	while (curr_item) {
1835 		btrfs_delayed_item_release_metadata(root, curr_item);
1836 		prev_item = curr_item;
1837 		curr_item = __btrfs_next_delayed_item(prev_item);
1838 		btrfs_release_delayed_item(prev_item);
1839 	}
1840 
1841 	curr_item = __btrfs_first_delayed_deletion_item(delayed_node);
1842 	while (curr_item) {
1843 		btrfs_delayed_item_release_metadata(root, curr_item);
1844 		prev_item = curr_item;
1845 		curr_item = __btrfs_next_delayed_item(prev_item);
1846 		btrfs_release_delayed_item(prev_item);
1847 	}
1848 
1849 	btrfs_release_delayed_iref(delayed_node);
1850 
1851 	if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
1852 		btrfs_delayed_inode_release_metadata(fs_info, delayed_node, false);
1853 		btrfs_release_delayed_inode(delayed_node);
1854 	}
1855 	mutex_unlock(&delayed_node->mutex);
1856 }
1857 
1858 void btrfs_kill_delayed_inode_items(struct btrfs_inode *inode)
1859 {
1860 	struct btrfs_delayed_node *delayed_node;
1861 
1862 	delayed_node = btrfs_get_delayed_node(inode);
1863 	if (!delayed_node)
1864 		return;
1865 
1866 	__btrfs_kill_delayed_node(delayed_node);
1867 	btrfs_release_delayed_node(delayed_node);
1868 }
1869 
1870 void btrfs_kill_all_delayed_nodes(struct btrfs_root *root)
1871 {
1872 	u64 inode_id = 0;
1873 	struct btrfs_delayed_node *delayed_nodes[8];
1874 	int i, n;
1875 
1876 	while (1) {
1877 		spin_lock(&root->inode_lock);
1878 		n = radix_tree_gang_lookup(&root->delayed_nodes_tree,
1879 					   (void **)delayed_nodes, inode_id,
1880 					   ARRAY_SIZE(delayed_nodes));
1881 		if (!n) {
1882 			spin_unlock(&root->inode_lock);
1883 			break;
1884 		}
1885 
1886 		inode_id = delayed_nodes[n - 1]->inode_id + 1;
1887 		for (i = 0; i < n; i++) {
1888 			/*
1889 			 * Don't increase refs in case the node is dead and
1890 			 * about to be removed from the tree in the loop below
1891 			 */
1892 			if (!refcount_inc_not_zero(&delayed_nodes[i]->refs))
1893 				delayed_nodes[i] = NULL;
1894 		}
1895 		spin_unlock(&root->inode_lock);
1896 
1897 		for (i = 0; i < n; i++) {
1898 			if (!delayed_nodes[i])
1899 				continue;
1900 			__btrfs_kill_delayed_node(delayed_nodes[i]);
1901 			btrfs_release_delayed_node(delayed_nodes[i]);
1902 		}
1903 	}
1904 }
1905 
1906 void btrfs_destroy_delayed_inodes(struct btrfs_fs_info *fs_info)
1907 {
1908 	struct btrfs_delayed_node *curr_node, *prev_node;
1909 
1910 	curr_node = btrfs_first_delayed_node(fs_info->delayed_root);
1911 	while (curr_node) {
1912 		__btrfs_kill_delayed_node(curr_node);
1913 
1914 		prev_node = curr_node;
1915 		curr_node = btrfs_next_delayed_node(curr_node);
1916 		btrfs_release_delayed_node(prev_node);
1917 	}
1918 }
1919 
1920