delayed-inode.c 50.6 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7
/*
 * Copyright (C) 2011 Fujitsu.  All rights reserved.
 * Written by Miao Xie <miaox@cn.fujitsu.com>
 */

#include <linux/slab.h>
8
#include <linux/iversion.h>
9 10 11
#include "delayed-inode.h"
#include "disk-io.h"
#include "transaction.h"
12
#include "ctree.h"
13
#include "qgroup.h"
14

15 16 17
#define BTRFS_DELAYED_WRITEBACK		512
#define BTRFS_DELAYED_BACKGROUND	128
#define BTRFS_DELAYED_BATCH		16
18 19 20 21 22

static struct kmem_cache *delayed_node_cache;

int __init btrfs_delayed_inode_init(void)
{
23
	delayed_node_cache = kmem_cache_create("btrfs_delayed_node",
24 25
					sizeof(struct btrfs_delayed_node),
					0,
26
					SLAB_MEM_SPREAD,
27 28 29 30 31 32
					NULL);
	if (!delayed_node_cache)
		return -ENOMEM;
	return 0;
}

33
void __cold btrfs_delayed_inode_exit(void)
34
{
35
	kmem_cache_destroy(delayed_node_cache);
36 37 38 39 40 41 42 43
}

static inline void btrfs_init_delayed_node(
				struct btrfs_delayed_node *delayed_node,
				struct btrfs_root *root, u64 inode_id)
{
	delayed_node->root = root;
	delayed_node->inode_id = inode_id;
44
	refcount_set(&delayed_node->refs, 0);
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
	delayed_node->ins_root = RB_ROOT;
	delayed_node->del_root = RB_ROOT;
	mutex_init(&delayed_node->mutex);
	INIT_LIST_HEAD(&delayed_node->n_list);
	INIT_LIST_HEAD(&delayed_node->p_list);
}

static inline int btrfs_is_continuous_delayed_item(
					struct btrfs_delayed_item *item1,
					struct btrfs_delayed_item *item2)
{
	if (item1->key.type == BTRFS_DIR_INDEX_KEY &&
	    item1->key.objectid == item2->key.objectid &&
	    item1->key.type == item2->key.type &&
	    item1->key.offset + 1 == item2->key.offset)
		return 1;
	return 0;
}

64 65
static struct btrfs_delayed_node *btrfs_get_delayed_node(
		struct btrfs_inode *btrfs_inode)
66 67
{
	struct btrfs_root *root = btrfs_inode->root;
68
	u64 ino = btrfs_ino(btrfs_inode);
69
	struct btrfs_delayed_node *node;
70

71
	node = READ_ONCE(btrfs_inode->delayed_node);
72
	if (node) {
73
		refcount_inc(&node->refs);
74 75 76 77
		return node;
	}

	spin_lock(&root->inode_lock);
78
	node = radix_tree_lookup(&root->delayed_nodes_tree, ino);
79

80 81
	if (node) {
		if (btrfs_inode->delayed_node) {
82
			refcount_inc(&node->refs);	/* can be accessed */
83
			BUG_ON(btrfs_inode->delayed_node != node);
84
			spin_unlock(&root->inode_lock);
85
			return node;
86
		}
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

		/*
		 * It's possible that we're racing into the middle of removing
		 * this node from the radix tree.  In this case, the refcount
		 * was zero and it should never go back to one.  Just return
		 * NULL like it was never in the radix at all; our release
		 * function is in the process of removing it.
		 *
		 * Some implementations of refcount_inc refuse to bump the
		 * refcount once it has hit zero.  If we don't do this dance
		 * here, refcount_inc() may decide to just WARN_ONCE() instead
		 * of actually bumping the refcount.
		 *
		 * If this node is properly in the radix, we want to bump the
		 * refcount twice, once for the inode and once for this get
		 * operation.
		 */
		if (refcount_inc_not_zero(&node->refs)) {
			refcount_inc(&node->refs);
			btrfs_inode->delayed_node = node;
		} else {
			node = NULL;
		}

111 112 113 114 115
		spin_unlock(&root->inode_lock);
		return node;
	}
	spin_unlock(&root->inode_lock);

116 117 118
	return NULL;
}

119
/* Will return either the node or PTR_ERR(-ENOMEM) */
120
static struct btrfs_delayed_node *btrfs_get_or_create_delayed_node(
121
		struct btrfs_inode *btrfs_inode)
122 123 124
{
	struct btrfs_delayed_node *node;
	struct btrfs_root *root = btrfs_inode->root;
125
	u64 ino = btrfs_ino(btrfs_inode);
126 127 128
	int ret;

again:
129
	node = btrfs_get_delayed_node(btrfs_inode);
130 131 132
	if (node)
		return node;

133
	node = kmem_cache_zalloc(delayed_node_cache, GFP_NOFS);
134 135
	if (!node)
		return ERR_PTR(-ENOMEM);
136
	btrfs_init_delayed_node(node, root, ino);
137

138
	/* cached in the btrfs inode and can be accessed */
139
	refcount_set(&node->refs, 2);
140

141
	ret = radix_tree_preload(GFP_NOFS);
142 143 144 145 146 147
	if (ret) {
		kmem_cache_free(delayed_node_cache, node);
		return ERR_PTR(ret);
	}

	spin_lock(&root->inode_lock);
148
	ret = radix_tree_insert(&root->delayed_nodes_tree, ino, node);
149 150
	if (ret == -EEXIST) {
		spin_unlock(&root->inode_lock);
151
		kmem_cache_free(delayed_node_cache, node);
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
		radix_tree_preload_end();
		goto again;
	}
	btrfs_inode->delayed_node = node;
	spin_unlock(&root->inode_lock);
	radix_tree_preload_end();

	return node;
}

/*
 * Call it when holding delayed_node->mutex
 *
 * If mod = 1, add this node into the prepared list.
 */
static void btrfs_queue_delayed_node(struct btrfs_delayed_root *root,
				     struct btrfs_delayed_node *node,
				     int mod)
{
	spin_lock(&root->lock);
172
	if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
173 174 175 176 177 178 179
		if (!list_empty(&node->p_list))
			list_move_tail(&node->p_list, &root->prepare_list);
		else if (mod)
			list_add_tail(&node->p_list, &root->prepare_list);
	} else {
		list_add_tail(&node->n_list, &root->node_list);
		list_add_tail(&node->p_list, &root->prepare_list);
180
		refcount_inc(&node->refs);	/* inserted into list */
181
		root->nodes++;
182
		set_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags);
183 184 185 186 187 188 189 190 191
	}
	spin_unlock(&root->lock);
}

