162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2007 Oracle.  All rights reserved.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <linux/fs.h>
762306a36Sopenharmony_ci#include <linux/slab.h>
862306a36Sopenharmony_ci#include <linux/sched.h>
962306a36Sopenharmony_ci#include <linux/sched/mm.h>
1062306a36Sopenharmony_ci#include <linux/writeback.h>
1162306a36Sopenharmony_ci#include <linux/pagemap.h>
1262306a36Sopenharmony_ci#include <linux/blkdev.h>
1362306a36Sopenharmony_ci#include <linux/uuid.h>
1462306a36Sopenharmony_ci#include <linux/timekeeping.h>
1562306a36Sopenharmony_ci#include "misc.h"
1662306a36Sopenharmony_ci#include "ctree.h"
1762306a36Sopenharmony_ci#include "disk-io.h"
1862306a36Sopenharmony_ci#include "transaction.h"
1962306a36Sopenharmony_ci#include "locking.h"
2062306a36Sopenharmony_ci#include "tree-log.h"
2162306a36Sopenharmony_ci#include "volumes.h"
2262306a36Sopenharmony_ci#include "dev-replace.h"
2362306a36Sopenharmony_ci#include "qgroup.h"
2462306a36Sopenharmony_ci#include "block-group.h"
2562306a36Sopenharmony_ci#include "space-info.h"
2662306a36Sopenharmony_ci#include "zoned.h"
2762306a36Sopenharmony_ci#include "fs.h"
2862306a36Sopenharmony_ci#include "accessors.h"
2962306a36Sopenharmony_ci#include "extent-tree.h"
3062306a36Sopenharmony_ci#include "root-tree.h"
3162306a36Sopenharmony_ci#include "defrag.h"
3262306a36Sopenharmony_ci#include "dir-item.h"
3362306a36Sopenharmony_ci#include "uuid-tree.h"
3462306a36Sopenharmony_ci#include "ioctl.h"
3562306a36Sopenharmony_ci#include "relocation.h"
3662306a36Sopenharmony_ci#include "scrub.h"
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_cistatic struct kmem_cache *btrfs_trans_handle_cachep;
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci/*
4162306a36Sopenharmony_ci * Transaction states and transitions
4262306a36Sopenharmony_ci *
4362306a36Sopenharmony_ci * No running transaction (fs tree blocks are not modified)
4462306a36Sopenharmony_ci * |
4562306a36Sopenharmony_ci * | To next stage:
4662306a36Sopenharmony_ci * |  Call start_transaction() variants. Except btrfs_join_transaction_nostart().
4762306a36Sopenharmony_ci * V
4862306a36Sopenharmony_ci * Transaction N [[TRANS_STATE_RUNNING]]
4962306a36Sopenharmony_ci * |
5062306a36Sopenharmony_ci * | New trans handles can be attached to transaction N by calling all
5162306a36Sopenharmony_ci * | start_transaction() variants.
5262306a36Sopenharmony_ci * |
5362306a36Sopenharmony_ci * | To next stage:
5462306a36Sopenharmony_ci * |  Call btrfs_commit_transaction() on any trans handle attached to
5562306a36Sopenharmony_ci * |  transaction N
5662306a36Sopenharmony_ci * V
5762306a36Sopenharmony_ci * Transaction N [[TRANS_STATE_COMMIT_PREP]]
5862306a36Sopenharmony_ci * |
5962306a36Sopenharmony_ci * | If there are simultaneous calls to btrfs_commit_transaction() one will win
6062306a36Sopenharmony_ci * | the race and the rest will wait for the winner to commit the transaction.
6162306a36Sopenharmony_ci * |
6262306a36Sopenharmony_ci * | The winner will wait for previous running transaction to completely finish
6362306a36Sopenharmony_ci * | if there is one.
6462306a36Sopenharmony_ci * |
6562306a36Sopenharmony_ci * Transaction N [[TRANS_STATE_COMMIT_START]]
6662306a36Sopenharmony_ci * |
6762306a36Sopenharmony_ci * | Then one of the following happens:
6862306a36Sopenharmony_ci * | - Wait for all other trans handle holders to release.
6962306a36Sopenharmony_ci * |   The btrfs_commit_transaction() caller will do the commit work.
7062306a36Sopenharmony_ci * | - Wait for current transaction to be committed by others.
7162306a36Sopenharmony_ci * |   Other btrfs_commit_transaction() caller will do the commit work.
7262306a36Sopenharmony_ci * |
7362306a36Sopenharmony_ci * | At this stage, only btrfs_join_transaction*() variants can attach
7462306a36Sopenharmony_ci * | to this running transaction.
7562306a36Sopenharmony_ci * | All other variants will wait for current one to finish and attach to
7662306a36Sopenharmony_ci * | transaction N+1.
7762306a36Sopenharmony_ci * |
7862306a36Sopenharmony_ci * | To next stage:
7962306a36Sopenharmony_ci * |  Caller is chosen to commit transaction N, and all other trans handle
8062306a36Sopenharmony_ci * |  haven been released.
8162306a36Sopenharmony_ci * V
8262306a36Sopenharmony_ci * Transaction N [[TRANS_STATE_COMMIT_DOING]]
8362306a36Sopenharmony_ci * |
8462306a36Sopenharmony_ci * | The heavy lifting transaction work is started.
8562306a36Sopenharmony_ci * | From running delayed refs (modifying extent tree) to creating pending
8662306a36Sopenharmony_ci * | snapshots, running qgroups.
8762306a36Sopenharmony_ci * | In short, modify supporting trees to reflect modifications of subvolume
8862306a36Sopenharmony_ci * | trees.
8962306a36Sopenharmony_ci * |
9062306a36Sopenharmony_ci * | At this stage, all start_transaction() calls will wait for this
9162306a36Sopenharmony_ci * | transaction to finish and attach to transaction N+1.
9262306a36Sopenharmony_ci * |
9362306a36Sopenharmony_ci * | To next stage:
9462306a36Sopenharmony_ci * |  Until all supporting trees are updated.
9562306a36Sopenharmony_ci * V
9662306a36Sopenharmony_ci * Transaction N [[TRANS_STATE_UNBLOCKED]]
9762306a36Sopenharmony_ci * |						    Transaction N+1
9862306a36Sopenharmony_ci * | All needed trees are modified, thus we only    [[TRANS_STATE_RUNNING]]
9962306a36Sopenharmony_ci * | need to write them back to disk and update	    |
10062306a36Sopenharmony_ci * | super blocks.				    |
10162306a36Sopenharmony_ci * |						    |
10262306a36Sopenharmony_ci * | At this stage, new transaction is allowed to   |
10362306a36Sopenharmony_ci * | start.					    |
10462306a36Sopenharmony_ci * | All new start_transaction() calls will be	    |
10562306a36Sopenharmony_ci * | attached to transid N+1.			    |
10662306a36Sopenharmony_ci * |						    |
10762306a36Sopenharmony_ci * | To next stage:				    |
10862306a36Sopenharmony_ci * |  Until all tree blocks are super blocks are    |
10962306a36Sopenharmony_ci * |  written to block devices			    |
11062306a36Sopenharmony_ci * V						    |
11162306a36Sopenharmony_ci * Transaction N [[TRANS_STATE_COMPLETED]]	    V
11262306a36Sopenharmony_ci *   All tree blocks and super blocks are written.  Transaction N+1
11362306a36Sopenharmony_ci *   This transaction is finished and all its	    [[TRANS_STATE_COMMIT_START]]
11462306a36Sopenharmony_ci *   data structures will be cleaned up.	    | Life goes on
11562306a36Sopenharmony_ci */
11662306a36Sopenharmony_cistatic const unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = {
11762306a36Sopenharmony_ci	[TRANS_STATE_RUNNING]		= 0U,
11862306a36Sopenharmony_ci	[TRANS_STATE_COMMIT_PREP]	= 0U,
11962306a36Sopenharmony_ci	[TRANS_STATE_COMMIT_START]	= (__TRANS_START | __TRANS_ATTACH),
12062306a36Sopenharmony_ci	[TRANS_STATE_COMMIT_DOING]	= (__TRANS_START |
12162306a36Sopenharmony_ci					   __TRANS_ATTACH |
12262306a36Sopenharmony_ci					   __TRANS_JOIN |
12362306a36Sopenharmony_ci					   __TRANS_JOIN_NOSTART),
12462306a36Sopenharmony_ci	[TRANS_STATE_UNBLOCKED]		= (__TRANS_START |
12562306a36Sopenharmony_ci					   __TRANS_ATTACH |
12662306a36Sopenharmony_ci					   __TRANS_JOIN |
12762306a36Sopenharmony_ci					   __TRANS_JOIN_NOLOCK |
12862306a36Sopenharmony_ci					   __TRANS_JOIN_NOSTART),
12962306a36Sopenharmony_ci	[TRANS_STATE_SUPER_COMMITTED]	= (__TRANS_START |
13062306a36Sopenharmony_ci					   __TRANS_ATTACH |
13162306a36Sopenharmony_ci					   __TRANS_JOIN |
13262306a36Sopenharmony_ci					   __TRANS_JOIN_NOLOCK |
13362306a36Sopenharmony_ci					   __TRANS_JOIN_NOSTART),
13462306a36Sopenharmony_ci	[TRANS_STATE_COMPLETED]		= (__TRANS_START |
13562306a36Sopenharmony_ci					   __TRANS_ATTACH |
13662306a36Sopenharmony_ci					   __TRANS_JOIN |
13762306a36Sopenharmony_ci					   __TRANS_JOIN_NOLOCK |
13862306a36Sopenharmony_ci					   __TRANS_JOIN_NOSTART),
13962306a36Sopenharmony_ci};
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_civoid btrfs_put_transaction(struct btrfs_transaction *transaction)
14262306a36Sopenharmony_ci{
14362306a36Sopenharmony_ci	WARN_ON(refcount_read(&transaction->use_count) == 0);
14462306a36Sopenharmony_ci	if (refcount_dec_and_test(&transaction->use_count)) {
14562306a36Sopenharmony_ci		BUG_ON(!list_empty(&transaction->list));
14662306a36Sopenharmony_ci		WARN_ON(!RB_EMPTY_ROOT(
14762306a36Sopenharmony_ci				&transaction->delayed_refs.href_root.rb_root));
14862306a36Sopenharmony_ci		WARN_ON(!RB_EMPTY_ROOT(
14962306a36Sopenharmony_ci				&transaction->delayed_refs.dirty_extent_root));
15062306a36Sopenharmony_ci		if (transaction->delayed_refs.pending_csums)
15162306a36Sopenharmony_ci			btrfs_err(transaction->fs_info,
15262306a36Sopenharmony_ci				  "pending csums is %llu",
15362306a36Sopenharmony_ci				  transaction->delayed_refs.pending_csums);
15462306a36Sopenharmony_ci		/*
15562306a36Sopenharmony_ci		 * If any block groups are found in ->deleted_bgs then it's
15662306a36Sopenharmony_ci		 * because the transaction was aborted and a commit did not
15762306a36Sopenharmony_ci		 * happen (things failed before writing the new superblock
15862306a36Sopenharmony_ci		 * and calling btrfs_finish_extent_commit()), so we can not
15962306a36Sopenharmony_ci		 * discard the physical locations of the block groups.
16062306a36Sopenharmony_ci		 */
16162306a36Sopenharmony_ci		while (!list_empty(&transaction->deleted_bgs)) {
16262306a36Sopenharmony_ci			struct btrfs_block_group *cache;
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci			cache = list_first_entry(&transaction->deleted_bgs,
16562306a36Sopenharmony_ci						 struct btrfs_block_group,
16662306a36Sopenharmony_ci						 bg_list);
16762306a36Sopenharmony_ci			list_del_init(&cache->bg_list);
16862306a36Sopenharmony_ci			btrfs_unfreeze_block_group(cache);
16962306a36Sopenharmony_ci			btrfs_put_block_group(cache);
17062306a36Sopenharmony_ci		}
17162306a36Sopenharmony_ci		WARN_ON(!list_empty(&transaction->dev_update_list));
17262306a36Sopenharmony_ci		kfree(transaction);
17362306a36Sopenharmony_ci	}
17462306a36Sopenharmony_ci}
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_cistatic noinline void switch_commit_roots(struct btrfs_trans_handle *trans)
17762306a36Sopenharmony_ci{
17862306a36Sopenharmony_ci	struct btrfs_transaction *cur_trans = trans->transaction;
17962306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
18062306a36Sopenharmony_ci	struct btrfs_root *root, *tmp;
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci	/*
18362306a36Sopenharmony_ci	 * At this point no one can be using this transaction to modify any tree
18462306a36Sopenharmony_ci	 * and no one can start another transaction to modify any tree either.
18562306a36Sopenharmony_ci	 */
18662306a36Sopenharmony_ci	ASSERT(cur_trans->state == TRANS_STATE_COMMIT_DOING);
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci	down_write(&fs_info->commit_root_sem);
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci	if (test_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags))
19162306a36Sopenharmony_ci		fs_info->last_reloc_trans = trans->transid;
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci	list_for_each_entry_safe(root, tmp, &cur_trans->switch_commits,
19462306a36Sopenharmony_ci				 dirty_list) {
19562306a36Sopenharmony_ci		list_del_init(&root->dirty_list);
19662306a36Sopenharmony_ci		free_extent_buffer(root->commit_root);
19762306a36Sopenharmony_ci		root->commit_root = btrfs_root_node(root);
19862306a36Sopenharmony_ci		extent_io_tree_release(&root->dirty_log_pages);
19962306a36Sopenharmony_ci		btrfs_qgroup_clean_swapped_blocks(root);
20062306a36Sopenharmony_ci	}
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci	/* We can free old roots now. */
20362306a36Sopenharmony_ci	spin_lock(&cur_trans->dropped_roots_lock);
20462306a36Sopenharmony_ci	while (!list_empty(&cur_trans->dropped_roots)) {
20562306a36Sopenharmony_ci		root = list_first_entry(&cur_trans->dropped_roots,
20662306a36Sopenharmony_ci					struct btrfs_root, root_list);
20762306a36Sopenharmony_ci		list_del_init(&root->root_list);
20862306a36Sopenharmony_ci		spin_unlock(&cur_trans->dropped_roots_lock);
20962306a36Sopenharmony_ci		btrfs_free_log(trans, root);
21062306a36Sopenharmony_ci		btrfs_drop_and_free_fs_root(fs_info, root);
21162306a36Sopenharmony_ci		spin_lock(&cur_trans->dropped_roots_lock);
21262306a36Sopenharmony_ci	}
21362306a36Sopenharmony_ci	spin_unlock(&cur_trans->dropped_roots_lock);
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci	up_write(&fs_info->commit_root_sem);
21662306a36Sopenharmony_ci}
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_cistatic inline void extwriter_counter_inc(struct btrfs_transaction *trans,
21962306a36Sopenharmony_ci					 unsigned int type)
22062306a36Sopenharmony_ci{
22162306a36Sopenharmony_ci	if (type & TRANS_EXTWRITERS)
22262306a36Sopenharmony_ci		atomic_inc(&trans->num_extwriters);
22362306a36Sopenharmony_ci}
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_cistatic inline void extwriter_counter_dec(struct btrfs_transaction *trans,
22662306a36Sopenharmony_ci					 unsigned int type)
22762306a36Sopenharmony_ci{
22862306a36Sopenharmony_ci	if (type & TRANS_EXTWRITERS)
22962306a36Sopenharmony_ci		atomic_dec(&trans->num_extwriters);
23062306a36Sopenharmony_ci}
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_cistatic inline void extwriter_counter_init(struct btrfs_transaction *trans,
23362306a36Sopenharmony_ci					  unsigned int type)
23462306a36Sopenharmony_ci{
23562306a36Sopenharmony_ci	atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0));
23662306a36Sopenharmony_ci}
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_cistatic inline int extwriter_counter_read(struct btrfs_transaction *trans)
23962306a36Sopenharmony_ci{
24062306a36Sopenharmony_ci	return atomic_read(&trans->num_extwriters);
24162306a36Sopenharmony_ci}
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_ci/*
24462306a36Sopenharmony_ci * To be called after doing the chunk btree updates right after allocating a new
24562306a36Sopenharmony_ci * chunk (after btrfs_chunk_alloc_add_chunk_item() is called), when removing a
24662306a36Sopenharmony_ci * chunk after all chunk btree updates and after finishing the second phase of
24762306a36Sopenharmony_ci * chunk allocation (btrfs_create_pending_block_groups()) in case some block
24862306a36Sopenharmony_ci * group had its chunk item insertion delayed to the second phase.
24962306a36Sopenharmony_ci */
25062306a36Sopenharmony_civoid btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans)
25162306a36Sopenharmony_ci{
25262306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci	if (!trans->chunk_bytes_reserved)
25562306a36Sopenharmony_ci		return;
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_ci	btrfs_block_rsv_release(fs_info, &fs_info->chunk_block_rsv,
25862306a36Sopenharmony_ci				trans->chunk_bytes_reserved, NULL);
25962306a36Sopenharmony_ci	trans->chunk_bytes_reserved = 0;
26062306a36Sopenharmony_ci}
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ci/*
26362306a36Sopenharmony_ci * either allocate a new transaction or hop into the existing one
26462306a36Sopenharmony_ci */
26562306a36Sopenharmony_cistatic noinline int join_transaction(struct btrfs_fs_info *fs_info,
26662306a36Sopenharmony_ci				     unsigned int type)
26762306a36Sopenharmony_ci{
26862306a36Sopenharmony_ci	struct btrfs_transaction *cur_trans;
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci	spin_lock(&fs_info->trans_lock);
27162306a36Sopenharmony_ciloop:
27262306a36Sopenharmony_ci	/* The file system has been taken offline. No new transactions. */
27362306a36Sopenharmony_ci	if (BTRFS_FS_ERROR(fs_info)) {
27462306a36Sopenharmony_ci		spin_unlock(&fs_info->trans_lock);
27562306a36Sopenharmony_ci		return -EROFS;
27662306a36Sopenharmony_ci	}
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci	cur_trans = fs_info->running_transaction;
27962306a36Sopenharmony_ci	if (cur_trans) {
28062306a36Sopenharmony_ci		if (TRANS_ABORTED(cur_trans)) {
28162306a36Sopenharmony_ci			spin_unlock(&fs_info->trans_lock);
28262306a36Sopenharmony_ci			return cur_trans->aborted;
28362306a36Sopenharmony_ci		}
28462306a36Sopenharmony_ci		if (btrfs_blocked_trans_types[cur_trans->state] & type) {
28562306a36Sopenharmony_ci			spin_unlock(&fs_info->trans_lock);
28662306a36Sopenharmony_ci			return -EBUSY;
28762306a36Sopenharmony_ci		}
28862306a36Sopenharmony_ci		refcount_inc(&cur_trans->use_count);
28962306a36Sopenharmony_ci		atomic_inc(&cur_trans->num_writers);
29062306a36Sopenharmony_ci		extwriter_counter_inc(cur_trans, type);
29162306a36Sopenharmony_ci		spin_unlock(&fs_info->trans_lock);
29262306a36Sopenharmony_ci		btrfs_lockdep_acquire(fs_info, btrfs_trans_num_writers);
29362306a36Sopenharmony_ci		btrfs_lockdep_acquire(fs_info, btrfs_trans_num_extwriters);
29462306a36Sopenharmony_ci		return 0;
29562306a36Sopenharmony_ci	}
29662306a36Sopenharmony_ci	spin_unlock(&fs_info->trans_lock);
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_ci	/*
29962306a36Sopenharmony_ci	 * If we are ATTACH or TRANS_JOIN_NOSTART, we just want to catch the
30062306a36Sopenharmony_ci	 * current transaction, and commit it. If there is no transaction, just
30162306a36Sopenharmony_ci	 * return ENOENT.
30262306a36Sopenharmony_ci	 */
30362306a36Sopenharmony_ci	if (type == TRANS_ATTACH || type == TRANS_JOIN_NOSTART)
30462306a36Sopenharmony_ci		return -ENOENT;
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_ci	/*
30762306a36Sopenharmony_ci	 * JOIN_NOLOCK only happens during the transaction commit, so
30862306a36Sopenharmony_ci	 * it is impossible that ->running_transaction is NULL
30962306a36Sopenharmony_ci	 */
31062306a36Sopenharmony_ci	BUG_ON(type == TRANS_JOIN_NOLOCK);
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_ci	cur_trans = kmalloc(sizeof(*cur_trans), GFP_NOFS);
31362306a36Sopenharmony_ci	if (!cur_trans)
31462306a36Sopenharmony_ci		return -ENOMEM;
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_ci	btrfs_lockdep_acquire(fs_info, btrfs_trans_num_writers);
31762306a36Sopenharmony_ci	btrfs_lockdep_acquire(fs_info, btrfs_trans_num_extwriters);
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_ci	spin_lock(&fs_info->trans_lock);
32062306a36Sopenharmony_ci	if (fs_info->running_transaction) {
32162306a36Sopenharmony_ci		/*
32262306a36Sopenharmony_ci		 * someone started a transaction after we unlocked.  Make sure
32362306a36Sopenharmony_ci		 * to redo the checks above
32462306a36Sopenharmony_ci		 */
32562306a36Sopenharmony_ci		btrfs_lockdep_release(fs_info, btrfs_trans_num_extwriters);
32662306a36Sopenharmony_ci		btrfs_lockdep_release(fs_info, btrfs_trans_num_writers);
32762306a36Sopenharmony_ci		kfree(cur_trans);
32862306a36Sopenharmony_ci		goto loop;
32962306a36Sopenharmony_ci	} else if (BTRFS_FS_ERROR(fs_info)) {
33062306a36Sopenharmony_ci		spin_unlock(&fs_info->trans_lock);
33162306a36Sopenharmony_ci		btrfs_lockdep_release(fs_info, btrfs_trans_num_extwriters);
33262306a36Sopenharmony_ci		btrfs_lockdep_release(fs_info, btrfs_trans_num_writers);
33362306a36Sopenharmony_ci		kfree(cur_trans);
33462306a36Sopenharmony_ci		return -EROFS;
33562306a36Sopenharmony_ci	}
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_ci	cur_trans->fs_info = fs_info;
33862306a36Sopenharmony_ci	atomic_set(&cur_trans->pending_ordered, 0);
33962306a36Sopenharmony_ci	init_waitqueue_head(&cur_trans->pending_wait);
34062306a36Sopenharmony_ci	atomic_set(&cur_trans->num_writers, 1);
34162306a36Sopenharmony_ci	extwriter_counter_init(cur_trans, type);
34262306a36Sopenharmony_ci	init_waitqueue_head(&cur_trans->writer_wait);
34362306a36Sopenharmony_ci	init_waitqueue_head(&cur_trans->commit_wait);
34462306a36Sopenharmony_ci	cur_trans->state = TRANS_STATE_RUNNING;
34562306a36Sopenharmony_ci	/*
34662306a36Sopenharmony_ci	 * One for this trans handle, one so it will live on until we
34762306a36Sopenharmony_ci	 * commit the transaction.
34862306a36Sopenharmony_ci	 */
34962306a36Sopenharmony_ci	refcount_set(&cur_trans->use_count, 2);
35062306a36Sopenharmony_ci	cur_trans->flags = 0;
35162306a36Sopenharmony_ci	cur_trans->start_time = ktime_get_seconds();
35262306a36Sopenharmony_ci
35362306a36Sopenharmony_ci	memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs));
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ci	cur_trans->delayed_refs.href_root = RB_ROOT_CACHED;
35662306a36Sopenharmony_ci	cur_trans->delayed_refs.dirty_extent_root = RB_ROOT;
35762306a36Sopenharmony_ci	atomic_set(&cur_trans->delayed_refs.num_entries, 0);
35862306a36Sopenharmony_ci
35962306a36Sopenharmony_ci	/*
36062306a36Sopenharmony_ci	 * although the tree mod log is per file system and not per transaction,
36162306a36Sopenharmony_ci	 * the log must never go across transaction boundaries.
36262306a36Sopenharmony_ci	 */
36362306a36Sopenharmony_ci	smp_mb();
36462306a36Sopenharmony_ci	if (!list_empty(&fs_info->tree_mod_seq_list))
36562306a36Sopenharmony_ci		WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when creating a fresh transaction\n");
36662306a36Sopenharmony_ci	if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
36762306a36Sopenharmony_ci		WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when creating a fresh transaction\n");
36862306a36Sopenharmony_ci	atomic64_set(&fs_info->tree_mod_seq, 0);
36962306a36Sopenharmony_ci
37062306a36Sopenharmony_ci	spin_lock_init(&cur_trans->delayed_refs.lock);
37162306a36Sopenharmony_ci
37262306a36Sopenharmony_ci	INIT_LIST_HEAD(&cur_trans->pending_snapshots);
37362306a36Sopenharmony_ci	INIT_LIST_HEAD(&cur_trans->dev_update_list);
37462306a36Sopenharmony_ci	INIT_LIST_HEAD(&cur_trans->switch_commits);
37562306a36Sopenharmony_ci	INIT_LIST_HEAD(&cur_trans->dirty_bgs);
37662306a36Sopenharmony_ci	INIT_LIST_HEAD(&cur_trans->io_bgs);
37762306a36Sopenharmony_ci	INIT_LIST_HEAD(&cur_trans->dropped_roots);
37862306a36Sopenharmony_ci	mutex_init(&cur_trans->cache_write_mutex);
37962306a36Sopenharmony_ci	spin_lock_init(&cur_trans->dirty_bgs_lock);
38062306a36Sopenharmony_ci	INIT_LIST_HEAD(&cur_trans->deleted_bgs);
38162306a36Sopenharmony_ci	spin_lock_init(&cur_trans->dropped_roots_lock);
38262306a36Sopenharmony_ci	list_add_tail(&cur_trans->list, &fs_info->trans_list);
38362306a36Sopenharmony_ci	extent_io_tree_init(fs_info, &cur_trans->dirty_pages,
38462306a36Sopenharmony_ci			IO_TREE_TRANS_DIRTY_PAGES);
38562306a36Sopenharmony_ci	extent_io_tree_init(fs_info, &cur_trans->pinned_extents,
38662306a36Sopenharmony_ci			IO_TREE_FS_PINNED_EXTENTS);
38762306a36Sopenharmony_ci	fs_info->generation++;
38862306a36Sopenharmony_ci	cur_trans->transid = fs_info->generation;
38962306a36Sopenharmony_ci	fs_info->running_transaction = cur_trans;
39062306a36Sopenharmony_ci	cur_trans->aborted = 0;
39162306a36Sopenharmony_ci	spin_unlock(&fs_info->trans_lock);
39262306a36Sopenharmony_ci
39362306a36Sopenharmony_ci	return 0;
39462306a36Sopenharmony_ci}
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_ci/*
39762306a36Sopenharmony_ci * This does all the record keeping required to make sure that a shareable root
39862306a36Sopenharmony_ci * is properly recorded in a given transaction.  This is required to make sure
39962306a36Sopenharmony_ci * the old root from before we joined the transaction is deleted when the
40062306a36Sopenharmony_ci * transaction commits.
40162306a36Sopenharmony_ci */
40262306a36Sopenharmony_cistatic int record_root_in_trans(struct btrfs_trans_handle *trans,
40362306a36Sopenharmony_ci			       struct btrfs_root *root,
40462306a36Sopenharmony_ci			       int force)
40562306a36Sopenharmony_ci{
40662306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = root->fs_info;
40762306a36Sopenharmony_ci	int ret = 0;
40862306a36Sopenharmony_ci
40962306a36Sopenharmony_ci	if ((test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
41062306a36Sopenharmony_ci	    root->last_trans < trans->transid) || force) {
41162306a36Sopenharmony_ci		WARN_ON(!force && root->commit_root != root->node);
41262306a36Sopenharmony_ci
41362306a36Sopenharmony_ci		/*
41462306a36Sopenharmony_ci		 * see below for IN_TRANS_SETUP usage rules
41562306a36Sopenharmony_ci		 * we have the reloc mutex held now, so there
41662306a36Sopenharmony_ci		 * is only one writer in this function
41762306a36Sopenharmony_ci		 */
41862306a36Sopenharmony_ci		set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
41962306a36Sopenharmony_ci
42062306a36Sopenharmony_ci		/* make sure readers find IN_TRANS_SETUP before
42162306a36Sopenharmony_ci		 * they find our root->last_trans update
42262306a36Sopenharmony_ci		 */
42362306a36Sopenharmony_ci		smp_wmb();
42462306a36Sopenharmony_ci
42562306a36Sopenharmony_ci		spin_lock(&fs_info->fs_roots_radix_lock);
42662306a36Sopenharmony_ci		if (root->last_trans == trans->transid && !force) {
42762306a36Sopenharmony_ci			spin_unlock(&fs_info->fs_roots_radix_lock);
42862306a36Sopenharmony_ci			return 0;
42962306a36Sopenharmony_ci		}
43062306a36Sopenharmony_ci		radix_tree_tag_set(&fs_info->fs_roots_radix,
43162306a36Sopenharmony_ci				   (unsigned long)root->root_key.objectid,
43262306a36Sopenharmony_ci				   BTRFS_ROOT_TRANS_TAG);
43362306a36Sopenharmony_ci		spin_unlock(&fs_info->fs_roots_radix_lock);
43462306a36Sopenharmony_ci		root->last_trans = trans->transid;
43562306a36Sopenharmony_ci
43662306a36Sopenharmony_ci		/* this is pretty tricky.  We don't want to
43762306a36Sopenharmony_ci		 * take the relocation lock in btrfs_record_root_in_trans
43862306a36Sopenharmony_ci		 * unless we're really doing the first setup for this root in
43962306a36Sopenharmony_ci		 * this transaction.
44062306a36Sopenharmony_ci		 *
44162306a36Sopenharmony_ci		 * Normally we'd use root->last_trans as a flag to decide
44262306a36Sopenharmony_ci		 * if we want to take the expensive mutex.
44362306a36Sopenharmony_ci		 *
44462306a36Sopenharmony_ci		 * But, we have to set root->last_trans before we
44562306a36Sopenharmony_ci		 * init the relocation root, otherwise, we trip over warnings
44662306a36Sopenharmony_ci		 * in ctree.c.  The solution used here is to flag ourselves
44762306a36Sopenharmony_ci		 * with root IN_TRANS_SETUP.  When this is 1, we're still
44862306a36Sopenharmony_ci		 * fixing up the reloc trees and everyone must wait.
44962306a36Sopenharmony_ci		 *
45062306a36Sopenharmony_ci		 * When this is zero, they can trust root->last_trans and fly
45162306a36Sopenharmony_ci		 * through btrfs_record_root_in_trans without having to take the
45262306a36Sopenharmony_ci		 * lock.  smp_wmb() makes sure that all the writes above are
45362306a36Sopenharmony_ci		 * done before we pop in the zero below
45462306a36Sopenharmony_ci		 */
45562306a36Sopenharmony_ci		ret = btrfs_init_reloc_root(trans, root);
45662306a36Sopenharmony_ci		smp_mb__before_atomic();
45762306a36Sopenharmony_ci		clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
45862306a36Sopenharmony_ci	}
45962306a36Sopenharmony_ci	return ret;
46062306a36Sopenharmony_ci}
46162306a36Sopenharmony_ci
46262306a36Sopenharmony_ci
46362306a36Sopenharmony_civoid btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
46462306a36Sopenharmony_ci			    struct btrfs_root *root)
46562306a36Sopenharmony_ci{
46662306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = root->fs_info;
46762306a36Sopenharmony_ci	struct btrfs_transaction *cur_trans = trans->transaction;
46862306a36Sopenharmony_ci
46962306a36Sopenharmony_ci	/* Add ourselves to the transaction dropped list */
47062306a36Sopenharmony_ci	spin_lock(&cur_trans->dropped_roots_lock);
47162306a36Sopenharmony_ci	list_add_tail(&root->root_list, &cur_trans->dropped_roots);
47262306a36Sopenharmony_ci	spin_unlock(&cur_trans->dropped_roots_lock);
47362306a36Sopenharmony_ci
47462306a36Sopenharmony_ci	/* Make sure we don't try to update the root at commit time */
47562306a36Sopenharmony_ci	spin_lock(&fs_info->fs_roots_radix_lock);
47662306a36Sopenharmony_ci	radix_tree_tag_clear(&fs_info->fs_roots_radix,
47762306a36Sopenharmony_ci			     (unsigned long)root->root_key.objectid,
47862306a36Sopenharmony_ci			     BTRFS_ROOT_TRANS_TAG);
47962306a36Sopenharmony_ci	spin_unlock(&fs_info->fs_roots_radix_lock);
48062306a36Sopenharmony_ci}
48162306a36Sopenharmony_ci
48262306a36Sopenharmony_ciint btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
48362306a36Sopenharmony_ci			       struct btrfs_root *root)
48462306a36Sopenharmony_ci{
48562306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = root->fs_info;
48662306a36Sopenharmony_ci	int ret;
48762306a36Sopenharmony_ci
48862306a36Sopenharmony_ci	if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
48962306a36Sopenharmony_ci		return 0;
49062306a36Sopenharmony_ci
49162306a36Sopenharmony_ci	/*
49262306a36Sopenharmony_ci	 * see record_root_in_trans for comments about IN_TRANS_SETUP usage
49362306a36Sopenharmony_ci	 * and barriers
49462306a36Sopenharmony_ci	 */
49562306a36Sopenharmony_ci	smp_rmb();
49662306a36Sopenharmony_ci	if (root->last_trans == trans->transid &&
49762306a36Sopenharmony_ci	    !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state))
49862306a36Sopenharmony_ci		return 0;
49962306a36Sopenharmony_ci
50062306a36Sopenharmony_ci	mutex_lock(&fs_info->reloc_mutex);
50162306a36Sopenharmony_ci	ret = record_root_in_trans(trans, root, 0);
50262306a36Sopenharmony_ci	mutex_unlock(&fs_info->reloc_mutex);
50362306a36Sopenharmony_ci
50462306a36Sopenharmony_ci	return ret;
50562306a36Sopenharmony_ci}
50662306a36Sopenharmony_ci
50762306a36Sopenharmony_cistatic inline int is_transaction_blocked(struct btrfs_transaction *trans)
50862306a36Sopenharmony_ci{
50962306a36Sopenharmony_ci	return (trans->state >= TRANS_STATE_COMMIT_START &&
51062306a36Sopenharmony_ci		trans->state < TRANS_STATE_UNBLOCKED &&
51162306a36Sopenharmony_ci		!TRANS_ABORTED(trans));
51262306a36Sopenharmony_ci}
51362306a36Sopenharmony_ci
51462306a36Sopenharmony_ci/* wait for commit against the current transaction to become unblocked
51562306a36Sopenharmony_ci * when this is done, it is safe to start a new transaction, but the current
51662306a36Sopenharmony_ci * transaction might not be fully on disk.
51762306a36Sopenharmony_ci */
51862306a36Sopenharmony_cistatic void wait_current_trans(struct btrfs_fs_info *fs_info)
51962306a36Sopenharmony_ci{
52062306a36Sopenharmony_ci	struct btrfs_transaction *cur_trans;
52162306a36Sopenharmony_ci
52262306a36Sopenharmony_ci	spin_lock(&fs_info->trans_lock);
52362306a36Sopenharmony_ci	cur_trans = fs_info->running_transaction;
52462306a36Sopenharmony_ci	if (cur_trans && is_transaction_blocked(cur_trans)) {
52562306a36Sopenharmony_ci		refcount_inc(&cur_trans->use_count);
52662306a36Sopenharmony_ci		spin_unlock(&fs_info->trans_lock);
52762306a36Sopenharmony_ci
52862306a36Sopenharmony_ci		btrfs_might_wait_for_state(fs_info, BTRFS_LOCKDEP_TRANS_UNBLOCKED);
52962306a36Sopenharmony_ci		wait_event(fs_info->transaction_wait,
53062306a36Sopenharmony_ci			   cur_trans->state >= TRANS_STATE_UNBLOCKED ||
53162306a36Sopenharmony_ci			   TRANS_ABORTED(cur_trans));
53262306a36Sopenharmony_ci		btrfs_put_transaction(cur_trans);
53362306a36Sopenharmony_ci	} else {
53462306a36Sopenharmony_ci		spin_unlock(&fs_info->trans_lock);
53562306a36Sopenharmony_ci	}
53662306a36Sopenharmony_ci}
53762306a36Sopenharmony_ci
53862306a36Sopenharmony_cistatic int may_wait_transaction(struct btrfs_fs_info *fs_info, int type)
53962306a36Sopenharmony_ci{
54062306a36Sopenharmony_ci	if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
54162306a36Sopenharmony_ci		return 0;
54262306a36Sopenharmony_ci
54362306a36Sopenharmony_ci	if (type == TRANS_START)
54462306a36Sopenharmony_ci		return 1;
54562306a36Sopenharmony_ci
54662306a36Sopenharmony_ci	return 0;
54762306a36Sopenharmony_ci}
54862306a36Sopenharmony_ci
54962306a36Sopenharmony_cistatic inline bool need_reserve_reloc_root(struct btrfs_root *root)
55062306a36Sopenharmony_ci{
55162306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = root->fs_info;
55262306a36Sopenharmony_ci
55362306a36Sopenharmony_ci	if (!fs_info->reloc_ctl ||
55462306a36Sopenharmony_ci	    !test_bit(BTRFS_ROOT_SHAREABLE, &root->state) ||
55562306a36Sopenharmony_ci	    root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
55662306a36Sopenharmony_ci	    root->reloc_root)
55762306a36Sopenharmony_ci		return false;
55862306a36Sopenharmony_ci
55962306a36Sopenharmony_ci	return true;
56062306a36Sopenharmony_ci}
56162306a36Sopenharmony_ci
56262306a36Sopenharmony_cistatic struct btrfs_trans_handle *
56362306a36Sopenharmony_cistart_transaction(struct btrfs_root *root, unsigned int num_items,
56462306a36Sopenharmony_ci		  unsigned int type, enum btrfs_reserve_flush_enum flush,
56562306a36Sopenharmony_ci		  bool enforce_qgroups)
56662306a36Sopenharmony_ci{
56762306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = root->fs_info;
56862306a36Sopenharmony_ci	struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv;
56962306a36Sopenharmony_ci	struct btrfs_trans_handle *h;
57062306a36Sopenharmony_ci	struct btrfs_transaction *cur_trans;
57162306a36Sopenharmony_ci	u64 num_bytes = 0;
57262306a36Sopenharmony_ci	u64 qgroup_reserved = 0;
57362306a36Sopenharmony_ci	bool reloc_reserved = false;
57462306a36Sopenharmony_ci	bool do_chunk_alloc = false;
57562306a36Sopenharmony_ci	int ret;
57662306a36Sopenharmony_ci
57762306a36Sopenharmony_ci	if (BTRFS_FS_ERROR(fs_info))
57862306a36Sopenharmony_ci		return ERR_PTR(-EROFS);
57962306a36Sopenharmony_ci
58062306a36Sopenharmony_ci	if (current->journal_info) {
58162306a36Sopenharmony_ci		WARN_ON(type & TRANS_EXTWRITERS);
58262306a36Sopenharmony_ci		h = current->journal_info;
58362306a36Sopenharmony_ci		refcount_inc(&h->use_count);
58462306a36Sopenharmony_ci		WARN_ON(refcount_read(&h->use_count) > 2);
58562306a36Sopenharmony_ci		h->orig_rsv = h->block_rsv;
58662306a36Sopenharmony_ci		h->block_rsv = NULL;
58762306a36Sopenharmony_ci		goto got_it;
58862306a36Sopenharmony_ci	}
58962306a36Sopenharmony_ci
59062306a36Sopenharmony_ci	/*
59162306a36Sopenharmony_ci	 * Do the reservation before we join the transaction so we can do all
59262306a36Sopenharmony_ci	 * the appropriate flushing if need be.
59362306a36Sopenharmony_ci	 */
59462306a36Sopenharmony_ci	if (num_items && root != fs_info->chunk_root) {
59562306a36Sopenharmony_ci		struct btrfs_block_rsv *rsv = &fs_info->trans_block_rsv;
59662306a36Sopenharmony_ci		u64 delayed_refs_bytes = 0;
59762306a36Sopenharmony_ci
59862306a36Sopenharmony_ci		qgroup_reserved = num_items * fs_info->nodesize;
59962306a36Sopenharmony_ci		/*
60062306a36Sopenharmony_ci		 * Use prealloc for now, as there might be a currently running
60162306a36Sopenharmony_ci		 * transaction that could free this reserved space prematurely
60262306a36Sopenharmony_ci		 * by committing.
60362306a36Sopenharmony_ci		 */
60462306a36Sopenharmony_ci		ret = btrfs_qgroup_reserve_meta_prealloc(root, qgroup_reserved,
60562306a36Sopenharmony_ci							 enforce_qgroups, false);
60662306a36Sopenharmony_ci		if (ret)
60762306a36Sopenharmony_ci			return ERR_PTR(ret);
60862306a36Sopenharmony_ci
60962306a36Sopenharmony_ci		/*
61062306a36Sopenharmony_ci		 * We want to reserve all the bytes we may need all at once, so
61162306a36Sopenharmony_ci		 * we only do 1 enospc flushing cycle per transaction start.  We
61262306a36Sopenharmony_ci		 * accomplish this by simply assuming we'll do num_items worth
61362306a36Sopenharmony_ci		 * of delayed refs updates in this trans handle, and refill that
61462306a36Sopenharmony_ci		 * amount for whatever is missing in the reserve.
61562306a36Sopenharmony_ci		 */
61662306a36Sopenharmony_ci		num_bytes = btrfs_calc_insert_metadata_size(fs_info, num_items);
61762306a36Sopenharmony_ci		if (flush == BTRFS_RESERVE_FLUSH_ALL &&
61862306a36Sopenharmony_ci		    !btrfs_block_rsv_full(delayed_refs_rsv)) {
61962306a36Sopenharmony_ci			delayed_refs_bytes = btrfs_calc_delayed_ref_bytes(fs_info,
62062306a36Sopenharmony_ci									  num_items);
62162306a36Sopenharmony_ci			num_bytes += delayed_refs_bytes;
62262306a36Sopenharmony_ci		}
62362306a36Sopenharmony_ci
62462306a36Sopenharmony_ci		/*
62562306a36Sopenharmony_ci		 * Do the reservation for the relocation root creation
62662306a36Sopenharmony_ci		 */
62762306a36Sopenharmony_ci		if (need_reserve_reloc_root(root)) {
62862306a36Sopenharmony_ci			num_bytes += fs_info->nodesize;
62962306a36Sopenharmony_ci			reloc_reserved = true;
63062306a36Sopenharmony_ci		}
63162306a36Sopenharmony_ci
63262306a36Sopenharmony_ci		ret = btrfs_reserve_metadata_bytes(fs_info, rsv, num_bytes, flush);
63362306a36Sopenharmony_ci		if (ret)
63462306a36Sopenharmony_ci			goto reserve_fail;
63562306a36Sopenharmony_ci		if (delayed_refs_bytes) {
63662306a36Sopenharmony_ci			btrfs_migrate_to_delayed_refs_rsv(fs_info, delayed_refs_bytes);
63762306a36Sopenharmony_ci			num_bytes -= delayed_refs_bytes;
63862306a36Sopenharmony_ci		}
63962306a36Sopenharmony_ci		btrfs_block_rsv_add_bytes(rsv, num_bytes, true);
64062306a36Sopenharmony_ci
64162306a36Sopenharmony_ci		if (rsv->space_info->force_alloc)
64262306a36Sopenharmony_ci			do_chunk_alloc = true;
64362306a36Sopenharmony_ci	} else if (num_items == 0 && flush == BTRFS_RESERVE_FLUSH_ALL &&
64462306a36Sopenharmony_ci		   !btrfs_block_rsv_full(delayed_refs_rsv)) {
64562306a36Sopenharmony_ci		/*
64662306a36Sopenharmony_ci		 * Some people call with btrfs_start_transaction(root, 0)
64762306a36Sopenharmony_ci		 * because they can be throttled, but have some other mechanism
64862306a36Sopenharmony_ci		 * for reserving space.  We still want these guys to refill the
64962306a36Sopenharmony_ci		 * delayed block_rsv so just add 1 items worth of reservation
65062306a36Sopenharmony_ci		 * here.
65162306a36Sopenharmony_ci		 */
65262306a36Sopenharmony_ci		ret = btrfs_delayed_refs_rsv_refill(fs_info, flush);
65362306a36Sopenharmony_ci		if (ret)
65462306a36Sopenharmony_ci			goto reserve_fail;
65562306a36Sopenharmony_ci	}
65662306a36Sopenharmony_ciagain:
65762306a36Sopenharmony_ci	h = kmem_cache_zalloc(btrfs_trans_handle_cachep, GFP_NOFS);
65862306a36Sopenharmony_ci	if (!h) {
65962306a36Sopenharmony_ci		ret = -ENOMEM;
66062306a36Sopenharmony_ci		goto alloc_fail;
66162306a36Sopenharmony_ci	}
66262306a36Sopenharmony_ci
66362306a36Sopenharmony_ci	/*
66462306a36Sopenharmony_ci	 * If we are JOIN_NOLOCK we're already committing a transaction and
66562306a36Sopenharmony_ci	 * waiting on this guy, so we don't need to do the sb_start_intwrite
66662306a36Sopenharmony_ci	 * because we're already holding a ref.  We need this because we could
66762306a36Sopenharmony_ci	 * have raced in and did an fsync() on a file which can kick a commit
66862306a36Sopenharmony_ci	 * and then we deadlock with somebody doing a freeze.
66962306a36Sopenharmony_ci	 *
67062306a36Sopenharmony_ci	 * If we are ATTACH, it means we just want to catch the current
67162306a36Sopenharmony_ci	 * transaction and commit it, so we needn't do sb_start_intwrite().
67262306a36Sopenharmony_ci	 */
67362306a36Sopenharmony_ci	if (type & __TRANS_FREEZABLE)
67462306a36Sopenharmony_ci		sb_start_intwrite(fs_info->sb);
67562306a36Sopenharmony_ci
67662306a36Sopenharmony_ci	if (may_wait_transaction(fs_info, type))
67762306a36Sopenharmony_ci		wait_current_trans(fs_info);
67862306a36Sopenharmony_ci
67962306a36Sopenharmony_ci	do {
68062306a36Sopenharmony_ci		ret = join_transaction(fs_info, type);
68162306a36Sopenharmony_ci		if (ret == -EBUSY) {
68262306a36Sopenharmony_ci			wait_current_trans(fs_info);
68362306a36Sopenharmony_ci			if (unlikely(type == TRANS_ATTACH ||
68462306a36Sopenharmony_ci				     type == TRANS_JOIN_NOSTART))
68562306a36Sopenharmony_ci				ret = -ENOENT;
68662306a36Sopenharmony_ci		}
68762306a36Sopenharmony_ci	} while (ret == -EBUSY);
68862306a36Sopenharmony_ci
68962306a36Sopenharmony_ci	if (ret < 0)
69062306a36Sopenharmony_ci		goto join_fail;
69162306a36Sopenharmony_ci
69262306a36Sopenharmony_ci	cur_trans = fs_info->running_transaction;
69362306a36Sopenharmony_ci
69462306a36Sopenharmony_ci	h->transid = cur_trans->transid;
69562306a36Sopenharmony_ci	h->transaction = cur_trans;
69662306a36Sopenharmony_ci	refcount_set(&h->use_count, 1);
69762306a36Sopenharmony_ci	h->fs_info = root->fs_info;
69862306a36Sopenharmony_ci
69962306a36Sopenharmony_ci	h->type = type;
70062306a36Sopenharmony_ci	INIT_LIST_HEAD(&h->new_bgs);
70162306a36Sopenharmony_ci
70262306a36Sopenharmony_ci	smp_mb();
70362306a36Sopenharmony_ci	if (cur_trans->state >= TRANS_STATE_COMMIT_START &&
70462306a36Sopenharmony_ci	    may_wait_transaction(fs_info, type)) {
70562306a36Sopenharmony_ci		current->journal_info = h;
70662306a36Sopenharmony_ci		btrfs_commit_transaction(h);
70762306a36Sopenharmony_ci		goto again;
70862306a36Sopenharmony_ci	}
70962306a36Sopenharmony_ci
71062306a36Sopenharmony_ci	if (num_bytes) {
71162306a36Sopenharmony_ci		trace_btrfs_space_reservation(fs_info, "transaction",
71262306a36Sopenharmony_ci					      h->transid, num_bytes, 1);
71362306a36Sopenharmony_ci		h->block_rsv = &fs_info->trans_block_rsv;
71462306a36Sopenharmony_ci		h->bytes_reserved = num_bytes;
71562306a36Sopenharmony_ci		h->reloc_reserved = reloc_reserved;
71662306a36Sopenharmony_ci	}
71762306a36Sopenharmony_ci
71862306a36Sopenharmony_ci	/*
71962306a36Sopenharmony_ci	 * Now that we have found a transaction to be a part of, convert the
72062306a36Sopenharmony_ci	 * qgroup reservation from prealloc to pertrans. A different transaction
72162306a36Sopenharmony_ci	 * can't race in and free our pertrans out from under us.
72262306a36Sopenharmony_ci	 */
72362306a36Sopenharmony_ci	if (qgroup_reserved)
72462306a36Sopenharmony_ci		btrfs_qgroup_convert_reserved_meta(root, qgroup_reserved);
72562306a36Sopenharmony_ci
72662306a36Sopenharmony_cigot_it:
72762306a36Sopenharmony_ci	if (!current->journal_info)
72862306a36Sopenharmony_ci		current->journal_info = h;
72962306a36Sopenharmony_ci
73062306a36Sopenharmony_ci	/*
73162306a36Sopenharmony_ci	 * If the space_info is marked ALLOC_FORCE then we'll get upgraded to
73262306a36Sopenharmony_ci	 * ALLOC_FORCE the first run through, and then we won't allocate for
73362306a36Sopenharmony_ci	 * anybody else who races in later.  We don't care about the return
73462306a36Sopenharmony_ci	 * value here.
73562306a36Sopenharmony_ci	 */
73662306a36Sopenharmony_ci	if (do_chunk_alloc && num_bytes) {
73762306a36Sopenharmony_ci		u64 flags = h->block_rsv->space_info->flags;
73862306a36Sopenharmony_ci
73962306a36Sopenharmony_ci		btrfs_chunk_alloc(h, btrfs_get_alloc_profile(fs_info, flags),
74062306a36Sopenharmony_ci				  CHUNK_ALLOC_NO_FORCE);
74162306a36Sopenharmony_ci	}
74262306a36Sopenharmony_ci
74362306a36Sopenharmony_ci	/*
74462306a36Sopenharmony_ci	 * btrfs_record_root_in_trans() needs to alloc new extents, and may
74562306a36Sopenharmony_ci	 * call btrfs_join_transaction() while we're also starting a
74662306a36Sopenharmony_ci	 * transaction.
74762306a36Sopenharmony_ci	 *
74862306a36Sopenharmony_ci	 * Thus it need to be called after current->journal_info initialized,
74962306a36Sopenharmony_ci	 * or we can deadlock.
75062306a36Sopenharmony_ci	 */
75162306a36Sopenharmony_ci	ret = btrfs_record_root_in_trans(h, root);
75262306a36Sopenharmony_ci	if (ret) {
75362306a36Sopenharmony_ci		/*
75462306a36Sopenharmony_ci		 * The transaction handle is fully initialized and linked with
75562306a36Sopenharmony_ci		 * other structures so it needs to be ended in case of errors,
75662306a36Sopenharmony_ci		 * not just freed.
75762306a36Sopenharmony_ci		 */
75862306a36Sopenharmony_ci		btrfs_end_transaction(h);
75962306a36Sopenharmony_ci		return ERR_PTR(ret);
76062306a36Sopenharmony_ci	}
76162306a36Sopenharmony_ci
76262306a36Sopenharmony_ci	return h;
76362306a36Sopenharmony_ci
76462306a36Sopenharmony_cijoin_fail:
76562306a36Sopenharmony_ci	if (type & __TRANS_FREEZABLE)
76662306a36Sopenharmony_ci		sb_end_intwrite(fs_info->sb);
76762306a36Sopenharmony_ci	kmem_cache_free(btrfs_trans_handle_cachep, h);
76862306a36Sopenharmony_cialloc_fail:
76962306a36Sopenharmony_ci	if (num_bytes)
77062306a36Sopenharmony_ci		btrfs_block_rsv_release(fs_info, &fs_info->trans_block_rsv,
77162306a36Sopenharmony_ci					num_bytes, NULL);
77262306a36Sopenharmony_cireserve_fail:
77362306a36Sopenharmony_ci	btrfs_qgroup_free_meta_prealloc(root, qgroup_reserved);
77462306a36Sopenharmony_ci	return ERR_PTR(ret);
77562306a36Sopenharmony_ci}
77662306a36Sopenharmony_ci
77762306a36Sopenharmony_cistruct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
77862306a36Sopenharmony_ci						   unsigned int num_items)
77962306a36Sopenharmony_ci{
78062306a36Sopenharmony_ci	return start_transaction(root, num_items, TRANS_START,
78162306a36Sopenharmony_ci				 BTRFS_RESERVE_FLUSH_ALL, true);
78262306a36Sopenharmony_ci}
78362306a36Sopenharmony_ci
78462306a36Sopenharmony_cistruct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
78562306a36Sopenharmony_ci					struct btrfs_root *root,
78662306a36Sopenharmony_ci					unsigned int num_items)
78762306a36Sopenharmony_ci{
78862306a36Sopenharmony_ci	return start_transaction(root, num_items, TRANS_START,
78962306a36Sopenharmony_ci				 BTRFS_RESERVE_FLUSH_ALL_STEAL, false);
79062306a36Sopenharmony_ci}
79162306a36Sopenharmony_ci
79262306a36Sopenharmony_cistruct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
79362306a36Sopenharmony_ci{
79462306a36Sopenharmony_ci	return start_transaction(root, 0, TRANS_JOIN, BTRFS_RESERVE_NO_FLUSH,
79562306a36Sopenharmony_ci				 true);
79662306a36Sopenharmony_ci}
79762306a36Sopenharmony_ci
79862306a36Sopenharmony_cistruct btrfs_trans_handle *btrfs_join_transaction_spacecache(struct btrfs_root *root)
79962306a36Sopenharmony_ci{
80062306a36Sopenharmony_ci	return start_transaction(root, 0, TRANS_JOIN_NOLOCK,
80162306a36Sopenharmony_ci				 BTRFS_RESERVE_NO_FLUSH, true);
80262306a36Sopenharmony_ci}
80362306a36Sopenharmony_ci
80462306a36Sopenharmony_ci/*
80562306a36Sopenharmony_ci * Similar to regular join but it never starts a transaction when none is
80662306a36Sopenharmony_ci * running or when there's a running one at a state >= TRANS_STATE_UNBLOCKED.
80762306a36Sopenharmony_ci * This is similar to btrfs_attach_transaction() but it allows the join to
80862306a36Sopenharmony_ci * happen if the transaction commit already started but it's not yet in the
80962306a36Sopenharmony_ci * "doing" phase (the state is < TRANS_STATE_COMMIT_DOING).
81062306a36Sopenharmony_ci */
81162306a36Sopenharmony_cistruct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *root)
81262306a36Sopenharmony_ci{
81362306a36Sopenharmony_ci	return start_transaction(root, 0, TRANS_JOIN_NOSTART,
81462306a36Sopenharmony_ci				 BTRFS_RESERVE_NO_FLUSH, true);
81562306a36Sopenharmony_ci}
81662306a36Sopenharmony_ci
81762306a36Sopenharmony_ci/*
81862306a36Sopenharmony_ci * btrfs_attach_transaction() - catch the running transaction
81962306a36Sopenharmony_ci *
82062306a36Sopenharmony_ci * It is used when we want to commit the current the transaction, but
82162306a36Sopenharmony_ci * don't want to start a new one.
82262306a36Sopenharmony_ci *
82362306a36Sopenharmony_ci * Note: If this function return -ENOENT, it just means there is no
82462306a36Sopenharmony_ci * running transaction. But it is possible that the inactive transaction
82562306a36Sopenharmony_ci * is still in the memory, not fully on disk. If you hope there is no
82662306a36Sopenharmony_ci * inactive transaction in the fs when -ENOENT is returned, you should
82762306a36Sopenharmony_ci * invoke
82862306a36Sopenharmony_ci *     btrfs_attach_transaction_barrier()
82962306a36Sopenharmony_ci */
83062306a36Sopenharmony_cistruct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
83162306a36Sopenharmony_ci{
83262306a36Sopenharmony_ci	return start_transaction(root, 0, TRANS_ATTACH,
83362306a36Sopenharmony_ci				 BTRFS_RESERVE_NO_FLUSH, true);
83462306a36Sopenharmony_ci}
83562306a36Sopenharmony_ci
83662306a36Sopenharmony_ci/*
83762306a36Sopenharmony_ci * btrfs_attach_transaction_barrier() - catch the running transaction
83862306a36Sopenharmony_ci *
83962306a36Sopenharmony_ci * It is similar to the above function, the difference is this one
84062306a36Sopenharmony_ci * will wait for all the inactive transactions until they fully
84162306a36Sopenharmony_ci * complete.
84262306a36Sopenharmony_ci */
84362306a36Sopenharmony_cistruct btrfs_trans_handle *
84462306a36Sopenharmony_cibtrfs_attach_transaction_barrier(struct btrfs_root *root)
84562306a36Sopenharmony_ci{
84662306a36Sopenharmony_ci	struct btrfs_trans_handle *trans;
84762306a36Sopenharmony_ci
84862306a36Sopenharmony_ci	trans = start_transaction(root, 0, TRANS_ATTACH,
84962306a36Sopenharmony_ci				  BTRFS_RESERVE_NO_FLUSH, true);
85062306a36Sopenharmony_ci	if (trans == ERR_PTR(-ENOENT)) {
85162306a36Sopenharmony_ci		int ret;
85262306a36Sopenharmony_ci
85362306a36Sopenharmony_ci		ret = btrfs_wait_for_commit(root->fs_info, 0);
85462306a36Sopenharmony_ci		if (ret)
85562306a36Sopenharmony_ci			return ERR_PTR(ret);
85662306a36Sopenharmony_ci	}
85762306a36Sopenharmony_ci
85862306a36Sopenharmony_ci	return trans;
85962306a36Sopenharmony_ci}
86062306a36Sopenharmony_ci
86162306a36Sopenharmony_ci/* Wait for a transaction commit to reach at least the given state. */
86262306a36Sopenharmony_cistatic noinline void wait_for_commit(struct btrfs_transaction *commit,
86362306a36Sopenharmony_ci				     const enum btrfs_trans_state min_state)
86462306a36Sopenharmony_ci{
86562306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = commit->fs_info;
86662306a36Sopenharmony_ci	u64 transid = commit->transid;
86762306a36Sopenharmony_ci	bool put = false;
86862306a36Sopenharmony_ci
86962306a36Sopenharmony_ci	/*
87062306a36Sopenharmony_ci	 * At the moment this function is called with min_state either being
87162306a36Sopenharmony_ci	 * TRANS_STATE_COMPLETED or TRANS_STATE_SUPER_COMMITTED.
87262306a36Sopenharmony_ci	 */
87362306a36Sopenharmony_ci	if (min_state == TRANS_STATE_COMPLETED)
87462306a36Sopenharmony_ci		btrfs_might_wait_for_state(fs_info, BTRFS_LOCKDEP_TRANS_COMPLETED);
87562306a36Sopenharmony_ci	else
87662306a36Sopenharmony_ci		btrfs_might_wait_for_state(fs_info, BTRFS_LOCKDEP_TRANS_SUPER_COMMITTED);
87762306a36Sopenharmony_ci
87862306a36Sopenharmony_ci	while (1) {
87962306a36Sopenharmony_ci		wait_event(commit->commit_wait, commit->state >= min_state);
88062306a36Sopenharmony_ci		if (put)
88162306a36Sopenharmony_ci			btrfs_put_transaction(commit);
88262306a36Sopenharmony_ci
88362306a36Sopenharmony_ci		if (min_state < TRANS_STATE_COMPLETED)
88462306a36Sopenharmony_ci			break;
88562306a36Sopenharmony_ci
88662306a36Sopenharmony_ci		/*
88762306a36Sopenharmony_ci		 * A transaction isn't really completed until all of the
88862306a36Sopenharmony_ci		 * previous transactions are completed, but with fsync we can
88962306a36Sopenharmony_ci		 * end up with SUPER_COMMITTED transactions before a COMPLETED
89062306a36Sopenharmony_ci		 * transaction. Wait for those.
89162306a36Sopenharmony_ci		 */
89262306a36Sopenharmony_ci
89362306a36Sopenharmony_ci		spin_lock(&fs_info->trans_lock);
89462306a36Sopenharmony_ci		commit = list_first_entry_or_null(&fs_info->trans_list,
89562306a36Sopenharmony_ci						  struct btrfs_transaction,
89662306a36Sopenharmony_ci						  list);
89762306a36Sopenharmony_ci		if (!commit || commit->transid > transid) {
89862306a36Sopenharmony_ci			spin_unlock(&fs_info->trans_lock);
89962306a36Sopenharmony_ci			break;
90062306a36Sopenharmony_ci		}
90162306a36Sopenharmony_ci		refcount_inc(&commit->use_count);
90262306a36Sopenharmony_ci		put = true;
90362306a36Sopenharmony_ci		spin_unlock(&fs_info->trans_lock);
90462306a36Sopenharmony_ci	}
90562306a36Sopenharmony_ci}
90662306a36Sopenharmony_ci
90762306a36Sopenharmony_ciint btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid)
90862306a36Sopenharmony_ci{
90962306a36Sopenharmony_ci	struct btrfs_transaction *cur_trans = NULL, *t;
91062306a36Sopenharmony_ci	int ret = 0;
91162306a36Sopenharmony_ci
91262306a36Sopenharmony_ci	if (transid) {
91362306a36Sopenharmony_ci		if (transid <= fs_info->last_trans_committed)
91462306a36Sopenharmony_ci			goto out;
91562306a36Sopenharmony_ci
91662306a36Sopenharmony_ci		/* find specified transaction */
91762306a36Sopenharmony_ci		spin_lock(&fs_info->trans_lock);
91862306a36Sopenharmony_ci		list_for_each_entry(t, &fs_info->trans_list, list) {
91962306a36Sopenharmony_ci			if (t->transid == transid) {
92062306a36Sopenharmony_ci				cur_trans = t;
92162306a36Sopenharmony_ci				refcount_inc(&cur_trans->use_count);
92262306a36Sopenharmony_ci				ret = 0;
92362306a36Sopenharmony_ci				break;
92462306a36Sopenharmony_ci			}
92562306a36Sopenharmony_ci			if (t->transid > transid) {
92662306a36Sopenharmony_ci				ret = 0;
92762306a36Sopenharmony_ci				break;
92862306a36Sopenharmony_ci			}
92962306a36Sopenharmony_ci		}
93062306a36Sopenharmony_ci		spin_unlock(&fs_info->trans_lock);
93162306a36Sopenharmony_ci
93262306a36Sopenharmony_ci		/*
93362306a36Sopenharmony_ci		 * The specified transaction doesn't exist, or we
93462306a36Sopenharmony_ci		 * raced with btrfs_commit_transaction
93562306a36Sopenharmony_ci		 */
93662306a36Sopenharmony_ci		if (!cur_trans) {
93762306a36Sopenharmony_ci			if (transid > fs_info->last_trans_committed)
93862306a36Sopenharmony_ci				ret = -EINVAL;
93962306a36Sopenharmony_ci			goto out;
94062306a36Sopenharmony_ci		}
94162306a36Sopenharmony_ci	} else {
94262306a36Sopenharmony_ci		/* find newest transaction that is committing | committed */
94362306a36Sopenharmony_ci		spin_lock(&fs_info->trans_lock);
94462306a36Sopenharmony_ci		list_for_each_entry_reverse(t, &fs_info->trans_list,
94562306a36Sopenharmony_ci					    list) {
94662306a36Sopenharmony_ci			if (t->state >= TRANS_STATE_COMMIT_START) {
94762306a36Sopenharmony_ci				if (t->state == TRANS_STATE_COMPLETED)
94862306a36Sopenharmony_ci					break;
94962306a36Sopenharmony_ci				cur_trans = t;
95062306a36Sopenharmony_ci				refcount_inc(&cur_trans->use_count);
95162306a36Sopenharmony_ci				break;
95262306a36Sopenharmony_ci			}
95362306a36Sopenharmony_ci		}
95462306a36Sopenharmony_ci		spin_unlock(&fs_info->trans_lock);
95562306a36Sopenharmony_ci		if (!cur_trans)
95662306a36Sopenharmony_ci			goto out;  /* nothing committing|committed */
95762306a36Sopenharmony_ci	}
95862306a36Sopenharmony_ci
95962306a36Sopenharmony_ci	wait_for_commit(cur_trans, TRANS_STATE_COMPLETED);
96062306a36Sopenharmony_ci	ret = cur_trans->aborted;
96162306a36Sopenharmony_ci	btrfs_put_transaction(cur_trans);
96262306a36Sopenharmony_ciout:
96362306a36Sopenharmony_ci	return ret;
96462306a36Sopenharmony_ci}
96562306a36Sopenharmony_ci
96662306a36Sopenharmony_civoid btrfs_throttle(struct btrfs_fs_info *fs_info)
96762306a36Sopenharmony_ci{
96862306a36Sopenharmony_ci	wait_current_trans(fs_info);
96962306a36Sopenharmony_ci}
97062306a36Sopenharmony_ci
97162306a36Sopenharmony_cibool btrfs_should_end_transaction(struct btrfs_trans_handle *trans)
97262306a36Sopenharmony_ci{
97362306a36Sopenharmony_ci	struct btrfs_transaction *cur_trans = trans->transaction;
97462306a36Sopenharmony_ci
97562306a36Sopenharmony_ci	if (cur_trans->state >= TRANS_STATE_COMMIT_START ||
97662306a36Sopenharmony_ci	    test_bit(BTRFS_DELAYED_REFS_FLUSHING, &cur_trans->delayed_refs.flags))
97762306a36Sopenharmony_ci		return true;
97862306a36Sopenharmony_ci
97962306a36Sopenharmony_ci	if (btrfs_check_space_for_delayed_refs(trans->fs_info))
98062306a36Sopenharmony_ci		return true;
98162306a36Sopenharmony_ci
98262306a36Sopenharmony_ci	return !!btrfs_block_rsv_check(&trans->fs_info->global_block_rsv, 50);
98362306a36Sopenharmony_ci}
98462306a36Sopenharmony_ci
98562306a36Sopenharmony_cistatic void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans)
98662306a36Sopenharmony_ci
98762306a36Sopenharmony_ci{
98862306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
98962306a36Sopenharmony_ci
99062306a36Sopenharmony_ci	if (!trans->block_rsv) {
99162306a36Sopenharmony_ci		ASSERT(!trans->bytes_reserved);
99262306a36Sopenharmony_ci		return;
99362306a36Sopenharmony_ci	}
99462306a36Sopenharmony_ci
99562306a36Sopenharmony_ci	if (!trans->bytes_reserved)
99662306a36Sopenharmony_ci		return;
99762306a36Sopenharmony_ci
99862306a36Sopenharmony_ci	ASSERT(trans->block_rsv == &fs_info->trans_block_rsv);
99962306a36Sopenharmony_ci	trace_btrfs_space_reservation(fs_info, "transaction",
100062306a36Sopenharmony_ci				      trans->transid, trans->bytes_reserved, 0);
100162306a36Sopenharmony_ci	btrfs_block_rsv_release(fs_info, trans->block_rsv,
100262306a36Sopenharmony_ci				trans->bytes_reserved, NULL);
100362306a36Sopenharmony_ci	trans->bytes_reserved = 0;
100462306a36Sopenharmony_ci}
100562306a36Sopenharmony_ci
100662306a36Sopenharmony_cistatic int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
100762306a36Sopenharmony_ci				   int throttle)
100862306a36Sopenharmony_ci{
100962306a36Sopenharmony_ci	struct btrfs_fs_info *info = trans->fs_info;
101062306a36Sopenharmony_ci	struct btrfs_transaction *cur_trans = trans->transaction;
101162306a36Sopenharmony_ci	int err = 0;
101262306a36Sopenharmony_ci
101362306a36Sopenharmony_ci	if (refcount_read(&trans->use_count) > 1) {
101462306a36Sopenharmony_ci		refcount_dec(&trans->use_count);
101562306a36Sopenharmony_ci		trans->block_rsv = trans->orig_rsv;
101662306a36Sopenharmony_ci		return 0;
101762306a36Sopenharmony_ci	}
101862306a36Sopenharmony_ci
101962306a36Sopenharmony_ci	btrfs_trans_release_metadata(trans);
102062306a36Sopenharmony_ci	trans->block_rsv = NULL;
102162306a36Sopenharmony_ci
102262306a36Sopenharmony_ci	btrfs_create_pending_block_groups(trans);
102362306a36Sopenharmony_ci
102462306a36Sopenharmony_ci	btrfs_trans_release_chunk_metadata(trans);
102562306a36Sopenharmony_ci
102662306a36Sopenharmony_ci	if (trans->type & __TRANS_FREEZABLE)
102762306a36Sopenharmony_ci		sb_end_intwrite(info->sb);
102862306a36Sopenharmony_ci
102962306a36Sopenharmony_ci	WARN_ON(cur_trans != info->running_transaction);
103062306a36Sopenharmony_ci	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
103162306a36Sopenharmony_ci	atomic_dec(&cur_trans->num_writers);
103262306a36Sopenharmony_ci	extwriter_counter_dec(cur_trans, trans->type);
103362306a36Sopenharmony_ci
103462306a36Sopenharmony_ci	cond_wake_up(&cur_trans->writer_wait);
103562306a36Sopenharmony_ci
103662306a36Sopenharmony_ci	btrfs_lockdep_release(info, btrfs_trans_num_extwriters);
103762306a36Sopenharmony_ci	btrfs_lockdep_release(info, btrfs_trans_num_writers);
103862306a36Sopenharmony_ci
103962306a36Sopenharmony_ci	btrfs_put_transaction(cur_trans);
104062306a36Sopenharmony_ci
104162306a36Sopenharmony_ci	if (current->journal_info == trans)
104262306a36Sopenharmony_ci		current->journal_info = NULL;
104362306a36Sopenharmony_ci
104462306a36Sopenharmony_ci	if (throttle)
104562306a36Sopenharmony_ci		btrfs_run_delayed_iputs(info);
104662306a36Sopenharmony_ci
104762306a36Sopenharmony_ci	if (TRANS_ABORTED(trans) || BTRFS_FS_ERROR(info)) {
104862306a36Sopenharmony_ci		wake_up_process(info->transaction_kthread);
104962306a36Sopenharmony_ci		if (TRANS_ABORTED(trans))
105062306a36Sopenharmony_ci			err = trans->aborted;
105162306a36Sopenharmony_ci		else
105262306a36Sopenharmony_ci			err = -EROFS;
105362306a36Sopenharmony_ci	}
105462306a36Sopenharmony_ci
105562306a36Sopenharmony_ci	kmem_cache_free(btrfs_trans_handle_cachep, trans);
105662306a36Sopenharmony_ci	return err;
105762306a36Sopenharmony_ci}
105862306a36Sopenharmony_ci
105962306a36Sopenharmony_ciint btrfs_end_transaction(struct btrfs_trans_handle *trans)
106062306a36Sopenharmony_ci{
106162306a36Sopenharmony_ci	return __btrfs_end_transaction(trans, 0);
106262306a36Sopenharmony_ci}
106362306a36Sopenharmony_ci
106462306a36Sopenharmony_ciint btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans)
106562306a36Sopenharmony_ci{
106662306a36Sopenharmony_ci	return __btrfs_end_transaction(trans, 1);
106762306a36Sopenharmony_ci}
106862306a36Sopenharmony_ci
106962306a36Sopenharmony_ci/*
107062306a36Sopenharmony_ci * when btree blocks are allocated, they have some corresponding bits set for
107162306a36Sopenharmony_ci * them in one of two extent_io trees.  This is used to make sure all of
107262306a36Sopenharmony_ci * those extents are sent to disk but does not wait on them
107362306a36Sopenharmony_ci */
107462306a36Sopenharmony_ciint btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
107562306a36Sopenharmony_ci			       struct extent_io_tree *dirty_pages, int mark)
107662306a36Sopenharmony_ci{
107762306a36Sopenharmony_ci	int err = 0;
107862306a36Sopenharmony_ci	int werr = 0;
107962306a36Sopenharmony_ci	struct address_space *mapping = fs_info->btree_inode->i_mapping;
108062306a36Sopenharmony_ci	struct extent_state *cached_state = NULL;
108162306a36Sopenharmony_ci	u64 start = 0;
108262306a36Sopenharmony_ci	u64 end;
108362306a36Sopenharmony_ci
108462306a36Sopenharmony_ci	while (find_first_extent_bit(dirty_pages, start, &start, &end,
108562306a36Sopenharmony_ci				     mark, &cached_state)) {
108662306a36Sopenharmony_ci		bool wait_writeback = false;
108762306a36Sopenharmony_ci
108862306a36Sopenharmony_ci		err = convert_extent_bit(dirty_pages, start, end,
108962306a36Sopenharmony_ci					 EXTENT_NEED_WAIT,
109062306a36Sopenharmony_ci					 mark, &cached_state);
109162306a36Sopenharmony_ci		/*
109262306a36Sopenharmony_ci		 * convert_extent_bit can return -ENOMEM, which is most of the
109362306a36Sopenharmony_ci		 * time a temporary error. So when it happens, ignore the error
109462306a36Sopenharmony_ci		 * and wait for writeback of this range to finish - because we
109562306a36Sopenharmony_ci		 * failed to set the bit EXTENT_NEED_WAIT for the range, a call
109662306a36Sopenharmony_ci		 * to __btrfs_wait_marked_extents() would not know that
109762306a36Sopenharmony_ci		 * writeback for this range started and therefore wouldn't
109862306a36Sopenharmony_ci		 * wait for it to finish - we don't want to commit a
109962306a36Sopenharmony_ci		 * superblock that points to btree nodes/leafs for which
110062306a36Sopenharmony_ci		 * writeback hasn't finished yet (and without errors).
110162306a36Sopenharmony_ci		 * We cleanup any entries left in the io tree when committing
110262306a36Sopenharmony_ci		 * the transaction (through extent_io_tree_release()).
110362306a36Sopenharmony_ci		 */
110462306a36Sopenharmony_ci		if (err == -ENOMEM) {
110562306a36Sopenharmony_ci			err = 0;
110662306a36Sopenharmony_ci			wait_writeback = true;
110762306a36Sopenharmony_ci		}
110862306a36Sopenharmony_ci		if (!err)
110962306a36Sopenharmony_ci			err = filemap_fdatawrite_range(mapping, start, end);
111062306a36Sopenharmony_ci		if (err)
111162306a36Sopenharmony_ci			werr = err;
111262306a36Sopenharmony_ci		else if (wait_writeback)
111362306a36Sopenharmony_ci			werr = filemap_fdatawait_range(mapping, start, end);
111462306a36Sopenharmony_ci		free_extent_state(cached_state);
111562306a36Sopenharmony_ci		cached_state = NULL;
111662306a36Sopenharmony_ci		cond_resched();
111762306a36Sopenharmony_ci		start = end + 1;
111862306a36Sopenharmony_ci	}
111962306a36Sopenharmony_ci	return werr;
112062306a36Sopenharmony_ci}
112162306a36Sopenharmony_ci
112262306a36Sopenharmony_ci/*
112362306a36Sopenharmony_ci * when btree blocks are allocated, they have some corresponding bits set for
112462306a36Sopenharmony_ci * them in one of two extent_io trees.  This is used to make sure all of
112562306a36Sopenharmony_ci * those extents are on disk for transaction or log commit.  We wait
112662306a36Sopenharmony_ci * on all the pages and clear them from the dirty pages state tree
112762306a36Sopenharmony_ci */
112862306a36Sopenharmony_cistatic int __btrfs_wait_marked_extents(struct btrfs_fs_info *fs_info,
112962306a36Sopenharmony_ci				       struct extent_io_tree *dirty_pages)
113062306a36Sopenharmony_ci{
113162306a36Sopenharmony_ci	int err = 0;
113262306a36Sopenharmony_ci	int werr = 0;
113362306a36Sopenharmony_ci	struct address_space *mapping = fs_info->btree_inode->i_mapping;
113462306a36Sopenharmony_ci	struct extent_state *cached_state = NULL;
113562306a36Sopenharmony_ci	u64 start = 0;
113662306a36Sopenharmony_ci	u64 end;
113762306a36Sopenharmony_ci
113862306a36Sopenharmony_ci	while (find_first_extent_bit(dirty_pages, start, &start, &end,
113962306a36Sopenharmony_ci				     EXTENT_NEED_WAIT, &cached_state)) {
114062306a36Sopenharmony_ci		/*
114162306a36Sopenharmony_ci		 * Ignore -ENOMEM errors returned by clear_extent_bit().
114262306a36Sopenharmony_ci		 * When committing the transaction, we'll remove any entries
114362306a36Sopenharmony_ci		 * left in the io tree. For a log commit, we don't remove them
114462306a36Sopenharmony_ci		 * after committing the log because the tree can be accessed
114562306a36Sopenharmony_ci		 * concurrently - we do it only at transaction commit time when
114662306a36Sopenharmony_ci		 * it's safe to do it (through extent_io_tree_release()).
114762306a36Sopenharmony_ci		 */
114862306a36Sopenharmony_ci		err = clear_extent_bit(dirty_pages, start, end,
114962306a36Sopenharmony_ci				       EXTENT_NEED_WAIT, &cached_state);
115062306a36Sopenharmony_ci		if (err == -ENOMEM)
115162306a36Sopenharmony_ci			err = 0;
115262306a36Sopenharmony_ci		if (!err)
115362306a36Sopenharmony_ci			err = filemap_fdatawait_range(mapping, start, end);
115462306a36Sopenharmony_ci		if (err)
115562306a36Sopenharmony_ci			werr = err;
115662306a36Sopenharmony_ci		free_extent_state(cached_state);
115762306a36Sopenharmony_ci		cached_state = NULL;
115862306a36Sopenharmony_ci		cond_resched();
115962306a36Sopenharmony_ci		start = end + 1;
116062306a36Sopenharmony_ci	}
116162306a36Sopenharmony_ci	if (err)
116262306a36Sopenharmony_ci		werr = err;
116362306a36Sopenharmony_ci	return werr;
116462306a36Sopenharmony_ci}
116562306a36Sopenharmony_ci
116662306a36Sopenharmony_cistatic int btrfs_wait_extents(struct btrfs_fs_info *fs_info,
116762306a36Sopenharmony_ci		       struct extent_io_tree *dirty_pages)
116862306a36Sopenharmony_ci{
116962306a36Sopenharmony_ci	bool errors = false;
117062306a36Sopenharmony_ci	int err;
117162306a36Sopenharmony_ci
117262306a36Sopenharmony_ci	err = __btrfs_wait_marked_extents(fs_info, dirty_pages);
117362306a36Sopenharmony_ci	if (test_and_clear_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags))
117462306a36Sopenharmony_ci		errors = true;
117562306a36Sopenharmony_ci
117662306a36Sopenharmony_ci	if (errors && !err)
117762306a36Sopenharmony_ci		err = -EIO;
117862306a36Sopenharmony_ci	return err;
117962306a36Sopenharmony_ci}
118062306a36Sopenharmony_ci
118162306a36Sopenharmony_ciint btrfs_wait_tree_log_extents(struct btrfs_root *log_root, int mark)
118262306a36Sopenharmony_ci{
118362306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = log_root->fs_info;
118462306a36Sopenharmony_ci	struct extent_io_tree *dirty_pages = &log_root->dirty_log_pages;
118562306a36Sopenharmony_ci	bool errors = false;
118662306a36Sopenharmony_ci	int err;
118762306a36Sopenharmony_ci
118862306a36Sopenharmony_ci	ASSERT(log_root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
118962306a36Sopenharmony_ci
119062306a36Sopenharmony_ci	err = __btrfs_wait_marked_extents(fs_info, dirty_pages);
119162306a36Sopenharmony_ci	if ((mark & EXTENT_DIRTY) &&
119262306a36Sopenharmony_ci	    test_and_clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags))
119362306a36Sopenharmony_ci		errors = true;
119462306a36Sopenharmony_ci
119562306a36Sopenharmony_ci	if ((mark & EXTENT_NEW) &&
119662306a36Sopenharmony_ci	    test_and_clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags))
119762306a36Sopenharmony_ci		errors = true;
119862306a36Sopenharmony_ci
119962306a36Sopenharmony_ci	if (errors && !err)
120062306a36Sopenharmony_ci		err = -EIO;
120162306a36Sopenharmony_ci	return err;
120262306a36Sopenharmony_ci}
120362306a36Sopenharmony_ci
120462306a36Sopenharmony_ci/*
120562306a36Sopenharmony_ci * When btree blocks are allocated the corresponding extents are marked dirty.
120662306a36Sopenharmony_ci * This function ensures such extents are persisted on disk for transaction or
120762306a36Sopenharmony_ci * log commit.
120862306a36Sopenharmony_ci *
120962306a36Sopenharmony_ci * @trans: transaction whose dirty pages we'd like to write
121062306a36Sopenharmony_ci */
121162306a36Sopenharmony_cistatic int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans)
121262306a36Sopenharmony_ci{
121362306a36Sopenharmony_ci	int ret;
121462306a36Sopenharmony_ci	int ret2;
121562306a36Sopenharmony_ci	struct extent_io_tree *dirty_pages = &trans->transaction->dirty_pages;
121662306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
121762306a36Sopenharmony_ci	struct blk_plug plug;
121862306a36Sopenharmony_ci
121962306a36Sopenharmony_ci	blk_start_plug(&plug);
122062306a36Sopenharmony_ci	ret = btrfs_write_marked_extents(fs_info, dirty_pages, EXTENT_DIRTY);
122162306a36Sopenharmony_ci	blk_finish_plug(&plug);
122262306a36Sopenharmony_ci	ret2 = btrfs_wait_extents(fs_info, dirty_pages);
122362306a36Sopenharmony_ci
122462306a36Sopenharmony_ci	extent_io_tree_release(&trans->transaction->dirty_pages);
122562306a36Sopenharmony_ci
122662306a36Sopenharmony_ci	if (ret)
122762306a36Sopenharmony_ci		return ret;
122862306a36Sopenharmony_ci	else if (ret2)
122962306a36Sopenharmony_ci		return ret2;
123062306a36Sopenharmony_ci	else
123162306a36Sopenharmony_ci		return 0;
123262306a36Sopenharmony_ci}
123362306a36Sopenharmony_ci
123462306a36Sopenharmony_ci/*
123562306a36Sopenharmony_ci * this is used to update the root pointer in the tree of tree roots.
123662306a36Sopenharmony_ci *
123762306a36Sopenharmony_ci * But, in the case of the extent allocation tree, updating the root
123862306a36Sopenharmony_ci * pointer may allocate blocks which may change the root of the extent
123962306a36Sopenharmony_ci * allocation tree.
124062306a36Sopenharmony_ci *
124162306a36Sopenharmony_ci * So, this loops and repeats and makes sure the cowonly root didn't
124262306a36Sopenharmony_ci * change while the root pointer was being updated in the metadata.
124362306a36Sopenharmony_ci */
124462306a36Sopenharmony_cistatic int update_cowonly_root(struct btrfs_trans_handle *trans,
124562306a36Sopenharmony_ci			       struct btrfs_root *root)
124662306a36Sopenharmony_ci{
124762306a36Sopenharmony_ci	int ret;
124862306a36Sopenharmony_ci	u64 old_root_bytenr;
124962306a36Sopenharmony_ci	u64 old_root_used;
125062306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = root->fs_info;
125162306a36Sopenharmony_ci	struct btrfs_root *tree_root = fs_info->tree_root;
125262306a36Sopenharmony_ci
125362306a36Sopenharmony_ci	old_root_used = btrfs_root_used(&root->root_item);
125462306a36Sopenharmony_ci
125562306a36Sopenharmony_ci	while (1) {
125662306a36Sopenharmony_ci		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
125762306a36Sopenharmony_ci		if (old_root_bytenr == root->node->start &&
125862306a36Sopenharmony_ci		    old_root_used == btrfs_root_used(&root->root_item))
125962306a36Sopenharmony_ci			break;
126062306a36Sopenharmony_ci
126162306a36Sopenharmony_ci		btrfs_set_root_node(&root->root_item, root->node);
126262306a36Sopenharmony_ci		ret = btrfs_update_root(trans, tree_root,
126362306a36Sopenharmony_ci					&root->root_key,
126462306a36Sopenharmony_ci					&root->root_item);
126562306a36Sopenharmony_ci		if (ret)
126662306a36Sopenharmony_ci			return ret;
126762306a36Sopenharmony_ci
126862306a36Sopenharmony_ci		old_root_used = btrfs_root_used(&root->root_item);
126962306a36Sopenharmony_ci	}
127062306a36Sopenharmony_ci
127162306a36Sopenharmony_ci	return 0;
127262306a36Sopenharmony_ci}
127362306a36Sopenharmony_ci
127462306a36Sopenharmony_ci/*
127562306a36Sopenharmony_ci * update all the cowonly tree roots on disk
127662306a36Sopenharmony_ci *
127762306a36Sopenharmony_ci * The error handling in this function may not be obvious. Any of the
127862306a36Sopenharmony_ci * failures will cause the file system to go offline. We still need
127962306a36Sopenharmony_ci * to clean up the delayed refs.
128062306a36Sopenharmony_ci */
128162306a36Sopenharmony_cistatic noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans)
128262306a36Sopenharmony_ci{
128362306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
128462306a36Sopenharmony_ci	struct list_head *dirty_bgs = &trans->transaction->dirty_bgs;
128562306a36Sopenharmony_ci	struct list_head *io_bgs = &trans->transaction->io_bgs;
128662306a36Sopenharmony_ci	struct list_head *next;
128762306a36Sopenharmony_ci	struct extent_buffer *eb;
128862306a36Sopenharmony_ci	int ret;
128962306a36Sopenharmony_ci
129062306a36Sopenharmony_ci	/*
129162306a36Sopenharmony_ci	 * At this point no one can be using this transaction to modify any tree
129262306a36Sopenharmony_ci	 * and no one can start another transaction to modify any tree either.
129362306a36Sopenharmony_ci	 */
129462306a36Sopenharmony_ci	ASSERT(trans->transaction->state == TRANS_STATE_COMMIT_DOING);
129562306a36Sopenharmony_ci
129662306a36Sopenharmony_ci	eb = btrfs_lock_root_node(fs_info->tree_root);
129762306a36Sopenharmony_ci	ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
129862306a36Sopenharmony_ci			      0, &eb, BTRFS_NESTING_COW);
129962306a36Sopenharmony_ci	btrfs_tree_unlock(eb);
130062306a36Sopenharmony_ci	free_extent_buffer(eb);
130162306a36Sopenharmony_ci
130262306a36Sopenharmony_ci	if (ret)
130362306a36Sopenharmony_ci		return ret;
130462306a36Sopenharmony_ci
130562306a36Sopenharmony_ci	ret = btrfs_run_dev_stats(trans);
130662306a36Sopenharmony_ci	if (ret)
130762306a36Sopenharmony_ci		return ret;
130862306a36Sopenharmony_ci	ret = btrfs_run_dev_replace(trans);
130962306a36Sopenharmony_ci	if (ret)
131062306a36Sopenharmony_ci		return ret;
131162306a36Sopenharmony_ci	ret = btrfs_run_qgroups(trans);
131262306a36Sopenharmony_ci	if (ret)
131362306a36Sopenharmony_ci		return ret;
131462306a36Sopenharmony_ci
131562306a36Sopenharmony_ci	ret = btrfs_setup_space_cache(trans);
131662306a36Sopenharmony_ci	if (ret)
131762306a36Sopenharmony_ci		return ret;
131862306a36Sopenharmony_ci
131962306a36Sopenharmony_ciagain:
132062306a36Sopenharmony_ci	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
132162306a36Sopenharmony_ci		struct btrfs_root *root;
132262306a36Sopenharmony_ci		next = fs_info->dirty_cowonly_roots.next;
132362306a36Sopenharmony_ci		list_del_init(next);
132462306a36Sopenharmony_ci		root = list_entry(next, struct btrfs_root, dirty_list);
132562306a36Sopenharmony_ci		clear_bit(BTRFS_ROOT_DIRTY, &root->state);
132662306a36Sopenharmony_ci
132762306a36Sopenharmony_ci		list_add_tail(&root->dirty_list,
132862306a36Sopenharmony_ci			      &trans->transaction->switch_commits);
132962306a36Sopenharmony_ci		ret = update_cowonly_root(trans, root);
133062306a36Sopenharmony_ci		if (ret)
133162306a36Sopenharmony_ci			return ret;
133262306a36Sopenharmony_ci	}
133362306a36Sopenharmony_ci
133462306a36Sopenharmony_ci	/* Now flush any delayed refs generated by updating all of the roots */
133562306a36Sopenharmony_ci	ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
133662306a36Sopenharmony_ci	if (ret)
133762306a36Sopenharmony_ci		return ret;
133862306a36Sopenharmony_ci
133962306a36Sopenharmony_ci	while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) {
134062306a36Sopenharmony_ci		ret = btrfs_write_dirty_block_groups(trans);
134162306a36Sopenharmony_ci		if (ret)
134262306a36Sopenharmony_ci			return ret;
134362306a36Sopenharmony_ci
134462306a36Sopenharmony_ci		/*
134562306a36Sopenharmony_ci		 * We're writing the dirty block groups, which could generate
134662306a36Sopenharmony_ci		 * delayed refs, which could generate more dirty block groups,
134762306a36Sopenharmony_ci		 * so we want to keep this flushing in this loop to make sure
134862306a36Sopenharmony_ci		 * everything gets run.
134962306a36Sopenharmony_ci		 */
135062306a36Sopenharmony_ci		ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
135162306a36Sopenharmony_ci		if (ret)
135262306a36Sopenharmony_ci			return ret;
135362306a36Sopenharmony_ci	}
135462306a36Sopenharmony_ci
135562306a36Sopenharmony_ci	if (!list_empty(&fs_info->dirty_cowonly_roots))
135662306a36Sopenharmony_ci		goto again;
135762306a36Sopenharmony_ci
135862306a36Sopenharmony_ci	/* Update dev-replace pointer once everything is committed */
135962306a36Sopenharmony_ci	fs_info->dev_replace.committed_cursor_left =
136062306a36Sopenharmony_ci		fs_info->dev_replace.cursor_left_last_write_of_item;
136162306a36Sopenharmony_ci
136262306a36Sopenharmony_ci	return 0;
136362306a36Sopenharmony_ci}
136462306a36Sopenharmony_ci
136562306a36Sopenharmony_ci/*
136662306a36Sopenharmony_ci * If we had a pending drop we need to see if there are any others left in our
136762306a36Sopenharmony_ci * dead roots list, and if not clear our bit and wake any waiters.
136862306a36Sopenharmony_ci */
136962306a36Sopenharmony_civoid btrfs_maybe_wake_unfinished_drop(struct btrfs_fs_info *fs_info)
137062306a36Sopenharmony_ci{
137162306a36Sopenharmony_ci	/*
137262306a36Sopenharmony_ci	 * We put the drop in progress roots at the front of the list, so if the
137362306a36Sopenharmony_ci	 * first entry doesn't have UNFINISHED_DROP set we can wake everybody
137462306a36Sopenharmony_ci	 * up.
137562306a36Sopenharmony_ci	 */
137662306a36Sopenharmony_ci	spin_lock(&fs_info->trans_lock);
137762306a36Sopenharmony_ci	if (!list_empty(&fs_info->dead_roots)) {
137862306a36Sopenharmony_ci		struct btrfs_root *root = list_first_entry(&fs_info->dead_roots,
137962306a36Sopenharmony_ci							   struct btrfs_root,
138062306a36Sopenharmony_ci							   root_list);
138162306a36Sopenharmony_ci		if (test_bit(BTRFS_ROOT_UNFINISHED_DROP, &root->state)) {
138262306a36Sopenharmony_ci			spin_unlock(&fs_info->trans_lock);
138362306a36Sopenharmony_ci			return;
138462306a36Sopenharmony_ci		}
138562306a36Sopenharmony_ci	}
138662306a36Sopenharmony_ci	spin_unlock(&fs_info->trans_lock);
138762306a36Sopenharmony_ci
138862306a36Sopenharmony_ci	btrfs_wake_unfinished_drop(fs_info);
138962306a36Sopenharmony_ci}
139062306a36Sopenharmony_ci
139162306a36Sopenharmony_ci/*
139262306a36Sopenharmony_ci * dead roots are old snapshots that need to be deleted.  This allocates
139362306a36Sopenharmony_ci * a dirty root struct and adds it into the list of dead roots that need to
139462306a36Sopenharmony_ci * be deleted
139562306a36Sopenharmony_ci */
139662306a36Sopenharmony_civoid btrfs_add_dead_root(struct btrfs_root *root)
139762306a36Sopenharmony_ci{
139862306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = root->fs_info;
139962306a36Sopenharmony_ci
140062306a36Sopenharmony_ci	spin_lock(&fs_info->trans_lock);
140162306a36Sopenharmony_ci	if (list_empty(&root->root_list)) {
140262306a36Sopenharmony_ci		btrfs_grab_root(root);
140362306a36Sopenharmony_ci
140462306a36Sopenharmony_ci		/* We want to process the partially complete drops first. */
140562306a36Sopenharmony_ci		if (test_bit(BTRFS_ROOT_UNFINISHED_DROP, &root->state))
140662306a36Sopenharmony_ci			list_add(&root->root_list, &fs_info->dead_roots);
140762306a36Sopenharmony_ci		else
140862306a36Sopenharmony_ci			list_add_tail(&root->root_list, &fs_info->dead_roots);
140962306a36Sopenharmony_ci	}
141062306a36Sopenharmony_ci	spin_unlock(&fs_info->trans_lock);
141162306a36Sopenharmony_ci}
141262306a36Sopenharmony_ci
141362306a36Sopenharmony_ci/*
141462306a36Sopenharmony_ci * Update each subvolume root and its relocation root, if it exists, in the tree
141562306a36Sopenharmony_ci * of tree roots. Also free log roots if they exist.
141662306a36Sopenharmony_ci */
141762306a36Sopenharmony_cistatic noinline int commit_fs_roots(struct btrfs_trans_handle *trans)
141862306a36Sopenharmony_ci{
141962306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
142062306a36Sopenharmony_ci	struct btrfs_root *gang[8];
142162306a36Sopenharmony_ci	int i;
142262306a36Sopenharmony_ci	int ret;
142362306a36Sopenharmony_ci
142462306a36Sopenharmony_ci	/*
142562306a36Sopenharmony_ci	 * At this point no one can be using this transaction to modify any tree
142662306a36Sopenharmony_ci	 * and no one can start another transaction to modify any tree either.
142762306a36Sopenharmony_ci	 */
142862306a36Sopenharmony_ci	ASSERT(trans->transaction->state == TRANS_STATE_COMMIT_DOING);
142962306a36Sopenharmony_ci
143062306a36Sopenharmony_ci	spin_lock(&fs_info->fs_roots_radix_lock);
143162306a36Sopenharmony_ci	while (1) {
143262306a36Sopenharmony_ci		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
143362306a36Sopenharmony_ci						 (void **)gang, 0,
143462306a36Sopenharmony_ci						 ARRAY_SIZE(gang),
143562306a36Sopenharmony_ci						 BTRFS_ROOT_TRANS_TAG);
143662306a36Sopenharmony_ci		if (ret == 0)
143762306a36Sopenharmony_ci			break;
143862306a36Sopenharmony_ci		for (i = 0; i < ret; i++) {
143962306a36Sopenharmony_ci			struct btrfs_root *root = gang[i];
144062306a36Sopenharmony_ci			int ret2;
144162306a36Sopenharmony_ci
144262306a36Sopenharmony_ci			/*
144362306a36Sopenharmony_ci			 * At this point we can neither have tasks logging inodes
144462306a36Sopenharmony_ci			 * from a root nor trying to commit a log tree.
144562306a36Sopenharmony_ci			 */
144662306a36Sopenharmony_ci			ASSERT(atomic_read(&root->log_writers) == 0);
144762306a36Sopenharmony_ci			ASSERT(atomic_read(&root->log_commit[0]) == 0);
144862306a36Sopenharmony_ci			ASSERT(atomic_read(&root->log_commit[1]) == 0);
144962306a36Sopenharmony_ci
145062306a36Sopenharmony_ci			radix_tree_tag_clear(&fs_info->fs_roots_radix,
145162306a36Sopenharmony_ci					(unsigned long)root->root_key.objectid,
145262306a36Sopenharmony_ci					BTRFS_ROOT_TRANS_TAG);
145362306a36Sopenharmony_ci			spin_unlock(&fs_info->fs_roots_radix_lock);
145462306a36Sopenharmony_ci
145562306a36Sopenharmony_ci			btrfs_free_log(trans, root);
145662306a36Sopenharmony_ci			ret2 = btrfs_update_reloc_root(trans, root);
145762306a36Sopenharmony_ci			if (ret2)
145862306a36Sopenharmony_ci				return ret2;
145962306a36Sopenharmony_ci
146062306a36Sopenharmony_ci			/* see comments in should_cow_block() */
146162306a36Sopenharmony_ci			clear_bit(BTRFS_ROOT_FORCE_COW, &root->state);
146262306a36Sopenharmony_ci			smp_mb__after_atomic();
146362306a36Sopenharmony_ci
146462306a36Sopenharmony_ci			if (root->commit_root != root->node) {
146562306a36Sopenharmony_ci				list_add_tail(&root->dirty_list,
146662306a36Sopenharmony_ci					&trans->transaction->switch_commits);
146762306a36Sopenharmony_ci				btrfs_set_root_node(&root->root_item,
146862306a36Sopenharmony_ci						    root->node);
146962306a36Sopenharmony_ci			}
147062306a36Sopenharmony_ci
147162306a36Sopenharmony_ci			ret2 = btrfs_update_root(trans, fs_info->tree_root,
147262306a36Sopenharmony_ci						&root->root_key,
147362306a36Sopenharmony_ci						&root->root_item);
147462306a36Sopenharmony_ci			if (ret2)
147562306a36Sopenharmony_ci				return ret2;
147662306a36Sopenharmony_ci			spin_lock(&fs_info->fs_roots_radix_lock);
147762306a36Sopenharmony_ci			btrfs_qgroup_free_meta_all_pertrans(root);
147862306a36Sopenharmony_ci		}
147962306a36Sopenharmony_ci	}
148062306a36Sopenharmony_ci	spin_unlock(&fs_info->fs_roots_radix_lock);
148162306a36Sopenharmony_ci	return 0;
148262306a36Sopenharmony_ci}
148362306a36Sopenharmony_ci
148462306a36Sopenharmony_ci/*
148562306a36Sopenharmony_ci * defrag a given btree.
148662306a36Sopenharmony_ci * Every leaf in the btree is read and defragged.
148762306a36Sopenharmony_ci */
148862306a36Sopenharmony_ciint btrfs_defrag_root(struct btrfs_root *root)
148962306a36Sopenharmony_ci{
149062306a36Sopenharmony_ci	struct btrfs_fs_info *info = root->fs_info;
149162306a36Sopenharmony_ci	struct btrfs_trans_handle *trans;
149262306a36Sopenharmony_ci	int ret;
149362306a36Sopenharmony_ci
149462306a36Sopenharmony_ci	if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state))
149562306a36Sopenharmony_ci		return 0;
149662306a36Sopenharmony_ci
149762306a36Sopenharmony_ci	while (1) {
149862306a36Sopenharmony_ci		trans = btrfs_start_transaction(root, 0);
149962306a36Sopenharmony_ci		if (IS_ERR(trans)) {
150062306a36Sopenharmony_ci			ret = PTR_ERR(trans);
150162306a36Sopenharmony_ci			break;
150262306a36Sopenharmony_ci		}
150362306a36Sopenharmony_ci
150462306a36Sopenharmony_ci		ret = btrfs_defrag_leaves(trans, root);
150562306a36Sopenharmony_ci
150662306a36Sopenharmony_ci		btrfs_end_transaction(trans);
150762306a36Sopenharmony_ci		btrfs_btree_balance_dirty(info);
150862306a36Sopenharmony_ci		cond_resched();
150962306a36Sopenharmony_ci
151062306a36Sopenharmony_ci		if (btrfs_fs_closing(info) || ret != -EAGAIN)
151162306a36Sopenharmony_ci			break;
151262306a36Sopenharmony_ci
151362306a36Sopenharmony_ci		if (btrfs_defrag_cancelled(info)) {
151462306a36Sopenharmony_ci			btrfs_debug(info, "defrag_root cancelled");
151562306a36Sopenharmony_ci			ret = -EAGAIN;
151662306a36Sopenharmony_ci			break;
151762306a36Sopenharmony_ci		}
151862306a36Sopenharmony_ci	}
151962306a36Sopenharmony_ci	clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state);
152062306a36Sopenharmony_ci	return ret;
152162306a36Sopenharmony_ci}
152262306a36Sopenharmony_ci
152362306a36Sopenharmony_ci/*
152462306a36Sopenharmony_ci * Do all special snapshot related qgroup dirty hack.
152562306a36Sopenharmony_ci *
152662306a36Sopenharmony_ci * Will do all needed qgroup inherit and dirty hack like switch commit
152762306a36Sopenharmony_ci * roots inside one transaction and write all btree into disk, to make
152862306a36Sopenharmony_ci * qgroup works.
152962306a36Sopenharmony_ci */
153062306a36Sopenharmony_cistatic int qgroup_account_snapshot(struct btrfs_trans_handle *trans,
153162306a36Sopenharmony_ci				   struct btrfs_root *src,
153262306a36Sopenharmony_ci				   struct btrfs_root *parent,
153362306a36Sopenharmony_ci				   struct btrfs_qgroup_inherit *inherit,
153462306a36Sopenharmony_ci				   u64 dst_objectid)
153562306a36Sopenharmony_ci{
153662306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = src->fs_info;
153762306a36Sopenharmony_ci	int ret;
153862306a36Sopenharmony_ci
153962306a36Sopenharmony_ci	/*
154062306a36Sopenharmony_ci	 * Save some performance in the case that qgroups are not
154162306a36Sopenharmony_ci	 * enabled. If this check races with the ioctl, rescan will
154262306a36Sopenharmony_ci	 * kick in anyway.
154362306a36Sopenharmony_ci	 */
154462306a36Sopenharmony_ci	if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
154562306a36Sopenharmony_ci		return 0;
154662306a36Sopenharmony_ci
154762306a36Sopenharmony_ci	/*
154862306a36Sopenharmony_ci	 * Ensure dirty @src will be committed.  Or, after coming
154962306a36Sopenharmony_ci	 * commit_fs_roots() and switch_commit_roots(), any dirty but not
155062306a36Sopenharmony_ci	 * recorded root will never be updated again, causing an outdated root
155162306a36Sopenharmony_ci	 * item.
155262306a36Sopenharmony_ci	 */
155362306a36Sopenharmony_ci	ret = record_root_in_trans(trans, src, 1);
155462306a36Sopenharmony_ci	if (ret)
155562306a36Sopenharmony_ci		return ret;
155662306a36Sopenharmony_ci
155762306a36Sopenharmony_ci	/*
155862306a36Sopenharmony_ci	 * btrfs_qgroup_inherit relies on a consistent view of the usage for the
155962306a36Sopenharmony_ci	 * src root, so we must run the delayed refs here.
156062306a36Sopenharmony_ci	 *
156162306a36Sopenharmony_ci	 * However this isn't particularly fool proof, because there's no
156262306a36Sopenharmony_ci	 * synchronization keeping us from changing the tree after this point
156362306a36Sopenharmony_ci	 * before we do the qgroup_inherit, or even from making changes while
156462306a36Sopenharmony_ci	 * we're doing the qgroup_inherit.  But that's a problem for the future,
156562306a36Sopenharmony_ci	 * for now flush the delayed refs to narrow the race window where the
156662306a36Sopenharmony_ci	 * qgroup counters could end up wrong.
156762306a36Sopenharmony_ci	 */
156862306a36Sopenharmony_ci	ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
156962306a36Sopenharmony_ci	if (ret) {
157062306a36Sopenharmony_ci		btrfs_abort_transaction(trans, ret);
157162306a36Sopenharmony_ci		return ret;
157262306a36Sopenharmony_ci	}
157362306a36Sopenharmony_ci
157462306a36Sopenharmony_ci	ret = commit_fs_roots(trans);
157562306a36Sopenharmony_ci	if (ret)
157662306a36Sopenharmony_ci		goto out;
157762306a36Sopenharmony_ci	ret = btrfs_qgroup_account_extents(trans);
157862306a36Sopenharmony_ci	if (ret < 0)
157962306a36Sopenharmony_ci		goto out;
158062306a36Sopenharmony_ci
158162306a36Sopenharmony_ci	/* Now qgroup are all updated, we can inherit it to new qgroups */
158262306a36Sopenharmony_ci	ret = btrfs_qgroup_inherit(trans, src->root_key.objectid, dst_objectid,
158362306a36Sopenharmony_ci				   inherit);
158462306a36Sopenharmony_ci	if (ret < 0)
158562306a36Sopenharmony_ci		goto out;
158662306a36Sopenharmony_ci
158762306a36Sopenharmony_ci	/*
158862306a36Sopenharmony_ci	 * Now we do a simplified commit transaction, which will:
158962306a36Sopenharmony_ci	 * 1) commit all subvolume and extent tree
159062306a36Sopenharmony_ci	 *    To ensure all subvolume and extent tree have a valid
159162306a36Sopenharmony_ci	 *    commit_root to accounting later insert_dir_item()
159262306a36Sopenharmony_ci	 * 2) write all btree blocks onto disk
159362306a36Sopenharmony_ci	 *    This is to make sure later btree modification will be cowed
159462306a36Sopenharmony_ci	 *    Or commit_root can be populated and cause wrong qgroup numbers
159562306a36Sopenharmony_ci	 * In this simplified commit, we don't really care about other trees
159662306a36Sopenharmony_ci	 * like chunk and root tree, as they won't affect qgroup.
159762306a36Sopenharmony_ci	 * And we don't write super to avoid half committed status.
159862306a36Sopenharmony_ci	 */
159962306a36Sopenharmony_ci	ret = commit_cowonly_roots(trans);
160062306a36Sopenharmony_ci	if (ret)
160162306a36Sopenharmony_ci		goto out;
160262306a36Sopenharmony_ci	switch_commit_roots(trans);
160362306a36Sopenharmony_ci	ret = btrfs_write_and_wait_transaction(trans);
160462306a36Sopenharmony_ci	if (ret)
160562306a36Sopenharmony_ci		btrfs_handle_fs_error(fs_info, ret,
160662306a36Sopenharmony_ci			"Error while writing out transaction for qgroup");
160762306a36Sopenharmony_ci
160862306a36Sopenharmony_ciout:
160962306a36Sopenharmony_ci	/*
161062306a36Sopenharmony_ci	 * Force parent root to be updated, as we recorded it before so its
161162306a36Sopenharmony_ci	 * last_trans == cur_transid.
161262306a36Sopenharmony_ci	 * Or it won't be committed again onto disk after later
161362306a36Sopenharmony_ci	 * insert_dir_item()
161462306a36Sopenharmony_ci	 */
161562306a36Sopenharmony_ci	if (!ret)
161662306a36Sopenharmony_ci		ret = record_root_in_trans(trans, parent, 1);
161762306a36Sopenharmony_ci	return ret;
161862306a36Sopenharmony_ci}
161962306a36Sopenharmony_ci
162062306a36Sopenharmony_ci/*
162162306a36Sopenharmony_ci * new snapshots need to be created at a very specific time in the
162262306a36Sopenharmony_ci * transaction commit.  This does the actual creation.
162362306a36Sopenharmony_ci *
162462306a36Sopenharmony_ci * Note:
162562306a36Sopenharmony_ci * If the error which may affect the commitment of the current transaction
162662306a36Sopenharmony_ci * happens, we should return the error number. If the error which just affect
162762306a36Sopenharmony_ci * the creation of the pending snapshots, just return 0.
162862306a36Sopenharmony_ci */
162962306a36Sopenharmony_cistatic noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
163062306a36Sopenharmony_ci				   struct btrfs_pending_snapshot *pending)
163162306a36Sopenharmony_ci{
163262306a36Sopenharmony_ci
163362306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
163462306a36Sopenharmony_ci	struct btrfs_key key;
163562306a36Sopenharmony_ci	struct btrfs_root_item *new_root_item;
163662306a36Sopenharmony_ci	struct btrfs_root *tree_root = fs_info->tree_root;
163762306a36Sopenharmony_ci	struct btrfs_root *root = pending->root;
163862306a36Sopenharmony_ci	struct btrfs_root *parent_root;
163962306a36Sopenharmony_ci	struct btrfs_block_rsv *rsv;
164062306a36Sopenharmony_ci	struct inode *parent_inode = pending->dir;
164162306a36Sopenharmony_ci	struct btrfs_path *path;
164262306a36Sopenharmony_ci	struct btrfs_dir_item *dir_item;
164362306a36Sopenharmony_ci	struct extent_buffer *tmp;
164462306a36Sopenharmony_ci	struct extent_buffer *old;
164562306a36Sopenharmony_ci	struct timespec64 cur_time;
164662306a36Sopenharmony_ci	int ret = 0;
164762306a36Sopenharmony_ci	u64 to_reserve = 0;
164862306a36Sopenharmony_ci	u64 index = 0;
164962306a36Sopenharmony_ci	u64 objectid;
165062306a36Sopenharmony_ci	u64 root_flags;
165162306a36Sopenharmony_ci	unsigned int nofs_flags;
165262306a36Sopenharmony_ci	struct fscrypt_name fname;
165362306a36Sopenharmony_ci
165462306a36Sopenharmony_ci	ASSERT(pending->path);
165562306a36Sopenharmony_ci	path = pending->path;
165662306a36Sopenharmony_ci
165762306a36Sopenharmony_ci	ASSERT(pending->root_item);
165862306a36Sopenharmony_ci	new_root_item = pending->root_item;
165962306a36Sopenharmony_ci
166062306a36Sopenharmony_ci	/*
166162306a36Sopenharmony_ci	 * We're inside a transaction and must make sure that any potential
166262306a36Sopenharmony_ci	 * allocations with GFP_KERNEL in fscrypt won't recurse back to
166362306a36Sopenharmony_ci	 * filesystem.
166462306a36Sopenharmony_ci	 */
166562306a36Sopenharmony_ci	nofs_flags = memalloc_nofs_save();
166662306a36Sopenharmony_ci	pending->error = fscrypt_setup_filename(parent_inode,
166762306a36Sopenharmony_ci						&pending->dentry->d_name, 0,
166862306a36Sopenharmony_ci						&fname);
166962306a36Sopenharmony_ci	memalloc_nofs_restore(nofs_flags);
167062306a36Sopenharmony_ci	if (pending->error)
167162306a36Sopenharmony_ci		goto free_pending;
167262306a36Sopenharmony_ci
167362306a36Sopenharmony_ci	pending->error = btrfs_get_free_objectid(tree_root, &objectid);
167462306a36Sopenharmony_ci	if (pending->error)
167562306a36Sopenharmony_ci		goto free_fname;
167662306a36Sopenharmony_ci
167762306a36Sopenharmony_ci	/*
167862306a36Sopenharmony_ci	 * Make qgroup to skip current new snapshot's qgroupid, as it is
167962306a36Sopenharmony_ci	 * accounted by later btrfs_qgroup_inherit().
168062306a36Sopenharmony_ci	 */
168162306a36Sopenharmony_ci	btrfs_set_skip_qgroup(trans, objectid);
168262306a36Sopenharmony_ci
168362306a36Sopenharmony_ci	btrfs_reloc_pre_snapshot(pending, &to_reserve);
168462306a36Sopenharmony_ci
168562306a36Sopenharmony_ci	if (to_reserve > 0) {
168662306a36Sopenharmony_ci		pending->error = btrfs_block_rsv_add(fs_info,
168762306a36Sopenharmony_ci						     &pending->block_rsv,
168862306a36Sopenharmony_ci						     to_reserve,
168962306a36Sopenharmony_ci						     BTRFS_RESERVE_NO_FLUSH);
169062306a36Sopenharmony_ci		if (pending->error)
169162306a36Sopenharmony_ci			goto clear_skip_qgroup;
169262306a36Sopenharmony_ci	}
169362306a36Sopenharmony_ci
169462306a36Sopenharmony_ci	key.objectid = objectid;
169562306a36Sopenharmony_ci	key.offset = (u64)-1;
169662306a36Sopenharmony_ci	key.type = BTRFS_ROOT_ITEM_KEY;
169762306a36Sopenharmony_ci
169862306a36Sopenharmony_ci	rsv = trans->block_rsv;
169962306a36Sopenharmony_ci	trans->block_rsv = &pending->block_rsv;
170062306a36Sopenharmony_ci	trans->bytes_reserved = trans->block_rsv->reserved;
170162306a36Sopenharmony_ci	trace_btrfs_space_reservation(fs_info, "transaction",
170262306a36Sopenharmony_ci				      trans->transid,
170362306a36Sopenharmony_ci				      trans->bytes_reserved, 1);
170462306a36Sopenharmony_ci	parent_root = BTRFS_I(parent_inode)->root;
170562306a36Sopenharmony_ci	ret = record_root_in_trans(trans, parent_root, 0);
170662306a36Sopenharmony_ci	if (ret)
170762306a36Sopenharmony_ci		goto fail;
170862306a36Sopenharmony_ci	cur_time = current_time(parent_inode);
170962306a36Sopenharmony_ci
171062306a36Sopenharmony_ci	/*
171162306a36Sopenharmony_ci	 * insert the directory item
171262306a36Sopenharmony_ci	 */
171362306a36Sopenharmony_ci	ret = btrfs_set_inode_index(BTRFS_I(parent_inode), &index);
171462306a36Sopenharmony_ci	if (ret) {
171562306a36Sopenharmony_ci		btrfs_abort_transaction(trans, ret);
171662306a36Sopenharmony_ci		goto fail;
171762306a36Sopenharmony_ci	}
171862306a36Sopenharmony_ci
171962306a36Sopenharmony_ci	/* check if there is a file/dir which has the same name. */
172062306a36Sopenharmony_ci	dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
172162306a36Sopenharmony_ci					 btrfs_ino(BTRFS_I(parent_inode)),
172262306a36Sopenharmony_ci					 &fname.disk_name, 0);
172362306a36Sopenharmony_ci	if (dir_item != NULL && !IS_ERR(dir_item)) {
172462306a36Sopenharmony_ci		pending->error = -EEXIST;
172562306a36Sopenharmony_ci		goto dir_item_existed;
172662306a36Sopenharmony_ci	} else if (IS_ERR(dir_item)) {
172762306a36Sopenharmony_ci		ret = PTR_ERR(dir_item);
172862306a36Sopenharmony_ci		btrfs_abort_transaction(trans, ret);
172962306a36Sopenharmony_ci		goto fail;
173062306a36Sopenharmony_ci	}
173162306a36Sopenharmony_ci	btrfs_release_path(path);
173262306a36Sopenharmony_ci
173362306a36Sopenharmony_ci	/*
173462306a36Sopenharmony_ci	 * pull in the delayed directory update
173562306a36Sopenharmony_ci	 * and the delayed inode item
173662306a36Sopenharmony_ci	 * otherwise we corrupt the FS during
173762306a36Sopenharmony_ci	 * snapshot
173862306a36Sopenharmony_ci	 */
173962306a36Sopenharmony_ci	ret = btrfs_run_delayed_items(trans);
174062306a36Sopenharmony_ci	if (ret) {	/* Transaction aborted */
174162306a36Sopenharmony_ci		btrfs_abort_transaction(trans, ret);
174262306a36Sopenharmony_ci		goto fail;
174362306a36Sopenharmony_ci	}
174462306a36Sopenharmony_ci
174562306a36Sopenharmony_ci	ret = record_root_in_trans(trans, root, 0);
174662306a36Sopenharmony_ci	if (ret) {
174762306a36Sopenharmony_ci		btrfs_abort_transaction(trans, ret);
174862306a36Sopenharmony_ci		goto fail;
174962306a36Sopenharmony_ci	}
175062306a36Sopenharmony_ci	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
175162306a36Sopenharmony_ci	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
175262306a36Sopenharmony_ci	btrfs_check_and_init_root_item(new_root_item);
175362306a36Sopenharmony_ci
175462306a36Sopenharmony_ci	root_flags = btrfs_root_flags(new_root_item);
175562306a36Sopenharmony_ci	if (pending->readonly)
175662306a36Sopenharmony_ci		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
175762306a36Sopenharmony_ci	else
175862306a36Sopenharmony_ci		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
175962306a36Sopenharmony_ci	btrfs_set_root_flags(new_root_item, root_flags);
176062306a36Sopenharmony_ci
176162306a36Sopenharmony_ci	btrfs_set_root_generation_v2(new_root_item,
176262306a36Sopenharmony_ci			trans->transid);
176362306a36Sopenharmony_ci	generate_random_guid(new_root_item->uuid);
176462306a36Sopenharmony_ci	memcpy(new_root_item->parent_uuid, root->root_item.uuid,
176562306a36Sopenharmony_ci			BTRFS_UUID_SIZE);
176662306a36Sopenharmony_ci	if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
176762306a36Sopenharmony_ci		memset(new_root_item->received_uuid, 0,
176862306a36Sopenharmony_ci		       sizeof(new_root_item->received_uuid));
176962306a36Sopenharmony_ci		memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
177062306a36Sopenharmony_ci		memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
177162306a36Sopenharmony_ci		btrfs_set_root_stransid(new_root_item, 0);
177262306a36Sopenharmony_ci		btrfs_set_root_rtransid(new_root_item, 0);
177362306a36Sopenharmony_ci	}
177462306a36Sopenharmony_ci	btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec);
177562306a36Sopenharmony_ci	btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec);
177662306a36Sopenharmony_ci	btrfs_set_root_otransid(new_root_item, trans->transid);
177762306a36Sopenharmony_ci
177862306a36Sopenharmony_ci	old = btrfs_lock_root_node(root);
177962306a36Sopenharmony_ci	ret = btrfs_cow_block(trans, root, old, NULL, 0, &old,
178062306a36Sopenharmony_ci			      BTRFS_NESTING_COW);
178162306a36Sopenharmony_ci	if (ret) {
178262306a36Sopenharmony_ci		btrfs_tree_unlock(old);
178362306a36Sopenharmony_ci		free_extent_buffer(old);
178462306a36Sopenharmony_ci		btrfs_abort_transaction(trans, ret);
178562306a36Sopenharmony_ci		goto fail;
178662306a36Sopenharmony_ci	}
178762306a36Sopenharmony_ci
178862306a36Sopenharmony_ci	ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
178962306a36Sopenharmony_ci	/* clean up in any case */
179062306a36Sopenharmony_ci	btrfs_tree_unlock(old);
179162306a36Sopenharmony_ci	free_extent_buffer(old);
179262306a36Sopenharmony_ci	if (ret) {
179362306a36Sopenharmony_ci		btrfs_abort_transaction(trans, ret);
179462306a36Sopenharmony_ci		goto fail;
179562306a36Sopenharmony_ci	}
179662306a36Sopenharmony_ci	/* see comments in should_cow_block() */
179762306a36Sopenharmony_ci	set_bit(BTRFS_ROOT_FORCE_COW, &root->state);
179862306a36Sopenharmony_ci	smp_wmb();
179962306a36Sopenharmony_ci
180062306a36Sopenharmony_ci	btrfs_set_root_node(new_root_item, tmp);
180162306a36Sopenharmony_ci	/* record when the snapshot was created in key.offset */
180262306a36Sopenharmony_ci	key.offset = trans->transid;
180362306a36Sopenharmony_ci	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
180462306a36Sopenharmony_ci	btrfs_tree_unlock(tmp);
180562306a36Sopenharmony_ci	free_extent_buffer(tmp);
180662306a36Sopenharmony_ci	if (ret) {
180762306a36Sopenharmony_ci		btrfs_abort_transaction(trans, ret);
180862306a36Sopenharmony_ci		goto fail;
180962306a36Sopenharmony_ci	}
181062306a36Sopenharmony_ci
181162306a36Sopenharmony_ci	/*
181262306a36Sopenharmony_ci	 * insert root back/forward references
181362306a36Sopenharmony_ci	 */
181462306a36Sopenharmony_ci	ret = btrfs_add_root_ref(trans, objectid,
181562306a36Sopenharmony_ci				 parent_root->root_key.objectid,
181662306a36Sopenharmony_ci				 btrfs_ino(BTRFS_I(parent_inode)), index,
181762306a36Sopenharmony_ci				 &fname.disk_name);
181862306a36Sopenharmony_ci	if (ret) {
181962306a36Sopenharmony_ci		btrfs_abort_transaction(trans, ret);
182062306a36Sopenharmony_ci		goto fail;
182162306a36Sopenharmony_ci	}
182262306a36Sopenharmony_ci
182362306a36Sopenharmony_ci	key.offset = (u64)-1;
182462306a36Sopenharmony_ci	pending->snap = btrfs_get_new_fs_root(fs_info, objectid, &pending->anon_dev);
182562306a36Sopenharmony_ci	if (IS_ERR(pending->snap)) {
182662306a36Sopenharmony_ci		ret = PTR_ERR(pending->snap);
182762306a36Sopenharmony_ci		pending->snap = NULL;
182862306a36Sopenharmony_ci		btrfs_abort_transaction(trans, ret);
182962306a36Sopenharmony_ci		goto fail;
183062306a36Sopenharmony_ci	}
183162306a36Sopenharmony_ci
183262306a36Sopenharmony_ci	ret = btrfs_reloc_post_snapshot(trans, pending);
183362306a36Sopenharmony_ci	if (ret) {
183462306a36Sopenharmony_ci		btrfs_abort_transaction(trans, ret);
183562306a36Sopenharmony_ci		goto fail;
183662306a36Sopenharmony_ci	}
183762306a36Sopenharmony_ci
183862306a36Sopenharmony_ci	/*
183962306a36Sopenharmony_ci	 * Do special qgroup accounting for snapshot, as we do some qgroup
184062306a36Sopenharmony_ci	 * snapshot hack to do fast snapshot.
184162306a36Sopenharmony_ci	 * To co-operate with that hack, we do hack again.
184262306a36Sopenharmony_ci	 * Or snapshot will be greatly slowed down by a subtree qgroup rescan
184362306a36Sopenharmony_ci	 */
184462306a36Sopenharmony_ci	ret = qgroup_account_snapshot(trans, root, parent_root,
184562306a36Sopenharmony_ci				      pending->inherit, objectid);
184662306a36Sopenharmony_ci	if (ret < 0)
184762306a36Sopenharmony_ci		goto fail;
184862306a36Sopenharmony_ci
184962306a36Sopenharmony_ci	ret = btrfs_insert_dir_item(trans, &fname.disk_name,
185062306a36Sopenharmony_ci				    BTRFS_I(parent_inode), &key, BTRFS_FT_DIR,
185162306a36Sopenharmony_ci				    index);
185262306a36Sopenharmony_ci	/* We have check then name at the beginning, so it is impossible. */
185362306a36Sopenharmony_ci	BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
185462306a36Sopenharmony_ci	if (ret) {
185562306a36Sopenharmony_ci		btrfs_abort_transaction(trans, ret);
185662306a36Sopenharmony_ci		goto fail;
185762306a36Sopenharmony_ci	}
185862306a36Sopenharmony_ci
185962306a36Sopenharmony_ci	btrfs_i_size_write(BTRFS_I(parent_inode), parent_inode->i_size +
186062306a36Sopenharmony_ci						  fname.disk_name.len * 2);
186162306a36Sopenharmony_ci	parent_inode->i_mtime = inode_set_ctime_current(parent_inode);
186262306a36Sopenharmony_ci	ret = btrfs_update_inode_fallback(trans, parent_root, BTRFS_I(parent_inode));
186362306a36Sopenharmony_ci	if (ret) {
186462306a36Sopenharmony_ci		btrfs_abort_transaction(trans, ret);
186562306a36Sopenharmony_ci		goto fail;
186662306a36Sopenharmony_ci	}
186762306a36Sopenharmony_ci	ret = btrfs_uuid_tree_add(trans, new_root_item->uuid,
186862306a36Sopenharmony_ci				  BTRFS_UUID_KEY_SUBVOL,
186962306a36Sopenharmony_ci				  objectid);
187062306a36Sopenharmony_ci	if (ret) {
187162306a36Sopenharmony_ci		btrfs_abort_transaction(trans, ret);
187262306a36Sopenharmony_ci		goto fail;
187362306a36Sopenharmony_ci	}
187462306a36Sopenharmony_ci	if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) {
187562306a36Sopenharmony_ci		ret = btrfs_uuid_tree_add(trans, new_root_item->received_uuid,
187662306a36Sopenharmony_ci					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
187762306a36Sopenharmony_ci					  objectid);
187862306a36Sopenharmony_ci		if (ret && ret != -EEXIST) {
187962306a36Sopenharmony_ci			btrfs_abort_transaction(trans, ret);
188062306a36Sopenharmony_ci			goto fail;
188162306a36Sopenharmony_ci		}
188262306a36Sopenharmony_ci	}
188362306a36Sopenharmony_ci
188462306a36Sopenharmony_cifail:
188562306a36Sopenharmony_ci	pending->error = ret;
188662306a36Sopenharmony_cidir_item_existed:
188762306a36Sopenharmony_ci	trans->block_rsv = rsv;
188862306a36Sopenharmony_ci	trans->bytes_reserved = 0;
188962306a36Sopenharmony_ciclear_skip_qgroup:
189062306a36Sopenharmony_ci	btrfs_clear_skip_qgroup(trans);
189162306a36Sopenharmony_cifree_fname:
189262306a36Sopenharmony_ci	fscrypt_free_filename(&fname);
189362306a36Sopenharmony_cifree_pending:
189462306a36Sopenharmony_ci	kfree(new_root_item);
189562306a36Sopenharmony_ci	pending->root_item = NULL;
189662306a36Sopenharmony_ci	btrfs_free_path(path);
189762306a36Sopenharmony_ci	pending->path = NULL;
189862306a36Sopenharmony_ci
189962306a36Sopenharmony_ci	return ret;
190062306a36Sopenharmony_ci}
190162306a36Sopenharmony_ci
190262306a36Sopenharmony_ci/*
190362306a36Sopenharmony_ci * create all the snapshots we've scheduled for creation
190462306a36Sopenharmony_ci */
190562306a36Sopenharmony_cistatic noinline int create_pending_snapshots(struct btrfs_trans_handle *trans)
190662306a36Sopenharmony_ci{
190762306a36Sopenharmony_ci	struct btrfs_pending_snapshot *pending, *next;
190862306a36Sopenharmony_ci	struct list_head *head = &trans->transaction->pending_snapshots;
190962306a36Sopenharmony_ci	int ret = 0;
191062306a36Sopenharmony_ci
191162306a36Sopenharmony_ci	list_for_each_entry_safe(pending, next, head, list) {
191262306a36Sopenharmony_ci		list_del(&pending->list);
191362306a36Sopenharmony_ci		ret = create_pending_snapshot(trans, pending);
191462306a36Sopenharmony_ci		if (ret)
191562306a36Sopenharmony_ci			break;
191662306a36Sopenharmony_ci	}
191762306a36Sopenharmony_ci	return ret;
191862306a36Sopenharmony_ci}
191962306a36Sopenharmony_ci
192062306a36Sopenharmony_cistatic void update_super_roots(struct btrfs_fs_info *fs_info)
192162306a36Sopenharmony_ci{
192262306a36Sopenharmony_ci	struct btrfs_root_item *root_item;
192362306a36Sopenharmony_ci	struct btrfs_super_block *super;
192462306a36Sopenharmony_ci
192562306a36Sopenharmony_ci	super = fs_info->super_copy;
192662306a36Sopenharmony_ci
192762306a36Sopenharmony_ci	root_item = &fs_info->chunk_root->root_item;
192862306a36Sopenharmony_ci	super->chunk_root = root_item->bytenr;
192962306a36Sopenharmony_ci	super->chunk_root_generation = root_item->generation;
193062306a36Sopenharmony_ci	super->chunk_root_level = root_item->level;
193162306a36Sopenharmony_ci
193262306a36Sopenharmony_ci	root_item = &fs_info->tree_root->root_item;
193362306a36Sopenharmony_ci	super->root = root_item->bytenr;
193462306a36Sopenharmony_ci	super->generation = root_item->generation;
193562306a36Sopenharmony_ci	super->root_level = root_item->level;
193662306a36Sopenharmony_ci	if (btrfs_test_opt(fs_info, SPACE_CACHE))
193762306a36Sopenharmony_ci		super->cache_generation = root_item->generation;
193862306a36Sopenharmony_ci	else if (test_bit(BTRFS_FS_CLEANUP_SPACE_CACHE_V1, &fs_info->flags))
193962306a36Sopenharmony_ci		super->cache_generation = 0;
194062306a36Sopenharmony_ci	if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags))
194162306a36Sopenharmony_ci		super->uuid_tree_generation = root_item->generation;
194262306a36Sopenharmony_ci}
194362306a36Sopenharmony_ci
194462306a36Sopenharmony_ciint btrfs_transaction_in_commit(struct btrfs_fs_info *info)
194562306a36Sopenharmony_ci{
194662306a36Sopenharmony_ci	struct btrfs_transaction *trans;
194762306a36Sopenharmony_ci	int ret = 0;
194862306a36Sopenharmony_ci
194962306a36Sopenharmony_ci	spin_lock(&info->trans_lock);
195062306a36Sopenharmony_ci	trans = info->running_transaction;
195162306a36Sopenharmony_ci	if (trans)
195262306a36Sopenharmony_ci		ret = (trans->state >= TRANS_STATE_COMMIT_START);
195362306a36Sopenharmony_ci	spin_unlock(&info->trans_lock);
195462306a36Sopenharmony_ci	return ret;
195562306a36Sopenharmony_ci}
195662306a36Sopenharmony_ci
195762306a36Sopenharmony_ciint btrfs_transaction_blocked(struct btrfs_fs_info *info)
195862306a36Sopenharmony_ci{
195962306a36Sopenharmony_ci	struct btrfs_transaction *trans;
196062306a36Sopenharmony_ci	int ret = 0;
196162306a36Sopenharmony_ci
196262306a36Sopenharmony_ci	spin_lock(&info->trans_lock);
196362306a36Sopenharmony_ci	trans = info->running_transaction;
196462306a36Sopenharmony_ci	if (trans)
196562306a36Sopenharmony_ci		ret = is_transaction_blocked(trans);
196662306a36Sopenharmony_ci	spin_unlock(&info->trans_lock);
196762306a36Sopenharmony_ci	return ret;
196862306a36Sopenharmony_ci}
196962306a36Sopenharmony_ci
197062306a36Sopenharmony_civoid btrfs_commit_transaction_async(struct btrfs_trans_handle *trans)
197162306a36Sopenharmony_ci{
197262306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
197362306a36Sopenharmony_ci	struct btrfs_transaction *cur_trans;
197462306a36Sopenharmony_ci
197562306a36Sopenharmony_ci	/* Kick the transaction kthread. */
197662306a36Sopenharmony_ci	set_bit(BTRFS_FS_COMMIT_TRANS, &fs_info->flags);
197762306a36Sopenharmony_ci	wake_up_process(fs_info->transaction_kthread);
197862306a36Sopenharmony_ci
197962306a36Sopenharmony_ci	/* take transaction reference */
198062306a36Sopenharmony_ci	cur_trans = trans->transaction;
198162306a36Sopenharmony_ci	refcount_inc(&cur_trans->use_count);
198262306a36Sopenharmony_ci
198362306a36Sopenharmony_ci	btrfs_end_transaction(trans);
198462306a36Sopenharmony_ci
198562306a36Sopenharmony_ci	/*
198662306a36Sopenharmony_ci	 * Wait for the current transaction commit to start and block
198762306a36Sopenharmony_ci	 * subsequent transaction joins
198862306a36Sopenharmony_ci	 */
198962306a36Sopenharmony_ci	btrfs_might_wait_for_state(fs_info, BTRFS_LOCKDEP_TRANS_COMMIT_PREP);
199062306a36Sopenharmony_ci	wait_event(fs_info->transaction_blocked_wait,
199162306a36Sopenharmony_ci		   cur_trans->state >= TRANS_STATE_COMMIT_START ||
199262306a36Sopenharmony_ci		   TRANS_ABORTED(cur_trans));
199362306a36Sopenharmony_ci	btrfs_put_transaction(cur_trans);
199462306a36Sopenharmony_ci}
199562306a36Sopenharmony_ci
199662306a36Sopenharmony_cistatic void cleanup_transaction(struct btrfs_trans_handle *trans, int err)
199762306a36Sopenharmony_ci{
199862306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
199962306a36Sopenharmony_ci	struct btrfs_transaction *cur_trans = trans->transaction;
200062306a36Sopenharmony_ci
200162306a36Sopenharmony_ci	WARN_ON(refcount_read(&trans->use_count) > 1);
200262306a36Sopenharmony_ci
200362306a36Sopenharmony_ci	btrfs_abort_transaction(trans, err);
200462306a36Sopenharmony_ci
200562306a36Sopenharmony_ci	spin_lock(&fs_info->trans_lock);
200662306a36Sopenharmony_ci
200762306a36Sopenharmony_ci	/*
200862306a36Sopenharmony_ci	 * If the transaction is removed from the list, it means this
200962306a36Sopenharmony_ci	 * transaction has been committed successfully, so it is impossible
201062306a36Sopenharmony_ci	 * to call the cleanup function.
201162306a36Sopenharmony_ci	 */
201262306a36Sopenharmony_ci	BUG_ON(list_empty(&cur_trans->list));
201362306a36Sopenharmony_ci
201462306a36Sopenharmony_ci	if (cur_trans == fs_info->running_transaction) {
201562306a36Sopenharmony_ci		cur_trans->state = TRANS_STATE_COMMIT_DOING;
201662306a36Sopenharmony_ci		spin_unlock(&fs_info->trans_lock);
201762306a36Sopenharmony_ci
201862306a36Sopenharmony_ci		/*
201962306a36Sopenharmony_ci		 * The thread has already released the lockdep map as reader
202062306a36Sopenharmony_ci		 * already in btrfs_commit_transaction().
202162306a36Sopenharmony_ci		 */
202262306a36Sopenharmony_ci		btrfs_might_wait_for_event(fs_info, btrfs_trans_num_writers);
202362306a36Sopenharmony_ci		wait_event(cur_trans->writer_wait,
202462306a36Sopenharmony_ci			   atomic_read(&cur_trans->num_writers) == 1);
202562306a36Sopenharmony_ci
202662306a36Sopenharmony_ci		spin_lock(&fs_info->trans_lock);
202762306a36Sopenharmony_ci	}
202862306a36Sopenharmony_ci
202962306a36Sopenharmony_ci	/*
203062306a36Sopenharmony_ci	 * Now that we know no one else is still using the transaction we can
203162306a36Sopenharmony_ci	 * remove the transaction from the list of transactions. This avoids
203262306a36Sopenharmony_ci	 * the transaction kthread from cleaning up the transaction while some
203362306a36Sopenharmony_ci	 * other task is still using it, which could result in a use-after-free
203462306a36Sopenharmony_ci	 * on things like log trees, as it forces the transaction kthread to
203562306a36Sopenharmony_ci	 * wait for this transaction to be cleaned up by us.
203662306a36Sopenharmony_ci	 */
203762306a36Sopenharmony_ci	list_del_init(&cur_trans->list);
203862306a36Sopenharmony_ci
203962306a36Sopenharmony_ci	spin_unlock(&fs_info->trans_lock);
204062306a36Sopenharmony_ci
204162306a36Sopenharmony_ci	btrfs_cleanup_one_transaction(trans->transaction, fs_info);
204262306a36Sopenharmony_ci
204362306a36Sopenharmony_ci	spin_lock(&fs_info->trans_lock);
204462306a36Sopenharmony_ci	if (cur_trans == fs_info->running_transaction)
204562306a36Sopenharmony_ci		fs_info->running_transaction = NULL;
204662306a36Sopenharmony_ci	spin_unlock(&fs_info->trans_lock);
204762306a36Sopenharmony_ci
204862306a36Sopenharmony_ci	if (trans->type & __TRANS_FREEZABLE)
204962306a36Sopenharmony_ci		sb_end_intwrite(fs_info->sb);
205062306a36Sopenharmony_ci	btrfs_put_transaction(cur_trans);
205162306a36Sopenharmony_ci	btrfs_put_transaction(cur_trans);
205262306a36Sopenharmony_ci
205362306a36Sopenharmony_ci	trace_btrfs_transaction_commit(fs_info);
205462306a36Sopenharmony_ci
205562306a36Sopenharmony_ci	if (current->journal_info == trans)
205662306a36Sopenharmony_ci		current->journal_info = NULL;
205762306a36Sopenharmony_ci
205862306a36Sopenharmony_ci	/*
205962306a36Sopenharmony_ci	 * If relocation is running, we can't cancel scrub because that will
206062306a36Sopenharmony_ci	 * result in a deadlock. Before relocating a block group, relocation
206162306a36Sopenharmony_ci	 * pauses scrub, then starts and commits a transaction before unpausing
206262306a36Sopenharmony_ci	 * scrub. If the transaction commit is being done by the relocation
206362306a36Sopenharmony_ci	 * task or triggered by another task and the relocation task is waiting
206462306a36Sopenharmony_ci	 * for the commit, and we end up here due to an error in the commit
206562306a36Sopenharmony_ci	 * path, then calling btrfs_scrub_cancel() will deadlock, as we are
206662306a36Sopenharmony_ci	 * asking for scrub to stop while having it asked to be paused higher
206762306a36Sopenharmony_ci	 * above in relocation code.
206862306a36Sopenharmony_ci	 */
206962306a36Sopenharmony_ci	if (!test_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags))
207062306a36Sopenharmony_ci		btrfs_scrub_cancel(fs_info);
207162306a36Sopenharmony_ci
207262306a36Sopenharmony_ci	kmem_cache_free(btrfs_trans_handle_cachep, trans);
207362306a36Sopenharmony_ci}
207462306a36Sopenharmony_ci
207562306a36Sopenharmony_ci/*
207662306a36Sopenharmony_ci * Release reserved delayed ref space of all pending block groups of the
207762306a36Sopenharmony_ci * transaction and remove them from the list
207862306a36Sopenharmony_ci */
207962306a36Sopenharmony_cistatic void btrfs_cleanup_pending_block_groups(struct btrfs_trans_handle *trans)
208062306a36Sopenharmony_ci{
208162306a36Sopenharmony_ci       struct btrfs_fs_info *fs_info = trans->fs_info;
208262306a36Sopenharmony_ci       struct btrfs_block_group *block_group, *tmp;
208362306a36Sopenharmony_ci
208462306a36Sopenharmony_ci       list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {
208562306a36Sopenharmony_ci               btrfs_delayed_refs_rsv_release(fs_info, 1);
208662306a36Sopenharmony_ci               list_del_init(&block_group->bg_list);
208762306a36Sopenharmony_ci       }
208862306a36Sopenharmony_ci}
208962306a36Sopenharmony_ci
209062306a36Sopenharmony_cistatic inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info)
209162306a36Sopenharmony_ci{
209262306a36Sopenharmony_ci	/*
209362306a36Sopenharmony_ci	 * We use try_to_writeback_inodes_sb() here because if we used
209462306a36Sopenharmony_ci	 * btrfs_start_delalloc_roots we would deadlock with fs freeze.
209562306a36Sopenharmony_ci	 * Currently are holding the fs freeze lock, if we do an async flush
209662306a36Sopenharmony_ci	 * we'll do btrfs_join_transaction() and deadlock because we need to
209762306a36Sopenharmony_ci	 * wait for the fs freeze lock.  Using the direct flushing we benefit
209862306a36Sopenharmony_ci	 * from already being in a transaction and our join_transaction doesn't
209962306a36Sopenharmony_ci	 * have to re-take the fs freeze lock.
210062306a36Sopenharmony_ci	 *
210162306a36Sopenharmony_ci	 * Note that try_to_writeback_inodes_sb() will only trigger writeback
210262306a36Sopenharmony_ci	 * if it can read lock sb->s_umount. It will always be able to lock it,
210362306a36Sopenharmony_ci	 * except when the filesystem is being unmounted or being frozen, but in
210462306a36Sopenharmony_ci	 * those cases sync_filesystem() is called, which results in calling
210562306a36Sopenharmony_ci	 * writeback_inodes_sb() while holding a write lock on sb->s_umount.
210662306a36Sopenharmony_ci	 * Note that we don't call writeback_inodes_sb() directly, because it
210762306a36Sopenharmony_ci	 * will emit a warning if sb->s_umount is not locked.
210862306a36Sopenharmony_ci	 */
210962306a36Sopenharmony_ci	if (btrfs_test_opt(fs_info, FLUSHONCOMMIT))
211062306a36Sopenharmony_ci		try_to_writeback_inodes_sb(fs_info->sb, WB_REASON_SYNC);
211162306a36Sopenharmony_ci	return 0;
211262306a36Sopenharmony_ci}
211362306a36Sopenharmony_ci
211462306a36Sopenharmony_cistatic inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
211562306a36Sopenharmony_ci{
211662306a36Sopenharmony_ci	if (btrfs_test_opt(fs_info, FLUSHONCOMMIT))
211762306a36Sopenharmony_ci		btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
211862306a36Sopenharmony_ci}
211962306a36Sopenharmony_ci
212062306a36Sopenharmony_ci/*
212162306a36Sopenharmony_ci * Add a pending snapshot associated with the given transaction handle to the
212262306a36Sopenharmony_ci * respective handle. This must be called after the transaction commit started
212362306a36Sopenharmony_ci * and while holding fs_info->trans_lock.
212462306a36Sopenharmony_ci * This serves to guarantee a caller of btrfs_commit_transaction() that it can
212562306a36Sopenharmony_ci * safely free the pending snapshot pointer in case btrfs_commit_transaction()
212662306a36Sopenharmony_ci * returns an error.
212762306a36Sopenharmony_ci */
212862306a36Sopenharmony_cistatic void add_pending_snapshot(struct btrfs_trans_handle *trans)
212962306a36Sopenharmony_ci{
213062306a36Sopenharmony_ci	struct btrfs_transaction *cur_trans = trans->transaction;
213162306a36Sopenharmony_ci
213262306a36Sopenharmony_ci	if (!trans->pending_snapshot)
213362306a36Sopenharmony_ci		return;
213462306a36Sopenharmony_ci
213562306a36Sopenharmony_ci	lockdep_assert_held(&trans->fs_info->trans_lock);
213662306a36Sopenharmony_ci	ASSERT(cur_trans->state >= TRANS_STATE_COMMIT_PREP);
213762306a36Sopenharmony_ci
213862306a36Sopenharmony_ci	list_add(&trans->pending_snapshot->list, &cur_trans->pending_snapshots);
213962306a36Sopenharmony_ci}
214062306a36Sopenharmony_ci
214162306a36Sopenharmony_cistatic void update_commit_stats(struct btrfs_fs_info *fs_info, ktime_t interval)
214262306a36Sopenharmony_ci{
214362306a36Sopenharmony_ci	fs_info->commit_stats.commit_count++;
214462306a36Sopenharmony_ci	fs_info->commit_stats.last_commit_dur = interval;
214562306a36Sopenharmony_ci	fs_info->commit_stats.max_commit_dur =
214662306a36Sopenharmony_ci			max_t(u64, fs_info->commit_stats.max_commit_dur, interval);
214762306a36Sopenharmony_ci	fs_info->commit_stats.total_commit_dur += interval;
214862306a36Sopenharmony_ci}
214962306a36Sopenharmony_ci
215062306a36Sopenharmony_ciint btrfs_commit_transaction(struct btrfs_trans_handle *trans)
215162306a36Sopenharmony_ci{
215262306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
215362306a36Sopenharmony_ci	struct btrfs_transaction *cur_trans = trans->transaction;
215462306a36Sopenharmony_ci	struct btrfs_transaction *prev_trans = NULL;
215562306a36Sopenharmony_ci	int ret;
215662306a36Sopenharmony_ci	ktime_t start_time;
215762306a36Sopenharmony_ci	ktime_t interval;
215862306a36Sopenharmony_ci
215962306a36Sopenharmony_ci	ASSERT(refcount_read(&trans->use_count) == 1);
216062306a36Sopenharmony_ci	btrfs_trans_state_lockdep_acquire(fs_info, BTRFS_LOCKDEP_TRANS_COMMIT_PREP);
216162306a36Sopenharmony_ci
216262306a36Sopenharmony_ci	clear_bit(BTRFS_FS_NEED_TRANS_COMMIT, &fs_info->flags);
216362306a36Sopenharmony_ci
216462306a36Sopenharmony_ci	/* Stop the commit early if ->aborted is set */
216562306a36Sopenharmony_ci	if (TRANS_ABORTED(cur_trans)) {
216662306a36Sopenharmony_ci		ret = cur_trans->aborted;
216762306a36Sopenharmony_ci		goto lockdep_trans_commit_start_release;
216862306a36Sopenharmony_ci	}
216962306a36Sopenharmony_ci
217062306a36Sopenharmony_ci	btrfs_trans_release_metadata(trans);
217162306a36Sopenharmony_ci	trans->block_rsv = NULL;
217262306a36Sopenharmony_ci
217362306a36Sopenharmony_ci	/*
217462306a36Sopenharmony_ci	 * We only want one transaction commit doing the flushing so we do not
217562306a36Sopenharmony_ci	 * waste a bunch of time on lock contention on the extent root node.
217662306a36Sopenharmony_ci	 */
217762306a36Sopenharmony_ci	if (!test_and_set_bit(BTRFS_DELAYED_REFS_FLUSHING,
217862306a36Sopenharmony_ci			      &cur_trans->delayed_refs.flags)) {
217962306a36Sopenharmony_ci		/*
218062306a36Sopenharmony_ci		 * Make a pass through all the delayed refs we have so far.
218162306a36Sopenharmony_ci		 * Any running threads may add more while we are here.
218262306a36Sopenharmony_ci		 */
218362306a36Sopenharmony_ci		ret = btrfs_run_delayed_refs(trans, 0);
218462306a36Sopenharmony_ci		if (ret)
218562306a36Sopenharmony_ci			goto lockdep_trans_commit_start_release;
218662306a36Sopenharmony_ci	}
218762306a36Sopenharmony_ci
218862306a36Sopenharmony_ci	btrfs_create_pending_block_groups(trans);
218962306a36Sopenharmony_ci
219062306a36Sopenharmony_ci	if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) {
219162306a36Sopenharmony_ci		int run_it = 0;
219262306a36Sopenharmony_ci
219362306a36Sopenharmony_ci		/* this mutex is also taken before trying to set
219462306a36Sopenharmony_ci		 * block groups readonly.  We need to make sure
219562306a36Sopenharmony_ci		 * that nobody has set a block group readonly
219662306a36Sopenharmony_ci		 * after a extents from that block group have been
219762306a36Sopenharmony_ci		 * allocated for cache files.  btrfs_set_block_group_ro
219862306a36Sopenharmony_ci		 * will wait for the transaction to commit if it
219962306a36Sopenharmony_ci		 * finds BTRFS_TRANS_DIRTY_BG_RUN set.
220062306a36Sopenharmony_ci		 *
220162306a36Sopenharmony_ci		 * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure
220262306a36Sopenharmony_ci		 * only one process starts all the block group IO.  It wouldn't
220362306a36Sopenharmony_ci		 * hurt to have more than one go through, but there's no
220462306a36Sopenharmony_ci		 * real advantage to it either.
220562306a36Sopenharmony_ci		 */
220662306a36Sopenharmony_ci		mutex_lock(&fs_info->ro_block_group_mutex);
220762306a36Sopenharmony_ci		if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN,
220862306a36Sopenharmony_ci				      &cur_trans->flags))
220962306a36Sopenharmony_ci			run_it = 1;
221062306a36Sopenharmony_ci		mutex_unlock(&fs_info->ro_block_group_mutex);
221162306a36Sopenharmony_ci
221262306a36Sopenharmony_ci		if (run_it) {
221362306a36Sopenharmony_ci			ret = btrfs_start_dirty_block_groups(trans);
221462306a36Sopenharmony_ci			if (ret)
221562306a36Sopenharmony_ci				goto lockdep_trans_commit_start_release;
221662306a36Sopenharmony_ci		}
221762306a36Sopenharmony_ci	}
221862306a36Sopenharmony_ci
221962306a36Sopenharmony_ci	spin_lock(&fs_info->trans_lock);
222062306a36Sopenharmony_ci	if (cur_trans->state >= TRANS_STATE_COMMIT_PREP) {
222162306a36Sopenharmony_ci		enum btrfs_trans_state want_state = TRANS_STATE_COMPLETED;
222262306a36Sopenharmony_ci
222362306a36Sopenharmony_ci		add_pending_snapshot(trans);
222462306a36Sopenharmony_ci
222562306a36Sopenharmony_ci		spin_unlock(&fs_info->trans_lock);
222662306a36Sopenharmony_ci		refcount_inc(&cur_trans->use_count);
222762306a36Sopenharmony_ci
222862306a36Sopenharmony_ci		if (trans->in_fsync)
222962306a36Sopenharmony_ci			want_state = TRANS_STATE_SUPER_COMMITTED;
223062306a36Sopenharmony_ci
223162306a36Sopenharmony_ci		btrfs_trans_state_lockdep_release(fs_info,
223262306a36Sopenharmony_ci						  BTRFS_LOCKDEP_TRANS_COMMIT_PREP);
223362306a36Sopenharmony_ci		ret = btrfs_end_transaction(trans);
223462306a36Sopenharmony_ci		wait_for_commit(cur_trans, want_state);
223562306a36Sopenharmony_ci
223662306a36Sopenharmony_ci		if (TRANS_ABORTED(cur_trans))
223762306a36Sopenharmony_ci			ret = cur_trans->aborted;
223862306a36Sopenharmony_ci
223962306a36Sopenharmony_ci		btrfs_put_transaction(cur_trans);
224062306a36Sopenharmony_ci
224162306a36Sopenharmony_ci		return ret;
224262306a36Sopenharmony_ci	}
224362306a36Sopenharmony_ci
224462306a36Sopenharmony_ci	cur_trans->state = TRANS_STATE_COMMIT_PREP;
224562306a36Sopenharmony_ci	wake_up(&fs_info->transaction_blocked_wait);
224662306a36Sopenharmony_ci	btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_COMMIT_PREP);
224762306a36Sopenharmony_ci
224862306a36Sopenharmony_ci	if (cur_trans->list.prev != &fs_info->trans_list) {
224962306a36Sopenharmony_ci		enum btrfs_trans_state want_state = TRANS_STATE_COMPLETED;
225062306a36Sopenharmony_ci
225162306a36Sopenharmony_ci		if (trans->in_fsync)
225262306a36Sopenharmony_ci			want_state = TRANS_STATE_SUPER_COMMITTED;
225362306a36Sopenharmony_ci
225462306a36Sopenharmony_ci		prev_trans = list_entry(cur_trans->list.prev,
225562306a36Sopenharmony_ci					struct btrfs_transaction, list);
225662306a36Sopenharmony_ci		if (prev_trans->state < want_state) {
225762306a36Sopenharmony_ci			refcount_inc(&prev_trans->use_count);
225862306a36Sopenharmony_ci			spin_unlock(&fs_info->trans_lock);
225962306a36Sopenharmony_ci
226062306a36Sopenharmony_ci			wait_for_commit(prev_trans, want_state);
226162306a36Sopenharmony_ci
226262306a36Sopenharmony_ci			ret = READ_ONCE(prev_trans->aborted);
226362306a36Sopenharmony_ci
226462306a36Sopenharmony_ci			btrfs_put_transaction(prev_trans);
226562306a36Sopenharmony_ci			if (ret)
226662306a36Sopenharmony_ci				goto lockdep_release;
226762306a36Sopenharmony_ci			spin_lock(&fs_info->trans_lock);
226862306a36Sopenharmony_ci		}
226962306a36Sopenharmony_ci	} else {
227062306a36Sopenharmony_ci		/*
227162306a36Sopenharmony_ci		 * The previous transaction was aborted and was already removed
227262306a36Sopenharmony_ci		 * from the list of transactions at fs_info->trans_list. So we
227362306a36Sopenharmony_ci		 * abort to prevent writing a new superblock that reflects a
227462306a36Sopenharmony_ci		 * corrupt state (pointing to trees with unwritten nodes/leafs).
227562306a36Sopenharmony_ci		 */
227662306a36Sopenharmony_ci		if (BTRFS_FS_ERROR(fs_info)) {
227762306a36Sopenharmony_ci			spin_unlock(&fs_info->trans_lock);
227862306a36Sopenharmony_ci			ret = -EROFS;
227962306a36Sopenharmony_ci			goto lockdep_release;
228062306a36Sopenharmony_ci		}
228162306a36Sopenharmony_ci	}
228262306a36Sopenharmony_ci
228362306a36Sopenharmony_ci	cur_trans->state = TRANS_STATE_COMMIT_START;
228462306a36Sopenharmony_ci	wake_up(&fs_info->transaction_blocked_wait);
228562306a36Sopenharmony_ci	spin_unlock(&fs_info->trans_lock);
228662306a36Sopenharmony_ci
228762306a36Sopenharmony_ci	/*
228862306a36Sopenharmony_ci	 * Get the time spent on the work done by the commit thread and not
228962306a36Sopenharmony_ci	 * the time spent waiting on a previous commit
229062306a36Sopenharmony_ci	 */
229162306a36Sopenharmony_ci	start_time = ktime_get_ns();
229262306a36Sopenharmony_ci
229362306a36Sopenharmony_ci	extwriter_counter_dec(cur_trans, trans->type);
229462306a36Sopenharmony_ci
229562306a36Sopenharmony_ci	ret = btrfs_start_delalloc_flush(fs_info);
229662306a36Sopenharmony_ci	if (ret)
229762306a36Sopenharmony_ci		goto lockdep_release;
229862306a36Sopenharmony_ci
229962306a36Sopenharmony_ci	ret = btrfs_run_delayed_items(trans);
230062306a36Sopenharmony_ci	if (ret)
230162306a36Sopenharmony_ci		goto lockdep_release;
230262306a36Sopenharmony_ci
230362306a36Sopenharmony_ci	/*
230462306a36Sopenharmony_ci	 * The thread has started/joined the transaction thus it holds the
230562306a36Sopenharmony_ci	 * lockdep map as a reader. It has to release it before acquiring the
230662306a36Sopenharmony_ci	 * lockdep map as a writer.
230762306a36Sopenharmony_ci	 */
230862306a36Sopenharmony_ci	btrfs_lockdep_release(fs_info, btrfs_trans_num_extwriters);
230962306a36Sopenharmony_ci	btrfs_might_wait_for_event(fs_info, btrfs_trans_num_extwriters);
231062306a36Sopenharmony_ci	wait_event(cur_trans->writer_wait,
231162306a36Sopenharmony_ci		   extwriter_counter_read(cur_trans) == 0);
231262306a36Sopenharmony_ci
231362306a36Sopenharmony_ci	/* some pending stuffs might be added after the previous flush. */
231462306a36Sopenharmony_ci	ret = btrfs_run_delayed_items(trans);
231562306a36Sopenharmony_ci	if (ret) {
231662306a36Sopenharmony_ci		btrfs_lockdep_release(fs_info, btrfs_trans_num_writers);
231762306a36Sopenharmony_ci		goto cleanup_transaction;
231862306a36Sopenharmony_ci	}
231962306a36Sopenharmony_ci
232062306a36Sopenharmony_ci	btrfs_wait_delalloc_flush(fs_info);
232162306a36Sopenharmony_ci
232262306a36Sopenharmony_ci	/*
232362306a36Sopenharmony_ci	 * Wait for all ordered extents started by a fast fsync that joined this
232462306a36Sopenharmony_ci	 * transaction. Otherwise if this transaction commits before the ordered
232562306a36Sopenharmony_ci	 * extents complete we lose logged data after a power failure.
232662306a36Sopenharmony_ci	 */
232762306a36Sopenharmony_ci	btrfs_might_wait_for_event(fs_info, btrfs_trans_pending_ordered);
232862306a36Sopenharmony_ci	wait_event(cur_trans->pending_wait,
232962306a36Sopenharmony_ci		   atomic_read(&cur_trans->pending_ordered) == 0);
233062306a36Sopenharmony_ci
233162306a36Sopenharmony_ci	btrfs_scrub_pause(fs_info);
233262306a36Sopenharmony_ci	/*
233362306a36Sopenharmony_ci	 * Ok now we need to make sure to block out any other joins while we
233462306a36Sopenharmony_ci	 * commit the transaction.  We could have started a join before setting
233562306a36Sopenharmony_ci	 * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
233662306a36Sopenharmony_ci	 */
233762306a36Sopenharmony_ci	spin_lock(&fs_info->trans_lock);
233862306a36Sopenharmony_ci	add_pending_snapshot(trans);
233962306a36Sopenharmony_ci	cur_trans->state = TRANS_STATE_COMMIT_DOING;
234062306a36Sopenharmony_ci	spin_unlock(&fs_info->trans_lock);
234162306a36Sopenharmony_ci
234262306a36Sopenharmony_ci	/*
234362306a36Sopenharmony_ci	 * The thread has started/joined the transaction thus it holds the
234462306a36Sopenharmony_ci	 * lockdep map as a reader. It has to release it before acquiring the
234562306a36Sopenharmony_ci	 * lockdep map as a writer.
234662306a36Sopenharmony_ci	 */
234762306a36Sopenharmony_ci	btrfs_lockdep_release(fs_info, btrfs_trans_num_writers);
234862306a36Sopenharmony_ci	btrfs_might_wait_for_event(fs_info, btrfs_trans_num_writers);
234962306a36Sopenharmony_ci	wait_event(cur_trans->writer_wait,
235062306a36Sopenharmony_ci		   atomic_read(&cur_trans->num_writers) == 1);
235162306a36Sopenharmony_ci
235262306a36Sopenharmony_ci	/*
235362306a36Sopenharmony_ci	 * Make lockdep happy by acquiring the state locks after
235462306a36Sopenharmony_ci	 * btrfs_trans_num_writers is released. If we acquired the state locks
235562306a36Sopenharmony_ci	 * before releasing the btrfs_trans_num_writers lock then lockdep would
235662306a36Sopenharmony_ci	 * complain because we did not follow the reverse order unlocking rule.
235762306a36Sopenharmony_ci	 */
235862306a36Sopenharmony_ci	btrfs_trans_state_lockdep_acquire(fs_info, BTRFS_LOCKDEP_TRANS_COMPLETED);
235962306a36Sopenharmony_ci	btrfs_trans_state_lockdep_acquire(fs_info, BTRFS_LOCKDEP_TRANS_SUPER_COMMITTED);
236062306a36Sopenharmony_ci	btrfs_trans_state_lockdep_acquire(fs_info, BTRFS_LOCKDEP_TRANS_UNBLOCKED);
236162306a36Sopenharmony_ci
236262306a36Sopenharmony_ci	/*
236362306a36Sopenharmony_ci	 * We've started the commit, clear the flag in case we were triggered to
236462306a36Sopenharmony_ci	 * do an async commit but somebody else started before the transaction
236562306a36Sopenharmony_ci	 * kthread could do the work.
236662306a36Sopenharmony_ci	 */
236762306a36Sopenharmony_ci	clear_bit(BTRFS_FS_COMMIT_TRANS, &fs_info->flags);
236862306a36Sopenharmony_ci
236962306a36Sopenharmony_ci	if (TRANS_ABORTED(cur_trans)) {
237062306a36Sopenharmony_ci		ret = cur_trans->aborted;
237162306a36Sopenharmony_ci		btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_UNBLOCKED);
237262306a36Sopenharmony_ci		goto scrub_continue;
237362306a36Sopenharmony_ci	}
237462306a36Sopenharmony_ci	/*
237562306a36Sopenharmony_ci	 * the reloc mutex makes sure that we stop
237662306a36Sopenharmony_ci	 * the balancing code from coming in and moving
237762306a36Sopenharmony_ci	 * extents around in the middle of the commit
237862306a36Sopenharmony_ci	 */
237962306a36Sopenharmony_ci	mutex_lock(&fs_info->reloc_mutex);
238062306a36Sopenharmony_ci
238162306a36Sopenharmony_ci	/*
238262306a36Sopenharmony_ci	 * We needn't worry about the delayed items because we will
238362306a36Sopenharmony_ci	 * deal with them in create_pending_snapshot(), which is the
238462306a36Sopenharmony_ci	 * core function of the snapshot creation.
238562306a36Sopenharmony_ci	 */
238662306a36Sopenharmony_ci	ret = create_pending_snapshots(trans);
238762306a36Sopenharmony_ci	if (ret)
238862306a36Sopenharmony_ci		goto unlock_reloc;
238962306a36Sopenharmony_ci
239062306a36Sopenharmony_ci	/*
239162306a36Sopenharmony_ci	 * We insert the dir indexes of the snapshots and update the inode
239262306a36Sopenharmony_ci	 * of the snapshots' parents after the snapshot creation, so there
239362306a36Sopenharmony_ci	 * are some delayed items which are not dealt with. Now deal with
239462306a36Sopenharmony_ci	 * them.
239562306a36Sopenharmony_ci	 *
239662306a36Sopenharmony_ci	 * We needn't worry that this operation will corrupt the snapshots,
239762306a36Sopenharmony_ci	 * because all the tree which are snapshoted will be forced to COW
239862306a36Sopenharmony_ci	 * the nodes and leaves.
239962306a36Sopenharmony_ci	 */
240062306a36Sopenharmony_ci	ret = btrfs_run_delayed_items(trans);
240162306a36Sopenharmony_ci	if (ret)
240262306a36Sopenharmony_ci		goto unlock_reloc;
240362306a36Sopenharmony_ci
240462306a36Sopenharmony_ci	ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
240562306a36Sopenharmony_ci	if (ret)
240662306a36Sopenharmony_ci		goto unlock_reloc;
240762306a36Sopenharmony_ci
240862306a36Sopenharmony_ci	/*
240962306a36Sopenharmony_ci	 * make sure none of the code above managed to slip in a
241062306a36Sopenharmony_ci	 * delayed item
241162306a36Sopenharmony_ci	 */
241262306a36Sopenharmony_ci	btrfs_assert_delayed_root_empty(fs_info);
241362306a36Sopenharmony_ci
241462306a36Sopenharmony_ci	WARN_ON(cur_trans != trans->transaction);
241562306a36Sopenharmony_ci
241662306a36Sopenharmony_ci	ret = commit_fs_roots(trans);
241762306a36Sopenharmony_ci	if (ret)
241862306a36Sopenharmony_ci		goto unlock_reloc;
241962306a36Sopenharmony_ci
242062306a36Sopenharmony_ci	/* commit_fs_roots gets rid of all the tree log roots, it is now
242162306a36Sopenharmony_ci	 * safe to free the root of tree log roots
242262306a36Sopenharmony_ci	 */
242362306a36Sopenharmony_ci	btrfs_free_log_root_tree(trans, fs_info);
242462306a36Sopenharmony_ci
242562306a36Sopenharmony_ci	/*
242662306a36Sopenharmony_ci	 * Since fs roots are all committed, we can get a quite accurate
242762306a36Sopenharmony_ci	 * new_roots. So let's do quota accounting.
242862306a36Sopenharmony_ci	 */
242962306a36Sopenharmony_ci	ret = btrfs_qgroup_account_extents(trans);
243062306a36Sopenharmony_ci	if (ret < 0)
243162306a36Sopenharmony_ci		goto unlock_reloc;
243262306a36Sopenharmony_ci
243362306a36Sopenharmony_ci	ret = commit_cowonly_roots(trans);
243462306a36Sopenharmony_ci	if (ret)
243562306a36Sopenharmony_ci		goto unlock_reloc;
243662306a36Sopenharmony_ci
243762306a36Sopenharmony_ci	/*
243862306a36Sopenharmony_ci	 * The tasks which save the space cache and inode cache may also
243962306a36Sopenharmony_ci	 * update ->aborted, check it.
244062306a36Sopenharmony_ci	 */
244162306a36Sopenharmony_ci	if (TRANS_ABORTED(cur_trans)) {
244262306a36Sopenharmony_ci		ret = cur_trans->aborted;
244362306a36Sopenharmony_ci		goto unlock_reloc;
244462306a36Sopenharmony_ci	}
244562306a36Sopenharmony_ci
244662306a36Sopenharmony_ci	cur_trans = fs_info->running_transaction;
244762306a36Sopenharmony_ci
244862306a36Sopenharmony_ci	btrfs_set_root_node(&fs_info->tree_root->root_item,
244962306a36Sopenharmony_ci			    fs_info->tree_root->node);
245062306a36Sopenharmony_ci	list_add_tail(&fs_info->tree_root->dirty_list,
245162306a36Sopenharmony_ci		      &cur_trans->switch_commits);
245262306a36Sopenharmony_ci
245362306a36Sopenharmony_ci	btrfs_set_root_node(&fs_info->chunk_root->root_item,
245462306a36Sopenharmony_ci			    fs_info->chunk_root->node);
245562306a36Sopenharmony_ci	list_add_tail(&fs_info->chunk_root->dirty_list,
245662306a36Sopenharmony_ci		      &cur_trans->switch_commits);
245762306a36Sopenharmony_ci
245862306a36Sopenharmony_ci	if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
245962306a36Sopenharmony_ci		btrfs_set_root_node(&fs_info->block_group_root->root_item,
246062306a36Sopenharmony_ci				    fs_info->block_group_root->node);
246162306a36Sopenharmony_ci		list_add_tail(&fs_info->block_group_root->dirty_list,
246262306a36Sopenharmony_ci			      &cur_trans->switch_commits);
246362306a36Sopenharmony_ci	}
246462306a36Sopenharmony_ci
246562306a36Sopenharmony_ci	switch_commit_roots(trans);
246662306a36Sopenharmony_ci
246762306a36Sopenharmony_ci	ASSERT(list_empty(&cur_trans->dirty_bgs));
246862306a36Sopenharmony_ci	ASSERT(list_empty(&cur_trans->io_bgs));
246962306a36Sopenharmony_ci	update_super_roots(fs_info);
247062306a36Sopenharmony_ci
247162306a36Sopenharmony_ci	btrfs_set_super_log_root(fs_info->super_copy, 0);
247262306a36Sopenharmony_ci	btrfs_set_super_log_root_level(fs_info->super_copy, 0);
247362306a36Sopenharmony_ci	memcpy(fs_info->super_for_commit, fs_info->super_copy,
247462306a36Sopenharmony_ci	       sizeof(*fs_info->super_copy));
247562306a36Sopenharmony_ci
247662306a36Sopenharmony_ci	btrfs_commit_device_sizes(cur_trans);
247762306a36Sopenharmony_ci
247862306a36Sopenharmony_ci	clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags);
247962306a36Sopenharmony_ci	clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags);
248062306a36Sopenharmony_ci
248162306a36Sopenharmony_ci	btrfs_trans_release_chunk_metadata(trans);
248262306a36Sopenharmony_ci
248362306a36Sopenharmony_ci	/*
248462306a36Sopenharmony_ci	 * Before changing the transaction state to TRANS_STATE_UNBLOCKED and
248562306a36Sopenharmony_ci	 * setting fs_info->running_transaction to NULL, lock tree_log_mutex to
248662306a36Sopenharmony_ci	 * make sure that before we commit our superblock, no other task can
248762306a36Sopenharmony_ci	 * start a new transaction and commit a log tree before we commit our
248862306a36Sopenharmony_ci	 * superblock. Anyone trying to commit a log tree locks this mutex before
248962306a36Sopenharmony_ci	 * writing its superblock.
249062306a36Sopenharmony_ci	 */
249162306a36Sopenharmony_ci	mutex_lock(&fs_info->tree_log_mutex);
249262306a36Sopenharmony_ci
249362306a36Sopenharmony_ci	spin_lock(&fs_info->trans_lock);
249462306a36Sopenharmony_ci	cur_trans->state = TRANS_STATE_UNBLOCKED;
249562306a36Sopenharmony_ci	fs_info->running_transaction = NULL;
249662306a36Sopenharmony_ci	spin_unlock(&fs_info->trans_lock);
249762306a36Sopenharmony_ci	mutex_unlock(&fs_info->reloc_mutex);
249862306a36Sopenharmony_ci
249962306a36Sopenharmony_ci	wake_up(&fs_info->transaction_wait);
250062306a36Sopenharmony_ci	btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_UNBLOCKED);
250162306a36Sopenharmony_ci
250262306a36Sopenharmony_ci	/* If we have features changed, wake up the cleaner to update sysfs. */
250362306a36Sopenharmony_ci	if (test_bit(BTRFS_FS_FEATURE_CHANGED, &fs_info->flags) &&
250462306a36Sopenharmony_ci	    fs_info->cleaner_kthread)
250562306a36Sopenharmony_ci		wake_up_process(fs_info->cleaner_kthread);
250662306a36Sopenharmony_ci
250762306a36Sopenharmony_ci	ret = btrfs_write_and_wait_transaction(trans);
250862306a36Sopenharmony_ci	if (ret) {
250962306a36Sopenharmony_ci		btrfs_handle_fs_error(fs_info, ret,
251062306a36Sopenharmony_ci				      "Error while writing out transaction");
251162306a36Sopenharmony_ci		mutex_unlock(&fs_info->tree_log_mutex);
251262306a36Sopenharmony_ci		goto scrub_continue;
251362306a36Sopenharmony_ci	}
251462306a36Sopenharmony_ci
251562306a36Sopenharmony_ci	ret = write_all_supers(fs_info, 0);
251662306a36Sopenharmony_ci	/*
251762306a36Sopenharmony_ci	 * the super is written, we can safely allow the tree-loggers
251862306a36Sopenharmony_ci	 * to go about their business
251962306a36Sopenharmony_ci	 */
252062306a36Sopenharmony_ci	mutex_unlock(&fs_info->tree_log_mutex);
252162306a36Sopenharmony_ci	if (ret)
252262306a36Sopenharmony_ci		goto scrub_continue;
252362306a36Sopenharmony_ci
252462306a36Sopenharmony_ci	/*
252562306a36Sopenharmony_ci	 * We needn't acquire the lock here because there is no other task
252662306a36Sopenharmony_ci	 * which can change it.
252762306a36Sopenharmony_ci	 */
252862306a36Sopenharmony_ci	cur_trans->state = TRANS_STATE_SUPER_COMMITTED;
252962306a36Sopenharmony_ci	wake_up(&cur_trans->commit_wait);
253062306a36Sopenharmony_ci	btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_SUPER_COMMITTED);
253162306a36Sopenharmony_ci
253262306a36Sopenharmony_ci	btrfs_finish_extent_commit(trans);
253362306a36Sopenharmony_ci
253462306a36Sopenharmony_ci	if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags))
253562306a36Sopenharmony_ci		btrfs_clear_space_info_full(fs_info);
253662306a36Sopenharmony_ci
253762306a36Sopenharmony_ci	fs_info->last_trans_committed = cur_trans->transid;
253862306a36Sopenharmony_ci	/*
253962306a36Sopenharmony_ci	 * We needn't acquire the lock here because there is no other task
254062306a36Sopenharmony_ci	 * which can change it.
254162306a36Sopenharmony_ci	 */
254262306a36Sopenharmony_ci	cur_trans->state = TRANS_STATE_COMPLETED;
254362306a36Sopenharmony_ci	wake_up(&cur_trans->commit_wait);
254462306a36Sopenharmony_ci	btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_COMPLETED);
254562306a36Sopenharmony_ci
254662306a36Sopenharmony_ci	spin_lock(&fs_info->trans_lock);
254762306a36Sopenharmony_ci	list_del_init(&cur_trans->list);
254862306a36Sopenharmony_ci	spin_unlock(&fs_info->trans_lock);
254962306a36Sopenharmony_ci
255062306a36Sopenharmony_ci	btrfs_put_transaction(cur_trans);
255162306a36Sopenharmony_ci	btrfs_put_transaction(cur_trans);
255262306a36Sopenharmony_ci
255362306a36Sopenharmony_ci	if (trans->type & __TRANS_FREEZABLE)
255462306a36Sopenharmony_ci		sb_end_intwrite(fs_info->sb);
255562306a36Sopenharmony_ci
255662306a36Sopenharmony_ci	trace_btrfs_transaction_commit(fs_info);
255762306a36Sopenharmony_ci
255862306a36Sopenharmony_ci	interval = ktime_get_ns() - start_time;
255962306a36Sopenharmony_ci
256062306a36Sopenharmony_ci	btrfs_scrub_continue(fs_info);
256162306a36Sopenharmony_ci
256262306a36Sopenharmony_ci	if (current->journal_info == trans)
256362306a36Sopenharmony_ci		current->journal_info = NULL;
256462306a36Sopenharmony_ci
256562306a36Sopenharmony_ci	kmem_cache_free(btrfs_trans_handle_cachep, trans);
256662306a36Sopenharmony_ci
256762306a36Sopenharmony_ci	update_commit_stats(fs_info, interval);
256862306a36Sopenharmony_ci
256962306a36Sopenharmony_ci	return ret;
257062306a36Sopenharmony_ci
257162306a36Sopenharmony_ciunlock_reloc:
257262306a36Sopenharmony_ci	mutex_unlock(&fs_info->reloc_mutex);
257362306a36Sopenharmony_ci	btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_UNBLOCKED);
257462306a36Sopenharmony_ciscrub_continue:
257562306a36Sopenharmony_ci	btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_SUPER_COMMITTED);
257662306a36Sopenharmony_ci	btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_COMPLETED);
257762306a36Sopenharmony_ci	btrfs_scrub_continue(fs_info);
257862306a36Sopenharmony_cicleanup_transaction:
257962306a36Sopenharmony_ci	btrfs_trans_release_metadata(trans);
258062306a36Sopenharmony_ci	btrfs_cleanup_pending_block_groups(trans);
258162306a36Sopenharmony_ci	btrfs_trans_release_chunk_metadata(trans);
258262306a36Sopenharmony_ci	trans->block_rsv = NULL;
258362306a36Sopenharmony_ci	btrfs_warn(fs_info, "Skipping commit of aborted transaction.");
258462306a36Sopenharmony_ci	if (current->journal_info == trans)
258562306a36Sopenharmony_ci		current->journal_info = NULL;
258662306a36Sopenharmony_ci	cleanup_transaction(trans, ret);
258762306a36Sopenharmony_ci
258862306a36Sopenharmony_ci	return ret;
258962306a36Sopenharmony_ci
259062306a36Sopenharmony_cilockdep_release:
259162306a36Sopenharmony_ci	btrfs_lockdep_release(fs_info, btrfs_trans_num_extwriters);
259262306a36Sopenharmony_ci	btrfs_lockdep_release(fs_info, btrfs_trans_num_writers);
259362306a36Sopenharmony_ci	goto cleanup_transaction;
259462306a36Sopenharmony_ci
259562306a36Sopenharmony_cilockdep_trans_commit_start_release:
259662306a36Sopenharmony_ci	btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_COMMIT_PREP);
259762306a36Sopenharmony_ci	btrfs_end_transaction(trans);
259862306a36Sopenharmony_ci	return ret;
259962306a36Sopenharmony_ci}
260062306a36Sopenharmony_ci
260162306a36Sopenharmony_ci/*
260262306a36Sopenharmony_ci * return < 0 if error
260362306a36Sopenharmony_ci * 0 if there are no more dead_roots at the time of call
260462306a36Sopenharmony_ci * 1 there are more to be processed, call me again
260562306a36Sopenharmony_ci *
260662306a36Sopenharmony_ci * The return value indicates there are certainly more snapshots to delete, but
260762306a36Sopenharmony_ci * if there comes a new one during processing, it may return 0. We don't mind,
260862306a36Sopenharmony_ci * because btrfs_commit_super will poke cleaner thread and it will process it a
260962306a36Sopenharmony_ci * few seconds later.
261062306a36Sopenharmony_ci */
261162306a36Sopenharmony_ciint btrfs_clean_one_deleted_snapshot(struct btrfs_fs_info *fs_info)
261262306a36Sopenharmony_ci{
261362306a36Sopenharmony_ci	struct btrfs_root *root;
261462306a36Sopenharmony_ci	int ret;
261562306a36Sopenharmony_ci
261662306a36Sopenharmony_ci	spin_lock(&fs_info->trans_lock);
261762306a36Sopenharmony_ci	if (list_empty(&fs_info->dead_roots)) {
261862306a36Sopenharmony_ci		spin_unlock(&fs_info->trans_lock);
261962306a36Sopenharmony_ci		return 0;
262062306a36Sopenharmony_ci	}
262162306a36Sopenharmony_ci	root = list_first_entry(&fs_info->dead_roots,
262262306a36Sopenharmony_ci			struct btrfs_root, root_list);
262362306a36Sopenharmony_ci	list_del_init(&root->root_list);
262462306a36Sopenharmony_ci	spin_unlock(&fs_info->trans_lock);
262562306a36Sopenharmony_ci
262662306a36Sopenharmony_ci	btrfs_debug(fs_info, "cleaner removing %llu", root->root_key.objectid);
262762306a36Sopenharmony_ci
262862306a36Sopenharmony_ci	btrfs_kill_all_delayed_nodes(root);
262962306a36Sopenharmony_ci
263062306a36Sopenharmony_ci	if (btrfs_header_backref_rev(root->node) <
263162306a36Sopenharmony_ci			BTRFS_MIXED_BACKREF_REV)
263262306a36Sopenharmony_ci		ret = btrfs_drop_snapshot(root, 0, 0);
263362306a36Sopenharmony_ci	else
263462306a36Sopenharmony_ci		ret = btrfs_drop_snapshot(root, 1, 0);
263562306a36Sopenharmony_ci
263662306a36Sopenharmony_ci	btrfs_put_root(root);
263762306a36Sopenharmony_ci	return (ret < 0) ? 0 : 1;
263862306a36Sopenharmony_ci}
263962306a36Sopenharmony_ci
264062306a36Sopenharmony_ci/*
264162306a36Sopenharmony_ci * We only mark the transaction aborted and then set the file system read-only.
264262306a36Sopenharmony_ci * This will prevent new transactions from starting or trying to join this
264362306a36Sopenharmony_ci * one.
264462306a36Sopenharmony_ci *
264562306a36Sopenharmony_ci * This means that error recovery at the call site is limited to freeing
264662306a36Sopenharmony_ci * any local memory allocations and passing the error code up without
264762306a36Sopenharmony_ci * further cleanup. The transaction should complete as it normally would
264862306a36Sopenharmony_ci * in the call path but will return -EIO.
264962306a36Sopenharmony_ci *
265062306a36Sopenharmony_ci * We'll complete the cleanup in btrfs_end_transaction and
265162306a36Sopenharmony_ci * btrfs_commit_transaction.
265262306a36Sopenharmony_ci */
265362306a36Sopenharmony_civoid __cold __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
265462306a36Sopenharmony_ci				      const char *function,
265562306a36Sopenharmony_ci				      unsigned int line, int errno, bool first_hit)
265662306a36Sopenharmony_ci{
265762306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
265862306a36Sopenharmony_ci
265962306a36Sopenharmony_ci	WRITE_ONCE(trans->aborted, errno);
266062306a36Sopenharmony_ci	WRITE_ONCE(trans->transaction->aborted, errno);
266162306a36Sopenharmony_ci	if (first_hit && errno == -ENOSPC)
266262306a36Sopenharmony_ci		btrfs_dump_space_info_for_trans_abort(fs_info);
266362306a36Sopenharmony_ci	/* Wake up anybody who may be waiting on this transaction */
266462306a36Sopenharmony_ci	wake_up(&fs_info->transaction_wait);
266562306a36Sopenharmony_ci	wake_up(&fs_info->transaction_blocked_wait);
266662306a36Sopenharmony_ci	__btrfs_handle_fs_error(fs_info, function, line, errno, NULL);
266762306a36Sopenharmony_ci}
266862306a36Sopenharmony_ci
266962306a36Sopenharmony_ciint __init btrfs_transaction_init(void)
267062306a36Sopenharmony_ci{
267162306a36Sopenharmony_ci	btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
267262306a36Sopenharmony_ci			sizeof(struct btrfs_trans_handle), 0,
267362306a36Sopenharmony_ci			SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
267462306a36Sopenharmony_ci	if (!btrfs_trans_handle_cachep)
267562306a36Sopenharmony_ci		return -ENOMEM;
267662306a36Sopenharmony_ci	return 0;
267762306a36Sopenharmony_ci}
267862306a36Sopenharmony_ci
267962306a36Sopenharmony_civoid __cold btrfs_transaction_exit(void)
268062306a36Sopenharmony_ci{
268162306a36Sopenharmony_ci	kmem_cache_destroy(btrfs_trans_handle_cachep);
268262306a36Sopenharmony_ci}
2683