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