/* Call it when holding delayed_node->mutex */
static void btrfs_dequeue_delayed_node(struct btrfs_delayed_root *root,
				       struct btrfs_delayed_node *node)
{
	spin_lock(&root->lock);
192
	if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
193
		root->nodes--;
194
		refcount_dec(&node->refs);	/* not in the list */
195 196 197
		list_del_init(&node->n_list);
		if (!list_empty(&node->p_list))
			list_del_init(&node->p_list);
198
		clear_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags);
199 200 201 202
	}
	spin_unlock(&root->lock);
}

203
static struct btrfs_delayed_node *btrfs_first_delayed_node(
204 205 206 207 208 209 210 211 212 213 214
			struct btrfs_delayed_root *delayed_root)
{
	struct list_head *p;
	struct btrfs_delayed_node *node = NULL;

	spin_lock(&delayed_root->lock);
	if (list_empty(&delayed_root->node_list))
		goto out;

	p = delayed_root->node_list.next;
	node = list_entry(p, struct btrfs_delayed_node, n_list);
215
	refcount_inc(&node->refs);
216 217 218 219 220 221
out:
	spin_unlock(&delayed_root->lock);

	return node;
}

222
static struct btrfs_delayed_node *btrfs_next_delayed_node(
223 224 225 226 227 228 229 230
						struct btrfs_delayed_node *node)
{
	struct btrfs_delayed_root *delayed_root;
	struct list_head *p;
	struct btrfs_delayed_node *next = NULL;

	delayed_root = node->root->fs_info->delayed_root;
	spin_lock(&delayed_root->lock);
231 232
	if (!test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
		/* not in the list */
233 234 235 236 237 238 239 240 241
		if (list_empty(&delayed_root->node_list))
			goto out;
		p = delayed_root->node_list.next;
	} else if (list_is_last(&node->n_list, &delayed_root->node_list))
		goto out;
	else
		p = node->n_list.next;

	next = list_entry(p, struct btrfs_delayed_node, n_list);
242
	refcount_inc(&next->refs);
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
out:
	spin_unlock(&delayed_root->lock);

	return next;
}

static void __btrfs_release_delayed_node(
				struct btrfs_delayed_node *delayed_node,
				int mod)
{
	struct btrfs_delayed_root *delayed_root;

	if (!delayed_node)
		return;

	delayed_root = delayed_node->root->fs_info->delayed_root;

	mutex_lock(&delayed_node->mutex);
	if (delayed_node->count)
		btrfs_queue_delayed_node(delayed_root, delayed_node, mod);
	else
		btrfs_dequeue_delayed_node(delayed_root, delayed_node);
	mutex_unlock(&delayed_node->mutex);

267
	if (refcount_dec_and_test(&delayed_node->refs)) {
268
		struct btrfs_root *root = delayed_node->root;
269

270
		spin_lock(&root->inode_lock);
271 272 273 274 275 276 277
		/*
		 * Once our refcount goes to zero, nobody is allowed to bump it
		 * back up.  We can delete it now.
		 */
		ASSERT(refcount_read(&delayed_node->refs) == 0);
		radix_tree_delete(&root->delayed_nodes_tree,
				  delayed_node->inode_id);
278
		spin_unlock(&root->inode_lock);
279
		kmem_cache_free(delayed_node_cache, delayed_node);
280 281 282 283 284 285 286 287
	}
}

static inline void btrfs_release_delayed_node(struct btrfs_delayed_node *node)
{
	__btrfs_release_delayed_node(node, 0);
}

288
static struct btrfs_delayed_node *btrfs_first_prepared_delayed_node(
289 290 291 292 293 294 295 296 297 298 299 300
					struct btrfs_delayed_root *delayed_root)
{
	struct list_head *p;
	struct btrfs_delayed_node *node = NULL;

	spin_lock(&delayed_root->lock);
	if (list_empty(&delayed_root->prepare_list))
		goto out;

	p = delayed_root->prepare_list.next;
	list_del_init(p);
	node = list_entry(p, struct btrfs_delayed_node, p_list);
301
	refcount_inc(&node->refs);
302 303 304 305 306 307 308 309 310 311 312 313
out:
	spin_unlock(&delayed_root->lock);

	return node;
}

static inline void btrfs_release_prepared_delayed_node(
					struct btrfs_delayed_node *node)
{
	__btrfs_release_delayed_node(node, 1);
}

314
static struct btrfs_delayed_item *btrfs_alloc_delayed_item(u32 data_len)
315 316 317 318 319 320 321 322
{
	struct btrfs_delayed_item *item;
	item = kmalloc(sizeof(*item) + data_len, GFP_NOFS);
	if (item) {
		item->data_len = data_len;
		item->ins_or_del = 0;
		item->bytes_reserved = 0;
		item->delayed_node = NULL;
323
		refcount_set(&item->refs, 1);
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
	}
	return item;
}

/*
 * __btrfs_lookup_delayed_item - look up the delayed item by key
 * @delayed_node: pointer to the delayed node
 * @key:	  the key to look up
 * @prev:	  used to store the prev item if the right item isn't found
 * @next:	  used to store the next item if the right item isn't found
 *
 * Note: if we don't find the right item, we will return the prev item and
 * the next item.
 */
static struct btrfs_delayed_item *__btrfs_lookup_delayed_item(
				struct rb_root *root,
				struct btrfs_key *key,
				struct btrfs_delayed_item **prev,
				struct btrfs_delayed_item **next)
{
	struct rb_node *node, *prev_node = NULL;
	struct btrfs_delayed_item *delayed_item = NULL;
	int ret = 0;

	node = root->rb_node;

	while (node) {
		delayed_item = rb_entry(node, struct btrfs_delayed_item,
					rb_node);
		prev_node = node;
		ret = btrfs_comp_cpu_keys(&delayed_item->key, key);
		if (ret < 0)
			node = node->rb_right;
		else if (ret > 0)
			node = node->rb_left;
		else
			return delayed_item;
	}

	if (prev) {
		if (!prev_node)
			*prev = NULL;
		else if (ret < 0)
			*prev = delayed_item;
		else if ((node = rb_prev(prev_node)) != NULL) {
			*prev = rb_entry(node, struct btrfs_delayed_item,
					 rb_node);
		} else
			*prev = NULL;
	}

	if (next) {
		if (!prev_node)
			*next = NULL;
		else if (ret > 0)
			*next = delayed_item;
		else if ((node = rb_next(prev_node)) != NULL) {
			*next = rb_entry(node, struct btrfs_delayed_item,
					 rb_node);
		} else
			*next = NULL;
	}
	return NULL;
}

389
static struct btrfs_delayed_item *__btrfs_lookup_delayed_insertion_item(
390 391 392
					struct btrfs_delayed_node *delayed_node,
					struct btrfs_key *key)
{
393
	return __btrfs_lookup_delayed_item(&delayed_node->ins_root, key,
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
					   NULL, NULL);
}

static int __btrfs_add_delayed_item(struct btrfs_delayed_node *delayed_node,
				    struct btrfs_delayed_item *ins,
				    int action)
{
	struct rb_node **p, *node;
	struct rb_node *parent_node = NULL;
	struct rb_root *root;
	struct btrfs_delayed_item *item;
	int cmp;

	if (action == BTRFS_DELAYED_INSERTION_ITEM)
		root = &delayed_node->ins_root;
	else if (action == BTRFS_DELAYED_DELETION_ITEM)
		root = &delayed_node->del_root;
	else
		BUG();
	p = &root->rb_node;
	node = &ins->rb_node;

	while (*p) {
		parent_node = *p;
		item = rb_entry(parent_node, struct btrfs_delayed_item,
				 rb_node);

		cmp = btrfs_comp_cpu_keys(&item->key, &ins->key);
		if (cmp < 0)
			p = &(*p)->rb_right;
		else if (cmp > 0)
			p = &(*p)->rb_left;
		else
			return -EEXIST;
	}

	rb_link_node(node, parent_node, p);
	rb_insert_color(node, root);
	ins->delayed_node = delayed_node;
	ins->ins_or_del = action;

	if (ins->key.type == BTRFS_DIR_INDEX_KEY &&
	    action == BTRFS_DELAYED_INSERTION_ITEM &&
	    ins->key.offset >= delayed_node->index_cnt)
			delayed_node->index_cnt = ins->key.offset + 1;

	delayed_node->count++;
	atomic_inc(&delayed_node->root->fs_info->delayed_root->items);
	return 0;
}

static int __btrfs_add_delayed_insertion_item(struct btrfs_delayed_node *node,
					      struct btrfs_delayed_item *item)
{
	return __btrfs_add_delayed_item(node, item,
					BTRFS_DELAYED_INSERTION_ITEM);
}

static int __btrfs_add_delayed_deletion_item(struct btrfs_delayed_node *node,
					     struct btrfs_delayed_item *item)
{
	return __btrfs_add_delayed_item(node, item,
					BTRFS_DELAYED_DELETION_ITEM);
}

459 460 461
static void finish_one_item(struct btrfs_delayed_root *delayed_root)
{
	int seq = atomic_inc_return(&delayed_root->items_seq);
462 463 464 465

	/*
	 * atomic_dec_return implies a barrier for waitqueue_active
	 */
466 467 468 469 470 471
	if ((atomic_dec_return(&delayed_root->items) <
	    BTRFS_DELAYED_BACKGROUND || seq % BTRFS_DELAYED_BATCH == 0) &&
	    waitqueue_active(&delayed_root->wait))
		wake_up(&delayed_root->wait);
}

472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
static void __btrfs_remove_delayed_item(struct btrfs_delayed_item *delayed_item)
{
	struct rb_root *root;
	struct btrfs_delayed_root *delayed_root;

	delayed_root = delayed_item->delayed_node->root->fs_info->delayed_root;

	BUG_ON(!delayed_root);
	BUG_ON(delayed_item->ins_or_del != BTRFS_DELAYED_DELETION_ITEM &&
	       delayed_item->ins_or_del != BTRFS_DELAYED_INSERTION_ITEM);

	if (delayed_item->ins_or_del == BTRFS_DELAYED_INSERTION_ITEM)
		root = &delayed_item->delayed_node->ins_root;
	else
		root = &delayed_item->delayed_node->del_root;

	rb_erase(&delayed_item->rb_node, root);
	delayed_item->delayed_node->count--;
490 491

	finish_one_item(delayed_root);
492 493 494 495 496 497
}

static void btrfs_release_delayed_item(struct btrfs_delayed_item *item)
{
	if (item) {
		__btrfs_remove_delayed_item(item);
498
		if (refcount_dec_and_test(&item->refs))
499 500 501 502
			kfree(item);
	}
}

503
static struct btrfs_delayed_item *__btrfs_first_delayed_insertion_item(
504 505 506 507 508 509 510 511 512 513 514 515
					struct btrfs_delayed_node *delayed_node)
{
	struct rb_node *p;
	struct btrfs_delayed_item *item = NULL;

	p = rb_first(&delayed_node->ins_root);
	if (p)
		item = rb_entry(p, struct btrfs_delayed_item, rb_node);

	return item;
}

516
static struct btrfs_delayed_item *__btrfs_first_delayed_deletion_item(
517 518 519 520 521 522 523 524 525 526 527 528
					struct btrfs_delayed_node *delayed_node)
{
	struct rb_node *p;
	struct btrfs_delayed_item *item = NULL;

	p = rb_first(&delayed_node->del_root);
	if (p)
		item = rb_entry(p, struct btrfs_delayed_item, rb_node);

	return item;
}

529
static struct btrfs_delayed_item *__btrfs_next_delayed_item(
530 531 532 533 534 535 536 537 538 539 540 541 542
						struct btrfs_delayed_item *item)
{
	struct rb_node *p;
	struct btrfs_delayed_item *next = NULL;

	p = rb_next(&item->rb_node);
	if (p)
		next = rb_entry(p, struct btrfs_delayed_item, rb_node);

	return next;
}

static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle *trans,
543
					       struct btrfs_root *root,
544 545 546 547
					       struct btrfs_delayed_item *item)
{
	struct btrfs_block_rsv *src_rsv;
	struct btrfs_block_rsv *dst_rsv;
548
	struct btrfs_fs_info *fs_info = root->fs_info;
549 550 551 552 553 554 555
	u64 num_bytes;
	int ret;

	if (!trans->bytes_reserved)
		return 0;

	src_rsv = trans->block_rsv;
556
	dst_rsv = &fs_info->delayed_block_rsv;
557

558
	num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
559
	ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, 1);
560
	if (!ret) {
561
		trace_btrfs_space_reservation(fs_info, "delayed_item",
562 563
					      item->key.objectid,
					      num_bytes, 1);
564
		item->bytes_reserved = num_bytes;
565
	}
566 567 568 569

	return ret;
}

570
static void btrfs_delayed_item_release_metadata(struct btrfs_root *root,
571 572
						struct btrfs_delayed_item *item)
{
573
	struct btrfs_block_rsv *rsv;
574
	struct btrfs_fs_info *fs_info = root->fs_info;
575

576 577 578
	if (!item->bytes_reserved)
		return;

579
	rsv = &fs_info->delayed_block_rsv;
580
	btrfs_qgroup_convert_reserved_meta(root, item->bytes_reserved);
581
	trace_btrfs_space_reservation(fs_info, "delayed_item",
582 583
				      item->key.objectid, item->bytes_reserved,
				      0);
584
	btrfs_block_rsv_release(fs_info, rsv,
585 586 587 588 589 590
				item->bytes_reserved);
}

static int btrfs_delayed_inode_reserve_metadata(
					struct btrfs_trans_handle *trans,
					struct btrfs_root *root,
591
					struct btrfs_inode *inode,
592 593
					struct btrfs_delayed_node *node)
{
594
	struct btrfs_fs_info *fs_info = root->fs_info;
595 596 597 598 599 600
	struct btrfs_block_rsv *src_rsv;
	struct btrfs_block_rsv *dst_rsv;
	u64 num_bytes;
	int ret;

	src_rsv = trans->block_rsv;
601
	dst_rsv = &fs_info->delayed_block_rsv;
602

603
	num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
604

605 606 607
	ret = btrfs_qgroup_reserve_meta_prealloc(root, num_bytes, true);
	if (ret < 0)
		return ret;
608 609 610 611 612 613 614
	/*
	 * btrfs_dirty_inode will update the inode under btrfs_join_transaction
	 * which doesn't reserve space for speed.  This is a problem since we
	 * still need to reserve space for this update, so try to reserve the
	 * space.
	 *
	 * Now if src_rsv == delalloc_block_rsv we'll let it just steal since
615
	 * we always reserve enough to update the inode item.
616
	 */
617
	if (!src_rsv || (!trans->bytes_reserved &&
618
			 src_rsv->type != BTRFS_BLOCK_RSV_DELALLOC)) {
619 620
		ret = btrfs_block_rsv_add(root, dst_rsv, num_bytes,
					  BTRFS_RESERVE_NO_FLUSH);
621 622 623 624 625 626
		/*
		 * Since we're under a transaction reserve_metadata_bytes could
		 * try to commit the transaction which will make it return
		 * EAGAIN to make us stop the transaction we have, so return
		 * ENOSPC instead so that btrfs_dirty_inode knows what to do.
		 */
627
		if (ret == -EAGAIN) {
628
			ret = -ENOSPC;
629 630
			btrfs_qgroup_free_meta_prealloc(root, num_bytes);
		}
631
		if (!ret) {
632
			node->bytes_reserved = num_bytes;
633
			trace_btrfs_space_reservation(fs_info,
634
						      "delayed_inode",
635
						      btrfs_ino(inode),
636 637
						      num_bytes, 1);
		}
638 639 640
		return ret;
	}

641
	ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, 1);
642
	if (!ret) {
643
		trace_btrfs_space_reservation(fs_info, "delayed_inode",
644
					      btrfs_ino(inode), num_bytes, 1);
645
		node->bytes_reserved = num_bytes;
646
	}
647 648 649 650

	return ret;
}

651
static void btrfs_delayed_inode_release_metadata(struct btrfs_fs_info *fs_info,
652 653
						struct btrfs_delayed_node *node,
						bool qgroup_free)
654 655 656 657 658 659
{
	struct btrfs_block_rsv *rsv;

	if (!node->bytes_reserved)
		return;

660 661
	rsv = &fs_info->delayed_block_rsv;
	trace_btrfs_space_reservation(fs_info, "delayed_inode",
662
				      node->inode_id, node->bytes_reserved, 0);
663
	btrfs_block_rsv_release(fs_info, rsv,
664
				node->bytes_reserved);
665 666 667 668 669 670
	if (qgroup_free)
		btrfs_qgroup_free_meta_prealloc(node->root,
				node->bytes_reserved);
	else
		btrfs_qgroup_convert_reserved_meta(node->root,
				node->bytes_reserved);
671 672 673 674 675 676 677
	node->bytes_reserved = 0;
}

/*
 * This helper will insert some continuous items into the same leaf according
 * to the free space of the leaf.
 */
678 679 680
static int btrfs_batch_insert_items(struct btrfs_root *root,
				    struct btrfs_path *path,
				    struct btrfs_delayed_item *item)
681
{
682
	struct btrfs_fs_info *fs_info = root->fs_info;
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
	struct btrfs_delayed_item *curr, *next;
	int free_space;
	int total_data_size = 0, total_size = 0;
	struct extent_buffer *leaf;
	char *data_ptr;
	struct btrfs_key *keys;
	u32 *data_size;
	struct list_head head;
	int slot;
	int nitems;
	int i;
	int ret = 0;

	BUG_ON(!path->nodes[0]);

	leaf = path->nodes[0];
699
	free_space = btrfs_leaf_free_space(fs_info, leaf);
700 701 702
	INIT_LIST_HEAD(&head);

	next = item;
703
	nitems = 0;
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735

	/*
	 * count the number of the continuous items that we can insert in batch
	 */
	while (total_size + next->data_len + sizeof(struct btrfs_item) <=
	       free_space) {
		total_data_size += next->data_len;
		total_size += next->data_len + sizeof(struct btrfs_item);
		list_add_tail(&next->tree_list, &head);
		nitems++;

		curr = next;
		next = __btrfs_next_delayed_item(curr);
		if (!next)
			break;

		if (!btrfs_is_continuous_delayed_item(curr, next))
			break;
	}

	if (!nitems) {
		ret = 0;
		goto out;
	}

	/*
	 * we need allocate some memory space, but it might cause the task
	 * to sleep, so we set all locked nodes in the path to blocking locks
	 * first.
	 */
	btrfs_set_path_blocking(path);

736
	keys = kmalloc_array(nitems, sizeof(struct btrfs_key), GFP_NOFS);
737 738 739 740 741
	if (!keys) {
		ret = -ENOMEM;
		goto out;
	}

742
	data_size = kmalloc_array(nitems, sizeof(u32), GFP_NOFS);
743 744 745 746 747 748 749 750 751 752 753 754 755 756
	if (!data_size) {
		ret = -ENOMEM;
		goto error;
	}

	/* get keys of all the delayed items */
	i = 0;
	list_for_each_entry(next, &head, tree_list) {
		keys[i] = next->key;
		data_size[i] = next->data_len;
		i++;
	}

	/* reset all the locked nodes in the patch to spinning locks. */
757
	btrfs_clear_path_blocking(path, NULL, 0);
758 759

	/* insert the keys of the items */
760
	setup_items_for_insert(root, path, keys, data_size,
761
			       total_data_size, total_size, nitems);
762 763 764 765 766 767 768 769 770 771

	/* insert the dir index items */
	slot = path->slots[0];
	list_for_each_entry_safe(curr, next, &head, tree_list) {
		data_ptr = btrfs_item_ptr(leaf, slot, char);
		write_extent_buffer(leaf, &curr->data,
				    (unsigned long)data_ptr,
				    curr->data_len);
		slot++;

772
		btrfs_delayed_item_release_metadata(root, curr);
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810

		list_del(&curr->tree_list);
		btrfs_release_delayed_item(curr);
	}

error:
	kfree(data_size);
	kfree(keys);
out:
	return ret;
}

/*
 * This helper can just do simple insertion that needn't extend item for new
 * data, such as directory name index insertion, inode insertion.
 */
static int btrfs_insert_delayed_item(struct btrfs_trans_handle *trans,
				     struct btrfs_root *root,
				     struct btrfs_path *path,
				     struct btrfs_delayed_item *delayed_item)
{
	struct extent_buffer *leaf;
	char *ptr;
	int ret;

	ret = btrfs_insert_empty_item(trans, root, path, &delayed_item->key,
				      delayed_item->data_len);
	if (ret < 0 && ret != -EEXIST)
		return ret;

	leaf = path->nodes[0];

	ptr = btrfs_item_ptr(leaf, path->slots[0], char);

	write_extent_buffer(leaf, delayed_item->data, (unsigned long)ptr,
			    delayed_item->data_len);
	btrfs_mark_buffer_dirty(leaf);

811
	btrfs_delayed_item_release_metadata(root, delayed_item);
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
	return 0;
}

/*
 * we insert an item first, then if there are some continuous items, we try
 * to insert those items into the same leaf.
 */
static int btrfs_insert_delayed_items(struct btrfs_trans_handle *trans,
				      struct btrfs_path *path,
				      struct btrfs_root *root,
				      struct btrfs_delayed_node *node)
{
	struct btrfs_delayed_item *curr, *prev;
	int ret = 0;

do_again:
	mutex_lock(&node->mutex);
	curr = __btrfs_first_delayed_insertion_item(node);
	if (!curr)
		goto insert_end;

	ret = btrfs_insert_delayed_item(trans, root, path, curr);
	if (ret < 0) {
835
		btrfs_release_path(path);
836 837 838 839 840 841 842 843
		goto insert_end;
	}

	prev = curr;
	curr = __btrfs_next_delayed_item(prev);
	if (curr && btrfs_is_continuous_delayed_item(prev, curr)) {
		/* insert the continuous items into the same leaf */
		path->slots[0]++;
844
		btrfs_batch_insert_items(root, path, curr);
845 846 847 848
	}
	btrfs_release_delayed_item(prev);
	btrfs_mark_buffer_dirty(path->nodes[0]);

849
	btrfs_release_path(path);
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
	mutex_unlock(&node->mutex);
	goto do_again;

insert_end:
	mutex_unlock(&node->mutex);
	return ret;
}

static int btrfs_batch_delete_items(struct btrfs_trans_handle *trans,
				    struct btrfs_root *root,
				    struct btrfs_path *path,
				    struct btrfs_delayed_item *item)
{
	struct btrfs_delayed_item *curr, *next;
	struct extent_buffer *leaf;
	struct btrfs_key key;
	struct list_head head;
	int nitems, i, last_item;
	int ret = 0;

	BUG_ON(!path->nodes[0]);

	leaf = path->nodes[0];

	i = path->slots[0];
	last_item = btrfs_header_nritems(leaf) - 1;
	if (i > last_item)
		return -ENOENT;	/* FIXME: Is errno suitable? */

	next = item;
	INIT_LIST_HEAD(&head);
	btrfs_item_key_to_cpu(leaf, &key, i);
	nitems = 0;
	/*
	 * count the number of the dir index items that we can delete in batch
	 */
	while (btrfs_comp_cpu_keys(&next->key, &key) == 0) {
		list_add_tail(&next->tree_list, &head);
		nitems++;

		curr = next;
		next = __btrfs_next_delayed_item(curr);
		if (!next)
			break;

		if (!btrfs_is_continuous_delayed_item(curr, next))
			break;

		i++;
		if (i > last_item)
			break;
		btrfs_item_key_to_cpu(leaf, &key, i);
	}

	if (!nitems)
		return 0;

	ret = btrfs_del_items(trans, root, path, path->slots[0], nitems);
	if (ret)
		goto out;

	list_for_each_entry_safe(curr, next, &head, tree_list) {
912
		btrfs_delayed_item_release_metadata(root, curr);
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
		list_del(&curr->tree_list);
		btrfs_release_delayed_item(curr);
	}

out:
	return ret;
}

static int btrfs_delete_delayed_items(struct btrfs_trans_handle *trans,
				      struct btrfs_path *path,
				      struct btrfs_root *root,
				      struct btrfs_delayed_node *node)
{
	struct btrfs_delayed_item *curr, *prev;
	int ret = 0;

do_again:
	mutex_lock(&node->mutex);
	curr = __btrfs_first_delayed_deletion_item(node);
	if (!curr)
		goto delete_fail;

	ret = btrfs_search_slot(trans, root, &curr->key, path, -1, 1);
	if (ret < 0)
		goto delete_fail;
	else if (ret > 0) {
		/*
		 * can't find the item which the node points to, so this node
		 * is invalid, just drop it.
		 */
		prev = curr;
		curr = __btrfs_next_delayed_item(prev);
		btrfs_release_delayed_item(prev);
		ret = 0;
947
		btrfs_release_path(path);
948 949
		if (curr) {
			mutex_unlock(&node->mutex);
950
			goto do_again;
951
		} else
952 953 954 955
			goto delete_fail;
	}

	btrfs_batch_delete_items(trans, root, path, curr);
956
	btrfs_release_path(path);
957 958 959 960
	mutex_unlock(&node->mutex);
	goto do_again;

delete_fail:
961
	btrfs_release_path(path);
962 963 964 965 966 967 968 969
	mutex_unlock(&node->mutex);
	return ret;
}

static void btrfs_release_delayed_inode(struct btrfs_delayed_node *delayed_node)
{
	struct btrfs_delayed_root *delayed_root;

970 971
	if (delayed_node &&
	    test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
972
		BUG_ON(!delayed_node->root);
973
		clear_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags);
974 975 976
		delayed_node->count--;

		delayed_root = delayed_node->root->fs_info->delayed_root;
977
		finish_one_item(delayed_root);
978 979 980
	}
}

981 982 983 984 985 986 987 988 989 990 991 992
static void btrfs_release_delayed_iref(struct btrfs_delayed_node *delayed_node)
{
	struct btrfs_delayed_root *delayed_root;

	ASSERT(delayed_node->root);
	clear_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags);
	delayed_node->count--;

	delayed_root = delayed_node->root->fs_info->delayed_root;
	finish_one_item(delayed_root);
}

993 994 995 996
static int __btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
					struct btrfs_root *root,
					struct btrfs_path *path,
					struct btrfs_delayed_node *node)
997
{
998
	struct btrfs_fs_info *fs_info = root->fs_info;
999 1000 1001
	struct btrfs_key key;
	struct btrfs_inode_item *inode_item;
	struct extent_buffer *leaf;
1002
	int mod;
1003 1004 1005
	int ret;

	key.objectid = node->inode_id;
1006
	key.type = BTRFS_INODE_ITEM_KEY;
1007
	key.offset = 0;
1008

1009 1010 1011 1012 1013 1014
	if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags))
		mod = -1;
	else
		mod = 1;

	ret = btrfs_lookup_inode(trans, root, path, &key, mod);
1015
	if (ret > 0) {
1016
		btrfs_release_path(path);
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
		return -ENOENT;
	} else if (ret < 0) {
		return ret;
	}

	leaf = path->nodes[0];
	inode_item = btrfs_item_ptr(leaf, path->slots[0],
				    struct btrfs_inode_item);
	write_extent_buffer(leaf, &node->inode_item, (unsigned long)inode_item,
			    sizeof(struct btrfs_inode_item));
	btrfs_mark_buffer_dirty(leaf);

1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
	if (!test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags))
		goto no_iref;

	path->slots[0]++;
	if (path->slots[0] >= btrfs_header_nritems(leaf))
		goto search;
again:
	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
	if (key.objectid != node->inode_id)
		goto out;

	if (key.type != BTRFS_INODE_REF_KEY &&
	    key.type != BTRFS_INODE_EXTREF_KEY)
		goto out;

	/*
	 * Delayed iref deletion is for the inode who has only one link,
	 * so there is only one iref. The case that several irefs are
	 * in the same item doesn't exist.
	 */
	btrfs_del_item(trans, root, path);
out:
	btrfs_release_delayed_iref(node);
no_iref:
	btrfs_release_path(path);
err_out:
1055
	btrfs_delayed_inode_release_metadata(fs_info, node, (ret < 0));
1056 1057
	btrfs_release_delayed_inode(node);

1058 1059 1060 1061 1062
	return ret;

search:
	btrfs_release_path(path);

1063
	key.type = BTRFS_INODE_EXTREF_KEY;
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
	key.offset = -1;
	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
	if (ret < 0)
		goto err_out;
	ASSERT(ret);

	ret = 0;
	leaf = path->nodes[0];
	path->slots[0]--;
	goto again;
1074 1075
}

1076 1077 1078 1079 1080 1081 1082 1083
static inline int btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
					     struct btrfs_root *root,
					     struct btrfs_path *path,
					     struct btrfs_delayed_node *node)
{
	int ret;

	mutex_lock(&node->mutex);
1084
	if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &node->flags)) {
1085 1086 1087 1088 1089 1090 1091 1092 1093
		mutex_unlock(&node->mutex);
		return 0;
	}

	ret = __btrfs_update_delayed_inode(trans, root, path, node);
	mutex_unlock(&node->mutex);
	return ret;
}

1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
static inline int
__btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
				   struct btrfs_path *path,
				   struct btrfs_delayed_node *node)
{
	int ret;

	ret = btrfs_insert_delayed_items(trans, path, node->root, node);
	if (ret)
		return ret;

	ret = btrfs_delete_delayed_items(trans, path, node->root, node);
	if (ret)
		return ret;

	ret = btrfs_update_delayed_inode(trans, node->root, path, node);
	return ret;
}

1113 1114 1115 1116 1117 1118
/*
 * Called when committing the transaction.
 * Returns 0 on success.
 * Returns < 0 on error and returns with an aborted transaction with any
 * outstanding delayed items cleaned up.
 */
1119
static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans, int nr)
1120
{
1121
	struct btrfs_fs_info *fs_info = trans->fs_info;
1122 1123 1124
	struct btrfs_delayed_root *delayed_root;
	struct btrfs_delayed_node *curr_node, *prev_node;
	struct btrfs_path *path;
1125
	struct btrfs_block_rsv *block_rsv;
1126
	int ret = 0;
1127
	bool count = (nr > 0);
1128

1129 1130 1131
	if (trans->aborted)
		return -EIO;

1132 1133 1134 1135 1136
	path = btrfs_alloc_path();
	if (!path)
		return -ENOMEM;
	path->leave_spinning = 1;

1137
	block_rsv = trans->block_rsv;
1138
	trans->block_rsv = &fs_info->delayed_block_rsv;
1139

1140
	delayed_root = fs_info->delayed_root;
1141 1142

	curr_node = btrfs_first_delayed_node(delayed_root);
1143
	while (curr_node && (!count || (count && nr--))) {
1144 1145
		ret = __btrfs_commit_inode_delayed_items(trans, path,
							 curr_node);
1146 1147
		if (ret) {
			btrfs_release_delayed_node(curr_node);
1148
			curr_node = NULL;
1149
			btrfs_abort_transaction(trans, ret);
1150 1151 1152 1153 1154 1155 1156 1157
			break;
		}

		prev_node = curr_node;
		curr_node = btrfs_next_delayed_node(curr_node);
		btrfs_release_delayed_node(prev_node);
	}

1158 1159
	if (curr_node)
		btrfs_release_delayed_node(curr_node);
1160
	btrfs_free_path(path);
1161
	trans->block_rsv = block_rsv;
1162

1163 1164 1165
	return ret;
}

1166
int btrfs_run_delayed_items(struct btrfs_trans_handle *trans)
1167
{
1168
	return __btrfs_run_delayed_items(trans, -1);
1169 1170
}

1171
int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans, int nr)
1172
{
1173
	return __btrfs_run_delayed_items(trans, nr);
1174 1175
}

1176
int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
1177
				     struct btrfs_inode *inode)
1178
{
1179
	struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
1180 1181
	struct btrfs_path *path;
	struct btrfs_block_rsv *block_rsv;
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
	int ret;

	if (!delayed_node)
		return 0;

	mutex_lock(&delayed_node->mutex);
	if (!delayed_node->count) {
		mutex_unlock(&delayed_node->mutex);
		btrfs_release_delayed_node(delayed_node);
		return 0;
	}
	mutex_unlock(&delayed_node->mutex);

1195
	path = btrfs_alloc_path();
1196 1197
	if (!path) {
		btrfs_release_delayed_node(delayed_node);
1198
		return -ENOMEM;
1199
	}
1200 1201 1202 1203 1204 1205 1206
	path->leave_spinning = 1;

	block_rsv = trans->block_rsv;
	trans->block_rsv = &delayed_node->root->fs_info->delayed_block_rsv;

	ret = __btrfs_commit_inode_delayed_items(trans, path, delayed_node);

1207
	btrfs_release_delayed_node(delayed_node);
1208 1209 1210
	btrfs_free_path(path);
	trans->block_rsv = block_rsv;

1211 1212 1213
	return ret;
}

1214
int btrfs_commit_inode_delayed_inode(struct btrfs_inode *inode)
1215
{
1216
	struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
1217
	struct btrfs_trans_handle *trans;
1218
	struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
1219 1220 1221 1222 1223 1224 1225 1226
	struct btrfs_path *path;
	struct btrfs_block_rsv *block_rsv;
	int ret;

	if (!delayed_node)
		return 0;

	mutex_lock(&delayed_node->mutex);
1227
	if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
		mutex_unlock(&delayed_node->mutex);
		btrfs_release_delayed_node(delayed_node);
		return 0;
	}
	mutex_unlock(&delayed_node->mutex);

	trans = btrfs_join_transaction(delayed_node->root);
	if (IS_ERR(trans)) {
		ret = PTR_ERR(trans);
		goto out;
	}

	path = btrfs_alloc_path();
	if (!path) {
		ret = -ENOMEM;
		goto trans_out;
	}
	path->leave_spinning = 1;

	block_rsv = trans->block_rsv;
1248
	trans->block_rsv = &fs_info->delayed_block_rsv;
1249 1250

	mutex_lock(&delayed_node->mutex);
1251
	if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags))
1252 1253 1254 1255 1256 1257 1258 1259 1260
		ret = __btrfs_update_delayed_inode(trans, delayed_node->root,
						   path, delayed_node);
	else
		ret = 0;
	mutex_unlock(&delayed_node->mutex);

	btrfs_free_path(path);
	trans->block_rsv = block_rsv;
trans_out:
1261
	btrfs_end_transaction(trans);
1262
	btrfs_btree_balance_dirty(fs_info);
1263 1264 1265 1266 1267 1268
out:
	btrfs_release_delayed_node(delayed_node);

	return ret;
}

1269
void btrfs_remove_delayed_node(struct btrfs_inode *inode)
1270 1271 1272
{
	struct btrfs_delayed_node *delayed_node;

1273
	delayed_node = READ_ONCE(inode->delayed_node);
1274 1275 1276
	if (!delayed_node)
		return;

1277
	inode->delayed_node = NULL;
1278 1279 1280
	btrfs_release_delayed_node(delayed_node);
}

1281 1282 1283
struct btrfs_async_delayed_work {
	struct btrfs_delayed_root *delayed_root;
	int nr;
1284
	struct btrfs_work work;
1285 1286
};

1287
static void btrfs_async_run_delayed_root(struct btrfs_work *work)
1288
{
1289 1290
	struct btrfs_async_delayed_work *async_work;
	struct btrfs_delayed_root *delayed_root;
1291 1292 1293 1294
	struct btrfs_trans_handle *trans;
	struct btrfs_path *path;
	struct btrfs_delayed_node *delayed_node = NULL;
	struct btrfs_root *root;
1295
	struct btrfs_block_rsv *block_rsv;
1296
	int total_done = 0;
1297

1298 1299
	async_work = container_of(work, struct btrfs_async_delayed_work, work);
	delayed_root = async_work->delayed_root;
1300 1301 1302 1303 1304

	path = btrfs_alloc_path();
	if (!path)
		goto out;

1305 1306 1307 1308
	do {
		if (atomic_read(&delayed_root->items) <
		    BTRFS_DELAYED_BACKGROUND / 2)
			break;
1309

1310 1311 1312
		delayed_node = btrfs_first_prepared_delayed_node(delayed_root);
		if (!delayed_node)
			break;
1313

1314 1315
		path->leave_spinning = 1;
		root = delayed_node->root;
1316

1317 1318 1319 1320 1321 1322 1323
		trans = btrfs_join_transaction(root);
		if (IS_ERR(trans)) {
			btrfs_release_path(path);
			btrfs_release_prepared_delayed_node(delayed_node);
			total_done++;
			continue;
		}
1324

1325 1326
		block_rsv = trans->block_rsv;
		trans->block_rsv = &root->fs_info->delayed_block_rsv;
1327

1328
		__btrfs_commit_inode_delayed_items(trans, path, delayed_node);
1329

1330 1331 1332
		trans->block_rsv = block_rsv;
		btrfs_end_transaction(trans);
		btrfs_btree_balance_dirty_nodelay(root->fs_info);
1333

1334 1335 1336
		btrfs_release_path(path);
		btrfs_release_prepared_delayed_node(delayed_node);
		total_done++;
1337

1338 1339
	} while ((async_work->nr == 0 && total_done < BTRFS_DELAYED_WRITEBACK)
		 || total_done < async_work->nr);
1340

1341 1342
	btrfs_free_path(path);
out:
1343 1344
	wake_up(&delayed_root->wait);
	kfree(async_work);
1345 1346
}

1347

1348
static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root,
1349
				     struct btrfs_fs_info *fs_info, int nr)
1350
{
1351
	struct btrfs_async_delayed_work *async_work;
1352

1353 1354
	async_work = kmalloc(sizeof(*async_work), GFP_NOFS);
	if (!async_work)
1355 1356
		return -ENOMEM;

1357
	async_work->delayed_root = delayed_root;
1358 1359
	btrfs_init_work(&async_work->work, btrfs_delayed_meta_helper,
			btrfs_async_run_delayed_root, NULL, NULL);
1360
	async_work->nr = nr;
1361

1362
	btrfs_queue_work(fs_info->delayed_workers, &async_work->work);
1363 1364 1365
	return 0;
}

1366
void btrfs_assert_delayed_root_empty(struct btrfs_fs_info *fs_info)
1367
{
1368
	WARN_ON(btrfs_first_delayed_node(fs_info->delayed_root));
1369 1370
}

1371
static int could_end_wait(struct btrfs_delayed_root *delayed_root, int seq)
1372 1373 1374
{
	int val = atomic_read(&delayed_root->items_seq);

1375
	if (val < seq || val >= seq + BTRFS_DELAYED_BATCH)
1376
		return 1;
1377 1378 1379 1380

	if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
		return 1;

1381 1382 1383
	return 0;
}

1384
void btrfs_balance_delayed_items(struct btrfs_fs_info *fs_info)
1385
{
1386
	struct btrfs_delayed_root *delayed_root = fs_info->delayed_root;
1387

1388 1389
	if ((atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND) ||
		btrfs_workqueue_normal_congested(fs_info->delayed_workers))
1390 1391 1392
		return;

	if (atomic_read(&delayed_root->items) >= BTRFS_DELAYED_WRITEBACK) {
1393
		int seq;
1394
		int ret;
1395 1396

		seq = atomic_read(&delayed_root->items_seq);
1397

1398
		ret = btrfs_wq_run_delayed_node(delayed_root, fs_info, 0);
1399 1400 1401
		if (ret)
			return;

1402 1403
		wait_event_interruptible(delayed_root->wait,
					 could_end_wait(delayed_root, seq));
1404
		return;
1405 1406
	}

1407
	btrfs_wq_run_delayed_node(delayed_root, fs_info, BTRFS_DELAYED_BATCH);
1408 1409
}

1410
/* Will return 0 or -ENOMEM */
1411
int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
1412 1413
				   struct btrfs_fs_info *fs_info,
				   const char *name, int name_len,
1414
				   struct btrfs_inode *dir,
1415 1416 1417 1418 1419 1420 1421 1422
				   struct btrfs_disk_key *disk_key, u8 type,
				   u64 index)
{
	struct btrfs_delayed_node *delayed_node;
	struct btrfs_delayed_item *delayed_item;
	struct btrfs_dir_item *dir_item;
	int ret;

1423
	delayed_node = btrfs_get_or_create_delayed_node(dir);
1424 1425 1426 1427 1428 1429 1430 1431 1432
	if (IS_ERR(delayed_node))
		return PTR_ERR(delayed_node);

	delayed_item = btrfs_alloc_delayed_item(sizeof(*dir_item) + name_len);
	if (!delayed_item) {
		ret = -ENOMEM;
		goto release_node;
	}

1433
	delayed_item->key.objectid = btrfs_ino(dir);
1434
	delayed_item->key.type = BTRFS_DIR_INDEX_KEY;
1435 1436 1437 1438
	delayed_item->key.offset = index;

	dir_item = (struct btrfs_dir_item *)delayed_item->data;
	dir_item->location = *disk_key;
1439 1440 1441 1442
	btrfs_set_stack_dir_transid(dir_item, trans->transid);
	btrfs_set_stack_dir_data_len(dir_item, 0);
	btrfs_set_stack_dir_name_len(dir_item, name_len);
	btrfs_set_stack_dir_type(dir_item, type);
1443 1444
	memcpy((char *)(dir_item + 1), name, name_len);

1445
	ret = btrfs_delayed_item_reserve_metadata(trans, dir->root, delayed_item);
1446 1447 1448 1449 1450 1451 1452
	/*
	 * we have reserved enough space when we start a new transaction,
	 * so reserving metadata failure is impossible
	 */
	BUG_ON(ret);


1453 1454 1455
	mutex_lock(&delayed_node->mutex);
	ret = __btrfs_add_delayed_insertion_item(delayed_node, delayed_item);
	if (unlikely(ret)) {
1456
		btrfs_err(fs_info,
1457 1458 1459
			  "err add delayed dir index item(name: %.*s) into the insertion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)",
			  name_len, name, delayed_node->root->objectid,
			  delayed_node->inode_id, ret);
1460 1461 1462 1463 1464 1465 1466 1467 1468
		BUG();
	}
	mutex_unlock(&delayed_node->mutex);

release_node:
	btrfs_release_delayed_node(delayed_node);
	return ret;
}

1469
static int btrfs_delete_delayed_insertion_item(struct btrfs_fs_info *fs_info,
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
					       struct btrfs_delayed_node *node,
					       struct btrfs_key *key)
{
	struct btrfs_delayed_item *item;

	mutex_lock(&node->mutex);
	item = __btrfs_lookup_delayed_insertion_item(node, key);
	if (!item) {
		mutex_unlock(&node->mutex);
		return 1;
	}

1482
	btrfs_delayed_item_release_metadata(node->root, item);
1483 1484 1485 1486 1487 1488
	btrfs_release_delayed_item(item);
	mutex_unlock(&node->mutex);
	return 0;
}

int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
1489
				   struct btrfs_fs_info *fs_info,
1490
				   struct btrfs_inode *dir, u64 index)
1491 1492 1493 1494 1495 1496
{
	struct btrfs_delayed_node *node;
	struct btrfs_delayed_item *item;
	struct btrfs_key item_key;
	int ret;

1497
	node = btrfs_get_or_create_delayed_node(dir);
1498 1499 1500
	if (IS_ERR(node))
		return PTR_ERR(node);

1501
	item_key.objectid = btrfs_ino(dir);
1502
	item_key.type = BTRFS_DIR_INDEX_KEY;
1503 1504
	item_key.offset = index;

1505
	ret = btrfs_delete_delayed_insertion_item(fs_info, node, &item_key);
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
	if (!ret)
		goto end;

	item = btrfs_alloc_delayed_item(0);
	if (!item) {
		ret = -ENOMEM;
		goto end;
	}

	item->key = item_key;

1517
	ret = btrfs_delayed_item_reserve_metadata(trans, dir->root, item);
1518 1519 1520 1521 1522 1523 1524 1525 1526
	/*
	 * we have reserved enough space when we start a new transaction,
	 * so reserving metadata failure is impossible.
	 */
	BUG_ON(ret);

	mutex_lock(&node->mutex);
	ret = __btrfs_add_delayed_deletion_item(node, item);
	if (unlikely(ret)) {
1527
		btrfs_err(fs_info,
1528 1529
			  "err add delayed dir index item(index: %llu) into the deletion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)",
			  index, node->root->objectid, node->inode_id, ret);
1530 1531 1532 1533 1534 1535 1536 1537
		BUG();
	}
	mutex_unlock(&node->mutex);
end:
	btrfs_release_delayed_node(node);
	return ret;
}

1538
int btrfs_inode_delayed_dir_index_count(struct btrfs_inode *inode)
1539
{
1540
	struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
1541 1542 1543 1544 1545 1546 1547 1548 1549

	if (!delayed_node)
		return -ENOENT;

	/*
	 * Since we have held i_mutex of this directory, it is impossible that
	 * a new directory index is added into the delayed node and index_cnt
	 * is updated now. So we needn't lock the delayed node.
	 */
1550 1551
	if (!delayed_node->index_cnt) {
		btrfs_release_delayed_node(delayed_node);
1552
		return -EINVAL;
1553
	}
1554

1555
	inode->index_cnt = delayed_node->index_cnt;
1556 1557
	btrfs_release_delayed_node(delayed_node);
	return 0;
1558 1559
}